- updated to units harvesting so they try to harvest different resources when blocked if other resources are close by.

This commit is contained in:
Mark Vejvoda 2010-10-21 19:26:14 +00:00
parent 3b5630bb73
commit ae367fb252
4 changed files with 45 additions and 3 deletions

View File

@ -602,6 +602,19 @@ void Faction::addResourceTargetToCache(const Vec2i &pos) {
cleanupResourceTypeTargetCache();
}
void Faction::removeResourceTargetFromCache(const Vec2i &pos) {
if(cacheResourceTargetList.size() > 0) {
for(int i = 0; i < cacheResourceTargetList.size(); ++i) {
const Vec2i &cache = cacheResourceTargetList[i];
if(cache == pos) {
cacheResourceTargetList.erase(cacheResourceTargetList.begin() + i);
break;
}
}
}
cleanupResourceTypeTargetCache();
}
Vec2i Faction::getClosestResourceTypeTargetFromCache(Unit *unit, const ResourceType *type) {
Vec2i result(-1);
if(cacheResourceTargetList.size() > 0) {
@ -614,7 +627,29 @@ Vec2i Faction::getClosestResourceTypeTargetFromCache(Unit *unit, const ResourceT
const Resource *resource = sc->getResource();
if(resource->getType() != NULL && resource->getType() == type) {
if(result.x < 0 || unit->getPos().dist(cache) < unit->getPos().dist(result)) {
result = cache;
if(unit->isBadHarvestPos(cache) == false) {
result = cache;
}
else {
const int harvestDistance = 5;
int size = unit->getType()->getSize();
for(int j = -harvestDistance; j <= size; ++j) {
for(int k = -harvestDistance; k <= size; ++k) {
Vec2i newPos = unit->getPos() + Vec2i(j,k);
if(map->isInside(newPos.x, newPos.y)) {
Resource *r= map->getSurfaceCell(map->toSurfCoords(newPos))->getResource();
if(r != NULL) {
if(r->getType() == type) {
if(unit->isBadHarvestPos(newPos) == false) {
result= newPos;
}
}
}
}
}
}
}
}
}
}

View File

@ -157,6 +157,7 @@ public:
void addCachedPath(const Vec2i &target, Unit *unit);
void addResourceTargetToCache(const Vec2i &pos);
void removeResourceTargetFromCache(const Vec2i &pos);
Vec2i getClosestResourceTypeTargetFromCache(Unit *unit, const ResourceType *type);
void cleanupResourceTypeTargetCache();

View File

@ -272,6 +272,9 @@ bool Map::isResourceNear(const Vec2i &pos, const ResourceType *rt, Vec2i &resour
if(fallbackToPeersHarvestingSameResource == true && unit != NULL) {
// Look for another unit that is currently harvesting the same resource
// type right now
/* This should not be needed due to the check below
*
for(int i = 0; i < unit->getFaction()->getUnitCount(); ++i) {
Unit *peerUnit = unit->getFaction()->getUnit(i);
if( peerUnit != NULL && peerUnit->getId() != unit->getId() &&
@ -293,6 +296,7 @@ bool Map::isResourceNear(const Vec2i &pos, const ResourceType *rt, Vec2i &resour
}
}
}
*/
// Check the faction cache for a known position where we can harvest

View File

@ -771,7 +771,7 @@ void UnitUpdater::updateHarvest(Unit *unit) {
}
//world->changePosCells(unit,unit->getPos()+unit->getDest());
if(map->isNextTo(unit->getPos(), store)){
if(map->isNextTo(unit->getPos(), store)) {
//update resources
int resourceAmount= unit->getLoadCount();
@ -800,7 +800,8 @@ void UnitUpdater::updateHarvest(Unit *unit) {
//if working
//unit->setLastHarvestResourceTarget(NULL);
SurfaceCell *sc= map->getSurfaceCell(Map::toSurfCoords(unit->getTargetPos()));
const Vec2i unitTargetPos = unit->getTargetPos();
SurfaceCell *sc= map->getSurfaceCell(Map::toSurfCoords(unitTargetPos));
Resource *r= sc->getResource();
if (r != NULL) {
@ -821,6 +822,7 @@ void UnitUpdater::updateHarvest(Unit *unit) {
if (r->decAmount(1)) {
const ResourceType *rt = r->getType();
sc->deleteResource();
unit->getFaction()->removeResourceTargetFromCache(unitTargetPos);
switch(this->game->getGameSettings()->getPathFinderType()) {
case pfBasic: