mirror of
https://github.com/glest/glest-source.git
synced 2025-02-24 19:52:25 +01:00
- 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() {
|
||||
texture = NULL;
|
||||
lastResourceTargettListPurge = 0;
|
||||
}
|
||||
|
||||
Faction::~Faction() {
|
||||
@ -669,15 +670,21 @@ void Faction::resetResourceAmount(const ResourceType *rt){
|
||||
assert(false);
|
||||
}
|
||||
|
||||
void Faction::addResourceTargetToCache(const Vec2i &pos) {
|
||||
bool duplicateEntry = false;
|
||||
bool Faction::isResourceTargetInCache(const Vec2i &pos, bool incrementUseCounter) {
|
||||
bool result = false;
|
||||
if(cacheResourceTargetList.size() > 0) {
|
||||
std::map<Vec2i,int>::iterator iter = cacheResourceTargetList.find(pos);
|
||||
if(iter != cacheResourceTargetList.end()) {
|
||||
result = (iter != cacheResourceTargetList.end());
|
||||
if(result == true && incrementUseCounter == true) {
|
||||
iter->second++;
|
||||
duplicateEntry = true;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void Faction::addResourceTargetToCache(const Vec2i &pos) {
|
||||
bool duplicateEntry = isResourceTargetInCache(pos,true);
|
||||
if(duplicateEntry == false) {
|
||||
cacheResourceTargetList[pos] = 1;
|
||||
}
|
||||
@ -698,10 +705,12 @@ void Faction::addCloseResourceTargetToCache(const Vec2i &pos) {
|
||||
for(int j = -harvestDistance; j <= harvestDistance; ++j) {
|
||||
for(int k = -harvestDistance; k <= harvestDistance; ++k) {
|
||||
Vec2i newPos = pos + Vec2i(j,k);
|
||||
if(map->isInside(newPos.x, newPos.y)) {
|
||||
Resource *r= map->getSurfaceCell(map->toSurfCoords(newPos))->getResource();
|
||||
if(r != NULL) {
|
||||
addResourceTargetToCache(newPos);
|
||||
if(isResourceTargetInCache(newPos) == false) {
|
||||
if(map->isInside(newPos.x, newPos.y)) {
|
||||
Resource *r= map->getSurfaceCell(map->toSurfCoords(newPos))->getResource();
|
||||
if(r != NULL) {
|
||||
addResourceTargetToCache(newPos);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -744,29 +753,33 @@ Vec2i Faction::getClosestResourceTypeTargetFromCache(Unit *unit, const ResourceT
|
||||
|
||||
void Faction::cleanupResourceTypeTargetCache(std::vector<Vec2i> *deleteListPtr) {
|
||||
if(cacheResourceTargetList.size() > 0) {
|
||||
std::vector<Vec2i> deleteList;
|
||||
if(deleteListPtr != NULL) {
|
||||
deleteList = *deleteListPtr;
|
||||
}
|
||||
else {
|
||||
for(std::map<Vec2i,int>::iterator iter = cacheResourceTargetList.begin();
|
||||
iter != cacheResourceTargetList.end(); ++iter) {
|
||||
const Vec2i &cache = iter->first;
|
||||
if(deleteListPtr != NULL || difftime(time(NULL),lastResourceTargettListPurge) >= 120) {
|
||||
lastResourceTargettListPurge = time(NULL);
|
||||
std::vector<Vec2i> deleteList;
|
||||
|
||||
if(world->getMap()->getSurfaceCell(world->getMap()->toSurfCoords(cache)) != NULL) {
|
||||
Resource *resource = world->getMap()->getSurfaceCell(world->getMap()->toSurfCoords(cache))->getResource();
|
||||
if(resource == NULL) {
|
||||
if(deleteListPtr != NULL) {
|
||||
deleteList = *deleteListPtr;
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
else {
|
||||
deleteList.push_back(cache);
|
||||
}
|
||||
}
|
||||
}
|
||||
for(int i = 0; i < deleteList.size(); ++i) {
|
||||
Vec2i &cache = deleteList[i];
|
||||
cacheResourceTargetList.erase(cache);
|
||||
for(int i = 0; i < deleteList.size(); ++i) {
|
||||
Vec2i &cache = deleteList[i];
|
||||
cacheResourceTargetList.erase(cache);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -85,6 +85,7 @@ private:
|
||||
|
||||
std::map<Vec2i, std::vector<FactionPathSuccessCache> > successfulPathFinderTargetList;
|
||||
std::map<Vec2i,int> cacheResourceTargetList;
|
||||
time_t lastResourceTargettListPurge;
|
||||
|
||||
public:
|
||||
Faction();
|
||||
@ -156,6 +157,7 @@ public:
|
||||
std::vector<Vec2i> findCachedPath(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 removeResourceTargetFromCache(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->inBailOutAttempt = false;
|
||||
this->lastHarvestResourceTarget.first = Vec2i(0);
|
||||
this->lastBadHarvestListPurge = 0;
|
||||
|
||||
level= NULL;
|
||||
loadType= NULL;
|
||||
@ -1686,11 +1687,19 @@ void Unit::logSynchData(string source) {
|
||||
void Unit::addBadHarvestPos(const Vec2i &value) {
|
||||
Chrono chron;
|
||||
chron.start();
|
||||
badHarvestPosList.push_back(std::pair<Vec2i,Chrono>(value,chron));
|
||||
//badHarvestPosList.push_back(std::pair<Vec2i,Chrono>(value,chron));
|
||||
badHarvestPosList[value] = chron;
|
||||
cleanupOldBadHarvestPos();
|
||||
}
|
||||
|
||||
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) {
|
||||
const std::pair<Vec2i,Chrono> &item = badHarvestPosList[i];
|
||||
if(item.first == value) {
|
||||
@ -1699,12 +1708,21 @@ void Unit::removeBadHarvestPos(const Vec2i &value) {
|
||||
}
|
||||
}
|
||||
cleanupOldBadHarvestPos();
|
||||
*/
|
||||
|
||||
}
|
||||
|
||||
bool Unit::isBadHarvestPos(const Vec2i &value, bool checkPeerUnits) const {
|
||||
//cleanupOldBadHarvestPos();
|
||||
|
||||
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) {
|
||||
const std::pair<Vec2i,Chrono> &item = badHarvestPosList[i];
|
||||
if(item.first == value) {
|
||||
@ -1712,7 +1730,7 @@ bool Unit::isBadHarvestPos(const Vec2i &value, bool checkPeerUnits) const {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
*/
|
||||
if(result == false && checkPeerUnits == true) {
|
||||
// Check if any other units of similar type have this position tagged
|
||||
// as bad?
|
||||
@ -1732,6 +1750,21 @@ bool Unit::isBadHarvestPos(const Vec2i &value, bool checkPeerUnits) const {
|
||||
}
|
||||
|
||||
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) {
|
||||
const std::pair<Vec2i,Chrono> &item = badHarvestPosList[i];
|
||||
|
||||
@ -1741,6 +1774,7 @@ void Unit::cleanupOldBadHarvestPos() {
|
||||
badHarvestPosList.erase(badHarvestPosList.begin() + i);
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
void Unit::setLastHarvestResourceTarget(const Vec2i *pos) {
|
||||
|
@ -300,7 +300,9 @@ private:
|
||||
// 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
|
||||
// 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,std::vector<Vec2i> > currentTargetPathTaken;
|
||||
@ -449,8 +451,8 @@ public:
|
||||
bool getInBailOutAttempt() const { return inBailOutAttempt; }
|
||||
void setInBailOutAttempt(bool value) { inBailOutAttempt = value; }
|
||||
|
||||
std::vector<std::pair<Vec2i,Chrono> > getBadHarvestPosList() const { return badHarvestPosList; }
|
||||
void setBadHarvestPosList(std::vector<std::pair<Vec2i,Chrono> > value) { badHarvestPosList = value; }
|
||||
//std::vector<std::pair<Vec2i,Chrono> > getBadHarvestPosList() const { return badHarvestPosList; }
|
||||
//void setBadHarvestPosList(std::vector<std::pair<Vec2i,Chrono> > value) { badHarvestPosList = value; }
|
||||
void addBadHarvestPos(const Vec2i &value);
|
||||
void removeBadHarvestPos(const Vec2i &value);
|
||||
bool isBadHarvestPos(const Vec2i &value,bool checkPeerUnits=true) const;
|
||||
|
Loading…
x
Reference in New Issue
Block a user