Advertisement
CaptainSpaceCat

Recipe Display

May 28th, 2015
312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.04 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. local header = "RecipeHub"
  108. function initializeTop()
  109.   words(header, "c", 1)
  110.   term.setCursorPos(1, 2)
  111.   for i = 1, w do
  112.     term.write("-")
  113.   end
  114. end
  115. initializeTop()
  116. paintutils.drawImage(craftImg, 1, 1)
  117. paintutils.drawImage(smeltImg, w - 9, 1)
  118. resetTxtBak()
  119. words("Add a Recipe", "l + 3", h - 1)
  120. words("Use a Recipe", "r - 3", h - 1)
  121. words("Begin", "c", h - 2)
  122.  
  123. local action = nil
  124. local finished = false
  125. local ended = false
  126. while not ended do
  127.   clicked = {os.pullEvent("mouse_click")}
  128.   if clickCheck(4, h - 1, 15, h - 1) then
  129.     words("Add a Recipe", "l + 3", h - 1, colors.white, colors.blue)
  130.     words("Use a Recipe", "r - 3", h - 1, colors.white, colors.black)
  131.     action = "add"
  132.   end
  133.   if clickCheck(w - 14, h - 1, w - 3, h - 1) then
  134.     words("Add a Recipe", "l + 3", h - 1, colors.white, colors.black)
  135.     words("Use a Recipe", "r - 3", h - 1, colors.white, colors.blue)
  136.     action = "use"
  137.   end
  138.   if clickCheck(18, h - 2, 22, h - 2) then
  139.     if action then
  140.       words("Begin", "c", h - 2, colors.green, colors.black)
  141.       sleep(.1)
  142.       local finished = false
  143. local subfinished = false
  144. local stage = 1
  145. local change = 0
  146. local iName = nil
  147. local data = nil
  148. local newRecipe = {}
  149. resetTxtBak()
  150. term.clear()
  151. if action == "add" then
  152.   action = nil
  153.   header = "Add a Recipe"
  154.   initializeTop()
  155.   words("Stage " .. tostring(stage), "l", 3)
  156.   words("Craft", "l + 5", 5)
  157.   words("Smelt", "r - 5", 5)
  158.   words("Name: ", "l", 7)
  159.   words("Record", "l", 9)
  160.   while not finished do
  161.     clicked = {os.pullEvent("mouse_click")}
  162.     if clickCheck(6, 5, 10, 5) then
  163.       words("Craft", "l + 5", 5, colors.white, colors.brown)
  164.       words("Smelt", "r - 5", 5, colors.white, colors.black)
  165.       action = "craft"
  166.     end
  167.     if clickCheck(w - 10, 5, w - 5, 5) then
  168.       words("Craft", "l + 5", 5, colors.white, colors.black)
  169.       words("Smelt", "r - 5", 5, colors.orange, colors.gray)
  170.       action = "smelt"
  171.     end
  172.     if clickCheck(1, 7, 12, 7) then
  173.       words(stringW, 1, 7, colors.yellow, colors.lightGray)
  174.       words("Name:", 1, 7)
  175.       term.setCursorPos(7, 7)
  176.       iName = read()
  177.       words(stringW, 1, 7, colors.white, colors.black)
  178.       words("Name:", 1, 7)
  179.       words(iName, "l + 6", 7)
  180.     end
  181.     if clickCheck(1, 9, 6, 9) then
  182.       words("Record", "l", 9, colors.green, colors.black)
  183.       if action and iName then
  184.         change = 0
  185.         for i = 1, 9 do
  186.           turtle.select(i + change)
  187.           data = turtle.getItemDetail(i + change)
  188.           if data then
  189.             newRecipe[i + change] = tostring(data.name)
  190.           end
  191.           if i == 3 then change = 1 end
  192.           if i == 6 then change = 2 end
  193.         end
  194.         shell.run("cd recipes")
  195.         oTemp = fs.open("recipes/temp", "w")
  196.         for line in io.lines("recipes/list") do
  197.           oTemp.writeLine(line)
  198.         end
  199.         os.loadAPI("recipes/list")
  200.         local num = #list.recipeslist + 1
  201.         oTemp.writeLine("recipeslist[" .. num .. "] = {}")
  202.         for i, v in pairs(newRecipe) do
  203.           oTemp.writeLine("recipeslist[" .. num .. "][" .. i .. "] = \"" .. v .. "\"")
  204.         end
  205.         oTemp.writeLine("recipeslist[" .. num .. "][12] = \"" .. action .. "\"")
  206.         oTemp.writeLine("recipeslist[" .. num .. "][13] = \"" .. iName .. "\"")
  207.         oTemp.flush()
  208.         oTemp.close()
  209.         shell.run("delete list")
  210.         shell.run("copy temp list")
  211.         shell.run("delete temp")
  212.         shell.run("cd /")
  213.         words("Record", "l", 9, colors.white, colors.black)
  214.         words("New Recipe", "l + 2", h - 1, colors.white, colors.black)
  215.         words("New Stage", "c", h - 1)
  216.         words("Finished", "r - 2", h - 1)
  217.         subfinished = false
  218.         while not subfinished do
  219.           clicked = {os.pullEvent("mouse_click")}
  220.           if clickCheck(3, h - 1, 12, h - 1) then
  221.             subfinished = true
  222.           end
  223.           if clickCheck(15, h - 1, 23, h - 1) then
  224.             subfinished = true
  225.             stage = stage + 1
  226.           end
  227.           if clickCheck(w - 7, h - 1, w, h - 1) then
  228.             subfinished = true
  229.             finished = true
  230.           end
  231.         end
  232.         resetTxtBak()
  233.         term.clear()
  234.         action = nil
  235.         header = "Add a Recipe"
  236.         initializeTop()
  237.         words("Stage " .. tostring(stage), "l", 3)
  238.         words("Craft", "l + 5", 5)
  239.         words("Smelt", "r - 5", 5)
  240.         words("Name: ", "l", 7)
  241.         words("Record", "l", 9)
  242.       else
  243.         for i = 1, 2 do
  244.           words("Record", 1, 9, colors.red, colors.black)
  245.           sleep(.1)
  246.           words("Record", 1, 9, colors.white, colors.black)
  247.           sleep(.1)
  248.         end
  249.       end  
  250.     end
  251.   end
  252. end
  253.     else
  254.       for i = 1, 2 do
  255.         words("Begin", "c", h - 2, colors.red, colors.black)
  256.         sleep(.1)
  257.         words("Begin", "c", h - 2, colors.white, colors.black)
  258.         sleep(.1)
  259.       end
  260.     end
  261.   end
  262. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement