diff --git a/docs/dev/Lua API.rst b/docs/dev/Lua API.rst index d07cb045e..33e27be2d 100644 --- a/docs/dev/Lua API.rst +++ b/docs/dev/Lua API.rst @@ -1775,6 +1775,10 @@ Items module Checks whether the item and all items it contains, if any, can be traded. +* ``dfhack.items.markForTrade(item, depot)`` + + Marks the given item for trade at the given depot. + * ``dfhack.items.isRouteVehicle(item)`` Checks whether the item is an assigned hauling vehicle. diff --git a/library/LuaApi.cpp b/library/LuaApi.cpp index d2c32cf63..5d9411434 100644 --- a/library/LuaApi.cpp +++ b/library/LuaApi.cpp @@ -2014,6 +2014,7 @@ static const LuaWrapper::FunctionReg dfhack_items_module[] = { WRAPM(Items, checkMandates), WRAPM(Items, canTrade), WRAPM(Items, canTradeWithContents), + WRAPM(Items, markForTrade), WRAPM(Items, isRouteVehicle), WRAPM(Items, isSquadEquipment), WRAPN(moveToGround, items_moveToGround), diff --git a/library/include/modules/Items.h b/library/include/modules/Items.h index 7f2c9ce69..08737fb2b 100644 --- a/library/include/modules/Items.h +++ b/library/include/modules/Items.h @@ -33,6 +33,7 @@ distribution. #include "MemAccess.h" #include "DataDefs.h" +#include "df/building_tradedepotst.h" #include "df/item.h" #include "df/item_type.h" #include "df/general_ref.h" @@ -199,6 +200,8 @@ DFHACK_EXPORT bool checkMandates(df::item *item); DFHACK_EXPORT bool canTrade(df::item *item); /// Checks whether the item and all items it contains, if any, can be traded 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); /// 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 03d6cda4a..f98c7c46b 100644 --- a/library/modules/Items.cpp +++ b/library/modules/Items.cpp @@ -39,6 +39,7 @@ distribution. using namespace std; #include "ModuleFactory.h" +#include "modules/Job.h" #include "modules/MapCache.h" #include "modules/Materials.h" #include "modules/Items.h" @@ -48,6 +49,7 @@ using namespace std; #include "df/body_part_template_flags.h" #include "df/building.h" #include "df/building_actual.h" +#include "df/building_tradedepotst.h" #include "df/caste_raw.h" #include "df/creature_raw.h" #include "df/general_ref.h" @@ -1637,6 +1639,42 @@ bool Items::canTradeWithContents(df::item *item) return true; } +bool Items::markForTrade(df::item *item, df::building_tradedepotst *depot) { + CHECK_NULL_POINTER(item); + CHECK_NULL_POINTER(depot); + + // validate that the depot is in a good state + if (depot->getBuildStage() < depot->getMaxBuildStage()) + return false; + if (depot->jobs.size() && depot->jobs[0]->job_type == df::job_type::DestroyBuilding) + return false; + + auto href = df::allocate(); + if (!href) + return false; + + auto job = new df::job(); + job->job_type = df::job_type::BringItemToDepot; + job->pos = df::coord(depot->centerx, depot->centery, depot->z); + + // job <-> item link + if (!Job::attachJobItem(job, item, df::job_item_ref::Hauled)) { + delete job; + delete href; + return false; + } + + // job <-> building link + href->building_id = depot->id; + depot->jobs.push_back(job); + job->general_refs.push_back(href); + + // add to job list + Job::linkIntoWorld(job); + + return true; +} + bool Items::isRouteVehicle(df::item *item) { CHECK_NULL_POINTER(item); diff --git a/plugins/logistics.cpp b/plugins/logistics.cpp index d62ee8c36..20b2c343f 100644 --- a/plugins/logistics.cpp +++ b/plugins/logistics.cpp @@ -323,31 +323,7 @@ public: bool designate(color_ostream& out, df::item* item) override { if (!depot) return false; - - auto href = df::allocate(); - if (!href) - return false; - - auto job = new df::job(); - job->job_type = df::job_type::BringItemToDepot; - job->pos = df::coord(depot->centerx, depot->centery, depot->z); - - // job <-> item link - if (!Job::attachJobItem(job, item, df::job_item_ref::Hauled)) { - delete job; - delete href; - return false; - } - - // job <-> building link - href->building_id = depot->id; - depot->jobs.push_back(job); - job->general_refs.push_back(href); - - // add to job list - Job::linkIntoWorld(job); - - return true; + return Items::markForTrade(item, depot); } private: