diff --git a/lib/int3.h b/lib/int3.h index b094654d8..41a8dd908 100644 --- a/lib/int3.h +++ b/lib/int3.h @@ -155,4 +155,22 @@ struct ShashInt3 }; static const int3 dirs[] = { int3(0,1,0),int3(0,-1,0),int3(-1,0,0),int3(+1,0,0), - int3(1,1,0),int3(-1,1,0),int3(1,-1,0),int3(-1,-1,0) }; \ No newline at end of file + int3(1,1,0),int3(-1,1,0),int3(1,-1,0),int3(-1,-1,0) }; + +//FIXME: make sure it's container and not just any +template +int3 findClosestTile (Container & container, int3 dest) +{ + int3 result(-1,-1,-1); + ui32 distance = std::numeric_limits::max(); + for (int3 tile : container) + { + ui32 currentDistance = dest.dist2dSQ(tile); + if (currentDistance < distance) + { + result = tile; + distance = currentDistance; + } + } + return result; +} \ No newline at end of file diff --git a/lib/rmg/CMapGenerator.cpp b/lib/rmg/CMapGenerator.cpp index cf2c65636..23b9242f0 100644 --- a/lib/rmg/CMapGenerator.cpp +++ b/lib/rmg/CMapGenerator.cpp @@ -253,8 +253,8 @@ void CMapGenerator::createConnections() setOccupied (guardPos, ETileType::FREE); //just in case monster is too weak to spawn zoneA->addMonster (this, guardPos, connection.getGuardStrength()); //TODO: set value according to template //zones can make paths only in their own area - zoneA->crunchPath (this, guardPos, zoneA->getPos(), zoneA->getId()); //make connection towards our zone center - zoneB->crunchPath (this, guardPos, zoneB->getPos(), zoneB->getId()); //make connection towards other zone center + zoneA->crunchPath (this, guardPos, zoneA->getPos(), zoneA->getId(), zoneA->getFreePaths()); //make connection towards our zone center + zoneB->crunchPath (this, guardPos, zoneB->getPos(), zoneB->getId(), zoneB->getFreePaths()); //make connection towards other zone center break; //we're done with this connection } } diff --git a/lib/rmg/CRmgTemplateZone.cpp b/lib/rmg/CRmgTemplateZone.cpp index c8a9dacb2..f1d969bde 100644 --- a/lib/rmg/CRmgTemplateZone.cpp +++ b/lib/rmg/CRmgTemplateZone.cpp @@ -313,7 +313,6 @@ void CRmgTemplateZone::setCenter(const float3 &f) center = float3 (std::min(std::max(f.x, 0.f), 1.f), std::min(std::max(f.y, 0.f), 1.f), f.z); } - bool CRmgTemplateZone::pointIsIn(int x, int y) { return true; @@ -338,6 +337,11 @@ std::set CRmgTemplateZone::getTileInfo () const return tileinfo; } +std::set* CRmgTemplateZone::getFreePaths() +{ + return &freePaths; +} + void CRmgTemplateZone::createBorder(CMapGenerator* gen) { for (auto tile : tileinfo) @@ -651,6 +655,7 @@ bool CRmgTemplateZone::createTreasurePile (CMapGenerator* gen, int3 &pos) int3 zoneCenter = getPos(); int3 closestTile = int3(-1,-1,-1); float minDistance = 1e10; + for (auto treasure : treasures) { if (zoneCenter.dist2d(treasure.first) < minDistance) @@ -1180,8 +1185,9 @@ bool CRmgTemplateZone::guardObject(CMapGenerator* gen, CGObjectInstance* object, for (auto tile : tiles) { //crunching path may fail if center of teh zone is dirrectly over wide object - if (crunchPath (gen, tile, getPos(), id)) //make sure object is accessible before surrounding it with blocked tiles + if (crunchPath (gen, tile, findClosestTile(freePaths, tile), id, &freePaths)) //required objects will contitute our core free paths { + //make sure object is accessible before surrounding it with blocked tiles guardTile = tile; break; } diff --git a/lib/rmg/CRmgTemplateZone.h b/lib/rmg/CRmgTemplateZone.h index 2ebd79589..3eb277b94 100644 --- a/lib/rmg/CRmgTemplateZone.h +++ b/lib/rmg/CRmgTemplateZone.h @@ -149,6 +149,7 @@ public: std::vector getConnections() const; void addTreasureInfo(CTreasureInfo & info); std::vector getTreasureInfo(); + std::set* getFreePaths(); ObjectInfo getRandomObject (CMapGenerator* gen, ui32 value); @@ -182,7 +183,7 @@ private: float3 center; std::set tileinfo; //irregular area assined to zone std::vector connections; //list of adjacent zones - std::map alreadyConnected; //TODO: allow multiple connections between two zones? + std::set freePaths; //core paths of free tiles that all other objects will be linked to bool pointIsIn(int x, int y); void addAllPossibleObjects (CMapGenerator* gen); //add objects, including zone-specific, to possibleObjects