Skip to content

Instantly share code, notes, and snippets.

@scarletcafe
Last active July 14, 2022 19:48
Show Gist options
  • Save scarletcafe/8d4eb83b18671bc7b6c9ce890ed77fe9 to your computer and use it in GitHub Desktop.
Save scarletcafe/8d4eb83b18671bc7b6c9ce890ed77fe9 to your computer and use it in GitHub Desktop.
turtlereplay.lua glass/ice
-- custom stuff
modemLocation = "left"
turtleName = "nether 2 blue ice"
useModem = true
-- ice or glass
local tArgs = { ... }
if #tArgs ~= 1 then
print("Please give either 'ice' or 'glass'")
return
end
if tArgs[1] == "ice" then
moves = {
{["action"]="checkpoint"}, -- special op
{["action"]="digUp", ["noreplay"]=true},
{["action"]="digDown", ["noreplay"]=true},
{["action"]="down"},
{["action"]="digDown", ["noreplay"]=true},
{["action"]="placeDown", ["block"]="minecraft:blue_ice", ["noreplay"]=true},
{["action"]="up"},
-- go forward
{["action"]="dig", ["noreplay"]=true},
{["action"]="forward"},
}
elseif tArgs[1] == "glass" then
moves = {
{["action"]="checkpoint"}, -- special op
{["action"]="digUp", ["noreplay"]=true},
{["action"]="digDown", ["noreplay"]=true},
{["action"]="down"},
{["action"]="digDown", ["noreplay"]=true},
{["action"]="up"},
{["action"]="placeDown", ["block"]="minecraft:glass_pane", ["noreplay"]=true},
-- go forward
{["action"]="dig", ["noreplay"]=true},
{["action"]="forward"},
}
end
-- preliminary protocol checks
if not turtle then
printError("turtlereplay is only available on turtles")
return
end
if useModem then
rednet.open(modemLocation)
end
-- library code
basicMoveMap = {
["forward"]=turtle.forward,
["back"]=turtle.back,
["up"]=turtle.up,
["down"]=turtle.down,
["turnLeft"]=turtle.turnLeft,
["turnRight"]=turtle.turnRight,
["dig"]=turtle.dig,
["digUp"]=turtle.digUp,
["digDown"]=turtle.digDown,
["place"]=turtle.place,
["placeUp"]=turtle.placeUp,
["placeDown"]=turtle.placeDown
}
inverseMoveMap = {
["forward"]="back",
["back"]="forward",
["up"]="down",
["down"]="up",
["turnLeft"]="turnRight",
["turnRight"]="turnLeft",
["dig"]="place",
["digUp"]="placeUp",
["digDown"]="placeDown",
["place"]="dig",
["placeUp"]="digUp",
["placeDown"]="digDown"
}
function doAction(action)
print(action)
if action["action"] == "checkpoint" then
-- checkpointed, delete undo steps
fs.delete("/.turtle.replay")
return 0
end
-- if a block is specified, make sure it is selected, else fail
if action["block"] ~= nil then
foundBlock = false
for i = 1,16 do
itemDetail = turtle.getItemDetail(i)
if itemDetail ~= nil and itemDetail["name"] == action["block"] then
turtle.select(i)
foundBlock = true
break
end
end
if not foundBlock then
return string.format("Couldn't find block in inventory: %s", action["block"])
end
end
-- if action name not recognized, fail
if basicMoveMap[action["action"]] == nil then
return string.format("Didn't recognize action: %s", action["action"])
end
-- do action
basicMoveMap[action["action"]]()
-- unless noreplay is set, record progress to checkpoint file
if action["noreplay"] ~= true then
fileHandle = fs.open("/.turtle.replay", fs.exists("/.turtle.replay") and "a" or "w")
fileHandle.writeLine(action["action"])
fileHandle.flush()
fileHandle.close()
end
return 0
end
function undoAll()
fileHandle = fs.open("/.turtle.replay", "r")
replayUndoText = fileHandle.readAll()
fileHandle.close()
replayUndoActions = {}
for s in replayUndoText:gmatch("[^\r\n]+") do
if s:len() ~= 0 then
table.insert(replayUndoActions, s)
end
end
for currentMove = 1, #replayUndoActions do
actualMove = #replayUndoActions + 1 - currentMove
forwardMove = replayUndoActions[actualMove]
inverseMove = inverseMoveMap[forwardMove]
print(string.format("Undoing %s -> %s", forwardMove, inverseMove))
basicMoveMap[inverseMove]()
end
end
function rescueMode()
if useModem then
print("Going into rescue mode.. (hold Ctrl-T to stop)")
while true do
rednet.broadcast(string.format("[%s/turtlereplay] RESCUE", turtleName), "turtlereplay")
sleep(2)
end
else
print("Rescue mode not available when modem is off. Halting anyway")
while true do
sleep(2)
end
end
end
-- entrypoint
if fs.exists("/.turtle.replay") then
print("I appear to have stopped. I may have gone outside of the chunk range.")
print("I'm going to undo the last actions I remember doing.")
undoAll()
print("All actions undone.")
fs.delete("/.turtle.replay")
rescueMode()
else
print("Replaying action set")
while true do
for currentMove = 1, #moves do
print(string.format("Doing %s", moves[currentMove]["action"]))
if useModem then
if moves[currentMove]["action"] == "checkpoint" then
rednet.broadcast(string.format("[%s/turtlereplay] CHECKPOINT", turtleName), "turtlereplay")
else
rednet.broadcast(string.format("[%s/turtlereplay] DO %s", turtleName, moves[currentMove]["action"]), "turtlereplay")
end
end
code = doAction(moves[currentMove])
if code ~= 0 then
print(string.format("We've hit some kind of error:\n%s\n Undoing to last checkpoint..", code))
undoAll()
print("Undone.")
fs.delete("/.turtle.replay")
rescueMode()
end
end
end
end
@avayert
Copy link

avayert commented Jul 14, 2022

hi I get this error

  File "script.lua", line 1
    -- custom stuff
              ^^^^^
SyntaxError: invalid syntax

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment