View difference between Paste ID: 6gGjncZC and TBNWcQyM
SHOW: | | - or go back to the newest paste.
1
if(fs.exists("conf") == false) then
2
    shell.run("pastebin get 38M5cNbZ conf")
3
    term.clear()
4
end
5-
local rangeUpgrade = 7 --Amount of Range Upgrades in Network, Including Relays
5+
local rangeUpgrade = 8 --Amount of Range Upgrades in Network, Including Relays
6-
local OffsetValue = 7 --How much you want your shield to be configured forward
6+
local OffsetValue = 0 --How much you want your shield to be configured forward
7
local shield = peripheral.find("warpdriveForceFieldProjector")
8
local laser = peripheral.find("warpdriveLaserCamera")
9
local lever = "front" -- format: front,back,left,right,top,bottom ONLY
10
local safedist = 15 --Set Safe Distance
11
-- Sets the frequency of the laser to 1420
12
laser.beamFrequency(1420)
13
 
14
-- initial message
15
print("Control System Online, Toggle Redstone To Toggle Shields, Press C to Configure")
16
 
17
18
local _, upgrades = shield.getUpgrades()
19
20
Size = rangeUpgrade * 16
21
22
while true do
23
    os.sleep(0.5)
24
    local event, key = os.pullEvent()     
25
        
26
    if event == "key" then
27
        -- keys are represented by numbers, the number for 'C' is 46
28
        if key == 46 then
29
            print("C key pressed, running 'conf' script...")
30
            shell.run("conf")
31
        end
32
    elseif event == "laserScanning" then
33-
        local type, lx, ly, lz, block = laser.getScanResult()
33+
        -- Laser scan target position
34-
        local fx, fy, fz = shield.getLocalPosition()
34+
local lx, ly, lz = laser.getScanResult()
35
36-
		t1x = lx - fx
36+
-- Shield position
37-
		t1y = ly - fy
37+
local fx, fy, fz = shield.getLocalPosition()
38-
        t1z = lz - fz
38+
39
function shieldOffset()
40-
        function shieldOffset()
40+
-- Calculate direction vector from shield position to laser scan target position
41-
            if t1x > 0 then xfunc = -1
41+
local dx = lx - fx
42-
            elseif t1x < 0 then xfunc = 1
42+
local dy = ly - fy
43-
            elseif t1x == 0 then xfunc = 0
43+
local dz = lz - fz
44-
            end
44+
45-
            if t1y > 0 then yfunc = -1
45+
-- Calculate the length of the direction vector
46-
            elseif t1y < 0 then yfunc = 1
46+
local distance = math.sqrt(dx * dx + dy * dy + dz * dz)
47-
            elseif t1y == 0 then yfunc = 0
47+
48-
            end
48+
-- Normalize the direction vector to have a length of 1 (unit vector)
49-
            if t1z > 0 then zfunc = -1
49+
dx = dx / distance
50-
            elseif t1z < 0 then zfunc = 1
50+
dy = dy / distance
51-
            elseif t1z == 0 then zfunc = 0
51+
dz = dz / distance
52-
            end
52+
53
-- Calculate the final target position by extending the direction vector by OffsetValue
54
local t1x = lx + dx * OffsetValue
55
local t1y = ly + dy * OffsetValue
56-
        function dis()
56+
local t1z = lz + dz * OffsetValue
57-
            l1x = lx - (xfunc*OffsetValue)
57+
58-
            l1y = ly - (yfunc*OffsetValue)
58+
59-
            l1z = lz - (zfunc*OffsetValue)
59+
60
        
61-
            tx = (l1x-fx) / Size
61+
        tx = (t1x-fx) / Size
62-
            ty = (l1y-fy) / Size
62+
        ty = (t1y-fy) / Size
63-
            tz = (l1z-fz) / Size
63+
        tz = (t1z-fz) / Size
64
65-
            -- Assuming (lx, ly, lz) and (fx, fy, fz) are the coordinates of your two points
65+
66-
            dx = l1x - fx
66+
67-
            dy = l1y - fy
67+
68-
            dz = l1z - fz
68+
69
            print("Target is too Close! Shield Disabled!")
70-
            distance = math.sqrt(dx*dx + dy*dy + dz*dz)
70+
71
        elseif distance > safedist then
72
            shield.translation(tx, ty, tz)
73
        end
74
    elseif event == "redstone" then
75
        dis()
76
        local on = redstone.getAnalogInput(lever)
77
        if on > 6 and distance > safedist then
78
            shield.enable(true)
79
        elseif on > 6 and distance < safedist then
80
            shield.enable(false)
81
             print("Target is too Close! Shield Disabled!")
82
        elseif on < 5 then
83
            shield.enable(false)
84
        end
85
    end
86
end
87