- updated to units harvesting so they try to harvest different resources when blocked if other resources are close by.
This commit is contained in:
parent
3b5630bb73
commit
ae367fb252
|
@ -602,6 +602,19 @@ void Faction::addResourceTargetToCache(const Vec2i &pos) {
|
||||||
cleanupResourceTypeTargetCache();
|
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 Faction::getClosestResourceTypeTargetFromCache(Unit *unit, const ResourceType *type) {
|
||||||
Vec2i result(-1);
|
Vec2i result(-1);
|
||||||
if(cacheResourceTargetList.size() > 0) {
|
if(cacheResourceTargetList.size() > 0) {
|
||||||
|
@ -614,7 +627,29 @@ Vec2i Faction::getClosestResourceTypeTargetFromCache(Unit *unit, const ResourceT
|
||||||
const Resource *resource = sc->getResource();
|
const Resource *resource = sc->getResource();
|
||||||
if(resource->getType() != NULL && resource->getType() == type) {
|
if(resource->getType() != NULL && resource->getType() == type) {
|
||||||
if(result.x < 0 || unit->getPos().dist(cache) < unit->getPos().dist(result)) {
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -157,6 +157,7 @@ public:
|
||||||
void addCachedPath(const Vec2i &target, Unit *unit);
|
void addCachedPath(const Vec2i &target, Unit *unit);
|
||||||
|
|
||||||
void addResourceTargetToCache(const Vec2i &pos);
|
void addResourceTargetToCache(const Vec2i &pos);
|
||||||
|
void removeResourceTargetFromCache(const Vec2i &pos);
|
||||||
Vec2i getClosestResourceTypeTargetFromCache(Unit *unit, const ResourceType *type);
|
Vec2i getClosestResourceTypeTargetFromCache(Unit *unit, const ResourceType *type);
|
||||||
void cleanupResourceTypeTargetCache();
|
void cleanupResourceTypeTargetCache();
|
||||||
|
|
||||||
|
|
|
@ -272,6 +272,9 @@ bool Map::isResourceNear(const Vec2i &pos, const ResourceType *rt, Vec2i &resour
|
||||||
if(fallbackToPeersHarvestingSameResource == true && unit != NULL) {
|
if(fallbackToPeersHarvestingSameResource == true && unit != NULL) {
|
||||||
// Look for another unit that is currently harvesting the same resource
|
// Look for another unit that is currently harvesting the same resource
|
||||||
// type right now
|
// type right now
|
||||||
|
|
||||||
|
/* This should not be needed due to the check below
|
||||||
|
*
|
||||||
for(int i = 0; i < unit->getFaction()->getUnitCount(); ++i) {
|
for(int i = 0; i < unit->getFaction()->getUnitCount(); ++i) {
|
||||||
Unit *peerUnit = unit->getFaction()->getUnit(i);
|
Unit *peerUnit = unit->getFaction()->getUnit(i);
|
||||||
if( peerUnit != NULL && peerUnit->getId() != unit->getId() &&
|
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
|
// Check the faction cache for a known position where we can harvest
|
||||||
|
|
|
@ -771,7 +771,7 @@ void UnitUpdater::updateHarvest(Unit *unit) {
|
||||||
}
|
}
|
||||||
|
|
||||||
//world->changePosCells(unit,unit->getPos()+unit->getDest());
|
//world->changePosCells(unit,unit->getPos()+unit->getDest());
|
||||||
if(map->isNextTo(unit->getPos(), store)){
|
if(map->isNextTo(unit->getPos(), store)) {
|
||||||
|
|
||||||
//update resources
|
//update resources
|
||||||
int resourceAmount= unit->getLoadCount();
|
int resourceAmount= unit->getLoadCount();
|
||||||
|
@ -800,7 +800,8 @@ void UnitUpdater::updateHarvest(Unit *unit) {
|
||||||
//if working
|
//if working
|
||||||
//unit->setLastHarvestResourceTarget(NULL);
|
//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();
|
Resource *r= sc->getResource();
|
||||||
|
|
||||||
if (r != NULL) {
|
if (r != NULL) {
|
||||||
|
@ -821,6 +822,7 @@ void UnitUpdater::updateHarvest(Unit *unit) {
|
||||||
if (r->decAmount(1)) {
|
if (r->decAmount(1)) {
|
||||||
const ResourceType *rt = r->getType();
|
const ResourceType *rt = r->getType();
|
||||||
sc->deleteResource();
|
sc->deleteResource();
|
||||||
|
unit->getFaction()->removeResourceTargetFromCache(unitTargetPos);
|
||||||
|
|
||||||
switch(this->game->getGameSettings()->getPathFinderType()) {
|
switch(this->game->getGameSettings()->getPathFinderType()) {
|
||||||
case pfBasic:
|
case pfBasic:
|
||||||
|
|
Loading…
Reference in New Issue
Block a user