From 56d8a23255f55c1b7fad1c43936737c2dc96f473 Mon Sep 17 00:00:00 2001 From: shweet Date: Thu, 9 Apr 2026 18:13:11 -0400 Subject: [PATCH] issues with item generation --- src/resource/xml/item.cpp | 8 ++------ src/resource/xml/item.hpp | 4 ++-- src/state/play/item/reward.cpp | 24 +++++++++++++++++++----- src/state/play/menu/arcade.cpp | 2 +- 4 files changed, 24 insertions(+), 14 deletions(-) diff --git a/src/resource/xml/item.cpp b/src/resource/xml/item.cpp index 694e6a5..a9203d5 100644 --- a/src/resource/xml/item.cpp +++ b/src/resource/xml/item.cpp @@ -58,7 +58,7 @@ namespace game::resource::xml Rarity rarity{}; query_string_attribute(child, "Name", &rarity.name); - child->QueryFloatAttribute("Chance", &rarity.chance); + child->QueryFloatAttribute("Weight", &rarity.weight); query_bool_attribute(child, "IsHidden", &rarity.isHidden); query_sound(child, "Sound", archive, soundRootPath, rarity.sound); @@ -183,11 +183,7 @@ namespace game::resource::xml } for (int i = 0; i < (int)rarities.size(); i++) - { - rarityIDsSortedByChance.emplace_back(i); - } - std::stable_sort(rarityIDsSortedByChance.begin(), rarityIDsSortedByChance.end(), - [&](int a, int b) { return rarities[a].chance > rarities[b].chance; }); + rarityIDs.emplace_back(i); isValid = true; logger.info(std::format("Initialized item schema: {}", path.c_str())); diff --git a/src/resource/xml/item.hpp b/src/resource/xml/item.hpp index 99b77cd..e0836f7 100644 --- a/src/resource/xml/item.hpp +++ b/src/resource/xml/item.hpp @@ -28,7 +28,7 @@ namespace game::resource::xml struct Rarity { std::string name{UNDEFINED}; - float chance{}; + float weight{}; bool isHidden{}; Audio sound{}; }; @@ -87,7 +87,7 @@ namespace game::resource::xml std::vector items{}; std::vector anm2s{}; - std::vector rarityIDsSortedByChance{}; + std::vector rarityIDs{}; std::unordered_map pools{}; Pool skillCheckRewardItemPool{}; diff --git a/src/state/play/item/reward.cpp b/src/state/play/item/reward.cpp index 67d1d32..7d4a447 100644 --- a/src/state/play/item/reward.cpp +++ b/src/state/play/item/reward.cpp @@ -9,13 +9,27 @@ namespace game::state::play::item int Reward::random_item_get(const resource::xml::Item& itemSchema, float chanceBonus) { const resource::xml::Item::Pool* pool{}; + auto totalWeight = 0.0f; - for (auto& id : itemSchema.rarityIDsSortedByChance) + for (auto& id : itemSchema.rarityIDs) { auto& rarity = itemSchema.rarities[id]; - if (rarity.chance <= 0.0f) continue; + if (rarity.weight <= 0.0f) continue; + totalWeight += rarity.weight * chanceBonus; + } - if (math::random_percent_roll(rarity.chance * chanceBonus)) + if (totalWeight <= 0.0f) return INVALID_ID; + + auto roll = math::random_roll(totalWeight); + + for (auto& id : itemSchema.rarityIDs) + { + auto& rarity = itemSchema.rarities[id]; + auto weight = rarity.weight * chanceBonus; + if (weight <= 0.0f) continue; + + roll -= weight; + if (roll <= 0.0f) { pool = &itemSchema.pools.at(id); rarity.sound.play(); @@ -40,8 +54,6 @@ namespace game::state::play::item const resource::xml::Item& itemSchema, const ImVec4& bounds, float rewardChance, float rewardRollCount, menu::ItemEffectManager::Mode mode) { - if (!math::random_percent_roll(rewardChance)) return 0; - auto rollCountWhole = std::max(0, (int)std::floor(rewardRollCount)); auto rollCountFraction = std::max(0.0f, rewardRollCount - (float)rollCountWhole); auto rollCount = rollCountWhole + (math::random_percent_roll(rollCountFraction) ? 1 : 0); @@ -49,6 +61,8 @@ namespace game::state::play::item for (int i = 0; i < rollCount; i++) { + if (!math::random_percent_roll(rewardChance)) continue; + auto itemID = random_item_get(itemSchema); if (itemID == INVALID_ID) continue; diff --git a/src/state/play/menu/arcade.cpp b/src/state/play/menu/arcade.cpp index e2e99e0..f11e329 100644 --- a/src/state/play/menu/arcade.cpp +++ b/src/state/play/menu/arcade.cpp @@ -222,7 +222,7 @@ namespace game::state::play::menu { case MENU: game_menu_draw(ORBIT); - //game_menu_draw(DUNGEON); + game_menu_draw(DUNGEON); game_menu_draw(SKILL_CHECK); break;