- 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)
This commit is contained in:
Mark Vejvoda 2010-09-29 04:38:41 +00:00
parent c9f8433d83
commit 80051e3194
3 changed files with 52 additions and 40 deletions

View File

@ -19,6 +19,7 @@
#include "unit.h"
#include "program.h"
#include "config.h"
#include <limits>
#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<int>::max();
const ResourceType *neededResource= NULL;
const TechTree *tt= aiInterface->getTechTree();
for(int i=0; i<tt->getResourceTypeCount(); ++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()<amount || amount==-1)){
amount= r->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");

View File

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

View File

@ -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++;
}
}