emt_monitor.lua

135 lines · 3.6 KB

Open raw

EMT Pocket Monitor

Copy & run

wget https://perlytiara.github.io/turtles.tips/raw/programs/perlytiara/EpicMiningTurtle/pocket/emt_monitor.lua
1-- EMT Pocket Monitor
2-- Monitors EpicMiningTurtle progress/inventory over rednet and can send simple commands
3
4local PROTOCOL = "EMT"
5local tracked_id = nil
6local last = nil
7local last_time = 0
8
9local function open_modem()
10 local opened = false
11 if peripheral and rednet then
12 peripheral.find("modem", function(side, modem)
13 if modem.isWireless and modem.isWireless() then
14 if not rednet.isOpen(side) then rednet.open(side) end
15 opened = true
16 end
17 end)
18 if not opened then
19 peripheral.find("modem", function(side)
20 if not rednet.isOpen(side) then rednet.open(side) end
21 opened = true
22 end)
23 end
24 end
25 return opened
26end
27
28local function draw()
29 term.clear()
30 term.setCursorPos(1,1)
31 write("EMT Pocket Monitor")
32 term.setCursorPos(1,2)
33 write("Tracking: "..(tracked_id and (tostring(tracked_id).." (set)") or "auto"))
34 term.setCursorPos(1,3)
35 if last then
36 write("From #"..tostring(last.sender_id).." "..(last.label or "").." fuel:"..tostring(last.fuel))
37 term.setCursorPos(1,4)
38 local p = last.progress or {}
39 write("Tunnel "..tostring(p.current_tunnel or 0).."/"..tostring(p.total_tunnels or 0).." Layer "..tostring(p.current_layer or 0).."/"..tostring(p.total_layers or 0))
40 term.setCursorPos(1,5)
41 local pos = last.pos
42 if pos then
43 write("Pos: x="..math.floor(pos.x+0.5).." y="..math.floor(pos.y+0.5).." z="..math.floor(pos.z+0.5))
44 else
45 write("Pos: (no GPS)")
46 end
47 term.setCursorPos(1,6)
48 write("Last: "..(last.kind or "status").." at t="..tostring(last_time))
49 term.setCursorPos(1,8)
50 write("Inventory:")
51 local row = 9
52 if last.inventory and #last.inventory > 0 then
53 for i=1, math.min(#last.inventory, 8) do
54 local it = last.inventory[i]
55 term.setCursorPos(1,row)
56 write(string.format("%2d: %-20s x%3d", it.slot, (it.name or "?"), (it.count or 0)))
57 row = row + 1
58 end
59 else
60 term.setCursorPos(1,row)
61 write("(empty)")
62 end
63 else
64 write("Waiting for turtle...")
65 end
66 term.setCursorPos(1,18)
67 write("Keys: [S]et monitor [R]efresh [P]ickup [Q]uit")
68end
69
70local function send_command(id, cmd)
71 if not id then return end
72 rednet.send(id, {command=cmd}, PROTOCOL)
73end
74
75local function handle_msg(id, msg)
76 if type(msg) ~= "table" then return end
77 if msg.kind == "start" or msg.kind == "status" or msg.kind == "heartbeat" or msg.kind == "progress" or msg.kind == "inventory" or msg.kind == "tunnel_complete" or msg.kind == "done" or msg.kind == "pickup" then
78 last = msg
79 last_time = os.clock()
80 if not tracked_id then tracked_id = id end
81 draw()
82 if msg.kind == "done" then
83 for i=1,3 do
84 if term and term.blit then
85 term.setCursorPos(1,7)
86 write("Turtle DONE!")
87 end
88 os.sleep(0.1)
89 end
90 end
91 end
92end
93
94local function main()
95 if not open_modem() then
96 print("No modem found")
97 return
98 end
99 draw()
100 -- Ask any turtles for current status
101 rednet.broadcast({command="request_status"}, PROTOCOL)
102 while true do
103 local e = { os.pullEvent() }
104 local ev = e[1]
105 if ev == "rednet_message" then
106 local id, msg, proto = e[2], e[3], e[4]
107 if proto == PROTOCOL then
108 handle_msg(id, msg)
109 end
110 elseif ev == "key" then
111 local key = e[2]
112 if key == keys.q then
113 break
114 elseif key == keys.r then
115 if tracked_id then send_command(tracked_id, "request_status") else rednet.broadcast({command="request_status"}, PROTOCOL) end
116 draw()
117 elseif key == keys.p then
118 if tracked_id then send_command(tracked_id, "request_pickup") end
119 draw()
120 elseif key == keys.s then
121 if last and last.sender_id then
122 tracked_id = last.sender_id
123 send_command(tracked_id, "set_monitor")
124 end
125 draw()
126 end
127 end
128 end
129end
130
131main()
132
133
134
135