Starting to work on moving over my scripts. Picked an easy one to start with, wanted to get your opinion before I continue
--change-values.lua v1.0
--[[TODO
--]]
function split(str, pat)
local t = {} -- NOTE: use {n = 0} in Lua-5.0
local fpat = "(.-)" .. pat
local last_end = 1
local s, e, cap = str:find(fpat, 1)
while s do
if s ~= 1 or cap ~= "" then
table.insert(t,cap)
end
last_end = e+1
s, e, cap = str:find(fpat, last_end)
end
if last_end <= #str then
cap = str:sub(last_end)
table.insert(t, cap)
end
return t
end
function createcallback(etype,unitTarget,ctype,strength,save)
return function(reseteffect)
effect(etype,unitTarget,ctype,strength,save,-1)
end
end
function effect(e,unitTarget,ctype,strength,save,dir)
local value = 0
local t = 0
local int16 = 30000
local int32 = 200000000
if (e == 'webbed' or e == 'stunned' or e == 'winded' or e == 'unconscious' or e == 'pain'
or e == 'nausea' or e == 'dizziness') then t = 1 end
if (e == 'paralysis' or e == 'numbness' or e == 'fever' or e == 'exhaustion'
or e == 'hunger' or e == 'thirst' or e == 'sleepiness') then t = 2 end
if e == 'blood' then t = 3 end
if e == 'infection' then t = 4 end
if (e == 'hunger' or e == 'thirst' or e == 'sleepiness') then e = e .. '_timer' end
if t == 1 then
value = unitTarget.counters[e]
if dir == 1 then save = value end
if ctype == 'fixed' then
value = value + strength
elseif ctype == 'percent' then
local percent = (100+strength)/100
value = math.floor(value*percent)
elseif ctype == 'set' then
value = strength
end
if value > int16 then value = int16 end
if value < 0 then value = 0 end
if dir == -1 then value = save end
unitTarget.counters[e] = value
elseif t == 2 then
value = unitTarget.counters2[e]
if dir == 1 then save = value end
if ctype == 'fixed' then
value = value + strength
elseif ctype == 'percent' then
local percent = (100+strength)/100
value = math.floor(value*percent)
elseif ctype == 'set' then
value = strength
end
if value > int16 then value = int16 end
if value < 0 then value = 0 end
if dir == -1 then value = save end
unitTarget.counters2[e] = value
elseif t == 3 then
if dir == 1 then save = value end
if ctype == 'fixed' then
value = value + strength
elseif ctype == 'percent' then
local percent = (100+strength)/100
value = math.floor(value*percent)
elseif ctype == 'set' then
value = strength
end
if value > unitTarget.body.blood_max then value = unitTarget.body.blood_max end
if value < 0 then value = 0 end
unitTarget.body.blood_count = value
elseif t == 4 then
value = unitTarget.body.infection_level
if dir == 1 then save = value end
if ctype == 'fixed' then
value = value + strength
elseif ctype == 'percent' then
local percent = (100+strength)/100
value = math.floor(value*percent)
elseif ctype == 'set' then
value = strength
end
if value > int16 then value = int16 end
if value < 0 then value = 0 end
unitTarget.body.infection_level = value
end
end
validArgs = validArgs or utils.invert({
'help',
'token',
'mode',
'value',
'dur',
'unit',
})
mode = mode or utils.invert({
'fixed',
'percent',
'set',
})
local args = utils.processArgs({...}, validArgs)
if args.help then
print("TODO - Help Section")
return
end
args.unit = df.unit.find(tonumber(args.unit))
args.mode = mode[args.mode or 'set']
args.token = split(args.token,'+')
args.value = split(args.value,'+')
if args.dur and tonumber(args.dur) then
dur = tonumber(args.dur)
else
dur = 0
end
for i,etype in ipairs(args.token) do
save = effect(etype,args.unit,args.mode,tonumber(args.value[i]),0,1)
if dur > 0 then
dfhack.timeout(dur,'ticks',createcallback(etype,args.unit,args.mode,tonumber(args.value[i]),save))
end
end
My experience with Lua is limited to only what I have done with DFHack so I am sure there are several things that could be better, but I tried to follow your example scripts (with reference to the processArgs and such. Thoughts? Comments? Suggestions? Angry Ranting?