浏览代码

- Attempt to move zones away from map boundaries
- Tweaking algorithm parameters
- Refactorings

DjWarmonger 11 年之前
父节点
当前提交
b9de3875d9
共有 4 个文件被更改,包括 62 次插入9 次删除
  1. 11 0
      lib/rmg/CMapGenerator.cpp
  2. 4 0
      lib/rmg/CMapGenerator.h
  3. 5 5
      lib/rmg/CRmgTemplateZone.cpp
  4. 42 4
      lib/rmg/CZonePlacer.cpp

+ 11 - 0
lib/rmg/CMapGenerator.cpp

@@ -14,6 +14,17 @@
 #include "CRmgTemplateZone.h"
 #include "CZonePlacer.h"
 
+void CMapGenerator::foreach_neighbour(const int3 &pos, std::function<void(const int3& pos)> foo)
+{
+	//for(const int3 &dir : dirs)
+	//{
+	//	const int3 n = pos + dir;
+	//	if(map->isInTheMap(n))
+	//		foo(pos+dir);
+	//}
+}
+
+
 CMapGenerator::CMapGenerator(shared_ptr<CMapGenOptions> mapGenOptions, int randomSeed /*= std::time(nullptr)*/) :
 	mapGenOptions(mapGenOptions), randomSeed(randomSeed)
 {

+ 4 - 0
lib/rmg/CMapGenerator.h

@@ -29,6 +29,9 @@ typedef std::vector<JsonNode> JsonVector;
 
 class CMapGenerator;
 
+//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) };
+
 class rmgException : std::exception
 {
 	std::string msg;
@@ -63,6 +66,7 @@ public:
 	CMapEditManager * editManager;
 
 	std::map<TRmgTemplateZoneId, CRmgTemplateZone*> getZones() const;
+	void foreach_neighbour(const int3 &pos, std::function<void(const int3& pos)> foo);
 
 private:
 	std::map<TRmgTemplateZoneId, CRmgTemplateZone*> zones;

+ 5 - 5
lib/rmg/CRmgTemplateZone.cpp

@@ -343,9 +343,9 @@ bool CRmgTemplateZone::fill(CMapGenerator* gen)
 			town->builtBuildings.insert(BuildingID::DEFAULT);
 			
 			placeObject(gen, town, getPos());
-			logGlobal->infoStream() << "Placed object";
+			logGlobal->traceStream() << "Placed object";
 
-			logGlobal->infoStream() << "Fill player info " << player_id;
+			logGlobal->traceStream() << "Fill player info " << player_id;
 			auto & playerInfo = gen->map->players[player_id];
 			// Update player info
 			playerInfo.allowedFactions.clear();
@@ -394,14 +394,14 @@ bool CRmgTemplateZone::fill(CMapGenerator* gen)
 	for(const auto &obj : required_objects)
 	{
 		int3 pos;
-		logGlobal->infoStream() << "Looking for place";
+		logGlobal->traceStream() << "Looking for place";
 		if ( ! findPlaceForObject(gen, obj, 3, pos))		
 		{
 			logGlobal->errorStream() << boost::format("Failed to fill zone %d due to lack of space") %id;
 			//TODO CLEANUP!
 			return false;
 		}
-		logGlobal->infoStream() << "Place found";
+		logGlobal->traceStream() << "Place found";
 
 		placeObject(gen, obj, pos);
 	}
@@ -546,7 +546,7 @@ bool CRmgTemplateZone::guardObject(CMapGenerator* gen, CGObjectInstance* object,
 			{
 				if (it->first != visitable)
 				{
-					logGlobal->infoStream() << boost::format("Block at %d %d") % it->first.x % it->first.y;
+					logGlobal->traceStream() << boost::format("Block at %d %d") % it->first.x % it->first.y;
 					if (!it->second.isOccupied() &&  !it->second.isObstacle())
 					{
 						tiles.push_back(it->first);

+ 42 - 4
lib/rmg/CZonePlacer.cpp

@@ -42,9 +42,9 @@ void CZonePlacer::placeZones(shared_ptr<CMapGenOptions> mapGenOptions, CRandomGe
 {
 	//some relaxation-simmulated annealing algorithm
 
-	const int iterations = 5;
+	const int iterations = 100;
 	float temperature = 1;
-	const float temperatureModifier = 0.9;
+	const float temperatureModifier = 0.99;
 
 	logGlobal->infoStream() << "Starting zone placement";
 
@@ -59,7 +59,7 @@ void CZonePlacer::placeZones(shared_ptr<CMapGenOptions> mapGenOptions, CRandomGe
 	for (auto zone : zones)
 	{
 		totalSize += zone.second->getSize();
-		zone.second->setCenter (float3(rand->nextDouble(0,1), rand->nextDouble(0,1), 0));
+		zone.second->setCenter (float3(rand->nextDouble(0.2,0.8), rand->nextDouble(0.2,0.8), 0)); //start away from borders
 	}
 	//prescale zones
 	float prescaler = sqrt (width * height / totalSize) / 3.14f; //let's assume we try to fit N circular zones with radius = size on a map
@@ -85,6 +85,7 @@ void CZonePlacer::placeZones(shared_ptr<CMapGenOptions> mapGenOptions, CRandomGe
 					float scaler = (distance - minDistance)/distance * temperature; //positive
 					auto positionVector = (otherZone->getCenter() - zone.second->getCenter()); //positive value
 					zone.second->setCenter (zone.second->getCenter() + positionVector * scaler); //positive movement
+					break; //only one move for each zone
 				}
 			}
 		}
@@ -104,9 +105,43 @@ void CZonePlacer::placeZones(shared_ptr<CMapGenOptions> mapGenOptions, CRandomGe
 					float scaler = (distance ? (distance - minDistance)/distance : 1) * temperature; //negative
 					auto positionVector = (otherZone.second->getCenter() - zone.second->getCenter()); //positive value
 					zone.second->setCenter (zone.second->getCenter() + positionVector * scaler); //negative movement
+					break; //only one move for each zone
 				}
 			}
 		}
+		for (auto zone : zones)
+		{
+			//move zones away from boundaries
+			auto pos = zone.second->getCenter();
+			float3 boundary(0,0,pos.z);
+			float size = zone.second->getSize() / mapSize;
+			if (pos.x < size)
+			{
+				boundary = float3 (0, pos.y, pos.z);
+			}
+			else if (pos.x > 1-size)
+			{
+				boundary = float3 (1-size, pos.y, pos.z);
+			}
+			else if (pos.y < size)
+			{
+				boundary = float3 (pos.x, 0, pos.z);
+			}
+			else if (pos.y > 1-size)
+			{
+				boundary = float3 (pos.x, 1-size, pos.z);
+			}
+			else
+				continue;
+
+			float distance = pos.dist2d(boundary);
+			float minDistance = size;
+
+			//move our zone away from boundary
+			float scaler = (distance ? (distance - minDistance)/distance : 1) * temperature; //negative
+			auto positionVector = (boundary - pos); //positive value
+			zone.second->setCenter (pos + positionVector * scaler); //negative movement
+		}
 		temperature *= temperatureModifier;
 	}
 	for (auto zone : zones) //finalize zone positions
@@ -133,11 +168,13 @@ Matlab code
 	float dy = abs(A.y - B.y) * scaleY;
 
 	//Horner scheme
-	return dx * (1 + dx * (0.1 + dx * 0.01)) + dy * (0.3 + dy * (-0.3 + dy * 0.03));
+	return dx * (1 + dx * (0.1 + dx * 0.01)) + dy * (0.2 + dy * (-0.2 + dy * 0.02));
 }
 
 void CZonePlacer::assignZones(shared_ptr<CMapGenOptions> mapGenOptions)
 {
+	logGlobal->infoStream()  << "Starting zone colouring";
+
 	auto width = mapGenOptions->getWidth();
 	auto height = mapGenOptions->getHeight();
 
@@ -174,4 +211,5 @@ void CZonePlacer::assignZones(shared_ptr<CMapGenOptions> mapGenOptions)
 			}
 		}
 	}
+	logGlobal->infoStream() << "Finished zone colouring";
 }