1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-07-17 01:32:21 +02:00

First working version

This commit is contained in:
Tomasz Zieliński
2024-02-02 14:27:32 +01:00
parent 9f7c986621
commit 178f960533
3 changed files with 142 additions and 160 deletions

View File

@ -20,6 +20,7 @@
#include "RmgMap.h"
#include "Zone.h"
#include "Functions.h"
#include "PenroseTiling.h"
VCMI_LIB_NAMESPACE_BEGIN
@ -524,7 +525,7 @@ void CZonePlacer::prepareZones(TZoneMap &zones, TZoneVector &zonesVector, const
std::vector<float> prescaler = { 0, 0 };
for (int i = 0; i < 2; i++)
prescaler[i] = std::sqrt((width * height) / (totalSize[i] * 3.14f));
prescaler[i] = std::sqrt((width * height) / (totalSize[i] * PI_CONSTANT));
mapSize = static_cast<float>(sqrt(width * height));
for(const auto & zone : zones)
{
@ -802,6 +803,7 @@ void CZonePlacer::moveOneZone(TZoneMap& zones, TForceVector& totalForces, TDista
float CZonePlacer::metric (const int3 &A, const int3 &B) const
{
/*
float dx = abs(A.x - B.x) * scaleX;
float dy = abs(A.y - B.y) * scaleY;
@ -811,9 +813,14 @@ float CZonePlacer::metric (const int3 &A, const int3 &B) const
3. Nonlinear mess for fuzzy edges
*/
/*
return dx * dx + dy * dy +
5 * std::sin(dx * dy / 10) +
25 * std::sin (std::sqrt(A.x * B.x) * (A.y - B.y) / 100 * (scaleX * scaleY));
*/
return A.dist2dSQ(B);
}
void CZonePlacer::assignZones(CRandomGenerator * rand)
@ -845,7 +852,13 @@ void CZonePlacer::assignZones(CRandomGenerator * rand)
return lhs.second / lhs.first->getSize() < rhs.second / rhs.first->getSize();
};
auto moveZoneToCenterOfMass = [](const std::shared_ptr<Zone> & zone) -> void
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();
@ -855,17 +868,17 @@ void CZonePlacer::assignZones(CRandomGenerator * rand)
}
int size = static_cast<int>(tiles.size());
assert(size);
zone->setPos(int3(total.x / size, total.y / size, total.z / 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();
/*
1. Create Voronoi diagram
2. find current center of mass for each zone. Move zone to that center to balance zones sizes
*/
// Find current center of mass for each zone. Move zone to that center to balance zones sizes
int3 pos;
for(pos.z = 0; pos.z < levels; pos.z++)
{
for(pos.x = 0; pos.x < width; pos.x++)
@ -890,14 +903,41 @@ void CZonePlacer::assignZones(CRandomGenerator * rand)
if(zone.second->area().empty())
throw rmgException("Empty zone is generated, probably RMG template is inappropriate for map size");
// FIXME: Is 2. correct and doesn't break balance?
moveZoneToCenterOfMass(zone.second);
}
//assign actual tiles to each zone using nonlinear norm for fine edges
for(const auto & zone : zones)
zone.second->clearTiles(); //now populate them again
// Assign zones to closest Penrose vertex
PenroseTiling penrose;
auto vertices = penrose.generatePenroseTiling(zones.size(), rand);
std::map<std::shared_ptr<Zone>, std::set<int3>> vertexMapping;
for (const auto & vertex : vertices)
{
distances.clear();
for(const auto & zone : zones)
{
distances.emplace_back(zone.second, zone.second->getCenter().dist2dSQ(float3(vertex.x(), vertex.y(), 0)));
}
auto closestZone = boost::min_element(distances, compareByDistance)->first;
vertexMapping[closestZone].insert(int3(vertex.x() * width, vertex.y() * height, closestZone->getPos().z)); //Closest vertex belongs to zone
}
for (const auto & p : vertexMapping)
{
for (const auto vertex : p.second)
{
logGlobal->info("Zone %2d is assigned to vertex %s", p.first->getId(), vertex.toString());
}
logGlobal->info("Zone %2d has total of %d vertices", p.first->getId(), p.second.size());
}
//Assign actual tiles to each zone using nonlinear norm for fine edges
for (pos.z = 0; pos.z < levels; pos.z++)
{
for (pos.x = 0; pos.x < width; pos.x++)
@ -905,22 +945,32 @@ void CZonePlacer::assignZones(CRandomGenerator * rand)
for (pos.y = 0; pos.y < height; pos.y++)
{
distances.clear();
for(const auto & zone : zones)
for(const auto & zoneVertex : vertexMapping)
{
if (zone.second->getPos().z == pos.z)
distances.emplace_back(zone.second, metric(pos, zone.second->getPos()));
else
distances.emplace_back(zone.second, std::numeric_limits<float>::max());
// FIXME: Find closest vertex, not closest zone
auto zone = zoneVertex.first;
for (const auto & vertex : zoneVertex.second)
{
if (zone->getCenter().z == pos.z)
distances.emplace_back(zone, metric(pos, vertex));
else
distances.emplace_back(zone, std::numeric_limits<float>::max());
}
}
auto zone = boost::min_element(distances, compareByDistance)->first; //closest tile belongs to zone
zone->area().add(pos);
map.setZoneID(pos, zone->getId());
auto closestZone = boost::min_element(distances, simpleCompareByDistance)->first; //closest tile belongs to zone
closestZone->area().add(pos);
map.setZoneID(pos, closestZone->getId());
}
}
}
//set position (town position) to center of mass of irregular zone
for(const auto & zone : zones)
{
if(zone.second->area().empty())
throw rmgException("Empty zone after Penrose tiling");
moveZoneToCenterOfMass(zone.second);
//TODO: similiar for islands