Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local tArgs = { ... }
- if #tArgs ~= 3 then
- print( "Usage: dig_roomV2 <Width><Length><0/1>" )
- return
- end
- local goTo
- local checkFuel
- local chackInv
- local width = tonumber( tArgs[1] )
- local length = tonumber( tArgs[2] )
- local DIRECT = tonumber( tArgs[3] )
- if width < 1 then
- print( "X size must be positive" )
- return
- end
- if length < 1 then
- print( "Depth must be positive" )
- return
- end
- if DIRECT ~= 0 and DIRECT ~= 1 then
- print( "Direct left = 0, right = 1" )
- return
- end
- local xPos = 0
- local yPos = 0
- local xDir = 1
- local yDir = 0
- local function checkInv()
- if turtle.getItemCount(16) > 0 then
- buf_x, buf_y, buf_xDir, buf_yDir = xPos, yPos, xDir, yDir
- goTo(0, 0, -1, 0)
- dropItems()
- goTo(buf_x, buf_y, buf_xDir, buf_yDir)
- return true
- end
- return false
- end
- local function checkFuel()
- if turtle.getFuelLevel() == 0 then
- print("Low fuel, wait fuel...")
- while turtle.getFuelLevel() == 0 do
- turtle.refuel()
- sleep(1)
- end
- end
- end
- local function dig(noCenter)
- while turtle.detectUp() do turtle.digUp() end
- checkInv()
- while turtle.detect() and not noCenter do turtle.dig() end
- checkInv()
- turtle.digDown()
- checkInv()
- end
- local function forward()
- if not turtle.forward() then
- checkFuel()
- turtle.attack()
- return false
- end
- xPos = xPos + xDir
- yPos = yPos + yDir
- -- print(xPos, yPos, xDir, yDir)
- return true
- end
- local function turnRight()
- if not turtle.turnRight() then
- checkFuel()
- turtle.attack()
- return false
- end
- xDir, yDir = -yDir, xDir
- return true
- end
- local function turnLeft()
- if not turtle.turnLeft() then
- checkFuel()
- turtle.attack()
- return false
- end
- xDir, yDir = yDir, -xDir
- return true
- end
- function fastTurn(xd, yd)
- local turnsLeft = 0
- local turnsRight = 0
- local buf_xDir = xDir
- local buf_yDir = yDir
- while buf_xDir ~= xd or buf_yDir ~= yd do
- buf_xDir, buf_yDir = buf_yDir, -buf_xDir
- turnsLeft = turnsLeft + 1
- end
- buf_xDir = xDir
- buf_yDir = yDir
- while buf_xDir ~= xd or buf_yDir ~= yd do
- buf_xDir, buf_yDir = -buf_yDir, buf_xDir
- turnsRight = turnsRight + 1
- end
- if turnsRight < turnsLeft then
- return true
- else
- return false
- end
- end
- function goTo(x, y, xd, yd)
- if x > xPos then
- turn = fastTurn(1, 0)
- while xDir ~= 1 do
- if turn then
- turnRight()
- else
- turnLeft()
- end
- end
- while xPos ~= x do
- forward()
- end
- --------------------------------
- if y > yPos then
- turn = fastTurn(0, 1)
- while yDir ~= 1 do
- if turn then
- turnRight()
- else
- turnLeft()
- end
- end
- while yPos ~= y do
- forward()
- end
- else
- turn = fastTurn(0, -1)
- while yDir ~= -1 do
- if turn then
- turnRight()
- else
- turnLeft()
- end
- end
- while yPos ~= y do
- forward()
- end
- end
- else
- if y > yPos then
- turn = fastTurn(0, 1)
- while yDir ~= 1 do
- if turn then
- turnRight()
- else
- turnLeft()
- end
- end
- while yPos ~= y do
- forward()
- end
- else
- turn = fastTurn(0, -1)
- while yDir ~= -1 do
- if turn then
- turnRight()
- else
- turnLeft()
- end
- end
- while yPos ~= y do
- forward()
- end
- end
- --------------------------------
- turn = fastTurn(-1, 0)
- while xDir ~= -1 do
- if turn then
- turnRight()
- else
- turnLeft()
- end
- end
- while xPos ~= x do
- forward()
- end
- end
- --------------------------------
- turn = fastTurn(xd, yd)
- while xDir ~= xd or yDir ~= yd do
- if turn then
- turnRight()
- else
- turnLeft()
- end
- end
- end
- function dropItems()
- local i = 1
- while i <= 16 do
- turtle.select(i)
- turtle.drop()
- i = i + 1
- end
- turtle.select(1)
- return true
- end
- Inc = 0
- turnEd = false
- while Inc < length do
- Jinc = 0
- while Jinc < width-1 do
- -- print(Inc, Jinc)
- dig()
- forward()
- Jinc = Jinc + 1
- end
- Inc = Inc + 1
- if Inc == length then
- dig(true)
- break
- end
- if not turnEd then
- if DIRECT == 0 then
- turnLeft()
- dig()
- forward()
- turnLeft()
- else
- turnRight()
- dig()
- forward()
- turnRight()
- end
- turnEd = true
- else
- if DIRECT ~= 0 then
- turnLeft()
- dig()
- forward()
- turnLeft()
- else
- turnRight()
- dig()
- forward()
- turnRight()
- end
- turnEd = false
- end
- end
- goTo(0,0,-1,0)
- dropItems()
- turnRight()
- turnRight()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement