MegiRicky

strykerArtiellery

Jul 22nd, 2024 (edited)
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.00 KB | None | 0 0
  1. local radar = peripheral.find("sp_radar")
  2. local pitchMotor = peripheral.wrap("back")
  3. local yawMotor = peripheral.find("electric_motor")
  4. local reader = peripheral.find("blockReader")
  5. local modem = peripheral.wrap("front")
  6.  
  7. local cannonPitchCh = 0
  8. local cannonYawCh = 1
  9. local pitchControl = "stop"
  10. local yawControl = "stop"
  11. print("What mode you want, type 0 for mouse aim, type 1 for controller control")
  12. local mode = tonumber(io.read())
  13.  
  14. if modem then
  15. modem.open(cannonPitchCh)
  16. modem.open(cannonYawCh)
  17. end
  18.  
  19. local function round(num)
  20. return math.floor(num * 100 + 0.5) / 100
  21. end
  22.  
  23. local function displayInChunks(text, chunkSize)
  24. local length = #text
  25. local start = 1
  26. while start <= length do
  27. local chunk = text:sub(start, start + chunkSize - 1)
  28. print(chunk)
  29. start = start + chunkSize
  30. if start <= length then
  31. print("Press any key to continue...")
  32. os.pullEvent("key")
  33. end
  34. end
  35. end
  36.  
  37. -- Initialize variables
  38. local yaw = 0
  39. local pitch = 0
  40. local player = {}
  41. local lookAngle = {}
  42. local sourceX, sourceY, sourceZ = 0, 0, 0
  43. local x, y, z = 0, 0, 0
  44. local pitch_tolerance = 1.2
  45. local yaw_tolerance = 1.2
  46. local adjustedPitch = 0
  47. yawMotor.setSpeed(0)
  48. pitchMotor.setSpeed(0)
  49.  
  50. local lastYawSpeed = 0
  51. local lastPitchSpeed = 0
  52.  
  53. local function toDegrees(radians)
  54. return radians * (180 / math.pi)
  55. end
  56.  
  57. -- Function to convert degrees to a bearing
  58. local function toBearing(degrees)
  59. local bearing = degrees % 360
  60. if bearing < 0 then
  61. bearing = 360 + bearing
  62. end
  63. return bearing
  64. end
  65.  
  66. local function couple()
  67. while true do
  68. redstone.setOutput("bottom", true)
  69. sleep(1)
  70. redstone.setOutput("bottom", false)
  71. sleep(10)
  72. end
  73. end
  74. redstone.setOutput("right", false)
  75. redstone.setOutput("left", false)
  76. sleep(0.1)
  77.  
  78. local function mouseAim()
  79. while true do
  80. redstone.setOutput("left", true)
  81. local result = radar.scanForPlayers(20)
  82. if #result > 0 then
  83. player = result[1]
  84. lookAngle = player.look_angle
  85. local playerPos = player.pos
  86. local lookVector = {x = lookAngle[1], y = lookAngle[2], z = lookAngle[3]}
  87.  
  88. -- Calculate target position
  89. local distance = 50 -- Distance to look ahead
  90. local targetPos = {
  91. x = playerPos[1] + lookVector.x * distance,
  92. y = playerPos[2] + lookVector.y * distance,
  93. z = playerPos[3] + lookVector.z * distance
  94. }
  95.  
  96. -- Get source position
  97. local sourcePos = ship.getWorldspacePosition()
  98. sourceX, sourceY, sourceZ = sourcePos.x, sourcePos.y + 2, sourcePos.z
  99.  
  100. -- Calculate required yaw and pitch to aim at the target position
  101. local dx = targetPos.x - sourceX
  102. local dy = targetPos.y - sourceY
  103. local dz = targetPos.z - sourceZ
  104.  
  105. -- Compute yaw (true bearing)
  106. yaw = math.deg(math.atan2(-dx, dz))
  107. if yaw < 0 then
  108. yaw = yaw + 360
  109. end
  110.  
  111. -- Compute pitch
  112. pitch = math.deg(math.atan2(dy, math.sqrt(dx * dx + dz * dz)))
  113.  
  114. -- Get ship rotation matrix
  115. local matrix = ship.getRotationMatrix()
  116. local forwardX = matrix[1][3]
  117. local forwardZ = matrix[3][3]
  118. local yawRadians = math.atan2(-forwardX, forwardZ)
  119. local yawDegrees = toDegrees(yawRadians)
  120. local yawBearing = toBearing(yawDegrees) - 180
  121. if yawBearing < 0 then
  122. yawBearing = yawBearing + 360
  123. end
  124.  
  125. -- Compensate for tank's roll and pitch
  126. local shipPitch = toDegrees(math.atan2(matrix[2][1], matrix[2][2])) * -1
  127. local shipRoll = toDegrees(-math.asin(matrix[2][3]))
  128.  
  129. currentYaw = reader.getBlockData().CannonYaw
  130. currentYaw = currentYaw - 180 + yawBearing
  131. currentPitch = reader.getBlockData().CannonPitch + shipPitch
  132.  
  133. -- Compensate for roll in pitch adjustment
  134. local yawTemp = reader.getBlockData().CannonYaw
  135. if yawTemp < 30 and yawTemp > 330 then
  136. adjustedPitch = pitch + shipRoll
  137. elseif yawTemp > 150 and yawTemp < 210 then
  138. adjustedPitch = pitch - shipRoll
  139. else
  140. adjustedPitch = pitch
  141. end
  142.  
  143.  
  144. -- Adjust yaw
  145. print("Player: " .. player.nickname)
  146. print("Required Yaw (True Bearing): " .. round(yaw) .. " degrees")
  147. print("Required Pitch: " .. round(adjustedPitch) .. " degrees")
  148. print("Current Pitch: " .. round(currentPitch) .. " degrees")
  149. print("Current Yaw: " .. round(currentYaw) .. " degrees")
  150. print("Ship Pitch: " .. round(shipPitch) .. " degrees")
  151. print("Ship Roll: " .. round(shipRoll) .. " degrees")
  152.  
  153. local deltaYaw = (yaw - currentYaw + 180) % 360 - 180
  154. print(deltaYaw)
  155. local yawSpeed = 0
  156. if math.abs(deltaYaw) > yaw_tolerance then
  157. if deltaYaw > 0 then
  158. print("Turning right")
  159. if math.abs(deltaYaw) > 30 then
  160. yawSpeed = -64
  161. elseif math.abs(deltaYaw) > 4 then
  162. yawSpeed = -32
  163. elseif math.abs(deltaYaw) < 4 then
  164. yawSpeed = -6
  165. end
  166. else
  167. print("Turning left")
  168. if math.abs(deltaYaw) > 30 then
  169. yawSpeed = 64
  170. elseif math.abs(deltaYaw) > 4 then
  171. yawSpeed = 32
  172. elseif math.abs(deltaYaw) < 4 then
  173. yawSpeed = 6
  174. end
  175. end
  176. end
  177. if yawSpeed ~= lastYawSpeed then
  178. yawMotor.setSpeed(yawSpeed)
  179. lastYawSpeed = yawSpeed
  180. end
  181.  
  182. -- Adjust pitch
  183. local deltaPitch = adjustedPitch - currentPitch
  184. local pitchSpeed = 0
  185. if math.abs(deltaPitch) > pitch_tolerance then
  186. if deltaPitch > 0 then
  187. print("Adjusting pitch up")
  188. if math.abs(deltaPitch) > 30 then
  189. pitchSpeed = -64
  190. elseif math.abs(deltaPitch) > 4 then
  191. pitchSpeed = -32
  192. elseif math.abs(deltaPitch) < 4 then
  193. pitchSpeed = -6
  194. end
  195. else
  196. print("Adjusting pitch down")
  197. if math.abs(deltaPitch) > 10 then
  198. pitchSpeed = 64
  199. elseif math.abs(deltaPitch) > 4 then
  200. pitchSpeed = 32
  201. elseif math.abs(deltaPitch) < 4 then
  202. pitchSpeed = 6
  203. end
  204. end
  205. end
  206. if pitchSpeed ~= lastPitchSpeed then
  207. pitchMotor.setSpeed(pitchSpeed)
  208. lastPitchSpeed = pitchSpeed
  209. end
  210. else
  211. print("No players detected.")
  212. end
  213. sleep(0.05)
  214. redstone.setOutput("bottom", false)
  215. end
  216. end
  217.  
  218. local function listenForCoordinates()
  219. while true do
  220. local event, side, senderChannel, replyChannel, message, senderDistance = os.pullEvent("modem_message")
  221. if senderChannel == cannonPitchCh then
  222. pitchControl = message
  223. elseif senderChannel == cannonYawCh then
  224. yawControl = message
  225. end
  226. end
  227. end
  228.  
  229. local function controller()
  230. while true do
  231. redstone.setOutput("left", true)
  232. print("Controller loop started")
  233. print("pitchControl: "..pitchControl)
  234. print("yawControl: "..yawControl)
  235. if pitchControl == "up" then
  236. pitchSpeed = -16
  237. elseif pitchControl == "down" then
  238. pitchSpeed = 16
  239. else
  240. pitchSpeed = 0
  241. end
  242. if pitchSpeed ~= lastPitchSpeed then
  243. pitchMotor.setSpeed(pitchSpeed)
  244. lastPitchSpeed = pitchSpeed
  245. end
  246. if yawControl == "left" then
  247. yawSpeed = 16
  248. print("Turning left")
  249. elseif yawControl == "right" then
  250. yawSpeed = -16
  251. print("Turning right")
  252. else
  253. print("stoping")
  254. yawSpeed = 0
  255. end
  256. if yawSpeed ~= lastYawSpeed then
  257. yawMotor.setSpeed(yawSpeed)
  258. lastYawSpeed = yawSpeed
  259. end
  260. sleep()
  261. end
  262. end
  263.  
  264. if mode == 0 then
  265. parallel.waitForAny(couple, mouseAim)
  266. elseif mode == 1 then
  267. parallel.waitForAny(listenForCoordinates, controller)
  268. else
  269. error("mode not valid")
  270. end
  271. redstone.setOutput("right", false)
  272.  
  273. --pastebin get qBPs42Q4 strykerArtiellery
Advertisement
Add Comment
Please, Sign In to add comment