SHOW:
|
|
- or go back to the newest paste.
1 | - | -- Warp Drive Shield Control via Rednet (“ztc” replacing laserScanning with SMineBroadcast) |
1 | + | -- Combined Shield Control & Monitor (“ztc+”) |
2 | ||
3 | -- 1) Configuration | |
4 | - | local shield = peripheral.find("warpdriveForceFieldProjector") |
4 | + | |
5 | - | local speaker = peripheral.find("warpdriveSpeaker") |
5 | + | |
6 | local lever = "front" | |
7 | ||
8 | - | local lastLx, lastLy, lastLz = 0, 0, 0 |
8 | + | -- 2) Wrap peripherals |
9 | local shield = peripheral.find("warpdriveForceFieldProjector") | |
10 | - | -- Speak only if we have a speaker attached |
10 | + | local speaker = peripheral.find("warpdriveSpeaker") |
11 | assert(shield, "ForceFieldProjector not found!") | |
12 | - | if speaker then speaker.speak(msg) end |
12 | + | |
13 | -- 3) Speak helper (no-op if no speaker) | |
14 | local function trySpeak(msg) | |
15 | - | -- Compute the offset point and distance |
15 | + | if speaker then speaker.speak(msg) end |
16 | end | |
17 | - | local dx = lx - fx |
17 | + | |
18 | - | local dy = ly - fy |
18 | + | -- 4) Compute the offset point and distance |
19 | - | local dz = lz - fz |
19 | + | |
20 | - | local distance = math.sqrt(dx*dx + dy*dy + dz*dz) |
20 | + | local dx, dy, dz = lx - fx, ly - fy, lz - fz |
21 | - | dx, dy, dz = dx/distance, dy/distance, dz/distance |
21 | + | local distance = math.sqrt(dx*dx + dy*dy + dz*dz) |
22 | - | local t1x = lx + dx * OffsetValue |
22 | + | dx, dy, dz = dx/distance, dy/distance, dz/distance |
23 | - | local t1y = ly + dy * OffsetValue |
23 | + | return |
24 | - | local t1z = lz + dz * OffsetValue |
24 | + | lx + dx * OffsetValue, |
25 | - | return t1x, t1y, t1z, distance |
25 | + | ly + dy * OffsetValue, |
26 | lz + dz * OffsetValue, | |
27 | distance | |
28 | - | -- Ensure configuration script is available |
28 | + | |
29 | ||
30 | - | shell.run("pastebin get 38M5cNbZ conf") |
30 | + | -- 5) Ensure config script exists |
31 | - | term.clear() |
31 | + | |
32 | shell.run("pastebin get 38M5cNbZ conf") | |
33 | term.clear() | |
34 | - | -- Open all modems for rednet |
34 | + | |
35 | ||
36 | - | if peripheral.getType(side) == "modem" then |
36 | + | -- 6) Open all modems for Rednet |
37 | - | rednet.open(side) |
37 | + | |
38 | if peripheral.getType(side) == "modem" then | |
39 | rednet.open(side) | |
40 | end | |
41 | end | |
42 | ||
43 | -- 7) Announce readiness | |
44 | - | -- Determine shield range multiplier |
44 | + | |
45 | trySpeak("Control System Online") | |
46 | ||
47 | - | ["1/4 x Range"] = 16, |
47 | + | -- 8) Determine shield range multiplier |
48 | - | ["2/4 x Range"] = 32, |
48 | + | |
49 | - | ["3/4 x Range"] = 48, |
49 | + | |
50 | - | ["4/4 x Range"] = 64, |
50 | + | ["1/4 x Range"] = 16, |
51 | - | ["0/4 x Range"] = 0, |
51 | + | ["2/4 x Range"] = 32, |
52 | - | })[upgrades:match("(%d/4 x Range)")] or 32 -- fallback to 32 |
52 | + | ["3/4 x Range"] = 48, |
53 | ["4/4 x Range"] = 64, | |
54 | - | -- Main event loop: handle rednet messages, redstone, and config key |
54 | + | ["0/4 x Range"] = 0, |
55 | - | while true do |
55 | + | })[upgrades:match("(%d/4 x Range)")] or 32 |
56 | - | local event, a, b, c = os.pullEventRaw() |
56 | + | |
57 | -- 9) State‐monitor coroutine: watch for shield enable/disable changes | |
58 | - | if event == "rednet_message" and c == "SMineBroadcast" then |
58 | + | local function stateMonitor() |
59 | - | -- Received new target coordinates |
59 | + | local prevEnabled = shield.enable() |
60 | - | lastLx, lastLy, lastLz = b.x, b.y, b.z |
60 | + | while true do |
61 | local enabled = shield.enable() | |
62 | - | local fx, fy, fz = shield.getLocalPosition() |
62 | + | if enabled ~= prevEnabled then |
63 | - | local t1x, t1y, t1z, distance = shieldOffset(lastLx, lastLy, lastLz, fx, fy, fz) |
63 | + | if enabled then |
64 | print("Shield state changed → ENABLED") | |
65 | - | local tx = (t1x - fx) / Size |
65 | + | trySpeak("Shield enabled") |
66 | - | local ty = (t1y - fy) / Size |
66 | + | rednet.broadcast("StatON", "SMiner") |
67 | - | local tz = (t1z - fz) / Size |
67 | + | else |
68 | print("Shield state changed → DISABLED") | |
69 | - | -- Round values for speaking |
69 | + | trySpeak("Shield disabled") |
70 | - | local rd = math.floor(distance + 0.5) |
70 | + | rednet.broadcast("StatOFF", "SMiner") |
71 | - | local rt1x = math.floor(t1x + 0.5) |
71 | + | end |
72 | - | local rt1y = math.floor(t1y + 0.5) |
72 | + | prevEnabled = enabled |
73 | - | local rt1z = math.floor(t1z + 0.5) |
73 | + | |
74 | os.sleep(0.5) | |
75 | - | if distance < safedist then |
75 | + | end |
76 | - | print("Target is too Close! Shield Disabled!") |
76 | + | |
77 | - | trySpeak("Target is too Close! Shield Disabled!") |
77 | + | |
78 | - | shield.enable(false) |
78 | + | -- 10) Rednet‐listener coroutine: respond to "check" queries |
79 | - | trySpeak("Target Dis: " .. rd) |
79 | + | local function rednetListener() |
80 | - | trySpeak("Offset Coordinates: x=" .. rt1x .. ", y=" .. rt1y .. ", z=" .. rt1z) |
80 | + | while true do |
81 | - | trySpeak("Target Coordinates: x=" .. lastLx .. ", y=" .. lastLy .. ", z=" .. lastLz) |
81 | + | local _, message = rednet.receive("SMiner") |
82 | - | else |
82 | + | if message == "check" then |
83 | - | shield.translation(tx, ty, tz) |
83 | + | local enabled = shield.enable() |
84 | - | trySpeak("Target Dis: " .. rd) |
84 | + | if enabled then |
85 | - | trySpeak("Offset Coordinates: x=" .. rt1x .. ", y=" .. rt1y .. ", z=" .. rt1z) |
85 | + | print("Received check → Shield currently ENABLED") |
86 | - | trySpeak("Target Coordinates: x=" .. lastLx .. ", y=" .. lastLy .. ", z=" .. lastLz) |
86 | + | trySpeak("Shield is enabled") |
87 | - | end |
87 | + | rednet.broadcast("StatON", "SMiner") |
88 | else | |
89 | print("Received check → Shield currently DISABLED") | |
90 | - | -- Toggle shields via redstone and safe distance |
90 | + | trySpeak("Shield is disabled") |
91 | - | local fx, fy, fz = shield.getLocalPosition() |
91 | + | rednet.broadcast("StatOFF", "SMiner") |
92 | - | local _, _, _, distance = shieldOffset(lastLx, lastLy, lastLz, fx, fy, fz) |
92 | + | end |
93 | - | local on = redstone.getAnalogInput(lever) |
93 | + | |
94 | end | |
95 | - | if on > 6 and distance > safedist then |
95 | + | |
96 | - | shield.enable(true) |
96 | + | |
97 | - | elseif on > 6 and distance < safedist then |
97 | + | -- 11) Main control coroutine: handle incoming target coords, redstone, config |
98 | - | print("Target is too Close! Shield Disabled!") |
98 | + | local function mainControl() |
99 | - | trySpeak("Target is too Close! Shield Disabled!") |
99 | + | local lastX, lastY, lastZ = 0,0,0 |
100 | - | shield.enable(false) |
100 | + | while true do |
101 | - | else |
101 | + | local event, p1, p2, p3 = os.pullEventRaw() |
102 | - | shield.enable(false) |
102 | + | if event == "rednet_message" and p3 == "SMineBroadcast" then |
103 | - | end |
103 | + | -- unpack broadcast as p1=sender, p2=table{x,y,z}, p3=protocol |
104 | lastX, lastY, lastZ = p2.x, p2.y, p2.z | |
105 | - | elseif event == "key" and a == keys.c then |
105 | + | |
106 | - | -- Run configuration |
106 | + | local fx, fy, fz = shield.getLocalPosition() |
107 | - | print("C key pressed, running 'conf' script...") |
107 | + | local t1x, t1y, t1z, distance = shieldOffset(lastX, lastY, lastZ, fx, fy, fz) |
108 | - | shell.run("conf") |
108 | + | |
109 | -- normalize for shield.translation | |
110 | local tx = (t1x - fx) / Size | |
111 | local ty = (t1y - fy) / Size | |
112 | local tz = (t1z - fz) / Size | |
113 | ||
114 | -- rounded for speech | |
115 | local rd = math.floor(distance + 0.5) | |
116 | local rt1x = math.floor(t1x + 0.5) | |
117 | local rt1y = math.floor(t1y + 0.5) | |
118 | local rt1z = math.floor(t1z + 0.5) | |
119 | ||
120 | if distance < safedist then | |
121 | print("Target is too Close! Shield Disabled!") | |
122 | trySpeak("Target is too close! Shield disabled!") | |
123 | shield.enable(false) | |
124 | trySpeak("Target distance " .. rd) | |
125 | trySpeak(("Offset coords x=%d, y=%d, z=%d"):format(rt1x,rt1y,rt1z)) | |
126 | trySpeak(("Target coords x=%d, y=%d, z=%d"):format(lastX,lastY,lastZ)) | |
127 | else | |
128 | shield.translation(tx, ty, tz) | |
129 | trySpeak("Target distance " .. rd) | |
130 | trySpeak(("Offset coords x=%d, y=%d, z=%d"):format(rt1x,rt1y,rt1z)) | |
131 | trySpeak(("Target coords x=%d, y=%d, z=%d"):format(lastX,lastY,lastZ)) | |
132 | end | |
133 | ||
134 | elseif event == "redstone" then | |
135 | -- front lever toggles shield (with safedist guard) | |
136 | local fx, fy, fz = shield.getLocalPosition() | |
137 | local _,_,_,distance = shieldOffset(lastX, lastY, lastZ, fx, fy, fz) | |
138 | local on = redstone.getAnalogInput(lever) | |
139 | ||
140 | if on > 6 and distance > safedist then | |
141 | shield.enable(true) | |
142 | elseif on > 6 then | |
143 | print("Target is too Close! Shield Disabled!") | |
144 | trySpeak("Target is too close! Shield disabled!") | |
145 | shield.enable(false) | |
146 | else | |
147 | shield.enable(false) | |
148 | end | |
149 | ||
150 | elseif event == "key" and p1 == keys.c then | |
151 | print("C key pressed → running 'conf'…") | |
152 | shell.run("conf") | |
153 | end | |
154 | end | |
155 | end | |
156 | ||
157 | -- 12) Spin up all three coroutines in parallel | |
158 | parallel.waitForAny(stateMonitor, rednetListener, mainControl) | |
159 |