- added a resource harvest success cache for stuck units to look at when they cannot harvest.

This commit is contained in:
Mark Vejvoda 2010-10-20 07:28:27 +00:00
parent ddb4c42bf5
commit 6104dedc76
5 changed files with 73 additions and 3 deletions

View File

@ -584,6 +584,57 @@ void Faction::resetResourceAmount(const ResourceType *rt){
assert(false);
}
void Faction::addResourceTypeTargetToCache(const ResourceType *type, const Vec2i &pos) {
bool duplicateEntry = false;
if(cacheResourceTypeTargetList.size() > 0) {
for(int i = 0; i < cacheResourceTypeTargetList.size(); ++i) {
std::pair<const ResourceType *, Vec2i> &cache = cacheResourceTypeTargetList[i];
if(cache.first == type && cache.second == pos) {
duplicateEntry = true;
break;
}
}
}
if(duplicateEntry == false) {
cacheResourceTypeTargetList.push_back(make_pair<const ResourceType *, Vec2i>(type,pos));
}
cleanupResourceTypeTargetCache();
}
Vec2i Faction::getClosestResourceTypeTargetFromCache(Unit *unit, const ResourceType *type) {
Vec2i result(-1);
if(cacheResourceTypeTargetList.size() > 0) {
for(int i = 0; i < cacheResourceTypeTargetList.size(); ++i) {
std::pair<const ResourceType *, Vec2i> &cache = cacheResourceTypeTargetList[i];
Resource *resource = world->getMap()->getSurfaceCell(world->getMap()->toSurfCoords(cache.second))->getResource();
if(resource != NULL && cache.first == type) {
if(result.x < 0 || unit->getPos().dist(cache.second) < unit->getPos().dist(result)) {
result = cache.second;
}
}
}
}
cleanupResourceTypeTargetCache();
return result;
}
void Faction::cleanupResourceTypeTargetCache() {
if(cacheResourceTypeTargetList.size() > 0) {
for(int i = cacheResourceTypeTargetList.size() - 1; i >= 0; --i) {
std::pair<const ResourceType *, Vec2i> &cache = cacheResourceTypeTargetList[i];
Resource *resource = world->getMap()->getSurfaceCell(world->getMap()->toSurfCoords(cache.second))->getResource();
if(resource == NULL) {
cacheResourceTypeTargetList.erase(cacheResourceTypeTargetList.begin() + i);
}
}
}
}
std::vector<Vec2i> Faction::findCachedPath(const Vec2i &target, Unit *unit) {
std::vector<Vec2i> result;
if(successfulPathFinderTargetList.find(target) == successfulPathFinderTargetList.end()) {

View File

@ -84,6 +84,7 @@ private:
bool thisFaction;
std::map<Vec2i, std::vector<FactionPathSuccessCache> > successfulPathFinderTargetList;
std::vector<std::pair<const ResourceType *, Vec2i> > cacheResourceTypeTargetList;
public:
Faction();
@ -155,6 +156,11 @@ public:
std::vector<Vec2i> findCachedPath(const Vec2i &target, Unit *unit);
void addCachedPath(const Vec2i &target, Unit *unit);
//std::map<const ResourceType *, Vec2i > cacheResourceTypeTargetList;
void addResourceTypeTargetToCache(const ResourceType *type, const Vec2i &pos);
Vec2i getClosestResourceTypeTargetFromCache(Unit *unit, const ResourceType *type);
void cleanupResourceTypeTargetCache();
std::string toString() const;
private:

View File

@ -678,8 +678,10 @@ bool Unit::anyCommand() const{
//return current command, assert that there is always one command
Command *Unit::getCurrCommand() const{
assert(!commands.empty());
return commands.front();
if(commands.empty() == false) {
return commands.front();
}
return NULL;
}
void Unit::replaceCurrCommand(Command *cmd) {

View File

@ -278,7 +278,7 @@ bool Map::isResourceNear(const Vec2i &pos, const ResourceType *rt, Vec2i &resour
peerUnit->getLoadType() == rt &&
peerUnit->getCurrCommand() != NULL) {
if(unit->getPos().dist(peerUnit->getCurrCommand()->getPos()) <= 30) {
if(unit->getPos().dist(peerUnit->getCurrCommand()->getPos()) <= 40) {
if(i == 0 || (unit->getPos().dist(peerUnit->getCurrCommand()->getPos()) < unit->getPos().dist(resourcePos))) {
resourcePos = peerUnit->getCurrCommand()->getPos();
}
@ -290,6 +290,15 @@ 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(unit->getPos().dist(resourcePos) <= 5) {
return true;
}
}
}
return false;
}

View File

@ -616,6 +616,7 @@ void UnitUpdater::updateHarvest(Unit *unit) {
unit->setTargetPos(targetPos);
command->setPos(targetPos);
unit->setLoadCount(0);
unit->getFaction()->addResourceTypeTargetToCache(r->getType(), targetPos);
switch(this->game->getGameSettings()->getPathFinderType()) {
case pfBasic:
@ -675,6 +676,7 @@ void UnitUpdater::updateHarvest(Unit *unit) {
unit->setTargetPos(targetPos);
command->setPos(targetPos);
unit->setLoadCount(0);
unit->getFaction()->addResourceTypeTargetToCache(r->getType(), targetPos);
switch(this->game->getGameSettings()->getPathFinderType()) {
case pfBasic: