added more guards in AI command processing

This commit is contained in:
Mark Vejvoda 2010-06-09 00:38:15 +00:00
parent 1e6262dde5
commit 6d519c4cdf
2 changed files with 87 additions and 6 deletions

View File

@ -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) {

View File

@ -582,7 +582,6 @@ void AiRuleProduce::produceSpecific(const ProduceTask *pt){
}
if(!backupProducers.empty())
{
vector<int> 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<int> productionCommandIndexes;
const UnitType *ut=aiInterface->getMyUnit(bestIndex)->getType();
for(int j=0; j<ut->getCommandTypeCount(); ++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());