From 300515184b76fc8f011351144e6a5c251447d69d Mon Sep 17 00:00:00 2001 From: Olav Bakke Svendsen Date: Tue, 14 May 2024 13:03:20 +0200 Subject: [PATCH] vines/turtle.lua: command strings testing 2 --- vines/turtle.lua | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/vines/turtle.lua b/vines/turtle.lua index 5bc5ef8..0cbe390 100644 --- a/vines/turtle.lua +++ b/vines/turtle.lua @@ -135,25 +135,37 @@ end -- "u15 r" = 15x up -> turn right -- "u1 5r" = invalid +-- commands : t f +local commands = + { ["u"] = turtle.up + , ["d"] = turtle.down + , ["f"] = turtle.forward + , ["b"] = turtle.back + , ["l"] = turtle.turnLeft + , ["r"] = turtle.turnRight + , ["h"] = harvest + } + -- runCommand : string -> true | false message -local runCommand = function(str) - local cmd, n, rest = string.match(string.gsub(str, "^%s+", ""), "^(%w)(%d*)(.+)") +local runCommand +runCommand = function(str) + local c, n, rest = string.match(string.gsub(str, "^%s+", ""), "^(%w)(%d*)(.*)") + if not c then return true end if n == "" then n = 1 else n = tonumber(n) end - for i = 1, n do - print(cmd, tostring(i).."/"..tostring(n)) - end - runCommand(rest) + for i = 1, n do commands[c]() end +-- print(cmd, tostring(i).."/"..tostring(n)) +-- end + return runCommand(rest) end -- for testing purposes - local sequence = function() print("pretending to unload stray items") print("pretending to refuel") print("pretending to stock up on shears") - local cmd = "rf4h" + local cmd = "rf3hfhflfr2" print(cmd) - runCommand(cmd) + return runCommand(cmd) end while true do @@ -174,9 +186,9 @@ while true do turtle.forward() elseif key == 83 then -- s, back turtle.back() - elseif key == 87 then -- a, turn left + elseif key == 65 then -- a, turn left turtle.turnLeft() - elseif key == 87 then -- d, turn right + elseif key == 68 then -- d, turn right turtle.turnRight() elseif key == 32 then -- Space, up turtle.up() @@ -185,6 +197,6 @@ while true do elseif key == 257 then -- Enter, run sequence print("Is the turtle indexed properly?\n(press y to confirm)") local _, char = os.pullEvent("char") - if char = "y" then sequence() else print("Aborted") end + if char == "y" then sequence() else print("Aborted") end end end -- 2.30.2