From d8c337ae7ba9bd6403497ccf01ce4a53e6c8bbcf Mon Sep 17 00:00:00 2001 From: Mark Vejvoda Date: Wed, 6 Apr 2011 17:50:20 +0000 Subject: [PATCH] - bugfix for resource usage checking - added new techtree validation in validation report to tell if a techtress has resources that are ununsed by the techtree --- source/glest_game/ai/ai_interface.cpp | 29 +----------------- source/glest_game/ai/ai_rule.cpp | 2 +- source/glest_game/main/main.cpp | 2 -- source/glest_game/types/faction_type.cpp | 38 +++++++++++++++++++++++- source/glest_game/types/faction_type.h | 1 + source/glest_game/types/tech_tree.cpp | 32 ++++++++++++++++++-- 6 files changed, 70 insertions(+), 34 deletions(-) diff --git a/source/glest_game/ai/ai_interface.cpp b/source/glest_game/ai/ai_interface.cpp index 74d7b097..9924a014 100644 --- a/source/glest_game/ai/ai_interface.cpp +++ b/source/glest_game/ai/ai_interface.cpp @@ -606,34 +606,7 @@ Map * AiInterface::getMap() { } bool AiInterface::factionUsesResourceType(const FactionType *factionType, const ResourceType *rt) { - bool factionUsesResourceType = false; - for(int j = 0; factionUsesResourceType == false && j < factionType->getUnitTypeCount(); ++j) { - const UnitType *ut= factionType->getUnitType(j); - for(int k = 0; factionUsesResourceType == false && k < ut->getCostCount(); ++k) { - const Resource *costResource = ut->getCost(k); - if(costResource != NULL && costResource->getType() == rt) { - factionUsesResourceType = true; - break; - } - } - if(factionUsesResourceType == false) { - for(int k = 0; factionUsesResourceType == false && k < ut->getCommandTypeCount(); ++k) { - const CommandType *commandType = ut->getCommandType(k); - if(commandType != NULL && commandType->getClass() == ccHarvest) { - const HarvestCommandType *hct = dynamic_cast(commandType); - if(hct != NULL && hct->getHarvestedResourceCount() > 0) { - for(int l = 0; factionUsesResourceType == false && l < hct->getHarvestedResourceCount(); ++l) { - if(hct->getHarvestedResource(l) == rt) { - factionUsesResourceType = true; - break; - } - } - } - } - } - } - } - + bool factionUsesResourceType = factionType->factionUsesResourceType(rt); return factionUsesResourceType; } diff --git a/source/glest_game/ai/ai_rule.cpp b/source/glest_game/ai/ai_rule.cpp index 16e4de1d..9b216626 100644 --- a/source/glest_game/ai/ai_rule.cpp +++ b/source/glest_game/ai/ai_rule.cpp @@ -500,7 +500,7 @@ void AiRuleProduce::produceGeneric(const ProduceTask *pt){ //add specific produce task if(!ableUnits.empty()){ - if(ai->outputAIBehaviourToConsole()) printf("produceGeneric !ableUnits.empty(), ableUnits.size() = [%d] Testing AI RULE Name[%s]\n",ableUnits.size(), this->getName().c_str()); + if(ai->outputAIBehaviourToConsole()) printf("produceGeneric !ableUnits.empty(), ableUnits.size() = [%d] Testing AI RULE Name[%s]\n",(int)ableUnits.size(), this->getName().c_str()); //priority for non produced units for(unsigned int i=0; i FactionType::validateFactionType() { std::vector FactionType::validateFactionTypeResourceTypes(vector &resourceTypes) { std::vector results; - for(int i=0; igetUnitTypeCount(); ++j) { + const UnitType *ut= this->getUnitType(j); + for(int k = 0; factionUsesResourceType == false && k < ut->getCostCount(); ++k) { + const Resource *costResource = ut->getCost(k); + //printf("#1 factionUsesResourceType, unit [%s] resource [%s] cost [%s]\n",ut->getName().c_str(),rt->getName().c_str(),costResource->getType()->getName().c_str()); + + if(costResource != NULL && costResource->getType()->getName() == rt->getName()) { + factionUsesResourceType = true; + break; + } + } + if(factionUsesResourceType == false) { + for(unsigned int k = 0; factionUsesResourceType == false && k < ut->getCommandTypeCount(); ++k) { + const CommandType *commandType = ut->getCommandType(k); + if(commandType != NULL && commandType->getClass() == ccHarvest) { + const HarvestCommandType *hct = dynamic_cast(commandType); + if(hct != NULL && hct->getHarvestedResourceCount() > 0) { + for(unsigned int l = 0; factionUsesResourceType == false && l < hct->getHarvestedResourceCount(); ++l) { + //printf("#2 factionUsesResourceType, unit [%s] resource [%s] harvest [%s]\n",ut->getName().c_str(),rt->getName().c_str(),hct->getHarvestedResource(l)->getName().c_str()); + + if(hct->getHarvestedResource(l)->getName() == rt->getName()) { + factionUsesResourceType = true; + break; + } + } + } + } + } + } + } + + return factionUsesResourceType; +} + std::string FactionType::toString() const { std::string result = ""; diff --git a/source/glest_game/types/faction_type.h b/source/glest_game/types/faction_type.h index 06d89de4..b94db1c6 100644 --- a/source/glest_game/types/faction_type.h +++ b/source/glest_game/types/faction_type.h @@ -75,6 +75,7 @@ public: std::vector validateFactionTypeUpgradeTypes(); void deletePixels(); + bool factionUsesResourceType(const ResourceType *rt) const; }; }}//end namespace diff --git a/source/glest_game/types/tech_tree.cpp b/source/glest_game/types/tech_tree.cpp index 39f2f7ff..5f082668 100644 --- a/source/glest_game/types/tech_tree.cpp +++ b/source/glest_game/types/tech_tree.cpp @@ -215,9 +215,37 @@ std::vector TechTree::validateFactionTypes() { std::vector TechTree::validateResourceTypes() { std::vector results; - for (int i = 0; i < factionTypes.size(); ++i) { + ResourceTypes resourceTypesNotUsed = resourceTypes; + for (unsigned int i = 0; i < factionTypes.size(); ++i) { + //printf("Validating [%d / %d] faction [%s]\n",i,(int)factionTypes.size(),factionTypes[i].getName().c_str()); + std::vector factionResults = factionTypes[i].validateFactionTypeResourceTypes(resourceTypes); - results.insert(results.end(), factionResults.begin(), factionResults.end()); + if(factionResults.size() > 0) { + results.insert(results.end(), factionResults.begin(), factionResults.end()); + } + + // Check if the faction uses the resources in this techtree + // Now lets find a matching faction resource type for the unit + for(int j = resourceTypesNotUsed.size() -1; j >= 0; --j) { + const ResourceType &rt = resourceTypesNotUsed[j]; + //printf("Validating [%d / %d] resourcetype [%s]\n",j,(int)resourceTypesNotUsed.size(),rt.getName().c_str()); + + if(factionTypes[i].factionUsesResourceType(&rt) == true) { + //printf("FOUND FACTION CONSUMER FOR RESOURCE - [%d / %d] faction [%s] resource [%d / %d] resourcetype [%s]\n",i,(int)factionTypes.size(),factionTypes[i].getName().c_str(),j,(int)resourceTypesNotUsed.size(),rt.getName().c_str()); + resourceTypesNotUsed.erase(resourceTypesNotUsed.begin() + j); + } + } + } + + if(resourceTypesNotUsed.size() > 0) { + //printf("FOUND unused resource Types [%d]\n",(int)resourceTypesNotUsed.size()); + + for (unsigned int i = 0; i < resourceTypesNotUsed.size(); ++i) { + const ResourceType &rt = resourceTypesNotUsed[i]; + char szBuf[4096]=""; + sprintf(szBuf,"The Resource type [%s] is not used by any units in this techtree!",rt.getName().c_str()); + results.push_back(szBuf); + } } return results; }