View difference between Paste ID: CWdMG7dM and 3i7qHRm0
SHOW: | | - or go back to the newest paste.
1
-- Warp Drive Shield Control via Rednet (“ztc” replacing laserScanning with SMineBroadcast)
2-
local shield = peripheral.find("warpdriveForceFieldProjector")
2+
3-
local lasers = peripheral.getNames()
3+
4-
local speaker = peripheral.find("warpdriveSpeaker")
4+
local shield      = peripheral.find("warpdriveForceFieldProjector")
5-
local lever = "front"
5+
local speaker     = peripheral.find("warpdriveSpeaker")
6-
local safedist = 15
6+
local lever       = "front"
7
local safedist    = 15
8
local lastLx, lastLy, lastLz = 0, 0, 0
9-
-- Helper: only speak if a speaker is present
9+
10
-- Speak only if we have a speaker attached
11
local function trySpeak(msg)
12
    if speaker then speaker.speak(msg) end
13
end
14-
function shieldOffset(lx, ly, lz, fx, fy, fz)
14+
15
-- Compute the offset point and distance
16
local function shieldOffset(lx, ly, lz, fx, fy, fz)
17
    local dx = lx - fx
18-
    local distance = math.sqrt(dx * dx + dy * dy + dz * dz)
18+
19-
    dx = dx / distance
19+
20-
    dy = dy / distance
20+
    local distance = math.sqrt(dx*dx + dy*dy + dz*dz)
21-
    dz = dz / distance
21+
    dx, dy, dz = dx/distance, dy/distance, dz/distance
22
    local t1x = lx + dx * OffsetValue
23
    local t1y = ly + dy * OffsetValue
24
    local t1z = lz + dz * OffsetValue
25
    return t1x, t1y, t1z, distance
26
end
27
28
-- Ensure configuration script is available
29
if not fs.exists("conf") then
30
    shell.run("pastebin get 38M5cNbZ conf")
31
    term.clear()
32
end
33-
for i = #lasers, 1, -1 do
33+
34-
    if peripheral.getType(lasers[i]) ~= "warpdriveLaserCamera" then
34+
-- Open all modems for rednet
35-
        table.remove(lasers, i)
35+
for _, side in ipairs(peripheral.getNames()) do
36-
    else
36+
    if peripheral.getType(side) == "modem" then
37-
        peripheral.wrap(lasers[i]).beamFrequency(1420)
37+
        rednet.open(side)
38
    end
39
end
40
41
print("Control System Online, Toggle Redstone To Toggle Shields, Press C to Configure")
42
trySpeak("Control System Online")
43
44
-- Determine shield range multiplier
45-
local Size
45+
46-
if upgrades:match("1/4 x Range") then
46+
local Size = ({
47-
    Size = 16
47+
    ["1/4 x Range"] = 16,
48-
elseif upgrades:match("2/4 x Range") then
48+
    ["2/4 x Range"] = 32,
49-
    Size = 32
49+
    ["3/4 x Range"] = 48,
50-
elseif upgrades:match("3/4 x Range") then
50+
    ["4/4 x Range"] = 64,
51-
    Size = 48
51+
    ["0/4 x Range"] = 0,
52-
elseif upgrades:match("4/4 x Range") then
52+
})[upgrades:match("(%d/4 x Range)")] or 32  -- fallback to 32
53-
    Size = 64
53+
54-
elseif upgrades:match("0/4 x Range") then
54+
-- Main event loop: handle rednet messages, redstone, and config key
55-
    Size = 0
55+
56
    local event, a, b, c = os.pullEventRaw()
57
58
    if event == "rednet_message" and c == "SMineBroadcast" then
59-
    local event, laserName, lx, ly, lz = os.pullEvent()
59+
        -- Received new target coordinates
60-
    if event == "laserScanning" then
60+
        lastLx, lastLy, lastLz = b.x, b.y, b.z
61-
        lastLx, lastLy, lastLz = tonumber(lx), tonumber(ly), tonumber(lz)
61+
62
        local fx, fy, fz = shield.getLocalPosition()
63
        local t1x, t1y, t1z, distance = shieldOffset(lastLx, lastLy, lastLz, fx, fy, fz)
64
65
        local tx = (t1x - fx) / Size
66
        local ty = (t1y - fy) / Size
67
        local tz = (t1z - fz) / Size
68
69-
        -- round once for speaking
69+
        -- Round values for speaking
70-
        local rd = math.floor(distance + 0.5)
70+
        local rd   = math.floor(distance + 0.5)
71-
        local rt1x = math.floor(t1x + 0.5)
71+
        local rt1x = math.floor(t1x     + 0.5)
72-
        local rt1y = math.floor(t1y + 0.5)
72+
        local rt1y = math.floor(t1y     + 0.5)
73-
        local rt1z = math.floor(t1z + 0.5)
73+
        local rt1z = math.floor(t1z     + 0.5)
74
75
        if distance < safedist then
76
            print("Target is too Close! Shield Disabled!")
77
            trySpeak("Target is too Close! Shield Disabled!")
78
            shield.enable(false)
79
            trySpeak("Target Dis: " .. rd)
80
            trySpeak("Offset Coordinates: x=" .. rt1x .. ", y=" .. rt1y .. ", z=" .. rt1z)
81-
            trySpeak("Laser Coordinates: x=" .. lastLx .. ", y=" .. lastLy .. ", z=" .. lastLz)
81+
            trySpeak("Target Coordinates: x=" .. lastLx .. ", y=" .. lastLy .. ", z=" .. lastLz)
82
        else
83
            shield.translation(tx, ty, tz)
84
            trySpeak("Target Dis: " .. rd)
85
            trySpeak("Offset Coordinates: x=" .. rt1x .. ", y=" .. rt1y .. ", z=" .. rt1z)
86-
            trySpeak("Laser Coordinates: x=" .. lastLx .. ", y=" .. lastLy .. ", z=" .. lastLz)
86+
            trySpeak("Target Coordinates: x=" .. lastLx .. ", y=" .. lastLy .. ", z=" .. lastLz)
87
        end
88
89
    elseif event == "redstone" then
90
        -- Toggle shields via redstone and safe distance
91
        local fx, fy, fz = shield.getLocalPosition()
92
        local _, _, _, distance = shieldOffset(lastLx, lastLy, lastLz, fx, fy, fz)
93
        local on = redstone.getAnalogInput(lever)
94
95
        if on > 6 and distance > safedist then
96
            shield.enable(true)
97
        elseif on > 6 and distance < safedist then
98
            print("Target is too Close! Shield Disabled!")
99
            trySpeak("Target is too Close! Shield Disabled!")
100
            shield.enable(false)
101
        else
102
            shield.enable(false)
103-
    elseif event == "key" and laserName == 46 then
103+
104
105
    elseif event == "key" and a == keys.c then
106
        -- Run configuration
107
        print("C key pressed, running 'conf' script...")
108
        shell.run("conf")
109
    end
110
end
111