From fe151abcc54bba4e3ab6b940cc446a64f759a64f Mon Sep 17 00:00:00 2001 From: Mark Vejvoda Date: Sat, 25 May 2013 07:36:21 +0000 Subject: [PATCH] ensure dead units clean up --- source/glest_game/type_instances/unit.cpp | 57 ++++++++++------------- source/glest_game/type_instances/unit.h | 2 +- 2 files changed, 26 insertions(+), 33 deletions(-) diff --git a/source/glest_game/type_instances/unit.cpp b/source/glest_game/type_instances/unit.cpp index 3c3bb7c7..319d42d0 100644 --- a/source/glest_game/type_instances/unit.cpp +++ b/source/glest_game/type_instances/unit.cpp @@ -1327,14 +1327,14 @@ Vec3f Unit::getCurrVector() const{ throw megaglest_runtime_error(szBuf); } - return getCurrVectorFlat() + Vec3f(0.f, type->getHeight()/2.f, 0.f); + return getCurrVectorFlat() + Vec3f(0.f, truncateDecimal(type->getHeight()/2.f), 0.f); } Vec3f Unit::getCurrVectorFlat() const{ return getVectorFlat(lastPos, pos); } -float Unit::getProgessAsFloat() const { +float Unit::getProgressAsFloat() const { float result = (static_cast(progress) / 100.f); result = truncateDecimal(result); return result; @@ -1347,17 +1347,17 @@ Vec3f Unit::getVectorFlat(const Vec2i &lastPosValue, const Vec2i &curPosValue) c float y2= computeHeight(curPosValue); if(currSkill->getClass() == scMove) { - v.x= lastPosValue.x + getProgessAsFloat() * (curPosValue.x - lastPosValue.x); - v.z= lastPosValue.y + getProgessAsFloat() * (curPosValue.y - lastPosValue.y); - v.y= y1 + getProgessAsFloat() * (y2-y1); + v.x= truncateDecimal(lastPosValue.x + getProgressAsFloat() * (curPosValue.x - lastPosValue.x)); + v.z= truncateDecimal(lastPosValue.y + getProgressAsFloat() * (curPosValue.y - lastPosValue.y)); + v.y= truncateDecimal(y1 + getProgressAsFloat() * (y2-y1)); } else { v.x= static_cast(curPosValue.x); v.z= static_cast(curPosValue.y); v.y= y2; } - v.x += type->getSize() / 2.f - 0.5f; - v.z += type->getSize() / 2.f - 0.5f; + v.x += truncateDecimal(type->getSize() / 2.f - 0.5f); + v.z += truncateDecimal(type->getSize() / 2.f - 0.5f); return v; } @@ -1900,24 +1900,19 @@ bool Unit::needToUpdate() { int Unit::getDiagonalFactor() { //speed modifier - //float diagonalFactor= 1.f; int diagonalFactor= 100; if(currSkill->getClass() == scMove) { //if moving in diagonal move slower Vec2i dest= pos - lastPos; if(abs(dest.x) + abs(dest.y) == 2) { diagonalFactor = 71; - //diagonalFactor = truncateDecimal(diagonalFactor); } } - return diagonalFactor; } int Unit::getHeightFactor() { - //speed modifier - //float heightFactor= 1.f; - float heightFactor= 100; + int heightFactor = 100; if(currSkill->getClass() == scMove) { //if moving to an higher cell move slower else move faster Cell *unitCell = map->getCell(pos); @@ -1930,9 +1925,6 @@ int Unit::getHeightFactor() { throw megaglest_runtime_error("targetCell == NULL"); } -// float heightDiff= unitCell->getHeight() - targetCell->getHeight(); -// heightFactor= clamp(1.f + heightDiff / 5.f, 0.2f, 5.f); -// heightFactor = truncateDecimal(heightFactor); int heightDiff= ((unitCell->getHeight() * 100.f) - (targetCell->getHeight() * 100.f)); heightFactor= clamp(100 + heightDiff / 500, 20, 500); } @@ -1941,19 +1933,21 @@ int Unit::getHeightFactor() { } int Unit::getSpeedDenominator(int updateFPS) { - //float speedDenominator = truncateDecimal(speedDivider * updateFPS); int speedDenominator = (speedDivider * updateFPS) * 100; return speedDenominator; } int Unit::getUpdatedProgress(int currentProgress, int updateFPS, int speed, int diagonalFactor, int heightFactor) { - //float speedDenominator = getSpeedDenominator(updateFPS); int speedDenominator = getSpeedDenominator(updateFPS); - //float newProgress = truncateDecimal(currentProgress); int newProgress = currentProgress; - //newProgress += truncateDecimal ((speed * diagonalFactor * heightFactor) / speedDenominator); - newProgress += ((speed * diagonalFactor * heightFactor) / speedDenominator); + int progressIncrease = ((speed * diagonalFactor * heightFactor) / speedDenominator); + // Ensure we increment at least a value of 1 of the action will be stuck infinitely + if(speed > 0 && diagonalFactor > 0 && heightFactor > 0 && progressIncrease == 0) { + progressIncrease = 1; + } + newProgress += progressIncrease; + return newProgress; } @@ -1987,9 +1981,8 @@ bool Unit::update() { lastAnimProgress= animProgress; const Game *game = Renderer::getInstance().getGame(); - //float speedDenominator = (speedDivider * game->getWorld()->getUpdateFps(this->getFactionIndex())); - //progress += (speed * diagonalFactor * heightFactor) / speedDenominator; - progress = getUpdatedProgress(progress, game->getWorld()->getUpdateFps(this->getFactionIndex()), + progress = getUpdatedProgress(progress, + game->getWorld()->getUpdateFps(this->getFactionIndex()), speed, diagonalFactor, heightFactor); //printf("Test progress = %d for unit [%d - %s]\n",progress,id,getType()->getName().c_str()); @@ -2008,17 +2001,17 @@ bool Unit::update() { if(currSkill->getClass() == scMorph) { targetProgress = this->getProgressRatio(); } - if(animProgressgetWorld()->getUpdateFps(this->getFactionIndex())); - int speedDenominator = getSpeedDenominator(game->getWorld()->getUpdateFps(this->getFactionIndex())); + float speedDenominator = static_cast(speedDivider) * + game->getWorld()->getUpdateFps(this->getFactionIndex()); animProgress += (currSkill->getAnimSpeed() * (truncateDecimal(static_cast(heightFactor) / 100.f))) / - (truncateDecimal(static_cast(speedDenominator) / 100.f)); + (truncateDecimal(speedDenominator)); } //update target updateTarget(); @@ -2026,15 +2019,15 @@ bool Unit::update() { //rotation if(currSkill->getClass() != scStop) { const int rotFactor= 2; - if(getProgessAsFloat() < 1.f / rotFactor) { + if(getProgressAsFloat() < 1.f / rotFactor) { if(type->getFirstStOfClass(scMove)){ if(abs((int)(lastRotation-targetRotation)) < 180) rotation= lastRotation + (targetRotation - lastRotation) * - getProgessAsFloat() * rotFactor; + getProgressAsFloat() * rotFactor; else { float rotationTerm = targetRotation > lastRotation ? -360.f: +360.f; rotation = lastRotation + (targetRotation - lastRotation + rotationTerm) * - getProgessAsFloat() * rotFactor; + getProgressAsFloat() * rotFactor; } } } diff --git a/source/glest_game/type_instances/unit.h b/source/glest_game/type_instances/unit.h index 948f2e31..969b7d5f 100644 --- a/source/glest_game/type_instances/unit.h +++ b/source/glest_game/type_instances/unit.h @@ -725,7 +725,7 @@ public: std::string toString() const; bool needToUpdate(); - float getProgessAsFloat() const; + float getProgressAsFloat() const; int getUpdateProgress(); int getDiagonalFactor(); int getHeightFactor();