From 335749a15e7a471dbb939167d30f4aa893f34849 Mon Sep 17 00:00:00 2001 From: Mark Vejvoda Date: Tue, 28 Dec 2010 02:17:44 +0000 Subject: [PATCH] - updated hp regeneration method to look for negative hp regen values and if health ticks below 0 the unit dies --- source/glest_game/type_instances/unit.cpp | 41 ++++++++++++++++------- source/glest_game/type_instances/unit.h | 8 ++--- source/glest_game/world/map.cpp | 2 +- 3 files changed, 34 insertions(+), 17 deletions(-) diff --git a/source/glest_game/type_instances/unit.cpp b/source/glest_game/type_instances/unit.cpp index 94d06a24..3bbe51ee 100644 --- a/source/glest_game/type_instances/unit.cpp +++ b/source/glest_game/type_instances/unit.cpp @@ -25,6 +25,7 @@ #include "renderer.h" #include "game.h" #include "socket.h" +#include "sound_renderer.h" #include "leak_dumper.h" @@ -1112,23 +1113,39 @@ void Unit::tick() { } //regenerate hp - hp+= type->getHpRegeneration(); - if(hp>type->getTotalMaxHp(&totalUpgrade)){ - hp= type->getTotalMaxHp(&totalUpgrade); + if(type->getHpRegeneration() >= 0) { + hp+= type->getHpRegeneration(); + if(hp>type->getTotalMaxHp(&totalUpgrade)){ + hp= type->getTotalMaxHp(&totalUpgrade); + } } + // If we have negative regeneration then check if the unit should die + else { + bool decHpResult = decHp(-type->getHpRegeneration()); + if(decHpResult) { + Unit::game->getWorld()->getStats()->die(getFactionIndex()); + game->getScriptManager()->onUnitDied(this); + } + StaticSound *sound= this->getType()->getFirstStOfClass(scDie)->getSound(); + if(sound != NULL && this->getFactionIndex() == Unit::game->getWorld()->getThisFactionIndex()) { + SoundRenderer::getInstance().playFx(sound); + } + } + //stop DamageParticles - if(hp>type->getTotalMaxHp(&totalUpgrade)/2 ){ + if(hp > type->getTotalMaxHp(&totalUpgrade) / 2) { stopDamageParticles(); } + //regenerate ep - ep+= type->getEpRegeneration(); + ep += type->getEpRegeneration(); if(ep>type->getTotalMaxEp(&totalUpgrade)){ ep= type->getTotalMaxEp(&totalUpgrade); } } } -int Unit::update2(){ +int Unit::update2() { progress2++; return progress2; } @@ -1185,12 +1202,12 @@ bool Unit::repair(){ } //decrements HP and returns if dead -bool Unit::decHp(int i){ - if(hp==0){ +bool Unit::decHp(int i) { + if(hp == 0) { return false; } - hp-=i; + hp -= i; if(type == NULL) { char szBuf[4096]=""; @@ -1199,12 +1216,12 @@ bool Unit::decHp(int i){ } //startDamageParticles - if(hpgetMaxHp()/2 ){ + if(hp < type->getMaxHp() / 2 ) { startDamageParticles(); } //stop DamageParticles on death - if(hp<=0){ + if(hp <= 0) { alive= false; hp=0; stopDamageParticles(); @@ -1213,7 +1230,7 @@ bool Unit::decHp(int i){ return false; } -string Unit::getDesc() const{ +string Unit::getDesc() const { Lang &lang= Lang::getInstance(); diff --git a/source/glest_game/type_instances/unit.h b/source/glest_game/type_instances/unit.h index 0661ad41..060c121a 100644 --- a/source/glest_game/type_instances/unit.h +++ b/source/glest_game/type_instances/unit.h @@ -151,7 +151,7 @@ public: // ===================================================== // class UnitPath // ===================================================== -/** Holds the next cells of a Unit movement +/** Holds the next cells of a Unit movement * @extends std::list */ class UnitPath : public list, public UnitPathInterface { @@ -175,14 +175,14 @@ public: bool empty() const {return list::empty();} /**< is path empty */ virtual void add(const Vec2i &pos) { push_front(pos);} /**< push onto front of path */ - + #if 0 // old style, to work with original PathFinder - Vec2i peek() {return back();} /**< peek at the next position */ + Vec2i peek() {return back();} /**< peek at the next position */ void pop() {this->pop_back();} /**< pop the next position off the path */ #else // new style, for the new RoutePlanner - Vec2i peek() {return front();} /**< peek at the next position */ + Vec2i peek() {return front();} /**< peek at the next position */ //virtual Vec2i pop() { Vec2i p= front(); erase(begin()); return p; } /**< pop the next position off the path */ void pop() { erase(begin()); } /**< pop the next position off the path */ #endif diff --git a/source/glest_game/world/map.cpp b/source/glest_game/world/map.cpp index 33aed314..86caaebf 100644 --- a/source/glest_game/world/map.cpp +++ b/source/glest_game/world/map.cpp @@ -747,7 +747,7 @@ Vec2i Map::findBestBuildApproach(const Unit *unit, Vec2i originalBuildPos,const Vec2i end = pos + Vec2i(ut->getSize()); for(int i = start.x; i <= end.x; ++i) { - for(int j = start.y; j <= end.y; ++j){ + for(int j = start.y; j <= end.y; ++j) { Vec2i testPos(i,j); if(isInUnitTypeCells(ut, originalBuildPos,testPos) == false) { float distance = unitBuilderPos.dist(testPos);