瀏覽代碼

Rearranged treasure pile info.

DjWarmonger 11 年之前
父節點
當前提交
1a34297c33
共有 3 個文件被更改,包括 27 次插入15 次删除
  1. 7 0
      lib/rmg/CRmgTemplateStorage.cpp
  2. 16 15
      lib/rmg/CRmgTemplateZone.cpp
  3. 4 0
      lib/rmg/CRmgTemplateZone.h

+ 7 - 0
lib/rmg/CRmgTemplateStorage.cpp

@@ -67,6 +67,7 @@ void CJsonRmgTemplateLoader::loadTemplates()
 				//treasures
 				//treasures
 				if (!zoneNode["treasure"].isNull())
 				if (!zoneNode["treasure"].isNull())
 				{
 				{
+					int totalDensity = 0;
 					//TODO: parse vector of different treasure settings
 					//TODO: parse vector of different treasure settings
 					if (zoneNode["treasure"].getType() == JsonNode::DATA_STRUCT)
 					if (zoneNode["treasure"].getType() == JsonNode::DATA_STRUCT)
 					{
 					{
@@ -76,6 +77,8 @@ void CJsonRmgTemplateLoader::loadTemplates()
 							ti.min = treasureInfo["min"].Float();
 							ti.min = treasureInfo["min"].Float();
 							ti.max = treasureInfo["max"].Float();
 							ti.max = treasureInfo["max"].Float();
 							ti.density = treasureInfo["density"].Float(); //TODO: use me
 							ti.density = treasureInfo["density"].Float(); //TODO: use me
+							totalDensity += ti.density;
+							ti.threshold = totalDensity;
 							zone->addTreasureInfo(ti);
 							zone->addTreasureInfo(ti);
 						}
 						}
 					}
 					}
@@ -87,9 +90,12 @@ void CJsonRmgTemplateLoader::loadTemplates()
 							ti.min = treasureInfo["min"].Float();
 							ti.min = treasureInfo["min"].Float();
 							ti.max = treasureInfo["max"].Float();
 							ti.max = treasureInfo["max"].Float();
 							ti.density = treasureInfo["density"].Float();
 							ti.density = treasureInfo["density"].Float();
+							totalDensity += ti.density;
+							ti.threshold = totalDensity;
 							zone->addTreasureInfo(ti);
 							zone->addTreasureInfo(ti);
 						}
 						}
 					}
 					}
+					zone->setTotalDensity (totalDensity);
 				}
 				}
 
 
 				zones[zone->getId()] = zone;
 				zones[zone->getId()] = zone;
@@ -113,6 +119,7 @@ void CJsonRmgTemplateLoader::loadTemplates()
 					{
 					{
 						zone->addTreasureInfo(treasureInfo);
 						zone->addTreasureInfo(treasureInfo);
 					}
 					}
+					zone->setTotalDensity (zones[zoneNode["treasureLikeZone"].Float()]->getTotalDensity());
 				}
 				}
 			}
 			}
 
 

+ 16 - 15
lib/rmg/CRmgTemplateZone.cpp

@@ -121,7 +121,7 @@ void CTileInfo::setTerrainType(ETerrainType value)
 }
 }
 
 
 CRmgTemplateZone::CRmgTemplateZone() : id(0), type(ETemplateZoneType::PLAYER_START), size(1),
 CRmgTemplateZone::CRmgTemplateZone() : id(0), type(ETemplateZoneType::PLAYER_START), size(1),
-	townsAreSameType(false), matchTerrainToTown(true)
+	townsAreSameType(false), matchTerrainToTown(true), totalDensity(0)
 {
 {
 	townTypes = getDefaultTownTypes();
 	townTypes = getDefaultTownTypes();
 	terrainTypes = getDefaultTerrainTypes();
 	terrainTypes = getDefaultTerrainTypes();
@@ -284,6 +284,16 @@ std::vector<TRmgTemplateZoneId> CRmgTemplateZone::getConnections() const
 	return connections;
 	return connections;
 }
 }
 
 
+void CRmgTemplateZone::setTotalDensity (ui16 val)
+{
+	totalDensity = val;
+}
+
+ui16 CRmgTemplateZone::getTotalDensity () const
+{
+	return totalDensity;
+}
+
 void CRmgTemplateZone::addTreasureInfo(CTreasureInfo & info)
 void CRmgTemplateZone::addTreasureInfo(CTreasureInfo & info)
 {
 {
 	treasureInfo.push_back(info);
 	treasureInfo.push_back(info);
@@ -485,23 +495,14 @@ bool CRmgTemplateZone::createTreasurePile (CMapGenerator* gen, int3 &pos)
 	if (treasureInfo.size())
 	if (treasureInfo.size())
 	{
 	{
 		//roulette wheel
 		//roulette wheel
-		std::vector<std::pair<ui16, CTreasureInfo>> tresholds;
-		ui16 total = 0;
-		//TODO: precalculate density chance for entire zone
-		for (auto ti : treasureInfo)
-		{
-			total += ti.density;
-			tresholds.push_back (std::make_pair (total, ti));
-		}
-
-		int r = gen->rand.nextInt (1, total);
+		int r = gen->rand.nextInt (1, totalDensity);
 
 
-		for (auto t : tresholds)
+		for (auto t : treasureInfo)
 		{
 		{
-			if (r <= t.first)
+			if (r <= t.threshold)
 			{
 			{
-				maxValue = t.second.max;
-				minValue = t.second.min;
+				maxValue = t.max;
+				minValue = t.min;
 				break;
 				break;
 			}
 			}
 		}
 		}

+ 4 - 0
lib/rmg/CRmgTemplateZone.h

@@ -59,6 +59,7 @@ public:
 	ui32 min;
 	ui32 min;
 	ui32 max;
 	ui32 max;
 	ui16 density;
 	ui16 density;
+	ui16 threshold; //how much must RNG roll to pick that zone
 };
 };
 
 
 struct DLL_LINKAGE ObjectInfo
 struct DLL_LINKAGE ObjectInfo
@@ -134,6 +135,8 @@ public:
 	void createBorder(CMapGenerator* gen);
 	void createBorder(CMapGenerator* gen);
 	bool crunchPath (CMapGenerator* gen, const int3 &src, const int3 &dst, TRmgTemplateZoneId zone);
 	bool crunchPath (CMapGenerator* gen, const int3 &src, const int3 &dst, TRmgTemplateZoneId zone);
 
 
+	void setTotalDensity (ui16 val);
+	ui16 getTotalDensity () const;
 	void addConnection(TRmgTemplateZoneId otherZone);
 	void addConnection(TRmgTemplateZoneId otherZone);
 	std::vector<TRmgTemplateZoneId> getConnections() const;
 	std::vector<TRmgTemplateZoneId> getConnections() const;
 	void addTreasureInfo(CTreasureInfo & info);
 	void addTreasureInfo(CTreasureInfo & info);
@@ -154,6 +157,7 @@ private:
 	std::set<ETerrainType> terrainTypes;
 	std::set<ETerrainType> terrainTypes;
 	boost::optional<TRmgTemplateZoneId> terrainTypeLikeZone, townTypeLikeZone;
 	boost::optional<TRmgTemplateZoneId> terrainTypeLikeZone, townTypeLikeZone;
 
 
+	ui16 totalDensity;
 	std::vector<CTreasureInfo> treasureInfo;
 	std::vector<CTreasureInfo> treasureInfo;
 	std::vector<ObjectInfo> possibleObjects;
 	std::vector<ObjectInfo> possibleObjects;