diff --git a/src/buildscript.lua b/src/buildscript.lua index 44a04ad..ffdada4 100644 --- a/src/buildscript.lua +++ b/src/buildscript.lua @@ -14,21 +14,13 @@ local buildscript = {} buildscript.VERSION = "0.0.1" -- Runs a movescript script, while ensuring that a given item is always selected. -function buildscript.runWithItem(ms_script, filterExpr) - local instructions = movescript.parse(ms_script) - for idx, instruction in pairs(instructions) do - itemscript.selectOrWait(filterExpr) - movescript.executeInstruction(instruction) - end +function buildscript.runWithItem(ms_script, filterExpr, settings) + movescript.run(ms_script, settings, function() itemscript.selectOrWait(filterExpr) end) end -- Runs a movescript script, while selecting random items that match a filter. function buildscript.runWithRandomItems(ms_script, filterExpr) - local instructions = movescript.parse(ms_script) - for idx, instruction in pairs(instructions) do - itemscript.selectRandomOrWait(filterExpr) - movescript.executeInstruction(instruction) - end + movescript.run(ms_script, settings, function() itemscript.selectRandomOrWait(filterExpr) end) end -- Parses a value for an argument specification from a raw value. diff --git a/src/movescript.lua b/src/movescript.lua index 60ba969..5314b57 100644 --- a/src/movescript.lua +++ b/src/movescript.lua @@ -294,12 +294,13 @@ end -- Executes a single instruction. An instruction is a table with an "action" -- and some attributes, such as if it needs fuel or not. -function movescript.executeInstruction(instruction, settings) +function movescript.executeInstruction(instruction, settings, preExecuteHook, postExecuteHook) + if settings == nil then settings = movescript.defaultSettings end if instruction.type == INSTRUCTION_TYPES.repeated then debug("Executing repeated instruction " .. instruction.count .. " times.", settings) for i = 1, instruction.count do for _, nestedInstruction in pairs(instruction.instructions) do - movescript.executeInstruction(nestedInstruction, settings) + movescript.executeInstruction(nestedInstruction, settings, preExecuteHook, postExecuteHook) end end elseif instruction.type == INSTRUCTION_TYPES.instruction then @@ -316,7 +317,9 @@ function movescript.executeInstruction(instruction, settings) refuelToAtLeast(fuelRequired, settings) end for i = 1, instruction.count do + if preExecuteHook ~= nil then preExecuteHook() end action.f(instruction.options, settings) + if postExecuteHook ~= nil then postExecuteHook() end end end end @@ -437,21 +440,21 @@ function movescript.parse(script, settings) return instructions end -function movescript.run(script, settings) +function movescript.run(script, settings, preExecuteHook, postExecuteHook) settings = settings or movescript.defaultSettings script = script or "" debug("Executing script: " .. script, settings) local instructions = movescript.parse(script, settings) for idx, instruction in pairs(instructions) do - movescript.executeInstruction(instruction, settings) + movescript.executeInstruction(instruction, settings, preExecuteHook, postExecuteHook) end end -function movescript.runFile(filename, settings) +function movescript.runFile(filename, settings, preExecuteHook, postExecuteHook) local f = fs.open(filename, "r") local script = f.readAll() f.close() - movescript.run(script, settings) + movescript.run(script, settings, preExecuteHook, postExecuteHook) end function movescript.validate(script, settings)