diff --git a/docs/changelog.txt b/docs/changelog.txt index d10407621..bf8182c73 100644 --- a/docs/changelog.txt +++ b/docs/changelog.txt @@ -46,6 +46,7 @@ changelog.txt uses a syntax similar to RST, with a few special sequences: ## Internals ## Lua +- ``dfhack.items.markForTrade``: new API for marking items for trade ## Removed 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/lua/gui/widgets.lua b/library/lua/gui/widgets.lua index a58ac228c..7a3201f28 100644 --- a/library/lua/gui/widgets.lua +++ b/library/lua/gui/widgets.lua @@ -1552,8 +1552,7 @@ function CycleHotkeyLabel:setOption(value_or_index, call_on_change) end end if not option_idx then - error(('cannot find option with value or index: "%s"') - :format(value_or_index)) + option_idx = 1 end local old_option_idx = self.option_idx self.option_idx = option_idx @@ -2392,9 +2391,15 @@ local function rangeslider_do_drag(self, width_per_idx) end end if new_left_idx and new_left_idx ~= self.get_left_idx_fn() then + if not new_right_idx and new_left_idx > self.get_right_idx_fn() then + self.on_right_change(new_left_idx) + end self.on_left_change(new_left_idx) end if new_right_idx and new_right_idx ~= self.get_right_idx_fn() then + if new_right_idx < self.get_left_idx_fn() then + self.on_left_change(new_right_idx) + end self.on_right_change(new_right_idx) end end diff --git a/library/modules/Gui.cpp b/library/modules/Gui.cpp index ee5eaa31c..dc6f47f59 100644 --- a/library/modules/Gui.cpp +++ b/library/modules/Gui.cpp @@ -216,6 +216,11 @@ DEFINE_GET_FOCUS_STRING_HANDLER(dwarfmode) newFocusString = baseFocus; newFocusString += "/ViewSheets"; newFocusString += "/" + enum_item_key(game->main_interface.view_sheets.active_sheet); + if (game->main_interface.view_sheets.active_sheet == df::view_sheet_type::BUILDING) { + auto bld = df::building::find(game->main_interface.view_sheets.viewing_bldid); + if (bld) + newFocusString += '/' + enum_item_key(bld->getType()); + } focusStrings.push_back(newFocusString); } @@ -328,6 +333,10 @@ DEFINE_GET_FOCUS_STRING_HANDLER(dwarfmode) if (game->main_interface.trade.open) { newFocusString = baseFocus; newFocusString += "/Trade"; + if (game->main_interface.trade.choosing_merchant) + newFocusString += "/ChoosingMerchant"; + else + newFocusString += "/Default"; focusStrings.push_back(newFocusString); } if (game->main_interface.job_details.open) { @@ -377,6 +386,7 @@ DEFINE_GET_FOCUS_STRING_HANDLER(dwarfmode) if (game->main_interface.unit_selector.open) { newFocusString = baseFocus; newFocusString += "/UnitSelector"; + newFocusString += '/' + enum_item_key(game->main_interface.unit_selector.context); focusStrings.push_back(newFocusString); } if (game->main_interface.announcement_alert.open) { 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: