Rise
The Vieneo Province
Loading...
Searching...
No Matches
weapon.cpp
Go to the documentation of this file.
1#include "weapon.h"
2#include "Viewscreen.h"
3#include "../MathUtilities.h"
5
6#include <cmath>
7
9{
10 viewscreen = ptrGame;
11 logger = ptrGame->logger;
12
13 s = 0;
14 r = 0;
15 closest = -1;
16
17 for (t = 0; t < MAX_WEAPONS; t++)
18 {
19 weaponarray[t].active = false;
21 weaponsequence[t] = 0;
22 weaponSpawnSeen[t] = false;
23 }
24}
25
27{
28 if (rand() < 4096 || dockprogress < 1.0f) // 12% chance
29 {
30 logger->Log("Weapons calling Undock...");
33 }
34 float f_temp = RandomFloat();
35 if (f_temp < ourcockpit.power)
36 {
37 ourcockpit.power = f_temp;
38 if (ourcockpit.power <= 0.5f)
39 {
42 }
43 }
44 if (rand() < 4096) // 12% chance
45 {
47 ourcockpit.vdat.thrustaug -= (char)(RandomFloat() * (float)(ourcockpit.vdat.thrustaug + 1.0f));
49 if (!playerships[s].docked && playerships[s].reference != REF_ONGROUND)
50 {
51 playerships[s].rollRadPerSec += RandomFloat() * 1.0f - 0.5f;
52 playerships[s].yawRadPerSec += RandomFloat() * 1.0f - 0.5f;
53 playerships[s].pitch += RandomFloat() * 1.0f - 0.5f;
54 }
55 }
56 if (rand() < 4096) // 12% chance
57 {
59 ourcockpit.vdat.yawdamper -= (char)(RandomFloat() * (float)(ourcockpit.vdat.yawdamper + 1.0f));
61 }
62 if (rand() < 2048) // 6% chance on right engine
63 {
65 ourcockpit.vdat.rightengine -= (char)(RandomFloat() * (float)(ourcockpit.vdat.rightengine + 1.0f));
67 }
68 if (rand() < 2048) // 6% chance on left engine
69 {
71 ourcockpit.vdat.leftengine -= (char)(RandomFloat() * (float)(ourcockpit.vdat.leftengine + 1.0f));
73 }
74 if (rand() < 2048) // 6% chance on tail engine
75 {
76 if (ourcockpit.vdat.clengine > 0)
77 ourcockpit.vdat.clengine -= (char)(RandomFloat() * (float)(ourcockpit.vdat.clengine + 1.0f));
79 }
80}
81
82void weapon::Update(float fElapsedTime)
83{
84 viewscreen->logger->AddToCallStack("weapon::Update");
85
86 HRESULT hr;
87
88 // Weapons handling
89 float furthest = farplaneC;
90 closest = -1;
91 for (t = 0; t < MAX_WEAPONS; t++)
92 {
93 if (!weaponarray[t].active)
94 continue;
95
96 bool hasAuthoritativePresentation = false;
98 if (!weaponarray[t].exploded && Telemetry::gSnapshotSystem.HasWeaponSamples(t))
99 {
101 t, Telemetry::MonotonicSeconds(), static_cast<double>(fElapsedTime));
102 if (state.valid)
103 {
104 hasAuthoritativePresentation = true;
108 weaponarray[t].velocity = RiseMath::Vector3(static_cast<float>(state.velocity.x),
109 static_cast<float>(state.velocity.y), static_cast<float>(state.velocity.z));
112 }
113 }
114 else
115 {
116 // Legacy/local fallback for weapons which have no authoritative snapshots.
117 cross = weaponarray[t].veliter * fElapsedTime;
119 cross = weaponarray[t].velleft;
120 weaponarray[t].velleft -= cross;
121 weaponarray[t].velocity += cross;
122
123 cross = weaponarray[t].positer * fElapsedTime;
125 cross = weaponarray[t].posleft;
126 weaponarray[t].posleft -= cross;
127 weaponarray[t].precisionx += static_cast<double>(cross.x);
128 weaponarray[t].precisiony += static_cast<double>(cross.y);
129 weaponarray[t].precisionz += static_cast<double>(cross.z);
130 }
131
135 static_cast<double>(fElapsedTime), hasAuthoritativePresentation);
136 double proposedx = endpoint.x;
137 double proposedy = endpoint.y;
138 double proposedz = endpoint.z;
139
140 RiseMath::Vector3 proposed;
141 proposed.x = static_cast<float>(proposedx);
142 proposed.y = static_cast<float>(proposedy);
143 proposed.z = static_cast<float>(proposedz);
144
145#pragma region Collision Detection (Weapons)
146
147 // Check against largest things first
148 RiseMath::Vector3 location;
149
150 // Check against docks
151 if (!weaponarray[t].exploded && weaponarray[t].reference >= REF_DOCKENUMCPOC)
152 {
153 viewscreen->logger->AddToCallStack("weapon::Update DOCK");
154
156
157 RiseMath::Vector3 dockoffset = proposed;
158 if (RiseMath::Vec3Length(&dockoffset) <= allobjects[s].bound)
159 {
160 for (r = 0; r < allobjects[s].components; r++)
161 {
162 // Relative XYZ position
163 // Extensions
164 RiseMath::Vector3 result;
165 RiseMath::Vec3Scale(&result, &allobjects[s].componentarray[r].extendby, allobjects[s].componentarray[r].extended);
166 RiseMath::Vec3Add(&result, &allobjects[s].componentarray[r].nominalxyz, &result);
167
168 location = -result;//*allobjects[s].scale;
169
170 RiseMath::Vec3Subtract(&cross, &location, &dockoffset);
171
172 if (RiseMath::Vec3Length(&cross) <= allobjects[s].componentarray[r].boundsphere)
173 {
174 weaponarray[t].target = 0; // did not hit a vessel
177 ExplodeWeapon(t, playerships[0].docked && playerships[0].reference == weaponarray[t].collidedWith, "Dock");
178
179#ifdef testingC
181#endif
182
183 proposedx = weaponarray[t].precisionx;
184 proposedy = weaponarray[t].precisiony;
185 proposedz = weaponarray[t].precisionz;
186
187 break; // component loop
188 }
189 }
190 } // not within main bounding sphere
191 }
192
193 // Check against ships
194 if (!weaponarray[t].exploded)
195 {
196 viewscreen->logger->AddToCallStack("weapon::Update SHIP");
197
198 for (s = 0; s < MAX_SCAN; s++) // including us
199 {
200 if (!playerships[s].active || !playerships[s].visible || playerships[s].reference == REF_INANOTHER)
201 continue;
202
203 // Not the one who launched it at least for a second for it to clear
204 if (playerships[s].vehicleId == weaponarray[t].origin && weaponarray[t].elapsed < 1.0f)
205 continue;
206
207 RiseMath::Vector3 result;
208 if (weaponarray[t].reference < REF_INANOTHER)
209 result = playerships[s].position - proposed;
210 else if (weaponarray[t].reference == playerships[s].reference)
211 result = playerships[s].dockoffset - proposed;
212 else
213 result.x = result.y = result.z = 999.0f; // no chance
214
215 if (RiseMath::Vec3Length(&result) >= playerships[s].bound)
216 continue;
217
218 for (r = 0; r < playerships[s].components; r++)
219 {
220 // types to skip: no bounding sphere, interior of cargo bay
221 if (!playerships[s].componentarray[r].visible ||
222 playerships[s].componentarray[r].type == 53 ||
223 playerships[s].componentarray[r].type == 54)
224 continue;
225
226 location = RiseMath::Vector3(0, 0, 0);
227 RiseMath::Vec3TransformCoord(&location, &location, &playerships[s].componentarray[r].matWorld);
228
229 if (weaponarray[t].reference < REF_INANOTHER)
230 cross = -location + playerships[0].position - proposed;
231 else
232 cross = -location + playerships[0].dockoffset - proposed;
233
234 if (RiseMath::Vec3Length(&cross) >= playerships[s].componentarray[r].boundsphere)
235 continue;
236
239 if (weaponarray[t].reference >= REF_INANOTHER)
241 else
243
244 char msg[99];
245 if (playerships[s].vehicleId == weaponarray[t].origin)
246 {
247 sprintf_s(msg, sizeof msg, "Vehicle Elapsed %i", s);
248 ExplodeWeapon(t, s == 0, msg);
249 }
250 else
251 {
252 sprintf_s(msg, sizeof msg, "Vehicle NotOrigin %i", s);
253 ExplodeWeapon(t, s == 0, msg);
254 }
255
256 proposedx = weaponarray[t].precisionx;
257 proposedy = weaponarray[t].precisiony;
258 proposedz = weaponarray[t].precisionz;
259 proposed = RiseMath::Vector3(static_cast<float>(proposedx), static_cast<float>(proposedy), static_cast<float>(proposedz));
260 break;
261 }
262
263 // It hit one of these objects!
264 if (weaponarray[t].exploded)
265 {
266 if (s == 0) // it was us!
268 s = MAX_SCAN; // break out of loop
269 }
270 } // object loop
271 }
272
273 // Check collision with other weapons
274 if (!weaponarray[t].exploded)
275 {
276 viewscreen->logger->AddToCallStack("weapon::Update WEAP");
277
278 for (s = 0; s < MAX_WEAPONS; s++)
279 {
280 if (s != t)
281 {
282 if (weaponarray[s].active && !weaponarray[s].exploded &&
283 (weaponarray[t].reference < REF_INANOTHER || weaponarray[t].reference == weaponarray[s].reference))
284 {
285 RiseMath::Vector3 result;
286 result.x = (float)weaponarray[s].precisionx;
287 result.y = (float)weaponarray[s].precisiony;
288 result.z = (float)weaponarray[s].precisionz;
289 RiseMath::Vec3Subtract(&result, &result, &proposed);
290 if (RiseMath::Vec3Length(&result) < 0.001f) // 1 meter
291 {
294 weaponarray[t].target = weaponarray[s].target = 0; // not a ship
296
297 ExplodeWeapon(t, false, "Weapon");
298 ExplodeWeapon(s, false, "Weapon");
299
300 proposedx = weaponarray[t].precisionx;
301 proposedy = weaponarray[t].precisiony;
302 proposedz = weaponarray[t].precisionz;
303 }
304 }
305 }
306 }
307 }
308
309 // Check collision with ground or layout/depth maps!
310 if (!weaponarray[t].exploded && !g_bAboveClouds) // only below when valid grid
311 {
312 viewscreen->logger->AddToCallStack("weapon::Update GRND/CITY");
313
314 char wcube = viewscreen->CalculateCube(proposed);
315 VECTOR2SHORT wtcp = viewscreen->CalculateCGC(proposed, wcube);
316 wtcp.u = 31 - (viewscreen->ptrGrid->tcp.u - wtcp.u);
317 wtcp.v = 31 - (viewscreen->ptrGrid->tcp.v - wtcp.v);
318 if (wtcp.u >= 0 && wtcp.u <= 62 && wtcp.v >= 0 && wtcp.v <= 62) // within visible grid
319 {
320 float weaponAGL = viewscreen->CalculateAGL(wtcp.u, wtcp.v, proposed, false);
321 if (weaponAGL <= 0)
322 {
324 weaponarray[t].target = 0; // did not hit a vessel
326 ExplodeWeapon(t, false, "Ground");
327 proposedx = weaponarray[t].precisionx;
328 proposedy = weaponarray[t].precisiony;
329 proposedz = weaponarray[t].precisionz;
330 continue;
331 }
332
333 float buildingAGL, buildingBGL;
334 // FIND CITY AGL ADJUSTMENT
335 VECTOR2SHORT newdepthpos; // NOLINT
336 float f_tempu, f_tempv, f_tempd;
337 RiseMath::Vector3 result = -proposed;
338 RiseMath::Vec3Normalize(&result, &result);
339
340 if (gridarray[wtcp.u][wtcp.v].type > PlotType::NotZoned)
341 {
342 const short buildZoneOrdinal = gridarray[wtcp.u][wtcp.v].buildzoneOrdinal;
343 if (buildZoneOrdinal != -1)
344 {
345 if (RiseMath::IntersectTri(&gridarray[wtcp.u + 1][wtcp.v].position, &gridarray[wtcp.u][wtcp.v].position, &gridarray[wtcp.u + 1][wtcp.v + 1].position, &centerC, &result, &f_tempu, &f_tempv, &f_tempd))
346 {
347 newdepthpos.u = static_cast<short>((1.0f - f_tempu) * static_cast<float>(depthFloorResolutionC)); // V2-V1
348 newdepthpos.v = static_cast<short>(f_tempv * static_cast<float>(depthFloorResolutionC)); // V3-V1
349 }
350 else if (RiseMath::IntersectTri(&gridarray[wtcp.u][wtcp.v + 1].position, &gridarray[wtcp.u + 1][wtcp.v + 1].position, &gridarray[wtcp.u][wtcp.v].position, &centerC, &result, &f_tempu, &f_tempv, &f_tempd))
351 {
352 newdepthpos.u = static_cast<short>(f_tempu * static_cast<float>(depthFloorResolutionC)); // V2-V1
353 newdepthpos.v = static_cast<short>((1.0f - f_tempv) * static_cast<float>(depthFloorResolutionC)); // V3-V1
354 }
355 else
356 newdepthpos.u = newdepthpos.v = -2;
357 }
358 else
359 newdepthpos.u = newdepthpos.v = -3;
360 }
361 else
362 newdepthpos.u = newdepthpos.v = -1;
363
364 if (weaponarray[t].olddepthpos.u != newdepthpos.u || weaponarray[t].olddepthpos.v != newdepthpos.v)
365 {
366 weaponarray[t].olddepthpos = newdepthpos;
367 buildingAGL = viewscreen->CheckDepthMap(newdepthpos, wtcp.u, wtcp.v);
368 buildingBGL = viewscreen->CheckFloorMap(newdepthpos, wtcp.u, wtcp.v);
369 }
370 else
371 {
372 buildingAGL = 0.0f;
373 buildingBGL = 0.0f;
374 }
375
376
377 if (wtcp.u > 15 && wtcp.u < 48 && wtcp.v > 15 && wtcp.v < 48) // in patched data
378 {
379 if (!weaponarray[t].exploded)
380 {
381 if (gridarray[wtcp.u][wtcp.v].IsUserColony())
382 {
383 const short buildZoneOrdinal = gridarray[wtcp.u][wtcp.v].buildzoneOrdinal;
384
385 if (buildZoneOrdinal != -1)
386 {
388 static_cast<float>(weaponarray[t].precisionx),
389 static_cast<float>(weaponarray[t].precisiony),
390 static_cast<float>(weaponarray[t].precisionz));
391 if (viewscreen->ptrBuildings->CheckCollision(buildZoneOrdinal, position, weaponarray[t].velocity, 0, fElapsedTime, nullptr))
392 {
393 char msg[99];
394 sprintf_s(msg, sizeof msg, "Weapon %i hit colony building!", t);
395 logger->Log(msg);
396 weaponarray[t].velocity = centerC;//playerships[ourshipC].barycentric;
397 weaponarray[t].target = 0; // did not hit a vessel
399 ExplodeWeapon(t, false, "Colony");
400 proposedx = weaponarray[t].precisionx;
401 proposedy = weaponarray[t].precisiony;
402 proposedz = weaponarray[t].precisionz;
403 }
404 }
405 }
406 }
407 }
408
409
410 if (weaponAGL < buildingBGL && buildingBGL != 255.0f) // so we can go under overhangs without movement.cpp repoing us
411 buildingAGL = 0.0f;
412 if (weaponAGL <= buildingAGL)
413 {
415 weaponarray[t].target = 0; // did not hit a vessel
417 ExplodeWeapon(t, false, "City");
418 proposedx = weaponarray[t].precisionx;
419 proposedy = weaponarray[t].precisiony;
420 proposedz = weaponarray[t].precisionz;
421 }
422 }
423 }
424
425#pragma endregion
426
427 weaponarray[t].elapsed += fElapsedTime;
428
429
430 // Animation
431 viewscreen->logger->AddToCallStack("weapon::Update ANIM");
432 if (!weaponarray[t].exploded)
433 {
434 // Expiry check
435 if (weaponarray[t].elapsed > 30.0f) // 30 seconds
436 weaponarray[t].active = false;
437
438 weaponarray[t].animate += fElapsedTime * 30.0f; // 30 fps
439 if (weaponarray[t].animate >= weaponanimateC)
440 weaponarray[t].animate = 0;
441 }
442 else
443 {
444 if (weaponarray[t].type == 1 || weaponarray[t].type == 2)
445 weaponarray[t].animate += fElapsedTime * 0.5f; // should take 2 seconds
446 else
447 weaponarray[t].animate += fElapsedTime * 0.5f / (powf(weaponarray[t].yieldKg, 0.333333f) / 8.291327f);
448 if (weaponarray[t].animate > 1.0f)
449 weaponarray[t].active = false;
450 }
451
452
453#ifdef smoketestC
454 // equally space them
455 RiseMath::Vector3 offset;
456 if (weaponarray[t].reference < REF_INANOTHER)
457 offset = (weaponarray[t].velocity - weaponarray[t].barycentric) * fElapsedTime;
458 else
459 offset = weaponarray[t].dockingvel * fElapsedTime;
460 weaponarray[t].smokeprog += offset;
461
462 for (s = 0; s < smoketrailC; s++)
463 {
464 if (weaponarray[t].smoketrail[s].animate < smoketrailC)
465 {
466 if (advanceframe)
467 weaponarray[t].smoketrail[s].animate++;
468 weaponarray[t].smoketrail[s].offset += offset;
469 }
470 }
471
472 if (RiseMath::Vec3Length(&weaponarray[t].smokeprog) > 0.02f) // 100 meters start a new one
473 {
474 weaponarray[t].smoketrail[weaponarray[t].smokestep].animate = 0; // start at beginning
475 if (rand() > 16384)
476 weaponarray[t].smoketrail[weaponarray[t].smokestep].animate++;
477 weaponarray[t].smoketrail[weaponarray[t].smokestep].offset = centerC; // start at current location
478 weaponarray[t].smokestep++;
479 if (weaponarray[t].smokestep >= smoketrailC)
480 weaponarray[t].smokestep = 0;
481 weaponarray[t].smokeprog = centerC;
482 }
483#endif
484
485
486
487 // Set at new location (may be old location)
488 weaponarray[t].precisionx = proposedx;
489 weaponarray[t].precisiony = proposedy;
490 weaponarray[t].precisionz = proposedz;
491 // weaponarray[t].position=proposed;
492
493 viewscreen->logger->AddToCallStack("weapon::Update DIST");
494 if (weaponarray[t].exploded || weaponarray[t].type == 1 || weaponarray[t].type == 2) // everything except active longarm missiles
495 {
496 // Find closest active weapon for illumination
497 if (weaponarray[t].reference < REF_INANOTHER)
498 RiseMath::Vec3Subtract(&cross, &proposed, &playerships[0].position);
499 else
500 RiseMath::Vec3Subtract(&cross, &proposed, &playerships[0].dockoffset);
501
502 float f_temp = RiseMath::Vec3Length(&cross);
503 if (weaponarray[t].yieldKg > 0)
504 f_temp /= (weaponarray[t].yieldKg / 570.0f); // longarm is 1
505 if (f_temp < furthest)
506 {
507 furthest = f_temp;
508 closest = t;
509 }
510 else if (f_temp > 60.0f) // 60km
511 {
512 weaponarray[t].active = false;
513 }
514 }
515 }
516
517 if (closest != -1) // Closest for illumination, only set for sinker and fuzer
518 {
519 viewscreen->logger->AddToCallStack("weapon::Update CLOSE1");
520
521 RiseMath::Vector4 weapcol;
522 RiseMath::Vector4 weappos;
523
524 if (weaponarray[closest].reference < REF_INANOTHER)
525 {
526 weappos.x = static_cast<float>(playerships[0].precisionx - weaponarray[closest].precisionx);
527 weappos.y = static_cast<float>(playerships[0].precisiony - weaponarray[closest].precisiony);
528 weappos.z = static_cast<float>(playerships[0].precisionz - weaponarray[closest].precisionz);
529 }
530 else
531 {
532 weappos.x = playerships[0].dockoffset.x - static_cast<float>(weaponarray[closest].precisionx);
533 weappos.y = playerships[0].dockoffset.y - static_cast<float>(weaponarray[closest].precisiony);
534 weappos.z = playerships[0].dockoffset.z - static_cast<float>(weaponarray[closest].precisionz);
535 }
536
537 viewscreen->logger->AddToCallStack("weapon::Update CLOSE2");
538
539 if (weaponarray[closest].exploded)
540 {
541 const float invAnimate = 1.0f - weaponarray[closest].animate;
542 if (weaponarray[closest].type == 1) // Fuzer
543 {
544 // greenish blue
545 weapcol.x = 0.25f * invAnimate;
546 weapcol.y = 0.5f * invAnimate;
547 weapcol.z = 0.5f * invAnimate;
548 }
549 else if (weaponarray[closest].type == 2) // sinker
550 {
551 // reddish
552 weapcol.x = 1.0f * invAnimate;
553 weapcol.y = 0.36f * invAnimate;
554 weapcol.z = 0.35f * invAnimate;
555 }
556 else
557 {
558 // orange
559 const float yieldMod = powf(weaponarray[closest].yieldKg, 0.333333f) / 8.291327f;
560 weapcol.x = 1.0f * yieldMod * invAnimate * invAnimate;
561 weapcol.y = 0.5f * yieldMod * invAnimate * invAnimate;
562 const float allowBlue = RiseMath::Min(0.0f, yieldMod - 1.0f); // 0 at longarm strength
563 weapcol.z = allowBlue * yieldMod * invAnimate * invAnimate;
564 }
565 }
566 else
567 {
568 if (weaponarray[closest].type == 1) // Fuzer
569 {
570 weapcol.x = 0.25f; weapcol.y = 0.5f; weapcol.z = 0.5f;
571 }
572 else if (weaponarray[closest].type == 2) // sinker
573 {
574 weapcol.x = 1.0f; weapcol.y = 0.36f; weapcol.z = 0.35f;
575 }
576 else
577 {
578 weapcol.x = weapcol.y = weapcol.z = 0.0f;
579 }
580 }
581
582 weapcol.w = 0.0f; // unused
583 V(viewscreen->gameclass->graphics->g_pEffect->SetVector("WeapCol", &weapcol));
584 weappos.w = 1.0f; // active
585 V(viewscreen->gameclass->graphics->g_pEffect->SetVector("WeapPos", &weappos));
586
587 viewscreen->logger->AddToCallStack("weapon::Update CLOSE3");
588 }
589 else
590 {
591 RiseMath::Vector4 weappos = RiseMath::Vector4(0, 0, 0, 0); // inactive
592 V(viewscreen->gameclass->graphics->g_pEffect->SetVector("WeapPos", &weappos));
593 }
594
595 viewscreen->logger->AddToCallStack("weapon::Update DONE");
596}
597
598void weapon::FireWeapon(unsigned char bay) const
599{
600 viewscreen->logger->AddToCallStack("weapon::FireWeapon");
601
602 if (dead || ourcockpit.power < 0.25f || fuel <= 0.0f)
603 return;
604
605 float compare = 0.0f;
606 if (ourcockpit.vdat.weapontype[bay] == 1)
607 compare = weaponfuzerreadyC;
608 else if (ourcockpit.vdat.weapontype[bay] == 2)
609 compare = weaponsinkerreadyC;
610 else if (ourcockpit.vdat.weapontype[bay] == 3)
611 compare = weaponlongarmreadyC;
612
613 if (weapontimer[bay] != compare || ourcockpit.vdat.weapontype[bay] == 0)
614 return;
615
616 weapontimer[bay] = 0.0f;
617
618 SWeaponPacket outpacket = {};
619 RiseMath::Vector3 result;
620
621 result.x = 0.0f; result.y = 0.0f; result.z = 0.5f; // 1-10km/s // launch velocity
622 RiseMath::Vec3TransformNormal(&result, &result, &playerships[0].matrixWorld);
623 if (playerships[0].reference < REF_INANOTHER)
624 outpacket.velocity = viewscreen->UniverseRelativeVelocityKms + result;
625 else
626 outpacket.velocity = playerships[0].dockingvel + result;
627 outpacket.reference = playerships[0].reference;
628
629 RiseMath::Vec3TransformCoord(&outpacket.position, &ourcockpit.launchPosition, &playerships[0].matrixWorld); // Find straight ahead vector
630
631 outpacket.type = ourcockpit.vdat.weapontype[bay]; // Weapon type
632 outpacket.status = 1; // New addition
633 outpacket.array = 255; // Not assigned yet
634 outpacket.origin = playerships[0].vehicleId;
635
636 // @todo this may not even be needed now since we send the targetVehicleId when we change targets
637 if (viewscreen->gameclass->bus->targetC >= 0)
638 outpacket.target = playerships[viewscreen->gameclass->bus->scanslot[viewscreen->gameclass->bus->targetC]].vehicleId; // Target for tracking
639 else
640 outpacket.target = 0; // No target
641
642 viewscreen->gameclass->networking->SendToServer(&outpacket, sizeof(SWeaponPacket), true);
643}
644
645void weapon::ExplodeWeapon(short t, bool wasus, std::string reason)
646{
647 viewscreen->logger->AddToCallStack("weapon::ExplodeWeapon");
648
649 if (!weaponarray[t].exploded) // if it wasn't sent by peer.cpp
650 {
651 SWeaponPacket outpacket = {};
652 outpacket.status = 3; // Explode
653 outpacket.velocity = centerC;// weaponarray[t].velocity;
654 outpacket.reference = weaponarray[t].reference;
656 outpacket.position.x = static_cast<float>(weaponarray[t].precisionx);
657 outpacket.position.y = static_cast<float>(weaponarray[t].precisiony);
658 outpacket.position.z = static_cast<float>(weaponarray[t].precisionz);
659 outpacket.origin = weaponarray[t].origin;
660 outpacket.type = weaponarray[t].type;
661 outpacket.array = static_cast<unsigned char>(t);
662 outpacket.target = weaponarray[t].target;
663 outpacket.yieldKg = weaponarray[t].yieldKg;
664 viewscreen->gameclass->networking->SendToServer(&outpacket, sizeof SWeaponPacket, true);
665
666 weaponarray[t].exploded = true;
667 weaponarray[t].animate = 0; // start over
668 weaponarray[t].elapsed = 0; // start over
672
673 char msg[199];
674 sprintf_s(msg, sizeof(msg), "We sent that weapon %i exploded at %.3f %.3f %.3f (%s)", t, outpacket.position.x, outpacket.position.y, outpacket.position.z, reason.c_str());
675 logger->Log(msg);
676 }
677 else
678 {
679 char msg[199];
680 sprintf_s(msg, sizeof(msg), "Received that weapon %i exploded at %.3f %.3f %.3f (%s)", t, static_cast<float>(weaponarray[t].precisionx), static_cast<float>(weaponarray[t].precisiony), static_cast<float>(weaponarray[t].precisionz), reason.c_str());
681 logger->Log(msg);
682 }
683
684 if (wasus)
685 {
686 if (weaponarray[t].type == 1)
688 else if (weaponarray[t].type == 2)
690 else if (weaponarray[t].type == 3)
692
693 }
694
695 // vector to/from the explosion from our location
696 RiseMath::Vector3 difference;
697 difference.x = static_cast<float>(weaponarray[t].precisionx);
698 difference.y = static_cast<float>(weaponarray[t].precisiony);
699 difference.z = static_cast<float>(weaponarray[t].precisionz);
700 difference += weaponarray[t].posleft; // visuals are smoothed but explosion needs to be where it actually hits
701 if (weaponarray[t].reference < REF_INANOTHER)
702 RiseMath::Vec3Subtract(&difference, &difference, &playerships[0].position);
703 else
704 RiseMath::Vec3Subtract(&difference, &difference, &playerships[0].dockoffset);
705 const float distance = RiseMath::Vec3Length(&difference);
706
707 char msg[99];
708 sprintf_s(msg, sizeof(msg), "Explosion! Distance: %.3f", distance);
709 logger->Log(msg);
710
711 sprintf_s(msg, sizeof(msg), "--- speed of sound at %.3f km MSL: %.3f kms", playerships[0].mslKm, viewscreen->ptrPropulsion->speedOfSoundKms);
712 logger->Log(msg);
713
714
715 // compare our motion to weapon location
717 const float distNextSecond = RiseMath::Vec3Length(&difference);
718 const float ourClosingRate = distance - distNextSecond;
719 const float closingRate = ourClosingRate + viewscreen->ptrPropulsion->speedOfSoundKms;
720 if (ourClosingRate >= 0) // closing
721 {
722 sprintf_s(msg, sizeof(msg), "--- we are closing at %.3f kms", ourClosingRate);
723 logger->Log(msg);
724 }
725 else // distancing
726 {
727 sprintf_s(msg, sizeof(msg), "--- we are distancing at %.3f kms", -ourClosingRate);
728 logger->Log(msg);
729 }
730
731#pragma region Shockwave
732
733 // shock wave/overpressure wave
734 if (weaponarray[t].yieldKg > 0) // longarm or explosionOnly
735 {
736 // we USED to for longarm get 0.01 at 0km and nothing beyond 1km
737 // now we use 9.4 kg * 0.001 for kt is 0.00047 at 0 and 0.00235 (1/4) at 1km
738 // convert from kg to kt
739 const float psi = powf(weaponarray[t].yieldKg * 0.000001f * 1.66667f / RiseMath::Max(powf(distance, 3.0f), 0.000000001f), 0.6f);
740
741 if (psi >= 0.1f)
742 {
743 // so if it was facing the explosion // update doesn't appear to be orientation or coefficient related!
744 const float yield = psi * 0.059f; // 5 psi is lethal for them in a ship we got 110 in hiroshima testing a couple hundred meters away in a C2A and GPT says 40-90 instantaneous peak 65 average ...
745
746 // e-10 was perfect 2.696208 * 0.000836 but car felt nothing
747 // c2a is 0.037093
748 // the above numbers were calibrated with a 1/55 frames per second baked in ... we now leave that up to the framemove.cpp and where it applies the shockmag to the sumofforces
749
750 // Convert to kPa
751 const float dP_kPa = psi * 6.89476f;
752
753 // Ambient (Vieneo ~2.6 bar)
754 const float p0_kPa = viewscreen->ptrPropulsion->staticDensity * SeaLevelPressureMb * 0.1f; // convert millibar to kPa
755 const float S = dP_kPa / p0_kPa; // dimensionless strength ratio
756
757 // Shock-front Mach (weak/moderate shock approx)
758 const float gamma = 1.4f;
759 const float C = (gamma + 1.0f) / (4.0f * gamma); // ~ 0.4286
760 float Ms = 1.0f + C * S;
761 Ms = RiseMath::Min(RiseMath::Max(Ms, 1.0f), 6.0f);
762
763 // Arrival times
764 float a0 = Ms * viewscreen->ptrPropulsion->speedOfSoundKms;
765 const float t_shock = distance / (a0 + ourClosingRate);
766
767 SShockWave shockWave;
768 shockWave.timetoshock = t_shock; // seconds at 1km speed of sound
769 shockWave.shockspeed = 4.0f; // 0.25 sec up and then down
770 shockWave.shockramp = 0.0f; // reset
771 RiseMath::Vec3Normalize(&shockWave.shockmag, &difference);
772 RiseMath::Matrix matrixInverse; // need to make new one because we have moved since
773 RiseMath::QuaternionNormalize(&playerships[0].orientation, &playerships[0].orientation);
774 RiseMath::MatrixRotationQuaternion(&matrixInverse, &playerships[0].orientation);
775 RiseMath::Vec3TransformNormal(&shockWave.shockmag, &shockWave.shockmag, &matrixInverse);
776 shockWave.shockmag *= yield; // 0 is far away, 0*0 is 0, .9 is close, .9*.9 is .81
777 shockwaves.emplace_back(shockWave);
778
779 // Reference knobs (tune once, leave forever)
780 const float dP_ref_kPa = 35.0f; // ~5 psi—classic “serious damage to humans” reference
781 const float r0_km = 0.1f; // 100 m reference distance for audio rolloff
782 const float alpha = 0.75f; // loudness vs overpressure curvature
783 const float beta = 1.2f; // distance rolloff exponent (1 = spherical)
784
785 // Distance rolloff (floors at r0 so near-field doesn’t explode)
786 const float roll = powf(r0_km / RiseMath::Max(distance, r0_km), beta);
787
788 // Overpressure mapping -> gain (dimensionless 0..1)
789 float gain_shock = roll * powf(RiseMath::Max(dP_kPa, 0.0f) / dP_ref_kPa, alpha);
790 gain_shock = RiseMath::Min(gain_shock, 1.0f); // limiter
791
792 // Log exactly what you used
793#ifdef _DEBUG
794 float t_sound = distance / closingRate; // 8km at .8 kms takes 10 seconds
795 char msg1[999];
796 sprintf_s(msg1, sizeof(msg1), "Blast timing: R=%.3f km, a0=%.3f, psi=%.3f, dP=%.2f kPa, p0=%.1f kPa, S=%.4f, Ms=%.4f, u_rad=%.3f, t_shock=%.6f, t_sound=%.6f, lead=%.2f ms",
797 distance, viewscreen->ptrPropulsion->speedOfSoundKms, psi, dP_kPa, p0_kPa, S, Ms, ourClosingRate, t_shock, t_sound, (t_sound - t_shock) * 1000.0f);
798 logger->Log(msg1);
799#endif
800
801 sprintf_s(msg, sizeof(msg), "--- yield: %.0f kg, delay %.3f, psi: %.3f, vol: %.3f", weaponarray[t].yieldKg, shockWave.timetoshock, psi, gain_shock);
802 logger->Log(msg);
803
804 viewscreen->gameclass->sound->AddDelayedSound(SOUND_SHOCKWAVE, t_shock, gain_shock, 1, true);
805 }
806 else
807 {
808 sprintf_s(msg, sizeof(msg), "--- yield: %.0f kg, no shock wave, psi: %.3f", weaponarray[t].yieldKg, psi);
809 logger->Log(msg);
810 }
811 }
812
813#pragma endregion
814
815 if (wasus)
816 return;
817
818 // explosion sound effect
819 // Tunables (pick once, tweak by ear)
820 const float r0_km = 0.01f; // 10 m reference to avoid singularity
821 const float Wref_kg = 1.0f; // reference yield
822 const float beta = 1.1f; // distance rolloff exponent for low-freq explosion "body"
823 const float knee = 0.6f; // soft-knee compressor (higher = softer)
824
825 // Source strength ~ W^(1/3)
826 float size = powf(RiseMath::Max(weaponarray[t].yieldKg, 0.001f) / Wref_kg, 1.0f / 3.0f);
827
828 // Geometric rolloff (multiplicative, not subtractive)
829 float roll = (r0_km / RiseMath::Max(distance, r0_km));
830 roll = powf(roll, beta);
831
832 // Raw gain before compression
833 float g = size * roll;
834
835 // Soft-knee compression to [0..1) that preserves dynamics:
836 // g' = g / (g + knee)
837 g = g / (g + knee);
838
839 // Hard clamp safety
840 float volume = Clamp(g, 0, 1);
841 const float delay = distance / closingRate; // 8km at .8 kms takes 10 seconds
842
843 sprintf_s(msg, sizeof(msg), "--- delay: %.3f, volume %.3f", delay, volume);
844 logger->Log(msg);
845
846 if (volume <= 0)
847 return;
848
849 SOUNDS soundEnum;
850 if (weaponarray[t].type == 1)
851 soundEnum = SOUND_EXPLODE1;
852 else if (weaponarray[t].type == 2) // sinker
853 soundEnum = SOUND_EXPLODE2;
854 else
855 soundEnum = static_cast<SOUNDS>(SOUND_EXPLODE3 + rand() % 3); // 3, 4, or 5
856
857 viewscreen->gameclass->sound->AddDelayedSound(soundEnum, delay, volume, 1, true);
858}
859
860void weapon::Render(Rendering::RenderDevice* pd3dDevice, RiseMath::Matrix* matrixLookAtLocal)
861{
862 viewscreen->logger->AddToCallStack("weapon::Render");
863
864 RiseMath::Matrix matrixTemp, matrixWorld;
865 RiseMath::Vector3 result;
866
867 viewscreen->logger->AddToCallStack("exploded weapons");
868
869 // Exploded weapons only
870 for (t = 0; t < MAX_WEAPONS; t++)
871 {
872 if (weaponarray[t].active && weaponarray[t].exploded)
873 {
874 RiseMath::MatrixRotationZ(&matrixTemp, -RiseMath::Pi); // 1.5
875 RiseMath::MatrixMultiply(&matrixWorld, &matrixTemp, matrixLookAtLocal);
876 if (weaponarray[t].type == 1 || weaponarray[t].type == 2)
877 RiseMath::MatrixScaling(&matrixTemp, 0.005f, 0.005f, 0.005f);
878 else if (weaponarray[t].yieldKg == 0.0f)
879 RiseMath::MatrixScaling(&matrixTemp, 0.06f, 0.06f, 0.06f); // ~100 foot radius
880 else
881 {
882 // so longarm is 0.06 ... at 570 kg
883 float scale = powf(weaponarray[t].yieldKg, 0.333333f) * 0.00723648f; // to keep animation size at 1:1 for longarm explosion
884 // scale up over the first second
885 if (weaponarray[t].elapsed < 1.0f)
886 scale *= sqrtf(weaponarray[t].elapsed);
887 RiseMath::MatrixScaling(&matrixTemp, scale, scale, scale);
888 }
889 RiseMath::MatrixMultiply(&matrixWorld, &matrixTemp, &matrixWorld);
890 RiseMath::MatrixRotationY(&matrixTemp, weaponarray[t].tilt);
891 RiseMath::MatrixMultiply(&matrixTemp, &matrixTemp, &matrixWorld); // matrixWorld is scaled lookat matrix
892
893 if (weaponarray[t].reference < REF_INANOTHER)
894 {
895 result.x = static_cast<float>(weaponarray[t].precisionx - playerships[0].precisionx);
896 result.y = static_cast<float>(weaponarray[t].precisiony - playerships[0].precisiony);
897 result.z = static_cast<float>(weaponarray[t].precisionz - playerships[0].precisionz);
898 }
899 else
900 {
901 result.x = static_cast<float>(weaponarray[t].precisionx) - playerships[0].dockoffset.x;
902 result.y = static_cast<float>(weaponarray[t].precisiony) - playerships[0].dockoffset.y;
903 result.z = static_cast<float>(weaponarray[t].precisionz) - playerships[0].dockoffset.z;
904 }
905 matrixTemp._41 = -result.x;
906 matrixTemp._42 = -result.y;
907 matrixTemp._43 = -result.z;
908
909 pd3dDevice->SetTransform(Rendering::TransformSlot::WORLD, &matrixTemp);
910
911 RiseMath::MatrixIdentity(&matrixTemp);
912 matrixTemp._33 = weaponarray[t].animate;
913 pd3dDevice->SetTransform(Rendering::TransformSlot::TEXTURE0, &matrixTemp);
915
916 if (weaponarray[t].type == 1) // fuzerX
917 {
919 pd3dDevice->SetRenderState(Rendering::RenderState::DESTBLEND, Rendering::Blend::INVSRCCOLOR); // for electric, // these two techniques are the same but use volumetric map!
920 pd3dDevice->SetTexture(0, viewscreen->m_pddsFuzerX);
922 }
923 else if (weaponarray[t].type == 2) // sinkerX
924 {
927 pd3dDevice->SetTexture(0, viewscreen->m_pddsSinkerX);
929 }
930 else // blastX
931 {
933 pd3dDevice->SetRenderState(Rendering::RenderState::DESTBLEND, Rendering::Blend::INVSRCALPHA); // for smokey explosion, he is the only one with alpha
934 pd3dDevice->SetTexture(0, viewscreen->m_pddsBlastX);
936 }
937
939 }
940 }
941
942 // Unexploded weapons...
947 /*pd3dDevice->SetTextureStageState(0, Rendering::TextureState::COLORARG2, Rendering::TextureArgument::TFACTOR);
948 pd3dDevice->SetTextureStageState(0, Rendering::TextureState::COLORARG1, Rendering::TextureArgument::DIFFUSE);
949*/
950// RiseMath::MatrixRotationY( &matrixTemp, weaponarray[t].tilt );
951// RiseMath::MatrixMultiply( &matrixTemp, &matrixTemp, &matrixWorld ); // matrixWorld is scaled lookat matrix
952
953 viewscreen->logger->AddToCallStack("unexploded weapons");
954
955 // Unexploded weapons...
956 for (t = 0; t < MAX_WEAPONS; t++)
957 {
958 if (weaponarray[t].active && !weaponarray[t].exploded)
959 {
960 if (weaponarray[t].type == 1 || weaponarray[t].type == 2) // sinker and fuzer
961 {
962 RiseMath::MatrixRotationZ(&matrixTemp, -RiseMath::Pi); // 1.5
963 RiseMath::MatrixMultiply(&matrixWorld, &matrixTemp, matrixLookAtLocal);
964 RiseMath::MatrixScaling(&matrixTemp, 0.005f, 0.005f, 0.005f);
965 RiseMath::MatrixMultiply(&matrixWorld, &matrixTemp, &matrixWorld);
966 }
967 else // longarm
968 {
969 result.x = static_cast<float>(weaponarray[t].precisionx);
970 result.y = static_cast<float>(weaponarray[t].precisiony);
971 result.z = static_cast<float>(weaponarray[t].precisionz);
972 RiseMath::Vec3Normalize(&result, &result);
973
974 RiseMath::Vector3 result1;
975 RiseMath::Vec3Normalize(&result1, &weaponarray[t].velocity);
976
977 RiseMath::MatrixLookAtRH(&matrixWorld, &centerC, &result1, &result); // eye at up
978 RiseMath::MatrixRotationY(&matrixTemp, -RiseMath::HalfPi); // now yaw, was roll
979 RiseMath::MatrixMultiply(&matrixWorld, &matrixWorld, &matrixTemp);
980 RiseMath::MatrixInverse(&matrixWorld, nullptr, &matrixWorld);
981 }
982
983
984 matrixTemp = matrixWorld; // reset to scaled lookat matrix
985
986 if (weaponarray[t].reference < REF_INANOTHER)
987 {
988 result.x = static_cast<float>(weaponarray[t].precisionx - playerships[0].precisionx);
989 result.y = static_cast<float>(weaponarray[t].precisiony - playerships[0].precisiony);
990 result.z = static_cast<float>(weaponarray[t].precisionz - playerships[0].precisionz);
991 }
992 else
993 {
994 result.x = static_cast<float>(weaponarray[t].precisionx) - playerships[0].dockoffset.x;
995 result.y = static_cast<float>(weaponarray[t].precisiony) - playerships[0].dockoffset.y;
996 result.z = static_cast<float>(weaponarray[t].precisionz) - playerships[0].dockoffset.z;
997 }
998 matrixTemp._41 = -result.x;
999 matrixTemp._42 = -result.y;
1000 matrixTemp._43 = -result.z;
1001
1002 if (playerships[0].mslKm < 63.0f)
1003 {
1004 r = static_cast<short>(255.0f * (viewscreen->ptrWeather->fFogEndHorizon * 2.0f - RiseMath::Vec3Length(&result)) / (viewscreen->ptrWeather->fFogEndHorizon));
1005 if (r < 0)
1006 r = 0;
1007 else if (r > 255)
1008 r = 255;
1009 }
1010 else
1011 r = 255;
1012 if (r == 0) continue;
1014
1015 pd3dDevice->SetTransform(Rendering::TransformSlot::WORLD, &matrixTemp);
1016 if (weaponarray[t].type == 1) // fuzer // both of these use a technique
1017 {
1018 pd3dDevice->SetFVF(RISE_FVF_NONORMAL);
1020 pd3dDevice->SetTexture(0, viewscreen->m_pddsWeapon[static_cast<int>(weaponarray[t].animate)]);
1022 }
1023 else if (weaponarray[t].type == 2) // sinker
1024 {
1025 pd3dDevice->SetFVF(RISE_FVF_NONORMAL);
1027 pd3dDevice->SetTexture(0, viewscreen->m_pddsSinker[static_cast<int>(weaponarray[t].animate)]);
1029 }
1030 else if (weaponarray[t].type == 3) // longarm missile // use the acl or bcl vehicle pass!
1031 {
1033 pd3dDevice->SetFVF(RISE_FVF_VERTEX2);
1034 pd3dDevice->SetStreamSource(0, polyobjects[13].VB, 0, sizeof(RiseVertex2Vertex));
1035 pd3dDevice->SetTexture(0, viewscreen->m_pddsCityTexture[101]); // WEP-Source
1036 pd3dDevice->DrawPrimitive(Rendering::Primitive::TriangleList, 0, polyobjects[13].polygons);
1038 pd3dDevice->SetFVF(RISE_FVF_VERTEX);
1039 }
1040 }
1041 }
1042
1043 viewscreen->logger->AddToCallStack("weapon::Render DONE");
1044}
1045
1047{
1048 const unsigned char slot = datapacket.array;
1049 if (slot >= MAX_WEAPONS || datapacket.sequence >= 255 || datapacket.status < 0 || datapacket.status > 4 ||
1050 datapacket.type < 0 || datapacket.type > 4 ||
1051 datapacket.reference <= REF_ENUMMIN || datapacket.reference >= REF_ENUMMAX ||
1052 !std::isfinite(datapacket.position.x) || !std::isfinite(datapacket.position.y) ||
1053 !std::isfinite(datapacket.position.z) || !std::isfinite(datapacket.velocity.x) ||
1054 !std::isfinite(datapacket.velocity.y) || !std::isfinite(datapacket.velocity.z) ||
1055 !std::isfinite(datapacket.yieldKg) || datapacket.yieldKg < 0.0f)
1056 {
1057 logger->Log("Weapon::ProcessPacket rejected malformed weapon telemetry", Logger::Level::Warn);
1058 return;
1059 }
1060
1061 const bool spawnEvent = datapacket.status == 1 || datapacket.status == 4;
1062 if (spawnEvent && weaponarray[slot].active && weaponSpawnSeen[slot] && datapacket.sequence == 0 &&
1063 weaponarray[slot].origin == datapacket.origin && weaponarray[slot].type == datapacket.type)
1064 {
1065 logger->Log("Weapon::ProcessPacket ignored a duplicate spawn event", Logger::Level::Debug);
1066 return;
1067 }
1068 if (spawnEvent)
1070
1071 if (datapacket.status == 0) // expired
1072 {
1073 if (weaponarray[slot].active && weaponSpawnSeen[slot] &&
1075 {
1076 logger->Log("Weapon::ProcessPacket ignored stale inactive event", Logger::Level::Debug);
1077 return;
1078 }
1079 weaponsequence[slot] = datapacket.sequence;
1080 weaponarray[slot].active = false;
1081 weaponSpawnSeen[slot] = false;
1083 return;
1084 }
1085
1086 if (datapacket.status == 2 && (!weaponarray[slot].active || !weaponSpawnSeen[slot]))
1087 {
1088 // The reliable spawn event may still be in flight. Dropping this unreliable
1089 // motion packet preserves the gameplay event and the next 20 Hz update recovers.
1090 logger->Log("Weapon::ProcessPacket deferred motion until spawn event", Logger::Level::Debug);
1091 return;
1092 }
1093 const bool impactWithoutSpawn = datapacket.status == 3 && !weaponarray[slot].active;
1094 if (impactWithoutSpawn)
1095 {
1096 // Reliable gameplay events are still meaningful if their reliable spawn event
1097 // has not been dispatched yet. Initialize enough state to render the impact.
1098 weaponarray[slot].active = true;
1099 weaponarray[slot].exploded = false;
1100 weaponarray[slot].animate = 0.0f;
1101 weaponarray[slot].elapsed = 0.0f;
1102 weaponarray[slot].origin = datapacket.origin;
1103 weaponarray[slot].target = datapacket.target;
1104 weaponarray[slot].type = datapacket.type;
1105 weaponarray[slot].reference = datapacket.reference;
1106 weaponarray[slot].yieldKg = datapacket.yieldKg;
1107 weaponSpawnSeen[slot] = true;
1108 }
1109
1110 if (datapacket.status == 1 || datapacket.status == 4) // new or explosionOnly
1111 {
1112 weaponarray[slot].active = true;
1113 weaponarray[slot].exploded = false;
1114 weaponarray[slot].animate = RandomFloat() * static_cast<float>(weaponanimateC - 1);
1115 weaponarray[slot].elapsed = 0.0f;
1116 weaponarray[slot].origin = datapacket.origin; // Who launched it or 0 for explosionOnly
1117 weaponarray[slot].target = datapacket.target; // Aimed at who
1118 weaponarray[slot].type = datapacket.type; // 1=fuzer, 2=sinker, 3=longarm, 4=explosionOnly
1119 weaponarray[slot].reference = datapacket.reference;
1121 weaponarray[slot].yieldKg = datapacket.yieldKg;
1122
1123 weaponarray[slot].velocity = datapacket.velocity;
1125 weaponarray[slot].precisionx = static_cast<double>(datapacket.position.x);
1126 weaponarray[slot].precisiony = static_cast<double>(datapacket.position.y);
1127 weaponarray[slot].precisionz = static_cast<double>(datapacket.position.z);
1128
1129 // So the theory is the FIRST packet IF IT HAS AN ORIGIN VEHICLE will use that vehicle's position, otherwise we use the universe coordinates
1130 if (datapacket.origin)
1131 {
1132 for (short t = 0; t < MAX_SCAN; t++)
1133 {
1134 if (playerships[t].active && playerships[t].vehicleId == datapacket.origin)
1135 {
1136 // when we get launch the position is offset from the vehicle that launched it
1137 if (weaponarray[slot].reference < REF_INANOTHER)
1138 {
1139 weaponarray[slot].precisionx = static_cast<double>(datapacket.position.x) + playerships[t].precisionx;
1140 weaponarray[slot].precisiony = static_cast<double>(datapacket.position.y) + playerships[t].precisiony;
1141 weaponarray[slot].precisionz = static_cast<double>(datapacket.position.z) + playerships[t].precisionz;
1142 }
1143 else
1144 {
1145 weaponarray[slot].precisionx = static_cast<double>(datapacket.position.x + playerships[t].dockoffset.x);
1146 weaponarray[slot].precisiony = static_cast<double>(datapacket.position.y + playerships[t].dockoffset.y);
1147 weaponarray[slot].precisionz = static_cast<double>(datapacket.position.z + playerships[t].dockoffset.z);
1148 }
1149 break;
1150 }
1151 }
1152 }
1153 else
1154 {
1155 // when we get explosionOnly then we get the universe coordinates
1156 weaponarray[slot].precisionx = static_cast<double>(datapacket.position.x);
1157 weaponarray[slot].precisiony = static_cast<double>(datapacket.position.y);
1158 weaponarray[slot].precisionz = static_cast<double>(datapacket.position.z);
1159 }
1160
1162
1163 if (datapacket.status != 4) // launch sound effect
1164 {
1165 for (short t = 0; t < MAX_SCAN; t++)
1166 {
1167 if (playerships[t].active && playerships[t].vehicleId == datapacket.origin)
1168 {
1169 if (t == 0)
1170 {
1171 logger->Log("Weapon::ProcessPacket Weapon launched!");
1172 if (datapacket.type == 1)
1173 viewscreen->gameclass->sound->PlayEx(SOUND_WEAPON1, false, 1, 1, 0, true, true, true);
1174 else if (datapacket.type == 2)
1175 viewscreen->gameclass->sound->PlayEx(SOUND_WEAPON2, false, 1, 1, 0, true, true, true);
1176 else if (datapacket.type == 3)
1177 viewscreen->gameclass->sound->PlayEx(SOUND_WEAPON3, false, 1, 1, 0, true, true, true);
1178 }
1179 else // not our vessel
1180 {
1182 float distance = RiseMath::Vec3Length(&result);
1183 if (distance < 20.0f) // 20 km limit
1184 {
1185 float f_temp = distance / 20.0f; // 0 is close, 1 is far
1186 f_temp = 1.0f - f_temp; // 1 is close, 0 is far
1187
1188 logger->Log("Weapon::ProcessPacket Weapon detected!");
1189 if (f_temp < 1.0f)
1190 {
1191 SOUNDS soundEnum;
1192 if (datapacket.type == 1)
1193 soundEnum = SOUND_WEAPON1;
1194 else if (datapacket.type == 2)
1195 soundEnum = SOUND_WEAPON2;
1196 else
1197 soundEnum = SOUND_WEAPON3;
1199 sqrtf(f_temp), 1, true);
1200 }
1201 }
1202 }
1203 break;
1204 }
1205 }
1206 }
1207 }
1208
1210 if (spawnEvent)
1211 {
1213 }
1214 else
1215 {
1216 snapshot.position = {datapacket.position.x, datapacket.position.y, datapacket.position.z};
1217 }
1218 snapshot.velocity = {datapacket.velocity.x, datapacket.velocity.y, datapacket.velocity.z};
1219 snapshot.orientation = {};
1220 snapshot.sequence = datapacket.sequence;
1221 snapshot.sampleTimeMs = datapacket.sampleTimeMs;
1222 snapshot.reference = datapacket.reference;
1223 snapshot.identity = datapacket.origin;
1224 snapshot.expectedIntervalSeconds = 0.05;
1225 snapshot.finalSample = datapacket.status != 2;
1226
1227 if (!impactWithoutSpawn)
1228 {
1230 slot, snapshot, Telemetry::MonotonicSeconds());
1231 if (acceptance != Telemetry::Acceptance::Accepted)
1232 {
1233 logger->Log("Weapon::ProcessPacket rejected duplicate, stale, or invalid weapon packet",
1235 return;
1236 }
1237 }
1238 weaponsequence[slot] = datapacket.sequence;
1239 if (spawnEvent)
1240 weaponSpawnSeen[slot] = true;
1241
1242 if (datapacket.status == 3 || datapacket.status == 4) // exploded weapons or explosionOnly
1243 {
1244 if (!weaponarray[slot].exploded)
1245 {
1246 weaponarray[slot].precisionx = datapacket.position.x;
1247 weaponarray[slot].precisiony = datapacket.position.y;
1248 weaponarray[slot].precisionz = datapacket.position.z;
1249 weaponarray[slot].velocity = datapacket.velocity;
1250 weaponarray[slot].reference = datapacket.reference;
1251 weaponarray[slot].collidedWith = datapacket.collidedWith;
1252 weaponarray[slot].target = datapacket.target;
1253 weaponarray[slot].yieldKg = datapacket.yieldKg;
1254 weaponarray[slot].exploded = true;
1255 weaponarray[slot].animate = 0; // start over
1256 weaponarray[slot].elapsed = 0; // start over
1257 if (weaponarray[slot].collidedWith == REF_INANOTHER)
1258 ExplodeWeapon(slot, datapacket.target == playerships[0].vehicleId, "Network Ship");
1259 else if (weaponarray[slot].collidedWith >= REF_DOCKENUMCPOC)
1260 ExplodeWeapon(slot, playerships[0].docked && playerships[0].reference == weaponarray[slot].collidedWith, "Network Dock");
1261 else
1262 ExplodeWeapon(slot, false, "Network Other");
1264 }
1265 }
1266}
#define V(expression)
HRESULT(WINAPI *CUniBuffer::_ScriptApplyDigitSubstitution)(const SCRIPT_DIGITSUBSTITUTE *
float RandomFloat()
constexpr auto Clamp(T1 val, T2 min, T3 max)
#define MAX_WEAPONS
#define MAX_SCAN
@ NotZoned
const RiseMath::Vector3 centerC
@ REF_DOCKENUMCPOC
Definition ServerTypes.h:53
@ REF_INANOTHER
Definition ServerTypes.h:45
@ REF_ENUMMIN
Definition ServerTypes.h:41
@ REF_ONGROUND
Definition ServerTypes.h:43
@ REF_ENUMMAX
Definition ServerTypes.h:56
@ REF_HIDDEN
Definition ServerTypes.h:52
SOUNDS
Definition Sound.h:23
@ SOUND_EXPLODE1
Definition Sound.h:295
@ SOUND_EXPLODEINT2
Definition Sound.h:301
@ SOUND_WEAPON2
Definition Sound.h:307
@ SOUND_WEAPON3
Definition Sound.h:308
@ SOUND_EXPLODE2
Definition Sound.h:296
@ SOUND_EXPLODE3
Definition Sound.h:297
@ SOUND_EXPLODEINT1
Definition Sound.h:300
@ SOUND_EXPLODEINT3
Definition Sound.h:302
@ SOUND_WEAPON1
Definition Sound.h:306
@ SOUND_SHOCKWAVE
Definition Sound.h:304
#define depthFloorResolutionC
Definition Viewscreen.h:43
short targetC
Definition Bus.h:480
short scanslot[MAX_SCAN]
Definition Bus.h:485
std::unique_ptr< Bus > bus
Definition GameClass.h:108
void RequestScannerData() const
std::unique_ptr< Sound > sound
Definition GameClass.h:104
std::unique_ptr< Networking > networking
Definition GameClass.h:103
void KillScannerData() const
std::unique_ptr< Graphics > graphics
Definition GameClass.h:105
Rendering::RenderMaterial * g_pEffect
Definition Graphics.h:30
void Log(const char *msg, Level level=Info, int errorCode=0)
Definition Logger.cpp:283
void AddToCallStack(const char *msg)
Definition Logger.cpp:355
@ Debug
Definition Logger.h:27
@ Warn
Definition Logger.h:26
void SendToServer(void *pData, DWORD dwSize, bool bGuaranteed, PacketOrdering order=ORDERING_NONE) const
HRESULT DrawPrimitive(Primitive type, UINT first, UINT count)
HRESULT SetStreamSource(UINT stream, RenderBuffer *buffer, UINT offset, UINT stride)
HRESULT SetRenderState(DWORD state, DWORD value)
HRESULT SetFVF(DWORD value)
HRESULT SetTransform(DWORD slot, const RiseMath::Matrix *value)
HRESULT SetTextureStageState(DWORD stage, DWORD state, DWORD value)
HRESULT SetTexture(DWORD stage, RenderTexture *texture)
HRESULT SetVector(const char *name, const RiseMath::Vector4 *value)
void AddDelayedSound(SOUNDS soundEnum, float delay, float volume=1.0f, float freq=1.0f, bool fromOutside=false)
Definition Sound.cpp:3376
float Play(SOUNDS soundEnum, float volume=1.0f)
Definition Sound.cpp:802
void PlayEx(SOUNDS soundEnum, bool loop, float volume=1.0f, float frequencyMod=1.0f, float pan=0.0f, bool restart=true, bool fromOutside=false, bool partOfHull=false)
Definition Sound.cpp:1006
void ResetWeapon(std::size_t slot, int identity)
void RemoveWeapon(std::size_t slot)
Acceptance PushWeapon(std::size_t slot, MotionSnapshot snapshot, double receiptSeconds)
PresentationState SampleWeapon(std::size_t slot, double nowSeconds, double elapsedSeconds)
Rendering::RenderTexture * m_pddsSinker[weaponanimateC]
Definition Viewscreen.h:250
float CheckFloorMap(VECTOR2SHORT newdepthpos, short u, short v)
Rendering::RenderTexture * m_pddsWeapon[weaponanimateC]
Definition Viewscreen.h:249
RiseMath::Vector3 UniverseRelativeVelocityKms
Definition Viewscreen.h:125
Rendering::RenderTexture * m_pddsSinkerX
Definition Viewscreen.h:252
GameClass * gameclass
Definition Viewscreen.h:439
std::unique_ptr< propulsion > ptrPropulsion
Definition Viewscreen.h:432
Rendering::RenderTexture * m_pddsCityTexture[citytextureC]
Definition Viewscreen.h:295
std::unique_ptr< weather > ptrWeather
Definition Viewscreen.h:434
std::unique_ptr< grid > ptrGrid
Definition Viewscreen.h:426
VECTOR2SHORT CalculateCGC(RiseMath::Vector3 position, char cube) const
float CalculateAGL(short u, short v, RiseMath::Vector3 position, bool makeleveler)
Rendering::RenderBuffer * m_avBillboardVertices
Definition Viewscreen.h:217
std::unique_ptr< buildings > ptrBuildings
Definition Viewscreen.h:428
float rcsThrustOutputY
Definition Viewscreen.h:373
void Undock() const
float CheckDepthMap(VECTOR2SHORT newdepthpos, short u, short v)
Rendering::RenderTexture * m_pddsBlastX
Definition Viewscreen.h:253
char CalculateCube(RiseMath::Vector3 position) const
Logger * logger
Definition Viewscreen.h:440
Rendering::RenderTexture * m_pddsFuzerX
Definition Viewscreen.h:251
bool CheckCollision(short buildZoneOrdinal, RiseMath::Vector3 position, RiseMath::Vector3 velocity, float bound, float ElapsedTime, RiseMath::Vector3 *cross) const
VECTOR2SHORT tcp
Definition grid.h:44
float speedOfSoundKms
Definition propulsion.h:82
float staticDensity
Definition propulsion.h:94
void Update(float fElapsedTime)
Definition weapon.cpp:82
void EnergyWeaponImpact()
Definition weapon.cpp:26
void FireWeapon(unsigned char bay) const
Definition weapon.cpp:598
Logger * logger
Definition weapon.h:25
weapon(Viewscreen *prtGame)
Definition weapon.cpp:8
short s
Definition weapon.h:27
short r
Definition weapon.h:27
int closest
Definition weapon.h:28
unsigned char weaponsequence[MAX_WEAPONS]
Definition weapon.h:31
bool weaponSpawnSeen[MAX_WEAPONS]
Definition weapon.h:32
short t
Definition weapon.h:27
void ExplodeWeapon(short t, bool wasus, std::string reason)
Definition weapon.cpp:645
void ProcessPacket(SWeaponPacket datapacket)
Definition weapon.cpp:1046
void Render(Rendering::RenderDevice *pd3dDevice, RiseMath::Matrix *matrixLookAtLocal)
Definition weapon.cpp:860
Viewscreen * viewscreen
Definition weapon.h:24
SWeaponData weaponarray[MAX_WEAPONS]
Definition weapon.h:30
float fFogEndHorizon
Definition weather.h:207
float weapontimer[2]
Definition globals.cpp:89
float fuel
Definition globals.cpp:90
DeathType dead
Definition globals.cpp:37
s_network_objects playerships[MAX_SCAN]
Definition globals.cpp:112
std::vector< SShockWave > shockwaves
Definition globals.cpp:43
s_universe_object allobjects[maxstarC]
Definition globals.cpp:108
Scockpit ourcockpit
Definition globals.cpp:114
float dockprogress
Definition globals.cpp:32
bool g_bAboveClouds
Definition globals.cpp:16
s_polygon_object polyobjects[maxpolyC]
Definition globals.cpp:106
LOCALGRID2 gridarray[64][64]
Definition globals.cpp:100
#define weaponlongarmreadyC
Definition globals.h:79
#define SeaLevelPressureMb
Definition globals.h:102
#define weaponfuzerreadyC
Definition globals.h:77
#define farplaneC
Definition globals.h:27
#define weaponsinkerreadyC
Definition globals.h:78
#define RISE_FVF_VERTEX
Definition globals.h:118
#define RISE_FVF_NONORMAL
Definition globals.h:125
#define weaponanimateC
Definition globals.h:55
#define RISE_FVF_VERTEX2
Definition globals.h:119
constexpr uint32_t INVSRCALPHA
constexpr uint32_t SRCCOLOR
constexpr uint32_t INVSRCCOLOR
constexpr uint32_t ONE
constexpr uint32_t SRCALPHA
constexpr uint32_t SRCBLEND
constexpr uint32_t ALPHABLENDENABLE
constexpr uint32_t DESTBLEND
constexpr uint32_t TEXTUREFACTOR
constexpr uint32_t BLENDFACTORALPHA
constexpr uint32_t COLOROP
constexpr uint32_t TEXTURETRANSFORMFLAGS
constexpr uint32_t DISABLE
constexpr uint32_t COUNT3
constexpr uint32_t TEXTURE0
constexpr uint32_t WORLD
float Vec3Length(const Vector3 *a)
Definition ClientMath.h:95
constexpr float Pi
Definition ClientMath.h:12
constexpr auto Min(A first, B second)
Definition ClientMath.h:15
bool IntersectTri(const Vector3 *a, const Vector3 *b, const Vector3 *c, const Vector3 *origin, const Vector3 *direction, float *u, float *v, float *distance)
Definition ClientMath.h:197
Matrix * MatrixLookAtRH(Matrix *out, const Vector3 *eye, const Vector3 *at, const Vector3 *up)
Definition ClientMath.h:141
Vector3 * Vec3TransformCoord(Vector3 *out, const Vector3 *a, const Matrix *m)
Definition ClientMath.h:109
Vector3 * Vec3Add(Vector3 *out, const Vector3 *a, const Vector3 *b)
Definition ClientMath.h:96
Matrix * MatrixMultiply(Matrix *out, const Matrix *a, const Matrix *b)
Definition ClientMath.h:116
Matrix * MatrixRotationQuaternion(Matrix *out, const Quaternion *q)
Definition ClientMath.h:135
Quaternion * QuaternionNormalize(Quaternion *out, const Quaternion *q)
Definition ClientMath.h:165
Vector3 * Vec3Subtract(Vector3 *out, const Vector3 *a, const Vector3 *b)
Definition ClientMath.h:97
Vector3 * Vec3TransformNormal(Vector3 *out, const Vector3 *a, const Matrix *m)
Definition ClientMath.h:107
Matrix * MatrixRotationY(Matrix *out, float value)
Definition ClientMath.h:127
Matrix * MatrixInverse(Matrix *out, float *determinant, const Matrix *a)
Definition ClientMath.h:118
constexpr float Tau
Definition ClientMath.h:12
Matrix * MatrixIdentity(Matrix *out)
Definition ClientMath.h:115
Matrix * MatrixScaling(Matrix *out, float x, float y, float z)
Definition ClientMath.h:124
Vector3 * Vec3Normalize(Vector3 *out, const Vector3 *a)
Definition ClientMath.h:103
Vector3 * Vec3Scale(Vector3 *out, const Vector3 *a, float scale)
Definition ClientMath.h:98
Matrix * MatrixRotationZ(Matrix *out, float value)
Definition ClientMath.h:128
constexpr auto Max(A first, B second)
Definition ClientMath.h:16
constexpr float HalfPi
Definition ClientMath.h:12
double MonotonicSeconds()
bool IsNewerModulo255(std::uint8_t incoming, std::uint8_t previous)
Vec3 WeaponFrameEndpoint(const Vec3 &presentationPosition, const Vec3 &velocity, double elapsedSeconds, bool hasAuthoritativePresentation)
SnapshotSystem gSnapshotSystem
PlotType type
Definition globals.h:276
bool IsUserColony() const
Definition globals.h:297
short buildzoneOrdinal
Definition globals.h:286
RiseMath::Vector3 position
Definition globals.h:273
float timetoshock
Definition globals.h:818
RiseMath::Vector3 shockmag
Definition globals.h:822
float shockspeed
Definition globals.h:820
float shockramp
Definition globals.h:819
char weapontype[MAX_WEAPPERSHIP]
char leftengine
char rightengine
char yawdamper
char thrustaug
char clengine
char type
Definition globals.h:383
RiseMath::Vector3 positer
Definition globals.h:382
RiseMath::Vector3 posleft
Definition globals.h:382
unsigned short target
Definition globals.h:388
double precisionz
Definition globals.h:380
float tilt
Definition globals.h:386
double precisionx
Definition globals.h:380
VECTOR2SHORT olddepthpos
Definition globals.h:389
float elapsed
Definition globals.h:386
bool active
Definition globals.h:387
bool exploded
Definition globals.h:387
RiseMath::Vector3 velleft
Definition globals.h:382
unsigned short origin
Definition globals.h:388
char reference
Definition globals.h:383
float yieldKg
Definition globals.h:390
char collidedWith
Definition globals.h:383
double precisiony
Definition globals.h:380
RiseMath::Vector3 velocity
Definition globals.h:381
float animate
Definition globals.h:384
RiseMath::Vector3 veliter
Definition globals.h:382
RiseMath::Vector3 position
unsigned short sampleTimeMs
RiseMath::Vector3 velocity
unsigned char sequence
unsigned char array
unsigned short origin
unsigned short target
float power
Definition globals.h:599
SVesselDC vdat
Definition globals.h:664
bool vdatChanged
Definition globals.h:665
RiseMath::Vector3 launchPosition
Definition globals.h:603
RiseMath::Vector3 nominalxyz
Definition globals.h:413
RiseMath::Vector3 barycentric
Definition globals.h:500
RiseMath::Vector3 dockingvel
Definition globals.h:500
RiseMath::Vector3 position
Definition globals.h:511
unsigned short components
Definition globals.h:551
double precisionz
Definition globals.h:512
RiseMath::Vector3 dockoffset
Definition globals.h:511
unsigned short vehicleId
Definition globals.h:537
double precisiony
Definition globals.h:512
RiseMath::Vector3 velocity
Definition globals.h:500
float rollRadPerSec
Definition globals.h:523
double precisionx
Definition globals.h:512
RiseMath::Matrix matrixWorld
Definition globals.h:517
s_mesh_component * componentarray
Definition globals.h:458
unsigned short components
Definition globals.h:457