diff --git a/docs/dev/Lua API.rst b/docs/dev/Lua API.rst index 8553d4e1a..11bac3f61 100644 --- a/docs/dev/Lua API.rst +++ b/docs/dev/Lua API.rst @@ -1778,9 +1778,10 @@ Items module for the item and ``false`` to get the price that the caravan will sell the item for. -* ``dfhack.items.isRequestedTradeGood(item)`` +* ``dfhack.items.isRequestedTradeGood(item[, caravan_state])`` - Returns whether any active caravan will pay extra for the given item. + Returns whether a caravan will pay extra for the given item. If caravan_state + is not given, checks all active caravans. * ``dfhack.items.createItem(item_type, item_subtype, mat_type, mat_index, unit)`` diff --git a/library/include/modules/Items.h b/library/include/modules/Items.h index c473654e3..7541660f4 100644 --- a/library/include/modules/Items.h +++ b/library/include/modules/Items.h @@ -204,7 +204,7 @@ DFHACK_EXPORT bool canTradeWithContents(df::item *item); /// marks the given item for trade at the given depot DFHACK_EXPORT bool markForTrade(df::item *item, df::building_tradedepotst *depot); /// Returns true if an active caravan will pay extra for the given item -DFHACK_EXPORT bool isRequestedTradeGood(df::item *item); +DFHACK_EXPORT bool isRequestedTradeGood(df::item *item, df::caravan_state *caravan = NULL); /// Checks whether the item is an assigned hauling vehicle DFHACK_EXPORT bool isRouteVehicle(df::item *item); diff --git a/library/modules/Items.cpp b/library/modules/Items.cpp index 06741894d..3fc08813c 100644 --- a/library/modules/Items.cpp +++ b/library/modules/Items.cpp @@ -1917,7 +1917,19 @@ static int32_t get_trade_agreement_multiplier(df::item *item, const df::caravan_ : get_sell_request_multiplier(item, caravan); } -bool Items::isRequestedTradeGood(df::item *item) { +static bool is_requested_trade_good(df::item *item, df::caravan_state *caravan) { + auto trade_state = caravan->trade_state; + if (caravan->time_remaining <= 0 || + (trade_state != df::caravan_state::T_trade_state::Approaching && + trade_state != df::caravan_state::T_trade_state::AtDepot)) + return false; + return get_buy_request_multiplier(item, caravan->buy_prices) > DEFAULT_AGREEMENT_MULTIPLIER; +} + +bool Items::isRequestedTradeGood(df::item *item, df::caravan_state *caravan) { + if (caravan) + return is_requested_trade_good(item, caravan); + for (auto caravan : df::global::plotinfo->caravans) { auto trade_state = caravan->trade_state; if (caravan->time_remaining <= 0 ||