· 2 years ago · Jul 27, 2023, 05:55 AM
1-- Load the warpdriveCommons API
2if warpdriveCommons then os.unloadAPI("warpdriveCommons") end
3if not os.loadAPI("warpdrive/warpdriveCommons") then error("missing warpdriveCommons") end
4local w = warpdriveCommons.w
5
6if fs.exists("conf") == false then
7 shell.run("pastebin get 38M5cNbZ conf")
8 term.clear()
9end
10
11local OffsetValue = 3 -- How much you want your shield to be configured forward
12local shield = peripheral.find("warpdriveForceFieldProjector")
13local lever = "front" -- format: front,back,left,right,top,bottom ONLY
14local safedist = 15 -- Set Safe Distance
15
16local lasers = {}
17
18function setupLasers()
19 local laserPeripherals = peripheral.getNames()
20 for _, name in ipairs(laserPeripherals) do
21 if peripheral.getType(name) == "warpdriveLaserCamera" then
22 local laser = peripheral.wrap(name)
23 laser.beamFrequency(1420) -- Sets the frequency of the laser to 1420
24 table.insert(lasers, laser)
25 end
26 end
27
28 if #lasers == 0 then
29 print("No laser peripherals found")
30 return false
31 end
32 return true
33end
34
35if not setupLasers() then return end
36
37print("Control System Online, Toggle Redstone To Toggle Shields, Press C to Configure")
38
39local _, upgrades = shield.getUpgrades()
40
41function getRange()
42 if upgrades:match("1/4 x Range") then
43 return 1
44 elseif upgrades:match("2/4 x Range") then
45 return 2
46 elseif upgrades:match("3/4 x Range") then
47 return 3
48 elseif upgrades:match("4/4 x Range") then
49 return 4
50 elseif upgrades:match("0/4 x Range") then
51 return 0
52 end
53end
54
55Size = getRange() * 16
56
57while true do
58 os.sleep(0.5)
59 local event, key = os.pullEvent()
60
61 if event == "key" then
62 if key == 46 then
63 print("C key pressed, running 'conf' script...")
64 shell.run("conf")
65 end
66 elseif event == "laserScanning" then
67 for _, laser in ipairs(lasers) do
68 local type, lx, ly, lz, block = laser.getScanResult()
69 lx, ly, lz = tonumber(lx), tonumber(ly), tonumber(lz) -- Convert to numbers
70 local fx, fy, fz = shield.getLocalPosition()
71
72 function shieldOffset()
73 local dx = lx - fx
74 local dy = ly - fy
75 local dz = lz - fz
76 local distance = math.sqrt(dx * dx + dy * dy + dz * dz)
77 dx = dx / distance
78 dy = dy / distance
79 dz = dz / distance
80 local t1x = lx + dx * OffsetValue
81 local t1y = ly + dy * OffsetValue
82 local t1z = lz + dz * OffsetValue
83 return t1x, t1y, t1z, distance -- Return the final target position and distance
84 end
85
86 local t1x, t1y, t1z, distance = shieldOffset()
87 tx = (t1x - fx) / Size
88 ty = (t1y - fy) / Size
89 tz = (t1z - fz) / Size
90
91 if distance < safedist then
92 print("Target is too Close! Shield Disabled!")
93 shield.enable(false) -- Disable shield if too close
94 elseif distance > safedist and distance > Size then
95 print("Target is too Far! Shield Disabled!")
96 shield.enable(false) -- Disable shield if too far
97 elseif distance > safedist and distance < Size then
98 shield.translation(tx, ty, tz)
99 end
100 end
101 elseif event == "redstone" then
102 local _, _, _, distance = shieldOffset()
103 local on = redstone.getAnalogInput(lever)
104 if on > 6 and distance > safedist and distance < Size then
105 shield.enable(true)
106 elseif on > 6 and distance > safedist and distance > Size then
107 print("Target is too Far! Shield Disabled!")
108 shield.enable(false) -- Disable shield if too far
109 elseif on > 6 and distance < safedist then
110 shield.enable(false)
111 print("Target is too Close! Shield Disabled!")
112 elseif on < 5 then
113 shield.enable(false)
114 end
115 end
116end
117