Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ----------------
- --DECLARATIONS--
- ----------------
- --Declare circle building variables
- local maxCircleRow = 0
- local maxCircleColumn = 0
- --Declare the empty main table to store the circle image data
- local ImageArray = {}
- -------------
- --FUNCTIONS--
- -------------
- ----------------------
- --COSMETIC FUNCTIONS--
- ----------------------
- function Pause()
- print("Press any key to continue...")
- read()
- end
- function Done()
- print("Construction complete!")
- end
- function ClearScreen()
- term.clear()
- term.setCursorPos(1,1)
- errMsgPathShown = false
- errMsgBlocksShown = false
- errMsgFuelShown = false
- end
- function Restart()
- me = shell.getRunningProgram()
- shell.run(me)
- end
- ------------------
- --MATH FUNCTIONS--
- ------------------
- function Even(num)
- if ( num % 2 ) == 0 then
- return true
- else
- return false
- end
- end
- function Round(num)
- return math.ceil(num)
- end
- -------------------------
- --ERROR CHECK FUNCTIONS--
- -------------------------
- --Make sure that the entered value is an integer
- function TypeCheck(number)
- number = tonumber(number)
- if type(number) ~= "number" then
- return false
- else
- return true
- end
- end
- function ErrorMsgFuel()
- if errMsgFuelShown == true then
- --do nothing
- else
- print("Out of fuel. Please add some!")
- errMsgFuelShown = true
- end
- os.sleep(1)
- end
- function ErrorMsgBlocks()
- if errMsgBlocksShown == true then
- --do nothing
- else
- print("Out of blocks. Please add more!")
- errMsgBlocksShown = true
- end
- os.sleep(1)
- end
- function ErrorMsgPath()
- if errMsgPathShown == true then
- --do nothing
- else
- print("The path is blocked. Please move!")
- errMsgPathShown = true
- end
- os.sleep(1)
- end
- -------------------------------
- --BUILDING/MOVEMENT FUNCTIONS--
- -------------------------------
- --Automatic refueling
- function ReFuel()
- if turtle.getFuelLevel() < 1 then
- ErrorMsgFuel()
- fuelSlot = 16
- while turtle.refuel() == false do
- turtle.select(fuelSlot)
- if fuelSlot == 1 then
- fuelSlot = 16
- else
- fuelSlot = fuelSlot - 1
- end
- os.sleep(0.2)
- end
- end
- end
- function PlaceBlock()
- --Check which slot has blocks in it and select it
- for i = 1, 16 do
- if turtle.getItemCount(i) > 0 then
- turtle.select(i)
- break
- end
- if i == 16 then
- ErrorMsgBlocks()
- end
- end
- --Check if there is a block below, if there is then ignore placing any
- if turtle.detectDown() == true then
- --Continue
- else
- --Try to place block until it succeeds
- while turtle.placeDown() == false do
- os.sleep(1)
- end
- end
- end
- function DestroyBlock()
- turtle.digDown()
- end
- function goForward()
- ReFuel()
- --Try to go forward until it succeeds
- while turtle.forward() == false do
- ErrorMsgPath()
- end
- PlaceBlock()
- end
- function goForwardDestroy()
- while turtle.forward() == false do
- ErrorMsgPath()
- end
- DestroyBlock()
- end
- function goForwardNoPlace()
- ReFuel()
- --Try to go forward until it succeeds
- while turtle.forward() == false do
- ErrorMsgPath()
- end
- end
- function turnRight()
- ReFuel()
- turtle.turnRight()
- end
- function turnLeft()
- ReFuel()
- turtle.turnLeft()
- end
- function goUp()
- ReFuel()
- while turtle.up() == false do
- --Try to go up until it succeeds
- ErrorMsgPath()
- end
- PlaceBlock()
- end
- function goUpNoPlace()
- ReFuel()
- while turtle.up() == false do
- --Try to go up until it succeeds
- ErrorMsgPath()
- end
- end
- function goDownNoPlace()
- ReFuel()
- while turtle.down() == false do
- --Try to go down until it succeeds
- ErrorMsgPath()
- end
- end
- function fillDownward()
- dZ = 0
- while turtle.detectDown() == false do
- goDownNoPlace()
- dZ = dZ + 1
- end
- for j = 1, dZ do
- goUp()
- end
- dZ = 0
- end
- ---------------------------------
- --HORISONTAL/MOVEMENT FUNCTIONS--
- ---------------------------------
- function HzPlaceBlock()
- --Check which slot has blocks in it and select it
- for i = 1, 16 do
- if turtle.getItemCount(i) > 0 then
- turtle.select(i)
- break
- end
- if i == 16 then
- ErrorMsgBlocks()
- end
- end
- --Try to place block until it succeeds
- while turtle.place() == false do
- os.sleep(1)
- end
- end
- function goBack()
- ReFuel()
- while turtle.back() == false do
- --Try to go back until it succeeds
- ErrorMsgPath()
- end
- HzPlaceBlock()
- end
- --------------------
- --CIRCLE FUNCTIONS--
- --------------------
- function DeclareCircle(diameter)
- --Declare the size of the table storing the circle, this is nessesary in lua
- for row = 0,diameter do
- ImageArray[row] = {}
- maxCircleRow = row - 1
- for column = 0,diameter do
- ImageArray[column] = {}
- maxCircleColumn = column - 1
- end
- end
- end
- function DefineCircle(r)
- --convert variable to integer
- r = tonumber(r)
- --box dimensions
- local bx = 1
- local by = 1
- --centered on block
- local cx = 1
- local cy = 1
- if cx ~= 0 then
- maxblocks_x = math.ceil((r - bx / 2) / bx) * 2 + 1
- else
- maxblocks_x = math.ceil(r / bx) * 2
- end
- if cy ~= 0 then
- maxblocks_y = math.ceil((r - by / 2) / by) * 2 + 1
- else
- maxblocks_y = math.ceil(r / by) * 2
- end
- countRow = 0
- for y = -maxblocks_y / 2, maxblocks_y / 2 do
- countCol = 0
- for x = -maxblocks_x / 2, maxblocks_x / 2 do
- distance = math.sqrt(math.pow(y * by, 2) + math.pow(x * bx, 2))
- if distance > r then
- block = 0
- else
- block = 1
- end
- --write(" r"..countRow.." c"..countCol.." b"..block.." ") --debug print rows and columns to check success
- ImageArray[countRow][countCol] = block
- countCol = countCol + 1
- end
- countRow = countRow + 1
- end
- end
- function PrintCircle()
- --print(maxCircleRow.." "..maxCircleColumn) --debug print size of declared circle
- for i = 0, maxCircleRow do
- for j = 0, maxCircleColumn do
- write(ImageArray[i][j])
- end
- print("")
- end
- end
- function PrintPartCircle()
- for i = 15, 30 do
- for j = 0, 38 do
- write(ImageArray[i][j])
- end
- print("")
- end
- end
- function GetCircleCoord(loc_row,loc_col)
- return ImageArray[loc_row][loc_col]
- end
- function HollowOutCircle()
- --middle of the circle
- for i = Round(maxCircleRow/5), maxCircleRow-Round(maxCircleRow/5) do
- Count = 0
- for j = 0, maxCircleColumn-1, 1 do
- --hollow out the circle
- if i == 1 or i == maxCircleRow-1 then
- --skip the first and last row
- else
- if ImageArray[i][j] == 1 then
- if Count == 1 then
- if ImageArray[i][j+1] == 0 then
- break
- else
- ImageArray[i][j] = 0
- end
- else
- Count = 1 + Count
- end
- end
- end
- end
- end
- --bottom of the circle
- for i = maxCircleRow-Round(maxCircleRow/5), maxCircleRow, 1 do
- Count = 0
- for j = 0, maxCircleColumn-1, 1 do
- --hollow out the circle
- if i == 1 or i == maxCircleRow-1 then
- --skip the first and last row
- else
- if ImageArray[i][j] == 1 and ImageArray[i+1][j] == 1 then
- --if Count == 1 then
- if ImageArray[i][j+1] == 0 then
- break
- else
- ImageArray[i][j] = 0
- end
- --else
- -- Count = 1 + Count
- --end
- end
- end
- end
- end
- --top of the circle
- for i = maxCircleRow-Round(maxCircleRow/5), 0, -1 do
- Count = 0
- for j = 0, maxCircleColumn-1, 1 do
- --hollow out the circle
- if i == 1 or i == maxCircleRow-1 then
- --skip the first and last row
- else
- if ImageArray[i][j] == 1 and ImageArray[i-1][j] == 1 then
- --if Count == 1 then
- if ImageArray[i][j+1] == 0 then
- break
- else
- ImageArray[i][j] = 0
- end
- --else
- Count = 1 + Count
- --end
- end
- end
- end
- end
- end
- function BuildCircle()
- for i = 0, maxCircleRow do
- for j = 0, maxCircleColumn-1, 1 do
- --if Even(j) == true then j = j + 1 end
- --if Even(j) == false then j = j + 1 end
- if ImageArray[i][j] == 1 then
- --print("j="..j.." i="..i)
- --PlaceBlock()
- fillDownward()
- goForwardNoPlace()
- else
- goForwardNoPlace()
- end
- end
- if Even(i) == false then
- --print("uneven row")
- turnLeft()
- goForwardNoPlace()
- turnLeft()
- else
- --print("even row")
- turnRight()
- goForwardNoPlace()
- turnRight()
- end
- end
- end
- function HzBuildCircle(Z)
- for i = 0, maxCircleRow do
- for j = 0, maxCircleColumn-1, 1 do
- --if Even(j) == true then j = j + 1 end
- --if Even(j) == false then j = j + 1 end
- if ImageArray[i][j] == 1 then
- --print("j="..j.." i="..i)
- --PlaceBlock()
- for i = 1, Z do
- goForwardNoPlace()
- end
- for i = 1, Z do
- goBack()
- end
- if Even(i) == false then
- --print("uneven row")
- goUpNoPlace()
- else
- --print("even row")
- goDownNoPlace()
- end
- else
- if Even(i) == false then
- --print("uneven row")
- goUpNoPlace()
- else
- --print("even row")
- goDownNoPlace()
- end
- end
- end
- --next row
- turnRight()
- goForwardNoPlace()
- turnLeft()
- end
- end
- function CirclePreview()
- --Cosmetic preview
- ClearScreen()
- print("Circle preview...")
- print("")
- print("Please note that this will look ugly")
- print("if the circle is larger than 18x18!")
- print("")
- Pause()
- ClearScreen()
- PrintCircle()
- Pause()
- ClearScreen()
- end
- ---------------
- --DESCRIPTION--
- ---------------
- while true do
- ClearScreen()
- print("Construction Turtle by Cragrim")
- print("==============================")
- print("Current fuel level is: "..turtle.getFuelLevel())
- print("What do you wish to build?")
- print("p: Plane/floor")
- print("f: Fence")
- print("w: Wall")
- print("s: Solid (fill a hole)")
- print("h: House (a hollow box)")
- print("c: Circle")
- print("a: antifloor")
- write("Command> ")
- local buildChoice = read()
- if buildChoice == "a" then
- -----------------
- --ANTIFLOOR BUILDER--
- -----------------
- ClearScreen()
- print("CTC - Antifloor builder")
- print("==============================")
- print("Please enter how many blocks in a grid")
- print("forward and to the right you wish to destroy.")
- print("")
- --START--
- write("How many blocks forward?: ")
- local Y = read()
- write("How many blocks to the right?: ")
- local X = read()
- if (TypeCheck(Y) == false) or (TypeCheck(X) == false) then
- print("You have not entered a valid number!")
- else
- ClearScreen()
- print("Beginning to destroy a plane... ")
- print(Y.." blocks forward and ")
- print(X.." blocks to the right.")
- DestroyBlock()
- --Build floor
- while X ~= 0 do
- for i = 2, Y do
- goForwardDestroy()
- end
- X = X - 1
- if X == 0 then break
- end
- turnRight()
- goForwardDestroy()
- turnRight()
- for i = 2, Y do
- goForwardDestroy()
- end
- X = X - 1
- if X == 0 then break
- end
- turnLeft()
- goForwardDestroy()
- turnLeft()
- end
- Done()
- end
- Pause()
- elseif buildChoice == "p" then
- -----------------
- --PLANE BUILDER--
- -----------------
- ClearScreen()
- print("CTC - Plane/floor builder")
- print("==============================")
- print("Please enter how many blocks in a grid")
- print("forward and to the right you wish to build.")
- print("")
- --START--
- write("How many blocks forward?: ")
- local Y = read()
- write("How many blocks to the right?: ")
- local X = read()
- if (TypeCheck(Y) == false) or (TypeCheck(X) == false) then
- print("You have not entered a valid number!")
- else
- ClearScreen()
- print("Beginning to build a plane... ")
- print(Y.." blocks forward and ")
- print(X.." blocks to the right.")
- PlaceBlock()
- --Build floor
- while X ~= 0 do
- for i = 2, Y do
- goForward()
- end
- X = X - 1
- if X == 0 then break
- end
- turnRight()
- goForward()
- turnRight()
- for i = 2, Y do
- goForward()
- end
- X = X - 1
- if X == 0 then break
- end
- turnLeft()
- goForward()
- turnLeft()
- end
- Done()
- end
- Pause()
- elseif buildChoice == "f" then
- -----------------
- --FENCE BUILDER--
- -----------------
- ClearScreen()
- print("CTC - Fence builder")
- print("==============================")
- print("Please enter how many blocks in a grid")
- print("forward, to the right and which height")
- print("you wish to build.")
- print("")
- write("How many blocks forward?: ")
- local Y = read()
- write("How many blocks to the right?: ")
- local X = read()
- write("How many blocks high?: ")
- local Z = read()
- if (TypeCheck(Y) == false) or (TypeCheck(X) == false) or (TypeCheck(Z) == false) then
- print("You have not entered a valid number!")
- else
- print(Y.." blocks forward and ")
- print(X.." blocks to the right.")
- print(Z.." blocks high.")
- PlaceBlock()
- --Build fence
- --First column, static Z
- for i = 1, Z do
- goUp()
- end
- for i = 2, Y do
- goForwardNoPlace()
- fillDownward()
- end
- turnRight()
- for i = 2, X do
- goForwardNoPlace()
- fillDownward()
- end
- turnRight()
- for i = 2, Y do
- goForwardNoPlace()
- fillDownward()
- end
- turnRight()
- for i = 3, X do
- goForwardNoPlace()
- fillDownward()
- end
- Done()
- end
- Pause()
- elseif buildChoice == "c" then
- ------------------
- --CIRCLE BUILDER--
- ------------------
- ClearScreen()
- print("CTC - Circle builder")
- print("==============================")
- print("Please enter which type of circle that")
- print("you wish to build.")
- print("f: filled")
- print("h: hollow")
- print("vf: vertical filled")
- print("vh: vertical hollow")
- print("")
- write("Circle type?: ")
- local buildCChoice = read()
- write("Radius in blocks?: ")
- local cmdRadius = read()
- write("How many blocks high/deep?: ")
- local Z = read()
- --Check that input is integers
- if (TypeCheck(cmdRadius) == false) or (TypeCheck(Z) == false) then
- print("You have not entered a valid number!")
- else
- --Declare the circle table to twice the radius
- DeclareCircle(cmdRadius*2+2)
- --Define the circle inside the table
- DefineCircle(cmdRadius)
- --Circle type
- if buildCChoice == "f" then
- CirclePreview()
- print("Constructing filled circle...")
- print(cmdRadius.." blocks in radius and ")
- print(Z.." blocks high.")
- --Go up to specified height
- for i = 1, Z do
- goUpNoPlace()
- end
- BuildCircle()
- --Return down specified height
- for i = 1, Z do
- goDownNoPlace()
- end
- Done()
- elseif buildCChoice == "h" then
- HollowOutCircle()
- CirclePreview()
- print("Constructing hollow circle...")
- print(cmdRadius.." blocks in radius and ")
- print(Z.." blocks high.")
- --Go up to specified height
- for i = 1, Z do
- goUpNoPlace()
- end
- BuildCircle()
- --Return down specified height
- for i = 1, Z do
- goDownNoPlace()
- end
- Done()
- elseif buildCChoice == "vh" then
- write("How high up?: ")
- local Zh = read()
- --Check that input is integers
- if (TypeCheck(Zh) == false) then
- print("You have not entered a valid number!")
- else
- HollowOutCircle()
- CirclePreview()
- print("Constructing vertical hollow circle...")
- print(cmdRadius.." blocks in radius and ")
- print(Z.." blocks deep.")
- print(Zh.." blocks up.")
- --Go up in the air
- for i = 1, Zh do
- goUpNoPlace()
- end
- HzBuildCircle(Z)
- --Return down
- for i = 1, Zh do
- goDownNoPlace()
- end
- Done()
- end
- elseif buildCChoice == "vf" then
- write("How high up?: ")
- local Zh = read()
- --Check that input is integers
- if (TypeCheck(Zh) == false) then
- print("You have not entered a valid number!")
- else
- CirclePreview()
- print("Constructing vertical filled circle...")
- print(cmdRadius.." blocks in radius and ")
- print(Z.." blocks deep.")
- print(Zh.." blocks up.")
- --Go up in the air
- for i = 1, Zh do
- goUpNoPlace()
- end
- HzBuildCircle(Z)
- --Return down
- for i = 1, Zh do
- goDownNoPlace()
- end
- Done()
- end
- else
- ClearScreen()
- print("Incorrect choice or not implemented.")
- print("Please try again!")
- os.sleep(1)
- end
- end
- Pause()
- elseif buildChoice == "w" then
- print("Not implemented yet, sorry!")
- os.sleep(1)
- elseif buildChoice == "s" then
- -----------------
- --SOLID BUILDER--
- -----------------
- ClearScreen()
- print("CTC - Solid (filler) builder")
- print("==============================")
- print("Please enter how many blocks in a grid")
- print("forward and to the right you wish to build.")
- print("")
- --START--
- write("How many blocks forward?: ")
- local Y = read()
- write("How many blocks to the right?: ")
- local X = read()
- write("How many blocks up?: ")
- local Z = read()
- if (TypeCheck(Y) == false) or (TypeCheck(X) == false) or (TypeCheck(Z) == false) then
- print("You have not entered a valid number!")
- else
- ClearScreen()
- print("Beginning to build a plane... ")
- print(Y.." blocks forward and ")
- print(X.." blocks to the right.")
- print(Z.." blocks up in the air.")
- for i = 1, Z do
- goUp()
- end
- PlaceBlock()
- --Build floor
- while X ~= 0 do
- for i = 2, Y do
- goForwardNoPlace()
- fillDownward()
- end
- X = X - 1
- if X == 0 then break
- end
- turnRight()
- goForwardNoPlace()
- fillDownward()
- turnRight()
- for i = 2, Y do
- goForwardNoPlace()
- fillDownward()
- end
- X = X - 1
- if X == 0 then break
- end
- turnLeft()
- goForwardNoPlace()
- fillDownward()
- turnLeft()
- end
- Done()
- end
- Pause()
- elseif buildChoice == "h" then
- print("Not implemented yetm, sorry!")
- os.sleep(1)
- elseif buildChoice == "exit" then
- ClearScreen()
- break
- else
- ClearScreen()
- print("Incorrect choice, please try again!")
- os.sleep(1)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment