t8rt0t

spaceeyeblib.lua

Jul 8th, 2024
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.49 KB | None | 0 0
  1. ---
  2. --- Original creator of formulas: @sashafiesta on Discord
  3. --- Original creator of python adaptation: @malexy on Discord
  4. --- Optimized and translated python code to lua: SpaceEye. (https://gist.github.com/SuperSpaceEye/c33443213605d1bf35f81737c9058dc2)
  5. --- Some lua optimizations: Autist69420
  6. ---
  7.  
  8. -- Simple micro-optimizations for better performance
  9. local table_insert = table.insert
  10. local rad, sin, cos, log, abs, min, pow = math.rad, math.sin, math.cos, math.log, math.abs, math.min, math.pow
  11. local tu = table.unpack
  12.  
  13. local function linspace(start, end_, num)
  14. local linspaced = {}
  15. if num == 0 then return linspaced end
  16. if num == 1 then
  17. table_insert(linspaced, start)
  18. return linspaced
  19. end
  20.  
  21. local delta = (end_ - start) / (num - 1)
  22.  
  23. for i = 0, num-2 do
  24. table_insert(linspaced, start+delta*i)
  25. end
  26. table_insert(linspaced, end_)
  27.  
  28. return linspaced
  29. end
  30.  
  31. local function range(start, stop, step)
  32. step = step or 1
  33. local pos = start
  34. return function ()
  35. if step > 0 then
  36. if pos >= stop then return nil end
  37. else
  38. if pos <= stop then return nil end
  39. end
  40. local lpos = pos
  41. pos = pos + step
  42. return lpos
  43. end
  44. end
  45.  
  46. local function flinspace(start, stop, num_elements, min, max)
  47. local items = linspace(math.max(start, min), math.min(stop, max), num_elements)
  48. local pos = 0
  49. return function() -- simple iterator
  50. pos = pos + 1
  51. return items[pos]
  52. end
  53. end
  54.  
  55. local function get_root(d, from_end)
  56. if from_end then
  57. for i = #d-1, 1, -1 do
  58. if d[i][1] > d[i+1][1] then return d[i+1] end
  59. end
  60. return d[1]
  61. else
  62. for i = 2, #d, 1 do
  63. if d[i-1][1] < d[i][1] then return d[i-1] end
  64. end
  65. return d[#d]
  66. end
  67. end
  68.  
  69. local function time_in_air(y0, y, Vy, gravity, max_steps)
  70. local t = 0
  71. local t_below = 9999999
  72.  
  73. gravity = gravity or 0.05
  74. max_steps = max_steps or 1000000
  75.  
  76. if y0 <= y then
  77. local y0p
  78. while t < max_steps do
  79. y0p = y0
  80. y0 = y0 + Vy
  81. Vy = 0.99 * Vy - gravity
  82. t = t + 1
  83.  
  84. if y0 > y then
  85. t_below = t-1
  86. break
  87. end
  88.  
  89. if y0 - y0p < 0 then
  90. return -1, -1
  91. end
  92. end
  93. end
  94.  
  95. while t < max_steps do
  96. y0 = y0 + Vy
  97. Vy = 0.99 * Vy - gravity
  98. t = t + 1
  99.  
  100. if y0 <= y then return t_below, t end
  101. end
  102.  
  103. return t_below, -1
  104. end
  105.  
  106. local function get_min(array)
  107. local min_delta_t = array[1][1]
  108. local pitch_ = 0;
  109. local airtime_ = 0;
  110. for i = 1, #array do
  111. if min_delta_t > array[i][1] then
  112. min_delta_t = array[i][1]
  113. pitch_ = array[i][2]
  114. airtime_ = array[i][3]
  115. end
  116. end
  117. return min_delta_t, pitch_, airtime_
  118. end
  119.  
  120. local function try_pitch(tried_pitch, initial_speed,
  121. length, distance, cannon, target, gravity, max_steps)
  122. gravity = gravity or 0.05
  123. max_steps = max_steps or 1000000
  124.  
  125. local tp_rad = rad(tried_pitch)
  126.  
  127. local Vw = cos(tp_rad) * initial_speed
  128. local Vy = sin(tp_rad) * initial_speed
  129.  
  130. local x_coord_2d = length * cos(tp_rad)
  131.  
  132. if Vw == 0 then return nil, false end
  133. local part = 1 - (distance - x_coord_2d) / (100 * Vw)
  134. if part <= 0 then return nil, false end
  135. local horizontal_time_to_target = abs(log(part) / (-0.010050335853501))
  136.  
  137. local y_coord_of_end_barrel = cannon[2] + sin(tp_rad) * length
  138.  
  139. local t_below, t_above = time_in_air(y_coord_of_end_barrel, target[2], Vy, gravity, max_steps)
  140.  
  141. if t_below < 0 then return nil, false end
  142.  
  143. local delta_t = min(
  144. abs(horizontal_time_to_target - t_below),
  145. abs(horizontal_time_to_target - t_above)
  146. )
  147.  
  148. return {delta_t, tried_pitch, delta_t + horizontal_time_to_target}, true
  149. end
  150.  
  151. local function try_pitches(iter, ...)
  152. local delta_times = {}
  153. for pitch in iter do
  154. local items, is_successful = try_pitch(pitch, ...)
  155. if is_successful then table.insert(delta_times, items) end
  156. end
  157. return delta_times
  158. end
  159.  
  160. -- Required parameters:
  161. -- cannon = table of three numbers: x, y, z of cannon
  162. -- target = same as cannon but for target
  163. -- initial_speed = speed in m/s
  164. -- length = length of a cannon
  165. -- Optional parameters:
  166. -- max_steps = maximum number of steps program will simulate projectile before declaring it unreachable
  167. -- max_delta_t_error = maximum difference between horizontal and vertical times to target before declaring target impossible to hit. Only matters if check_impossible is enabled
  168. -- amin = minimum cannon angle
  169. -- amax = maximum cannon angle
  170. -- gravity = x m/tick
  171. -- num_iterations = number of refining steps after roughly calculating angle
  172. -- num_elements = number of elements to test during refining stage
  173. -- check_impossible = does additional check for targets that are impossible to hit
  174. local function calculate_pitch(cannon, target, initial_speed, length,
  175. optional)
  176. local max_steps, max_delta_t_error, amin, amax, gravity, num_iterations, num_elements, check_impossible
  177. optional = optional or {}
  178. max_steps = optional.max_steps or optional[1] or 100000
  179. max_delta_t_error = optional.max_delta_t_error or optional[2] or 1
  180. amin = optional.amin or optional[3] or -30
  181. amax = optional.amax or optional[4] or 60
  182. gravity = optional.gravity or optional[5] or 0.05
  183. num_iterations = optional.num_iterations or optional[6] or 5
  184. num_elements = optional.num_elements or optional[7] or 20
  185. check_impossible = optional.check_impossible or optional[8] or true
  186.  
  187. local Dx, Dz = cannon[1] - target[1], cannon[3] - target[3]
  188. local distance = math.sqrt(Dx * Dx + Dz * Dz)
  189.  
  190. local delta_times = try_pitches(range(amax, amin-1, -1), initial_speed, length, distance, cannon, target, gravity, max_steps)
  191. if #delta_times == 0 then return {-1, -1, -1}, {-1, -1, -1} end
  192.  
  193. local dT1, p1, at1 = tu(get_root(delta_times, false))
  194. local dT2, p2, at2 = tu(get_root(delta_times, true))
  195.  
  196. local c1 = true
  197. local c2 = not p1 == p2
  198. local same_res = p1 == p2
  199.  
  200. local dTs1, dTs2
  201.  
  202. for i in range(0, num_iterations) do
  203. if c1 then dTs1 = try_pitches(flinspace(p1-pow(10,-i), p1+pow(10,-i), num_elements, amin, amax), initial_speed, length, distance, cannon, target, gravity, max_steps) end
  204. if c2 then dTs2 = try_pitches(flinspace(p2-pow(10,-i), p2+pow(10,-i), num_elements, amin, amax), initial_speed, length, distance, cannon, target, gravity, max_steps) end
  205.  
  206. if c1 and #dTs1 == 0 then c1=false end
  207. if c2 and #dTs2 == 0 then c2=false end
  208.  
  209. if not c1 and not c2 then return {-1, -1, -1}, {-1, -1, -1} end
  210.  
  211. if c1 then dT1, p1, at1 = get_min(dTs1) end
  212. if c2 then dT2, p2, at2 = get_min(dTs2) end
  213. end
  214.  
  215. if same_res then dT2, p2, at2 = dT1, p1, at1 end
  216.  
  217. local r1, r2 = {dT1, p1, at1}, {dT2, p2, at2}
  218. if check_impossible and dT1 > max_delta_t_error then r1 = {-1, -1, -1} end
  219. if check_impossible and dT2 > max_delta_t_error then r2 = {-1, -1, -1} end
  220.  
  221. return r1, r2
  222. end
  223.  
  224. local function calculate_yaw(Dx, Dz, direction)
  225. local yaw
  226. if Dx ~= 0 then
  227. yaw = math.atan(Dz/Dx) * 180/math.pi
  228. else
  229. yaw = 90
  230. end
  231.  
  232. if Dx >= 0 then
  233. yaw = yaw + 180
  234. end
  235.  
  236. local dirs = {90, 180, 270, 0}
  237. return (yaw + dirs[direction]) % 360
  238. end
  239.  
  240. local function ballistics_to_target(cannon, target, power, direction, R1, R2, length)
  241. local directions = {north=1, west=2, south=3, east=4}
  242. direction = directions[direction]
  243. if direction == nil then error("Invalid direction") end
  244.  
  245. local Dx, Dz = cannon[1] - target[1], cannon[3] - target[3]
  246.  
  247. local r1, r2 = calculate_pitch(cannon, target, power, length)
  248. local yaw = calculate_yaw(Dx, Dz, direction)
  249.  
  250. local rt = {}
  251. rt.yaw = yaw
  252. rt.yaw_time = yaw * 20 / (0.75 * R1)
  253.  
  254. for k, v in pairs({[1]=r1, [2]=r2}) do
  255. local t = {pitch=-1, pitch_time=-1, airtime=-1, fuze_time=-1}
  256. if v[1] ~= -1 then
  257. t.delta_t = v[1]
  258. t.pitch = v[2]
  259. t.airtime = v[3]
  260. t.pitch_time = t.pitch * 20 / (0.75 * R2)
  261. t.precision = 1 - t.delta_t / t.airtime
  262. end
  263. table.insert(rt, k, t)
  264. end
  265.  
  266. return rt
  267. end
  268.  
  269.  
  270. return function(cannonCoord,targetCoord,powderCharges,cannonDir,cannonLength)
  271. local rt = ballistics_to_target(cannonCoord,targetCoord,powderCharges,cannonDir,1,1,cannonLength)
  272. return rt.yaw,rt[1].pitch,rt[2].pitch,rt
  273. end
  274.  
  275. -- print("For the cannon coordinates, please input the coordinates of the cannon mount.")
  276.  
  277. -- cannonCoord = {}
  278. -- print("x coord of cannon : ")
  279. -- table_insert(cannonCoord, tonumber(io.read()))
  280. -- print("y coord of cannon : ")
  281. -- table_insert(cannonCoord, tonumber(io.read())+2)
  282. -- print("z coord of cannon : ")
  283. -- table_insert(cannonCoord, tonumber(io.read()))
  284.  
  285.  
  286. -- targetCoord = {}
  287. -- print("x coord of target : ")
  288. -- table_insert(targetCoord, tonumber(io.read()))
  289. -- print("y coord of target : ")
  290. -- table_insert(targetCoord, tonumber(io.read()))
  291. -- print("z coord of target : ")
  292. -- table_insert(targetCoord, tonumber(io.read()))
  293.  
  294. -- print("Number of powder charges (int) : ")
  295. -- powderCharges = tonumber(io.read())
  296.  
  297. -- print("What is the standart direction of the cannon ? (north, south, east, west)")
  298. -- directionOfCannon = io.read()
  299.  
  300. -- print("What is the RPM of the yaw axis ?")
  301. -- yawRPM = tonumber(io.read())
  302. -- print("What is the RPM of the pitch axis ?")
  303. -- pitchRPM = tonumber(io.read())
  304.  
  305. -- print("What is the length of the cannon ? (From the block held by the mount to the tip of the cannon, the held block excluded) ")
  306. -- cannonLength = tonumber(io.read())
  307.  
  308. -- local cannonCoord = {323, 73+2, 32}
  309. -- local targetCoord = {350, 84, -113}
  310. -- local powderCharges = 8
  311. -- local directionOfCannon = "north"
  312. -- local yawRPM = 1
  313. -- local pitchRPM = 1
  314. -- local cannonLength = 31
  315.  
  316.  
  317. -- local rt = ballistics_to_target(
  318. -- cannonCoord,
  319. -- targetCoord,
  320. -- powderCharges,
  321. -- directionOfCannon,
  322. -- yawRPM,
  323. -- pitchRPM,
  324. -- cannonLength
  325. -- )
  326.  
  327. -- print("Yaw is ", rt.yaw)
  328. -- print("With the yaw axis set at ", yawRPM, " rpm, the cannon must take ", rt.yaw_time, " ticks of turning the yaw axis.")
  329.  
  330. -- if rt[1].pitch ~= -1 then
  331. -- print("\nHigh shot:")
  332. -- print("Pitch is ", rt[1].pitch)
  333. -- print("Airtime is", rt[1].airtime, "ticks")
  334. -- print("With the pitch axis set at ", pitchRPM, " rpm, the cannon must take ", rt[1].pitch_time, " ticks of turning the pitch axis.")
  335. -- print("Precision: ", rt[1].precision)
  336. -- else
  337. -- print("\nHigh shot is impossible")
  338. -- end
  339.  
  340. -- if rt[2].pitch ~= -1 then
  341. -- print("\nLow shot:")
  342. -- print("Pitch is ", rt[2].pitch)
  343. -- print("Airtime is", rt[2].airtime, "ticks")
  344. -- print("With the pitch axis set at ", pitchRPM, " rpm, the cannon must take ", rt[2].pitch_time, " ticks of turning the pitch axis.")
  345. -- print("Precision: ", rt[2].precision)
  346. -- else
  347. -- print("\nLow shot is impossible")
  348. -- end
  349.  
  350. -- local args = {...}
  351.  
  352.  
  353. -- print(textutils.serialize({calculate_pitch({0,0,0},{tonumber(args[1]) or 1000,tonumber(args[2]) or 0,0},320,31)}))
  354. -- print(textutils.serialize({require("ballisticLib").getAngle(tonumber(args[1]) or 1000,tonumber(args[2]) or 0,320,0.1)}))
Advertisement
Add Comment
Please, Sign In to add comment