From a8bf8a04ae700eec1d513da204cf6410b07e7fb5 Mon Sep 17 00:00:00 2001 From: Myk Taylor Date: Mon, 28 Aug 2023 15:42:56 -0700 Subject: [PATCH] add in value for units contained in cages --- docs/changelog.txt | 1 + library/modules/Items.cpp | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/docs/changelog.txt b/docs/changelog.txt index 2eb4c39fa..2d78ef76e 100644 --- a/docs/changelog.txt +++ b/docs/changelog.txt @@ -62,6 +62,7 @@ Template for new versions: - Core: reload scripts in mods when a world is unloaded and immediately loaded again - Core: fix text getting added to DFHack text entry widgets when Alt- or Ctrl- keys are hit - `orders`: prevent import/export overlay from appearing on the create workorder screen +- `caravan`: corrected prices for cages that have units inside of them ## Misc Improvements - Surround DFHack-specific UI elements with square brackets instead of red-yellow blocks for better readability diff --git a/library/modules/Items.cpp b/library/modules/Items.cpp index 0b91bdd47..bf4437ef7 100644 --- a/library/modules/Items.cpp +++ b/library/modules/Items.cpp @@ -1910,6 +1910,21 @@ static int32_t get_sell_request_multiplier(df::item *item, const df::caravan_sta return get_sell_request_multiplier(item, caravan_he->resources, &sell_prices->price[0]); } +static int32_t get_sell_request_multiplier(df::unit *unit, const df::caravan_state *caravan) { + const df::entity_sell_prices *sell_prices = caravan->sell_prices; + if (!sell_prices) + return DEFAULT_AGREEMENT_MULTIPLIER; + + auto caravan_he = df::historical_entity::find(caravan->entity); + if (!caravan_he) + return DEFAULT_AGREEMENT_MULTIPLIER; + + auto & resources = caravan_he->resources; + int32_t price = get_price(resources.animals.pet_races, unit->race, resources.animals.pet_castes, unit->caste, + sell_prices->price[df::entity_sell_category::Pets]); + return (price != -1) ? price : DEFAULT_AGREEMENT_MULTIPLIER; +} + static bool is_requested_trade_good(df::item *item, df::caravan_state *caravan) { auto trade_state = caravan->trade_state; if (caravan->time_remaining <= 0 || @@ -2031,6 +2046,28 @@ int Items::getValue(df::item *item, df::caravan_state *caravan) if (divisor > 1) value /= divisor; } + + // Add in value from units contained in cages + if (item_type == item_type::CAGE) { + for (auto gref : item->general_refs) { + if (gref->getType() != df::general_ref_type::CONTAINS_UNIT) + continue; + auto unit = gref->getUnit(); + if (!unit) + continue; + df::creature_raw *raw = world->raws.creatures.all[unit->race]; + df::caste_raw *caste = raw->caste.at(unit->caste); + int unit_value = caste->misc.petvalue; + if (Units::isWar(unit) || Units::isHunter(unit)) + unit_value *= 2; + if (caravan) { + unit_value *= get_sell_request_multiplier(unit, caravan); + unit_value >>= 7; + } + value += unit_value; + } + } + return value; }