From 833da7d329af22540af1649b598291d6520f6b7d Mon Sep 17 00:00:00 2001 From: Myk Taylor Date: Sun, 27 Apr 2025 11:37:58 -0700 Subject: [PATCH] make english name in readable names optional --- docs/dev/Lua API.rst | 23 ++++++++++---------- library/LuaApi.cpp | 5 +++-- library/include/modules/Units.h | 4 ++-- library/modules/Units.cpp | 37 ++++++++++++++++++++------------- 4 files changed, 39 insertions(+), 30 deletions(-) diff --git a/docs/dev/Lua API.rst b/docs/dev/Lua API.rst index bf7ee35eff..e810a18440 100644 --- a/docs/dev/Lua API.rst +++ b/docs/dev/Lua API.rst @@ -1773,17 +1773,18 @@ Units module Get human-readable baby or child name (e.g., "dwarven baby" or "dwarven child"). -* ``dfhack.units.getReadableName(unit or historical_figure)`` - - Returns a string that includes the native and english language name of the - unit (if any), the race of the unit (if different from fort), whether it is - trained for war or hunting, any syndrome-given descriptions (such as - "necromancer"), the training level (if tame), and profession or noble role. - If a ``historical_figure`` is passed instead of a unit, some information - (e.g., agitation status) is not available, and the profession may be - different (e.g., "Monk") from what is displayed in fort mode. - -* ``dfhack.units.getAge(unit[,true_age])`` +* ``dfhack.units.getReadableName(unit or historical_figure[, skip_english])`` + + Returns a string that includes the native and english language name (if + ``skip_english`` is not ``true``) of the unit (if any), the race of the unit + (if different from fort), whether it is trained for war or hunting, any + syndrome-given descriptions (such as "necromancer"), the training level (if + tame), and profession or noble role. If a ``historical_figure`` is passed + instead of a unit, some information (e.g., agitation status) is not + available, and the profession may be different (e.g., "Monk") from what is + displayed in fort mode. + +* ``dfhack.units.getAge(unit[, true_age])`` Returns the age of the unit in years as a floating-point value. If ``true_age`` is true, ignores false identities. diff --git a/library/LuaApi.cpp b/library/LuaApi.cpp index 04b7741828..228399c84f 100644 --- a/library/LuaApi.cpp +++ b/library/LuaApi.cpp @@ -2291,10 +2291,11 @@ static int units_assignTrainer(lua_State *L) { } static int units_getReadablename(lua_State *L) { + bool skip_english = lua_toboolean(L, 2); // defaults to false if (auto unit = Lua::GetDFObject(L, 1)) - Lua::Push(L, Units::getReadableName(unit)); + Lua::Push(L, Units::getReadableName(unit, skip_english)); else if (auto hf = Lua::GetDFObject(L, 1)) - Lua::Push(L, Units::getReadableName(hf)); + Lua::Push(L, Units::getReadableName(hf, skip_english)); else luaL_argerror(L, 1, "Expected a unit or a historical figure"); return 1; diff --git a/library/include/modules/Units.h b/library/include/modules/Units.h index 86238308d7..0cba251644 100644 --- a/library/include/modules/Units.h +++ b/library/include/modules/Units.h @@ -281,9 +281,9 @@ DFHACK_EXPORT std::string getRaceBabyName(df::unit *unit, bool plural = false); DFHACK_EXPORT std::string getRaceChildNameById(int32_t race_id, bool plural = false); DFHACK_EXPORT std::string getRaceChildName(df::unit *unit, bool plural = false); // Full readable name including profession. HF profession might be different from unit profession. -DFHACK_EXPORT std::string getReadableName(df::historical_figure *hf); +DFHACK_EXPORT std::string getReadableName(df::historical_figure *hf, bool skip_english = false); // Full readable name including profession, curse name, and tame description. -DFHACK_EXPORT std::string getReadableName(df::unit *unit); +DFHACK_EXPORT std::string getReadableName(df::unit *unit, bool skip_english = false); // Unit's age (in non-integer years). Ignore false identities if true_age. DFHACK_EXPORT double getAge(df::unit *unit, bool true_age = false); diff --git a/library/modules/Units.cpp b/library/modules/Units.cpp index 4ccce42277..ce135b477d 100644 --- a/library/modules/Units.cpp +++ b/library/modules/Units.cpp @@ -1153,7 +1153,25 @@ static string getTameTag(df::unit *unit) { } } -string Units::getReadableName(df::historical_figure *hf) { +template +static string formatReadableName(T *unit_or_hf, const string &prof_name, bool skip_english) { + auto visible_name = Units::getVisibleName(unit_or_hf); + string native_name = Translation::translateName(visible_name); + + if (native_name.empty()) + return prof_name; + + if (skip_english) + return native_name + ", " + prof_name; + + string english_name = Translation::translateName(visible_name, true, true); + if (english_name.empty()) + return native_name + ", " + prof_name; + + return native_name + " \"" + english_name + "\", " + prof_name; +} + +string Units::getReadableName(df::historical_figure *hf, bool skip_english) { CHECK_NULL_POINTER(hf); string prof_name = getProfessionName(hf, false, false, true); @@ -1167,11 +1185,10 @@ string Units::getReadableName(df::historical_figure *hf) { prof_name = "Ghostly " + prof_name; } - string name = Translation::translateName(getVisibleName(hf)); - return name.empty() ? prof_name : name + ", " + prof_name; + return formatReadableName(hf, prof_name, skip_english); } -string Units::getReadableName(df::unit *unit) { +string Units::getReadableName(df::unit *unit, bool skip_english) { CHECK_NULL_POINTER(unit); string prof_name = getProfessionName(unit, false, false, true); @@ -1190,17 +1207,7 @@ string Units::getReadableName(df::unit *unit) { if (isTame(unit)) prof_name += " (" + getTameTag(unit) + ")"; - auto visible_name = getVisibleName(unit); - string native_name = Translation::translateName(visible_name); - - if (native_name.empty()) - return prof_name; - - string english_name = Translation::translateName(visible_name, true, true); - if (english_name.empty()) - return native_name + ", " + prof_name; - - return native_name + " \"" + english_name + "\", " + prof_name; + return formatReadableName(unit, prof_name, skip_english); } double Units::getAge(df::unit *unit, bool true_age) {