Update gm-unit.lua

tabs to spaces.
develop
warmist 2015-07-26 12:54:23 +03:00
parent 0e939531ab
commit d9da55f6bd
1 changed files with 150 additions and 150 deletions

@ -10,186 +10,186 @@ local args={...}
local target local target
--TODO: add more ways to guess what unit you want to edit --TODO: add more ways to guess what unit you want to edit
if args[1]~= nil then if args[1]~= nil then
target=df.units.find(args[1]) target=df.units.find(args[1])
else else
target=dfhack.gui.getSelectedUnit(true) target=dfhack.gui.getSelectedUnit(true)
end end
if target==nil then if target==nil then
qerror("No unit to edit") --TODO: better error message qerror("No unit to edit") --TODO: better error message
end end
local editors={} local editors={}
function add_editor(editor_class) function add_editor(editor_class)
table.insert(editors,{text=editor_class.ATTRS.frame_title,on_submit=function ( unit ) table.insert(editors,{text=editor_class.ATTRS.frame_title,on_submit=function ( unit )
editor_class{target_unit=unit}:show() editor_class{target_unit=unit}:show()
end}) end})
end end
-------------------------------various subeditors--------- -------------------------------various subeditors---------
--TODO set local sould or better yet skills vector to reduce long skill list access typing --TODO set local sould or better yet skills vector to reduce long skill list access typing
editor_skills=defclass(editor_skills,gui.FramedScreen) editor_skills=defclass(editor_skills,gui.FramedScreen)
editor_skills.ATTRS={ editor_skills.ATTRS={
frame_style = gui.GREY_LINE_FRAME, frame_style = gui.GREY_LINE_FRAME,
frame_title = "Skill editor", frame_title = "Skill editor",
target_unit = DEFAULT_NIL, target_unit = DEFAULT_NIL,
learned_only= false, learned_only= false,
} }
function list_skills(unit,learned_only) function list_skills(unit,learned_only)
local s_=df.job_skill local s_=df.job_skill
local u_skills=unit.status.current_soul.skills local u_skills=unit.status.current_soul.skills
local ret={} local ret={}
for i,v in ipairs(s_) do for i,v in ipairs(s_) do
if i>0 then if i>0 then
local u_skill=utils.binsearch(u_skills,i,"id") local u_skill=utils.binsearch(u_skills,i,"id")
if u_skill or not learned_only then if u_skill or not learned_only then
if not u_skill then if not u_skill then
u_skill={rating=-1,experience=0} u_skill={rating=-1,experience=0}
end end
local rating local rating
if u_skill.rating >=0 then if u_skill.rating >=0 then
rating=df.skill_rating.attrs[u_skill.rating] rating=df.skill_rating.attrs[u_skill.rating]
else else
rating={caption="<unlearned>",xp_threshold=0} rating={caption="<unlearned>",xp_threshold=0}
end end
local text=string.format("%s: %s %d %d/%d",df.job_skill.attrs[i].caption,rating.caption,u_skill.rating,u_skill.experience,rating.xp_threshold) local text=string.format("%s: %s %d %d/%d",df.job_skill.attrs[i].caption,rating.caption,u_skill.rating,u_skill.experience,rating.xp_threshold)
table.insert(ret,{text=text,id=i}) table.insert(ret,{text=text,id=i})
end end
end end
end end
return ret return ret
end end
function editor_skills:update_list(no_save_place) function editor_skills:update_list(no_save_place)
local skill_list=list_skills(self.target_unit,self.learned_only) local skill_list=list_skills(self.target_unit,self.learned_only)
if no_save_place then if no_save_place then
self.subviews.skills:setChoices(skill_list) self.subviews.skills:setChoices(skill_list)
else else
self.subviews.skills:setChoices(skill_list,self.subviews.skills:getSelected()) self.subviews.skills:setChoices(skill_list,self.subviews.skills:getSelected())
end end
end end
function editor_skills:init( args ) function editor_skills:init( args )
if self.target_unit.status.current_soul==nil then if self.target_unit.status.current_soul==nil then
qerror("Unit does not have soul, can't edit skills") qerror("Unit does not have soul, can't edit skills")
end end
local skill_list=list_skills(self.target_unit,self.learned_only) local skill_list=list_skills(self.target_unit,self.learned_only)
self:addviews{ self:addviews{
widgets.FilteredList{ widgets.FilteredList{
choices=skill_list, choices=skill_list,
frame = {t=0, b=1,l=1}, frame = {t=0, b=1,l=1},
view_id="skills", view_id="skills",
}, },
widgets.Label{ widgets.Label{
frame = { b=0,l=1}, frame = { b=0,l=1},
text ={{text= ": exit editor ", text ={{text= ": exit editor ",
key = "LEAVESCREEN", key = "LEAVESCREEN",
on_activate= self:callback("dismiss") on_activate= self:callback("dismiss")
}, },
{text=": remove level ", {text=": remove level ",
key = "SECONDSCROLL_UP", key = "SECONDSCROLL_UP",
on_activate=self:callback("level_skill",-1)}, on_activate=self:callback("level_skill",-1)},
{text=": add level ", {text=": add level ",
key = "SECONDSCROLL_DOWN", key = "SECONDSCROLL_DOWN",
on_activate=self:callback("level_skill",1)} on_activate=self:callback("level_skill",1)}
, ,
{text=": show learned only ", {text=": show learned only ",
key = "CHANGETAB", key = "CHANGETAB",
on_activate=function () on_activate=function ()
self.learned_only=not self.learned_only self.learned_only=not self.learned_only
self:update_list(true) self:update_list(true)
end} end}
} }
}, },
} }
end end
function editor_skills:get_cur_skill() function editor_skills:get_cur_skill()
local list_wid=self.subviews.skills local list_wid=self.subviews.skills
local _,choice=list_wid:getSelected() local _,choice=list_wid:getSelected()
if choice==nil then if choice==nil then
qerror("Nothing selected") qerror("Nothing selected")
end end
local u_skill=utils.binsearch(self.target_unit.status.current_soul.skills,choice.id,"id") local u_skill=utils.binsearch(self.target_unit.status.current_soul.skills,choice.id,"id")
return choice,u_skill return choice,u_skill
end end
function editor_skills:level_skill(lvl) function editor_skills:level_skill(lvl)
local sk_en,sk=self:get_cur_skill() local sk_en,sk=self:get_cur_skill()
if lvl >0 then if lvl >0 then
local rating local rating
if sk then if sk then
rating=sk.rating+lvl rating=sk.rating+lvl
else else
rating=lvl-1 rating=lvl-1
end end
utils.insert_or_update(self.target_unit.status.current_soul.skills, {new=true, id=sk_en.id, rating=rating}, 'id') --TODO set exp? utils.insert_or_update(self.target_unit.status.current_soul.skills, {new=true, id=sk_en.id, rating=rating}, 'id') --TODO set exp?
elseif sk and sk.rating==0 and lvl<0 then elseif sk and sk.rating==0 and lvl<0 then
utils.erase_sorted_key(self.target_unit.status.current_soul.skills,sk_en.id,"id") utils.erase_sorted_key(self.target_unit.status.current_soul.skills,sk_en.id,"id")
elseif sk and lvl<0 then elseif sk and lvl<0 then
utils.insert_or_update(self.target_unit.status.current_soul.skills, {new=true, id=sk_en.id, rating=sk.rating+lvl}, 'id') --TODO set exp? utils.insert_or_update(self.target_unit.status.current_soul.skills, {new=true, id=sk_en.id, rating=sk.rating+lvl}, 'id') --TODO set exp?
end end
self:update_list() self:update_list()
end end
function editor_skills:remove_rust(skill) function editor_skills:remove_rust(skill)
--TODO --TODO
end end
add_editor(editor_skills) add_editor(editor_skills)
------- counters editor ------- counters editor
editor_counters=defclass(editor_counters,gui.FramedScreen) editor_counters=defclass(editor_counters,gui.FramedScreen)
editor_counters.ATTRS={ editor_counters.ATTRS={
frame_style = gui.GREY_LINE_FRAME, frame_style = gui.GREY_LINE_FRAME,
frame_title = "Counters editor", frame_title = "Counters editor",
target_unit = DEFAULT_NIL, target_unit = DEFAULT_NIL,
counters1={ counters1={
"think_counter", "think_counter",
"job_counter", "job_counter",
"swap_counter", "swap_counter",
"winded", "winded",
"stunned", "stunned",
"unconscious", "unconscious",
"suffocation", "suffocation",
"webbed", "webbed",
"soldier_mood_countdown", "soldier_mood_countdown",
"soldier_mood", --todo enum, "soldier_mood", --todo enum,
"pain", "pain",
"nausea", "nausea",
"dizziness", "dizziness",
}, },
counters2={ counters2={
"paralysis", "paralysis",
"numbness", "numbness",
"fever", "fever",
"exhaustion", "exhaustion",
"hunger_timer", "hunger_timer",
"thirst_timer", "thirst_timer",
"sleepiness_timer", "sleepiness_timer",
"stomach_content", "stomach_content",
"stomach_food", "stomach_food",
"vomit_timeout", "vomit_timeout",
"stored_fat" --TODO what to reset to? "stored_fat" --TODO what to reset to?
} }
} }
function editor_counters:fill_counters() function editor_counters:fill_counters()
local ret={} local ret={}
local u=self.target_unit local u=self.target_unit
for i,v in ipairs(self.counters1) do for i,v in ipairs(self.counters1) do
table.insert(ret,{f=u.counters:_field(v),name=v}) table.insert(ret,{f=u.counters:_field(v),name=v})
end end
for i,v in ipairs(self.counters2) do for i,v in ipairs(self.counters2) do
table.insert(ret,{f=u.counters2:_field(v),name=v}) table.insert(ret,{f=u.counters2:_field(v),name=v})
end end
return ret return ret
end end
function editor_counters:update_counters() function editor_counters:update_counters()
for i,v in ipairs(self.counter_list) do for i,v in ipairs(self.counter_list) do
v.text=string.format("%s:%d",v.name,v.f.value) v.text=string.format("%s:%d",v.name,v.f.value)
end end
self.subviews.counters:setChoices(self.counter_list) self.subviews.counters:setChoices(self.counter_list)
end end
function editor_counters:set_cur_counter(value,index,choice) function editor_counters:set_cur_counter(value,index,choice)
choice.f.value=value choice.f.value=value
self:update_counters() self:update_counters()
end end
function editor_counters:choose_cur_counter(index,choice) function editor_counters:choose_cur_counter(index,choice)
dialog.showInputPrompt(choice.name,"Enter new value:",COLOR_WHITE, dialog.showInputPrompt(choice.name,"Enter new value:",COLOR_WHITE,
@ -198,38 +198,38 @@ function editor_counters:choose_cur_counter(index,choice)
end) end)
end end
function editor_counters:init( args ) function editor_counters:init( args )
if self.target_unit==nil then if self.target_unit==nil then
qerror("invalid unit") qerror("invalid unit")
end end
self.counter_list=self:fill_counters() self.counter_list=self:fill_counters()
self:addviews{ self:addviews{
widgets.FilteredList{ widgets.FilteredList{
choices=self.counter_list, choices=self.counter_list,
frame = {t=0, b=1,l=1}, frame = {t=0, b=1,l=1},
view_id="counters", view_id="counters",
on_submit=self:callback("choose_cur_counter"), on_submit=self:callback("choose_cur_counter"),
on_submit2=self:callback("set_cur_counter",0),--TODO some things need to be set to different defaults on_submit2=self:callback("set_cur_counter",0),--TODO some things need to be set to different defaults
}, },
widgets.Label{ widgets.Label{
frame = { b=0,l=1}, frame = { b=0,l=1},
text ={{text= ": exit editor ", text ={{text= ": exit editor ",
key = "LEAVESCREEN", key = "LEAVESCREEN",
on_activate= self:callback("dismiss") on_activate= self:callback("dismiss")
}, },
{text=": reset counter ", {text=": reset counter ",
key = "SEC_SELECT", key = "SEC_SELECT",
}, },
{text=": set counter ", {text=": set counter ",
key = "SELECT", key = "SELECT",
} }
} }
}, },
} }
self:update_counters() self:update_counters()
end end
add_editor(editor_counters) add_editor(editor_counters)
-------------------------------main window---------------- -------------------------------main window----------------
@ -243,16 +243,16 @@ unit_editor.ATTRS={
function unit_editor:init(args) function unit_editor:init(args)
self:addviews{ self:addviews{
widgets.FilteredList{ widgets.FilteredList{
choices=editors, choices=editors,
on_submit=function (idx,choice) on_submit=function (idx,choice)
if choice.on_submit then if choice.on_submit then
choice.on_submit(self.target_unit) choice.on_submit(self.target_unit)
end end
end end
}, },
widgets.Label{ widgets.Label{
frame = { b=0,l=1}, frame = { b=0,l=1},
text ={{text= ": exit editor", text ={{text= ": exit editor",
key = "LEAVESCREEN", key = "LEAVESCREEN",