Advertisement
UNOBTANIUM

Next Generation Diamond Pipe

Jun 22nd, 2013
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.60 KB | None | 0 0
  1. local item = {}
  2. local sorter
  3. local unknownAmount = 0
  4. local sorterSide = "down"
  5. local chestSide = "north"
  6. local computerDirection = "north"
  7. local unknownDirection = "north"
  8. local direction = {["down"]=0,["up"]=1,["north"]=2,["south"]=3,["west"]=4,["east"]=5}
  9. local antiSide = {["down"]="up",["up"]="down",["north"]="south",["south"]="north",["west"]="east",["east"]="west"}
  10.  
  11. function clear()
  12.  term.clear()
  13.  term.setCursorPos(1,1)
  14. end
  15.  
  16. function saveVar()
  17.  local file = fs.open("SorterVar","w")
  18.   file.writeLine(unknownAmount)
  19.   file.writeLine(sorterSide)
  20.   file.writeLine(chestSide)
  21.   file.writeLine(computerDirection)
  22.   file.writeLine(unknownDirection)
  23.   file.writeLine(countArray(item))
  24.   for k,v in pairs(item) do
  25.    file.writeLine(k)
  26.    for i=1,3 do
  27.     file.writeLine(item[k][i])
  28.    end
  29.   end
  30.  file.close()
  31. end
  32.  
  33. function loadVar()
  34.  if not fs.exists("SorterVar") then return false end
  35.  local file = fs.open("SorterVar","r")
  36.   unknownAmount = tonumber(file.readLine())
  37.   sorterSide = file.readLine()
  38.   chestSide = file.readLine()
  39.   computerDirection = file.readLine()
  40.   unknownDirection = file.readLine()
  41.   local amount = file.readLine()
  42.   for i=1,amount do
  43.    local k = file.readLine()
  44.    local a = file.readLine()
  45.    local b = file.readLine()
  46.    local c = tonumber(file.readLine())
  47.    item[k] = {a,b,c}
  48.   end
  49.  file.close()
  50.  return true
  51. end
  52.  
  53. function addItem()
  54.  clear()
  55.  centered("NEW ITEM",1)
  56.  centered("Tipe in the id plus metadata of the item", 4)
  57.  centered("Example: " .. math.ceil(math.random()*3000) .. ":" .. math.ceil(math.random()*100) .. "  or  " .. math.ceil(math.random()*3000), 6)
  58.  left("",20,8)
  59.  local newId = read()
  60.  clear()
  61.  centered("NEW ITEM",1)
  62.  centered("If you want to you can name it here.",4)
  63.  centered("This name will be shown in your list.",5)
  64.  centered("By just hitting Enter",6)
  65.  centered("you just use the id as name",7)
  66.  left("",15,9)
  67.  local newName = read()
  68.  if newName == "" then
  69.   newName = newId
  70.  end
  71.  clear()
  72.  centered("NEW ITEM",1)
  73.  centered("In which direction should this item be send?",4)
  74.  local newDirection = getDir()
  75.  clear()
  76.  centered("FINAL",1)
  77.  centered("Do you want to add this item?",4)
  78.  centered("Y/N",5)
  79.  centered(("ID: " .. newId) ,8)
  80.  centered(("Name: " .. newName) ,9)
  81.  centered(("Direction: " .. newDirection) ,10)
  82.  while true do
  83.   local event = {os.pullEvent("char")}
  84.   if event[2] == "y" then
  85.    item[newId]={newName, newDirection, 0}
  86.    saveVar()
  87.    return true
  88.   elseif event[2] == "n" then
  89.    return false
  90.   end
  91.  end
  92. end
  93.  
  94. function getRelDir()
  95.  left("1 - Behind",20,10)
  96.  left("2 - Right",20,11)
  97.  left("3 - Front",20,12)
  98.  left("4 - Left",20,13)
  99.  while true do
  100.   local event = {os.pullEvent("char")}
  101.   local input = tonumber(event[2])
  102.   if type(input) == "number" and input >= 1 and input <= 6 then
  103.   if input == 1 then
  104.     return "back"
  105.    elseif input == 2 then
  106.     return "right"
  107.    elseif input == 3 then
  108.     return "front"
  109.    elseif input == 4 then
  110.     return "left"
  111.    end
  112.   end
  113.  end
  114. end
  115.  
  116. function getDir()
  117.  left("1 - Below",20,8)
  118.  left("2 - Above",20,9)
  119.  left("3 - Behind",20,10)
  120.  left("4 - Right",20,11)
  121.  left("5 - Front",20,12)
  122.  left("6 - Left",20,13)
  123.  while true do
  124.   local event = {os.pullEvent("char")}
  125.   local input = tonumber(event[2])
  126.   if type(input) == "number" and input >= 1 and input <= 6 then
  127.    if input == 1 then
  128.     return "down"
  129.    elseif input == 2 then
  130.     return "up"
  131.    elseif input == 3 then
  132.     return antiSide[computerDirection]
  133.    elseif input == 4 then
  134.     if computerDirection == "north" then
  135.      return "west"
  136.     elseif computerDirection == "east" then
  137.      return "north"
  138.     elseif computerDirection == "south" then
  139.      return "east"
  140.     elseif computerDirection == "west" then
  141.      return "south"
  142.     end
  143.    elseif input == 5 then
  144.     return computerDirection
  145.    elseif input == 6 then
  146.     if computerDirection == "north" then
  147.      return "east"
  148.     elseif computerDirection == "east" then
  149.      return "south"
  150.     elseif computerDirection == "south" then
  151.      return "west"
  152.     elseif computerDirection == "west" then
  153.      return "north"
  154.     end
  155.    end
  156.   end
  157.  end
  158. end
  159.  
  160. function setSides()
  161.  centered("COMPUTER FACING",1)
  162.  centered("In which direction is the computer facing?",4)
  163.  centered("Look in the direction the screen",5)
  164.  centered("of the computer is facing,",6)
  165.  centered("hit F3 and search for the direction name.",7)
  166.  left("1 - North",20,9)
  167.  left("2 - East",20,10)
  168.  left("3 - South",20,11)
  169.  left("4 - West",20,12)
  170.  while true do
  171.   local event = {os.pullEvent("char")}
  172.   local input = tonumber(event[2])
  173.   if type(input == "number") and input >= 1 and input <= 4 then
  174.    if input == 1 then
  175.     computerDirection = "north"
  176.    elseif input == 2 then
  177.     computerDirection = "east"
  178.    elseif input == 3 then
  179.     computerDirection = "south"
  180.    elseif input == 4 then
  181.     computerDirection = "west"
  182.    end
  183.    break
  184.   end
  185.  end
  186.  clear()
  187.  sleep(0.1)
  188.  centered("SORTER SIDE",1)
  189.  centered("On which side of the computer does the",4)
  190.  centered("interactive sorter take place?",5)
  191.  sorterSide = getRelDir()
  192.  clear()
  193.  sleep(0.1)
  194.  centered("CHEST SIDE",1)
  195.  centered("Imagine the interactive sorter is facing",4)
  196.  centered("the same direction as the computer does.",5)
  197.  centered("From which side should the interactive sorter",6)
  198.  centered("take items out of the chest?",7)
  199.  chestSide = getDir()
  200.  clear()
  201.  sleep(0.1)
  202.  centered("UNKNOWN ITEM SIDE",1)
  203.  centered("Where should all items go,",3)
  204.  centered("which aren't listed by you?",4)
  205.  centered("Once again, imagine the interactive sorter is",5)
  206.  centered("facing in the same direction",6)
  207.  centered("as the computer does!",7)
  208.  unknownDirection = getDir()
  209.  saveVar()
  210.  clear()
  211. end
  212.  
  213. function getID(uuid)
  214.  if uuid > 32768 then
  215.   meta = math.floor(uuid/32768)
  216.   id = uuid%32768
  217.   return ( id .. ":" .. meta )
  218.  else
  219.   return tostring(uuid)
  220.  end
  221. end
  222.  
  223. function countArray(a)
  224.  local amount = 0
  225.  for k,v in pairs(a) do
  226.   amount = amount + 1
  227.  end
  228.  return amount
  229. end
  230.  
  231. function centered(text,y,x)
  232.  x = x or 25
  233.  term.setCursorPos(x - #text/2, y)
  234.  term.write(text)
  235. end
  236.  
  237. function left(text,x,y)
  238.  term.setCursorPos(x,y)
  239.  term.write(text)
  240. end
  241.  
  242. -- CHANGE ITEM
  243.  
  244. function changeItem(choosenId)
  245.  clear()
  246.  centered("CHANGE ITEM",1)
  247.  centered(("ID: " .. choosenId) ,4)
  248.  centered(("Name: " .. item[choosenId][1]) ,5)
  249.  centered(("Direction: " .. item[choosenId][2]) ,6)
  250.  centered(("Amount: " .. item[choosenId][3] ),7)
  251.  centered("How should this item be named?",9)
  252.  centered("",10)
  253.  local newName = read()
  254.  clear()
  255.  centered("CHANGE ITEM",1)
  256.  centered(("ID: " .. choosenId) ,3)
  257.  centered(("Name: " .. newName) ,4)
  258.  centered(("Direction: " .. item[choosenId][2]) ,5)
  259.  centered(("Amount: " .. item[choosenId][3] ),6)
  260.  centered("Which side should the item be send?",7)
  261.  local newDir = getDir()
  262.  clear()
  263.  centered("CHANGE ITEM",1)
  264.  centered("Do you want to make this change?",4)
  265.  centered("Y/N",5)
  266.  while true do
  267.   local event = {os.pullEvent("char")}
  268.   if event[2] == "y" then
  269.    item[choosenId]={newName, newDir, item[choosenId][3]}
  270.    saveVar()
  271.    return
  272.   elseif event[2] == "n" then
  273.    return
  274.   end
  275.  end
  276. end
  277.  
  278. -- OVERVIEW
  279.  
  280. function overview()
  281.  clear()
  282.  local mode = "CHANGE"
  283.  local more = 0
  284.  while true do
  285.   clear()
  286.   local amountItems = countArray(item)
  287.   centered("OVERVIEW ITEMS " .. more+1 .. " TO " .. more+7,1)
  288.   local counter = 1-more
  289.   if more == 0 then
  290.    centered("Not listed items: " .. unknownAmount, 3)
  291.   end
  292.   for k,v in pairs(item) do
  293.    if counter >= 1 and counter <= 7 then
  294.     left( counter .. " - " .. item[k][1] ,5,counter+3)
  295.     left(item[k][2] ,36,counter+3)
  296.     left(tostring(item[k][3]),43,counter+3)
  297.    end
  298.    counter = counter + 1
  299.   end
  300.   left("8 - Mode: " .. mode ,15,15)
  301.   if more > 0 and more+7 >= amountItems then
  302.    left("9 - Back to the first items",15,16)
  303.   elseif more+7 < amountItems then
  304.    left("9 - More items",15,16)
  305.   end
  306.   left("0 - Back to the menu",15,17)
  307.   local event = {os.pullEvent("char")}
  308.   local input = tonumber(event[2])
  309.   if type(input) == "number" then
  310.    if input == 8 then
  311.     if mode == "CHANGE" then
  312.      mode = "DELETE"
  313.     elseif mode == "DELETE" then
  314.      mode = "CHANGE"
  315.     end
  316.    elseif input == 9 then
  317.     if more > 0 and  more+7 >= amountItems then
  318.      more = 0
  319.     elseif more+7 < amountItems then
  320.      more = more + 7
  321.     end
  322.    elseif input == 0 then
  323.     return
  324.    elseif input >= 1 and input <= 7 then
  325.     if more+input <= amountItems then
  326.      local choosenId = ""
  327.      local anotherCounter = 1
  328.      for k,v in pairs(item) do
  329.       if anotherCounter == more+input then
  330.        choosenId = k
  331.        break
  332.       end
  333.       anotherCounter = anotherCounter + 1
  334.      end
  335.      if mode == "CHANGE" then
  336.       changeItem(choosenId)
  337.      elseif mode == "DELETE" then
  338.       item[choosenId] = nil
  339.       saveVar()
  340.      end
  341.     end
  342.    end
  343.   end
  344.  end
  345. end
  346.  
  347. -- MENU
  348.  
  349. function menu()
  350.  while true do
  351.   clear()
  352.   centered("NEXT GENERATION DIAMOND PIPE",1)
  353.   left("1 - Start",10,4)
  354.   left("2 - Overview",10,5)
  355.   left("3 - Add specific item",10,6)
  356.   left("4 - Change settings",10,7)
  357.   left("by UNOBTANIUM",37,19)
  358.   local event = {os.pullEvent("char")}
  359.   if event[2] == "1" then
  360.    return
  361.   elseif event[2] == "2" then
  362.    overview()
  363.   elseif event[2] == "3" then
  364.    addItem()
  365.   elseif event[2] == "4" then
  366.    setVar()
  367.   end
  368.  end
  369. end
  370.  
  371. -- RUN
  372.  
  373. function run()
  374.  local inventory = sorter.list(direction[chestSide])
  375.  if countArray(inventory) == 0 then
  376.   return false
  377.  end
  378.  local chest = {}
  379.  for k,v in pairs(inventory) do
  380.   if type(chest[k]) == "nil" then
  381.    chest[k] = v
  382.   else
  383.    chest[k] = chest[k] + v
  384.   end
  385.  end
  386.  for k,v in pairs(chest) do
  387.   local itemID = getID(k)
  388.   if type(item[itemID]) == "table" then
  389.    sorter.extract(direction[chestSide],k,direction[item[getID(k)][2]],v)
  390.    item[itemID] = {item[itemID][1],item[itemID][2],item[itemID][3]+v}
  391.   else
  392.    sorter.extract(direction[chestSide],k,direction[unknownDirection],v)
  393.    unknownAmount = unknownAmount + v
  394.   end
  395.  end
  396.  saveVar()
  397.  return true
  398. end
  399.  
  400.  
  401. clear()
  402. if not loadVar() then
  403.  setSides()
  404. end
  405. sorter = peripheral.wrap(sorterSide)
  406. clear()
  407. centered("NEXT GENERATION DIAMOND PIPE",1)
  408. left("1 - Start",10,4)
  409. left("2 - Overview",10,5)
  410. left("3 - Add specific item",10,6)
  411. left("4 - Change settings",10,7)
  412. centered("RUNNING",9)
  413. left("by UNOBTANIUM",37,19)
  414. while true do
  415.  if run() then
  416.   os.startTimer(1.5)
  417.  else
  418.   os.startTimer(5)
  419.  end
  420.  while true do
  421.   local event = {os.pullEvent()}
  422.   if event[1] == "timer" then
  423.    break
  424.   elseif event[1] == "key" then
  425.    menu()
  426.    centered("RUNNING",9)
  427.    break
  428.   end
  429.  end
  430. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement