Advertisement
happydude11209

Shape Builder ComputerCraft Beta

Apr 11th, 2013
455
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 40.74 KB | None | 0 0
  1. -- Variable Setup
  2. -- Command Line input Table
  3. local argTable = {...}
  4.  
  5. -- Flag Variables: These are conditions for different features (all flags are named foo_bar, all other variables are named fooBar)
  6. local cmd_line = false
  7. local cmd_line_resume = false
  8. local cmd_line_cost_only = false
  9. local chain_next_shape = false -- This tells goHome() where to end, if true it goes to (0, 0, positionZ) if false it goes to (-1, -1, 0)
  10. local special_chain = false -- For certain shapes that finish where the next chained shape should start, goHome() will  only turn to face 0 if true
  11. local cost_only = false
  12. local sim_mode = false
  13.  
  14. -- Record Keeping Variables: These are for recoding the blocks and fuel used
  15. local blocks = 0
  16. local fuel = 0
  17.  
  18. -- Position Tracking Variables: These are for keeping track of the turtle's position
  19. local positionX = 0
  20. local positionY = 0
  21. local positionZ = 0
  22. local facing = 0
  23. local gpsPositionX = 0
  24. local gpsPositionY = 0
  25. local gpsPositionZ = 0
  26. local gpsFacing = 0
  27.  
  28. -- General Variables: Other variables that don't fit in the other categories
  29. local resupply = false
  30. local can_use_gps = false
  31. local returntohome = false -- whether the turtle shall return to start after build
  32. local choice = ""
  33.  
  34. -- Progress Table: These variables are the tables that the turtle's progress is tracked in
  35. local tempProgTable = {}
  36. local progTable = {} --This is the LOCAL table!  used for local stuff only, and is ONLY EVER WRITTEN when sim_mode is FALSE
  37. local progFileName = "ShapesProgressFile"
  38.  
  39.  
  40. -- Utility functions
  41.  
  42. function writeOut(...) -- ... lets writeOut() pass any arguments to print(). so writeOut(1,2,3) is the same as print(1,2,3). previously writeOut(1,2,3) would have been the same as print(1)
  43.     for i, v in ipairs(arg) do
  44.         print(v)
  45.     end
  46. end
  47.  
  48. function wrapmodules() --checks for and wraps turtle modules
  49.     local test = 0
  50.     if peripheral.getType("left" )== "resupply" then
  51.         rs=peripheral.wrap("left")
  52.         resupply = true
  53.         return "resupply"
  54.     elseif peripheral.getType("right") == "resupply" then
  55.         rs=peripheral.wrap("right")
  56.         resupply = true
  57.         return "resupply"
  58.     end
  59.     if peripheral.getType("left") == "modem" then
  60.         modem=peripheral.wrap("left")
  61.         test, _, _ = gps.locate(1)
  62.         if test ~= nil then
  63.             can_use_gps = true
  64.         end
  65.         return "modem"
  66.     elseif peripheral.getType("right") == "modem" then
  67.         modem=peripheral.wrap("right")
  68.         test, _, _ = gps.locate(1)
  69.         if test ~= nil then
  70.             can_use_gps = true
  71.         end
  72.         return "modem"
  73.     else
  74.         return false
  75.     end
  76. end
  77.  
  78. function linkToRSStation() -- Links to rs station
  79.     if rs.link() then
  80.         return true
  81.     else
  82.         writeOut("Please put Resupply Station to the left of the turtle and press Enter to continue")
  83.         io.read()
  84.         linkToRSStation()
  85.     end
  86. end
  87.  
  88. function compareResources()
  89.     if (turtle.compareTo(1) == false) then
  90.         turtle.drop()
  91.     end
  92. end
  93.  
  94. function checkResources()
  95.     if resupply then
  96.         if turtle.getItemCount(activeslot) <= 1 then
  97.             while not(rs.resupply(1)) do
  98.                 os.sleep(0.5)
  99.             end
  100.         end
  101.     else
  102.         compareResources()
  103.         while (turtle.getItemCount(activeslot) <= 1) do
  104.             if (activeslot == 16) and (turtle.getItemCount(activeslot)<=1) then
  105.                 writeOut("Turtle is empty, please put building block in slots and press enter to continue")
  106.                 io.read()
  107.                 activeslot = 1
  108.                 turtle.select(activeslot)
  109.             else
  110.                 activeslot = activeslot+1
  111.                 -- writeOut("Turtle slot almost empty, trying slot "..activeslot)
  112.                 turtle.select(activeslot)
  113.             end
  114.             compareResources()
  115.             os.sleep(0.2)
  116.         end
  117.     end
  118. end
  119.  
  120. function checkFuel()
  121.     if (not(tonumber(turtle.getFuelLevel()) == nil)) then
  122.         while turtle.getFuelLevel() < 50 do
  123.             writeOut("Turtle almost out of fuel, pausing. Please drop fuel in inventory. And press enter.")
  124.             io.read()
  125.             turtle.refuel()
  126.         end
  127.     end
  128. end
  129.  
  130. function placeBlock()
  131.     blocks = blocks + 1
  132.     simulationCheck()
  133.     if cost_only then
  134.         return
  135.     end
  136.     if turtle.detectDown() and not turtle.compareDown() then
  137.         turtle.digDown()
  138.     end
  139.     checkResources()
  140.     turtle.placeDown()
  141.     progressUpdate()
  142. end
  143.  
  144. function round(toBeRounded, decimalPlace) -- Needed for hexagon and octagon
  145.     local multiplier = 10^(decimalPlace or 0)
  146.     return math.floor(toBeRounded * multiplier + 0.5) / multiplier
  147. end
  148.  
  149. -- Navigation functions
  150. -- Allow the turtle to move while tracking its position
  151. -- This allows us to just give a destination point and have it go there
  152.  
  153. function turnRightTrack()
  154.     simulationCheck()
  155.     facing = facing + 1
  156.     if facing >= 4 then
  157.         facing = 0
  158.     end
  159.     progressUpdate()
  160.     if cost_only then
  161.         return
  162.     end
  163.     turtle.turnRight()
  164. end
  165.  
  166. function turnLeftTrack()
  167.     simulationCheck()
  168.     facing = facing - 1
  169.     if facing < 0 then
  170.         facing = 3
  171.     end
  172.     progressUpdate()
  173.     if cost_only then
  174.         return
  175.     end
  176.     turtle.turnLeft()
  177. end
  178.  
  179. function turnAroundTrack()
  180.     turnLeftTrack()
  181.     turnLeftTrack()
  182. end
  183.  
  184. function turnToFace(direction)
  185.     if direction >= 4 or direction < 0 then
  186.         return false
  187.     end
  188.     while facing ~= direction do
  189.         turnLeftTrack()
  190.     end
  191.     return true
  192. end
  193.  
  194. function safeForward()
  195.     simulationCheck()
  196.     if facing == 0 then
  197.         positionY = positionY + 1
  198.     elseif facing == 1 then
  199.         positionX = positionX + 1
  200.     elseif facing == 2 then
  201.         positionY = positionY - 1
  202.     elseif facing == 3 then
  203.         positionX = positionX - 1
  204.     end
  205.     fuel = fuel + 1
  206.     progressUpdate()
  207.     if cost_only then
  208.         return
  209.     end
  210.     checkFuel()
  211.     local success = false
  212.     local tries = 0
  213.     while not success do
  214.         success = turtle.forward()
  215.         if not success then
  216.             while (not success) and tries < 3 do
  217.                 tries = tries + 1
  218.                 turtle.dig()
  219.                 success = turtle.forward()
  220.                 sleep(0.3)
  221.             end
  222.             if not success then
  223.                 writeOut("Blocked attempting to move forward.")
  224.                 writeOut("Please clear and press enter to continue.")
  225.                 io.read()
  226.             end
  227.         end
  228.     end
  229. end
  230.  
  231. function safeBack()
  232.     simulationCheck()
  233.     if facing == 0 then
  234.         positionY = positionY - 1
  235.     elseif facing == 1 then
  236.         positionX = positionX - 1
  237.     elseif facing == 2 then
  238.         positionY = positionY + 1
  239.     elseif facing == 3 then
  240.         positionX = positionX + 1
  241.     end
  242.     fuel = fuel + 1
  243.     progressUpdate()
  244.     if cost_only then
  245.         return
  246.     end
  247.     checkFuel()
  248.     local success = false
  249.     local tries = 0
  250.     while not success do
  251.         success = turtle.back()
  252.         if not success then
  253.             turnAroundTrack()
  254.             while turtle.detect() and tries < 3 do
  255.                 tries = tries + 1
  256.                 if turtle.dig() then
  257.                     break
  258.                 end
  259.                 sleep(0.3)
  260.             end
  261.             turnAroundTrack()
  262.             success = turtle.back()
  263.             if not success then
  264.                 writeOut("Blocked attempting to move back.")
  265.                 writeOut("Please clear and press enter to continue.")
  266.                 io.read()
  267.             end
  268.         end
  269.     end
  270. end
  271.  
  272. function safeUp()
  273.     simulationCheck()
  274.     positionZ = positionZ + 1
  275.     fuel = fuel + 1
  276.     progressUpdate()
  277.     if cost_only then
  278.         return
  279.     end
  280.     checkFuel()
  281.     local success = false
  282.     while not success do
  283.         success = turtle.up()
  284.         if not success then
  285.             while turtle.detectUp() do
  286.                 if not turtle.digUp() then
  287.                     writeOut("Blocked attempting to move up.")
  288.                     writeOut("Please clear and press enter to continue.")
  289.                     io.read()
  290.                 end
  291.             end
  292.         end
  293.     end
  294. end
  295.  
  296. function safeDown()
  297.     simulationCheck()
  298.     positionZ = positionZ - 1
  299.     fuel = fuel + 1
  300.     progressUpdate()
  301.     if cost_only then
  302.         return
  303.     end
  304.     checkFuel()
  305.     local success = false
  306.     while not success do
  307.         success = turtle.down()
  308.         if not success then
  309.             while turtle.detectDown() do
  310.                 if not turtle.digDown() then
  311.                     writeOut("Blocked attempting to move down.")
  312.                     writeOut("Please clear and press enter to continue.")
  313.                     io.read()
  314.                 end
  315.             end
  316.         end
  317.     end
  318. end
  319.  
  320. function moveY(targetY)
  321.     if targetY == positionY then
  322.         return
  323.     end
  324.     if (facing ~= 0 and facing ~= 2) then -- Check axis
  325.         turnRightTrack()
  326.     end
  327.     while targetY > positionY do
  328.         if facing == 0 then
  329.             safeForward()
  330.         else
  331.             safeBack()
  332.         end
  333.     end
  334.     while targetY < positionY do
  335.         if facing == 2 then
  336.             safeForward()
  337.         else
  338.             safeBack()
  339.         end
  340.     end
  341. end
  342.  
  343. function moveX(targetX)
  344.     if targetX == positionX then
  345.         return
  346.     end
  347.     if (facing ~= 1 and facing ~= 3) then -- Check axis
  348.         turnRightTrack()
  349.     end
  350.     while targetX > positionX do
  351.         if facing == 1 then
  352.             safeForward()
  353.         else
  354.             safeBack()
  355.         end
  356.     end
  357.     while targetX < positionX do
  358.         if facing == 3 then
  359.             safeForward()
  360.         else
  361.             safeBack()
  362.         end
  363.     end
  364. end
  365.  
  366. function moveZ(targetZ)
  367.     if targetZ == positionZ then
  368.         return
  369.     end
  370.     while targetZ < positionZ do
  371.         safeDown()
  372.     end
  373.     while targetZ > positionZ do
  374.         safeUp()
  375.     end
  376. end
  377.  
  378. -- I *HIGHLY* suggest formatting all shape subroutines to use the format that dome() uses;  specifically, navigateTo(x,y,[z]) then placeBlock().  This should ensure proper "data recording" and also makes readability better
  379. function navigateTo(targetX, targetY, targetZ, move_z_first)
  380.     targetZ = targetZ or positionZ -- If targetZ isn't used in the function call, it defaults to its current z position, this should make it compatible with all previous implementations of navigateTo()
  381.     move_z_first = move_z_first or false -- Defaults to moving z last, if true is passed as 4th argument, it moves vertically first
  382.    
  383.     if move_z_first then
  384.         moveZ(targetZ)
  385.     end
  386.    
  387.     if facing == 0 or facing == 2 then -- Y axis
  388.         moveY(targetY)
  389.         moveX(targetX)
  390.     else
  391.         moveX(targetX)
  392.         moveY(targetY)
  393.     end
  394.    
  395.     if not move_z_first then
  396.         moveZ(targetZ)
  397.     end
  398. end
  399.  
  400. function goHome()
  401.     if chain_next_shape then
  402.         if not special_chain then
  403.             navigateTo(0, 0) -- So another program can chain multiple shapes together to create bigger structures
  404.         end
  405.     else
  406.         navigateTo(-1, -1, 0) -- So the user can collect the turtle when it is done -- also not 0,0,0 because some shapes use the 0,0 column
  407.     end
  408.     turnToFace(0)
  409. end
  410.  
  411. -- Shape Building functions
  412.  
  413. function line(length)
  414.     if length <= 0 then
  415.         error("Error, length can not be 0")
  416.     end
  417.     local i
  418.     for i = 1, length do
  419.         placeBlock()
  420.         if i ~= length then
  421.             safeForward()
  422.         end
  423.     end
  424. end
  425.  
  426. function rectangle(depth, width)
  427.     if depth <= 0 then
  428.         error("Error, depth can not be 0")
  429.     end
  430.     if width <= 0 then
  431.         error("Error, width can not be 0")
  432.     end
  433.     local lengths = {depth, width, depth, width }
  434.     local j
  435.     for j=1,4 do
  436.         line(lengths[j])
  437.         turnRightTrack()
  438.     end
  439. end
  440.  
  441. function square(width)
  442.     rectangle(width, width)
  443. end
  444.  
  445. function wall(length, height)
  446.     local i
  447.     local j
  448.     for i = 1, length do
  449.         for j = 1, height do
  450.             placeBlock()
  451.             if j < height then
  452.                 safeUp()
  453.             end
  454.         end
  455.         safeForward()
  456.         for j = 1, height - 1 do
  457.             safeDown()
  458.         end
  459.     end
  460.     turnLeftTrack()
  461. end
  462.  
  463. function platform(x, y)
  464.     local forward = true
  465.     for counterY = 0, y - 1 do
  466.         for counterX = 0, x - 1 do
  467.             if forward then
  468.                 navigateTo(counterX, counterY)
  469.             else
  470.                 navigateTo(x - counterX - 1, counterY)
  471.             end
  472.             placeBlock()
  473.         end
  474.         if forward then
  475.             forward = false
  476.         else
  477.             forward = true
  478.         end
  479.     end
  480. end
  481.  
  482. function stair(width, height)
  483.     turnRightTrack()
  484.     local counterX = 1
  485.     local counterY = 0
  486.     local goForward = 0
  487.     while counterY < height do
  488.         while counterX < width do
  489.             placeBlock()
  490.             safeForward()
  491.             counterX = counterX + 1
  492.         end
  493.         placeBlock()
  494.         counterX = 1
  495.         counterY = counterY + 1
  496.         if counterY < height then
  497.             if goForward == 1 then
  498.                 turnRightTrack()
  499.                 safeUp()
  500.                 safeForward()
  501.                 turnRightTrack()
  502.                 goForward = 0
  503.             else
  504.                 turnLeftTrack()
  505.                 safeUp()
  506.                 safeForward()
  507.                 turnLeftTrack()
  508.                 goForward = 1
  509.             end
  510.         end
  511.     end
  512. end
  513.  
  514. function circle(radius)
  515.     width = radius * 2 + 1
  516.     sqrt3 = 3 ^ 0.5
  517.     boundaryRadius = radius + 1.0
  518.     boundary2 = boundaryRadius ^ 2
  519.     z = radius
  520.     cz2 = (radius - z) ^ 2
  521.     limitOffsetY = (boundary2 - cz2) ^ 0.5
  522.     maxOffestY = math.ceil(limitOffsetY)
  523.     -- We do first the +x side, then the -x side to make movement efficient
  524.     for side = 0, 1 do
  525.         -- On the right we go from small y to large y, on the left reversed
  526.         -- This makes us travel clockwise (from below) around each layer
  527.         if (side == 0) then
  528.             yStart = radius - maxOffestY
  529.             yEnd = radius + maxOffestY
  530.             yStep = 1
  531.         else
  532.             yStart = radius + maxOffestY
  533.             yEnd = radius - maxOffestY
  534.             yStep = -1
  535.         end
  536.         for y = yStart, yEnd, yStep do
  537.             cy2 = (radius - y) ^ 2
  538.             remainder2 = (boundary2 - cz2 - cy2)
  539.             if remainder2 >= 0 then
  540.                 -- This is the maximum difference in x from the centre we can be without definitely being outside the radius
  541.                 maxOffsetX = math.ceil((boundary2 - cz2 - cy2) ^ 0.5)
  542.                     -- Only do either the +x or -x side
  543.                 if (side == 0) then
  544.                     -- +x side
  545.                     xStart = radius
  546.                     xEnd = radius + maxOffsetX
  547.                 else
  548.                     -- -x side
  549.                     xStart = radius - maxOffsetX
  550.                     xEnd = radius - 1
  551.                 end
  552.                 -- Reverse direction we traverse xs when in -y side
  553.                 if y > radius then
  554.                     temp = xStart
  555.                     xStart = xEnd
  556.                     xEnd = temp
  557.                     xStep = -1
  558.                 else
  559.                     xStep = 1
  560.                 end
  561.                     for x = xStart, xEnd, xStep do
  562.                     cx2 = (radius - x) ^ 2
  563.                     distanceToCentre = (cx2 + cy2 + cz2) ^ 0.5
  564.                     -- Only blocks within the radius but still within 1 3d-diagonal block of the edge are eligible
  565.                     if distanceToCentre < boundaryRadius and distanceToCentre + sqrt3 >= boundaryRadius then
  566.                         offsets = {{0, 1, 0}, {0, -1, 0}, {1, 0, 0}, {-1, 0, 0}, {0, 0, 1}, {0, 0, -1}}
  567.                         for i=1,6 do
  568.                             offset = offsets[i]
  569.                             dx = offset[1]
  570.                             dy = offset[2]
  571.                             dz = offset[3]
  572.                             if ((radius - (x + dx)) ^ 2 + (radius - (y + dy)) ^ 2 + (radius - (z + dz)) ^ 2) ^ 0.5 >= boundaryRadius then
  573.                                 -- This is a point to use
  574.                                 navigateTo(x, y)
  575.                                 placeBlock()
  576.                                 break
  577.                             end
  578.                         end
  579.                     end
  580.                 end
  581.             end
  582.         end
  583.     end
  584. end
  585.  
  586. function dome(typus, radius)
  587.     -- Main dome and sphere building routine
  588.     width = radius * 2 + 1
  589.     sqrt3 = 3 ^ 0.5
  590.     boundaryRadius = radius + 1.0
  591.     boundary2 = boundaryRadius ^ 2
  592.     if typus == "dome" then
  593.         zstart = radius
  594.     elseif typus == "sphere" then
  595.         zstart = 0
  596.     elseif typus == "bowl" then
  597.         zstart = 0
  598.     end
  599.     if typus == "bowl" then
  600.         zend = radius
  601.     else
  602.         zend = width - 1
  603.     end
  604.  
  605.     -- This loop is for each vertical layer through the sphere or dome.
  606.     for z = zstart,zend do
  607.         if not cost_only and z ~= zstart then
  608.             safeUp()
  609.         end
  610.         --writeOut("Layer " .. z)
  611.         cz2 = (radius - z) ^ 2
  612.         limitOffsetY = (boundary2 - cz2) ^ 0.5
  613.         maxOffestY = math.ceil(limitOffsetY)
  614.         -- We do first the +x side, then the -x side to make movement efficient
  615.         for side = 0,1 do
  616.             -- On the right we go from small y to large y, on the left reversed
  617.             -- This makes us travel clockwise (from below) around each layer
  618.             if (side == 0) then
  619.                 yStart = radius - maxOffestY
  620.                 yEnd = radius + maxOffestY
  621.                 yStep = 1
  622.             else
  623.                 yStart = radius + maxOffestY
  624.                 yEnd = radius - maxOffestY
  625.                 yStep = -1
  626.             end
  627.             for y = yStart,yEnd,yStep do
  628.                 cy2 = (radius - y) ^ 2
  629.                 remainder2 = (boundary2 - cz2 - cy2)
  630.                 if remainder2 >= 0 then
  631.                     -- This is the maximum difference in x from the centre we can be without definitely being outside the radius
  632.                     maxOffsetX = math.ceil((boundary2 - cz2 - cy2) ^ 0.5)
  633.                     -- Only do either the +x or -x side
  634.                     if (side == 0) then
  635.                         -- +x side
  636.                         xStart = radius
  637.                         xEnd = radius + maxOffsetX
  638.                     else
  639.                         -- -x side
  640.                         xStart = radius - maxOffsetX
  641.                         xEnd = radius - 1
  642.                     end
  643.                     -- Reverse direction we traverse xs when in -y side
  644.                     if y > radius then
  645.                         temp = xStart
  646.                         xStart = xEnd
  647.                         xEnd = temp
  648.                         xStep = -1
  649.                     else
  650.                         xStep = 1
  651.                     end
  652.  
  653.                     for x = xStart,xEnd,xStep do
  654.                         cx2 = (radius - x) ^ 2
  655.                         distanceToCentre = (cx2 + cy2 + cz2) ^ 0.5
  656.                         -- Only blocks within the radius but still within 1 3d-diagonal block of the edge are eligible
  657.                         if distanceToCentre < boundaryRadius and distanceToCentre + sqrt3 >= boundaryRadius then
  658.                             offsets = {{0, 1, 0}, {0, -1, 0}, {1, 0, 0}, {-1, 0, 0}, {0, 0, 1}, {0, 0, -1}}
  659.                             for i=1,6 do
  660.                                 offset = offsets[i]
  661.                                 dx = offset[1]
  662.                                 dy = offset[2]
  663.                                 dz = offset[3]
  664.                                 if ((radius - (x + dx)) ^ 2 + (radius - (y + dy)) ^ 2 + (radius - (z + dz)) ^ 2) ^ 0.5 >= boundaryRadius then
  665.                                     -- This is a point to use
  666.                                     navigateTo(x, y)
  667.                                     placeBlock()
  668.                                     break
  669.                                 end
  670.                             end
  671.                         end
  672.                     end
  673.                 end
  674.             end
  675.         end
  676.     end
  677. end
  678.  
  679. function hexagon(sideLength)
  680.     local changeX = sideLength / 2
  681.     local changeY = round(math.sqrt(3) * changeX, 0)
  682.     changeX = round(changeX, 0)
  683.     local counter = 0
  684.  
  685.     navigateTo(changeX, 0)
  686.  
  687.     for currentSide = 1, 6 do
  688.         counter = 0
  689.  
  690.         if currentSide == 1 then
  691.             for placed = 1, sideLength do
  692.                 navigateTo(positionX + 1, positionY)
  693.                 placeBlock()
  694.             end
  695.         elseif currentSide == 2 then
  696.             navigateTo(positionX, positionY + 1)
  697.             while positionY <= changeY do
  698.                 if counter == 0 or counter == 2 or counter == 4 then
  699.                     navigateTo(positionX + 1, positionY)
  700.                 end
  701.                 placeBlock()
  702.                 navigateTo(positionX, positionY + 1)
  703.                 counter = counter + 1
  704.                 if counter == 5 then
  705.                     counter = 0
  706.                 end
  707.             end
  708.         elseif currentSide == 3 then
  709.             while positionY <= (2 * changeY) do
  710.                 if counter == 0 or counter == 2 or counter == 4 then
  711.                     navigateTo(positionX - 1, positionY)
  712.                 end
  713.                 placeBlock()
  714.                 navigateTo(positionX, positionY + 1)
  715.                 counter = counter + 1
  716.                 if counter == 5 then
  717.                     counter = 0
  718.                 end
  719.             end
  720.         elseif currentSide == 4 then
  721.             for placed = 1, sideLength do
  722.                 navigateTo(positionX - 1, positionY)
  723.                 placeBlock()
  724.             end
  725.         elseif currentSide == 5 then
  726.             navigateTo(positionX, positionY - 1)
  727.             while positionY >= changeY do
  728.                 if counter == 0 or counter == 2 or counter == 4 then
  729.                     navigateTo(positionX - 1, positionY)
  730.                 end
  731.                 placeBlock()
  732.                 navigateTo(positionX, positionY - 1)
  733.                 counter = counter + 1
  734.                 if counter == 5 then
  735.                     counter = 0
  736.                 end
  737.             end
  738.         elseif currentSide == 6 then
  739.             while positionY >= 0 do
  740.                 if counter == 0 or counter == 2 or counter == 4 then
  741.                     navigateTo(positionX + 1, positionY)
  742.                 end
  743.                 placeBlock()
  744.                 navigateTo(positionX, positionY - 1)
  745.                 counter = counter + 1
  746.                 if counter == 5 then
  747.                     counter = 0
  748.                 end
  749.             end
  750.         end
  751.     end
  752. end
  753.  
  754. function octagon(sideLength)
  755.     local sideLength2 = sideLength - 1
  756.     local change = round(sideLength2 / math.sqrt(2), 0)
  757.  
  758.     navigateTo(change, 0)
  759.  
  760.     for currentSide = 1, 8 do
  761.         if currentSide == 1 then
  762.             for placed = 1, sideLength2 do
  763.                 navigateTo(positionX + 1, positionY)
  764.                 placeBlock()
  765.             end
  766.         elseif currentSide == 2 then
  767.             for placed = 1, change do
  768.                 navigateTo(positionX + 1, positionY + 1)
  769.                 placeBlock()
  770.             end
  771.         elseif currentSide == 3 then
  772.             for placed = 1, sideLength2 do
  773.                 navigateTo(positionX, positionY + 1)
  774.                 placeBlock()
  775.             end
  776.         elseif currentSide == 4 then
  777.             for placed = 1, change do
  778.                 navigateTo(positionX - 1, positionY + 1)
  779.                 placeBlock()
  780.             end
  781.         elseif currentSide == 5 then
  782.             for placed = 1, sideLength2 do
  783.                 navigateTo(positionX - 1, positionY)
  784.                 placeBlock()
  785.             end
  786.         elseif currentSide == 6 then
  787.             for placed = 1, change do
  788.                 navigateTo(positionX - 1, positionY - 1)
  789.                 placeBlock()
  790.             end
  791.         elseif currentSide == 7 then
  792.         for placed = 1, sideLength2 do
  793.                 navigateTo(positionX, positionY - 1)
  794.                 placeBlock()
  795.             end
  796.         elseif currentSide == 8 then
  797.             for placed = 1, change do
  798.                 navigateTo(positionX + 1, positionY - 1)
  799.                 placeBlock()
  800.             end
  801.         end
  802.     end
  803. end
  804.  
  805. -- Previous Progress Resuming, Simulation functions, Command Line, and File Backend
  806.  
  807. -- Will check for a "progress" file.
  808. function CheckForPrevious()
  809.     if fs.exists(progFileName) then
  810.         return true
  811.     else
  812.         return false
  813.     end
  814. end
  815.  
  816. -- Creates a progress file, containing a serialized table consisting of the shape type, shape input params, and the last known x, y, and z coords of the turtle (beginning of build project)
  817. function ProgressFileCreate()
  818.     if not CheckForPrevious() then
  819.         fs.makeDir(progFileName)
  820.         return true
  821.     else
  822.         return false
  823.     end
  824. end
  825.  
  826. -- Deletes the progress file (at the end of the project, also at beginning if user chooses to delete old progress)
  827. function ProgressFileDelete()
  828.     if fs.exists(progFileName) then
  829.         fs.delete(progFileName)
  830.         return true
  831.     else
  832.         return false
  833.     end
  834. end
  835.  
  836. -- To read the shape params from the file.  Shape type, and input params (e.g. "dome" and radius)
  837. function ReadShapeParams()
  838.     -- TODO. Unneeded for now, can just use the table elements directly
  839. end
  840.  
  841. function WriteShapeParams(...) -- The ... lets it take any number of arguments and stores it to the table arg{} | This is still unused anywhere
  842.     local paramTable = arg
  843.     local paramName = "param"
  844.     local paramName2 = paramName
  845.     for i, v in ipairs(paramTable) do -- Iterates through the args passed to the function, ex. paramTable[1] i = 1 so paramName2 should be "param1", tested and works!
  846.         paramName2 = paramName .. i
  847.         tempProgTable[paramName2] = v
  848.         progTable[paramName2] = v
  849.     end
  850. end
  851.  
  852. -- function to write the progress to the file (x, y, z)
  853. function writeProgress()
  854.     local progFile
  855.     local progString = ""
  856.     if not (sim_mode or cost_only) then
  857.         progString = textutils.serialize(progTable) -- Put in here to save processing time when in cost_only
  858.         progFile = fs.open(progFileName, "w")
  859.         progFile.write(progString)
  860.         progFile.close()
  861.     end
  862.  
  863. end
  864.  
  865. -- Reads progress from file (shape, x, y, z, facing, blocks, param1, param2, param3)
  866. function readProgress()
  867.     local progFile = fs.open(progFileName, "r")
  868.     local readProgTable = textutils.unserialize(progFile.readAll())
  869.     progFile.close()
  870.     return readProgTable
  871. end
  872.  
  873. -- compares the progress read from the file to the current sim progress.  needs all four params
  874. function compareProgress() -- return boolean
  875.     local progTableIn = progTable
  876.     local readProgTable = readProgress()
  877.     if (progTableIn.shape == readProgTable.shape and progTableIn.x == readProgTable.x and progTableIn.y == readProgTable.y and progTableIn.blocks == readProgTable.blocks and progTableIn.facing == readProgTable.facing) then
  878.         writeOut("All caught up!")
  879.         return true -- We're caught up!
  880.     else
  881.         return false -- Not there yet...
  882.     end
  883. end
  884.  
  885. function getGPSInfo() -- TODO: finish this
  886.     position = gps.locate()
  887.     gpsPositionX = position.x
  888.     gpsPositionZ = position.y
  889.     gpsPositionY = position.z
  890.    
  891. end
  892.  
  893. function setSimFlags(b)
  894.     sim_mode = b
  895.     cost_only = b
  896.     if cmd_line_cost_only then
  897.         cost_only = true
  898.     end
  899. end
  900.  
  901. function simulationCheck() -- checks state of the simulation
  902.     if sim_mode then
  903.         if compareProgress() then
  904.             setSimFlags(false) -- If we're caught up, un-set flags
  905.         else
  906.             setSimFlags(true)  -- If not caught up, just re-affirm that the flags are set
  907.         end
  908.     end
  909. end
  910.  
  911. function continueQuery()
  912.     if cmd_line_resume then
  913.          return true
  914.     else
  915.          if not cmd_line then
  916.              writeOut("Do you want to continue the last job?")
  917.              local yes = io.read()
  918.              if yes == "y" then
  919.                  return true
  920.              else
  921.                  return false
  922.              end
  923.          end
  924.     end
  925. end
  926.  
  927. function progressUpdate()  -- This ONLY updates the local table variable.  Writing is handled above. -- I want to change this to allow for any number of params
  928.     progTable = {shape = choice, param1 = tempProgTable.param1, param2 = tempProgTable.param2, param3 = tempProgTable.param3, param4 = tempProgTable.param4, x = positionX, y = positionY, z = positionZ, facing = facing, blocks = blocks}
  929.     if not sim_mode then
  930.         writeProgress()
  931.     end
  932. end
  933.  
  934.  -- Command Line
  935. function checkCommandLine() --True if arguments were passed
  936.     if #argTable > 0 then
  937.         cmd_line = true
  938.         return true
  939.     else
  940.         cmd_line = false
  941.         return false
  942.     end
  943. end
  944.  
  945. function needsHelp() -- True if -h is passed
  946.     for i, v in pairs(argTable) do
  947.         if v == "-h" or v == "-help" or v == "--help" then
  948.             return true
  949.         else
  950.             return false
  951.         end
  952.     end
  953. end
  954.  
  955. function setFlagsFromCommandLine() -- Sets count_only, chain_next_shape, and sim_mode
  956.     for i, v in pairs(argTable) do
  957.         if v == "-c" or v == "-cost" or v == "--cost" then
  958.             cost_only = true
  959.             cmd_line_cost_only = true
  960.             writeOut("Cost only mode")
  961.         end
  962.         if v == "-z" or v == "-chain" or v == "--chain" then
  963.             chain_next_shape = true
  964.             writeOut("Chained shape mode")
  965.         end
  966.         if v == "-r" or v == "-resume" or v == "--resume" then
  967.             cmd_line_resume = true
  968.             writeOut("Resuming")
  969.         end
  970.     end
  971. end
  972.  
  973. function setTableFromCommandLine() -- Sets progTable and tempProgTable from command line arguments
  974.     progTable.shape = argTable[1]
  975.     tempProgTable.shape = argTable[1]
  976.     local paramName = "param"
  977.     local paramName2 = paramName
  978.     for i = 2, #argTable do
  979.         local addOn = tostring(i - 1)
  980.         paramName2 = paramName .. addOn
  981.         progTable[paramName2] = argTable[i]
  982.         tempProgTable[paramName2] = argTable[i]
  983.     end
  984. end
  985.  
  986. -- Menu, Drawing and Main functions
  987.  
  988. function choiceFunction()
  989.     if sim_mode == false and cmd_line == false then -- If we are NOT resuming progress
  990.         choice = io.read()
  991.         choice = string.lower(choice) -- All checks are aginst lower case words so this is to ensure that
  992.         tempProgTable = {shape = choice}
  993.         progTable = {shape = choice}
  994.         if choice == "next" then
  995.             WriteMenu2()
  996.             choice = io.read()
  997.             choice = string.lower(choice) -- All checks are aginst lower case words so this is to ensure that
  998.         end
  999.         if choice == "end" or choice == "exit" then
  1000.             writeOut("Goodbye.")
  1001.             return
  1002.         end
  1003.         if choice == "help" then
  1004.             showHelp()
  1005.             return
  1006.         end
  1007.         if choice == "credits" then
  1008.             showCredits()
  1009.             return
  1010.         end
  1011.         writeOut("Building a "..choice)
  1012.         writeOut("Want to just calculate the cost? [y/n]")
  1013.         local yes = io.read()
  1014.         yes = string.lower(yes)
  1015.         if yes == 'y' then
  1016.             cost_only = true
  1017.         end
  1018.         writeOut("Want turtle to return to start after build? [y/n]")
  1019.         local yes = io.read()
  1020.         yes = string.lower(yes)
  1021.         if yes == 'y' then
  1022.             returntohome = true
  1023.         end
  1024.     elseif sim_mode == true then -- If we ARE resuming progress
  1025.         tempProgTable = readProgress()
  1026.         choice = tempProgTable.shape
  1027.         choice = string.lower(choice) -- All checks are aginst lower case words so this is to ensure that
  1028.     elseif cmd_line == true then -- If running from command line
  1029.         choice = tempProgTable.shape
  1030.         choice = string.lower(choice) -- All checks are aginst lower case words so this is to ensure that
  1031.         writeOut("Building a "..choice)
  1032.     end
  1033.     if not cost_only then
  1034.         turtle.select(1)
  1035.         activeslot = 1
  1036.         if turtle.getItemCount(activeslot) == 0 then
  1037.             if resupply then
  1038.                 writeOut("Please put building blocks in the first slot.")
  1039.             else
  1040.                 writeOut("Please put building blocks in the first slot (and more if you need them)")
  1041.             end
  1042.             while turtle.getItemCount(activeslot) == 0 do
  1043.                 os.sleep(2)
  1044.             end
  1045.         end
  1046.     else
  1047.         activeslot = 1
  1048.     end
  1049.    
  1050.     -- Comparisons
  1051.     if choice == "rectangle" then
  1052.         local depth = 0
  1053.         local width = 0
  1054.         if sim_mode == false and cmd_line == false then
  1055.             writeOut("How deep does it need to be?")
  1056.             depth = io.read()
  1057.             writeOut("How wide does it need to be?")
  1058.             width = io.read()
  1059.         elseif sim_mode == true or cmd_line == true then
  1060.             depth = tempProgTable.param1
  1061.             width = tempProgTable.param2
  1062.         end
  1063.         depth = tonumber(depth)
  1064.         width = tonumber(width)
  1065.         tempProgTable.param1 = depth
  1066.         tempProgTable.param2 = width
  1067.         progTable = {param1 = depth, param2 = width} -- THIS is here because we NEED to update the local table!
  1068.         rectangle(depth, width)
  1069.     end
  1070.     if choice == "square" then
  1071.         local sideLength
  1072.         if sim_mode == false and cmd_line == false then
  1073.             writeOut("What depth/width does it need to be?")
  1074.             sideLength = io.read()
  1075.         elseif sim_mode == true or cmd_line == true then
  1076.             sideLength = tempProgTable.param1
  1077.         end
  1078.         sideLength = tonumber(sideLength)
  1079.         tempProgTable.param1 = sideLength
  1080.         progTable = {param1 = sideLength}
  1081.         square(sideLength)
  1082.     end
  1083.     if choice == "line" then
  1084.         local lineLength = 0
  1085.         if sim_mode == false and cmd_line == false then
  1086.             writeOut("How long does it need to be?")
  1087.             lineLength = io.read()
  1088.         elseif sim_mode == true or cmd_line == true then
  1089.             lineLength = tempProgTable.param1
  1090.         end
  1091.         lineLength = tonumber(lineLength)
  1092.         tempProgTable.param1 = lineLength
  1093.         progTable = {param1 = lineLength}
  1094.         line(lineLength)
  1095.     end
  1096.     if choice == "wall" then
  1097.     local depth = 0
  1098.     local height = 0
  1099.         if sim_mode == false and cmd_line == false then
  1100.             writeOut("How deep does it need to be?")
  1101.             depth = io.read()
  1102.             writeOut("How high does it need to be?")
  1103.             height = io.read()
  1104.         elseif sim_mode == true or cmd_line == true then
  1105.             depth = tempProgTable.param1
  1106.             height = tempProgTable.param2
  1107.         end        
  1108.         depth = tonumber(depth)
  1109.         height = tonumber(height)
  1110.         tempProgTable.param1 = depth
  1111.         tempProgTable.param2 = height
  1112.         progTable = {param1 = depth, param2 = height}
  1113.         wall(depth, height)
  1114.     end
  1115.     if choice == "platform" then
  1116.         local depth = 0
  1117.         local width = 0
  1118.         if sim_mode == false and cmd_line == false then
  1119.             writeOut("How deep does it need to be?")
  1120.             depth = io.read()
  1121.             writeOut("How wide does it need to be?")
  1122.             width = io.read()
  1123.         elseif sim_mode == true or cmd_line == true then
  1124.             depth = tempProgTable.param1   
  1125.             width = tempProgTable.param2       
  1126.         end    
  1127.         depth = tonumber(depth)
  1128.         width = tonumber(width)
  1129.         tempProgTable.param1 = depth
  1130.         tempProgTable.param2 = width
  1131.         progTable = {param1 = depth, param2 = width}
  1132.         platform(depth, width)
  1133.     end
  1134.     if choice == "stair" then
  1135.         local width = 0
  1136.         local height = 0
  1137.         if sim_mode == false and cmd_line == false then
  1138.             writeOut("How wide does it need to be?")
  1139.             width = io.read()
  1140.             writeOut("How high does it need to be?")
  1141.             height = io.read()
  1142.         elseif sim_mode == true or cmd_line == true then
  1143.             width = tempProgTable.param1
  1144.             height = tempProgTable.param2
  1145.         end
  1146.         width = tonumber(width)
  1147.         height = tonumber(height)
  1148.         tempProgTable.param1 = width
  1149.         tempProgTable.param2 = height
  1150.         progTable = {param1 = width, param2 = height}
  1151.         stair(width, height)
  1152.         special_chain = true
  1153.     end
  1154.     if choice == "cuboid" then
  1155.         local depth = 0
  1156.         local width = 0
  1157.         local height = 0
  1158.         local hollow = ""
  1159.         if sim_mode == false and cmd_line == false then
  1160.             writeOut("How deep does it need to be?")
  1161.             depth = io.read()
  1162.             writeOut("How wide does it need to be?")
  1163.             width = io.read()
  1164.             writeOut("How high does it need to be?")
  1165.             height = io.read()
  1166.             writeOut("Does it need to be hollow? (y/n)")
  1167.             hollow = io.read()
  1168.         elseif sim_mode == true or cmd_line == true then
  1169.             depth = tempProgTable.param1
  1170.             width = tempProgTable.param2
  1171.             height = tempProgTable.param3
  1172.             hollow = tempProgTable.param4
  1173.         end
  1174.         depth = tonumber(depth)
  1175.         width = tonumber(width)
  1176.         height = tonumber(height)
  1177.         tempProgTable.param1 = depth
  1178.         tempProgTable.param2 = width
  1179.         tempProgTable.param3 = height
  1180.         tempProgTable.param4 = hollow
  1181.         if height < 3 then
  1182.             height = 3
  1183.         end
  1184.         if depth < 3 then
  1185.             depth = 3
  1186.         end
  1187.         if width < 3 then
  1188.             width = 3
  1189.         end
  1190.         progTable = {param1 = depth, param2 = width, param3 = height}
  1191.         platform(depth, width)     
  1192.         while (facing > 0) do
  1193.             turnLeftTrack()
  1194.         end
  1195.         turnAroundTrack()
  1196.         if ((width % 2) == 0) then
  1197.             -- This is for reorienting the turtle to build the walls correct in relation to the floor and ceiling
  1198.             turnLeftTrack()
  1199.         end
  1200.         if not(hollow == "n") then
  1201.             for i = 1, height - 2 do
  1202.                 safeUp()
  1203.                 if ((width % 2) == 0) then -- This as well
  1204.                 rectangle(depth, width)
  1205.                 else
  1206.                 rectangle(width, depth)
  1207.                 end
  1208.             end
  1209.         else
  1210.             for i = 1, height - 2 do
  1211.                 safeUp()
  1212.                 platform(depth,width)
  1213.             end
  1214.         end
  1215.         safeUp()
  1216.         platform(depth, width)
  1217.     end
  1218.     if choice == "1/2-sphere" or choice == "1/2 sphere" then
  1219.         local radius = 0
  1220.         local half = ""
  1221.         if sim_mode == false and cmd_line == false then
  1222.             writeOut("What radius does it need to be?")
  1223.             radius = io.read()
  1224.             writeOut("What half of the sphere does it need to be?(bottom/top)")
  1225.             half = io.read()
  1226.         elseif sim_mode == true or cmd_line == true then
  1227.             radius = tempProgTable.param1
  1228.             half = tempProgTable.param2
  1229.         end
  1230.         radius = tonumber(radius)
  1231.         tempProgTable.param1 = radius
  1232.         tempProgTable.param2 = half
  1233.         progTable = {param1 = radius, param2 = half}
  1234.         half = string.lower(half)
  1235.         if half == "bottom" then
  1236.             dome("bowl", radius)
  1237.         else
  1238.             dome("dome", radius)
  1239.         end
  1240.     end
  1241.     if choice == "dome" then
  1242.         local radius = 0
  1243.         if sim_mode == false and cmd_line == false then
  1244.             writeOut("What radius does it need to be?")
  1245.             radius = io.read()
  1246.         elseif sim_mode == true or cmd_line == true then
  1247.             radius = tempProgTable.param1
  1248.         end
  1249.         radius = tonumber(radius)
  1250.         tempProgTable.param1 = radius
  1251.         progTable = {param1 = radius}
  1252.         dome("dome", radius)
  1253.     end
  1254.     if choice == "bowl" then
  1255.         local radius = 0
  1256.         if sim_mode == false and cmd_line == false then
  1257.             writeOut("What radius does it need to be?")
  1258.             radius = io.read()
  1259.         elseif sim_mode == true or cmd_line == true then
  1260.             radius = tempProgTable.param1
  1261.         end
  1262.         radius = tonumber(radius)
  1263.         tempProgTable.param1 = radius
  1264.         progTable = {param1 = radius}
  1265.         dome("bowl", radius)
  1266.     end
  1267.     if choice == "circle" then
  1268.         local radius = 0
  1269.         if sim_mode == false and cmd_line == false then
  1270.             writeOut("What radius does it need to be?")
  1271.             radius = io.read()
  1272.         elseif sim_mode == true or cmd_line == true then
  1273.             radius = tempProgTable.param1
  1274.         end
  1275.         radius = tonumber(radius)
  1276.         tempProgTable.param1 = radius
  1277.         progTable = {param1 = radius}
  1278.         circle(radius)
  1279.     end
  1280.     if choice == "cylinder" then
  1281.         local radius = 0
  1282.         local height = 0
  1283.         if sim_mode == false and cmd_line == false then
  1284.             writeOut("What radius does it need to be?")
  1285.             radius = io.read()
  1286.             writeOut("What height does it need to be?")
  1287.             height = io.read()
  1288.         elseif sim_mode == true or cmd_line == true then
  1289.             radius = tempProgTable.param1
  1290.             height = tempProgTable.param2
  1291.         end
  1292.         radius = tonumber(radius)
  1293.         height = tonumber(height)
  1294.         tempProgTable.param1 = radius
  1295.         tempProgTable.param2 = height
  1296.         progTable = {param1 = radius, param2 = height}
  1297.         for i = 1, height do
  1298.             circle(radius)
  1299.             safeUp()
  1300.         end
  1301.     end
  1302.     if choice == "pyramid" then
  1303.         local length = 0
  1304.         local hollow = ""
  1305.         if sim_mode == false and cmd_line == false then
  1306.             writeOut("What depth/width does it need to be?")
  1307.             length = io.read()
  1308.             writeOut("Does it need to be hollow [y/n]?")
  1309.             hollow = io.read()
  1310.         elseif sim_mode == true or cmd_line == true then
  1311.             length = tempProgTable.param1
  1312.             hollow = tempProgTable.param2
  1313.         end
  1314.         length = tonumber(length)
  1315.         tempProgTable.param1 = length
  1316.         tempProgTable.param2 = hollow
  1317.         progTable = {param1 = length, param2 = hollow}
  1318.         if hollow == 'y' or hollow == 'yes' or hollow == 'true' then
  1319.             hollow = true
  1320.         else
  1321.             hollow = false
  1322.         end
  1323.         height = math.ceil(length / 2)
  1324.         for i = 1, height do
  1325.             if hollow then
  1326.                 rectangle(length, length)
  1327.             else
  1328.                 platform(length, length)
  1329.                 navigateTo(0,0)
  1330.                 while facing ~= 0 do
  1331.                     turnRightTrack()
  1332.                 end
  1333.             end
  1334.             if i ~= height then
  1335.                 safeUp()
  1336.                 safeForward()
  1337.                 turnRightTrack()
  1338.                 safeForward()
  1339.                 turnLeftTrack()
  1340.                 length = length - 2
  1341.             end
  1342.         end
  1343.     end
  1344.     if choice == "sphere" then
  1345.         local radius = 0
  1346.         if sim_mode == false and cmd_line == false then
  1347.             writeOut("What radius does it need to be?")
  1348.             radius = io.read()
  1349.         elseif sim_mode == true or cmd_line == true then
  1350.             radius = tempProgTable.param1
  1351.         end
  1352.         radius = tonumber(radius)
  1353.         tempProgTable.param1 = radius
  1354.         progTable = {param1 = radius}
  1355.         dome("sphere", radius)
  1356.     end
  1357.     if choice == "hexagon" then
  1358.         local length = 0
  1359.         if sim_mode == false and cmd_line == false then
  1360.             writeOut("How long do each side need to be?")
  1361.             length = io.read()
  1362.         elseif sim_mode == true or cmd_line == true then
  1363.             length = tempProgTable.param1
  1364.         end
  1365.         length = tonumber(length)
  1366.         tempProgTable.param1 = length
  1367.         progTable = {param1 = length}
  1368.         hexagon(length)
  1369.     end
  1370.     if choice == "octagon" then
  1371.         local length = 0
  1372.         if sim_mode == false and cmd_line == false then
  1373.             writeOut("How long do each side need to be?")
  1374.             length = io.read()
  1375.         elseif sim_mode == true or cmd_line == true then
  1376.             length = tempProgTable.param1
  1377.         end
  1378.         length = tonumber(length)
  1379.         tempProgTable.param1 = length
  1380.         progTable = {param1 = length}
  1381.         octagon(length)
  1382.     end
  1383.     if choice == "6-prism" or choice == "6 prism" then
  1384.         local length = 0
  1385.         local height = 0
  1386.         if sim_mode == false and cmd_line == false then
  1387.             writeOut("How long do each side need to be?")
  1388.             length = io.read()
  1389.             writeOut("What height does it need to be?")
  1390.             height = io.read()
  1391.         elseif sim_mode == true or cmd_line == true then
  1392.             length = tempProgTable.param1
  1393.             height = tempProgTable.param2
  1394.         end
  1395.         length = tonumber(length)
  1396.         height = tonumber(height)
  1397.         tempProgTable.param1 = length
  1398.         tempProgTable.param2 = height
  1399.         progTable = {param1 = length, param2 = height}
  1400.         for i = 1, height do
  1401.             hexagon(length)
  1402.             safeUp()
  1403.         end
  1404.     end
  1405.     if choice == "8-prism" or choice == "8 prism" then
  1406.         local length = 0
  1407.         local height = 0
  1408.         if sim_mode == false and cmd_line == false then
  1409.             writeOut("How long do each side need to be?")
  1410.             length = io.read()
  1411.             writeOut("What height does it need to be?")
  1412.             height = io.read()
  1413.         elseif sim_mode == true or cmd_line == true then
  1414.             length = tempProgTable.param1
  1415.             height = tempProgTable.param2
  1416.         end
  1417.         length = tonumber(length)
  1418.         height = tonumber(height)
  1419.         tempProgTable.param1 = length
  1420.         tempProgTable.param2 = height
  1421.         progTable = {param1 = length, param2 = height}
  1422.         for i = 1, height do
  1423.             octagon(length)
  1424.             safeUp()
  1425.         end
  1426.     end
  1427.     if returntohome then
  1428.         goHome() -- After all shape building has finished
  1429.     end
  1430.     writeOut("Done") -- Saves a few lines when put here rather than in each if statement
  1431. end
  1432.  
  1433. function WriteMenu()
  1434.     term.clear()
  1435.     term.setCursorPos(1, 1)
  1436.     writeOut("Shape Maker 1.5 by Keridos/Happydude/pokemane")
  1437.     if resupply then                    -- Any ideas to make this more compact/betterlooking (in terms of code)?
  1438.         writeOut("Resupply Mode Active")
  1439.     elseif (resupply and can_use_gps) then
  1440.         writeOut("Resupply and GPS Mode Active")
  1441.     elseif can_use_gps then
  1442.         writeOut("GPS Mode Active")
  1443.     else
  1444.         writeOut("")
  1445.     end
  1446.     if not cmd_line then
  1447.         writeOut("What should be built? [page 1/2]");
  1448.         writeOut("next for page 2")
  1449.         writeOut("+---------+-----------+-------+-------+")
  1450.         writeOut("| square  | rectangle | wall  | line  |")
  1451.         writeOut("| cylinder| platform  | stair | cuboid|")
  1452.         writeOut("| pyramid | 1/2-sphere| circle| next  |")
  1453.         writeOut("+---------+-----------+-------+-------+")
  1454.         writeOut("")
  1455.     end
  1456. end
  1457.  
  1458. function WriteMenu2()
  1459.     term.clear()
  1460.     term.setCursorPos(1, 1)
  1461.     writeOut("Shape Maker 1.5 by Keridos/Happydude/pokemane")
  1462.     if resupply then                    -- Any ideas to make this more compact/betterlooking (in terms of code)?
  1463.         writeOut("Resupply Mode Active")
  1464.     elseif (resupply and can_use_gps) then
  1465.         writeOut("Resupply and GPS Mode Active")
  1466.     elseif can_use_gps then
  1467.         writeOut("GPS Mode Active")
  1468.     else
  1469.         writeOut("")
  1470.     end
  1471.     writeOut("What should be built [page 2/2]?");
  1472.     writeOut("")
  1473.     writeOut("+---------+-----------+-------+-------+")
  1474.     writeOut("| hexagon | octagon   | help  |       |")
  1475.     writeOut("| 6-prism | 8-prism   | end   |       |")
  1476.     writeOut("| sphere  | credits   |       |       |")
  1477.     writeOut("+---------+-----------+-------+-------+")
  1478.     writeOut("")
  1479. end
  1480.  
  1481. function showHelp()
  1482.     writeOut("Usage: shape [shape-type [param1 param2 param3 ...]] [-c] [-h] [-z] [-r]")
  1483.     writeOut("-c: Activate cost only mode")
  1484.     writeOut("-h: Show this page")
  1485.     writeOut("-z: Set chain_next_shape to true, lets you chain together multiple shapes")
  1486.     io.read() -- Pause here
  1487.     writeOut("-r: Resume the last shape if there are any (Note: This is disabled until we can iron out the kinks)")
  1488.     writeOut("shape-type can be any of the shapes in the menu")
  1489.     writeOut("After shape-type input all of the paramaters for the shape")
  1490.     io.read() -- Pause here, too
  1491. end
  1492.  
  1493. function showCredits()
  1494.     writeOut("Credits for the shape builder:")
  1495.     writeOut("Based on work by Michiel,Vliekkie, and Aeolun")
  1496.     writeOut("Sphere/dome code by pruby")
  1497.     writeOut("Additional improvements by Keridos,Happydude and pokemane")
  1498.     io.read() -- Pause here, too
  1499. end
  1500.  
  1501. function main()
  1502.     if wrapmodules()=="resupply" then
  1503.         linkToRSStation()
  1504.     end
  1505.     if checkCommandLine() then
  1506.         if needsHelp() then
  1507.             showHelp()
  1508.             return -- Close the program after help info is shown
  1509.         end
  1510.         setFlagsFromCommandLine()
  1511.         setTableFromCommandLine()
  1512.     end
  1513.     if (CheckForPrevious()) then  -- Will check to see if there was a previous job and gps is enabled, and if so, ask if the user would like to re-initialize to current progress status
  1514.         if not continueQuery() then -- If the user doesn't want to continue
  1515.             ProgressFileDelete()
  1516.             setSimFlags(false) -- Just to be safe
  1517.             WriteMenu()
  1518.             choiceFunction()
  1519.         else    -- If the user wants to continue
  1520.             setSimFlags(true)
  1521.             choiceFunction()
  1522.         end
  1523.     else
  1524.         setSimFlags(false)
  1525.         WriteMenu()
  1526.         choiceFunction()
  1527.     end
  1528.     if (blocks ~= 0) and (fuel ~= 0) then -- Do not show on help or credits page or when selecting end
  1529.         writeOut("Blocks used: " .. blocks)
  1530.         writeOut("Fuel used: " .. fuel)
  1531.     end
  1532.     ProgressFileDelete() -- Removes file upon successful completion of a job, or completion of a previous job.
  1533.     progTable = {}
  1534.     tempProgTable = {}
  1535. end
  1536.  
  1537. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement