Advertisement
BrunoZockt

OCM-BetaV2.6

Mar 1st, 2017
883
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 90.54 KB | None | 0 0
  1. --[[ORE COLLECTING MINER V2.5 by BrunoZockt
  2.  
  3. MIT License
  4.  
  5. Copyright (c) 2018 Bruno Heberle
  6.  
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13.  
  14. The above copyright notice and this permission notice shall be included in all
  15. copies or substantial portions of the Software.
  16.  
  17. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. SOFTWARE.
  24.  
  25.  
  26. V1.0 released 22.07.2016 17kb
  27. V1.1 released 16.08.2016 17kb
  28. V2.0 released 07.10.2017 35kb
  29. V2.1 released 15.12.2017 40kb
  30. V2.2 released 17.12.2017 42kb
  31. V2.3 released 11.02.2018 46kb
  32. V2.4 released 19.06.2018 57kb
  33. V2.5 released 15.07.2018 68kb
  34. This program is a work in progress and
  35. will automatically update itself.
  36. If you have any problems, questions or
  37. suggestions I'd be happy if you wrote
  38. an e-mail to 8run0z0ckt@gmail.com.
  39. Feel free to use this program whenever
  40. you want. If you want to use parts of
  41. my program for developing, do so but
  42. don't say it's yours - a sidenote
  43. e.g. "by BrunoZockt" would be nice :)
  44. If you want to understand this program
  45. I recommend to read the variables first
  46. and then the rest of the code from the
  47. bottom to the top! Maybe I will add
  48. some comments for better understanding
  49. soon! The names of my functions and
  50. variables may sound strange because
  51. some are german and others are not
  52. really creative, but I hope you
  53. understand it anyways ;) ]]--
  54.  
  55. local HOST_ENV = _ENV or getfenv()
  56. local OUR_ENV = {}
  57.  
  58. setmetatable(OUR_ENV, {__index = HOST_ENV, OUR_ENV = OUR_ENV})
  59. setfenv(1, OUR_ENV)
  60.  
  61. -----###-----configurable variables-----###-----
  62.  
  63. settings = {
  64.   ["language"] = "en",
  65.   ["tunnelspace"] = 4,
  66.   ["quantity"] = 10,
  67.   ["length"] = 8,
  68.   ["ignor"] = {"minecraft:stone", "minecraft:cobblestone", "minecraft:dirt", "minecraft:lava", "minecraft:flowing_lava", "minecraft:water", "minecraft:flowing_water", "minecraft:torch"},
  69.   ["chestSelect"] = 3,
  70.   ["torches"] = true,
  71.   ["mainTorches"] = true,
  72.   ["lateralTorches"] = 1,
  73.   ["Autofuel"] = true,
  74.   ["floor"] = true,
  75.   ["trash"] = false,
  76.   ["walls"] = false
  77. }
  78.  
  79. -----###-----constants-----###-----
  80.  
  81. local programName = shell.getRunningProgram()
  82. local label = os.getComputerLabel() or "the turtle"
  83. local chestSlot = 16
  84. local fuelLevel = turtle.getFuelLevel()
  85. local w, h = term.getSize()
  86. local kill = false
  87. chestList = {"minecraft:chest", "IronChest:BlockIronChest", "IronChest:BlockIronChest:1", "IronChest:BlockIronChest:2", "IronChest:BlockIronChest:3", "IronChest:BlockIronChest:4", "IronChest:BlockIronChest:5", "IronChest:BlockIronChest:6"}
  88. enderChestList = {"EnderStorage:enderChest", "ThermalExpansion:Strongbox", "ThermalExpansion:Tesseract"}
  89. cardinal = {"North", "West", "South", "East"}
  90.  
  91. -----###-----mining variables-----###-----
  92.  
  93. variables = {
  94.   ["searching"] = false,
  95.   ["level"] = 0,
  96.   ["direction"] = 1,
  97.   ["cache"] = {},
  98.   ["maxSpace"] = false,
  99.   ["setup"] = false,
  100.   ["index"] = 2,
  101.   ["slots"] = {"_","_","_","_","_","_","_","_","_","_","_","_","_","_","_","_"},
  102.   ["torchPositions"] = {},
  103.   ["stats"] = {
  104.     ["dug"] = 0,
  105.     ["ores"] = 0,
  106.     ["time"] = 0,
  107.     ["moves"] = 0
  108.   },
  109.   ["TorchDemand"] = 0,
  110.   ["orientation"] = {},
  111.   ["CrosswayAmount"] = 0,
  112.   ["FuelDemand"] = 0,
  113.   ["startDay"] = 0,
  114.   ["startTime"] = 0
  115. }
  116.  
  117. -----###-----menu variables-----###-----
  118.  
  119. local select = 1
  120. local menustate
  121. local checkbox = false
  122. local gone = {}
  123. local timer = {}
  124. local chest = {"enderchest", "chest", "none"}
  125. local objects = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}
  126. local errors = {false, false, false, false, false, false, false, false}
  127. local Check = {["Fuellevel"] = false, ["torches"] = false, ["chest"] = false}
  128.  
  129. local menuVars = {
  130.   ["notificationCenter"] = {
  131.     time = 0
  132.   },
  133.   ["drawOptions"] = {
  134.     shift = 0,
  135.     shift2 = 0,
  136.     saved = false,
  137.     scroll = 0,
  138.     scroll2 = 0,
  139.     focus = 100,
  140.     ypos
  141.   },
  142.   ["drawPopup"] = {
  143.     scroll = 0,
  144.     lastscroll = 0,
  145.     ypos = 9
  146.   },
  147.   ["drawNews"] = {
  148.     scroll = 0
  149.   }
  150. }
  151.  
  152.  
  153. -----###-----helpful functions-----###-----
  154.  
  155. function Sprint(str, xpos, ypos)
  156.   term.setCursorPos(xpos, ypos)
  157.   term.write(str)
  158. end
  159.  
  160. function printCentered(str, ypos)
  161.   term.setCursorPos(w/2 - #str/2 + 1, ypos)
  162.   term.write(str)
  163. end
  164.  
  165. function printRight(str, ypos, xoffset)
  166.   if xoffset == nil then
  167.     xoffset = 0
  168.   end
  169.   term.setCursorPos(w - xoffset - (#str - 1), ypos)
  170.   term.write(str)
  171. end
  172.  
  173. function printLeft(str, ypos)
  174.   term.setCursorPos(1, ypos)
  175.   term.write(str)
  176. end
  177.  
  178. function printB(tab, y, stop, start)
  179.   if start == nil then
  180.     start = 1
  181.   end
  182.   if stop == nil then
  183.     stop = w
  184.   end
  185.   local letters = 0
  186.   for i in pairs(tab) do
  187.     letters = letters + #tab[i]
  188.   end
  189.   local space = (stop-start)+1-letters
  190.   term.setCursorPos(start, y)
  191.   if (space/(#tab+1))%1 == 0 then
  192.     for i in ipairs(tab) do
  193.       term.write(string.rep(" ", space/(#tab+1))..tab[i])
  194.     end
  195.   elseif (space/(#tab))%2 == 0 then
  196.     term.write(string.rep(" ", (space/#tab)/2))
  197.     for i in ipairs(tab) do
  198.       term.write(tab[i]..string.rep(" ", space/#tab))
  199.     end
  200.   elseif space == #tab-1 then
  201.     term.write(tab[1])
  202.     for i = 2, #tab do
  203.       term.write(" "..tab[i])
  204.     end
  205.   else
  206.     for i in ipairs(tab) do
  207.       term.write(string.rep(" ", round(space/(#tab-i+2), "u"))..tab[i])
  208.       space = space - round(space/(#tab-i+2), "u")
  209.     end
  210.   end
  211. end
  212.  
  213. function printWrapped(tab, xpos, ypos, Space, sep)
  214.   local sep = sep or " "
  215.   local leftSpace = Space
  216.   local newxpos = xpos
  217.   for word = 1, #tab do
  218.     if Space < #tab[word] + #sep then
  219.       return false
  220.     elseif leftSpace >= #tab[word]+#sep then
  221.       Sprint(tab[word]..sep, newxpos, ypos)
  222.       leftSpace = leftSpace - (#tab[word]+#sep)
  223.       newxpos = newxpos + #tab[word]+#sep
  224.     else
  225.       ypos = ypos + 1
  226.       leftSpace = Space
  227.       Sprint(tab[word]..sep, xpos, ypos)
  228.       leftSpace = leftSpace - (#tab[word]+#sep)
  229.       newxpos = xpos + (#tab[word]+#sep)
  230.     end
  231.   end
  232.   return ypos
  233. end
  234.  
  235. function printLine(xpos, start, stop, point, endPoint)
  236.   for i = start, stop do
  237.     Sprint("|", xpos, i)
  238.   end
  239.   if point ~= nil then
  240.     Sprint(point, xpos, start)
  241.     Sprint((endPoint or point), xpos, stop)
  242.   end
  243. end
  244.  
  245. function Splitter(inputstr, sep)
  246.   if inputstr == nil then
  247.     return nil
  248.   elseif sep == nil then
  249.     sep = "%s"
  250.   end
  251.   local t, i = {}, 1
  252.   for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
  253.     t[i] = str
  254.     i = i + 1
  255.   end
  256.   return t
  257. end
  258.  
  259. local Version = tonumber(Splitter(os.version())[2])
  260.  
  261. function ItemCount(Block, Slot)
  262.   if Slot == 0 then
  263.     return false
  264.   elseif Slot == nil then
  265.     return false
  266.   end
  267.   local loops = 1
  268.   if type(Block) == "table" then
  269.     loops = #Block
  270.   end
  271.   if Version >= 1.64 then
  272.     for i = 1, loops do
  273.       local Data = turtle.getItemDetail(Slot)
  274.       if Data == nil then
  275.         return 0
  276.       elseif loops == 1 then
  277.         if Data.name == Block then
  278.           return Data.count
  279.         end
  280.       else
  281.         if Data.name == Block[i] then
  282.           return Data.count
  283.         end
  284.       end
  285.     end
  286.     return false
  287.   else
  288.     return turtle.getItemCount(Slot)
  289.   end
  290. end
  291.  
  292. function Color()
  293.   if Version <= 1.45 then
  294.     return false
  295.   elseif term.isColor() then
  296.     return true
  297.   else
  298.     return false
  299.   end
  300. end
  301.  
  302. function MoveObjects(deleted, mode)
  303.   if mode == 1 then
  304.     for k, v in ipairs(gone) do
  305.       if v == deleted then
  306.         return
  307.       end
  308.     end
  309.     for k, v in ipairs(objects) do
  310.       if k > deleted and k < 12 then
  311.         objects[k] = objects[k] - 1
  312.       end
  313.     end
  314.     table.insert(gone, deleted)
  315.   elseif mode == 2 then
  316.     for k, v in ipairs(gone) do
  317.       if v == deleted then
  318.         table.remove(gone, k)
  319.         for k, v in ipairs(objects) do
  320.           if k > deleted and k < 12 then
  321.             objects[k] = objects[k] + 1
  322.           end
  323.         end
  324.       end
  325.     end
  326.   end
  327. end
  328.  
  329. function OreCounter()
  330.   if variables.searching == true then
  331.     variables.stats["ores"] = variables.stats["ores"] + 1
  332.     if variables.stats["ores"] == 1 then
  333.       print(lang.status(settings.language, 1))
  334.     else
  335.       print(lang.status(settings.language, 2, variables.stats["ores"]))
  336.     end
  337.   end
  338. end
  339.  
  340. function SlotCalculator(code, param)
  341.   local output = {}
  342.   local item
  343.   if param ~= nil and param ~= "check" then
  344.     item = param
  345.   elseif param == "check" then
  346.     if code == "T" then
  347.       item = "minecraft:torch"
  348.     elseif code == "C" then
  349.       if settings.chestSelect == 1 then
  350.         item = enderChestList
  351.       elseif settings.chestSelect == 2 then
  352.         item = chestList
  353.       end
  354.     end
  355.   end
  356.   for n = 1, #variables.slots do
  357.     if variables.slots[n] == code then
  358.       if param == "empty" then
  359.         table.insert(output, n)
  360.       elseif param == nil then
  361.         if turtle.getItemCount(n) > 0 then
  362.           table.insert(output, n)
  363.         end
  364.       elseif ItemCount(item, n) ~= false and ItemCount(item, n) ~= 0 then
  365.         table.insert(output, n)
  366.       end
  367.     end
  368.   end
  369.   return output
  370. end
  371.  
  372. function refuel(Amount, Execute)
  373.   local ActualAmount = turtle.getItemCount()
  374.   if ActualAmount < Amount then
  375.     if Execute == true then
  376.       if turtle.refuel() == false then
  377.         return false
  378.       else
  379.         while turtle.refuel() ~= false do
  380.         end
  381.         return ActualAmount
  382.       end
  383.     else
  384.       return false
  385.     end
  386.   else
  387.     turtle.refuel(Amount)
  388.     return true
  389.   end
  390. end
  391.  
  392. function basicInputHandler(orientation, event, key)
  393.   if event == "key" then
  394.     if key == 28 then
  395.       menustate = menu[menustate].options[select]
  396.       select = 1
  397.     elseif orientation == "vertical" then
  398.       if key == 200 and select > 1 then
  399.         select = select-1
  400.       elseif key == 208 and select < #menu[menustate].options then
  401.         select = select + 1
  402.       end
  403.     else
  404.       if key == 203 and select > 1 then
  405.         select = select - 1
  406.       elseif key == 205 and select < #menu[menustate].options then
  407.         select = select + 1
  408.       end
  409.     end
  410.   end
  411. end
  412.  
  413. function save(path, content, todolist)
  414.     local file = fs.open(path, "w")
  415.   if todolist then
  416.     file.writeLine("todoList = "..textutils.serialize(content))
  417.   elseif type(content) == "table" then
  418.     for k, v in pairs(content) do
  419.       if type(content[k]) == "table" then
  420.         file.writeLine(k.." = "..textutils.serialize(content[k]))
  421.       else
  422.         file.writeLine(k.." = "..tostring(content[k]))
  423.       end
  424.     end
  425.   else
  426.       file.writeLine(content)
  427.   end
  428.     file.close()
  429. end
  430.  
  431. function translate(str, category)
  432.   if category == "layout" then
  433.     n = 35
  434.   end
  435.   for i = 1, n do
  436.     if str == lang[category]("en", i) then
  437.       return lang[category](settings.language, i)
  438.     end
  439.   end
  440. end
  441.  
  442. function round(int, mode, dez)
  443.   if dez == nil then
  444.     dez = 1
  445.   end
  446.   if mode == "d" then
  447.     mode = 0.4999
  448.   elseif mode == "u" then
  449.     mode = 0.5
  450.   end
  451.   return math.floor(int/dez+mode)*dez
  452. end
  453.  
  454. local function compile(chunk) -- returns compiled chunk or nil and error message
  455.   if type(chunk) ~= "string" then
  456.     error("expected string, got ".. type(chunk), 2)
  457.   end
  458.  
  459.   local function findChunkName(var)
  460.     for k,v in pairs(HOST_ENV) do
  461.       if v==var then
  462.         return k
  463.       end
  464.     end
  465.     return "Unknown chunk"
  466.   end
  467.  
  468.   return load(chunk, findChunkName(chunk), "t", OUR_ENV)
  469. end
  470.  
  471. function insert(code)
  472.   table.insert(todoList, variables.index, code)
  473.   variables.index = variables.index + 1
  474.   save("database/OCM/resume/todoList", todoList, true)
  475. end
  476.  
  477. -----###-----improved functions-----###-----
  478.  
  479. function dig(gravel)
  480.   local a = false
  481.   insert('OreCounter()')
  482.   variables.stats["dug"] = variables.stats["dug"] + 1
  483.   if gravel then
  484.     while turtle.dig() do
  485.       os.sleep(0.05)
  486.       a = true
  487.     end
  488.     return a
  489.   else
  490.     return turtle.dig()
  491.   end
  492. end
  493.  
  494. function digUp()
  495.   insert('OreCounter()')
  496.   variables.stats["dug"] = variables.stats["dug"] + 1
  497.   return turtle.digUp()
  498. end
  499.  
  500. function digDown()
  501.   insert('OreCounter()')
  502.   variables.stats["dug"] = variables.stats["dug"] + 1
  503.   return turtle.digDown()
  504. end
  505.  
  506. function face(int)
  507.   if int == variables.direction then
  508.     return
  509.   elseif (int + variables.direction)%2 == 0 then
  510.     return turn()
  511.   elseif math.abs(int - variables.direction) == 1 then
  512.     if variables.direction < int then
  513.       return left()
  514.     else
  515.       return right()
  516.     end
  517.   else
  518.     if variables.direction < int then
  519.       return right()
  520.     else
  521.       return left()
  522.     end
  523.   end
  524. end
  525.  
  526. function right()
  527.   turtle.turnRight()
  528.   variables.direction = variables.direction - 1
  529.   if variables.direction == 0 then
  530.     variables.direction = 4
  531.   end
  532. end
  533.  
  534. function left()
  535.   turtle.turnLeft()
  536.   variables.direction = variables.direction + 1
  537.   if variables.direction == 5 then
  538.     variables.direction = 1
  539.   end
  540. end
  541.  
  542. function turn()
  543.   left()
  544.   left()
  545. end
  546.  
  547. function Freeway()
  548.   variables.searching = false
  549.   insert([[while not turtle.forward() do
  550.     if turtle.getFuelLevel() == 0 then
  551.       Fuel()
  552.     elseif dig() == false then
  553.       turtle.attack()
  554.     end
  555.   end]])
  556.   variables.stats["moves"] = variables.stats["moves"] + 1
  557. end
  558.  
  559. function FreewayUp()
  560.   variables.searching = false
  561.   insert("variables.level = variables.level + 1")
  562.   insert([[while not turtle.up() do
  563.     if turtle.getFuelLevel() == 0 then
  564.       Fuel()
  565.     elseif digUp() == false then
  566.       turtle.attackUp()
  567.     end
  568.   end]])
  569.   variables.stats['moves'] = variables.stats['moves'] + 1
  570. end
  571.  
  572. function FreewayDown()
  573.   variables.searching = false
  574.   insert("variables.level = variables.level - 1")
  575.   insert([[while not turtle.down() do
  576.     if turtle.getFuelLevel() == 0 then
  577.       Fuel()
  578.     elseif digDown() == false then
  579.       turtle.attackDown()
  580.     end
  581.   end]])
  582.   variables.stats['moves'] = variables.stats['moves'] + 1
  583. end
  584.  
  585. function FreewayBack()
  586.   variables.searching = false
  587.   while not turtle.back() do
  588.     if turtle.getFuelLevel() == 0 then
  589.       Fuel()
  590.     else
  591.       turn()
  592.       insert([[if dig() == false then
  593.         for i = 1, 20 do
  594.           turtle.attack()
  595.           os.sleep(0.05)
  596.         end
  597.       end]])
  598.       turn()
  599.     end
  600.   end
  601.   variables.stats["moves"] = variables.stats["moves"] + 1
  602. end
  603.  
  604.  
  605. -----###-----drawMenus-----###-----
  606.  
  607. function notificationCenter()
  608.   local advice = {
  609.   lang.adv(settings.language, 1, ErrorSlot),
  610.   lang.adv(settings.language, 2, 1),
  611.   lang.adv(settings.language, 3, chestSlot),
  612.   lang.adv(settings.language, 4, chestSlot),
  613.   lang.adv(settings.language, 5, chestSlot),
  614.   lang.adv(settings.language, 6, chestSlot),
  615.   lang.adv(settings.language, 7, 1),
  616.   lang.adv(settings.language, 8, 1)
  617.   }
  618.   if menuVars["notificationCenter"].time > 5 then
  619.     for i = 1, #errors do
  620.       if errors[i] ~= false then
  621.         printWrapped(Splitter(advice[i], " "), 1, objects[8], w - 10)
  622.         return
  623.       end
  624.     end
  625.   end
  626.   for i = 1, 4 do
  627.     printLeft(lang.layout(settings.language, i), objects[i+7])
  628.   end
  629.   menuVars["notificationCenter"].time = menuVars["notificationCenter"].time + 0.5
  630. end
  631.  
  632. function drawPopup(event, p1, p2, p3)
  633.   local errorMessages = {
  634.     lang.popup(settings.language, 1, ErrorSlot),
  635.     lang.popup(settings.language, 2),
  636.     lang.popup(settings.language, 1, chestSlot),
  637.     lang.popup(settings.language, 3),
  638.     lang.popup(settings.language, 1, chestSlot),
  639.     lang.popup(settings.language, 4),
  640.     lang.popup(settings.language, 5),
  641.     lang.popup(settings.language, 6)
  642.   }
  643.   local leftBorder = round(w/9, "u")
  644.   local rightBorder = round(w+1-w/9, "u")
  645.   local winWidth = rightBorder-leftBorder-1
  646.  
  647.   menuVars["drawPopup"].lastscroll = menuVars["drawPopup"].scroll
  648.  
  649.   if event == "mouse_scroll" then
  650.     menuVars["drawPopup"].scroll = menuVars["drawPopup"].scroll - p1
  651.   elseif event == "mouse_click" and p1 == 1 then
  652.     if p2 == leftBorder+winWidth then
  653.       if p3 == 6 then
  654.         menuVars["drawPopup"].scroll = menuVars["drawPopup"].scroll + 1
  655.       elseif p3 == h-3 then
  656.         menuVars["drawPopup"].scroll = menuVars["drawPopup"].scroll - 1
  657.       end
  658.     end
  659.   elseif event == "key" then
  660.     if p1 == 208 then
  661.       menuVars["drawPopup"].scroll = menuVars["drawPopup"].scroll - 1
  662.     elseif p1 == 200 then
  663.       menuVars["drawPopup"].scroll = menuVars["drawPopup"].scroll + 1
  664.     end
  665.   end
  666.   if menuVars["drawPopup"].scroll < -(menuVars["drawPopup"].ypos-menuVars["drawPopup"].lastscroll-(math.ceil((h-6)/10)+6)) then
  667.     menuVars["drawPopup"].scroll = -(menuVars["drawPopup"].ypos-menuVars["drawPopup"].lastscroll-(math.ceil((h-6)/10)+6))
  668.   elseif menuVars["drawPopup"].scroll > 0 then
  669.     menuVars["drawPopup"].scroll = 0
  670.   end
  671.  
  672.   printLine(leftBorder, 4, h-1, "+")
  673.   printLine(rightBorder, 4, h-1, "+")
  674.   menuVars["drawPopup"].ypos = math.ceil((h-6)/10)+5+menuVars["drawPopup"].scroll
  675.   for i = 1, #errors do
  676.     if errors[i] == true then
  677.       menuVars["drawPopup"].ypos = printWrapped(Splitter("- "..errorMessages[i]), leftBorder+1, menuVars["drawPopup"].ypos+1, winWidth, " ")
  678.     end
  679.   end
  680.   printCentered(string.rep("-", winWidth), 4)
  681.   printCentered(string.rep("-", winWidth), h-1)
  682.   printCentered(string.rep(" ", winWidth), 5)
  683.   printCentered(string.rep(" ", winWidth), 6)
  684.   printWrapped(Splitter(lang.popup(settings.language, 7, label)), leftBorder+1, math.ceil((h-6)/10)+4, winWidth, " ")
  685.   printCentered(string.rep(" ", winWidth), h-3)
  686.   printCentered(string.rep(" ", winWidth), 3)
  687.   drawHeader()
  688.   printLine(rightBorder-1, 6, h-3, "^", "v")
  689.   if select == 1 then
  690.     printB({">"..lang.popup(settings.language, 8).."<"," "..lang.popup(settings.language, 9).." ", " "..lang.popup(settings.language, 10).." "}, math.ceil((h-6)/10*9)+4, rightBorder-1, leftBorder+1)
  691.   elseif select == 2 then
  692.     printB({" "..lang.popup(settings.language, 8).." ",">"..lang.popup(settings.language, 9).."<", " "..lang.popup(settings.language, 10).." "}, math.ceil((h-6)/10*9)+4, rightBorder-1, leftBorder+1)
  693.   else
  694.     printB({" "..lang.popup(settings.language, 8).." "," "..lang.popup(settings.language, 9).." ", ">"..lang.popup(settings.language, 10).."<"}, math.ceil((h-6)/10*9)+4, rightBorder-1, leftBorder+1)
  695.   end
  696. end
  697.  
  698. function drawHeader()
  699.   printCentered("ORE SEARCHING STRIP MINER by BrunoZockt", 1)
  700.   printLeft(string.rep("-", w), 2)
  701. end
  702.  
  703. function drawUpdate(event, p1, p2, p3)
  704.   local leftBorder = round(w/6, "u")
  705.   local rightBorder = round(w/6*5, "u")
  706.   local winWidth = rightBorder-leftBorder-1
  707.   local space = round(winWidth-13, "u")
  708.   if event == "mouse_click" and p1 == 1 then
  709.     if p2 == leftBorder+3 and p3 == math.ceil((h-6)/2)+5 then
  710.       checkbox = not checkbox
  711.     elseif p3 == math.ceil((h-6)/10*9)+4 then
  712.       if p2 >= leftBorder+round(0.25*space, "u")+1 and p2 <= leftBorder+round(0.25*space, "u")+11 then
  713.         if select == 1 then
  714.           menustate = menu[menustate].options[select]
  715.         else
  716.           select = 1
  717.         end
  718.       elseif leftBorder+round(0.75*space, "u")+12 <= p2 and p2 <= leftBorder+round(0.75*space, "u")+13 then
  719.         if select == 2 then
  720.           menustate = menu[menustate].options[select]
  721.         else
  722.           select = 2
  723.         end
  724.       end
  725.     end
  726.   elseif event == "key" then
  727.     if p1 == 203 and select > 1 then
  728.       select = select - 1
  729.     elseif p1 == 205 and select < #menu[menustate].options then
  730.       select = select + 1
  731.     elseif p1 == 28 then
  732.       menustate = menu[menustate].options[select]
  733.     end
  734.   end
  735.   printCentered(lang.layout(settings.language, 5), math.ceil((h-6)/6.9)+4)
  736.   printCentered(string.rep("-", math.floor(w/3*2)), 4)
  737.   printCentered(string.rep("-", math.floor(w/3*2)), 12)
  738.   printLine(leftBorder, 4, 12, "+")
  739.   printLine(rightBorder, 4, 12, "+")
  740.   Sprint("_", leftBorder+3, math.ceil((h-6)/2)+4)
  741.   Sprint("|_|"..lang.layout(settings.language, 6), leftBorder+2, math.ceil((h-6)/2)+5)
  742.   if select == 1 then
  743.     printB({">"..lang.layout(settings.language, 7).."<", " "..lang.layout(settings.language, 8).." "}, math.ceil((h-6)/10*9)+4, rightBorder-1, leftBorder+1)
  744.   elseif select == 2 then
  745.     printB({" "..lang.layout(settings.language, 7).." ", ">"..lang.layout(settings.language, 8).."<"}, math.ceil((h-6)/10*9)+4, rightBorder-1, leftBorder+1)
  746.   end
  747.   if checkbox == true then
  748.     Sprint("X", leftBorder+3, math.ceil((h-6)/2)+5)
  749.   end
  750. end
  751.  
  752. function drawNews(event, p1, p2, p3)
  753.   if event == "mouse_scroll" then
  754.     menuVars["drawNews"].scroll = menuVars["drawNews"].scroll - p1
  755.   elseif event == "key" then
  756.     if p1 == 28 then
  757.       menustate = menu[menustate].options[select]
  758.       select = 1
  759.       menuVars["drawNews"].scroll = 0
  760.     elseif p1 == 208 then
  761.       menuVars["drawNews"].scroll = menuVars["drawNews"].scroll - 1
  762.     elseif p1 == 200 then
  763.       menuVars["drawNews"].scroll = menuVars["drawNews"].scroll + 1
  764.     end
  765.   elseif event == "mouse_click" then
  766.     if p1 == 1 then
  767.       if p2 == w then
  768.         if p3 == 3 then
  769.           menuVars["drawNews"].scroll = menuVars["drawNews"].scroll + 1
  770.         elseif p3 == h then
  771.           menuVars["drawNews"].scroll = menuVars["drawNews"].scroll - 1
  772.         end
  773.       elseif p2 >= w/2-2 and p2 <= w/2+2 and p3 == h then
  774.         menustate = menu[menustate].options[select]
  775.         select = 1
  776.         menuVars["drawNews"].scroll = 0
  777.       end
  778.     end
  779.   end
  780.   if menuVars["drawNews"].scroll > 0 then
  781.     menuVars["drawNews"].scroll = 0
  782.   elseif menuVars["drawNews"].scroll < -183 then
  783.     menuVars["drawNews"].scroll = -183
  784.   end
  785.   Sprint("Patch 2.5.1", 1, 3+menuVars["drawNews"].scroll)
  786.   Sprint("-----------", 1, 4+menuVars["drawNews"].scroll)
  787.   Sprint("1.NEW FEATURES", 3, 6+menuVars["drawNews"].scroll)
  788.   local ypos = printWrapped(Splitter("- You can now choose to let the turtle build walls and ceiling to prevent the hallways from being flooded by water or lava."), 5, 7+menuVars["drawNews"].scroll, w-5, " ")
  789.   ypos = printWrapped(Splitter("- If torches need to be placed, the turtle will now determine it's cardial direction at the beginning of digging."), 5, ypos+1, w-5, " ")
  790.   ypos = printWrapped(Splitter("- Torches will -now for real- be placed under all circumstances (see 'Bug fixes')."), 5, ypos+1, w-5, " ")
  791.   Sprint("2.OPTIMIZATIONS", 3, ypos+2)
  792.   ypos = printWrapped(Splitter("- When trash-option or chest-option is true, the turtle will now keep 1 Stack of cobblestone to place floor or walls, if selected."), 5, ypos+3, w-5, " ")
  793.   ypos = printWrapped(Splitter("- Added the page counter in the bottom left"), 5, ypos+1, w-5, " ")
  794.   Sprint("3.Bug fixes", 3, ypos+2)
  795.   ypos = printWrapped(Splitter("- There was a 50% chance (turtle heading north or south) that 50% of the torches (either on the right or the left crosstunnel) were lost. That is because torches are by default placed heading west if possible. The turtle will now determine where it is heading and adjust the way it places torches accordingly so that it never loses a single torch."), 5, ypos+3, w-5, " ")
  796.   ypos = printWrapped(Splitter("- When in default settings, 'chest' was set to 'none' there would be a slot for a chest anyways."), 5, ypos+1, w-5, " ")
  797.   ypos = printWrapped(Splitter("- I missed two instances in my code that would cause a crash when no cobblestone was available. However the probability to hit this crash was soooo low that I'm sure nobody ever encountered it."), 5, ypos+1, w-5, " ")
  798.   Sprint("Patch 2.5", 1, ypos+2)
  799.   Sprint("---------", 1, ypos+3)
  800.   Sprint("1.NEW FEATURES", 3, ypos+5)
  801.   ypos = printWrapped(Splitter("- If you are using ComputerCraft Version 1.64 or higher, the turtle won't determine whether to mine a block or not by comparing it to the Ignor-slots but by comparing it with the new Ignor-list, which you can add blocks to. This is not only easier and more flexible but also a lot faster."), 5, ypos+6, w-5, " ")
  802.   ypos = printWrapped(Splitter("- If you want to, the turtle will now throw away items contained on the Ignor-list."), 5, ypos+1, w-5, " ")
  803.   ypos = printWrapped(Splitter("- The decision whether to empty the inventory into a chest, also takes the length of the lateral tunnel into account now, to prevent the loss of items."), 5, ypos+1, w-5, " ")
  804.   ypos = printWrapped(Splitter("- Torches will now be placed immediatly after digging the tunnel and not only after searching through the tunnel, to prevent mobspawns."), 5, ypos+1, w-5, " ")
  805.   ypos = printWrapped(Splitter("- Torches will now be placed under all circumstances, even if a block for the torch to hold on to has to be placed."), 5, ypos+1, w-5, " ")
  806.   ypos = printWrapped(Splitter("- As soon as torch-slots become free because all its torches have been placed, it changes to an empty slot now, which makes it available for ores."), 5, ypos+1, w-5, " ")
  807.   ypos = printWrapped(Splitter("- The program wont throw an error and terminate, when it doesn't have blocks it wants to place, it simply doesn't place them."), 5, ypos+1, w-5, " ")
  808.   Sprint("2.OPTIMIZATIONS", 3, ypos+2)
  809.   ypos = printWrapped(Splitter("- The turtle will also search right and left at the very first two blocks of the tunnel, just in case you are lazy."), 5, ypos+3, w-5, " ")
  810.   ypos = printWrapped(Splitter("- When you decide to use no chest, the chest-slot will now be converted to an empty slot, available as inventory."), 5, ypos+1, w-5, " ")
  811.   ypos = printWrapped(Splitter("- The floor will now be placed after digging out ores, so that there will definitly be no holes in the ground."), 5, ypos+1, w-5, " ")
  812.   ypos = printWrapped(Splitter("- The inventory will now be cleared after finishing."), 5, ypos+1, w-5, " ")
  813.   ypos = printWrapped(Splitter("- Optimized the dig() function to run faster, increasing the overall speed of the program drasticly."), 5, ypos+1, w-5, " ")
  814.   Sprint("3.Bug fixes", 3, ypos+2)
  815.   ypos = printWrapped(Splitter("- In the main hallway, there was a 25% chance that the block a torch was just placed on gets destroyed a second after, and thereby losing the torch."), 5, ypos+3, w-5, " ")
  816.   ypos = printWrapped(Splitter("- In rare cases when selected normal chest, the turtle would try to return to its chest after the program had finished, resulting in the turtle vanishing to Australia."), 5, ypos+1, w-5, " ")
  817.   ypos = printWrapped(Splitter("- Asking for fuel and taking that fuel wasn't always sync."), 5, ypos+1, w-5, " ")
  818.   ypos = printWrapped(Splitter("- The turtle would sometimes put the coal it needs to fuel itself into the chest."), 5, ypos+1, w-5, " ")
  819.   ypos = printWrapped(Splitter("- When a tunnellength <= 4 was chosen the turtle placed way too much torches."), 5, ypos+1, w-5, " ")
  820.   ypos = printWrapped(Splitter("- Many variables weren't refreshed after changing some options, messing up the preparation page."), 5, ypos+1, w-5, " ")
  821.   ypos = printWrapped(Splitter("- When running out of fuel the turtle would think that it encountered an obstacle, constantly digging and hitting the air in front."), 5, ypos+1, w-5, " ")
  822.   Sprint("Patch 2.4", 1, ypos+2)
  823.   Sprint("---------", 1, ypos+3)
  824.   Sprint("1.NEW FEATURES", 3, ypos+5)
  825.   ypos = printWrapped(Splitter("- Option to place a floor, if needed"), 5, ypos+6, w-5, " ")
  826.   ypos = printWrapped(Splitter("- Option to change language. Currently available: english, german"), 5, ypos+1, w-5, " ")
  827.   ypos = printWrapped(Splitter("- Added a scrollbar to the options page, too"), 5, ypos+1, w-5, " ")
  828.   Sprint("2.OPTIMIZATIONS", 3, ypos+2)
  829.   ypos = printWrapped(Splitter("- The positions of the Buttons are determined more intelligent now, therefore being evenly spaced on every possible monitor size"), 5, ypos+3, w-5, " ")
  830.   ypos = printWrapped(Splitter("- The structure of code is more centralized now, which results in faster processing and less blocked storage"), 5, ypos+1, w-5, " ")
  831.   ypos = printWrapped(Splitter("- The Buttons of the options page won't slide up anymore when there is enough space, was a stupid feature anyways..."), 5, ypos+1, w-5, " ")
  832.   Sprint("3.Bug fixes", 3, ypos+2)
  833.   ypos = printWrapped(Splitter("- When more than 12 stacks torches were needed the ignore slots were overwritten, and wouldn't reset even when the options were changed to need less torches"), 5, ypos+3, w-5, " ")
  834.   ypos = printWrapped(Splitter("- Fixed a bug that caused the turtle to claim having found an ore, even though it had not"), 5, ypos+1, w-5, " ")
  835.   ypos = printWrapped(Splitter("- Fixed a bug where the turtle would place torches at wrong positions when the options were changed before start"), 5, ypos+1, w-5, " ")
  836.   ypos = printWrapped(Splitter("- Everytime after placing a torch the turtle would freeze for a few seconds (related to the bug above), this precious time is being saved now"), 5, ypos+1, w-5, " ")
  837.   Sprint("Patch 2.3", 1, ypos+2)
  838.   Sprint("---------", 1, ypos+3)
  839.   Sprint("1.LAYOUT", 3, ypos+5)
  840.   ypos = printWrapped(Splitter("- Popup on execution in case of Errors"), 5, ypos+6, w-5, " ")
  841.   ypos = printWrapped(Splitter("- Tiny optical improvement on the scrollbar"), 5, ypos+1, w-5, " ")
  842.   Sprint("2.NEW FEATURES", 3, ypos+2)
  843.   ypos = printWrapped(Splitter("- Option to make the turtle place the floor when it's missing"), 5, ypos+3, w-5, " ")
  844.   ypos = printWrapped(Splitter("- Captions in the startmenu are visible for at least 5 seconds now"), 5, ypos+1, w-5, " ")
  845.   Sprint("3.Bug fixes", 3, ypos+2)
  846.   ypos = printWrapped(Splitter("- Autofuel would always refuel 5 coal, no matter how much was needed"), 5, ypos+3, w-5, " ")
  847.   ypos = printWrapped(Splitter("- Options sometimes couldn't be selected or the layout got ugly"), 5, ypos+1, w-5, " ")
  848.   ypos = printWrapped(Splitter("- In the startmenu the displayed amount of necessary torches was wrong"), 5, ypos+1, w-5, " ")
  849.   Sprint("Patch 2.2", 1, ypos+2)
  850.   Sprint("---------", 1, ypos+3)
  851.   Sprint("1.NEW LAYOUT", 3, ypos+5)
  852.   ypos = printWrapped(Splitter("- The awesome update window!"), 5, ypos+6, w-6, " ")
  853.   ypos = printWrapped(Splitter("-> Anti-Annoying-Checkbox ;)"), 7, ypos+1, w-8, " ")
  854.   ypos = printWrapped(Splitter("- This awesome news window!"), 5, ypos+1, w-6, " ")
  855.   ypos = printWrapped(Splitter("-> Very functional scrollbar (mouse_wheel, arrow-keys or arrow-buttons can be used)"), 7, ypos+1, w-8, " ")
  856.   Sprint("2.NEW FEATURES", 3, ypos+2)
  857.   ypos = printWrapped(Splitter("- Favorite options are now savable"), 5, ypos+3, w-6, " ")
  858.   ypos = printWrapped(Splitter("- There is a new function that, when activated, places torches automatically: Perfectly spaced so no mobs can spawn!!!"), 5, ypos+1, w-6, " ")
  859.   ypos = printWrapped(Splitter("- Torches on the main floor are placed instantly now, to prevent mobs from spawning"), 5, ypos+1, w-6, " ")
  860.   Sprint("3.Bug fixes", 3, ypos+2)
  861.   ypos = printWrapped(Splitter("- Putting wrong items into a torchslot resulted in a wrong Errormessage"), 5, ypos+3, w-6, " ")
  862.   ypos = printWrapped(Splitter("- When normal chest was selected, the program crashed trying to deploy the chest"), 5, ypos+1, w-6, " ")
  863.   printLeft(string.rep(" ", w-2), h)
  864.   printLeft("Page "..tostring(math.ceil((-menuVars["drawNews"].scroll+1)/10)).."/24", h)
  865.   printCentered(">Ok<", h)
  866.   printLine(w, 3, h, "^", "v")
  867.   drawHeader()
  868. end
  869.  
  870. function drawHome(event, p1, p2, p3)
  871.   for i = 1, 3 do
  872.     printCentered(lang.layout(settings.language, i+8), round((h-5)*(i/4), "u")+i+2)
  873.   end
  874.   printCentered(string.rep("-", #lang.layout(settings.language, select+8)), round((h-5)*(select/4), "u")+select+3)
  875. end
  876.  
  877. function drawStart(event, p1, p2, p3)
  878.   --Fuellevel
  879.   if turtle.getFuelLevel() ~= "unlimited" then
  880.     MoveObjects(3, 2)
  881.     printLeft(lang.layout(settings.language, 12), objects[3])
  882.     if settings.Autofuel then
  883.       fuelLevel = turtle.getFuelLevel()
  884.       if fuelLevel < 3*variables.FuelDemand/settings.quantity then
  885.         Fuellevel = "low"
  886.         errors[8] = true
  887.         printCentered(lang.layout(settings.language, 13), objects[3])
  888.       elseif fuelLevel >= variables.FuelDemand then
  889.         Fuellevel = "perfect"
  890.         errors[8] = false
  891.       elseif fuelLevel > variables.FuelDemand*0.5 then
  892.         Fuellevel = "ready"
  893.         errors[8] = false
  894.       else
  895.         Fuellevel = "risky"
  896.         errors[8] = false
  897.       end
  898.     else
  899.       if fuelLevel >= variables.FuelDemand*1.5 then
  900.         Fuellevel = "perfect"
  901.       elseif fuelLevel >= variables.FuelDemand*1 then
  902.         Fuellevel = "ready"
  903.       else
  904.         Fuellevel = "low"
  905.         printCentered(lang.layout(settings.language, 13), objects[3])
  906.       end
  907.     end
  908.     if help[1] == false then
  909.       help[1] = fuelLevel
  910.     end
  911.     if Color() then
  912.       if Fuellevel == "perfect" then
  913.         term.setBackgroundColor(colors.lime)
  914.       elseif Fuellevel == "ready" or Fuellevel == "risky" then
  915.         term.setBackgroundColor(colors.yellow)
  916.       elseif Fuellevel == "low" then
  917.         term.setBackgroundColor(colors.red)
  918.       end
  919.       printRight(fuelLevel.."/"..variables.FuelDemand, objects[3])
  920.       term.setBackgroundColor(colors.black)
  921.     else
  922.       if Fuellevel == "perfect" then
  923.         printRight("+ "..fuelLevel.."/"..variables.FuelDemand, objects[3])
  924.       elseif Fuellevel == "ready" or Fuellevel == "risky" then
  925.         printRight("+/- "..fuelLevel.."/"..variables.FuelDemand, objects[3])
  926.       elseif Fuellevel == "low" then
  927.         printRight("- "..fuelLevel.."/"..variables.FuelDemand, objects[3])
  928.       end
  929.     end
  930.   else
  931.     MoveObjects(3, 1)
  932.   end
  933.  
  934.   --Torches
  935.   local TorchAmount = 0
  936.   ErrorSlot = 0
  937.   for i = 1, #variables.slots do
  938.     if variables.slots[i] == "T" then
  939.       if ItemCount("minecraft:torch", i) ~= false then
  940.         TorchAmount = TorchAmount + ItemCount("minecraft:torch", i)
  941.       else
  942.         ErrorSlot = i
  943.       end
  944.     end
  945.   end
  946.   if ErrorSlot ~= 0 then
  947.     errors[1] = true
  948.   else
  949.     errors[1] = false
  950.   end
  951.   if variables.TorchDemand ~= 0 then
  952.     MoveObjects(4, 2)
  953.     printLeft(lang.layout(settings.language, 14), objects[4])
  954.     if TorchAmount < variables.TorchDemand then
  955.       Check.torch = false
  956.       if Color() then
  957.         term.setBackgroundColor(colors.red)
  958.       end
  959.     else
  960.       Check.torch = true
  961.       if Color() then
  962.         term.setBackgroundColor(colors.lime)
  963.       end
  964.     end
  965.     printRight(TorchAmount.."/"..variables.TorchDemand, objects[4])
  966.     if Check.torch == false then
  967.       errors[7] = true
  968.     else
  969.       errors[7] = false
  970.     end
  971.     term.setBackgroundColor(colors.black)
  972.   else
  973.     MoveObjects(4, 1)
  974.     errors[7] = false
  975.   end
  976.  
  977.   --Chest
  978.   errors[2] = false
  979.   errors[3] = false
  980.   errors[4] = false
  981.   errors[5] = false
  982.   errors[6] = false
  983.   Check.chest = false
  984.   if settings.chestSelect == 1 then
  985.     MoveObjects(5, 2)
  986.     printLeft(lang.layout(settings.language, 15), objects[5])
  987.     if Version < 1.64 then
  988.       if turtle.getItemCount(chestSlot) == 0 then
  989.         errors[4] = true
  990.       else
  991.         Check.chest = true
  992.       end
  993.     else
  994.       if ItemCount("minecraft:ender_chest", chestSlot) ~= false and ItemCount("minecraft:ender_chest", chestSlot) > 0 then
  995.         errors[2] = true
  996.       elseif ItemCount(enderChestList, chestSlot) == false then
  997.         errors[3] = true
  998.       elseif ItemCount(enderChestList, chestSlot) == 0 then
  999.         errors[4] = true
  1000.       else
  1001.         Check.chest = true
  1002.       end
  1003.     end
  1004.   elseif settings.chestSelect == 2 then
  1005.     MoveObjects(5, 2)
  1006.     printLeft(lang.layout(settings.language, 18), objects[5])
  1007.     if ItemCount(chestList, chestSlot) == false then
  1008.       errors[5] = true
  1009.     elseif ItemCount(chestList, chestSlot) == 0 then
  1010.       errors[6] = true
  1011.     else
  1012.       Check.chest = true
  1013.     end
  1014.   else
  1015.     Check.chest = true
  1016.     MoveObjects(5, 1)
  1017.   end
  1018.   if Check.chest == false then
  1019.     if Color() then
  1020.       term.setBackgroundColor(colors.red)
  1021.     end
  1022.     printRight(lang.layout(settings.language, 16), objects[5])
  1023.   else
  1024.     if Color() then
  1025.       term.setBackgroundColor(colors.lime)
  1026.     end
  1027.     printRight(lang.layout(settings.language, 17), objects[5])
  1028.   end
  1029.   term.setBackgroundColor(colors.black)
  1030.  
  1031.   --Autofuel
  1032.   if settings.Autofuel == true then
  1033.     MoveObjects(6, 2)
  1034.     printLeft(lang.layout(settings.language, 19), objects[6])
  1035.     term.setBackgroundColor(colors.lime)
  1036.     printRight(lang.layout(settings.language, 17), objects[6])
  1037.     term.setBackgroundColor(colors.black)
  1038.     if help[4] == false then
  1039.       help[4] = turtle.getItemCount(AutofuelSlot)
  1040.     end
  1041.   else
  1042.     MoveObjects(6, 1)
  1043.   end
  1044.  
  1045.   printLeft(string.rep("-", w), objects[7])
  1046.  
  1047.   --Inventory
  1048.   if Version < 1.64 then
  1049.     for i = 1, #variables.slots do
  1050.       if variables.slots[i] == "I" then
  1051.         variables.slots[i] = "_"
  1052.       end
  1053.     end
  1054.     for i = 1, settings.ignor do
  1055.       variables.slots[i] = "I"
  1056.     end
  1057.   end
  1058.   for k, v in ipairs(SlotCalculator("T", "empty")) do
  1059.     variables.slots[v] = "_"
  1060.   end
  1061.   local stupid = SlotCalculator("_", "empty")
  1062.   for k, v in ipairs(stupid) do
  1063.     if k > #stupid-math.ceil(variables.TorchDemand/64) then
  1064.       variables.slots[v] = "T"
  1065.     end
  1066.   end
  1067.   notificationCenter()
  1068.   printRight("|"..variables.slots[1].."|"..variables.slots[2].."|"..variables.slots[3].."|"..variables.slots[4].."|", objects[8])
  1069.   printRight("|"..variables.slots[5].."|"..variables.slots[6].."|"..variables.slots[7].."|"..variables.slots[8].."|", objects[9])
  1070.   printRight("|"..variables.slots[9].."|"..variables.slots[10].."|"..variables.slots[11].."|"..variables.slots[12].."|", objects[10])
  1071.   printRight("|"..variables.slots[13].."|"..variables.slots[14].."|"..variables.slots[15].."|"..variables.slots[16].."|", objects[11])
  1072.  
  1073.   if objects[11] < 12 then
  1074.     printLeft(string.rep("-", w), 12)
  1075.   end
  1076.  
  1077.   --Buttons
  1078.   if select == 1 then
  1079.     printB({">"..lang.layout(settings.language, 20).."<", " "..lang.layout(settings.language, 10).." ", " "..lang.layout(settings.language, 21).." "}, objects[13])
  1080.   elseif select == 2 then
  1081.     printB({" "..lang.layout(settings.language, 20).." ", ">"..lang.layout(settings.language, 10).."<", " "..lang.layout(settings.language, 21).." "}, objects[13])
  1082.   elseif select == 3 then
  1083.     printB({" "..lang.layout(settings.language, 20).." ", " "..lang.layout(settings.language, 10).." ", ">"..lang.layout(settings.language, 21).."<"}, objects[13])
  1084.   end
  1085.  
  1086.   --Action
  1087.   if fuelLevel < 3*variables.FuelDemand/settings.quantity then
  1088.     local FuelAmount = math.ceil((3*variables.FuelDemand/settings.quantity-fuelLevel)/80)
  1089.     for i = 1, #SlotCalculator("_", "minecraft:coal") do
  1090.       turtle.select(SlotCalculator("_", "minecraft:coal")[i])
  1091.       local output = refuel(FuelAmount, true)
  1092.       if output == true then
  1093.         errors[8] = false
  1094.         Fuellevel = "risky"
  1095.       else
  1096.         FuelAmount = FuelAmount - output
  1097.       end
  1098.     end
  1099.   end
  1100. end
  1101.  
  1102. function drawOptions(event, p1, p2, p3)
  1103.   if menuVars["drawOptions"].focus == 100 then
  1104.     calculateFuelDemand()
  1105.     TorchCalculator()
  1106.     if menuVars["drawOptions"].saved == true and select == 3 then
  1107.       select = 2
  1108.     end
  1109.   end
  1110.   if event == "mouse_scroll" then
  1111.     if menuVars["drawOptions"].focus == 100 then
  1112.       menuVars["drawOptions"].scroll = menuVars["drawOptions"].scroll - p1
  1113.     elseif menuVars["drawOptions"].focus == 13+menuVars["drawOptions"].scroll+menuVars["drawOptions"].shift and ((p1 < 0 and menuVars["drawOptions"].scroll2 < 0) or (p1 >= 0 and h < menuVars["drawOptions"].ypos)) then
  1114.       menuVars["drawOptions"].scroll2 = menuVars["drawOptions"].scroll2 - p1
  1115.     end
  1116.   elseif event == "mouse_click" and p1 == 1 and menuVars["drawOptions"].focus == 100 then
  1117.     if p2 == w then
  1118.       if p3 == 3 then
  1119.         menuVars["drawOptions"].scroll = menuVars["drawOptions"].scroll + 1
  1120.       elseif p3 == h then
  1121.         menuVars["drawOptions"].scroll = menuVars["drawOptions"].scroll - 1
  1122.       end
  1123.     else
  1124.       menuVars["drawOptions"].focus = p3
  1125.       if menuVars["drawOptions"].focus > 15+menuVars["drawOptions"].shift+menuVars["drawOptions"].shift2 or menuVars["drawOptions"].focus > h-3 or menuVars["drawOptions"].focus < 3 then
  1126.         menuVars["drawOptions"].focus = 100
  1127.       end
  1128.     end
  1129.   elseif event == "key" then
  1130.     if p1 == 28 and menuVars["drawOptions"].focus ~= 0 then
  1131.       menuVars["drawOptions"].saved = false
  1132.       menuVars["drawOptions"].focus = 100
  1133.     elseif p1 == 208 and menuVars["drawOptions"].focus == 100 then
  1134.       menuVars["drawOptions"].scroll = menuVars["drawOptions"].scroll - 1
  1135.     elseif p1 == 200 and menuVars["drawOptions"].focus == 100 then
  1136.       menuVars["drawOptions"].scroll = menuVars["drawOptions"].scroll + 1
  1137.     else
  1138.       if settings.torches == false then
  1139.         if menuVars["drawOptions"].focus == 8+menuVars["drawOptions"].scroll then
  1140.           if p1 == 203 and settings.mainTorches == false then
  1141.             settings.mainTorches = true
  1142.           elseif p1 == 205 and settings.mainTorches == true then
  1143.             settings.mainTorches = false
  1144.           end
  1145.         end
  1146.       end
  1147.       menuVars["drawOptions"].shift2 = 0
  1148.       if Version >= 1.64 then
  1149.         if menuVars["drawOptions"].focus == 14+menuVars["drawOptions"].scroll+menuVars["drawOptions"].shift then
  1150.           if p1 == 203 and settings.trash == false then
  1151.             settings.trash = true
  1152.           elseif p1 == 205 and settings.trash == true then
  1153.             settings.trash = false
  1154.           end
  1155.         end
  1156.       else
  1157.         menuVars["drawOptions"].shift2 = menuVars["drawOptions"].shift2-1
  1158.       end
  1159.       if menuVars["drawOptions"].focus == 3+menuVars["drawOptions"].scroll then
  1160.         if p1 == 203 and settings.language == "de" then
  1161.           settings.language = "en"
  1162.         elseif p1 == 205 and settings.language == "en" then
  1163.           settings.language = "de"
  1164.         end
  1165.       elseif menuVars["drawOptions"].focus == 4+menuVars["drawOptions"].scroll then
  1166.         if p1 == 203 and settings.tunnelspace == 4 then
  1167.           settings.tunnelspace = 3
  1168.         elseif p1 == 205 and settings.tunnelspace == 3 then
  1169.           settings.tunnelspace = 4
  1170.         end
  1171.       elseif menuVars["drawOptions"].focus == 7+menuVars["drawOptions"].scroll then
  1172.         if p1 == 203 and settings.chestSelect > 1 then
  1173.           settings.chestSelect = settings.chestSelect - 1
  1174.         elseif p1 == 205 and settings.chestSelect < #chest then
  1175.           settings.chestSelect = settings.chestSelect + 1
  1176.         end
  1177.         for k, v in ipairs(chest) do
  1178.           if k == settings.chestSelect then
  1179.             v = true
  1180.           else
  1181.             v = false
  1182.           end
  1183.         end
  1184.       elseif menuVars["drawOptions"].focus == 8+menuVars["drawOptions"].scroll then
  1185.         if p1 == 203 and settings.torches == false then
  1186.           settings.torches = true
  1187.         elseif p1 == 205 and settings.torches == true then
  1188.           settings.torches = false
  1189.         end
  1190.       elseif menuVars["drawOptions"].focus == 11+menuVars["drawOptions"].scroll+menuVars["drawOptions"].shift then
  1191.         if p1 == 203 and settings.Autofuel == false then
  1192.           settings.Autofuel = true
  1193.         elseif p1 == 205 and settings.Autofuel == true then
  1194.           settings.Autofuel = false
  1195.         end
  1196.       elseif menuVars["drawOptions"].focus == 12+menuVars["drawOptions"].scroll+menuVars["drawOptions"].shift then
  1197.         if p1 == 203 and settings.floor == false then
  1198.           settings.floor = true
  1199.         elseif p1 == 205 and settings.floor == true then
  1200.           settings.floor = false
  1201.         end
  1202.       elseif menuVars["drawOptions"].focus == 13+menuVars["drawOptions"].scroll+menuVars["drawOptions"].shift then
  1203.         if p1 == 200 and menuVars["drawOptions"].scroll2 < 0 then
  1204.           menuVars["drawOptions"].scroll2 = menuVars["drawOptions"].scroll2+1
  1205.         elseif p1 == 208 and h < menuVars["drawOptions"].ypos then
  1206.           menuVars["drawOptions"].scroll2 = menuVars["drawOptions"].scroll2-1
  1207.         elseif Version >= 1.64 then
  1208.           if p1 == 57 or p1 == 14 then
  1209.             for i = 1, 16 do
  1210.               local data = turtle.getItemDetail(i)
  1211.               if data then
  1212.                 for k, v in ipairs(settings.ignor) do
  1213.                   if v == data.name then
  1214.                     table.remove(settings.ignor, k)
  1215.                   end
  1216.                 end
  1217.                 if p1 == 57 then
  1218.                   table.insert(settings.ignor, data.name)
  1219.                 end
  1220.               end
  1221.             end
  1222.           end
  1223.         end
  1224.       elseif menuVars["drawOptions"].focus == 15+menuVars["drawOptions"].scroll+menuVars["drawOptions"].shift then
  1225.         if p1 == 203 and settings.walls == false then
  1226.           settings.walls = true
  1227.         elseif p1 == 205 and settings.walls == true then
  1228.           settings.walls = false
  1229.         end
  1230.       end
  1231.     end
  1232.   end
  1233.   if menuVars["drawOptions"].scroll > 0 then
  1234.     menuVars["drawOptions"].scroll = 0
  1235.   elseif menuVars["drawOptions"].scroll < -(5+menuVars["drawOptions"].shift+menuVars["drawOptions"].shift2) then
  1236.     menuVars["drawOptions"].scroll = -(5+menuVars["drawOptions"].shift+menuVars["drawOptions"].shift2)
  1237.   end
  1238.  
  1239.   if settings.torches == false then
  1240.     if menuVars["drawOptions"].focus == 9+menuVars["drawOptions"].scroll then
  1241.       printLeft(lang.layout(settings.language, 31), menuVars["drawOptions"].focus)
  1242.       if settings.mainTorches == true then
  1243.         printRight(">"..lang.layout(settings.language, 29).."<  "..lang.layout(settings.language, 30).." ", menuVars["drawOptions"].focus)
  1244.       else
  1245.         printRight(lang.layout(settings.language, 29).."  >"..lang.layout(settings.language, 30).."<", menuVars["drawOptions"].focus)
  1246.       end
  1247.     elseif menuVars["drawOptions"].focus == 10+menuVars["drawOptions"].scroll then
  1248.       printLeft(lang.layout(settings.language, 32), menuVars["drawOptions"].focus)
  1249.       term.setCursorPos(w-2, menuVars["drawOptions"].focus)
  1250.       settings.lateralTorches = tonumber(read())
  1251.       term.clear()
  1252.       drawHeader()
  1253.       return drawOptions("key", 28)
  1254.     end
  1255.     menuVars["drawOptions"].shift = 0
  1256.   else
  1257.     menuVars["drawOptions"].shift = -2
  1258.   end
  1259.   if Version >= 1.64 then
  1260.     if menuVars["drawOptions"].focus == 14+menuVars["drawOptions"].scroll+menuVars["drawOptions"].shift then
  1261.       printLeft(lang.layout(settings.language, 39), menuVars["drawOptions"].focus)
  1262.       if settings.trash == true then
  1263.         printRight(">"..lang.layout(settings.language, 29).."<  "..lang.layout(settings.language, 30).." ", menuVars["drawOptions"].focus)
  1264.       else
  1265.         printRight(lang.layout(settings.language, 29).."  >"..lang.layout(settings.language, 30).."<", menuVars["drawOptions"].focus)
  1266.       end
  1267.     end
  1268.   end
  1269.   if menuVars["drawOptions"].focus == 3+menuVars["drawOptions"].scroll then
  1270.     printLeft(lang.layout(settings.language, 34), menuVars["drawOptions"].focus)
  1271.     if settings.language == "en" then
  1272.       printRight(">en<   de ", menuVars["drawOptions"].focus)
  1273.     else
  1274.       printRight("en   >de<", menuVars["drawOptions"].focus)
  1275.     end
  1276.   elseif menuVars["drawOptions"].focus == 4+menuVars["drawOptions"].scroll then
  1277.     printLeft(lang.layout(settings.language, 22), menuVars["drawOptions"].focus)
  1278.     if settings.tunnelspace == 3 then
  1279.       printRight(">2<  3 ", menuVars["drawOptions"].focus)
  1280.     else
  1281.       printRight("2  >3<", menuVars["drawOptions"].focus)
  1282.     end
  1283.   elseif menuVars["drawOptions"].focus == 5+menuVars["drawOptions"].scroll then
  1284.     printLeft(lang.layout(settings.language, 23), menuVars["drawOptions"].focus)
  1285.     term.setCursorPos(w-2, menuVars["drawOptions"].focus)
  1286.     settings.quantity = tonumber(read())
  1287.     term.clear()
  1288.     drawHeader()
  1289.     return drawOptions("key", 28)
  1290.   elseif menuVars["drawOptions"].focus == 6+menuVars["drawOptions"].scroll then
  1291.     printLeft(lang.layout(settings.language, 24), menuVars["drawOptions"].focus)
  1292.     term.setCursorPos(w-2, menuVars["drawOptions"].focus)
  1293.     settings.length = tonumber(read())
  1294.     term.clear()
  1295.     drawHeader()
  1296.     return drawOptions("key", 28)
  1297.   elseif menuVars["drawOptions"].focus == 7+menuVars["drawOptions"].scroll then
  1298.     printLeft(lang.layout(settings.language, 18), menuVars["drawOptions"].focus)
  1299.     variables.slots[16] = "C"
  1300.     if settings.chestSelect == 1 then
  1301.       printRight(">"..lang.layout(settings.language, 25).."< "..lang.layout(settings.language, 26).."  "..lang.layout(settings.language, 27).." ", menuVars["drawOptions"].focus)
  1302.     elseif settings.chestSelect == 2 then
  1303.       printRight(lang.layout(settings.language, 25).." >"..lang.layout(settings.language, 26).."< "..lang.layout(settings.language, 27).." ", menuVars["drawOptions"].focus)
  1304.     else
  1305.       printRight(lang.layout(settings.language, 25).."  "..lang.layout(settings.language, 26).." >"..lang.layout(settings.language, 27).."<", menuVars["drawOptions"].focus)
  1306.       if variables.slots[16] == "C" then
  1307.         variables.slots[16] = "_"
  1308.       end
  1309.     end
  1310.   elseif menuVars["drawOptions"].focus == 8+menuVars["drawOptions"].scroll then
  1311.     printLeft(lang.layout(settings.language, 28), menuVars["drawOptions"].focus)
  1312.     if settings.torches == true then
  1313.       printRight(">"..lang.layout(settings.language, 29).."< "..lang.layout(settings.language, 30).." ", menuVars["drawOptions"].focus)
  1314.     else
  1315.       printRight(" "..lang.layout(settings.language, 29).." >"..lang.layout(settings.language, 30).."<", menuVars["drawOptions"].focus)
  1316.     end
  1317.   elseif menuVars["drawOptions"].focus == 11+menuVars["drawOptions"].scroll+menuVars["drawOptions"].shift then
  1318.     printLeft(lang.layout(settings.language, 19), menuVars["drawOptions"].focus)
  1319.     if settings.Autofuel == true then
  1320.       printRight(">"..lang.layout(settings.language, 29).."<   "..lang.layout(settings.language, 30).." ", menuVars["drawOptions"].focus)
  1321.     else
  1322.       printRight(lang.layout(settings.language, 29).."   >"..lang.layout(settings.language, 30).."<", menuVars["drawOptions"].focus)
  1323.     end
  1324.   elseif menuVars["drawOptions"].focus == 12+menuVars["drawOptions"].scroll+menuVars["drawOptions"].shift then
  1325.     printLeft(lang.layout(settings.language, 33), menuVars["drawOptions"].focus)
  1326.     if settings.floor == true then
  1327.       printRight(">"..lang.layout(settings.language, 29).."<   "..lang.layout(settings.language, 30).." ", menuVars["drawOptions"].focus)
  1328.     else
  1329.       printRight(lang.layout(settings.language, 29).."   >"..lang.layout(settings.language, 30).."<", menuVars["drawOptions"].focus)
  1330.     end
  1331.   elseif menuVars["drawOptions"].focus == 13+menuVars["drawOptions"].scroll+menuVars["drawOptions"].shift then
  1332.     if Version >= 1.64 then
  1333.       printLeft(lang.layout(settings.language, 36), 3)
  1334.       local ypos = printWrapped(textutils.unserialize(string.gsub(textutils.serialize(settings.ignor), "minecraft:(%a+)", function(q) return q.."," end)), 1, 4, w)
  1335.       menuVars["drawOptions"].ypos = printWrapped(Splitter(lang.layout(settings.language, 37, label)), 1, ypos+2+menuVars["drawOptions"].scroll2, w-1)
  1336.       for i = 1, ypos+1 do
  1337.         printLeft(string.rep(" ", w), i)
  1338.       end
  1339.       drawHeader()
  1340.       printLeft(lang.layout(settings.language, 36), 3)
  1341.       printWrapped(textutils.unserialize(string.gsub(textutils.serialize(settings.ignor), "minecraft:(%a+_*%a*)", function(q) return q.."," end)), 1, 4, w)
  1342.       printLine(w, ypos+2, h, "^", "v")
  1343.     else
  1344.       printLeft(lang.layout(settings.language, 38), menuVars["drawOptions"].focus)
  1345.       term.setCursorPos(w-2, menuVars["drawOptions"].focus)
  1346.       settings.ignor = tonumber(read())
  1347.       term.clear()
  1348.       drawHeader()
  1349.       return drawOptions("key", 28)
  1350.     end
  1351.   elseif menuVars["drawOptions"].focus == 15+menuVars["drawOptions"].scroll+menuVars["drawOptions"].shift then
  1352.     printLeft(lang.layout(settings.language, 40), menuVars["drawOptions"].focus)
  1353.     if settings.walls == true then
  1354.       printRight(">"..lang.layout(settings.language, 29).."<   "..lang.layout(settings.language, 30).." ", menuVars["drawOptions"].focus)
  1355.     else
  1356.       printRight(lang.layout(settings.language, 29).."   >"..lang.layout(settings.language, 30).."<", menuVars["drawOptions"].focus)
  1357.     end
  1358.   elseif menuVars["drawOptions"].focus == 100 then
  1359.     printLeft(lang.layout(settings.language, 34), 3+menuVars["drawOptions"].scroll)
  1360.     printLeft(lang.layout(settings.language, 22), 4+menuVars["drawOptions"].scroll)
  1361.     printLeft(lang.layout(settings.language, 23), 5+menuVars["drawOptions"].scroll)
  1362.     printLeft(lang.layout(settings.language, 24), 6+menuVars["drawOptions"].scroll)
  1363.     printLeft(lang.layout(settings.language, 18), 7+menuVars["drawOptions"].scroll)
  1364.     printLeft(lang.layout(settings.language, 28), 8+menuVars["drawOptions"].scroll)
  1365.     if settings.torches == false then
  1366.       printLeft(lang.layout(settings.language, 31), 9+menuVars["drawOptions"].scroll)
  1367.       printLeft(lang.layout(settings.language, 32), 10+menuVars["drawOptions"].scroll)
  1368.     end
  1369.     printLeft(lang.layout(settings.language, 19), 11+menuVars["drawOptions"].scroll+menuVars["drawOptions"].shift)
  1370.     printLeft(lang.layout(settings.language, 33), 12+menuVars["drawOptions"].scroll+menuVars["drawOptions"].shift)
  1371.     printLeft("#"..lang.layout(settings.language, 36), 13+menuVars["drawOptions"].scroll+menuVars["drawOptions"].shift)
  1372.     printLeft(lang.layout(settings.language, 39), 14+menuVars["drawOptions"].scroll+menuVars["drawOptions"].shift)
  1373.     printLeft(lang.layout(settings.language, 40), 15+menuVars["drawOptions"].scroll+menuVars["drawOptions"].shift)
  1374.     printRight(settings.language, 3+menuVars["drawOptions"].scroll, 1)
  1375.     printRight(tostring(settings.tunnelspace-1), 4+menuVars["drawOptions"].scroll, 1)
  1376.     printRight(tostring(settings.quantity), 5+menuVars["drawOptions"].scroll, 1)
  1377.     printRight(tostring(settings.length), 6+menuVars["drawOptions"].scroll, 1)
  1378.     printRight(translate(chest[tonumber(settings.chestSelect)], "layout"), 7+menuVars["drawOptions"].scroll, 1)
  1379.     printRight(translate(tostring(settings.torches), "layout"), 8+menuVars["drawOptions"].scroll, 1)
  1380.     if settings.torches == false then
  1381.       printRight(translate(tostring(settings.mainTorches), "layout"), 9+menuVars["drawOptions"].scroll, 1)
  1382.       printRight(tostring(settings.lateralTorches), 10+menuVars["drawOptions"].scroll, 1)
  1383.     end
  1384.     printRight(translate(tostring(settings.Autofuel), "layout"), 11+menuVars["drawOptions"].scroll+menuVars["drawOptions"].shift, 1)
  1385.     printRight(translate(tostring(settings.floor), "layout"), 12+menuVars["drawOptions"].scroll+menuVars["drawOptions"].shift, 1)
  1386.     if Version >= 1.64 then
  1387.       printRight(tostring(#settings.ignor-4), 13+menuVars["drawOptions"].scroll+menuVars["drawOptions"].shift, 1)
  1388.     else
  1389.       printRight(tostring(settings.ignor), 13+menuVars["drawOptions"].scroll+menuVars["drawOptions"].shift, 1)
  1390.     end
  1391.     printRight(translate(tostring(settings.trash), "layout"), 14+menuVars["drawOptions"].scroll+menuVars["drawOptions"].shift, 1)
  1392.     printRight(translate(tostring(settings.walls), "layout"), 15+menuVars["drawOptions"].scroll+menuVars["drawOptions"].shift, 1)
  1393.     printLeft(string.rep(" ", w-1), h)
  1394.     printLeft(string.rep(" ", w-1), h-1)
  1395.     printLeft(string.rep(" ", w-1), h-2)
  1396.     printB({lang.layout(settings.language, 9), lang.layout(settings.language, 21), lang.layout(settings.language, 35)}, 12)
  1397.     if select == 1 then
  1398.       printB({string.rep("_", #lang.layout(settings.language, 9)), string.rep(" ", #lang.layout(settings.language, 21)), string.rep(" ", #lang.layout(settings.language, 35))}, 13)
  1399.     elseif select == 2 then
  1400.       printB({string.rep(" ", #lang.layout(settings.language, 9)), string.rep("_", #lang.layout(settings.language, 21)), string.rep(" ", #lang.layout(settings.language, 35))}, 13)
  1401.     else
  1402.       printB({string.rep(" ", #lang.layout(settings.language, 9)), string.rep(" ", #lang.layout(settings.language, 21)), string.rep("_", #lang.layout(settings.language, 35))}, 13)
  1403.     end
  1404.     printLine(w, 3, h-2, "^", "v")
  1405.     drawHeader()
  1406.   end
  1407. end
  1408.  
  1409. function drawResume(time)
  1410.   term.setCursorPos(1, 3)
  1411.   print("Program will resume in "..tostring(time).." seconds.\n")
  1412.   print("Press Enter to stop resuming...")
  1413. end
  1414.  
  1415. menu = {
  1416.   ["Update"] = {
  1417.     options = {"News", "Home"},
  1418.     orientation = "horizontal",
  1419.     draw = drawUpdate
  1420.   },
  1421.   ["News"] = {
  1422.     options = {"Home"},
  1423.     orientation = "horizontal",
  1424.     draw = drawNews
  1425.   },
  1426.   ["Home"] = {
  1427.     options = {"Start", "Options", "Quit"},
  1428.     orientation = "vertical",
  1429.     draw = drawHome
  1430.   },
  1431.   ["Start"] = {
  1432.     options = {"Go!", "Options", "Home"},
  1433.     orientation = "horizontal",
  1434.     draw = drawStart
  1435.   },
  1436.   ["Options"] = {
  1437.     options = {"Start", "Home", "Save as default"},
  1438.     orientation = "horizontal",
  1439.     draw = drawOptions
  1440.   },
  1441.   ["Popup"] = {
  1442.     options = {"Go!", "Start", "Quit"},
  1443.     orientation = "horizontal",
  1444.     draw = drawPopup
  1445.   }
  1446. }
  1447.  
  1448. -----###-----sequences-----###-----
  1449.  
  1450. function Return()
  1451.   face(1)
  1452.   if #variables.cache == 0 then
  1453.     return
  1454.   elseif variables.cache[#variables.cache] == 5 then
  1455.     insert('FreewayDown()')
  1456.   elseif variables.cache[#variables.cache] == 6 then
  1457.     insert('FreewayUp()')
  1458.   else
  1459.     insert('FreewayBack()')
  1460.     insert('variables.direction = variables.cache[#variables.cache]')
  1461.   end
  1462.   insert('face(1)')
  1463.   insert('table.remove(variables.cache, #variables.cache)')
  1464. end
  1465.  
  1466. function compare(method, slotNum)
  1467.   turtle.select(slotNum)
  1468.   if method == "down" then
  1469.     return turtle.compareDown()
  1470.   elseif method == "up" then
  1471.     return turtle.compareUp()
  1472.   elseif method == "front" then
  1473.     return turtle.compare()
  1474.   end
  1475. end
  1476.  
  1477. function CompareDown(walls)
  1478.   if Version >= 1.64 then
  1479.     local boolean, data = turtle.inspectDown()
  1480.     if boolean then
  1481.       for k, v in pairs(settings.ignor) do
  1482.         if v == data.name then
  1483.           return
  1484.         end
  1485.       end
  1486.     else
  1487.       return
  1488.     end
  1489.   elseif turtle.detectDown() then
  1490.     for k, v in pairs(SlotCalculator("I")) do
  1491.       if compare("down", k) == true then
  1492.         return
  1493.       end
  1494.     end
  1495.   else
  1496.     return
  1497.   end
  1498.   insert('variables.searching = true')
  1499.   insert('turtle.select(SlotCalculator("_", "empty")[1])')
  1500.   insert('digDown()')
  1501.   insert('FreewayDown()')
  1502.   insert('table.insert(variables.cache, 6)')
  1503.   insert('CompareAll("CompareUp()")')
  1504. end
  1505.  
  1506. function CompareUp(walls)
  1507.   if Version >= 1.64 then
  1508.     local boolean, data = turtle.inspectUp()
  1509.     if boolean then
  1510.       for k, v in pairs(settings.ignor) do
  1511.         if v == data.name then
  1512.           return (walls and placeWall("placeUp")) or nil
  1513.         end
  1514.       end
  1515.     else
  1516.       return (walls and placeWall("placeUp")) or nil
  1517.     end
  1518.   elseif turtle.detectUp() then
  1519.     for k, v in pairs(SlotCalculator("I")) do
  1520.       if compare("up", k) == true then
  1521.         return
  1522.       end
  1523.     end
  1524.   else
  1525.     return (walls and placeWall("placeUp")) or nil
  1526.   end
  1527.   insert('variables.searching = true')
  1528.   insert('turtle.select(SlotCalculator("_", "empty")[1])')
  1529.   insert('digUp()')
  1530.   insert('FreewayUp()')
  1531.   insert('table.insert(variables.cache, 5)')
  1532.   if walls == true then
  1533.     insert('placeWall()')
  1534.   end
  1535.   insert('CompareAll("CompareDown()")')
  1536. end
  1537.  
  1538. function CompareRight(walls)
  1539.   Compare(4, walls)
  1540. end
  1541.  
  1542. function CompareBack(walls)
  1543.   Compare(3, walls)
  1544. end
  1545.  
  1546. function CompareLeft(walls)
  1547.   Compare(2, walls)
  1548. end
  1549.  
  1550. function Compare(facing, walls)
  1551.   if facing == nil then
  1552.     facing = 1
  1553.   elseif type(facing) == "boolean" then
  1554.     walls = facing
  1555.     facing = 1
  1556.   end
  1557.   face(facing)
  1558.   if Version >= 1.64 then
  1559.     local boolean, data = turtle.inspect()
  1560.     if boolean then
  1561.       for k, v in pairs(settings.ignor) do
  1562.         if v == data.name then
  1563.           return (walls and placeWall("placeUp")) or nil
  1564.         end
  1565.       end
  1566.     else
  1567.       return (walls and placeWall()) or nil
  1568.     end
  1569.   elseif turtle.detect() then
  1570.     for k, v in pairs(SlotCalculator("I")) do
  1571.       if compare("front", k) == true then
  1572.         return
  1573.       end
  1574.     end
  1575.   else
  1576.     return (walls and placeWall()) or nil
  1577.   end
  1578.   insert('variables.searching = true')
  1579.   insert('turtle.select(SlotCalculator("_", "empty")[1])')
  1580.   insert('dig()')
  1581.   insert('Freeway()')
  1582.   insert('table.insert(variables.cache, variables.direction)')
  1583.   if walls == true then
  1584.     insert('placeWall("placeUp")')
  1585.   end
  1586.   insert('variables.direction = 1')
  1587.   insert('CompareAll("CompareBack()")')
  1588. end
  1589.  
  1590. function CompareAll(...)
  1591.   local exceptions = {...}
  1592.   local walls
  1593.   if type(exceptions[1]) == "boolean" then
  1594.     walls = exceptions[1]
  1595.     table.remove(exceptions, 1)
  1596.   end
  1597.   insert('Compare()')
  1598.   insert('CompareLeft()')
  1599.   insert('CompareBack()')
  1600.   insert('CompareRight()')
  1601.   insert('CompareUp()')
  1602.   insert('CompareDown()')
  1603.   insert('Return()')
  1604.   local sub = 0
  1605.   for j = 2, 8-sub do
  1606.     for k, v in ipairs(exceptions) do
  1607.       if v == todoList[j] then
  1608.         table.remove(todoList, j)
  1609.         table.remove(exceptions, k)
  1610.         j = j - 1
  1611.         sub = sub + 1
  1612.       end
  1613.     end
  1614.   end
  1615.   if walls == true then
  1616.     for i = 2, 15 do
  1617.       if todoList[i] == 'Return()' then
  1618.         break
  1619.       elseif string.find(todoList[i], 'placeWall(') == nil then
  1620.         if string.find(todoList[i], 'Up') ~= nil then
  1621.           table.insert(todoList, i+1, 'placeWall("placeUp")')
  1622.         elseif string.find(todoList[i], 'Down') == nil then
  1623.           table.insert(todoList, i+1, 'placeWall()')
  1624.         end
  1625.       end
  1626.     end
  1627.   end
  1628. end
  1629.  
  1630. function trash(keptCobble)
  1631.   for k, v in ipairs(SlotCalculator("_")) do
  1632.     local data = turtle.getItemDetail(v)
  1633.     for k2, v2 in pairs(settings.ignor) do
  1634.       if v2 == data.name then
  1635.         if not keptCobble and v2 == "minecraft:cobblestone" then
  1636.           keptCobble = true
  1637.         else
  1638.           turtle.select(v)
  1639.           turtle.drop()
  1640.         end
  1641.       end
  1642.     end
  1643.   end
  1644. end
  1645.  
  1646. function Enderchest(keptCoal, keptCobble)
  1647.   if (#SlotCalculator("_") >= #SlotCalculator("_", "empty")-1-math.ceil(settings.length/12) or keptCoal == true) and #SlotCalculator("C", "check") ~= 0 then
  1648.     turtle.select(SlotCalculator("C", "check")[1])
  1649.     if variables.level == 1 then
  1650.       insert('FreewayDown()')
  1651.     end
  1652.     insert('FreewayBack()')
  1653.     insert([[while not turtle.place() do
  1654.       turtle.attack()
  1655.     end]])
  1656.     insert([[for k, v in ipairs(SlotCalculator("_")) do
  1657.       if settings.Autofuel and not keptCoal and Version >= 1.64 and turtle.getItemDetail(v).name == "minecraft:coal" then
  1658.         keptCoal = true
  1659.       elseif not keptCobble and v == "minecraft:cobblestone" then
  1660.         keptCobble = true
  1661.       else
  1662.         turtle.select(v)
  1663.         while not turtle.drop() do
  1664.           print(lang.status(settings.language, 3))
  1665.           sleep(10)
  1666.         end
  1667.       end
  1668.     end
  1669.     turtle.select(SlotCalculator("C", "empty")[1])]])
  1670.     insert('dig()')
  1671.     insert('Freeway()')
  1672.     insert([[turtle.select(SlotCalculator("_", "empty")[1])
  1673.     print(lang.status(settings.language, 4))]])
  1674.   end
  1675. end
  1676.  
  1677. function NormalChest(keptCoal, keptCobble)
  1678.   local function goToOrigin()
  1679.     insert('turn()')
  1680.     insert([[if variables.level == 1 then
  1681.       FreewayDown()
  1682.     end
  1683.     for Way_Back = 1, variables.CrosswayAmount*settings.tunnelspace do
  1684.       Freeway()
  1685.     end]])
  1686.     insert('turn()')
  1687.   end
  1688.   local function dropOff()
  1689.     insert([[for k, v in ipairs(SlotCalculator("_")) do
  1690.       if settings.Autofuel and not keptCoal and Version >= 1.64 and turtle.getItemDetail(v).name == "minecraft:coal" then
  1691.         keptCoal = true
  1692.       elseif not keptCobble and v == "minecraft:cobblestone" then
  1693.         keptCobble = true
  1694.       else
  1695.         turtle.select(v)
  1696.         while not turtle.dropDown() and turtle.getItemCount() ~= 0 do
  1697.           print(lang.status(settings.language, 3))
  1698.           sleep(10)
  1699.         end
  1700.       end
  1701.     end]])
  1702.   end
  1703.   if variables.setup == nil then
  1704.     if #SlotCalculator("C", "check") == 0 then
  1705.       variables.setup = false
  1706.     elseif #SlotCalculator("_") >= #SlotCalculator("_", "empty")-1-math.ceil(settings.length/12) then
  1707.       goToOrigin()
  1708.       insert('turtle.select(SlotCalculator("C", "check")[1])')
  1709.       insert([[while not turtle.placeDown() do
  1710.         digDown()
  1711.       end]])
  1712.       insert('variables.setup = true')
  1713.       dropOff()
  1714.       insert([[for Way_Back = 1, variables.CrosswayAmount*settings.tunnelspace do
  1715.         Freeway()
  1716.       end]])
  1717.       insert('print(lang.status(settings.language, 4))')
  1718.     end
  1719.   elseif variables.setup == true then
  1720.     goToOrigin()
  1721.     dropOff()
  1722.     insert([[for Way_Back = 1, variables.CrosswayAmount*settings.tunnelspace do
  1723.       Freeway()
  1724.     end]])
  1725.     insert('print(lang.status(settings.language, 4))')
  1726.   end
  1727. end
  1728.  
  1729. function Fuel()
  1730.   local FuelAmount = math.ceil(3*variables.FuelDemand/settings.quantity/80)
  1731.   fuelLevel = turtle.getFuelLevel()
  1732.   if fuelLevel < 3*variables.FuelDemand/settings.quantity then
  1733.     while true do
  1734.       while #SlotCalculator("_", "minecraft:coal") > 0 do
  1735.         turtle.select(SlotCalculator("_", "minecraft:coal")[1])
  1736.         local output = refuel(FuelAmount, true)
  1737.         if output == true then
  1738.           print(lang.status(settings.language, 5))
  1739.           return
  1740.         else
  1741.           FuelAmount = FuelAmount - output
  1742.         end
  1743.       end
  1744.       term.clear()
  1745.       term.setCursorPos(1,1)
  1746.       print(lang.status(settings.language, 6))
  1747.       write(lang.status(settings.language, 7))
  1748.       for k, v in ipairs(SlotCalculator("_", "empty")) do
  1749.         if k == #SlotCalculator("_", "empty") then
  1750.           print(v)
  1751.         else
  1752.           write(v..", ")
  1753.         end
  1754.       end
  1755.       os.pullEvent()
  1756.     end
  1757.   end
  1758. end
  1759.  
  1760. function placeFloor()
  1761.   if settings.floor == true and turtle.detectDown() == false and #SlotCalculator("_", "minecraft:cobblestone") ~= 0 then
  1762.     turtle.select(SlotCalculator("_", "minecraft:cobblestone")[1])
  1763.     turtle.placeDown()
  1764.   end
  1765. end
  1766.  
  1767. function placeWall(param)
  1768.   if param == true then
  1769.     param = "place"
  1770.   else
  1771.     param = param or "place"
  1772.   end
  1773.   if #SlotCalculator("_", "minecraft:cobblestone") > 0 then
  1774.     turtle.select(SlotCalculator("_", "minecraft:cobblestone")[1])
  1775.     turtle[param]()
  1776.     return true
  1777.   end
  1778.   return false
  1779. end
  1780.  
  1781. -----###-----Processing-----###-----
  1782. function TorchCalculator()
  1783.   local Tspace
  1784.   if settings.torches == true then
  1785.     if (settings.tunnelspace == 4 and settings.length <= 8) or (settings.tunnelspace == 3 and settings.length <= 10) then
  1786.       settings.lateralTorches = 1
  1787.     else
  1788.       settings.lateralTorches = math.floor((settings.length-math.floor(16/settings.tunnelspace))/12)+1
  1789.     end
  1790.     if (settings.tunnelspace == 4 and settings.length <= 3) or (settings.tunnelspace == 3 and settings.length <= 4) then
  1791.       settings.mainTorches = true
  1792.       settings.lateralTorches = 0
  1793.     elseif (settings.tunnelspace == 4 and ((settings.length+2)%12 <= 5 or settings.length == 9)) or (settings.tunnelspace == 3 and (settings.length+1)%12 <= 5) then
  1794.       settings.mainTorches = true
  1795.     else
  1796.       settings.mainTorches = false
  1797.     end
  1798.   end
  1799.   if settings.lateralTorches >= 1 then
  1800.     if settings.mainTorches == true then
  1801.       Tspace = round((settings.length+(settings.tunnelspace-1)-settings.lateralTorches)/(settings.lateralTorches+0.5), "u")
  1802.     else
  1803.       Tspace = (settings.length+(settings.tunnelspace-1)-settings.lateralTorches)/settings.lateralTorches
  1804.     end
  1805.     if Tspace == 11 then
  1806.       variables.maxSpace = true
  1807.     else
  1808.       variables.maxSpace = false
  1809.     end
  1810.     local done = false
  1811.     while not done do
  1812.       variables.torchPositions = {}
  1813.       table.insert(variables.torchPositions, round(Tspace/2, "d")+1)
  1814.       for i = 2, settings.lateralTorches do
  1815.         table.insert(variables.torchPositions, round(Tspace+1+variables.torchPositions[i-1], "d"))
  1816.       end
  1817.       if variables.torchPositions[#variables.torchPositions] > settings.length then
  1818.         Tspace = Tspace - 0.1
  1819.       else
  1820.         done = true
  1821.       end
  1822.     end
  1823.   else
  1824.     variables.maxSpace = false
  1825.     variables.torchPositions = {}
  1826.   end
  1827.   variables.TorchDemand = 2*(settings.lateralTorches*settings.quantity)
  1828.   if settings.mainTorches == true then
  1829.     variables.TorchDemand = variables.TorchDemand + settings.quantity
  1830.   end
  1831.   for k, v in ipairs(SlotCalculator("T", "empty")) do
  1832.     variables.slots[v] = "_"
  1833.   end
  1834.   local stupid = SlotCalculator("_", "empty")
  1835.   for k, v in ipairs(stupid) do
  1836.     if k > #stupid-math.ceil(variables.TorchDemand/64) then
  1837.       variables.slots[v] = "T"
  1838.     end
  1839.   end
  1840. end
  1841.  
  1842. function calculateFuelDemand()
  1843.   if settings.chestSelect == 1 then
  1844.     variables.FuelDemand = ((settings.length*2+settings.tunnelspace)*3+3)*settings.quantity
  1845.   elseif settings.chestSelect == 2 then
  1846.     variables.FuelDemand = math.ceil(((settings.length*2+settings.tunnelspace)*3+2)*settings.quantity+settings.tunnelspace*(settings.quantity/2)^2)
  1847.   else
  1848.     variables.FuelDemand = ((settings.length*2+settings.tunnelspace)*3+2)*settings.quantity
  1849.   end
  1850.   if variables.maxSpace == true then
  1851.     variables.FuelDemand = variables.FuelDemand + #variables.torchPositions
  1852.   end
  1853. end
  1854.  
  1855.  
  1856. -----###-----Core functions-----###-----
  1857. function update()
  1858.   if not fs.exists("database/"..programName.."/state") then
  1859.     save("database/"..programName.."/state", "0")
  1860.   end
  1861.   save("database/"..programName.."/programName", programName)
  1862.   file = fs.open("database/"..programName.."/state", "r")
  1863.   local fileData = {}
  1864.   local line = file.readLine()
  1865.   repeat
  1866.     table.insert(fileData, line)
  1867.     line = file.readLine()
  1868.   until line == nil
  1869.   file.close()
  1870.   updated = fileData[1]
  1871.  
  1872.   if updated == "0" then
  1873.     kill = true
  1874.     term.clear()
  1875.     print("loading...")
  1876.     shell.run("pastebin get cR9vEiTc database/"..programName.."/lang")
  1877.     term.clear()
  1878.     local function installAsStartup()
  1879.       if fs.exists("startup") then
  1880.         os.loadAPI("startup")
  1881.         print(startup.name)
  1882.         print("1")
  1883.         os.pullEvent("key")
  1884.         if startup.name == "SupHa - Startup Handler by BrunoZockt" then
  1885.           print("2")
  1886.           os.pullEvent("key")
  1887.           for k, v in ipairs(startup.getList()) do
  1888.             if v == programName then
  1889.               return
  1890.             end
  1891.           end
  1892.           startup.set(programName)
  1893.           print("3")
  1894.           os.pullEvent("key")
  1895.         else
  1896.           os.unloadAPI("startup")
  1897.           fs.move("startup", "old_startup")
  1898.           shell.run("pastebin get 19VG8eD6 startup")
  1899.           os.loadAPI("startup")
  1900.           startup.set("old_startup", "standart")
  1901.           print("4")
  1902.           os.pullEvent("key")
  1903.           startup.set(programName)
  1904.           print("5")
  1905.           os.pullEvent("key")
  1906.         end
  1907.       else
  1908.         shell.run("pastebin get 19VG8eD6 startup")
  1909.         os.loadAPI("startup")
  1910.         startup.set(programName)
  1911.         print("6")
  1912.         os.pullEvent("key")
  1913.       end
  1914.     end
  1915.     installAsStartup()
  1916.     --if shell.run("database/"..programName.."/updater") == false then
  1917.       term.clear()
  1918.       print("loading...")
  1919.       shell.run("pastebin get 4JpA6xHu database/"..programName.."/updater")
  1920.       term.clear()
  1921.       shell.run("database/"..programName.."/updater")
  1922.     --end
  1923.   end
  1924. end
  1925.  
  1926. function getVariables(path)
  1927.   local extSettings = {}
  1928.   if fs.exists("database/"..programName.."/"..path) then
  1929.     file = fs.open("database/"..programName.."/"..path, "r")
  1930.     local line = Splitter(file.readLine(), " = ")
  1931.     repeat
  1932.       if line[2] == "{" or line[2] == "{}" then
  1933.         local str = line[2]
  1934.         if line[2] == "{" then
  1935.         repeat
  1936.           local tline = file.readLine()
  1937.           if tline ~= nil then
  1938.             str = str..tline
  1939.           end
  1940.         until tline == "}"
  1941.         end
  1942.         extSettings[line[1]] = textutils.unserialize(str)
  1943.       else
  1944.         extSettings[line[1]] = line[2]
  1945.       end
  1946.       line = Splitter(file.readLine(), " = ")
  1947.     until line == nil
  1948.     file.close()
  1949.     for k, v in pairs(extSettings) do
  1950.       if type(extSettings[k]) ~= "table" then
  1951.         if not tonumber(extSettings[k]) then
  1952.           if extSettings[k] == "true" or extSettings[k] == "false" then
  1953.             extSettings[k] = extSettings[k] == "true"
  1954.           end
  1955.         else
  1956.           extSettings[k] = tonumber(extSettings[k])
  1957.         end
  1958.       end
  1959.       if Splitter(path, "/")[#Splitter(path, "/")] == "settings" then
  1960.         settings[k] = extSettings[k]
  1961.       elseif Splitter(path, "/")[#Splitter(path, "/")] == "variables" then
  1962.         variables[k] = extSettings[k]
  1963.       end
  1964.     end
  1965.   end
  1966.   if fs.exists("database/"..programName.."/updateCheckbox/V2.5") then
  1967.     local file = fs.open("database/"..programName.."/updateCheckbox/V2.5", "r")
  1968.     checkbox = file.readLine() == "true"
  1969.     file.close()
  1970.   end
  1971.   if checkbox == true then
  1972.     menustate = "Home"
  1973.   else
  1974.     menustate = "Update"
  1975.   end
  1976.   if settings.chestSelect < 3 and #SlotCalculator("C", "empty") == 0 then
  1977.     variables.slots[16] = "C"
  1978.   end
  1979.   calculateFuelDemand()
  1980.   TorchCalculator()
  1981. end
  1982.  
  1983. function getList(path)
  1984.   local file = fs.open("database/"..programName.."/"..path, "r")
  1985.   todoList = textutils.unserialize(Splitter(file.readLine(), " = ")[2]..file.readAll())
  1986.   file.close()
  1987. end
  1988.  
  1989. function saveVars(path, content)
  1990.   save("database/"..programName.."/"..path, content)
  1991. end
  1992.  
  1993. function compStep(n)
  1994.   if n == nil then
  1995.     n = 1
  1996.   else
  1997.     n = tonumber(n)
  1998.   end
  1999.   for i = 1, n do
  2000.     insert('turtle.select(SlotCalculator("T", "empty")[1])')
  2001.     insert('Freeway()')
  2002.     if variables.level == 0 then
  2003.       insert('CompareAll("Compare()", "CompareBack()", "CompareUp()")')
  2004.       insert('placeFloor()')
  2005.       insert('turtle.select(SlotCalculator("T", "empty")[1])')
  2006.       insert('FreewayUp()')
  2007.       insert('CompareAll("Compare()", "CompareBack()", "CompareDown()")')
  2008.     elseif variables.level == 1 then
  2009.       insert('CompareAll("Compare()", "CompareBack()", "CompareDown()")')
  2010.       insert('turtle.select(SlotCalculator("T", "empty")[1])')
  2011.       insert('FreewayDown()')
  2012.       insert('CompareAll("Compare()", "CompareBack()", "CompareUp()")')
  2013.       insert('placeFloor()')
  2014.     end
  2015.   end
  2016. end
  2017.  
  2018. function mainstep(n, first)
  2019.   if n == nil then
  2020.     n = 1
  2021.   else
  2022.     n = tonumber(n)
  2023.   end
  2024.   turtle.select(SlotCalculator("_", "empty")[1])
  2025.   for i = 1, n do
  2026.     insert('variables.searching = false')
  2027.     insert('dig()')
  2028.     insert('Freeway()')
  2029.     insert([[if variables.level == 0 then
  2030.       insert('placeFloor()')
  2031.       insert('digUp()')
  2032.       if ]]..tostring(first)..[[ and ]]..tostring(i)..[[ == 1 or (settings.walls == true and ]]..tostring(i)..[[ == 2 and settings.tunnelspace == 4) then
  2033.         insert('CompareAll("Compare()", "CompareBack()", "CompareUp()")')
  2034.       else
  2035.         insert('CompareDown()')
  2036.       end
  2037.       insert('FreewayUp()')
  2038.       if ]]..tostring(first)..[[ and ]]..tostring(i)..[[ == 1 or (settings.walls == true and ]]..tostring(i)..[[ == 2 and settings.tunnelspace == 4) then
  2039.         insert('CompareAll("Compare()", "CompareBack()", "CompareDown()")')
  2040.       else
  2041.         insert('CompareUp()')
  2042.         insert('placeWall("placeUp")')
  2043.       end
  2044.     elseif variables.level == 1 then
  2045.       insert('digDown()')
  2046.       if ]]..tostring(first)..[[ and ]]..tostring(i)..[[ == 1 or (settings.walls == true and ]]..tostring(i)..[[ == 2 and settings.tunnelspace == 4) then
  2047.         insert('CompareAll("Compare()", "CompareBack()", "CompareDown()")')
  2048.       else
  2049.         insert('CompareUp()')
  2050.         insert('placeWall("placeUp")')
  2051.       end
  2052.       if i == 2 and settings.mainTorches then
  2053.         insert('variables.searching = false')
  2054.         insert('dig(true)')
  2055.       end
  2056.       insert('FreewayDown()')
  2057.       if ]]..tostring(first)..[[ and ]]..tostring(i)..[[ == 1 or (settings.walls == true and ]]..tostring(i)..[[ == 2 and settings.tunnelspace == 4) then
  2058.         insert('CompareAll("Compare()", "CompareBack()", "CompareUp()")')
  2059.       else
  2060.         insert('CompareDown()')
  2061.       end
  2062.       insert('placeFloor()')
  2063.     end]])
  2064.     if settings.mainTorches == true then
  2065.       if i == 2 and #SlotCalculator('T', 'check') ~= 0 then
  2066.         insert([[turtle.select(SlotCalculator('T', 'check')[1])
  2067.         if variables.level == 1 then
  2068.           FreewayDown()
  2069.         end]])
  2070.         insert([[if not turtle.placeUp() then
  2071.           insert('FreewayUp()')
  2072.           insert('left()')
  2073.           insert('turtle.select(SlotCalculator("_", "minecraft:cobblestone")[1])')
  2074.           insert('dig()')
  2075.           insert('turtle.place()')
  2076.           insert('right()')
  2077.           insert('FreewayDown()')
  2078.           insert('turtle.select(SlotCalculator("T", "check")[1])')
  2079.           insert('turtle.placeUp()')
  2080.         end]])
  2081.         insert([[for k, v in ipairs(SlotCalculator('T', 'empty')) do
  2082.           variables.slots[v] = '_'
  2083.         end
  2084.         local stupid = SlotCalculator('_', 'empty')
  2085.         for k, v in ipairs(stupid) do
  2086.           if k > #stupid-math.ceil(variables.TorchDemand/64) then
  2087.             variables.slots[v] = 'T'
  2088.           end
  2089.         end
  2090.         variables.TorchDemand = variables.TorchDemand - 1]])
  2091.       end
  2092.     end
  2093.   end
  2094. end
  2095.  
  2096. function stepAside(n)
  2097.   if n == nil then
  2098.     n = 1
  2099.   else
  2100.     n = tonumber(n)
  2101.   end
  2102.   for i = 1, n do
  2103.     insert('variables.searching = false')
  2104.     insert('dig()')
  2105.     insert('Freeway()')
  2106.     if variables.level == 1 then
  2107.       insert('digDown()')
  2108.     elseif variables.level == 0 then
  2109.       insert('placeFloor()')
  2110.       insert('digUp()')
  2111.     end
  2112.   end
  2113. end
  2114.  
  2115. function youJustGotLittUp(steps, func, left)
  2116.   if left == nil then
  2117.     left = false
  2118.   end
  2119.   for Steprepeat = 1, steps do
  2120.     if func == stepAside then
  2121.       insert('stepAside()')
  2122.     end
  2123.     for k, v in ipairs(variables.torchPositions) do
  2124.       if ((func == stepAside and settings.length-v+1 == Steprepeat) or (func == compStep and v == Steprepeat)) and #SlotCalculator("T", "check") > 0 then
  2125.         insert([[turtle.select(SlotCalculator("T", "check")[1])
  2126.         if variables.maxSpace == true then
  2127.           if settings.mainTorches == false and left == true then
  2128.             if k%2 == #variables.torchPositions%2 then
  2129.               if variables.level == 1 then
  2130.                 if variables.orientation[1] == "North" or variables.orientation[1] == "South" then
  2131.                   insert('dig()')
  2132.                 end
  2133.                 insert('FreewayDown()')
  2134.               elseif variables.orientation[1] == "North" or variables.orientation[1] == "South" then
  2135.                 insert('FreewayUp()')
  2136.                 insert('dig()')
  2137.                 insert('FreewayDown()')
  2138.               end
  2139.             else
  2140.               if variables.level == 0 then
  2141.                 insert('FreewayUp()')
  2142.               end
  2143.             end
  2144.           else
  2145.             if k%2 == #variables.torchPositions%2 then
  2146.               if variables.level == 0 then
  2147.                 insert('FreewayUp()')
  2148.               end
  2149.             else
  2150.               if variables.level == 1 then
  2151.                 if variables.orientation[1] == "North" or variables.orientation[1] == "South" then
  2152.                   insert('dig()')
  2153.                 end
  2154.                 insert('FreewayDown()')
  2155.               elseif variables.orientation[1] == "North" or variables.orientation[1] == "South" then
  2156.                 insert('FreewayDown()')
  2157.                 insert('dig()')
  2158.                 insert('FreewayUp()')
  2159.               end
  2160.             end
  2161.           end
  2162.         elseif variables.orientation[1] == "North" or variables.orientation[1] == "South" then
  2163.           if variables.level == 0 then
  2164.             insert('FreewayUp()')
  2165.           end
  2166.         end]])
  2167.         insert([[if variables.level == 1 then
  2168.           turtle.placeDown()
  2169.         elseif variables.level == 0 then
  2170.           if not turtle.placeUp() then
  2171.             insert('FreewayUp()')
  2172.             insert('left()')
  2173.             insert('turtle.select(SlotCalculator("_", "minecraft:cobblestone")[1])')
  2174.             insert('dig()')
  2175.             insert('turtle.place()')
  2176.             insert('right()')
  2177.             insert('FreewayDown()')
  2178.             insert('turtle.select(SlotCalculator("T", "check")[1])')
  2179.             insert('turtle.placeUp()')
  2180.           end
  2181.         end]])
  2182.         insert([[for k, v in ipairs(SlotCalculator("T", "empty")) do
  2183.           variables.slots[v] = "_"
  2184.         end
  2185.         local stupid = SlotCalculator("_", "empty")
  2186.         for k, v in ipairs(stupid) do
  2187.           if k > #stupid-math.ceil(variables.TorchDemand/64) then
  2188.             variables.slots[v] = "T"
  2189.           end
  2190.         end]])
  2191.         insert([[if func == compStep then
  2192.           variables.TorchDemand = variables.TorchDemand - 1
  2193.         end
  2194.         turtle.select(SlotCalculator("_", "empty")[1])]])
  2195.       end
  2196.     end
  2197.     if func == compStep then
  2198.       if Steprepeat == settings.length then
  2199.         insert('Freeway()')
  2200.       else
  2201.         insert('compStep()')
  2202.       end
  2203.     end
  2204.   end
  2205. end
  2206.  
  2207. function tunnel(first)
  2208.   if settings.Autofuel == true then
  2209.     insert('Fuel()')
  2210.   end
  2211.   if settings.trash == true then
  2212.     insert('trash()')
  2213.   end
  2214.   if settings.chestSelect == 1 then
  2215.     insert('Enderchest()')
  2216.   elseif settings.chestSelect == 2 then
  2217.     insert('NormalChest()')
  2218.   end
  2219.   insert("mainstep(settings.tunnelspace, "..tostring(first)..")")
  2220.   insert("turtle.turnRight()")
  2221.   insert('old_orientation = {}')
  2222.   insert([[for k,v in ipairs(variables.orientation) do
  2223.     old_orientation[k] = v
  2224.   end]])
  2225.   for i = 1, 4 do
  2226.     insert('variables.orientation['..tostring(i)..'] = old_orientation[('..tostring(i)..'+2)%4+1]')
  2227.   end
  2228.   insert("youJustGotLittUp(settings.length, stepAside)")
  2229.   insert([[if variables.level == 1 then
  2230.     insert('CompareAll("CompareBack()", "CompareDown()")')
  2231.     insert('FreewayDown()')
  2232.     insert('CompareAll("CompareBack()", "CompareUp()")')
  2233.   else
  2234.     insert('CompareAll("CompareBack()", "CompareUp()")')
  2235.     insert('FreewayUp()')
  2236.     insert('CompareAll("CompareBack()", "CompareDown()")')
  2237.   end]])
  2238.   insert('face(3)')
  2239.   insert('old_orientation = {}')
  2240.   insert([[for k,v in ipairs(variables.orientation) do
  2241.     old_orientation[k] = v
  2242.   end]])
  2243.   for i = 1, 4 do
  2244.     insert('variables.orientation['..tostring(i)..'] = old_orientation[('..tostring(i)..'+1)%4+1]')
  2245.   end
  2246.   insert('variables.direction = 1')
  2247.   insert("youJustGotLittUp(settings.length, compStep)")
  2248.   insert("youJustGotLittUp(settings.length, stepAside, true)")
  2249.   insert([[if variables.level == 1 then
  2250.     insert('CompareAll("CompareBack()", "CompareDown()")')
  2251.     insert('FreewayDown()')
  2252.     insert('CompareAll("CompareBack()", "CompareUp()")')
  2253.   else
  2254.     insert('CompareAll("CompareBack()", "CompareUp()")')
  2255.     insert('FreewayUp()')
  2256.     insert('CompareAll("CompareBack()", "CompareDown()")')
  2257.   end]])
  2258.   insert('face(3)')
  2259.   insert('old_orientation = {}')
  2260.   insert([[for k,v in ipairs(variables.orientation) do
  2261.     old_orientation[k] = v
  2262.   end]])
  2263.   for i = 1, 4 do
  2264.     insert('variables.orientation['..tostring(i)..'] = old_orientation[('..tostring(i)..'+1)%4+1]')
  2265.   end
  2266.   insert('variables.direction = 1')
  2267.   insert("youJustGotLittUp(settings.length, compStep, true)")
  2268.   insert('turtle.turnLeft()')
  2269.   insert('old_orientation = {}')
  2270.   insert([[for k,v in ipairs(variables.orientation) do
  2271.     old_orientation[k] = v
  2272.   end]])
  2273.   for i = 1, 4 do
  2274.     insert('variables.orientation['..tostring(i)..'] = old_orientation[('..tostring(i)..')%4+1]')
  2275.   end
  2276. end
  2277.  
  2278. function go()
  2279.   saveVars("resume/settings", settings)
  2280.   variables.startDay = os.day()
  2281.   variables.startTime = os.time()
  2282.   for Tunnelrepeat = 1, settings.quantity do
  2283.     insert("tunnel("..tostring(Tunnelrepeat == 1)..")")
  2284.     insert([[variables.CrosswayAmount = variables.CrosswayAmount + 1
  2285.     print(lang.status(settings.language, 8, variables.CrosswayAmount, settings.quantity))]])
  2286.   end
  2287.   insert('print(lang.status(settings.language, 9))')
  2288.   insert('turn()')
  2289.   insert([[if variables.level == 1 then
  2290.     FreewayDown()
  2291.   end]])
  2292.   for Way_Back = 1, settings.quantity*settings.tunnelspace do
  2293.     insert('Freeway()')
  2294.   end
  2295.   if settings.trash == true then
  2296.     insert('trash()')
  2297.   end
  2298.   if settings.chestSelect == 1 then
  2299.     insert("Enderchest(true)")
  2300.   elseif settings.chestSelect == 2 then
  2301.     insert("NormalChest(true, true)")
  2302.   end
  2303.   insert([[local endDay = os.day()
  2304.   local endTime = os.time()
  2305.   variables.stats["time"] = (endDay-variables.startDay)*24.000+(endTime-variables.startTime)]])
  2306. end
  2307.  
  2308. function Navigation()
  2309.   table.insert(timer, os.startTimer(0.05))
  2310.   while true do
  2311.     local tempState = menustate
  2312.     local event, p1, p2, p3 = os.pullEvent()
  2313.     while (event == "timer" and p1 ~= timer[#timer]) or event == "key_up" do
  2314.       event, p1, p2, p3 = os.pullEvent()
  2315.     end
  2316.     if menuVars["drawOptions"].focus == 100 then
  2317.       basicInputHandler(menu[menustate].orientation, event, p1)
  2318.     end
  2319.     if menustate == "Quit" then
  2320.       break
  2321.     elseif menustate == "Save as default" then
  2322.       saveVars("settings", settings)
  2323.       menuVars["drawOptions"].saved = true
  2324.       menustate = "Options"
  2325.     elseif menustate == "Go!" then
  2326.       menustate = "Popup"
  2327.       local ready = true
  2328.       for i = 1, #errors do
  2329.         if errors[i] == true then
  2330.           ready = false
  2331.         end
  2332.       end
  2333.       if (Fuellevel ~= "low" and ready == true) or tempState == "Popup" then
  2334.         return execute()
  2335.       end
  2336.     end
  2337.     term.clear()
  2338.     drawHeader()
  2339.     if tempState == menustate then
  2340.       menu[menustate].draw(event, p1, p2, p3)
  2341.     else
  2342.       menu[menustate].draw()
  2343.     end
  2344.     if tempState == "Update" then
  2345.       save("database/"..programName.."/updateCheckbox/V2.5", checkbox)
  2346.     end
  2347.     table.insert(timer, os.startTimer(0.5))
  2348.   end
  2349. end
  2350.  
  2351. function extractstats()
  2352.   save("database/"..programName.."/variables.stats", variables.stats)
  2353. end
  2354.  
  2355. function close()
  2356.   fs.delete("database/"..programName.."/resume")
  2357.   term.clear()
  2358.   term.setCursorPos(1,1)
  2359.   if Color() then
  2360.     term.setTextColor(colors.yellow)
  2361.     print(os.version())
  2362.     term.setTextColor(colors.white)
  2363.   else
  2364.     print(os.version())
  2365.   end
  2366.   term.setCursorPos(1, 2)
  2367. end
  2368.  
  2369. -----###-----Executive-----###------
  2370.  
  2371. local function compile(chunk) -- returns compiled chunk or nil and error message
  2372.   if type(chunk) ~= "string" then
  2373.     error("expected string, got ".. type(chunk), 2)
  2374.   end
  2375.  
  2376.   local function findChunkName(var)
  2377.     for k,v in pairs(HOST_ENV) do
  2378.       if v==var then
  2379.         return k
  2380.       end
  2381.     end
  2382.     return "Unknown chunk"
  2383.   end
  2384.  
  2385.   return load(chunk, findChunkName(chunk), "t", OUR_ENV)
  2386. end
  2387.  
  2388. function getOrientation(resume)
  2389.   if resume then
  2390.     for i = 1, 2-variables.level do
  2391.       insert('FreewayUp()')
  2392.     end
  2393.   end
  2394.   if #SlotCalculator("T", "check") ~= 0 then
  2395.     insert('Freeway()')
  2396.     if Version >= 1.64 then
  2397.       insert('FreewayUp()')
  2398.       for i = 1, 3 do
  2399.         insert('right()')
  2400.         insert('dig(true)')
  2401.       end
  2402.       insert('right()')
  2403.       insert([[if turtle.detect() == false then
  2404.         turtle.select(SlotCalculator("_", "minecraft:cobblestone")[1])
  2405.         turtle.place()
  2406.       end]])
  2407.       insert('FreewayDown()')
  2408.       insert('turtle.select(SlotCalculator("T", "check")[1])')
  2409.       insert('turtle.placeUp()')
  2410.  
  2411.       insert('torch = {"West", "East", "North", "South"}')
  2412.       insert('_, data = turtle.inspectUp()')
  2413.       insert('new_orientation = torch[data.metadata]')
  2414.  
  2415.       insert('digUp()')
  2416.       insert('FreewayBack()')
  2417.       insert('_, data, torch = nil, nil, nil')
  2418.     else
  2419.       insert('Freeway()')
  2420.       insert('digDown()')
  2421.       insert('FreewayUp()')
  2422.       for i = 1, 4 do
  2423.         insert([[if turtle.detect() == false then
  2424.           turtle.select(SlotCalculator("_", "minecraft:cobblestone")[1])
  2425.           turtle.place()
  2426.         end]])
  2427.         insert('right()')
  2428.       end
  2429.       insert('FreewayDown()')
  2430.       insert('turtle.select(SlotCalculator("T", "check")[1])')
  2431.       insert('turtle.placeUp()')
  2432.  
  2433.       insert('torch = {"West", "South", "East", "North"}')
  2434.       for i = 1, 4 do
  2435.         insert('Freeway()')
  2436.         insert('digUp()')
  2437.         insert('os.sleep(0.5)')
  2438.         insert('FreewayBack()')
  2439.         insert([[if turtle.detectUp() == false then
  2440.           i = ]]..tostring(i)..[[
  2441.           insert('turtle.select(SlotCalculator("T", "check")[1])')
  2442.           insert('turtle.suckDown()')
  2443.           insert('new_orientation = torch[i]')
  2444.           insert('i = nil')
  2445.         end]])
  2446.         insert('turtle.turnLeft()')
  2447.       end
  2448.       insert('face(variables.direction)')
  2449.       insert('FreewayBack()')
  2450.     end
  2451.   end
  2452.   if resume then
  2453.     for i = 1, 2-variables.level do
  2454.       insert('FreewayDown()')
  2455.     end
  2456.     insert([[for k, v in pairs(cardinal) do
  2457.       if v == new_orientation then
  2458.         new_orientation = k
  2459.       end
  2460.       if v == variables.orientation[variables.direction] then
  2461.         variables.orientation[variables.direction] = k
  2462.       end
  2463.     end]])
  2464.     insert([[if new_orientation ~= variables.orientation[variables.direction] then
  2465.       if math.abs(new_orientation-variables.orientation[variables.direction]) == 2 then
  2466.         insert('turtle.turnLeft()')
  2467.         insert('turtle.turnLeft()')
  2468.       elseif (new_orientation-variables.orientation[variables.direction]+1)%4 == 0 then
  2469.         insert('turtle.turnLeft()')
  2470.       elseif (new_orientation-variables.orientation[variables.direction]-1)%4 == 0 then
  2471.         insert('turtle.turnRight()')
  2472.       end
  2473.     end]])
  2474.     insert('variables.orientation[variables.direction] = cardinal[variables.orientation[variables.direction]]')
  2475.   else
  2476.     insert([[for k, v in ipairs(cardinal) do
  2477.       if new_orientation == v then
  2478.         for i = 1, 4 do
  2479.           table.insert(variables.orientation, cardinal[(k+i-2)%4+1])
  2480.         end
  2481.       end
  2482.     end]])
  2483.   end
  2484. end
  2485.  
  2486. function execute(resume)
  2487.   if not resume then
  2488.     todoList = {"go()"}
  2489.   end
  2490.   table.insert(todoList, 1, 'getOrientation('..tostring(resume)..')')
  2491.   while #todoList > 0 do
  2492.     if resume then
  2493.       os.pullEvent("key")
  2494.     end
  2495.     compile(todoList[1])()
  2496.     table.remove(todoList, 1)
  2497.     variables.index = 2
  2498.     save("database/"..programName.."/resume/todoList", todoList, true)
  2499.     saveVars("resume/variables", variables)
  2500.   end
  2501. end
  2502.  
  2503. function main()
  2504.   update()
  2505.   if kill == true then
  2506.     return
  2507.   end
  2508.   save("database/"..programName.."/state", "0")
  2509.   getVariables("settings")
  2510.   Navigation()
  2511.   --extractstats()
  2512.   close()
  2513. end
  2514.  
  2515. os.loadAPI("database/"..programName.."/lang")
  2516. if fs.exists("database/"..programName.."/resume") then
  2517.   local countdown = 3
  2518.   local count = {}
  2519.   term.clear()
  2520.   drawResume(countdown)
  2521.   table.insert(count, os.startTimer(1))
  2522.   repeat
  2523.     local event, p1 = os.pullEvent()
  2524.     while (event == "timer" and p1 ~= count[#count]) do
  2525.       event, p1 = os.pullEvent()
  2526.     end
  2527.     if event == "key" then
  2528.       if p1 == 28 then
  2529.         fs.delete("database/OCM/resume")
  2530.         return main()
  2531.       end
  2532.     elseif event == "timer" and p1 == count[#count] then
  2533.       countdown = countdown - 1
  2534.     end
  2535.     term.clear()
  2536.     drawResume(countdown)
  2537.     table.insert(count, os.startTimer(1))
  2538.   until countdown == 0
  2539.   getVariables("resume/settings")
  2540.   getVariables("resume/variables")
  2541.   getList("resume/todoList")
  2542.   execute(true)
  2543.   extractstats()
  2544.   close()
  2545. else
  2546.   main()
  2547. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement