From 2401be1b3b4ed423542e232deeafa8406ffc79b8 Mon Sep 17 00:00:00 2001
From: Alexander Gavrilov
dfhack.job.printItemDetails(jobitem,idx)
Prints info about the job item.
dfhack.job.getGeneralRef(job, type)
+Searches for a general_ref with the given type.
+dfhack.job.getSpecificRef(job, type)
+Searches for a specific_ref with the given type.
+dfhack.job.getHolder(job)
Returns the building holding the job.
dfhack.units.getPosition(unit)
Returns true x,y,z of the unit, or nil if invalid; may be not equal to unit.pos if caged.
dfhack.units.getGeneralRef(unit, type)
+Searches for a general_ref with the given type.
+dfhack.units.getSpecificRef(unit, type)
+Searches for a specific_ref with the given type.
+dfhack.units.getContainer(unit)
Returns the container (cage) item or nil.
dfhack.units.getEffectiveSkill(unit, skill)
Computes the effective rating for the given skill, taking into account exhaustion, pain etc.
dfhack.units.getExperience(unit, skill[, total])
+Returns the experience value for the given skill. If total is true, adds experience implied by the current rating.
+dfhack.units.computeMovementSpeed(unit)
Computes number of frames * 100 it takes the unit to move in its current state of mind and body.
dfhack.buildings.getGeneralRef(building, type)
+Searches for a general_ref with the given type.
+dfhack.buildings.getSpecificRef(building, type)
+Searches for a specific_ref with the given type.
+dfhack.buildings.setOwner(item,unit)
Replaces the owner of the building. If unit is nil, removes ownership. Returns false in case of error.
diff --git a/Lua API.rst b/Lua API.rst index 126bc7f9d..4087ff0aa 100644 --- a/Lua API.rst +++ b/Lua API.rst @@ -970,6 +970,10 @@ Units module Computes the effective rating for the given skill, taking into account exhaustion, pain etc. +* ``dfhack.units.getExperience(unit, skill[, total])`` + + Returns the experience value for the given skill. If ``total`` is true, adds experience implied by the current rating. + * ``dfhack.units.computeMovementSpeed(unit)`` Computes number of frames * 100 it takes the unit to move in its current state of mind and body. diff --git a/library/LuaApi.cpp b/library/LuaApi.cpp index b186316a8..a4ebbea5b 100644 --- a/library/LuaApi.cpp +++ b/library/LuaApi.cpp @@ -1180,6 +1180,7 @@ static const LuaWrapper::FunctionReg dfhack_units_module[] = { WRAPM(Units, getAge), WRAPM(Units, getNominalSkill), WRAPM(Units, getEffectiveSkill), + WRAPM(Units, getExperience), WRAPM(Units, computeMovementSpeed), WRAPM(Units, getProfessionName), WRAPM(Units, getCasteProfessionName), diff --git a/library/include/modules/Units.h b/library/include/modules/Units.h index 93e11afb1..c2eb7ca18 100644 --- a/library/include/modules/Units.h +++ b/library/include/modules/Units.h @@ -238,6 +238,8 @@ DFHACK_EXPORT double getAge(df::unit *unit, bool true_age = false); DFHACK_EXPORT int getNominalSkill(df::unit *unit, df::job_skill skill_id, bool use_rust = false); 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 int computeMovementSpeed(df::unit *unit); struct NoblePosition { diff --git a/library/modules/Units.cpp b/library/modules/Units.cpp index ddbb8cb79..b19399c6c 100644 --- a/library/modules/Units.cpp +++ b/library/modules/Units.cpp @@ -909,6 +909,24 @@ int Units::getNominalSkill(df::unit *unit, df::job_skill skill_id, bool use_rust return 0; } +int Units::getExperience(df::unit *unit, df::job_skill skill_id, bool total) +{ + CHECK_NULL_POINTER(unit); + + if (!unit->status.current_soul) + return 0; + + auto skill = binsearch_in_vector(unit->status.current_soul->skills, &df::unit_skill::id, skill_id); + if (!skill) + return 0; + + int xp = skill->experience; + // exact formula used by the game: + if (total && skill->rating > 0) + xp += 500*skill->rating + 100*skill->rating*(skill->rating - 1)/2; + return xp; +} + int Units::getEffectiveSkill(df::unit *unit, df::job_skill skill_id) { /*