FS2_Open
Open source remastering of the Freespace 2 engine
flak.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) Volition, Inc. 1999. All rights reserved.
3  *
4  * All source code herein is the property of Volition, Inc. You may not sell
5  * or otherwise commercially exploit the source or things you created based on the
6  * source.
7  *
8 */
9 
10 
11 
12 #include "object/object.h"
13 #include "weapon/flak.h"
14 #include "weapon/muzzleflash.h"
15 
16 // --------------------------------------------------------------------------------------------------------------------------------------
17 // FLAK FUNCTIONS
18 //
19 
24 {
25 }
26 
31 {
32 }
33 
37 void flak_pick_range(object *objp, vec3d *firing_pos, vec3d *predicted_target_pos, float weapon_subsys_strength)
38 {
39  float final_range;
40  float det_range;
41  vec3d temp;
42 
43  // make sure this flak object is valid
44  Assert(objp->type == OBJ_WEAPON);
45  Assert(objp->instance >= 0);
48 
50 
51  // get the range to the target
52  vm_vec_sub(&temp, &objp->pos, predicted_target_pos);
53  final_range = vm_vec_mag(&temp);
54 
55  //Is it larger than det_range?
56  det_range = wip->det_range;
57  if(det_range != 0.0f && final_range > det_range)
58  {
59  flak_set_range(objp, det_range);
60  return;
61  }
62 
63  // add in some randomness
64  float random_range = ((wip->flak_detonation_accuracy + (wip->flak_detonation_accuracy * 0.65f * (1.0f - weapon_subsys_strength))) * frand_range(-1.0f, 1.0f));
65  final_range += random_range;
66 
67  // make sure we're firing at least 10 meters away, or the weapons' arm distance if one was defined.
68  if (wip->arm_dist > 0.0f) {
69  if(final_range < wip->arm_dist){
70  final_range = wip->arm_dist;
71  }
72  } else {
73  if (final_range < 10.0f) {
74  final_range = 10.0f;
75  }
76  }
77 
78  // set the range
79  flak_set_range(objp, final_range);
80 }
81 
86 void flak_jitter_aim(vec3d *dir, float dist_to_target, float weapon_subsys_strength, weapon_info* wip)
87 {
88  vec3d rand_twist_pre, rand_twist_post;
89  matrix temp;
90  vec3d final_aim;
91  float error_val;
92 
93  // get the matrix needed to rotate the base direction to the actual direction
94  vm_vector_2_matrix(&temp, dir, NULL, NULL);
95 
96  // error value
97  error_val = wip->flak_targeting_accuracy + (wip->flak_targeting_accuracy * 0.65f * (1.0f - weapon_subsys_strength));
98 
99  // scale the rvec by some random value and make it the "pre-twist" value
100  float rand_dist = frand_range(0.0f, error_val);
101  // no jitter - so do nothing
102  if(rand_dist <= 0.0f){
103  return;
104  }
105  vm_vec_copy_scale(&rand_twist_pre, &temp.vec.rvec, rand_dist);
106 
107  // now rotate the twist vector around the x axis (the base aim axis) at a random angle
108  vm_rot_point_around_line(&rand_twist_post, &rand_twist_pre, fl_radians(359.0f * frand_range(0.0f, 1.0f)), &vmd_zero_vector, dir);
109 
110  // add the resulting vector to the base aim vector and normalize
111  final_aim = *dir;
112  vm_vec_scale(&final_aim, dist_to_target);
113  vm_vec_add(dir, &final_aim, &rand_twist_post);
114  vm_vec_normalize(dir);
115 }
116 
120 void flak_muzzle_flash(vec3d *pos, vec3d *dir, physics_info *pip, int turret_weapon_class)
121 {
122  // sanity
123  Assert((turret_weapon_class >= 0) && (turret_weapon_class < Num_weapon_types));
124  if((turret_weapon_class < 0) || (turret_weapon_class >= Num_weapon_types)){
125  return;
126  }
127  Assert(Weapon_info[turret_weapon_class].wi_flags & WIF_FLAK);
128  if(!(Weapon_info[turret_weapon_class].wi_flags & WIF_FLAK)){
129  return;
130  }
131 
132  if(Weapon_info[turret_weapon_class].muzzle_flash < 0){
133  return;
134  }
135 
136  mflash_create(pos, dir, pip, Weapon_info[turret_weapon_class].muzzle_flash);
137 }
138 
142 void flak_set_range(object *objp, float range)
143 {
144  Assert(objp->type == OBJ_WEAPON);
145  Assert(objp->instance >= 0);
146 
147  // setup the flak info
148  Weapons[objp->instance].det_range = range;
149 }
150 
154 float flak_get_range(object *objp)
155 {
156  Assert(objp->type == OBJ_WEAPON);
157  Assert(objp->instance >= 0);
158 
159  return Weapons[objp->instance].det_range;
160 }
int muzzle_flash
Definition: weapon.h:463
weapon Weapons[MAX_WEAPONS]
Definition: weapons.cpp:78
void flak_level_close()
Definition: flak.cpp:30
float frand_range(float min, float max)
Return a floating point number in the range min..max.
Definition: floating.cpp:50
weapon_info Weapon_info[MAX_WEAPON_TYPES]
Definition: weapons.cpp:79
float vm_vec_mag(const vec3d *v)
Definition: vecmat.cpp:325
void mflash_create(vec3d *gun_pos, vec3d *gun_dir, physics_info *pip, int mflash_type, object *local)
Assert(pm!=NULL)
float det_range
Definition: weapon.h:185
Definition: pstypes.h:88
GLclampf f
Definition: Glext.h:7097
object * objp
Definition: lua.cpp:3105
vec3d pos
Definition: object.h:152
float flak_get_range(object *objp)
Definition: flak.cpp:154
int weapon_info_index
Definition: weapon.h:164
void flak_level_init()
Definition: flak.cpp:23
int instance
Definition: object.h:150
#define WIF_FLAK
Definition: weapon.h:75
matrix * vm_vector_2_matrix(matrix *m, const vec3d *fvec, const vec3d *uvec, const vec3d *rvec)
Definition: vecmat.cpp:850
GLenum GLint * range
Definition: Glext.h:7096
void flak_muzzle_flash(vec3d *pos, vec3d *dir, physics_info *pip, int turret_weapon_class)
Definition: flak.cpp:120
struct matrix::@228::@230 vec
void vm_vec_scale(vec3d *dest, float s)
Definition: vecmat.cpp:248
#define OBJ_WEAPON
Definition: object.h:33
void vm_rot_point_around_line(vec3d *out, const vec3d *in, float angle, const vec3d *line_point, const vec3d *line_dir)
Definition: vecmat.cpp:1395
void flak_set_range(object *objp, float range)
Definition: flak.cpp:142
int wi_flags
Definition: weapon.h:384
void vm_vec_copy_scale(vec3d *dest, const vec3d *src, float s)
Definition: vecmat.cpp:257
int Num_weapon_types
Definition: weapons.cpp:105
void vm_vec_sub(vec3d *dest, const vec3d *src0, const vec3d *src1)
Definition: vecmat.cpp:168
float flak_detonation_accuracy
Definition: weapon.h:374
hull_check pos
Definition: lua.cpp:5050
void flak_pick_range(object *objp, vec3d *firing_pos, vec3d *predicted_target_pos, float weapon_subsys_strength)
Definition: flak.cpp:37
int temp
Definition: lua.cpp:4996
float det_range
Definition: weapon.h:372
char type
Definition: object.h:146
vec3d vmd_zero_vector
Definition: vecmat.cpp:24
float flak_targeting_accuracy
Definition: weapon.h:375
void vm_vec_add(vec3d *dest, const vec3d *src0, const vec3d *src1)
Definition: vecmat.cpp:159
float arm_dist
Definition: weapon.h:370
float vm_vec_normalize(vec3d *v)
Definition: vecmat.cpp:460
#define fl_radians(fl)
Definition: floating.h:42
void flak_jitter_aim(vec3d *dir, float dist_to_target, float weapon_subsys_strength, weapon_info *wip)
Definition: flak.cpp:86