Debian Codegraph @

Descent3/weapon.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 --- HISTORICAL COMMENTS FOLLOW ---
19
20 * $Logfile: /DescentIII/main/weapon.cpp $
21 * $Revision: 91 $
22 * $Date: 4/19/00 5:07p $
23 * $Author: Matt $
24 *
25 * $Log: /DescentIII/main/weapon.cpp $
26 *
27 * 91 4/19/00 5:07p Matt
28 * From Duane for 1.4
29 * Added checks, asserts, and fixes for bad return values
30 *
31 * 90 11/03/99 6:29p Chris
32 * Improved the auto-selection code for dual fire secondary weapons
33 *
34 * 89 10/21/99 9:30p Jeff
35 * B.A. Macintosh code merge
36 *
37 * 88 5/20/99 9:56p Jeff
38 * AddWeaponToPlayer checks for max ammo when adding it
39 *
40 * 87 5/17/99 12:20p Samir
41 * don't print out any poweup weapon pickup messages in AddWeaponToPlayer.
42 *
43 * 86 5/08/99 6:18p Chris
44 * Fixed up no hearing cases
45 *
46 * 85 5/08/99 4:12p Chris
47 * Added AI hearing noises... version 1
48 *
49 * 84 4/14/99 4:19a Jeff
50 * more case mismatch fixes in #includes
51 *
52 * 83 4/12/99 12:49p Jeff
53 * added recoil_force to weapon's page
54 *
55 * 82 3/31/99 10:27a Samir
56 * code to reset auto select states when ship is initializiaed and code to
57 * save and load these states from disk.
58 *
59 * 81 3/05/99 12:19p Matt
60 * Delete now-unused function AddWeaponAmmoToPlayer()
61 *
62 * 80 3/03/99 5:08p Samir
63 * AddWeaponAmmoToPlayer added.
64 *
65 * 79 2/25/99 1:29p Jason
66 * fixed zoom problem
67 *
68 * 78 2/20/99 7:06p Matt
69 * Changed a few HUD messages
70 *
71 * 77 2/10/99 3:48p Jason
72 * table filter changes
73 *
74 * 76 1/31/99 7:26p Matt
75 * Renamed a bunch of functions to have HUD capitalized
76 *
77 * 75 12/15/98 11:51a Samir
78 * fixed bugs with autoselect weapons when they're supposed to be skipped.
79 *
80 * 74 12/04/98 5:13p Jason
81 * took out extra mprintfs
82 *
83 * 73 10/22/98 2:41p Samir
84 * fixed autoselection for good.
85 *
86 * 72 10/21/98 11:52p Samir
87 * print different message if autoselecting a recently acquired weapon.
88 *
89 * 71 10/21/98 12:19p Samir
90 * fixed small bug in SwitchPlayerWeapon for skipped weapons.
91 *
92 * 70 10/21/98 11:14a Samir
93 * autoselect bug when selecting into a higher class without ammo/energy
94 * fixed.
95 *
96 * 69 10/19/98 11:31p Samir
97 * cycle up instead of down.
98 *
99 * 68 10/19/98 11:27p Samir
100 * fixed weapon cycling (switchplayerweapon)
101 *
102 * 67 10/07/98 9:46p Samir
103 * fixed small bug in SwitchPlayerWeapon
104 *
105 * 66 10/07/98 9:36p Samir
106 * added a function to switch a player's weapon to another in the given
107 * weapon class.
108 *
109 * 65 10/07/98 5:23p Jeff
110 * samir fixed an autoselect bug
111 *
112 * 64 10/07/98 4:13p Samir
113 * cleaned up weapon selection code.
114 *
115 * 63 9/30/98 4:31p Samir
116 * added functions to handle weapon select list.
117 *
118 * 62 7/31/98 5:17p Samir
119 * fixed weapon selection bug because secondaries weren't working properly
120 * and removed guided missile from autoselect list.
121 *
122 * 61 7/30/98 11:09a Jason
123 * added weapons that freeze and deform terrain
124 *
125 * 60 7/01/98 12:12p Jason
126 * added countermeasures
127 *
128 * 59 6/22/98 6:26p Jason
129 * added gravity field effect for weapons
130 *
131 * 58 6/19/98 12:04p Jason
132 *
133 * 57 6/17/98 3:27p Jeff
134 * Changes made for localization
135 *
136 * 56 6/16/98 10:54a Jeff
137 *
138 * 55 5/27/98 7:12p Samir
139 * yet another silly weapon selection(primary) bug fixed due to hasty
140 * checking in.
141 *
142 * 54 5/27/98 6:02p Samir
143 * foolishly, I don't check for primary weapon ammo when selecting
144 * weapons.
145 *
146 * 53 5/26/98 10:48p Samir
147 * weapon selection FIXED!
148 *
149 * 52 5/26/98 5:06p Samir
150 * changed name of weapon indices and added function to get weapon's icon
151 * image.
152 *
153 * 51 5/25/98 6:39p Jason
154 * got icons working for weapons
155 *
156 * 50 5/22/98 6:25p Samir
157 * fixed 'Rocker' bug.
158 *
159 * 49 5/22/98 11:59a Chris
160 * Fixed improper uses of FindSoundName and made the proper static sounds
161 *
162 * 48 5/14/98 12:56p Jason
163 * changes to help lower memory usage
164 *
165 * 47 4/24/98 5:32p Samir
166 * added reset reticle when setting a new weapon.
167 *
168 * 46 4/24/98 8:02a Samir
169 * added a int16_t weapon name array.
170 *
171 * 45 4/22/98 1:08p Chris
172 * Fixed auto-firing of weapons after auto-selection
173 *
174 * 44 4/08/98 12:16p Samir
175 * Weapon selection should be improved.
176 *
177 * 43 4/07/98 3:31p Jason
178 * got particle effects working with weapons
179 *
180 * 42 4/06/98 4:53p Jason
181 * got pageable polymodels working correctly with editor
182 *
183 * 41 4/02/98 3:54p Jason
184 * first pass in getting polymodel paging to work
185 *
186 * 40 3/23/98 3:52p Jason
187 * added weapon names
188 *
189 * 39 3/17/98 2:40p Samir
190 * reorg of hud/gauge system.
191 *
192 * 38 2/26/98 6:00p Jason
193 * fixed a bug where a OBJ_GHOST object (ie a dead player) could try to
194 * select a new weapon
195 *
196 * 37 2/17/98 11:33p Matt
197 * Use new function to stop player firing activity when switching weapons
198 *
199 * 36 2/17/98 5:04p Samir
200 * Fixed selection of secondary weapons.
201 *
202 * 35 2/17/98 4:06p Jason
203 * Fixed DrawAlphaBlendedScreen not zbuffering correctly
204 *
205 * 34 2/17/98 3:47p Matt
206 * Revamped weapon system and got sounds for spray and fusion weapons
207 * working. Still need to implements getting continuous & cutoff sounds
208 * from player ship.
209 *
210 * 33 2/15/98 1:18a Jason
211 * fixed zbuffer problem
212 *
213 * 32 2/14/98 10:48p Jason
214 * got preferred rendering working
215 *
216 * 31 2/11/98 2:04p Jason
217 * got spawning weapons working
218 *
219 * 30 2/05/98 7:06p Jason
220 * fixed weapon naming bug
221 *
222 * 29 2/05/98 6:53p Jason
223 * added new weapon slot
224 *
225 * 28 2/05/98 6:29p Jason
226 * added user settable explode time/size
227 *
228 * 27 2/04/98 9:28p Jason
229 * added some new weapons effects
230 *
231 * 26 2/02/98 4:07p Jason
232 * added a couple of weapons
233 *
234 * 25 1/29/98 6:06p Jason
235 * added new weapons
236 *
237 * 24 1/28/98 3:18p Jason
238 * Made AddWeaponToPlayer take a playernum
239 *
240 *
241 * 23 1/26/98 11:01a Jason
242 * incremental checkin for multiplayer
243 *
244 * 22 1/23/98 6:25p Jason
245 * Got spray weapons working
246 *
247 * 21 12/22/97 6:19p Chris
248 * Moved weapon battery firing sound off the projectile (weapon) and into
249 * the weapon battery.
250 *
251 * 20 11/14/97 6:57p Samir
252 * Maybe fixed selection of weapons <-> auto selection dependencies.
253 *
254 * 19 11/14/97 5:47p Mark
255 * Fixed some weapon selection hokeyness.
256 *
257 * 18 11/12/97 6:01p Samir
258 * Added omega and guided names to weapon list.
259 *
260 * 17 11/12/97 1:13p Jason
261 * added weapons that can ramp up
262 *
263 * 16 11/11/97 1:28p Samir
264 * Weapon selection should change hud, I think.
265 *
266 * 15 11/05/97 12:21p Chris
267 * Added weapon remap for player ship weapon batteries
268 *
269 * 14 11/04/97 5:52p Jason
270 * added DrawAplhaBlendedScreen function
271 *
272 * 13 10/30/97 4:02p Matt
273 * Added the flare
274 *
275 * 12 10/15/97 5:20p Jason
276 * did a HUGE overhaul of the bitmap system
277 *
278 * 11 9/16/97 3:03p Samir
279 * fixed autoselection of weapons when you don't have enough energy.
280 *
281 * 10 9/15/97 3:37p Samir
282 * Updated some more names for weapons.
283 *
284 * 9 9/10/97 11:45a Chris
285 * FIXED a major remapping bug
286 * Added support for weapon batteries
287 *
288 * 8 9/10/97 10:12a Samir
289 * Added weapon name text.
290 *
291 * 7 9/08/97 5:12p Samir
292 * Fixed weapon selection
293 *
294 * 6 9/05/97 12:25p Samir
295 * Added autoselection of weapons.
296 *
297 * 5 9/04/97 3:59p Samir
298 * Added code to switch weapons.
299 *
300 * 4 8/07/97 3:28p Jason
301 * Assign -1 as the default sound for weapons
302 *
303 * 3 8/07/97 1:01p Mark
304 * fixed a dumb bug
305 *
306 * 2 8/07/97 12:53p Mark
307 * FROM JASON: Made oof readable as discharge files
308 *
309 * 7 4/28/97 6:46p Jason
310 * made ships have multiple gun points
311 *
312 * 6 4/25/97 3:59p Jason
313 * fixed a bug that my last checkin caused
314 *
315 * 5 4/25/97 3:31p Jason
316 * implemented better memory management for vclips and bitmaps
317 *
318 * 4 4/17/97 2:54p Jason
319 * added clueless weapon picker upper
320 *
321 * 3 4/14/97 1:50p Jason
322 * first pass at getting weapons to fire
323 *
324 * 2 3/31/97 4:35p Jason
325 * added weapon page functionality
326 *
327 * 1 3/31/97 4:13p Jason
328 * weapons implementation file
329 *
330
331 *
332 * $NoKeywords: $
333 */
334
335 #include "weapon.h"
336 #include "pstypes.h"
337 #include "pserror.h"
338 #include "object.h"
339 #include "3d.h"
340 #include <stdlib.h>
341 #include <stdio.h>
342 #include <stdarg.h>
343 #include <string.h>
344 #include "bitmap.h"
345 #include "vclip.h"
346 #include "game.h"
347 #include "polymodel.h"
348 #include "player.h"
349 #include "hud.h"
350 #include "hlsoundlib.h"
351 #include "soundload.h"
352 #include "objinfo.h"
353 #include "gametexture.h"
354 #include "ship.h"
355 #include "gauges.h"
356 #include "sounds.h"
357 #include "stringtable.h"
358 #include "Macros.h"
359 #include "cfile.h"
360 #include "AIMain.h"
361
362 #include <algorithm>
363
364 // #include "samirlog.h"
365 #define LOGFILE(_s)
366
367 weapon Weapons[MAX_WEAPONS];
368 int Num_weapons = 0;
369
370 const char *Static_weapon_names[] = {
371 // Primary weapons
372 "Laser",
373 "Vauss",
374 "Microwave",
375 "Plasma",
376 "Fusion",
377 "Super Laser",
378 "Mass Driver",
379 "Napalm",
380 "EMD Gun",
381 "Omega",
382 // Secondary weapons
383 "Concussion",
384 "Homing",
385 "Impact Mortar",
386 "Smart",
387 "Mega",
388 "Frag",
389 "Guided",
390 "Napalm Rocket",
391 "Cyclone",
392 "Black Shark",
393 // The flares
394 "Yellow Flare",
395 };
396
397 int Static_weapon_names_msg[] = {
398 // Primary weapons
399 TXI_WPN_LASER,
400 TXI_WPN_VAUSS,
401 TXI_WPN_MICROWAVE,
402 TXI_WPN_PLASMA,
403 TXI_WPN_FUSION,
404 TXI_WPN_SUPLASER,
405 TXI_WPN_MASSDRIVER,
406 TXI_WPN_NAPALM,
407 TXI_WPN_EMDGUN,
408 TXI_WPN_OMEGA,
409 // Secondary weapons
410 TXI_WPN_CONCUSSION,
411 TXI_WPN_HOMING,
412 TXI_WPN_IMPACT,
413 TXI_WPN_SMART,
414 TXI_WPN_MEGA,
415 TXI_WPN_FRAG,
416 TXI_WPN_GUIDED,
417 TXI_WPN_NAPALMR,
418 TXI_WPN_CYCLONE,
419 TXI_WPN_BLACKSHARK,
420 // The flares
421 TXI_WPN_YELL_FLARE,
422 };
423
424 int Static_weapon_ckpt_names[][2] = {
425 // Primary weapons
426 {TXI_WPNC_LASER_1, TXI_WPNC_LASER_2},
427 {TXI_WPNC_VAUSS_1, TXI_WPNC_VAUSS_2},
428 {TXI_WPNC_MICRO_1, TXI_WPNC_MICRO_2},
429 {TXI_WPNC_PLASMA_1, TXI_WPNC_PLASMA_2},
430 {TXI_WPNC_FUSION_1, TXI_WPNC_FUSION_2},
431 {TXI_WPNC_SUPLAS_1, TXI_WPNC_SUPLAS_2},
432 {TXI_WPNC_MASSD_1, TXI_WPNC_MASSD_2},
433 {TXI_WPNC_NAPALM_1, TXI_WPNC_NAPALM_2},
434 {TXI_WPNC_EMD_1, TXI_WPNC_EMD_2},
435 {TXI_WPNC_OMEGA_1, TXI_WPNC_OMEGA_2},
436 // Secondary weapons
437 {TXI_WPNC_CONC_1, TXI_WPNC_CONC_2},
438 {TXI_WPNC_HOMING_1, TXI_WPNC_HOMING_2},
439 {TXI_WPNC_IMPACT_1, TXI_WPNC_IMPACT_2},
440 {TXI_WPNC_SMART_1, TXI_WPNC_SMART_2},
441 {TXI_WPNC_MEGA_1, TXI_WPNC_MEGA_2},
442 {TXI_WPNC_FRAG_1, TXI_WPNC_FRAG_2},
443 {TXI_WPNC_GUID_1, TXI_WPNC_GUID_2},
444 {TXI_WPNC_NAPALMR_1, TXI_WPNC_NAPALMR_2},
445 {TXI_WPNC_CYCLONE_1, TXI_WPNC_CYCLONE_2},
446 {TXI_WPNC_BLKSHRK_1, TXI_WPNC_BLKSHRK_2},
447 // The flares
448 {TXI_WPNC_YELFLARE_1, TXI_WPNC_YELFLARE_2},
449 };
450
451 // Sets all weapons to unused
452 void InitWeapons() {
453 for (int i = 0; i < MAX_WEAPONS; i++) {
454 Weapons[i].used = 0;
455 Weapons[i].name[0] = 0;
456 }
457 Num_weapons = 0;
458 }
459
460 // Allocs a weapon for use, returns -1 if error, else index on success
461 int AllocWeapon() {
462 for (int i = MAX_STATIC_WEAPONS; i < MAX_WEAPONS; i++) {
463 if (Weapons[i].used == 0) {
464 memset(&Weapons[i], 0, sizeof(weapon));
465 for (int t = 0; t < MAX_WEAPON_SOUNDS; t++)
466 Weapons[i].sounds[t] = SOUND_NONE_INDEX;
467 Weapons[i].alpha = 1.0;
468 Weapons[i].spawn_handle = -1;
469 Weapons[i].alternate_spawn_handle = -1;
470 Weapons[i].icon_handle = -1;
471 Weapons[i].scorch_handle = -1;
472 Weapons[i].gravity_size = 1;
473 Weapons[i].gravity_time = 1;
474 Weapons[i].explode_size = 1;
475 Weapons[i].explode_time = 1;
476 Weapons[i].particle_count = 0;
477 Weapons[i].particle_size = 1;
478 Weapons[i].particle_life = 0;
479 Weapons[i].particle_handle = -1;
480 Weapons[i].robot_spawn_handle = -1;
481 Weapons[i].alternate_chance = 0;
482 Weapons[i].terrain_damage_size = 0;
483 Weapons[i].terrain_damage_depth = 0;
484 Weapons[i].used = 1;
485 Weapons[i].recoil_force = 0.0f;
486 Num_weapons++;
487 return i;
488 }
489 }
490
491 Int3(); // No weapons free!
492 return -1;
493 }
494
495 // Frees weapon index n and all associated images
496 void FreeWeapon(int n) {
497 ASSERT(Weapons[n].used > 0);
498
499 Weapons[n].used = 0;
500 Weapons[n].name[0] = 0;
501 Num_weapons--;
502 }
503
504 // Gets next weapon from n that has actually been alloced
505 int GetNextWeapon(int n) {
506 int i;
507
508 ASSERT(n >= 0 && n < MAX_WEAPONS);
509
510 if (Num_weapons == 0)
511 return -1;
512
513 for (i = n + 1; i < MAX_WEAPONS; i++)
514 if (Weapons[i].used)
515 return i;
516 for (i = 0; i < n; i++)
517 if (Weapons[i].used)
518 return i;
519
520 // this is the only one
521
522 return n;
523 }
524
525 // Gets previous weapon from n that has actually been alloced
526 int GetPrevWeapon(int n) {
527 int i;
528
529 ASSERT(n >= 0 && n < MAX_WEAPONS);
530
531 if (Num_weapons == 0)
532 return -1;
533
534 for (i = n - 1; i >= 0; i--) {
535 if (Weapons[i].used)
536 return i;
537 }
538 for (i = MAX_WEAPONS - 1; i > n; i--) {
539 if (Weapons[i].used)
540 return i;
541 }
542
543 // this is the only one
544 return n;
545 }
546 // Searches thru all weapons for a specific name, returns -1 if not found
547 // or index of weapon with name
548 int FindWeaponName(const char *name) {
549 int i;
550
551 ASSERT(name != NULL);
552
553 for (i = 0; i < MAX_WEAPONS; i++)
554 if (Weapons[i].used && !stricmp(name, Weapons[i].name))
555 return i;
556
557 return -1;
558 }
559
560 // Given a weapon handle, returns that weapons bitmap
561 // If the weapon is animated, returns framenum mod num_of_frames in the animation
562 // Also figures in gametime
563 int GetWeaponHudImage(int handle, int framenum) {
564 if (Weapons[handle].flags & WF_HUD_ANIMATED) {
565 float cur_frametime;
566 int int_frame;
567
568 vclip *vc = &GameVClips[Weapons[handle].hud_image_handle];
569 ASSERT(vc->used);
570
571 cur_frametime = Gametime / vc->frame_time;
572 int_frame = cur_frametime;
573 int_frame += framenum;
574
575 return (vc->frames[int_frame % vc->num_frames]);
576 } else
577 return (Weapons[handle].hud_image_handle);
578 }
579
580 // Given a filename, loads either the bitmap or vclip found in that file. If type
581 // is not NULL, sets it to 1 if file is animation, otherwise sets it to zero
582 int LoadWeaponHudImage(char *filename, int *type) {
583 int anim = 0, bm_handle;
584 char extension[10];
585
586 int len = strlen(filename);
587
588 if (len < 4)
589 return -1;
590
591 strncpy(extension, &filename[len - 3], 5);
592
593 if ((!strnicmp("oaf", extension, 3)) || (!strnicmp("ifl", extension, 3)) || (!strnicmp("abm", extension, 3)))
594 anim = 1;
595
596 if (type != NULL)
597 *type = anim;
598
599 if (anim)
600 bm_handle = AllocLoadVClip(IGNORE_TABLE(filename), NOT_TEXTURE, 0);
601 else
602 bm_handle = bm_AllocLoadFileBitmap(IGNORE_TABLE(filename), 0);
603
604 return bm_handle;
605 }
606
607 // Given a weapon handle, returns that weapons discharge bitmap
608 int GetWeaponFireImage(int handle, int frame) {
609 if (Weapons[handle].flags & WF_IMAGE_VCLIP) {
610 float cur_frametime;
611 int int_frame;
612
613 PageInVClip(Weapons[handle].fire_image_handle);
614
615 vclip *vc = &GameVClips[Weapons[handle].fire_image_handle];
616 ASSERT(vc->used >= 1);
617
618 cur_frametime = Gametime / vc->frame_time;
619 int_frame = cur_frametime;
620 int_frame += frame;
621 return (vc->frames[int_frame % vc->num_frames]);
622 } else
623 return (Weapons[handle].fire_image_handle);
624 }
625
626 // Given a filename, loads either the bitmap or model found in that file. If type
627 // is not NULL, sets it to 1 if file is model, otherwise sets it to zero
628 int LoadWeaponFireImage(char *filename, int *type, int *anim, int pageable) {
629 int model = 0, bm_handle;
630 int is_vclip = 0;
631 char extension[10];
632
633 int len = strlen(filename);
634
635 if (len < 4)
636 return -1;
637
638 strncpy(extension, &filename[len - 3], 5);
639
640 if ((!strnicmp("pof", extension, 3)) || (!strnicmp("oof", extension, 3)))
641 model = 1;
642
643 if (type != NULL)
644 *type = model;
645 if (anim != NULL)
646 *anim = is_vclip;
647
648 if (model)
649 bm_handle = LoadPolyModel(IGNORE_TABLE(filename), 0);
650 else {
651 bm_handle = LoadTextureImage(IGNORE_TABLE(filename), anim, NOT_TEXTURE, 0);
652 }
653
654 return bm_handle;
655 }
656
657 // Given a weapon name, assigns that weapon to a specific index into
658 // the Weapons array. Returns -1 if the named weapon is not found, 0 if the weapon
659 // is already in its place, or 1 if successfully moved
660 int MatchWeaponToIndex(char *name, int dest_index) {
661
662 ASSERT(dest_index >= 0 && dest_index < MAX_WEAPONS);
663
664 int cur_index = FindWeaponName(name);
665 int new_index;
666
667 if (cur_index == -1) {
668 // The weapon we are trying to find is not loaded or doesn't exist
669 Int3(); // Get Jason or Matt
670 return -1;
671 }
672
673 if (cur_index == dest_index)
674 return 0; // hurray, we're already matched up!
675
676 if (Weapons[dest_index].used) {
677 // This weapon is currently in use. Copy the present data
678 // into a new weapon and then assign our desired weapon to that index
679
680 new_index = AllocWeapon();
681 if (new_index >= 0) // DAJ -1FIX
682 memcpy(&Weapons[new_index], &Weapons[dest_index], sizeof(weapon));
683
684 // Now copy our new info over and free the old one
685 memcpy(&Weapons[dest_index], &Weapons[cur_index], sizeof(weapon));
686 FreeWeapon(cur_index);
687 } else {
688 // This slot is unused, so just take it
689
690 Weapons[dest_index].used = 1;
691 Num_weapons++;
692
693 memcpy(&Weapons[dest_index], &Weapons[cur_index], sizeof(weapon));
694 FreeWeapon(cur_index);
695 return 0;
696 }
697
698 return new_index; // we made it!
699 }
700
701 // Moves a weapon from a given index into a new one (above MAX_STATIC_WEAPONS)
702 // returns new index
703 int MoveWeaponFromIndex(int index) {
704 ASSERT(index >= 0 && index < MAX_STATIC_WEAPONS);
705 ASSERT(Weapons[index].used);
706
707 int new_index = AllocWeapon();
708 memcpy(&Weapons[new_index], &Weapons[index], sizeof(weapon));
709 FreeWeapon(index);
710
711 return new_index;
712 }
713
714 // This is a very confusing function. It takes all the weapons that we have loaded
715 // and remaps then into their proper places (if they are static).
716 void RemapWeapons() {
717 int limit = sizeof(Static_weapon_names) / sizeof(void *);
718 int i;
719
720 for (i = 0; i < MAX_STATIC_WEAPONS; i++) {
721 if (Weapons[i].used) {
722 int match = -1;
723 for (int t = 0; t < limit; t++)
724 if (!stricmp(Weapons[i].name, Static_weapon_names[t])) {
725 match = t;
726 break;
727 }
728
729 if (match == -1) // this weapon is not supposed to be in the static area
730 {
731 int new_index;
732
733 new_index = MoveWeaponFromIndex(i);
734 RemapAllWeaponObjects(i, new_index);
735 } else // this weapon is a static weapon, make sure its in its place
736 {
737 if (i != match) // its not where it belongs, move it
738 {
739 int new_index;
740
741 new_index = MatchWeaponToIndex(Weapons[i].name, match);
742
743 if (new_index != 0)
744 RemapAllWeaponObjects(match, new_index);
745 RemapAllWeaponObjects(i, match);
746 }
747 }
748 }
749 }
750
751 for (i = MAX_STATIC_WEAPONS; i < MAX_WEAPONS; i++) {
752 if (Weapons[i].used) {
753 int match = -1;
754 for (int t = 0; t < limit; t++)
755 if (!stricmp(Weapons[i].name, Static_weapon_names[t])) {
756 match = t;
757 break;
758 }
759
760 if (match != -1) // this is a static weapon that isn't supposed to be in the free area
761 {
762
763 int new_index;
764 new_index = MatchWeaponToIndex(Weapons[i].name, match);
765 if (new_index != 0)
766 RemapAllWeaponObjects(match, new_index);
767 RemapAllWeaponObjects(i, match);
768 }
769 }
770 }
771 }
772
773 // goes thru every entity that could possible have a weapon index (ie objects, weapons, etc)
774 // and changes the old index to the new index
775 void RemapAllWeaponObjects(int old_index, int new_index) {
776 int i, j, k;
777
778 // Remaps flying weapons
779 for (i = 0; i < MAX_OBJECTS; i++) {
780
781 if (Objects[i].type == OBJ_WEAPON) {
782 if (Objects[i].id == old_index) {
783 Objects[i].id = new_index;
784 }
785 }
786 }
787
788 // Remaps weapons contained by generic objects
789 for (i = 0; i < MAX_OBJECT_IDS; i++) {
790 if (Object_info[i].static_wb) {
791 for (j = 0; j < MAX_WBS_PER_OBJ; j++) {
792 for (k = 0; k < MAX_WB_GUNPOINTS; k++) {
793 if (Object_info[i].static_wb[j].gp_weapon_index[k] == old_index) {
794 Object_info[i].static_wb[j].gp_weapon_index[k] = new_index;
795 }
796 }
797 }
798 }
799 }
800
801 // Remaps weapons contained by Ships
802 for (i = 0; i < MAX_SHIPS; i++) {
803 for (j = 0; j < MAX_WBS_PER_OBJ; j++) {
804 for (k = 0; k < MAX_WB_FIRING_MASKS; k++) {
805 if (Ships[i].static_wb[j].gp_weapon_index[k] == old_index) {
806 Ships[i].static_wb[j].gp_weapon_index[k] = new_index;
807 }
808 }
809 }
810 }
811
812 // Do spawns
813 for (i = 0; i < MAX_WEAPONS; i++) {
814 if (!Weapons[i].used)
815 continue;
816
817 if (Weapons[i].spawn_handle == old_index)
818 Weapons[i].spawn_handle = new_index;
819 if (Weapons[i].alternate_spawn_handle == old_index)
820 Weapons[i].alternate_spawn_handle = new_index;
821 }
822 }
823
824 bool IsWeaponSecondary(int index) {
825 if (index < SECONDARY_INDEX)
826 return false;
827 else
828 return true;
829 }
830
831 ///////////////////////////////////////////////////////////////////////////
832 // Weapon acquirement
833
834 // unconditionally adds a weapon and ammo to a player.
835 int AddWeaponToPlayer(int slot, int weap_index, int ammo) {
836 bool select_new = false;
837
838 LOGFILE((_logfp, "Adding weapon(%d,ammo=%d) to player.\n", weap_index, ammo));
839
840 Players[slot].weapon_flags |= HAS_FLAG(weap_index);
841
842 ship *ship = &Ships[Players[slot].ship_index];
843 otype_wb_info *wb = &ship->static_wb[weap_index];
844 ASSERT(wb != NULL);
845
846 // if secondary or primary that uses ammo, then use the ammo
847 if ((weap_index >= SECONDARY_INDEX) || wb->ammo_usage) {
848 // figure out much ammo to add
849 int added = std::min(ship->max_ammo[weap_index] - Players[slot].weapon_ammo[weap_index], ammo);
850
851 // now add it
852 Players[slot].weapon_ammo[weap_index] += (uint16_t)added;
853 }
854
855 if (slot == Player_num) {
856 if (weap_index < SECONDARY_INDEX)
857 select_new = AutoSelectWeapon(PW_PRIMARY, weap_index);
858 else
859 select_new = AutoSelectWeapon(PW_SECONDARY, weap_index);
860
861 // this should be done in multisafe code now.
862 // if (!select_new) {
863 // AddHUDMessage ("%s!",TXT(Static_weapon_names_msg[weap_index]));
864 //}
865 }
866
867 return 1;
868 }
869
870 ///////////////////////////////////////////////////////////////////////////
871 // Weapon selection
872
873 // Note that we allocate five keys per category of weapon (primary, secondary)
874 // We essentially select the weapon in slot passed.
875
876 #define NUM_PRIMARY_SLOTS 5
877 #define NUM_SECONDARY_SLOTS 5
878
879 // This is NOT a mask of weapons available to the player. This is a mask of what CLASS of
880 // weapon this slot is currently in. The code below checks this mask to see if it should
881 // select the higher class weapon in that slot when switching to that slot.
882 static uint16_t Weapon_slot_mask = 0;
883
884 void SelectPrimaryWeapon(int slot);
885 void SelectSecondaryWeapon(int slot);
886 void SetPrimaryWeapon(int index, int slot);
887 void SetSecondaryWeapon(int index, int slot);
888
889 static inline bool is_weapon_available(unsigned player_weapon_flags, int new_weapon, uint16_t ammo = 0xffff) {
890 return ((player_weapon_flags & HAS_FLAG(new_weapon)) && ammo > 0) ? true : false;
891 }
892
893 // used for sequencing
894 void ResetWeaponSelectStates(uint16_t new_state) { Weapon_slot_mask = new_state; }
895
896 void SaveWeaponSelectStates(CFILE *fp) { cf_WriteShort(fp, Weapon_slot_mask); }
897
898 void LoadWeaponSelectStates(CFILE *fp) {
899 uint16_t state = (uint16_t)cf_ReadShort(fp);
900 ResetWeaponSelectStates(state);
901 }
902
903 void SelectWeapon(int slot) {
904 if (Player_object->type != OBJ_PLAYER)
905 return; // This can happen when a player is dead and tries to select a weapon
906
907 if (slot < NUM_PRIMARY_SLOTS)
908 SelectPrimaryWeapon(slot);
909 else
910 SelectSecondaryWeapon(slot);
911 }
912
913 // slot ranges 0-4
914 void SelectPrimaryWeapon(int slot) {
915 // get slot of currently selected weapon
916 int oldslot;
917 int nw_low, nw_high;
918 unsigned avail_flags;
919 player *plr = &Players[Player_num];
920
921 avail_flags = plr->weapon_flags;
922 oldslot = plr->weapon[PW_PRIMARY].index % NUM_PRIMARY_SLOTS;
923
924 // do selection. if we are selecting the same slot of weapon, then we select to the next
925 // level of weapon. when going from highest level, go to lowest
926 if (oldslot == slot) {
927 uint16_t nw_low = (plr->weapon[PW_PRIMARY].index + NUM_PRIMARY_SLOTS) % MAX_PRIMARY_WEAPONS;
928
929 if (is_weapon_available(avail_flags, nw_low)) {
930 // toggle class of weapon in specified slot (save for selection)
931 SetPrimaryWeapon(nw_low, slot);
932 } else {
933 AddHUDMessage(TXT_WPNNOTAVAIL);
934 Sound_system.Play2dSound(SOUND_DO_NOT_HAVE_IT);
935
936 ain_hear hear;
937 hear.f_directly_player = true;
938 hear.hostile_level = 0.1f;
939 hear.curiosity_level = 0.5f;
940 hear.max_dist = AI_SOUND_SHORT_DIST;
941 AINotify(&Objects[Players[Player_num].objnum], AIN_HEAR_NOISE, (void *)&hear);
942 }
943
944 return;
945 } else {
946 // we are selecting a new weapon slot.
947 nw_low = slot % NUM_PRIMARY_SLOTS;
948 nw_high = nw_low + NUM_PRIMARY_SLOTS;
949
950 if (Weapon_slot_mask & (1 << slot)) {
951 // we think we have a higher class of weapon.
952 if (is_weapon_available(avail_flags, nw_high)) {
953 // if this slot had the higher class of weapon then check if we still have the higher class weapon.
954 // toggle class of weapon in specified slot (save for selection)
955 SetPrimaryWeapon(nw_high, slot);
956 } else if (is_weapon_available(avail_flags, nw_low)) {
957 // check if we have the lower class.
958 // toggle class of weapon in specified slot (save for selection)
959 SetPrimaryWeapon(nw_low, slot);
960 } else {
961 AddHUDMessage(TXT_WPNNOTAVAIL);
962 Sound_system.Play2dSound(SOUND_DO_NOT_HAVE_IT);
963
964 ain_hear hear;
965 hear.f_directly_player = true;
966 hear.hostile_level = 0.1f;
967 hear.curiosity_level = 0.5f;
968 hear.max_dist = AI_SOUND_SHORT_DIST;
969 AINotify(&Objects[Players[Player_num].objnum], AIN_HEAR_NOISE, (void *)&hear);
970 }
971 } else {
972 // if this is a lower class of weapon, make sure this slot is flagged as having the lower version
973 if (is_weapon_available(avail_flags, nw_low)) {
974 // if this slot had the higher class of weapon then check if we still have the higher class weapon.
975 // toggle class of weapon in specified slot (save for selection)
976 SetPrimaryWeapon(nw_low, slot);
977 } else if (is_weapon_available(avail_flags, nw_high)) {
978 // check if we have the lower class.
979 // toggle class of weapon in specified slot (save for selection)
980 SetPrimaryWeapon(nw_high, slot);
981 } else {
982 AddHUDMessage(TXT_WPNNOTAVAIL);
983 Sound_system.Play2dSound(SOUND_DO_NOT_HAVE_IT);
984
985 ain_hear hear;
986 hear.f_directly_player = true;
987 hear.hostile_level = 0.1f;
988 hear.curiosity_level = 0.5f;
989 hear.max_dist = AI_SOUND_SHORT_DIST;
990 AINotify(&Objects[Players[Player_num].objnum], AIN_HEAR_NOISE, (void *)&hear);
991 }
992 }
993 }
994 }
995
996 // slot ranges 5-9
997 void SelectSecondaryWeapon(int slot) {
998 // get slot of currently selected weapon
999 int oldslot;
1000 int nw_low, nw_high;
1001 unsigned avail_flags;
1002 player *plr = &Players[Player_num];
1003
1004 avail_flags = plr->weapon_flags;
1005 oldslot = (plr->weapon[PW_SECONDARY].index % NUM_SECONDARY_SLOTS) + NUM_PRIMARY_SLOTS;
1006
1007 // do selection. if we are selecting the same slot of weapon, then we select to the next
1008 // level of weapon. when going from highest level, go to lowest
1009 if (oldslot == slot) {
1010 nw_low = SECONDARY_INDEX + ((plr->weapon[PW_SECONDARY].index + NUM_SECONDARY_SLOTS) % MAX_SECONDARY_WEAPONS);
1011
1012 if (is_weapon_available(avail_flags, nw_low, plr->weapon_ammo[nw_low])) {
1013 // toggle class of weapon in specified slot (save for selection)
1014 SetSecondaryWeapon(nw_low, slot);
1015 } else {
1016 AddHUDMessage(TXT_WPNNOTAVAIL);
1017 Sound_system.Play2dSound(SOUND_DO_NOT_HAVE_IT);
1018
1019 ain_hear hear;
1020 hear.f_directly_player = true;
1021 hear.hostile_level = 0.1f;
1022 hear.curiosity_level = 0.5f;
1023 hear.max_dist = AI_SOUND_SHORT_DIST;
1024 AINotify(&Objects[Players[Player_num].objnum], AIN_HEAR_NOISE, (void *)&hear);
1025 }
1026 return;
1027 }
1028
1029 // we are selecting a new weapon slot.
1030 nw_low = (slot % NUM_SECONDARY_SLOTS) + SECONDARY_INDEX;
1031 nw_high = nw_low + NUM_SECONDARY_SLOTS;
1032
1033 if (Weapon_slot_mask & (1 << slot)) {
1034 // we think we have a higher class of weapon.
1035 if (is_weapon_available(avail_flags, nw_high, plr->weapon_ammo[nw_high])) {
1036 // if this slot had the higher class of weapon then check if we still have the higher class weapon.
1037 SetSecondaryWeapon(nw_high, slot);
1038 } else if (is_weapon_available(avail_flags, nw_low, plr->weapon_ammo[nw_low])) {
1039 SetSecondaryWeapon(nw_low, slot);
1040 } else {
1041 AddHUDMessage(TXT_WPNNOTAVAIL);
1042 Sound_system.Play2dSound(SOUND_DO_NOT_HAVE_IT);
1043
1044 ain_hear hear;
1045 hear.f_directly_player = true;
1046 hear.hostile_level = 0.1f;
1047 hear.curiosity_level = 0.5f;
1048 hear.max_dist = AI_SOUND_SHORT_DIST;
1049 AINotify(&Objects[Players[Player_num].objnum], AIN_HEAR_NOISE, (void *)&hear);
1050 }
1051 } else {
1052 // if this is a lower class of weapon, make sure this slot is flagged as having the lower version
1053 if (is_weapon_available(avail_flags, nw_low, plr->weapon_ammo[nw_low])) {
1054 // if this slot had the higher class of weapon then check if we still have the higher class weapon.
1055 SetSecondaryWeapon(nw_low, slot);
1056 } else if (is_weapon_available(avail_flags, nw_high, plr->weapon_ammo[nw_high])) {
1057 // check if we have the lower class.
1058 SetSecondaryWeapon(nw_high, slot);
1059 } else {
1060 AddHUDMessage(TXT_WPNNOTAVAIL);
1061 Sound_system.Play2dSound(SOUND_DO_NOT_HAVE_IT);
1062
1063 ain_hear hear;
1064 hear.f_directly_player = true;
1065 hear.hostile_level = 0.1f;
1066 hear.curiosity_level = 0.5f;
1067 hear.max_dist = AI_SOUND_SHORT_DIST;
1068 AINotify(&Objects[Players[Player_num].objnum], AIN_HEAR_NOISE, (void *)&hear);
1069 }
1070 }
1071 }
1072
1073 void SetPrimaryWeapon(int index, int slot) {
1074 dynamic_wb_info *p_dwb = &Player_object->dynamic_wb[index];
1075
1076 if (index < NUM_PRIMARY_SLOTS)
1077 Weapon_slot_mask &= ~(1 << slot);
1078 else
1079 Weapon_slot_mask |= (1 << slot);
1080
1081 if (index != Players[Player_num].weapon[PW_PRIMARY].index) {
1082 ClearPlayerFiring(Player_object, PW_PRIMARY);
1083 Players[Player_num].weapon[PW_PRIMARY].index = index;
1084
1085 AddHUDMessage(TXT_WPNSELECT, TXT(Static_weapon_names_msg[index]));
1086 // SetGaugeModified(GGF_PRIMARYLOAD, true);
1087 Sound_system.Play2dSound(SOUND_CHANGE_PRIMARY);
1088
1089 ain_hear hear;
1090 hear.f_directly_player = true;
1091 hear.hostile_level = 0.2f;
1092 hear.curiosity_level = 0.5f;
1093 hear.max_dist = AI_SOUND_SHORT_DIST;
1094 AINotify(&Objects[Players[Player_num].objnum], AIN_HEAR_NOISE, (void *)&hear);
1095
1096 // Stop any firing activity
1097 p_dwb->last_fire_time = Gametime;
1098 // resets reticle to current weapon.
1099 ResetReticle();
1100 }
1101 }
1102
1103 void SetSecondaryWeapon(int index, int slot) {
1104 dynamic_wb_info *p_dwb = &Player_object->dynamic_wb[index];
1105
1106 if (index < (NUM_SECONDARY_SLOTS + SECONDARY_INDEX))
1107 Weapon_slot_mask &= ~(1 << slot);
1108 else
1109 Weapon_slot_mask |= (1 << slot);
1110
1111 // Stop any firing activity
1112 ClearPlayerFiring(Player_object, PW_SECONDARY);
1113
1114 if (index != Players[Player_num].weapon[PW_SECONDARY].index) {
1115 Players[Player_num].weapon[PW_SECONDARY].index = index;
1116
1117 AddHUDMessage(TXT_WPNSELECT, TXT(Static_weapon_names_msg[index]));
1118 // SetGaugeModified(GGF_SECONDARYLOAD, true);
1119 Sound_system.Play2dSound(SOUND_CHANGE_SECONDARY);
1120
1121 ain_hear hear;
1122 hear.f_directly_player = true;
1123 hear.hostile_level = 0.1f;
1124 hear.curiosity_level = 0.3f;
1125 hear.max_dist = AI_SOUND_SHORT_DIST;
1126 AINotify(&Objects[Players[Player_num].objnum], AIN_HEAR_NOISE, (void *)&hear);
1127
1128 // Stop any firing activity
1129 ClearPlayerFiring(Player_object, PW_SECONDARY);
1130 p_dwb->last_fire_time = Gametime;
1131 // resets reticle to current weapon.
1132 ResetReticle();
1133 }
1134 }
1135
1136 ///////////////////////////////////////////////////////////////////////////
1137 // Weapon AUTO selection
1138
1139 const uint16_t SELLIST_START = 0x7ffe, SELLIST_END = 0x7fff;
1140
1141 static uint16_t PrimaryWpnSelectList[] = {SELLIST_START, LASER_INDEX, VAUSS_INDEX, MICROWAVE_INDEX,
1142 PLASMA_INDEX, FUSION_INDEX, SUPER_LASER_INDEX, MASSDRIVER_INDEX,
1143 NAPALM_INDEX, EMD_INDEX, OMEGA_INDEX, SELLIST_END};
1144
1145 static uint16_t SecondaryWpnSelectList[] = {
1146 SELLIST_START, CONCUSSION_INDEX, HOMING_INDEX, IMPACTMORTAR_INDEX, SMART_INDEX, MEGA_INDEX,
1147 FRAG_INDEX, GUIDED_INDEX + WPNSEL_SKIP, NAPALMROCKET_INDEX, CYCLONE_INDEX, BLACKSHARK_INDEX, SELLIST_END};
1148
1149 uint16_t GetAutoSelectPrimaryWpnIdx(int slot) {
1150 int i = -1;
1151
1152 while (PrimaryWpnSelectList[i + 1] != SELLIST_END) {
1153 if (slot == i)
1154 return PrimaryWpnSelectList[i + 1];
1155 i++;
1156 }
1157
1158 return WPNSEL_INVALID;
1159 }
1160
1161 uint16_t GetAutoSelectSecondaryWpnIdx(int slot) {
1162 int i = -1;
1163
1164 while (SecondaryWpnSelectList[i + 1] != SELLIST_END) {
1165 if (slot == i)
1166 return SecondaryWpnSelectList[i + 1];
1167 i++;
1168 }
1169
1170 return WPNSEL_INVALID;
1171 }
1172
1173 void SetAutoSelectPrimaryWpnIdx(int slot, uint16_t idx) {
1174 if (slot < 0 || slot >= MAX_PRIMARY_WEAPONS)
1175 Int3();
1176
1177 PrimaryWpnSelectList[slot + 1] = idx;
1178 }
1179
1180 void SetAutoSelectSecondaryWpnIdx(int slot, uint16_t idx) {
1181 if (slot < 0 || slot >= MAX_SECONDARY_WEAPONS)
1182 Int3();
1183
1184 SecondaryWpnSelectList[slot + 1] = idx;
1185 }
1186
1187 const uint16_t IWPNSEL_SKIP = (uint16_t) ((~WPNSEL_SKIP) & 0xFFFF);
1188
1189 #define WPNINDEX(_index) (sel_list[(_index)] & IWPNSEL_SKIP)
1190
1191 // automatically switches weapon up to next level in autoselect order to this value. and type.
1192 int SwitchPlayerWeapon(int weapon_type) {
1193 player *plr;
1194 ship *ship;
1195 int new_index;
1196 void (*setwpnfunc)(int, int); // Call either primary or secondary set weapon function
1197 uint16_t *sel_list;
1198 int plr_wpn_index;
1199
1200 plr = &Players[Player_num];
1201 ship = &Ships[plr->ship_index];
1202 plr_wpn_index = plr->weapon[weapon_type].index;
1203
1204 setwpnfunc = (weapon_type == PW_SECONDARY) ? SetSecondaryWeapon : SetPrimaryWeapon;
1205 sel_list = (weapon_type == PW_SECONDARY) ? SecondaryWpnSelectList : PrimaryWpnSelectList;
1206
1207 new_index = 0;
1208 while (WPNINDEX(new_index) != plr_wpn_index && WPNINDEX(new_index) != SELLIST_END)
1209 new_index++;
1210
1211 if (WPNINDEX(new_index) == SELLIST_END) {
1212 Int3(); // every weapon should be in list that's in the game. bad
1213 new_index = 1; // 1st item after SELLIST_START
1214 }
1215
1216 // select weapon if we can. find a weapon we can select, if we go back to start, return.
1217 // THIS CODE IS SIMILAR BUT NOT THE SAME AS THE AUTO SELECT CODE.
1218 int old_index = new_index;
1219 new_index++;
1220
1221 while (old_index != new_index) {
1222 if (WPNINDEX(new_index) == SELLIST_END) {
1223 new_index = 0;
1224 } else if (WPNINDEX(new_index) == SELLIST_START) {
1225 new_index++;
1226 } else {
1227 uint16_t wpn_index = WPNINDEX(new_index);
1228 otype_wb_info *wb = &ship->static_wb[wpn_index];
1229 int slot = (weapon_type == PW_SECONDARY)
1230 ? (((wpn_index - SECONDARY_INDEX) % NUM_SECONDARY_SLOTS) + NUM_PRIMARY_SLOTS)
1231 : (wpn_index % NUM_PRIMARY_SLOTS);
1232
1233 // mprintf(0, "wpn_index = %d\n", wpn_index);
1234
1235 if ((Players[Player_num].weapon_flags & HAS_FLAG(wpn_index)) && !(sel_list[new_index] & WPNSEL_SKIP)) {
1236
1237 if (wpn_index >= SECONDARY_INDEX && wb->ammo_usage && (wb->ammo_usage <= plr->weapon_ammo[wpn_index])) {
1238 // we've found a weapon to select to that uses ammo!
1239 (*setwpnfunc)(wpn_index, slot);
1240 LOGFILE((_logfp, "ammo wpn: switch to new index %d\n", wpn_index));
1241 break;
1242 } else if (wpn_index < SECONDARY_INDEX && wb->ammo_usage && plr->weapon_ammo[wpn_index]) {
1243 // we've found a weapon to select to that uses ammo!
1244 (*setwpnfunc)(wpn_index, slot);
1245 LOGFILE((_logfp, "ammo wpn: switch to new index %d\n", wpn_index));
1246 break;
1247 } else if (!wb->ammo_usage && (plr->energy >= wb->energy_usage)) {
1248 // we've found an energy weapon to select to!
1249 (*setwpnfunc)(wpn_index, slot);
1250 LOGFILE((_logfp, "energy wpn:switch to new index %d\n", wpn_index));
1251 break;
1252 }
1253 }
1254
1255 new_index++;
1256 // mprintf(0, "new_index = %d\n", new_index);
1257 }
1258 }
1259
1260 return sel_list[new_index] & (~WPNSEL_SKIP);
1261 }
1262
1263 // Auto selects a weapon, usually the next best weapon.
1264 // step_up = true if we're going to next higher in order.
1265 // step_up = false if we're going to next lower in order.
1266 bool AutoSelectWeapon(int weapon_type, int new_wpn) {
1267 player *plr;
1268 ship *ship;
1269 uint16_t *sel_list; // a weapon selection list
1270 int weapon_index; // the current weapon index
1271 int list_index; // index into a selection list
1272 int list_initial; // initial index in list.
1273 int slot;
1274 bool sel_new_wpn = false;
1275
1276 void (*setwpnfunc)(int, int); // Call either primary or secondary set weapon function
1277
1278 LOGFILE((_logfp, "Entering AutoSelect\n"));
1279
1280 ASSERT((weapon_type == PW_PRIMARY) || (weapon_type == PW_SECONDARY));
1281 plr = &Players[Player_num];
1282 weapon_index = plr->weapon[weapon_type].index;
1283 ship = &Ships[plr->ship_index];
1284
1285 // choose primary or secondary list and select function
1286 sel_list = (weapon_type == PW_SECONDARY) ? SecondaryWpnSelectList : PrimaryWpnSelectList;
1287 setwpnfunc = (weapon_type == PW_SECONDARY) ? SetSecondaryWeapon : SetPrimaryWeapon;
1288
1289 list_index = 0;
1290 list_initial = 0;
1291 while (WPNINDEX(list_index) != SELLIST_END) {
1292 if (WPNINDEX(list_index) == weapon_index)
1293 list_initial = list_index;
1294 list_index++;
1295 }
1296
1297 list_index--;
1298
1299 // this code takes care of selecting a GIVEN new weapon over the existing.
1300 if (new_wpn > -1) {
1301 while (WPNINDEX(list_index) != SELLIST_START && WPNINDEX(list_index) != new_wpn)
1302 list_index--;
1303 if (!(sel_list[list_index] & WPNSEL_SKIP)) {
1304 if (list_initial >= list_index) {
1305 uint16_t index = WPNINDEX(list_initial);
1306 otype_wb_info *wb = &ship->static_wb[index];
1307 if (index >= SECONDARY_INDEX && wb->ammo_usage && (wb->ammo_usage <= plr->weapon_ammo[index])) {
1308 LOGFILE((_logfp, "keep current ammo weapon...(ind=%d)\n", list_index));
1309 return sel_new_wpn; // the current weapon supercedes the new weapon, (or is the same) so return
1310 } else if (index < SECONDARY_INDEX && wb->ammo_usage && plr->weapon_ammo[index]) {
1311 LOGFILE((_logfp, "keep current ammo weapon...(ind=%d)\n", list_index));
1312 return sel_new_wpn; // the current weapon supercedes the new weapon, (or is the same) so return
1313 } else if (!wb->ammo_usage && (plr->energy >= wb->energy_usage)) {
1314 LOGFILE((_logfp, "keep current energy weapon...(ind=%d)\n", list_index));
1315 return sel_new_wpn; // the current weapon supercedes the new weapon, (or is the same) so return
1316 }
1317 LOGFILE((_logfp, "tried to keep current weapon, but no ammo!...(ind=%d)\n", list_index));
1318 }
1319 } else {
1320 LOGFILE((_logfp, "tried to autoselect skipped weapon, will not do...(ind=%d)\n", list_index));
1321 return sel_new_wpn;
1322 }
1323 }
1324
1325 while (1) {
1326 uint16_t index = sel_list[list_index];
1327 otype_wb_info *wb = &ship->static_wb[index];
1328
1329 if (index == SELLIST_START) {
1330 break;
1331 } else {
1332 // we have a real weapon coming up, lets see if we have it, and if we do, then do we have
1333 // ammo
1334 if (!(index & WPNSEL_SKIP)) {
1335 if (plr->weapon_flags & HAS_FLAG(index)) {
1336 slot = (weapon_type == PW_SECONDARY) ? (((index - SECONDARY_INDEX) % NUM_SECONDARY_SLOTS) + NUM_PRIMARY_SLOTS)
1337 : (index % NUM_PRIMARY_SLOTS);
1338 if (new_wpn == -1) {
1339 // if no new weapon, then select to next best weapon that can be used
1340 if (index >= SECONDARY_INDEX && wb->ammo_usage && (wb->ammo_usage <= plr->weapon_ammo[index])) {
1341 // we've found a weapon to select to that uses ammo!
1342 LOGFILE((_logfp, "ammo wpn: auto select to new index %d\n", index));
1343 (*setwpnfunc)(index, slot);
1344 break;
1345 } else if (index <= SECONDARY_INDEX && wb->ammo_usage && plr->weapon_ammo[index]) {
1346 // we've found a weapon to select to that uses ammo!
1347 LOGFILE((_logfp, "ammo wpn: auto select to new index %d\n", index));
1348 (*setwpnfunc)(index, slot);
1349 break;
1350 } else if (!wb->ammo_usage && (plr->energy >= wb->energy_usage)) {
1351 // we've found an energy weapon to select to!
1352 LOGFILE((_logfp, "energy wpn: auto select to new index %d\n", index));
1353 (*setwpnfunc)(index, slot);
1354 break;
1355 }
1356 } else {
1357 if (index < SECONDARY_INDEX || !wb->ammo_usage ||
1358 (index >= SECONDARY_INDEX && wb->ammo_usage && (wb->ammo_usage <= plr->weapon_ammo[index]))) {
1359 // if new weapon, then always select that weapon (already assumes that new weapon is better.)
1360 LOGFILE((_logfp, "auto select to new index with new weapon!! %d\n", index));
1361 (*setwpnfunc)(index, slot);
1362 sel_new_wpn = true;
1363 break;
1364 }
1365 }
1366 }
1367 }
1368 }
1369
1370 list_index--;
1371 }
1372
1373 return sel_new_wpn;
1374 }
1375
1376 // Draws an alpha blended polygon over the entire 3d rendering scene
1377 // The r,g,b floats specify the color
1378 void DrawAlphaBlendedScreen(float r, float g, float b, float alpha) {
1379 g3Point *pntlist[4], points[4];
1380 ddgr_color color;
1381 int i;
1382
1383 color = GR_RGB(r * 255, g * 255, b * 255);
1384
1385 // Set our four corners to cover the screen
1386 points[0].p3_sx = 0;
1387 points[0].p3_sy = 0;
1388 points[1].p3_sx = Game_window_w;
1389 points[1].p3_sy = 0;
1390 points[2].p3_sx = Game_window_w;
1391 points[2].p3_sy = Game_window_h;
1392 points[3].p3_sx = 0;
1393 points[3].p3_sy = Game_window_h;
1394
1395 for (i = 0; i < 4; i++) {
1396 points[i].p3_z = 0;
1397 points[i].p3_flags = PF_PROJECTED;
1398 pntlist[i] = &points[i];
1399 }
1400
1401 rend_SetZBufferState(0);
1402 rend_SetTextureType(TT_FLAT);
1403 rend_SetAlphaType(AT_CONSTANT);
1404 rend_SetAlphaValue(alpha * 255);
1405 rend_SetLighting(LS_NONE);
1406 rend_SetFlatColor(color);
1407
1408 rend_DrawPolygon2D(0, pntlist, 4);
1409 rend_SetZBufferState(1);
1410 }
1411
1412 // Retreives the weapon in the weapon array based off of an 'index' from 0-19 (non-mapped
1413 // primary and secondaries)
1414 weapon *GetWeaponFromIndex(int player, int index) {
1415 ship *ship = &Ships[Players[player].ship_index];
1416 otype_wb_info *wb = &ship->static_wb[index];
1417 object *pobj = &Objects[Players[player].objnum];
1418 poly_model *pm = &Poly_models[pobj->rtype.pobj_info.model_num];
1419 dynamic_wb_info *dyn_wb = &pobj->dynamic_wb[index];
1420 int cur_m_bit;
1421
1422 for (cur_m_bit = 0; cur_m_bit < pm->poly_wb[0].num_gps; cur_m_bit++) {
1423 if (wb->gp_fire_masks[dyn_wb->cur_firing_mask] & (0x01 << cur_m_bit)) {
1424 return &Weapons[wb->gp_weapon_index[cur_m_bit]];
1425 }
1426 }
1427
1428 return NULL;
1429 }