| 11 | namespace Star { |
| 12 | |
| 13 | TreasureDatabase::TreasureDatabase() { |
| 14 | auto assets = Root::singleton().assets(); |
| 15 | |
| 16 | auto& treasurePools = assets->scanExtension("treasurepools"); |
| 17 | auto& treasureChests = assets->scanExtension("treasurechests"); |
| 18 | |
| 19 | assets->queueJsons(treasurePools); |
| 20 | assets->queueJsons(treasureChests); |
| 21 | |
| 22 | for (auto& file : treasurePools) { |
| 23 | for (auto const& pair : assets->json(file).iterateObject()) { |
| 24 | if (m_treasurePools.contains(pair.first)) |
| 25 | throw TreasureException(strf("Duplicate TreasurePool config '{}' from file '{}'", pair.first, file)); |
| 26 | |
| 27 | auto& treasurePool = m_treasurePools[pair.first]; |
| 28 | for (auto const& entry : pair.second.iterateArray()) { |
| 29 | if (entry.size() != 2) |
| 30 | throw TreasureException("Wrong size for TreasurePool entry, list must be 2"); |
| 31 | |
| 32 | float startLevel = entry.getFloat(0); |
| 33 | auto config = entry.get(1); |
| 34 | |
| 35 | ItemPool itemPool; |
| 36 | |
| 37 | for (auto const& entry : config.getArray("fill", {})) |
| 38 | if (entry.contains("pool")) |
| 39 | itemPool.fill.append(entry.getString("pool")); |
| 40 | else if (entry.contains("item")) |
| 41 | itemPool.fill.append(ItemDescriptor(entry.get("item"))); |
| 42 | else |
| 43 | throw TreasureException(strf("TreasurePool entry '{}' did not specify a valid 'item' or 'pool'", entry)); |
| 44 | |
| 45 | for (auto const& entry : config.getArray("pool", {})) { |
| 46 | if (!entry.contains("weight")) |
| 47 | throw TreasureException(strf("TreasurePool entry '{}' did not specify a weight", entry)); |
| 48 | |
| 49 | if (entry.contains("pool")) |
| 50 | itemPool.pool.add(entry.getFloat("weight"), entry.getString("pool")); |
| 51 | else if (entry.contains("item")) |
| 52 | itemPool.pool.add(entry.getFloat("weight"), ItemDescriptor(entry.get("item"))); |
| 53 | else |
| 54 | throw TreasureException(strf("TreasurePool entry '{}' did not specify a valid 'item' or 'pool'", entry)); |
| 55 | } |
| 56 | |
| 57 | auto poolRounds = config.get("poolRounds", 1); |
| 58 | if (poolRounds.canConvert(Json::Type::Float)) { |
| 59 | itemPool.poolRounds = WeightedPool<int>(List<std::pair<double, int>>{{1.0, poolRounds.toFloat()}}); |
| 60 | } else { |
| 61 | for (auto const& pair : poolRounds.iterateArray()) |
| 62 | itemPool.poolRounds.add(pair.getDouble(0), pair.getInt(1)); |
| 63 | } |
| 64 | |
| 65 | itemPool.levelVariance = jsonToVec2F(config.get("levelVariance", JsonArray{0, 0})); |
| 66 | itemPool.allowDuplication = config.getBool("allowDuplication", true); |
| 67 | |
| 68 | treasurePool.addPoint(startLevel, std::move(itemPool)); |
| 69 | } |
| 70 | } |
nothing calls this directly
no test coverage detected