-
Notifications
You must be signed in to change notification settings - Fork 182
Expand file tree
/
Copy pathobject.cpp
More file actions
2334 lines (1943 loc) · 66 KB
/
Copy pathobject.cpp
File metadata and controls
2334 lines (1943 loc) · 66 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 "asteroid/asteroid.h"
#include "cmeasure/cmeasure.h"
#include "debris/debris.h"
#include "debugconsole/console.h"
#include "fireball/fireballs.h"
#include "freespace.h"
#include "globalincs/linklist.h"
#include "globalincs/pstypes.h"
#include "globalincs/vmallocator.h"
#include "iff_defs/iff_defs.h"
#include "io/timer.h"
#include "jumpnode/jumpnode.h"
#include "lighting/lighting.h"
#include "lighting/lighting_profiles.h"
#include "mission/missionparse.h" //For 2D Mode
#include "network/multi.h"
#include "network/multiutil.h"
#include "network//multi_obj.h"
#include "object/deadobjectdock.h"
#include "object/objcollide.h"
#include "object/object.h"
#include "object/objectdock.h"
#include "object/objectshield.h"
#include "object/objectsnd.h"
#include "observer/observer.h"
#include "prop/prop.h"
#include "scripting/global_hooks.h"
#include "scripting/api/libs/graphics.h"
#include "scripting/scripting.h"
#include "playerman/player.h"
#include "radar/radar.h"
#include "radar/radarsetup.h"
#include "render/3d.h"
#include "ship/afterburner.h"
#include "ship/ship.h"
#include "starfield/starfield.h"
#include "tracing/tracing.h"
#include "weapon/beam.h"
#include "weapon/shockwave.h"
#include "weapon/swarm.h"
#include "weapon/weapon.h"
#include "tracing/Monitor.h"
#include "graphics/light.h"
#include "graphics/color.h"
#include "math/curve.h"
extern void ship_reset_disabled_physics(object *objp, int ship_class);
/*
* Global variables
*/
object obj_free_list;
object obj_used_list;
object obj_create_list;
object *Player_obj = NULL;
object *Viewer_obj = NULL;
//Data for objects
object Objects[MAX_OBJECTS];
SCP_map<int, raw_pof_obj> Pof_objects;
#ifdef OBJECT_CHECK
checkobject CheckObjects[MAX_OBJECTS];
#endif
int Num_objects=-1;
int Highest_object_index=-1;
int Highest_ever_object_index=0;
int Object_next_signature = 1; //0 is bogus, start at 1
int Object_inited = 0;
int Show_waypoints = 0;
object_h::object_h(int in_objnum)
: objnum(in_objnum)
{
if (objnum >= 0 && objnum < MAX_OBJECTS)
sig = Objects[objnum].signature;
else
objnum = -1;
}
object_h::object_h(const object* in_objp)
{
if (in_objp)
{
objnum = OBJ_INDEX(in_objp);
sig = in_objp->signature;
}
}
object_h::object_h()
{}
bool object_h::isValid() const
{
// a signature of 0 is invalid, per obj_init()
if (objnum < 0 || sig <= 0 || objnum >= MAX_OBJECTS)
return false;
return Objects[objnum].signature == sig;
}
object* object_h::objp() const
{
return &Objects[objnum];
}
object* object_h::objp_or_null() const
{
return isValid() ? &Objects[objnum] : nullptr;
}
//WMC - Made these prettier
const char *Object_type_names[MAX_OBJECT_TYPES] = {
//XSTR:OFF
"None",
"Ship",
"Weapon",
"Fireball",
"Start",
"Waypoint",
"Debris",
"Countermeasure",
"Ghost",
"Point",
"Shockwave",
"Wing",
"Observer",
"Asteroid",
"Jump Node",
"Beam",
"Raw Pof"
//XSTR:ON
};
obj_flag_name Object_flag_names[] = {
{ Object::Object_Flags::Invulnerable, "invulnerable", },
{ Object::Object_Flags::Protected, "protect-ship", },
{ Object::Object_Flags::Beam_protected, "beam-protect-ship", },
{ Object::Object_Flags::No_shields, "no-shields", },
{ Object::Object_Flags::Targetable_as_bomb, "targetable-as-bomb", },
{ Object::Object_Flags::Flak_protected, "flak-protect-ship", },
{ Object::Object_Flags::Laser_protected, "laser-protect-ship", },
{ Object::Object_Flags::Missile_protected, "missile-protect-ship", },
{ Object::Object_Flags::Immobile, "immobile", },
{ Object::Object_Flags::Dont_change_position, "don't-change-position", },
{ Object::Object_Flags::Dont_change_orientation, "don't-change-orientation", },
{ Object::Object_Flags::Collides, "collides", },
{ Object::Object_Flags::Attackable_if_no_collide, "ai-attackable-if-no-collide", },
};
obj_flag_description Object_flag_descriptions[] = {
{ Object::Object_Flags::Invulnerable, "Stops this object from taking any damage."},
{ Object::Object_Flags::Protected, "Ship and Turret AI will ignore and not attack this object."},
{ Object::Object_Flags::Beam_protected, "Turrets with beam weapons will ignore and not attack this object."},
{ Object::Object_Flags::No_shields, "This object will have no shields. (If this object can otherwise have shields, its shield energy will be fully reallocated to other ETS systems.)"},
{ Object::Object_Flags::Targetable_as_bomb, "Allows this object to be targeted with the bomb targeting key."},
{ Object::Object_Flags::Flak_protected, "Turrets with flak weapons will ignore and not attack this object."},
{ Object::Object_Flags::Laser_protected, "Turrets with laser weapons will ignore and not attack this object."},
{ Object::Object_Flags::Missile_protected, "Turrets with missile weapons will ignore and not attack this object."},
{ Object::Object_Flags::Immobile, "Will not let an object change position or orientation. Upon destruction it will still do the death roll and explosion."},
{ Object::Object_Flags::Dont_change_position, "Will not let an object change position. Upon destruction it will still do the death roll and explosion."},
{ Object::Object_Flags::Dont_change_orientation, "Will not let an object change orientation. Upon destruction it will still do the death roll and explosion."},
{ Object::Object_Flags::Collides, "This object will collide with other objects."},
{ Object::Object_Flags::Attackable_if_no_collide, "Allows the AI to attack this object, even if no-collide is set. (Normally an object that does not collide is also not attacked.)"},
{ Object::Object_Flags::Player_ship, "Ship is a player ship."},
{ Object::Object_Flags::Special_warpin, "Ship uses the special Knossos warp-in animation."},
};
extern const int Num_object_flag_names = sizeof(Object_flag_names) / sizeof(obj_flag_name);
extern const size_t Num_object_flag_descriptions = sizeof(Object_flag_descriptions) / sizeof(obj_flag_description);
#ifdef OBJECT_CHECK
checkobject::checkobject()
: type(0), signature(0), parent_sig(0)
{
flags.reset();
}
#endif
// all we need to set are the pointers, but type, parent, and instance are useful to set as well
object::object()
: next(nullptr), prev(nullptr), signature(0), type(OBJ_NONE), parent(-1), parent_sig(0), instance(-1), pos(vmd_zero_vector), orient(vmd_identity_matrix),
radius(0.0f), last_pos(vmd_zero_vector), last_orient(vmd_identity_matrix), hull_strength(0.0f), sim_hull_strength(0.0f), net_signature(0), num_pairs(0),
dock_list(nullptr), dead_dock_list(nullptr), collision_group_id(0)
{
memset(&(this->phys_info), 0, sizeof(physics_info));
}
object::~object()
{
dock_free_dock_list(this);
dock_free_dead_dock_list(this);
}
// DO NOT set next and prev to NULL because they keep the object on the free and used lists
void object::clear()
{
signature = num_pairs = collision_group_id = 0;
parent = parent_sig = instance = -1;
type = OBJ_NONE;
flags.reset();
pos = last_pos = vmd_zero_vector;
orient = last_orient = vmd_identity_matrix;
radius = hull_strength = sim_hull_strength = 0.0f;
physics_init( &phys_info );
shield_quadrant.clear();
objsnd_num.clear();
net_signature = 0;
pre_move_event.clear();
post_move_event.clear();
// just in case nobody called obj_delete last mission
dock_free_dock_list(this);
dock_free_dead_dock_list(this);
}
/**
* Scan the object list, freeing down to num_used objects
*
* @param target_num_used Number of used objects to free down to
* @return Returns number of slots freed
*/
int free_object_slots(int target_num_used)
{
int i, olind, deleted_weapons;
int obj_list[MAX_OBJECTS];
int num_already_free, num_to_free, original_num_to_free;
object *objp;
olind = 0;
// calc num_already_free by walking the obj_free_list
num_already_free = 0;
for ( objp = GET_FIRST(&obj_free_list); objp != END_OF_LIST(&obj_free_list); objp = GET_NEXT(objp) )
num_already_free++;
if (MAX_OBJECTS - num_already_free < target_num_used)
return 0;
for ( objp = GET_FIRST(&obj_used_list); objp != END_OF_LIST(&obj_used_list); objp = GET_NEXT(objp) ) {
if (objp->flags[Object::Object_Flags::Should_be_dead]) {
num_already_free++;
if (MAX_OBJECTS - num_already_free < target_num_used)
return num_already_free;
} else
switch (objp->type) {
case OBJ_NONE:
num_already_free++;
if (MAX_OBJECTS - num_already_free < target_num_used)
return 0;
break;
case OBJ_FIREBALL:
case OBJ_WEAPON:
case OBJ_DEBRIS:
// case OBJ_CMEASURE:
obj_list[olind++] = OBJ_INDEX(objp);
break;
case OBJ_GHOST:
case OBJ_SHIP:
case OBJ_START:
case OBJ_WAYPOINT:
case OBJ_POINT:
case OBJ_SHOCKWAVE:
case OBJ_WING:
case OBJ_OBSERVER:
case OBJ_ASTEROID:
case OBJ_JUMP_NODE:
case OBJ_BEAM:
case OBJ_RAW_POF:
case OBJ_PROP:
break;
default:
Int3(); // Hey, what kind of object is this? Unknown!
break;
}
}
num_to_free = MAX_OBJECTS - target_num_used - num_already_free;
original_num_to_free = num_to_free;
if (num_to_free > olind) {
nprintf(("allender", "Warning: Asked to free %i objects, but can only free %i.\n", num_to_free, olind));
num_to_free = olind;
}
for (i=0; i<num_to_free; i++)
if ( (Objects[obj_list[i]].type == OBJ_DEBRIS) && (!Debris[Objects[obj_list[i]].instance].flags[Debris_Flags::DoNotExpire]) ) {
num_to_free--;
nprintf(("allender", "Freeing DEBRIS object %3i\n", obj_list[i]));
Objects[obj_list[i]].flags.set(Object::Object_Flags::Should_be_dead);
}
if (num_to_free <= 0) {
return original_num_to_free;
}
for (i=0; i<num_to_free; i++) {
object *tmp_obj = &Objects[obj_list[i]];
if ( (tmp_obj->type == OBJ_FIREBALL) && (fireball_is_perishable(tmp_obj)) ) {
num_to_free--;
if (num_to_free <= 0) {
return original_num_to_free;
}
nprintf(("allender", "Freeing FIREBALL object %3i\n", obj_list[i]));
tmp_obj->flags.set(Object::Object_Flags::Should_be_dead);
}
}
deleted_weapons = collide_remove_weapons();
num_to_free -= deleted_weapons;
if ( num_to_free <= 0){
return original_num_to_free;
}
for (i=0; i<num_to_free; i++){
if ( Objects[obj_list[i]].type == OBJ_WEAPON ) {
num_to_free--;
Objects[obj_list[i]].flags.set(Object::Object_Flags::Should_be_dead);
}
}
if (!num_to_free){
return original_num_to_free;
}
return original_num_to_free - num_to_free;
}
// Goober5000
// This helper function does not check the object type and is not intended as a public API.
float get_hull_or_sim_hull_pct_helper(const object *objp, const float object::*strength_field, const ship *shipp, bool allow_negative)
{
Assert(objp && shipp);
if (!objp || !shipp)
return 0.0f;
float total_strength = shipp->ship_max_hull_strength;
Assert(total_strength > 0.0f); // unlike shield, no ship can have 0 hull
if (total_strength == 0.0f)
return 0.0f;
if (!allow_negative && objp->*strength_field < 0.0f) // this sometimes happens when a ship is being destroyed
return 0.0f;
return objp->*strength_field / total_strength;
}
float get_hull_pct(const object *objp, bool allow_negative)
{
Assert(objp && objp->type == OBJ_SHIP);
if (!objp || objp->type != OBJ_SHIP)
return 0.0f;
return get_hull_or_sim_hull_pct_helper(objp, &object::hull_strength, &Ships[objp->instance], allow_negative);
}
float get_hull_pct(const ship_registry_entry *ship_entry, bool allow_negative)
{
return get_hull_or_sim_hull_pct_helper(ship_entry->objp(), &object::hull_strength, ship_entry->shipp(), allow_negative);
}
float get_sim_hull_pct(const object *objp, bool allow_negative)
{
Assert(objp && objp->type == OBJ_SHIP);
if (!objp || objp->type != OBJ_SHIP)
return 0.0f;
return get_hull_or_sim_hull_pct_helper(objp, &object::sim_hull_strength, &Ships[objp->instance], allow_negative);
}
float get_sim_hull_pct(const ship_registry_entry *ship_entry, bool allow_negative)
{
return get_hull_or_sim_hull_pct_helper(ship_entry->objp(), &object::sim_hull_strength, ship_entry->shipp(), allow_negative);
}
// Goober5000
// This helper function does not check the object type and is not intended as a public API.
float get_shield_pct_helper(const object *objp, const ship *shipp)
{
Assert(objp && shipp);
if (!objp || !shipp)
return 0.0f;
float total_strength = shield_get_max_strength(shipp);
if (total_strength == 0.0f)
return 0.0f;
return shield_get_strength(objp) / total_strength;
}
float get_shield_pct(const object *objp)
{
Assert(objp);
if (!objp)
return 0.0f;
// bah - we might have asteroids
if (objp->type != OBJ_SHIP)
return 0.0f;
return get_shield_pct_helper(objp, &Ships[objp->instance]);
}
float get_shield_pct(const ship_registry_entry *ship_entry)
{
return get_shield_pct_helper(ship_entry->objp(), ship_entry->shipp());
}
static void on_script_state_destroy(lua_State*) {
// Since events are mostly used for scripting, we clear the event handlers when the Lua state is destroyed
for (auto& obj : Objects) {
obj.pre_move_event.clear();
obj.post_move_event.clear();
}
}
/**
* Sets up the free list & init player & whatever else
*/
void obj_init()
{
int i;
object *objp;
Object_inited = 1;
for (i = 0; i < MAX_OBJECTS; ++i)
Objects[i].clear();
Viewer_obj = NULL;
list_init( &obj_free_list );
list_init( &obj_used_list );
list_init( &obj_create_list );
// Link all object slots into the free list
objp = Objects;
for (i=0; i<MAX_OBJECTS; i++) {
list_append(&obj_free_list, objp);
objp++;
}
Object_next_signature = 1; //0 is invalid, others start at 1
Num_objects = 0;
Highest_object_index = 0;
obj_reset_colliders();
Script_system.OnStateDestroy.add(on_script_state_destroy);
}
void obj_shutdown()
{
for (auto& obj : Objects) {
obj.clear();
}
}
static int num_objects_hwm = 0;
/**
* Allocates an object
*
* Generally, obj_create() should be called to get an object, since it
* fills in important fields and does the linking.
*
* @return the number of a free object, updating Highest_object_index
* @return -1 if no free objects
*/
int obj_allocate(bool essential)
{
int objnum;
object *objp;
if (!Object_inited) {
mprintf(("Why hasn't obj_init() been called yet?\n"));
obj_init();
}
if ( (Num_objects >= MAX_OBJECTS-10) && essential ) {
int num_freed;
num_freed = free_object_slots(MAX_OBJECTS-10);
nprintf(("warning", " *** Freed %i objects\n", num_freed));
}
if (Num_objects >= MAX_OBJECTS) {
mprintf(("Object creation failed - too many objects!\n" ));
return -1;
}
// Find next available object
objp = GET_FIRST(&obj_free_list);
Assert ( objp != &obj_free_list ); // shouldn't have the dummy element
// remove objp from the free list
list_remove( &obj_free_list, objp );
// insert objp onto the end of create list
list_append( &obj_create_list, objp );
// increment counter
Num_objects++;
if (Num_objects > num_objects_hwm) {
num_objects_hwm = Num_objects;
}
// get objnum
objnum = OBJ_INDEX(objp);
if (objnum > Highest_object_index) {
Highest_object_index = objnum;
if (Highest_object_index > Highest_ever_object_index)
Highest_ever_object_index = Highest_object_index;
}
return objnum;
}
/**
* Frees up an object
*
* Generally, obj_delete() should be called to get rid of an object.
* This function deallocates the object entry after the object has been unlinked
*/
void obj_free(int objnum)
{
object *objp;
if (!Object_inited) {
mprintf(("Why hasn't obj_init() been called yet?\n"));
obj_init();
}
Assert( objnum >= 0 ); // Trying to free bogus object!!!
// get object pointer
objp = &Objects[objnum];
// remove objp from the used list
list_remove( &obj_used_list, objp );
// add objp to the end of the free
list_append( &obj_free_list, objp );
// decrement counter
Num_objects--;
Objects[objnum].type = OBJ_NONE;
Assert(Num_objects >= 0);
if (objnum == Highest_object_index) {
while (Highest_object_index >= 0 && Objects[Highest_object_index].type == OBJ_NONE) {
--Highest_object_index;
}
}
}
/**
* Create a raw pof item
* @return the objnum of this raw POF
*/
int obj_raw_pof_create(const char* pof_filename, const matrix* orient, const vec3d* pos)
{
static int next_raw_pof_id = 0;
// Unlikely this would ever be hit.. but just in case
if (next_raw_pof_id >= INT_MAX || next_raw_pof_id < 0) {
Error(LOCATION, "Too many RAW_POF objects created!");
return -1;
}
if (!VALID_FNAME(pof_filename)) {
Warning(LOCATION, "Invalid tech model POF: %s", pof_filename);
return -1;
}
int model_num = model_load(pof_filename);
if (model_num < 0) {
Warning(LOCATION, "Failed to load tech model: %s", pof_filename);
return -1;
}
int id = next_raw_pof_id++;
Pof_objects[id] = {model_num, -1, {}};
flagset<Object::Object_Flags> flags;
flags.set(Object::Object_Flags::Renders);
int objnum = obj_create(OBJ_RAW_POF, -1, id, orient, pos, model_get_radius(model_num), flags);
Pof_objects[id].model_instance = model_create_instance(objnum, model_num);
return objnum;
}
/**
* Initialize a new object. Adds to the list for the given segment.
*
* The object will be a non-rendering, non-physics object. Pass -1 if no parent.
* @return the object number
*/
int obj_create(ubyte type, int parent_obj, int instance, const matrix *orient,
const vec3d *pos, float radius, const flagset<Object::Object_Flags> &flags, bool essential)
{
int objnum;
object *obj;
// Find next free object
objnum = obj_allocate(essential);
if (objnum == -1) //no free objects
return -1;
obj = &Objects[objnum];
Assert(obj->type == OBJ_NONE); //make sure unused
// clear object in preparation for setting of custom values
obj->clear();
Assert(Object_next_signature > 0); // 0 is bogus!
obj->signature = Object_next_signature++;
obj->type = type;
obj->instance = instance;
obj->parent = parent_obj;
if (obj->parent != -1) {
obj->parent_sig = Objects[parent_obj].signature;
} else {
obj->parent_sig = obj->signature;
}
obj->flags = flags;
obj->flags.set(Object::Object_Flags::Not_in_coll);
if (pos) {
obj->pos = *pos;
obj->last_pos = *pos;
}
if (orient) {
obj->orient = *orient;
obj->last_orient = *orient;
}
obj->radius = radius;
obj->shield_quadrant.resize(DEFAULT_SHIELD_SECTIONS); // Might be changed by the ship creation code
return objnum;
}
void obj_delete_all()
{
int counter = 0;
for (int i = 0; i < MAX_OBJECTS; ++i)
{
if (Objects[i].type == OBJ_NONE)
continue;
++counter;
obj_delete(i);
}
mprintf(("Cleanup: Deleted %i objects\n", counter));
}
/**
* Remove object from the world
* If Player_obj, don't remove it!
*
* @param objnum Object number to remove
*/
void obj_delete(int objnum)
{
object *objp;
Assert(objnum >= 0 && objnum < MAX_OBJECTS);
objp = &Objects[objnum];
if (objp->type == OBJ_NONE) {
mprintf(("obj_delete() called for already deleted object %d.\n", objnum));
return;
};
// Remove all object pairs
obj_remove_collider(objnum);
switch( objp->type ) {
case OBJ_WEAPON:
weapon_delete( objp );
break;
case OBJ_SHIP:
if ((objp == Player_obj) && !Fred_running) {
objp->type = OBJ_GHOST;
objp->flags.remove(Object::Object_Flags::Should_be_dead);
// we have to traverse the ship_obj list and remove this guy from it as well
ship_obj *moveup = GET_FIRST(&Ship_obj_list);
while(moveup != END_OF_LIST(&Ship_obj_list)){
if(OBJ_INDEX(objp) == moveup->objnum){
list_remove(&Ship_obj_list,moveup);
break;
}
moveup = GET_NEXT(moveup);
}
physics_init(&objp->phys_info);
obj_snd_delete_type(OBJ_INDEX(objp));
return;
} else
ship_delete( objp );
break;
case OBJ_FIREBALL:
fireball_delete( objp );
break;
case OBJ_SHOCKWAVE:
shockwave_delete( objp );
break;
case OBJ_START:
case OBJ_WAYPOINT:
case OBJ_POINT:
break; // requires no action, handled by the Fred code.
case OBJ_JUMP_NODE:
break; // requires no further action, handled by jumpnode deconstructor.
case OBJ_DEBRIS:
debris_delete( objp );
break;
case OBJ_ASTEROID:
asteroid_delete(objp);
break;
/* case OBJ_CMEASURE:
cmeasure_delete( objp );
break;*/
case OBJ_GHOST:
if(!(Game_mode & GM_MULTIPLAYER)){
mprintf(("Warning: Tried to delete a ghost!\n"));
objp->flags.remove(Object::Object_Flags::Should_be_dead);
return;
} else {
// we need to be able to delete GHOST objects in multiplayer to allow for player respawns.
nprintf(("Network","Deleting GHOST object\n"));
objp->net_signature = 0;
}
break;
case OBJ_OBSERVER:
observer_delete(objp);
break;
case OBJ_BEAM:
break;
case OBJ_RAW_POF:
model_delete_instance(Pof_objects[objp->instance].model_instance);
Pof_objects.erase(objp->instance);
break;
case OBJ_PROP:
prop_delete(objp);
break;
case OBJ_NONE:
Int3();
break;
default:
Error( LOCATION, "Unhandled object type %d in obj_delete_all_that_should_be_dead", objp->type );
}
// this avoids include issues from physics state, multi interpolate and object code
extern void multi_interpolate_clear_helper(int objnum);
// clean up interpolation info
multi_interpolate_clear_helper(objnum);
// delete any dock information we still have
dock_free_dock_list(objp);
dock_free_dead_dock_list(objp);
// if a persistant sound has been created, delete it
obj_snd_delete_type(OBJ_INDEX(objp));
objp->type = OBJ_NONE; //unused!
objp->signature = 0;
obj_free(objnum);
}
// ------------------------------------------------------------------------------------------------------------------
void obj_delete_all_that_should_be_dead()
{
object *objp, *temp;
if (!Object_inited) {
mprintf(("Why hasn't obj_init() been called yet?\n"));
obj_init();
}
// Move all objects
objp = GET_FIRST(&obj_used_list);
while( objp !=END_OF_LIST(&obj_used_list) ) {
// Goober5000 - HACK HACK HACK - see obj_move_all
objp->flags.remove(Object::Object_Flags::Docked_already_handled);
temp = GET_NEXT(objp);
if ( objp->flags[Object::Object_Flags::Should_be_dead] )
obj_delete( OBJ_INDEX(objp) ); // MWA says that john says that let obj_delete handle everything because of the editor
objp = temp;
}
}
/**
* Add all newly created objects to the end of the used list and create their
* object pairs for collision detection
*/
void obj_merge_created_list(void)
{
// The old way just merged the two. This code takes one out of the create list,
// creates object pairs for it, and then adds it to the used list.
// OLD WAY: list_merge( &obj_used_list, &obj_create_list );
object *objp = GET_FIRST(&obj_create_list);
while( objp !=END_OF_LIST(&obj_create_list) ) {
list_remove( obj_create_list, objp );
// Add it to the object pairs array
obj_add_collider(OBJ_INDEX(objp));
// Then add it to the object used list
list_append( &obj_used_list, objp );
objp = GET_FIRST(&obj_create_list);
}
// Make sure the create list is empty.
list_init(&obj_create_list);
}
int physics_paused = 0, ai_paused = 0;
// Goober5000
extern void call_doa(object *child, object *parent);
void obj_move_one_docked_object(object *objp, object *parent_objp)
{
// in FRED, just move and return
if (Fred_running)
{
call_doa(objp, parent_objp);
return;
}
// support ships (and anyone else, for that matter) don't keep up if they're undocking
ai_info *aip = &Ai_info[Ships[objp->instance].ai_index];
if ( (aip->mode == AIM_DOCK) && (aip->submode >= AIS_UNDOCK_1) )
{
if (aip->goal_objnum == OBJ_INDEX(parent_objp))
{
return;
}
}
// check the guy that I'm docked with and don't move if he's undocking from me
ai_info *other_aip = &Ai_info[Ships[parent_objp->instance].ai_index];
if ( (other_aip->mode == AIM_DOCK) && (other_aip->submode >= AIS_UNDOCK_1) )
{
if (other_aip->goal_objnum == OBJ_INDEX(objp))
{
return;
}
}
// we're here, so we move with our parent object
call_doa(objp, parent_objp);
}
/**
* Deals with firing player things like lasers, missiles, etc.
*
* Separated out because of multiplayer issues.
*/
void obj_player_fire_stuff( object *objp, control_info ci )
{
ship *shipp;
Assert( objp->flags[Object::Object_Flags::Player_ship]);
// try and get the ship pointer
shipp = NULL;
if((objp->type == OBJ_SHIP) && (objp->instance >= 0) && (objp->instance < MAX_SHIPS)){
shipp = &Ships[objp->instance];
} else {
return;
}
ship_weapon* swp = &shipp->weapons;
// single player pilots, and all players in multiplayer take care of firing their own primaries
if(!(Game_mode & GM_MULTIPLAYER) || (objp == Player_obj))
{
if ( ci.fire_primary_count ) {
// flag the ship as having the trigger down
if(shipp != NULL){
shipp->flags.set(Ship::Ship_Flags::Trigger_down);
swp->flags.set(Ship::Weapon_Flags::Primary_trigger_down);
}
// fire non-streaming primaries here
// Cyborg17, this is where the inaccurate multi shots are being shot...
// so let's let the new system take over instead by excluding client player shots
// on the server.
if (!(MULTIPLAYER_MASTER) || (objp == Player_obj)) {
ship_fire_primary(objp);
}
} else {
// unflag the ship as having the trigger down
if(shipp != NULL){
shipp->flags.remove(Ship::Ship_Flags::Trigger_down);
swp->flags.remove(Ship::Weapon_Flags::Primary_trigger_down);
ship_stop_fire_primary(objp); //if it hasn't fired do the "has just stoped fireing" stuff
}
}
if ( ci.fire_countermeasure_count ) {
ship_launch_countermeasure( objp );
}
}
// single player and multiplayer masters do all of the following
if ( !MULTIPLAYER_CLIENT
// Cyborg17 - except clients now fire dumbfires for rollback on the server
|| !(Weapon_info[swp->secondary_bank_weapons[shipp->weapons.current_secondary_bank]].is_homing())) {
if (ci.fire_secondary_count) {
if ( !ship_start_secondary_fire(objp) ) {
ship_fire_secondary( objp );
}
// kill the secondary count
ci.fire_secondary_count = 0;
} else {
if ( ship_stop_secondary_fire(objp) ) {
ship_fire_secondary( objp );
}
}
}
if ( MULTIPLAYER_CLIENT && objp == Player_obj ) {
if (Weapon_info[swp->secondary_bank_weapons[shipp->weapons.current_secondary_bank]].trigger_lock) {
if (ci.fire_secondary_count) {
ship_start_secondary_fire(objp);
} else {
ship_stop_secondary_fire(objp);
}
}
}
// everyone does the following for their own ships.
if ( ci.afterburner_start ){
if (Ships[objp->instance].flags[Ship::Ship_Flags::Maneuver_despite_engines] || !ship_subsystems_blown(&Ships[objp->instance], SUBSYSTEM_ENGINE)) {
afterburners_start( objp );
}
}
if ( ci.afterburner_stop ){
afterburners_stop( objp, 1 );
}
}
void obj_move_call_physics(object *objp, float frametime)
{
TRACE_SCOPE(tracing::Physics);
// Do physics for objects with OF_PHYSICS flag set and with some engine strength remaining.
if ( objp->flags[Object::Object_Flags::Physics] ) {
// only set phys info if ship is not dead
if ((objp->type == OBJ_SHIP) && !(Ships[objp->instance].flags[Ship::Ship_Flags::Dying])) {
ship *shipp = &Ships[objp->instance];
if (!shipp->flags[Ship::Ship_Flags::Maneuver_despite_engines]) {
bool engines_blown = ship_subsystems_blown(shipp, SUBSYSTEM_ENGINE);
if ( ship_subsys_disrupted(shipp, SUBSYSTEM_ENGINE) ) {
engines_blown = true;
}
if (engines_blown) { // All this is necessary to make ship gradually come to a stop after engines are blown.
vm_vec_zero(&objp->phys_info.desired_vel);
vm_vec_zero(&objp->phys_info.desired_rotvel);
vm_mat_zero(&objp->phys_info.ai_desired_orient);
objp->phys_info.flags |= (PF_REDUCED_DAMP | PF_DEAD_DAMP);