From 0a68e3a6a490e7fb7033df6c7224dae3e2cded43 Mon Sep 17 00:00:00 2001 From: Mike Hoffert Date: Sat, 19 Jul 2014 10:57:34 -0600 Subject: [PATCH] Implemented starting value for HP Same syntax as for EP: Where `start-percentage` can be replaced by `start-value` to use an absolute number. Note that unlike EP, HP defaults to the max-hp value *before* upgrades are applied. This behavior is not changed. To make units spawn with their fully upgraded HP, set their start-percentage to 1.0. --- source/glest_game/type_instances/unit.cpp | 12 ++++++++- source/glest_game/types/unit_type.cpp | 33 +++++++++++++++++++++++ source/glest_game/types/unit_type.h | 6 +++++ 3 files changed, 50 insertions(+), 1 deletion(-) diff --git a/source/glest_game/type_instances/unit.cpp b/source/glest_game/type_instances/unit.cpp index 70258b7c..9ba9d44b 100644 --- a/source/glest_game/type_instances/unit.cpp +++ b/source/glest_game/type_instances/unit.cpp @@ -1951,7 +1951,17 @@ void Unit::born(const CommandType *ct) { checkItemInVault(&this->hp,this->hp); int original_hp = this->hp; - this->hp= type->getMaxHp(); + + + //set hp from start hp + checkItemInVault(&this->ep,this->ep); + if(type->getStartHpType() == UnitType::stValue) { + this->hp= type->getStartHpValue(); + } + else { + this->hp= type->getStartHpPercentage() * type->getTotalMaxHp(&totalUpgrade); + } + if(original_hp != this->hp) { //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); game->getScriptManager()->onUnitTriggerEvent(this,utet_HPChanged); diff --git a/source/glest_game/types/unit_type.cpp b/source/glest_game/types/unit_type.cpp index 960b19c5..c8e0be10 100644 --- a/source/glest_game/types/unit_type.cpp +++ b/source/glest_game/types/unit_type.cpp @@ -125,6 +125,9 @@ UnitType::UnitType() : ProducibleType() { epRegeneration= 0; maxUnitCount= 0; maxHp=0; + startHpValue=0; + startHpPercentage=1.0; + startHpType=stValue; maxEp=0; startEpValue=0; startEpPercentage=0; @@ -250,6 +253,36 @@ void UnitType::loaddd(int id,const string &dir, const TechTree *techTree, } addItemToVault(&(this->epRegeneration),this->epRegeneration); + // Check that we don't use both start-value and start-percentage, as they are mutually + // exclusive + if(parametersNode->getChild("max-hp")->hasAttribute("start-value") && + parametersNode->getChild("max-hp")->hasAttribute("start-percentage")) { + throw megaglest_runtime_error("Unit " + name + + " has both start-value and start-percentage for HP", validationMode); + } + + //startHpValue -- the *absolute* value to use for starting HP + if(parametersNode->getChild("max-hp")->hasAttribute("start-value")) { + //checkItemInVault(&(this->startEp),this->startEp); + startHpValue= parametersNode->getChild("max-hp")->getAttribute("start-value")->getIntValue(); + startHpType= stValue; + } + addItemToVault(&(this->startHpValue),this->startHpValue); + + //startHpPercentage -- the *relative* value to use for starting HP + if(parametersNode->getChild("max-hp")->hasAttribute("start-percentage")) { + startHpPercentage= parametersNode->getChild("max-hp")->getAttribute("start-percentage")->getFloatValue(); + startHpType= stPercentage; + } + + // No start value set; use max HP before upgrades + if(!parametersNode->getChild("max-hp")->hasAttribute("start-value") && + !parametersNode->getChild("max-hp")->hasAttribute("start-percentage")) { + startHpValue= parametersNode->getChild("max-hp")->getAttribute("value")->getIntValue(); + startHpType= stValue; + } + addItemToVault(&(this->startHpPercentage),this->startHpPercentage); + // Check that we don't use both start-value and start-percentage, as they are mutually // exclusive if(parametersNode->getChild("max-ep")->hasAttribute("start-value") && diff --git a/source/glest_game/types/unit_type.h b/source/glest_game/types/unit_type.h index 5f2a4cd9..d623eec0 100644 --- a/source/glest_game/types/unit_type.h +++ b/source/glest_game/types/unit_type.h @@ -109,6 +109,9 @@ private: //basic int id; int maxHp; + int startHpValue; + double startHpPercentage; + StartType startHpType; int hpRegeneration; int maxEp; int startEpValue; @@ -186,6 +189,9 @@ public: inline int getId() const {return id;} inline int getMaxHp() const {return maxHp;} inline int getHpRegeneration() const {return hpRegeneration;} + inline int getStartHpValue() const {return startHpValue;} + inline double getStartHpPercentage() const {return startHpPercentage;} + inline StartType getStartHpType() const {return startHpType;} inline int getMaxEp() const {return maxEp;} inline int getEpRegeneration() const {return epRegeneration;} inline int getStartEpValue() const {return startEpValue;}