Bläddra i källkod

Merge pull request #3636 from vcmi/rmg_tweaks

Rmg tweaks
DjWarmonger 1 år sedan
förälder
incheckning
306896601a

+ 18 - 2
lib/rmg/CZonePlacer.cpp

@@ -24,6 +24,8 @@
 
 VCMI_LIB_NAMESPACE_BEGIN
 
+//#define ZONE_PLACEMENT_LOG true
+
 class CRandomGenerator;
 
 CZonePlacer::CZonePlacer(RmgMap & map)
@@ -248,7 +250,8 @@ void CZonePlacer::placeOnGrid(CRandomGenerator* rand)
 	}
 
 	//TODO: toggle with a flag
-	logGlobal->info("Initial zone grid:");
+#ifdef ZONE_PLACEMENT_LOG
+	logGlobal->trace("Initial zone grid:");
 	for (size_t x = 0; x < gridSize; ++x)
 	{
 		std::string s;
@@ -263,8 +266,9 @@ void CZonePlacer::placeOnGrid(CRandomGenerator* rand)
 				s += " -- ";
 			}
 		}
-		logGlobal->info(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)
@@ -372,7 +376,9 @@ void CZonePlacer::placeZones(CRandomGenerator * rand)
 				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;
 	};
@@ -419,7 +425,9 @@ void CZonePlacer::placeZones(CRandomGenerator * rand)
 	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
 	}
 }
 
@@ -688,7 +696,9 @@ void CZonePlacer::moveOneZone(TZoneMap& zones, TForceVector& totalForces, TDista
 		return lhs.first > rhs.first; //Largest dispalcement first
 	});
 
+#ifdef ZONE_PLACEMENT_LOG
 	logGlobal->trace("Worst misplacement/movement ratio: %3.2f", misplacedZones.front().first);
+#endif
 
 	if (misplacedZones.size() >= 2)
 	{
@@ -721,7 +731,9 @@ void CZonePlacer::moveOneZone(TZoneMap& zones, TForceVector& totalForces, TDista
 		}
 		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();
@@ -764,7 +776,9 @@ void CZonePlacer::moveOneZone(TZoneMap& zones, TForceVector& totalForces, TDista
 		{
 			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
 		}
@@ -792,7 +806,9 @@ void CZonePlacer::moveOneZone(TZoneMap& zones, TForceVector& totalForces, TDista
 		{
 			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
 		}

+ 4 - 5
lib/rmg/Zone.cpp

@@ -240,7 +240,7 @@ void Zone::fractalize()
 	//Squared
 	float minDistance = 9 * 9;
 	float freeDistance = pos.z ? (10 * 10) : 6 * 6;
-	float spanFactor = (pos.z ? 0.25 : 0.5f); //Narrower passages in the Underground
+	float spanFactor = (pos.z ? 0.3f : 0.45f); //Narrower passages in the Underground
 	float marginFactor = 1.0f;
 
 	int treasureValue = 0;
@@ -263,10 +263,10 @@ void Zone::fractalize()
 			// A quater at max density
 			marginFactor = (0.25f + ((std::max(0, (600 - treasureValue))) / (600.f - 400)) * 0.75f);
 		}
-		else if (treasureValue < 125)
+		else if (treasureValue < 100)
 		{
 			//Dense obstacles
-			spanFactor *= (treasureValue / 125.f);
+			spanFactor *= (treasureValue / 100.f);
 			vstd::amax(spanFactor, 0.15f);
 		}
 		if (treasureDensity <= 10)
@@ -278,7 +278,7 @@ void Zone::fractalize()
 	float blockDistance = minDistance * spanFactor; //More obstacles in the Underground
 	freeDistance = freeDistance * marginFactor;
 	vstd::amax(freeDistance, 4 * 4);
-	logGlobal->info("Zone %d: treasureValue %d blockDistance: %2.f, freeDistance: %2.f", getId(), treasureValue, blockDistance, freeDistance);
+	logGlobal->trace("Zone %d: treasureValue %d blockDistance: %2.f, freeDistance: %2.f", getId(), treasureValue, blockDistance, freeDistance);
 	
 	if(type != ETemplateZoneType::JUNCTION)
 	{
@@ -381,7 +381,6 @@ void Zone::initModificators()
 	{
 		modificator->init();
 	}
-	logGlobal->info("Zone %d modificators initialized", getId());
 }
 
 CRandomGenerator& Zone::getRand()

+ 2 - 2
lib/rmg/modificators/Modificator.cpp

@@ -80,7 +80,7 @@ void Modificator::run()
 
 	if(!finished)
 	{
-		logGlobal->info("Modificator zone %d - %s - started", zone.getId(), getName());
+		logGlobal->trace("Modificator zone %d - %s - started", zone.getId(), getName());
 		CStopWatch processTime;
 		try
 		{
@@ -94,7 +94,7 @@ void Modificator::run()
 		dump();
 #endif
 		finished = true;
-		logGlobal->info("Modificator zone %d - %s - done (%d ms)", zone.getId(), getName(), processTime.getDiff());
+		logGlobal->trace("Modificator zone %d - %s - done (%d ms)", zone.getId(), getName(), processTime.getDiff());
 	}
 }
 

+ 8 - 3
lib/rmg/modificators/ObjectManager.cpp

@@ -597,7 +597,7 @@ void ObjectManager::placeObject(rmg::Object & object, bool guarded, bool updateD
 	{
 		objectsVisitableArea.add(instance->getVisitablePosition());
 		objects.push_back(&instance->object());
-		if(auto * m = zone.getModificator<RoadPlacer>())
+		if(auto * rp = zone.getModificator<RoadPlacer>())
 		{
 			if (instance->object().blockVisit && !instance->object().removable)
 			{
@@ -607,7 +607,7 @@ void ObjectManager::placeObject(rmg::Object & object, bool guarded, bool updateD
 			else if(instance->object().appearance->isVisitableFromTop())
 			{
 				//Passable objects
-				m->areaForRoads().add(instance->getVisitablePosition());
+				rp->areaForRoads().add(instance->getVisitablePosition());
 			}
 			else if(!instance->object().appearance->isVisitableFromTop())
 			{
@@ -621,7 +621,12 @@ void ObjectManager::placeObject(rmg::Object & object, bool guarded, bool updateD
 					(!instance->object().blockingAt(tile + int3(0, 1, 0)) && 
 					instance->object().blockingAt(tile));
 				});				
-				m->areaIsolated().unite(borderAbove);
+				rp->areaIsolated().unite(borderAbove);
+			}
+
+			if (object.isGuarded())
+			{
+				rp->areaVisitable().add(instance->getVisitablePosition());
 			}
 		}
 

+ 4 - 4
lib/rmg/modificators/QuestArtifactPlacer.cpp

@@ -40,14 +40,14 @@ void QuestArtifactPlacer::addQuestArtZone(std::shared_ptr<Zone> otherZone)
 
 void QuestArtifactPlacer::addQuestArtifact(const ArtifactID& id)
 {
-	logGlobal->info("Need to place quest artifact %s", VLC->artifacts()->getById(id)->getNameTranslated());
+	logGlobal->trace("Need to place quest artifact %s", VLC->artifacts()->getById(id)->getNameTranslated());
 	RecursiveLock lock(externalAccessMutex);
 	questArtifactsToPlace.emplace_back(id);
 }
 
 void QuestArtifactPlacer::removeQuestArtifact(const ArtifactID& id)
 {
-	logGlobal->info("Will not try to place quest artifact %s", VLC->artifacts()->getById(id)->getNameTranslated());
+	logGlobal->trace("Will not try to place quest artifact %s", VLC->artifacts()->getById(id)->getNameTranslated());
 	RecursiveLock lock(externalAccessMutex);
 	vstd::erase_if_present(questArtifactsToPlace, id);
 }
@@ -76,7 +76,7 @@ void QuestArtifactPlacer::findZonesForQuestArts()
 		}
 	}
 
-	logGlobal->info("Number of nearby zones suitable for quest artifacts: %d", questArtZones.size());
+	logGlobal->trace("Number of nearby zones suitable for quest artifacts: %d", questArtZones.size());
 }
 
 void QuestArtifactPlacer::placeQuestArtifacts(CRandomGenerator & rand)
@@ -92,7 +92,7 @@ void QuestArtifactPlacer::placeQuestArtifacts(CRandomGenerator & rand)
 				continue;
 
 			auto artifactToReplace = *RandomGeneratorUtil::nextItem(artifactsToReplace, rand);
-			logGlobal->info("Replacing %s at %s with the quest artifact %s",
+			logGlobal->trace("Replacing %s at %s with the quest artifact %s",
 				artifactToReplace->getObjectName(),
 				artifactToReplace->getPosition().toString(),
 				VLC->artifacts()->getById(artifactToPlace)->getNameTranslated());

+ 22 - 4
lib/rmg/modificators/RoadPlacer.cpp

@@ -52,6 +52,11 @@ rmg::Area & RoadPlacer::areaIsolated()
 	return isolated;
 }
 
+rmg::Area & RoadPlacer::areaVisitable()
+{
+	return visitableTiles;
+}
+
 const rmg::Area & RoadPlacer::getRoads() const
 {
 	return roads;
@@ -64,7 +69,9 @@ bool RoadPlacer::createRoad(const int3 & dst)
 	rmg::Path path(searchArea);
 	path.connect(roads);
 
-	auto simpleRoutig = [this](const int3& src, const int3& dst)
+	const float VISITABLE_PENALTY = 1.33f;
+
+	auto simpleRoutig = [this, VISITABLE_PENALTY](const int3& src, const int3& dst)
 	{
 		if(areaIsolated().contains(dst))
 		{
@@ -72,14 +79,19 @@ bool RoadPlacer::createRoad(const int3 & dst)
 		}
 		else
 		{
-			return 1.0f;
+			auto ret = 1.0f;
+			if (visitableTiles.contains(src) || visitableTiles.contains(dst))
+			{
+				ret *= VISITABLE_PENALTY;
+			}
+			return ret;
 		}
 	};
 	
 	auto res = path.search(dst, true, simpleRoutig);
 	if(!res.valid())
 	{
-		auto desperateRoutig = [this](const int3& src, const int3& dst) -> float
+		auto desperateRoutig = [this, VISITABLE_PENALTY](const int3& src, const int3& dst) -> float
 		{
 			//Do not allow connections straight up through object not visitable from top
 			if(std::abs((src - dst).y) == 1)
@@ -98,7 +110,13 @@ bool RoadPlacer::createRoad(const int3 & dst)
 			}
 
 			float weight = dst.dist2dSQ(src);
-			return weight * weight;
+
+			auto ret =  weight * weight;
+			if (visitableTiles.contains(src) || visitableTiles.contains(dst))
+			{
+				ret *= VISITABLE_PENALTY;
+			}
+			return ret;
 		};
 		res = path.search(dst, false, desperateRoutig);
 

+ 3 - 0
lib/rmg/modificators/RoadPlacer.h

@@ -25,8 +25,10 @@ public:
 	void addRoadNode(const int3 & node);
 	void connectRoads(); //fills "roads" according to "roadNodes"
 	
+	// TODO: Use setters?
 	rmg::Area & areaForRoads();
 	rmg::Area & areaIsolated();
+	rmg::Area & areaVisitable();
 	const rmg::Area & getRoads() const;
 	
 protected:
@@ -38,6 +40,7 @@ protected:
 	rmg::Area roads; //all tiles with roads
 	rmg::Area areaRoads;
 	rmg::Area isolated;
+	rmg::Area visitableTiles; // Tiles occupied by removable or passable objects
 };
 
 VCMI_LIB_NAMESPACE_END