From 521180b08659759b2fe8bd355995ddc669a0ada6 Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Sun, 10 Aug 2025 11:20:22 -0400 Subject: [PATCH] Refactor out the junk randomizer and put it in rituals where it belongs. --- rituals.cpp | 21 +++++++++++++++++++++ rituals.hpp | 3 +++ systems.cpp | 15 +-------------- 3 files changed, 25 insertions(+), 14 deletions(-) diff --git a/rituals.cpp b/rituals.cpp index 62986c8..267e7cf 100644 --- a/rituals.cpp +++ b/rituals.cpp @@ -196,4 +196,25 @@ namespace ritual { contents.erase(item_id); } } + + JunkPile random_junk(components::GameConfig &config, int count) { + dbc::check(count > 0, "cant' call random_junk with count < 0"); + // this means the entity dropped loot, so make a lootable tombstone + ritual::JunkPile pile; + auto& junk = config.rituals["junk"]; + + ritual::JunkPile select_from; + for(auto& el : junk.items()) { + select_from.contents.push_back(el.key()); + } + + for(int i = 0; i < count; i++) { + size_t max_junk = select_from.contents.size(); + auto& item = select_from.contents.at(Random::uniform(size_t(0), max_junk-1)); + pile.contents.push_back(item); + } + + dbc::check(pile.contents.size() > 0, "ritual::random_junk returned junk size == 0"); + return pile; + } } diff --git a/rituals.hpp b/rituals.hpp index 6bad22c..46cba11 100644 --- a/rituals.hpp +++ b/rituals.hpp @@ -2,6 +2,7 @@ #include "goap.hpp" #include "ai.hpp" #include "config.hpp" +#include "components.hpp" namespace ritual { using JunkItem = std::string; @@ -96,4 +97,6 @@ namespace ritual { bool no_selections(); void consume_crafting(); }; + + JunkPile random_junk(components::GameConfig& config, int count); } diff --git a/systems.cpp b/systems.cpp index 33e8013..eb0fc92 100644 --- a/systems.cpp +++ b/systems.cpp @@ -144,20 +144,7 @@ void System::distribute_loot(GameLevel &level, Position target_pos) { auto loot_entity = world.entity(); if(inventory_count > 0) { - // this means the entity dropped loot, so make a lootable tombstone - ritual::JunkPile pile; - auto& junk = config.rituals["junk"]; - - ritual::JunkPile select_from; - for(auto& el : junk.items()) { - select_from.contents.push_back(el.key()); - } - - for(int i = 0; i < inventory_count; i++) { - size_t max_junk = select_from.contents.size(); - auto& item = select_from.contents.at(Random::uniform(size_t(0), max_junk-1)); - pile.contents.push_back(item); - } + auto pile = ritual::random_junk(config, inventory_count); auto entity_data = config.devices["GRAVE_STONE"]; components::configure_entity(world, loot_entity, entity_data["components"]);