For reference, this is the stuff that handles the creation and modification of persistent variables.
keys = arg[1] .. '_counters_1'
types = arg[2]
pers,status = dfhack.persistent.get(keys)
num = 1
if not pers then
dfhack.persistent.save({key=keys,value=types,ints={arg[3],0,0,0,0,0,1}})
else
if pers.ints[7] <= 6 then
if string.match(pers.value,types) ~= nil then
local valuea = split(pers.value,'_')
for i,x in ipairs(valuea) do
if x == types then pers.ints[i] = pers.ints[i] + arg[3] end
end
else
if pers.ints[7] < 6 then
pers.value = pers.value .. '_' .. types
pers.ints[7] = pers.ints[7] + 1
pers.ints[pers.ints[7]] = arg[3]
dfhack.persistent.save({key=pers.key,value=pers.value,ints=pers.ints})
elseif pers.ints[7] == 6 then
pers.ints[7] = 7
dfhack.persistent.save({key=arg[1] .. '_counters_2', value=types,ints={arg[3],0,0,0,0,0,0}})
end
end
else
num = math.floor(pers.ints[7]/7)+1
match = false
local valuea = split(pers.value,'_')
for i,x in ipairs(valuea) do
if x == types then
pers.ints[i] = pers.ints[i] + arg[3]
match = true
end
end
if not match then
for j = 2, num, 1 do
keysa = arg[1] .. '_counters_' .. tostring(j)
persa,status = dfhack.persistent.get(keysa)
local valuea = split(persa.value,'_')
for i,x in ipairs(valuea) do
if x == types then
persa.ints[i] = persa.ints[i] + arg[3]
dfhack.persistent.save({key=persa.key,value=persa.value,ints=persa.ints})
match = true
end
end
end
end
if not match then
pers.ints[7] = pers.ints[7] + 1
if math.floor(pers.ints[7]/7) == pers.ints[7]/7 then
print(pers.ints[7])
keysa = arg[1]..'_counters_'..tostring(num+1)
dfhack.persistent.save({key=keysa, value=types,ints={arg[3],0,0,0,0,0,0}})
else
persa.value = persa.value..'_'..types
print(pers.ints[7]-(num-1)*7+1)
persa.ints[pers.ints[7]-(num-1)*7+1] = persa.ints[pers.ints[7]-(num-1)*7+1] + arg[3]
dfhack.persistent.save({key=persa.key,value=persa.value,ints=persa.ints})
end
end
end
dfhack.persistent.save({key=pers.key,value=pers.value,ints=pers.ints})
end
I just got rid of the last bug that was bothering me, so I will be uploading the script (and a few others, like a delay script) this weekend most likely.