cc/test.lua

272 lines
8.1 KiB
Lua

local config = require "config"
local pretty = require "cc.pretty"
local permset = require "permset"
local main_me = {}
if config.has_me then
main_me = peripheral.wrap(config.main_system_ae_bridge_side)
end
local colony = peripheral.wrap(config.colony_interface_side)
local requests = {}
local orders = {}
local current_tab = config.initial_tab
-- 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()
local tw,th = term.getSize()
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 permset.has(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 permset.has(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()
local tw,th = term.getSize()
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.res_count then
term.setCursorPos(tw-2-#val.res_count, line_num)
term.write(val.res_count)
else
term.setCursorPos(tw-2, 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(" ")
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 permset.has(allowed_items, item_name) then
permset.remove(allowed_items, item_name)
else
permset.add(allowed_items, 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(config.monitor_side) then
monitor = peripheral.wrap(config.monitor_side)
monitor.setTextScale(config.monitor_scale)
term.redirect(monitor)
end
function get_requests()
requests = {}
local reqs = colony.getRequests()
local me_items = config.has_me
if config.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 permset.has(allowed_items, it.name) 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}, config.main_me_inventory_side)
end
end
end
end
end
if found == false then
table.insert(requests, {req=req, answer=nil})
end
wait_for(config.step_sleep_time)
end
end
function get_orders()
orders = {}
local colony_orders = colony.getWorkOrders()
if colony_orders ~= nil then
for i, wo in pairs(colony_orders) do
local res = colony.getWorkOrderResources(wo.id)
if res == nil then
res = "0"
else
res = "" .. #res
end
table.insert(orders, {id=wo.id, type=wo.workOrderType, target=wo.type, pos=wo.builder, res_count=res, claimed=wo.isClaimed})
wait_for(config.step_sleep_time)
end
end
end
while true do
currently_working = true
get_requests()
get_orders()
currently_working = false
wait_for(config.round_sleep_time)
end