From f18061a7eeb6b345671514fa73608f012654ee72 Mon Sep 17 00:00:00 2001 From: James McCulloch Date: Tue, 13 Jul 2010 11:05:35 +0000 Subject: [PATCH] fix build-site and store adjacency maps for potential building rotation fix potential annotated map update problem with morphing to a smaller UnitType --- source/glest_game/ai/cartographer.cpp | 29 +++++++++++-------- source/glest_game/ai/cartographer.h | 23 ++++++++------- source/glest_game/ai/route_planner.cpp | 8 +++-- source/glest_game/ai/route_planner.h | 2 +- source/glest_game/graphics/debug_renderer.cpp | 6 ++-- source/glest_game/world/unit_updater.cpp | 9 +++--- source/glest_game/world/unit_updater.h | 2 +- 7 files changed, 45 insertions(+), 34 deletions(-) diff --git a/source/glest_game/ai/cartographer.cpp b/source/glest_game/ai/cartographer.cpp index ea217d64..b563ff32 100644 --- a/source/glest_game/ai/cartographer.cpp +++ b/source/glest_game/ai/cartographer.cpp @@ -25,7 +25,7 @@ #include -#if _GAE_DEBUG_EDITION_ +#if DEBUG_RENDERING_ENABLED # include "debug_renderer.h" #endif @@ -177,8 +177,10 @@ void Cartographer::fixupResourceMaps(const ResourceType *rt, const Vec2i &pos) { PatchMap<1>* Cartographer::buildSiteMap(BuildSiteMapKey key) { PF_TRACE(); PatchMap<1> *sMap = siteMaps[key] = buildAdjacencyMap(key.buildingType, key.buildingPosition, - key.workerField, key.workerSize); -// IF_DEBUG_EDITION( debugAddBuildSiteMap(sMap); ) + key.buildingFacing, key.workerField, key.workerSize); +# ifdef DEBUG_RENDERING_ENABLED + debugAddBuildSiteMap(sMap); +# endif 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(); - BuildSiteMapKey key(ut, pos, worker->getCurrField(), worker->getType()->getSize()); + BuildSiteMapKey key(ut, pos, facing, worker->getCurrField(), worker->getType()->getSize()); return getSiteMap(key); } - void Cartographer::onStoreDestroyed(Unit *unit) { ///@todo fixme // delete storeMaps[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(); const Vec2i mapPos = pos + (OrdinalOffsets[odNorthWest] * size); 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)); while (rIter.more()) { 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); } } @@ -269,7 +272,8 @@ PatchMap<1>* Cartographer::buildAdjacencyMap(const UnitType *uType, const Vec2i return pMap; } -/*IF_DEBUG_EDITION( +#ifdef DEBUG_RENDERING_ENABLED + void Cartographer::debugAddBuildSiteMap(PatchMap<1> *siteMap) { Rectangle mapBounds = siteMap->getBounds(); 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) { Vec2i pos(mapBounds.x + lx, y); if (siteMap->getInfluence(pos)) { - g_debugRenderer.addBuildSiteCell(pos); + getDebugRenderer().addBuildSiteCell(pos); } } } } -) -*/ + +#endif + void Cartographer::tick() { PF_TRACE(); if (clusterMap->isDirty()) { diff --git a/source/glest_game/ai/cartographer.h b/source/glest_game/ai/cartographer.h index 2b9d4b45..83256bd0 100644 --- a/source/glest_game/ai/cartographer.h +++ b/source/glest_game/ai/cartographer.h @@ -59,11 +59,13 @@ struct StoreMapKey { struct BuildSiteMapKey { const UnitType *buildingType; Vec2i buildingPosition; + CardinalDir buildingFacing; Field workerField; int workerSize; - BuildSiteMapKey(const UnitType *type, const Vec2i &pos, Field f, int s) - : buildingType(type), buildingPosition(pos), workerField(f), workerSize(s) {} + BuildSiteMapKey(const UnitType *type, const Vec2i &pos, CardinalDir facing, Field f, int s) + : buildingType(type), buildingPosition(pos), buildingFacing(facing) + , workerField(f), workerSize(s) {} bool operator<(const BuildSiteMapKey &that) const { return (memcmp(this, &that, sizeof(BuildSiteMapKey)) < 0); @@ -115,19 +117,20 @@ private: void initResourceMap(ResourceMapKey key, PatchMap<1> *pMap); 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) { - return buildAdjacencyMap(uType, pos, fLand, 1); - } + PatchMap<1>* buildAdjacencyMap(const UnitType *uType, const Vec2i &pos, CardinalDir facing, Field f, int sz); PatchMap<1>* buildStoreMap(StoreMapKey key) { - return (storeMaps[key] = buildAdjacencyMap(key.storeUnit->getType(), - key.storeUnit->getPos(), key.workerField, key.workerSize)); + return (storeMaps[key] = + 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); +# ifdef DEBUG_RENDERING_ENABLED + void debugAddBuildSiteMap(PatchMap<1>*); +# endif + public: Cartographer(World *world); virtual ~Cartographer(); @@ -153,7 +156,7 @@ public: PatchMap<1>* getStoreMap(const Unit *store, const Unit *worker); 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 &iMap, const Vec2i &pos, float range); void buildGlestimalMap(Field f, V2iList &positions); diff --git a/source/glest_game/ai/route_planner.cpp b/source/glest_game/ai/route_planner.cpp index 909b278c..573da3c5 100644 --- a/source/glest_game/ai/route_planner.cpp +++ b/source/glest_game/ai/route_planner.cpp @@ -184,10 +184,12 @@ TravelState RoutePlanner::findPathToStore(Unit *unit, const Unit *store) { 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(); - PMap1Goal goal(world->getCartographer()->getSiteMap(buildingType, buildingPos, unit)); - return findPathToGoal(unit, goal, buildingPos + Vec2i(buildingType->getSize() / 2)); + PatchMap<1> *pMap = world->getCartographer()->getSiteMap(bType, bPos, bFacing, unit); + PMap1Goal goal(pMap); + return findPathToGoal(unit, goal, bPos + Vec2i(bType->getSize() / 2)); } diff --git a/source/glest_game/ai/route_planner.h b/source/glest_game/ai/route_planner.h index a0001a8e..d734584d 100644 --- a/source/glest_game/ai/route_planner.h +++ b/source/glest_game/ai/route_planner.h @@ -159,7 +159,7 @@ public: 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; diff --git a/source/glest_game/graphics/debug_renderer.cpp b/source/glest_game/graphics/debug_renderer.cpp index 8d30cf9e..bda67ee4 100644 --- a/source/glest_game/graphics/debug_renderer.cpp +++ b/source/glest_game/graphics/debug_renderer.cpp @@ -131,12 +131,12 @@ DebugRenderer::DebugRenderer() { showFrustum = captureFrustum = gridTextures = - buildSiteMaps = influenceMap = - false; - AAStarTextures = HAAStarOverlay = + false; + + buildSiteMaps = true; } diff --git a/source/glest_game/world/unit_updater.cpp b/source/glest_game/world/unit_updater.cpp index e1ccd561..baa06112 100644 --- a/source/glest_game/world/unit_updater.cpp +++ b/source/glest_game/world/unit_updater.cpp @@ -287,7 +287,7 @@ void UnitUpdater::updateBuild(Unit *unit){ //if not building const UnitType *ut= command->getUnitType(); - switch (routePlanner->findPathToBuildSite(unit, ut, command->getPos())) { + switch (routePlanner->findPathToBuildSite(unit, ut, command->getPos(), command->getFacing())) { case PathFinder::tsOnTheWay: unit->setCurrSkill(bct->getMoveSkillType()); break; @@ -383,8 +383,7 @@ void UnitUpdater::updateHarvest(Unit *unit){ Resource *r= map->getSurfaceCell(Map::toSurfCoords(command->getPos()))->getResource(); if(r!=NULL && hct->canHarvest(r->getType())){ //if can harvest dest. pos - if(/*unit->getPos().dist(command->getPos())isResourceNear(unit->getPos(), unit->getType()->getSize(), r->getType(), targetPos)) { + if (map->isResourceNear(unit->getPos(), unit->getType()->getSize(), r->getType(), targetPos)) { //if it finds resources it starts harvesting unit->setCurrSkill(hct->getHarvestSkillType()); unit->setTargetPos(targetPos); @@ -635,6 +634,7 @@ void UnitUpdater::updateMorph(Unit *unit){ unit->update2(); if(unit->getProgress2()>mct->getProduced()->getProductionTime()){ + int oldSize = unit->getType()->getSize(); bool needMapUpdate = unit->getType()->isMobile() != mct->getMorphUnit()->isMobile(); //finish the command @@ -644,7 +644,8 @@ void UnitUpdater::updateMorph(Unit *unit){ gui->onSelectionChanged(); } 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); } diff --git a/source/glest_game/world/unit_updater.h b/source/glest_game/world/unit_updater.h index ea364042..bf21a2d6 100644 --- a/source/glest_game/world/unit_updater.h +++ b/source/glest_game/world/unit_updater.h @@ -43,7 +43,7 @@ private: private: static const int maxResSearchRadius= 10; - static const int harvestDistance= 5; + //static const int harvestDistance= 5; static const int ultraResourceFactor= 3; static const int megaResourceFactor= 4;