Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- os.loadAPI("file")
- [["move" API]]
- fuelSlot = 0
- defaultSlot = 0
- minFuel = 0
- x = 0
- y = 0
- z = 0
- dirx = 0
- dirz = 0
- function register(slot, default, fuel, facex, facez)
- if not start() then
- fuelSlot = slot
- defaultSlot = default
- minFuel = fuel
- dirx = facex
- dirz = facez
- end
- end
- function start()
- vars = file.read("movement")
- if vars == {} then
- return false
- end
- fuelSlot = vars["fuelSlot"]
- defaultSlot = vars["defaultSlot"]
- minFuel = vars["minFuel"]
- x = vars["x"]
- y = vars["y"]
- z = vars["z"]
- dirx = vars["dirx"]
- dirz = vars["dirz"]
- return true
- end
- function write()
- file.write("movement", {["fuelSlot"]=fuelSlot,
- ["defaultSlot"]=defaultSlot,
- ["minFuel"]=minFuel,
- ["x"]=x,
- ["y"]=y,
- ["z"]=z,
- ["dirx"]=dirx,
- ["dirz"]=dirz})
- end
- function move_(direction)
- if direction=="forward" then
- x += dirx
- z += dirz
- elseif direction=="back" then
- x -= dirx
- z -= dirz
- elseif direction=="up" then
- y += 1
- elseif direction=="down" then
- y -= 1
- else
- failed("MOVEDIR")
- end
- write()
- end
- function turn(direction)
- if direction=="right" then
- if dirx == 0 then
- dirx = dirz*-1
- dirz = 0
- else
- dirz = dirx
- dirx = 0
- end
- elseif direction=="left" then
- if dirx == 0 then
- dirx = dirz
- dirz = 0
- else
- dirz = dirx*-1
- dirx = 0
- end
- else
- failed("TURNDIR")
- end
- write()
- end
- function refuel()
- if turtle.getFuelLevel() < minFuel then
- turtle.select(fuelSlot)
- if not turtle.refuel(1) then
- failed("FUEL")
- end
- turtle.select(defaultSlot)
- end
- end
- function failed(message)
- print("Failed with error : " .. message)
- write()
- shell.exit()
- end
- function dig()
- if not turtle.dig() then
- failed("DIG")
- end
- end
- function digUp()
- if not turtle.digUp() then
- failed("DIGUP")
- end
- end
- function digDown()
- if not turtle.digDown() then
- failed("DIGDN")
- end
- end
- function rt()
- turtle.turnRight()
- turn("right")
- end
- function lt()
- turtle.turnLeft()
- turn("left")
- end
- function fwd()
- refuel()
- while not turtle.forward() do
- dig()
- end
- move_("forward")
- end
- function back()
- refuel()
- while not turtle.back() do
- rt()
- rt()
- dig()
- rt()
- rt()
- end
- move_("back")
- end
- function up()
- refuel()
- while not turtle.up() do
- digUp()
- end
- move_("up")
- end
- function down()
- refuel()
- while not turtle.down() do
- digDown()
- end
- move_("down")
- end
- function seekdir(seek, dir):
- if dir=="x" then
- if dirx ~= seek then
- if dirz == seek then
- lt()
- elseif dirx == seek*-1 then
- rt()
- rt()
- else
- rt()
- end
- end
- elseif dir=="z" then
- if dirz ~= seekz then
- if dirx == seekz then
- rt()
- elseif dirz == seekz*-1 then
- lt()
- lt()
- else
- lt()
- end
- end
- else
- failed("SEEKDIR")
- end
- function seekcoords(seekx, seeky, seekz, seekdirx, seekdirz)
- toxd = x - seekx
- tox = toxd / math.abs(toxd)
- toyd = y - seeky
- toy = toyd / math.abs(toyd)
- tozd = z - seekz
- toz = tozd / math.abs(tozd)
- seekdir(tox, "x")
- for i=1,toxd do
- fwd()
- end
- seekdir(toz, "z")
- for i=1,tozd do
- fwd()
- end
- seekdir(seekdirx, "x")
- seekdir(seekdirz, "z")
- for i=1,toyd do
- if toy > 0 then
- up()
- elseif toy < 0 then
- down()
- end
- end
- end
- function getx()
- return x
- end
- function gety()
- return y
- end
- function getz()
- return z
- end
- function getfacex()
- return dirx
- end
- function getfacez()
- return dirz
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement