Bläddra i källkod

Use CompoundMapObjectID in ObjectInfo

Tomasz Zieliński 1 år sedan
förälder
incheckning
4d4538a48d

+ 2 - 6
lib/rmg/ObjectConfig.cpp

@@ -32,16 +32,12 @@ void ObjectConfig::addBannedObject(const CompoundMapObjectID & objid)
 
 void ObjectConfig::addCustomObject(const ObjectInfo & object, const CompoundMapObjectID & objid)
 {
-	// FIXME: Need id / subId
-
-	// FIXME: Add templates and possibly other info
 	customObjects.push_back(object);
 	auto & lastObject = customObjects.back();
 	lastObject.setAllTemplates(objid.primaryID, objid.secondaryID);
 
 	assert(lastObject.templates.size() > 0);
-	auto temp = lastObject.templates.front();
-	logGlobal->info("Added custom object of type %d.%d", temp->id, temp->subid);
+	logGlobal->info("Added custom object of type %d.%d", objid.primaryID, objid.secondaryID);
 }
 
 void ObjectConfig::serializeJson(JsonSerializeFormat & handler)
@@ -154,7 +150,7 @@ void ObjectConfig::serializeJson(JsonSerializeFormat & handler)
 			VLC->objtypeh->resolveObjectCompoundId(objectName,
 				[this, objectValue, objectProbability, objectMaxPerZone](CompoundMapObjectID objid)
 				{
-					ObjectInfo object;
+					ObjectInfo object(objid.primaryID, objid.secondaryID);
 					
 					// TODO: Configure basic generateObject function
 

+ 17 - 1
lib/rmg/ObjectInfo.cpp

@@ -18,15 +18,24 @@
 
 VCMI_LIB_NAMESPACE_BEGIN
 
-ObjectInfo::ObjectInfo():
+ObjectInfo::ObjectInfo(si32 ID, si32 subID):
+	primaryID(ID),
+	secondaryID(subID),
 	destroyObject([](CGObjectInstance * obj){}),
 	maxPerZone(std::numeric_limits<ui32>::max())
 {
 }
 
+ObjectInfo::ObjectInfo(CompoundMapObjectID id):
+	ObjectInfo(id.primaryID, id.secondaryID)
+{
+}
+
 ObjectInfo::ObjectInfo(const ObjectInfo & other)
 {
 	templates = other.templates;
+	primaryID = other.primaryID;
+	secondaryID = other.secondaryID;
 	value = other.value;
 	probability = other.probability;
 	maxPerZone = other.maxPerZone;
@@ -40,6 +49,8 @@ ObjectInfo & ObjectInfo::operator=(const ObjectInfo & other)
 		return *this;
 
 	templates = other.templates;
+	primaryID = other.primaryID;
+	secondaryID = other.secondaryID;
 	value = other.value;
 	probability = other.probability;
 	maxPerZone = other.maxPerZone;
@@ -66,4 +77,9 @@ void ObjectInfo::setTemplates(MapObjectID type, MapObjectSubID subtype, TerrainI
 	templates = templHandler->getTemplates(terrainType);
 }
 
+CompoundMapObjectID ObjectInfo::getCompoundID() const
+{
+	return CompoundMapObjectID(primaryID, secondaryID);
+}
+
 VCMI_LIB_NAMESPACE_END

+ 5 - 1
lib/rmg/ObjectInfo.h

@@ -20,11 +20,14 @@ class CGObjectInstance;
 
 struct DLL_LINKAGE ObjectInfo
 {
-	ObjectInfo();
+	ObjectInfo(si32 ID, si32 subID);
+	ObjectInfo(CompoundMapObjectID id);
 	ObjectInfo(const ObjectInfo & other);
 	ObjectInfo & operator=(const ObjectInfo & other);
 
 	std::vector<std::shared_ptr<const ObjectTemplate>> templates;
+	si32 primaryID;
+	si32 secondaryID;
 	ui32 value = 0;
 	ui16 probability = 0;
 	ui32 maxPerZone = 1;
@@ -35,6 +38,7 @@ struct DLL_LINKAGE ObjectInfo
 	void setAllTemplates(MapObjectID type, MapObjectSubID subtype);
 	void setTemplates(MapObjectID type, MapObjectSubID subtype, TerrainId terrain);
 
+	CompoundMapObjectID getCompoundID() const;
 	//bool matchesId(const CompoundMapObjectID & id) const;
 };
 

+ 2 - 1
lib/rmg/modificators/ObjectDistributor.cpp

@@ -45,7 +45,6 @@ void ObjectDistributor::init()
 
 void ObjectDistributor::distributeLimitedObjects()
 {
-	ObjectInfo oi;
 	auto zones = map.getZones();
 
 	for (auto primaryID : VLC->objtypeh->knownObjects())
@@ -81,6 +80,8 @@ void ObjectDistributor::distributeLimitedObjects()
 					RandomGeneratorUtil::randomShuffle(matchingZones, zone.getRand());
 					for (auto& zone : matchingZones)
 					{
+						ObjectInfo oi(primaryID, secondaryID);
+						
 						oi.generateObject = [cb=map.mapInstance->cb, primaryID, secondaryID]() -> CGObjectInstance *
 						{
 							return VLC->objtypeh->getHandlerFor(primaryID, secondaryID)->create(cb, nullptr);

+ 28 - 38
lib/rmg/modificators/TreasurePlacer.cpp

@@ -47,6 +47,16 @@ void TreasurePlacer::process()
 		return;
 	}
 
+	tierValues = generator.getConfig().pandoraCreatureValues;
+	// Add all native creatures
+	for(auto const & cre : VLC->creh->objects)
+	{
+		if(!cre->special && cre->getFaction() == zone.getTownType())
+		{
+			creatures.push_back(cre.get());
+		}
+	}
+
 	// Get default objects
 	addAllPossibleObjects();
 	// Override with custom objects
@@ -64,19 +74,6 @@ void TreasurePlacer::init()
 	DEPENDENCY(ConnectionsPlacer);
 	DEPENDENCY_ALL(PrisonHeroPlacer);
 	DEPENDENCY(RoadPlacer);
-
-	// FIXME: Starting zones get Pandoras with neutral creatures
-
-	// Add all native creatures
-	for(auto const & cre : VLC->creh->objects)
-	{
-		if(!cre->special && cre->getFaction() == zone.getTownType())
-		{
-			creatures.push_back(cre.get());
-		}
-	}
-
-	tierValues = generator.getConfig().pandoraCreatureValues;
 }
 
 void TreasurePlacer::addObjectToRandomPool(const ObjectInfo& oi)
@@ -112,8 +109,6 @@ void TreasurePlacer::addAllPossibleObjects()
 
 void TreasurePlacer::addCommonObjects()
 {
-	ObjectInfo oi;
-	
 	for(auto primaryID : VLC->objtypeh->knownObjects())
 	{
 		for(auto secondaryID : VLC->objtypeh->knownSubObjects(primaryID))
@@ -127,12 +122,13 @@ void TreasurePlacer::addCommonObjects()
 					//Skip objects with per-map limit here
 					continue;
 				}
+				ObjectInfo oi(primaryID, secondaryID);
 				setBasicProperties(oi, CompoundMapObjectID(primaryID, secondaryID));
 
 				oi.value = rmgInfo.value;
 				oi.probability = rmgInfo.rarity;
-
 				oi.maxPerZone = rmgInfo.zoneLimit;
+
 				if(!oi.templates.empty())
 					addObjectToRandomPool(oi);
 			}
@@ -172,7 +168,7 @@ void TreasurePlacer::addPrisons()
 		size_t prisonsLeft = getMaxPrisons();
 		for (int i = prisonsLevels - 1; i >= 0; i--)
 		{
-			ObjectInfo oi; // Create new instance which will hold destructor operation
+			ObjectInfo oi(Obj::PRISON, 0); // Create new instance which will hold destructor operation
 
 			oi.value = generator.getConfig().prisonValues[i];
 			if (oi.value > zone.getMaxTreasureValue())
@@ -216,9 +212,7 @@ void TreasurePlacer::addDwellings()
 {
 	if(zone.getType() == ETemplateZoneType::WATER)
 		return;
-	
-	ObjectInfo oi;
-	
+
 	//dwellings
 	auto dwellingTypes = {Obj::CREATURE_GENERATOR1, Obj::CREATURE_GENERATOR4};
 	
@@ -245,6 +239,9 @@ void TreasurePlacer::addDwellings()
 			if(cre->getFaction() == zone.getTownType())
 			{
 				auto nativeZonesCount = static_cast<float>(map.getZoneCount(cre->getFaction()));
+				ObjectInfo oi(dwellingType, secondaryID);
+				setBasicProperties(oi, CompoundMapObjectID(dwellingType, secondaryID));
+
 				oi.value = static_cast<ui32>(cre->getAIValue() * cre->getGrowth() * (1 + (nativeZonesCount / map.getTotalZoneCount()) + (nativeZonesCount / 2)));
 				oi.probability = 40;
 				
@@ -254,7 +251,6 @@ void TreasurePlacer::addDwellings()
 					obj->tempOwner = PlayerColor::NEUTRAL;
 					return obj;
 				};
-				oi.setTemplates(dwellingType, secondaryID, zone.getTerrainType());
 				if(!oi.templates.empty())
 					addObjectToRandomPool(oi);
 			}
@@ -267,7 +263,7 @@ void TreasurePlacer::addScrolls()
 	if(zone.getType() == ETemplateZoneType::WATER)
 		return;
 
-	ObjectInfo oi;
+	ObjectInfo oi(Obj::SPELL_SCROLL, 0);
 
 	for(int i = 0; i < generator.getConfig().scrollValues.size(); i++)
 	{
@@ -308,8 +304,7 @@ void TreasurePlacer::addPandoraBoxes()
 
 void TreasurePlacer::addPandoraBoxesWithGold()
 {
-	ObjectInfo oi;
-	//pandora box with gold
+	ObjectInfo oi(Obj::PANDORAS_BOX, 0);
 	for(int i = 1; i < 5; i++)
 	{
 		oi.generateObject = [this, i]() -> CGObjectInstance *
@@ -334,8 +329,7 @@ void TreasurePlacer::addPandoraBoxesWithGold()
 
 void TreasurePlacer::addPandoraBoxesWithExperience()
 {
-	ObjectInfo oi;
-	//pandora box with experience
+	ObjectInfo oi(Obj::PANDORAS_BOX, 0);
 	for(int i = 1; i < 5; i++)
 	{
 		oi.generateObject = [this, i]() -> CGObjectInstance *
@@ -360,9 +354,7 @@ void TreasurePlacer::addPandoraBoxesWithExperience()
 
 void TreasurePlacer::addPandoraBoxesWithCreatures()
 {
-	ObjectInfo oi;
-	//pandora box with creatures
-
+	ObjectInfo oi(Obj::PANDORAS_BOX, 0);
 	for(auto * creature : creatures)
 	{
 		int creaturesAmount = creatureToCount(creature);
@@ -391,7 +383,7 @@ void TreasurePlacer::addPandoraBoxesWithCreatures()
 
 void TreasurePlacer::addPandoraBoxesWithSpells()
 {
-	ObjectInfo oi;
+	ObjectInfo oi(Obj::PANDORAS_BOX, 0);
 	//Pandora with 12 spells of certain level
 	for(int i = 1; i <= GameConstants::SPELL_LEVELS; i++)
 	{
@@ -494,7 +486,7 @@ void TreasurePlacer::addSeerHuts()
 {
 	//Seer huts with creatures or generic rewards
 
-	ObjectInfo oi;
+	ObjectInfo oi(Obj::SEER_HUT, 0);
 
 	if(zone.getConnectedZoneIds().size()) //Unlikely, but...
 	{
@@ -1120,7 +1112,7 @@ void TreasurePlacer::ObjectPool::updateObject(MapObjectID id, MapObjectSubID sub
 		- Pandora Boxes
 	*/
 	// FIXME: This will drop all templates
-	customObjects[CompoundMapObjectID(id, subid)] = info;
+	customObjects.insert(std::make_pair(CompoundMapObjectID(id, subid), info));
 }
 
 void TreasurePlacer::ObjectPool::patchWithZoneConfig(const Zone & zone, TreasurePlacer * tp)
@@ -1147,8 +1139,7 @@ void TreasurePlacer::ObjectPool::patchWithZoneConfig(const Zone & zone, Treasure
 
 	vstd::erase_if(possibleObjects, [this, &categoriesSet](const ObjectInfo & oi) -> bool
 	{
-		auto temp = oi.templates.front();
-		auto category = getObjectCategory(CompoundMapObjectID(temp->id, temp->subid));
+		auto category = getObjectCategory(oi.getCompoundID());
 		if (categoriesSet.count(category))
 		{
 			logGlobal->info("Removing object %s from possible objects", oi.templates.front()->stringID);
@@ -1168,7 +1159,7 @@ void TreasurePlacer::ObjectPool::patchWithZoneConfig(const Zone & zone, Treasure
 	{
 		for (const auto & templ : object.templates)
 		{
-			CompoundMapObjectID key(templ->id, templ->subid);
+			CompoundMapObjectID key = object.getCompoundID();
 			if (bannedObjectsSet.count(key))
 			{
 				// FIXME: Stopped working, nothing is banned
@@ -1184,10 +1175,9 @@ void TreasurePlacer::ObjectPool::patchWithZoneConfig(const Zone & zone, Treasure
 	// FIXME: Access TreasurePlacer from ObjectPool
 	for (auto & object : configuredObjects)
 	{
-		auto temp = object.templates.front();
-		tp->setBasicProperties(object, CompoundMapObjectID(temp->id, temp->subid));
+		tp->setBasicProperties(object, object.getCompoundID());
 		addObject(object);
-		logGlobal->info("Added custom object of type %d.%d", temp->id, temp->subid);
+		logGlobal->info("Added custom object of type %d.%d", object.primaryID, object.secondaryID);
 	}
 	// TODO: Overwrite or add to possibleObjects