diff --git a/source/glest_game/type_instances/faction.cpp b/source/glest_game/type_instances/faction.cpp index e12c60045..d0dcaafaa 100644 --- a/source/glest_game/type_instances/faction.cpp +++ b/source/glest_game/type_instances/faction.cpp @@ -1122,6 +1122,18 @@ namespace Glest { } } + //required resources + for (int i = 0; i < rt->getCostCount(); i++) { + const ResourceType *resource = rt->getCost(i)->getType(); + int cost = rt->getCost(i)->getAmount(); + if (cost > 0) { + int available = getResource(resource)->getAmount(); + if (cost > available) { + return false; + } + } + } + if (dynamic_cast (rt) != NULL) { const UnitType *producedUnitType = dynamic_cast (rt); diff --git a/source/glest_game/types/element_type.cpp b/source/glest_game/types/element_type.cpp index 37a358a0c..b6c48dfc5 100644 --- a/source/glest_game/types/element_type.cpp +++ b/source/glest_game/types/element_type.cpp @@ -56,38 +56,74 @@ namespace Glest { // ===================================================== string RequirableType::getReqDesc(bool translatedValue) const { - bool anyReqs = false; + return getReqDesc(false, translatedValue); + } - string reqString = ""; - for (int i = 0; i < getUnitReqCount(); ++i) { - if (getUnitReq(i) == NULL) { - throw megaglest_runtime_error("getUnitReq(i) == NULL"); + const Resource *RequirableType::getCost(const ResourceType * rt) const { + for (int i = 0; i < (int) costs.size(); ++i) { + if (costs[i].getType() == rt) { + return &costs[i]; + } + } + return NULL; + } + + + string RequirableType::getResourceReqDesc(bool lineBreaks, + bool translatedValue) const { + string str = ""; + for (int i = 0; i < getCostCount(); ++i) { + if (getCost(i)->getAmount() != 0) { + str += getCost(i)->getType()->getName(translatedValue); + str += ": " + intToStr(getCost(i)->getAmount()); + if (lineBreaks == true) { + str += "\n"; + } else { + str += " "; + } + } + } + + return str; + } + + string RequirableType::getUnitAndUpgradeReqDesc(bool lineBreaks, + bool translatedValue) + const { + string str = ""; + for (int i = 0; i < getUnitReqCount(); ++i) { + str += getUnitReq(i)->getName(translatedValue); + if (lineBreaks == true) { + str += "\n"; + } else { + str += " "; } - reqString += getUnitReq(i)->getName(translatedValue); - reqString += "\n"; - anyReqs = true; } for (int i = 0; i < getUpgradeReqCount(); ++i) { - if (getUpgradeReq(i) == NULL) { - throw megaglest_runtime_error("getUpgradeReq(i) == NULL"); + str += getUpgradeReq(i)->getName(translatedValue); + if (lineBreaks == true) { + str += "\n"; + } else { + str += " "; } - - reqString += getUpgradeReq(i)->getName(translatedValue); - reqString += "\n"; - anyReqs = true; } - string str = getName(translatedValue); - if (anyReqs) { - return str + " " + Lang::getInstance().getString("Reqs", - (translatedValue == - true ? "" : - "english")) + - ":\n" + reqString; - } else { - return str; + return str; + } + + string RequirableType::getReqDesc(bool ignoreResourceRequirements, bool translatedValue) const { + string str = + getName(translatedValue) + " " + + Lang::getInstance().getString("Reqs", + (translatedValue == + true ? "" : "english")) + ":\n"; + if (ignoreResourceRequirements == false) { + str += getResourceReqDesc(true, translatedValue); } + + str += getUnitAndUpgradeReqDesc(true, translatedValue); + return str; } //void RequirableType::saveGame(XmlNode *rootNode) const { @@ -124,77 +160,6 @@ namespace Glest { ProducibleType::~ProducibleType() { } - const Resource *ProducibleType::getCost(const ResourceType * rt) const { - for (int i = 0; i < (int) costs.size(); ++i) { - if (costs[i].getType() == rt) { - return &costs[i]; - } - } - return NULL; - } - - string ProducibleType::getReqDesc(bool translatedValue) const { - return getReqDesc(false, translatedValue); - } - - string ProducibleType::getResourceReqDesc(bool lineBreaks, - bool translatedValue) const { - string str = ""; - for (int i = 0; i < getCostCount(); ++i) { - if (getCost(i)->getAmount() != 0) { - str += getCost(i)->getType()->getName(translatedValue); - str += ": " + intToStr(getCost(i)->getAmount()); - if (lineBreaks == true) { - str += "\n"; - } else { - str += " "; - } - } - } - - return str; - } - - string ProducibleType::getUnitAndUpgradeReqDesc(bool lineBreaks, - bool translatedValue) - const { - string str = ""; - for (int i = 0; i < getUnitReqCount(); ++i) { - str += getUnitReq(i)->getName(translatedValue); - if (lineBreaks == true) { - str += "\n"; - } else { - str += " "; - } - } - - for (int i = 0; i < getUpgradeReqCount(); ++i) { - str += getUpgradeReq(i)->getName(translatedValue); - if (lineBreaks == true) { - str += "\n"; - } else { - str += " "; - } - } - - return str; - } - - string ProducibleType::getReqDesc(bool ignoreResourceRequirements, - bool translatedValue) const { - string str = - getName(translatedValue) + " " + - Lang::getInstance().getString("Reqs", - (translatedValue == - true ? "" : "english")) + ":\n"; - if (ignoreResourceRequirements == false) { - str += getResourceReqDesc(true, translatedValue); - } - - str += getUnitAndUpgradeReqDesc(true, translatedValue); - return str; - } - //void ProducibleType::saveGame(XmlNode *rootNode) const { // RequirableType::saveGame(rootNode); // diff --git a/source/glest_game/types/element_type.h b/source/glest_game/types/element_type.h index f0b80b8d0..33716f05f 100644 --- a/source/glest_game/types/element_type.h +++ b/source/glest_game/types/element_type.h @@ -74,10 +74,12 @@ namespace Glest { private: typedef vector < const UnitType *>UnitReqs; typedef vector < const UpgradeType *>UpgradeReqs; + typedef vector < Resource > Costs; protected: UnitReqs unitReqs; //needed units UpgradeReqs upgradeReqs; //needed upgrades + Costs costs; //needed costs public: //get @@ -93,10 +95,24 @@ namespace Glest { const UnitType *getUnitReq(int i) const { return unitReqs[i]; } + //get + int getCostCount() const { + return (int) costs.size(); + } + const Resource *getCost(int i) const { + return &costs[i]; + } + const Resource *getCost(const ResourceType * rt) const; //other virtual string getReqDesc(bool translatedValue) const; + string getResourceReqDesc(bool lineBreaks, bool translatedValue) const; + string getUnitAndUpgradeReqDesc(bool lineBreaks, + bool translatedValue) const; + string getReqDesc(bool ignoreResourceRequirements, + bool translatedValue) const; + //virtual void saveGame(XmlNode *rootNode) const; }; @@ -108,11 +124,7 @@ namespace Glest { // ===================================================== class ProducibleType :public RequirableType { - private: - typedef vector < Resource > Costs; - protected: - Costs costs; Texture2D *cancelImage; int productionTime; @@ -120,14 +132,6 @@ namespace Glest { ProducibleType(); virtual ~ProducibleType(); - //get - int getCostCount() const { - return (int) costs.size(); - } - const Resource *getCost(int i) const { - return &costs[i]; - } - const Resource *getCost(const ResourceType * rt) const; int getProductionTime() const { return productionTime; } @@ -135,16 +139,6 @@ namespace Glest { return cancelImage; } - //varios - void checkCostStrings(TechTree * techTree); - - virtual string getReqDesc(bool translatedValue) const; - string getResourceReqDesc(bool lineBreaks, bool translatedValue) const; - string getUnitAndUpgradeReqDesc(bool lineBreaks, - bool translatedValue) const; - string getReqDesc(bool ignoreResourceRequirements, - bool translatedValue) const; - // virtual void saveGame(XmlNode *rootNode) const; // void loadGame(const XmlNode *rootNode); };