Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Variables
- args = { ... }
- cost_only = false
- sim_mode = false
- blocks = 0
- fuel = 0
- positionx = 0
- positiony = 0
- positionz = 0
- facing = 0
- activeslot = 1
- --Functions
- function round(num, idp)
- local mult = 10^(idp or 0)
- return math.floor(num * mult + 0.5) / mult
- end
- function printUsage()
- print("Usage: ngon [<Number of Sides> [Side Length] [Height]] [-c, cost] [-h, help]")
- print("Please use numbers for Number of Sides, Side Length, and Height")
- print("Only 6 or 8 may be passed for Number of sides.")
- print("If Height isnt't given, a height of one will be used")
- print("-h or -help: display this help page")
- print("-c or -cost: cost only mode, doesn't move or place blocks")
- end
- function initialize(wrongNumSidesValue)
- if #args > 0 then
- if #args > 5 then
- printUsage()
- return false
- else
- for i,v in ipairs(args) do
- if v == "-c" or v == "-cost" then
- cost_only = true
- print("Cost only mode activated")
- elseif v == "-h" or v == "-help" then
- printUsage()
- return false
- end
- end
- end
- end
- if not wrongNumSidesValue then
- print("Welcome to the N-gon Builder.")
- print()
- end
- if not wrongNumSidesValue then
- if #args >= 3 then
- numSides = tonumber(args[1])
- sideLength = tonumber(args[2])
- height = tonumber(args[3])
- elseif #args >= 2 then
- numSides = tonumber(args[1])
- sideLength = tonumber(args[2])
- height = 1
- elseif #args == 1 then
- numSides = tonumber(args[1])
- print("How long should each side be?")
- sideLength = tonumber(io.read())
- height = 1
- else
- print("How many sides do you want?")
- numSides = tonumber(io.read())
- print("How long should each side be?")
- sideLength = tonumber(io.read())
- height = 1
- end
- end
- if wrongNumSidesValue then
- print("How many sides do you want?")
- numSides = tonumber(io.read())
- print("How long should each side be?")
- sideLength = tonumber(io.read())
- height = 1
- end
- if numSides == 8 or numSides == 6 then
- --all is good in the world
- else
- print("Number of sides must either be 6 or 8.")
- initialize(true)
- end
- insideAngle = ((numSides - 2) * 180) / numSides
- if not wrongNumSidesValue then
- print("Building an " .. numSides .. "-gon, with side legth " .. sideLength)
- print("Each vertex will have an angle of " .. insideAngle .. " degrees, for a total of " .. insideAngle * numSides .. " degrees.")
- return true
- end
- end
- -- From pruby's sdbuild and shape.lua from Keridos
- function checkFuel()
- if (not(tonumber(turtle.getFuelLevel()) == nil)) then
- while turtle.getFuelLevel() < 50 do
- print("Turtle almost out of fuel, pausing. Please drop fuel in inventory. And press enter.")
- io.read()
- turtle.refuel()
- end
- end
- end
- function checkResources()
- while turtle.getItemCount(activeslot) <= 0 do
- if activeslot == 16 then
- print("Turtle is empty, please put building block in slots and press enter to continue")
- io.read()
- activeslot = 1
- turtle.select(activeslot)
- else
- activeslot = activeslot+1
- print("Turtle slot empty, trying slot "..activeslot)
- turtle.select(activeslot)
- end
- os.sleep(0.2)
- end
- end
- function placeBlock()
- -- Cost calculation mode - don't move
- blocks = blocks + 1
- if cost_only then
- return
- end
- if turtle.detectDown() and not turtle.compareDown() then
- turtle.digDown()
- end
- checkResources()
- turtle.placeDown()
- end
- -- Navigation features
- -- allow the turtle to move while tracking its position
- -- this allows us to just give a destination point and have it go there
- function turnRightTrack()
- if cost_only then
- return
- end
- turtle.turnRight()
- facing = facing + 1
- if facing >= 4 then
- facing = 0
- end
- end
- function turnLeftTrack()
- if cost_only then
- return
- end
- turtle.turnLeft()
- facing = facing - 1
- if facing < 0 then
- facing = 3
- end
- end
- function turnAroundTrack()
- turnLeftTrack()
- turnLeftTrack()
- end
- function safeForward()
- fuel = fuel + 1
- if cost_only then
- return
- end
- checkFuel()
- success = false
- while not success do
- success = turtle.forward()
- if not success then
- while turtle.detect() do
- if not turtle.dig() then
- print("Blocked attempting to move forward.")
- print("Please clear and press enter to continue.")
- io.read()
- end
- end
- end
- end
- end
- function safeBack()
- fuel = fuel + 1
- if cost_only then
- return
- end
- checkFuel()
- success = false
- while not success do
- success = turtle.back()
- if not success then
- turnAroundTrack();
- while turtle.detect() do
- if not turtle.dig() then
- break;
- end
- end
- turnAroundTrack()
- success = turtle.back()
- if not success then
- print("Blocked attempting to move back.")
- print("Please clear and press enter to continue.")
- io.read()
- end
- end
- end
- end
- function safeUp()
- fuel = fuel + 1
- if cost_only then
- return
- end
- checkFuel()
- success = false
- while not success do
- success = turtle.up()
- if not success then
- while turtle.detectUp() do
- if not turtle.digUp() then
- print("Blocked attempting to move up.")
- print("Please clear and press enter to continue.")
- io.read()
- end
- end
- end
- end
- end
- function safeDown()
- fuel = fuel + 1
- if cost_only then
- return
- end
- checkFuel()
- success = false
- while not success do
- success = turtle.down()
- if not success then
- while turtle.detectDown() do
- if not turtle.digDown() then
- print("Blocked attempting to move down.")
- print("Please clear and press enter to continue.")
- io.read()
- end
- end
- end
- end
- end
- function moveY(targety)
- if targety == positiony then
- return
- end
- if (facing ~= 0 and facing ~= 2) then -- check axis
- turnRightTrack()
- end
- while targety > positiony do
- if facing == 0 then
- safeForward()
- else
- safeBack()
- end
- positiony = positiony + 1
- end
- while targety < positiony do
- if facing == 2 then
- safeForward()
- else
- safeBack()
- end
- positiony = positiony - 1
- end
- end
- function moveX(targetx)
- if targetx == positionx then
- return
- end
- if (facing ~= 1 and facing ~= 3) then -- check axis
- turnRightTrack()
- end
- while targetx > positionx do
- if facing == 1 then
- safeForward()
- else
- safeBack()
- end
- positionx = positionx + 1
- end
- while targetx < positionx do
- if facing == 3 then
- safeForward()
- else
- safeBack()
- end
- positionx = positionx - 1
- end
- end
- function navigateTo(targetx, targety)
- if facing == 0 or facing == 2 then -- Y axis
- moveY(targety)
- moveX(targetx)
- else
- moveX(targetx)
- moveY(targety)
- end
- end
- function buildHexagon()
- local changex = sideLength / 2
- local changey = round(math.sqrt(3) * changex, 0)
- changex = round(changex, 0)
- local counter = 0
- navigateTo(changex, 0)
- for currentSide = 1, 6 do
- counter = 0
- if currentSide == 1 then
- for placed = 1, sideLength do
- navigateTo(positionx + 1, positiony)
- placeBlock()
- end
- elseif currentSide == 2 then
- navigateTo(positionx, positiony + 1)
- while positiony <= changey do
- if counter == 0 or counter == 2 or counter == 4 then
- navigateTo(positionx + 1, positiony)
- end
- placeBlock()
- navigateTo(positionx, positiony + 1)
- counter = counter + 1
- if counter == 5 then
- counter = 0
- end
- end
- elseif currentSide == 3 then
- while positiony <= (2 * changey) do
- if counter == 0 or counter == 2 or counter == 4 then
- navigateTo(positionx - 1, positiony)
- end
- placeBlock()
- navigateTo(positionx, positiony + 1)
- counter = counter + 1
- if counter == 5 then
- counter = 0
- end
- end
- elseif currentSide == 4 then
- for placed = 1, sideLength do
- navigateTo(positionx - 1, positiony)
- placeBlock()
- end
- elseif currentSide == 5 then
- navigateTo(positionx, positiony - 1)
- while positiony >= changey do
- if counter == 0 or counter == 2 or counter == 4 then
- navigateTo(positionx - 1, positiony)
- end
- placeBlock()
- navigateTo(positionx, positiony - 1)
- counter = counter + 1
- if counter == 5 then
- counter = 0
- end
- end
- elseif currentSide == 6 then
- while positiony >= 0 do
- if counter == 0 or counter == 2 or counter == 4 then
- navigateTo(positionx + 1, positiony)
- end
- placeBlock()
- navigateTo(positionx, positiony - 1)
- counter = counter + 1
- if counter == 5 then
- counter = 0
- end
- end
- end
- end
- navigateTo(0, 0)
- while facing ~= 0 do
- turnLeftTrack()
- end
- end
- function buildOctagon()
- local sideLength2 = sideLength - 1
- local change = round(sideLength2 / math.sqrt(2), 0)
- navigateTo(change, 0)
- for currentSide = 1, 8 do
- if currentSide == 1 then
- for placed = 1, sideLength2 do
- navigateTo(positionx + 1, positiony)
- placeBlock()
- end
- elseif currentSide == 2 then
- for placed = 1, change do
- navigateTo(positionx + 1, positiony + 1)
- placeBlock()
- end
- elseif currentSide == 3 then
- for placed = 1, sideLength2 do
- navigateTo(positionx, positiony + 1)
- placeBlock()
- end
- elseif currentSide == 4 then
- for placed = 1, change do
- navigateTo(positionx - 1, positiony + 1)
- placeBlock()
- end
- elseif currentSide == 5 then
- for placed = 1, sideLength2 do
- navigateTo(positionx - 1, positiony)
- placeBlock()
- end
- elseif currentSide == 6 then
- for placed = 1, change do
- navigateTo(positionx - 1, positiony - 1)
- placeBlock()
- end
- elseif currentSide == 7 then
- for placed = 1, sideLength2 do
- navigateTo(positionx, positiony - 1)
- placeBlock()
- end
- elseif currentSide == 8 then
- for placed = 1, change do
- navigateTo(positionx + 1, positiony - 1)
- placeBlock()
- end
- end
- end
- navigateTo(0, 0)
- while facing ~= 0 do
- turnLeftTrack()
- end
- end
- function hexPrism()
- for z = 1, height do
- buildHexagon()
- safeUp()
- end
- end
- function octPrism()
- for z = 1, height do
- buildOctagon()
- safeUp()
- end
- end
- --Main Code
- if not initialize(false) then
- --If help was shown close program and do nothing else
- return
- end
- if numSides == 6 then
- hexPrism()
- elseif numSides == 8 then
- octPrism()
- end
Advertisement
Add Comment
Please, Sign In to add comment