Advertisement
CaptainSpaceCat

Recipe Collector

May 27th, 2015
385
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 14.18 KB | None | 0 0
  1. local w, h = term.getSize()
  2. term.setBackgroundColor(colors.black)
  3. term.clear()
  4. turtle.select(1)
  5.  
  6. if not fs.exists("recipes") then
  7.   fs.makeDir("recipes")
  8.   oTemp = fs.open("recipes/list", "w")
  9.   oTemp.writeLine("recipeslist = {}")
  10.   oTemp.flush()
  11.   oTemp.close()
  12. end
  13.  
  14. if not fs.exists("*crafting") then
  15.   oTemp = fs.open("*crafting", "w")
  16.   oTemp.writeLine("c7cccccc7c")
  17.   oTemp.writeLine("700c00c007")
  18.   oTemp.writeLine("c00c00c00c")
  19.   oTemp.writeLine("cccccccccc")
  20.   oTemp.writeLine("c00c00c00c")
  21.   oTemp.writeLine("c00c00c00c")
  22.   oTemp.writeLine("cccccccccc")
  23.   oTemp.writeLine("c00c00c00c")
  24.   oTemp.writeLine("700c00c007")
  25.   oTemp.writeLine("c7cccccc7c")
  26.   oTemp.flush()
  27.   oTemp.close()
  28. end
  29.  
  30. if not fs.exists("*smelting") then
  31.   oTemp = fs.open("*smelting", "w")
  32.   oTemp.writeLine("7777777777")
  33.   oTemp.writeLine("77ffffff77")
  34.   oTemp.writeLine("7ffffffff7")
  35.   oTemp.writeLine("7ffffffff7")
  36.   oTemp.writeLine("7777777777")
  37.   oTemp.writeLine("8888888888")
  38.   oTemp.writeLine("88ee4e1e88")
  39.   oTemp.writeLine("81e41ee4e8")
  40.   oTemp.writeLine("8ee1e4e148")
  41.   oTemp.writeLine("8888888888")
  42.   oTemp.flush()
  43.   oTemp.close()
  44. end
  45.  
  46.  
  47. local craftImg = paintutils.loadImage("*crafting")
  48. local smeltImg = paintutils.loadImage("*smelting")
  49. local stringW = ""
  50. for i = 1, w do
  51.   stringW = stringW .. " "
  52. end
  53.  
  54. local offset = 0
  55. function words(string, x, y, txtcol, bakcol)
  56.   if txtcol then
  57.     term.setTextColor(txtcol)
  58.   end
  59.   if bakcol then
  60.     term.setBackgroundColor(bakcol)
  61.   end
  62.   if not tonumber(x) and x then
  63.     if string.sub(x, 0, 1) == "l" then
  64.       if x == "l" or x == "left" then
  65.         term.setCursorPos(1, y)
  66.       elseif string.sub(x, 3, 3) == "+" or string.sub(x, 6, 6) == "+" then
  67.         offset = tonumber(string.sub(x, 5, 5)) or tonumber(string.sub(x, 8, 8))
  68.         term.setCursorPos(1 + offset, y)
  69.       end
  70.     elseif string.sub(x, 0, 1) == "c" then
  71.       if x == "center" or x == "c" then
  72.         term.setCursorPos(w / 2 - #string / 2 + 1, y)
  73.       elseif string.sub(x, 3, 3) == "+" or string.sub(x, 8, 8) == "+" then
  74.         offset = tonumber(string.sub(x, 5, 5)) or tonumber(string.sub(x, 10, 10))
  75.         term.setCursorPos(w / 2 - #string / 2 + 1 + offset, y)
  76.       elseif string.sub(x, 3, 3) == "-" or string.sub(x, 8, 8) == "-" then
  77.         offset = tonumber(string.sub(x, 5, 5)) or tonumber(string.sub(x, 10, 10))
  78.         term.setCursorPos(w / 2 - #string / 2 + 1 - offset, y)
  79.       end
  80.     elseif string.sub(x, 0, 1) == "r" then
  81.       if x == "right" or x == "r" then
  82.         term.setCursorPos(w - #string + 1, y)
  83.       elseif string.sub(x, 3, 3) == "-" or string.sub(x, 7, 7) == "-" then
  84.         offset = tonumber(string.sub(x, 5, 5)) or tonumber(string.sub(x, 9, 9))
  85.         term.setCursorPos(w - #string + 1 - offset, y)
  86.       end
  87.     end
  88.   else
  89.     term.setCursorPos(x, y)
  90.   end
  91.   term.write(string)
  92. end
  93.  
  94. function clickCheck(x, y, x2, y2)
  95.   if clicked[3] >= x and clicked[3] <= x2 and clicked[4] >= y and clicked[4] <= y2 then
  96.     return true
  97.   else
  98.     return false
  99.   end
  100. end
  101.  
  102. function resetTxtBak()
  103.   term.setBackgroundColor(colors.black)
  104.   term.setTextColor(colors.white)
  105. end
  106.  
  107. function initializeTop()
  108.   words(header, "c", 1)
  109.   term.setCursorPos(1, 2)
  110.   for i = 1, w do
  111.     term.write("-")
  112.   end
  113. end
  114.  
  115. header = "RecipeHub"
  116. local action = nil
  117. local finished = false
  118. local ended = false
  119. local initializedMain = false
  120. while not ended do      ------======MAIN======------
  121.   while not initializedMain do
  122.     resetTxtBak()
  123.     term.clear()
  124.     header = "RecipeHub"
  125.     initializeTop()
  126.     paintutils.drawImage(craftImg, 1, 1)
  127.     paintutils.drawImage(smeltImg, w - 9, 1)
  128.     resetTxtBak()
  129.     words("Add a Recipe", "l + 3", h - 1)
  130.     words("Use a Recipe", "r - 3", h - 1)
  131.     words("Begin", "c", h - 2)
  132.     initializedMain = true
  133.   end
  134.   clicked = {os.pullEvent("mouse_click")}
  135.   if clickCheck(4, h - 1, 15, h - 1) then
  136.     words("Add a Recipe", "l + 3", h - 1, colors.white, colors.blue)
  137.     words("Use a Recipe", "r - 3", h - 1, colors.white, colors.black)
  138.     action = "add"
  139.   end
  140.   if clickCheck(w - 14, h - 1, w - 3, h - 1) then
  141.     words("Add a Recipe", "l + 3", h - 1, colors.white, colors.black)
  142.     words("Use a Recipe", "r - 3", h - 1, colors.white, colors.blue)
  143.     action = "use"
  144.   end
  145.   if clickCheck(18, h - 2, 22, h - 2) then
  146.     if action then
  147.       words("Begin", "c", h - 2, colors.green, colors.black)
  148.       sleep(.1)
  149.       local finished = false
  150.       local subfinished = false
  151.       local stage = 1
  152.       local change = 0
  153.       local iName = nil
  154.       local data = nil
  155.       local newRecipe = {}
  156.       resetTxtBak()
  157.       term.clear()
  158.       if action == "add" then        ------======ADD======------
  159.         action = nil
  160.         header = "Add a Recipe"
  161.         initializeTop()
  162.         words("Stage " .. tostring(stage), "l", 3)
  163.         words("Craft", "l + 5", 5)
  164.         words("Smelt", "r - 5", 5)
  165.         words("Name: ", "l", 7)
  166.         words("Record", "l", 9)
  167.         words("Back", "r", 1, colors.white, colors.black)
  168.         while not finished do
  169.           clicked = {os.pullEvent("mouse_click")}
  170.           if clickCheck(6, 5, 10, 5) then
  171.             words("Craft", "l + 5", 5, colors.white, colors.brown)
  172.             words("Smelt", "r - 5", 5, colors.white, colors.black)
  173.             action = "Craft"
  174.           end
  175.           if clickCheck(w - 10, 5, w - 5, 5) then
  176.             words("Craft", "l + 5", 5, colors.white, colors.black)
  177.             words("Smelt", "r - 5", 5, colors.orange, colors.gray)
  178.             action = "Smelt"
  179.           end
  180.           if clickCheck(1, 7, 12, 7) then
  181.             words(stringW, 1, 7, colors.yellow, colors.lightGray)
  182.             words("Name:", 1, 7)
  183.             term.setCursorPos(7, 7)
  184.             iName = read()
  185.             words(stringW, 1, 7, colors.white, colors.black)
  186.             words("Name:", 1, 7)
  187.             words(iName, "l + 6", 7)
  188.           end
  189.           if clickCheck(w - 3, 1, w, 1) then
  190.             words("Back", "r", 1, colors.white, colors.red)
  191.             sleep(.2)
  192.             initializedMain = false
  193.             finished = true
  194.           else
  195.             words("Back", "r", 1, colors.white, colors.black)
  196.           end
  197.           if clickCheck(1, 9, 6, 9) then
  198.             words("Record", "l", 9, colors.green, colors.black)
  199.             if action and iName then
  200.               change = 0
  201.               for i = 1, 9 do
  202.                 turtle.select(i + change)
  203.                 data = nil
  204.                 data = turtle.getItemDetail(i + change)
  205.                 if data then
  206.                   newRecipe[i + change] = tostring(data.name)
  207.                 end
  208.                 if i == 3 then change = 1 end
  209.                 if i == 6 then change = 2 end
  210.               end
  211.               shell.run("cd recipes")
  212.               oTemp = fs.open("recipes/temp", "w")
  213.               for line in io.lines("recipes/list") do
  214.                 oTemp.writeLine(line)
  215.               end
  216.               os.loadAPI("recipes/list")
  217.               local num = #list.recipeslist + 1
  218.               oTemp.writeLine("recipeslist[" .. num .. "] = {}")
  219.               for i, v in pairs(newRecipe) do
  220.                 oTemp.writeLine("recipeslist[" .. num .. "][" .. i .. "] = \"" .. v .. "\"")
  221.               end
  222.               oTemp.writeLine("recipeslist[" .. num .. "][12] = \"" .. action .. "\"")
  223.               oTemp.writeLine("recipeslist[" .. num .. "][13] = \"" .. iName .. "\"")
  224.               oTemp.flush()
  225.               oTemp.close()
  226.               shell.run("delete list")
  227.               shell.run("copy temp list")
  228.               shell.run("delete temp")
  229.               shell.run("cd /")
  230.               words("Record", "l", 9, colors.white, colors.black)
  231.               words("New Recipe", "l + 2", h - 1, colors.white, colors.black)
  232.               words("New Stage", "c", h - 1)
  233.               words("Finished", "r - 2", h - 1)
  234.               subfinished = false
  235.               while not subfinished do
  236.                 clicked = {os.pullEvent("mouse_click")}
  237.                 if clickCheck(3, h - 1, 12, h - 1) then
  238.                   words("New Recipe", "l + 2", h - 1, colors.yellow, colors.black)
  239.                   sleep(.2)
  240.                   subfinished = true
  241.                 end
  242.                 if clickCheck(15, h - 1, 23, h - 1) then
  243.                   words("New Stage", "c", h - 1, colors.orange, colors.black)
  244.                   sleep(.2)
  245.                   subfinished = true
  246.                   stage = stage + 1
  247.                 end
  248.                 if clickCheck(w - 9, h - 1, w - 2, h - 1) then
  249.                   words("Finished", "r - 2", h - 1, colors.red, colors.black)
  250.                   sleep(.2)
  251.                   initializedMain = false
  252.                   subfinished = true
  253.                   finished = true
  254.                 end
  255.               end
  256.               resetTxtBak()
  257.               term.clear()
  258.               action = nil
  259.               header = "Add a Recipe"
  260.               initializeTop()
  261.               words("Stage " .. tostring(stage), "l", 3)
  262.               words("Craft", "l + 5", 5)
  263.               words("Smelt", "r - 5", 5)
  264.               words("Name: ", "l", 7)
  265.               words("Record", "l", 9)
  266.               words("Back", "r", 1, colors.white, colors.black)
  267.             else
  268.               for i = 1, 2 do
  269.                 words("Record", 1, 9, colors.red, colors.black)
  270.                 sleep(.1)
  271.                 words("Record", 1, 9, colors.white, colors.black)
  272.                 sleep(.1)
  273.               end
  274.             end  
  275.           end
  276.         end
  277.       elseif action == "use" then      ------======USE======------
  278.         action = nil
  279.         local recipelength = 0
  280.         local activeitems = 0
  281.         header = "Use a Recipe"
  282.         local active = nil
  283.         initializeTop()
  284.         words("Back", "r", 1, colors.white, colors.black)
  285.         os.loadAPI("recipes/list")
  286.         for i = 1, #list.recipeslist do
  287.           words(list.recipeslist[i][13], "l", i + 2, colors.white, colors.black)
  288.         end
  289.         while not finished do
  290.           clicked = {os.pullEvent("mouse_click")}
  291.           if active and clickCheck(w - 5, active + 2, w, active + 2) then
  292.             words(list.recipeslist[active][12] .. "ing...", "r", active + 2, colors.cyan, colors.white)
  293.             sleep(.2)
  294.             if list.recipeslist[active][12] == "Craft" then    ------======CRAFTING======------
  295.               shell.run("go left 2")
  296.               activeitems = 0
  297.               turtle.select(13)
  298.               while activeitems ~= recipelength - 2 and turtle.suckUp() do
  299.                 data = turtle.getItemDetail(13)
  300.                 recipelength = 0
  301.                 for i, v in pairs(list.recipeslist[active]) do
  302.                   recipelength = recipelength + 1
  303.                   if data.name == v then
  304.                     activeitems = activeitems + 1
  305.                     turtle.transferTo(i, 1)
  306.                   end
  307.                 end
  308.                 turtle.drop()
  309.               end
  310.               if activeitems == recipelength - 2 then
  311.                 turtle.select(4)
  312.                 turtle.craft(1)
  313.               end
  314.               turtle.select(1)
  315.               while turtle.suck() do
  316.                 turtle.dropUp()
  317.               end
  318.               for i = 1, 16 do
  319.                 turtle.select(i)
  320.                 turtle.dropUp()
  321.               end
  322.               shell.run("go left 2")
  323.             elseif list.recipeslist[active][12] == "Smelt" then          ------======SMELTING======------
  324.               shell.run("go left 2")
  325.               activeitems = 0
  326.               turtle.select(13)
  327.               while activeitems ~= 2 and turtle.suckUp() do
  328.                 data = turtle.getItemDetail(13)
  329.                 recipelength = 0
  330.                 for i, v in pairs(list.recipeslist[active]) do
  331.                   recipelength = recipelength + 1
  332.                   if data.name == v then
  333.                     activeitems = activeitems + 1
  334.                     turtle.transferTo(i, 1)
  335.                   end
  336.                   if data.name == "minecraft:planks" then
  337.                     turtle.transferTo(15)
  338.                   end
  339.                 end
  340.                 turtle.drop()
  341.               end
  342.               if activeitems == 2 then
  343.                 turtle.select(13)
  344.                 while turtle.suck() do
  345.                   turtle.dropUp()
  346.                 end
  347.                 shell.run("go left forward up forward")
  348.                 turtle.select(1)
  349.                 turtle.dropDown(1)
  350.                 shell.run("go back down 2 forward")
  351.                 turtle.select(15)
  352.                 turtle.dropUp(1)
  353.                 while not turtle.suckUp() do
  354.                   sleep(1)
  355.                 end
  356.                 shell.run("go back up back left")
  357.                 for i = 1, 16 do
  358.                   turtle.select(i)
  359.                   turtle.dropUp()
  360.                 end
  361.               end
  362.             end
  363.           end
  364.           active = nil
  365.           for i = 1, #list.recipeslist do
  366.             if clickCheck(1, i + 2, w, i + 2) then
  367.               if list.recipeslist[i][12] == "Craft" then
  368.                 term.setBackgroundColor(colors.brown)
  369.                 term.setTextColor(colors.white)
  370.               elseif list.recipeslist[i][12] == "Smelt" then
  371.                 term.setBackgroundColor(colors.gray)
  372.                 term.setTextColor(colors.orange)
  373.               end
  374.               active = i
  375.               words(stringW, 1, i + 2)
  376.               words(list.recipeslist[i][12], "r", i + 2)
  377.               words(list.recipeslist[i][13], "l", i + 2)
  378.             else
  379.               words(stringW, 1, i + 2, colors.white, colors.black)
  380.               words(list.recipeslist[i][13], "l", i + 2)
  381.             end
  382.           end
  383.           if clickCheck(w - 3, 1, w, 1) then
  384.             words("Back", "r", 1, colors.white, colors.red)
  385.             sleep(.2)
  386.             initializedMain = false
  387.             finished = true
  388.           else
  389.             words("Back", "r", 1, colors.white, colors.black)
  390.           end
  391.         end
  392.       end
  393.     else
  394.       for i = 1, 2 do
  395.         words("Begin", "c", h - 2, colors.red, colors.black)
  396.         sleep(.1)
  397.         words("Begin", "c", h - 2, colors.white, colors.black)
  398.         sleep(.1)
  399.       end
  400.     end
  401.   end
  402. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement