fix build-site and store adjacency maps for potential building rotation

fix potential annotated map update problem with morphing to a smaller UnitType
This commit is contained in:
James McCulloch 2010-07-13 11:05:35 +00:00
parent ff61cd9c36
commit f18061a7ee
7 changed files with 45 additions and 34 deletions

View File

@ -25,7 +25,7 @@
#include <algorithm> #include <algorithm>
#if _GAE_DEBUG_EDITION_ #if DEBUG_RENDERING_ENABLED
# include "debug_renderer.h" # include "debug_renderer.h"
#endif #endif
@ -177,8 +177,10 @@ void Cartographer::fixupResourceMaps(const ResourceType *rt, const Vec2i &pos) {
PatchMap<1>* Cartographer::buildSiteMap(BuildSiteMapKey key) { PatchMap<1>* Cartographer::buildSiteMap(BuildSiteMapKey key) {
PF_TRACE(); PF_TRACE();
PatchMap<1> *sMap = siteMaps[key] = buildAdjacencyMap(key.buildingType, key.buildingPosition, PatchMap<1> *sMap = siteMaps[key] = buildAdjacencyMap(key.buildingType, key.buildingPosition,
key.workerField, key.workerSize); key.buildingFacing, key.workerField, key.workerSize);
// IF_DEBUG_EDITION( debugAddBuildSiteMap(sMap); ) # ifdef DEBUG_RENDERING_ENABLED
debugAddBuildSiteMap(sMap);
# endif
return sMap; return sMap;
} }
@ -216,20 +218,21 @@ PatchMap<1>* Cartographer::getSiteMap(BuildSiteMapKey key) {
} }
PatchMap<1>* Cartographer::getSiteMap(const UnitType *ut, const Vec2i &pos, Unit *worker) { PatchMap<1>* Cartographer::getSiteMap(const UnitType *ut, const Vec2i &pos,
CardinalDir facing, Unit *worker) {
PF_TRACE(); PF_TRACE();
BuildSiteMapKey key(ut, pos, worker->getCurrField(), worker->getType()->getSize()); BuildSiteMapKey key(ut, pos, facing, worker->getCurrField(), worker->getType()->getSize());
return getSiteMap(key); return getSiteMap(key);
} }
void Cartographer::onStoreDestroyed(Unit *unit) { void Cartographer::onStoreDestroyed(Unit *unit) {
///@todo fixme ///@todo fixme
// delete storeMaps[unit]; // delete storeMaps[unit];
// storeMaps.erase(unit); // storeMaps.erase(unit);
} }
PatchMap<1>* Cartographer::buildAdjacencyMap(const UnitType *uType, const Vec2i &pos, Field f, int size) { PatchMap<1>* Cartographer::buildAdjacencyMap(const UnitType *uType, const Vec2i &pos,
CardinalDir facing, Field f, int size) {
PF_TRACE(); PF_TRACE();
const Vec2i mapPos = pos + (OrdinalOffsets[odNorthWest] * size); const Vec2i mapPos = pos + (OrdinalOffsets[odNorthWest] * size);
const int sx = pos.x; const int sx = pos.x;
@ -246,7 +249,7 @@ PatchMap<1>* Cartographer::buildAdjacencyMap(const UnitType *uType, const Vec2i
Util::RectIterator rIter(pos, pos + Vec2i(uType->getSize() - 1)); Util::RectIterator rIter(pos, pos + Vec2i(uType->getSize() - 1));
while (rIter.more()) { while (rIter.more()) {
Vec2i gpos = rIter.next(); Vec2i gpos = rIter.next();
if (!uType->hasCellMap() || uType->getCellMapCell(gpos.x - sx, gpos.y - sy, CardinalDir::NORTH)) { if (!uType->hasCellMap() || uType->getCellMapCell(gpos.x - sx, gpos.y - sy, facing)) {
tmpMap.setInfluence(gpos, 1); tmpMap.setInfluence(gpos, 1);
} }
} }
@ -269,7 +272,8 @@ PatchMap<1>* Cartographer::buildAdjacencyMap(const UnitType *uType, const Vec2i
return pMap; return pMap;
} }
/*IF_DEBUG_EDITION( #ifdef DEBUG_RENDERING_ENABLED
void Cartographer::debugAddBuildSiteMap(PatchMap<1> *siteMap) { void Cartographer::debugAddBuildSiteMap(PatchMap<1> *siteMap) {
Rectangle mapBounds = siteMap->getBounds(); Rectangle mapBounds = siteMap->getBounds();
for (int ly = 0; ly < mapBounds.h; ++ly) { for (int ly = 0; ly < mapBounds.h; ++ly) {
@ -277,13 +281,14 @@ PatchMap<1>* Cartographer::buildAdjacencyMap(const UnitType *uType, const Vec2i
for (int lx = 0; lx < mapBounds.w; ++lx) { for (int lx = 0; lx < mapBounds.w; ++lx) {
Vec2i pos(mapBounds.x + lx, y); Vec2i pos(mapBounds.x + lx, y);
if (siteMap->getInfluence(pos)) { if (siteMap->getInfluence(pos)) {
g_debugRenderer.addBuildSiteCell(pos); getDebugRenderer().addBuildSiteCell(pos);
} }
} }
} }
} }
)
*/ #endif
void Cartographer::tick() { void Cartographer::tick() {
PF_TRACE(); PF_TRACE();
if (clusterMap->isDirty()) { if (clusterMap->isDirty()) {

View File

@ -59,11 +59,13 @@ struct StoreMapKey {
struct BuildSiteMapKey { struct BuildSiteMapKey {
const UnitType *buildingType; const UnitType *buildingType;
Vec2i buildingPosition; Vec2i buildingPosition;
CardinalDir buildingFacing;
Field workerField; Field workerField;
int workerSize; int workerSize;
BuildSiteMapKey(const UnitType *type, const Vec2i &pos, Field f, int s) BuildSiteMapKey(const UnitType *type, const Vec2i &pos, CardinalDir facing, Field f, int s)
: buildingType(type), buildingPosition(pos), workerField(f), workerSize(s) {} : buildingType(type), buildingPosition(pos), buildingFacing(facing)
, workerField(f), workerSize(s) {}
bool operator<(const BuildSiteMapKey &that) const { bool operator<(const BuildSiteMapKey &that) const {
return (memcmp(this, &that, sizeof(BuildSiteMapKey)) < 0); return (memcmp(this, &that, sizeof(BuildSiteMapKey)) < 0);
@ -115,19 +117,20 @@ private:
void initResourceMap(ResourceMapKey key, PatchMap<1> *pMap); void initResourceMap(ResourceMapKey key, PatchMap<1> *pMap);
void fixupResourceMaps(const ResourceType *rt, const Vec2i &pos); void fixupResourceMaps(const ResourceType *rt, const Vec2i &pos);
PatchMap<1>* buildAdjacencyMap(const UnitType *uType, const Vec2i &pos, Field f, int sz); PatchMap<1>* buildAdjacencyMap(const UnitType *uType, const Vec2i &pos, CardinalDir facing, Field f, int sz);
PatchMap<1>* buildAdjacencyMap(const UnitType *uType, const Vec2i &pos) {
return buildAdjacencyMap(uType, pos, fLand, 1);
}
PatchMap<1>* buildStoreMap(StoreMapKey key) { PatchMap<1>* buildStoreMap(StoreMapKey key) {
return (storeMaps[key] = buildAdjacencyMap(key.storeUnit->getType(), return (storeMaps[key] =
key.storeUnit->getPos(), key.workerField, key.workerSize)); buildAdjacencyMap(key.storeUnit->getType(), key.storeUnit->getPos(),
key.storeUnit->getModelFacing(), key.workerField, key.workerSize));
} }
// IF_DEBUG_EDITION( void debugAddBuildSiteMap(PatchMap<1>*); )
PatchMap<1>* buildSiteMap(BuildSiteMapKey key); PatchMap<1>* buildSiteMap(BuildSiteMapKey key);
# ifdef DEBUG_RENDERING_ENABLED
void debugAddBuildSiteMap(PatchMap<1>*);
# endif
public: public:
Cartographer(World *world); Cartographer(World *world);
virtual ~Cartographer(); virtual ~Cartographer();
@ -153,7 +156,7 @@ public:
PatchMap<1>* getStoreMap(const Unit *store, const Unit *worker); PatchMap<1>* getStoreMap(const Unit *store, const Unit *worker);
PatchMap<1>* getSiteMap(BuildSiteMapKey key); PatchMap<1>* getSiteMap(BuildSiteMapKey key);
PatchMap<1>* getSiteMap(const UnitType *ut, const Vec2i &pos, Unit *worker); PatchMap<1>* getSiteMap(const UnitType *ut, const Vec2i &pos, CardinalDir facing, Unit *worker);
void adjustGlestimalMap(Field f, TypeMap<float> &iMap, const Vec2i &pos, float range); void adjustGlestimalMap(Field f, TypeMap<float> &iMap, const Vec2i &pos, float range);
void buildGlestimalMap(Field f, V2iList &positions); void buildGlestimalMap(Field f, V2iList &positions);

View File

@ -184,10 +184,12 @@ TravelState RoutePlanner::findPathToStore(Unit *unit, const Unit *store) {
return findPathToGoal(unit, goal, target); return findPathToGoal(unit, goal, target);
} }
TravelState RoutePlanner::findPathToBuildSite(Unit *unit, const UnitType *buildingType, const Vec2i &buildingPos) { TravelState RoutePlanner::findPathToBuildSite(Unit *unit, const UnitType *bType,
const Vec2i &bPos, CardinalDir bFacing) {
PF_TRACE(); PF_TRACE();
PMap1Goal goal(world->getCartographer()->getSiteMap(buildingType, buildingPos, unit)); PatchMap<1> *pMap = world->getCartographer()->getSiteMap(bType, bPos, bFacing, unit);
return findPathToGoal(unit, goal, buildingPos + Vec2i(buildingType->getSize() / 2)); PMap1Goal goal(pMap);
return findPathToGoal(unit, goal, bPos + Vec2i(bType->getSize() / 2));
} }

View File

@ -159,7 +159,7 @@ public:
TravelState findPathToStore(Unit *unit, const Unit *store); TravelState findPathToStore(Unit *unit, const Unit *store);
TravelState findPathToBuildSite(Unit *unit, const UnitType *buildingType, const Vec2i &buildingPos); TravelState findPathToBuildSite(Unit *unit, const UnitType *bType, const Vec2i &bPos, CardinalDir bFacing);
bool isLegalMove(Unit *unit, const Vec2i &pos) const; bool isLegalMove(Unit *unit, const Vec2i &pos) const;

View File

@ -131,12 +131,12 @@ DebugRenderer::DebugRenderer() {
showFrustum = showFrustum =
captureFrustum = captureFrustum =
gridTextures = gridTextures =
buildSiteMaps =
influenceMap = influenceMap =
false;
AAStarTextures = AAStarTextures =
HAAStarOverlay = HAAStarOverlay =
false;
buildSiteMaps =
true; true;
} }

View File

@ -287,7 +287,7 @@ void UnitUpdater::updateBuild(Unit *unit){
//if not building //if not building
const UnitType *ut= command->getUnitType(); const UnitType *ut= command->getUnitType();
switch (routePlanner->findPathToBuildSite(unit, ut, command->getPos())) { switch (routePlanner->findPathToBuildSite(unit, ut, command->getPos(), command->getFacing())) {
case PathFinder::tsOnTheWay: case PathFinder::tsOnTheWay:
unit->setCurrSkill(bct->getMoveSkillType()); unit->setCurrSkill(bct->getMoveSkillType());
break; break;
@ -383,8 +383,7 @@ void UnitUpdater::updateHarvest(Unit *unit){
Resource *r= map->getSurfaceCell(Map::toSurfCoords(command->getPos()))->getResource(); Resource *r= map->getSurfaceCell(Map::toSurfCoords(command->getPos()))->getResource();
if(r!=NULL && hct->canHarvest(r->getType())){ if(r!=NULL && hct->canHarvest(r->getType())){
//if can harvest dest. pos //if can harvest dest. pos
if(/*unit->getPos().dist(command->getPos())<harvestDistance &&*/ if (map->isResourceNear(unit->getPos(), unit->getType()->getSize(), r->getType(), targetPos)) {
map->isResourceNear(unit->getPos(), unit->getType()->getSize(), r->getType(), targetPos)) {
//if it finds resources it starts harvesting //if it finds resources it starts harvesting
unit->setCurrSkill(hct->getHarvestSkillType()); unit->setCurrSkill(hct->getHarvestSkillType());
unit->setTargetPos(targetPos); unit->setTargetPos(targetPos);
@ -635,6 +634,7 @@ void UnitUpdater::updateMorph(Unit *unit){
unit->update2(); unit->update2();
if(unit->getProgress2()>mct->getProduced()->getProductionTime()){ if(unit->getProgress2()>mct->getProduced()->getProductionTime()){
int oldSize = unit->getType()->getSize();
bool needMapUpdate = unit->getType()->isMobile() != mct->getMorphUnit()->isMobile(); bool needMapUpdate = unit->getType()->isMobile() != mct->getMorphUnit()->isMobile();
//finish the command //finish the command
@ -644,7 +644,8 @@ void UnitUpdater::updateMorph(Unit *unit){
gui->onSelectionChanged(); gui->onSelectionChanged();
} }
if (needMapUpdate) { if (needMapUpdate) {
world->getCartographer()->updateMapMetrics(unit->getPos(), unit->getType()->getSize()); int size = std::max(oldSize, unit->getType()->getSize());
world->getCartographer()->updateMapMetrics(unit->getPos(), size);
} }
scriptManager->onUnitCreated(unit); scriptManager->onUnitCreated(unit);
} }

View File

@ -43,7 +43,7 @@ private:
private: private:
static const int maxResSearchRadius= 10; static const int maxResSearchRadius= 10;
static const int harvestDistance= 5; //static const int harvestDistance= 5;
static const int ultraResourceFactor= 3; static const int ultraResourceFactor= 3;
static const int megaResourceFactor= 4; static const int megaResourceFactor= 4;