Advertisement
Deriboy

mob_farm_interface

Feb 18th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.72 KB | None | 0 0
  1. local modem = peripheral.wrap("right")
  2. rednet.open("bottom")
  3. local speaker = peripheral.wrap("top")
  4. local monitor = peripheral.wrap("left")
  5.  
  6. local toControllerDir = "south" -- direction from cage to controller
  7. local toCageDir = "north" -- direction from controller to cage
  8.  
  9. monitor.setBackgroundColor(colors.black)
  10. monitor.clear()
  11. local LIST_MAX_HEIGHT = 24
  12. local LIST_MAX_WIDTH = 37
  13.  
  14. local WINDOW_HEIGHT = 26
  15. local WINDOW_WIDTH = 39
  16.  
  17. local list_length = 0
  18. local scroll_window = window.create(monitor,1,1,LIST_MAX_WIDTH,LIST_MAX_HEIGHT)
  19.  
  20. local selection = {}
  21. local old_selection = {}
  22. local selection_count = 0
  23. local old_selection_count = 0
  24. local mode = "Auto"
  25.  
  26. local raw_shard_list = {}
  27. local shard_list = {}  
  28. local ordered_shard_list = {}
  29. local list_pos = 1
  30.  
  31. local controllers = {[1] = "storagedrawers:controllerSlave_0",
  32.                      [2] = "storagedrawers:controllerSlave_1",
  33.                      [3] = "storagedrawers:controllerSlave_2",
  34.                      [4] = "storagedrawers:controllerSlave_3",
  35.                      [5] = "storagedrawers:controller_0",
  36.                      }
  37.  
  38. local cages = {[1] = "botania:redStringContainer_0",
  39.                [2] = "botania:redStringContainer_1",
  40.                [3] = "botania:redStringContainer_2",
  41.                [4] = "botania:redStringContainer_3",
  42.                [5] = "botania:redStringContainer_4",
  43.                }
  44.                
  45. local function broadcast_mode()
  46.   rednet.broadcast(mode,"mob_farm")
  47. end
  48.  
  49. function shallowcopy(orig)
  50.     local orig_type = type(orig)
  51.     local copy
  52.     if orig_type == 'table' then
  53.         copy = {}
  54.         for orig_key, orig_value in pairs(orig) do
  55.             copy[orig_key] = orig_value
  56.         end
  57.     else -- number, string, boolean, etc
  58.         copy = orig
  59.     end
  60.     return copy
  61. end
  62.  
  63. local function update_shards()
  64.   for i=1,#cages do
  65.     local cage = cages[i]
  66.     modem.callRemote(cage,"pushItems",toControllerDir,1)
  67.   end
  68.  
  69.   local controller_to_push_from = 1
  70.   for mob,num in pairs(selection) do  
  71.     for i=1, modem.callRemote("storagedrawers:controller_0","size") do
  72.       local stack = modem.callRemote("storagedrawers:controller_0","getItemMeta",i)
  73.       if stack and stack.displayName == mob then
  74.         for j=1, num do
  75.           if controller_to_push_from > #controllers then
  76.             error("not enough soul cages for this!")
  77.           end
  78.           modem.callRemote(controllers[controller_to_push_from],"pushItems",toCageDir,i,1)
  79.           controller_to_push_from = controller_to_push_from + 1
  80.         end
  81.       end
  82.     end
  83.    
  84.    
  85.   end
  86.  
  87. end
  88.  
  89.  
  90.  
  91. local function get_selected_number(x,y)
  92.   local num = x - 29
  93.   local item = list_pos + y - 1
  94.  
  95.   if num < 1 or num > 5 then
  96.     num = nil
  97.   end
  98.   if y > LIST_MAX_HEIGHT or y > list_length then
  99.     item = nil
  100.   end
  101.   return num,item
  102. end
  103.  
  104. local function draw_list(delta_d) -- -1: up, 0: stay, 1: down
  105.   local new_pos = list_pos + delta_d
  106.   if delta_d ~= 0 then
  107.     if new_pos < 1 or list_length - new_pos + 1 < LIST_MAX_HEIGHT then
  108.       print("out of bounds!")
  109.       os.queueEvent("sound","bass",10,2)
  110.     else
  111.       list_pos = new_pos
  112.       os.queueEvent("sound","hat",10,8)
  113.     end
  114.   end
  115.   scroll_window.clear()
  116.   local row = 1
  117.   for i=list_pos,math.min(#ordered_shard_list,LIST_MAX_HEIGHT + list_pos) do
  118.     local element = ordered_shard_list[i]
  119.     local selected_count = 0
  120.     selected_count = selection[element[1]]
  121.    
  122.     if selected_count ~= nil and selected_count > 0 then
  123.       scroll_window.setTextColor(colors.green)
  124.     else
  125.       scroll_window.setTextColor(colors.white)
  126.     end
  127.     scroll_window.setBackgroundColor(colors.black)
  128.     scroll_window.setCursorPos(1,row)
  129.     scroll_window.write(element[1])
  130.    
  131.     for c=1,element[2] do
  132.       scroll_window.setCursorPos(30+c-1,row)
  133.       if c == selected_count then
  134.         scroll_window.setTextColor(colors.green)
  135.       else
  136.         scroll_window.setTextColor(colors.white)
  137.       end
  138.       scroll_window.write(c)
  139.     end
  140.     row = row + 1
  141.   end
  142.  
  143.   monitor.setBackgroundColor(colors.black)
  144.   monitor.setTextColor(colors.white)
  145.   monitor.setCursorPos(WINDOW_WIDTH,1)
  146.   monitor.write("^")
  147.   monitor.setCursorPos(WINDOW_WIDTH,LIST_MAX_HEIGHT)
  148.   monitor.write("v")
  149.  
  150.   monitor.setCursorPos(1,WINDOW_HEIGHT)
  151.   monitor.setTextColor(colors.white)
  152.   monitor.setBackgroundColor(colors.red)
  153.   if mode == "Auto" then
  154.     monitor.setBackgroundColor(colors.green)
  155.   else
  156.     monitor.setBackgroundColor(colors.red)
  157.   end
  158.   monitor.write("Auto")
  159.  
  160.   monitor.setCursorPos(6,WINDOW_HEIGHT)
  161.   monitor.setTextColor(colors.white)
  162.   if mode == "Manual" then
  163.     monitor.setBackgroundColor(colors.green)
  164.   else
  165.     monitor.setBackgroundColor(colors.red)
  166.   end
  167.   monitor.write("Manual")
  168.  
  169.   monitor.setCursorPos(13,WINDOW_HEIGHT)
  170.   monitor.setTextColor(colors.white)
  171.   monitor.setBackgroundColor(colors.green)
  172.   if mode == "XP" then
  173.     monitor.setBackgroundColor(colors.green)
  174.   else
  175.     monitor.setBackgroundColor(colors.red)
  176.   end
  177.   monitor.write("XP")
  178.  
  179.   monitor.setCursorPos(17,WINDOW_HEIGHT)
  180.   monitor.setTextColor(colors.white)
  181.   monitor.setBackgroundColor(colors.yellow)
  182.   monitor.write("Cancel")
  183.  
  184.   monitor.setCursorPos(24,WINDOW_HEIGHT)
  185.   monitor.setTextColor(colors.white)
  186.   monitor.setBackgroundColor(colors.blue)
  187.   monitor.write("Execute")
  188.  
  189.   monitor.setCursorPos(32,WINDOW_HEIGHT)
  190.   monitor.setTextColor(colors.white)
  191.   monitor.setBackgroundColor(colors.red)
  192.   monitor.write("Clear")
  193.  
  194.  
  195. end
  196.  
  197. local function read_input()
  198.   local change = false
  199.  
  200.   while true do
  201.     local event,_,x,y = os.pullEvent("monitor_touch")
  202.    
  203.     if x == WINDOW_WIDTH and y == 1 then
  204.         os.queueEvent("update_display",-1)
  205.     elseif x == WINDOW_WIDTH and y == 24 then
  206.         os.queueEvent("update_display",1)
  207.     elseif x <= 4 and y == WINDOW_HEIGHT then
  208.       --auto
  209.       mode = "Auto"
  210.       broadcast_mode()
  211.       os.queueEvent("sound","hat",10,8)
  212.       os.queueEvent("update_display",0)
  213.     elseif x <= 11 and x >= 6 and  y == WINDOW_HEIGHT then
  214.       --manual
  215.       mode = "Manual"
  216.       broadcast_mode()
  217.       os.queueEvent("sound","hat",10,8)
  218.       os.queueEvent("update_display",0)
  219.     elseif x <= 14 and x >= 13 and y == WINDOW_HEIGHT then
  220.       --xp
  221.       mode = "XP"
  222.       broadcast_mode()
  223.       os.queueEvent("sound","hat",10,8)
  224.       os.queueEvent("update_display",0)
  225.     elseif x <= 22 and x >= 17 and y == WINDOW_HEIGHT then
  226.       --cancel
  227.       selection = shallowcopy(old_selection)
  228.       selection_count = old_selection_count
  229.       os.queueEvent("update_display",0)
  230.       os.queueEvent("sound","hat",10,8)
  231.     elseif x <= 30 and x >= 24 and y == WINDOW_HEIGHT then
  232.       --execute
  233.       old_selection = shallowcopy(selection)  
  234.       old_selection_count = selection_count
  235.       os.queueEvent("update_display",0)
  236.       os.queueEvent("sound","hat",10,8)
  237.       os.queueEvent("update_shards","go")
  238.     elseif x <= 36 and x >= 32 and y == WINDOW_HEIGHT then
  239.       --clear
  240.       selection = {}  
  241.       selection_count = 0
  242.       os.queueEvent("update_display",0)
  243.       os.queueEvent("sound","hat",10,8)
  244.     else
  245.       num,item = get_selected_number(x,y)
  246.       local prev_count = 0
  247.       if num and item then
  248.         if selection[ordered_shard_list[item][1]] then
  249.           prev_count = selection[ordered_shard_list[item][1]]
  250.         end
  251.         if num == prev_count then
  252.           selection[ordered_shard_list[item][1]] = 0
  253.           selection_count  = selection_count - prev_count
  254.           os.queueEvent("update_display",0)
  255.           os.queueEvent("sound","hat",10,8)
  256.         elseif selection_count + num <= 5 + prev_count and num <= ordered_shard_list[item][2] then
  257.           selection[ordered_shard_list[item][1]] = num
  258.           selection_count  = selection_count + num - prev_count
  259.           os.queueEvent("update_display",0)
  260.           os.queueEvent("sound","hat",10,8)
  261.         else
  262.           os.queueEvent("sound","bass",10,2)
  263.         end
  264.       elseif item then
  265.         local current_num = selection[ordered_shard_list[item][1]]
  266.         if not current_num or current_num == 0 then
  267.           selection[ordered_shard_list[item][1]] = 1
  268.           selection_count = selection_count + 1
  269.           os.queueEvent("update_display",0)
  270.           os.queueEvent("sound","hat",10,8)
  271.         else
  272.           selection[ordered_shard_list[item][1]] = 0
  273.           selection_count = selection_count - current_num
  274.           os.queueEvent("update_display",0)
  275.           os.queueEvent("sound","hat",10,8)
  276.         end
  277.      
  278.       end
  279.      
  280.     end
  281.  
  282.   end
  283. end
  284.  
  285. local function display_controller()
  286.   draw_list(0)
  287.   while true do
  288.     local event,dir = os.pullEvent("update_display")
  289.     draw_list(dir)
  290.    
  291.   end
  292. end
  293.  
  294. local function shard_controller()
  295.   update_shards()
  296.   while true do
  297.     os.pullEvent("update_shards")
  298.     update_shards()
  299.   end
  300. end
  301.  
  302. local function sound_engine()
  303.   while true do
  304.     local event,instrument,volume,pitch = os.pullEvent("sound")
  305.     speaker.playNote(instrument,volume,pitch)
  306.   end
  307. end
  308.  
  309. broadcast_mode()
  310. update_shards()
  311. raw_shard_list = modem.callRemote("storagedrawers:controller_0","list")
  312. for k,v in pairs(raw_shard_list) do
  313.   local stack = modem.callRemote("storagedrawers:controller_0","getItemMeta",k)
  314.   local name = stack.displayName
  315.   shard_list[name] = stack.count
  316.   list_length = list_length + 1
  317. end
  318.  
  319.  
  320. for k,v in pairs(shard_list) do
  321.   table.insert(ordered_shard_list,{k,v})
  322. end
  323. table.sort(ordered_shard_list,function (a,b) return a[1] < b[1] end)
  324.  
  325.  
  326. local success,err = pcall(
  327.   parallel.waitForAny(
  328.     read_input,
  329.     sound_engine,
  330.     display_controller,
  331.     shard_controller
  332.   )
  333. )
  334. if err == "Terminated" then
  335.   print("Terminated")
  336. else
  337.   print(err)
  338. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement