happydude11209

Ngon Builder stand alone

Apr 7th, 2013
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.25 KB | None | 0 0
  1. --Variables
  2. args = { ... }
  3.  
  4. cost_only = false
  5. sim_mode = false
  6.  
  7. blocks = 0
  8. fuel = 0
  9.  
  10. positionx = 0
  11. positiony = 0
  12. positionz = 0
  13. facing = 0
  14.  
  15. activeslot = 1
  16.  
  17. --Functions
  18. function round(num, idp)
  19.   local mult = 10^(idp or 0)
  20.   return math.floor(num * mult + 0.5) / mult
  21. end
  22.  
  23. function printUsage()
  24.     print("Usage: ngon [<Number of Sides> [Side Length] [Height]] [-c, cost] [-h, help]")
  25.     print("Please use numbers for Number of Sides, Side Length, and Height")
  26.     print("Only 6 or 8 may be passed for Number of sides.")
  27.     print("If Height isnt't given, a height of one will be used")
  28.     print("-h or -help: display this help page")
  29.     print("-c or -cost: cost only mode, doesn't move or place blocks")
  30. end
  31.  
  32. function initialize(wrongNumSidesValue)
  33.     if #args > 0 then
  34.         if #args > 5 then
  35.             printUsage()
  36.             return false
  37.         else
  38.             for i,v in ipairs(args) do
  39.                 if v == "-c" or v == "-cost" then
  40.                     cost_only = true
  41.                     print("Cost only mode activated")
  42.                 elseif v == "-h" or v == "-help" then
  43.                     printUsage()
  44.                     return false
  45.                 end
  46.             end
  47.         end
  48.     end
  49.    
  50.     if not wrongNumSidesValue then
  51.         print("Welcome to the N-gon Builder.")
  52.         print()
  53.     end
  54.  
  55.     if not wrongNumSidesValue then
  56.         if #args >= 3 then
  57.             numSides = tonumber(args[1])
  58.            
  59.             sideLength = tonumber(args[2])
  60.            
  61.             height = tonumber(args[3])
  62.         elseif #args >= 2 then
  63.             numSides = tonumber(args[1])
  64.            
  65.             sideLength = tonumber(args[2])
  66.            
  67.             height = 1
  68.         elseif #args == 1 then
  69.             numSides = tonumber(args[1])
  70.            
  71.             print("How long should each side be?")
  72.             sideLength = tonumber(io.read())
  73.            
  74.             height = 1
  75.         else
  76.             print("How many sides do you want?")
  77.             numSides = tonumber(io.read())
  78.  
  79.             print("How long should each side be?")
  80.             sideLength = tonumber(io.read())
  81.            
  82.             height = 1
  83.         end
  84.     end
  85.    
  86.     if wrongNumSidesValue then
  87.         print("How many sides do you want?")
  88.         numSides = tonumber(io.read())
  89.  
  90.         print("How long should each side be?")
  91.         sideLength = tonumber(io.read())
  92.        
  93.         height = 1
  94.     end
  95.    
  96.     if numSides == 8 or numSides == 6  then
  97.         --all is good in the world
  98.     else
  99.         print("Number of sides must either be 6 or 8.")
  100.         initialize(true)
  101.     end
  102.  
  103.     insideAngle = ((numSides - 2) * 180) / numSides
  104.  
  105.     if not wrongNumSidesValue then
  106.         print("Building an " .. numSides .. "-gon, with side legth " .. sideLength)
  107.         print("Each vertex will have an angle of " .. insideAngle .. " degrees, for a total of " .. insideAngle * numSides .. " degrees.")
  108.         return true
  109.     end
  110. end
  111.  
  112. -- From pruby's sdbuild and shape.lua from Keridos
  113. function checkFuel()
  114.     if (not(tonumber(turtle.getFuelLevel()) == nil)) then
  115.         while turtle.getFuelLevel() < 50 do
  116.             print("Turtle almost out of fuel, pausing. Please drop fuel in inventory. And press enter.")
  117.             io.read()
  118.             turtle.refuel()
  119.         end
  120.     end
  121. end
  122.  
  123. function checkResources()
  124.     while turtle.getItemCount(activeslot) <= 0 do
  125.         if activeslot == 16 then
  126.             print("Turtle is empty, please put building block in slots and press enter to continue")
  127.             io.read()
  128.             activeslot = 1
  129.             turtle.select(activeslot)
  130.         else
  131.             activeslot = activeslot+1
  132.             print("Turtle slot empty, trying slot "..activeslot)
  133.             turtle.select(activeslot)
  134.         end
  135.         os.sleep(0.2)
  136.     end
  137. end
  138.  
  139. function placeBlock()
  140.     -- Cost calculation mode - don't move
  141.     blocks = blocks + 1
  142.     if cost_only then
  143.         return
  144.     end
  145.     if turtle.detectDown() and not turtle.compareDown() then
  146.         turtle.digDown()
  147.     end
  148.     checkResources()
  149.     turtle.placeDown()
  150. end
  151.  
  152. -- Navigation features
  153. -- allow the turtle to move while tracking its position
  154. -- this allows us to just give a destination point and have it go there
  155.  
  156. function turnRightTrack()
  157.     if cost_only then
  158.         return
  159.     end
  160.     turtle.turnRight()
  161.     facing = facing + 1
  162.     if facing >= 4 then
  163.         facing = 0
  164.     end
  165. end
  166.  
  167. function turnLeftTrack()
  168.     if cost_only then
  169.         return
  170.     end
  171.     turtle.turnLeft()
  172.     facing = facing - 1
  173.     if facing < 0 then
  174.         facing = 3
  175.     end
  176. end
  177.  
  178. function turnAroundTrack()
  179.     turnLeftTrack()
  180.     turnLeftTrack()
  181. end
  182.  
  183. function safeForward()
  184.     fuel = fuel + 1
  185.     if cost_only then
  186.         return
  187.     end
  188.     checkFuel()
  189.     success = false
  190.     while not success do
  191.         success = turtle.forward()
  192.         if not success then
  193.             while turtle.detect() do
  194.                 if not turtle.dig() then
  195.                     print("Blocked attempting to move forward.")
  196.                     print("Please clear and press enter to continue.")
  197.                     io.read()
  198.                 end
  199.             end
  200.         end
  201.     end
  202. end
  203.  
  204. function safeBack()
  205.     fuel = fuel + 1
  206.     if cost_only then
  207.         return
  208.     end
  209.     checkFuel()
  210.     success = false
  211.     while not success do
  212.         success = turtle.back()
  213.         if not success then
  214.             turnAroundTrack();
  215.             while turtle.detect() do
  216.                 if not turtle.dig() then
  217.                     break;
  218.                 end
  219.             end
  220.             turnAroundTrack()
  221.             success = turtle.back()
  222.             if not success then
  223.                 print("Blocked attempting to move back.")
  224.                 print("Please clear and press enter to continue.")
  225.                 io.read()
  226.             end
  227.         end
  228.     end
  229. end
  230.  
  231. function safeUp()
  232.     fuel = fuel + 1
  233.     if cost_only then
  234.         return
  235.     end
  236.     checkFuel()
  237.     success = false
  238.     while not success do
  239.         success = turtle.up()
  240.         if not success then
  241.             while turtle.detectUp() do
  242.                 if not turtle.digUp() then
  243.                     print("Blocked attempting to move up.")
  244.                     print("Please clear and press enter to continue.")
  245.                     io.read()
  246.                 end
  247.             end
  248.         end
  249.     end
  250. end
  251.  
  252. function safeDown()
  253.     fuel = fuel + 1
  254.     if cost_only then
  255.         return
  256.     end
  257.     checkFuel()
  258.     success = false
  259.     while not success do
  260.         success = turtle.down()
  261.         if not success then
  262.             while turtle.detectDown() do
  263.                 if not turtle.digDown() then
  264.                     print("Blocked attempting to move down.")
  265.                     print("Please clear and press enter to continue.")
  266.                     io.read()
  267.                 end
  268.             end
  269.         end
  270.     end
  271. end
  272.  
  273. function moveY(targety)
  274.     if targety == positiony then
  275.         return
  276.     end
  277.     if (facing ~= 0 and facing ~= 2) then -- check axis
  278.         turnRightTrack()
  279.     end
  280.     while targety > positiony do
  281.         if facing == 0 then
  282.             safeForward()
  283.         else
  284.             safeBack()
  285.         end
  286.         positiony = positiony + 1
  287.     end
  288.     while targety < positiony do
  289.         if facing == 2 then
  290.             safeForward()
  291.         else
  292.             safeBack()
  293.         end
  294.         positiony = positiony - 1
  295.     end
  296. end
  297.  
  298. function moveX(targetx)
  299.     if targetx == positionx then
  300.         return
  301.     end
  302.     if (facing ~= 1 and facing ~= 3) then -- check axis
  303.         turnRightTrack()
  304.     end
  305.     while targetx > positionx do
  306.         if facing == 1 then
  307.             safeForward()
  308.         else
  309.             safeBack()
  310.         end
  311.         positionx = positionx + 1
  312.     end
  313.     while targetx < positionx do
  314.         if facing == 3 then
  315.             safeForward()
  316.         else
  317.             safeBack()
  318.         end
  319.         positionx = positionx - 1
  320.     end
  321. end
  322.  
  323. function navigateTo(targetx, targety)
  324.     if facing == 0 or facing == 2 then -- Y axis
  325.         moveY(targety)
  326.         moveX(targetx)
  327.     else
  328.         moveX(targetx)
  329.         moveY(targety)
  330.     end
  331. end
  332.  
  333. function buildHexagon()
  334.     local changex = sideLength / 2
  335.     local changey = round(math.sqrt(3) * changex, 0)
  336.     changex = round(changex, 0)
  337.     local counter = 0
  338.    
  339.     navigateTo(changex, 0)
  340.    
  341.     for currentSide = 1, 6 do
  342.         counter = 0
  343.        
  344.         if currentSide == 1 then
  345.             for placed = 1, sideLength do
  346.                 navigateTo(positionx + 1, positiony)
  347.                 placeBlock()
  348.             end
  349.         elseif currentSide == 2 then
  350.             navigateTo(positionx, positiony + 1)
  351.             while positiony <= changey do
  352.                 if counter == 0 or counter == 2 or counter == 4 then
  353.                     navigateTo(positionx + 1, positiony)
  354.                 end
  355.                 placeBlock()
  356.                 navigateTo(positionx, positiony + 1)
  357.                 counter = counter + 1
  358.                 if counter == 5 then
  359.                     counter = 0
  360.                 end
  361.             end
  362.         elseif currentSide == 3 then
  363.             while positiony <= (2 * changey) do
  364.                 if counter == 0 or counter == 2 or counter == 4 then
  365.                     navigateTo(positionx - 1, positiony)
  366.                 end
  367.                 placeBlock()
  368.                 navigateTo(positionx, positiony + 1)
  369.                 counter = counter + 1
  370.                 if counter == 5 then
  371.                     counter = 0
  372.                 end
  373.             end
  374.         elseif currentSide == 4 then
  375.             for placed = 1, sideLength do
  376.                 navigateTo(positionx - 1, positiony)
  377.                 placeBlock()
  378.             end
  379.         elseif currentSide == 5 then
  380.         navigateTo(positionx, positiony - 1)
  381.             while positiony >= changey do
  382.                 if counter == 0 or counter == 2 or counter == 4 then
  383.                     navigateTo(positionx - 1, positiony)
  384.                 end
  385.                 placeBlock()
  386.                 navigateTo(positionx, positiony - 1)
  387.                 counter = counter + 1
  388.                 if counter == 5 then
  389.                     counter = 0
  390.                 end
  391.             end
  392.         elseif currentSide == 6 then
  393.             while positiony >= 0 do
  394.                 if counter == 0 or counter == 2 or counter == 4 then
  395.                     navigateTo(positionx + 1, positiony)
  396.                 end
  397.                 placeBlock()
  398.                 navigateTo(positionx, positiony - 1)
  399.                 counter = counter + 1
  400.                 if counter == 5 then
  401.                     counter = 0
  402.                 end
  403.             end
  404.         end
  405.     end
  406.    
  407.     navigateTo(0, 0)
  408.     while facing ~= 0 do
  409.         turnLeftTrack()
  410.     end
  411. end
  412.  
  413. function buildOctagon()
  414.     local sideLength2 = sideLength - 1
  415.     local change = round(sideLength2 / math.sqrt(2), 0)
  416.    
  417.     navigateTo(change, 0)
  418.    
  419.     for currentSide = 1, 8 do
  420.         if currentSide == 1 then
  421.             for placed = 1, sideLength2 do
  422.                 navigateTo(positionx + 1, positiony)
  423.                 placeBlock()
  424.             end
  425.         elseif currentSide == 2 then
  426.             for placed = 1, change do
  427.                 navigateTo(positionx + 1, positiony + 1)
  428.                 placeBlock()
  429.             end
  430.         elseif currentSide == 3 then
  431.             for placed = 1, sideLength2 do
  432.                 navigateTo(positionx, positiony + 1)
  433.                 placeBlock()
  434.             end
  435.         elseif currentSide == 4 then
  436.             for placed = 1, change do
  437.                 navigateTo(positionx - 1, positiony + 1)
  438.                 placeBlock()
  439.             end
  440.         elseif currentSide == 5 then
  441.             for placed = 1, sideLength2 do
  442.                 navigateTo(positionx - 1, positiony)
  443.                 placeBlock()
  444.             end
  445.         elseif currentSide == 6 then
  446.             for placed = 1, change do
  447.                 navigateTo(positionx - 1, positiony - 1)
  448.                 placeBlock()
  449.             end
  450.         elseif currentSide == 7 then
  451.         for placed = 1, sideLength2 do
  452.                 navigateTo(positionx, positiony - 1)
  453.                 placeBlock()
  454.             end
  455.         elseif currentSide == 8 then
  456.             for placed = 1, change do
  457.                 navigateTo(positionx + 1, positiony - 1)
  458.                 placeBlock()
  459.             end
  460.         end
  461.     end
  462.    
  463.     navigateTo(0, 0)
  464.     while facing ~= 0 do
  465.     turnLeftTrack()
  466.     end
  467. end
  468.  
  469. function hexPrism()
  470.     for z = 1, height do
  471.         buildHexagon()
  472.         safeUp()
  473.     end
  474. end
  475.  
  476. function octPrism()
  477.     for z = 1, height do
  478.         buildOctagon()
  479.         safeUp()
  480.     end
  481. end
  482.  
  483. --Main Code
  484. if not initialize(false) then
  485.     --If help was shown close program and do nothing else
  486.     return
  487. end
  488.  
  489. if numSides == 6 then
  490.     hexPrism()
  491. elseif numSides == 8 then
  492.     octPrism()
  493. end
Advertisement
Add Comment
Please, Sign In to add comment