From 81a64a8e67336dbe5af454babbec8b0885238da9 Mon Sep 17 00:00:00 2001 From: DjWarmonger Date: Sat, 7 Jun 2014 15:51:03 +0200 Subject: [PATCH] Templates can now use multiple configurations of treasure piles. --- config/rmg.json | 40 ++++++++++++---- lib/rmg/CRmgTemplateStorage.cpp | 26 +++++++--- lib/rmg/CRmgTemplateZone.cpp | 84 ++++++++++++++++++++------------- 3 files changed, 103 insertions(+), 47 deletions(-) diff --git a/config/rmg.json b/config/rmg.json index e4881b7de..a015da1d0 100644 --- a/config/rmg.json +++ b/config/rmg.json @@ -10,7 +10,10 @@ { "type" : "playerStart", "size" : 1, "owner" : 1, "playerTowns" : { "castles" : 1 }, "neutralTowns" : { "towns" : 1 }, "townsAreSameType" : true, - "treasure" : {"min" : 1500, "max": 3000} + "treasure" : [ + {"min" : 2100, "max": 3000, "density" : 5}, + {"min" : 300, "max": 1500, "density" : 10} + ] }, "2" : { @@ -33,7 +36,10 @@ "5" : { "type" : "treasure", "size" : 2, "terrainTypes" : [ "sand" ], "neutralTowns" : { "castles" : 1 }, - "treasure" : {"min" : 9000, "max": 10000} + "treasure" : [ + {"min" : 9000, "max": 10000, "density" : 3}, + {"min" : 6000, "max": 10000, "density" : 15} + ] } }, "connections" : @@ -54,7 +60,10 @@ { "type" : "playerStart", "size" : 1, "owner" : 1, "playerTowns" : { "castles" : 1 }, - "treasure" : {"min" : 500, "max": 2000} + "treasure" : [ + {"min" : 400, "max": 1000, "density" : 16}, + {"min" : 1500, "max": 2500, "density" : 4} + ] }, "2" : { @@ -65,7 +74,10 @@ "3" : { "type" : "treasure", "size" : 2, "neutralTowns" : { "towns" : 1 }, "townTypeLikeZone" : "1", - "treasure" : {"min" : 2000, "max": 4000} + "treasure" : [ + {"min" : 2000, "max": 4000, "density" : 15}, + {"min" : 4000, "max": 5000, "density" : 5} + ] }, "4" : { @@ -75,7 +87,10 @@ "5" : { "type" : "treasure", "size" : 3, "neutralTowns" : { "castles" : 1 }, "terrainTypes" : [ "sand" ], - "treasure" : {"min" : 11000, "max": 12000} + "treasure" : [ + {"min" : 11000, "max": 12000, "density" : 5}, + {"min" : 6000, "max": 11000, "density" : 10} + ] } }, "connections" : @@ -98,7 +113,10 @@ { "type" : "playerStart", "size" : 3, "owner" : 1, "playerTowns" : { "castles" : 1 }, - "treasure" : {"min" : 1000, "max": 2000} + "treasure" : [ + {"min" : 300, "max": 2000, "density": 15}, + {"min" : 2100, "max": 2500, "density": 5} + ] }, "2" : { @@ -112,13 +130,19 @@ "playerTowns" : { "castles" : 1 }, "treasureLikeZone" : 1 }, - "4" : { "type" : "treasure", "size" : 1, "terrainTypeLikeZone" : "1", "treasure" : {"min" : 3000, "max": 10000} }, + "4" : { "type" : "treasure", "size" : 1, "terrainTypeLikeZone" : "1", "treasure" : [ + {"min" : 3000, "max": 10000, "density" : 12}, + {"min" : 6000, "max": 10000, "density" : 6} + ]}, "5" : { "type" : "treasure", "size" : 1, "terrainTypeLikeZone" : "1", "treasureLikeZone" : 4}, "6" : { "type" : "treasure", "size" : 1, "terrainTypeLikeZone" : "2", "treasureLikeZone" : 4 }, "7" : { "type" : "treasure", "size" : 1, "terrainTypeLikeZone" : "2", "treasureLikeZone" : 4 }, "8" : { "type" : "treasure", "size" : 1, "terrainTypeLikeZone" : "3", "treasureLikeZone" : 4 }, "9" : { "type" : "treasure", "size" : 1, "terrainTypeLikeZone" : "3", "treasureLikeZone" : 4 }, - "10" : { "type" : "treasure", "size" : 1, "neutralTowns" : { "towns" : 1 }, "treasure" : {"min" : 21000, "max": 25000} }, + "10" : { "type" : "treasure", "size" : 1, "neutralTowns" : { "towns" : 1 }, "treasure" : [ + {"min" : 21000, "max": 25000, "density" : 3}, + {"min" : 10000, "max": 21000, "density" : 10} + ]}, "11" : { "type" : "treasure", "size" : 1, "neutralTowns" : { "towns" : 1 }, "treasureLikeZone" : 10 }, "12" : { "type" : "treasure", "size" : 1, "neutralTowns" : { "towns" : 1 }, "treasureLikeZone" : 10 } }, diff --git a/lib/rmg/CRmgTemplateStorage.cpp b/lib/rmg/CRmgTemplateStorage.cpp index 435a99587..43bb2ffb3 100644 --- a/lib/rmg/CRmgTemplateStorage.cpp +++ b/lib/rmg/CRmgTemplateStorage.cpp @@ -68,13 +68,27 @@ void CJsonRmgTemplateLoader::loadTemplates() if (!zoneNode["treasure"].isNull()) { //TODO: parse vector of different treasure settings - auto treasureInfo = zoneNode["treasure"].Struct(); + if (zoneNode["treasure"].getType() == JsonNode::DATA_STRUCT) { - CTreasureInfo ti; - ti.min = treasureInfo["min"].Float(); - ti.max = treasureInfo["max"].Float(); - ti.density = 1; //TODO: use me - zone->addTreasureInfo(ti); + auto treasureInfo = zoneNode["treasure"].Struct(); + { + CTreasureInfo ti; + ti.min = treasureInfo["min"].Float(); + ti.max = treasureInfo["max"].Float(); + ti.density = treasureInfo["density"].Float(); //TODO: use me + zone->addTreasureInfo(ti); + } + } + else if (zoneNode["treasure"].getType() == JsonNode::DATA_VECTOR) + { + for (auto treasureInfo : zoneNode["treasure"].Vector()) + { + CTreasureInfo ti; + ti.min = treasureInfo["min"].Float(); + ti.max = treasureInfo["max"].Float(); + ti.density = treasureInfo["density"].Float(); + zone->addTreasureInfo(ti); + } } } diff --git a/lib/rmg/CRmgTemplateZone.cpp b/lib/rmg/CRmgTemplateZone.cpp index d458f5f47..b92ebf1b7 100644 --- a/lib/rmg/CRmgTemplateZone.cpp +++ b/lib/rmg/CRmgTemplateZone.cpp @@ -471,11 +471,29 @@ bool CRmgTemplateZone::createTreasurePile (CMapGenerator* gen, int3 &pos) int maxValue = 5000; int minValue = 1500; - //TODO: choose random treasure info based on density if (treasureInfo.size()) { - maxValue = treasureInfo.front().max; - minValue = treasureInfo.front().min; + //roulette wheel + std::vector> 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); + + for (auto t : tresholds) + { + if (r <= t.first) + { + maxValue = t.second.max; + minValue = t.second.min; + break; + } + } } int currentValue = 0; @@ -1094,37 +1112,37 @@ void CRmgTemplateZone::addAllPossibleObjects (CMapGenerator* gen) } //non-removable object for test - oi.generateObject = [gen]() -> CGObjectInstance * - { - auto obj = new CGMagicWell(); - obj->ID = Obj::MAGIC_WELL; - obj->subID = 0; - return obj; - }; - oi.value = 250; - oi.probability = 100; - possibleObjects.push_back (oi); + //oi.generateObject = [gen]() -> CGObjectInstance * + //{ + // auto obj = new CGMagicWell(); + // obj->ID = Obj::MAGIC_WELL; + // obj->subID = 0; + // return obj; + //}; + //oi.value = 250; + //oi.probability = 100; + //possibleObjects.push_back (oi); - oi.generateObject = [gen]() -> CGObjectInstance * - { - auto obj = new CGObelisk(); - obj->ID = Obj::OBELISK; - obj->subID = 0; - return obj; - }; - oi.value = 3500; - oi.probability = 200; - possibleObjects.push_back (oi); + //oi.generateObject = [gen]() -> CGObjectInstance * + //{ + // auto obj = new CGObelisk(); + // obj->ID = Obj::OBELISK; + // obj->subID = 0; + // return obj; + //}; + //oi.value = 3500; + //oi.probability = 200; + //possibleObjects.push_back (oi); - oi.generateObject = [gen]() -> CGObjectInstance * - { - auto obj = new CBank(); - obj->ID = Obj::CREATURE_BANK; - obj->subID = 5; //naga bank - return obj; - }; - oi.value = 3000; - oi.probability = 100; - possibleObjects.push_back (oi); + //oi.generateObject = [gen]() -> CGObjectInstance * + //{ + // auto obj = new CBank(); + // obj->ID = Obj::CREATURE_BANK; + // obj->subID = 5; //naga bank + // return obj; + //}; + //oi.value = 3000; + //oi.probability = 100; + //possibleObjects.push_back (oi); }