From 6819ee9928006f90cefa238a1d2bb85e6cfb44bd Mon Sep 17 00:00:00 2001 From: myk002 Date: Fri, 5 Feb 2021 16:45:39 -0800 Subject: [PATCH] make alchemist flag valid for controllable civs --- docs/Lua API.rst | 9 +++++++++ docs/changelog.txt | 3 +++ library/LuaApi.cpp | 1 + library/include/modules/Units.h | 1 + library/modules/Units.cpp | 22 ++++++++++++++++++++++ plugins/manipulator.cpp | 27 +++++++++++++++++++++++++++ 6 files changed, 63 insertions(+) diff --git a/docs/Lua API.rst b/docs/Lua API.rst index 81c662728..da5ac206b 100644 --- a/docs/Lua API.rst +++ b/docs/Lua API.rst @@ -1270,6 +1270,15 @@ Units module Returns the age of the unit in years as a floating-point value. If ``true_age`` is true, ignores false identities. +* ``dfhack.units.isValidLabor(unit, unit_labor)`` + + Returns whether the indicated labor is settable for the given unit. + +* ``dfhack.units.setLaborValidity(unit_labor)`` + + Sets the given labor as valid for all playable units in the game (that is, for + all civilizations whose members can be residents of your fort). + * ``dfhack.units.getNominalSkill(unit, skill[, use_rust])`` Retrieves the nominal skill level for the given unit. If ``use_rust`` diff --git a/docs/changelog.txt b/docs/changelog.txt index 989a9c558..3f36239ec 100644 --- a/docs/changelog.txt +++ b/docs/changelog.txt @@ -33,6 +33,9 @@ changelog.txt uses a syntax similar to RST, with a few special sequences: # Future +## Misc Improvements +- `autohauler`: allow the ``Alchemist`` labor to be enabled in `manipulator` and other labor screens so it can be used for its intended purpose of flagging that no hauling labors should be assigned to a dwarf. Before, the only way to set the flag was to use an external program like Dwarf Therapist. + # 0.47.04-r5 ## Fixes diff --git a/library/LuaApi.cpp b/library/LuaApi.cpp index d7f3b269f..1c115471a 100644 --- a/library/LuaApi.cpp +++ b/library/LuaApi.cpp @@ -1600,6 +1600,7 @@ static const LuaWrapper::FunctionReg dfhack_units_module[] = { WRAPM(Units, getEffectiveSkill), WRAPM(Units, getExperience), WRAPM(Units, isValidLabor), + WRAPM(Units, setLaborValidity), WRAPM(Units, computeMovementSpeed), WRAPM(Units, computeSlowdownFactor), WRAPM(Units, getProfessionName), diff --git a/library/include/modules/Units.h b/library/include/modules/Units.h index e2d34254d..4a8452203 100644 --- a/library/include/modules/Units.h +++ b/library/include/modules/Units.h @@ -163,6 +163,7 @@ DFHACK_EXPORT int getEffectiveSkill(df::unit *unit, df::job_skill skill_id); DFHACK_EXPORT int getExperience(df::unit *unit, df::job_skill skill_id, bool total = false); DFHACK_EXPORT bool isValidLabor(df::unit *unit, df::unit_labor labor); +DFHACK_EXPORT bool setLaborValidity(df::unit_labor labor, bool isValid); DFHACK_EXPORT int computeMovementSpeed(df::unit *unit); DFHACK_EXPORT float computeSlowdownFactor(df::unit *unit); diff --git a/library/modules/Units.cpp b/library/modules/Units.cpp index a8f96b29f..6ca2e852a 100644 --- a/library/modules/Units.cpp +++ b/library/modules/Units.cpp @@ -893,6 +893,28 @@ bool Units::isValidLabor(df::unit *unit, df::unit_labor labor) return true; } +bool Units::setLaborValidity(df::unit_labor labor, bool isValid) +{ + if (!is_valid_enum_item(labor)) + return false; + if (labor == df::unit_labor::NONE) + return false; + for (size_t i = 0; i < world->raws.entities.size(); ++i) + { + if (!world->raws.entities[i] || + world->raws.entities[i]->flags.is_set(df::entity_raw_flags::LAYER_LINKED) || // Animal people + world->raws.entities[i]->flags.is_set(df::entity_raw_flags::GENERATED) || // Vault guardians + !world->raws.entities[i]->flags.is_set(df::entity_raw_flags::CIV_CONTROLLABLE) || // non-controllable civs + world->raws.entities[i]->translation == "") // Kobolds + { + continue; + } + + world->raws.entities[i]->jobs.permitted_labor[labor] = isValid; + } + return true; +} + inline void adjust_speed_rating(int &rating, bool is_adventure, int value, int dwarf100, int dwarf200, int adv50, int adv75, int adv100, int adv200) { if (is_adventure) diff --git a/plugins/manipulator.cpp b/plugins/manipulator.cpp index b333afae5..ad067276e 100644 --- a/plugins/manipulator.cpp +++ b/plugins/manipulator.cpp @@ -2279,6 +2279,16 @@ struct unitlist_hook : df::viewscreen_unitlistst IMPLEMENT_VMETHOD_INTERPOSE(unitlist_hook, feed); IMPLEMENT_VMETHOD_INTERPOSE(unitlist_hook, render); +static void enable_alchemist(color_ostream &out) +{ + if (!Units::setLaborValidity(unit_labor::ALCHEMIST, true)) + { + // informational only; this is a non-fatal error + out.printerr("manipulator: Could not flag Alchemist as a valid skill; Alchemist will not" + " be settable from DF or DFHack labor management screens.\n"); + } +} + DFhackCExport command_result plugin_enable(color_ostream &out, bool enable) { if (!gps) @@ -2291,6 +2301,9 @@ DFhackCExport command_result plugin_enable(color_ostream &out, bool enable) return CR_FAILURE; is_enabled = enable; + + if (is_enabled) + enable_alchemist(out); } return CR_OK; @@ -2306,6 +2319,20 @@ DFhackCExport command_result plugin_init ( color_ostream &out, vector