Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Script for Computercraft
- -- Script to make a platform for the player to build on
- -- Input Parameters:
- -- 1. Length of the platform (y-axis)
- -- 2. Width of the platform (x-axis)
- -- 3. Depth of the platform (z-axis)
- -- There are internal variables tracking the Coordinates and the Orientation. To ensure the Coordinates dont get messed up, there will be custom functions for moving and rotating the turtle that will be used everywhere in the Proram.
- -- From the orignal Facing: forward = positive y, right: positive x, down: negative z
- -- --> The Platform will span over the internal coordinates x: 0 to Length-1, y: 0 to Width-1, z: -1 to -Depth
- -- The rotation will be represented by the numbers 0,1,2,3 --> 0: original rotation, 1: right, 2: 180d, 3: left
- --[[
- Needed Functions:
- function "checkBlocks":
- First the Turtle will check if there are still Blocks in selected Slot to be placed.
- - If There are: return.
- - If there are not: change to the next slot and check again. Repeat this until either all except for the last slot were Checked or a slot with Items was found.
- - If A slot with Items was found: Keep the selection on it and return.
- - If no Slot with items was found: select Slot 1 and call the function "getNewBlocks". After That return.
- function "getNewBlocks":
- variables in this scope will cache the coordinates the Turtle started this function from.
- -> in Beginning: x_hold = x, y_hold = y, z_hold = z, rot_hold = rot
- The turtle will go to the coordinates x=0,y=-1,z=0 with the rotation 2 using the function "GoToPosition"
- ->GoToPosition(0,-1,0,2)
- The Turtle will pick blocks from the chest to fill the inventory up (except for the last slot of the inventory (It should stay empty)).
- ->turtle.select(1), suck, turtle.select(2), suck, .... , turtle.select(15), suck, turtle,select(1), return
- After that the Turtle will go to the Coordinates it started from and rotate to the original position.
- -> GoToPosition(x_hold,y_hold,z_hold,rot_hold)
- the function will return so that the turtle can complete its job.
- function "GoToPosition"(targetX, targetY, targetZ, targetRot):
- togoX = targetX-x
- togoY = targetY-y
- togoZ = targetZ-z
- while togoZ !=0:
- if togoZ < 0:
- customDown
- elseif togoZ > 0:
- customUp
- togoZ = targetZ-z
- while togoX != 0:
- local desiredRot
- if togoX > 0: desiredRot = 1
- if togoX < 0: desiredRot = 3
- rotateTo(desiredRot)
- customForward
- togoX = targetX-x
- while togoY != 0:
- local desiredRot
- if togoY > 0: desiredRot = 0
- if togoY < 0: desiredRot = 2
- rotateTo(desiredRot)
- customForward
- togoY = targetY-y
- rotateTo(targetRot)
- return
- function "rotateTo"(desiredRot):
- toRot = desiredRot - rotation
- if toRot == 3: customLeft
- while rotation != desiredRotation:
- customRight
- custom Moving/Rotation:
- 1. customForward:
- turtle.forwards()
- if rotation = 0: y+=1
- if rotation = 1: x+=1
- if rotation = 2: y-=1
- if rotation = 3: x-=1
- customBackwards:
- turtle.backwards()
- if rotation = 0: y-=1
- if rotation = 1: x-=1
- if rotation = 2: y+=1
- if rotation = 3: x+=1
- customUp:
- turtle.up()
- z+=1
- customDown:
- turtle.down()
- z-=1
- customRight:
- turtle.turnRight()
- if rotation >= 3: rotation = 0
- else: rotation +=1
- customLeft:
- turtle.turnLeft()
- if rotation <= 0: rotation = 3
- else: rotation -=1
- Outline:
- 1. turtle will check if it has enough Fuel for the job (return error and exit if not)
- - For checking the amount of fuel needed assume the turtle will need to walk 4 * Depth * length * Width blocks.
- 2. Turtle takes one step forward and sets the internal coordinates and Roation to x=0,y=0,z=0,rotation=0
- 3. Turtle calls function "getNewBlocks" to fill up most of the inventory
- 4. Turtle will start its job
- (After every placed block: the Turtle will call the function "checkBlocks")
- While 0 <= x <= Width-1:
- While 0 <= y <= Length-1:
- While 0 >= z >= -Depth+1:
- selectLastSlot(=slot 16)
- turtle.DigDown
- customDown
- throw away item from selected (last) slot (if there is one now)
- While 0 >= z >= -Depth+1:
- checkBlocks
- customUp
- turtle.PlaceDown
- if y >= Length-1 and rotation == 0:
- customRight
- customForward
- customRight
- elseif y <= 0 and rotation == 2
- customLeft
- customForward
- customLeft
- else:
- customForward
- 5. Turtle will return to original Position (GoToPosition(0,-1,0,0))
- ]]
- -- Initialize needed Variables:
- local x = 0
- local y = 0
- local z = 0
- local rotation = 0
- local length = 0
- local width = 0
- local depth = 0
- local fuelNeeded = 0
- --getting the input parameters:
- local args = {...}
- if #args ~= 3 then
- print("Usage: turtleMakePlatform <length> <width> <depth>")
- return
- end
- length = tonumber(args[1])
- width = tonumber(args[2])
- depth = tonumber(args[3])
- if length <= 0 or width <= 0 or depth <= 0 then
- print("Length, Width and Depth must be positive numbers.")
- return
- end
- print("Creating a platform with dimensions: \nLength = " .. length .. ", Width = " .. width .. ", Depth = " .. depth)
- --[[
- custom Moving/Rotation:
- 1. customForward:
- turtle.forwards()
- if rotation = 0: y+=1
- if rotation = 1: x+=1
- if rotation = 2: y-=1
- if rotation = 3: x-=1
- customBackwards:
- turtle.backwards()
- if rotation = 0: y-=1
- if rotation = 1: x-=1
- if rotation = 2: y+=1
- if rotation = 3: x+=1
- customUp:
- turtle.up()
- z+=1
- customDown:
- turtle.down()
- z-=1
- customRight:
- turtle.turnRight()
- if rotation >= 3: rotation = 0
- else: rotation +=1
- customLeft:
- turtle.turnLeft()
- if rotation <= 0: rotation = 3
- else: rotation -=1
- ]]
- function customForward()
- -- print("Moving forward")
- --check if the turtle can move forward
- if not turtle.forward() then
- print("Cannot move forward")
- return false
- end
- if rotation == 0 then
- y = y + 1
- elseif rotation == 1 then
- x = x + 1
- elseif rotation == 2 then
- y = y - 1
- elseif rotation == 3 then
- x = x - 1
- end
- end
- function customBackwards()
- --print("Moving backwards")
- --check if the turtle can move backwards
- if not turtle.back() then
- print("Cannot move backwards")
- return false
- end
- if rotation == 0 then
- y = y - 1
- elseif rotation == 1 then
- x = x - 1
- elseif rotation == 2 then
- y = y + 1
- elseif rotation == 3 then
- x = x + 1
- end
- end
- function customUp()
- -- print("Moving up")
- --check if the turtle can move up
- if not turtle.up() then
- print("Cannot move up")
- return false
- end
- z = z + 1
- end
- function customDown()
- -- print("Moving down")
- --check if the turtle can move down
- if not turtle.down() then
- print("Cannot move down")
- return false
- end
- z = z - 1
- end
- function customRight()
- turtle.turnRight()
- rotation = (rotation + 1) % 4
- end
- function customLeft()
- turtle.turnLeft()
- rotation = (rotation - 1) % 4
- end
- --[[
- function "rotateTo"(desiredRot):
- toRot = desiredRot - rotation
- if toRot == 3: customLeft
- while rotation != desiredRotation:
- customRight
- ]]
- function rotateTo(desiredRot)
- local toRot = (desiredRot - rotation) % 4
- if toRot == 3 then
- customLeft()
- end
- while rotation ~= desiredRot do
- customRight()
- end
- end
- --[[
- function "GoToPosition"(targetX, targetY, targetZ, targetRot):
- togoX = targetX-x
- togoY = targetY-y
- togoZ = targetZ-z
- while togoZ !=0:
- if togoZ < 0:
- customDown
- elseif togoZ > 0:
- customUp
- togoZ = targetZ-z
- while togoX != 0:
- local desiredRot
- if togoX > 0: desiredRot = 1
- if togoX < 0: desiredRot = 3
- rotateTo(desiredRot)
- customForward
- togoX = targetX-x
- while togoY != 0:
- local desiredRot
- if togoY > 0: desiredRot = 0
- if togoY < 0: desiredRot = 2
- rotateTo(desiredRot)
- customForward
- togoY = targetY-y
- rotateTo(targetRot)
- return
- ]]
- function goToPosition(targetX, targetY, targetZ, targetRot)
- print("Going to position: " .. targetX .. ", " .. targetY .. ", " .. targetZ .. ", " .. targetRot)
- local togoX = targetX - x
- local togoY = targetY - y
- local togoZ = targetZ - z
- while togoZ ~= 0 do
- if togoZ < 0 then
- customDown()
- elseif togoZ > 0 then
- customUp()
- end
- togoZ = targetZ - z
- end
- while togoX ~= 0 do
- local desiredRot = (togoX > 0) and 1 or 3
- rotateTo(desiredRot)
- customForward()
- togoX = targetX - x
- end
- while togoY ~= 0 do
- local desiredRot = (togoY > 0) and 0 or 2
- rotateTo(desiredRot)
- customForward()
- togoY = targetY - y
- end
- rotateTo(targetRot)
- end
- -- goToPosition but with the difference that it goes in the opposite order -> first y, then x, then z
- function goToPositionBack(targetX, targetY, targetZ, targetRot)
- print("Going back to position: " .. targetX .. ", " .. targetY .. ", " .. targetZ .. ", " .. targetRot)
- local togoX = targetX - x
- local togoY = targetY - y
- local togoZ = targetZ - z
- print("togoY: " .. togoY)
- while togoY ~= 0 do
- local desiredRot = (togoY > 0) and 0 or 2
- print("desiredRot: " .. desiredRot)
- rotateTo(desiredRot)
- customForward()
- togoY = targetY - y
- end
- print("togoX: " .. togoX)
- while togoX ~= 0 do
- local desiredRot = (togoX > 0) and 1 or 3
- print("desiredRot: " .. desiredRot)
- rotateTo(desiredRot)
- customForward()
- togoX = targetX - x
- end
- print("togoZ: " .. togoZ)
- while togoZ ~= 0 do
- if togoZ < 0 then
- customDown()
- elseif togoZ > 0 then
- customUp()
- end
- togoZ = targetZ - z
- end
- rotateTo(targetRot)
- end
- --[[
- function "getNewBlocks":
- variables in this scope will cache the coordinates the Turtle started this function from.
- -> in Beginning: x_hold = x, y_hold = y, z_hold = z, rot_hold = rot
- The turtle will go to the coordinates x=0,y=-1,z=0 with the rotation 2 using the function "GoToPosition"
- ->GoToPosition(0,-1,0,2)
- The Turtle will pick blocks from the chest to fill the inventory up (except for the last slot of the inventory (It should stay empty)).
- ->turtle.select(1), suck, turtle.select(2), suck, .... , turtle.select(15), suck, turtle,select(1), return
- After that the Turtle will go to the Coordinates it started from and rotate to the original position.
- -> GoToPosition(x_hold,y_hold,z_hold,rot_hold)
- the function will return so that the turtle can complete its job.
- ]]
- function getNewBlocks()
- print("Getting new blocks...")
- local x_hold = x
- local y_hold = y
- local z_hold = z
- local rot_hold = rotation
- goToPosition(0, -1, 0, 2)
- for i = 1, 15 do
- turtle.select(i)
- turtle.suck()
- end
- turtle.select(1)
- goToPositionBack(x_hold, y_hold, z_hold, rot_hold)
- end
- --[[
- function "checkBlocks":
- First the Turtle will check if there are still Blocks in selected Slot to be placed.
- - If There are: return.
- - If there are not: change to the next slot and check again. Repeat this until either all except for the last slot were Checked or a slot with Items was found.
- - If A slot with Items was found: Keep the selection on it and return.
- - If no Slot with items was found: select Slot 1 and call the function "getNewBlocks". After That return.
- ]]
- function checkBlocks()
- -- If current slot has blocks, we're good
- if turtle.getItemCount() > 0 then
- return
- end
- -- Look for a slot with blocks
- for i = 1, 15 do
- turtle.select(i)
- if turtle.getItemCount() > 0 then
- return
- end
- end
- -- If we got here, no blocks were found
- turtle.select(1)
- getNewBlocks()
- end
- --[[
- 1. turtle will check if it has enough Fuel for the job (return error and exit if not)
- - For checking the amount of fuel needed assume the turtle will need to walk 4 * Depth * length * Width blocks.
- ]]
- function checkFuel()
- fuelNeeded = 4 * depth * length * width
- if turtle.getFuelLevel() < fuelNeeded then
- print("Not enough fuel. Please refuel.")
- return false
- end
- return true
- end
- if not checkFuel() then
- return
- end
- -- 2. Turtle takes one step forward and sets the internal coordinates and Roation to x=0,y=0,z=0,rotation=0
- function initializeTurtle()
- customForward()
- x = 0
- y = 0
- z = 0
- rotation = 0
- end
- initializeTurtle()
- -- 3. Turtle calls function "getNewBlocks" to fill up most of the inventory
- getNewBlocks()
- --[[4. Turtle will start its job
- (After every placed block: the Turtle will call the function "checkBlocks")
- While 0 <= x <= Width-1:
- While 0 <= y <= Length-1:
- While 0 >= z >= -Depth+1:
- selectLastSlot(=slot 16)
- turtle.DigDown
- customDown
- throw away item from selected (last) slot (if there is one now)
- While 0 >= z >= -Depth+1:
- checkBlocks
- customUp
- turtle.PlaceDown
- if y >= Length-1 and rotation == 0:
- customRight
- customForward
- customRight
- elseif y <= 0 and rotation == 2
- customLeft
- customForward
- customLeft
- else:
- customForward
- ]]
- function makePlatform()
- print("Making platform...")
- while y >= 0 and y <= length-1 and x >= 0 and x <= width-1 do
- print("y= " .. y)
- -- Dig down to the required depth
- while z <= 0 and z >= -depth+1 do
- turtle.select(16) -- Select the last slot (slot 16)
- turtle.digDown() -- Dig down
- customDown() -- Move down (this updates z)
- turtle.drop() -- Drop the item in the last slot (if there is one now)
- end
- -- Place blocks while coming back up
- while z < 0 and z >= -depth do
- turtle.select(16) -- Select the last slot (slot 16)
- turtle.digUp() -- Dig up (if there is a block)
- checkBlocks()
- customUp() -- Move up (this updates z)
- turtle.placeDown() -- Place block down
- end
- if y >= length - 1 and rotation == 0 then
- customRight()
- customForward()
- customRight()
- elseif y <= 0 and rotation == 2 then
- customLeft()
- customForward()
- customLeft()
- else
- customForward()
- end
- end
- end
- makePlatform()
- -- 5. Turtle will return to original Position (GoToPosition(0,-1,0,0))
- goToPosition(0, -1, 0, 2)
- -- put all Items into the chest (unless the chest is full)
- for i = 1, 15 do
- turtle.select(i)
- turtle.drop()
- end
- turtle.select(1)
- customRight()
- customRight()
- -- notification that the job is done
- print("Platform created successfully!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement