Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local version=1.2
- -- aDY5Ez8M
- -- r
- -- sEi'2014-2022
- --[[
- R macros WIP by sEi'2014-2022 - (C)opyleft (Do what you want with the script)
- Get this script here: http://pastebin.com/aDY5Ez8M
- or ingame: pastebin get aDY5Ez8M r
- Used for simple handling of a (Minecraft) Computercraft turtle from the command-line
- My first script and i ended up using it a lot - Bad coding but it works :)
- Name the script whatever you want, i name it "r" so its quick and easy to use!
- Place this file in the root of the turtle directory and use like the examples below
- SYNTAX: <Program name> <Orders> [Run this program after completion]
- Example 1) (Type text in the turtle console and press enter)
- r fffurrfffdll
- This orders the turtle to go forward 3, up 1, turning right 2 times, forward 3,
- down 1, turning left 2 times. - If anything is blocking the path it will stop
- with an error message.
- Example 2)
- r Bffrff3y
- NOTE: Since the orders starts with a "B" then it will dig blocks blocking the path.
- This orders the turtle to move 2 forward, turn right, move 2 forward and finally place a block
- from slot #3 below the turtle.
- RESERVED COMMANDS:
- help = Prints the reserved commands.
- b = Set BREAK = false (default) - Turtle is NOT breaking blocks that are blocking the path.
- B = Set BREAK = True - Turtle IS breaking (digging) blocks that are blocking the path.
- P = Place a block in front of turtle from selected slot (default=1).
- If BREAK = False (default) - If the spot is not empty then the TURTLE STOP with an error message.
- If BREAK = True - If the spot is not empty then the TURTLE DIG until spot is empty and
- then place the block.
- l = Turn Left 1
- r = Turn Right 1
- f = Go forward 1
- F = Go Forward UNTIL path is blocked. (BREAK true have ofc no effect!)
- u = Go Up 1.
- U = Go Up UNTIL path is blocked. Use with CAUTION 8^) (BREAK true have ofc no effect!)
- d = Go Down 1.
- D = Go Down UNTIL path is blocked. (BREAK true have ofc no effect!)
- x = Go Back 1.
- X = Go Back UNTIL path is blocked. (BREAK true have ofc no effect!)
- y = Place block Down
- Y = Place block Up
- i = Dig in front of Turtle (BREAK true/false have no effect!)
- z = Dig Down 1 time (BREAK true/false have no effect!)
- Z = Dig Up 1 time (BREAK true/false have no effect!)
- w = Do NOT warn when only 1 in current slot
- W = (default) Warn when only 1 in current slot
- 0 (zero) = Use any slot
- 1-9 = Slot selection (atm. you cannot select slots with a higher number than 9)
- I = Ignore blocked path (NOTE: Will give unpredictable results!)
- ----
- I have made some nifty little batch files where i am using R via the shell command!
- None published though, just mentioning as inspiration.
- Rudimentary batch demo here building a house: https://pastebin.com/r2eMA7WB
- ingame: pastebin get r2eMA7WB house
- ----
- ]]--
- -- INIT
- local sCommands = "bBlrPfFuUdDxXyYizZ0123456789wW"
- local tArgs={...}
- local sProg = tArgs[1] or "help"
- local sArg2 =tArgs[2] or ""
- local iDefSlot = 0
- iCurSlot = 1 --iDefSlot
- local bBreak = false
- local bError = false
- local bIgnore = false
- local oT = turtle
- local max = #sProg
- local sBak = ""
- local sOrder
- local iWarn = 1
- local iEmpty1 = 4
- -- FUNCTIONS
- local function HelpText()
- --local sHT = "SYNTAX: <Program name> <Orders>"
- local sHT = "TURN:[l]eft,[r]ight"
- sHT = sHT.."\nSTEP:[f]orward,[u]p,[d]own,[x]=reverse"
- sHT = sHT.."\nMOVE UNTIL BLOCKED:[F],[U],[D],[X]"
- sHT = sHT.."\nBREAK:[B]=on,[b]=off(default)"
- sHT = sHT.."\nSLOT SELECT:[1-9]or[0]=use any(default)"
- sHT = sHT.."\nPLACE BLOCK:[P]front,[Y]up,[y]down"
- sHT = sHT.."\nDIG:[i]front,[z]down,[Z]up"
- sHT = sHT.."\nEXAMPLE: r Bffdrrffrru"
- sHT = sHT.."\n=Set to break blocks if in our path and"
- sHT = sHT.."\nforw2,down1,right2,forw2,right2,up1"
- sHT = sHT.."\n(See more HELP in the code)"
- --sHT = sHT.."\nALL: "..sCommands
- return sHT
- end
- local function doThis(sChar,sLastMsg)
- --doFuel()
- if bError==false then
- --crude checks
- if tonumber(sChar) ~= nil then --is number then is slotselect
- local iChar=tonumber(sChar)
- if iChar==0 then
- local iTemp=0
- while iTemp==0 do
- iTemp = findAny()
- if iTemp>0 then
- oT.select(iTemp)
- else
- --print("No more materials (ANY)")
- print("--ERROR--\nNo more materials (ANY)\nI choose to Pause\n--END--")
- print("Fill any slot and press ENTER\nor press and hold CTRL-T to quit")
- doWaitForEnter()
- end
- end
- else
- iCurSlot=iChar
- oT.select(iCurSlot)
- end
- end
- if sChar=="I" then bIgnore=true end
- if sChar=="P" then
- if oT.detect() then --Something in the front and we wanna put there
- if bBreak then
- while oT.detect() do
- oT.dig()
- sleep(1)
- end
- myPlace("front")
- else
- if bIgnore==false then
- error("--ERROR--\nBlock in front of me\nI choose to Exit\n--END--")
- end
- end
- else
- myPlace("front")
- end
- end
- if sChar=="w" then
- iWarn=0
- elseif sChar=="W" then
- iWarn=1
- elseif sChar=="i"then
- oT.dig()
- elseif sChar=="z"then
- oT.digDown()
- elseif sChar=="Z"then
- oT.digUp()
- elseif sChar=="Y" then
- if oT.detectUp() then
- if bBreak then
- while oT.detectUp() do
- oT.digUp()
- end
- myPlace("up")
- else
- if bIgnore==false then
- error("--ERROR--\nBlock over me\nI choose to Exit\n--END--")
- --bError=true
- end
- end
- else
- myPlace("up")
- end
- elseif sChar=="y" then
- if oT.detectDown() then
- if bBreak then
- while oT.detectDown() do
- oT.digDown()
- end
- myPlace("down")
- else
- if bIgnore==false then
- error("--ERROR--\nBlock under me\nI choose to Exit\n--END--")
- --bError=true
- end
- end
- else
- myPlace("down")
- end
- elseif sChar == "U" then
- while oT.detectUp() == false do
- doFuel()
- oT.up()
- end
- elseif sChar == "D" then
- while oT.detectDown() == false do
- doFuel()
- oT.down()
- end
- elseif sChar == "F" then
- while oT.detect() == false do
- doFuel()
- oT.forward()
- end
- elseif sChar=="x"then
- oT.turnRight()
- oT.turnRight()
- walk()
- oT.turnRight()
- oT.turnRight()
- elseif sChar=="X" then
- oT.turnRight()
- oT.turnRight()
- while oT.detect() == false do
- doFuel()
- oT.forward()
- end
- oT.turnRight()
- oT.turnRight()
- elseif sChar=="b" then
- bBreak = false
- elseif sChar=="B" then
- bBreak = true
- elseif sChar=="f" then
- walk()
- elseif sChar=="l" then
- oT.turnLeft()
- elseif sChar=="r" then
- oT.turnRight()
- elseif sChar=="u" then
- if oT.detectUp() then
- if bBreak then
- while oT.detectUp() do
- oT.digUp()
- sleep(1)
- end
- else
- if bIgnore==false then
- error("--ERROR--\nBlock over me\nI choose to Exit\n--END--")
- --bError=true
- end
- end
- end
- doFuel()
- oT.up()
- elseif sChar=="d" then
- if oT.detectDown() then
- if bBreak then
- while oT.detectDown() do
- oT.digDown()
- sleep(1)
- end
- else
- if bIgnore==false then
- error("--ERROR--\nBlock under me\nI choose to Exit\n--END--")
- --bError=true
- end
- end
- end
- doFuel()
- oT.down()
- end
- end
- end
- function doFuel()
- while turtle.getFuelLevel() ~= "unlimited" and turtle.getFuelLevel() < 1 do
- for i = 16, 1,-1 do -- loop through the slots (backwards to not interfere with important slots 1-9 if possible)
- turtle.select(i) -- change to the slot
- if turtle.refuel(0) then -- it's valid fuel
- local halfStack = math.ceil(turtle.getItemCount(i)/2) -- work out half of the amount of fuel in the slot
- turtle.refuel(halfStack) -- consume half the stack as fuel
- break
- end
- end
- turtle.select(iCurSlot)
- if turtle.getFuelLevel() < 1 then
- print("OUT OF FUEL - Put some fuel in a slot and press ENTER")
- doWaitForEnter()
- end
- end
- end
- function myPlace(sDir)
- if iCurSlot>0 then
- oT.select(iCurSlot)
- else
- local iTemp=0
- while iTemp==0 do
- iTemp = findAny()
- if iTemp>0 then
- oT.select(iTemp)
- iCurSlot=iTemp
- else
- --print("No more materials (ANY)")
- print("--ERROR--\nNo more materials (ANY)\nI choose to Pause\n--END--")
- print("Fill any slot and press ENTER\nor press and hold CTRL-T to quit")
- doWaitForEnter()
- end
- end
- end
- if oT.getItemCount(iCurSlot)==1 then
- local iT
- --fill current slot up if any similar items around since we only have one item left in current slot.
- for iT=1,16 do
- if iT~=iCurSlot then
- if oT.compareTo(iT) then
- local iTemp2=64-oT.getItemCount(iCurSlot)
- oT.select(iT)
- --local iTemp=oT.getItemCount(iCurSlot)
- --if iTemp2<=iTemp then
- -- iTemp=iTemp2
- --end
- oT.transferTo(iCurSlot,iTemp2)
- sleep(1)
- oT.select(iCurSlot)
- break
- end
- end
- end
- end
- if iEmpty1 and (iCurSlot ~= iEmpty1)then
- while oT.getItemCount(iCurSlot)==iWarn do
- print("--ERROR--\nNo more materials in slot: "..iCurSlot.."\nI choose to Pause\n--END--")
- print("Fill slot: "..iCurSlot.." and press ENTER\nor press and hold CTRL-T to quit")
- doWaitForEnter()
- end
- end
- if sDir=="dummy" then
- --oT.place()
- elseif sDir=="front" then
- oT.place()
- elseif sDir=="up" then
- oT.placeUp()
- elseif sDir=="down" then
- oT.placeDown()
- else --error
- error("ERROR: No such direction: "..sDir.."\nI choose to END\n--END--")
- --bError=true
- return
- end
- end
- function walk()
- if oT.detect() and bBreak then
- while oT.detect() do
- oT.dig()
- sleep(1)
- end
- doFuel()
- oT.forward()
- elseif oT.detect() then
- if bIgnore==false then
- error("--ERROR--\nBlock in front of me\nI choose to Exit\n--END--")
- --bError=true
- end
- else
- doFuel()
- oT.forward()
- end
- end
- function findAny() --Find a slot containing anything
- local iT
- for iT=1,15 do
- if oT.getItemCount(iT) >0 then return iT end
- end
- return 0
- end
- function doWaitForEnter()
- while true do
- --Pull the event
- local _, key = os.pullEvent("key")
- if key == keys.enter then
- break
- end
- end
- end
- --INIT BOT
- --oT.select(iDefSlot)
- -- MAIN
- if sProg=="help" then
- --print("For help type: r help")
- --print("Reserved commands: "..sCommands)
- print(HelpText())
- else
- while false do -- ass tests (false = this block is not run)
- local sTemp=tArgs[2]
- if tArgs[2] then
- print("Found Number: "..sProg)
- else
- print("Found NO 2'nd argument")
- end
- return
- end
- --un rem next line to monitor whats going on
- --print("Running this: "..sProg)
- local iT
- for iT=1,max do
- sOrder=string.sub(sProg,iT,iT)
- doThis(sOrder,sBak)
- sBak=sOrder
- end
- -- if bError==true then
- -- print("There was error(s)\nPress any key to exit")
- -- os.pullEvent("key")
- -- end
- if #sArg2>0 then
- shell.run(sArg2)
- end
- end
Add Comment
Please, Sign In to add comment