- finally found and fixed out of synch related to AI.

- added ability to log all unit activity to world synch logfiles so we can diff game info from different network clients
This commit is contained in:
Mark Vejvoda
2010-11-30 23:32:39 +00:00
parent e62f3ca64c
commit b02da7d1e0
13 changed files with 300 additions and 84 deletions

View File

@@ -426,6 +426,16 @@ bool AiInterface::isResourceNear(const Vec2i &pos, const ResourceType *rt, Vec2i
if(r != NULL) {
if(r->getType() == rt) {
resourcePos= pos + Vec2i(i,j);
//if(faction != NULL) {
// char szBuf[4096]="";
// sprintf(szBuf,"[%s::%s Line: %d] [isresourcenear] pos [%s] resourcePos [%s] fallbackToPeersHarvestingSameResource [%d] rt [%s]",
// __FILE__,__FUNCTION__,__LINE__,pos.getString().c_str(),resourcePos.getString().c_str(),fallbackToPeersHarvestingSameResource,rt->getName().c_str());
// SystemFlags::OutputDebug(SystemFlags::debugWorldSynch,"----------------------------------- START [%d] ------------------------------------------------\n",faction->getFrameCount());
// SystemFlags::OutputDebug(SystemFlags::debugWorldSynch,"%s",szBuf);
// SystemFlags::OutputDebug(SystemFlags::debugWorldSynch,"------------------------------------ END [%d] -------------------------------------------------\n",faction->getFrameCount());
//}
return true;
}
}
@@ -442,12 +452,31 @@ bool AiInterface::isResourceNear(const Vec2i &pos, const ResourceType *rt, Vec2i
Vec2i result = faction->getClosestResourceTypeTargetFromCache(pos, rt);
if(result.x >= 0) {
resourcePos = result;
//char szBuf[4096]="";
// sprintf(szBuf,"[%s::%s Line: %d] [isresourcenear] pos [%s] resourcePos [%s] fallbackToPeersHarvestingSameResource [%d] rt [%s]",
// __FILE__,__FUNCTION__,__LINE__,pos.getString().c_str(),resourcePos.getString().c_str(),fallbackToPeersHarvestingSameResource,rt->getName().c_str());
//SystemFlags::OutputDebug(SystemFlags::debugWorldSynch,"----------------------------------- START [%d] ------------------------------------------------\n",faction->getFrameCount());
//SystemFlags::OutputDebug(SystemFlags::debugWorldSynch,"%s",szBuf);
//SystemFlags::OutputDebug(SystemFlags::debugWorldSynch,"------------------------------------ END [%d] -------------------------------------------------\n",faction->getFrameCount());
if(pos.dist(resourcePos) <= size) {
return true;
}
}
}
//if(faction != NULL) {
// char szBuf[4096]="";
// sprintf(szBuf,"[%s::%s Line: %d] [isresourcenear] pos [%s] resourcePos [%s] fallbackToPeersHarvestingSameResource [%d] rt [%s] getCacheResourceTargetListSize() [%d]",
// __FILE__,__FUNCTION__,__LINE__,pos.getString().c_str(),resourcePos.getString().c_str(),fallbackToPeersHarvestingSameResource,rt->getName().c_str(),faction->getCacheResourceTargetListSize());
// SystemFlags::OutputDebug(SystemFlags::debugWorldSynch,"----------------------------------- START [%d] ------------------------------------------------\n",faction->getFrameCount());
// SystemFlags::OutputDebug(SystemFlags::debugWorldSynch,"%s",szBuf);
// SystemFlags::OutputDebug(SystemFlags::debugWorldSynch,"------------------------------------ END [%d] -------------------------------------------------\n",faction->getFrameCount());
//}
return false;
}
@@ -516,7 +545,7 @@ bool AiInterface::getNearestSightedResource(const ResourceType *rt, const Vec2i
}
}
faction->addResourceTargetToCache(resPos,false);
//faction->addResourceTargetToCache(resPos,false);
}
}
}

View File

@@ -44,16 +44,16 @@ const int PathFinder::pathFindRefresh= 10;
PathFinder::PathFinder() {
nodePool.clear();
lookupCacheCanMove.clear();
moveLookupCacheApproxCanMove.clear();
//lookupCacheCanMove.clear();
//moveLookupCacheApproxCanMove.clear();
map=NULL;
}
PathFinder::PathFinder(const Map *map) {
nodePool.clear();
lookupCacheCanMove.clear();
moveLookupCacheApproxCanMove.clear();
//lookupCacheCanMove.clear();
//moveLookupCacheApproxCanMove.clear();
map=NULL;
init(map);
@@ -61,16 +61,16 @@ PathFinder::PathFinder(const Map *map) {
void PathFinder::init(const Map *map) {
nodePool.resize(pathFindNodesMax);
lookupCacheCanMove.clear();
moveLookupCacheApproxCanMove.clear();
//lookupCacheCanMove.clear();
//moveLookupCacheApproxCanMove.clear();
this->map= map;
}
PathFinder::~PathFinder(){
nodePool.clear();
lookupCacheCanMove.clear();
moveLookupCacheApproxCanMove.clear();
//lookupCacheCanMove.clear();
//moveLookupCacheApproxCanMove.clear();
map=NULL;
}
@@ -81,8 +81,15 @@ TravelState PathFinder::findPath(Unit *unit, const Vec2i &finalPos, bool *wasStu
}
if(clearLookupCache == true) {
lookupCacheCanMove.clear();
moveLookupCacheApproxCanMove.clear();
//lookupCacheCanMove.clear();
//moveLookupCacheApproxCanMove.clear();
}
if(SystemFlags::getSystemSettingType(SystemFlags::debugWorldSynch).enabled == true) {
char szBuf[4096]="";
sprintf(szBuf,"[%s::%s Line: %d] [findPath] unit->getPos() [%s] finalPos [%s]",
__FILE__,__FUNCTION__,__LINE__,unit->getPos().getString().c_str(),finalPos.getString().c_str());
unit->logSynchData(szBuf);
}
//route cache
@@ -176,6 +183,14 @@ TravelState PathFinder::findPath(Unit *unit, const Vec2i &finalPos, bool *wasStu
// We will try to bail out of the immediate area
if( ts == tsBlocked && unit->getInBailOutAttempt() == false &&
path->isStuck() == true) {
if(SystemFlags::getSystemSettingType(SystemFlags::debugWorldSynch).enabled == true) {
char szBuf[4096]="";
sprintf(szBuf,"[%s::%s Line: %d] [attempting to BAIL OUT] finalPos [%s] ts [%d]",
__FILE__,__FUNCTION__,__LINE__,finalPos.getString().c_str(),ts);
unit->logSynchData(szBuf);
}
if(wasStuck != NULL) {
*wasStuck = true;
}
@@ -185,7 +200,16 @@ TravelState PathFinder::findPath(Unit *unit, const Vec2i &finalPos, bool *wasStu
for(int bailoutY = -20; bailoutY <= 20 && ts == tsBlocked; ++bailoutY) {
const Vec2i newFinalPos = finalPos + Vec2i(bailoutX,bailoutY);
//if(map->canMove(unit, unit->getPos(), newFinalPos, &lookupCacheCanMove)) {
if(map->canMove(unit, unit->getPos(), newFinalPos)) {
bool canUnitMove = map->canMove(unit, unit->getPos(), newFinalPos);
if(SystemFlags::getSystemSettingType(SystemFlags::debugWorldSynch).enabled == true) {
char szBuf[4096]="";
sprintf(szBuf,"[%s::%s Line: %d] [attempting to BAIL OUT] finalPos [%s] newFinalPos [%s] ts [%d] canUnitMove [%d]",
__FILE__,__FUNCTION__,__LINE__,finalPos.getString().c_str(),newFinalPos.getString().c_str(),ts,canUnitMove);
unit->logSynchData(szBuf);
}
if(canUnitMove) {
ts= aStar(unit, newFinalPos, true);
}
}
@@ -302,7 +326,8 @@ TravelState PathFinder::aStar(Unit *unit, const Vec2i &targetPos, bool inBailout
for(int j = -1; j <= 1 && nodeLimitReached == false; ++j) {
Vec2i sucPos= node->pos + Vec2i(i, j);
bool canUnitMoveToCell = map->aproxCanMove(unit, node->pos, sucPos, &moveLookupCacheApproxCanMove);
//bool canUnitMoveToCell = map->aproxCanMove(unit, node->pos, sucPos, &moveLookupCacheApproxCanMove);
bool canUnitMoveToCell = map->aproxCanMove(unit, node->pos, sucPos);
if(openPos(sucPos) == false && canUnitMoveToCell == true) {
//if node is not open and canMove then generate another node
@@ -348,15 +373,22 @@ TravelState PathFinder::aStar(Unit *unit, const Vec2i &targetPos, bool inBailout
commandDesc = command->getCommandType()->toString();
}
std::pair<Vec2i,Chrono> lastHarvest = unit->getLastHarvestResourceTarget();
std::pair<Vec2i,int> lastHarvest = unit->getLastHarvestResourceTarget();
char szBuf[1024]="";
sprintf(szBuf,"State: blocked, cmd [%s] pos: [%s], dest pos: [%s], lastHarvest = [%s - %lld], reason A= %d, B= %d, C= %d, D= %d, E= %d, F = %d",commandDesc.c_str(),unit->getPos().getString().c_str(), targetPos.getString().c_str(),lastHarvest.first.getString().c_str(),(long long int)lastHarvest.second.getMillis(), pathFound,(lastNode == firstNode),path->getBlockCount(), path->isBlocked(), nodeLimitReached,path->isStuck());
sprintf(szBuf,"State: blocked, cmd [%s] pos: [%s], dest pos: [%s], lastHarvest = [%s - %d], reason A= %d, B= %d, C= %d, D= %d, E= %d, F = %d",commandDesc.c_str(),unit->getPos().getString().c_str(), targetPos.getString().c_str(),lastHarvest.first.getString().c_str(),lastHarvest.second, pathFound,(lastNode == firstNode),path->getBlockCount(), path->isBlocked(), nodeLimitReached,path->isStuck());
unit->setCurrentUnitTitle(szBuf);
}
ts= tsBlocked;
path->incBlockCount();
if(SystemFlags::getSystemSettingType(SystemFlags::debugWorldSynch).enabled == true) {
char szBuf[4096]="";
sprintf(szBuf,"[%s::%s Line: %d] [path for unit BLOCKED] openNodesList.size() [%ld] openPosList.size() [%ld] finalPos [%s] targetPos [%s] inBailout [%d] ts [%d]",
__FILE__,__FUNCTION__,__LINE__,openNodesList.size(),openPosList.size(),finalPos.getString().c_str(),targetPos.getString().c_str(),inBailout,ts);
unit->logSynchData(szBuf);
}
}
else {
//on the way
@@ -376,6 +408,13 @@ TravelState PathFinder::aStar(Unit *unit, const Vec2i &targetPos, bool inBailout
path->add(currNode->next->pos);
}
if(SystemFlags::getSystemSettingType(SystemFlags::debugWorldSynch).enabled == true) {
char szBuf[4096]="";
sprintf(szBuf,"[%s::%s Line: %d] [Setting new path for unit] openNodesList.size() [%ld] openPosList.size() [%ld] finalPos [%s] targetPos [%s] inBailout [%d] ts [%d]",
__FILE__,__FUNCTION__,__LINE__,openNodesList.size(),openPosList.size(),finalPos.getString().c_str(),targetPos.getString().c_str(),inBailout,ts);
unit->logSynchData(szBuf);
}
if(SystemFlags::getSystemSettingType(SystemFlags::debugPathFinder).enabled == true) {
string commandDesc = "none";
Command *command= unit->getCurrCommand();

View File

@@ -66,7 +66,7 @@ private:
int nodePoolCount;
const Map *map;
std::map<Vec2i, std::map<Vec2i, std::map<int, std::map<Field,bool> > > > lookupCacheCanMove;
//std::map<Vec2i, std::map<Vec2i, std::map<int, std::map<Field,bool> > > > lookupCacheCanMove;
std::map<Vec2i, std::map<Vec2i, std::map<int, std::map<int, std::map<Field,bool> > > > > moveLookupCacheApproxCanMove;
public:

View File

@@ -1712,8 +1712,8 @@ void Game::render2d(){
if(perfLogging == true && difftime(time(NULL),lastRenderLog2d) >= 1) {
lastRenderLog2d = time(NULL);
SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s] Line: %d Statistics: %s\n",__FILE__,__FUNCTION__,__LINE__,str.c_str());
SystemFlags::OutputDebug(SystemFlags::debugWorldSynch,"In [%s::%s] Line: %d Statistics: %s\n",__FILE__,__FUNCTION__,__LINE__,str.c_str());
SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] Statistics: %s\n",__FILE__,__FUNCTION__,__LINE__,str.c_str());
//SystemFlags::OutputDebug(SystemFlags::debugWorldSynch,"In [%s::%s Line: %d] Statistics: %s\n",__FILE__,__FUNCTION__,__LINE__,str.c_str());
}
}

View File

@@ -92,6 +92,9 @@ std::string Command::toString() const {
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__, __LINE__);
result += ", unitRef.getUnit() id = " + intToStr(unitRef.getUnitId());
if(unitRef.getUnit() != NULL) {
result += ", unitRef.getUnit() = " + unitRef.getUnit()->toString();
}
}
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__, __LINE__);

View File

@@ -34,7 +34,7 @@ namespace Glest{ namespace Game{
Faction::Faction() {
texture = NULL;
lastResourceTargettListPurge = 0;
//lastResourceTargettListPurge = 0;
cachingDisabled=false;
}
@@ -633,6 +633,17 @@ void Faction::addResourceTargetToCache(const Vec2i &pos,bool incrementUseCounter
if(duplicateEntry == false) {
cacheResourceTargetList[pos] = 1;
if(SystemFlags::getSystemSettingType(SystemFlags::debugWorldSynch).enabled == true) {
char szBuf[4096]="";
sprintf(szBuf,"[%s::%s Line: %d] [addResourceTargetToCache] pos [%s]cacheResourceTargetList.size() [%ld]",
__FILE__,__FUNCTION__,__LINE__,pos.getString().c_str(),cacheResourceTargetList.size());
//unit->logSynchData(szBuf);
SystemFlags::OutputDebug(SystemFlags::debugWorldSynch,"----------------------------------- START [%d] ------------------------------------------------\n",getFrameCount());
SystemFlags::OutputDebug(SystemFlags::debugWorldSynch,"%s",szBuf);
SystemFlags::OutputDebug(SystemFlags::debugWorldSynch,"------------------------------------ END [%d] -------------------------------------------------\n",getFrameCount());
}
}
}
}
@@ -644,6 +655,17 @@ void Faction::removeResourceTargetFromCache(const Vec2i &pos) {
if(iter != cacheResourceTargetList.end()) {
cacheResourceTargetList.erase(pos);
if(SystemFlags::getSystemSettingType(SystemFlags::debugWorldSynch).enabled == true) {
char szBuf[4096]="";
sprintf(szBuf,"[%s::%s Line: %d] [removeResourceTargetFromCache] pos [%s]cacheResourceTargetList.size() [%ld]",
__FILE__,__FUNCTION__,__LINE__,pos.getString().c_str(),cacheResourceTargetList.size());
//unit->logSynchData(szBuf);
SystemFlags::OutputDebug(SystemFlags::debugWorldSynch,"----------------------------------- START [%d] ------------------------------------------------\n",getFrameCount());
SystemFlags::OutputDebug(SystemFlags::debugWorldSynch,"%s",szBuf);
SystemFlags::OutputDebug(SystemFlags::debugWorldSynch,"------------------------------------ END [%d] -------------------------------------------------\n",getFrameCount());
}
}
}
}
@@ -746,6 +768,14 @@ Vec2i Faction::getClosestResourceTypeTargetFromCache(Unit *unit, const ResourceT
}
if(deleteList.size() > 0) {
if(SystemFlags::getSystemSettingType(SystemFlags::debugWorldSynch).enabled == true) {
char szBuf[4096]="";
sprintf(szBuf,"[%s::%s Line: %d] [cleaning old resource targets] deleteList.size() [%ld] cacheResourceTargetList.size() [%ld] result [%s]",
__FILE__,__FUNCTION__,__LINE__,deleteList.size(),cacheResourceTargetList.size(),result.getString().c_str());
unit->logSynchData(szBuf);
}
cleanupResourceTypeTargetCache(&deleteList);
}
}
@@ -754,11 +784,14 @@ Vec2i Faction::getClosestResourceTypeTargetFromCache(Unit *unit, const ResourceT
return result;
}
// CANNOT MODIFY the cache here since the AI calls this method and the AI is only controlled
// by the server for network games and it would cause out of synch since clients do not call
// this method so DO NOT modify the cache here!
Vec2i Faction::getClosestResourceTypeTargetFromCache(const Vec2i &pos, const ResourceType *type) {
Vec2i result(-1);
if(cachingDisabled == false) {
if(cacheResourceTargetList.size() > 0) {
std::vector<Vec2i> deleteList;
//std::vector<Vec2i> deleteList;
const int harvestDistance = 5;
const Map *map = world->getMap();
@@ -780,9 +813,9 @@ Vec2i Faction::getClosestResourceTypeTargetFromCache(const Vec2i &pos, const Res
}
}
}
else {
deleteList.push_back(newPos);
}
//else {
// deleteList.push_back(newPos);
//}
}
}
}
@@ -808,19 +841,28 @@ Vec2i Faction::getClosestResourceTypeTargetFromCache(const Vec2i &pos, const Res
}
}
}
else {
deleteList.push_back(cache);
}
}
else {
deleteList.push_back(cache);
//else {
// deleteList.push_back(cache);
//}
}
//else {
// deleteList.push_back(cache);
//}
}
}
if(deleteList.size() > 0) {
cleanupResourceTypeTargetCache(&deleteList);
}
//char szBuf[4096]="";
//sprintf(szBuf,"[%s::%s Line: %d] [looking for resource targets] result [%s] deleteList.size() [%ld] cacheResourceTargetList.size() [%ld] foundCloseResource [%d]",
// __FILE__,__FUNCTION__,__LINE__,result.getString().c_str(),deleteList.size(),cacheResourceTargetList.size(),foundCloseResource);
//unit->logSynchData(szBuf);
//SystemFlags::OutputDebug(SystemFlags::debugWorldSynch,"----------------------------------- START [%d] ------------------------------------------------\n",getFrameCount());
//SystemFlags::OutputDebug(SystemFlags::debugWorldSynch,"%s",szBuf);
//SystemFlags::OutputDebug(SystemFlags::debugWorldSynch,"------------------------------------ END [%d] -------------------------------------------------\n",getFrameCount());
//if(deleteList.size() > 0) {
// cleanupResourceTypeTargetCache(&deleteList);
//}
}
}
@@ -830,8 +872,12 @@ Vec2i Faction::getClosestResourceTypeTargetFromCache(const Vec2i &pos, const Res
void Faction::cleanupResourceTypeTargetCache(std::vector<Vec2i> *deleteListPtr) {
if(cachingDisabled == false) {
if(cacheResourceTargetList.size() > 0) {
if(deleteListPtr != NULL || difftime(time(NULL),lastResourceTargettListPurge) >= 120) {
lastResourceTargettListPurge = time(NULL);
const int cleanupInterval = (GameConstants::updateFps * 5);
bool needToCleanup = (getFrameCount() % cleanupInterval == 0);
//if(deleteListPtr != NULL || difftime(time(NULL),lastResourceTargettListPurge) >= 120) {
if(deleteListPtr != NULL || needToCleanup == true) {
//lastResourceTargettListPurge = time(NULL);
std::vector<Vec2i> deleteList;
if(deleteListPtr != NULL) {
@@ -854,9 +900,21 @@ void Faction::cleanupResourceTypeTargetCache(std::vector<Vec2i> *deleteListPtr)
}
}
for(int i = 0; i < deleteList.size(); ++i) {
Vec2i &cache = deleteList[i];
cacheResourceTargetList.erase(cache);
if(deleteList.size() > 0) {
if(SystemFlags::getSystemSettingType(SystemFlags::debugWorldSynch).enabled == true) {
char szBuf[4096]="";
sprintf(szBuf,"[%s::%s Line: %d] [cleaning old resource targets] deleteList.size() [%ld] cacheResourceTargetList.size() [%ld], needToCleanup [%d]",
__FILE__,__FUNCTION__,__LINE__,deleteList.size(),cacheResourceTargetList.size(),needToCleanup);
//unit->logSynchData(szBuf);
SystemFlags::OutputDebug(SystemFlags::debugWorldSynch,"----------------------------------- START [%d] ------------------------------------------------\n",getFrameCount());
SystemFlags::OutputDebug(SystemFlags::debugWorldSynch,"%s",szBuf);
SystemFlags::OutputDebug(SystemFlags::debugWorldSynch,"------------------------------------ END [%d] -------------------------------------------------\n",getFrameCount());
}
for(int i = 0; i < deleteList.size(); ++i) {
Vec2i &cache = deleteList[i];
cacheResourceTargetList.erase(cache);
}
}
}
}
@@ -1053,6 +1111,16 @@ Unit * Faction::findClosestUnitWithSkillClass( const Vec2i &pos,const CommandCla
return result;
}
int Faction::getFrameCount() {
int frameCount = 0;
const Game *game = Renderer::getInstance().getGame();
if(game != NULL && game->getWorld() != NULL) {
frameCount = game->getWorld()->getFrameCount();
}
return frameCount;
}
std::string Faction::toString() const {
std::string result = "";

View File

@@ -88,7 +88,8 @@ private:
std::map<Vec2i, std::vector<FactionPathSuccessCache> > successfulPathFinderTargetList;
std::map<Vec2i,int> cacheResourceTargetList;
std::map<Vec2i,bool> cachedCloseResourceTargetLookupList;
time_t lastResourceTargettListPurge;
//time_t lastResourceTargettListPurge;
//int lastResourceTargettListPurge;
//std::map<CommandClass,std::map<int,int> > cacheUnitCommandClassList;
// This cache stores the units free cell movement calcs during a world
@@ -172,6 +173,7 @@ public:
Vec2i getClosestResourceTypeTargetFromCache(Unit *unit, const ResourceType *type);
Vec2i getClosestResourceTypeTargetFromCache(const Vec2i &pos, const ResourceType *type);
void cleanupResourceTypeTargetCache(std::vector<Vec2i> *deleteListPtr);
int getCacheResourceTargetListSize() const { return cacheResourceTargetList.size(); }
Unit * findClosestUnitWithSkillClass(const Vec2i &pos,const CommandClass &cmdClass,
const std::vector<SkillClass> &skillClassList,
@@ -179,6 +181,7 @@ public:
void deletePixels();
int getFrameCount();
std::string toString() const;
private:

View File

@@ -184,7 +184,7 @@ Unit::Unit(int id, UnitPathInterface *unitpath, const Vec2i &pos, const UnitType
this->screenPos = Vec3f(0.0);
this->inBailOutAttempt = false;
this->lastHarvestResourceTarget.first = Vec2i(0);
this->lastBadHarvestListPurge = 0;
//this->lastBadHarvestListPurge = 0;
level= NULL;
loadType= NULL;
@@ -1730,26 +1730,26 @@ void Unit::logSynchData(string source) {
progress2,
(unitPath != NULL ? unitPath->toString().c_str() : "NULL"));
if(lastSynchDataString != string(szBuf)) {
if(lastSynchDataString != string(szBuf) || lastSource != source) {
lastSynchDataString = string(szBuf);
lastSource = source;
SystemFlags::OutputDebug(SystemFlags::debugWorldSynch,
"%s %s",
source.c_str(),
szBuf);
SystemFlags::OutputDebug(SystemFlags::debugWorldSynch,"----------------------------------- START [%d] ------------------------------------------------\n",getFrameCount());
SystemFlags::OutputDebug(SystemFlags::debugWorldSynch,"%s %s",source.c_str(),szBuf);
SystemFlags::OutputDebug(SystemFlags::debugWorldSynch,"------------------------------------ END [%d] -------------------------------------------------\n",getFrameCount());
}
}
}
void Unit::addBadHarvestPos(const Vec2i &value) {
Chrono chron;
chron.start();
badHarvestPosList[value] = chron;
//Chrono chron;
//chron.start();
badHarvestPosList[value] = getFrameCount();
cleanupOldBadHarvestPos();
}
void Unit::removeBadHarvestPos(const Vec2i &value) {
std::map<Vec2i,Chrono>::iterator iter = badHarvestPosList.find(value);
std::map<Vec2i,int>::iterator iter = badHarvestPosList.find(value);
if(iter != badHarvestPosList.end()) {
badHarvestPosList.erase(value);
}
@@ -1759,7 +1759,7 @@ void Unit::removeBadHarvestPos(const Vec2i &value) {
bool Unit::isBadHarvestPos(const Vec2i &value, bool checkPeerUnits) const {
bool result = false;
std::map<Vec2i,Chrono>::const_iterator iter = badHarvestPosList.find(value);
std::map<Vec2i,int>::const_iterator iter = badHarvestPosList.find(value);
if(iter != badHarvestPosList.end()) {
result = true;
}
@@ -1782,17 +1782,31 @@ bool Unit::isBadHarvestPos(const Vec2i &value, bool checkPeerUnits) const {
}
void Unit::cleanupOldBadHarvestPos() {
if(difftime(time(NULL),lastBadHarvestListPurge) >= 240) {
lastBadHarvestListPurge = time(NULL);
//if(difftime(time(NULL),lastBadHarvestListPurge) >= 240) {
//lastBadHarvestListPurge = time(NULL);
const int cleanupInterval = (GameConstants::updateFps * 5);
bool needToCleanup = (getFrameCount() % cleanupInterval == 0);
if(needToCleanup == true) {
//printf("========================> cleanupOldBadHarvestPos() [%d] badHarvestPosList.size [%ld]\n",getFrameCount(),badHarvestPosList.size());
std::vector<Vec2i> purgeList;
for(std::map<Vec2i,Chrono>::iterator iter = badHarvestPosList.begin(); iter != badHarvestPosList.end(); iter++) {
if(iter->second.getMillis() >= 2400000) {
for(std::map<Vec2i,int>::iterator iter = badHarvestPosList.begin(); iter != badHarvestPosList.end(); iter++) {
if(getFrameCount() - iter->second >= cleanupInterval) {
//printf("cleanupOldBadHarvestPos() [%d][%d]\n",getFrameCount(),iter->second);
purgeList.push_back(iter->first);
}
}
for(int i = 0; i < purgeList.size(); ++i) {
const Vec2i &item = purgeList[i];
badHarvestPosList.erase(item);
if(purgeList.size() > 0) {
char szBuf[4096]="";
sprintf(szBuf,"[%s::%s Line: %d] [cleaning old bad harvest targets] purgeList.size() [%ld]",
__FILE__,__FUNCTION__,__LINE__,purgeList.size());
logSynchData(szBuf);
for(int i = 0; i < purgeList.size(); ++i) {
const Vec2i &item = purgeList[i];
badHarvestPosList.erase(item);
}
}
}
}
@@ -1807,14 +1821,16 @@ void Unit::setLastHarvestResourceTarget(const Vec2i *pos) {
if(resourceLocation != lastHarvestResourceTarget.first) {
lastHarvestResourceTarget.first = resourceLocation;
Chrono chron;
chron.start();
lastHarvestResourceTarget.second = chron;
//Chrono chron;
//chron.start();
lastHarvestResourceTarget.second = getFrameCount();
}
else {
// If we cannot harvest for > 10 seconds tag the position
// as a bad one
if(lastHarvestResourceTarget.second.getMillis() > 10000) {
const int addInterval = (GameConstants::updateFps * 5);
if(lastHarvestResourceTarget.second - getFrameCount() >= addInterval) {
printf("-----------------------> setLastHarvestResourceTarget() [%d][%d]\n",getFrameCount(),lastHarvestResourceTarget.second);
addBadHarvestPos(resourceLocation);
}
}

View File

@@ -286,6 +286,7 @@ private:
CardinalDir modelFacing;
std::string lastSynchDataString;
std::string lastSource;
int lastRenderFrame;
bool visible;
@@ -301,9 +302,9 @@ private:
// list may be used to tell areas of the game to ignore those cells for a
// period of time
//std::vector<std::pair<Vec2i,Chrono> > badHarvestPosList;
std::map<Vec2i,Chrono> badHarvestPosList;
time_t lastBadHarvestListPurge;
std::pair<Vec2i,Chrono> lastHarvestResourceTarget;
std::map<Vec2i,int> badHarvestPosList;
//time_t lastBadHarvestListPurge;
std::pair<Vec2i,int> lastHarvestResourceTarget;
std::pair<Vec2i,std::vector<Vec2i> > currentTargetPathTaken;
@@ -459,11 +460,12 @@ public:
void cleanupOldBadHarvestPos();
void setLastHarvestResourceTarget(const Vec2i *pos);
std::pair<Vec2i,Chrono> getLastHarvestResourceTarget() const { return lastHarvestResourceTarget;}
std::pair<Vec2i,int> getLastHarvestResourceTarget() const { return lastHarvestResourceTarget;}
std::pair<Vec2i,std::vector<Vec2i> > getCurrentTargetPathTaken() const { return currentTargetPathTaken; }
void addCurrentTargetPathTakenCell(const Vec2i &target,const Vec2i &cell);
void logSynchData(string source="");
std::string toString() const;
private:
@@ -474,7 +476,6 @@ private:
void stopDamageParticles();
void startDamageParticles();
void logSynchData(string source="");
int getFrameCount();
};

View File

@@ -324,6 +324,14 @@ bool Map::isResourceNear(const Vec2i &pos, const ResourceType *rt, Vec2i &resour
Vec2i result = unit->getFaction()->getClosestResourceTypeTargetFromCache(unit, rt);
if(result.x >= 0) {
resourcePos = result;
if(SystemFlags::getSystemSettingType(SystemFlags::debugWorldSynch).enabled == true) {
char szBuf[4096]="";
sprintf(szBuf,"[%s::%s Line: %d] [found peer harvest pos] pos [%s] resourcePos [%s] unit->getFaction()->getCacheResourceTargetListSize() [%d]",
__FILE__,__FUNCTION__,__LINE__,pos.getString().c_str(),resourcePos.getString().c_str(),unit->getFaction()->getCacheResourceTargetListSize());
unit->logSynchData(szBuf);
}
if(unit->getPos().dist(resourcePos) <= size) {
return true;
}

View File

@@ -248,6 +248,12 @@ void UnitUpdater::updateMove(Unit *unit) {
Vec2i pos= command->getUnit()!=NULL? command->getUnit()->getCenteredPos(): command->getPos();
if(SystemFlags::getSystemSettingType(SystemFlags::debugWorldSynch).enabled == true) {
char szBuf[4096]="";
sprintf(szBuf,"[%s::%s Line: %d] [updateMove] pos [%s] cmd [%s]",__FILE__,__FUNCTION__,__LINE__,pos.getString().c_str(),command->toString().c_str());
unit->logSynchData(szBuf);
}
TravelState tsValue = tsImpossible;
switch(this->game->getGameSettings()->getPathFinderType()) {
case pfBasic:
@@ -280,7 +286,7 @@ void UnitUpdater::updateMove(Unit *unit) {
// ==================== updateAttack ====================
void UnitUpdater::updateAttack(Unit *unit){
void UnitUpdater::updateAttack(Unit *unit) {
Command *command= unit->getCurrCommand();
const AttackCommandType *act= static_cast<const AttackCommandType*>(command->getCommandType());
Unit *target= NULL;
@@ -308,6 +314,13 @@ void UnitUpdater::updateAttack(Unit *unit){
pos= command->getPos();
}
if(SystemFlags::getSystemSettingType(SystemFlags::debugWorldSynch).enabled == true) {
char szBuf[4096]="";
sprintf(szBuf,"[%s::%s Line: %d] [updateAttack] pos [%s] unit->getPos() [%s]",__FILE__,__FUNCTION__,__LINE__,
pos.getString().c_str(),unit->getPos().getString().c_str());
unit->logSynchData(szBuf);
}
TravelState tsValue = tsImpossible;
switch(this->game->getGameSettings()->getPathFinderType()) {
case pfBasic:
@@ -378,6 +391,14 @@ void UnitUpdater::updateBuild(Unit *unit) {
//Vec2i buildPos = (command->getPos()-Vec2i(1));
Vec2i buildPos = map->findBestBuildApproach(unit->getPos(), command->getPos(), ut);
//Vec2i buildPos = (command->getPos() + Vec2i(ut->getSize() / 2));
if(SystemFlags::getSystemSettingType(SystemFlags::debugWorldSynch).enabled == true) {
char szBuf[4096]="";
sprintf(szBuf,"[%s::%s Line: %d] [updateBuild] unit->getPos() [%s] command->getPos() [%s] buildPos [%s]",
__FILE__,__FUNCTION__,__LINE__,unit->getPos().getString().c_str(),command->getPos().getString().c_str(),buildPos.getString().c_str());
unit->logSynchData(szBuf);
}
tsValue = pathFinder->findPath(unit, buildPos);
}
break;
@@ -619,6 +640,13 @@ void UnitUpdater::updateHarvest(Unit *unit) {
if(canHarvestDestPos == false) {
unit->setLastHarvestResourceTarget(&targetPos);
if(SystemFlags::getSystemSettingType(SystemFlags::debugWorldSynch).enabled == true) {
char szBuf[4096]="";
sprintf(szBuf,"[%s::%s Line: %d] [updateHarvest] unit->getPos() [%s] command->getPos() [%s]",
__FILE__,__FUNCTION__,__LINE__,unit->getPos().getString().c_str(),command->getPos().getString().c_str());
unit->logSynchData(szBuf);
}
//if not continue walking
bool wasStuck = false;
TravelState tsValue = tsImpossible;
@@ -690,6 +718,14 @@ void UnitUpdater::updateHarvest(Unit *unit) {
if(targetPos.x >= 0) {
//if not continue walking
if(SystemFlags::getSystemSettingType(SystemFlags::debugWorldSynch).enabled == true) {
char szBuf[4096]="";
sprintf(szBuf,"[%s::%s Line: %d] [updateHarvest #2] unit->getPos() [%s] command->getPos() [%s] targetPos [%s]",
__FILE__,__FUNCTION__,__LINE__,unit->getPos().getString().c_str(),command->getPos().getString().c_str(),targetPos.getString().c_str());
unit->logSynchData(szBuf);
}
wasStuck = false;
TravelState tsValue = tsImpossible;
switch(this->game->getGameSettings()->getPathFinderType()) {
@@ -735,6 +771,14 @@ void UnitUpdater::updateHarvest(Unit *unit) {
//if loaded, return to store
Unit *store= world->nearestStore(unit->getPos(), unit->getFaction()->getIndex(), unit->getLoadType());
if(store!=NULL) {
if(SystemFlags::getSystemSettingType(SystemFlags::debugWorldSynch).enabled == true) {
char szBuf[4096]="";
sprintf(szBuf,"[%s::%s Line: %d] [updateHarvest #3] unit->getPos() [%s] store->getCenteredPos() [%s]",
__FILE__,__FUNCTION__,__LINE__,unit->getPos().getString().c_str(),store->getCenteredPos().getString().c_str());
unit->logSynchData(szBuf);
}
TravelState tsValue = tsImpossible;
switch(this->game->getGameSettings()->getPathFinderType()) {
case pfBasic:
@@ -1073,6 +1117,12 @@ void UnitUpdater::updateRepair(Unit *unit) {
else {
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
if(SystemFlags::getSystemSettingType(SystemFlags::debugWorldSynch).enabled == true) {
char szBuf[4096]="";
sprintf(szBuf,"[%s::%s Line: %d] [updateRepair] unit->getPos() [%s] command->getPos()() [%s] repairPos [%s]",__FILE__,__FUNCTION__,__LINE__,unit->getPos().getString().c_str(),command->getPos().getString().c_str(),repairPos.getString().c_str());
unit->logSynchData(szBuf);
}
TravelState ts;
switch(this->game->getGameSettings()->getPathFinderType()) {
case pfBasic:
@@ -1464,7 +1514,6 @@ bool UnitUpdater::attackableOnRange(const Unit *unit, Unit **rangedPtr, const At
return unitOnRange(unit, range, rangedPtr, ast);
}
/*
bool UnitUpdater::findCachedCellsEnemies(Vec2i center, int range, int size, vector<Unit*> &enemies,
const AttackSkillType *ast, const Unit *unit,
const Unit *commandTarget) {
@@ -1491,7 +1540,6 @@ bool UnitUpdater::findCachedCellsEnemies(Vec2i center, int range, int size, vect
return result;
}
*/
void UnitUpdater::findEnemiesForCell(const AttackSkillType *ast, Cell *cell, const Unit *unit,
const Unit *commandTarget,vector<Unit*> &enemies) {
@@ -1536,12 +1584,12 @@ bool UnitUpdater::unitOnRange(const Unit *unit, int range, Unit **rangedPtr,
Vec2f floatCenter = unit->getFloatCenteredPos();
bool foundInCache = true;
//if(findCachedCellsEnemies(center,range,size,enemies,ast,
// unit,commandTarget) == false) {
if(findCachedCellsEnemies(center,range,size,enemies,ast,
unit,commandTarget) == false) {
foundInCache = false;
//nearby cells
//UnitRangeCellsLookupItem cacheItem;
UnitRangeCellsLookupItem cacheItem;
for(int i=center.x-range; i<center.x+range+size; ++i){
for(int j=center.y-range; j<center.y+range+size; ++j){
@@ -1554,17 +1602,17 @@ bool UnitUpdater::unitOnRange(const Unit *unit, int range, Unit **rangedPtr,
Cell *cell = map->getCell(i,j);
findEnemiesForCell(ast,cell,unit,commandTarget,enemies);
//cacheItem.rangeCellList.push_back(cell);
cacheItem.rangeCellList.push_back(cell);
}
}
}
// Ok update our caches with the latest info
//if(cacheItem.rangeCellList.size() > 0) {
// cacheItem.UnitRangeCellsLookupItemCacheTimerCountIndex = UnitRangeCellsLookupItemCacheTimerCount++;
// UnitRangeCellsLookupItemCache[center][size][range] = cacheItem;
//}
//}
if(cacheItem.rangeCellList.size() > 0) {
cacheItem.UnitRangeCellsLookupItemCacheTimerCountIndex = UnitRangeCellsLookupItemCacheTimerCount++;
UnitRangeCellsLookupItemCache[center][size][range] = cacheItem;
}
}
//attack enemies that can attack first
for(int i = 0; i< enemies.size(); ++i) {

View File

@@ -71,14 +71,14 @@ private:
Game *game;
RandomGen random;
//std::map<Vec2i, std::map<int, std::map<int, UnitRangeCellsLookupItem > > > UnitRangeCellsLookupItemCache;
std::map<Vec2i, std::map<int, std::map<int, UnitRangeCellsLookupItem > > > UnitRangeCellsLookupItemCache;
//std::map<int,ExploredCellsLookupKey> ExploredCellsLookupItemCacheTimer;
//int UnitRangeCellsLookupItemCacheTimerCount;
int UnitRangeCellsLookupItemCacheTimerCount;
//bool findCachedCellsEnemies(Vec2i center, int range,
// int size, vector<Unit*> &enemies,
// const AttackSkillType *ast, const Unit *unit,
// const Unit *commandTarget);
bool findCachedCellsEnemies(Vec2i center, int range,
int size, vector<Unit*> &enemies,
const AttackSkillType *ast, const Unit *unit,
const Unit *commandTarget);
void findEnemiesForCell(const AttackSkillType *ast, Cell *cell, const Unit *unit,
const Unit *commandTarget,vector<Unit*> &enemies);

View File

@@ -366,7 +366,8 @@ void SystemFlags::handleDebug(DebugType type, const char *fmt, ...) {
if(currentDebugLog.fileStream->is_open() == true) {
MutexSafeWrapper safeMutex(currentDebugLog.mutex);
if (type != debugPathFinder && type != debugError) {
// All items in the if clause we don't want timestamps
if (type != debugPathFinder && type != debugError && type != debugWorldSynch) {
(*currentDebugLog.fileStream) << "[" << szBuf2 << "] " << szBuf;
}
else if (type == debugError) {