diff --git a/source/glest_game/types/skill_type.cpp b/source/glest_game/types/skill_type.cpp index 006b707f..14f218b4 100644 --- a/source/glest_game/types/skill_type.cpp +++ b/source/glest_game/types/skill_type.cpp @@ -61,34 +61,16 @@ bool AttackBoost::isAffected(const Unit *source, const Unit *dest) const { else { // All units are affected (including enemies) if(targetType == abtAll) { - destUnitMightApply = (boostUnitList.empty() == true); - - // Specify which units are affected - for(unsigned int i = 0; i < boostUnitList.size(); ++i) { - const UnitType *ut = boostUnitList[i]; - if(dest->getType()->getId() == ut->getId()) { - destUnitMightApply = true; - break; - } - } - + destUnitMightApply = (boostUnitList.empty() && tags.empty()); + destUnitMightApply = isInUnitListOrTags(dest->getType()); } // Only same faction units are affected else if(targetType == abtFaction) { //if(boostUnitList.empty() == true) { if(source->getFactionIndex() == dest->getFactionIndex()) { //destUnitMightApply = true; - destUnitMightApply = (boostUnitList.empty() == true); - - // Specify which units are affected - for(unsigned int i = 0; i < boostUnitList.size(); ++i) { - const UnitType *ut = boostUnitList[i]; - if(dest->getType()->getId() == ut->getId()) { - destUnitMightApply = true; - break; - } - } - + destUnitMightApply = (boostUnitList.empty() && tags.empty()); + destUnitMightApply = isInUnitListOrTags(dest->getType()); } //} } @@ -97,16 +79,8 @@ bool AttackBoost::isAffected(const Unit *source, const Unit *dest) const { //if(boostUnitList.empty() == true) { if(source->isAlly(dest) == true) { //destUnitMightApply = true; - destUnitMightApply = (boostUnitList.empty() == true); - - // Specify which units are affected - for(unsigned int i = 0; i < boostUnitList.size(); ++i) { - const UnitType *ut = boostUnitList[i]; - if(dest->getType()->getId() == ut->getId()) { - destUnitMightApply = true; - break; - } - } + destUnitMightApply = (boostUnitList.empty() && tags.empty()); + destUnitMightApply = isInUnitListOrTags(dest->getType()); } //} } @@ -115,28 +89,13 @@ bool AttackBoost::isAffected(const Unit *source, const Unit *dest) const { //if(boostUnitList.empty() == true) { if(source->isAlly(dest) == false) { //destUnitMightApply = true; - destUnitMightApply = (boostUnitList.empty() == true); - - // Specify which units are affected - for(unsigned int i = 0; i < boostUnitList.size(); ++i) { - const UnitType *ut = boostUnitList[i]; - if(dest->getType()->getId() == ut->getId()) { - destUnitMightApply = true; - break; - } - } + destUnitMightApply = (boostUnitList.empty() && tags.empty()); + destUnitMightApply = isInUnitListOrTags(dest->getType()); } //} } else if(targetType == abtUnitTypes) { - // Specify which units are affected - for(unsigned int i = 0; i < boostUnitList.size(); ++i) { - const UnitType *ut = boostUnitList[i]; - if(dest->getType()->getId() == ut->getId()) { - destUnitMightApply = true; - break; - } - } + destUnitMightApply = isInUnitListOrTags(dest->getType()); } } @@ -151,6 +110,32 @@ bool AttackBoost::isAffected(const Unit *source, const Unit *dest) const { return result; } +bool AttackBoost::isInUnitListOrTags(const UnitType *unitType) const { + // Specify which units are affected + std::set::iterator it; + for (it = boostUnitList.begin(); it != boostUnitList.end(); ++it) { + const UnitType *boostUnit = *it; + if(unitType->getId() == boostUnit->getId()) { + return true; + } + } + set unitTags = unitType->getTags(); + set intersect; + set_intersection(tags.begin(),tags.end(),unitTags.begin(),unitTags.end(), + std::inserter(intersect,intersect.begin())); + if(!intersect.empty()) return true; + + // Otherwise no match + return false; +} + +string AttackBoost::getTagName(string tag, bool translatedValue) const { + if(translatedValue == false) return tag; + + Lang &lang = Lang::getInstance(); + return lang.getTechTreeString("TagName_" + tag, tag.c_str()); +} + string AttackBoost::getDesc(bool translatedValue) const{ Lang &lang= Lang::getInstance(); string str= ""; @@ -190,14 +175,31 @@ string AttackBoost::getDesc(bool translatedValue) const{ str+= lang.getString("AffectedUnitsFromAll") +":\n"; } - if(boostUnitList.empty() == false) { - for(int i=0; i < (int)boostUnitList.size(); ++i){ - str+= " "+boostUnitList[i]->getName(translatedValue)+"\n"; - } + if(boostUnitList.empty() && tags.empty()) { + str+= lang.getString("All")+"\n"; } else { - str+= lang.getString("All")+"\n"; + // We want the output to be sorted, so convert the set to a vector and sort that + std::vector outputUnits(boostUnitList.begin(), boostUnitList.end()); + std::sort(outputUnits.begin(), outputUnits.end(), UnitTypeSorter()); + + vector::iterator unitIter; + for (unitIter = outputUnits.begin(); unitIter != outputUnits.end(); ++unitIter) { + const UnitType *unit = *unitIter; + str+= indent+unit->getName(translatedValue)+"\n"; + } + + // Do the same for tags + std::vector outputTags(tags.begin(), tags.end()); + std::sort(outputTags.begin(), outputTags.end()); + + vector::iterator tagIter; + for (tagIter = outputTags.begin(); tagIter != outputTags.end(); ++tagIter) { + string tag = *tagIter; + str+= indent + lang.getString("TagDesc", (translatedValue == true ? "" : "english")) + + " " + getTagName(tag,translatedValue) + "\n"; + } } return str; @@ -222,10 +224,18 @@ void AttackBoost::loadGame(const XmlNode *rootNode, Faction *faction, const Skil string unitTypeName = node->getAttribute("name")->getValue(); const UnitType *unitType = faction->getType()->getUnitType(unitTypeName); if(unitType != NULL) { - boostUnitList.push_back(unitType); + boostUnitList.insert(unitType); } } } + if(attackBoostNode->hasChild("tag")) { + vector tagNodeList = attackBoostNode->getChildList("tag"); + for(unsigned int i = 0; i < tagNodeList.size(); ++i) { + XmlNode *node = tagNodeList[i]; + string tagName = node->getAttribute("name")->getValue(); + tags.insert(tagName); + } + } //boostUpgrade.loadGame(attackBoostNode,faction); boostUpgrade = skillType->getAttackBoost()->boostUpgrade; @@ -252,10 +262,17 @@ void AttackBoost::saveGame(XmlNode *rootNode) const { // AttackBoostTargetType targetType; attackBoostNode->addAttribute("targetType",intToStr(targetType), mapTagReplacements); // vector boostUnitList; - for(unsigned int i = 0; i < boostUnitList.size(); ++i) { - const UnitType *ut = boostUnitList[i]; + std::set::iterator unitIter; + for (unitIter = boostUnitList.begin(); unitIter != boostUnitList.end(); ++unitIter) { + const UnitType *unit = *unitIter; XmlNode *unitTypeNode = attackBoostNode->addChild("UnitType"); - unitTypeNode->addAttribute("name",ut->getName(false), mapTagReplacements); + unitTypeNode->addAttribute("name",unit->getName(false), mapTagReplacements); + } + std::set::iterator tagIter; + for (tagIter = tags.begin(); tagIter != tags.end(); ++tagIter) { + string tag = *tagIter; + XmlNode *unitTypeNode = attackBoostNode->addChild("tag"); + unitTypeNode->addAttribute("name", tag, mapTagReplacements); } // UpgradeTypeBase boostUpgrade; boostUpgrade.saveGame(attackBoostNode); @@ -352,38 +369,18 @@ void SkillType::loadAttackBoost(const XmlNode *attackBoostsNode, const XmlNode * if(targetType == "ally") { attackBoost.targetType = abtAlly; - for(int i = 0;i < (int)attackBoostNode->getChild("target")->getChildCount();++i) { - const XmlNode *boostUnitNode = attackBoostNode->getChild("target")->getChild("unit-type", i); - attackBoost.boostUnitList.push_back(ft->getUnitType(boostUnitNode->getAttribute("name")->getRestrictedValue())); - } } else if(targetType == "foe") { attackBoost.targetType = abtFoe; - for(int i = 0;i < (int)attackBoostNode->getChild("target")->getChildCount();++i) { - const XmlNode *boostUnitNode = attackBoostNode->getChild("target")->getChild("unit-type", i); - attackBoost.boostUnitList.push_back(ft->getUnitType(boostUnitNode->getAttribute("name")->getRestrictedValue())); - } } else if(targetType == "faction") { attackBoost.targetType = abtFaction; - for(int i = 0;i < (int)attackBoostNode->getChild("target")->getChildCount();++i) { - const XmlNode *boostUnitNode = attackBoostNode->getChild("target")->getChild("unit-type", i); - attackBoost.boostUnitList.push_back(ft->getUnitType(boostUnitNode->getAttribute("name")->getRestrictedValue())); - } } else if(targetType == "unit-types") { attackBoost.targetType = abtUnitTypes; - for(int i = 0;i < (int)attackBoostNode->getChild("target")->getChildCount();++i) { - const XmlNode *boostUnitNode = attackBoostNode->getChild("target")->getChild("unit-type", i); - attackBoost.boostUnitList.push_back(ft->getUnitType(boostUnitNode->getAttribute("name")->getRestrictedValue())); - } } else if(targetType == "all") { attackBoost.targetType = abtAll; - for(int i = 0;i < (int)attackBoostNode->getChild("target")->getChildCount();++i) { - const XmlNode *boostUnitNode = attackBoostNode->getChild("target")->getChild("unit-type", i); - attackBoost.boostUnitList.push_back(ft->getUnitType(boostUnitNode->getAttribute("name")->getRestrictedValue())); - } } else { char szBuf[8096] = ""; @@ -391,6 +388,21 @@ void SkillType::loadAttackBoost(const XmlNode *attackBoostsNode, const XmlNode * throw megaglest_runtime_error(szBuf); } + // Load the regular targets + const XmlNode *targetNode = attackBoostNode->getChild("target"); + vector targetNodes = targetNode->getChildList("unit-type"); + for(size_t i = 0;i < targetNodes.size(); ++i) { + string unitName = targetNodes.at(i)->getAttribute("name")->getRestrictedValue(); + attackBoost.boostUnitList.insert(ft->getUnitType(unitName)); + } + + // Load tags + vector tagNodes = targetNode->getChildList("tag"); + for(size_t i = 0;i < tagNodes.size(); ++i) { + string unitName = tagNodes.at(i)->getAttribute("name")->getRestrictedValue(); + attackBoost.tags.insert(unitName); + } + attackBoost.boostUpgrade.load(attackBoostNode,attackBoost.name); if(attackBoostNode->hasChild("particles") == true) { const XmlNode *particleNode = attackBoostNode->getChild("particles"); diff --git a/source/glest_game/types/skill_type.h b/source/glest_game/types/skill_type.h index 15f32360..cad0f652 100644 --- a/source/glest_game/types/skill_type.h +++ b/source/glest_game/types/skill_type.h @@ -30,6 +30,7 @@ #include "projectile_type.h" #include "upgrade_type.h" #include "leak_dumper.h" +#include using Shared::Sound::StaticSound; using Shared::Xml::XmlNode; @@ -98,7 +99,8 @@ public: bool allowMultipleBoosts; int radius; AttackBoostTargetType targetType; - vector boostUnitList; + std::set boostUnitList; + std::set tags; UpgradeTypeBase boostUpgrade; UnitParticleSystemType *unitParticleSystemTypeForSourceUnit; @@ -109,9 +111,20 @@ public: bool isAffected(const Unit *source, const Unit *dest) const; virtual string getDesc(bool translatedValue) const; + string getTagName(string tag, bool translatedValue=false) const; virtual void saveGame(XmlNode *rootNode) const; virtual void loadGame(const XmlNode *rootNode, Faction *faction, const SkillType *skillType); + +private: + /** + * Checks if a unit is affected by the attack boost by checking if either the UnitType is in + * the #boostUnitList or shares a tag with #tags. + * @param unitType The unit type to check. + * @return True if the unit *might* be affected by the attack boost (still have to check if it's + * in range), false otherwise. + */ + bool isInUnitListOrTags(const UnitType *unitType) const; }; class AnimationAttributes { diff --git a/source/glest_game/types/unit_type.cpp b/source/glest_game/types/unit_type.cpp index 7fbdccf8..82129a6a 100644 --- a/source/glest_game/types/unit_type.cpp +++ b/source/glest_game/types/unit_type.cpp @@ -602,7 +602,7 @@ void UnitType::loaddd(int id,const string &dir, const TechTree *techTree, if(parametersNode->hasChild("resources-death")) { const XmlNode *deathResourcesNode= parametersNode->getChild("resources-death"); - for(int i=0; i < deathResourcesNode->getChildCount(); ++i){ + for(size_t i=0; i < deathResourcesNode->getChildCount(); ++i){ const XmlNode *resourceNode= deathResourcesNode->getChild("resource", i); string name= resourceNode->getAttribute("name")->getRestrictedValue(); @@ -656,6 +656,17 @@ void UnitType::loaddd(int id,const string &dir, const TechTree *techTree, } } + // Tags + if(parametersNode->hasChild("tags")) { + const XmlNode *tagsNode= parametersNode->getChild("tags"); + + for(size_t i=0; i < tagsNode->getChildCount(); ++i){ + const XmlNode *resourceNode= tagsNode->getChild("tag", i); + string tag= resourceNode->getAttribute("value")->getRestrictedValue(); + tags.insert(tag); + } + } + //image const XmlNode *imageNode= parametersNode->getChild("image"); image= Renderer::getInstance().newTexture2D(rsGame); diff --git a/source/glest_game/types/unit_type.h b/source/glest_game/types/unit_type.h index fee7f903..2bc37fb0 100644 --- a/source/glest_game/types/unit_type.h +++ b/source/glest_game/types/unit_type.h @@ -193,6 +193,7 @@ private: StoredResources storedResources; Levels levels; LootableResources lootableResources; + std::set tags; //meeting point bool meetingPoint; @@ -265,6 +266,7 @@ public: inline const Resource *getStoredResource(int i) const {return &storedResources[i];} int getLootableResourceCount() const {return lootableResources.size();} inline const LootableResource getLootableResource(int i) const {return lootableResources.at(i);} + const set &getTags() const {return tags;} bool getCellMapCell(int x, int y, CardinalDir facing) const; inline bool getMeetingPoint() const {return meetingPoint;} inline bool getCountUnitDeathInStats() const {return countUnitDeathInStats;} @@ -329,6 +331,17 @@ private: void computeFirstCtOfClass(); }; +/** + * Used to sort UnitType. Sorts by *translated* unit name. Sorting is case sensitive and done in + * lexical order. + */ +struct UnitTypeSorter +{ + bool operator()( const UnitType *left, const UnitType *right ) const { + return left->getName(true) < right->getName(true); + } +}; + }}//end namespace diff --git a/source/glest_game/types/upgrade_type.cpp b/source/glest_game/types/upgrade_type.cpp index b47a97ce..2c5a6e2f 100644 --- a/source/glest_game/types/upgrade_type.cpp +++ b/source/glest_game/types/upgrade_type.cpp @@ -601,18 +601,43 @@ string UpgradeType::getName(bool translatedValue) const { return lang.getTechTreeString("UpgradeTypeName_" + name,name.c_str()); } +string UpgradeType::getTagName(string tag, bool translatedValue) const { + if(translatedValue == false) return tag; + + Lang &lang = Lang::getInstance(); + return lang.getTechTreeString("TagName_" + tag, tag.c_str()); +} + string UpgradeType::getReqDesc(bool translatedValue) const{ Lang &lang= Lang::getInstance(); string str= ProducibleType::getReqDesc(translatedValue); string indent=" "; - if(getEffectCount()>0){ + if(!effects.empty() || !tags.empty()){ str+= "\n"+ lang.getString("Upgrades",(translatedValue == true ? "" : "english"))+"\n"; } str+=UpgradeTypeBase::getDesc(translatedValue); - if(getEffectCount()>0){ + if(!effects.empty() || !tags.empty()){ str+= lang.getString("AffectedUnits",(translatedValue == true ? "" : "english"))+"\n"; - for(int i=0; igetName(translatedValue)+"\n"; + + // We want the output to be sorted, so convert the set to a vector and sort that + std::vector outputUnits(effects.begin(), effects.end()); + std::sort(outputUnits.begin(), outputUnits.end(), UnitTypeSorter()); + + vector::iterator unitIter; + for (unitIter = outputUnits.begin(); unitIter != outputUnits.end(); ++unitIter) { + const UnitType *unit = *unitIter; + str+= indent+unit->getName(translatedValue)+"\n"; + } + + // Do the same for tags + std::vector outputTags(tags.begin(), tags.end()); + std::sort(outputTags.begin(), outputTags.end()); + + vector::iterator tagIter; + for (tagIter = outputTags.begin(); tagIter != outputTags.end(); ++tagIter) { + string tag = *tagIter; + str+= indent + lang.getString("TagDesc", (translatedValue == true ? "" : "english")) + + " " + getTagName(tag,translatedValue) + "\n"; } } return str; @@ -757,29 +782,24 @@ void UpgradeType::load(const string &dir, const TechTree *techTree, sortedItems.clear(); hasDup = false; - //effects + //effects -- get list of affected units const XmlNode *effectsNode= upgradeNode->getChild("effects"); - for(int i = 0; i < (int)effectsNode->getChildCount(); ++i) { - const XmlNode *unitNode= effectsNode->getChild("unit", i); + vector unitNodes= effectsNode->getChildList("unit"); + for(size_t i = 0; i < unitNodes.size(); ++i) { + const XmlNode *unitNode= unitNodes.at(i); string name= unitNode->getAttribute("name")->getRestrictedValue(); - if(sortedItems.find(name) != sortedItems.end()) { - hasDup = true; - } - - sortedItems[name] = 0; + effects.insert(factionType->getUnitType(name)); } - if(hasDup) { - printf("WARNING, upgrade type [%s] has one or more duplicate effects\n",this->getName().c_str()); + //effects -- convert tags into units + vector tagNodes= effectsNode->getChildList("tag"); + for(size_t i = 0; i < tagNodes.size(); ++i) { + const XmlNode *tagNode= tagNodes.at(i); + string name= tagNode->getAttribute("name")->getRestrictedValue(); + tags.insert(name); } - for(std::map::iterator iterMap = sortedItems.begin(); - iterMap != sortedItems.end(); ++iterMap) { - effects.push_back(factionType->getUnitType(iterMap->first)); - } - sortedItems.clear(); - //values UpgradeTypeBase::load(upgradeNode,name); } @@ -792,7 +812,15 @@ void UpgradeType::load(const string &dir, const TechTree *techTree, } bool UpgradeType::isAffected(const UnitType *unitType) const{ - return find(effects.begin(), effects.end(), unitType)!=effects.end(); + if(std::find(effects.begin(), effects.end(), unitType)!=effects.end()) return true; + + const set unitTags = unitType->getTags(); + set intersect; + set_intersection(tags.begin(),tags.end(),unitTags.begin(),unitTags.end(), + std::inserter(intersect,intersect.begin())); + if(!intersect.empty()) return true; + + return false; } //void UpgradeType::saveGame(XmlNode *rootNode) const { diff --git a/source/glest_game/types/upgrade_type.h b/source/glest_game/types/upgrade_type.h index 5d8fa483..5db3218d 100644 --- a/source/glest_game/types/upgrade_type.h +++ b/source/glest_game/types/upgrade_type.h @@ -30,6 +30,7 @@ #include "conversion.h" #include "xml_parser.h" #include "leak_dumper.h" +#include using Shared::Util::Checksum; using namespace Shared::Util; @@ -266,9 +267,10 @@ public: class UpgradeType: public UpgradeTypeBase, public ProducibleType { private: /** - * List of unit types (the "classes" of units, eg, swordman) that are affected by this upgrade. - */ - vector effects; + * Set of unit types (the "classes" of units, eg, swordman) that are affected by this upgrade. + */ + std::set effects; + std::set tags; public: /** @@ -301,17 +303,7 @@ public: * appears in the XMLs. */ virtual string getName(bool translatedValue=false) const; - - /** - * Returns the number of UnitTypes affected by this upgrade. - */ - int getEffectCount() const {return (int)effects.size();} - - /** - * Returns a particular unit type affected by this upgrade. - * @param i Index of the unit type in the #effects list. - */ - const UnitType * getEffect(int i) const {return effects[i];} + string getTagName(string tag, bool translatedValue=false) const; /** * Determines if a unit is affected by this upgrade.