switch to integer for unit progress and some related items to fix out of synch on cross platform

This commit is contained in:
Mark Vejvoda 2013-05-25 02:42:57 +00:00
parent e456096ffc
commit 4d0917e13b
3 changed files with 82 additions and 82 deletions

View File

@ -370,14 +370,14 @@ void FactionThread::execute() {
//update = true; //update = true;
if(update == true) { if(update == true) {
if(SystemFlags::getSystemSettingType(SystemFlags::debugWorldSynch).enabled == true) { if(SystemFlags::getSystemSettingType(SystemFlags::debugWorldSynch).enabled == true) {
float updateProgressValue = unit->getUpdateProgress(); int updateProgressValue = unit->getUpdateProgress();
int speed = unit->getCurrSkill()->getTotalSpeed(unit->getTotalUpgrade()); int speed = unit->getCurrSkill()->getTotalSpeed(unit->getTotalUpgrade());
float df = unit->getDiagonalFactor(); int df = unit->getDiagonalFactor();
float hf = unit->getHeightFactor(); int hf = unit->getHeightFactor();
bool changedActiveCommand = unit->isChangedActiveCommand(); bool changedActiveCommand = unit->isChangedActiveCommand();
char szBuf[8096]=""; char szBuf[8096]="";
snprintf(szBuf,8096,"unit->needToUpdate() returned: %d updateProgressValue: %f speed: %d changedActiveCommand: %d df: %f hf: %f",update,updateProgressValue,speed,changedActiveCommand,df,hf); snprintf(szBuf,8096,"unit->needToUpdate() returned: %d updateProgressValue: %d speed: %d changedActiveCommand: %d df: %d hf: %d",update,updateProgressValue,speed,changedActiveCommand,df,hf);
unit->logSynchDataThreaded(__FILE__,__LINE__,szBuf); unit->logSynchDataThreaded(__FILE__,__LINE__,szBuf);
} }
@ -394,14 +394,14 @@ void FactionThread::execute() {
} }
else { else {
if(SystemFlags::getSystemSettingType(SystemFlags::debugWorldSynch).enabled == true) { if(SystemFlags::getSystemSettingType(SystemFlags::debugWorldSynch).enabled == true) {
float updateProgressValue = unit->getUpdateProgress(); int updateProgressValue = unit->getUpdateProgress();
int speed = unit->getCurrSkill()->getTotalSpeed(unit->getTotalUpgrade()); int speed = unit->getCurrSkill()->getTotalSpeed(unit->getTotalUpgrade());
float df = unit->getDiagonalFactor(); int df = unit->getDiagonalFactor();
float hf = unit->getHeightFactor(); int hf = unit->getHeightFactor();
bool changedActiveCommand = unit->isChangedActiveCommand(); bool changedActiveCommand = unit->isChangedActiveCommand();
char szBuf[8096]=""; char szBuf[8096]="";
snprintf(szBuf,8096,"unit->needToUpdate() returned: %d updateProgressValue: %f speed: %d changedActiveCommand: %d df: %f hf: %f",update,updateProgressValue,speed,changedActiveCommand,df,hf); snprintf(szBuf,8096,"unit->needToUpdate() returned: %d updateProgressValue: %d speed: %d changedActiveCommand: %d df: %d hf: %d",update,updateProgressValue,speed,changedActiveCommand,df,hf);
unit->logSynchDataThreaded(__FILE__,__LINE__,szBuf); unit->logSynchDataThreaded(__FILE__,__LINE__,szBuf);
} }

View File

@ -35,7 +35,7 @@ using namespace Shared::Util;
namespace Glest{ namespace Game{ namespace Glest{ namespace Game{
const float CHANGE_COMMAND_SPEED = 325.0; const int CHANGE_COMMAND_SPEED = 325;
//Mutex Unit::mutexDeletedUnits; //Mutex Unit::mutexDeletedUnits;
//map<void *,bool> Unit::deletedUnits; //map<void *,bool> Unit::deletedUnits;
@ -388,7 +388,7 @@ void UnitAttackBoostEffectOriginator::saveGame(XmlNode *rootNode) {
// class Unit // class Unit
// ===================================================== // =====================================================
const float Unit::speedDivider= 100.f; 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
const int Unit::invalidId= -1; const int Unit::invalidId= -1;
@ -1332,27 +1332,12 @@ Vec3f Unit::getCurrVector() const{
Vec3f Unit::getCurrVectorFlat() const{ Vec3f Unit::getCurrVectorFlat() const{
return getVectorFlat(lastPos, pos); return getVectorFlat(lastPos, pos);
/* }
Vec3f v;
float y1= computeHeight(lastPos); float Unit::getProgessAsFloat() const {
float y2= computeHeight(pos); float result = (static_cast<float>(progress) / 100.f);
result = truncateDecimal<float>(result);
if(currSkill->getClass() == scMove) { return result;
v.x= lastPos.x + progress * (pos.x-lastPos.x);
v.z= lastPos.y + progress * (pos.y-lastPos.y);
v.y= y1+progress*(y2-y1);
}
else{
v.x= static_cast<float>(pos.x);
v.z= static_cast<float>(pos.y);
v.y= y2;
}
v.x+= type->getSize()/2.f-0.5f;
v.z+= type->getSize()/2.f-0.5f;
return v;
*/
} }
Vec3f Unit::getVectorFlat(const Vec2i &lastPosValue, const Vec2i &curPosValue) const { Vec3f Unit::getVectorFlat(const Vec2i &lastPosValue, const Vec2i &curPosValue) const {
@ -1362,9 +1347,9 @@ Vec3f Unit::getVectorFlat(const Vec2i &lastPosValue, const Vec2i &curPosValue) c
float y2= computeHeight(curPosValue); float y2= computeHeight(curPosValue);
if(currSkill->getClass() == scMove) { if(currSkill->getClass() == scMove) {
v.x= lastPosValue.x + progress * (curPosValue.x-lastPosValue.x); v.x= lastPosValue.x + getProgessAsFloat() * (curPosValue.x - lastPosValue.x);
v.z= lastPosValue.y + progress * (curPosValue.y-lastPosValue.y); v.z= lastPosValue.y + getProgessAsFloat() * (curPosValue.y - lastPosValue.y);
v.y= y1+progress*(y2-y1); v.y= y1 + getProgessAsFloat() * (y2-y1);
} }
else{ else{
v.x= static_cast<float>(curPosValue.x); v.x= static_cast<float>(curPosValue.x);
@ -1859,10 +1844,10 @@ const CommandType *Unit::computeCommandType(const Vec2i &pos, const Unit *target
return commandType; return commandType;
} }
float Unit::getUpdateProgress() { int Unit::getUpdateProgress() {
if(progress > 1.f) { if(progress > 100) {
char szBuf[8096]=""; char szBuf[8096]="";
snprintf(szBuf,8096,"In [%s::%s Line: %d] ERROR: progress > 1.f, progress = [%f]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,progress); snprintf(szBuf,8096,"In [%s::%s Line: %d] ERROR: progress > 100, progress = [%d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,progress);
throw megaglest_runtime_error(szBuf); throw megaglest_runtime_error(szBuf);
} }
@ -1872,7 +1857,7 @@ float Unit::getUpdateProgress() {
throw megaglest_runtime_error(szBuf); throw megaglest_runtime_error(szBuf);
} }
float newProgress = progress; int newProgress = progress;
if(currSkill->getClass() != scDie) { if(currSkill->getClass() != scDie) {
//speed //speed
int speed = currSkill->getTotalSpeed(&totalUpgrade); int speed = currSkill->getTotalSpeed(&totalUpgrade);
@ -1882,8 +1867,8 @@ float Unit::getUpdateProgress() {
} }
//speed modifier //speed modifier
float diagonalFactor = getDiagonalFactor(); int diagonalFactor = getDiagonalFactor();
float heightFactor = getHeightFactor(); int heightFactor = getHeightFactor();
//update progresses //update progresses
const Game *game = Renderer::getInstance().getGame(); const Game *game = Renderer::getInstance().getGame();
@ -1905,32 +1890,34 @@ float Unit::getUpdateProgress() {
bool Unit::needToUpdate() { bool Unit::needToUpdate() {
bool return_value = false; bool return_value = false;
if(currSkill->getClass() != scDie) { if(currSkill->getClass() != scDie) {
float newProgress = getUpdateProgress(); int newProgress = getUpdateProgress();
if(newProgress >= 1.f) { if(newProgress >= 100) {
return_value = true; return_value = true;
} }
} }
return return_value; return return_value;
} }
float Unit::getDiagonalFactor() { int Unit::getDiagonalFactor() {
//speed modifier //speed modifier
float diagonalFactor= 1.f; //float diagonalFactor= 1.f;
int diagonalFactor= 100;
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 = 0.71f; diagonalFactor = 71;
diagonalFactor = truncateDecimal<float>(diagonalFactor); //diagonalFactor = truncateDecimal<float>(diagonalFactor);
} }
} }
return diagonalFactor; return diagonalFactor;
} }
float Unit::getHeightFactor() { int Unit::getHeightFactor() {
//speed modifier //speed modifier
float heightFactor= 1.f; //float heightFactor= 1.f;
float heightFactor= 100;
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);
@ -1943,30 +1930,35 @@ float Unit::getHeightFactor() {
throw megaglest_runtime_error("targetCell == NULL"); throw megaglest_runtime_error("targetCell == NULL");
} }
float heightDiff= unitCell->getHeight() - targetCell->getHeight(); // float heightDiff= unitCell->getHeight() - targetCell->getHeight();
heightFactor= clamp(1.f + heightDiff / 5.f, 0.2f, 5.f); // heightFactor= clamp(1.f + heightDiff / 5.f, 0.2f, 5.f);
heightFactor = truncateDecimal<float>(heightFactor); // heightFactor = truncateDecimal<float>(heightFactor);
int heightDiff= ((unitCell->getHeight() * 100.f) - (targetCell->getHeight() * 100.f));
heightFactor= clamp(100 + heightDiff / 500, 20, 500);
} }
return heightFactor; return heightFactor;
} }
float Unit::getSpeedDenominator(int updateFPS) { int Unit::getSpeedDenominator(int updateFPS) {
float speedDenominator = truncateDecimal<float>(speedDivider * updateFPS); //float speedDenominator = truncateDecimal<float>(speedDivider * updateFPS);
int speedDenominator = (speedDivider * updateFPS) * 100;
return speedDenominator; return speedDenominator;
} }
float Unit::getUpdatedProgress(float currentProgress, int updateFPS, int speed, int Unit::getUpdatedProgress(int currentProgress, int updateFPS, int speed,
float diagonalFactor, float heightFactor) { int diagonalFactor, int heightFactor) {
float speedDenominator = getSpeedDenominator(updateFPS); //float speedDenominator = getSpeedDenominator(updateFPS);
float newProgress = truncateDecimal<float>(currentProgress); int speedDenominator = getSpeedDenominator(updateFPS);
newProgress += truncateDecimal<float> //float newProgress = truncateDecimal<float>(currentProgress);
((speed * diagonalFactor * heightFactor) / speedDenominator); int newProgress = currentProgress;
//newProgress += truncateDecimal<float> ((speed * diagonalFactor * heightFactor) / speedDenominator);
newProgress += ((speed * diagonalFactor * heightFactor) / speedDenominator);
return newProgress; return newProgress;
} }
bool Unit::update() { bool Unit::update() {
assert(progress <= 1.f); assert(progress <= 100);
//highlight //highlight
if(highlight > 0.f) { if(highlight > 0.f) {
@ -1988,8 +1980,8 @@ bool Unit::update() {
} }
//speed modifier //speed modifier
float diagonalFactor = getDiagonalFactor(); int diagonalFactor = getDiagonalFactor();
float heightFactor = getHeightFactor(); int heightFactor = getHeightFactor();
//update progresses //update progresses
lastAnimProgress= animProgress; lastAnimProgress= animProgress;
@ -2000,6 +1992,8 @@ bool Unit::update() {
progress = getUpdatedProgress(progress, game->getWorld()->getUpdateFps(this->getFactionIndex()), progress = getUpdatedProgress(progress, game->getWorld()->getUpdateFps(this->getFactionIndex()),
speed, diagonalFactor, heightFactor); speed, diagonalFactor, heightFactor);
//printf("Test progress = %d for unit [%d - %s]\n",progress,id,getType()->getName().c_str());
if(isAnimProgressBound() == true) { if(isAnimProgressBound() == true) {
float targetProgress=0; float targetProgress=0;
if(currSkill->getClass() == scBeBuilt) { if(currSkill->getClass() == scBeBuilt) {
@ -2015,14 +2009,16 @@ bool Unit::update() {
targetProgress = this->getProgressRatio(); targetProgress = this->getProgressRatio();
} }
if(animProgress<targetProgress){ if(animProgress<targetProgress){
float diff=targetProgress-animProgress; float diff = targetProgress - animProgress;
animProgress=animProgress+diff/(GameConstants::updateFps); animProgress=animProgress + diff / (GameConstants::updateFps);
} }
} }
else { else {
//float speedDenominator = (speedDivider * game->getWorld()->getUpdateFps(this->getFactionIndex())); //float speedDenominator = (speedDivider * game->getWorld()->getUpdateFps(this->getFactionIndex()));
float speedDenominator = getSpeedDenominator(game->getWorld()->getUpdateFps(this->getFactionIndex())); int speedDenominator = getSpeedDenominator(game->getWorld()->getUpdateFps(this->getFactionIndex()));
animProgress += (currSkill->getAnimSpeed() * heightFactor) / speedDenominator; animProgress += (currSkill->getAnimSpeed() *
(truncateDecimal<float>(static_cast<float>(heightFactor) / 100.f))) /
(truncateDecimal<float>(static_cast<float>(speedDenominator) / 100.f));
} }
//update target //update target
updateTarget(); updateTarget();
@ -2030,13 +2026,15 @@ bool Unit::update() {
//rotation //rotation
if(currSkill->getClass() != scStop) { if(currSkill->getClass() != scStop) {
const int rotFactor= 2; const int rotFactor= 2;
if(progress < 1.f / rotFactor) { if(getProgessAsFloat() < 1.f / rotFactor) {
if(type->getFirstStOfClass(scMove)){ if(type->getFirstStOfClass(scMove)){
if(abs((int)(lastRotation-targetRotation)) < 180) if(abs((int)(lastRotation-targetRotation)) < 180)
rotation= lastRotation + (targetRotation - lastRotation) * progress * rotFactor; rotation= lastRotation + (targetRotation - lastRotation) *
getProgessAsFloat() * rotFactor;
else { else {
float rotationTerm = targetRotation > lastRotation ? -360.f: +360.f; float rotationTerm = targetRotation > lastRotation ? -360.f: +360.f;
rotation = lastRotation + (targetRotation - lastRotation + rotationTerm) * progress * rotFactor; rotation = lastRotation + (targetRotation - lastRotation + rotationTerm) *
getProgessAsFloat() * rotFactor;
} }
} }
} }
@ -2108,14 +2106,14 @@ bool Unit::update() {
bool return_value = false; bool return_value = false;
//checks //checks
if(progress >= 1.f) { if(progress >= 100) {
lastRotation= targetRotation; lastRotation= targetRotation;
if(currSkill->getClass() != scDie) { if(currSkill->getClass() != scDie) {
progress = 0.f; progress = 0;
return_value = true; return_value = true;
} }
else { else {
progress= 1.f; progress= 100;
deadCount++; deadCount++;
if(deadCount >= maxDeadCount) { if(deadCount >= maxDeadCount) {
toBeUndertaken= true; toBeUndertaken= true;
@ -3537,7 +3535,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 [%f], progress2 [%d]\nUnit Path [%s]\n", "FrameCount [%d] Unit = %d [%s][%s] pos = %s, lastPos = %s, targetPos = %s, targetVec = %s, meetingPos = %s, progress [%d], progress2 [%d]\nUnit Path [%s]\n",
getFrameCount(), getFrameCount(),
id, id,
getFullName().c_str(), getFullName().c_str(),
@ -3765,7 +3763,7 @@ std::string Unit::toString() const {
result += " ep = " + intToStr(this->ep); result += " ep = " + intToStr(this->ep);
result += " loadCount = " + intToStr(this->loadCount); result += " loadCount = " + intToStr(this->loadCount);
result += " deadCount = " + intToStr(this->deadCount); result += " deadCount = " + intToStr(this->deadCount);
result += " progress = " + floatToStr(this->progress,16); result += " progress = " + intToStr(this->progress);
result += "\n"; result += "\n";
result += " lastAnimProgress = " + floatToStr(this->lastAnimProgress,16); result += " lastAnimProgress = " + floatToStr(this->lastAnimProgress,16);
result += " animProgress = " + floatToStr(this->animProgress,16); result += " animProgress = " + floatToStr(this->animProgress,16);
@ -3863,7 +3861,7 @@ void Unit::saveGame(XmlNode *rootNode) {
// int deadCount; // int deadCount;
unitNode->addAttribute("deadCount",intToStr(deadCount), mapTagReplacements); unitNode->addAttribute("deadCount",intToStr(deadCount), mapTagReplacements);
// float progress; //between 0 and 1 // float progress; //between 0 and 1
unitNode->addAttribute("progress",floatToStr(progress,16), mapTagReplacements); unitNode->addAttribute("progress",intToStr(progress), mapTagReplacements);
// float lastAnimProgress; //between 0 and 1 // float lastAnimProgress; //between 0 and 1
unitNode->addAttribute("lastAnimProgress",floatToStr(lastAnimProgress,16), mapTagReplacements); unitNode->addAttribute("lastAnimProgress",floatToStr(lastAnimProgress,16), mapTagReplacements);
// float animProgress; //between 0 and 1 // float animProgress; //between 0 and 1
@ -4185,7 +4183,7 @@ 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
result->progress = unitNode->getAttribute("progress")->getFloatValue(); result->progress = unitNode->getAttribute("progress")->getIntValue();
// float lastAnimProgress; //between 0 and 1 // float lastAnimProgress; //between 0 and 1
result->lastAnimProgress = unitNode->getAttribute("lastAnimProgress")->getFloatValue(); result->lastAnimProgress = unitNode->getAttribute("lastAnimProgress")->getFloatValue();
// float animProgress; //between 0 and 1 // float animProgress; //between 0 and 1

View File

@ -331,7 +331,7 @@ private:
#endif #endif
public: public:
static const float speedDivider; static const int speedDivider;
static const int maxDeadCount; static const int maxDeadCount;
static const int invalidId; static const int invalidId;
@ -346,7 +346,8 @@ private:
int ep; int ep;
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
float lastAnimProgress; //between 0 and 1 float lastAnimProgress; //between 0 and 1
float animProgress; //between 0 and 1 float animProgress; //between 0 and 1
float highlight; float highlight;
@ -724,10 +725,11 @@ public:
std::string toString() const; std::string toString() const;
bool needToUpdate(); bool needToUpdate();
float getUpdateProgress(); float getProgessAsFloat() const;
float getDiagonalFactor(); int getUpdateProgress();
float getHeightFactor(); int getDiagonalFactor();
float getSpeedDenominator(int updateFPS); int getHeightFactor();
int getSpeedDenominator(int updateFPS);
bool isChangedActiveCommand() const { return changedActiveCommand; } bool isChangedActiveCommand() const { return changedActiveCommand; }
bool isLastStuckFrameWithinCurrentFrameTolerance(); bool isLastStuckFrameWithinCurrentFrameTolerance();
@ -774,7 +776,7 @@ private:
void morphAttackBoosts(Unit *unit); void morphAttackBoosts(Unit *unit);
float getUpdatedProgress(float currentProgress, int updateFPS, int speed, float diagonalFactor, float heightFactor); int getUpdatedProgress(int currentProgress, int updateFPS, int speed, int diagonalFactor, int heightFactor);
void logSynchDataCommon(string file,int line,string source="",bool threadedMode=false); void logSynchDataCommon(string file,int line,string source="",bool threadedMode=false);
}; };