Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if posx == nil then posx = 1 end
- if posy == nil then posy = 0 end
- if north == nil then north = 1 end
- if east == nil then east = 2 end
- if south == nil then south = 3 end
- if west == nil then west = 4 end
- if crashcount == nil then crashcount = 0 end
- if fieldx == nil then fieldx = 9 end
- if fieldy == nil then fieldy = 9 end
- if wait == nil then wait = 4200 end
- if waitafter == nil then waitafter = true end
- function refuel()
- if turtle.getFuelLevel() < 324 then
- turtle.select(15)
- turtle.refuel()
- turtle.select(1)
- end
- end
- if direction == nil then direction = 0 end
- function turn(newdirection)
- if newdirection==direction then
- return true
- else
- while not (direction==newdirection) do
- if newdirection > direction then
- turtle.turnLeft((newdirection - direction))
- direction = direction + 1
- else turtle.turnRight((newdirection - direction))
- direction = direction + 1
- end
- end
- end
- end
- function coord()
- print("Position: "..posx.." / "..posy)
- end
- function iposx()
- posx = posx + 1
- end
- function iposy()
- posy = posy + 1
- end
- function dposx()
- posx = posx - 1
- end
- function dposy()
- posy = posy - 1
- end
- function mine()
- print("doing stuff here")
- turtle.select(1)
- turtle.placeDown()
- turtle.digDown()
- for i=14,9,-1 do
- turtle.select(i)
- turtle.placeDown()
- end
- turtle.select(1)
- end
- function move(dx, dy)
- --coord()
- retr = nil
- if dy >= posy then
- retr = (moveDirY(posy, dy) and moveDirX(posx, dx))
- else
- retr = (moveDirX(posx, dx) and moveDirY(posy, dy))
- end
- --print("move() :: "..tostring(retr).." ::")
- return retr
- --coord()
- end
- function moveDirX(ax, dx)
- if ax==dx then return (not detect()) end
- if ax == 0 then return step(dx,0) else
- delta = dx - ax
- --print("dX: "..tostring(delta))
- return step(delta, 0)
- end
- end
- function moveDirY(ay, dy)
- if (ay==dy) then return (not detect())
- else
- if ay == 0 then return step(dy, 1)
- else
- delta = dy - ay
- --print("dY: "..tostring(delta))
- return step(delta, 1)
- end
- end
- end
- function betrag(zahl1)
- if zahl1 < 0 then return zahl1 * -1
- else return zahl1
- end
- end
- function step(count, axis)
- refuel()
- retr = nil
- if axis == 0 then
- if count < 0 then
- retr = moveLeft(betrag(count))
- else
- retr = moveRight(betrag(count))
- end
- else
- if count > 0 then
- retr = moveForward(betrag(count))
- else
- retr = moveBack(betrag(count))
- end
- end
- --print("step() :: "..tostring(retr).." ::")
- --sleep(3)
- return retr
- end
- function problem()
- if turtle.getFuelLevel() < (fieldx*fieldy) and turtle.getItemCount(16) == 0 then
- print("not enough fuel. Homing!")
- return true
- else
- return false
- end
- end
- function detect()
- if turtle.detect() then
- crashcount = crashcount + 1
- return true
- else
- return false
- end
- end
- function moveRight(ci)
- if problem() then return false end
- refuel()
- turtle.turnRight(1)
- for i=1, ci do
- print("Moving right. New: "..(posx+1).." / "..posy)
- if not detect() and turtle.forward() then
- iposx()
- else
- return false
- end
- end
- turtle.turnLeft(1)
- return true
- end
- function moveLeft(ci)
- if problem() then return false end
- refuel()
- turtle.turnLeft(1)
- for i=1, ci do
- print("Moving left. New: "..(posx-1).." / "..posy)
- if not detect() and turtle.forward() then
- dposx()
- else
- return false
- end
- end
- turtle.turnRight(1)
- return true
- end
- function moveForward(ci)
- if problem() then return false end
- refuel()
- for i=1, ci do
- print("Moving forward. New: "..posx.." / "..(posy+1))
- if not detect() and turtle.forward() then
- iposy()
- else return false
- end
- end
- return true
- end
- function moveBack(ci)
- if problem() then return false end
- refuel()
- turn180()
- for i=1, ci do
- print("Moving back. New: "..posx.." / "..(posy-1))
- if not detect() and turtle.forward() then
- dposy()
- else return false
- end
- end
- turn180()
- return true
- end
- function turn180()
- turtle.turnRight()
- turtle.turnRight()
- end
- function moveup(uct)
- for i=1, uct do
- turtle.up(1)
- end
- end
- function movedown(dct)
- for i=1, dct do
- turtle.down(1)
- end
- end
- function homing()
- print("Assuming crash. Homing...")
- turtle.select(15)
- while not turtle.compareDown() do
- if not turtle.forward(1) then
- turtle.turnLeft(1)
- end
- end
- turtle.turnLeft(1)
- turtle.forward(1)
- turtle.turnLeft(1)
- posx=1
- posy=0
- direction = 0
- crashcount= -1
- print("Sending -true- to caller")
- return true
- end
- function unlocked()
- if turtle.getItemCount(15) > 0 then
- return true
- else
- return false
- end
- end
- function empty()
- turtle.turnLeft(1)
- turtle.forward()
- turtle.select(15)
- if turtle.compareDown() then
- for i=14,9,-1 do
- turtle.dropDown()
- end
- else return false
- end
- turtle.back()
- turtle.select(1)
- turtle.turnRight(1)
- end
- function restock()
- turtle.turnRight(1)
- turtle.forward()
- turtle.down(1)
- turtle.select(15)
- if turtle.compareDown() then
- turtle.select(16)
- turtle.suckDown()
- turtle.select(1)
- turtle.up()
- turtle.back()
- turtle.turnLeft(1)
- return true
- else return false
- end
- end
- function loop1()
- print("Initializing Harvester GPS alpha 1.101")
- sleep(3)
- while unlocked() do
- for f=1, 8 do
- for i=1, fieldx do
- for j=1, fieldy do
- --if crashcount > 4 then homing() end
- if not ((i==5) and (j==5)) then
- --move(i,j)
- if not move(i,j) then return false end
- mine()
- else
- if not move(i,j) then return false end
- end
- end
- refuel()
- end
- move(1,0)
- empty()
- moveup(4)
- end
- return true
- end
- end
- while true do
- if not waitafter then
- print("Sleep before farming...")
- else
- print("\\")
- print("Getting shit done.")
- end
- if not waitafter then
- sleep(wait)
- end
- while(loop1()) do end
- while not homing() do
- if not unlocked() then
- return false
- end
- end
- print("Homing done.")
- print("Restarting Harvester...")
- empty()
- restock()
- if waitafter and (crashcount >= 0) then
- print("Sleeping after...") sleep(wait)
- else print("sleep is for the poor." )
- crashcount = 0
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement