commit 1b4f601e9fa98b5c500fd9ff75e0a1247e4864ce Author: Benedikt Ziemons Date: Sat Jun 1 01:12:12 2024 +0200 first commit diff --git a/permset.lua b/permset.lua new file mode 100644 index 0000000..f1b5fb0 --- /dev/null +++ b/permset.lua @@ -0,0 +1,44 @@ +local function new(file) + local set = {} + local fh = fs.open(file, "r") + if fh == nil then + -- no such file, return empty set + return {inner=set, file=file} + end + local line = fh.readLine() + while line ~= nil do + if line then + set[line] = true + end + line = fh.readLine() + end + fh.close() + return {inner=set, file=file} +end + +local function writeSet(set) + local fh = fs.open(set.file, "w") + for it, allowed in pairs(set.inner) do + if allowed then + fh.writeLine(it) + end + end + fh.flush() + fh.close() +end + +local function add(set, item) + set.inner[item] = true + writeSet(set) +end + +local function remove(set, item) + set.inner[item] = false + writeSet(set) +end + +local function has(set, item) + return set.inner[item] == true +end + +return {new=new, writeSet=writeSet, add=add, remove=remove, has=has} diff --git a/test.lua b/test.lua new file mode 100644 index 0000000..aa4eb72 --- /dev/null +++ b/test.lua @@ -0,0 +1,283 @@ +-- options -- +local has_me = true +local main_system_ae_bridge_side = "right" +local main_me_inventory_side = "up" +local colony_interface_side = "left" +local monitor_side = "top" +local monitor_scale = 1.0 +local step_sleep_time = 0.5 +local round_sleep_time = 60.0 +local current_tab = 1 + + +-- runtime variables -- +local pretty = require "cc.pretty" +local permset = require "permset" + +local main_me = {} +if has_me then + main_me = peripheral.wrap(main_system_ae_bridge_side) +end +local colony = peripheral.wrap(colony_interface_side) + +local requests = {} +local orders = {} + +-- to match click positions back to item names +local display_list = {} +-- list of allowed item names, will be requested +local allowed_items = permset.new(".allowed_items") +-- display working state +local currently_working = true +-- current start index to view requests +local page_start_idx = 1 +-- schedule immediate reload +local immediate_reload = false + + +function requestsDisplay() + for i, val in pairs(requests) do + local line_num = i-page_start_idx+3 + if line_num > 2 and line_num < th then + term.setTextColor(colors.white) + term.setCursorPos(1,line_num) + term.write(val.req.name) + term.write(" ") + if val.answer == nil then + term.setTextColor(colors.orange) + term.write("N/A") + else + if allowed_items[val.answer.name] then + term.setTextColor(colors.green) + else + term.setTextColor(colors.yellow) + end + term.write(val.answer.name) + display_list[line_num] = val.answer.name + term.setTextColor(colors.white) + term.setCursorPos(tw-4,line_num) + term.write(" [") + if allowed_items[val.answer.name] then + term.setTextColor(colors.green) + term.write("X") + term.setTextColor(colors.white) + else + term.write(" ") + end + term.write("] ") + end + end + end +end + +function ordersDisplay() + for i, val in pairs(orders) do + local line_num = (i-page_start_idx)*2+3 + if line_num > 2 and line_num < th-1 then + term.setTextColor(colors.lightGray) + term.setCursorPos(1,line_num) + term.write(val.id) + term.write(" ") + if val.pos then + term.write(val.pos.x .. "," .. val.pos.y .. "," .. val.pos.z) + term.write(" ") + end + term.setCursorPos(2,line_num+1) + term.setTextColor(colors.white) + if val.type then + term.write(val.type) + term.write(" ") + end + if val.target then + term.write(val.target) + term.write(" ") + end + if val.item then + term.setCursorPos(tw-6-#val.item, line_num) + term.write(val.item) + else + term.setCursorPos(tw-6, line_num) + end + term.write(" ") + if val.claimed then + term.setTextColor(colors.yellow) + term.write("C") + term.setTextColor(colors.white) + else + term.write(" ") + end + term.write(" ") + if val.delivering then + term.setTextColor(colors.green) + term.write("D") + term.setTextColor(colors.white) + else + term.write(" ") + end + term.write(" ") + end + end +end + +local tabs = { + -- to display requests and answer with an ME interface + [1]={name="requests", display_func=requestsDisplay}, + -- to display colony work orders + [2]={name="orders", display_func=ordersDisplay}, +} + + +function display() + local tw,th = term.getSize() + term.setCursorBlink(false) + term.setBackgroundColor(colors.black) + term.clear() + display_list = {} + + term.setCursorPos(1,1) + term.setBackgroundColor(colors.yellow) + term.setTextColor(colors.black) + term.clearLine() + + term.write(" " .. string.upper(tabs[current_tab].name) .. " ") + if currently_working == false then + term.setCursorPos(tw-8,1) + term.write(" R ") + end + term.setCursorPos(tw-5,1) + term.write(" ^ v ") + term.setBackgroundColor(colors.black) + + tabs[current_tab].display_func() + + if currently_working then + local cx,cy = term.getCursorPos() + term.setCursorPos(1,cy+2) + term.setCursorBlink(true) + term.setTextColor(colors.gray) + term.write("Working... ") + end +end + + +function page_down(th) + page_start_idx = page_start_idx+th-3 +end + + +function page_up(th) + page_start_idx = math.max(1, page_start_idx-th+3) +end + + +function on_touch(touch_x, touch_y) + local tw,th = term.getSize() + if touch_y == 1 then + if touch_x < (#tabs[current_tab].name + 2) then + current_tab = current_tab + 1 + if current_tab > #tabs then + current_tab = 1 + end + return true + elseif currently_working == false and touch_x > tw-9 and touch_x < tw-5 then + immediate_reload = true + return true + elseif touch_x > tw-6 and touch_x < tw-2 then + page_up(th) + return true + elseif touch_x > tw-3 and touch_x <= tw then + page_down(th) + return true + end + elseif current_tab == 1 and touch_x > tw-5 and display_list[touch_y] ~= nil then + local item_name = display_list[touch_y] + if allowed_items[item_name] then + remove_allowed_item(item_name) + else + add_allowed_item(item_name) + end + return true + end + return false +end + + +function wait_for(time_secs) + display() + local timer_id = os.startTimer(time_secs) + while true do + local event_data = {os.pullEvent()} + if event_data[1] == "monitor_touch" or (event_data[1] == "mouse_click" and event_data[2] == 1) then + if on_touch(event_data[3], event_data[4]) then + display() + end + if immediate_reload == true then + immediate_reload = false + break + end + elseif event_data[1] == "timer" then + if event_data[2] == timer_id then + break + end + end + end +end + + +if peripheral.isPresent(monitor_side) then + monitor = peripheral.wrap(monitor_side) + monitor.setTextScale(monitor_scale) + term.redirect(monitor) +end + +function get_requests() + requests = {} + local reqs = colony.getRequests() + local me_items = has_me + if has_me then + me_items = main_me.listItems() + end + for i, req in pairs(reqs) do + local found = false + if me_items then + local left_amount = req.count + for j, it in pairs(req.items) do + for k, have_it in pairs(me_items) do + if have_it.name == it.name then + found = true + table.insert(requests, {req=req, answer=have_it}) + if left_amount > 0 and allowed_items[it.name] == true then + local export_amount = math.min(have_it.amount, left_amount) + left_amount = left_amount - export_amount + main_me.exportItem({name=it.name, count=export_amount}, main_me_inventory_side) + end + end + end + end + end + if found == false then + table.insert(requests, {req=req, answer=nil}) + end + wait_for(step_sleep_time) + end +end + +function get_orders() + orders = {} + local orders = colony.getWorkOrders() + if orders ~= nil then + for i, order in pairs(orders) do + local res = colony.getWorkOrderResources(order.id) + table.insert(orders, {id=order.id, type=order.workOrderType, target=order.type, pos=order.builder, item=res.item, claimed=order.isClaimed, delivering=res.delivering}) + wait_for(step_sleep_time) + end + end +end + +while true do + currently_working = true + get_requests() + get_orders() + currently_working = false + wait_for(round_sleep_time) +end