bugfix for both progress and animation smoothness

This commit is contained in:
Mark Vejvoda
2013-06-03 14:30:52 +00:00
parent e4e7a7dc00
commit 25450ebb85
4 changed files with 70 additions and 39 deletions

View File

@@ -389,7 +389,7 @@ void UnitAttackBoostEffectOriginator::saveGame(XmlNode *rootNode) {
// ===================================================== // =====================================================
const float Unit::ANIMATION_SPEED_MULTIPLIER = 100000.f; const float Unit::ANIMATION_SPEED_MULTIPLIER = 100000.f;
const float Unit::PROGRESS_SPEED_MULTIPLIER = 100.f; const float Unit::PROGRESS_SPEED_MULTIPLIER = 100000.f;
const int Unit::speedDivider= 100; const int Unit::speedDivider= 100;
const int Unit::maxDeadCount= 1000; //time in until the corpse disapears - should be about 40 seconds const int Unit::maxDeadCount= 1000; //time in until the corpse disapears - should be about 40 seconds
@@ -1887,10 +1887,10 @@ const CommandType *Unit::computeCommandType(const Vec2i &pos, const Unit *target
return commandType; return commandType;
} }
int Unit::getUpdateProgress() { int64 Unit::getUpdateProgress() {
if(progress > PROGRESS_SPEED_MULTIPLIER) { if(progress > PROGRESS_SPEED_MULTIPLIER) {
char szBuf[8096]=""; char szBuf[8096]="";
snprintf(szBuf,8096,"In [%s::%s Line: %d] ERROR: progress > %f, progress = [%d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,PROGRESS_SPEED_MULTIPLIER,progress); snprintf(szBuf,8096,"In [%s::%s Line: %d] ERROR: progress > %f, progress = [" MG_I64_SPECIFIER "]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,PROGRESS_SPEED_MULTIPLIER,progress);
throw megaglest_runtime_error(szBuf); throw megaglest_runtime_error(szBuf);
} }
@@ -1900,7 +1900,7 @@ int Unit::getUpdateProgress() {
throw megaglest_runtime_error(szBuf); throw megaglest_runtime_error(szBuf);
} }
int newProgress = progress; int64 newProgress = progress;
if(currSkill->getClass() != scDie) { if(currSkill->getClass() != scDie) {
//speed //speed
int speed = currSkill->getTotalSpeed(&totalUpgrade); int speed = currSkill->getTotalSpeed(&totalUpgrade);
@@ -1910,8 +1910,8 @@ int Unit::getUpdateProgress() {
} }
//speed modifier //speed modifier
int diagonalFactor = getDiagonalFactor(); int64 diagonalFactor = getDiagonalFactor();
int heightFactor = getHeightFactor(PROGRESS_SPEED_MULTIPLIER); int64 heightFactor = getHeightFactor(PROGRESS_SPEED_MULTIPLIER);
//update progresses //update progresses
const Game *game = Renderer::getInstance().getGame(); const Game *game = Renderer::getInstance().getGame();
@@ -1922,8 +1922,7 @@ int Unit::getUpdateProgress() {
throw megaglest_runtime_error("game->getWorld() == NULL"); throw megaglest_runtime_error("game->getWorld() == NULL");
} }
newProgress = getUpdatedProgress(progress, newProgress = getUpdatedProgress(progress, GameConstants::updateFps,
game->getWorld()->getUpdateFps(this->getFactionIndex()),
speed, diagonalFactor, heightFactor); speed, diagonalFactor, heightFactor);
} }
@@ -1941,21 +1940,21 @@ bool Unit::needToUpdate() {
return return_value; return return_value;
} }
int Unit::getDiagonalFactor() { int64 Unit::getDiagonalFactor() {
//speed modifier //speed modifier
int diagonalFactor= PROGRESS_SPEED_MULTIPLIER; int64 diagonalFactor= PROGRESS_SPEED_MULTIPLIER;
if(currSkill->getClass() == scMove) { if(currSkill->getClass() == scMove) {
//if moving in diagonal move slower //if moving in diagonal move slower
Vec2i dest= pos - lastPos; Vec2i dest= pos - lastPos;
if(abs(dest.x) + abs(dest.y) == 2) { if(abs(dest.x) + abs(dest.y) == 2) {
diagonalFactor = 71; diagonalFactor = 0.71f * PROGRESS_SPEED_MULTIPLIER;
} }
} }
return diagonalFactor; return diagonalFactor;
} }
int Unit::getHeightFactor(float speedMultiplier) { int64 Unit::getHeightFactor(float speedMultiplier) {
int heightFactor = speedMultiplier; int64 heightFactor = speedMultiplier;
if(currSkill->getClass() == scMove) { if(currSkill->getClass() == scMove) {
//if moving to an higher cell move slower else move faster //if moving to an higher cell move slower else move faster
Cell *unitCell = map->getCell(pos); Cell *unitCell = map->getCell(pos);
@@ -1968,7 +1967,7 @@ int Unit::getHeightFactor(float speedMultiplier) {
throw megaglest_runtime_error("targetCell == NULL"); throw megaglest_runtime_error("targetCell == NULL");
} }
int heightDiff= (truncateDecimal<float>(unitCell->getHeight() * speedMultiplier,2) - int64 heightDiff= (truncateDecimal<float>(unitCell->getHeight() * speedMultiplier,2) -
truncateDecimal<float>(targetCell->getHeight() * speedMultiplier,2)); truncateDecimal<float>(targetCell->getHeight() * speedMultiplier,2));
heightFactor= clamp(speedMultiplier + heightDiff / (5.f * speedMultiplier), 0.2f * speedMultiplier, 5.f * speedMultiplier); heightFactor= clamp(speedMultiplier + heightDiff / (5.f * speedMultiplier), 0.2f * speedMultiplier, 5.f * speedMultiplier);
} }
@@ -1976,22 +1975,26 @@ int Unit::getHeightFactor(float speedMultiplier) {
return heightFactor; return heightFactor;
} }
int Unit::getSpeedDenominator(int updateFPS) { int64 Unit::getSpeedDenominator(int64 updateFPS) {
int speedDenominator = (speedDivider * updateFPS) * PROGRESS_SPEED_MULTIPLIER; int64 speedDenominator = (speedDivider * updateFPS) * PROGRESS_SPEED_MULTIPLIER;
return speedDenominator; return speedDenominator;
} }
int Unit::getUpdatedProgress(int currentProgress, int updateFPS, int speed, int64 Unit::getUpdatedProgress(int64 currentProgress, int64 updateFPS, int64 speed,
int diagonalFactor, int heightFactor) { int64 diagonalFactor, int64 heightFactor) {
int speedDenominator = getSpeedDenominator(updateFPS); int64 speedDenominator = getSpeedDenominator(updateFPS);
int newProgress = currentProgress; int64 newProgress = currentProgress;
int progressIncrease = ((speed * diagonalFactor * heightFactor) / speedDenominator); int64 progressIncrease = ((speed * diagonalFactor * heightFactor) / speedDenominator);
// Ensure we increment at least a value of 1 of the action will be stuck infinitely // 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) { if(speed > 0 && diagonalFactor > 0 && heightFactor > 0 && progressIncrease == 0) {
progressIncrease = 1; progressIncrease = 1;
} }
newProgress += progressIncrease; newProgress += progressIncrease;
// if(currSkill->getClass() == scMove || (currSkill->getClass() == scStop && this->loadCount > 0)) {
// printf("speedDenominator: " MG_I64_SPECIFIER " currentProgress: " MG_I64_SPECIFIER " speed: " MG_I64_SPECIFIER " diagonalFactor: " MG_I64_SPECIFIER " heightFactor: " MG_I64_SPECIFIER " progressIncrease: " MG_I64_SPECIFIER " newProgress: " MG_I64_SPECIFIER " TOP #: " MG_I64_SPECIFIER "\n",speedDenominator,currentProgress,speed,diagonalFactor,heightFactor,progressIncrease,newProgress,(speed * diagonalFactor * heightFactor));
// }
return newProgress; return newProgress;
} }
@@ -2246,15 +2249,15 @@ bool Unit::update() {
} }
//speed modifier //speed modifier
int diagonalFactor = getDiagonalFactor(); int64 diagonalFactor = getDiagonalFactor();
int heightFactor = getHeightFactor(PROGRESS_SPEED_MULTIPLIER); int64 heightFactor = getHeightFactor(PROGRESS_SPEED_MULTIPLIER);
//update progresses //update progresses
this->lastAnimProgress= this->animProgress; this->lastAnimProgress= this->animProgress;
const Game *game = Renderer::getInstance().getGame(); const Game *game = Renderer::getInstance().getGame();
progress = getUpdatedProgress(progress, progress = getUpdatedProgress(progress,
game->getWorld()->getUpdateFps(this->getFactionIndex()), GameConstants::updateFps,
speed, diagonalFactor, heightFactor); speed, diagonalFactor, heightFactor);
//printf("Test progress = %d for unit [%d - %s]\n",progress,id,getType()->getName().c_str()); //printf("Test progress = %d for unit [%d - %s]\n",progress,id,getType()->getName().c_str());
@@ -2295,10 +2298,10 @@ bool Unit::update() {
} }
} }
else { else {
int heightFactor = getHeightFactor(ANIMATION_SPEED_MULTIPLIER); int64 heightFactor = getHeightFactor(ANIMATION_SPEED_MULTIPLIER);
int speedDenominator = speedDivider * int64 speedDenominator = speedDivider *
game->getWorld()->getUpdateFps(this->getFactionIndex()); game->getWorld()->getUpdateFps(this->getFactionIndex());
int progressIncrease = (currSkill->getAnimSpeed() * heightFactor) / speedDenominator; int64 progressIncrease = (currSkill->getAnimSpeed() * heightFactor) / speedDenominator;
// Ensure we increment at least a value of 1 of the action will be stuck infinitely // Ensure we increment at least a value of 1 of the action will be stuck infinitely
if(currSkill->getAnimSpeed() > 0 && heightFactor > 0 && progressIncrease == 0) { if(currSkill->getAnimSpeed() > 0 && heightFactor > 0 && progressIncrease == 0) {
progressIncrease = 1; progressIncrease = 1;
@@ -3818,7 +3821,7 @@ void Unit::logSynchDataCommon(string file,int line,string source,bool threadedMo
if(SystemFlags::getSystemSettingType(SystemFlags::debugWorldSynch).enabled == true) { if(SystemFlags::getSystemSettingType(SystemFlags::debugWorldSynch).enabled == true) {
char szBuf[8096]=""; char szBuf[8096]="";
snprintf(szBuf,8096, snprintf(szBuf,8096,
"FrameCount [%d] Unit = %d [%s][%s] pos = %s, lastPos = %s, targetPos = %s, targetVec = %s, meetingPos = %s, progress [%d], progress2 [%d]\nUnit Path [%s]\n", "FrameCount [%d] Unit = %d [%s][%s] pos = %s, lastPos = %s, targetPos = %s, targetVec = %s, meetingPos = %s, progress [" MG_I64_SPECIFIER "], progress2 [%d]\nUnit Path [%s]\n",
getFrameCount(), getFrameCount(),
id, id,
getFullName().c_str(), getFullName().c_str(),
@@ -4478,11 +4481,28 @@ Unit * Unit::loadGame(const XmlNode *rootNode, GameSettings *settings, Faction *
// int deadCount; // int deadCount;
result->deadCount = unitNode->getAttribute("deadCount")->getIntValue(); result->deadCount = unitNode->getAttribute("deadCount")->getIntValue();
// float progress; //between 0 and 1 // float progress; //between 0 and 1
try {
result->progress = unitNode->getAttribute("progress")->getIntValue();
}
catch(const exception &ex) {
result->progress = unitNode->getAttribute("progress")->getFloatValue(); result->progress = unitNode->getAttribute("progress")->getFloatValue();
}
// float lastAnimProgress; //between 0 and 1 // float lastAnimProgress; //between 0 and 1
try {
result->lastAnimProgress = unitNode->getAttribute("lastAnimProgress")->getIntValue();
}
catch(const exception &ex) {
result->lastAnimProgress = unitNode->getAttribute("lastAnimProgress")->getFloatValue(); result->lastAnimProgress = unitNode->getAttribute("lastAnimProgress")->getFloatValue();
}
// float animProgress; //between 0 and 1 // float animProgress; //between 0 and 1
try {
result->animProgress = unitNode->getAttribute("animProgress")->getIntValue();
}
catch(const exception &ex) {
result->animProgress = unitNode->getAttribute("animProgress")->getFloatValue(); result->animProgress = unitNode->getAttribute("animProgress")->getFloatValue();
}
// float highlight; // float highlight;
result->highlight = unitNode->getAttribute("highlight")->getFloatValue(); result->highlight = unitNode->getAttribute("highlight")->getFloatValue();
// int progress2; // int progress2;

View File

@@ -349,9 +349,9 @@ private:
int loadCount; int loadCount;
int deadCount; int deadCount;
//float progress; //between 0 and 1 //float progress; //between 0 and 1
int progress; //between 0 and 1 int64 progress; //between 0 and 1
int lastAnimProgress; //between 0 and 1 int64 lastAnimProgress; //between 0 and 1
int animProgress; //between 0 and 1 int64 animProgress; //between 0 and 1
float highlight; float highlight;
int progress2; int progress2;
int kills; int kills;
@@ -734,10 +734,10 @@ public:
std::string toString() const; std::string toString() const;
bool needToUpdate(); bool needToUpdate();
float getProgressAsFloat() const; float getProgressAsFloat() const;
int getUpdateProgress(); int64 getUpdateProgress();
int getDiagonalFactor(); int64 getDiagonalFactor();
int getHeightFactor(float speedMultiplier=PROGRESS_SPEED_MULTIPLIER); int64 getHeightFactor(float speedMultiplier=PROGRESS_SPEED_MULTIPLIER);
int getSpeedDenominator(int updateFPS); int64 getSpeedDenominator(int64 updateFPS);
bool isChangedActiveCommand() const { return changedActiveCommand; } bool isChangedActiveCommand() const { return changedActiveCommand; }
bool isLastStuckFrameWithinCurrentFrameTolerance(); bool isLastStuckFrameWithinCurrentFrameTolerance();
@@ -784,7 +784,7 @@ private:
void morphAttackBoosts(Unit *unit); void morphAttackBoosts(Unit *unit);
int getUpdatedProgress(int currentProgress, int updateFPS, int speed, int diagonalFactor, int heightFactor); int64 getUpdatedProgress(int64 currentProgress, int64 updateFPS, int64 speed, int64 diagonalFactor, int64 heightFactor);
void logSynchDataCommon(string file,int line,string source="",bool threadedMode=false); void logSynchDataCommon(string file,int line,string source="",bool threadedMode=false);
void updateAttackBoostProgress(const Game* game); void updateAttackBoostProgress(const Game* game);

View File

@@ -232,6 +232,7 @@ void copyStringToBuffer(char *buffer, int bufferSize, const string& s);
//numeric fcs //numeric fcs
int clamp(int value, int min, int max); int clamp(int value, int min, int max);
float clamp(float value, float min, float max); float clamp(float value, float min, float max);
int64 clamp(int64 value, int64 min, int64 max);
float saturate(float value); float saturate(float value);
int round(float f); int round(float f);

View File

@@ -727,6 +727,16 @@ int clamp(int value, int min, int max){
return value; return value;
} }
int64 clamp(int64 value, int64 min, int64 max){
if (value<min){
return min;
}
if (value>max){
return max;
}
return value;
}
float clamp(float value, float min, float max){ float clamp(float value, float min, float max){
if (value<min){ if (value<min){
return min; return min;