Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if fs.exists("conf") == false then
- shell.run("pastebin get 38M5cNbZ conf")
- term.clear()
- end
- local OffsetValue = 3 -- How much you want your shield to be configured forward
- local shield = peripheral.find("warpdriveForceFieldProjector")
- local laser = peripheral.find("warpdriveLaserCamera")
- local lever = "front" -- format: front,back,left,right,top,bottom ONLY
- local safedist = 15 -- Set Safe Distance
- laser.beamFrequency(1420) -- Sets the frequency of the laser to 1420
- -- initial message
- print("Control System Online, Toggle Redstone To Toggle Shields, Press C to Configure")
- local _, upgrades = shield.getUpgrades()
- --- this function is really janky but I don't want to make it better
- 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
- -- keys are represented by numbers, the number for 'C' is 46
- if key == 46 then
- print("C key pressed, running 'conf' script...")
- shell.run("conf")
- end
- elseif event == "laserScanning" then
- -- Laser scan target position
- local type, lx, ly, lz, block = laser.getScanResult()
- lx, ly, lz = tonumber(lx), tonumber(ly), tonumber(lz) -- Convert to numbers
- -- Shield position
- local fx, fy, fz = shield.getLocalPosition()
- function shieldOffset()
- -- Calculate direction vector from shield position to laser scan target position
- local dx = lx - fx
- local dy = ly - fy
- local dz = lz - fz
- -- Calculate the length of the direction vector
- local distance = math.sqrt(dx * dx + dy * dy + dz * dz)
- -- Normalize the direction vector to have a length of 1 (unit vector)
- dx = dx / distance
- dy = dy / distance
- dz = dz / distance
- -- Calculate the final target position by extending the direction vector by OffsetValue
- 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
- 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