From a0434a7225adaa1b4dee830d0addf1a636a7dec6 Mon Sep 17 00:00:00 2001 From: cron Date: Thu, 5 Nov 2020 19:11:17 +0000 Subject: [PATCH] turtle: add tlang scheduler --- clientmods/turtle/init.lua | 28 ++++++++++++++++++++++++++++ clientmods/turtle/tlang.lua | 11 ++++++++--- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/clientmods/turtle/init.lua b/clientmods/turtle/init.lua index 641525e35..4c6086bc5 100644 --- a/clientmods/turtle/init.lua +++ b/clientmods/turtle/init.lua @@ -2,6 +2,9 @@ turtle = {} +local mod_prefix = minetest.get_modpath(minetest.get_current_modname()) +tlang = dofile(mod_prefix .. "/tlang.lua") + local iter = {} local iter_storage = {} @@ -374,3 +377,28 @@ minetest.register_chatcommand("quarry", { end }) + +turtle.states = {} + +function turtle.schedule(name, state) + turtle.states[#turtle.states] = {name = name, state = state} +end + +function turtle.run_states() + local dead = {} + for i, v in ipairs(turtle.states) do + local ret = tlang.step(v.state) + if ret ~= true then + if type(ret) == "string" then + minetest.display_chat_message("Turtle/tlang ERROR in " .. v.name .. ": " .. ret) + end + dead[#dead] = i + end + end + + for i, v in ipairs(dead) do + table.remove(turtle.states, v) + end +end + +minetest.register_globalstep(turtle.run_states) diff --git a/clientmods/turtle/tlang.lua b/clientmods/turtle/tlang.lua index 6fdb0f123..2b9a2aba2 100644 --- a/clientmods/turtle/tlang.lua +++ b/clientmods/turtle/tlang.lua @@ -1,8 +1,13 @@ local tlang = {} -tlang.lex = dofile("tlang_lex.lua") -tlang.parse = dofile("tlang_parse.lua") -tlang.builtins, tlang.gassign, tlang.step = dofile("tlang_vm.lua") +local prefix = "" +if minetest ~= nil then + prefix = minetest.get_modpath(minetest.get_current_modname()) .. "/" +end + +tlang.lex = dofile(prefix .. "tlang_lex.lua") +tlang.parse = dofile(prefix .. "tlang_parse.lua") +tlang.builtins, tlang.gassign, tlang.step = dofile(prefix .. "tlang_vm.lua") -- TODO --[[