Advertisement
Munchester

auto5000

Oct 21st, 2013
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.95 KB | None | 0 0
  1. -- CONFIG
  2. local selection = 1
  3. local selections = 0
  4. local event, p1, p2, p3, p4, p5 = 0, 0, 0, 0, 0, 0
  5.  
  6. local state = "init"
  7. local menu = {}
  8. local work = {}
  9. local fuel = 0
  10.  
  11.  
  12. -- Colors
  13. local isAdvanced = term.isColor and term.isColor()
  14. local tBarBg = 0
  15. local tBarTx = 0
  16. if isAdvanced then
  17.   -- Color scheme for advanced computers
  18. else
  19.   -- Color scheme for basic computers and turtles
  20.   tBarBg = colors.white
  21.   tBarTx = colors.black
  22.   stdBg = colors.black
  23.   stdTx = colors.white
  24.   btnSelectBg = colors.white
  25.   btnSelectTx = colors.black
  26.   btnNormBg = colors.black
  27.   btnNormTx = colors.white
  28. end
  29.  
  30. function clearScreen()
  31.   term.setBackgroundColor(stdBg)
  32.   term.setTextColor(stdTx)
  33.   term.clear()
  34.   term.setCursorPos(1,1)
  35. end
  36. function drawTitleBar(extraLabel)
  37.   term.setBackgroundColor(tBarBg)
  38.   term.setTextColor(tBarTx)
  39.   term.clearLine()
  40.   term.setCursorPos(3,1)
  41.   if extraLabel ~= "" then
  42.     print("Automaton 5000 - " .. extraLabel)
  43.   else
  44.     print("Automaton 5000")
  45.   end
  46. end
  47. function drawStatusBar(message)
  48.   term.setBackgroundColor(tBarBg)
  49.   term.setTextColor(tBarTx)
  50.   term.setCursorPos(1,13)
  51.   term.clearLine()
  52.   io.write(message)
  53. end
  54. function drawButton(pos, label)
  55.   if pos == selection then
  56.     term.setBackgroundColor(btnSelectBg)
  57.     term.setTextColor(btnSelectTx)
  58.   else
  59.     term.setBackgroundColor(btnNormBg)
  60.     term.setTextColor(btnNormTx)
  61.   end
  62.   if pos == 1 then
  63.     term.setCursorPos(3,3)
  64.   end
  65.   if pos == 2 then
  66.     term.setCursorPos(3,5)
  67.   end
  68.   if pos == 3 then
  69.     term.setCursorPos(3,7)
  70.   end
  71.   if pos == 4 then
  72.     term.setCursorPos(3,9)
  73.   end
  74.   if pos == 5 then
  75.     term.setCursorPos(3,11)
  76.   end
  77.   print("> " .. label)
  78. end
  79. function drawConfirmation(pos, label)
  80.   if pos == selection then
  81.     term.setBackgroundColor(btnSelectBg)
  82.     term.setTextColor(btnSelectTx)
  83.   else
  84.     term.setBackgroundColor(btnNormBg)
  85.     term.setTextColor(btnNormTx)
  86.   end
  87.   if pos == 1 then
  88.     term.setCursorPos(3,11)
  89.   end
  90.   if pos == 2 then
  91.     term.setCursorPos(15,11)
  92.   end
  93.   print("> " .. label)
  94. end
  95. function drawDescription()
  96.   term.setBackgroundColor(stdBg)
  97.   term.setTextColor(stdTx)
  98.   term.setCursorPos(1,2)
  99.   print(menu.description)
  100. end
  101. function toggleConfirmation()
  102.   if selection == 1 then
  103.     selection = 2
  104.   else
  105.     selection = 1
  106.   end
  107. end
  108.  
  109. function place(slotNum)
  110.   turtle.select(slotNum)
  111.   turtle.placeDown()
  112. end
  113. function forward()
  114.   checkFuel()
  115.   if not turtle.forward() then
  116.     turtle.dig()
  117.     return forward()
  118.   end
  119. end
  120. function up()
  121.   checkFuel()
  122.   if not turtle.up() then
  123.     turtle.digUp()
  124.     return up()
  125.   end
  126. end
  127. function turnRight()
  128.   turtle.turnRight()
  129. end
  130. function turnLeft()
  131.   turtle.turnLeft()
  132. end
  133. function checkFuel()
  134.   fuel = turtle.getFuelLevel()
  135.   --print(fuel)
  136.   if fuel == 0 then
  137.     turtle.select(16)
  138.       turtle.refuel(1)
  139.   end
  140.   fuel = turtle.getFuelLevel()
  141.   if fuel == 0 then
  142.     error("Ran out of fuel")
  143.   end
  144.   if turtle.getItemCount(16) < 2 then
  145.     fuelLow = 1
  146.   else
  147.     fuelLow = 0
  148.   end
  149.   if fuelLow == 1 then
  150.     print("Fuel is low...")
  151.   end
  152. end
  153. function placeRectangle(width, length, slot)
  154.   local dir = true
  155.   for i = 1,width do
  156.     for j = 1,length do
  157.       place(slot)
  158.       if j ~= length then
  159.         forward()
  160.       end
  161.     end
  162.     if i ~= width then
  163.       dir = moveOver(dir)
  164.     end
  165.   end
  166. end
  167. function moveOver(dir)
  168.   if dir then
  169.     turnRight()
  170.     forward()
  171.     turnRight()
  172.   else
  173.     turnLeft()
  174.     forward()
  175.     turnLeft()
  176.   end
  177.   return not dir
  178. end
  179. function mineForwards()
  180.   while turtle.detect() do
  181.     turtle.dig()
  182.     sleep(0.9)
  183.   end
  184.   forward()
  185.   while turtle.detectUp() do
  186.     turtle.digUp()
  187.     sleep(0.9)
  188.   end
  189.   turtle.digDown()
  190. end
  191. function compare(slot)
  192.   turtle.select(slot)
  193.   return turtle.compare()
  194. end
  195.  
  196. -- Mining functions
  197. function tunnel(tunnelWidth)
  198.   tunnelWidth = tunnelWidth or 2
  199.   while true do
  200.     mineForwards()
  201.     if tunnelWidth > 1 then
  202.       turnRight()
  203.       for i = 2,tunnelWidth do
  204.         mineForwards()
  205.       end
  206.       turnLeft()
  207.       mineForwards()
  208.       turnLeft()
  209.       for i = 2,tunnelWidth do
  210.         mineForwards()
  211.       end
  212.       turnRight()
  213.     end
  214.   end
  215. end
  216.  
  217. -- Tree Farm
  218. function killTree()
  219.   local offset = 1
  220.   mineForwards()
  221.   up()
  222.   while turtle.detectUp() do
  223.     for i = 1, 4 do
  224.       turnRight()
  225.       turtle.dig()
  226.     end
  227.     up()
  228.     offset = offset + 1
  229.   end
  230.   while offset > 0 do
  231.     turtle.down()
  232.     offset = offset - 1
  233.   end
  234.   place(14) -- replant
  235.   turnRight()
  236.   turnRight()
  237.   forward()
  238.   turnRight()
  239.   turnRight()
  240. end
  241. function patrolForTrees()
  242.   up()
  243.   up()
  244.   turnLeft()
  245.   forward()
  246.   forward()
  247.   turnRight()
  248.   forward()
  249.   forward()
  250.   --first nodes
  251.   for i = 1, 5 do
  252.     turnLeft()
  253.     if compare(15) then
  254.       killTree()
  255.     end
  256.     turnRight()
  257.     turnRight()
  258.     if compare(15) then
  259.       killTree()
  260.     end
  261.     turnLeft()
  262.     forward()
  263.     forward()
  264.   end
  265.   turnRight()
  266.   for i = 1, 4 do
  267.     forward()
  268.   end
  269.   turnRight()
  270.   forward()
  271.   forward()
  272.   for i = 1, 5 do
  273.     turnLeft()
  274.     if compare(15) then
  275.       killTree()
  276.     end
  277.     turnRight()
  278.     turnRight()
  279.     if compare(15) then
  280.       killTree()
  281.     end
  282.     turnLeft()
  283.     forward()
  284.     forward()
  285.   end
  286.   turnRight()
  287.   forward()
  288.   forward()
  289.   turtle.down()
  290.   turtle.down()
  291.   turnRight()
  292. end
  293. function farmTrees()
  294.   while true do
  295.     patrolForTrees()
  296.     sleep(100)
  297.   end
  298. end
  299.  
  300. -- Building functions
  301. function buildHut()
  302.   up()
  303.   -- Floor
  304.   placeRectangle(7,7,1)
  305.   up()
  306.   -- Walls
  307.   turtle.turnRight()
  308.   turtle.turnRight()
  309.   for i = 1,3 do
  310.     for j = 1,4 do
  311.       place(2)
  312.       forward()
  313.       placeRectangle(1,5,3)
  314.       forward()
  315.       place(2)
  316.       turtle.turnRight()
  317.     end
  318.     up()
  319.   end
  320.   -- Ceiling
  321.   placeRectangle(7,7,4)
  322. end
  323. function buildFarm()
  324.   -- Floor
  325.   up()
  326.   placeRectangle(11,5,1)
  327.   forward()
  328.   turnLeft()
  329.   placeRectangle(5,11,2)
  330.   turnRight()
  331.   forward()
  332.   turnRight()
  333.   placeRectangle(1,9,1)
  334.   forward()
  335.   placeRectangle(1,2,2)
  336.   -- Dirt/water layer
  337.   up()
  338.   turnRight()
  339.   for i = 1, 4 do
  340.     placeRectangle(1,11,3)
  341.     turnRight()
  342.   end
  343.   turnRight()
  344.   forward()
  345.   turnLeft()
  346.   forward()
  347.   placeRectangle(4,9,4)
  348.   turnLeft()
  349.   forward()
  350.   turnLeft()
  351.   placeRectangle(5,4,4)
  352.   forward()
  353.   turnLeft()
  354.   placeRectangle(5,4,5)
  355.   forward()
  356.   turnLeft()
  357.   placeRectangle(1,4,5)
  358.   forward()
  359.   place(6)
  360.   -- Walls/fences
  361.   up()
  362.   for i = 1, 5 do
  363.     forward()
  364.   end
  365.   turnRight()
  366.   for i = 1, 5 do
  367.     forward()
  368.   end
  369.   turnRight()
  370.   for i = 1, 4 do
  371.     placeRectangle(1,11,7)
  372.     turnRight()
  373.   end
  374.   -- Wall/fence cap
  375.   up()
  376.   for i = 1, 4 do
  377.     placeRectangle(1,11,8)
  378.     turnRight()
  379.   end
  380. end
  381. function buildTreeFarm()
  382.   -- Main layer
  383.   up()
  384.   for i = 1, 4 do
  385.     placeRectangle(1,11,1)
  386.     turnRight()
  387.   end
  388.   turnRight()
  389.   forward()
  390.   turnLeft()
  391.   forward()
  392.   placeRectangle(7,9,2)
  393.   turnRight()
  394.   forward()
  395.   placeRectangle(9,2,3)
  396. end
  397.  
  398. local menus = {
  399.   -- high level options
  400.   [0] = {
  401.     title = "Main Menu",
  402.     type = "menu",
  403.     options = {1, 2, 3, 4}
  404.   },
  405.   [1] = {
  406.     title = "Resource Gathering",
  407.     type = "menu",
  408.     options = {11, 12, 13, 0}
  409.   },
  410.   [2] = {
  411.     title = "Buildings",
  412.     type = "menu",
  413.     options = {21, 22, 23, 24, 0}
  414.   },
  415.   [3] = {
  416.     title = "Miscellaneous",
  417.     type = "menu",
  418.     options = {31, 0}
  419.   },
  420.   [4] = {
  421.     title = "Exit",
  422.     type = "menu",
  423.     options = {-1}
  424.   },
  425.   -- resource options
  426.   [11] = {
  427.     title = "Mining",
  428.     type = "menu",
  429.     options = {111, 112, 113, 1}
  430.   },
  431.   [12] = {
  432.     title = "Tree Farming",
  433.     type = "menu",
  434.     options = {121, 1}
  435.   },
  436.   [121] = {
  437.     title = "Chopping trees",
  438.     type = "work",
  439.     options = {4},
  440.     work = farmTrees
  441.   },
  442.   [13] = {
  443.     title = "Crops (no)",
  444.     type = "menu",
  445.     options = {0}
  446.   },
  447.   -- buildings options
  448.   [21] = {
  449.     title = "Hut",
  450.     type = "menu",
  451.     options = {211, 2}
  452.   },
  453.   [22] = {
  454.     title = "Farm",
  455.     type = "menu",
  456.     options = {221, 2}
  457.   },
  458.   [23] = {
  459.     title = "Tree Farm",
  460.     type = "menu",
  461.     options = {231, 2}
  462.   },
  463.   [24] = {
  464.     title = "Tower",
  465.     type = "menu",
  466.     options = {241, 2}
  467.   },
  468.   -- miscellaneous
  469.   [31] = {
  470.     title = "Miscellaneous",
  471.     type = "menu",
  472.     options = {0}
  473.   },
  474.   -- Mining options
  475.   [111] = {
  476.     title = "Branch (no)",
  477.     type = "menu",
  478.     options = {0}
  479.   },
  480.   [112] = {
  481.     title = "Excavate (no)",
  482.     type = "menu",
  483.     options = {0}
  484.   },
  485.   [113] = {
  486.     title = "Tunnel",
  487.     type = "confirm",
  488.     options = {1131,11},
  489.     description = [[
  490. Gonna dig me a tunnel
  491. a nice long tunnel...
  492. gonna be 2x3 and go on
  493. forever...
  494. ]]
  495.   },
  496.   [1131] = {
  497.     title = "Tunnelling",
  498.     type = "work",
  499.     options = {4},
  500.     work = tunnel
  501.   },
  502.   -- Buildings to build
  503.   [211] = {
  504.     title = "Build Hut",
  505.     type = "confirm",
  506.     options = {2111, 21},
  507.     description = [[
  508. This will build a simple 7x7x5 hut.
  509. Fuel goes in slot #16.
  510. Floor pieces go in slot #1.
  511. Corner pieces go in slot #2.
  512. Wall pieces go in slot #3.
  513. Ceiling pieces go in slot #4.
  514. Door goes in slot #5.
  515. Turtle starts in lower-left corner.
  516. ]]
  517.   },
  518.   [2111] = {
  519.     title = "Building a hut...",
  520.     type = "work",
  521.     options = {4},
  522.     work = buildHut
  523.   },
  524.   [221] = {
  525.     title = "Build Farm",
  526.     type = "confirm",
  527.     options = {2211, 21},
  528.     description = [[
  529. This will build a 11x11x5 farm.
  530. Materials needed:
  531. 64F| 57F| 44W| 64D
  532. 64D| 1 W| 64W| 64W
  533.  - | -  | -  | -
  534.  - | -  | -  | *
  535. Turtle starts in lower-left corner.
  536. ]]
  537.   },
  538.   [2211] = {
  539.     title = "Building a farm...",
  540.     type = "work",
  541.     options = {4},
  542.     work = buildFarm
  543.   },
  544.   [231] = {
  545.     title = "Build Tree Farm",
  546.     type = "confirm",
  547.     options = {2311, 21},
  548.     description = [[
  549. This will build a 11x11x1 tree farm.
  550. Materials needed:
  551. 64W| 64D| 64D| -
  552.  - | -  | -  | -
  553.  - | -  | -  | -
  554.  - | -  | -  | *
  555. Turtle starts in lower-left corner.
  556. ]]
  557.   },
  558.   [2311] = {
  559.     title = "Building a tree farm...",
  560.     type = "work",
  561.     options = {4},
  562.     work = buildTreeFarm
  563.   },
  564.   [241] = {
  565.     title = "Build Tower",
  566.     type = "confirm",
  567.     options = {2411, 21},
  568.     description = [[
  569. This will build a 5x5x10 tower.
  570. Materials needed:
  571.  - | -  | -  | -
  572.  - | -  | -  | -
  573.  - | -  | -  | -
  574.  - | -  | -  | *
  575. Turtle starts in lower-left corner.
  576. ]]
  577.   },
  578.   [2411] = {
  579.     title = "Building a tower...",
  580.     type = "work",
  581.     options = {4},
  582.     work = buildTower
  583.   }
  584. }
  585. local gui = {
  586.   menu = function(m)
  587.     return menus[m]
  588.   end
  589. }
  590.  
  591.  
  592. function tableLength(T)
  593.   local count = 0
  594.   for _ in pairs(T) do count = count + 1 end
  595.   return count
  596. end
  597.  
  598. function redrawGui()
  599.   -- Settings
  600.   selections = tableLength(menu.options)
  601.   -- Reset colors
  602.   clearScreen()
  603.   -- Title bar
  604.   drawTitleBar(menu.title)
  605.   -- Buttons
  606.   if menu.type == "menu" then
  607.     for i,v in ipairs(menu.options) do
  608.       drawButton(i, menus[v].title)
  609.     end
  610.   end
  611.   if menu.type == "confirm" then
  612.     drawDescription(menu.description)
  613.     drawConfirmation(1, "Yes")
  614.     drawConfirmation(2, "No")
  615.   end
  616.   if menu.type == "work" then
  617.   end
  618.   -- Status bar
  619.   drawStatusBar(menu.title .. " " .. selection .. " / " .. selections)
  620. end
  621.  
  622. function getMenuKeys()
  623.   while true do
  624.     event, p1, p2, p3, p4, p5 = os.pullEvent()
  625.     if event == "key" then
  626.       if p1 == 200 then --Up arrow
  627.         if selection > 1 then
  628.           selection = selection - 1
  629.         else
  630.           selection = selections
  631.         end
  632.         break
  633.       end
  634.       if p1 == 203 then --Left arrow
  635.         if menu.type == "confirm" then
  636.           toggleConfirmation()
  637.         end
  638.         break
  639.       end
  640.       if p1 == 205 then --Right arrow
  641.         if menu.type == "confirm" then
  642.           toggleConfirmation()
  643.         end
  644.         break
  645.       end
  646.       if p1 == 208 then --Down arrow
  647.         if selection < selections then
  648.           selection = selection + 1
  649.         else
  650.           selection = 1
  651.         end
  652.         break
  653.       end
  654.       if p1 == 28 then --Return
  655.         if menu.options[selection] == 4 then
  656.           return false
  657.         end
  658.         menu = menus[menu.options[selection]]
  659.         selection = 1
  660.         if menu.type == "work" then
  661.           --state = "working"
  662.           menu.work()
  663.         end
  664.         break
  665.       end
  666.     end
  667.   end
  668.  
  669.   return true
  670. end
  671.  
  672. -- Main loop
  673. function main()
  674.   while true do
  675.     if state == "init" then
  676.       menu = gui.menu(0)
  677.       redrawGui()
  678.       selection = 1
  679.       state = "menus"
  680.     end
  681.     if state == "menus" then
  682.       redrawGui()
  683.       if getMenuKeys() == false then
  684.         break
  685.       end
  686.     end
  687.   end
  688. end
  689. -- Cleanup function - Leaves the user with a clear screen
  690. function cleanup()
  691.   term.setBackgroundColor(colors.black)
  692.   term.setTextColor(colors.white)
  693.   term.clear()
  694.   term.setCursorPos(1,1)
  695. end
  696.  
  697.  
  698.  
  699.  
  700.  
  701. -- mainMenu()
  702. -- Call Main Loop and cleanup once it has finished
  703. main()
  704. cleanup()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement