Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local radar = peripheral.find("sp_radar")
- local cannon = peripheral.find("cbcmf_compact_cannon_mount")
- if not(cannon) then cannon = peripheral.find("cbc_cannon_mount") end
- local projectileSpeed = 240
- local g = 0.05
- local vm
- local shipPos
- local pInfo
- local scanInterval = 0.05
- local locked = false
- local friendlyIDs
- local friendlyIDFile = "friendly_ids.txt"
- print("Do you want to enable auto fire, 'yes' or 'no', default: yes")
- local auto = io.read()
- if auto == "yes" then
- auto = true
- elseif auto == "no" then
- auto = false
- else
- auto = true
- end
- local function readFriendlyIDs(filename)
- if not fs.exists(filename) then
- print("Friendly IDs file '" .. filename .. "' does not exist. Creating it.")
- local file = fs.open(filename, "w")
- print("Input friendly IDs (player names) separated by commas:")
- local ids = io.read()
- file.write(ids)
- file.close()
- else
- print("Do you want to edit the config? (yes/no),press enter to skip")
- local editChoice = io.read()
- if editChoice == "" then
- editChoice = "no"
- end
- if editChoice:lower() == "yes" then
- if fs.exists(filename) then
- fs.delete(filename)
- print("Friendly IDs file '" .. filename .. "' does not exist. Creating it.")
- local file = fs.open(filename, "w")
- print("Input friendly IDs (player names) separated by commas:")
- local ids = io.read()
- file.write(ids)
- file.close()
- end
- end
- end
- local file = fs.open(filename, "r")
- local friendlyIDs = {}
- if file then
- local line = file.readLine()
- while line do
- print("Line read: " .. line) -- Debug print for the line
- -- Split the line by commas and insert each name into the friendlyIDs table
- for name in string.gmatch(line, '([^,]+)') do
- print("Friendly ID (Name): " .. name) -- Debug print for each name
- table.insert(friendlyIDs, name) -- Store the player name
- end
- line = file.readLine() -- Read the next line
- end
- file.close()
- end
- return friendlyIDs
- end
- -- Function to check if a player ID is friendly
- local function isFriendly(playerID)
- for _, friendlyID in ipairs(friendlyIDs) do
- if playerID == friendlyID then
- return true
- end
- end
- return false
- end
- -- Read friendly IDs from the file
- friendlyIDs = readFriendlyIDs(friendlyIDFile)
- print("Friendly IDs: " .. textutils.serialize(friendlyIDs)) -- Debug print
- if cannon.assemble() then
- print("asseble failed, press enter to contiue")
- io.read()
- end
- local function calculateVelocity(pos1, pos2, dt)
- local vx = (pos2[1] - pos1[1]) / dt
- local vy = (pos2[2] - pos1[2]) / dt
- local vz = (pos2[3] - pos1[3]) / dt
- return {x=vx, y=0, z=vz}
- end
- local function findRelativeAngle(targetYaw,targetPitch)
- rot = ship.getQuaternion()
- cacheYaw = math.pi - math.rad(targetYaw)
- cachePitch = - math.rad(targetPitch)
- rotMatAdj11 = 1 - 2*(rot.x^2 + rot.y^2)
- rotMatAdj12 = 2*(rot.z * rot.x + rot.y * rot.w)
- rotMatAdj13 = 2*(rot.z * rot.y - rot.x * rot.w)
- rotMatAdj21 = 2*(rot.z * rot.x - rot.y * rot.w)
- rotMatAdj22 = 1 - 2*(rot.z^2 + rot.y^2)
- rotMatAdj23 = 2*(rot.x * rot.y + rot.z * rot.w)
- rotMatAdj31 = 2*(rot.z * rot.y + rot.x * rot.w)
- rotMatAdj32 = 2*(rot.x * rot.y - rot.z * rot.w)
- rotMatAdj33 = 1 - 2*(rot.z^2 + rot.x^2)
- rotMatTGT11 = math.cos(cacheYaw) * math.cos(cachePitch)
- rotMatTGT21 = math.sin(cacheYaw) * math.cos(cachePitch)
- rotMatTGT31 = - math.sin(cachePitch)
- rotMatRSLT11 = rotMatAdj11 * rotMatTGT11 + rotMatAdj12 * rotMatTGT21 + rotMatAdj13 * rotMatTGT31
- rotMatRSLT21 = rotMatAdj21 * rotMatTGT11 + rotMatAdj22 * rotMatTGT21 + rotMatAdj23 * rotMatTGT31
- rotMatRSLT31 = rotMatAdj31 * rotMatTGT11 + rotMatAdj32 * rotMatTGT21 + rotMatAdj33 * rotMatTGT31
- turretYaw = math.atan2(rotMatRSLT21, rotMatRSLT11)
- barrelPitch = math.asin(-rotMatRSLT31)
- return math.deg(-turretYaw),math.deg(-barrelPitch)
- end
- local function calculateRange(angle, u, cd, g, c_est, projectileSpeed)
- local radians = math.rad(angle)
- local u = projectileSpeed/20
- local part1 = u * math.cos(radians) / math.log(cd)
- local part2 = ((g * cd) / (g * cd + (1 - cd) * u * math.sin(radians))) ^ (2 + c_est * projectileSpeed * math.sin(radians)) - 1
- local XR = part1 * part2
- return XR
- end
- local function findBestPitch(targetX, targetY, targetZ, sourceX, sourceY, sourceZ, initialVelocity, g, cd, c_est, projectileSpeed)
- local bestLowPitch = nil
- local bestHighPitch = nil
- local bestLowDistance = math.huge
- local bestHighDistance = math.huge
- local targetDistance = math.sqrt((targetX - sourceX)^2 + (targetZ - sourceZ)^2)
- for pitch = 0, 70, 0.01 do -- Iterate over pitch angles
- local calculatedRange = calculateRange(pitch, initialVelocity, cd, g, c_est, projectileSpeed)
- local distanceDifference = math.abs(calculatedRange - targetDistance)
- -- Find the low-angle solution
- if pitch <= 30 then
- if distanceDifference < bestLowDistance then
- bestLowDistance = distanceDifference
- bestLowPitch = pitch
- end
- -- Find the high-angle solution
- elseif pitch > 30 then
- if distanceDifference < bestHighDistance then
- bestHighDistance = distanceDifference
- bestHighPitch = pitch
- end
- end
- end
- -- If the target is within 400 blocks, prioritize the low-angle solution
- if targetDistance <= 830 then
- return bestLowPitch or bestLowPitch
- else
- return bestHighPitch or bestLowPitch
- end
- end
- local function aimCannon(targetPos, targetVel, sourceX, sourceY, sourceZ)
- if sourceX and sourceY and sourceZ then
- local dx = targetPos.x - sourceX
- local dy = targetPos.y - sourceY
- local dz = targetPos.z - sourceZ
- local distance = math.sqrt(dx * dx + dy * dy + dz * dz)
- local estimateTime = distance / (projectileSpeed) -- assuming velocity units and time units need adjustment
- local estimateX = targetPos.x + targetVel.x * estimateTime
- local estimateY = targetPos.y + targetVel.y * estimateTime
- local estimateZ = targetPos.z + targetVel.z * estimateTime
- local dx = estimateX - sourceX
- local dy = estimateY - sourceY
- local dz = estimateZ - sourceZ
- local horizontalDistance = math.sqrt(dx * dx + dz * dz)
- local pitch = math.deg(math.atan2(dy, horizontalDistance))
- local yaw = math.deg(math.atan2(-dx, dz))
- yaw = (yaw + 180) % 360
- -- Adjust pitch based on distance adjustments
- pitch = pitch + findBestPitch(estimateX, estimateY, estimateZ, sourceX, sourceY, sourceZ, projectileSpeed, g, 0.99, 0.0028, projectileSpeed)
- local shipRoll = math.deg(ship.getRoll())
- local shipYaw = math.deg(ship.getYaw())
- if shipYaw < 0 then shipYaw = shipYaw + 360 end
- local shipPitch = math.deg(ship.getPitch())
- local requiredRelativeYaw = yaw - shipYaw
- if requiredRelativeYaw > 180 then
- requiredRelativeYaw = requiredRelativeYaw - 360
- elseif requiredRelativeYaw < -180 then
- requiredRelativeYaw = requiredRelativeYaw + 360
- end
- local requiredRelativeYaw,requiredRelativePitch = findRelativeAngle(yaw,pitch)
- if math.abs(math.abs(requiredRelativePitch) - math.abs(cannon.getPitch())) > 0.2 then
- cannon.setPitch(requiredRelativePitch)
- end
- if math.abs(requiredRelativeYaw - cannon.getYaw()) > 0.1 then
- cannon.setYaw(requiredRelativeYaw)
- end
- end
- end
- local function main()
- while true do
- shipPos = ship.getWorldspacePosition()
- -- First scan for players
- local targetScan1 = radar.scanForPlayers(100)
- -- Variables to hold the closest target data
- local closestPlayer = nil
- local closestDistance = math.huge
- local playerPos1 = nil
- local pInfo = nil
- -- Loop through each player detected in the first scan
- for _, player in pairs(targetScan1) do
- if player and player.pos then
- local dx = player.pos[1] - shipPos.x
- local dy = player.pos[2] - shipPos.y
- local dz = player.pos[3] - shipPos.z
- local distance = math.sqrt(dx * dx + dy * dy + dz * dz)
- -- If this player is closer than the current closest, update the closest target
- if isFriendly(player.nickname) then
- print("Skipping friendly player ID: " .. player.nickname)
- else
- -- If this player is closer than the current closest, update the closest target
- if distance < closestDistance then
- closestDistance = distance
- playerPos1 = player.pos
- pInfo = {
- id = player.id,
- nickname = player.nickname,
- pos = playerPos1,
- distance = distance,
- }
- end
- end
- end
- end
- -- Wait for a short period (scanInterval) to perform the second scan
- os.sleep(scanInterval)
- if pInfo and playerPos1 then
- -- Second scan to calculate velocity
- local targetScan2 = radar.scanForPlayers(300)
- -- Find the same player in the second scan based on position
- local playerPos2 = nil
- for _, player in pairs(targetScan2) do
- if player and player.pos and player.nickname == pInfo.nickname then
- playerPos2 = player.pos
- break
- end
- end
- -- If both positions are available, calculate velocity
- if playerPos1 and playerPos2 then
- pInfo.pos = playerPos2
- pInfo.velocity = calculateVelocity(playerPos1, playerPos2, scanInterval)
- else
- pInfo.velocity = {x=0, y=0, z=0} -- Default velocity if we cannot calculate it
- end
- print(textutils.serialize(pInfo.pos))
- -- Aim the cannon at the target using the new position and velocity
- if pInfo.pos and pInfo.velocity then
- aimCannon({x=pInfo.pos[1], y = pInfo.pos[2], z = pInfo.pos[3]}, pInfo.velocity, shipPos.x, shipPos.y, shipPos.z)
- locked = true
- else
- locked = false
- end
- else
- locked = false
- end
- os.sleep()
- end
- end
- local function autoFire()
- while true do
- if locked then
- print("locked")
- end
- if locked and auto then
- print("firing")
- cannon.fire()
- end
- sleep(0.2)
- end
- end
- parallel.waitForAny(
- main,
- autoFire
- )
Advertisement
Add Comment
Please, Sign In to add comment