diff --git a/docs/changelog.txt b/docs/changelog.txt index 4a8a100df..e6a9dd36d 100644 --- a/docs/changelog.txt +++ b/docs/changelog.txt @@ -50,8 +50,9 @@ changelog.txt uses a syntax similar to RST, with a few special sequences: - Price calculations fixed for many item types ## Lua -- ``dfhack.items.markForTrade``: new API for marking items for trade - ``dfhack.units.getUnitByNobleRole``, ``dfhack.units.getUnitsByNobleRole``: unit lookup API by role +- ``dfhack.items.markForTrade``: mark items for trade +- ``dfhack.items.isRequestedTradeGood``: discover whether an item is named in a trade agreement with an active caravan - ``dfhack.items.getValue``: gained optional ``caravan`` and ``caravan_buying`` parameters for prices that take trader races and agreements into account ## Removed diff --git a/docs/dev/Lua API.rst b/docs/dev/Lua API.rst index e81a34de3..8553d4e1a 100644 --- a/docs/dev/Lua API.rst +++ b/docs/dev/Lua API.rst @@ -1778,6 +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)`` + + Returns whether any active caravan will pay extra for the given item. + * ``dfhack.items.createItem(item_type, item_subtype, mat_type, mat_index, unit)`` Creates an item, similar to the `createitem` plugin. diff --git a/library/LuaApi.cpp b/library/LuaApi.cpp index 7fb14baeb..7afb3b9ea 100644 --- a/library/LuaApi.cpp +++ b/library/LuaApi.cpp @@ -2020,6 +2020,7 @@ static const LuaWrapper::FunctionReg dfhack_items_module[] = { WRAPM(Items, getSubtypeDef), WRAPM(Items, getItemBaseValue), WRAPM(Items, getValue), + WRAPM(Items, isRequestedTradeGood), WRAPM(Items, createItem), WRAPM(Items, checkMandates), WRAPM(Items, canTrade), diff --git a/library/include/modules/Items.h b/library/include/modules/Items.h index 6cce5d2f1..c473654e3 100644 --- a/library/include/modules/Items.h +++ b/library/include/modules/Items.h @@ -203,6 +203,8 @@ DFHACK_EXPORT bool canTrade(df::item *item); 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); /// 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 537be7c3a..06741894d 100644 --- a/library/modules/Items.cpp +++ b/library/modules/Items.cpp @@ -1917,6 +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) { + for (auto caravan : df::global::plotinfo->caravans) { + 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)) + continue; + if (get_buy_request_multiplier(item, caravan->buy_prices) > DEFAULT_AGREEMENT_MULTIPLIER) + return true; + } + return false; +} + int Items::getValue(df::item *item, df::caravan_state *caravan, bool caravan_buying) { CHECK_NULL_POINTER(item);