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 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027
|
/*
* CZonePlacer.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 "CZonePlacer.h"
#include "../TerrainHandler.h"
#include "../entities/faction/CFaction.h"
#include "../entities/faction/CTownHandler.h"
#include "../mapping/CMap.h"
#include "../mapping/CMapEditManager.h"
#include "../VCMI_Lib.h"
#include "CMapGenOptions.h"
#include "RmgMap.h"
#include "Zone.h"
#include "Functions.h"
#include "PenroseTiling.h"
#include <vstd/RNG.h>
VCMI_LIB_NAMESPACE_BEGIN
//#define ZONE_PLACEMENT_LOG true
CZonePlacer::CZonePlacer(RmgMap & map)
: width(0), height(0), mapSize(0),
gravityConstant(1e-3f),
stiffnessConstant(3e-3f),
stifness(0),
stiffnessIncreaseFactor(1.03f),
bestTotalDistance(1e10),
bestTotalOverlap(1e10),
map(map)
{
}
int3 CZonePlacer::cords(const float3 & f) const
{
return int3(static_cast<si32>(std::max(0.f, (f.x * map.width()) - 1)), static_cast<si32>(std::max(0.f, (f.y * map.height() - 1))), f.z);
}
float CZonePlacer::getDistance (float distance) const
{
return (distance ? distance * distance : 1e-6f);
}
void CZonePlacer::findPathsBetweenZones()
{
auto zones = map.getZones();
std::set<std::shared_ptr<Zone>> zonesToCheck;
// Iterate through each pair of nodes in the graph
for (const auto& zone : zones)
{
int start = zone.first;
distancesBetweenZones[start][start] = 0; // Distance from a node to itself is 0
std::queue<int> q;
std::map<int, bool> visited;
visited[start] = true;
q.push(start);
// Perform Breadth-First Search from the starting node
while (!q.empty())
{
int current = q.front();
q.pop();
const auto& currentZone = zones.at(current);
const auto& connectedZoneIds = currentZone->getConnections();
for (auto & connection : connectedZoneIds)
{
switch (connection.getConnectionType())
{
//Do not consider virtual connections for graph distance
case rmg::EConnectionType::REPULSIVE:
case rmg::EConnectionType::FORCE_PORTAL:
continue;
}
auto neighbor = connection.getOtherZoneId(current);
if (current == neighbor)
{
//Do not consider self-connections
continue;
}
if (!visited[neighbor])
{
visited[neighbor] = true;
q.push(neighbor);
distancesBetweenZones[start][neighbor] = distancesBetweenZones[start][current] + 1;
}
}
}
}
}
void CZonePlacer::placeOnGrid(vstd::RNG* rand)
{
auto zones = map.getZones();
assert(zones.size());
//Make sure there are at least as many grid fields as the number of zones
size_t gridSize = std::ceil(std::sqrt(zones.size()));
typedef boost::multi_array<std::shared_ptr<Zone>, 2> GridType;
GridType grid(boost::extents[gridSize][gridSize]);
TZoneVector zonesVector(zones.begin(), zones.end());
//Place first zone
auto firstZone = zonesVector[0].second;
size_t x = 0;
size_t y = 0;
auto getRandomEdge = [rand, gridSize](size_t& x, size_t& y)
{
switch (rand->nextInt(0, 3) % 4)
{
case 0:
x = 0;
y = gridSize / 2;
break;
case 1:
x = gridSize - 1;
y = gridSize / 2;
break;
case 2:
x = gridSize / 2;
y = 0;
break;
case 3:
x = gridSize / 2;
y = gridSize - 1;
break;
}
};
switch (firstZone->getType())
{
case ETemplateZoneType::PLAYER_START:
case ETemplateZoneType::CPU_START:
if (firstZone->getConnectedZoneIds().size() > 2)
{
getRandomEdge(x, y);
}
else
{
//Random corner
if (rand->nextInt(0, 1) == 1)
{
x = 0;
}
else
{
x = gridSize - 1;
}
if (rand->nextInt(0, 1) == 1)
{
y = 0;
}
else
{
y = gridSize - 1;
}
}
break;
case ETemplateZoneType::TREASURE:
if (gridSize & 1) //odd
{
x = y = (gridSize / 2);
}
else
{
//One of 4 squares in the middle
x = (gridSize / 2) - 1 + rand->nextInt(0, 1);
y = (gridSize / 2) - 1 + rand->nextInt(0, 1);
}
break;
case ETemplateZoneType::JUNCTION:
getRandomEdge(x, y);
break;
}
grid[x][y] = firstZone;
//Ignore z placement for simplicity
for (size_t i = 1; i < zones.size(); i++)
{
auto zone = zonesVector[i].second;
auto connectedZoneIds = zone->getConnectedZoneIds();
float maxDistance = -1000.0;
int3 mostDistantPlace;
//Iterate over free positions
for (size_t freeX = 0; freeX < gridSize; ++freeX)
{
for (size_t freeY = 0; freeY < gridSize; ++freeY)
{
if (!grid[freeX][freeY])
{
//There is free space left here
int3 potentialPos(freeX, freeY, 0);
//Compute distance to every existing zone
float distance = 0;
for (size_t existingX = 0; existingX < gridSize; ++existingX)
{
for (size_t existingY = 0; existingY < gridSize; ++existingY)
{
auto existingZone = grid[existingX][existingY];
if (existingZone)
{
//There is already zone here
float localDistance = 0.0f;
auto graphDistance = distancesBetweenZones[zone->getId()][existingZone->getId()];
if (graphDistance > 1)
{
//No direct connection
localDistance = potentialPos.dist2d(int3(existingX, existingY, 0)) * graphDistance;
}
else
{
//Has direct connection - place as close as possible
localDistance = -potentialPos.dist2d(int3(existingX, existingY, 0));
}
localDistance *= scaleForceBetweenZones(zone, existingZone);
distance += localDistance;
}
}
}
if (distance > maxDistance)
{
maxDistance = distance;
mostDistantPlace = potentialPos;
}
}
}
}
//Place in a free slot
grid[mostDistantPlace.x][mostDistantPlace.y] = zone;
}
//TODO: toggle with a flag
#ifdef ZONE_PLACEMENT_LOG
logGlobal->trace("Initial zone grid:");
for (size_t x = 0; x < gridSize; ++x)
{
std::string s;
for (size_t y = 0; y < gridSize; ++y)
{
if (grid[x][y])
{
s += (boost::format("%3d ") % grid[x][y]->getId()).str();
}
else
{
s += " -- ";
}
}
logGlobal->trace(s);
}
#endif
//Set initial position for zones - random position in square centered around (x, y)
for (size_t x = 0; x < gridSize; ++x)
{
for (size_t y = 0; y < gridSize; ++y)
{
auto zone = grid[x][y];
if (zone)
{
//i.e. for grid size 5 we get range (0.25 - 4.75)
auto targetX = rand->nextDouble(x + 0.25f, x + 0.75f);
vstd::abetween(targetX, 0.5, gridSize - 0.5);
auto targetY = rand->nextDouble(y + 0.25f, y + 0.75f);
vstd::abetween(targetY, 0.5, gridSize - 0.5);
zone->setCenter(float3(targetX / gridSize, targetY / gridSize, zone->getPos().z));
}
}
}
}
float CZonePlacer::scaleForceBetweenZones(const std::shared_ptr<Zone> zoneA, const std::shared_ptr<Zone> zoneB) const
{
if (zoneA->getOwner() && zoneB->getOwner()) //Players participate in game
{
int firstPlayer = zoneA->getOwner().value();
int secondPlayer = zoneB->getOwner().value();
//Players with lower indexes (especially 1 and 2) will be placed further apart
return (1.0f + (2.0f / (firstPlayer * secondPlayer)));
}
else
{
return 1;
}
}
void CZonePlacer::placeZones(vstd::RNG * rand)
{
logGlobal->info("Starting zone placement");
width = map.getMapGenOptions().getWidth();
height = map.getMapGenOptions().getHeight();
auto zones = map.getZones();
vstd::erase_if(zones, [](const std::pair<TRmgTemplateZoneId, std::shared_ptr<Zone>> & pr)
{
return pr.second->getType() == ETemplateZoneType::WATER;
});
bool underground = map.getMapGenOptions().getHasTwoLevels();
findPathsBetweenZones();
placeOnGrid(rand);
/*
Fruchterman-Reingold algorithm
Let's assume we try to fit N circular zones with radius = size on a map
Connected zones attract, intersecting zones and map boundaries push back
*/
TZoneVector zonesVector(zones.begin(), zones.end());
assert (zonesVector.size());
RandomGeneratorUtil::randomShuffle(zonesVector, *rand);
//0. set zone sizes and surface / underground level
prepareZones(zones, zonesVector, underground, rand);
std::map<std::shared_ptr<Zone>, float3> bestSolution;
TForceVector forces;
TForceVector totalForces; // both attraction and pushback, overcomplicated?
TDistanceVector distances;
TDistanceVector overlaps;
auto evaluateSolution = [this, zones, &distances, &overlaps, &bestSolution]() -> bool
{
bool improvement = false;
float totalDistance = 0;
float totalOverlap = 0;
for (const auto& zone : distances) //find most misplaced zone
{
totalDistance += zone.second;
float overlap = overlaps[zone.first];
totalOverlap += overlap;
}
//check fitness function
if ((totalDistance + 1) * (totalOverlap + 1) < (bestTotalDistance + 1) * (bestTotalOverlap + 1))
{
//multiplication is better for auto-scaling, but stops working if one factor is 0
improvement = true;
}
//Save best solution
if (improvement)
{
bestTotalDistance = totalDistance;
bestTotalOverlap = totalOverlap;
for (const auto& zone : zones)
bestSolution[zone.second] = zone.second->getCenter();
}
#ifdef ZONE_PLACEMENT_LOG
logGlobal->trace("Total distance between zones after this iteration: %2.4f, Total overlap: %2.4f, Improved: %s", totalDistance, totalOverlap , improvement);
#endif
return improvement;
};
//Start with low stiffness. Bigger graphs need more time and more flexibility
for (stifness = stiffnessConstant / zones.size(); stifness <= stiffnessConstant;)
{
//1. attract connected zones
attractConnectedZones(zones, forces, distances);
for(const auto & zone : forces)
{
zone.first->setCenter (zone.first->getCenter() + zone.second);
totalForces[zone.first] = zone.second; //override
}
//2. separate overlapping zones
separateOverlappingZones(zones, forces, overlaps);
for(const auto & zone : forces)
{
zone.first->setCenter (zone.first->getCenter() + zone.second);
totalForces[zone.first] += zone.second; //accumulate
}
bool improved = evaluateSolution();
if (!improved)
{
//3. now perform drastic movement of zone that is completely not linked
//TODO: Don't do this is fitness was improved
moveOneZone(zones, totalForces, distances, overlaps);
improved |= evaluateSolution();
}
if (!improved)
{
//Only cool down if we didn't see any improvement
stifness *= stiffnessIncreaseFactor;
}
}
logGlobal->trace("Best fitness reached: total distance %2.4f, total overlap %2.4f", bestTotalDistance, bestTotalOverlap);
for(const auto & zone : zones) //finalize zone positions
{
zone.second->setPos (cords (bestSolution[zone.second]));
#ifdef ZONE_PLACEMENT_LOG
logGlobal->trace("Placed zone %d at relative position %s and coordinates %s", zone.first, zone.second->getCenter().toString(), zone.second->getPos().toString());
#endif
}
}
void CZonePlacer::prepareZones(TZoneMap &zones, TZoneVector &zonesVector, const bool underground, vstd::RNG * rand)
{
std::vector<float> totalSize = { 0, 0 }; //make sure that sum of zone sizes on surface and uderground match size of the map
int zonesOnLevel[2] = { 0, 0 };
//even distribution for surface / underground zones. Surface zones always have priority.
TZoneVector zonesToPlace;
std::map<TRmgTemplateZoneId, int> levels;
//first pass - determine fixed surface for zones
for(const auto & zone : zonesVector)
{
if (!underground) //this step is ignored
zonesToPlace.push_back(zone);
else //place players depending on their factions
{
if(std::optional<int> owner = zone.second->getOwner())
{
auto player = PlayerColor(*owner - 1);
auto playerSettings = map.getMapGenOptions().getPlayersSettings();
FactionID faction = FactionID::RANDOM;
if (playerSettings.size() > player)
{
faction = std::next(playerSettings.begin(), player)->second.getStartingTown();
}
else
{
logGlobal->trace("Player %d (starting zone %d) does not participate in game", player.getNum(), zone.first);
}
if (faction == FactionID::RANDOM) //TODO: check this after a town has already been randomized
zonesToPlace.push_back(zone);
else
{
auto & tt = (*VLC->townh)[faction]->nativeTerrain;
if(tt == ETerrainId::NONE)
{
//any / random
zonesToPlace.push_back(zone);
}
else
{
const auto & terrainType = VLC->terrainTypeHandler->getById(tt);
if(terrainType->isUnderground() && !terrainType->isSurface())
{
//underground only
zonesOnLevel[1]++;
levels[zone.first] = 1;
}
else
{
//surface
zonesOnLevel[0]++;
levels[zone.first] = 0;
}
}
}
}
else //no starting zone or no underground altogether
{
zonesToPlace.push_back(zone);
}
}
}
for(const auto & zone : zonesToPlace)
{
if (underground) //only then consider underground zones
{
int level = 0;
if (zonesOnLevel[1] < zonesOnLevel[0]) //only if there are less underground zones
level = 1;
else
level = 0;
levels[zone.first] = level;
zonesOnLevel[level]++;
}
else
levels[zone.first] = 0;
}
for(const auto & zone : zonesVector)
{
int level = levels[zone.first];
totalSize[level] += (zone.second->getSize() * zone.second->getSize());
float3 center = zone.second->getCenter();
center.z = level;
zone.second->setCenter(center);
}
/*
prescale zones
formula: sum((prescaler*n)^2)*pi = WH
prescaler = sqrt((WH)/(sum(n^2)*pi))
*/
std::vector<float> prescaler = { 0, 0 };
for (int i = 0; i < 2; i++)
prescaler[i] = std::sqrt((width * height) / (totalSize[i] * PI_CONSTANT));
mapSize = static_cast<float>(sqrt(width * height));
for(const auto & zone : zones)
{
zone.second->setSize(static_cast<int>(zone.second->getSize() * prescaler[zone.second->getCenter().z]));
}
}
void CZonePlacer::attractConnectedZones(TZoneMap & zones, TForceVector & forces, TDistanceVector & distances) const
{
for(const auto & zone : zones)
{
float3 forceVector(0, 0, 0);
float3 pos = zone.second->getCenter();
float totalDistance = 0;
for (const auto & connection : zone.second->getConnections())
{
switch (connection.getConnectionType())
{
//Do not consider virtual connections for graph distance
case rmg::EConnectionType::REPULSIVE:
case rmg::EConnectionType::FORCE_PORTAL:
continue;
}
if (connection.getZoneA() == connection.getZoneB())
{
//Do not consider self-connections
continue;
}
auto otherZone = zones[connection.getOtherZoneId(zone.second->getId())];
float3 otherZoneCenter = otherZone->getCenter();
auto distance = static_cast<float>(pos.dist2d(otherZoneCenter));
forceVector += (otherZoneCenter - pos) * distance * gravityConstant * scaleForceBetweenZones(zone.second, otherZone); //positive value
//Attract zone centers always
float minDistance = 0;
if (pos.z != otherZoneCenter.z)
minDistance = 0; //zones on different levels can overlap completely
else
minDistance = (zone.second->getSize() + otherZone->getSize()) / mapSize; //scale down to (0,1) coordinates
if (distance > minDistance)
totalDistance += (distance - minDistance);
}
distances[zone.second] = totalDistance;
forceVector.z = 0; //operator - doesn't preserve z coordinate :/
forces[zone.second] = forceVector;
}
}
void CZonePlacer::separateOverlappingZones(TZoneMap &zones, TForceVector &forces, TDistanceVector &overlaps)
{
for(const auto & zone : zones)
{
float3 forceVector(0, 0, 0);
float3 pos = zone.second->getCenter();
float overlap = 0;
//separate overlapping zones
for(const auto & otherZone : zones)
{
float3 otherZoneCenter = otherZone.second->getCenter();
//zones on different levels don't push away
if (zone == otherZone || pos.z != otherZoneCenter.z)
continue;
auto distance = static_cast<float>(pos.dist2d(otherZoneCenter));
float minDistance = (zone.second->getSize() + otherZone.second->getSize()) / mapSize;
if (distance < minDistance)
{
float3 localForce = (((otherZoneCenter - pos)*(minDistance / (distance ? distance : 1e-3f))) / getDistance(distance)) * stifness;
//negative value
localForce *= scaleForceBetweenZones(zone.second, otherZone.second);
forceVector -= localForce * (distancesBetweenZones[zone.second->getId()][otherZone.second->getId()] / 2.0f);
overlap += (minDistance - distance); //overlapping of small zones hurts us more
}
}
//move zones away from boundaries
//do not scale boundary distance - zones tend to get squashed
float size = zone.second->getSize() / mapSize;
auto pushAwayFromBoundary = [&forceVector, pos, size, &overlap, this](float x, float y)
{
float3 boundary = float3(x, y, pos.z);
auto distance = static_cast<float>(pos.dist2d(boundary));
overlap += std::max<float>(0, distance - size); //check if we're closer to map boundary than value of zone size
forceVector -= (boundary - pos) * (size - distance) / this->getDistance(distance) * this->stifness; //negative value
};
if (pos.x < size)
{
pushAwayFromBoundary(0, pos.y);
}
if (pos.x > 1 - size)
{
pushAwayFromBoundary(1, pos.y);
}
if (pos.y < size)
{
pushAwayFromBoundary(pos.x, 0);
}
if (pos.y > 1 - size)
{
pushAwayFromBoundary(pos.x, 1);
}
//Always move repulsive zones away, no matter their distance
//TODO: Consider z plane?
for (auto& connection : zone.second->getConnections())
{
if (connection.getConnectionType() == rmg::EConnectionType::REPULSIVE)
{
auto & otherZone = zones[connection.getOtherZoneId(zone.second->getId())];
float3 otherZoneCenter = otherZone->getCenter();
//TODO: Roll into lambda?
auto distance = static_cast<float>(pos.dist2d(otherZoneCenter));
float minDistance = (zone.second->getSize() + otherZone->getSize()) / mapSize;
float3 localForce = (((otherZoneCenter - pos)*(minDistance / (distance ? distance : 1e-3f))) / getDistance(distance)) * stifness;
localForce *= (distancesBetweenZones[zone.second->getId()][otherZone->getId()]);
forceVector -= localForce * scaleForceBetweenZones(zone.second, otherZone);
}
}
overlaps[zone.second] = overlap;
forceVector.z = 0; //operator - doesn't preserve z coordinate :/
forces[zone.second] = forceVector;
}
}
void CZonePlacer::moveOneZone(TZoneMap& zones, TForceVector& totalForces, TDistanceVector& distances, TDistanceVector& overlaps)
{
//The more zones, the greater total distance expected
//Also, higher stiffness make expected movement lower
const int maxDistanceMovementRatio = zones.size() * zones.size() * (stiffnessConstant / stifness);
typedef std::pair<float, std::shared_ptr<Zone>> Misplacement;
std::vector<Misplacement> misplacedZones;
float totalDistance = 0;
float totalOverlap = 0;
for (const auto& zone : distances) //find most misplaced zone
{
if (vstd::contains(lastSwappedZones, zone.first->getId()))
{
continue;
}
totalDistance += zone.second;
float overlap = overlaps[zone.first];
totalOverlap += overlap;
//if distance to actual movement is long, the zone is misplaced
float ratio = (zone.second + overlap) / static_cast<float>(totalForces[zone.first].mag());
if (ratio > maxDistanceMovementRatio)
{
misplacedZones.emplace_back(std::make_pair(ratio, zone.first));
}
}
if (misplacedZones.empty())
return;
boost::sort(misplacedZones, [](const Misplacement& lhs, Misplacement& rhs)
{
return lhs.first > rhs.first; //Largest displacement first
});
#ifdef ZONE_PLACEMENT_LOG
logGlobal->trace("Worst misplacement/movement ratio: %3.2f", misplacedZones.front().first);
#endif
if (misplacedZones.size() >= 2)
{
//Swap 2 misplaced zones
auto firstZone = misplacedZones.front().second;
std::shared_ptr<Zone> secondZone;
std::set<TRmgTemplateZoneId> connectedZones;
for (const auto& connection : firstZone->getConnections())
{
switch (connection.getConnectionType())
{
//Do not consider virtual connections for graph distance
case rmg::EConnectionType::REPULSIVE:
case rmg::EConnectionType::FORCE_PORTAL:
continue;
}
if (connection.getZoneA() == connection.getZoneB())
{
//Do not consider self-connections
continue;
}
connectedZones.insert(connection.getOtherZoneId(firstZone->getId()));
}
auto level = firstZone->getCenter().z;
for (size_t i = 1; i < misplacedZones.size(); i++)
{
//Only swap zones on the same level
//Don't swap zones that should be connected (Jebus)
if (misplacedZones[i].second->getCenter().z == level &&
!vstd::contains(connectedZones, misplacedZones[i].second->getId()))
{
secondZone = misplacedZones[i].second;
break;
}
}
if (secondZone)
{
#ifdef ZONE_PLACEMENT_LOG
logGlobal->trace("Swapping two misplaced zones %d and %d", firstZone->getId(), secondZone->getId());
#endif
auto firstCenter = firstZone->getCenter();
auto secondCenter = secondZone->getCenter();
firstZone->setCenter(secondCenter);
secondZone->setCenter(firstCenter);
lastSwappedZones.insert(firstZone->getId());
lastSwappedZones.insert(secondZone->getId());
return;
}
}
lastSwappedZones.clear(); //If we didn't swap zones in this iteration, we can do it in the next
//find most distant zone that should be attracted and move inside it
std::shared_ptr<Zone> targetZone;
auto misplacedZone = misplacedZones.front().second;
float3 ourCenter = misplacedZone->getCenter();
if ((totalDistance / (bestTotalDistance + 1)) > (totalOverlap / (bestTotalOverlap + 1)))
{
//Move one zone towards most distant zone to reduce distance
float maxDistance = 0;
for (auto con : misplacedZone->getConnections())
{
if (con.getConnectionType() == rmg::EConnectionType::REPULSIVE)
{
continue;
}
auto otherZone = zones[con.getOtherZoneId(misplacedZone->getId())];
float distance = static_cast<float>(otherZone->getCenter().dist2dSQ(ourCenter));
if (distance > maxDistance)
{
maxDistance = distance;
targetZone = otherZone;
}
}
if (targetZone)
{
float3 vec = targetZone->getCenter() - ourCenter;
float newDistanceBetweenZones = (std::max(misplacedZone->getSize(), targetZone->getSize())) / mapSize;
#ifdef ZONE_PLACEMENT_LOG
logGlobal->trace("Trying to move zone %d %s towards %d %s. Direction is %s", misplacedZone->getId(), ourCenter.toString(), targetZone->getId(), targetZone->getCenter().toString(), vec.toString());
#endif
misplacedZone->setCenter(targetZone->getCenter() - vec.unitVector() * newDistanceBetweenZones); //zones should now overlap by half size
}
}
else
{
//Move misplaced zone away from overlapping zone
float maxOverlap = 0;
for(const auto & otherZone : zones)
{
float3 otherZoneCenter = otherZone.second->getCenter();
if (otherZone.second == misplacedZone || otherZoneCenter.z != ourCenter.z)
continue;
auto distance = static_cast<float>(otherZoneCenter.dist2dSQ(ourCenter));
if (distance > maxOverlap)
{
maxOverlap = distance;
targetZone = otherZone.second;
}
}
if (targetZone)
{
float3 vec = ourCenter - targetZone->getCenter();
float newDistanceBetweenZones = (misplacedZone->getSize() + targetZone->getSize()) / mapSize;
#ifdef ZONE_PLACEMENT_LOG
logGlobal->trace("Trying to move zone %d %s away from %d %s. Direction is %s", misplacedZone->getId(), ourCenter.toString(), targetZone->getId(), targetZone->getCenter().toString(), vec.toString());
#endif
misplacedZone->setCenter(targetZone->getCenter() + vec.unitVector() * newDistanceBetweenZones); //zones should now be just separated
}
}
//Don't swap that zone in next iteration
lastSwappedZones.insert(misplacedZone->getId());
}
float CZonePlacer::metric (const int3 &A, const int3 &B) const
{
return A.dist2dSQ(B);
}
void CZonePlacer::assignZones(vstd::RNG * rand)
{
logGlobal->info("Starting zone colouring");
auto width = map.getMapGenOptions().getWidth();
auto height = map.getMapGenOptions().getHeight();
auto zones = map.getZones();
vstd::erase_if(zones, [](const std::pair<TRmgTemplateZoneId, std::shared_ptr<Zone>> & pr)
{
return pr.second->getType() == ETemplateZoneType::WATER;
});
using Dpair = std::pair<std::shared_ptr<Zone>, float>;
std::vector <Dpair> distances;
distances.reserve(zones.size());
//now place zones correctly and assign tiles to each zone
auto compareByDistance = [](const Dpair & lhs, const Dpair & rhs) -> bool
{
//bigger zones have smaller distance
return lhs.second / lhs.first->getSize() < rhs.second / rhs.first->getSize();
};
auto simpleCompareByDistance = [](const Dpair & lhs, const Dpair & rhs) -> bool
{
//bigger zones have smaller distance
return lhs.second < rhs.second;
};
auto moveZoneToCenterOfMass = [width, height](const std::shared_ptr<Zone> & zone) -> void
{
int3 total(0, 0, 0);
auto tiles = zone->area()->getTiles();
for(const auto & tile : tiles)
{
total += tile;
}
int size = static_cast<int>(tiles.size());
assert(size);
auto newPos = int3(total.x / size, total.y / size, total.z / size);
zone->setPos(newPos);
zone->setCenter(float3(float(newPos.x) / width, float(newPos.y) / height, newPos.z));
};
int levels = map.levels();
// Find current center of mass for each zone. Move zone to that center to balance zones sizes
std::vector<RmgMap::Zones> zonesOnLevel;
for(int level = 0; level < levels; level++)
{
zonesOnLevel.push_back(map.getZonesOnLevel(level));
}
int3 pos;
for(pos.z = 0; pos.z < levels; pos.z++)
{
for(pos.x = 0; pos.x < width; pos.x++)
{
for(pos.y = 0; pos.y < height; pos.y++)
{
distances.clear();
for(const auto & zone : zonesOnLevel[pos.z])
{
distances.emplace_back(zone.second, static_cast<float>(pos.dist2dSQ(zone.second->getPos())));
}
boost::min_element(distances, compareByDistance)->first->area()->add(pos); //closest tile belongs to zone
}
}
}
for(const auto & zone : zones)
{
if(zone.second->area()->empty())
throw rmgException("Empty zone is generated, probably RMG template is inappropriate for map size");
moveZoneToCenterOfMass(zone.second);
}
for(const auto & zone : zones)
zone.second->clearTiles(); //now populate them again
PenroseTiling penrose;
for (int level = 0; level < levels; level++)
{
//Create different tiling for each level
auto vertices = penrose.generatePenroseTiling(zonesOnLevel[level].size(), rand);
// Assign zones to closest Penrose vertex
std::map<std::shared_ptr<Zone>, std::set<int3>> vertexMapping;
for (const auto & vertex : vertices)
{
distances.clear();
for(const auto & zone : zonesOnLevel[level])
{
distances.emplace_back(zone.second, zone.second->getCenter().dist2dSQ(float3(vertex.x(), vertex.y(), level)));
}
auto closestZone = boost::min_element(distances, compareByDistance)->first;
vertexMapping[closestZone].insert(int3(vertex.x() * width, vertex.y() * height, level)); //Closest vertex belongs to zone
}
//Assign actual tiles to each zone
pos.z = level;
for (pos.x = 0; pos.x < width; pos.x++)
{
for (pos.y = 0; pos.y < height; pos.y++)
{
distances.clear();
for(const auto & zoneVertex : vertexMapping)
{
auto zone = zoneVertex.first;
for (const auto & vertex : zoneVertex.second)
{
distances.emplace_back(zone, metric(pos, vertex));
}
}
//Tile closest to vertex belongs to zone
auto closestZone = boost::min_element(distances, simpleCompareByDistance)->first;
closestZone->area()->add(pos);
map.setZoneID(pos, closestZone->getId());
}
}
for(const auto & zone : zonesOnLevel[level])
{
if(zone.second->area()->empty())
{
// FIXME: Some vertices are duplicated, but it's not a source of problem
logGlobal->error("Zone %d at %s is empty, dumping Penrose tiling", zone.second->getId(), zone.second->getCenter().toString());
for (const auto & vertex : vertices)
{
logGlobal->warn("Penrose Vertex: %s", vertex.toString());
}
throw rmgException("Empty zone after Penrose tiling");
}
}
}
//set position (town position) to center of mass of irregular zone
for(const auto & zone : zones)
{
moveZoneToCenterOfMass(zone.second);
//TODO: similar for islands
#define CREATE_FULL_UNDERGROUND true //consider linking this with water amount
if (zone.second->isUnderground())
{
if (!CREATE_FULL_UNDERGROUND)
{
auto discardTiles = collectDistantTiles(*zone.second, zone.second->getSize() + 1.f);
for(const auto & t : discardTiles)
zone.second->area()->erase(t);
}
//make sure that terrain inside zone is not a rock
auto v = zone.second->area()->getTilesVector();
map.getMapProxy()->drawTerrain(*rand, v, ETerrainId::SUBTERRANEAN);
}
}
logGlobal->info("Finished zone colouring");
}
const TDistanceMap& CZonePlacer::getDistanceMap()
{
return distancesBetweenZones;
}
VCMI_LIB_NAMESPACE_END
|