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 | ||
| 6 | - | local OffsetValue = 3 -- How much you want your shield to be configured forward |
| 6 | + | local OffsetValue = 3 |
| 7 | local shield = peripheral.find("warpdriveForceFieldProjector")
| |
| 8 | - | local laser = peripheral.find("warpdriveLaserCamera")
|
| 8 | + | local lasers = peripheral.getNames() -- get all peripherals |
| 9 | - | local lever = "front" -- format: front,back,left,right,top,bottom ONLY |
| 9 | + | local lever = "front" |
| 10 | - | local safedist = 15 -- Set Safe Distance |
| 10 | + | local safedist = 15 |
| 11 | - | laser.beamFrequency(1420) -- Sets the frequency of the laser to 1420 |
| 11 | + | |
| 12 | -- filter only warpdriveLaserCamera peripherals | |
| 13 | - | -- initial message |
| 13 | + | for i=#lasers, 1, -1 do |
| 14 | if peripheral.getType(lasers[i]) ~= "warpdriveLaserCamera" then | |
| 15 | table.remove(lasers, i) | |
| 16 | else | |
| 17 | - | --- this function is really janky but I don't want to make it better |
| 17 | + | peripheral.wrap(lasers[i]).beamFrequency(1420) |
| 18 | end | |
| 19 | end | |
| 20 | ||
| 21 | print("Control System Online, Toggle Redstone To Toggle Shields, Press C to Configure")
| |
| 22 | ||
| 23 | local _, upgrades = shield.getUpgrades() | |
| 24 | ||
| 25 | function getRange() | |
| 26 | if upgrades:match("1/4 x Range") then
| |
| 27 | return 1 | |
| 28 | elseif upgrades:match("2/4 x Range") then
| |
| 29 | return 2 | |
| 30 | elseif upgrades:match("3/4 x Range") then
| |
| 31 | - | |
| 31 | + | |
| 32 | elseif upgrades:match("4/4 x Range") then
| |
| 33 | return 4 | |
| 34 | elseif upgrades:match("0/4 x Range") then
| |
| 35 | return 0 | |
| 36 | end | |
| 37 | end | |
| 38 | ||
| 39 | - | -- keys are represented by numbers, the number for 'C' is 46 |
| 39 | + | |
| 40 | ||
| 41 | while true do | |
| 42 | os.sleep(0.5) | |
| 43 | local event, key = os.pullEvent() | |
| 44 | - | elseif event == "laserScanning" then |
| 44 | + | |
| 45 | - | -- Laser scan target position |
| 45 | + | |
| 46 | - | local type, lx, ly, lz, block = laser.getScanResult() |
| 46 | + | |
| 47 | - | lx, ly, lz = tonumber(lx), tonumber(ly), tonumber(lz) -- Convert to numbers |
| 47 | + | |
| 48 | shell.run("conf")
| |
| 49 | - | -- Shield position |
| 49 | + | |
| 50 | - | local fx, fy, fz = shield.getLocalPosition() |
| 50 | + | |
| 51 | ||
| 52 | - | function shieldOffset() |
| 52 | + | for _, laserName in ipairs(lasers) do |
| 53 | - | -- Calculate direction vector from shield position to laser scan target position |
| 53 | + | local laser = peripheral.wrap(laserName) |
| 54 | - | local dx = lx - fx |
| 54 | + | if event == "laserScanning" then |
| 55 | - | local dy = ly - fy |
| 55 | + | local type, lx, ly, lz, block = laser.getScanResult() |
| 56 | - | local dz = lz - fz |
| 56 | + | lx, ly, lz = tonumber(lx), tonumber(ly), tonumber(lz) |
| 57 | local fx, fy, fz = shield.getLocalPosition() | |
| 58 | - | -- Calculate the length of the direction vector |
| 58 | + | |
| 59 | - | local distance = math.sqrt(dx * dx + dy * dy + dz * dz) |
| 59 | + | function shieldOffset() |
| 60 | local dx = lx - fx | |
| 61 | - | -- Normalize the direction vector to have a length of 1 (unit vector) |
| 61 | + | local dy = ly - fy |
| 62 | - | dx = dx / distance |
| 62 | + | local dz = lz - fz |
| 63 | - | dy = dy / distance |
| 63 | + | local distance = math.sqrt(dx * dx + dy * dy + dz * dz) |
| 64 | - | dz = dz / distance |
| 64 | + | dx = dx / distance |
| 65 | dy = dy / distance | |
| 66 | - | -- Calculate the final target position by extending the direction vector by OffsetValue |
| 66 | + | dz = dz / distance |
| 67 | - | local t1x = lx + dx * OffsetValue |
| 67 | + | local t1x = lx + dx * OffsetValue |
| 68 | - | local t1y = ly + dy * OffsetValue |
| 68 | + | local t1y = ly + dy * OffsetValue |
| 69 | - | local t1z = lz + dz * OffsetValue |
| 69 | + | local t1z = lz + dz * OffsetValue |
| 70 | return t1x, t1y, t1z, distance | |
| 71 | - | return t1x, t1y, t1z, distance -- Return the final target position and distance |
| 71 | + | end |
| 72 | ||
| 73 | local t1x, t1y, t1z, distance = shieldOffset() | |
| 74 | - | local t1x, t1y, t1z, distance = shieldOffset() |
| 74 | + | |
| 75 | tx = (t1x - fx) / Size | |
| 76 | - | tx = (t1x - fx) / Size |
| 76 | + | ty = (t1y - fy) / Size |
| 77 | - | ty = (t1y - fy) / Size |
| 77 | + | tz = (t1z - fz) / Size |
| 78 | - | tz = (t1z - fz) / Size |
| 78 | + | |
| 79 | if distance < safedist then | |
| 80 | - | if distance < safedist then |
| 80 | + | print("Target is too Close! Shield Disabled!")
|
| 81 | - | print("Target is too Close! Shield Disabled!")
|
| 81 | + | shield.enable(false) |
| 82 | - | shield.enable(false) -- Disable shield if too close |
| 82 | + | |
| 83 | print("Target is too Far! Shield Disabled!")
| |
| 84 | - | print("Target is too Far! Shield Disabled!")
|
| 84 | + | shield.enable(false) |
| 85 | - | shield.enable(false) -- Disable shield if too far |
| 85 | + | elseif distance > safedist and distance < Size then |
| 86 | - | elseif distance > safedist and distance < Size then |
| 86 | + | shield.translation(tx, ty, tz) |
| 87 | - | shield.translation(tx, ty, tz) |
| 87 | + | end |
| 88 | elseif event == "redstone" then | |
| 89 | - | elseif event == "redstone" then |
| 89 | + | local _, _, _, distance = shieldOffset() |
| 90 | - | local _, _, _, distance = shieldOffset() |
| 90 | + | local on = redstone.getAnalogInput(lever) |
| 91 | - | local on = redstone.getAnalogInput(lever) |
| 91 | + | if on > 6 and distance > safedist and distance < Size then |
| 92 | - | if on > 6 and distance > safedist and distance < Size then |
| 92 | + | shield.enable(true) |
| 93 | - | shield.enable(true) |
| 93 | + | elseif on > 6 and distance > safedist and distance > Size then |
| 94 | - | elseif on > 6 and distance > safedist and distance > Size then |
| 94 | + | print("Target is too Far! Shield Disabled!")
|
| 95 | - | print("Target is too Far! Shield Disabled!")
|
| 95 | + | shield.enable(false) |
| 96 | - | shield.enable(false) -- Disable shield if too far |
| 96 | + | elseif on > 6 and distance < safedist then |
| 97 | - | elseif on > 6 and distance < safedist then |
| 97 | + | shield.enable(false) |
| 98 | - | shield.enable(false) |
| 98 | + | print("Target is too Close! Shield Disabled!")
|
| 99 | - | print("Target is too Close! Shield Disabled!")
|
| 99 | + | elseif on < 5 then |
| 100 | - | elseif on < 5 then |
| 100 | + | shield.enable(false) |
| 101 | - | shield.enable(false) |
| 101 | + | end |
| 102 | end | |
| 103 | end | |
| 104 | end | |
| 105 |