Created
July 31, 2023 01:50
-
-
Save SammyForReal/4d74851e52aa321008b2959062c87511 to your computer and use it in GitHub Desktop.
Make turtles run with certain instructions seamingless and savely as intended!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local coal_slot = 16 | |
local wait_delay = 30 -- In seconds | |
local start = { -154, 69, 74 } | |
--[[ Instruction set: | |
- Move: { <dir>, <steps> } | |
- dir = "north", "east", "south", "west", "y" | |
- steps = how many steps it should go, e.g: 3 | |
- Placing: { "place", <side>, <slot> } | |
- side = "left" or "right" | |
- slot = 1 to 16 | |
- Diggy diggy hole: { "dig", <side> } | |
- side = "left" or "right" | |
- slot = 1 to 16 | |
- Refuel: { "refuel", <side> } | |
- side = "up", "down", nil | |
- Sleep: { "sleep", <amount> } | |
- amount >= 1 | |
]] | |
local instructions = { | |
{"refuel", "down"}, | |
-- Going outside of our hole | |
{"east", 2}, | |
{"north", 1}, | |
{"east", 2}, | |
-- Placing down the plushies! | |
{"place", "south", 1}, | |
{"east", 1}, | |
{"place", "south", 2}, | |
{"east", 1}, | |
{"place", "south", 3}, | |
{"east", 1}, | |
{"place", "south", 4}, | |
-- Waiting at the end... | |
{"east", 1}, | |
{"sleep", wait_delay}, | |
-- Removing them! D: | |
{"west", 1}, | |
{"dig", "south", 4}, | |
{"west", 1}, | |
{"dig", "south", 3}, | |
{"west", 1}, | |
{"dig", "south", 2}, | |
{"west", 1}, | |
{"dig", "south", 1}, | |
-- Going back to beginning. | |
{"west", 2}, | |
{"south", 1}, | |
{"west", 2}, | |
-- Because we went to the beginning by | |
-- ourselfs, we will LOOP now smoothly! | |
} | |
----------------------------------------- | |
term.setTextColor(colors.white) | |
print("Running setup.") | |
local function shortestRotation(from, to) | |
local clockwise = (to - from + 4) % 4 | |
local counterclockwise = (from - to + 4) % 4 | |
return clockwise <= counterclockwise and "turnRight" or "turnLeft" | |
end | |
local function calculateDir(x, y, z, dir, amount) | |
if dir == "east" then | |
x = x+amount | |
elseif dir == "west" then | |
x = x+amount | |
elseif dir == "south" then | |
z = z+amount | |
elseif dir == "north" then | |
z = z-amount | |
end | |
return x, y, z | |
end | |
local rotation = "north" | |
local function lookTo(newRot) | |
local toName = { [0]="west", "north", "east", "south", "west", "north" } | |
local toNum = { north = 1, east = 2, south = 3, west = 4 } | |
local dir = shortestRotation(toNum[rotation], toNum[newRot]) | |
for i=1, 4 do | |
if newRot == rotation then | |
return | |
end | |
turtle[dir]() | |
rotation = toName[toNum[rotation] - (dir == "turnLeft" and 1 or -1)] | |
end | |
error("Could not rotate properly!") | |
end | |
local function refuel() | |
if turtle.getFuelLevel() <= 10 then | |
if turtle.getItemCount(coal_slot) >= 1 then | |
turtle.select(coal_slot) | |
turtle.refuel(1) | |
else | |
error("No fuel left to keep going!") | |
end | |
end | |
end | |
local function reverseInstructions(index) | |
index = index and index or 1 | |
print("- Reversing instructions. Starting at instruction index '"..index.."'") | |
for i=index, 1, -1 do | |
local step = instructions[i] | |
if step[1] == "place" or step[1] == "sleep" or step[1] == "dig" or step[1] == "refuel" then | |
-- Yeah... lets not do this one here. | |
elseif step[1] == 'y' then | |
for j=1, step[2] do | |
turtle[step[2] > 0 and "down" or "up"]() | |
end | |
elseif step[1] == "north" or step[1] == "east" or step[1] == "south" or step[1] == "west" then | |
lookTo(step[1]) | |
for j=1, math.floor(math.abs(step[2])) do | |
refuel() | |
turtle.back() | |
end | |
end | |
end | |
end | |
do -- Setup | |
term.setTextColor(colors.lightGray) | |
print("- Get current location.") | |
local x, y, z = gps.locate(5) | |
if not x then | |
printError("Can't locate GPS location") | |
return | |
end | |
print("- Get current rotation of turtle.") | |
do -- Get rotation of turtle | |
-- Make one step to calcualte, it's not pretty but whatever | |
refuel() | |
local success = turtle.forward() | |
local dir = "back" | |
if not success then | |
refuel() | |
success = turtle.back() | |
dir = "forward" | |
if not success then | |
turtle.turnLeft() | |
refuel() | |
success = turtle.forward() | |
dir = "back" | |
if not success then | |
refuel() | |
success = turtle.back() | |
dir = "forward" | |
if not success then | |
printError("Im stuck! Can't move :(") | |
return | |
end | |
end | |
end | |
end | |
-- Reverse action to make sure we are still on track | |
local x2,y2,z2 = gps.locate(5) | |
turtle[dir]() | |
-- And here are the results! | |
if x2 > x then | |
rotation = "east" | |
elseif x2 < x then | |
rotation = "west" | |
elseif z2 > z then | |
rotation = "south" | |
elseif z2 < z then | |
rotation = "north" | |
end | |
end | |
print("- Trying to rest the turtle.") | |
do -- Go back to beginning | |
local success = false | |
if not (x == start[1] and y == start[2] and z == start[3]) then | |
-- Are we on track? | |
local curX, curY, curZ = start[1], start[2], start[3] | |
for i, step in ipairs(instructions) do | |
curX, curY, curZ = calculateDir(curX, curY, curZ, step[1], step[2]) | |
if curX == x and curY == y and curZ == z then | |
-- Yes we are | |
reverseInstructions(i) | |
success = true | |
break | |
end | |
end | |
else | |
print(" Oh nvm, no need for that!") | |
success = true | |
end | |
if not success then | |
printError("Im not on track! Place me down where I should start!") | |
return | |
end | |
end | |
end | |
term.setTextColor(colors.white) | |
print("All done. Now starting!") | |
sleep(0.5) | |
term.setTextColor(term.isColor() and colors.yellow or colors.white) | |
print("Going through given manuscript; CTRL+T to cancel.") | |
while true do | |
-- Following instructions | |
for _, step in ipairs(instructions) do | |
if step[1] == "place" then | |
lookTo(step[2]) | |
turtle.select(step[3]) | |
turtle.place() | |
elseif step[1] == "dig" then | |
lookTo(step[2]) | |
turtle.select(step[3]) | |
turtle.dig() | |
elseif step[1] == "sleep" then | |
sleep(step[2]) | |
elseif step[1] == "refuel" then | |
local count = turtle.getItemCount(coal_slot) | |
if count < 64 then | |
turtle.select(coal_slot) | |
if step[2] == "down" then | |
turtle.suckDown(64-count) | |
elseif step[2] == "down" then | |
turtle.suckUp(64-count) | |
else | |
turtle.suck(64-count) | |
end | |
end | |
elseif step[1] == 'y' then | |
for j=1, step[2] do | |
turtle[step[2] > 0 and "up" or "down"]() | |
end | |
elseif step[1] == "north" or step[1] == "east" or step[1] == "south" or step[1] == "west" then | |
lookTo(step[1]) | |
for j=1, math.floor(math.abs(step[2])) do | |
refuel() | |
turtle.forward() | |
end | |
else | |
printError("Unknown instruction! The following step is invalid:") | |
printError(" "..textutils.serialise(step)) | |
return | |
end | |
end | |
-- Resetting | |
local x, y, z = gps.locate(5) | |
if not x then | |
printError("Could not locate position using GPS!") | |
return | |
end | |
if not (x == start[1] and y == start[2] and z == start[3]) then | |
local success = false | |
local curX, curY, curZ = start[1], start[2], start[3] | |
for i, step in ipairs(instructions) do | |
if step[1] == 'x' then | |
curX = curX + step[2] | |
elseif step[1] == 'y' then | |
curY = curY + step[2] | |
elseif step[1] == 'z' then | |
curZ = curZ + step[2] | |
end | |
if curX == x and curY == y and curZ == z then | |
reverseInstructions(i) | |
success = true | |
break | |
end | |
end | |
if not success then | |
reverseInstructions(#instructions) | |
end | |
end | |
sleep() | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment