Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local modem = peripheral.wrap("right")
- rednet.open("bottom")
- local speaker = peripheral.wrap("top")
- local monitor = peripheral.wrap("left")
- local toControllerDir = "south" -- direction from cage to controller
- local toCageDir = "north" -- direction from controller to cage
- monitor.setBackgroundColor(colors.black)
- monitor.clear()
- local LIST_MAX_HEIGHT = 24
- local LIST_MAX_WIDTH = 37
- local WINDOW_HEIGHT = 26
- local WINDOW_WIDTH = 39
- local list_length = 0
- local scroll_window = window.create(monitor,1,1,LIST_MAX_WIDTH,LIST_MAX_HEIGHT)
- local selection = {}
- local old_selection = {}
- local selection_count = 0
- local old_selection_count = 0
- local mode = "Auto"
- local raw_shard_list = {}
- local shard_list = {}
- local ordered_shard_list = {}
- local list_pos = 1
- local controllers = {[1] = "storagedrawers:controllerSlave_0",
- [2] = "storagedrawers:controllerSlave_1",
- [3] = "storagedrawers:controllerSlave_2",
- [4] = "storagedrawers:controllerSlave_3",
- [5] = "storagedrawers:controller_0",
- }
- local cages = {[1] = "botania:redStringContainer_0",
- [2] = "botania:redStringContainer_1",
- [3] = "botania:redStringContainer_2",
- [4] = "botania:redStringContainer_3",
- [5] = "botania:redStringContainer_4",
- }
- local function broadcast_mode()
- rednet.broadcast(mode,"mob_farm")
- end
- function shallowcopy(orig)
- local orig_type = type(orig)
- local copy
- if orig_type == 'table' then
- copy = {}
- for orig_key, orig_value in pairs(orig) do
- copy[orig_key] = orig_value
- end
- else -- number, string, boolean, etc
- copy = orig
- end
- return copy
- end
- local function update_shards()
- for i=1,#cages do
- local cage = cages[i]
- modem.callRemote(cage,"pushItems",toControllerDir,1)
- end
- local controller_to_push_from = 1
- for mob,num in pairs(selection) do
- for i=1, modem.callRemote("storagedrawers:controller_0","size") do
- local stack = modem.callRemote("storagedrawers:controller_0","getItemMeta",i)
- if stack and stack.displayName == mob then
- for j=1, num do
- if controller_to_push_from > #controllers then
- error("not enough soul cages for this!")
- end
- modem.callRemote(controllers[controller_to_push_from],"pushItems",toCageDir,i,1)
- controller_to_push_from = controller_to_push_from + 1
- end
- end
- end
- end
- end
- local function get_selected_number(x,y)
- local num = x - 29
- local item = list_pos + y - 1
- if num < 1 or num > 5 then
- num = nil
- end
- if y > LIST_MAX_HEIGHT or y > list_length then
- item = nil
- end
- return num,item
- end
- local function draw_list(delta_d) -- -1: up, 0: stay, 1: down
- local new_pos = list_pos + delta_d
- if delta_d ~= 0 then
- if new_pos < 1 or list_length - new_pos + 1 < LIST_MAX_HEIGHT then
- print("out of bounds!")
- os.queueEvent("sound","bass",10,2)
- else
- list_pos = new_pos
- os.queueEvent("sound","hat",10,8)
- end
- end
- scroll_window.clear()
- local row = 1
- for i=list_pos,math.min(#ordered_shard_list,LIST_MAX_HEIGHT + list_pos) do
- local element = ordered_shard_list[i]
- local selected_count = 0
- selected_count = selection[element[1]]
- if selected_count ~= nil and selected_count > 0 then
- scroll_window.setTextColor(colors.green)
- else
- scroll_window.setTextColor(colors.white)
- end
- scroll_window.setBackgroundColor(colors.black)
- scroll_window.setCursorPos(1,row)
- scroll_window.write(element[1])
- for c=1,element[2] do
- scroll_window.setCursorPos(30+c-1,row)
- if c == selected_count then
- scroll_window.setTextColor(colors.green)
- else
- scroll_window.setTextColor(colors.white)
- end
- scroll_window.write(c)
- end
- row = row + 1
- end
- monitor.setBackgroundColor(colors.black)
- monitor.setTextColor(colors.white)
- monitor.setCursorPos(WINDOW_WIDTH,1)
- monitor.write("^")
- monitor.setCursorPos(WINDOW_WIDTH,LIST_MAX_HEIGHT)
- monitor.write("v")
- monitor.setCursorPos(1,WINDOW_HEIGHT)
- monitor.setTextColor(colors.white)
- monitor.setBackgroundColor(colors.red)
- if mode == "Auto" then
- monitor.setBackgroundColor(colors.green)
- else
- monitor.setBackgroundColor(colors.red)
- end
- monitor.write("Auto")
- monitor.setCursorPos(6,WINDOW_HEIGHT)
- monitor.setTextColor(colors.white)
- if mode == "Manual" then
- monitor.setBackgroundColor(colors.green)
- else
- monitor.setBackgroundColor(colors.red)
- end
- monitor.write("Manual")
- monitor.setCursorPos(13,WINDOW_HEIGHT)
- monitor.setTextColor(colors.white)
- monitor.setBackgroundColor(colors.green)
- if mode == "XP" then
- monitor.setBackgroundColor(colors.green)
- else
- monitor.setBackgroundColor(colors.red)
- end
- monitor.write("XP")
- monitor.setCursorPos(17,WINDOW_HEIGHT)
- monitor.setTextColor(colors.white)
- monitor.setBackgroundColor(colors.yellow)
- monitor.write("Cancel")
- monitor.setCursorPos(24,WINDOW_HEIGHT)
- monitor.setTextColor(colors.white)
- monitor.setBackgroundColor(colors.blue)
- monitor.write("Execute")
- monitor.setCursorPos(32,WINDOW_HEIGHT)
- monitor.setTextColor(colors.white)
- monitor.setBackgroundColor(colors.red)
- monitor.write("Clear")
- end
- local function read_input()
- local change = false
- while true do
- local event,_,x,y = os.pullEvent("monitor_touch")
- if x == WINDOW_WIDTH and y == 1 then
- os.queueEvent("update_display",-1)
- elseif x == WINDOW_WIDTH and y == 24 then
- os.queueEvent("update_display",1)
- elseif x <= 4 and y == WINDOW_HEIGHT then
- --auto
- mode = "Auto"
- broadcast_mode()
- os.queueEvent("sound","hat",10,8)
- os.queueEvent("update_display",0)
- elseif x <= 11 and x >= 6 and y == WINDOW_HEIGHT then
- --manual
- mode = "Manual"
- broadcast_mode()
- os.queueEvent("sound","hat",10,8)
- os.queueEvent("update_display",0)
- elseif x <= 14 and x >= 13 and y == WINDOW_HEIGHT then
- --xp
- mode = "XP"
- broadcast_mode()
- os.queueEvent("sound","hat",10,8)
- os.queueEvent("update_display",0)
- elseif x <= 22 and x >= 17 and y == WINDOW_HEIGHT then
- --cancel
- selection = shallowcopy(old_selection)
- selection_count = old_selection_count
- os.queueEvent("update_display",0)
- os.queueEvent("sound","hat",10,8)
- elseif x <= 30 and x >= 24 and y == WINDOW_HEIGHT then
- --execute
- old_selection = shallowcopy(selection)
- old_selection_count = selection_count
- os.queueEvent("update_display",0)
- os.queueEvent("sound","hat",10,8)
- os.queueEvent("update_shards","go")
- elseif x <= 36 and x >= 32 and y == WINDOW_HEIGHT then
- --clear
- selection = {}
- selection_count = 0
- os.queueEvent("update_display",0)
- os.queueEvent("sound","hat",10,8)
- else
- num,item = get_selected_number(x,y)
- local prev_count = 0
- if num and item then
- if selection[ordered_shard_list[item][1]] then
- prev_count = selection[ordered_shard_list[item][1]]
- end
- if num == prev_count then
- selection[ordered_shard_list[item][1]] = 0
- selection_count = selection_count - prev_count
- os.queueEvent("update_display",0)
- os.queueEvent("sound","hat",10,8)
- elseif selection_count + num <= 5 + prev_count and num <= ordered_shard_list[item][2] then
- selection[ordered_shard_list[item][1]] = num
- selection_count = selection_count + num - prev_count
- os.queueEvent("update_display",0)
- os.queueEvent("sound","hat",10,8)
- else
- os.queueEvent("sound","bass",10,2)
- end
- elseif item then
- local current_num = selection[ordered_shard_list[item][1]]
- if not current_num or current_num == 0 then
- selection[ordered_shard_list[item][1]] = 1
- selection_count = selection_count + 1
- os.queueEvent("update_display",0)
- os.queueEvent("sound","hat",10,8)
- else
- selection[ordered_shard_list[item][1]] = 0
- selection_count = selection_count - current_num
- os.queueEvent("update_display",0)
- os.queueEvent("sound","hat",10,8)
- end
- end
- end
- end
- end
- local function display_controller()
- draw_list(0)
- while true do
- local event,dir = os.pullEvent("update_display")
- draw_list(dir)
- end
- end
- local function shard_controller()
- update_shards()
- while true do
- os.pullEvent("update_shards")
- update_shards()
- end
- end
- local function sound_engine()
- while true do
- local event,instrument,volume,pitch = os.pullEvent("sound")
- speaker.playNote(instrument,volume,pitch)
- end
- end
- broadcast_mode()
- update_shards()
- raw_shard_list = modem.callRemote("storagedrawers:controller_0","list")
- for k,v in pairs(raw_shard_list) do
- local stack = modem.callRemote("storagedrawers:controller_0","getItemMeta",k)
- local name = stack.displayName
- shard_list[name] = stack.count
- list_length = list_length + 1
- end
- for k,v in pairs(shard_list) do
- table.insert(ordered_shard_list,{k,v})
- end
- table.sort(ordered_shard_list,function (a,b) return a[1] < b[1] end)
- local success,err = pcall(
- parallel.waitForAny(
- read_input,
- sound_engine,
- display_controller,
- shard_controller
- )
- )
- if err == "Terminated" then
- print("Terminated")
- else
- print(err)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement