Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Load the warpdriveCommons API
- if warpdriveCommons then os.unloadAPI("warpdriveCommons") end
- if not os.loadAPI("warpdrive/warpdriveCommons") then error("missing warpdriveCommons") end
- local w = warpdriveCommons.w
- if fs.exists("conf") == false then
- shell.run("pastebin get 38M5cNbZ conf")
- term.clear()
- end
- local OffsetValue = 0 -- How much you want your shield to be configured forward
- local shield = peripheral.find("warpdriveForceFieldProjector")
- local lever = "front" -- format: front,back,left,right,top,bottom ONLY
- local safedist = 5 -- Set Safe Distance
- local lasers = {}
- function setupLasers()
- local laserPeripherals = peripheral.getNames()
- for _, name in ipairs(laserPeripherals) do
- if peripheral.getType(name) == "warpdriveLaserCamera" then
- local laser = peripheral.wrap(name)
- laser.beamFrequency(1420) -- Sets the frequency of the laser to 1420
- table.insert(lasers, laser)
- end
- end
- if #lasers == 0 then
- print("No laser peripherals found")
- return false
- end
- return true
- end
- if not setupLasers() then return end
- print("Control System Online, Toggle Redstone To Toggle Shields, Press C to Configure")
- local _, upgrades = shield.getUpgrades()
- function getRange()
- if upgrades:match("1/4 x Range") then
- return 1
- elseif upgrades:match("2/4 x Range") then
- return 2
- elseif upgrades:match("3/4 x Range") then
- return 3
- elseif upgrades:match("4/4 x Range") then
- return 4
- elseif upgrades:match("0/4 x Range") then
- return 0
- end
- end
- Size = getRange() * 16
- while true do
- os.sleep(0.5)
- local event, key = os.pullEvent()
- if event == "key" then
- if key == 46 then
- print("C key pressed, running 'conf' script...")
- shell.run("conf")
- end
- elseif event == "laserScanning" then
- for _, laser in ipairs(lasers) do
- local type, lx, ly, lz, block = laser.getScanResult()
- lx, ly, lz = tonumber(lx), tonumber(ly), tonumber(lz) -- Convert to numbers
- local fx, fy, fz = shield.getLocalPosition()
- function shieldOffset()
- local dx = lx - fx
- local dy = ly - fy
- local dz = lz - fz
- local distance = math.sqrt(dx * dx + dy * dy + dz * dz)
- dx = dx / distance
- dy = dy / distance
- dz = dz / distance
- local t1x = lx + dx * OffsetValue
- local t1y = ly + dy * OffsetValue
- local t1z = lz + dz * OffsetValue
- return t1x, t1y, t1z, distance -- Return the final target position and distance
- end
- local t1x, t1y, t1z, distance = shieldOffset()
- tx = (t1x - fx) / Size
- ty = (t1y - fy) / Size
- tz = (t1z - fz) / Size
- if distance < safedist then
- print("Target is too Close! Shield Disabled!")
- shield.enable(false) -- Disable shield if too close
- elseif distance > safedist and distance > Size then
- print("Target is too Far! Shield Disabled!")
- shield.enable(false) -- Disable shield if too far
- elseif distance > safedist and distance < Size then
- shield.translation(tx, ty, tz)
- end
- end
- elseif event == "redstone" then
- local _, _, _, distance = shieldOffset()
- local on = redstone.getAnalogInput(lever)
- if on > 6 and distance > safedist and distance < Size then
- shield.enable(true)
- elseif on > 6 and distance > safedist and distance > Size then
- print("Target is too Far! Shield Disabled!")
- shield.enable(false) -- Disable shield if too far
- elseif on > 6 and distance < safedist then
- shield.enable(false)
- print("Target is too Close! Shield Disabled!")
- elseif on < 5 then
- shield.enable(false)
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement