zloop1982 / bwapi

Automatically exported from code.google.com/p/bwapi
0 stars 0 forks source link

Probably crash by a null pointer #483

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
My situation is a fight between a Marine and a Zealot. The Marine is controlled 
by my IA. At some point when the marine dies in a crash in Starcraft. I suspect 
it is something related to that function.

Unit* findNearestEnemy(Unit* unit)
{
    Unit* nearestEnemy;

    log("findNearestEnemy\n");
    if(unit != NULL) {

        std::set<Unit*>& enemiesUnits =  unit->getUnitsInRadius(RADIUS_ENEMIES_TO_ATTACK);

        if(!enemiesUnits.empty()) {

            int minDistance = INT_MAX;
            log("enemiesUnits\n");
            for (std::set<Unit*>::iterator it = enemiesUnits.begin(); it!=enemiesUnits.end(); ++it) {
                Unit* possibleEnemy = (*it);
                log("possibleEnemy\n");
                if(possibleEnemy != NULL) {

                    // Não é uma tropa aliada
                    if(possibleEnemy->getPlayer()->getID() != Broodwar->self()->getID()) {

                        //Procura o mais perto
                        int distance = distanceUnit(unit, possibleEnemy);
                        if(distance < minDistance) {
                            nearestEnemy = possibleEnemy;
                        }
                    }
                }
                else {
                    log("is here?");
                }
            }
        }

        return nearestEnemy;
    }
    log("Take NULL");
    return NULL;
}

Unfortunately I could not use debug to check it. Can someone help me?

Original issue reported on code.google.com by regis.cl...@gmail.com on 26 Feb 2013 at 2:05

GoogleCodeExporter commented 9 years ago
Check 
"if(possibleEnemy != NULL && possibleEnemy->exists() ) {"

and use
"if(possibleEnemy->getPlayer() != Broodwar->self()) {"
since getID is not necessary

and you can also use "possibleEnemy->getDistance(unit)"

Original comment by AHeinerm on 26 Feb 2013 at 7:47

GoogleCodeExporter commented 9 years ago

Original comment by AHeinerm on 1 Mar 2013 at 9:39

GoogleCodeExporter commented 9 years ago

Original comment by AHeinerm on 19 Mar 2013 at 7:27