- added NULL guard for units without a move command to avoid segfault

This commit is contained in:
Mark Vejvoda 2011-02-26 00:41:00 +00:00
parent 5787fb3e98
commit 823fd842d4
2 changed files with 18 additions and 8 deletions

View File

@ -660,16 +660,21 @@ void Ai::unblockUnits() {
iterMap2 != iterMap->second.end(); iterMap2++) { iterMap2 != iterMap->second.end(); iterMap2++) {
int idx = iterMap2->first; int idx = iterMap2->first;
const Unit *adjacentUnit = iterMap2->second; const Unit *adjacentUnit = iterMap2->second;
if(adjacentUnit != NULL && adjacentUnit->getType()->getFirstCtOfClass(ccMove) != NULL) {
const CommandType *ct = adjacentUnit->getType()->getFirstCtOfClass(ccMove);
for(int moveAttempt = 1; moveAttempt <= villageRadius; ++moveAttempt) { for(int moveAttempt = 1; moveAttempt <= villageRadius; ++moveAttempt) {
Vec2i pos= Vec2i( Vec2i pos= Vec2i(
random.randRange(-villageRadius*2, villageRadius*2), random.randRange(-villageRadius*2, villageRadius*2)) + random.randRange(-villageRadius*2, villageRadius*2), random.randRange(-villageRadius*2, villageRadius*2)) +
adjacentUnit->getPos(); adjacentUnit->getPos();
bool canUnitMoveToCell = map->aproxCanMove(adjacentUnit, adjacentUnit->getPos(), pos); bool canUnitMoveToCell = map->aproxCanMove(adjacentUnit, adjacentUnit->getPos(), pos);
if(canUnitMoveToCell == true) { if(canUnitMoveToCell == true) {
const CommandType *ct = adjacentUnit->getType()->getFirstCtOfClass(ccMove);
CommandResult r = aiInterface->giveCommand(adjacentUnit,ct, pos); if(ct != NULL) {
CommandResult r = aiInterface->giveCommand(adjacentUnit,ct, pos);
}
}
} }
} }
} }

View File

@ -146,6 +146,11 @@ CommandResult AiInterface::giveCommand(const Unit *unit, const CommandType *comm
sprintf(szBuf,"In [%s::%s Line: %d] Can not find AI unittype with unit id: %d, AI factionIndex = %d. Game out of synch.",__FILE__,__FUNCTION__,__LINE__,unit->getId(),factionIndex); sprintf(szBuf,"In [%s::%s Line: %d] Can not find AI unittype with unit id: %d, AI factionIndex = %d. Game out of synch.",__FILE__,__FUNCTION__,__LINE__,unit->getId(),factionIndex);
throw runtime_error(szBuf); throw runtime_error(szBuf);
} }
if(commandType == NULL) {
char szBuf[1024]="";
sprintf(szBuf,"In [%s::%s Line: %d] commandType == NULL, unit id: %d, AI factionIndex = %d. Game out of synch.",__FILE__,__FUNCTION__,__LINE__,unit->getId(),factionIndex);
throw runtime_error(szBuf);
}
const CommandType* ct= unit->getType()->findCommandTypeById(commandType->getId()); const CommandType* ct= unit->getType()->findCommandTypeById(commandType->getId());
if(ct == NULL) { if(ct == NULL) {
char szBuf[4096]=""; char szBuf[4096]="";