@antti5/ship-tool-common 1.0.5 → 1.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,5 @@
1
1
  import Vector3 from './Vector3.js';
2
2
 
3
-
4
3
  const G = 9.8;
5
4
  const G_VEC = new Vector3(0, -G, 0);
6
5
  const GAS_CST_R = 8.31447; // N.m / (mol.K)
@@ -10,7 +9,6 @@ const STATIC_PRESSURE_LVL0 = 101325.0; // Pa
10
9
  const TEMPERATURE_LAPSE_RATE_LVL0 = -0.0065; // K / m
11
10
  const PRESSURE_CST_H0 = G * AIR_MOLAR_MASS / (GAS_CST_R * TEMPERATURE_LAPSE_RATE_LVL0);
12
11
 
13
-
14
12
  class BulletSim {
15
13
  constructor(mass, diam, dragCoeff, vel, pos, dir) {
16
14
  this.mass = mass;
@@ -36,9 +34,10 @@ class BulletSim {
36
34
  const drag = this.area * 0.5 * this.dragCoeff * airDensity * this.vel.sqnorm();
37
35
  const acceleration = this.vel.normalized().mul(-drag / this.mass).add(G_VEC);
38
36
 
39
- /* Start with a time step of 640 ms, and when shell is about to land start halving
37
+ /* Start with a time step of 160 ms, and when shell is about to land start halving
40
38
  the time step down to 10 ms. This ends the trajectory very close to the surface. */
41
- let dt = 0.64;
39
+
40
+ let dt = 0.16;
42
41
  while (this.pos.y + this.vel.y * dt * 2 < 0 && dt > 0.01)
43
42
  dt = Math.max(dt / 2, 0.01);
44
43
 
@@ -48,5 +47,4 @@ class BulletSim {
48
47
  }
49
48
  }
50
49
 
51
-
52
50
  export default BulletSim;
@@ -2,7 +2,6 @@ import Vector3 from './Vector3.js';
2
2
  import BulletSim from './BulletSim.js';
3
3
  import TrajectoryPoint from './TrajectoryPoint.js';
4
4
 
5
-
6
5
  class Trajectory {
7
6
 
8
7
  constructor(
@@ -1,6 +1,5 @@
1
1
  import Vector3 from './Vector3.js';
2
2
 
3
-
4
3
  const BALLISTIC_FLATTENING = 1.0;
5
4
 
6
5
  class TrajectoryPoint {
@@ -119,5 +119,4 @@ class Vector3 {
119
119
  }
120
120
  }
121
121
 
122
-
123
122
  export default Vector3;
@@ -0,0 +1,408 @@
1
+ # Below is the original version of the trajectory calculation, taken from: https://pastebin.com/GXUt7BMJ
2
+
3
+ # Everything here is reverse from the .exe version 5.16.0
4
+ # Clean versions of functions are proposed where the assembly was not optimized
5
+ # (of course it's ASM translated in C++ then translated in Python)
6
+ # by ThiSpawn on 25/12/2016, fuck christmas
7
+
8
+ from math import exp, log, pi, atan2, sqrt, sin, cos
9
+ from copy import copy
10
+
11
+
12
+ def isclose(a, b, rel_tol=1e-09, abs_tol=0.0):
13
+ return abs(a-b) <= max(rel_tol * max(abs(a), abs(b)), abs_tol)
14
+
15
+
16
+ class Vector3(object):
17
+ def __init__(self, x=0.0, y=0.0, z=0.0):
18
+ self.x = x
19
+ self.y = y
20
+ self.z = z
21
+
22
+ def __iadd__(self, other):
23
+ self.x += other.x
24
+ self.y += other.y
25
+ self.z += other.z
26
+ return self
27
+
28
+ def __add__(self, other):
29
+ return Vector3(
30
+ self.x + other.x,
31
+ self.y + other.y,
32
+ self.z + other.z)
33
+
34
+ def __isub__(self, other):
35
+ self.x -= other.x
36
+ self.y -= other.y
37
+ self.z -= other.z
38
+ return self
39
+
40
+ def __sub__(self, other):
41
+ return Vector3(
42
+ self.x - other.x,
43
+ self.y - other.y,
44
+ self.z - other.z)
45
+
46
+ def __imul__(self, other):
47
+ if isinstance(other, Vector3):
48
+ self.x *= other.x
49
+ self.y *= other.y
50
+ self.z *= other.z
51
+ else:
52
+ self.x *= other
53
+ self.y *= other
54
+ self.z *= other
55
+ return self
56
+
57
+ def __mul__(self, other):
58
+ if isinstance(other, Vector3):
59
+ return Vector3(
60
+ self.x * other.x,
61
+ self.y * other.y,
62
+ self.z * other.z)
63
+ return Vector3(
64
+ self.x * other,
65
+ self.y * other,
66
+ self.z * other)
67
+
68
+ def __idiv__(self, other):
69
+ if isinstance(other, Vector3):
70
+ self.x /= other.x
71
+ self.y /= other.y
72
+ self.z /= other.z
73
+ else:
74
+ self.x /= other
75
+ self.y /= other
76
+ self.z /= other
77
+ return self
78
+
79
+ def __div__(self, other):
80
+ if isinstance(other, Vector3):
81
+ return Vector3(
82
+ self.x / other.x,
83
+ self.y / other.y,
84
+ self.z / other.z)
85
+ return Vector3(
86
+ self.x / other,
87
+ self.y / other,
88
+ self.z / other)
89
+
90
+ def __str__(self):
91
+ return '({:f}, {:f}, {:f})'.format(self.x, self.y, self.z)
92
+
93
+ def dot(self, other):
94
+ isinstance(other, Vector3)
95
+ return self.x * other.x + self.y * other.y + self.z * other.z
96
+
97
+ def sqnorm(self):
98
+ return self.dot(self)
99
+
100
+ def len(self):
101
+ return sqrt(self.sqnorm())
102
+
103
+ def normalize(self):
104
+ n = self.len()
105
+ self.x /= n
106
+ self.y /= n
107
+ self.z /= n
108
+
109
+ def normalized(self):
110
+ return self / self.len()
111
+
112
+ def update(self, other):
113
+ isinstance(other, Vector3)
114
+ self.x = other.x
115
+ self.y = other.y
116
+ self.z = other.z
117
+
118
+
119
+ BALLISTIC_FLATTENING = 1.0
120
+ BALLISTIC_SCALE = 60.0
121
+
122
+
123
+ def flatten_traj_height(h):
124
+ if BALLISTIC_FLATTENING != 0.0 and h > 0:
125
+ h = log(BALLISTIC_FLATTENING * h + 1.0) / BALLISTIC_FLATTENING
126
+ return h
127
+
128
+
129
+ def unflatten_traj_height(h):
130
+ if BALLISTIC_FLATTENING != 0.0 and h > 0:
131
+ h = (exp(BALLISTIC_FLATTENING * h) - 1.0) / BALLISTIC_FLATTENING
132
+ return h
133
+
134
+
135
+ def set_ballistic_flattening(f):
136
+ global BALLISTIC_FLATTENING
137
+ BALLISTIC_FLATTENING = f
138
+
139
+
140
+ def set_ballistic_scale(f):
141
+ global BALLISTIC_SCALE
142
+ BALLISTIC_SCALE = f
143
+
144
+
145
+ def d_to_area(d):
146
+ return 0.25 * d * d * pi
147
+
148
+
149
+ def unflatten_pos_and_vel_original(pos, vel_dir):
150
+ assert isinstance(pos, Vector3)
151
+ assert isinstance(vel_dir, Vector3)
152
+ y = unflatten_traj_height(pos.y)
153
+
154
+ new_pos = pos + vel_dir * 0.001
155
+ new_pos.y = unflatten_traj_height(new_pos.y)
156
+
157
+ pos.y = y
158
+
159
+ angle_z2x = atan2(vel_dir.x, vel_dir.z)
160
+ dz = new_pos.z - pos.z
161
+ dx = new_pos.x - pos.x
162
+ pitch_angle = -atan2(new_pos.y - pos.y, sqrt(dz * dz + dx * dx))
163
+
164
+ vel_dir.x = sin(angle_z2x) * cos(pitch_angle)
165
+ vel_dir.y = sin(-pitch_angle)
166
+ vel_dir.z = cos(angle_z2x) * cos(pitch_angle)
167
+
168
+ return pos, vel_dir
169
+
170
+
171
+ # the calculus seemed dumb, and it was.
172
+ def unflatten_pos_and_vel(pos, vel_dir):
173
+ assert isinstance(pos, Vector3)
174
+ assert isinstance(vel_dir, Vector3)
175
+
176
+ vel_dir *= 0.001 # small delta to approximate non linear warp of the tangent
177
+ y0 = unflatten_traj_height(pos.y)
178
+ y1 = unflatten_traj_height(pos.y + vel_dir.y)
179
+
180
+ pos.y = y0
181
+ vel_dir.y = y1 - y0
182
+ vel_dir.normalize()
183
+
184
+ return pos, vel_dir
185
+
186
+ if 0:
187
+ __a = unflatten_pos_and_vel_original(Vector3(20.0, 3.0, 10.0), Vector3(10.0, 5.0, 5.0))
188
+ __b = unflatten_pos_and_vel(Vector3(20.0, 3.0, 10.0), Vector3(10.0, 5.0, 5.0))
189
+ assert isclose(__a[0].x, __b[0].x)
190
+ assert isclose(__a[0].y, __b[0].y)
191
+ assert isclose(__a[0].z, __b[0].z)
192
+ assert isclose(__a[1].x, __b[1].x)
193
+ assert isclose(__a[1].y, __b[1].y)
194
+ assert isclose(__a[1].z, __b[1].z)
195
+
196
+
197
+ # Lesta values, yes.. G is not as precise as the rest :P
198
+ G = 9.8
199
+ G_VEC = Vector3(0, -G, 0)
200
+ GAS_CST_R = 8.31447 # N.m / (mol.K)
201
+ AIR_MOLAR_MASS = 0.0289644 # kg / mol
202
+ SEALEVEL_TEMPERATURE = 288.15 # K
203
+ STATIC_PRESSURE_LVL0 = 101325.0 # Pa
204
+ TEMPERATURE_LAPSE_RATE_LVL0 = -0.0065 # K / m
205
+ PRESSURE_CST_H0 = G * AIR_MOLAR_MASS / (GAS_CST_R * TEMPERATURE_LAPSE_RATE_LVL0)
206
+
207
+
208
+ TEMPERATURE_LAPSE_RATE_LVL0_NEG = 0.0065 # K / m
209
+ PRESSURE_CST_H0_NEG = -PRESSURE_CST_H0
210
+
211
+
212
+ def altitude_temperature(y):
213
+ return SEALEVEL_TEMPERATURE + y * TEMPERATURE_LAPSE_RATE_LVL0
214
+
215
+
216
+ def altitude_pressure_original(y):
217
+ return STATIC_PRESSURE_LVL0 * pow(
218
+ 1 - y * TEMPERATURE_LAPSE_RATE_LVL0_NEG / SEALEVEL_TEMPERATURE,
219
+ PRESSURE_CST_H0_NEG)
220
+
221
+
222
+ def altitude_pressure(y):
223
+ return STATIC_PRESSURE_LVL0 * pow(
224
+ SEALEVEL_TEMPERATURE / altitude_temperature(y),
225
+ PRESSURE_CST_H0)
226
+
227
+ assert isclose(altitude_pressure_original(260), altitude_pressure(260))
228
+
229
+
230
+ def altitude_pressure_n_temperature(y):
231
+ temperature = altitude_temperature(y)
232
+ return STATIC_PRESSURE_LVL0 * pow(
233
+ SEALEVEL_TEMPERATURE / temperature,
234
+ PRESSURE_CST_H0), temperature
235
+
236
+
237
+ class TrajPoint(object):
238
+ def __init__(self, pos, v, time):
239
+ self.pos = pos
240
+ self.v = v
241
+ self.time = time
242
+
243
+
244
+ class BulletSim(object):
245
+ def __init__(self, mass, d, air_drag_coeff, velocity0, s_pos, s_dir):
246
+ self.mass = mass
247
+ self.d = d
248
+ self.airDragCoeff = air_drag_coeff
249
+ self.v0 = velocity0
250
+ self.pos = s_pos
251
+ self.vel_vec = s_dir.normalized() * velocity0
252
+ self.A = 0.25 * d * d * pi
253
+ self.time = 0.0
254
+ self.steps = 0
255
+
256
+ def step_original(self):
257
+ dy = self.pos.y * 0.48749998 * 0.0013333333
258
+ dt = max(min(dy * exp(dy), 0.8125), 0.1001)
259
+ vel_vec = self.vel_vec
260
+
261
+ p, temperature = altitude_pressure_n_temperature(self.pos.y)
262
+ air_density = AIR_MOLAR_MASS * p / (GAS_CST_R * temperature)
263
+ drag = self.A * 0.5 * self.airDragCoeff * air_density * vel_vec.sqnorm()
264
+
265
+ pitch = atan2(vel_vec.y, sqrt(vel_vec.z * vel_vec.z + vel_vec.x * vel_vec.x))
266
+ yaw = atan2(vel_vec.z, vel_vec.x)
267
+
268
+ a_drag = -drag / self.mass
269
+ a_horizontal = cos(pitch) * a_drag
270
+ a_vertical = sin(pitch) * a_drag - G
271
+
272
+ a = Vector3(
273
+ cos(yaw) * a_horizontal,
274
+ a_vertical,
275
+ sin(yaw) * a_horizontal)
276
+
277
+ self.pos += vel_vec * dt
278
+ self.vel_vec += a * dt
279
+ self.steps += 1
280
+
281
+ def step(self):
282
+ dy = self.pos.y * 0.00064999
283
+ dt = max(min(dy * exp(dy), 0.8125), 0.1001)
284
+ vel_vec = self.vel_vec
285
+
286
+ p, temperature = altitude_pressure_n_temperature(self.pos.y)
287
+ air_density = AIR_MOLAR_MASS * p / (GAS_CST_R * temperature)
288
+ drag = self.A * 0.5 * self.airDragCoeff * air_density * vel_vec.sqnorm()
289
+
290
+ a = (vel_vec.normalized() * (-drag / self.mass) + G_VEC)
291
+
292
+ self.pos += vel_vec * dt
293
+ self.vel_vec += a * dt
294
+ self.steps += 1
295
+
296
+ if 0:
297
+ _bullet0 = BulletSim(3.0, 0.04, 0.35, 300.0, Vector3(20.0, 3.0, 10.0), Vector3(10.0, 5.0, 5.0))
298
+ _bullet0.step_original()
299
+ _bullet1 = BulletSim(3.0, 0.04, 0.35, 300.0, Vector3(20.0, 3.0, 10.0), Vector3(10.0, 5.0, 5.0))
300
+ _bullet1.step()
301
+ assert isclose(_bullet0.vel_vec.x, _bullet1.vel_vec.x)
302
+ assert isclose(_bullet0.vel_vec.y, _bullet1.vel_vec.y)
303
+ assert isclose(_bullet0.vel_vec.z, _bullet1.vel_vec.z)
304
+
305
+
306
+ # flatten_output is not part of Lesta's code, it's added for comparison with real trajectory
307
+ # Lesta's bug: if the input shoot_pos is not already flattened, their code doesn't flatten the first trajectory point.
308
+ def build_trajectory(mass, d, air_drag_coeff, velocity, shoot_pos, shoot_dir,
309
+ draft, ret_stride, flat_input, t0, trajectory, flatten_output=False):
310
+ s_pos = copy(shoot_pos)
311
+ s_dir = copy(shoot_dir)
312
+
313
+ # added (not original)
314
+ flat_s_pos = copy(shoot_pos)
315
+ if flat_input:
316
+ unflatten_pos_and_vel(s_pos, s_dir)
317
+ else:
318
+ flat_s_pos.y = flatten_traj_height(flat_s_pos.y)
319
+
320
+ initial_len = len(trajectory)
321
+ trajectory.append(TrajPoint(flat_s_pos if flatten_output else s_pos, velocity, t0))
322
+
323
+ bullet = BulletSim(mass, d, air_drag_coeff, velocity, s_pos * BALLISTIC_SCALE, s_dir)
324
+
325
+ if bullet.pos.y >= 0.0:
326
+ cur_i = initial_len
327
+ inv_ballistic_scale = 1.0 / BALLISTIC_SCALE
328
+ while cur_i < 1024:
329
+ cur_i += 1
330
+ bullet.step()
331
+ bullet_world_pos = copy(bullet.pos) * inv_ballistic_scale
332
+ if flatten_output:
333
+ bullet_world_pos.y = flatten_traj_height(bullet_world_pos.y)
334
+ trajectory.append(TrajPoint(
335
+ bullet_world_pos,
336
+ bullet.vel_vec.len(),
337
+ t0 + bullet.time))
338
+ if bullet_world_pos.y <= 0.0:
339
+ break
340
+ else:
341
+ raise OverflowError(
342
+ "Oversize of vs array (1024): Arguments: mass:{:f}, d:{:f}, airDragCoeff:{:f}, v:{:f}, pos:{!s}, "
343
+ "dir:{!s}, draft:{!s}\n".format(mass, d, air_drag_coeff, velocity, shoot_pos, shoot_dir, draft))
344
+
345
+ # possible exploit here : assumed underwater with already 2 points and going down..
346
+ p0, p1 = trajectory[-2:]
347
+ pos0, pos1 = p0.pos, p1.pos
348
+ v37 = 1.0 - pos1.y / (pos1.y - pos0.y)
349
+ if v37 >= 0.00000011920929: # lastlast bullet was overwater
350
+ p1.pos = pos0 + (pos1 - pos0) * v37
351
+ p1.time = p0.time + (p1.time - p0.time) * v37
352
+ elif trajectory:
353
+ trajectory.pop()
354
+
355
+ if ret_stride > 1:
356
+ # fixed (official code is wrong: cur_i = 0)
357
+ cur_i = initial_len
358
+ end = len(trajectory)
359
+ if initial_len < end:
360
+ i = initial_len
361
+ while i < end:
362
+ trajectory[cur_i] = trajectory[i]
363
+ i += ret_stride
364
+ cur_i += 1
365
+ # official code is weird:
366
+ # if trajectory[-1].v != trajectory[cur_i - 1].v:
367
+ if cur_i != end:
368
+ trajectory[cur_i] = trajectory[-1]
369
+ del trajectory[cur_i + 1:]
370
+ # new size was wrong if cur_i inited with 0:
371
+ # new_size = initial_len + cur_i;
372
+
373
+ return 1
374
+
375
+ if 1:
376
+ import matplotlib.pyplot as plt
377
+ set_ballistic_scale(60.0)
378
+ for i in range(1, 15):
379
+ traj = []
380
+ build_trajectory(3.0, 0.04, 0.35, 300.0, Vector3(0.0, 0.2, 0.0), Vector3(1.0, 0.1*i, 0), 0, 1, False, 0, traj)
381
+ xs, ys = zip(*((p.pos.x, p.pos.y) for p in traj))
382
+ plt.plot(xs, ys, 'r-')
383
+ flattened_ys = list(flatten_traj_height(y) for y in ys)
384
+ plt.plot(xs, flattened_ys, 'b-')
385
+ plt.xlabel('dist (m/ballistic_scale)')
386
+ plt.ylabel('height (m/ballistic_scale)')
387
+ plt.grid(True)
388
+ plt.axis('equal')
389
+ print "final_velocity : ", traj[-1].v
390
+ plt.show()
391
+
392
+ WATER_DENSITY = 1000.0
393
+
394
+
395
+ def get_dist_underwater(unknown0, unknown1, d, mass, water_drag):
396
+ # drag_coeff = (d_to_area(d) * a5 * 102.0 * G) / (2.0 * a4)
397
+ drag_coeff = (d_to_area(d) * 0.5 * water_drag * WATER_DENSITY) / mass
398
+ return log(unknown0 * unknown1 * drag_coeff + 1.0) / drag_coeff
399
+
400
+
401
+ def get_velo_underwater(t, velocity0, d, mass, water_drag):
402
+ drag_coeff = (d_to_area(d) * 0.5 * water_drag * WATER_DENSITY) / mass
403
+ return velocity0 * exp(-t * drag_coeff)
404
+
405
+
406
+ def get_time_underwater(unknown0, unknown1, d, mass, water_drag):
407
+ drag_coeff = (d_to_area(d) * 0.5 * water_drag * WATER_DENSITY) / mass
408
+ return (exp(unknown0 * drag_coeff) - 1.0) / (unknown1 * drag_coeff)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@antti5/ship-tool-common",
3
- "version": "1.0.5",
3
+ "version": "1.0.7",
4
4
  "description": "Ship Tool common utils and library code",
5
5
  "repository": {
6
6
  "type": "git",