Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --File: /cattech/moves.lua
- local lists = dofile("/cattech/lists.lua")
- -- Positions at start of script.
- local moves = {isDigNotFail = true}
- -- f Forward, h Height, s Sideways
- moves.position = { direction = 0, f = 0, h = 0, s = 0 }
- -- ------------------------------------------------------------------------------------------------------------------------
- -- ------------------------------------------------------------------------------------------------------------------------
- -- Simple movement commands
- -- ------------------------------------------------------------------------------------------------------------------------
- -- ------------------------------------------------------------------------------------------------------------------------
- function moves.digUp()
- while (turtle.digUp()) do
- sleep(0.5)
- end
- end
- function moves.digDown()
- while (turtle.digDown()) do
- sleep(0.5)
- end
- end
- function moves.digForward()
- while (turtle.dig()) do
- sleep(0.5)
- end
- end
- function moves.turnLeft()
- if (turtle.turnLeft()) then
- moves.position.direction=(moves.position.direction-1) % 4
- return true
- end
- return false
- end
- function moves.turnRight()
- if (turtle.turnRight()) then
- moves.position.direction=(moves.position.direction+1) % 4
- return true
- end
- return false
- end
- function moves.backward()
- if (moves.isDigNotFail) then
- moves.turnLeft()
- moves.turnLeft()
- moves.digForward()
- local success = moves.forward()
- if success then
- moves._recordMoveFwd(-1)
- end
- moves.turnRight()
- moves.turnRight()
- return success
- else
- local success = turtle.back()
- if success then
- moves._recordMoveFwd(-1)
- end
- return success
- end
- end
- function moves.forward()
- if (moves.isDigNotFail) then
- moves.digForward()
- end
- local success = turtle.forward()
- if success then
- moves._recordMoveFwd(1)
- end
- return success
- end
- function moves.up()
- if (moves.isDigNotFail) then
- moves.digUp()
- end
- local success = turtle.up()
- if success then
- moves._recordMoveUD(1)
- end
- return success
- end
- function moves.down()
- if (moves.isDigNotFail) then
- moves.digDown()
- end
- local success = turtle.down()
- if success then
- moves._recordMoveUD(-1)
- end
- return success
- end
- function moves.face(direction)
- local goal=0
- --if direction == "forward" then
- -- goal=0
- --end
- if direction == "right" then
- goal=1
- end
- if direction == "left" then
- goal=2
- end
- if direction == "backward" then
- goal=3
- end
- -- moves.position = { direction = 0, f = 0, h = 0, s = 0 }
- local dir = math.abs(moves.position.direction - goal)
- while moves.position.direction ~= goal do
- if (dir == -1) then
- moves.turnRight()
- else
- moves.turnLeft()
- end
- end
- end
- function moves.secureDown()
- local success,blockData = turtle.inspectDown()
- if isLiquid(blockData) or (not success) then
- local placeItem = selectTurtleItemByList(lists.secureBlocks)
- if placeItem ~= nil then
- turtle.placeDown(placeItem)
- return true
- end
- end
- return false
- end
- function moves.secureUp()
- local success,blockData = turtle.inspectUp()
- if isLiquid(blockData) then
- local placeItem = selectTurtleItemByList(lists.secureBlocks)
- if placeItem ~= nil then
- turtle.placeUp(placeItem)
- return true
- end
- end
- return false
- end
- function moves.secureForward()
- local success,blockData = turtle.inspect()
- if isLiquid(blockData) then
- local placeItem = selectTurtleItemByList(lists.secureBlocks)
- if placeItem ~= nil then
- turtle.place(placeItem)
- return true
- end
- end
- return false
- end
- -- ------------------------------------------------------------------------------------------------------------------------
- -- ------------------------------------------------------------------------------------------------------------------------
- -- Internal routines
- -- ------------------------------------------------------------------------------------------------------------------------
- -- ------------------------------------------------------------------------------------------------------------------------
- function moves._recordMoveFwd(delta)
- if moves.direction== 0 then
- moves.position.f = moves.position.f + delta
- elseif moves.direction == 1 then
- moves.position.s = moves.position.s + delta
- elseif moves.direction == 2 then
- moves.position.f = moves.position.f - delta
- elseif moves.direction == 3 then
- moves.position.s = moves.position.s - delta
- end
- end
- function moves._recordMoveUD(delta)
- moves.position.h = moves.position.h + delta
- end
- -- ------------------------------------------------------------------------------------------------------------------------
- -- ------------------------------------------------------------------------------------------------------------------------
- -- Complex movement functionality.
- -- ------------------------------------------------------------------------------------------------------------------------
- -- ------------------------------------------------------------------------------------------------------------------------
- -- Starting from the current position, carve out the right and left side, and move up, repeating to maxHeight steps up, then return.
- function moves.digTunnelSlice(maxHeight)
- for i=1,maxHeight do
- moves.turnRight()
- moves.digForward()
- moves.turnLeft()
- moves.turnLeft()
- moves.digForward()
- moves.turnRight()
- if i < maxHeight then
- moves.up()
- end
- end
- -- Ends up on the new floor/stair
- for i=1,maxHeight -1 do
- moves.down()
- end
- end
- function moves.secureTunnelSlice(maxHeight)
- local isUnsecure = scanForNeedToSecure()
- log("debug","chk isUnsecure :" .. tostring(isUnsecure))
- if (isUnsecure == 0) then
- return(isUnsecure)
- end
- moves.secureDown()
- moves.turnRight()
- moves.forward()
- moves.secureDown()
- moves.secureForward()
- for i=1,maxHeight-1 do
- moves.up()
- moves.secureForward()
- end
- moves.turnLeft()
- moves.turnLeft()
- moves.secureUp()
- for i=1,2 do
- moves.forward()
- moves.secureUp()
- end
- moves.secureForward()
- for i=1,maxHeight-1 do
- moves.down()
- moves.secureForward()
- end
- moves.secureDown()
- moves.turnRight()
- moves.turnRight()
- moves.forward()
- moves.secureDown()
- moves.turnLeft()
- end
- function moves.placeLight(upBlocks)
- local success = false
- local placeItem = selectTurtleItemByList(lists.placableLights)
- if (placeItem ~= nil) then
- for i = 1, upBlocks do
- moves.up()
- end
- moves.turnRight()
- moves.digForward()
- local placeThis = {placeItem.name}
- success = placeItemByList(placeThis)
- moves.turnLeft()
- for i = 1, upBlocks do
- moves.down()
- end
- end
- return success
- end
- -- ------------------------------------------------------------------------------------------------------------------------
- -- ------------------------------------------------------------------------------------------------------------------------
- return moves
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement