Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- MRP library - Move and Refuel Program
- -- Version: 2.0.0
- -- by @markman4897
- -- Place an output (ender)chest in the first slot.
- -- (the turtle will dump all its contents there)
- --
- -- Place the fuel (ender)chest in the second slot.
- -- (the turtle will take lava buckets, refill
- -- and place empty buckets back into that chest)
- --
- -- Write the coordinates of the turtle into the
- -- variables below (x_coord, z_coord and y_coord).
- --
- -- Write the orientation of the turtle into the
- -- variable below (dir). 1 means north, 2 means
- -- east, 3 means south and 4 means west.
- -- Functions of the program:
- -- - Movement tracking by coordinates
- -- - Auto refuel with (ender)chest in slot 2 with
- -- lava bucket.
- -- - Dump all items into (ender)chest in slot 1.
- -- Enjoy basic library.
- -- TODO:
- -- - add option to set dir in string or int
- --
- -- - change length for times in dig function
- --
- -- - revise function set (recursive calling)
- -- - revise function get (maybe change return)
- --
- -- - make move function use native api so it can
- -- handle interrupts
- --
- -- - make mapper turtle (that sends the map via
- -- rednet to central map computer for later and
- -- massive use)
- --
- -- - change the dig and move function from while
- -- to repeat
- -- - drop the recursion in dig and move functions
- -- - move attack from dig to move function
- --
- -- - tutorial on how to merge programs with the
- -- library if people don't want two files
- -- (replace MPR.get-s first and then delete all
- -- "MPR."... i think, and then just paste the
- -- program at the end of this library)
- --
- -- - add alternative method for refueling (fuel
- -- instead of a chest)
- --
- -- - add alternative method for invDump (go to
- -- dump chest when full)
- -- - add exceptions in inv check for when you need
- -- to go to dump chest (one more slot free)
- --
- -- - switch fuel chest slot and dump chest slot
- -- =========
- -- Variables
- -- =========
- -- coordinate values
- x_coord = 0
- z_coord = 0
- y_coord = 0
- dir = 1
- directions = {"north", "east", "south", "west"}
- -- other variables
- dumpSlot = 1
- fuelSlot = 2
- -- =========
- -- Functions
- -- =========
- -- Set and get function to change variables here
- function set(a,b,c,d)
- c = c or 0
- d = d or 0
- if a == "x" then
- x_coord = b
- elseif a == "z" then
- z_coord = b
- elseif a == "y" then
- y_coord = b
- elseif a == "dir" then
- dir = b
- elseif c ~= 0 and d == 0 then
- -- could do this with calling this function 4 times...
- -- is it better tho? maybe faster or lighter??
- x_coord = a
- z_coord = b
- y_coord = c
- elseif c ~= 0 and d ~= 0 then
- -- could do the same here...
- x_coord = a
- z_coord = b
- y_coord = c
- dir = d
- else
- return false
- end
- return true
- end
- function get(a)
- if a == "x" then
- b = x_coord
- elseif a == "z" then
- b = z_coord
- elseif a == "y" then
- b = y_coord
- elseif a == "dir" then
- b = dir
- else
- return {x_coord,z_coord,y_coord,dir} -- TEST THIS!!!
- -- maybe just return 4 variables instead of list?
- end
- return b
- end
- -- Refuel function (with fuel chest full of lava buckets)
- function refuel()
- print("Starting refueling.")
- turtle.select(fuelSlot)
- if turtle.back() then
- print("-moving back")
- turtle.place()
- turtle.suck()
- turtle.refuel()
- turtle.drop()
- turtle.dig()
- turtle.forward()
- elseif turtle.up() then
- print("-moving up")
- turtle.placeDown()
- turtle.suckDown()
- turtle.refuel()
- turtle.dropDown()
- turtle.digDown()
- turtle.down()
- elseif turtle.down() then
- print("-moving down")
- turtle.placeUp()
- turtle.suckUp()
- turtle.refuel()
- turtle.dropUp()
- turtle.digUp()
- turtle.up()
- else
- print("Couldn't refill! (rogue turn?)")
- end
- turtle.select(dumpSlot)
- print("-Done refueling")
- return true
- end
- -- Check how many slots are not empty
- function checkInv()
- out = 0
- for i=3,16,1 do
- if turtle.getItemCount(i) ~= 0 then
- out = out+1
- end
- end
- return out
- end
- -- Movement function
- function move(direction, length, force)
- -- check fuel and refill if nececary
- if turtle.getFuelLevel() < 10 then
- refuel()
- end
- length = length or 1
- force = force or false
- force = not(force)
- if direction == "up" then
- while not(turtle.up()) do
- print("Trying to move up")
- if force then
- print("-couldn't move.")
- return false
- end
- end
- y_coord = y_coord+1
- elseif direction == "down" then
- while not(turtle.down()) do
- print("Trying to move down")
- if force then
- print("-couldn't move.")
- return false
- end
- end
- y_coord = y_coord-1
- elseif direction == "forw" then
- while not(turtle.forward()) do
- print("Trying to move forward")
- if force then
- print("-couldn't move.")
- return false
- end
- end
- if dir == 1 then
- z_coord = z_coord-1
- elseif dir == 2 then
- x_coord = x_coord+1
- elseif dir == 3 then
- z_coord = z_coord+1
- elseif dir == 4 then
- x_coord = x_coord-1
- end
- elseif direction == "back" then
- while not(turtle.back()) do
- print("Trying to move back")
- if force then
- print("-couldn't move.")
- return false
- end
- end
- if dir == 1 then
- z_coord = z_coord+1
- elseif dir == 2 then
- x_coord = x_coord-1
- elseif dir == 3 then
- z_coord = z_coord-1
- elseif dir == 4 then
- x_coord = x_coord+1
- end
- end
- print("-XYZ:",x_coord,",",z_coord,",",y_coord)
- length = length - 1
- if length>0 then
- move(direction, length)
- end
- return true
- end
- -- Digging function
- function dig(direction, length, attack)
- length = length or 1
- attack = attack or false
- i=length
- while i>0 do
- -- check if inventory is almost full
- -- do that every 64*x
- if (length%64)==0 then
- print("Checking inventory")
- if checkInv()>12 then
- emptyInv()
- end
- end
- if direction == "up" then
- a,b=nil
- a,b = turtle.digUp()
- while not(a) do
- print("Trying to dig up")
- if b ~= nil then
- print("-there's nothing to dig.")
- if attack then
- print("-punching!")
- for i=1,8,1 do
- turtle.attackUp()
- end
- end
- break
- end
- end
- elseif direction == "down" then
- a,b=nil
- a,b = turtle.digDown()
- while not(a) do
- print("Trying to dig down")
- if b ~= nil then
- print("-there's nothing to dig.")
- if attack then
- print("-punching!")
- for i=1,8,1 do
- turtle.attackDown()
- end
- end
- break
- end
- end
- elseif direction == "forw" then
- a,b=nil
- a,b = turtle.dig()
- while not(a) do
- print("Trying to dig")
- if b ~= nil then
- print("-there's nothing to dig.")
- if attack then
- print("-punching!")
- for i=1,8,1 do
- turtle.attack()
- end
- end
- break
- end
- end
- end
- i = i - 1
- end
- return true
- end
- -- Dig + move function = mine function
- function mine(direction, length, attack)
- length = length or 1
- attack = attack or nil
- -- shouldn't be needed but just in case
- -- could just make it turn 2 times
- if direction == "back" then
- print("Invalid direction.")
- return false
- end
- for i=1,length,1 do
- -- check if inventory is almost full
- -- do that every 12*x and first time
- if i==1 or (i%12)==0 then
- print("Checking inventory")
- if checkInv()>12 then
- emptyInv()
- end
- end
- dig(direction,1,attack)
- move(direction)
- end
- return true
- end
- -- Turn functions
- function turn(direction, length)
- length = length or 1
- if direction == "left" then
- print("Turning left")
- turtle.turnLeft()
- dir = ((dir-2)%4)+1
- print("-direction:",directions[dir])
- elseif direction == "right" then
- print("Turning right")
- turtle.turnRight()
- dir = ((dir)%4)+1
- print("-direction:",directions[dir])
- elseif direction == "north" then
- print("Turning to north")
- if dir == 2 then
- turn("left")
- elseif dir == 3 then
- turn("left",2)
- elseif dir == 4 then
- turn("right")
- end
- elseif direction == "east" then
- print("Turning to east")
- if dir == 3 then
- turn("left")
- elseif dir == 4 then
- turn("left",2)
- elseif dir == 1 then
- turn("right")
- end
- elseif direction == "south" then
- print("Turning to south")
- if dir == 4 then
- turn("left")
- elseif dir == 1 then
- turn("left",2)
- elseif dir == 2 then
- turn("right")
- end
- elseif direction == "west" then
- print("Turning to west")
- if dir == 1 then
- turn("left")
- elseif dir == 2 then
- turn("left",2)
- elseif dir == 3 then
- turn("right")
- end
- end
- length = length - 1
- if length>0 then
- turn(direction, length)
- end
- return true
- end
- -- Empty inventory function
- function emptyInv()
- print("Starting inventory dump.")
- turtle.select(dumpSlot)
- if turtle.back() then
- print("-moving back")
- turtle.place()
- for i=3,16 do
- turtle.select(i)
- turtle.drop()
- end
- turtle.select(dumpSlot)
- turtle.dig()
- turtle.forward()
- elseif turtle.up() then
- print("-moving up")
- turtle.placeDown()
- for i=3,16 do
- turtle.select(i)
- turtle.dropDown()
- end
- turtle.select(dumpSlot)
- turtle.digDown()
- turtle.down()
- elseif turtle.down() then
- print("-moving down")
- turtle.placeUp()
- for i=3,16 do
- turtle.select(i)
- turtle.dropUp()
- end
- turtle.select(dumpSlot)
- turtle.digUp()
- turtle.up()
- else
- print("Couldn't dump inventory! (rogue turn?)")
- end
- turtle.select(dumpSlot)
- print("-Done dumping")
- return true
- end
- -- ============
- -- Main program
- -- ============
- -- Check if there is anything in slot 1 and 2
- if turtle.getItemCount(1) == 0 then
- print("WARNING: There is no dump chest!")
- end
- if turtle.getItemCount(2) == 0 then
- print("WARNING: There is no fuel chest!")
- end
- -- /
- -- === === === === ===
- -- END OF LIBRARY
- -- === === === === ===
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement