- some performance improvements with the new cached resource data
This commit is contained in:
parent
c3d41f5082
commit
7cb1d6a5b1
|
@ -33,6 +33,7 @@ namespace Glest{ namespace Game{
|
||||||
|
|
||||||
Faction::Faction() {
|
Faction::Faction() {
|
||||||
texture = NULL;
|
texture = NULL;
|
||||||
|
lastResourceTargettListPurge = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
Faction::~Faction() {
|
Faction::~Faction() {
|
||||||
|
@ -669,15 +670,21 @@ void Faction::resetResourceAmount(const ResourceType *rt){
|
||||||
assert(false);
|
assert(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Faction::addResourceTargetToCache(const Vec2i &pos) {
|
bool Faction::isResourceTargetInCache(const Vec2i &pos, bool incrementUseCounter) {
|
||||||
bool duplicateEntry = false;
|
bool result = false;
|
||||||
if(cacheResourceTargetList.size() > 0) {
|
if(cacheResourceTargetList.size() > 0) {
|
||||||
std::map<Vec2i,int>::iterator iter = cacheResourceTargetList.find(pos);
|
std::map<Vec2i,int>::iterator iter = cacheResourceTargetList.find(pos);
|
||||||
if(iter != cacheResourceTargetList.end()) {
|
result = (iter != cacheResourceTargetList.end());
|
||||||
|
if(result == true && incrementUseCounter == true) {
|
||||||
iter->second++;
|
iter->second++;
|
||||||
duplicateEntry = true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Faction::addResourceTargetToCache(const Vec2i &pos) {
|
||||||
|
bool duplicateEntry = isResourceTargetInCache(pos,true);
|
||||||
if(duplicateEntry == false) {
|
if(duplicateEntry == false) {
|
||||||
cacheResourceTargetList[pos] = 1;
|
cacheResourceTargetList[pos] = 1;
|
||||||
}
|
}
|
||||||
|
@ -698,10 +705,12 @@ void Faction::addCloseResourceTargetToCache(const Vec2i &pos) {
|
||||||
for(int j = -harvestDistance; j <= harvestDistance; ++j) {
|
for(int j = -harvestDistance; j <= harvestDistance; ++j) {
|
||||||
for(int k = -harvestDistance; k <= harvestDistance; ++k) {
|
for(int k = -harvestDistance; k <= harvestDistance; ++k) {
|
||||||
Vec2i newPos = pos + Vec2i(j,k);
|
Vec2i newPos = pos + Vec2i(j,k);
|
||||||
if(map->isInside(newPos.x, newPos.y)) {
|
if(isResourceTargetInCache(newPos) == false) {
|
||||||
Resource *r= map->getSurfaceCell(map->toSurfCoords(newPos))->getResource();
|
if(map->isInside(newPos.x, newPos.y)) {
|
||||||
if(r != NULL) {
|
Resource *r= map->getSurfaceCell(map->toSurfCoords(newPos))->getResource();
|
||||||
addResourceTargetToCache(newPos);
|
if(r != NULL) {
|
||||||
|
addResourceTargetToCache(newPos);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -744,29 +753,33 @@ Vec2i Faction::getClosestResourceTypeTargetFromCache(Unit *unit, const ResourceT
|
||||||
|
|
||||||
void Faction::cleanupResourceTypeTargetCache(std::vector<Vec2i> *deleteListPtr) {
|
void Faction::cleanupResourceTypeTargetCache(std::vector<Vec2i> *deleteListPtr) {
|
||||||
if(cacheResourceTargetList.size() > 0) {
|
if(cacheResourceTargetList.size() > 0) {
|
||||||
std::vector<Vec2i> deleteList;
|
if(deleteListPtr != NULL || difftime(time(NULL),lastResourceTargettListPurge) >= 120) {
|
||||||
if(deleteListPtr != NULL) {
|
lastResourceTargettListPurge = time(NULL);
|
||||||
deleteList = *deleteListPtr;
|
std::vector<Vec2i> deleteList;
|
||||||
}
|
|
||||||
else {
|
|
||||||
for(std::map<Vec2i,int>::iterator iter = cacheResourceTargetList.begin();
|
|
||||||
iter != cacheResourceTargetList.end(); ++iter) {
|
|
||||||
const Vec2i &cache = iter->first;
|
|
||||||
|
|
||||||
if(world->getMap()->getSurfaceCell(world->getMap()->toSurfCoords(cache)) != NULL) {
|
if(deleteListPtr != NULL) {
|
||||||
Resource *resource = world->getMap()->getSurfaceCell(world->getMap()->toSurfCoords(cache))->getResource();
|
deleteList = *deleteListPtr;
|
||||||
if(resource == NULL) {
|
}
|
||||||
|
else {
|
||||||
|
for(std::map<Vec2i,int>::iterator iter = cacheResourceTargetList.begin();
|
||||||
|
iter != cacheResourceTargetList.end(); ++iter) {
|
||||||
|
const Vec2i &cache = iter->first;
|
||||||
|
|
||||||
|
if(world->getMap()->getSurfaceCell(world->getMap()->toSurfCoords(cache)) != NULL) {
|
||||||
|
Resource *resource = world->getMap()->getSurfaceCell(world->getMap()->toSurfCoords(cache))->getResource();
|
||||||
|
if(resource == NULL) {
|
||||||
|
deleteList.push_back(cache);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
deleteList.push_back(cache);
|
deleteList.push_back(cache);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
deleteList.push_back(cache);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
for(int i = 0; i < deleteList.size(); ++i) {
|
||||||
for(int i = 0; i < deleteList.size(); ++i) {
|
Vec2i &cache = deleteList[i];
|
||||||
Vec2i &cache = deleteList[i];
|
cacheResourceTargetList.erase(cache);
|
||||||
cacheResourceTargetList.erase(cache);
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -85,6 +85,7 @@ private:
|
||||||
|
|
||||||
std::map<Vec2i, std::vector<FactionPathSuccessCache> > successfulPathFinderTargetList;
|
std::map<Vec2i, std::vector<FactionPathSuccessCache> > successfulPathFinderTargetList;
|
||||||
std::map<Vec2i,int> cacheResourceTargetList;
|
std::map<Vec2i,int> cacheResourceTargetList;
|
||||||
|
time_t lastResourceTargettListPurge;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Faction();
|
Faction();
|
||||||
|
@ -156,6 +157,7 @@ public:
|
||||||
std::vector<Vec2i> findCachedPath(const Vec2i &target, Unit *unit);
|
std::vector<Vec2i> findCachedPath(const Vec2i &target, Unit *unit);
|
||||||
void addCachedPath(const Vec2i &target, Unit *unit);
|
void addCachedPath(const Vec2i &target, Unit *unit);
|
||||||
|
|
||||||
|
bool isResourceTargetInCache(const Vec2i &pos,bool incrementUseCounter=false);
|
||||||
void addResourceTargetToCache(const Vec2i &pos);
|
void addResourceTargetToCache(const Vec2i &pos);
|
||||||
void removeResourceTargetFromCache(const Vec2i &pos);
|
void removeResourceTargetFromCache(const Vec2i &pos);
|
||||||
void addCloseResourceTargetToCache(const Vec2i &pos);
|
void addCloseResourceTargetToCache(const Vec2i &pos);
|
||||||
|
|
|
@ -184,6 +184,7 @@ Unit::Unit(int id, UnitPathInterface *unitpath, const Vec2i &pos, const UnitType
|
||||||
this->screenPos = Vec3f(0.0);
|
this->screenPos = Vec3f(0.0);
|
||||||
this->inBailOutAttempt = false;
|
this->inBailOutAttempt = false;
|
||||||
this->lastHarvestResourceTarget.first = Vec2i(0);
|
this->lastHarvestResourceTarget.first = Vec2i(0);
|
||||||
|
this->lastBadHarvestListPurge = 0;
|
||||||
|
|
||||||
level= NULL;
|
level= NULL;
|
||||||
loadType= NULL;
|
loadType= NULL;
|
||||||
|
@ -1686,11 +1687,19 @@ void Unit::logSynchData(string source) {
|
||||||
void Unit::addBadHarvestPos(const Vec2i &value) {
|
void Unit::addBadHarvestPos(const Vec2i &value) {
|
||||||
Chrono chron;
|
Chrono chron;
|
||||||
chron.start();
|
chron.start();
|
||||||
badHarvestPosList.push_back(std::pair<Vec2i,Chrono>(value,chron));
|
//badHarvestPosList.push_back(std::pair<Vec2i,Chrono>(value,chron));
|
||||||
|
badHarvestPosList[value] = chron;
|
||||||
cleanupOldBadHarvestPos();
|
cleanupOldBadHarvestPos();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Unit::removeBadHarvestPos(const Vec2i &value) {
|
void Unit::removeBadHarvestPos(const Vec2i &value) {
|
||||||
|
std::map<Vec2i,Chrono>::iterator iter = badHarvestPosList.find(value);
|
||||||
|
if(iter != badHarvestPosList.end()) {
|
||||||
|
badHarvestPosList.erase(value);
|
||||||
|
}
|
||||||
|
cleanupOldBadHarvestPos();
|
||||||
|
|
||||||
|
/*
|
||||||
for(int i = 0; i < badHarvestPosList.size(); ++i) {
|
for(int i = 0; i < badHarvestPosList.size(); ++i) {
|
||||||
const std::pair<Vec2i,Chrono> &item = badHarvestPosList[i];
|
const std::pair<Vec2i,Chrono> &item = badHarvestPosList[i];
|
||||||
if(item.first == value) {
|
if(item.first == value) {
|
||||||
|
@ -1699,12 +1708,21 @@ void Unit::removeBadHarvestPos(const Vec2i &value) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
cleanupOldBadHarvestPos();
|
cleanupOldBadHarvestPos();
|
||||||
|
*/
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Unit::isBadHarvestPos(const Vec2i &value, bool checkPeerUnits) const {
|
bool Unit::isBadHarvestPos(const Vec2i &value, bool checkPeerUnits) const {
|
||||||
//cleanupOldBadHarvestPos();
|
//cleanupOldBadHarvestPos();
|
||||||
|
|
||||||
bool result = false;
|
bool result = false;
|
||||||
|
|
||||||
|
std::map<Vec2i,Chrono>::const_iterator iter = badHarvestPosList.find(value);
|
||||||
|
if(iter != badHarvestPosList.end()) {
|
||||||
|
result = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
for(int i = 0; i < badHarvestPosList.size(); ++i) {
|
for(int i = 0; i < badHarvestPosList.size(); ++i) {
|
||||||
const std::pair<Vec2i,Chrono> &item = badHarvestPosList[i];
|
const std::pair<Vec2i,Chrono> &item = badHarvestPosList[i];
|
||||||
if(item.first == value) {
|
if(item.first == value) {
|
||||||
|
@ -1712,7 +1730,7 @@ bool Unit::isBadHarvestPos(const Vec2i &value, bool checkPeerUnits) const {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
if(result == false && checkPeerUnits == true) {
|
if(result == false && checkPeerUnits == true) {
|
||||||
// Check if any other units of similar type have this position tagged
|
// Check if any other units of similar type have this position tagged
|
||||||
// as bad?
|
// as bad?
|
||||||
|
@ -1732,6 +1750,21 @@ bool Unit::isBadHarvestPos(const Vec2i &value, bool checkPeerUnits) const {
|
||||||
}
|
}
|
||||||
|
|
||||||
void Unit::cleanupOldBadHarvestPos() {
|
void Unit::cleanupOldBadHarvestPos() {
|
||||||
|
if(difftime(time(NULL),lastBadHarvestListPurge) >= 240) {
|
||||||
|
lastBadHarvestListPurge = time(NULL);
|
||||||
|
std::vector<Vec2i> purgeList;
|
||||||
|
for(std::map<Vec2i,Chrono>::iterator iter = badHarvestPosList.begin(); iter != badHarvestPosList.end(); iter++) {
|
||||||
|
if(iter->second.getMillis() >= 2400000) {
|
||||||
|
purgeList.push_back(iter->first);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for(int i = 0; i < purgeList.size(); ++i) {
|
||||||
|
const Vec2i &item = purgeList[i];
|
||||||
|
badHarvestPosList.erase(item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
for(int i = badHarvestPosList.size() - 1; i >= 0; --i) {
|
for(int i = badHarvestPosList.size() - 1; i >= 0; --i) {
|
||||||
const std::pair<Vec2i,Chrono> &item = badHarvestPosList[i];
|
const std::pair<Vec2i,Chrono> &item = badHarvestPosList[i];
|
||||||
|
|
||||||
|
@ -1741,6 +1774,7 @@ void Unit::cleanupOldBadHarvestPos() {
|
||||||
badHarvestPosList.erase(badHarvestPosList.begin() + i);
|
badHarvestPosList.erase(badHarvestPosList.begin() + i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
void Unit::setLastHarvestResourceTarget(const Vec2i *pos) {
|
void Unit::setLastHarvestResourceTarget(const Vec2i *pos) {
|
||||||
|
|
|
@ -300,7 +300,9 @@ private:
|
||||||
// constantly getting blocked from getting to the resource so this
|
// constantly getting blocked from getting to the resource so this
|
||||||
// list may be used to tell areas of the game to ignore those cells for a
|
// list may be used to tell areas of the game to ignore those cells for a
|
||||||
// period of time
|
// period of time
|
||||||
std::vector<std::pair<Vec2i,Chrono> > badHarvestPosList;
|
//std::vector<std::pair<Vec2i,Chrono> > badHarvestPosList;
|
||||||
|
std::map<Vec2i,Chrono> badHarvestPosList;
|
||||||
|
time_t lastBadHarvestListPurge;
|
||||||
std::pair<Vec2i,Chrono> lastHarvestResourceTarget;
|
std::pair<Vec2i,Chrono> lastHarvestResourceTarget;
|
||||||
|
|
||||||
std::pair<Vec2i,std::vector<Vec2i> > currentTargetPathTaken;
|
std::pair<Vec2i,std::vector<Vec2i> > currentTargetPathTaken;
|
||||||
|
@ -449,8 +451,8 @@ public:
|
||||||
bool getInBailOutAttempt() const { return inBailOutAttempt; }
|
bool getInBailOutAttempt() const { return inBailOutAttempt; }
|
||||||
void setInBailOutAttempt(bool value) { inBailOutAttempt = value; }
|
void setInBailOutAttempt(bool value) { inBailOutAttempt = value; }
|
||||||
|
|
||||||
std::vector<std::pair<Vec2i,Chrono> > getBadHarvestPosList() const { return badHarvestPosList; }
|
//std::vector<std::pair<Vec2i,Chrono> > getBadHarvestPosList() const { return badHarvestPosList; }
|
||||||
void setBadHarvestPosList(std::vector<std::pair<Vec2i,Chrono> > value) { badHarvestPosList = value; }
|
//void setBadHarvestPosList(std::vector<std::pair<Vec2i,Chrono> > value) { badHarvestPosList = value; }
|
||||||
void addBadHarvestPos(const Vec2i &value);
|
void addBadHarvestPos(const Vec2i &value);
|
||||||
void removeBadHarvestPos(const Vec2i &value);
|
void removeBadHarvestPos(const Vec2i &value);
|
||||||
bool isBadHarvestPos(const Vec2i &value,bool checkPeerUnits=true) const;
|
bool isBadHarvestPos(const Vec2i &value,bool checkPeerUnits=true) const;
|
||||||
|
|
Loading…
Reference in New Issue