diff --git a/source/glest_game/ai/ai_interface.cpp b/source/glest_game/ai/ai_interface.cpp index 5b92ba1a..c481fcea 100644 --- a/source/glest_game/ai/ai_interface.cpp +++ b/source/glest_game/ai/ai_interface.cpp @@ -123,6 +123,33 @@ CommandResult AiInterface::giveCommand(int unitIndex, CommandClass commandClass, CommandResult AiInterface::giveCommand(int unitIndex, const CommandType *commandType, const Vec2i &pos){ assert(this->gameSettings != NULL); + const Unit *unit = getMyUnit(unitIndex); + if(unit == NULL) { + char szBuf[1024]=""; + sprintf(szBuf,"In [%s::%s Line: %d] Can not find AI unit with index: %d, AI factionIndex = %d. Game out of synch.",__FILE__,__FUNCTION__,__LINE__,unitIndex,factionIndex); + throw runtime_error(szBuf); + } + const UnitType* unitType= unit->getType(); + if(unitType == NULL) { + char szBuf[1024]=""; + sprintf(szBuf,"In [%s::%s Line: %d] Can not find AI unittype with unit index: %d, AI factionIndex = %d. Game out of synch.",__FILE__,__FUNCTION__,__LINE__,unitIndex,factionIndex); + throw runtime_error(szBuf); + } + const CommandType* ct= unit->getType()->findCommandTypeById(commandType->getId()); + if(ct == NULL) { + char szBuf[4096]=""; + sprintf(szBuf,"In [%s::%s Line: %d]\nCan not find AI command type for:\nunit = %d\n[%s]\n[%s]\nactual local factionIndex = %d.\nGame out of synch.", + __FILE__,__FUNCTION__,__LINE__, + unit->getId(), unit->getFullName().c_str(),unit->getDesc().c_str(), + unit->getFaction()->getIndex()); + + SystemFlags::OutputDebug(SystemFlags::debugSystem,"%s\n",szBuf); + + std::string worldLog = world->DumpWorldToLog(); + std::string sError = "worldLog = " + worldLog + " " + string(szBuf); + throw runtime_error(sError); + } + if(this->gameSettings->getEnableServerControlledAI() == true && this->gameSettings->isNetworkGame() == true && NetworkManager::getInstance().getNetworkRole() == nrServer) { @@ -151,6 +178,33 @@ CommandResult AiInterface::giveCommand(int unitIndex, const CommandType *command CommandResult AiInterface::giveCommand(int unitIndex, const CommandType *commandType, const Vec2i &pos, const UnitType *ut){ assert(this->gameSettings != NULL); + const Unit *unit = getMyUnit(unitIndex); + if(unit == NULL) { + char szBuf[1024]=""; + sprintf(szBuf,"In [%s::%s Line: %d] Can not find AI unit with index: %d, AI factionIndex = %d. Game out of synch.",__FILE__,__FUNCTION__,__LINE__,unitIndex,factionIndex); + throw runtime_error(szBuf); + } + const UnitType* unitType= unit->getType(); + if(unitType == NULL) { + char szBuf[1024]=""; + sprintf(szBuf,"In [%s::%s Line: %d] Can not find AI unittype with unit index: %d, AI factionIndex = %d. Game out of synch.",__FILE__,__FUNCTION__,__LINE__,unitIndex,factionIndex); + throw runtime_error(szBuf); + } + const CommandType* ct= unit->getType()->findCommandTypeById(commandType->getId()); + if(ct == NULL) { + char szBuf[4096]=""; + sprintf(szBuf,"In [%s::%s Line: %d]\nCan not find AI command type for:\nunit = %d\n[%s]\n[%s]\nactual local factionIndex = %d.\nGame out of synch.", + __FILE__,__FUNCTION__,__LINE__, + unit->getId(), unit->getFullName().c_str(),unit->getDesc().c_str(), + unit->getFaction()->getIndex()); + + SystemFlags::OutputDebug(SystemFlags::debugSystem,"%s\n",szBuf); + + std::string worldLog = world->DumpWorldToLog(); + std::string sError = "worldLog = " + worldLog + " " + string(szBuf); + throw runtime_error(sError); + } + if(this->gameSettings->getEnableServerControlledAI() == true && this->gameSettings->isNetworkGame() == true && NetworkManager::getInstance().getNetworkRole() == nrServer) { @@ -180,6 +234,33 @@ CommandResult AiInterface::giveCommand(int unitIndex, const CommandType *command assert(this->gameSettings != NULL); assert(this->commander != NULL); + const Unit *unit = getMyUnit(unitIndex); + if(unit == NULL) { + char szBuf[1024]=""; + sprintf(szBuf,"In [%s::%s Line: %d] Can not find AI unit with index: %d, AI factionIndex = %d. Game out of synch.",__FILE__,__FUNCTION__,__LINE__,unitIndex,factionIndex); + throw runtime_error(szBuf); + } + const UnitType* unitType= unit->getType(); + if(unitType == NULL) { + char szBuf[1024]=""; + sprintf(szBuf,"In [%s::%s Line: %d] Can not find AI unittype with unit index: %d, AI factionIndex = %d. Game out of synch.",__FILE__,__FUNCTION__,__LINE__,unitIndex,factionIndex); + throw runtime_error(szBuf); + } + const CommandType* ct= unit->getType()->findCommandTypeById(commandType->getId()); + if(ct == NULL) { + char szBuf[4096]=""; + sprintf(szBuf,"In [%s::%s Line: %d]\nCan not find AI command type for:\nunit = %d\n[%s]\n[%s]\nactual local factionIndex = %d.\nGame out of synch.", + __FILE__,__FUNCTION__,__LINE__, + unit->getId(), unit->getFullName().c_str(),unit->getDesc().c_str(), + unit->getFaction()->getIndex()); + + SystemFlags::OutputDebug(SystemFlags::debugSystem,"%s\n",szBuf); + + std::string worldLog = world->DumpWorldToLog(); + std::string sError = "worldLog = " + worldLog + " " + string(szBuf); + throw runtime_error(sError); + } + if(this->gameSettings->getEnableServerControlledAI() == true && this->gameSettings->isNetworkGame() == true && NetworkManager::getInstance().getNetworkRole() == nrServer) { diff --git a/source/glest_game/ai/ai_rule.cpp b/source/glest_game/ai/ai_rule.cpp index f83bc2ad..55e15e60 100644 --- a/source/glest_game/ai/ai_rule.cpp +++ b/source/glest_game/ai/ai_rule.cpp @@ -582,7 +582,6 @@ void AiRuleProduce::produceSpecific(const ProduceTask *pt){ } if(!backupProducers.empty()) { - vector productionCommandIndexes; int randomstart=ai->getRandom()->randRange(0, backupProducers.size()-1); int lowestCommandCount=1000000; int currentProducerIndex=backupProducers[randomstart]; @@ -605,6 +604,7 @@ void AiRuleProduce::produceSpecific(const ProduceTask *pt){ } } // a good producer is found, lets choose a warrior production + vector productionCommandIndexes; const UnitType *ut=aiInterface->getMyUnit(bestIndex)->getType(); for(int j=0; jgetCommandTypeCount(); ++j){ const CommandType *ct= ut->getCommandType(j); @@ -626,7 +626,7 @@ void AiRuleProduce::produceSpecific(const ProduceTask *pt){ else {// do it like normal CPU SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - defCt = producersDefaultCommandType[bestIndex]; + defCt = (bestIndex < producersDefaultCommandType.size() ? producersDefaultCommandType[bestIndex] : NULL); aiInterface->giveCommand(bestIndex, defCt); } } @@ -635,11 +635,11 @@ void AiRuleProduce::produceSpecific(const ProduceTask *pt){ if(currentCommandCount==0) { SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - defCt = producersDefaultCommandType[bestIndex]; + defCt = (bestIndex < producersDefaultCommandType.size() ? producersDefaultCommandType[bestIndex] : NULL); aiInterface->giveCommand(bestIndex, defCt); } SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - defCt = producersDefaultCommandType[bestIndex]; + defCt = (bestIndex < producersDefaultCommandType.size() ? producersDefaultCommandType[bestIndex] : NULL); aiInterface->giveCommand(bestIndex, defCt); } } @@ -647,7 +647,7 @@ void AiRuleProduce::produceSpecific(const ProduceTask *pt){ { int pIndex = ai->getRandom()->randRange(0, producers.size()-1); int producerIndex= producers[pIndex]; - defCt = producersDefaultCommandType[pIndex]; + defCt = (pIndex < producersDefaultCommandType.size() ? producersDefaultCommandType[pIndex] : NULL); SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] producers.size() = %d, producerIndex = %d, pIndex = %d, producersDefaultCommandType.size() = %d\n",__FILE__,__FUNCTION__,__LINE__,producers.size(),producerIndex,pIndex,producersDefaultCommandType.size()); aiInterface->giveCommand(producerIndex, defCt); } @@ -847,7 +847,7 @@ void AiRuleBuild::buildSpecific(const BuildTask *bt){ //if free pos give command, else retry if(ai->findPosForBuilding(bt->getUnitType(), searchPos, pos)){ - defBct = buildersDefaultCommandType[bIndex]; + defBct = (bIndex < buildersDefaultCommandType.size() ? buildersDefaultCommandType[bIndex] : NULL); SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] builderIndex = %d, bIndex = %d, defBct = %p\n",__FILE__,__FUNCTION__,__LINE__,builderIndex,bIndex,defBct); aiInterface->giveCommand(builderIndex, defBct, pos, bt->getUnitType());