From 80051e31940a8167dd7698bd1c436112f2d4d899 Mon Sep 17 00:00:00 2001 From: Mark Vejvoda Date: Wed, 29 Sep 2010 04:38:41 +0000 Subject: [PATCH] - bugfix for AI units that do nothing when resources exist in the tech that the unit cannot harvest. (thanks RealtimeFreak for pointing this out and giving me a demo tech for testing) - bugfix for restoring saved game settings (ignore closed slots that have faction set to random) --- source/glest_game/ai/ai.cpp | 30 ++++++---- source/glest_game/ai/ai.h | 2 +- .../menu/menu_state_custom_game.cpp | 60 ++++++++++--------- 3 files changed, 52 insertions(+), 40 deletions(-) diff --git a/source/glest_game/ai/ai.cpp b/source/glest_game/ai/ai.cpp index 24bc21c4..0124ab3c 100644 --- a/source/glest_game/ai/ai.cpp +++ b/source/glest_game/ai/ai.cpp @@ -19,6 +19,7 @@ #include "unit.h" #include "program.h" #include "config.h" +#include #include "leak_dumper.h" using namespace Shared::Graphics; @@ -185,18 +186,27 @@ float Ai::getRatioOfClass(UnitClass uc){ } } -const ResourceType *Ai::getNeededResource(){ +const ResourceType *Ai::getNeededResource(int unitIndex) { - int amount= -1; + int amount = numeric_limits::max(); const ResourceType *neededResource= NULL; const TechTree *tt= aiInterface->getTechTree(); - for(int i=0; igetResourceTypeCount(); ++i){ + for(int i = 0; i < tt->getResourceTypeCount(); ++i) { const ResourceType *rt= tt->getResourceType(i); const Resource *r= aiInterface->getResource(rt); - if(rt->getClass()!=rcStatic && rt->getClass()!=rcConsumable && (r->getAmount()getAmount(); - neededResource= rt; + if( rt->getClass() != rcStatic && rt->getClass() != rcConsumable && + r->getAmount() < amount) { + + // Now MAKE SURE the unit has a harvest command for this resource + // AND that the resource is within eye-sight to avoid units + // standing around doing nothing. + const HarvestCommandType *hct= aiInterface->getMyUnit(unitIndex)->getType()->getFirstHarvestCommand(rt); + Vec2i resPos; + if(hct != NULL && aiInterface->getNearestSightedResource(rt, aiInterface->getHomeLocation(), resPos)) { + amount= r->getAmount(); + neededResource= rt; + } } } @@ -472,14 +482,14 @@ void Ai::returnBase(int unitIndex){ //aiInterface->printLog(1, "Order return to base pos:" + intToStr(pos.x)+", "+intToStr(pos.y)+": "+rrToStr(r)+"\n"); } -void Ai::harvest(int unitIndex){ +void Ai::harvest(int unitIndex) { - const ResourceType *rt= getNeededResource(); + const ResourceType *rt= getNeededResource(unitIndex); - if(rt!=NULL){ + if(rt != NULL) { const HarvestCommandType *hct= aiInterface->getMyUnit(unitIndex)->getType()->getFirstHarvestCommand(rt); Vec2i resPos; - if(hct!=NULL && aiInterface->getNearestSightedResource(rt, aiInterface->getHomeLocation(), resPos)){ + if(hct != NULL && aiInterface->getNearestSightedResource(rt, aiInterface->getHomeLocation(), resPos)) { resPos= resPos+Vec2i(random.randRange(-2, 2), random.randRange(-2, 2)); aiInterface->giveCommand(unitIndex, hct, resPos); //aiInterface->printLog(4, "Order harvest pos:" + intToStr(resPos.x)+", "+intToStr(resPos.y)+": "+rrToStr(r)+"\n"); diff --git a/source/glest_game/ai/ai.h b/source/glest_game/ai/ai.h index 5ec61f15..4f5be024 100644 --- a/source/glest_game/ai/ai.h +++ b/source/glest_game/ai/ai.h @@ -159,7 +159,7 @@ public: int getCountOfClass(UnitClass uc); float getRatioOfClass(UnitClass uc); - const ResourceType *getNeededResource(); + const ResourceType *getNeededResource(int unitIndex); bool isStableBase(); bool findPosForBuilding(const UnitType* building, const Vec2i &searchPos, Vec2i &pos); bool findAbleUnit(int *unitIndex, CommandClass ability, bool idleOnly); diff --git a/source/glest_game/menu/menu_state_custom_game.cpp b/source/glest_game/menu/menu_state_custom_game.cpp index b8790460..23534b76 100644 --- a/source/glest_game/menu/menu_state_custom_game.cpp +++ b/source/glest_game/menu/menu_state_custom_game.cpp @@ -602,38 +602,40 @@ void MenuStateCustomGame::mouseClick(int x, int y, MouseButton mouseButton){ SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); // Check for random faction selection and choose the faction now - if(listBoxFactions[i].getSelectedItem() == formatString(GameConstants::RANDOMFACTION_SLOTNAME)) { - SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d] i = %d\n",__FILE__,__FUNCTION__,__LINE__,i); - - // Max 1000 tries to get a random, unused faction - for(int findRandomFaction = 1; findRandomFaction < 1000; ++findRandomFaction) { - srand(time(NULL) + findRandomFaction); - int selectedFactionIndex = rand() % listBoxFactions[i].getItemCount(); - string selectedFactionName = listBoxFactions[i].getItem(selectedFactionIndex); - - SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d] selectedFactionName [%s] selectedFactionIndex = %d, findRandomFaction = %d\n",__FILE__,__FUNCTION__,__LINE__,selectedFactionName.c_str(),selectedFactionIndex,findRandomFaction); - - if( selectedFactionName != formatString(GameConstants::RANDOMFACTION_SLOTNAME) && - selectedFactionName != formatString(GameConstants::OBSERVER_SLOTNAME) && - std::find(randomFactionSelectionList.begin(),randomFactionSelectionList.end(),selectedFactionName) == randomFactionSelectionList.end()) { - - SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); - listBoxFactions[i].setSelectedItem(selectedFactionName); - randomFactionSelectionList.push_back(selectedFactionName); - break; - } - } - + if(listBoxControls[i].getSelectedItemIndex() != ctClosed) { if(listBoxFactions[i].getSelectedItem() == formatString(GameConstants::RANDOMFACTION_SLOTNAME)) { - SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d] RandomCount = %d\n",__FILE__,__FUNCTION__,__LINE__,RandomCount); + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d] i = %d\n",__FILE__,__FUNCTION__,__LINE__,i); - listBoxFactions[i].setSelectedItemIndex(RandomCount); - randomFactionSelectionList.push_back(listBoxFactions[i].getItem(RandomCount)); + // Max 1000 tries to get a random, unused faction + for(int findRandomFaction = 1; findRandomFaction < 1000; ++findRandomFaction) { + srand(time(NULL) + findRandomFaction); + int selectedFactionIndex = rand() % listBoxFactions[i].getItemCount(); + string selectedFactionName = listBoxFactions[i].getItem(selectedFactionIndex); + + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d] selectedFactionName [%s] selectedFactionIndex = %d, findRandomFaction = %d\n",__FILE__,__FUNCTION__,__LINE__,selectedFactionName.c_str(),selectedFactionIndex,findRandomFaction); + + if( selectedFactionName != formatString(GameConstants::RANDOMFACTION_SLOTNAME) && + selectedFactionName != formatString(GameConstants::OBSERVER_SLOTNAME) && + std::find(randomFactionSelectionList.begin(),randomFactionSelectionList.end(),selectedFactionName) == randomFactionSelectionList.end()) { + + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); + listBoxFactions[i].setSelectedItem(selectedFactionName); + randomFactionSelectionList.push_back(selectedFactionName); + break; + } + } + + if(listBoxFactions[i].getSelectedItem() == formatString(GameConstants::RANDOMFACTION_SLOTNAME)) { + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d] RandomCount = %d\n",__FILE__,__FUNCTION__,__LINE__,RandomCount); + + listBoxFactions[i].setSelectedItemIndex(RandomCount); + randomFactionSelectionList.push_back(listBoxFactions[i].getItem(RandomCount)); + } + + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d] i = %d, listBoxFactions[i].getSelectedItem() [%s]\n",__FILE__,__FUNCTION__,__LINE__,i,listBoxFactions[i].getSelectedItem().c_str()); + + RandomCount++; } - - SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d] i = %d, listBoxFactions[i].getSelectedItem() [%s]\n",__FILE__,__FUNCTION__,__LINE__,i,listBoxFactions[i].getSelectedItem().c_str()); - - RandomCount++; } }