MegiRicky

AAPinfantryLock.lua

Sep 29th, 2024 (edited)
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.28 KB | None | 0 0
  1. local radar = peripheral.find("sp_radar")
  2. local cannon = peripheral.find("cbcmf_compact_cannon_mount")
  3. if not(cannon) then cannon = peripheral.find("cbc_cannon_mount") end
  4.  
  5. local projectileSpeed = 240
  6. local g = 0.05
  7. local vm
  8. local shipPos
  9. local pInfo
  10. local scanInterval = 0.05
  11. local locked = false
  12.  
  13. local friendlyIDs
  14. local friendlyIDFile = "friendly_ids.txt"
  15.  
  16. print("Do you want to enable auto fire, 'yes' or 'no', default: yes")
  17. local auto = io.read()
  18.  
  19. if auto == "yes" then
  20. auto = true
  21. elseif auto == "no" then
  22. auto = false
  23. else
  24. auto = true
  25. end
  26.  
  27. local function readFriendlyIDs(filename)
  28. if not fs.exists(filename) then
  29. print("Friendly IDs file '" .. filename .. "' does not exist. Creating it.")
  30. local file = fs.open(filename, "w")
  31. print("Input friendly IDs (player names) separated by commas:")
  32. local ids = io.read()
  33. file.write(ids)
  34. file.close()
  35. else
  36. print("Do you want to edit the config? (yes/no),press enter to skip")
  37. local editChoice = io.read()
  38. if editChoice == "" then
  39. editChoice = "no"
  40. end
  41. if editChoice:lower() == "yes" then
  42. if fs.exists(filename) then
  43. fs.delete(filename)
  44. print("Friendly IDs file '" .. filename .. "' does not exist. Creating it.")
  45. local file = fs.open(filename, "w")
  46. print("Input friendly IDs (player names) separated by commas:")
  47. local ids = io.read()
  48. file.write(ids)
  49. file.close()
  50. end
  51. end
  52.  
  53.  
  54. end
  55.  
  56.  
  57. local file = fs.open(filename, "r")
  58. local friendlyIDs = {}
  59. if file then
  60. local line = file.readLine()
  61. while line do
  62. print("Line read: " .. line) -- Debug print for the line
  63.  
  64. -- Split the line by commas and insert each name into the friendlyIDs table
  65. for name in string.gmatch(line, '([^,]+)') do
  66. print("Friendly ID (Name): " .. name) -- Debug print for each name
  67. table.insert(friendlyIDs, name) -- Store the player name
  68. end
  69. line = file.readLine() -- Read the next line
  70. end
  71. file.close()
  72. end
  73. return friendlyIDs
  74. end
  75.  
  76. -- Function to check if a player ID is friendly
  77. local function isFriendly(playerID)
  78. for _, friendlyID in ipairs(friendlyIDs) do
  79. if playerID == friendlyID then
  80. return true
  81. end
  82. end
  83. return false
  84. end
  85.  
  86. -- Read friendly IDs from the file
  87. friendlyIDs = readFriendlyIDs(friendlyIDFile)
  88. print("Friendly IDs: " .. textutils.serialize(friendlyIDs)) -- Debug print
  89.  
  90. if cannon.assemble() then
  91. print("asseble failed, press enter to contiue")
  92. io.read()
  93. end
  94.  
  95. local function calculateVelocity(pos1, pos2, dt)
  96. local vx = (pos2[1] - pos1[1]) / dt
  97. local vy = (pos2[2] - pos1[2]) / dt
  98. local vz = (pos2[3] - pos1[3]) / dt
  99. return {x=vx, y=0, z=vz}
  100. end
  101.  
  102. local function findRelativeAngle(targetYaw,targetPitch)
  103. rot = ship.getQuaternion()
  104. cacheYaw = math.pi - math.rad(targetYaw)
  105. cachePitch = - math.rad(targetPitch)
  106. rotMatAdj11 = 1 - 2*(rot.x^2 + rot.y^2)
  107. rotMatAdj12 = 2*(rot.z * rot.x + rot.y * rot.w)
  108. rotMatAdj13 = 2*(rot.z * rot.y - rot.x * rot.w)
  109. rotMatAdj21 = 2*(rot.z * rot.x - rot.y * rot.w)
  110. rotMatAdj22 = 1 - 2*(rot.z^2 + rot.y^2)
  111. rotMatAdj23 = 2*(rot.x * rot.y + rot.z * rot.w)
  112. rotMatAdj31 = 2*(rot.z * rot.y + rot.x * rot.w)
  113. rotMatAdj32 = 2*(rot.x * rot.y - rot.z * rot.w)
  114. rotMatAdj33 = 1 - 2*(rot.z^2 + rot.x^2)
  115.  
  116. rotMatTGT11 = math.cos(cacheYaw) * math.cos(cachePitch)
  117. rotMatTGT21 = math.sin(cacheYaw) * math.cos(cachePitch)
  118. rotMatTGT31 = - math.sin(cachePitch)
  119.  
  120. rotMatRSLT11 = rotMatAdj11 * rotMatTGT11 + rotMatAdj12 * rotMatTGT21 + rotMatAdj13 * rotMatTGT31
  121. rotMatRSLT21 = rotMatAdj21 * rotMatTGT11 + rotMatAdj22 * rotMatTGT21 + rotMatAdj23 * rotMatTGT31
  122. rotMatRSLT31 = rotMatAdj31 * rotMatTGT11 + rotMatAdj32 * rotMatTGT21 + rotMatAdj33 * rotMatTGT31
  123.  
  124. turretYaw = math.atan2(rotMatRSLT21, rotMatRSLT11)
  125. barrelPitch = math.asin(-rotMatRSLT31)
  126. return math.deg(-turretYaw),math.deg(-barrelPitch)
  127. end
  128.  
  129. local function calculateRange(angle, u, cd, g, c_est, projectileSpeed)
  130. local radians = math.rad(angle)
  131. local u = projectileSpeed/20
  132. local part1 = u * math.cos(radians) / math.log(cd)
  133. local part2 = ((g * cd) / (g * cd + (1 - cd) * u * math.sin(radians))) ^ (2 + c_est * projectileSpeed * math.sin(radians)) - 1
  134. local XR = part1 * part2
  135.  
  136. return XR
  137. end
  138.  
  139. local function findBestPitch(targetX, targetY, targetZ, sourceX, sourceY, sourceZ, initialVelocity, g, cd, c_est, projectileSpeed)
  140. local bestLowPitch = nil
  141. local bestHighPitch = nil
  142. local bestLowDistance = math.huge
  143. local bestHighDistance = math.huge
  144. local targetDistance = math.sqrt((targetX - sourceX)^2 + (targetZ - sourceZ)^2)
  145.  
  146. for pitch = 0, 70, 0.01 do -- Iterate over pitch angles
  147. local calculatedRange = calculateRange(pitch, initialVelocity, cd, g, c_est, projectileSpeed)
  148. local distanceDifference = math.abs(calculatedRange - targetDistance)
  149.  
  150. -- Find the low-angle solution
  151. if pitch <= 30 then
  152. if distanceDifference < bestLowDistance then
  153. bestLowDistance = distanceDifference
  154. bestLowPitch = pitch
  155. end
  156. -- Find the high-angle solution
  157. elseif pitch > 30 then
  158. if distanceDifference < bestHighDistance then
  159. bestHighDistance = distanceDifference
  160. bestHighPitch = pitch
  161. end
  162. end
  163. end
  164.  
  165. -- If the target is within 400 blocks, prioritize the low-angle solution
  166. if targetDistance <= 830 then
  167. return bestLowPitch or bestLowPitch
  168. else
  169. return bestHighPitch or bestLowPitch
  170. end
  171. end
  172.  
  173. local function aimCannon(targetPos, targetVel, sourceX, sourceY, sourceZ)
  174. if sourceX and sourceY and sourceZ then
  175. local dx = targetPos.x - sourceX
  176. local dy = targetPos.y - sourceY
  177. local dz = targetPos.z - sourceZ
  178. local distance = math.sqrt(dx * dx + dy * dy + dz * dz)
  179.  
  180. local estimateTime = distance / (projectileSpeed) -- assuming velocity units and time units need adjustment
  181. local estimateX = targetPos.x + targetVel.x * estimateTime
  182. local estimateY = targetPos.y + targetVel.y * estimateTime
  183. local estimateZ = targetPos.z + targetVel.z * estimateTime
  184.  
  185. local dx = estimateX - sourceX
  186. local dy = estimateY - sourceY
  187. local dz = estimateZ - sourceZ
  188.  
  189. local horizontalDistance = math.sqrt(dx * dx + dz * dz)
  190. local pitch = math.deg(math.atan2(dy, horizontalDistance))
  191.  
  192. local yaw = math.deg(math.atan2(-dx, dz))
  193. yaw = (yaw + 180) % 360
  194.  
  195. -- Adjust pitch based on distance adjustments
  196. pitch = pitch + findBestPitch(estimateX, estimateY, estimateZ, sourceX, sourceY, sourceZ, projectileSpeed, g, 0.99, 0.0028, projectileSpeed)
  197.  
  198. local shipRoll = math.deg(ship.getRoll())
  199. local shipYaw = math.deg(ship.getYaw())
  200. if shipYaw < 0 then shipYaw = shipYaw + 360 end
  201. local shipPitch = math.deg(ship.getPitch())
  202.  
  203. local requiredRelativeYaw = yaw - shipYaw
  204. if requiredRelativeYaw > 180 then
  205. requiredRelativeYaw = requiredRelativeYaw - 360
  206. elseif requiredRelativeYaw < -180 then
  207. requiredRelativeYaw = requiredRelativeYaw + 360
  208. end
  209.  
  210. local requiredRelativeYaw,requiredRelativePitch = findRelativeAngle(yaw,pitch)
  211. if math.abs(math.abs(requiredRelativePitch) - math.abs(cannon.getPitch())) > 0.2 then
  212. cannon.setPitch(requiredRelativePitch)
  213. end
  214. if math.abs(requiredRelativeYaw - cannon.getYaw()) > 0.1 then
  215. cannon.setYaw(requiredRelativeYaw)
  216. end
  217. end
  218. end
  219.  
  220. local function main()
  221. while true do
  222. shipPos = ship.getWorldspacePosition()
  223.  
  224. -- First scan for players
  225. local targetScan1 = radar.scanForPlayers(100)
  226.  
  227. -- Variables to hold the closest target data
  228. local closestPlayer = nil
  229. local closestDistance = math.huge
  230. local playerPos1 = nil
  231. local pInfo = nil
  232.  
  233. -- Loop through each player detected in the first scan
  234. for _, player in pairs(targetScan1) do
  235. if player and player.pos then
  236. local dx = player.pos[1] - shipPos.x
  237. local dy = player.pos[2] - shipPos.y
  238. local dz = player.pos[3] - shipPos.z
  239. local distance = math.sqrt(dx * dx + dy * dy + dz * dz)
  240.  
  241. -- If this player is closer than the current closest, update the closest target
  242. if isFriendly(player.nickname) then
  243. print("Skipping friendly player ID: " .. player.nickname)
  244. else
  245. -- If this player is closer than the current closest, update the closest target
  246. if distance < closestDistance then
  247. closestDistance = distance
  248. playerPos1 = player.pos
  249. pInfo = {
  250. id = player.id,
  251. nickname = player.nickname,
  252. pos = playerPos1,
  253. distance = distance,
  254. }
  255. end
  256. end
  257. end
  258. end
  259.  
  260. -- Wait for a short period (scanInterval) to perform the second scan
  261. os.sleep(scanInterval)
  262.  
  263. if pInfo and playerPos1 then
  264. -- Second scan to calculate velocity
  265. local targetScan2 = radar.scanForPlayers(300)
  266.  
  267. -- Find the same player in the second scan based on position
  268. local playerPos2 = nil
  269. for _, player in pairs(targetScan2) do
  270. if player and player.pos and player.nickname == pInfo.nickname then
  271. playerPos2 = player.pos
  272. break
  273. end
  274. end
  275.  
  276. -- If both positions are available, calculate velocity
  277. if playerPos1 and playerPos2 then
  278. pInfo.pos = playerPos2
  279. pInfo.velocity = calculateVelocity(playerPos1, playerPos2, scanInterval)
  280. else
  281. pInfo.velocity = {x=0, y=0, z=0} -- Default velocity if we cannot calculate it
  282. end
  283. print(textutils.serialize(pInfo.pos))
  284. -- Aim the cannon at the target using the new position and velocity
  285. if pInfo.pos and pInfo.velocity then
  286. aimCannon({x=pInfo.pos[1], y = pInfo.pos[2], z = pInfo.pos[3]}, pInfo.velocity, shipPos.x, shipPos.y, shipPos.z)
  287. locked = true
  288. else
  289. locked = false
  290. end
  291. else
  292. locked = false
  293. end
  294. os.sleep()
  295. end
  296. end
  297.  
  298. local function autoFire()
  299. while true do
  300. if locked then
  301. print("locked")
  302. end
  303. if locked and auto then
  304. print("firing")
  305. cannon.fire()
  306. end
  307. sleep(0.2)
  308. end
  309. end
  310.  
  311. parallel.waitForAny(
  312. main,
  313. autoFire
  314. )
Advertisement
Add Comment
Please, Sign In to add comment