How does this look? Do functions need their own lua files or can they be in the same lua file as the rest of the script?
holiday_shamrock.lua
holiday_shamrock_countdown()
function holiday_shamrock_countdown()
local year_tick_total = 1200*28*12
local current_tick = df.global.cur_year_tick
local date_shamrock = 1200*(2*28+17)
local ticks = date_shamrock - current_tick
if ticks < 0 then
ticks = ticks + year_tick_total
end
dfhack.timeout(ticks+1,'ticks',function () holiday_shamrock(ticks) end)
end
function holiday_shamrock(ticks)
pos = getPositionSurface()
dfhack.run_script(modtools/create-unit -race holiday_LEPRECHAUN -caste MALE -name PLAINS -location pos -age 1)
dfhack.run_script("modtools/create-unit","-race","holiday_LEPRECHAUN","-caste","MALE","-name","PLAINS","-location",pos,"-age",1)
dfhack.timeout(ticks+1,'ticks',holiday_shamrock_countdown)
holiday_shamrock_countdown()
end
function getPositionSurface()
local rand = dfhack.random.new()
local mapx, mapy, mapz = dfhack.maps.getTileSize()
randx = rand:random(mapx)
randy = rand:random(mapy)
local pos = {}
pos.x = randx
pos.y = randy
pos.z = mapz - 1
local j = 0
while dfhack.maps.ensureTileBlock(pos.x,pos.y,pos.z-j).designation[pos.x%16][pos.y%16].outside do
j = j + 1
end
pos.z = pos.z - j + 1
return pos
end
EDIT:
I assume the following would go in OnMapLoad.init?
dfhack.run_script(holiday_shamrock.lua)
Modified code in post, a couple comments;
1. The function call in the timeout has a specific format in that it can't take input variables. To get around this we create a sudo function whose only purpose is to call the function you want.
2. The function getPositionSurface returns a table with x, y, z values. When you call it you have to assign that table to a variable. Without the 'pos =' part your holiday_shamrock function wouldn't know what the variable pos means.
3. dfhack.run_script takes its input as a set, not as a whole. So you need to seperate them out with commas
4. Unless you are passing a variable you need to use quotes (single or double) to identify what you are passing as a string. As you can see, everything in the dfhack.run_script call it a string except for pos which is a variable (and the 1)
5. It isn't necessary to do another timeout call in the holiday_shamrock function, you can just straight call the holiday_shamrock_countdown() function and it will set up another call back itself
The OnMapLoad.init shouldn't need the dfhack.run_script part (in fact it might not even know what dfhack is) you should just be able to put
holiday_shamrock
no .lua needed. It will then run the file