Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- belts
- -- Lays down conveyor belts in a rectangular pattern
- -- All belts face the forward direction
- -- Written by HarvDad, June 2014
- args = {...}
- nArgs = #args
- version = "belts: Rev 0.1"
- instructions = "Place stack(s) of conveyor belts starting in first slot."
- usage = "Usage: belts <length> <width>"
- x = 0
- y = 0
- z = 0
- face = 0
- -- The following 'face' directions are relative to the starting position of the turtle in this program
- north = 0
- west = 1
- south = 2
- east = 3
- goForward = 0
- goBackward = 1
- direction = goForward
- currentSlot = 1
- function setFace(f)
- if f == 0 then
- if face == 0 then return end
- if face == 1 then right() return end
- if face == 2 then right() right() return end
- if face == 3 then left() return end
- end
- if f == 1 then
- if face == 0 then left() return end
- if face == 1 then return end
- if face == 2 then right() return end
- if face == 3 then right() right() return end
- end
- if f == 2 then
- if face == 0 then left() left() return end
- if face == 1 then left() return end
- if face == 2 then return end
- if face == 3 then right() return end
- end
- if f == 3 then
- if face == 0 then right() return end
- if face == 1 then left() left() return end
- if face == 2 then left() return end
- if face == 3 then return end
- end
- end
- function left()
- if face == 0 then face = 1 turtle.turnLeft() return end
- if face == 1 then face = 2 turtle.turnLeft() return end
- if face == 2 then face = 3 turtle.turnLeft() return end
- if face == 3 then face = 0 turtle.turnLeft() return end
- print("function left\(\): Bad face value: ", face)
- end
- function right()
- if face == 0 then face = 3 turtle.turnRight() return end
- if face == 1 then face = 0 turtle.turnRight() return end
- if face == 2 then face = 1 turtle.turnRight() return end
- if face == 3 then face = 2 turtle.turnRight() return end
- print("function right\(\): Bad face value: ", face)
- end
- function up()
- while turtle.detectUp() do -- This loop added in case of falling sand or whatever
- turtle.digUp()
- end
- turtle.up()
- y = y+1
- end
- function rise(nBlocks)
- local i
- for i=1,nBlocks do
- up()
- end
- end
- function down()
- if turtle.detectDown() then
- turtle.digDown()
- end
- turtle.down()
- y = y-1
- end
- function moveForward(nBlocks)
- for i = 1, nBlocks do
- turtle.forward()
- if face == north then
- z = z + 1
- elseif face == south then
- z = z - 1
- elseif face == east then
- x = x + 1
- elseif face == west then
- x = x - 1
- end
- end
- end
- function moveBackward(nBlocks)
- for i = 1, nBlocks do
- turtle.back()
- if face == north then
- z = z - 1
- elseif face == south then
- z = z + 1
- elseif face == east then
- x = x - 1
- elseif face == west then
- x = x + 1
- end
- end
- end
- function home(targetY)
- -- print("home:face ", face, ", x = ", x, ", z = ", z)
- if x < 0 then
- setFace(east)
- while x < 0 do
- moveForward(1)
- end
- else
- if x > 0 then
- setFace(west)
- while x > 0 do
- moveForward(1)
- end
- end
- end
- if z < 0 then
- setFace(north)
- while z < 0 do
- moveForward(1)
- end
- else
- if z > 0 then
- setFace(south)
- while z > 0 do
- moveForward(1)
- end
- end
- end
- if y > 0 then
- while y > 0 do
- down()
- end
- end
- setFace(0)
- end
- function layBeltRow(direction, length)
- for j=1,length do
- if turtle.getItemCount(currentSlot) < 1 then
- currentSlot = currentSlot + 1
- if turtle.getItemCount(currentSlot) < 1 then
- abort("No more belts in inventory.")
- end
- turtle.select(currentSlot)
- end
- turtle.placeDown()
- if j < length then
- if direction == goForward then
- moveForward(1)
- else
- moveBackward(1)
- end
- end
- end
- end
- rowHeading = west
- function goToNextRow()
- currentFace = face
- if face == north or face == south then
- setFace(rowHeading)
- moveForward(1)
- setFace(currentFace)
- else
- setFace(rowHeading)
- moveForward(1)
- setFace(currentFace)
- end
- end
- function reverseDirection()
- if direction == goForward then
- direction = goBackward
- else
- direction = goForward
- end
- end
- function layBeltSection(length, width)
- for i=1,width do
- layBeltRow(direction, length)
- if i < width then
- goToNextRow()
- reverseDirection()
- end
- end
- end
- if nArgs == 0 or (nArgs == 1 and args[1]== "help") then
- print(version)
- print(instructions)
- print(usage)
- return
- end
- if nArgs < 2 or nArgs > 3 then
- print(usage)
- return
- end
- length = tonumber(args[1])
- if length == nil then
- print("\"", args[1], "\" is not a valid length")
- return
- end
- if length < 1 then
- print("length must be a positive number greater than zero")
- end
- width = tonumber(args[2])
- if width == nil then
- print("\"", args[2], "\" is not a valid width")
- return
- end
- if width < 1 then
- print("width must be a positive number greater than zero")
- end
- layBeltSection(length, width)
- home()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement