mirror of
https://github.com/vcmi/vcmi.git
synced 2025-07-17 01:32:21 +02:00
Fix crash with empty zone
This commit is contained in:
@ -906,8 +906,13 @@ void CZonePlacer::assignZones(CRandomGenerator * rand)
|
|||||||
for(const auto & zone : zones)
|
for(const auto & zone : zones)
|
||||||
zone.second->clearTiles(); //now populate them again
|
zone.second->clearTiles(); //now populate them again
|
||||||
|
|
||||||
// Assign zones to closest Penrose vertex
|
|
||||||
PenroseTiling penrose;
|
PenroseTiling penrose;
|
||||||
|
for (int level = 0; level < levels; level++)
|
||||||
|
{
|
||||||
|
//Create different tiling for each level
|
||||||
|
// Assign zones to closest Penrose vertex
|
||||||
|
// TODO: Count zones on a level exactly?
|
||||||
|
|
||||||
auto vertices = penrose.generatePenroseTiling(zones.size() / map.levels(), rand);
|
auto vertices = penrose.generatePenroseTiling(zones.size() / map.levels(), rand);
|
||||||
|
|
||||||
std::map<std::shared_ptr<Zone>, std::set<int3>> vertexMapping;
|
std::map<std::shared_ptr<Zone>, std::set<int3>> vertexMapping;
|
||||||
@ -917,16 +922,20 @@ void CZonePlacer::assignZones(CRandomGenerator * rand)
|
|||||||
distances.clear();
|
distances.clear();
|
||||||
for(const auto & zone : zones)
|
for(const auto & zone : zones)
|
||||||
{
|
{
|
||||||
distances.emplace_back(zone.second, zone.second->getCenter().dist2dSQ(float3(vertex.x(), vertex.y(), 0)));
|
if (zone.second->isUnderground() == level)
|
||||||
|
{
|
||||||
|
// FIXME: Only take into account zones on the same level as vertex
|
||||||
|
// TODO: Create separate mapping for zones on different levels
|
||||||
|
distances.emplace_back(zone.second, zone.second->getCenter().dist2dSQ(float3(vertex.x(), vertex.y(), level)));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
auto closestZone = boost::min_element(distances, compareByDistance)->first;
|
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
|
vertexMapping[closestZone].insert(int3(vertex.x() * width, vertex.y() * height, level)); //Closest vertex belongs to zone
|
||||||
}
|
}
|
||||||
|
|
||||||
//Assign actual tiles to each zone
|
//Assign actual tiles to each zone
|
||||||
for (pos.z = 0; pos.z < levels; pos.z++)
|
pos.z = level;
|
||||||
{
|
|
||||||
for (pos.x = 0; pos.x < width; pos.x++)
|
for (pos.x = 0; pos.x < width; pos.x++)
|
||||||
{
|
{
|
||||||
for (pos.y = 0; pos.y < height; pos.y++)
|
for (pos.y = 0; pos.y < height; pos.y++)
|
||||||
@ -937,27 +946,38 @@ void CZonePlacer::assignZones(CRandomGenerator * rand)
|
|||||||
auto zone = zoneVertex.first;
|
auto zone = zoneVertex.first;
|
||||||
for (const auto & vertex : zoneVertex.second)
|
for (const auto & vertex : zoneVertex.second)
|
||||||
{
|
{
|
||||||
if (zone->getCenter().z == pos.z)
|
if (zone->isUnderground() == level)
|
||||||
distances.emplace_back(zone, metric(pos, vertex));
|
distances.emplace_back(zone, metric(pos, vertex));
|
||||||
else
|
else
|
||||||
distances.emplace_back(zone, std::numeric_limits<float>::max());
|
distances.emplace_back(zone, std::numeric_limits<float>::max());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//Tile closes to vertex belongs to zone
|
//Tile closest to vertex belongs to zone
|
||||||
auto closestZone = boost::min_element(distances, simpleCompareByDistance)->first;
|
auto closestZone = boost::min_element(distances, simpleCompareByDistance)->first;
|
||||||
closestZone->area().add(pos);
|
closestZone->area().add(pos);
|
||||||
map.setZoneID(pos, closestZone->getId());
|
map.setZoneID(pos, closestZone->getId());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for(const auto & zone : zones)
|
||||||
|
{
|
||||||
|
if(zone.second->isUnderground() == level && 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
|
//set position (town position) to center of mass of irregular zone
|
||||||
for(const auto & zone : zones)
|
for(const auto & zone : zones)
|
||||||
{
|
{
|
||||||
if(zone.second->area().empty())
|
|
||||||
throw rmgException("Empty zone after Penrose tiling");
|
|
||||||
|
|
||||||
moveZoneToCenterOfMass(zone.second);
|
moveZoneToCenterOfMass(zone.second);
|
||||||
|
|
||||||
//TODO: similiar for islands
|
//TODO: similiar for islands
|
||||||
|
@ -38,6 +38,16 @@ bool Point2D::operator < (const Point2D& other) const
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string Point2D::toString() const
|
||||||
|
{
|
||||||
|
//Performance is important here
|
||||||
|
std::string result = "(" +
|
||||||
|
std::to_string(this->x()) + " " +
|
||||||
|
std::to_string(this->y()) + ")";
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
Triangle::Triangle(bool t_123, const TIndices & inds):
|
Triangle::Triangle(bool t_123, const TIndices & inds):
|
||||||
tiling(t_123),
|
tiling(t_123),
|
||||||
indices(inds)
|
indices(inds)
|
||||||
|
@ -32,6 +32,8 @@ public:
|
|||||||
Point2D rotated(float radians) const;
|
Point2D rotated(float radians) const;
|
||||||
|
|
||||||
bool operator < (const Point2D& other) const;
|
bool operator < (const Point2D& other) const;
|
||||||
|
|
||||||
|
std::string toString() const;
|
||||||
};
|
};
|
||||||
|
|
||||||
Point2D rotatePoint(const Point2D& point, double radians, const Point2D& origin);
|
Point2D rotatePoint(const Point2D& point, double radians, const Point2D& origin);
|
||||||
|
Reference in New Issue
Block a user