From 8e281e2faf73bab45a08bfd1e511e40bb0950693 Mon Sep 17 00:00:00 2001 From: Titus Tscharntke Date: Sun, 22 Aug 2010 21:09:35 +0000 Subject: [PATCH] new attribute MaxUnitCount in units and yellow select circle for allies --- source/glest_game/ai/node_pool.cpp | 2 +- source/glest_game/graphics/renderer.cpp | 3 + source/glest_game/type_instances/faction.cpp | 32 ++++- source/glest_game/type_instances/faction.h | 2 + source/glest_game/type_instances/unit.cpp | 27 ++++- source/glest_game/type_instances/unit.h | 2 + source/glest_game/types/command_type.cpp | 2 +- source/glest_game/types/element_type.h | 117 +++++++++++++++++++ source/glest_game/types/unit_type.cpp | 21 ++++ source/glest_game/types/unit_type.h | 6 + 10 files changed, 208 insertions(+), 6 deletions(-) create mode 100644 source/glest_game/types/element_type.h diff --git a/source/glest_game/ai/node_pool.cpp b/source/glest_game/ai/node_pool.cpp index e8042cd8..62b74e3e 100644 --- a/source/glest_game/ai/node_pool.cpp +++ b/source/glest_game/ai/node_pool.cpp @@ -131,4 +131,4 @@ list* NodePool::getClosedNodes() { #endif // _GAE_DEBUG_EDITION_ -}} \ No newline at end of file +}} diff --git a/source/glest_game/graphics/renderer.cpp b/source/glest_game/graphics/renderer.cpp index d76a6700..15334203 100644 --- a/source/glest_game/graphics/renderer.cpp +++ b/source/glest_game/graphics/renderer.cpp @@ -1758,6 +1758,9 @@ void Renderer::renderSelectionEffects(){ if(world->getThisFactionIndex()==unit->getFactionIndex()){ glColor4f(0, unit->getHpRatio(), 0, 0.3f); } + else if ( world->getThisTeamIndex()==unit->getTeam()){ + glColor4f(unit->getHpRatio(), unit->getHpRatio(), 0, 0.3f); + } else{ glColor4f(unit->getHpRatio(), 0, 0, 0.3f); } diff --git a/source/glest_game/type_instances/faction.cpp b/source/glest_game/type_instances/faction.cpp index f3d25b9a..02a55074 100644 --- a/source/glest_game/type_instances/faction.cpp +++ b/source/glest_game/type_instances/faction.cpp @@ -133,7 +133,7 @@ void Faction::finishUpgrade(const UpgradeType *ut){ // ==================== reqs ==================== -//checks if all required units and upgrades are present +//checks if all required units and upgrades are present and maxUnitCount is within limit bool Faction::reqsOk(const RequirableType *rt) const{ assert(rt != NULL); //required units @@ -158,10 +158,38 @@ bool Faction::reqsOk(const RequirableType *rt) const{ return false; } } + + if(dynamic_cast(rt) != NULL ){ + const CommandType *ct=(CommandType *) rt; + if(ct->getProduced() != NULL && dynamic_cast(ct->getProduced()) != NULL ){ + + const UnitType *producedUnitType= (UnitType *) ct->getProduced(); + if(producedUnitType->getMaxUnitCount()>0){ + if(producedUnitType->getMaxUnitCount()<=getCountForMaxUnitCount(producedUnitType)){ + return false; + } + } + } + } return true; - } +int Faction::getCountForMaxUnitCount(const UnitType *unitType) const{ + int count=0; + //calculate current unit count + for(int j=0; jgetType(); + if(unitType==currentUt && unit->isOperative()){ + count++; + } + //check if there is any command active which already produces this unit + count=count+unit->getCountOfProducedUnits(unitType); + } + return count; +} + + bool Faction::reqsOk(const CommandType *ct) const{ assert(ct != NULL); if(ct->getProduced()!=NULL && !reqsOk(ct->getProduced())){ diff --git a/source/glest_game/type_instances/faction.h b/source/glest_game/type_instances/faction.h index 32ecc3ef..f3790e73 100644 --- a/source/glest_game/type_instances/faction.h +++ b/source/glest_game/type_instances/faction.h @@ -122,6 +122,8 @@ public: //reqs bool reqsOk(const RequirableType *rt) const; bool reqsOk(const CommandType *ct) const; + int getCountForMaxUnitCount(const UnitType *unitType) const; + //diplomacy bool isAlly(const Faction *faction); diff --git a/source/glest_game/type_instances/unit.cpp b/source/glest_game/type_instances/unit.cpp index 373a1151..8a79f25e 100644 --- a/source/glest_game/type_instances/unit.cpp +++ b/source/glest_game/type_instances/unit.cpp @@ -648,6 +648,22 @@ unsigned int Unit::getCommandSize() const{ return commands.size(); } +//return current command, assert that there is always one command +int Unit::getCountOfProducedUnits(const UnitType *ut) const{ + int count=0; + for(Commands::const_iterator it= commands.begin(); it!=commands.end(); ++it){ + const CommandType* ct=(*it)->getCommandType(); + if(ct->getClass()==ccProduce || ct->getClass()==ccMorph || ct->getClass()==ccBuild ){ + const UnitType *producedUnitType= static_cast(ct->getProduced()); + if(producedUnitType==ut) + { + count++; + } + } + } + return count; +} + #define deleteSingleCommand(command) {\ undoCommand(command);\ delete command;\ @@ -1097,7 +1113,14 @@ string Unit::getDesc() const{ //str+="Pos: "+v2iToStr(pos)+"\n"; //hp - string str= "\n" + lang.get("Hp")+ ": " + intToStr(hp) + "/" + intToStr(type->getTotalMaxHp(&totalUpgrade)); + string str= "\n"; + + //maxUnitCount + if(type->getMaxUnitCount()>0){ + str += lang.get("MaxUnitCount")+ ": " + intToStr(faction->getCountForMaxUnitCount(type)) + "/" + intToStr(type->getMaxUnitCount()); + } + + str += "\n"+lang.get("Hp")+ ": " + intToStr(hp) + "/" + intToStr(type->getTotalMaxHp(&totalUpgrade)); if(type->getHpRegeneration()!=0){ str+= " (" + lang.get("Regeneration") + ": " + intToStr(type->getHpRegeneration()) + ")"; } @@ -1336,7 +1359,7 @@ CommandResult Unit::checkCommand(Command *command) const{ } if(!faction->checkCosts(builtUnit)){ return crFailRes; - } + } } //upgrade command specific, check that upgrade is not upgraded diff --git a/source/glest_game/type_instances/unit.h b/source/glest_game/type_instances/unit.h index 9ed7edb4..7f8731cc 100644 --- a/source/glest_game/type_instances/unit.h +++ b/source/glest_game/type_instances/unit.h @@ -49,6 +49,7 @@ enum CommandResult{ crSuccess, crFailRes, crFailReqs, + crFailUnitCount, crFailUndefined, crSomeFailed }; @@ -342,6 +343,7 @@ public: //command related bool anyCommand() const; Command *getCurrCommand() const; + int getCountOfProducedUnits(const UnitType *ut) const; unsigned int getCommandSize() const; CommandResult giveCommand(Command *command, bool tryQueue = false); //give a command CommandResult finishCommand(); //command finished diff --git a/source/glest_game/types/command_type.cpp b/source/glest_game/types/command_type.cpp index b8404588..de73b919 100644 --- a/source/glest_game/types/command_type.cpp +++ b/source/glest_game/types/command_type.cpp @@ -601,7 +601,7 @@ string ProduceCommandType::getDesc(const TotalUpgrade *totalUpgrade) const{ } str+= "\n" + getProducedUnit()->getReqDesc(); - + return str; } diff --git a/source/glest_game/types/element_type.h b/source/glest_game/types/element_type.h new file mode 100644 index 00000000..5011d4c7 --- /dev/null +++ b/source/glest_game/types/element_type.h @@ -0,0 +1,117 @@ +// ============================================================== +// This file is part of Glest (www.glest.org) +// +// Copyright (C) 2001-2008 Marti�o Figueroa +// +// You can redistribute this code and/or modify it under +// the terms of the GNU General Public License as published +// by the Free Software Foundation; either version 2 of the +// License, or (at your option) any later version +// ============================================================== + +#ifndef _GLEST_GAME_ELEMENTTYPE_H_ +#define _GLEST_GAME_ELEMENTTYPE_H_ + +#include +#include + +#include "texture.h" +#include "resource.h" + +using std::vector; +using std::string; + +using Shared::Graphics::Texture2D; + +namespace Glest{ namespace Game{ + +class UpgradeType; +class TechTree; +class UnitType; +class UpgradeType; +class DisplayableType; +class ResourceType; + +// ===================================================== +// class DisplayableType +// +/// Base class for anything that has a name and a portrait +// ===================================================== + +class DisplayableType{ +protected: + string name; //name + Texture2D *image; //portrait + +public: + DisplayableType(); + virtual ~DisplayableType(){}; + + //get + string getName() const {return name;} + const Texture2D *getImage() const {return image;} +}; + + +// ===================================================== +// class RequirableType +// +/// Base class for anything that has requirements +// ===================================================== + +class RequirableType: public DisplayableType{ +private: + typedef vector UnitReqs; + typedef vector UpgradeReqs; + +protected: + UnitReqs unitReqs; //needed units + UpgradeReqs upgradeReqs; //needed upgrades + +public: + //get + int getUpgradeReqCount() const {return upgradeReqs.size();} + int getUnitReqCount() const {return unitReqs.size();} + const UpgradeType *getUpgradeReq(int i) const {return upgradeReqs[i];} + const UnitType *getUnitReq(int i) const {return unitReqs[i];} + + //other + virtual string getReqDesc() const; +}; + + +// ===================================================== +// class ProducibleType +// +/// Base class for anything that can be produced +// ===================================================== + +class ProducibleType: public RequirableType{ +private: + typedef vector Costs; + +protected: + Costs costs; + Texture2D *cancelImage; + int productionTime; + +public: + ProducibleType(); + virtual ~ProducibleType(); + + //get + int getCostCount() const {return costs.size();} + const Resource *getCost(int i) const {return &costs[i];} + const Resource *getCost(const ResourceType *rt) const; + int getProductionTime() const {return productionTime;} + const Texture2D *getCancelImage() const {return cancelImage;} + + //varios + void checkCostStrings(TechTree *techTree); + + virtual string getReqDesc() const; +}; + +}}//end namespace + +#endif diff --git a/source/glest_game/types/unit_type.cpp b/source/glest_game/types/unit_type.cpp index ea5d61ee..feddaf13 100644 --- a/source/glest_game/types/unit_type.cpp +++ b/source/glest_game/types/unit_type.cpp @@ -77,6 +77,7 @@ UnitType::UnitType(){ cellMap= NULL; hpRegeneration= 0; epRegeneration= 0; + maxUnitCount= 0; } UnitType::~UnitType(){ @@ -137,6 +138,11 @@ void UnitType::load(int id,const string &dir, const TechTree *techTree, const Fa epRegeneration= parametersNode->getChild("max-ep")->getAttribute("regeneration")->getIntValue(); } + //maxUnitCount + if(parametersNode->hasChild("max-unit-count")){ + maxUnitCount= parametersNode->getChild("max-unit-count")->getAttribute("value")->getIntValue(); + } + //armor armor= parametersNode->getChild("armor")->getAttribute("value")->getIntValue(); @@ -629,6 +635,19 @@ string UnitType::getCommandTypeListDesc() const { } +string UnitType::getReqDesc() const{ + Lang &lang= Lang::getInstance(); + string desc = "Limits: "; + string resultTxt=""; + if(getMaxUnitCount()>0){ + resultTxt+="\n"+lang.get("MaxUnitCount")+" "+intToStr(getMaxUnitCount()); + } + if(resultTxt=="") + return RequirableType::getReqDesc(); + else + return RequirableType::getReqDesc()+"\nLimits: "+resultTxt; +} + std::string UnitType::toString() const { std::string result = ""; @@ -637,6 +656,8 @@ std::string UnitType::toString() const { result += " hpRegeneration = " + intToStr(hpRegeneration); result += " maxEp = " + intToStr(maxEp); result += " epRegeneration = " + intToStr(epRegeneration); + result += " maxUnitCount = " + intToStr(getMaxUnitCount()); + for(int i = 0; i < fieldCount; i++) { result += " fields index = " + intToStr(i) + " value = " + intToStr(fields[i]); diff --git a/source/glest_game/types/unit_type.h b/source/glest_game/types/unit_type.h index 52ba1cce..7aba13b7 100644 --- a/source/glest_game/types/unit_type.h +++ b/source/glest_game/types/unit_type.h @@ -86,6 +86,8 @@ private: int hpRegeneration; int maxEp; int epRegeneration; + int maxUnitCount; + ///@todo remove fields, multiple fields are not supported by the engine bool fields[fieldCount]; //fields: land, sea or air @@ -137,6 +139,7 @@ public: int getHpRegeneration() const {return hpRegeneration;} int getMaxEp() const {return maxEp;} int getEpRegeneration() const {return epRegeneration;} + int getMaxUnitCount() const {return maxUnitCount;} bool getField(Field field) const {return fields[field];} Field getField() const {return field;} bool getProperty(Property property) const {return properties[property];} @@ -195,6 +198,9 @@ public: float getRotatedBuildPos() { return rotatedBuildPos; } void setRotatedBuildPos(float value) { rotatedBuildPos = value; } + //other + virtual string getReqDesc() const; + std::string toString() const; private: