Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions docs/dev/Lua API.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 3 additions & 2 deletions library/LuaApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<df::unit>(L, 1))
Lua::Push(L, Units::getReadableName(unit));
Lua::Push(L, Units::getReadableName(unit, skip_english));
else if (auto hf = Lua::GetDFObject<df::historical_figure>(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;
Expand Down
4 changes: 2 additions & 2 deletions library/include/modules/Units.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
37 changes: 22 additions & 15 deletions library/modules/Units.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1153,7 +1153,25 @@ static string getTameTag(df::unit *unit) {
}
}

string Units::getReadableName(df::historical_figure *hf) {
template<typename T>
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);

Expand All @@ -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);

Expand All @@ -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) {
Expand Down