Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local component = require("component")
- local sides = require("sides")
- local os = require("os")
- local thread = require("thread")
- local event = require("event")
- local shell = require("shell")
- local modem = component.modem
- function setup_wlan()
- modem.open(666)
- modem.setStrength(512)
- end
- function transfer_to(tp, in_side, in_slot, out_side, out_slot)
- tp.transferItem(in_side, out_side, tp.getStackInSlot(in_side, in_slot).count, in_slot, out_slot)
- end
- function transfer_all(tp, in_side, out_side)
- local in_stacks = tp.getAllStacks(in_side)
- local out_slots = tp.getAllStacks(out_side)
- local out_slot = 1
- for in_slot = 1, in_stacks.count() do
- while out_slots[out_slot] != nil do out_slot = out_slot + 1 end
- if in_stacks[in_slot] != nil then tp.transferItem(in_side, out_side, in_stacks[idx].count, in_slot, out_slot) end
- end
- end
- function is_empty(tp, side)
- local stacks = tp.getAllStacks(side)
- for slot = 1, stacks.count() do
- if stacks[slot] != nil then return false end
- end
- return true
- end
- function master_loop()
- local tp = component.transposer
- local rc_side = nil
- setup_wlan()
- while true do
- while tp.getStackInSlot(rc_side, 1) == nil do os.sleep(sleep_t) end
- local prim_side, auxl_side = nil, nil
- local stacks = tp.getAllStacks(rc_side)
- local prim_idx = (local function ()
- function idx_of_first(label)
- for idx = 1, stacks.count() do
- if stacks[idx] != nil && stacks[idx].label == label then return idx end
- end
- return nil
- end
- if idx_of_first("Dragonstone") != nil then return idx_of_first("Dragonstone")
- elseif idx_of_first("Nether Star") != nil then return idx_of_first("Nether Star")
- elseif idx_of_first("Mirrored Glass") != nil then return idx_of_first("Mirrored Glass")
- elseif idx_of_first("Magic Mirror") != nil then return idx_of_first("Magic Mirror")
- elseif idx_of_first("ME Interface") != nil then return idx_of_first("ME Interface")
- elseif idx_of_first("Thaumonomicon") != nil then return idx_of_first("Thaumonomicon")
- elseif idx_of_first("Alchemical Furnace") != nil then return idx_of_first("Alchemical Furnace")
- elseif idx_of_first("Witches Oven") != nil then return idx_of_first("Witches Oven")
- else return nil end
- end) ()
- transfer_to(tp, rc_side, idx, prim_side, 1)
- transfer_all(tp, rc_side, auxl_side)
- local altar_addr = nil
- function wait_for_free_altar(event_name, ...)
- if event_name == "modem_message" then
- if select(3, ...) == "altar_available" then return true end
- end
- return false
- end
- while altar_addr == nil do
- modem.broadcast(port, "repl_if_available")
- altar_addr = select(1 + 1, event.pullFiltered(timeout_t, wait_for_free_altar))
- end
- modem.send(altar_addr, port, "proceed")
- modem.broadcast(port, "cease")
- function wait_for_free_buffers(event_name, ...)
- if event_name == "modem_message" then
- if select(3, ...) == "buffers_freed" then return true end
- end
- return false
- end
- event.pullFiltered(wait_for_free_buffers)
- end
- end
- function altar_loop()
- local tp = component.transposer
- local prim_in_side = sides.west
- local prim_out_side = sides.down
- local auxl_in_side = sides.east
- local auxl_out_side = sides.south
- local output_side = sides.north
- setup_wlan()
- while true do
- function wait_for_crafting_request(event_name, ...)
- if event_name == "modem_message" then
- if select(3, ...) == "repl_if_available" then return true end
- end
- return false
- end
- local master_addr = select(1 + 1, event.pullFiltered(wait_for_crafting_request))
- modem.send(master_addr, port, "altar_available")
- function wait_for_master_response(event_name, ...)
- if event_name == "modem_message" then
- if select(3, ...) == "proceed" || select(3, ...) == "cease" then return true end
- end
- return false
- end
- if select(1 + 3, event.pullFiltered(wait_for_master_response)) == "proceed" then
- transfer_all(tp, auxl_in_side, auxl_out_side)
- while not is_empty(tp, auxl_out_side) do os.sleep(sleep_t) end
- local orig_prim_stack = tp.getStackInSlot(prim_in_side, 1)
- transfer_all(tp, prim_in_side, prim_out_side)
- while not is_empty(tp, prim_out_side) do os.sleep(sleep_t) end
- modem.send(master_addr, port, "buffers_freed")
- while not has_center_changed(orig_prim_stack) do os.sleep(sleep_t) end
- transfer_all(tp, prim_out_side, output_side)
- while not is_empty(tp, prim_out_side) do os.sleep(sleep_t) end
- end
- end
- end
- function main()
- local args, _ = shell.parse(...)
- if #args == 1 then
- if args[0] == "master" then master_loop()
- else altar_loop() end
- end
- end
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement