forked from scp-fs2open/fs2open.github.com
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebris.cpp
More file actions
1346 lines (1112 loc) · 42.5 KB
/
Copy pathdebris.cpp
File metadata and controls
1346 lines (1112 loc) · 42.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) Volition, Inc. 1999. All rights reserved.
*
* All source code herein is the property of Volition, Inc. You may not sell
* or otherwise commercially exploit the source or things you created based on the
* source.
*
*/
#include "cmdline/cmdline.h"
#include "debris/debris.h"
#include "fireball/fireballs.h"
#include "freespace.h"
#include "gamesnd/gamesnd.h"
#include "globalincs/linklist.h"
#include "io/timer.h"
#include "network/multi.h"
#include "network/multimsgs.h"
#include "network/multiutil.h"
#include "object/objcollide.h"
#include "object/objectsnd.h"
#include "particle/particle.h"
#include "particle/ParticleEffect.h"
#include "particle/volumes/LegacyAACuboidVolume.h"
#include "radar/radarsetup.h"
#include "render/3d.h"
#include "scripting/global_hooks.h"
#include "scripting/hook_api.h"
#include "scripting/scripting.h"
#include "scripting/api/objs/vecmath.h"
#include "ship/ship.h"
#include "ship/shipfx.h"
#include "species_defs/species_defs.h"
#include "tracing/Monitor.h"
#include "utils/Random.h"
#include "weapon/weapon.h"
constexpr int DEBRIS_SOUND_DELAY = 2000; // time to start debris sound after created
int Num_hull_pieces; // number of hull pieces in existence
// we now maintain a kind of "virtual" Hull_debris_list,
// but all we really need to know is how many hull pieces there are and whether a debris piece is on it
// This list holds debris pieces that are set to expire when their lifetime runs out;
// pieces that were placed by FREDers (i.e. that have the DoNotExpire flag) should not be on it.
SCP_vector<debris> Debris;
int Debris_inited = 0;
int Debris_model = -1;
int Debris_vaporize_model = -1;
int Debris_num_submodels = 0;
particle::ParticleEffectHandle Debris_hit_particle;
#define DEBRIS_INDEX(dp) (int)(dp-Debris.data())
// Find the first available arc slot. If none is available, and no_create is false, add one.
debris_electrical_arc *debris_find_or_create_electrical_arc_slot(debris *db, bool no_create);
/**
* Start the sequence of a piece of debris writhing in unholy agony!!!
*/
static void debris_start_death_roll(object *debris_obj, debris *debris_p, vec3d *hitpos = nullptr)
{
auto sip = &Ship_info[debris_p->ship_info_index];
if (debris_p->is_hull) {
// tell everyone else to blow up the piece of debris
if( MULTIPLAYER_MASTER )
send_debris_update_packet(debris_obj,DEBRIS_UPDATE_NUKE);
if (sip->debris_end_particles.isValid()) {
auto source = particle::ParticleManager::get()->createSource(sip->debris_end_particles);
// Use the position since the object is going to be invalid soon
auto host = std::make_unique<EffectHostVector>(debris_obj->pos, debris_obj->orient, debris_obj->phys_info.vel);
host->setRadius(debris_obj->radius);
source->setHost(std::move(host));
source->setNormal(debris_obj->orient.vec.uvec);
source->finishCreation();
} else {
int fireball_type = fireball_ship_explosion_type(sip);
if(fireball_type < 0) {
fireball_type = FIREBALL_EXPLOSION_LARGE1 + Random::next(FIREBALL_NUM_LARGE_EXPLOSIONS);
}
fireball_create( &debris_obj->pos, fireball_type, FIREBALL_LARGE_EXPLOSION, OBJ_INDEX(debris_obj), debris_obj->radius*1.75f);
}
// only play debris destroy sound if hull piece and it has been around for at least 2 seconds
if ( Missiontime > debris_p->time_started + 2*F1_0 ) {
auto snd_id = sip->debris_explosion_sound;
if (snd_id.isValid()) {
snd_play_3d( gamesnd_get_game_sound(snd_id), &debris_obj->pos, &View_position, debris_obj->radius );
}
}
} else {
if (sip->shrapnel_end_particles.isValid()) {
auto source = particle::ParticleManager::get()->createSource(sip->shrapnel_end_particles);
// Use the position since the object is going to be invalid soon
auto host = std::make_unique<EffectHostVector>(debris_obj->pos, debris_obj->orient, debris_obj->phys_info.vel);
host->setRadius(debris_obj->radius);
source->setHost(std::move(host));
source->setNormal(debris_obj->orient.vec.uvec);
source->finishCreation();
}
}
if (scripting::hooks::OnDebrisDeath->isActive()) {
scripting::hooks::OnDebrisDeath->run(scripting::hook_param_list(
scripting::hook_param("Debris", 'o', debris_obj),
scripting::hook_param("Hitpos",
'o',
scripting::api::l_Vector.Set(hitpos ? *hitpos : vmd_zero_vector),
hitpos != nullptr)));
}
debris_obj->flags.set(Object::Object_Flags::Should_be_dead);
}
/**
* This will get called at the start of each level.
*/
void debris_init()
{
if ( !Debris_inited ) {
Debris_inited = 1;
}
Debris_model = -1;
Debris_vaporize_model = -1;
Debris_num_submodels = 0;
// Reset everything between levels
Debris.clear();
Debris.reserve(SOFT_LIMIT_DEBRIS_PIECES);
Num_hull_pieces = 0;
Debris_hit_particle = particle::ParticleManager::get()->addEffect(particle::ParticleEffect(
"", //Name
::util::UniformFloatRange(10.f), //Particle num
particle::ParticleEffect::Duration::ONETIME, //Single Particle Emission
::util::UniformFloatRange(), //No duration
::util::UniformFloatRange (-1.f), //Single particle only
particle::ParticleEffect::ShapeDirection::ALIGNED, //Particle direction
::util::UniformFloatRange(1.f), //Velocity Inherit
false, //Velocity Inherit absolute?
std::make_unique<particle::LegacyAACuboidVolume>(0.3f, 1.f, true), //Velocity volume
::util::UniformFloatRange(0.f, 10.f), //Velocity volume multiplier
particle::ParticleEffect::VelocityScaling::NONE, //Velocity directional scaling
std::nullopt, //Orientation-based velocity
std::nullopt, //Position-based velocity
nullptr, //Position volume
particle::ParticleEffectHandle::invalid(), //Trail
1.f, //Chance
true, //Affected by detail
1.f, //Culling range multiplier
true, //Disregard Animation Length. Must be true for everything using particle::Anim_bitmap_X
false, //Don't reverse animation
false, //parent local
false, //ignore velocity inherit if parented
false, //position velocity inherit absolute?
std::nullopt, //Local velocity offset
std::nullopt, //Local offset
::util::UniformFloatRange(0.25f, 0.75f), //Lifetime
::util::UniformFloatRange(0.2f, 0.4f), //Radius
particle::Anim_bitmap_id_fire)); //Bitmap
}
/**
* Page in debris bitmaps at level load
*/
void debris_page_in()
{
uint i;
Debris_model = model_load( NOX("debris01.pof") );
if (Debris_model >= 0) {
polymodel * pm;
pm = model_get(Debris_model);
Debris_num_submodels = pm->n_models;
}
Debris_vaporize_model = model_load( NOX("debris02.pof") );
for (i=0; i<Species_info.size(); i++ )
{
species_info *species = &Species_info[i];
nprintf(( "Paging", "Paging in debris texture '%s'\n", species->debris_texture.filename));
species->debris_texture.bitmap_id = bm_load(species->debris_texture.filename);
if (species->debris_texture.bitmap_id < 0)
{
Warning( LOCATION, "Couldn't load species %s debris\ntexture, '%s'\n", species->species_name, species->debris_texture.filename);
}
bm_page_in_texture(species->debris_texture.bitmap_id);
}
}
MONITOR(NumSmallDebrisRend)
MONITOR(NumHullDebrisRend)
/**
* Add item to [virtual] Hull_debris_list
*/
void debris_add_to_hull_list(debris *db)
{
if ( db->is_hull && !db->flags[Debris_Flags::OnHullDebrisList] ) {
Num_hull_pieces++;
db->flags.set(Debris_Flags::OnHullDebrisList);
}
}
/**
* Remove item from [virtual] Hull_debris_list
*/
void debris_remove_from_hull_list(debris *db)
{
if ( db->is_hull && db->flags[Debris_Flags::OnHullDebrisList] ) {
Num_hull_pieces--;
db->flags.remove(Debris_Flags::OnHullDebrisList);
}
}
/**
* Delete the debris object.
* This is only ever called via obj_delete(). Do not call directly.
* Use debris_start_death_roll() if you want to force a debris piece to die.
*/
void debris_delete( object * obj )
{
int num;
debris *db;
num = obj->instance;
Assert(num >= 0 && num < (int)Debris.size());
Assert(Debris[num].objnum == OBJ_INDEX(obj));
db = &Debris[num];
if (db->model_instance_num >= 0) {
model_delete_instance(db->model_instance_num);
db->model_instance_num = -1;
}
if ( db->is_hull ) {
debris_remove_from_hull_list(db);
}
db->flags.reset();
db->objnum = -1;
}
/**
* Do various updates to debris: check if time to die, start fireballs
* Maybe delete debris if it's very far away from player.
*
* @param obj pointer to debris object
* @param frame_time time elapsed since last debris_move() called
*/
void debris_process_post(object * obj, float frame_time)
{
int num = obj->instance;
int objnum = OBJ_INDEX(obj);
Assert(num >= 0 && num < (int)Debris.size());
Assert(Debris[num].objnum == objnum);
debris *db = &Debris[num];
if ( db->is_hull ) {
radar_plot_object( obj );
if ( timestamp_elapsed(db->sound_delay) ) {
obj_snd_assign(objnum, db->ambient_sound, &vmd_zero_vector);
db->sound_delay = TIMESTAMP::invalid();
}
}
if (!db->flags[Debris_Flags::DoNotExpire]) {
Assertion(db->lifeleft >= 0.0f, "A ship with a negative lifeleft should also have the DoNotExpire flag!");
db->lifeleft -= frame_time;
if (db->lifeleft < 0.0f) {
debris_start_death_roll(obj, db);
}
}
// ================== DO THE ELECTRIC ARCING STUFF =====================
if ( db->arc_frequency <= 0 ) {
return; // If arc_frequency <= 0, this piece has no arcs on it
}
if ( !timestamp_elapsed(db->arc_timeout) && timestamp_elapsed(db->arc_next_time)) {
db->arc_next_time = _timestamp_rand(db->arc_frequency,db->arc_frequency*2 );
db->arc_frequency += 100;
if (db->is_hull) {
int n_arcs = Random::next(1, 3); // Create 1-3 sparks
vec3d v1 = submodel_get_random_point(db->model_num, db->submodel_num);
vec3d v2 = submodel_get_random_point(db->model_num, db->submodel_num);
vec3d v3 = submodel_get_random_point(db->model_num, db->submodel_num);
vec3d v4 = submodel_get_random_point(db->model_num, db->submodel_num);
int lifetime = Random::next(100, 1000);
// Create the spark effects
for (int n = 0; n < n_arcs; n++) {
auto arc = debris_find_or_create_electrical_arc_slot(db, db->electrical_arcs.size() >= MAX_DEBRIS_ARCS);
if (arc) {
arc->timestamp = _timestamp(lifetime); // live up to a second
switch( n ) {
case 0:
arc->endpoint_1 = v1;
arc->endpoint_2 = v2;
break;
case 1:
arc->endpoint_1 = v2;
arc->endpoint_2 = v3;
break;
case 2:
arc->endpoint_1 = v2;
arc->endpoint_2 = v4;
break;
default:
UNREACHABLE("Unhandled case %d for electrical arc creation in debris_process_post()!", n);
}
}
}
// rotate v2 out of local coordinates into world.
// Use v2 since it is used in every bolt. See above switch().
vec3d snd_pos;
vm_vec_unrotate(&snd_pos, &v2, &obj->orient);
vm_vec_add2(&snd_pos, &obj->pos );
//Play a sound effect
if ( lifetime > 750 ) {
// 1.00 second effect
snd_play_3d( gamesnd_get_game_sound(GameSounds::DEBRIS_ARC_05), &snd_pos, &View_position, obj->radius );
} else if ( lifetime > 500 ) {
// 0.75 second effect
snd_play_3d( gamesnd_get_game_sound(GameSounds::DEBRIS_ARC_04), &snd_pos, &View_position, obj->radius );
} else if ( lifetime > 250 ) {
// 0.50 second effect
snd_play_3d( gamesnd_get_game_sound(GameSounds::DEBRIS_ARC_03), &snd_pos, &View_position, obj->radius );
} else if ( lifetime > 100 ) {
// 0.25 second effect
snd_play_3d( gamesnd_get_game_sound(GameSounds::DEBRIS_ARC_02), &snd_pos, &View_position, obj->radius );
} else {
// 0.10 second effect
snd_play_3d( gamesnd_get_game_sound(GameSounds::DEBRIS_ARC_01), &snd_pos, &View_position, obj->radius );
}
}
}
for (auto &arc: db->electrical_arcs) {
if (arc.timestamp.isValid()) {
if (timestamp_elapsed(arc.timestamp)) {
// Kill off the spark
arc.timestamp = TIMESTAMP::invalid();
} else {
// Maybe move a vertex.... 20% of the time maybe?
int mr = Random::next();
if ( mr < Random::MAX_VALUE/5 ) {
auto pt = submodel_get_random_point(db->model_num, db->submodel_num);
if (mr % 2 == 0)
arc.endpoint_1 = pt;
else
arc.endpoint_2 = pt;
}
}
}
}
}
/**
* Locate the oldest hull debris chunk. Search through the Hull_debris_list, which is a list
* of all the hull debris chunks.
*/
int debris_find_oldest()
{
int oldest_index;
fix oldest_time;
oldest_index = -1;
oldest_time = 0x7fffffff;
for (auto &db: Debris) {
if (db.flags[Debris_Flags::OnHullDebrisList]) {
if ((db.time_started < oldest_time) && !(Objects[db.objnum].flags[Object::Object_Flags::Should_be_dead])) {
oldest_index = DEBRIS_INDEX(&db);
oldest_time = db.time_started;
}
}
}
return oldest_index;
}
#define DEBRIS_ROTVEL_SCALE 5.0f
void calc_debris_physics_properties( physics_info *pi, vec3d *min, vec3d *max, float density );
MONITOR(NumSmallDebris)
MONITOR(NumHullDebris)
/**
* Create debris from an object
*
* @param source_obj Source object
* @param model_num Model number
* @param submodel_num Sub-model number
* @param pos Position in global coordinates
* @param exp_center Explosion center in global coordinates
* @param hull_flag Whether this debris is a chunk of a ship's hull (as opposed to shrapnel)
* @param exp_force Explosion force, used to assign velocity to pieces. 1.0f assigns velocity like before. 2.0f assigns twice as much to non-inherited part of velocity
* @param source_subsys The subsystem this debris came from, if any
*/
object *debris_create(object *source_obj, int model_num, int submodel_num, const vec3d *pos, const vec3d *exp_center, bool hull_flag, float exp_force, const ship_subsys* source_subsys)
{
int parent_objnum;
object *obj;
ship *shipp;
bool vaporize;
parent_objnum = OBJ_INDEX(source_obj);
Assert( (source_obj->type == OBJ_SHIP ) || (source_obj->type == OBJ_GHOST));
Assert( source_obj->instance >= 0 && source_obj->instance < MAX_SHIPS );
shipp = &Ships[source_obj->instance];
vaporize = (shipp->flags[Ship::Ship_Flags::Vaporize]);
obj = debris_create_only(parent_objnum, shipp->ship_info_index, shipp->alt_type_index, shipp->team, -1.0f, -1, model_num, submodel_num, pos, nullptr, hull_flag, vaporize, shipp->debris_damage_type_idx);
if (obj != nullptr)
{
debris_create_set_velocity(&Debris[obj->instance], shipp, exp_center, exp_force, source_subsys);
debris_create_fire_hook(obj, source_obj);
const auto& sip = Ship_info[Ships[source_obj->instance].ship_info_index];
particle::ParticleEffectHandle flame_effect;
if (source_subsys != nullptr) {
if (hull_flag) {
if (source_subsys->system_info->debris_flame_particles.isValid()) {
flame_effect = source_subsys->system_info->debris_flame_particles;
} else {
flame_effect = sip.default_subsys_debris_flame_particles;
}
} else {
if (source_subsys->system_info->shrapnel_flame_particles.isValid()) {
flame_effect = source_subsys->system_info->shrapnel_flame_particles;
} else {
flame_effect = sip.default_subsys_shrapnel_flame_particles;
}
}
} else {
flame_effect = hull_flag ? sip.debris_flame_particles : sip.shrapnel_flame_particles;
}
if (flame_effect.isValid()) {
auto source = particle::ParticleManager::get()->createSource(flame_effect);
source->setHost(std::make_unique<EffectHostObject>(obj, vmd_zero_vector));
source->setTriggerRadius(source_obj->radius);
source->setTriggerVelocity(vm_vec_mag_quick(&source_obj->phys_info.vel));
source->finishCreation();
}
}
return obj;
}
object *debris_create_only(int parent_objnum, int parent_ship_class, int alt_type_index, int team, float hull_strength, int spark_timeout, int model_num, int submodel_num, const vec3d *pos, const matrix *orient, bool hull_flag, bool vaporize, int damage_type_idx)
{
int objnum;
object *obj;
debris *db;
ship_info *sip;
matrix orient_buf;
// try to maintain our soft limit
if (hull_flag && (Num_hull_pieces >= SOFT_LIMIT_DEBRIS_PIECES)) {
// cause oldest hull debris chunk to blow up
int oldest_n = debris_find_oldest();
if (oldest_n >= 0)
debris_start_death_roll(&Objects[Debris[oldest_n].objnum], &Debris[oldest_n]);
}
int n = 0;
for (auto &db_temp: Debris) {
if ( !(db_temp.flags[Debris_Flags::Used]) )
break;
++n;
}
// we might have to create a new slot
if (n == (int)Debris.size()) {
Debris.emplace_back();
}
db = &Debris[n];
// validate or deduce some parameters
if ( pos == nullptr ) {
if (parent_objnum >= 0) {
pos = &Objects[parent_objnum].pos;
} else {
pos = &vmd_zero_vector;
}
}
if ( orient == nullptr ) {
if (parent_objnum >= 0 && hull_flag) {
orient = &Objects[parent_objnum].orient;
} else {
// non-hull debris has no relation to its parent orientation
vec3d rand;
vm_vec_rand_vec(&rand);
vm_vector_2_matrix_norm(&orient_buf, &rand);
orient = &orient_buf;
}
}
sip = (parent_ship_class < 0) ? nullptr : &Ship_info[parent_ship_class];
if (hull_flag && !sip)
{
Warning(LOCATION, "Cannot create hull debris without a ship class!");
return nullptr;
}
if (hull_strength < 0.0f)
{
if (parent_objnum >= 0 && Objects[parent_objnum].type == OBJ_SHIP)
hull_strength = Ships[Objects[parent_objnum].instance].ship_max_hull_strength / 8.0f;
else
hull_strength = 10.0f;
}
if (hull_flag && (model_num < 0 || submodel_num < 0))
{
Warning(LOCATION, "Model and submodel numbers must be specified for hull debris!");
return nullptr;
}
if (team < 0 && hull_flag)
{
if (parent_objnum >= 0 && Objects[parent_objnum].type == OBJ_SHIP)
team = Ships[Objects[parent_objnum].instance].team;
else if (Player_ship)
team = Player_ship->team;
else
team = 0;
}
if (damage_type_idx < 0)
{
if (parent_objnum >= 0 && Objects[parent_objnum].type == OBJ_SHIP)
damage_type_idx = Ships[Objects[parent_objnum].instance].debris_damage_type_idx;
}
// done validation
if (!hull_flag) {
if (model_num >= 0) {
db->model_num = model_num;
db->submodel_num = (sip == nullptr || sip->generic_debris_num_submodels <= 0) ? 0 : Random::next(sip->generic_debris_num_submodels);
}
else if (vaporize) {
db->model_num = Debris_vaporize_model;
db->submodel_num = (Debris_num_submodels <= 0 ? 0 : Random::next(Debris_num_submodels));
}
else {
db->model_num = Debris_model;
db->submodel_num = (Debris_num_submodels <= 0 ? 0 : Random::next(Debris_num_submodels));
}
}
else {
db->model_num = model_num;
db->submodel_num = submodel_num;
}
float radius = submodel_get_radius(db->model_num, db->submodel_num);
// if its generic, maybe cull it if its too small and far
if (!hull_flag) {
float dist = vm_vec_dist_quick(pos, &Eye_position);
// Make vaporize debris seen from farther away
if (vaporize) {
dist /= 2.0f;
}
if (dist > radius * 200.0f) {
return nullptr;
}
}
// Create Debris piece n!
if(hull_flag)
{
// set lifeleft based on tabled entries if they are not negative
// coded originally by WMC then clean-up added by wookieejedi
if(sip->debris_min_lifetime >= 0.0f && sip->debris_max_lifetime >= 0.0f)
{
db->lifeleft = (( sip->debris_max_lifetime - sip->debris_min_lifetime ) * frand()) + sip->debris_min_lifetime;
}
else if(sip->debris_min_lifetime >= 0.0f)
{
if(db->lifeleft < sip->debris_min_lifetime)
db->lifeleft = sip->debris_min_lifetime;
}
else if(sip->debris_max_lifetime >= 0.0f)
{
if(db->lifeleft > sip->debris_max_lifetime)
db->lifeleft = sip->debris_max_lifetime;
}
// By default, make some pieces blow up shortly after explosion.
else if (Random::next() < (Random::MAX_VALUE / 6))
{
db->lifeleft = 2.0f * (frand()) + 0.5f;
}
else
{
db->flags.set(Debris_Flags::DoNotExpire);
db->lifeleft = -1.0f; // large hull pieces stay around forever
}
}
else
{
// small non-hull pieces should stick around longer the larger they are
// sqrtf should make sure its never too crazy long
db->lifeleft = (frand() * 2.0f + 0.1f) * sqrtf(radius);
}
// increase lifetime for vaporized debris
if (vaporize) {
db->lifeleft *= 3.0f;
}
db->flags.set(Debris_Flags::Used);
db->is_hull = hull_flag;
db->source_objnum = parent_objnum;
db->damage_type_idx = damage_type_idx;
db->ship_info_index = parent_ship_class;
db->team = team;
db->ambient_sound = (sip == nullptr) ? gamesnd_id(-1) : sip->debris_ambient_sound;
db->arc_timeout = TIMESTAMP::never(); // if not changed, timestamp_elapsed() will return false
db->time_started = Missiontime;
db->species = (sip == nullptr) ? -1 : sip->species;
db->parent_alt_name = alt_type_index;
db->damage_mult = 1.0f;
db->electrical_arcs.clear();
if ( db->is_hull ) {
// Percent of debris pieces with arcs controlled via table (default 50%)
if (frand() < sip->debris_arc_percent) {
db->arc_frequency = 1000;
} else {
db->arc_frequency = 0;
}
} else {
db->arc_frequency = 0;
}
db->arc_next_time = _timestamp_rand(500,2000); //start one 1/2 - 2 secs later
flagset<Object::Object_Flags> default_flags;
default_flags.set(Object::Object_Flags::Renders);
default_flags.set(Object::Object_Flags::Physics);
default_flags.set(Object::Object_Flags::Collides, hull_flag != 0);
objnum = obj_create(OBJ_DEBRIS, parent_objnum, n, orient, pos, radius, default_flags, false);
if ( objnum == -1 ) {
mprintf(("Couldn't create debris object -- out of object slots\n"));
return nullptr;
}
db->objnum = objnum;
obj = &Objects[objnum];
db->model_instance_num = hull_flag ? model_create_instance(objnum, db->model_num) : -1;
// ensure that any texture replace on parent ship gets copied to debris --wookieejedi
int parent_model_instance_num;
if ((db->model_instance_num >= 0) && (parent_objnum >= 0) && ((parent_model_instance_num = object_get_model_instance_num(&Objects[parent_objnum])) >= 0))
model_get_instance(db->model_instance_num)->texture_replace = model_get_instance(parent_model_instance_num)->texture_replace;
// assign the network signature. The signature will be 0 for non-hull pieces, but since that
// is our invalid signature, it should be okay.
obj->net_signature = 0;
if ( (Game_mode & GM_MULTIPLAYER) && hull_flag ) {
obj->net_signature = multi_get_next_network_signature( MULTI_SIG_DEBRIS );
}
obj->hull_strength = hull_strength;
if (hull_flag) {
float min_hp = sip->debris_min_hitpoints;
float max_hp = sip->debris_max_hitpoints;
if (sip->debris_hitpoints_radius_multi >= 0.0f) {
min_hp *= sip->debris_hitpoints_radius_multi * radius;
max_hp *= sip->debris_hitpoints_radius_multi * radius;
}
if (min_hp >= 0.0f && max_hp >= 0.0f)
{
obj->hull_strength = ((max_hp - min_hp) * frand()) + min_hp;
}
else if(min_hp >= 0.0f)
{
if (obj->hull_strength < min_hp)
obj->hull_strength = min_hp;
}
else if(max_hp >= 0.0f)
{
if (obj->hull_strength > max_hp)
obj->hull_strength = max_hp;
}
db->damage_mult = sip->debris_damage_mult;
db->sound_delay = _timestamp(DEBRIS_SOUND_DELAY);
// limit the amount of time that fireballs appear
// let fireball length be linked to radius of ship. Range is .33 radius => 3.33 radius seconds.
if (spark_timeout >= 0) {
db->arc_timeout = _timestamp(spark_timeout);
} else if (parent_objnum >= 0) {
float t = 1000*Objects[parent_objnum].radius/3 + (fl2i(1000*3*Objects[parent_objnum].radius) == 0 ? 0 : Random::next(fl2i(1000*3*Objects[parent_objnum].radius)));
db->arc_timeout = _timestamp(fl2i(t)); // fireballs last from 5 - 30 seconds
} else {
db->arc_timeout = TIMESTAMP::immediate();
}
if (parent_objnum >= 0 && Objects[parent_objnum].radius >= Min_radius_for_persistent_debris) {
db->flags.set(Debris_Flags::DoNotExpire);
} else {
debris_add_to_hull_list(db);
}
}
db->max_hull = obj->hull_strength;
if (hull_flag) {
MONITOR_INC(NumHullDebris,1);
} else {
MONITOR_INC(NumSmallDebris,1);
}
return obj;
}
void debris_create_set_velocity(const debris *db, const ship *source_shipp, const vec3d *exp_center, float exp_force, const ship_subsys* source_subsys)
{
auto obj = &Objects[db->objnum];
auto source_obj = (source_shipp == nullptr) ? nullptr : &Objects[source_shipp->objnum];
physics_info *pi = &obj->phys_info;
vec3d rotvel, radial_vel, to_center;
if ( exp_center )
vm_vec_sub( &to_center, &obj->pos, exp_center );
else
vm_vec_zero(&to_center);
float scale;
if ( db->is_hull ) {
scale = exp_force * i2fl(Random::next(10, 29)); // for radial_vel away from location of blast center
// set up physics mass and I_inv for hull debris pieces
auto pm = model_get(db->model_num);
vec3d *min, *max;
min = &pm->submodel[db->submodel_num].min;
max = &pm->submodel[db->submodel_num].max;
float density = source_subsys != nullptr ? source_subsys->system_info->density : Ship_info[source_shipp->ship_info_index].debris_density;
calc_debris_physics_properties( &obj->phys_info, min, max, density);
}
else {
scale = exp_force * i2fl(Random::next(10, 29)); // for radial_vel away from blast center (non-hull)
}
if ( vm_vec_mag_squared( &to_center ) < 0.1f ) {
vm_vec_rand_vec_quick(&radial_vel);
vm_vec_scale(&radial_vel, scale );
}
else {
vm_vec_normalize(&to_center);
vm_vec_copy_scale(&radial_vel, &to_center, scale );
}
if (source_obj)
{
// DA: here we need to vel_from_rot = w x to_center, where w is world is unrotated to world coords and offset is the
// displacement fromt the center of the parent object to the center of the debris piece
vec3d world_rotvel, vel_from_rotvel;
vm_vec_unrotate ( &world_rotvel, &source_obj->phys_info.rotvel, &source_obj->orient );
vm_vec_cross ( &vel_from_rotvel, &world_rotvel, &to_center );
vm_vec_scale ( &vel_from_rotvel, DEBRIS_ROTVEL_SCALE);
vm_vec_add (&obj->phys_info.vel, &radial_vel, &source_obj->phys_info.vel);
vm_vec_add2(&obj->phys_info.vel, &vel_from_rotvel);
}
float radius = submodel_get_radius(db->model_num, db->submodel_num);
// make sure rotational velocity does not get too high
if (radius < 1.0) {
radius = 1.0f;
}
scale = (6.0f + i2fl(Random::next(4))) / radius;
vm_vec_rand_vec_quick(&rotvel);
vm_vec_scale(&rotvel, scale);
pi->rotvel = rotvel;
pi->flags |= (PF_DEAD_DAMP | PF_BALLISTIC);
check_rotvel_limit( &obj->phys_info );
// check that debris is not created with too high a velocity
if (db->is_hull)
{
shipfx_debris_limit_speed(db, source_shipp);
}
// blow out his reverse thrusters. Or drag, same thing.
pi->rotdamp = 10000.0f;
if (source_shipp != nullptr) {
pi->gravity_const = Ship_info[source_shipp->ship_info_index].debris_gravity_const;
} else {
pi->gravity_const = 1.0f;
}
vm_vec_zero(&pi->max_vel); // make so he can't turn on his own VOLITION anymore.
vm_vec_zero(&pi->max_rotvel); // make so he can't change speed on his own VOLITION anymore.
// ensure vel is valid
Assert( !vm_is_vec_nan(&obj->phys_info.vel) );
}
void debris_create_fire_hook(object *obj, object *source_obj)
{
if (scripting::hooks::OnDebrisCreated->isActive()) {
scripting::hooks::ShipSourceConditions conditions;
conditions.source_shipp = (source_obj != nullptr && source_obj->type == OBJ_SHIP) ? &Ships[source_obj->instance] : nullptr;
scripting::hooks::OnDebrisCreated->run(std::move(conditions),
scripting::hook_param_list(
scripting::hook_param("Debris", 'o', obj),
scripting::hook_param("Source", 'o', source_obj)));
}
}
/**
* Alas, poor debris_obj got whacked. Fortunately, we know who did it, where and how hard, so we
* can do something about it.
*/
void debris_hit(object *debris_obj, object * /*other_obj*/, vec3d *hitpos, float damage, vec3d* force)
{
debris *debris_p = &Debris[debris_obj->instance];
// Do a little particle spark shower to show we hit
if (Debris_hit_particle.isValid()){ // Where the particles emit from
vec3d tmp_norm;
vm_vec_sub( &tmp_norm, hitpos, &debris_obj->pos );
vm_vec_normalize_safe(&tmp_norm);
matrix orient;
vm_vector_2_matrix_norm(&orient, &tmp_norm);
auto source = particle::ParticleManager::get()->createSource(Debris_hit_particle);
auto host = std::make_unique<EffectHostVector>(*hitpos, orient, debris_obj->phys_info.vel);
host->setRadius(debris_obj->radius);
source->setHost(std::move(host));
source->finishCreation();
}
// multiplayer clients bail here
if(MULTIPLAYER_CLIENT){
return;
}
if ( damage < 0.0f ) {
damage = 0.0f;
}
if (hitpos && force && The_mission.ai_profile->flags[AI::Profile_Flags::Whackable_debris]) {
vec3d rel_hit_pos = *hitpos - debris_obj->pos;
physics_calculate_and_apply_whack(force, &rel_hit_pos, &debris_obj->phys_info, &debris_obj->orient, &debris_obj->phys_info.I_body_inv);
}
debris_obj->hull_strength -= damage;
if (debris_obj->hull_strength < 0.0f) {
debris_start_death_roll(debris_obj, debris_p, hitpos);
} else {
// otherwise, give all the other players an update on the debris
if(MULTIPLAYER_MASTER){
send_debris_update_packet(debris_obj,DEBRIS_UPDATE_UPDATE);
}
}
}
/**
* See if poor debris object *obj got whacked by evil *other_obj at point *hitpos.
* NOTE: debris_hit_info pointer NULL for debris:weapon collision, otherwise debris:ship collision.
* @return true if hit, else return false.
*/
int debris_check_collision(object *pdebris, object *other_obj, vec3d *hitpos, collision_info_struct *debris_hit_info, vec3d* hitNormal)
{
mc_info mc;
Assert( pdebris->type == OBJ_DEBRIS );
int num = pdebris->instance;
Assert( num >= 0 && num < (int)Debris.size() );
Assert( Debris[num].objnum == OBJ_INDEX(pdebris));
// debris_hit_info NULL - so debris-weapon collision
if ( debris_hit_info == NULL ) {
// debris weapon collision
Assert( other_obj->type == OBJ_WEAPON );
mc.model_instance_num = -1;
mc.model_num = Debris[num].model_num; // Fill in the model to check
mc.submodel_num = Debris[num].submodel_num;
mc.orient = &pdebris->orient; // The object's orient
mc.pos = &pdebris->pos; // The object's position
mc.p0 = &other_obj->last_pos; // Point 1 of ray to check
mc.p1 = &other_obj->pos; // Point 2 of ray to check
mc.flags = (MC_CHECK_MODEL | MC_SUBMODEL);
if (model_collide(&mc)) {
*hitpos = mc.hit_point_world;
if (hitNormal)
{
vec3d normal;
if (mc.model_instance_num >= 0)
model_instance_local_to_global_dir(&normal, &mc.hit_normal, mc.model_instance_num, mc.hit_submodel, mc.orient);
else
model_local_to_global_dir(&normal, &mc.hit_normal, mc.model_num, mc.hit_submodel, mc.orient);
*hitNormal = normal;
}
}
weapon *wp = &Weapons[other_obj->instance];
wp->collisionInfo = new mc_info; // The weapon will free this memory later
*wp->collisionInfo = mc;
return mc.num_hits;
}
// debris ship collision -- use debris_hit_info to calculate physics
object *pship_obj = other_obj;
Assert( pship_obj->type == OBJ_SHIP );
object *heavy = debris_hit_info->heavy;
object *lighter = debris_hit_info->light;
object *heavy_obj = heavy;
object *light_obj = lighter;
vec3d zero, p0, p1;
vm_vec_zero(&zero);
vm_vec_sub(&p0, &lighter->last_pos, &heavy->last_pos);
vm_vec_sub(&p1, &lighter->pos, &heavy->pos);
mc.pos = &zero; // The object's position
mc.p0 = &p0; // Point 1 of ray to check
mc.p1 = &p1; // Point 2 of ray to check
// find the light object's position in the heavy object's reference frame at last frame and also in this frame.
vec3d p0_temp, p0_rotated;
// Collision detection from rotation enabled if at rotation is less than 30 degree in frame
// This should account for all ships
if ( (vm_vec_mag_squared(&heavy->phys_info.rotvel) * flFrametime*flFrametime) < (PI*PI/36) ) {
// collide_rotate calculate (1) start position and (2) relative velocity
debris_hit_info->collide_rotate = true;
vm_vec_rotate(&p0_temp, &p0, &heavy->last_orient);
vm_vec_unrotate(&p0_rotated, &p0_temp, &heavy->orient);
mc.p0 = &p0_rotated; // Point 1 of ray to check
vm_vec_sub(&debris_hit_info->light_rel_vel, &p1, &p0_rotated);
vm_vec_scale(&debris_hit_info->light_rel_vel, 1/flFrametime);
} else {
debris_hit_info->collide_rotate = false;
vm_vec_sub(&debris_hit_info->light_rel_vel, &lighter->phys_info.vel, &heavy->phys_info.vel);
}