forked from scp-fs2open/fs2open.github.com
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbeam.cpp
More file actions
4590 lines (3834 loc) · 150 KB
/
Copy pathbeam.cpp
File metadata and controls
4590 lines (3834 loc) · 150 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 <algorithm>
#include "asteroid/asteroid.h"
#include "cmdline/cmdline.h"
#include "debris/debris.h"
#include "debugconsole/console.h"
#include "freespace.h"
#include "gamesnd/gamesnd.h"
#include "globalincs/linklist.h"
#include "graphics/color.h"
#include "hud/hudets.h"
#include "hud/hudmessage.h"
#include "hud/hudshield.h"
#include "iff_defs/iff_defs.h"
#include "io/timer.h"
#include "lighting/lighting.h"
#include "lighting/lighting_profiles.h"
#include "math/fvi.h"
#include "math/curve.h"
#include "math/staticrand.h"
#include "nebula/neb.h"
#include "mod_table/mod_table.h"
#include "network/multi.h"
#include "network/multimsgs.h"
#include "object/objcollide.h"
#include "object/object.h"
#include "object/objectshield.h"
#include "parse/parselo.h"
#include "prop/prop.h"
#include "scripting/global_hooks.h"
#include "scripting/scripting.h"
#include "scripting/api/objs/model.h"
#include "scripting/api/objs/vecmath.h"
#include "particle/particle.h"
#include "playerman/player.h"
#include "render/3d.h"
#include "ship/ship.h"
#include "ship/shipfx.h"
#include "ship/shiphit.h"
#include "weapon/beam.h"
#include "weapon/weapon.h"
#include "globalincs/globals.h"
#include "globalincs/vmallocator.h"
#include "tracing/tracing.h"
// ------------------------------------------------------------------------------------------------
// BEAM WEAPON DEFINES/VARS
//
// this is the constant which defines when a beam is an "area" beam. meaning, when we switch on sphereline checking and when
// a beam gets "stopped" by an object. It is a percentage of the object radius which the beam must be wider than
#define BEAM_AREA_PERCENT 0.4f
// randomness factor - all beam weapon aiming is adjusted by +/- some factor within this range
#define BEAM_RANDOM_FACTOR 0.4f
#define MAX_SHOT_POINTS 30
#define SHOT_POINT_TIME 200 // 5 arcs a second
#define TOOLTIME 1500.0f
std::array<beam, MAX_BEAMS> Beams; // all beams
beam Beam_free_list; // free beams
beam Beam_used_list; // used beams
int Beam_count = 0; // how many beams are in use
// debug stuff - keep track of how many collision tests we perform a second and how many we toss a second
#define BEAM_TEST_STAMP_TIME 4000 // every 4 seconds
int Beam_test_stamp = -1;
int Beam_test_ints = 0;
int Beam_test_ship = 0;
int Beam_test_ast = 0;
int Beam_test_framecount = 0;
// beam warmup completion %
#define BEAM_WARMUP_PCT(b) ( ((float)Weapon_info[b->weapon_info_index].b_info.beam_warmup - (float)timestamp_until(b->warmup_stamp)) / (float)Weapon_info[b->weapon_info_index].b_info.beam_warmup )
// beam warmdown completion %
#define BEAM_WARMDOWN_PCT(b) ( ((float)Weapon_info[b->weapon_info_index].b_info.beam_warmdown - (float)timestamp_until(b->warmdown_stamp)) / (float)Weapon_info[b->weapon_info_index].b_info.beam_warmdown )
// link into the physics paused system
extern int physics_paused;
// beam lighting info
#define MAX_BEAM_LIGHT_INFO 100
typedef struct beam_light_info {
beam *bm; // beam casting the light
int objnum; // object getting light cast on it
ubyte source; // 0 to light the shooter, 1 for lighting any ship the beam passes, 2 to light the collision ship
vec3d c_point; // collision point for type 2 lights
} beam_light_info;
beam_light_info Beam_lights[MAX_BEAM_LIGHT_INFO];
int Beam_light_count = 0;
float b_whack_small = 2000.0f; // used to be 500.0f with the retail whack bug
float b_whack_big = 10000.0f; // used to be 1500.0f with the retail whack bug
float b_whack_damage = 150.0f;
DCF(b_whack_small, "Sets the whack factor for small whacks (Default is 2000f)")
{
dc_stuff_float(&b_whack_small);
}
DCF(b_whack_big, "Sets the whack factor for big whacks (Default is 10000f)")
{
dc_stuff_float(&b_whack_big);
}
DCF(b_whack_damage, "Sets the whack damage threshold (Default is 150f)")
{
if (dc_optional_string_either("help", "--help")) {
dc_printf("Sets the threshold to determine whether a big whack or a small whack should be applied. Values equal or greater than this threshold will trigger a big whack, while smaller values will trigger a small whack\n");
return;
}
dc_stuff_float(&b_whack_damage);
}
// ------------------------------------------------------------------------------------------------
// BEAM WEAPON FORWARD DECLARATIONS
//
// delete a beam
void beam_delete(beam *b);
// handle a hit on a specific object
void beam_handle_collisions(beam *b);
// fills in binfo
void beam_get_binfo(beam* b, float accuracy, int num_shots, int burst_seed, float burst_shot_rotation, float per_burst_shot_rotation);
// aim the beam (setup last_start and last_shot - the endpoints). also recalculates object collision info
void beam_aim(beam *b);
// direct fire type functions
void beam_type_direct_fire_move(beam *b);
// slashing type functions
void beam_type_slashing_move(beam *b);
// targeting type functions
void beam_type_targeting_move(beam *b);
// antifighter type functions
void beam_type_antifighter_move(beam *b);
// stuffs the index of the current pulse in shot_index
// stuffs 0 in fire_wait if the beam is active, 1 if it is between pulses
void beam_type_antifighter_get_status(beam *b, int *shot_index, int *fire_wait);
// normal firing type functions
void beam_type_normal_move(beam *b);
// given a model #, and an object, stuff 2 good world coord points
void beam_get_octant_points(int modelnum, object *objp, int seed, vec3d *v1, vec3d *v2);
// given an object, return its model num
int beam_get_model(object *objp);
// for rendering the beam effect
// output top and bottom vectors
// fvec == forward vector (eye viewpoint basically. in world coords)
// pos == world coordinate of the point we're calculating "around"
// w == width of the diff between top and bottom around pos
void beam_calc_facing_pts(vec3d *top, vec3d *bot, vec3d *fvec, vec3d *pos, float w, float z_add);
// render the muzzle glow for a beam weapon
void beam_render_muzzle_glow(beam *b);
// generate particles for the muzzle glow
void beam_generate_muzzle_particles(beam *b);
// throw some jitter into the aim - based upon shot_aim
void beam_jitter_aim(beam *b, float aim);
// if it is legal for the beam to continue firing
// returns -1 if the beam should stop firing immediately
// returns 0 if the beam should go to warmdown
// returns 1 if the beam can continue along its way
int beam_ok_to_fire(beam *b);
// start the warmup phase for the beam
void beam_start_warmup(beam *b);
// start the firing phase for the beam, return 0 if the beam failed to start, and should be deleted altogether
int beam_start_firing(beam *b);
// start the warmdown phase for the beam
void beam_start_warmdown(beam *b);
// add a collision to the beam for this frame (to be evaluated later)
void beam_add_collision(beam *b, object *hit_object, mc_info *cinfo, int quad = -1, bool exit_flag = false);
// mark an object as being lit
void beam_add_light(beam *b, int objnum, int source, vec3d *c_point);
// apply lighting from any beams
void beam_apply_lighting();
// recalculate beam sounds (looping sounds relative to the player)
void beam_recalc_sounds(beam *b);
// apply a whack to a ship
void beam_apply_whack(beam *b, object *objp, vec3d *hit_point);
// if the beam is likely to tool a given target before its lifetime expires
int beam_will_tool_target(beam *b, object *objp);
// ------------------------------------------------------------------------------------------------
// BEAM WEAPON FUNCTIONS
//
// init at game startup
void beam_init()
{
beam_level_close();
}
// initialize beam weapons for this level
void beam_level_init()
{
// intialize beams
int idx;
Beam_count = 0;
list_init( &Beam_free_list );
list_init( &Beam_used_list );
Beams.fill({});
// Link all object slots into the free list
for (idx=0; idx<MAX_BEAMS; idx++) {
Beams[idx].objnum = -1;
list_append(&Beam_free_list, &Beams[idx] );
}
// reset muzzle particle spew timestamp
}
// shutdown beam weapons for this level
void beam_level_close()
{
// clear the beams
list_init( &Beam_free_list );
list_init( &Beam_used_list );
}
// get the width of the widest section of the beam
float beam_get_widest(beam* b)
{
int idx;
float widest = -1.0f;
// sanity
Assert(b->weapon_info_index >= 0);
if (b->weapon_info_index < 0) {
return -1.0f;
}
// lookup
for (idx = 0; idx < Weapon_info[b->weapon_info_index].b_info.beam_num_sections; idx++) {
if (Weapon_info[b->weapon_info_index].b_info.sections[idx].width > widest) {
widest = Weapon_info[b->weapon_info_index].b_info.sections[idx].width;
}
}
// return
return widest;
}
static void beam_set_state(weapon_info* wip, beam* bm, WeaponState state)
{
if (bm->weapon_state == state)
{
// No change
return;
}
bm->weapon_state = state;
auto map_entry = wip->state_effects.find(bm->weapon_state);
if ((map_entry != wip->state_effects.end()) && map_entry->second.isValid())
{
auto source = particle::ParticleManager::get()->createSource(map_entry->second);
source->setHost(std::make_unique<EffectHostBeam>(&Objects[bm->objnum]));
source->finishCreation();
}
}
// return false if the particular beam fire method doesn't have all the required, and specific to its fire method, info
bool beam_has_valid_params(beam_fire_info* fire_info) {
switch (fire_info->fire_method) {
case BFM_TURRET_FIRED:
if (fire_info->shooter == nullptr || fire_info->turret == nullptr || fire_info->target == nullptr)
return false;
break;
case BFM_TURRET_FORCE_FIRED:
if (fire_info->shooter == nullptr || fire_info->turret == nullptr || (fire_info->target == nullptr && !(fire_info->bfi_flags & BFIF_TARGETING_COORDS)))
return false;
break;
case BFM_FIGHTER_FIRED:
if (fire_info->shooter == nullptr || fire_info->turret == nullptr)
return false;
break;
case BFM_SPAWNED:
case BFM_SEXP_FLOATING_FIRED:
if (!(fire_info->bfi_flags & BFIF_FLOATING_BEAM) || (fire_info->target == nullptr && !(fire_info->bfi_flags & BFIF_TARGETING_COORDS)))
return false;
break;
case BFM_SUBSPACE_STRIKE:
if (!(fire_info->bfi_flags & BFIF_FLOATING_BEAM) || (fire_info->target == nullptr))
return false;
break;
default:
Assertion(false, "Unrecognized beam fire method in beam_has_valid_params");
return false;
}
// let's also validate the type of target, if applicable
if (fire_info->target != nullptr) {
if ((fire_info->target->type != OBJ_SHIP) && (fire_info->target->type != OBJ_ASTEROID) && (fire_info->target->type != OBJ_DEBRIS) && (fire_info->target->type != OBJ_WEAPON))
return false;
}
return true;
}
// fire a beam, returns nonzero on success. the innards of the code handle all the rest, foo
int beam_fire(beam_fire_info *fire_info)
{
beam *new_item;
weapon_info *wip;
ship *firing_ship = NULL;
int objnum;
// sanity check
if(fire_info == NULL){
Int3();
return -1;
}
// if we're out of beams, bail
if(Beam_count >= MAX_BEAMS){
return -1;
}
// make sure the beam_info_index is valid
if ((fire_info->beam_info_index < 0) || (fire_info->beam_info_index >= weapon_info_size()) || !(Weapon_info[fire_info->beam_info_index].wi_flags[Weapon::Info_Flags::Beam])) {
UNREACHABLE("beam_info_index (%d) invalid (either <0, >= %d, or not actually a beam)!\n", fire_info->beam_info_index, weapon_info_size());
return -1;
}
wip = &Weapon_info[fire_info->beam_info_index];
// copied from weapon_create()
if ((wip->num_substitution_patterns > 0) && (fire_info->shooter != nullptr)) {
// using substitution
// get to the instance of the gun
Assertion(fire_info->shooter->type == OBJ_SHIP, "Expected type OBJ_SHIP, got %d", fire_info->shooter->type);
Assertion((fire_info->shooter->instance < MAX_SHIPS) && (fire_info->shooter->instance >= 0),
"Ship index is %d, which is out of range [%d,%d)", fire_info->shooter->instance, 0, MAX_SHIPS);
ship* parent_shipp = &(Ships[fire_info->shooter->instance]);
Assert(parent_shipp != nullptr);
size_t* position = get_pointer_to_weapon_fire_pattern_index(fire_info->beam_info_index, fire_info->shooter->instance, fire_info->turret);
Assertion(position != nullptr, "'%s' is trying to fire a weapon that is not selected", Ships[fire_info->shooter->instance].ship_name);
size_t curr_pos = *position;
if ((parent_shipp->flags[Ship::Ship_Flags::Primary_linked]) && curr_pos > 0) {
curr_pos--;
}
++(*position);
*position = (*position) % wip->num_substitution_patterns;
if (wip->weapon_substitution_pattern[curr_pos] == -1) {
// weapon doesn't want any sub
return -1;
}
else if (wip->weapon_substitution_pattern[curr_pos] != fire_info->beam_info_index) {
fire_info->beam_info_index = wip->weapon_substitution_pattern[curr_pos];
// weapon wants to sub with weapon other than me
return beam_fire(fire_info);
}
}
if (!beam_has_valid_params(fire_info))
return -1;
if (fire_info->shooter != NULL) {
firing_ship = &Ships[fire_info->shooter->instance];
}
// get a free beam
new_item = GET_FIRST(&Beam_free_list);
Assert( new_item != &Beam_free_list ); // shouldn't have the dummy element
if(new_item == &Beam_free_list){
return -1;
}
// make sure that our textures are loaded as well
extern bool weapon_is_used(int weapon_index);
extern void weapon_load_bitmaps(int weapon_index);
if ( !weapon_is_used(fire_info->beam_info_index) ) {
weapon_load_bitmaps(fire_info->beam_info_index);
}
// remove from the free list
list_remove( &Beam_free_list, new_item );
// insert onto the end of used list
list_append( &Beam_used_list, new_item );
// increment counter
Beam_count++;
// fill in some values
new_item->warmup_stamp = -1;
new_item->warmdown_stamp = -1;
new_item->weapon_info_index = fire_info->beam_info_index;
new_item->objp = fire_info->shooter;
new_item->sig = (fire_info->shooter != NULL) ? fire_info->shooter->signature : 0;
new_item->subsys = fire_info->turret;
new_item->life_left = wip->b_info.beam_life;
new_item->life_total = wip->b_info.beam_life;
new_item->r_collision_count = 0;
new_item->f_collision_count = 0;
new_item->target = fire_info->target;
new_item->target_subsys = fire_info->target_subsys;
new_item->target_sig = (fire_info->target != NULL) ? fire_info->target->signature : 0;
new_item->beam_sound_loop = sound_handle::invalid();
new_item->type = wip->b_info.beam_type;
new_item->local_fire_postion = fire_info->local_fire_postion;
new_item->framecount = 0;
new_item->flags = 0;
new_item->shot_index = 0;
new_item->current_width_factor = wip->b_info.beam_initial_width < 0.1f ? 0.1f : wip->b_info.beam_initial_width;
new_item->team = (firing_ship == NULL) ? fire_info->team : static_cast<char>(firing_ship->team);
new_item->range = wip->b_info.range;
new_item->damage_threshold = wip->b_info.damage_threshold;
new_item->bank = fire_info->bank;
new_item->beam_glow_frame = 0.0f;
new_item->firingpoint = (fire_info->bfi_flags & BFIF_FLOATING_BEAM) ? -1 : fire_info->turret->turret_next_fire_pos;
new_item->last_start = fire_info->starting_pos;
new_item->type5_rot_speed = wip->b_info.t5info.continuous_rot;
new_item->rotates = wip->b_info.beam_type == BeamType::OMNI && wip->b_info.t5info.continuous_rot_axis != Type5BeamRotAxis::UNSPECIFIED;
new_item->modular_curves_instance = wip->beam_curves.create_instance();
if (fire_info->bfi_flags & BFIF_FORCE_FIRING)
new_item->flags |= BF_FORCE_FIRING;
if (fire_info->bfi_flags & BFIF_IS_FIGHTER_BEAM)
new_item->flags |= BF_IS_FIGHTER_BEAM;
if (fire_info->bfi_flags & BFIF_FLOATING_BEAM)
new_item->flags |= BF_FLOATING_BEAM;
if (fire_info->bfi_flags & BFIF_TARGETING_COORDS) {
new_item->flags |= BF_TARGETING_COORDS;
new_item->target_pos1 = fire_info->target_pos1;
new_item->target_pos2 = fire_info->target_pos2;
} else {
vm_vec_zero(&new_item->target_pos1);
vm_vec_zero(&new_item->target_pos2);
}
for (float &frame : new_item->beam_section_frame)
frame = 0.0f;
// beam collision and light width
if (wip->b_info.beam_width > 0.0f) {
new_item->beam_collide_width = wip->b_info.beam_width;
new_item->beam_light_width = wip->b_info.beam_width;
} else {
float widest = beam_get_widest(new_item);
new_item->beam_collide_width = wip->collision_radius_override > 0.0f ? wip->collision_radius_override : widest;
new_item->beam_light_width = widest;
}
if (fire_info->bfi_flags & BFIF_IS_FIGHTER_BEAM && new_item->type != BeamType::OMNI) {
new_item->type = BeamType::TARGETING;
}
// if the targeted subsystem is not NULL, force it to be a direct fire beam
if(new_item->target_subsys != nullptr && new_item->type != BeamType::TARGETING && new_item->type != BeamType::OMNI){
new_item->type = BeamType::DIRECT_FIRE;
}
// antifighter beam weapons can only fire at small ships and missiles
if(new_item->type == BeamType::ANTIFIGHTER){
// if its a targeted ship, get the target ship
if((fire_info->target != NULL) && (fire_info->target->type == OBJ_SHIP) && (fire_info->target->instance >= 0)){
ship *target_ship = &Ships[fire_info->target->instance];
// maybe force to be direct fire
if(Ship_info[target_ship->ship_info_index].class_type > -1 && (Ship_types[Ship_info[target_ship->ship_info_index].class_type].flags[Ship::Type_Info_Flags::Beams_easily_hit])){
new_item->type = BeamType::DIRECT_FIRE;
}
}
}
// ----------------------------------------------------------------------
// THIS IS THE CRITICAL POINT FOR MULTIPLAYER
// beam_get_binfo(...) determines exactly how the beam will behave over the course of its life
// it fills in binfo, which we can pass to clients in multiplayer
if(fire_info->beam_info_override != NULL){
new_item->binfo = *fire_info->beam_info_override;
} else {
float burst_rot = 0.0f;
if (new_item->type == BeamType::OMNI && !wip->b_info.t5info.burst_rot_pattern.empty()) {
burst_rot = wip->b_info.t5info.burst_rot_pattern[fire_info->burst_index % wip->b_info.t5info.burst_rot_pattern.size()];
}
beam_get_binfo(new_item, fire_info->accuracy, wip->b_info.beam_shots,fire_info->burst_seed, burst_rot, fire_info->per_burst_rotation); // to fill in b_info - the set of directional aim vectors
}
flagset<Object::Object_Flags> default_flags;
if (!wip->wi_flags[Weapon::Info_Flags::No_collide])
default_flags.set(Object::Object_Flags::Collides);
if (wip->wi_flags[Weapon::Info_Flags::Can_damage_shooter])
default_flags.set(Object::Object_Flags::Collides_with_parent);
// create the associated object
objnum = obj_create(OBJ_BEAM, ((fire_info->shooter != NULL) ? OBJ_INDEX(fire_info->shooter) : -1), BEAM_INDEX(new_item), &vmd_identity_matrix, &vmd_zero_vector, 1.0f, default_flags);
if(objnum < 0){
beam_delete(new_item);
mprintf(("obj_create() failed for a beam weapon because you are running out of object slots!\n"));
return -1;
}
new_item->objnum = objnum;
if (new_item->objp != nullptr && Weapons_inherit_parent_collision_group) {
Objects[objnum].collision_group_id = new_item->objp->collision_group_id;
}
// this sets up all info for the first frame the beam fires
beam_aim(new_item); // to fill in shot_point, etc.
// check to see if its legal to fire at this guy
if (beam_ok_to_fire(new_item) != 1) {
beam_delete(new_item);
mprintf(("Killing beam at initial fire because of illegal targeting!!!\n"));
return -1;
}
// if we're a multiplayer master - send a packet
if (MULTIPLAYER_MASTER) {
send_beam_fired_packet(fire_info, &new_item->binfo);
}
// start the warmup phase
beam_start_warmup(new_item);
//Do particles
if (wip->b_info.beam_muzzle_effect.isValid()) {
auto source = particle::ParticleManager::get()->createSource(wip->b_info.beam_muzzle_effect);
std::unique_ptr<EffectHost> host;
if (new_item->objp == nullptr) {
vec3d beam_dir = new_item->last_shot - new_item->last_start;
matrix orient;
vm_vector_2_matrix(&orient, &beam_dir);
host = std::make_unique<EffectHostVector>(new_item->last_start, orient, vmd_zero_vector);
}
else if (new_item->subsys == nullptr || new_item->firingpoint < 0) {
vec3d beam_dir = new_item->last_shot - new_item->last_start;
matrix orient;
vm_vector_2_matrix(&orient, &beam_dir);
vec3d local_pos = new_item->last_start - new_item->objp->pos;
vm_vec_rotate(&local_pos, &local_pos, &new_item->objp->orient);
orient = new_item->objp->orient * orient;
host = std::make_unique<EffectHostObject>(new_item->objp, local_pos, orient);
}
else {
bool is_fighterbeam = false;
if (new_item->objp->type == OBJ_SHIP) {
auto shipp = &Ships[new_item->objp->instance];
if (new_item->subsys == &shipp->fighter_beam_turret_data) {
is_fighterbeam = true;
}
}
host = std::make_unique<EffectHostTurret>(new_item->objp, new_item->subsys->system_info->turret_gun_sobj, new_item->firingpoint, is_fighterbeam);
}
source->setHost(std::move(host));
source->setTriggerRadius(wip->b_info.beam_muzzle_radius);
source->finishCreation();
}
return objnum;
}
// fire a targeting beam, returns objnum on success. a much much simplified version of a beam weapon
// targeting lasers last _one_ frame. For a continuous stream - they must be created every frame.
// this allows it to work smoothly in multiplayer (detect "trigger down". every frame just create a targeting laser firing straight out of the
// object. this way you get all the advantages of nice rendering and collisions).
// NOTE : only references beam_info_index and shooter
int beam_fire_targeting(fighter_beam_fire_info *fire_info)
{
beam *new_item;
weapon_info *wip;
int objnum;
ship *firing_ship;
// sanity check
if(fire_info == NULL){
Int3();
return -1;
}
// if we're out of beams, bail
if(Beam_count >= MAX_BEAMS){
return -1;
}
// make sure the beam_info_index is valid
Assert((fire_info->beam_info_index >= 0) && (fire_info->beam_info_index < weapon_info_size()) && (Weapon_info[fire_info->beam_info_index].wi_flags[Weapon::Info_Flags::Beam]));
if((fire_info->beam_info_index < 0) || (fire_info->beam_info_index >= weapon_info_size()) || !(Weapon_info[fire_info->beam_info_index].wi_flags[Weapon::Info_Flags::Beam])){
return -1;
}
wip = &Weapon_info[fire_info->beam_info_index];
// make sure a ship is firing this
Assert((fire_info->shooter->type == OBJ_SHIP) && (fire_info->shooter->instance >= 0) && (fire_info->shooter->instance < MAX_SHIPS));
if ( (fire_info->shooter->type != OBJ_SHIP) || (fire_info->shooter->instance < 0) || (fire_info->shooter->instance >= MAX_SHIPS) ) {
return -1;
}
firing_ship = &Ships[fire_info->shooter->instance];
// get a free beam
new_item = GET_FIRST(&Beam_free_list);
Assert( new_item != &Beam_free_list ); // shouldn't have the dummy element
// remove from the free list
list_remove( &Beam_free_list, new_item );
// insert onto the end of used list
list_append( &Beam_used_list, new_item );
// increment counter
Beam_count++;
// maybe allocate some extra data based on the beam type
Assert(wip->b_info.beam_type == BeamType::TARGETING);
if(wip->b_info.beam_type != BeamType::TARGETING){
return -1;
}
// fill in some values
new_item->warmup_stamp = fire_info->warmup_stamp;
new_item->warmdown_stamp = fire_info->warmdown_stamp;
new_item->weapon_info_index = fire_info->beam_info_index;
new_item->objp = fire_info->shooter;
new_item->sig = fire_info->shooter->signature;
new_item->subsys = NULL;
new_item->life_left = fire_info->life_left;
new_item->life_total = fire_info->life_total;
new_item->r_collision_count = 0;
new_item->f_collision_count = 0;
new_item->target = NULL;
new_item->target_subsys = NULL;
new_item->target_sig = 0;
new_item->beam_sound_loop = sound_handle::invalid();
new_item->type = BeamType::TARGETING;
new_item->local_fire_postion = fire_info->local_fire_postion;
new_item->framecount = 0;
new_item->flags = 0;
new_item->shot_index = 0;
new_item->current_width_factor = wip->b_info.beam_initial_width < 0.1f ? 0.1f : wip->b_info.beam_initial_width;
new_item->team = (char)firing_ship->team;
new_item->range = wip->b_info.range;
new_item->damage_threshold = wip->b_info.damage_threshold;
// beam collision and light width
if (wip->b_info.beam_width > 0.0f) {
new_item->beam_collide_width = wip->b_info.beam_width;
new_item->beam_light_width = wip->b_info.beam_width;
}
else {
float widest = beam_get_widest(new_item);
new_item->beam_collide_width = wip->collision_radius_override > 0.0f ? wip->collision_radius_override : widest;
new_item->beam_light_width = widest;
}
// targeting type beams are a very special weapon type - binfo has no meaning
flagset<Object::Object_Flags> initial_flags;
if (!wip->wi_flags[Weapon::Info_Flags::No_collide])
initial_flags.set(Object::Object_Flags::Collides);
// create the associated object
objnum = obj_create(OBJ_BEAM, OBJ_INDEX(fire_info->shooter), BEAM_INDEX(new_item), &vmd_identity_matrix, &vmd_zero_vector, 1.0f, initial_flags);
if(objnum < 0){
beam_delete(new_item);
nprintf(("General", "obj_create() failed for beam weapon! bah!\n"));
return -1;
}
new_item->objnum = objnum;
// this sets up all info for the first frame the beam fires
beam_aim(new_item); // to fill in shot_point, etc.
if(Beams[Objects[objnum].instance].objnum != objnum){
Int3();
return -1;
}
return objnum;
}
// return an object index of the guy who's firing this beam
int beam_get_parent(const object *bm)
{
beam *b;
// get a handle to the beam
Assert(bm->type == OBJ_BEAM);
Assert(bm->instance >= 0);
if(bm->type != OBJ_BEAM){
return -1;
}
if(bm->instance < 0){
return -1;
}
b = &Beams[bm->instance];
if(b->objp == NULL){
return -1;
}
// if the object handle is invalid
if(b->objp->signature != b->sig){
return -1;
}
// return the handle
return OBJ_INDEX(b->objp);
}
// return weapon_info_index of beam
int beam_get_weapon_info_index(const object *bm)
{
Assert(bm->type == OBJ_BEAM);
if (bm->type != OBJ_BEAM) {
return -1;
}
Assert(bm->instance >= 0 && bm->instance < MAX_BEAMS);
if (bm->instance < 0) {
return -1;
}
//make sure it's returning a valid info index
Assert((Beams[bm->instance].weapon_info_index > -1) && (Beams[bm->instance].weapon_info_index < weapon_info_size()));
// return weapon_info_index
return Beams[bm->instance].weapon_info_index;
}
// given a beam object, get the # of collisions which happened during the last collision check (typically, last frame)
int beam_get_num_collisions(int objnum)
{
// sanity checks
if((objnum < 0) || (objnum >= MAX_OBJECTS)){
Int3();
return -1;
}
if((Objects[objnum].instance < 0) || (Objects[objnum].instance >= MAX_BEAMS)){
Int3();
return -1;
}
if(Beams[Objects[objnum].instance].objnum != objnum){
Int3();
return -1;
}
if(Beams[Objects[objnum].instance].objnum < 0){
Int3();
return -1;
}
// return the # of recent collisions
return Beams[Objects[objnum].instance].r_collision_count;
}
// stuff collision info, returns 1 on success
int beam_get_collision(int objnum, int num, int *collision_objnum, mc_info **cinfo)
{
// sanity checks
if((objnum < 0) || (objnum >= MAX_OBJECTS)){
Int3();
return 0;
}
if((Objects[objnum].instance < 0) || (Objects[objnum].instance >= MAX_BEAMS)){
Int3();
return 0;
}
if((Beams[Objects[objnum].instance].objnum != objnum) || (Beams[Objects[objnum].instance].objnum < 0)){
Int3();
return 0;
}
if(num >= Beams[Objects[objnum].instance].r_collision_count){
Int3();
return 0;
}
// return - success
*cinfo = &Beams[Objects[objnum].instance].r_collisions[num].cinfo;
*collision_objnum = Beams[Objects[objnum].instance].r_collisions[num].c_objnum;
return 1;
}
// pause all looping beam sounds
void beam_pause_sounds()
{
beam *moveup = NULL;
// set all beam volumes to 0
moveup = GET_FIRST(&Beam_used_list);
if(moveup == NULL){
return;
}
while(moveup != END_OF_LIST(&Beam_used_list)){
// set the volume to 0, if he has a looping beam sound
if (moveup->beam_sound_loop.isValid()) {
snd_set_volume(moveup->beam_sound_loop, 0.0f);
}
// next beam
moveup = GET_NEXT(moveup);
}
}
// unpause looping beam sounds
void beam_unpause_sounds()
{
beam *moveup = NULL;
// recalc all beam sounds
moveup = GET_FIRST(&Beam_used_list);
if(moveup == NULL){
return;
}
while(moveup != END_OF_LIST(&Beam_used_list)){
if (Cmdline_no_3d_sound) {
beam_recalc_sounds(moveup);
} else {
if (moveup->beam_sound_loop.isValid()) {
snd_set_volume(moveup->beam_sound_loop, 1.0f);
}
}
// next beam
moveup = GET_NEXT(moveup);
}
}
void beam_get_global_turret_gun_info(const object *objp, const ship_subsys *ssp, vec3d *gpos, bool avg_origin, vec3d *gvec, bool use_angles, const vec3d *targetp, bool fighter_beam)
{
if (fighter_beam)
{
ship_get_global_turret_gun_info(objp, ssp, gpos, avg_origin, nullptr, use_angles, targetp);
*gvec = objp->orient.vec.fvec;
}
else
ship_get_global_turret_gun_info(objp, ssp, gpos, avg_origin, gvec, use_angles, targetp);
}
// -----------------------------===========================------------------------------
// BEAM MOVEMENT FUNCTIONS
// -----------------------------===========================------------------------------
// move a direct fire type beam weapon
void beam_type_direct_fire_move(beam *b)
{
vec3d dir;
// LEAVE THIS HERE OTHERWISE MUZZLE GLOWS DRAW INCORRECTLY WHEN WARMING UP OR DOWN
// get the "originating point" of the beam for this frame. essentially bashes last_start
if (b->subsys != NULL)
beam_get_global_turret_gun_info(b->objp, b->subsys, &b->last_start, false, nullptr, true, nullptr, (b->flags & BF_IS_FIGHTER_BEAM) != 0);
// if the "warming up" timestamp has not expired
if((b->warmup_stamp != -1) || (b->warmdown_stamp != -1)){
return;
}
// put the "last_shot" point arbitrarily far away
vm_vec_sub(&dir, &b->last_shot, &b->last_start);
vm_vec_normalize_quick(&dir);
vm_vec_scale_add(&b->last_shot, &b->last_start, &dir, b->range);
Assert(is_valid_vec(&b->last_shot));
}
// move a slashing type beam weapon
#define BEAM_T(b) ((b->life_total - b->life_left) / b->life_total)
void beam_type_slashing_move(beam *b)
{
vec3d actual_dir;
float dot_save;
// LEAVE THIS HERE OTHERWISE MUZZLE GLOWS DRAW INCORRECTLY WHEN WARMING UP OR DOWN
// get the "originating point" of the beam for this frame. essentially bashes last_start
if (b->subsys != NULL)
beam_get_global_turret_gun_info(b->objp, b->subsys, &b->last_start, false, nullptr, true, nullptr, (b->flags & BF_IS_FIGHTER_BEAM) != 0);
// if the "warming up" timestamp has not expired
if((b->warmup_stamp != -1) || (b->warmdown_stamp != -1)){
return;
}
// see if we are sharing a firing direction with another beam that is not this beam
// (beam_aim took care of most of the bookkeeping)
if ((b->subsys != nullptr) && (b->subsys->shared_fire_direction_beam_objnum >= 0) && (b->subsys->shared_fire_direction_beam_objnum != b->objnum)) {
auto shared_candidate_objp = &Objects[b->subsys->shared_fire_direction_beam_objnum];
auto shared_fire_reference = &Beams[shared_candidate_objp->instance];
// use the same vector, then we're done
vec3d beam_vector;
vm_vec_sub(&beam_vector, &shared_fire_reference->last_shot, &shared_fire_reference->last_start);
vm_vec_add(&b->last_shot, &b->last_start, &beam_vector);
return;
}
// if the two direction vectors are _really_ close together, just use the original direction
dot_save = vm_vec_dot(&b->binfo.dir_a, &b->binfo.dir_b);
if((double)dot_save >= 0.999999999){
actual_dir = b->binfo.dir_a;
}
// otherwise move towards the dir we calculated when firing this beam
else {
vm_vec_interp_constant(&actual_dir, &b->binfo.dir_a, &b->binfo.dir_b, BEAM_T(b));
}
// now recalculate shot_point to be shooting through our new point
vm_vec_scale_add(&b->last_shot, &b->last_start, &actual_dir, b->range);
bool is_valid = is_valid_vec(&b->last_shot);
Assert(is_valid);
if(!is_valid){
actual_dir = b->binfo.dir_a;
vm_vec_scale_add(&b->last_shot, &b->last_start, &actual_dir, b->range);
}
}
// targeting type beams functions
void beam_type_targeting_move(beam *b)
{
// If floating and targeting, synthesize orientation from known points
// because the object could be validly nullptr as when fired in the lab
if ((b->flags & BF_FLOATING_BEAM) && (b->flags & BF_TARGETING_COORDS)) {
// Compute forward vector from start to target
vec3d fvec;
vm_vec_sub(&fvec, &b->target_pos1, &b->last_start);
vm_vec_normalize_safe(&fvec);
// Final beam endpoint
vm_vec_scale_add(&b->last_shot, &b->last_start, &fvec, b->range);
} else {
Assertion(b->objp != nullptr, "Targeting beam does not have a valid parent object!");
Assertion(b->objp->instance >= 0, "Targeting beam parent object instance is invalid!");
// Standard case: beam fired from a real object
// start point
vm_vec_unrotate(&b->last_start, &b->local_fire_postion, &b->objp->orient);
vm_vec_add2(&b->last_start, &b->objp->pos);
// end point
vm_vec_scale_add(&b->last_shot, &b->last_start, &b->objp->orient.vec.fvec, b->range);
}
}
// antifighter type beam functions
void beam_type_antifighter_move(beam *b)
{
int shot_index, fire_wait;
vec3d dir;
// LEAVE THIS HERE OTHERWISE MUZZLE GLOWS DRAW INCORRECTLY WHEN WARMING UP OR DOWN
// get the "originating point" of the beam for this frame. essentially bashes last_start
if (b->subsys != NULL)
beam_get_global_turret_gun_info(b->objp, b->subsys, &b->last_start, false, nullptr, true, nullptr, (b->flags & BF_IS_FIGHTER_BEAM) != 0);
// if the "warming up" timestamp has not expired
if((b->warmup_stamp != -1) || (b->warmdown_stamp != -1)){
return;
}