Skip to content

Commit 8e23ce8

Browse files
authored
Add feature to customize how CMs are launched (scp-fs2open#7224)
1 parent f6588f8 commit 8e23ce8

3 files changed

Lines changed: 52 additions & 12 deletions

File tree

code/cmeasure/cmeasure.cpp

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,36 +25,50 @@ const float CMEASURE_DETONATE_DISTANCE = 40.0f;
2525

2626
//Used to set a countermeasure velocity after being launched from a ship as a countermeasure
2727
//ie not as a primary or secondary.
28-
void cmeasure_set_ship_launch_vel(object *objp, object *parent_objp, int arand)
28+
void cmeasure_set_ship_launch_vel(object* objp, object* parent_objp, int arand)
2929
{
30-
vec3d vel, rand_vec;
30+
vec3d vel;
31+
vec3d rand_vec;
32+
vec3d world_dir;
3133

32-
//Get cmeasure rear velocity in world
33-
vm_vec_scale_add(&vel, &parent_objp->phys_info.vel, &parent_objp->orient.vec.fvec, -25.0f);
34+
weapon* wp = &Weapons[objp->instance];
35+
auto* wip = &Weapon_info[wp->weapon_info_index];
36+
37+
// Start with parent velocity
38+
vel = parent_objp->phys_info.vel;
39+
40+
// Normalize direction
41+
vec3d local_dir = wip->cm_launch_vec;
42+
if (vm_vec_mag_squared(&local_dir) > 0.0f) {
43+
vm_vec_normalize(&local_dir);
44+
}
3445

35-
//Get random velocity vector
36-
static_randvec(arand+1, &rand_vec);
46+
// Convert from ship to world space
47+
vm_vec_unrotate(&world_dir, &local_dir, &parent_objp->orient);
3748

38-
//Add it to the rear velocity
39-
vm_vec_scale_add2(&vel, &rand_vec, 2.0f);
49+
// Apply launch impulse
50+
vm_vec_scale_add2(&vel, &world_dir, wip->cm_launch_speed);
51+
52+
// Add random variance
53+
static_randvec(arand + 1, &rand_vec);
54+
vm_vec_scale_add2(&vel, &rand_vec, wip->cm_launch_variance);
4055

4156
objp->phys_info.vel = vel;
4257

4358
//Zero out this stuff so it isn't moving
4459
vm_vec_zero(&objp->phys_info.rotvel);
4560
vm_vec_zero(&objp->phys_info.max_vel);
4661
vm_vec_zero(&objp->phys_info.max_rotvel);
47-
62+
4863
// blow out his reverse thrusters. Or drag, same thing.
4964
objp->phys_info.rotdamp = 10000.0f;
5065
objp->phys_info.side_slip_time_const = 10000.0f;
5166

52-
objp->phys_info.max_vel.xyz.z = -25.0f;
53-
vm_vec_copy_scale(&objp->phys_info.desired_vel, &objp->orient.vec.fvec, objp->phys_info.max_vel.xyz.z );
67+
// Desired velocity should align with launch direction, not hardcoded backward
68+
vm_vec_copy_scale(&objp->phys_info.desired_vel, &world_dir, wip->cm_launch_speed);
5469

5570
// if this cmeasure has a single segment trail, let the trail know since we just changed the velocity
5671
// yeah this is hacky but that's what this function gets for CHANGING the velocity on objects with a ""CONSTANT VELOCITY""
57-
weapon* wp = &Weapons[objp->instance];
5872
if (wp->trail_ptr && wp->trail_ptr->single_segment) {
5973
wp->trail_ptr->vel[0] = objp->phys_info.vel;
6074
wp->trail_ptr->vel[1] = objp->phys_info.vel;

code/weapon/weapon.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,9 @@ struct weapon_info
627627
float cm_heat_effectiveness;
628628
float cm_effective_rad;
629629
float cm_detonation_rad;
630+
vec3d cm_launch_vec;
631+
float cm_launch_speed;
632+
float cm_launch_variance;
630633
bool cm_kill_single; // should the countermeasure kill just the single decoyed missile within CMEASURE_DETONATE_DISTANCE?
631634
int cmeasure_timer_interval; // how many milliseconds between pulses
632635
int cmeasure_firewait; // delay in milliseconds between countermeasure firing --wookieejedi

code/weapon/weapons.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2842,6 +2842,26 @@ int parse_weapon(int subtype, bool replace, const char *filename)
28422842
if (optional_string("+Missile Detonation Radius:"))
28432843
stuff_float(&wip->cm_detonation_rad);
28442844

2845+
if (optional_string("+Launch Vector:")) {
2846+
stuff_vec3d(&wip->cm_launch_vec);
2847+
}
2848+
2849+
if (optional_string("+Launch Speed:")) {
2850+
stuff_float(&wip->cm_launch_speed);
2851+
if (wip->cm_launch_speed < 0) {
2852+
Warning(LOCATION, "Countermeasure launch speed for weapon \'%s\' should be non-negative. Value will not be used.", wip->name);
2853+
wip->cm_launch_speed = 25.0f;
2854+
}
2855+
}
2856+
2857+
if (optional_string("+Launch Variance:")) {
2858+
stuff_float(&wip->cm_launch_variance);
2859+
if (wip->cm_launch_variance < 0) {
2860+
Warning(LOCATION, "Countermeasure launch variance for weapon \'%s\' should be non-negative. Value will not be used.", wip->name);
2861+
wip->cm_launch_variance = 2.0f;
2862+
}
2863+
}
2864+
28452865
if (optional_string("+Single Missile Kill:"))
28462866
stuff_boolean(&wip->cm_kill_single);
28472867

@@ -9684,6 +9704,9 @@ void weapon_info::reset()
96849704
this->cm_heat_effectiveness = 1.0f;
96859705
this->cm_effective_rad = MAX_CMEASURE_TRACK_DIST;
96869706
this->cm_detonation_rad = CMEASURE_DETONATE_DISTANCE;
9707+
this->cm_launch_vec = {{{0.0f, 0.0f, -1.0f}}};
9708+
this->cm_launch_speed = 25.0f;
9709+
this->cm_launch_variance = 2.0f;
96879710
this->cm_kill_single = false;
96889711
this->cmeasure_timer_interval = 0;
96899712
this->cmeasure_firewait = CMEASURE_WAIT;

0 commit comments

Comments
 (0)