diff --git a/docs/dev/Lua API.rst b/docs/dev/Lua API.rst index 11bac3f61..6862f1d1e 100644 --- a/docs/dev/Lua API.rst +++ b/docs/dev/Lua API.rst @@ -1797,7 +1797,12 @@ Items module * ``dfhack.items.canTradeWithContents(item)`` - Checks whether the item and all items it contains, if any, can be traded. + Returns false if the item or any contained items cannot be traded. + +* ``canTradeAnyWithContents(item)`` + + Returns true if the item is empty and can be traded or if the item contains + any item that can be traded. * ``dfhack.items.markForTrade(item, depot)`` diff --git a/library/LuaApi.cpp b/library/LuaApi.cpp index 7afb3b9ea..55d265baf 100644 --- a/library/LuaApi.cpp +++ b/library/LuaApi.cpp @@ -2025,6 +2025,7 @@ static const LuaWrapper::FunctionReg dfhack_items_module[] = { WRAPM(Items, checkMandates), WRAPM(Items, canTrade), WRAPM(Items, canTradeWithContents), + WRAPM(Items, canTradeAnyWithContents), WRAPM(Items, markForTrade), WRAPM(Items, isRouteVehicle), WRAPM(Items, isSquadEquipment), diff --git a/library/include/modules/Items.h b/library/include/modules/Items.h index 7541660f4..beccd669a 100644 --- a/library/include/modules/Items.h +++ b/library/include/modules/Items.h @@ -199,8 +199,10 @@ DFHACK_EXPORT int32_t createItem(df::item_type type, int16_t item_subtype, int16 DFHACK_EXPORT bool checkMandates(df::item *item); /// Checks whether the item can be traded DFHACK_EXPORT bool canTrade(df::item *item); -/// Checks whether the item and all items it contains, if any, can be traded +/// Returns false if the item or any contained items cannot be traded DFHACK_EXPORT bool canTradeWithContents(df::item *item); +/// Returns true if the item is empty and can be traded or if the item contains any item that can be traded +DFHACK_EXPORT bool canTradeAnyWithContents(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 diff --git a/library/modules/Items.cpp b/library/modules/Items.cpp index 3fc08813c..3694e0203 100644 --- a/library/modules/Items.cpp +++ b/library/modules/Items.cpp @@ -2172,6 +2172,27 @@ bool Items::canTradeWithContents(df::item *item) return true; } +bool Items::canTradeAnyWithContents(df::item *item) +{ + CHECK_NULL_POINTER(item); + + if (item->flags.bits.in_inventory) + return false; + + vector contained_items; + getContainedItems(item, &contained_items); + + if (contained_items.empty()) + return canTrade(item); + + for (df::item *cit : contained_items) { + if (canTrade(cit)) + return true; + } + + return false; +} + bool Items::markForTrade(df::item *item, df::building_tradedepotst *depot) { CHECK_NULL_POINTER(item); CHECK_NULL_POINTER(depot); diff --git a/plugins/logistics.cpp b/plugins/logistics.cpp index a7da0e553..643fb4941 100644 --- a/plugins/logistics.cpp +++ b/plugins/logistics.cpp @@ -317,7 +317,7 @@ public: } bool can_designate(color_ostream& out, df::item* item) override { - return Items::canTradeWithContents(item); + return Items::canTradeAnyWithContents(item); } bool designate(color_ostream& out, df::item* item) override {