From 6e26641799e612fa63b55b40651b01a84d2a166b Mon Sep 17 00:00:00 2001 From: Mark Vejvoda Date: Tue, 13 Jul 2010 22:51:27 +0000 Subject: [PATCH] - a possible fix for the AI. this change will randomize all possible produce, morph and build operations instead of just using the last commandtype found for candidate units --- source/glest_game/ai/ai.cpp | 2 +- source/glest_game/ai/ai_interface.cpp | 2 +- source/glest_game/ai/ai_rule.cpp | 56 +++++++++++++++++++++------ 3 files changed, 46 insertions(+), 14 deletions(-) diff --git a/source/glest_game/ai/ai.cpp b/source/glest_game/ai/ai.cpp index 50c56d27..8044ae99 100644 --- a/source/glest_game/ai/ai.cpp +++ b/source/glest_game/ai/ai.cpp @@ -1,7 +1,7 @@ // ============================================================== // This file is part of Glest (www.glest.org) // -// Copyright (C) 2001-2008 Martio Figueroa +// Copyright (C) 2001-2008 Martiño Figueroa // // You can redistribute this code and/or modify it under // the terms of the GNU General Public License as published diff --git a/source/glest_game/ai/ai_interface.cpp b/source/glest_game/ai/ai_interface.cpp index 6524e324..db910505 100644 --- a/source/glest_game/ai/ai_interface.cpp +++ b/source/glest_game/ai/ai_interface.cpp @@ -1,7 +1,7 @@ // ============================================================== // This file is part of Glest (www.glest.org) // -// Copyright (C) 2001-2008 Martio Figueroa +// Copyright (C) 2001-2008 Martiño Figueroa // // You can redistribute this code and/or modify it under // the terms of the GNU General Public License as published diff --git a/source/glest_game/ai/ai_rule.cpp b/source/glest_game/ai/ai_rule.cpp index 429f67ee..38eb37ec 100644 --- a/source/glest_game/ai/ai_rule.cpp +++ b/source/glest_game/ai/ai_rule.cpp @@ -1,7 +1,7 @@ // ============================================================== // This file is part of Glest (www.glest.org) // -// Copyright (C) 2001-2008 Martio Figueroa +// Copyright (C) 2001-2008 Martiño Figueroa // // You can redistribute this code and/or modify it under // the terms of the GNU General Public License as published @@ -491,7 +491,9 @@ void AiRuleProduce::produceSpecific(const ProduceTask *pt){ //produce specific unit vector producers; - map producersDefaultCommandType; + // Hold a list of units which can produce or morph + // then a list of commandtypes for each unit + map > producersDefaultCommandType; const CommandType *defCt= NULL; //for each unit @@ -511,7 +513,7 @@ void AiRuleProduce::produceSpecific(const ProduceTask *pt){ if(aiInterface->reqsOk(ct)){ defCt= ct; producers.push_back(i); - producersDefaultCommandType[i] = ct; + producersDefaultCommandType[i].push_back(ct); } } } @@ -572,7 +574,6 @@ void AiRuleProduce::produceSpecific(const ProduceTask *pt){ } // need to calculate another producer, maybe its better to produce another warrior with another producer vector backupProducers; - map backupProducersDefaultCommandType; // find another producer unit which is free and produce any kind of warrior. //for each unit for(int i=0; igetMyUnitCount(); ++i){ @@ -587,7 +588,6 @@ void AiRuleProduce::produceSpecific(const ProduceTask *pt){ if(unitType->hasSkillClass(scAttack) && !unitType->hasCommandClass(ccHarvest) && aiInterface->reqsOk(ct)) {//this can produce a warrior backupProducers.push_back(i); - backupProducersDefaultCommandType[i] = ct; } } } @@ -659,7 +659,12 @@ void AiRuleProduce::produceSpecific(const ProduceTask *pt){ SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); defCt = NULL; if(producersDefaultCommandType.find(bestIndex) != producersDefaultCommandType.end()) { - defCt = producersDefaultCommandType[bestIndex]; + int bestCommandTypeCount = producersDefaultCommandType[bestIndex].size(); + int bestCommandTypeIndex = ai->getRandom()->randRange(0, bestCommandTypeCount-1); + + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] bestCommandTypeIndex = %d, bestCommandTypeCount = %d\n",__FILE__,__FUNCTION__,__LINE__,bestCommandTypeIndex,bestCommandTypeCount); + + defCt = producersDefaultCommandType[bestIndex][bestCommandTypeIndex]; } aiInterface->giveCommand(bestIndex, defCt); } @@ -671,14 +676,26 @@ void AiRuleProduce::produceSpecific(const ProduceTask *pt){ SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); defCt = NULL; if(producersDefaultCommandType.find(bestIndex) != producersDefaultCommandType.end()) { - defCt = producersDefaultCommandType[bestIndex]; + //defCt = producersDefaultCommandType[bestIndex]; + int bestCommandTypeCount = producersDefaultCommandType[bestIndex].size(); + int bestCommandTypeIndex = ai->getRandom()->randRange(0, bestCommandTypeCount-1); + + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] bestCommandTypeIndex = %d, bestCommandTypeCount = %d\n",__FILE__,__FUNCTION__,__LINE__,bestCommandTypeIndex,bestCommandTypeCount); + + defCt = producersDefaultCommandType[bestIndex][bestCommandTypeIndex]; } aiInterface->giveCommand(bestIndex, defCt); } SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); defCt = NULL; if(producersDefaultCommandType.find(bestIndex) != producersDefaultCommandType.end()) { - defCt = producersDefaultCommandType[bestIndex]; + //defCt = producersDefaultCommandType[bestIndex]; + int bestCommandTypeCount = producersDefaultCommandType[bestIndex].size(); + int bestCommandTypeIndex = ai->getRandom()->randRange(0, bestCommandTypeCount-1); + + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] bestCommandTypeIndex = %d, bestCommandTypeCount = %d\n",__FILE__,__FUNCTION__,__LINE__,bestCommandTypeIndex,bestCommandTypeCount); + + defCt = producersDefaultCommandType[bestIndex][bestCommandTypeIndex]; } aiInterface->giveCommand(bestIndex, defCt); } @@ -689,7 +706,13 @@ void AiRuleProduce::produceSpecific(const ProduceTask *pt){ int producerIndex= producers[pIndex]; defCt = NULL; if(producersDefaultCommandType.find(producerIndex) != producersDefaultCommandType.end()) { - defCt = producersDefaultCommandType[producerIndex]; + //defCt = producersDefaultCommandType[producerIndex]; + int bestCommandTypeCount = producersDefaultCommandType[producerIndex].size(); + int bestCommandTypeIndex = ai->getRandom()->randRange(0, bestCommandTypeCount-1); + + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] bestCommandTypeIndex = %d, bestCommandTypeCount = %d\n",__FILE__,__FUNCTION__,__LINE__,bestCommandTypeIndex,bestCommandTypeCount); + + defCt = producersDefaultCommandType[producerIndex][bestCommandTypeIndex]; } 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()); @@ -845,7 +868,9 @@ void AiRuleBuild::buildSpecific(const BuildTask *bt){ } vector builders; - map buildersDefaultCommandType; + // Hold a list of units which can build + // then a list of build commandtypes for each unit + map > buildersDefaultCommandType; const BuildCommandType *defBct= NULL; //for each unit @@ -872,7 +897,7 @@ void AiRuleBuild::buildSpecific(const BuildTask *bt){ if(bt->getUnitType()==building){ if(aiInterface->reqsOk(bct)){ builders.push_back(i); - buildersDefaultCommandType[i] = bct; + buildersDefaultCommandType[i].push_back(bct); defBct= bct; } } @@ -894,7 +919,14 @@ void AiRuleBuild::buildSpecific(const BuildTask *bt){ defBct = NULL; if(buildersDefaultCommandType.find(builderIndex) != buildersDefaultCommandType.end()) { - defBct = buildersDefaultCommandType[builderIndex]; + //defBct = buildersDefaultCommandType[builderIndex]; + int bestCommandTypeCount = buildersDefaultCommandType[builderIndex].size(); + int bestCommandTypeIndex = ai->getRandom()->randRange(0, bestCommandTypeCount-1); + + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] bestCommandTypeIndex = %d, bestCommandTypeCount = %d\n",__FILE__,__FUNCTION__,__LINE__,bestCommandTypeIndex,bestCommandTypeCount); + + defBct = buildersDefaultCommandType[builderIndex][bestCommandTypeIndex]; + } SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] builderIndex = %d, bIndex = %d, defBct = %p\n",__FILE__,__FUNCTION__,__LINE__,builderIndex,bIndex,defBct);