1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
|
/*
* ConnectionsPlacer.cpp, part of VCMI engine
*
* Authors: listed in file AUTHORS in main folder
*
* License: GNU General Public License v2.0 or later
* Full text of license available in license.txt file, in main folder
*
*/
#include "StdInc.h"
#include "ConnectionsPlacer.h"
#include "../CMapGenerator.h"
#include "../RmgMap.h"
#include "../../TerrainHandler.h"
#include "../../mapObjectConstructors/AObjectTypeHandler.h"
#include "../../mapObjectConstructors/CObjectClassesHandler.h"
#include "../../mapObjects/CGCreature.h"
#include "../../mapping/CMapEditManager.h"
#include "../RmgObject.h"
#include "ObjectManager.h"
#include "../Functions.h"
#include "RoadPlacer.h"
#include "../TileInfo.h"
#include "WaterAdopter.h"
#include "WaterProxy.h"
#include "TownPlacer.h"
#include <vstd/RNG.h>
VCMI_LIB_NAMESPACE_BEGIN
std::pair<Zone::Lock, Zone::Lock> ConnectionsPlacer::lockZones(std::shared_ptr<Zone> otherZone)
{
if (zone.getId() == otherZone->getId())
return {};
while (true)
{
auto lock1 = Zone::Lock(zone.areaMutex, boost::try_to_lock);
auto lock2 = Zone::Lock(otherZone->areaMutex, boost::try_to_lock);
if (lock1.owns_lock() && lock2.owns_lock())
{
return { std::move(lock1), std::move(lock2) };
}
}
}
void ConnectionsPlacer::process()
{
collectNeighbourZones();
auto diningPhilosophers = [this](std::function<void(const rmg::ZoneConnection&)> f)
{
for (auto& c : dConnections)
{
if (c.getZoneA() == c.getZoneB())
{
// Zone can always be connected to itself, but only by monolith pair
RecursiveLock lock(externalAccessMutex);
if (!vstd::contains(dCompleted, c))
{
placeMonolithConnection(c);
continue;
}
}
auto otherZone = map.getZones().at(c.getZoneB());
auto* cp = otherZone->getModificator<ConnectionsPlacer>();
while (cp)
{
RecursiveLock lock1(externalAccessMutex, boost::try_to_lock);
RecursiveLock lock2(cp->externalAccessMutex, boost::try_to_lock);
if (lock1.owns_lock() && lock2.owns_lock())
{
if (!vstd::contains(dCompleted, c))
{
f(c);
}
break;
}
}
}
};
diningPhilosophers([this](const rmg::ZoneConnection& c)
{
forcePortalConnection(c);
});
diningPhilosophers([this](const rmg::ZoneConnection& c)
{
selfSideDirectConnection(c);
});
createBorder();
diningPhilosophers([this](const rmg::ZoneConnection& c)
{
selfSideIndirectConnection(c);
});
}
void ConnectionsPlacer::init()
{
DEPENDENCY(WaterAdopter);
DEPENDENCY(TownPlacer);
POSTFUNCTION(RoadPlacer);
POSTFUNCTION(ObjectManager);
auto id = zone.getId();
for(auto c : map.getMapGenOptions().getMapTemplate()->getConnectedZoneIds())
{
// Only consider connected zones
if (c.getZoneA() == id || c.getZoneB() == id)
{
addConnection(c);
}
}
}
void ConnectionsPlacer::addConnection(const rmg::ZoneConnection& connection)
{
dConnections.push_back(connection);
}
void ConnectionsPlacer::otherSideConnection(const rmg::ZoneConnection & connection)
{
dCompleted.push_back(connection);
}
void ConnectionsPlacer::forcePortalConnection(const rmg::ZoneConnection & connection)
{
// This should always succeed
if (connection.getConnectionType() == rmg::EConnectionType::FORCE_PORTAL)
{
placeMonolithConnection(connection);
}
}
void ConnectionsPlacer::selfSideDirectConnection(const rmg::ZoneConnection & connection)
{
bool success = false;
auto otherZoneId = connection.getOtherZoneId(zone.getId());
auto & otherZone = map.getZones().at(otherZoneId);
bool createRoad = shouldGenerateRoad(connection);
//1. Try to make direct connection
//Do if it's not prohibited by terrain settings
const auto * ourTerrain = VLC->terrainTypeHandler->getById(zone.getTerrainType());
const auto * otherTerrain = VLC->terrainTypeHandler->getById(otherZone->getTerrainType());
bool directProhibited = vstd::contains(ourTerrain->prohibitTransitions, otherZone->getTerrainType())
|| vstd::contains(otherTerrain->prohibitTransitions, zone.getTerrainType());
auto lock = lockZones(otherZone);
auto directConnectionIterator = dNeighbourZones.find(otherZoneId);
if (directConnectionIterator != dNeighbourZones.end())
{
if (connection.getConnectionType() == rmg::EConnectionType::WIDE)
{
for (auto borderPos : directConnectionIterator->second)
{
//TODO: Refactor common code with direct connection
int3 potentialPos = zone.areaPossible()->nearest(borderPos);
assert(borderPos != potentialPos);
auto safetyGap = rmg::Area({ potentialPos });
safetyGap.unite(safetyGap.getBorderOutside());
safetyGap.intersect(zone.areaPossible().get());
if (!safetyGap.empty())
{
safetyGap.intersect(otherZone->areaPossible().get());
if (safetyGap.empty())
{
rmg::Area border(zone.area()->getBorder());
border.unite(otherZone->area()->getBorder());
auto costFunction = [&border](const int3& s, const int3& d)
{
return 1.f / (1.f + border.distanceSqr(d));
};
auto ourArea = zone.areaForRoads();
auto theirArea = otherZone->areaForRoads();
theirArea.add(potentialPos);
rmg::Path ourPath(ourArea);
rmg::Path theirPath(theirArea);
ourPath.connect(zone.freePaths().get());
ourPath = ourPath.search(potentialPos, true, costFunction);
theirPath.connect(otherZone->freePaths().get());
theirPath = theirPath.search(potentialPos, true, costFunction);
if (ourPath.valid() && theirPath.valid())
{
zone.connectPath(ourPath);
otherZone->connectPath(theirPath);
otherZone->getModificator<ObjectManager>()->updateDistances(potentialPos);
success = true;
break;
}
}
}
}
}
}
if (connection.getConnectionType() == rmg::EConnectionType::FICTIVE ||
connection.getConnectionType() == rmg::EConnectionType::REPULSIVE)
{
//Fictive or repulsive connections are not real, take no action
dCompleted.push_back(connection);
return;
}
float maxDist = -10e6;
if(!success && !directProhibited && directConnectionIterator != dNeighbourZones.end())
{
int3 guardPos(-1, -1, -1);
int3 roadNode;
for (auto borderPos : directConnectionIterator->second)
{
int3 potentialPos = zone.areaPossible()->nearest(borderPos);
assert(borderPos != potentialPos);
//Check if guard pos doesn't touch any 3rd zone. This would create unwanted passage to 3rd zone
bool adjacentZone = false;
map.foreach_neighbour(potentialPos, [this, &adjacentZone, otherZoneId](int3 & pos)
{
auto zoneId = map.getZoneID(pos);
if (zoneId != zone.getId() && zoneId != otherZoneId)
{
adjacentZone = true;
}
});
if (adjacentZone)
{
continue;
}
//Take into account distance to objects from both sides
float dist = std::min(map.getTileInfo(potentialPos).getNearestObjectDistance(),
map.getTileInfo(borderPos).getNearestObjectDistance());
if (dist > 3) //Don't place guards at adjacent tiles
{
auto safetyGap = rmg::Area({ potentialPos });
safetyGap.unite(safetyGap.getBorderOutside());
safetyGap.intersect(zone.areaPossible().get());
if (!safetyGap.empty())
{
safetyGap.intersect(otherZone->areaPossible().get());
if (safetyGap.empty())
{
float distanceToCenter = zone.getPos().dist2d(potentialPos) * otherZone->getPos().dist2d(potentialPos);
auto localDist = (dist - distanceToCenter) * //Prefer close to zone center
(std::max(distanceToCenter, dist) / std::min(distanceToCenter, dist));
//Distance to center dominates and is negative, so imbalanced proportions will result in huge penalty
if (localDist > maxDist)
{
maxDist = localDist;
guardPos = potentialPos;
roadNode = borderPos;
}
}
}
}
}
if(guardPos.valid())
{
assert(zone.getModificator<ObjectManager>());
auto & manager = *zone.getModificator<ObjectManager>();
auto * monsterType = manager.chooseGuard(connection.getGuardStrength(), true);
rmg::Area border(zone.area()->getBorder());
border.unite(otherZone->area()->getBorder());
auto localCostFunction = rmg::Path::createCurvedCostFunction(zone.area()->getBorder());
auto otherCostFunction = rmg::Path::createCurvedCostFunction(otherZone->area()->getBorder());
auto ourArea = zone.areaForRoads();
auto theirArea = otherZone->areaForRoads();
theirArea.add(guardPos);
rmg::Path ourPath(ourArea);
rmg::Path theirPath(theirArea);
ourPath.connect(zone.freePaths().get());
ourPath = ourPath.search(guardPos, true, localCostFunction);
theirPath.connect(otherZone->freePaths().get());
theirPath = theirPath.search(guardPos, true, otherCostFunction);
if(ourPath.valid() && theirPath.valid())
{
zone.connectPath(ourPath);
otherZone->connectPath(theirPath);
if(monsterType)
{
rmg::Object monster(*monsterType);
monster.setPosition(guardPos);
manager.placeObject(monster, false, true);
//Place objects away from the monster in the other zone, too
otherZone->getModificator<ObjectManager>()->updateDistances(monster);
}
else
{
//Update distances from empty passage, too
zone.areaPossible()->erase(guardPos);
zone.freePaths()->add(guardPos);
map.setOccupied(guardPos, ETileType::FREE);
manager.updateDistances(guardPos);
otherZone->getModificator<ObjectManager>()->updateDistances(guardPos);
}
if (createRoad)
{
assert(zone.getModificator<RoadPlacer>());
zone.getModificator<RoadPlacer>()->addRoadNode(guardPos);
assert(otherZone->getModificator<RoadPlacer>());
otherZone->getModificator<RoadPlacer>()->addRoadNode(roadNode);
}
assert(otherZone->getModificator<ConnectionsPlacer>());
otherZone->getModificator<ConnectionsPlacer>()->otherSideConnection(connection);
success = true;
}
}
}
//2. connect via water
bool waterMode = map.getMapGenOptions().getWaterContent() != EWaterContent::NONE;
if(waterMode && zone.isUnderground() == otherZone->isUnderground())
{
if(generator.getZoneWater() && generator.getZoneWater()->getModificator<WaterProxy>())
{
if(generator.getZoneWater()->getModificator<WaterProxy>()->waterKeepConnection(connection, createRoad))
{
assert(otherZone->getModificator<ConnectionsPlacer>());
otherZone->getModificator<ConnectionsPlacer>()->otherSideConnection(connection);
success = true;
}
}
}
if(success)
dCompleted.push_back(connection);
}
void ConnectionsPlacer::selfSideIndirectConnection(const rmg::ZoneConnection & connection)
{
bool success = false;
auto otherZoneId = (connection.getZoneA() == zone.getId() ? connection.getZoneB() : connection.getZoneA());
auto & otherZone = map.getZones().at(otherZoneId);
bool allowRoad = shouldGenerateRoad(connection);
//3. place subterrain gates
if(zone.isUnderground() != otherZone->isUnderground())
{
int3 zShift(0, 0, zone.getPos().z - otherZone->getPos().z);
auto lock = lockZones(otherZone);
std::scoped_lock doubleLock(zone.areaMutex, otherZone->areaMutex);
auto commonArea = zone.areaPossible().get() * (otherZone->areaPossible().get() + zShift);
if(!commonArea.empty())
{
assert(zone.getModificator<ObjectManager>());
auto & manager = *zone.getModificator<ObjectManager>();
assert(otherZone->getModificator<ObjectManager>());
auto & managerOther = *otherZone->getModificator<ObjectManager>();
auto factory = VLC->objtypeh->getHandlerFor(Obj::SUBTERRANEAN_GATE, 0);
auto * gate1 = factory->create(map.mapInstance->cb, nullptr);
auto * gate2 = factory->create(map.mapInstance->cb, nullptr);
rmg::Object rmgGate1(*gate1);
rmg::Object rmgGate2(*gate2);
rmgGate1.setTemplate(zone.getTerrainType(), zone.getRand());
rmgGate2.setTemplate(otherZone->getTerrainType(), zone.getRand());
bool guarded1 = manager.addGuard(rmgGate1, connection.getGuardStrength(), true);
bool guarded2 = managerOther.addGuard(rmgGate2, connection.getGuardStrength(), true);
int minDist = 3;
rmg::Path path2(otherZone->area().get());
rmg::Path path1 = manager.placeAndConnectObject(commonArea, rmgGate1, [this, minDist, &path2, &rmgGate1, &zShift, guarded2, &managerOther, &rmgGate2 ](const int3 & tile)
{
auto ti = map.getTileInfo(tile);
auto otherTi = map.getTileInfo(tile - zShift);
float dist = ti.getNearestObjectDistance();
float otherDist = otherTi.getNearestObjectDistance();
if(dist < minDist || otherDist < minDist)
return -1.f;
//This could fail is accessibleArea is below the map
rmg::Area toPlace(rmgGate1.getArea());
toPlace.unite(toPlace.getBorderOutside()); // Add a bit of extra space around
toPlace.erase_if([this](const int3 & tile)
{
return !map.isOnMap(tile);
});
toPlace.translate(-zShift);
path2 = managerOther.placeAndConnectObject(toPlace, rmgGate2, minDist, guarded2, true, ObjectManager::OptimizeType::NONE);
return path2.valid() ? (dist * otherDist) : -1.f;
}, guarded1, true, ObjectManager::OptimizeType::DISTANCE);
if(path1.valid() && path2.valid())
{
manager.placeObject(rmgGate1, guarded1, true, allowRoad);
managerOther.placeObject(rmgGate2, guarded2, true, allowRoad);
replaceWithCurvedPath(path1, zone, rmgGate1.getVisitablePosition());
replaceWithCurvedPath(path2, *otherZone, rmgGate2.getVisitablePosition());
zone.connectPath(path1);
otherZone->connectPath(path2);
assert(otherZone->getModificator<ConnectionsPlacer>());
otherZone->getModificator<ConnectionsPlacer>()->otherSideConnection(connection);
success = true;
}
}
}
//4. place monoliths/portals
if(!success)
{
placeMonolithConnection(connection);
}
}
void ConnectionsPlacer::placeMonolithConnection(const rmg::ZoneConnection & connection)
{
auto otherZoneId = (connection.getZoneA() == zone.getId() ? connection.getZoneB() : connection.getZoneA());
auto & otherZone = map.getZones().at(otherZoneId);
bool allowRoad = shouldGenerateRoad(connection);
auto factory = VLC->objtypeh->getHandlerFor(Obj::MONOLITH_TWO_WAY, generator.getNextMonlithIndex());
auto * teleport1 = factory->create(map.mapInstance->cb, nullptr);
auto * teleport2 = factory->create(map.mapInstance->cb, nullptr);
RequiredObjectInfo obj1(teleport1, connection.getGuardStrength(), allowRoad);
RequiredObjectInfo obj2(teleport2, connection.getGuardStrength(), allowRoad);
zone.getModificator<ObjectManager>()->addRequiredObject(obj1);
otherZone->getModificator<ObjectManager>()->addRequiredObject(obj2);
dCompleted.push_back(connection);
assert(otherZone->getModificator<ConnectionsPlacer>());
otherZone->getModificator<ConnectionsPlacer>()->otherSideConnection(connection);
}
void ConnectionsPlacer::collectNeighbourZones()
{
auto border = zone.area()->getBorderOutside();
for(const auto & i : border)
{
if(!map.isOnMap(i))
continue;
auto zid = map.getZoneID(i);
assert(zid != zone.getId());
dNeighbourZones[zid].insert(i);
}
}
bool ConnectionsPlacer::shouldGenerateRoad(const rmg::ZoneConnection& connection) const
{
return connection.getRoadOption() == rmg::ERoadOption::ROAD_TRUE ||
(connection.getRoadOption() == rmg::ERoadOption::ROAD_RANDOM && zone.getRand().nextDouble(0, 1) >= 0.5f);
}
void ConnectionsPlacer::createBorder()
{
rmg::Area borderArea(zone.area()->getBorder());
rmg::Area borderOutsideArea(zone.area()->getBorderOutside());
auto blockBorder = borderArea.getSubarea([this, &borderOutsideArea](const int3 & t)
{
auto tile = borderOutsideArea.nearest(t);
return map.isOnMap(tile) && map.getZones()[map.getZoneID(tile)]->getType() != ETemplateZoneType::WATER;
});
//No border for wide connections
for (auto& connection : zone.getConnections()) // We actually placed that connection already
{
auto otherZone = connection.getOtherZoneId(zone.getId());
if (connection.getConnectionType() == rmg::EConnectionType::WIDE)
{
auto sharedBorder = borderArea.getSubarea([this, otherZone, &borderOutsideArea](const int3 & t)
{
auto tile = borderOutsideArea.nearest(t);
return map.isOnMap(tile) && map.getZones()[map.getZoneID(tile)]->getId() == otherZone;
});
blockBorder.subtract(sharedBorder);
}
};
auto areaPossible = zone.areaPossible();
for(const auto & tile : blockBorder.getTilesVector())
{
if(map.isPossible(tile))
{
map.setOccupied(tile, ETileType::BLOCKED);
areaPossible->erase(tile);
}
map.foreachDirectNeighbour(tile, [this, &areaPossible](int3 &nearbyPos)
{
if(map.isPossible(nearbyPos) && map.getZoneID(nearbyPos) == zone.getId())
{
map.setOccupied(nearbyPos, ETileType::BLOCKED);
areaPossible->erase(nearbyPos);
}
});
}
}
VCMI_LIB_NAMESPACE_END
|