Skip to content

Commit

Permalink
Only use native functions to obtain skill/experience
Browse files Browse the repository at this point in the history
The former cache was actually slightly slower than calling the
corresponding functions directly
  • Loading branch information
lethosor committed May 16, 2015
1 parent 79d6bc1 commit 5619a38
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 19 deletions.
9 changes: 4 additions & 5 deletions manipulator/gui/manipulator.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ function manipulator:init(args)
self.grid_idx = 1
self.grid_rows = {}
self.diff_enabled = storage.diff_enabled
skill_cache:clear()
p_start('init units')
for idx, u in pairs(self.units) do
self.grid_rows[u] = penarray.new(#SKILL_COLUMNS, 1)
Expand Down Expand Up @@ -184,12 +183,12 @@ function manipulator:onRenderBody(p)
end
p:string(unit.status.labors[col.labor] and 'Enabled' or 'Not Enabled', {fg = COLOR_LIGHTBLUE})
else
local skill = skill_cache:get(unit, col.skill)
local lvl = skill.rating
local lvl = skills.rating(unit, col.skill)
local prof = df.job_skill.attrs[col.skill].caption_noun
p:string((lvl > 0 and SKILL_LEVELS[lvl].name or 'Not') .. ' ' .. prof, {fg = COLOR_LIGHTBLUE})
if lvl < #SKILL_LEVELS then
p:string(' '):string(('(%i/%i)'):format(skill.experience, SKILL_LEVELS[lvl > 0 and lvl or 1].points), {fg = COLOR_LIGHTBLUE})
p:string(' '):string(('(%i/%i)'):format(skills.experience(unit, col.skill),
SKILL_LEVELS[lvl > 0 and lvl or 1].points), {fg = COLOR_LIGHTBLUE})
end
end
p:newline()
Expand Down Expand Up @@ -227,7 +226,7 @@ function manipulator:update_grid_tile(x, y)
local skill = SKILL_COLUMNS[x].skill
local labor = SKILL_COLUMNS[x].labor
if skill ~= df.job_skill.NONE then
local level = skill_cache:get(unit, skill).rating
local level = skills.rating(unit, skill)
c = level > 0 and SKILL_LEVELS[level].abbr or '-'
end
if labor ~= df.unit_labor.NONE then
Expand Down
30 changes: 16 additions & 14 deletions manipulator/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,20 @@ if dfhack.units.getKillCount == nil then
end
end

skills = {}
function skills.experience(unit, skill)
return dfhack.units.getExperience(unit._native, skill)
end

function skills.rating(unit, skill)
local rating = dfhack.units.getNominalSkill(unit._native, skill) + 1
local exp = skills.experience(unit, skill)
if exp == 0 and rating == 1 then
return 0
end
return rating
end

OutputString = dfhack.screen.paintString

function OutputKeyString(pen, x, y, key, str)
Expand Down Expand Up @@ -199,23 +213,11 @@ function UnitAttrCache:clear()
self.cache = {}
end

skill_cache = UnitAttrCache()
function skill_cache:lookup(unit, skill)
local ret = {
rating = dfhack.units.getNominalSkill(unit._native, skill) + 1,
experience = dfhack.units.getExperience(unit._native, skill)
}
if ret.experience == 0 and ret.rating == 1 then
ret.rating = 0
end
return ret
end

sort = {
skill = function(u1, u2, skill)
local level_diff = skill_cache:get(u2, skill).rating - skill_cache:get(u1, skill).rating
local level_diff = skills.rating(u2, skill) - skills.rating(u1, skill)
if level_diff ~= 0 then return level_diff end
return skill_cache:get(u2, skill).experience - skill_cache:get(u1, skill).experience
return skills.experience(u2, skill) - skills.experience(u1, skill)
end
}

Expand Down

0 comments on commit 5619a38

Please sign in to comment.