Debian Codegraph @

Descent3/robotfire.cpp

raw symbols SCIP
1 /*
2 * Descent 3
3 * Copyright (C) 2024 Parallax Software
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include "robotfire.h"
20 #include "object.h"
21 #include "objinfo.h"
22
23 #include "config.h" // for game toggles.
24 #include "objinfo.h"
25 #include "weapon.h"
26 #include "ship.h"
27 #include "game.h"
28 #include "polymodel.h"
29 #include "sounds.h"
30 #include "hlsoundlib.h"
31 #include "multi.h"
32 #include "player.h"
33 #include "demofile.h"
34 #include "SmallViews.h"
35 #include "physics.h"
36 #include "AIMain.h"
37
38 #include <stdlib.h>
39
40 #include "psrand.h"
41 // Fires a multiplayer and AI on/off weapon
42 void FireOnOffWeapon(object *objp) {
43 if (objp->type == OBJ_PLAYER) {
44 int slot = objp->id;
45
46 int ship_index = Players[slot].ship_index;
47 int wb_index = Players[slot].weapon[PW_PRIMARY].index;
48
49 if (WBIsBatteryReady(objp, &Ships[ship_index].static_wb[wb_index], wb_index)) {
50 WBFireBattery(objp, &Ships[ship_index].static_wb[wb_index], 0, wb_index);
51 }
52 } else if (objp->ai_info) {
53 char wb_index = objp->ai_info->last_special_wb_firing;
54 if (wb_index > MAX_WBS_PER_OBJ) { // DAJ
55 mprintf(2, "FireOnOffWeapon wb_index %d > MAX_WBS_PER_OBJ\n", wb_index);
56 return;
57 }
58
59 if (WBIsBatteryReady(objp, &Object_info[objp->id].static_wb[wb_index], wb_index)) {
60 WBFireBattery(objp, &Object_info[objp->id].static_wb[wb_index], 0, wb_index);
61 }
62 }
63 }
64
65 void WBSetupFireAnim(object *obj, otype_wb_info *static_wb, int wb_index) {
66 dynamic_wb_info *p_dwb = &obj->dynamic_wb[wb_index];
67
68 p_dwb->wb_anim_mask = p_dwb->cur_firing_mask;
69
70 if (static_wb->anim_start_frame != static_wb->anim_end_frame) {
71 p_dwb->flags |= DWBF_ANIMATING;
72 p_dwb->wb_anim_frame = static_wb->anim_start_frame[p_dwb->cur_firing_mask];
73 }
74
75 if (static_wb->anim_start_frame[p_dwb->cur_firing_mask] == static_wb->anim_fire_frame[p_dwb->cur_firing_mask]) {
76 WBFireBattery(obj, static_wb, wb_index);
77 p_dwb->flags |= DWBF_ANIM_FIRED;
78 }
79 }
80
81 void WBFireAnimFrame(object *obj, otype_wb_info *static_wb, int wb_index) {
82 dynamic_wb_info *p_dwb = &obj->dynamic_wb[wb_index];
83 const int cur_mask = p_dwb->wb_anim_mask;
84
85 p_dwb->wb_anim_frame += Frametime * ((static_wb->anim_end_frame[cur_mask] - static_wb->anim_start_frame[cur_mask]) /
86 static_wb->anim_time[cur_mask]);
87
88 if ((p_dwb->flags & DWBF_ANIM_FIRED) == 0) {
89 if (p_dwb->wb_anim_frame >= static_wb->anim_fire_frame[cur_mask]) {
90 WBFireBattery(obj, static_wb, wb_index);
91 p_dwb->flags |= DWBF_ANIM_FIRED;
92 }
93 }
94
95 if (p_dwb->wb_anim_frame >= static_wb->anim_end_frame[cur_mask]) {
96 p_dwb->wb_anim_frame = 0.0;
97 p_dwb->flags &= ~(DWBF_ANIM_FIRED | DWBF_ANIMATING);
98 }
99 }
100
101 bool WBIsBatteryReady(object *obj, otype_wb_info *static_wb, int wb_index) {
102 dynamic_wb_info *p_dwb = &obj->dynamic_wb[wb_index];
103 float scalar = 1.0;
104 float quad_time = 0.0f;
105
106 if (obj->type == OBJ_PLAYER)
107 scalar = Players[obj->id].weapon_recharge_scalar;
108
109 if (p_dwb->flags & DWBF_QUAD) {
110 // Add 25% on the firing time of quaded weapons
111 quad_time = static_wb->gp_fire_wait[p_dwb->cur_firing_mask] * 0.25f;
112 }
113
114 if (!(p_dwb->flags & DWBF_ANIMATING) &&
115 (p_dwb->last_fire_time + (static_wb->gp_fire_wait[p_dwb->cur_firing_mask] * scalar) + quad_time) < Gametime) {
116 return true;
117 }
118
119 return false;
120 }
121
122 #include "stringtable.h"
123
124 // Allows for multiple configurations of a single wb
125 void WBFireBattery(object *obj, otype_wb_info *static_wb, int poly_wb_index, int dynamic_wb_index,
126 float damage_scalar) {
127 int cur_m_bit;
128 poly_model *pm = &Poly_models[obj->rtype.pobj_info.model_num];
129 vector ave_pos;
130 int num_fired = 0;
131 vm_MakeZero(&ave_pos);
132 pos_state cur_pos;
133 dynamic_wb_info *p_dwb = &obj->dynamic_wb[dynamic_wb_index];
134 int weapon_obj;
135 int must_send = 0;
136 int first = 1;
137 int saved_weapon_id = 0;
138
139 uint8_t fire_mask;
140
141 if (p_dwb->flags & DWBF_QUAD)
142 fire_mask = static_wb->gp_quad_fire_mask;
143 else
144 fire_mask = static_wb->gp_fire_masks[p_dwb->cur_firing_mask];
145
146 for (cur_m_bit = 0; cur_m_bit < pm->poly_wb[poly_wb_index].num_gps; cur_m_bit++) {
147 if (fire_mask & (0x01 << cur_m_bit)) {
148 saved_weapon_id = static_wb->gp_weapon_index[cur_m_bit];
149 weapon_obj = FireWeaponFromObject(
150 obj, static_wb->gp_weapon_index[cur_m_bit], pm->poly_wb[poly_wb_index].gp_index[cur_m_bit],
151 (static_wb->flags & WBF_FIRE_FVEC) != 0, (static_wb->flags & WBF_FIRE_TARGET) != 0);
152
153 if (first) {
154 first = 0;
155 if (obj->type == OBJ_PLAYER && obj->id == Player_num &&
156 Weapons[static_wb->gp_weapon_index[cur_m_bit]].recoil_force > 0) {
157 vector force_vec = obj->orient.fvec * -Weapons[static_wb->gp_weapon_index[cur_m_bit]].recoil_force;
158 phys_apply_force(obj, &force_vec);
159 }
160 }
161
162 if (weapon_obj >= 0) {
163 ave_pos += Objects[weapon_obj].pos;
164 Objects[weapon_obj].ctype.laser_info.multiplier = damage_scalar;
165 num_fired++;
166
167 // If this is a permissable game, accelerate this object for one frame by the client ping time
168 if ((Game_mode & GM_MULTI) && Netgame.local_role == LR_CLIENT && (Netgame.flags & NF_PERMISSABLE))
169 Objects[weapon_obj].flags |= OF_PING_ACCELERATE;
170
171 if (Weapons[Objects[weapon_obj].id].flags & WF_COUNTERMEASURE)
172 must_send = 1;
173 }
174 }
175 }
176
177 if (num_fired) {
178 ave_pos /= num_fired;
179
180 cur_pos.roomnum = obj->roomnum;
181 cur_pos.position = &ave_pos;
182 cur_pos.orient = &obj->orient; // chrishack -- incorrect for directional sounds
183 // chrishack -- actually we only need an fvec (direction of fire -- NOT OBJECT)
184
185 if (Game_mode & GM_MULTI) {
186 if (Netgame.local_role == LR_SERVER) {
187 if (obj->control_type == CT_AI) {
188 // Stuff the sound
189 MultiSendRobotFireSound(static_wb->fm_fire_sound_index[p_dwb->cur_firing_mask], OBJNUM(obj));
190 }
191 }
192 }
193 if (Demo_flags == DF_RECORDING) {
194 DemoWrite3DSound(static_wb->fm_fire_sound_index[p_dwb->cur_firing_mask], OBJNUM(obj), SND_PRIORITY_HIGHEST);
195 }
196 if (obj == Viewer_object)
197 Sound_system.Play3dSound(static_wb->fm_fire_sound_index[p_dwb->cur_firing_mask], SND_PRIORITY_HIGHEST, &cur_pos,
198 MAX_GAME_VOLUME, SIF_NO_3D_EFFECTS);
199 else
200 Sound_system.Play3dSound(static_wb->fm_fire_sound_index[p_dwb->cur_firing_mask], SND_PRIORITY_HIGHEST, &cur_pos);
201
202 if (obj->type == OBJ_PLAYER) {
203 ain_hear hear;
204 hear.f_directly_player = true;
205 hear.hostile_level = 1.0f;
206 hear.curiosity_level = 0.0f;
207 hear.max_dist = Sounds[static_wb->fm_fire_sound_index[p_dwb->cur_firing_mask]].max_distance;
208 AINotify(obj, AIN_HEAR_NOISE, (void *)&hear);
209 }
210 }
211
212 if ((static_wb->flags & WBF_GUIDED) && obj->type == OBJ_PLAYER) {
213 ASSERT(num_fired <= 1); // Only one guided can be fired at a time
214 if (num_fired > 0) {
215 if (Players[obj->id].guided_obj != NULL) // if there already is a guided, release it
216 {
217 ReleaseGuidedMissile(obj->id);
218 }
219
220 Players[obj->id].guided_obj = &Objects[weapon_obj];
221 Objects[weapon_obj].mtype.phys_info.flags |= (PF_HOMING | PF_GUIDED);
222
223 // draw guided view in small window
224 if (!Game_toggles.guided_mainview && obj->id == Player_num) {
225 int window = (Missile_camera_window < 0) ? SVW_LEFT : Missile_camera_window;
226 CreateSmallView(window, Objects[weapon_obj].handle, SVF_POPUP + SVF_BIGGER + SVF_CROSSHAIRS, 0.0,
227 D3_DEFAULT_ZOOM, -1, TXT_HUD_GUIDED);
228 }
229 }
230 }
231
232 if ((static_wb->flags & WBF_USER_TIMEOUT) && obj->type == OBJ_PLAYER) {
233 ASSERT(num_fired <= 1); // Only one guided can be fired at a time
234 if (num_fired > 0) {
235 if (Players[obj->id].user_timeout_obj != NULL) // if there already is a guided, release it
236 {
237 ReleaseUserTimeoutMissile(obj->id);
238 }
239
240 Players[obj->id].user_timeout_obj = &Objects[weapon_obj];
241 }
242 }
243
244 if (num_fired >= 1 && obj->type == OBJ_PLAYER && obj->id == Player_num && (Game_mode & GM_MULTI) &&
245 !(Netgame.flags & NF_PERMISSABLE)) {
246 ship *ship = &Ships[Players[obj->id].ship_index];
247 otype_wb_info *wb = &ship->static_wb[dynamic_wb_index];
248 int on_off_weapon = (wb->flags & WBF_ON_OFF);
249
250 if (!on_off_weapon) // Normal projectile weapon
251 {
252 MultiSendFirePlayerWB(Player_num, dynamic_wb_index, p_dwb->cur_firing_mask, must_send, damage_scalar);
253 }
254 }
255
256 // Don't do any of this if its the local client and a permissable netgame
257 if (!((Game_mode & GM_MULTI) && obj->type == OBJ_PLAYER && obj->id == Player_num && Netgame.flags & NF_PERMISSABLE)) {
258 p_dwb->last_fire_time = Gametime;
259
260 if (num_fired >= 1 && obj->type == OBJ_PLAYER) {
261 Players[obj->id].last_fire_weapon_time = Gametime;
262 }
263
264 if (static_wb->flags & WBF_RANDOM_FIRE_ORDER) {
265 p_dwb->cur_firing_mask = ((float)ps_rand() / (float)D3_RAND_MAX) * static_wb->num_masks;
266 } else {
267 p_dwb->cur_firing_mask++;
268 }
269
270 if (p_dwb->cur_firing_mask >= static_wb->num_masks)
271 p_dwb->cur_firing_mask = 0;
272 }
273
274 if (obj->ai_info) {
275 for (cur_m_bit = 0; cur_m_bit < pm->poly_wb[poly_wb_index].num_gps; cur_m_bit++) {
276 if (static_wb->gp_fire_masks[p_dwb->cur_firing_mask] & (0x01 << cur_m_bit)) {
277 int weapon_id = static_wb->gp_weapon_index[cur_m_bit];
278
279 if (Weapons[weapon_id].phys_info.flags & PF_FIXED_VELOCITY)
280 obj->ai_info->weapon_speed = Weapons[weapon_id].phys_info.velocity.z;
281 else if (Weapons[weapon_id].phys_info.flags & PF_USES_THRUST)
282 obj->ai_info->weapon_speed = Weapons[weapon_id].phys_info.full_thrust / Weapons[weapon_id].phys_info.drag;
283 else
284 obj->ai_info->weapon_speed = 0.0;
285 break;
286 }
287 }
288 }
289
290 // RType Code...
291 if (!((Game_mode & GM_MULTI) && (Netgame.local_role == LR_CLIENT))) {
292 if (obj->type == OBJ_PLAYER && obj->id >= 0) {
293 object *bobj = ObjGet(Buddy_handle[obj->id]);
294 if (bobj && bobj->type == OBJ_ROBOT && (bobj->ai_info->flags & AIF_GB_MIMIC_PLAYER_FIRING_HACK)) {
295 if (dynamic_wb_index != OMEGA_INDEX && dynamic_wb_index != NAPALM_INDEX && dynamic_wb_index < 10) {
296 FireWeaponFromObject(bobj, saved_weapon_id, 0, true, false);
297 }
298 }
299 }
300 }
301 }
302
303 void WBFireBattery(object *obj, otype_wb_info *static_wb, int wb_index) {
304 WBFireBattery(obj, static_wb, wb_index, wb_index);
305 }
306
307 void WBClearInfo(object *obj) {
308 int i, j;
309 poly_model *pm = &Poly_models[obj->rtype.pobj_info.model_num];
310 int num_wbs = pm->num_wbs;
311
312 if (obj->type == OBJ_PLAYER)
313 num_wbs = MAX_WBS_PER_OBJ;
314
315 for (i = 0; i < num_wbs; i++) {
316 obj->dynamic_wb[i].last_fire_time = -1.0f;
317 obj->dynamic_wb[i].cur_firing_mask = 0;
318
319 for (j = 0; j < MAX_WB_TURRETS; j++) {
320 obj->dynamic_wb[i].norm_turret_angle[j] = 0.0f;
321 obj->dynamic_wb[i].turret_direction[j] = WB_MOVE_STILL;
322 obj->dynamic_wb[i].turret_next_think_time[j] = 0.0f;
323 }
324
325 obj->dynamic_wb[i].wb_anim_mask = 0;
326 obj->dynamic_wb[i].wb_anim_frame = 0.0f;
327
328 vm_MakeZero(&obj->dynamic_wb[i].cur_target);
329
330 obj->dynamic_wb[i].flags = DWBF_ENABLED | DWBF_AUTOMATIC;
331 }
332 }
333
334 void WBClearInfo(otype_wb_info static_wb[]) {
335 int i;
336 int j;
337
338 if (static_wb == NULL)
339 return;
340
341 for (i = 0; i < MAX_WBS_PER_OBJ; i++) {
342 static_wb[i].aiming_gp_index = 0;
343 for (j = 0; j < MAX_WB_GUNPOINTS; j++) {
344 static_wb[i].gp_weapon_index[j] = LASER_INDEX;
345 }
346
347 for (j = 0; j < MAX_WB_FIRING_MASKS; j++) {
348 static_wb[i].fm_fire_sound_index[j] = SOUND_NONE_INDEX;
349 static_wb[i].anim_start_frame[j] = 0.0;
350 static_wb[i].anim_fire_frame[j] = 0.0;
351 static_wb[i].anim_end_frame[j] = 0.0;
352 static_wb[i].anim_time[j] = 0.0;
353 }
354
355 static_wb[i].num_masks = 1;
356
357 for (j = 0; j < MAX_WB_GUNPOINTS; j++) {
358 static_wb[i].gp_fire_masks[j] = 0;
359 static_wb[i].gp_fire_wait[j] = 1.0f;
360 }
361 // Makes sure a robot with a weapon bank can fire it even if the artist forget to set the weapon info for
362 // this robot
363 static_wb[i].gp_fire_masks[0] = 1;
364
365 static_wb[i].flags = 0;
366 static_wb[i].aiming_flags = 0;
367 static_wb[i].aiming_3d_dot = 0.9f;
368 static_wb[i].aiming_3d_dist = 1000.0f;
369 static_wb[i].aiming_XZ_dot = 0.9f;
370 }
371 }
372
373 // If the wb_index is negative, all wbs are enabled/disabled
374 void WBEnable(object *obj, int wb_index, bool f_enable) {
375 poly_model *pm = &Poly_models[obj->rtype.pobj_info.model_num];
376 int i;
377
378 if (wb_index < 0) {
379 for (i = 0; i < pm->num_wbs; i++) {
380 WBEnable(obj, i, f_enable);
381 }
382 } else {
383 if ((obj->type != OBJ_PLAYER && wb_index < pm->num_wbs) || (obj->type == OBJ_PLAYER)) {
384 if (f_enable)
385 obj->dynamic_wb[wb_index].flags |= DWBF_ENABLED;
386 else
387 obj->dynamic_wb[wb_index].flags &= ~DWBF_ENABLED;
388 }
389 }
390 }