diff --git a/docs/Lua API.rst b/docs/Lua API.rst index be935c822..7eacd7b96 100644 --- a/docs/Lua API.rst +++ b/docs/Lua API.rst @@ -1306,6 +1306,14 @@ Items module Checks whether the item and all items it contains, if any, can be traded. +* ``dfhack.items.isRouteVehicle(item)`` + + Checks whether the item is an assigned hauling vehicle. + +* ``dfhack.items.isSquadEquipment(item)`` + + Checks whether the item is assigned to a squad. + Maps module ----------- diff --git a/docs/changelog.txt b/docs/changelog.txt index b5fa62a9f..0fefa4b8c 100644 --- a/docs/changelog.txt +++ b/docs/changelog.txt @@ -42,6 +42,8 @@ changelog.txt uses a syntax similar to RST, with a few special sequences: - ``Items::checkMandates()`` - ``Items::canTrade()`` - ``Items::canTradeWithContents()`` + - ``Items::isRouteVehicle()`` + - ``Items::isSquadEquipment()`` # 0.44.10-beta1 diff --git a/library/LuaApi.cpp b/library/LuaApi.cpp index 9e5ddd1ea..af71e729b 100644 --- a/library/LuaApi.cpp +++ b/library/LuaApi.cpp @@ -1758,6 +1758,8 @@ static const LuaWrapper::FunctionReg dfhack_items_module[] = { WRAPM(Items, checkMandates), WRAPM(Items, canTrade), WRAPM(Items, canTradeWithContents), + WRAPM(Items, isRouteVehicle), + WRAPM(Items, isSquadEquipment), WRAPN(moveToGround, items_moveToGround), WRAPN(moveToContainer, items_moveToContainer), WRAPN(moveToInventory, items_moveToInventory), diff --git a/library/include/modules/Items.h b/library/include/modules/Items.h index a09900ddf..776e935eb 100644 --- a/library/include/modules/Items.h +++ b/library/include/modules/Items.h @@ -190,6 +190,11 @@ 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); +/// Checks whether the item is an assigned hauling vehicle +DFHACK_EXPORT bool isRouteVehicle(df::item *item); +/// Checks whether the item is assigned to a squad +DFHACK_EXPORT bool isSquadEquipment(df::item *item); + } } diff --git a/library/modules/Items.cpp b/library/modules/Items.cpp index 5c08065f9..a1b4ace4d 100644 --- a/library/modules/Items.cpp +++ b/library/modules/Items.cpp @@ -85,6 +85,7 @@ using namespace std; #include "df/ui.h" #include "df/unit.h" #include "df/unit_inventory_item.h" +#include "df/vehicle.h" #include "df/vermin.h" #include "df/viewscreen_itemst.h" #include "df/world.h" @@ -1485,3 +1486,23 @@ bool Items::canTradeWithContents(df::item *item) return true; } + +bool Items::isRouteVehicle(df::item *item) +{ + CHECK_NULL_POINTER(item); + int id = item->getVehicleID(); + if (id < 0) return false; + + auto vehicle = df::vehicle::find(id); + return vehicle && vehicle->route_id >= 0; +} + +bool Items::isSquadEquipment(df::item *item) +{ + CHECK_NULL_POINTER(item); + if (!ui) + return false; + + auto &vec = ui->equipment.items_assigned[item->getType()]; + return binsearch_index(vec, &df::item::id, item->id) >= 0; +} diff --git a/plugins/workflow.cpp b/plugins/workflow.cpp index bfc4b639f..8396a974e 100644 --- a/plugins/workflow.cpp +++ b/plugins/workflow.cpp @@ -40,7 +40,6 @@ #include "df/plant_raw.h" #include "df/inorganic_raw.h" #include "df/builtin_mats.h" -#include "df/vehicle.h" using std::vector; using std::string; @@ -1159,21 +1158,6 @@ static bool itemInRealJob(df::item *item) != job_type_class::Hauling; } -static bool isRouteVehicle(df::item *item) -{ - int id = item->getVehicleID(); - if (id < 0) return false; - - auto vehicle = df::vehicle::find(id); - return vehicle && vehicle->route_id >= 0; -} - -static bool isAssignedSquad(df::item *item) -{ - auto &vec = ui->equipment.items_assigned[item->getType()]; - return binsearch_index(vec, &df::item::id, item->id) >= 0; -} - static void map_job_items(color_ostream &out) { for (size_t i = 0; i < constraints.size(); i++) @@ -1288,10 +1272,10 @@ static void map_job_items(color_ostream &out) item->flags.bits.owned || item->flags.bits.in_chest || item->isAssignedToStockpile() || - isRouteVehicle(item) || + Items::isRouteVehicle(item) || itemInRealJob(item) || itemBusy(item) || - isAssignedSquad(item)) + Items::isSquadEquipment(item)) { is_invalid = true; cv->item_inuse_count++;