]> icculus.org git repositories - taylor/freespace2.git/blob - src/missionui/missionweaponchoice.cpp
Various 64-bit platform fixes
[taylor/freespace2.git] / src / missionui / missionweaponchoice.cpp
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
6  * the source.
7  */
8
9 /*
10  * $Logfile: /Freespace2/code/MissionUI/MissionWeaponChoice.cpp $
11  * $Revision$
12  * $Date$
13  * $Author$
14  *
15  * C module for the weapon loadout screen
16  *
17  * $Log$
18  * Revision 1.8  2005/03/29 02:18:47  taylor
19  * Various 64-bit platform fixes
20  * Fix compiler errors with MAKE_FS1 and fix gr_set_bitmap() too
21  * Make sure that turrets can fire at asteroids for FS1 (needed for a couple missions)
22  * Streaming audio support (big thanks to Pierre Willenbrock!!)
23  * Removed dependance on strings.tbl for FS1 since we don't actually need it now
24  *
25  * Revision 1.7  2004/09/20 01:31:44  theoddone33
26  * GCC 3.4 fixes.
27  *
28  * Revision 1.6  2003/06/11 18:30:33  taylor
29  * plug memory leaks
30  *
31  * Revision 1.5  2003/05/25 02:30:43  taylor
32  * Freespace 1 support
33  *
34  * Revision 1.4  2002/06/16 01:43:23  relnev
35  * fixed demo dogfight multiplayer mission
36  *
37  * minor sound changes
38  *
39  * Revision 1.3  2002/06/09 04:41:23  relnev
40  * added copyright header
41  *
42  * Revision 1.2  2002/05/07 03:16:46  theoddone33
43  * The Great Newline Fix
44  *
45  * Revision 1.1.1.1  2002/05/03 03:28:10  root
46  * Initial import.
47  *
48  * 
49  * 40    11/01/99 11:22a Jefff
50  * some weapon name translations
51  * 
52  * 39    9/10/99 10:26a Jefff
53  * default weapon anim selection fix
54  * 
55  * 38    9/06/99 3:30p Mikek
56  * Added system for restricting weapon choices based on ship class for
57  * dogfight missions.
58  * 
59  * 37    8/27/99 11:59a Jefff
60  * fixed unnecessary sound playing
61  * 
62  * 36    8/05/99 3:40p Jefff
63  * hi-res text adjustments
64  * 
65  * 35    8/05/99 11:32a Jefff
66  * fixed hi-res weapon anims not loading from packfile
67  * 
68  * 34    7/30/99 4:35p Andsager
69  * Clean up change ship sound
70  * 
71  * 33    7/30/99 4:22p Andsager
72  * restored ship and weapon anim sounds for demo.  Added sound for
73  * changing ship in weapon loadout screen.
74  * 
75  * 32    7/27/99 7:34p Jefff
76  * Lotsa clean up, moving stuff around, generally making the whole damn
77  * thing work right.
78  * 
79  * 31    7/27/99 3:01p Jefff
80  * crippled wl_start_slot_animation to prevent loading of overhead ship
81  * .anis.  also began modifications for multiplayer interface.
82  * 
83  * 30    7/25/99 5:49p Jefff
84  * initial weapon ani is now one of the ship's weapons.  this will fall
85  * back to the original search if the ship should be weaponless.
86  * 
87  * 29    7/24/99 6:03p Jefff
88  * Added "lock" text to multiplayer lock button
89  * 
90  * 28    7/21/99 6:02p Jefff
91  * fixed weapon counts running into weapon icons
92  * 
93  * 27    7/21/99 3:24p Andsager
94  * Modify demo to use 2 frame ship and weapon select anis and cut sounds
95  * associated with ani
96  * 
97  * 26    7/20/99 7:07p Jefff
98  * added "reset" text above reset button
99  * 
100  * 25    7/20/99 1:48p Jefff
101  * Long weapon titles now wrap to next line
102  * 
103  * 24    7/20/99 10:44a Jefff
104  * increased WEAPON_DESC_MAX_LINES to 6
105  * 
106  * 23    7/20/99 12:26a Jefff
107  * text wipe sound FX
108  * 
109  * 22    7/19/99 5:08p Jefff
110  * Added text descriptions for selected weapons, complete with wipe
111  * effect.
112  * 
113  * 21    7/16/99 1:50p Dave
114  * 8 bit aabitmaps. yay.
115  * 
116  * 20    7/15/99 9:20a Andsager
117  * FS2_DEMO initial checkin
118  * 
119  * 19    7/11/99 3:20p Kellys
120  * Make sure we load high-res weapon rotations in 1024
121  * 
122  * 18    7/09/99 5:54p Dave
123  * Seperated cruiser types into individual types. Added tons of new
124  * briefing icons. Campaign screen.
125  * 
126  * 17    7/09/99 12:21a Dave
127  * Change weapon loop anim frame.
128  * 
129  * 16    3/25/99 2:45p Neilk
130  * Fixed lock button
131  * 
132  * 15    3/23/99 9:23p Neilk
133  * fixed various multiplayer lock button problems
134  * 
135  * 14    3/10/99 6:21p Neilk
136  * Added new artwork for hires
137  * 
138  * 13    2/21/99 6:01p Dave
139  * Fixed standalone WSS packets. 
140  * 
141  * 12    2/18/99 11:46a Neilk
142  * hires interface coord support
143  * 
144  * 11    2/11/99 3:08p Dave
145  * PXO refresh button. Very preliminary squad war support.
146  * 
147  * 10    2/01/99 5:55p Dave
148  * Removed the idea of explicit bitmaps for buttons. Fixed text
149  * highlighting for disabled gadgets.
150  * 
151  * 9     1/30/99 1:29a Dave
152  * Fixed nebula thumbnail problem. Full support for 1024x768 choose pilot
153  * screen.  Fixed beam weapon death messages.
154  * 
155  * 8     1/29/99 4:17p Dave
156  * New interface screens.
157  * 
158  * 7     1/27/99 9:56a Dave
159  * Temporary checkin of beam weapons for Dan to make cool sounds.
160  * 
161  * 6     12/18/98 1:13a Dave
162  * Rough 1024x768 support for Direct3D. Proper detection and usage through
163  * the launcher.
164  * 
165  * 5     11/30/98 1:07p Dave
166  * 16 bit conversion, first run.
167  * 
168  * 4     11/17/98 11:12a Dave
169  * Removed player identification by address. Now assign explicit id #'s.
170  * 
171  * 3     10/13/98 9:29a Dave
172  * Started neatening up freespace.h. Many variables renamed and
173  * reorganized. Added AlphaColors.[h,cpp]
174  * 
175  * 2     10/07/98 10:53a Dave
176  * Initial checkin.
177  * 
178  * 1     10/07/98 10:50a Dave
179  * 
180  * 119   6/17/98 11:06a Lawrance
181  * put in weapon description tooltip
182  * 
183  * 118   6/13/98 6:01p Hoffoss
184  * Externalized all new (or forgot to be added) strings to all the code.
185  * 
186  * 117   6/01/98 11:43a John
187  * JAS & MK:  Classified all strings for localization.
188  * 
189  * 116   5/19/98 8:35p Dave
190  * Revamp PXO channel listing system. Send campaign goals/events to
191  * clients for evaluation. Made lock button pressable on all screens. 
192  * 
193  * 115   5/18/98 2:49p Lawrance
194  * Put in weapon animation sound hook
195  * 
196  * 114   5/08/98 7:52p Lawrance
197  * Add code to loop new weapon animations
198  * 
199  * 113   5/06/98 11:50p Lawrance
200  * Clean up help overlay code for loadout screens
201  * 
202  * 112   5/04/98 5:27p Johnson
203  * Fixed a team vs. team weapons loadout bug.
204  * 
205  * 111   5/03/98 1:55a Lawrance
206  * Fix some sound problems with loadout screens
207  * 
208  * 110   4/30/98 6:03p Lawrance
209  * Make drag and drop work better.
210  * 
211  * 109   4/27/98 6:02p Dave
212  * Modify how missile scoring works. Fixed a team select ui bug. Speed up
213  * multi_lag system. Put in new main hall.
214  * 
215  * 108   4/24/98 2:17a Lawrance
216  * repositioin ship name in weapons loadout
217  * 
218  * 107   4/22/98 7:24p Dave
219  * Made sure the "player/ships" locked button for multiplayer appears on
220  * all briefing screens.
221  * 
222  * 106   4/21/98 6:45p Dave
223  * AL: Fix bug with replacement weapons on ships
224  * 
225  * 105   4/17/98 5:27p Dave
226  * More work on the multi options screen. Fixed many minor ui todo bugs.
227  * 
228  * 104   4/16/98 8:05p Lawrance
229  * fix some bugs with numbers over-writing weapon icons
230  * 
231  * 103   4/10/98 4:51p Hoffoss
232  * Made several changes related to tooltips.
233  * 
234  * 102   4/08/98 1:18a Lawrance
235  * Fix bug that could occur when picking alternate weapons (ie count could
236  * have been wrong due to missile size)
237  * 
238  * 101   4/07/98 5:42p Dave
239  * Put in support for ui display of voice system status (recording,
240  * playing back, etc). Make sure main hall music is stopped before
241  * entering a multiplayer game via ingame join.
242  * 
243  * 100   4/02/98 11:40a Lawrance
244  * check for #ifdef DEMO instead of #ifdef DEMO_RELEASE
245  * 
246  * 99    3/31/98 1:50p Duncan
247  * ALAN: fix bugs with selecting alternate weapons 
248  * 
249  * 98    3/30/98 12:18a Lawrance
250  * change some DEMO_RELEASE code to not compile code rather than return
251  * early
252  * 
253  * 97    3/29/98 1:24p Dave
254  * Make chatbox not clear between multiplayer screens. Select player ship
255  * as default in mp team select and weapons select screens. Made create
256  * game mission list use 2 fixed size columns.
257  * 
258  * 96    3/29/98 12:55a Lawrance
259  * Get demo build working with limited set of data.
260  * 
261  * 95    3/26/98 5:47p Lawrance
262  * Implement default weapon picking if default weapons not available in
263  * pool
264  * 
265  * 94    3/25/98 8:43p Hoffoss
266  * Changed anim_play() to not be so damn complex when you try and call it.
267  * 
268  * 93    3/25/98 11:23a Mike
269  * Fix stack overwrite due to async between MAX_SECONDARY_WEAPONS and
270  * MAX_WL_SECONDARY.
271  * 
272  * 92    3/21/98 2:47p Dave
273  * Fixed chat packet routing problem on standalone. Fixed wss_request
274  * packet routing on standalone.
275  * 
276  * 91    3/19/98 5:34p Lawrance
277  * Tweak drag/drop behavior in weapons loadout
278  * 
279  * 90    3/14/98 2:48p Dave
280  * Cleaned up observer joining code. Put in support for file xfers to
281  * ingame joiners (observers or not). Revamped and reinstalled pseudo
282  * lag/loss system.
283  * 
284  * 89    3/12/98 4:03p Lawrance
285  * don't press buttons when icon dropped on them
286  * 
287  * 88    3/10/98 1:35p Lawrance
288  * Fix default selected weapon bug
289  * 
290  * 87    3/09/98 9:55p Lawrance
291  * Move secondary icons to the right to avoid overlap with numbers
292  * 
293  * 86    3/09/98 11:13a Lawrance
294  * Fix up drop sound effects used in loadout screens.
295  * 
296  * 85    3/06/98 5:36p Dave
297  * Finished up first rev of team vs team. Probably needs to be debugged
298  * thoroughly.
299  * 
300  * 84    3/05/98 5:03p Dave
301  * More work on team vs. team support for multiplayer. Need to fix bugs in
302  * weapon select.
303  * 
304  * 83    3/01/98 3:26p Dave
305  * Fixed a few team select bugs. Put in multiplayer intertface sounds.
306  * Corrected how ships are disabled/enabled in team select/weapon select
307  * screens.
308  * 
309  * 82    2/28/98 7:04p Lawrance
310  * Don't show reset button in multiplayer
311  * 
312  * 81    2/27/98 11:32a Andsager
313  * AL: Fix bug with showing valid weapon slots for ships that arrive late.
314  * 
315  * 80    2/26/98 4:59p Allender
316  * groundwork for team vs team briefings.  Moved weaponry pool into the
317  * Team_data structure.  Added team field into the p_info structure.
318  * Allow for mutliple structures in the briefing code.
319  * 
320  * 79    2/24/98 6:21p Lawrance
321  * Integrate new reset button into loadout screens
322  * 
323  * 78    2/22/98 4:17p John
324  * More string externalization classification... 190 left to go!
325  * 
326  * 77    2/22/98 12:19p John
327  * Externalized some strings
328  * 
329  * 76    2/19/98 6:26p Dave
330  * Fixed a few file xfer bugs. Tweaked mp team select screen. Put in
331  * initial support for player data uploading.
332  * 
333  * 75    2/18/98 10:37p Dave
334  * Fixed a mp team select bug which allowed players to commit at
335  * inappropriate times.
336  * 
337  * 74    2/18/98 3:56p Dave
338  * Several bugs fixed for mp team select screen. Put in standalone packet
339  * routing for team select.
340  * 
341  * 73    2/16/98 10:27a Lawrance
342  * Fix bug in wl_cull_illegal_weapons()
343  * 
344  * 72    2/15/98 11:28p Allender
345  * allow increase in MAX_WEAPON_TYPES by chaning all bitfield references
346  * 
347  * 71    2/13/98 5:15p Lawrance
348  * Fix help overlay bug in weapons loadout.
349  * 
350  * 70    2/13/98 3:46p Dave
351  * Put in dynamic chatbox sizing. Made multiplayer file lookups use cfile
352  * functions.
353  * 
354  * 69    2/09/98 11:24p Lawrance
355  * Allow player to click on disallowed weapons, but don't let them drag
356  * them away from the list.
357  * 
358  * 68    2/07/98 5:47p Lawrance
359  * reset flashing if a button gets highlighted
360  * 
361  * 67    2/06/98 5:15p Jasen
362  * AL: Allow up to 10000 of any given missile in the pool
363  * 
364  * 66    2/05/98 11:21p Lawrance
365  * When flashing buttons, use highlight frame
366  * 
367  * 65    2/02/98 3:36p Jasen
368  * AL: remove weapon debug name, and allow for 2 frame weapon anis
369  * 
370  * 64    2/02/98 3:21p Jasen
371  * Updated coords for weapon anim location.
372  * 
373  * 63    1/20/98 5:52p Lawrance
374  * account for no player ship when moving to weapons loadout
375  * 
376  * 62    1/20/98 2:23p Dave
377  * Removed optimized build warnings. 99% done with ingame join fixes.
378  * 
379  * 61    1/20/98 11:08a Lawrance
380  * Fix sound error when clicking on a weapon.
381  * 
382  * 60    1/19/98 2:17p Lawrance
383  * Fix bug that was not recognizing non-default weapons on late-arriving
384  * wings.
385  * 
386  * 59    1/15/98 4:09p Lawrance
387  * fix weapon scrolling bug
388  * 
389  * 58    1/14/98 6:44p Lawrance
390  * Take out unnecessary instance checking when freeing anims.
391  * 
392  * 57    1/12/98 5:17p Dave
393  * Put in a bunch of multiplayer sequencing code. Made weapon/ship select
394  * work through the standalone.
395  * 
396  * 56    1/10/98 12:47a Lawrance
397  * update some comments
398  * 
399  * 55    1/09/98 6:06p Dave
400  * Put in network sound support for multiplayer ship/weapon select
401  * screens. Made clients exit game correctly through warp effect. Fixed
402  * main hall menu help overlay bug.
403  * 
404  * 54    1/09/98 4:08p John
405  * Fixed a bug loading too many icon frames
406  * 
407  * 53    1/08/98 10:55p Lawrance
408  * Fix bug where forbidden weapons were not updated when a ship was
409  * changed before weapons loadout is entered.
410  * 
411  * 52    1/08/98 5:19p Sandeep
412  * Alan made a change which fixed missions with more than 4 types of
413  * secondary weapons.
414  * 
415  * 51    1/08/98 11:38a Lawrance
416  * correct typo
417  * 
418  * 50    1/08/98 11:36a Lawrance
419  * Get ship select and weapons loadout icon dropping sound effects working
420  * for single and multiplayer
421  * 
422  * 49    1/02/98 9:10p Lawrance
423  * Big changes to how colors get set on the HUD.
424  * 
425  * 48    12/30/97 6:08p Lawrance
426  * Fix bug where player discarded ship before entering weapon select
427  * 
428  * 47    12/29/97 4:21p Lawrance
429  * Flash buttons on briefing/ship select/weapons loadout when enough time
430  * has elapsed without activity.
431  * 
432  * 46    12/29/97 10:11a Lawrance
433  * Don't play drop sound when a weapon is clicked on in a slot.
434  * 
435  * 45    12/24/97 8:54p Lawrance
436  * Integrating new popup code
437  * 
438  * 44    12/24/97 1:19p Lawrance
439  * fix some bugs with the multiplayer ship/weapons loadout
440  * 
441  * 43    12/23/97 5:25p Allender
442  * more fixes to multiplayer ship selection.  Fixed strange reentrant
443  * problem with cf_callback when loading freespace data
444  * 
445  * 42    12/23/97 11:59a Allender
446  * changes to ship/wespon selection for multplayer.  added sounds to some
447  * interface screens.  Update and modiied end-of-briefing packets -- yet
448  * to be tested.
449  * 
450  * 41    12/23/97 11:48a Lawrance
451  * fix bug that could cause assert when swapping secondary weapons
452  * 
453  * 40    12/22/97 6:18p Lawrance
454  * Get save/restore of player loadout working with new code
455  * 
456  * 39    12/22/97 1:40a Lawrance
457  * Re-write ship select/weapons loadout to be multiplayer friendly
458  * 
459  * 38    12/19/97 1:23p Dave
460  * Put in multiplayer groundwork for new weapon/ship select screens.
461  * 
462  * 37    12/19/97 12:44p Dave
463  * Put in final touches on ship/weapon select. However, this is all going
464  * to be rewritten.
465  * 
466  * $NoKeywords: $
467  */
468
469 #include "missionscreencommon.h"
470 #include "missionweaponchoice.h"
471 #include "missionshipchoice.h"
472 #include "timer.h"
473 #include "key.h"
474 #include "mouse.h"
475 #include "bmpman.h"
476 #include "2d.h"
477 #include "snazzyui.h"
478 #include "animplay.h"
479 #include "freespace.h"
480 #include "gamesequence.h"
481 #include "multi.h"
482 #include "missionbrief.h"
483 #include "ui.h"
484 #include "gamesnd.h"
485 #include "animplay.h"
486 #include "contexthelp.h"
487 #include "chatbox.h"
488 #include "linklist.h"
489 #include "multimsgs.h"
490 #include "popup.h"
491 #include "multiteamselect.h"
492 #include "multiui.h"
493 #include "alphacolors.h"
494 #include "localize.h"
495
496 //#define MAX_PRIMARY_BANKS             3
497 //#define MAX_SECONDARY_BANKS   3       //      Lowered from 5 to 3 by MK on 3/25/98.  This needs to be <= MAX_WL_SECONDARY or you'll get stack overwrites.
498
499 #define IS_BANK_PRIMARY(x)                      (x<3?1:0)
500 #define IS_BANK_SECONDARY(x)            (x>2?1:0)
501
502 #define IS_LIST_PRIMARY(x)                      (Weapon_info[x].subtype==WP_MISSILE?0:1)
503 #define IS_LIST_SECONDARY(x)            (Weapon_info[x].subtype==WP_MISSILE?1:0)
504
505 //XSTR:OFF
506 #if (MAX_PRIMARY_BANKS > MAX_WL_PRIMARY)
507 #error "Illegal: MAX_PRIMARY_BANKS greater than MAX_WL_PRIMARY"
508 #endif
509
510 #if (MAX_SECONDARY_BANKS > MAX_WL_SECONDARY)
511 #error "Illegal: MAX_SECONDARY_BANKS greater than MAX_WL_SECONDARY"
512 #endif
513 //XSTR:ON
514
515 //////////////////////////////////////////////////////////////////
516 // Game-wide globals
517 //////////////////////////////////////////////////////////////////
518
519 // This game-wide global flag is set to 1 to indicate that the weapon
520 // select screen has been opened and memory allocated.  This flag
521 // is needed so we can know if weapon_select_close() needs to called if
522 // restoring a game from the Options screen invoked from weapon select
523 int Weapon_select_open = 0;
524
525 //////////////////////////////////////////////////////////////////
526 // UI Data
527 //////////////////////////////////////////////////////////////////
528
529 typedef struct wl_bitmap_group
530 {
531         int first_frame;
532         int num_frames;
533 } wl_bitmap_group;
534
535 #if defined(FS2_DEMO) || defined(FS1_DEMO)
536 #define WEAPON_ANIM_LOOP_FRAME                          1
537 #elif MAKE_FS1
538 #define WEAPON_ANIM_LOOP_FRAME                          32
539 #else
540 #define WEAPON_ANIM_LOOP_FRAME                          52                      // frame (from 0) to loop weapon anim
541 #endif
542
543 #define WEAPON_ICON_FRAME_NORMAL                                0
544 #define WEAPON_ICON_FRAME_HOT                                   1
545 #define WEAPON_ICON_FRAME_SELECTED                      2
546 #define WEAPON_ICON_FRAME_DISABLED                      3
547
548 // Weapn loadout specific buttons
549 #define NUM_WEAPON_BUTTONS                                              7
550 #define WL_BUTTON_SCROLL_PRIMARY_UP                     0
551 #define WL_BUTTON_SCROLL_PRIMARY_DOWN           1
552 #define WL_BUTTON_SCROLL_SECONDARY_UP           2
553 #define WL_BUTTON_SCROLL_SECONDARY_DOWN 3
554 #define WL_BUTTON_RESET                                                 4
555 #define WL_BUTTON_DUMMY                                                 5
556 #define WL_BUTTON_MULTI_LOCK                                    6
557 UI_WINDOW       Weapon_ui_window;
558 //UI_BUTTON     Weapon_buttons[NUM_WEAPON_BUTTONS];
559
560 static char *Wl_mask_single[GR_NUM_RESOLUTIONS] = {
561         "weaponloadout-m",
562         "2_weaponloadout-m"
563 };
564
565 static char *Wl_mask_multi[GR_NUM_RESOLUTIONS] = {
566         "weaponloadoutmulti-m",
567         "2_weaponloadoutmulti-m"
568 };
569
570 static char *Wl_loadout_select_mask[GR_NUM_RESOLUTIONS] = {
571         "weaponloadout-m",
572         "2_weaponloadout-m"
573 };
574
575
576 static char *Weapon_select_background_fname[GR_NUM_RESOLUTIONS] = {
577 #ifdef MAKE_FS1
578         "Brief",
579         "Brief"
580 #else
581         "WeaponLoadout",
582         "2_WeaponLoadout"
583 #endif
584 };
585
586 static char *Weapon_select_multi_background_fname[GR_NUM_RESOLUTIONS] = {
587 #ifdef MAKE_FS1
588         "Brief",
589         "Brief"
590 #else
591         "WeaponLoadoutMulti",
592         "2_WeaponLoadoutMulti"
593 #endif
594 };
595
596 int Weapon_select_background_bitmap;    // bitmap for weapon select brackground
597
598 static MENU_REGION      Weapon_select_region[NUM_WEAPON_REGIONS];
599 static int                              Num_weapon_select_regions;
600
601 // Mask bitmap pointer and Mask bitmap_id
602 static bitmap*  WeaponSelectMaskPtr;            // bitmap pointer to the weapon select mask bitmap
603 static ubyte*   WeaponSelectMaskData;   // pointer to actual bitmap data
604 static int              Weaponselect_mask_w, Weaponselect_mask_h;
605 static int              WeaponSelectMaskBitmap; // bitmap id of the weapon select mask bitmap
606
607
608 // convenient struct for handling all button controls
609 struct wl_buttons {
610         char *filename;
611         int x, y, xt, yt;
612         int hotspot;
613         UI_BUTTON button;  // because we have a class inside this struct, we need the constructor below..
614
615         wl_buttons(char *name, int x1, int y1, int xt1, int yt1, int h) : filename(name), x(x1), y(y1), xt(xt1), yt(yt1), hotspot(h) {}
616 };
617
618 static wl_buttons Buttons[GR_NUM_RESOLUTIONS][NUM_WEAPON_BUTTONS] = {
619         {
620 #ifdef MAKE_FS1
621                 wl_buttons("WLB_26",    0,              123,    -1,     -1,     26),
622                 wl_buttons("WLB_27",    0,              272,    -1,     -1,     27),
623                 wl_buttons("WLB_26",    0,              302,    -1,     -1,     8),
624                 wl_buttons("WLB_27",    0,              452,    -1,     -1,     9),
625                 wl_buttons("ssb_39",    566,    317,    -1,     -1,     39),
626                 wl_buttons("ssb_39",    0,              0,              -1,     -1,     99),
627                 wl_buttons("TSB_34",    601,    344,    -1,     -1,     50)
628 #else
629                 wl_buttons("WLB_26",            24,     125,            -1,             -1,     26),
630                 wl_buttons("WLB_27",            24,     276,            -1,             -1,     27),
631                 wl_buttons("WLB_08",            24,     303,            -1,             -1,     8),
632                 wl_buttons("WLB_09",            24,     454,            -1,             -1,     9),
633                 wl_buttons("ssb_39",            571,    347,            -1,             -1,     39),
634                 wl_buttons("ssb_39",            0,              0,                      -1,             -1,     99),
635                 wl_buttons("TSB_34",            603,    374,            -1,             -1,     34)
636 #endif
637         },
638         {
639                 wl_buttons("2_WLB_26",  39,     200,            -1,             -1,     26),
640                 wl_buttons("2_WLB_27",  39,     442,            -1,             -1,     27),
641                 wl_buttons("2_WLB_08",  39,     485,            -1,             -1,     8),
642                 wl_buttons("2_WLB_09",  39,     727,            -1,             -1,     9),
643                 wl_buttons("2_ssb_39",  913,    556,            -1,             -1,     39),
644                 wl_buttons("2_ssb_39",  0,              0,                      -1,             -1,     99),
645                 wl_buttons("2_TSB_34",  966,    599,            -1,             -1,     34)
646         }
647 };
648
649 //static wl_bitmap_group wl_button_bitmaps[NUM_WEAPON_BUTTONS];
650
651 static int Weapon_button_scrollable[NUM_WEAPON_BUTTONS] = {0, 0, 0, 0, 0, 0, 0};
652
653 #define MAX_WEAPON_ICONS_ON_SCREEN 8
654
655 // X and Y locations of the weapon icons in the scrollable lists
656 //int Weapon_icon_x[MAX_WEAPON_ICONS_ON_SCREEN] = {27, 27, 27, 27, 36, 36, 36, 36};
657 //int Weapon_icon_y[MAX_WEAPON_ICONS_ON_SCREEN] = {152, 182, 212, 242, 331, 361, 391, 421};
658 static int Wl_weapon_icon_coords[GR_NUM_RESOLUTIONS][MAX_WEAPON_ICONS_ON_SCREEN][2] = {
659         {
660                 {27, 152},
661                 {27, 182},
662                 {27, 212},
663                 {27, 242},
664                 {36, 331},
665                 {36, 361},
666                 {36, 391},
667                 {36, 421}
668         },
669         {
670                 {59, 251},
671                 {59, 299},
672                 {59, 347},
673                 {59, 395},
674                 {59, 538},
675                 {59, 586},
676                 {59, 634},
677                 {59, 682}
678         }
679 };
680
681
682 static int Wl_bank_coords[GR_NUM_RESOLUTIONS][MAX_WL_WEAPONS][2] = {
683         {
684 #ifdef MAKE_FS1
685                 {106,127},
686                 {106,154},
687                 {106,181},
688                 {324,127},
689                 {324,154},
690                 {324,181},
691                 {324,208},
692 #else
693                 {106,127},
694                 {106,158},
695                 {106,189},
696                 {322,127},
697                 {322,158},
698                 {322,189},
699                 {322,220},
700 #endif
701         },
702         {
703                 {170,203},
704                 {170,246},
705                 {170,290},
706                 {552,203},
707                 {552,246},
708                 {552,290},
709                 {552,333},
710         }
711 };
712
713 static int Wl_bank_count_draw_flags[MAX_WL_WEAPONS] = { 
714         0, 0, 0,                        // primaries -- dont draw counts
715         1, 1, 1, 1              // secondaries -- do draw counts
716 };
717
718
719 static int Weapon_anim_class = -1;
720 static int Last_wl_ship_class;
721
722 static int Wl_overhead_coords[GR_NUM_RESOLUTIONS][2] = {
723         {
724                 // GR_640
725 #ifdef MAKE_FS1
726                 93, 117
727 #else
728                 91, 117
729 #endif
730         },                      
731         {
732                 // GR_1024
733                 156, 183
734         }
735 };
736
737 static int Wl_weapon_ani_coords[GR_NUM_RESOLUTIONS][2] = {
738         {
739 #ifdef MAKE_FS1
740                 384, 116
741 #else
742                 408, 82                 // GR_640
743 #endif
744         },
745         {
746                 648, 128                        // GR_1024
747         }
748 };
749
750 static int Wl_weapon_ani_coords_multi[GR_NUM_RESOLUTIONS][2] = {
751         {
752 #ifdef MAKE_FS1
753                 385, 118
754 #else
755                 408, 143                        // GR_640
756 #endif
757         },
758         {
759                 648, 226                        // GR_1024
760         }
761 };
762
763
764 static int Wl_weapon_desc_coords[GR_NUM_RESOLUTIONS][2] = {
765         {
766                 508, 283                // GR_640
767         },
768         {
769                 813, 453                // GR_1024
770         }
771 };
772
773 static int Wl_delta_x, Wl_delta_y;
774
775 static int Wl_ship_name_coords[GR_NUM_RESOLUTIONS][2] = {
776         {
777                 85, 106
778         },
779         {
780                 136, 170
781         }
782 };
783
784
785 ///////////////////////////////////////////////////////////////////////
786 // UI data structs
787 ///////////////////////////////////////////////////////////////////////
788 typedef struct wl_ship_class_info
789 {
790         int                             overhead_bitmap;
791         anim_t                          *anim;
792         anim_instance_t *anim_instance;
793 } wl_ship_class_info;
794
795 wl_ship_class_info      Wl_ships[MAX_SHIP_TYPES];
796
797 typedef struct wl_icon_info
798 {
799         int                             icon_bmaps[NUM_ICON_FRAMES];
800         int                             current_icon_bmap;
801         int                             can_use;
802         anim_t                          *anim;
803         anim_instance_t *anim_instance;
804 } wl_icon_info;
805
806 wl_icon_info    Wl_icons_teams[MAX_TEAMS][MAX_WEAPON_TYPES];
807 wl_icon_info    *Wl_icons;
808
809 int Plist[MAX_WEAPON_TYPES];    // used to track scrolling of primary icon list
810 int Plist_start, Plist_size;
811
812 int Slist[MAX_WEAPON_TYPES];    // used to track scrolling of primary icon list
813 int Slist_start, Slist_size;
814
815 static int Selected_wl_slot = -1;                       // Currently selected ship slot
816 static int Selected_wl_class = -1;                      // Class of weapon that is selected
817 static int Hot_wl_slot = -1;                                    //      Ship slot that mouse is over (0..11)
818 static int Hot_weapon_icon = -1;                                // Icon number (0-7) which has mouse over it
819 static int Hot_weapon_bank = -1;                                // index (0-7) for weapon slot on ship that has a droppable icon over it
820 static int Hot_weapon_bank_icon = -1;
821
822 static int Wl_mouse_down_on_region = -1;
823
824
825 // weapon desc stuff
826 #ifndef MAKE_FS1
827 #define WEAPON_DESC_WIPE_TIME                   1.5f                    // time in seconds for wipe to occur (over WEAPON_DESC_MAX_LENGTH number of chars)
828 #define WEAPON_DESC_MAX_LINES                   7                               // max lines in the description incl. title
829 #define WEAPON_DESC_MAX_LENGTH          50                              // max chars per line of description text
830 static int Weapon_desc_wipe_done = 0;
831 static float Weapon_desc_wipe_time_elapsed = 0.0f;
832 static char Weapon_desc_lines[WEAPON_DESC_MAX_LINES][WEAPON_DESC_MAX_LENGTH];                   // 1st 2 lines are title, rest are desc
833
834 // maximum width the weapon title can be -- used in the line breaking 
835 int Weapon_title_max_width[GR_NUM_RESOLUTIONS] = { 200, 320 };
836
837 static int Wl_new_weapon_title_coords[GR_NUM_RESOLUTIONS][2] = {
838         {
839                 408, 75         // GR_640
840         },
841         {
842                 648, 118                // GR_1024
843         }
844 };
845
846 static int Wl_new_weapon_title_coords_multi[GR_NUM_RESOLUTIONS][2] = {
847         {
848                 408, 136                // GR_640
849         },
850         {
851                 648, 216                // GR_1024
852         }
853 };
854
855 static int Wl_new_weapon_desc_coords[GR_NUM_RESOLUTIONS][2] = {
856         {
857                 408, 247                // GR_640
858         },
859         {
860                 648, 395                // GR_1024
861         }
862 };
863
864 static int Wl_new_weapon_desc_coords_multi[GR_NUM_RESOLUTIONS][2] = {
865         {
866                 408, 308                // GR_640
867         },
868         {
869                 648, 493                // GR_1024
870         }
871 };
872 #endif
873
874 // ship select text
875 #ifndef MAKE_FS1
876 #define WEAPON_SELECT_NUM_TEXT                  2
877
878 UI_XSTR Weapon_select_text[GR_NUM_RESOLUTIONS][WEAPON_SELECT_NUM_TEXT] = {
879         { // GR_640
880                 { "Reset",                      1337,           580,    337,    UI_XSTR_COLOR_GREEN, -1, &Buttons[0][WL_BUTTON_RESET].button },
881                 { "Lock",                       1270,           602,    364,    UI_XSTR_COLOR_GREEN, -1, &Buttons[0][WL_BUTTON_MULTI_LOCK].button }
882         }, 
883         { // GR_1024
884                 { "Reset",                      1337,           938,    546,    UI_XSTR_COLOR_GREEN, -1, &Buttons[1][WL_BUTTON_RESET].button },
885                 { "Lock",                       1270,           964,    584,    UI_XSTR_COLOR_GREEN, -1, &Buttons[1][WL_BUTTON_MULTI_LOCK].button }
886         }
887 };
888 #endif
889
890
891 ///////////////////////////////////////////////////////////////////////
892 // Carried Icon
893 ///////////////////////////////////////////////////////////////////////
894 typedef struct carried_icon
895 {
896         int weapon_class;               // index Wl_icons[] for carried icon (-1 if carried from bank)
897         int num;                                        // number of units of weapon
898         int from_bank;                  // bank index that icon came from (0..2 primary, 3..6 secondary).  -1 if from list
899         int from_slot;                  // ship slot that weapon is part of 
900         int from_x, from_y;
901 } carried_icon;
902
903 static carried_icon Carried_wl_icon;
904
905 // forward declarations
906 void draw_wl_icons();
907 void wl_draw_ship_weapons(int index);
908 void wl_pick_icon_from_list(int index);
909 void pick_from_ship_slot(int num);
910 void start_weapon_animation(int weapon_class);
911 void stop_weapon_animation();
912 void wl_start_slot_animation(int n);
913 int wl_get_pilot_subsys_index(p_object *pobjp);
914
915 void wl_reset_to_defaults();
916
917 void wl_set_selected_slot(int slot_num);
918 void wl_maybe_reset_selected_slot();
919 void wl_maybe_reset_selected_weapon_class();
920
921 void wl_render_icon_count(int num, int x, int y);
922 void wl_render_weapon_desc();
923
924
925
926 // carry icon functions
927 void wl_reset_carried_icon();
928 int wl_icon_being_carried();
929 void wl_set_carried_icon(int from_bank, int from_slot, int weapon_class);
930
931
932 // Determine hack offset for how to draw fury missile icon. 
933 int wl_fury_missile_offset_hack(int weapon_class, int num_missiles)
934 {
935         if ( weapon_class < 0 ) {
936                 return 0;
937         }
938
939         if ( num_missiles < 100 ) {
940                 return 0 ;
941         }                       
942
943         if ( !strnicmp(Weapon_info[weapon_class].name, NOX("fury"), 4) ) {
944                 return 3;
945         }
946
947         return 0;
948 }
949
950 char *wl_tooltip_handler(char *str)
951 {
952         if (Selected_wl_class < 0)
953                 return NULL;
954
955         if (!stricmp(str, "@weapon_desc")) {
956                 char *str;
957                 int x, y, w, h;
958
959                 str = Weapon_info[Selected_wl_class].desc;
960                 gr_get_string_size(&w, &h, str);
961                 x = Wl_weapon_desc_coords[gr_screen.res][0] - w / 2;
962                 y = Wl_weapon_desc_coords[gr_screen.res][1] - h / 2;
963
964                 gr_set_color_fast(&Color_black);
965                 gr_rect(x - 5, y - 5, w + 10, h + 10);
966
967                 gr_set_color_fast(&Color_bright_white);
968                 gr_string(x, y, str);
969                 return NULL;
970         }
971
972         return NULL;
973 }
974
975 // reset the data inside the Carried_wl_icon
976 void wl_reset_carried_icon()
977 {
978         Carried_wl_icon.weapon_class = -1;
979         Carried_wl_icon.num = 0;
980         Carried_wl_icon.from_bank = -1;
981         Carried_wl_icon.from_slot = -1;
982 }
983
984 // Is an icon being carried?
985 int wl_icon_being_carried()
986 {
987         if ( Carried_wl_icon.weapon_class >= 0 ) {
988                 return 1;
989         }
990
991         return 0;
992 }
993
994 // Set carried icon data 
995 void wl_set_carried_icon(int from_bank, int from_slot, int weapon_class)
996 {
997         int mx,my;
998
999         Carried_wl_icon.from_bank = from_bank;
1000         Carried_wl_icon.from_slot = from_slot;
1001         Carried_wl_icon.weapon_class = weapon_class;
1002
1003         mouse_get_pos( &mx, &my );
1004         Carried_wl_icon.from_x=mx;
1005         Carried_wl_icon.from_y=my;
1006
1007         Buttons[gr_screen.res][WL_BUTTON_DUMMY].button.capture_mouse();
1008 }
1009
1010 // determine if the carried icon has moved
1011 int wl_carried_icon_moved()
1012 {
1013         int mx, my;
1014         mouse_get_pos( &mx, &my );
1015         if ( Carried_wl_icon.from_x != mx || Carried_wl_icon.from_y != my) {
1016                 return 1;
1017         }
1018
1019         return 0;
1020 }
1021
1022 // return the index for the pilot subsystem in the parse object
1023 int wl_get_pilot_subsys_index(p_object *pobjp)
1024 {
1025         int pilot_index, start_index, end_index, i;
1026
1027         // see if there is a PILOT subystem
1028         start_index = pobjp->subsys_index;
1029         end_index = start_index + pobjp->subsys_count;
1030         pilot_index = -1;
1031         for ( i = start_index; i < end_index; i++ ) {
1032                 if ( !stricmp(Subsys_status[i].name, NOX("pilot") ) ) {
1033                         pilot_index = i;
1034                         break;
1035                 }
1036         }
1037
1038         if ( pilot_index == -1 ) {
1039                 Error(LOCATION,"Parse object doesn't have a pilot subsystem\n");
1040                 return -1;
1041         }
1042
1043         return pilot_index;
1044 }
1045
1046 // Pause the current weapon animation
1047 void wl_pause_anim()
1048 {
1049         if ( Weapon_anim_class >= 0 && Wl_icons[Weapon_anim_class].anim_instance ) {
1050                 anim_pause(Wl_icons[Weapon_anim_class].anim_instance);
1051         }
1052 }
1053
1054 // Unpause the current weapon animation
1055 void wl_unpause_anim()
1056 {
1057         if ( Weapon_anim_class >= 0 && Wl_icons[Weapon_anim_class].anim_instance ) {
1058                 anim_unpause(Wl_icons[Weapon_anim_class].anim_instance);
1059         }
1060 }
1061
1062 // ---------------------------------------------------------------------------------
1063 // weapon_button_do()
1064 //
1065 void weapon_button_do(int i)
1066 {
1067         switch ( i ) {
1068                         case PRIMARY_SCROLL_UP:
1069                                 if ( common_scroll_up_pressed(&Plist_start, Plist_size, 4) ) {
1070                                         gamesnd_play_iface(SND_SCROLL);
1071                                 } else {
1072                                         gamesnd_play_iface(SND_GENERAL_FAIL);
1073                                 }
1074                         break;
1075
1076                         case PRIMARY_SCROLL_DOWN:
1077                                 if ( common_scroll_down_pressed(&Plist_start, Plist_size, 4) ) {
1078                                         gamesnd_play_iface(SND_SCROLL);
1079                                 } else {
1080                                         gamesnd_play_iface(SND_GENERAL_FAIL);
1081                                 }
1082                         break;
1083
1084                         case SECONDARY_SCROLL_UP:
1085                                 if ( common_scroll_up_pressed(&Slist_start, Slist_size, 4) ) {
1086                                         gamesnd_play_iface(SND_SCROLL);
1087                                 } else {
1088                                         gamesnd_play_iface(SND_GENERAL_FAIL);
1089                                 }
1090                         break;
1091
1092                         case SECONDARY_SCROLL_DOWN:
1093                                 if ( common_scroll_down_pressed(&Slist_start, Slist_size, 4) ) {
1094                                         gamesnd_play_iface(SND_SCROLL);
1095                                 } else {
1096                                         gamesnd_play_iface(SND_GENERAL_FAIL);
1097                                 }
1098                         break;
1099
1100                         case WL_RESET_BUTTON_MASK:
1101                                 wl_reset_to_defaults();
1102                                 break;
1103
1104                         case WL_BUTTON_MULTI_LOCK:
1105                                 Assert(Game_mode & GM_MULTIPLAYER);                             
1106                                 // the "lock" button has been pressed
1107                                 multi_ts_lock_pressed();
1108
1109                                 // disable the button if it is now locked
1110                                 if(multi_ts_is_locked()){
1111                                         Buttons[gr_screen.res][WL_BUTTON_MULTI_LOCK].button.disable();
1112                                 }
1113                                 break;
1114
1115                         default:
1116                         break;
1117         }
1118 }
1119
1120 // -------------------------------------------------------------------
1121 // weapon_check_buttons()
1122 //
1123 // Check if any weapons loadout screen buttons have been pressed, and 
1124 // call weapon_button_do() if they have.
1125 //
1126 void weapon_check_buttons()
1127 {
1128         int                     i;
1129         wl_buttons      *b;
1130
1131         for ( i = 0; i < NUM_WEAPON_BUTTONS; i++ ) {
1132                 b = &Buttons[gr_screen.res][i];
1133                 
1134                 if ( b->button.pressed() ) {
1135                         if(i == WL_BUTTON_MULTI_LOCK){
1136                                 weapon_button_do(i);
1137                         } else {
1138                                 weapon_button_do(b->hotspot);
1139                         }
1140                 }
1141         }
1142 }
1143
1144 // -------------------------------------------------------------------
1145 // wl_redraw_pressed_buttons()
1146 //
1147 // Redraw any weapon loadout buttons that are pressed down.  This function is needed
1148 // since we sometimes need to draw pressed buttons last to ensure the entire
1149 // button gets drawn (and not overlapped by other buttons)
1150 //
1151 void wl_redraw_pressed_buttons()
1152 {
1153         int                     i;
1154         wl_buttons      *b;
1155         
1156         common_redraw_pressed_buttons();
1157
1158         for ( i = 0; i < NUM_WEAPON_BUTTONS; i++ ) {
1159                 b = &Buttons[gr_screen.res][i];
1160                 if ( b->button.button_down() ) {
1161                         b->button.draw_forced(2);
1162                 }
1163         }
1164 }
1165
1166 // ---------------------------------------------------------------------------------
1167 // weapon_buttons_init()
1168 //
1169 void weapon_buttons_init()
1170 {
1171         wl_buttons      *b;
1172         int                     i;
1173
1174         for ( i = 0; i < NUM_WEAPON_BUTTONS; i++ ) {
1175                 b = &Buttons[gr_screen.res][i];
1176                 b->button.create( &Weapon_ui_window, "", Buttons[gr_screen.res][i].x, Buttons[gr_screen.res][i].y, 60, 30, Weapon_button_scrollable[i]);
1177                 // set up callback for when a mouse first goes over a button
1178                 b->button.set_highlight_action( common_play_highlight_sound );
1179                 b->button.set_bmaps(Buttons[gr_screen.res][i].filename);
1180                 b->button.link_hotspot(Buttons[gr_screen.res][i].hotspot);
1181         }
1182
1183         if ( Game_mode & GM_MULTIPLAYER ) {
1184                 Buttons[gr_screen.res][WL_BUTTON_RESET].button.hide();
1185                 Buttons[gr_screen.res][WL_BUTTON_RESET].button.disable();               
1186
1187                 // if we're not the host of the game (or a team captain in team vs. team mode), disable the lock button
1188                 if(Netgame.type_flags & NG_TYPE_TEAM){
1189                         if(!(Net_player->flags & NETINFO_FLAG_TEAM_CAPTAIN)){
1190                                 Buttons[gr_screen.res][WL_BUTTON_MULTI_LOCK].button.disable();          
1191                         }
1192                 } else {
1193                         if(!(Net_player->flags & NETINFO_FLAG_GAME_HOST)){
1194                                 Buttons[gr_screen.res][WL_BUTTON_MULTI_LOCK].button.disable();                          
1195                         }
1196                 }
1197         } else {                
1198                 Buttons[gr_screen.res][WL_BUTTON_MULTI_LOCK].button.hide();
1199                 Buttons[gr_screen.res][WL_BUTTON_MULTI_LOCK].button.disable();
1200         }
1201
1202 #ifndef MAKE_FS1
1203         // add all xstrs
1204         for(i=0; i<WEAPON_SELECT_NUM_TEXT; i++) {
1205                 Weapon_ui_window.add_XSTR(&Weapon_select_text[gr_screen.res][i]);
1206         }
1207 #endif
1208
1209         Buttons[gr_screen.res][WL_BUTTON_DUMMY].button.hide();
1210         Buttons[gr_screen.res][WL_BUTTON_DUMMY].button.disable();       
1211 }
1212
1213 // ---------------------------------------------------------------------------------
1214 // wl_render_overhead_view()
1215 //
1216 void wl_render_overhead_view(float frametime)
1217 {
1218         char                                            name[NAME_LENGTH + CALLSIGN_LEN];
1219         wl_ship_class_info      *wl_ship;
1220         int                                             ship_class;
1221
1222         if ( Selected_wl_slot == -1 ) {
1223                 return;
1224         }
1225
1226         ship_class = Wss_slots[Selected_wl_slot].ship_class;
1227
1228         // check if ship class has changed and maybe play sound
1229         if (Last_wl_ship_class != ship_class) {
1230                 if (Last_wl_ship_class != -1) {
1231                         gamesnd_play_iface(SND_ICON_DROP);
1232                 }
1233                 Last_wl_ship_class = ship_class;
1234         }
1235
1236         wl_ship = &Wl_ships[ship_class];
1237
1238         if ( wl_ship->anim_instance == NULL ) {
1239                 if ( wl_ship->overhead_bitmap < 0 ) {
1240                         // load the bitmap
1241                         if (gr_screen.res == GR_640)
1242                         {
1243                                 // lo-res
1244                                 wl_ship->overhead_bitmap = bm_load(Ship_info[ship_class].overhead_filename);
1245                         } else {
1246                                 // high-res
1247                                 char filename[NAME_LENGTH+2] = "2_";
1248                                 strcat(filename, Ship_info[ship_class].overhead_filename);
1249                                 wl_ship->overhead_bitmap = bm_load(filename);
1250                         }
1251                         if ( wl_ship->overhead_bitmap < 0 ) {
1252                                 Int3();                 // bad things happened
1253                                 return;
1254                         }
1255                 }
1256                 gr_set_bitmap(wl_ship->overhead_bitmap, GR_ALPHABLEND_NONE, GR_BITBLT_MODE_NORMAL, 1.0f, -1, -1);
1257                 gr_bitmap(Wl_overhead_coords[gr_screen.res][0], Wl_overhead_coords[gr_screen.res][1]);
1258         }
1259
1260         ss_return_name(Selected_wl_slot/4, Selected_wl_slot%4, name);
1261         gr_set_color_fast(&Color_normal);
1262         gr_string(Wl_ship_name_coords[gr_screen.res][0], Wl_ship_name_coords[gr_screen.res][1], name);
1263 }
1264
1265 // ---------------------------------------------------------------------------------
1266 // wl_get_ship_class()
1267 //
1268 //
1269 int wl_get_ship_class(int wl_slot)
1270 {
1271         return Wss_slots[wl_slot].ship_class;
1272 }
1273
1274 //      Return true if weapon_flags indicates a weapon that is legal for use in current game type.
1275 //      Function added by MK on 9/6/99 to support separate legal loadouts for dogfight missions.
1276 int weapon_allowed_for_game_type(int weapon_flags)
1277 {
1278         int     rval = 0;
1279
1280 /* disable check for demo since it doesn't have "$Allowed Dogfight PBanks" */
1281 #if !(defined(FS2_DEMO) || defined(FS1_DEMO))
1282         if ((Game_mode & GM_MULTIPLAYER) && (Netgame.type_flags & NG_TYPE_DOGFIGHT)) {
1283                 if (weapon_flags & (1 << 1))
1284                         rval = 1;
1285         } else
1286 #endif  
1287         if (weapon_flags & (1 << 0))
1288                 rval  = 1;
1289
1290         return rval;
1291 }
1292
1293 // go through the possible weapons to choose from, and flag some as disabled since
1294 // that ship class cannot use that kind of weapon.  The weapon filter is specified
1295 // in ships.tbl, where each ship has a list of all the possible weapons it can use.
1296 void wl_set_disabled_weapons(int ship_class)
1297 {
1298         int                             i;
1299         ship_info               *sip;
1300
1301         if ( ship_class == - 1 )
1302                 return;
1303
1304         Assert(ship_class >= 0 && ship_class < MAX_SHIP_TYPES);
1305
1306         sip = &Ship_info[ship_class];
1307
1308         for ( i = 0; i < MAX_WEAPON_TYPES; i++ ) {
1309                 //      Determine whether weapon #i is allowed on this ship class in the current type of mission.
1310                 //      As of 9/6/99, the only difference is dogfight missions have a different list of legal weapons.
1311                 Wl_icons[i].can_use = weapon_allowed_for_game_type(sip->allowed_weapons[i]);
1312         }
1313 }
1314
1315 // ---------------------------------------------------------------------------------
1316 // maybe_select_wl_slot()
1317 //
1318 // A slot index was clicked on, mabye change Selected_wl_slot
1319 void maybe_select_wl_slot(int block, int slot)
1320 {
1321         int sidx;
1322
1323         if ( Wss_num_wings <= 0 )
1324                 return;
1325
1326         sidx = block*4 + slot;
1327         if ( Wss_slots[sidx].ship_class < 0 ) {
1328                 return;
1329         }
1330
1331         wl_set_selected_slot(sidx);
1332 }
1333
1334 // ---------------------------------------------------------------------------------
1335 // maybe_select_new_weapon()
1336 //
1337 // Change to the weapon that corresponds to index in the weapon list
1338 //
1339 // input:       index           =>              weapon icon index (0-7)
1340 //
1341 void maybe_select_new_weapon(int index)
1342 {
1343         int weapon_class;
1344
1345         // if a weapon is being carried, do nothing
1346         if ( wl_icon_being_carried() ) {
1347                 return;
1348         }
1349
1350         int region_index = ICON_PRIMARY_0+index;
1351         if ( index > 3 ) {
1352                 region_index = ICON_SECONDARY_0 + (index - 4);
1353         }
1354
1355         if ( Wl_mouse_down_on_region != region_index ) {
1356                 return;
1357         }
1358
1359         if ( index < 4 ) {
1360                 weapon_class = Plist[Plist_start+index];
1361         } else {
1362                 weapon_class = Slist[Slist_start+index-4];
1363         }
1364
1365         if ( weapon_class >= 0 ) {
1366                 Selected_wl_class = weapon_class;
1367                 wl_pick_icon_from_list(index);
1368         }
1369 }
1370
1371 // ---------------------------------------------------------------------------------
1372 // maybe_select_new_ship_weapon()
1373 //
1374 // Change to the weapon that corresponds to the ship weapon slot
1375 //
1376 // input: index ->      index of bank (0..2 primary, 0..6 secondary)
1377 void maybe_select_new_ship_weapon(int index)
1378 {
1379         int *wep, *wep_count;
1380
1381         if ( Selected_wl_slot == -1 )
1382                 return;
1383
1384         if ( wl_icon_being_carried() ) {
1385                 return;
1386         }
1387
1388         wep = Wss_slots[Selected_wl_slot].wep;
1389         wep_count = Wss_slots[Selected_wl_slot].wep_count;
1390
1391         if ( wep[index] < 0 || wep_count[index] <= 0 ) {
1392                 return;
1393         }
1394
1395         if ( Wl_mouse_down_on_region != (ICON_SHIP_PRIMARY_0+index) ) {
1396                 return;
1397         }
1398
1399
1400         Selected_wl_class = wep[index];
1401 }
1402
1403 // Initialize Wl_pool[] to mission deafult
1404 void wl_init_pool(team_data *td)
1405 {
1406         int i;
1407
1408         for ( i = 0; i < MAX_WEAPON_TYPES; i++ ) {
1409                 Wl_pool[i] = -1;
1410         }
1411
1412         for ( i = 0; i < MAX_WEAPON_TYPES; i++ ) {
1413                 Wl_pool[i] = td->weaponry_pool[i];      // read from mission
1414         }
1415 }
1416
1417 // load the icons for a specific ship class
1418 void wl_load_icons(int weapon_class)
1419 {
1420         wl_icon_info    *icon;
1421         int                             first_frame, num_frames, i;
1422
1423         icon = &Wl_icons[weapon_class];
1424
1425         first_frame = bm_load_animation(Weapon_info[weapon_class].icon_filename, &num_frames);
1426         if ( first_frame == -1 ) {
1427                 Int3(); // Could not load in icon frames.. get Alan
1428                 return;
1429         }
1430
1431         for ( i = 0; i < num_frames ; i++ ) {
1432                 icon->icon_bmaps[i] = first_frame+i;
1433         }
1434 }
1435
1436 // Load in a specific weapon animation.  The data is loaded as a memory-mapped file since these animations
1437 // can be large.
1438 void wl_load_anim(int weapon_class)
1439 {
1440         char animation_filename[CF_MAX_FILENAME_LENGTH+4];
1441         wl_icon_info    *icon;
1442
1443         icon = &Wl_icons[weapon_class];
1444         Assert( icon->anim == NULL );
1445         
1446         // 1024x768 SUPPORT
1447         // If we are in 1024x768, we first want to append "2_" in front of the filename
1448         if (gr_screen.res == GR_1024) {
1449                 Assert(strlen(Weapon_info[weapon_class].anim_filename) <= 30);
1450                 strcpy(animation_filename, "2_");
1451                 strcat(animation_filename, Weapon_info[weapon_class].anim_filename);
1452
1453                 // now check if file exists
1454                 // GRR must add a .ANI at the end for detection
1455                 strcat(animation_filename,".ani");
1456                 icon->anim = anim_load(animation_filename, 1);
1457
1458                 if (icon->anim == NULL) {
1459                         mprintf(("Weapon ANI: Can not find %s, using lowres version instead.\n",animation_filename)); 
1460                         strcpy(animation_filename, Weapon_info[weapon_class].anim_filename);
1461                         icon->anim = anim_load(animation_filename, 1);
1462                 }
1463
1464                 /*
1465                 if (!cf_exist(animation_filename, CF_TYPE_INTERFACE)) {
1466                         // file does not exist, use original low res version
1467                         mprintf(("Weapon ANI: Can not find %s, using lowres version instead.\n",animation_filename)); 
1468                         strcpy(animation_filename, Weapon_info[weapon_class].anim_filename);
1469                 } else {
1470                         animation_filename[strlen(animation_filename) - 4] = '\0';
1471                         mprintf(("Weapon ANI: Found hires version of %s\n",animation_filename));
1472                 }
1473                 */
1474         } else {
1475                 strcpy(animation_filename, Weapon_info[weapon_class].anim_filename);
1476                 // load the compressed ship animation into memory 
1477                 // NOTE: if last parm of load_anim is 1, the anim file is mapped to memory 
1478                 icon->anim = anim_load(animation_filename, 1);
1479         }
1480
1481         if ( icon->anim == NULL ) {
1482                 Int3();         // couldn't load anim filename.. get Alan
1483         }
1484 }
1485
1486 // Load in any weapon animations.  This function assumes that Wl_pool has been inited.
1487 void wl_load_all_anims()
1488 {
1489         #ifndef DEMO // not for FS2_DEMO
1490
1491         int i;
1492
1493         // init anim members for weapon animations
1494         for ( i = 0; i < MAX_WEAPON_TYPES; i++ ) {
1495                 Wl_icons[i].anim = NULL;
1496                 Wl_icons[i].anim_instance = NULL;
1497         }
1498
1499         // init anim member for overhead ship animations
1500         for ( i = 0; i < MAX_SHIP_TYPES; i++ ) {
1501                 Wl_ships[i].anim = NULL;
1502                 Wl_ships[i].anim_instance = NULL;
1503         }
1504
1505         // load up the animations used for weapons (they are memory-mapped)
1506         for ( i = 0; i < MAX_WEAPON_TYPES; i++ ) {
1507                 if ( Wl_pool[i] > 0 ) {
1508                         wl_load_anim(i);
1509                 }
1510         }
1511
1512         #endif
1513 }
1514
1515 // release any anim instances
1516 void wl_unload_all_anim_instances()
1517 {
1518         // stop any weapon anim instances
1519         for ( int i = 0; i < MAX_WEAPON_TYPES; i++ ) {
1520                 if ( Wl_icons[i].anim_instance ) {
1521                         anim_release_render_instance(Wl_icons[i].anim_instance);
1522                         Wl_icons[i].anim_instance = NULL;
1523                 }
1524         }
1525
1526         // stop any overhead anim instances
1527         for (int i = 0; i < MAX_SHIP_TYPES; i++ ) {
1528                 if ( Wl_ships[i].anim_instance ) {
1529                         anim_release_render_instance(Wl_ships[i].anim_instance);
1530                         Wl_ships[i].anim_instance = NULL;
1531                 }
1532         }
1533 }
1534
1535 // free source anim data if allocated
1536 void wl_unload_all_anims()
1537 {
1538         int i;
1539
1540         // unload overhead anim instances
1541         for ( i = 0; i < MAX_SHIP_TYPES; i++ ) {
1542                 if ( Wl_ships[i].anim ) {
1543                         anim_free(Wl_ships[i].anim);
1544                         Wl_ships[i].anim = NULL;
1545                 }
1546         }
1547
1548         // unload weapon anim instances
1549         for ( i = 0; i < MAX_WEAPON_TYPES; i++ ) {
1550                 if ( Wl_icons[i].anim ) {
1551                         anim_free(Wl_icons[i].anim);
1552                         Wl_icons[i].anim = NULL;
1553                 }
1554         }
1555 }
1556
1557 // load all the icons for weapons in the pool
1558 void wl_load_all_icons()
1559 {
1560         #ifndef DEMO // not for FS2_DEMO
1561
1562         int i, j;
1563
1564         for ( i = 0; i < MAX_WEAPON_TYPES; i++ ) {
1565                 // clear out data
1566                 Wl_icons[i].anim = NULL;
1567                 Wl_icons[i].anim_instance = NULL;
1568                 for ( j = 0; j < NUM_ICON_FRAMES; j++ ) {
1569                         Wl_icons[i].icon_bmaps[j] = -1;
1570                 }
1571
1572                 if ( Wl_pool[i] > 0 ) {
1573                         wl_load_icons(i);
1574                 }
1575         }
1576
1577         #endif
1578 }
1579
1580 //      wl_unload_icons() frees the bitmaps used for weapon icons 
1581 void wl_unload_icons()
1582 {
1583         int                                     i,j;
1584         wl_icon_info            *icon;
1585
1586         for ( i = 0; i < MAX_WEAPON_TYPES; i++ ) {
1587                 icon = &Wl_icons[i];
1588
1589                 for ( j = 0; j < NUM_ICON_FRAMES; j++ ) {
1590                         if ( icon->icon_bmaps[j] >= 0 ) {
1591                                 bm_release(icon->icon_bmaps[j]);
1592                                 icon->icon_bmaps[j] = -1;
1593                         }
1594                 }
1595         }
1596 }
1597
1598 // init ship-class specific data
1599 void wl_init_ship_class_data()
1600 {
1601         int i;
1602         wl_ship_class_info      *wl_ship;
1603
1604         for ( i=0; i<MAX_SHIP_TYPES; i++ ) {
1605                 wl_ship = &Wl_ships[i];
1606                 wl_ship->overhead_bitmap = -1;
1607                 wl_ship->anim = NULL;
1608                 wl_ship->anim_instance = NULL;
1609         }
1610 }
1611
1612 // free any allocated ship-class specific data
1613 void wl_free_ship_class_data()
1614 {
1615         int i;
1616         wl_ship_class_info      *wl_ship;
1617
1618         for ( i=0; i<MAX_SHIP_TYPES; i++ ) {
1619                 wl_ship = &Wl_ships[i];
1620
1621                 if ( wl_ship->overhead_bitmap != -1 ) {
1622                         bm_release(wl_ship->overhead_bitmap);
1623                         wl_ship->overhead_bitmap = -1;
1624                 }
1625
1626                 if ( wl_ship->anim != NULL ) {
1627                         wl_ship->anim = NULL;
1628                 }
1629
1630                 if ( wl_ship->anim_instance != NULL ) {
1631                         wl_ship->anim_instance = NULL;
1632                 }
1633         }
1634 }
1635
1636 // Set selected slot to first placed ship
1637 void wl_reset_selected_slot()
1638 {
1639         int i;
1640         Selected_wl_slot = -1;
1641
1642         // in multiplayer, select the slot of the player's ship by default
1643         if((Game_mode & GM_MULTIPLAYER) && !MULTI_PERM_OBSERVER(Net_players[MY_NET_PLAYER_NUM]) && (Wss_slots[Net_player->p_info.ship_index].ship_class >= 0)){
1644                 wl_set_selected_slot(Net_player->p_info.ship_index);
1645                 return;
1646         }
1647
1648         for ( i=0; i<MAX_WSS_SLOTS; i++ ) {
1649                 if ( !ss_disabled_slot(i) ) {
1650                         if ( ss_wing_slot_is_console_player(i) && (Wss_slots[i].ship_class >= 0) ) {
1651                                 wl_set_selected_slot(i);
1652                                 return;
1653                         }
1654                 }
1655         }
1656
1657         // Didn't locate player ship, so just select the first ship we find
1658         for ( i=0; i<MAX_WSS_SLOTS; i++ ) {
1659                 if ( Wss_slots[i].ship_class >=  0 ) {
1660                         wl_set_selected_slot(i);
1661                         break;
1662                 }
1663         }
1664 }
1665
1666 // called whenever it is possible that the current selected slot has had it's ship disappear
1667 void wl_maybe_reset_selected_slot()
1668 {
1669         int reset=0;
1670
1671         if ( Selected_wl_slot == -1 ) {
1672                 reset = 1;
1673         }
1674
1675         if ( Wss_slots[Selected_wl_slot].ship_class < 0 ) {
1676                 reset = 1;
1677         }
1678
1679         if ( reset ) {
1680                 wl_reset_selected_slot();
1681         }
1682 }
1683
1684 // If Selected_wl_class is -1, choose the first weapon available from the pool for an animation
1685 //  - on second thought, choose the first weapon that is oin the ship, then go to the pools
1686 void wl_maybe_reset_selected_weapon_class()
1687 {
1688         int i;
1689
1690         if ( Selected_wl_class >= 0 ) 
1691                 return;
1692
1693         // try to locate a weapon class to show animation for
1694         // first check for a weapon on the ship
1695         for (i=0; i<MAX_WL_WEAPONS; i++) {
1696                 // if alpha 1 has a weapon in bank i, set it as the selected type
1697                 if (Wss_slots[0].wep_count[i] >= 0) {
1698                         Selected_wl_class = Wss_slots[0].wep[i];
1699                         return;
1700                 }
1701         }
1702
1703         // then check for a primary weapon in the pool
1704         for ( i = 0; i < Plist_size; i++ ) {
1705                 if ( Plist[i] >= 0 ) {
1706                         Selected_wl_class = Plist[i];
1707                         return;
1708                 }
1709         }
1710
1711         // finally, if no others found yet, check for a secondary weapon in the pool
1712         for ( i = 0; i < Slist_size; i++ ) {
1713                 if ( Slist[i] >= 0 ) {
1714                         Selected_wl_class = Slist[i];
1715                         return;
1716                 }
1717         }
1718 }
1719
1720 // start an overhead animation, since selected slot has changed
1721 void wl_start_slot_animation(int n)
1722 {
1723 #ifndef DEMO // not for FS2_DEMO
1724
1725         // don't use ani's
1726         // fallback code in wl_render_overhead_view() will 
1727         // use the .pcx files
1728         // should prolly scrub out the 1e06 lines of dead code this leaves
1729 #ifndef MAKE_FS1
1730         return;
1731 #else
1732         int                                             ship_class;
1733         wl_ship_class_info      *wl_ship;
1734         anim_play_struct                aps;
1735
1736         if ( n < 0 ) {
1737                 return;
1738         }
1739
1740         ship_class = Wss_slots[n].ship_class;
1741         
1742         if ( ship_class < 0 ) {
1743                 Int3();
1744                 return;
1745         }
1746
1747         wl_ship = &Wl_ships[ship_class];
1748
1749         // maybe this animation is already playing?
1750         if ( wl_ship->anim_instance ) {
1751                 anim_stop_playing(wl_ship->anim_instance);
1752                 wl_ship->anim_instance = NULL;
1753         }
1754         
1755         // maybe we have to load this animation
1756         if ( wl_ship->anim == NULL ) {
1757                 wl_ship->anim = anim_load(Ship_info[ship_class].overhead_filename, 1);
1758                 if ( wl_ship->anim == NULL ) {
1759                         Int3();         // couldn't load anim filename.. get Alan
1760                         return;
1761                 }
1762         }
1763
1764         anim_play_init(&aps, wl_ship->anim, Wl_overhead_coords[gr_screen.res][0], Wl_overhead_coords[gr_screen.res][1]);
1765         aps.screen_id = ON_WEAPON_SELECT;
1766         aps.framerate_independent = 1;
1767         aps.skip_frames = 0;
1768         wl_ship->anim_instance = anim_play(&aps);
1769 #endif // !MAKE_FS1
1770 #endif // !DEMO
1771 }
1772
1773 // Call when Selected_wl_slot needs to be changed
1774 void wl_set_selected_slot(int slot_num)
1775 {
1776         if ( (slot_num >= 0) && (slot_num != Selected_wl_slot) ) {
1777                 // slot has changed.... start an animation
1778                 wl_start_slot_animation(slot_num);
1779 /*
1780                 if ( Current_screen == ON_WEAPON_SELECT ) {
1781                         gamesnd_play_iface(SND_OVERHEAD_SHIP_ANIM);
1782                 }
1783 */
1784   }
1785
1786         Selected_wl_slot = slot_num;
1787         if ( Selected_wl_slot >= 0 ) {
1788                 wl_set_disabled_weapons(Wss_slots[slot_num].ship_class);
1789         }
1790 }
1791
1792 // determine how many missiles of type 'wi_index' will fit into capacity
1793 int wl_calc_missile_fit(int wi_index, int capacity)
1794 {
1795         if ( wi_index < 0 ) {
1796                 return 0;
1797         }
1798
1799         Assert(Weapon_info[wi_index].subtype == WP_MISSILE);
1800         return fl2i( capacity / Weapon_info[wi_index].cargo_size + 0.5f );
1801 }
1802
1803 // fill out the weapons for this ship_class
1804 void wl_get_ship_class_weapons(int ship_class, int *wep, int *wep_count)
1805 {
1806         ship_info       *sip;
1807         int i;
1808
1809         Assert(ship_class >= 0 && ship_class < MAX_SHIP_TYPES);
1810         sip = &Ship_info[ship_class];
1811
1812         // reset weapons arrays
1813         for ( i=0; i < MAX_WL_WEAPONS; i++ ) {
1814                 wep[i] = -1;
1815                 wep_count[i] = -1;      // -1 means weapon bank doesn't exist.. 0 just means it is empty
1816         }
1817
1818         for ( i = 0; i < sip->num_primary_banks; i++ ) {
1819                 wep[i] = sip->primary_bank_weapons[i];
1820                 wep_count[i] = 1;
1821         }
1822
1823         for ( i = 0; i < sip->num_secondary_banks; i++ ) {
1824                 wep[i+MAX_WL_PRIMARY] = sip->secondary_bank_weapons[i];
1825                 wep_count[i+MAX_WL_PRIMARY] = wl_calc_missile_fit(sip->secondary_bank_weapons[i], sip->secondary_bank_ammo_capacity[i]);
1826         }
1827 }
1828
1829 // fill out the wep[] and wep_count[] arrays for a ship
1830 void wl_get_ship_weapons(int ship_index, int *wep, int *wep_count)
1831 {
1832         int                     i;
1833         wing                    *wp;
1834         ship_weapon     *swp;
1835
1836         Assert(ship_index >= 0);
1837
1838         Assert(Ships[ship_index].wingnum >= 0);
1839         wp = &Wings[Ships[ship_index].wingnum];
1840         swp = &Ships[ship_index].weapons;
1841
1842         for ( i = 0; i < swp->num_primary_banks; i++ ) {
1843                 wep[i] = swp->primary_bank_weapons[i];
1844                 wep_count[i] = 1;
1845                 if ( wep[i] == -1 ) {
1846                         wep_count[i] = 0;
1847                 }
1848         }
1849
1850         for ( i = 0; i < swp->num_secondary_banks; i++ ) {
1851                 wep[i+MAX_WL_PRIMARY] = swp->secondary_bank_weapons[i];
1852                 wep_count[i+MAX_WL_PRIMARY] = swp->secondary_bank_ammo[i];
1853                 if ( wep[i+MAX_WL_PRIMARY] == -1 ) {
1854                         wep_count[i+MAX_WL_PRIMARY] = 0;
1855                 }
1856         }
1857 }
1858
1859 // set wep and wep_count from a ship which sits in the ship_arrivals[] list at index sa_index
1860 void wl_get_parseobj_weapons(int sa_index, int ship_class, int *wep, int *wep_count)
1861 {
1862         int                             i,      pilot_index;
1863         subsys_status   *ss;
1864         ship_info               *sip;
1865         p_object                        *pobjp;
1866
1867         pobjp = &ship_arrivals[sa_index];
1868         sip = &Ship_info[ship_class];
1869
1870         pilot_index = wl_get_pilot_subsys_index(pobjp);
1871
1872         if ( pilot_index == -1 )
1873                 return;
1874
1875         ss = &Subsys_status[pilot_index];
1876
1877         if ( ss->primary_banks[0] != SUBSYS_STATUS_NO_CHANGE ) {
1878                 for ( i=0; i < MAX_PRIMARY_BANKS; i++ ) {
1879                         wep[i] = ss->primary_banks[i];          
1880                 } // end for
1881         }
1882
1883         if ( ss->secondary_banks[0] != SUBSYS_STATUS_NO_CHANGE ) {
1884                 for ( i=0; i < MAX_SECONDARY_BANKS; i++ ) {
1885                         wep[i+MAX_WL_PRIMARY] = ss->secondary_banks[i]; 
1886                 } // end for
1887         }
1888
1889         // ammo counts could still be modified
1890         for ( i=0; i < MAX_SECONDARY_BANKS; i++ ) {
1891                 if ( wep[i+MAX_WL_PRIMARY] >= 0 ) {
1892                         wep_count[i+MAX_WL_PRIMARY] = wl_calc_missile_fit(wep[i+MAX_WL_PRIMARY], fl2i(ss->secondary_ammo[i]/100.0f * sip->secondary_bank_ammo_capacity[i] + 0.5f));
1893                 }
1894         }
1895 }
1896
1897 // ensure that there aren't any bogus weapons assigned by default
1898 void wl_cull_illegal_weapons(int ship_class, int *wep, int *wep_count)
1899 {
1900         int i;
1901         for ( i=0; i < MAX_WL_WEAPONS; i++ ) {
1902                 if ( wep[i] < 0 ) {
1903                         continue;
1904                 }
1905
1906                 if ( !weapon_allowed_for_game_type(Ship_info[ship_class].allowed_weapons[wep[i]]) ) {
1907 //                      wep[i] = -1;
1908                         wep_count[i] = 0;
1909                 }
1910         }
1911 }
1912
1913 // get the weapons info that should be on ship by default
1914 void wl_get_default_weapons(int ship_class, int slot_num, int *wep, int *wep_count)
1915 {
1916         int original_ship_class, i;
1917
1918         Assert(slot_num >= 0 && slot_num < MAX_WSS_SLOTS);
1919
1920         // clear out wep and wep_count
1921         for ( i = 0; i < MAX_WL_WEAPONS; i++ ) {
1922                 wep[i] = -1;
1923                 wep_count[i] = -1;
1924         }
1925
1926         if ( ship_class < 0 )
1927                 return;
1928
1929         original_ship_class = ss_return_original_ship_class(slot_num);
1930
1931         if ( original_ship_class != ship_class ) {
1932                 wl_get_ship_class_weapons(ship_class, wep, wep_count);
1933         } else {
1934                 int sa_index;   // ship arrival index
1935                 sa_index = ss_return_saindex(slot_num);
1936
1937                 if ( sa_index >= 0 ) {
1938                         // still a parse object
1939                         wl_get_ship_class_weapons(ship_class, wep, wep_count);
1940                         wl_get_parseobj_weapons(sa_index, ship_class, wep, wep_count);
1941                 } else {
1942                         // ship has been created
1943 //                      wl_get_ship_weapons(slot_num/4, slot_num%4, wep, wep_count);
1944                         int ship_index = -1;
1945                         p_object *pobjp;
1946                         ss_return_ship(slot_num/4, slot_num%4, &ship_index, &pobjp);
1947                         Assert(ship_index != -1);
1948                         wl_get_ship_weapons(ship_index, wep, wep_count);
1949                 }
1950         }
1951
1952         // ensure that there aren't any bogus weapons assigned by default
1953         wl_cull_illegal_weapons(ship_class, wep, wep_count);    
1954 }
1955
1956 // function to add a weapon_class to ui lists
1957 void wl_add_index_to_list(int wi_index)
1958 {
1959         int i;
1960         if ( Weapon_info[wi_index].subtype == WP_MISSILE ) {
1961
1962                 for ( i=0; i<Slist_size; i++ ) {
1963                         if ( Slist[i] == wi_index )
1964                                 break;
1965                 }
1966
1967                 if ( i == Slist_size ) 
1968                         Slist[Slist_size++] = wi_index;
1969
1970         } else {
1971                 for ( i=0; i<Plist_size; i++ ) {
1972                         if ( Plist[i] == wi_index )
1973                                 break;
1974                 }
1975
1976                 if ( i == Plist_size ) 
1977                         Plist[Plist_size++] = wi_index;
1978         }
1979 }
1980
1981 // remove the weapons specificed by wep[] and wep_count[] from Wl_pool[].
1982 void wl_remove_weps_from_pool(int *wep, int *wep_count, int ship_class)
1983 {
1984         int i, wi_index;
1985
1986         for ( i = 0; i < MAX_WL_WEAPONS; i++ ) {
1987                 wi_index = wep[i];
1988                 if ( wi_index >= 0 ) {
1989                         if ( (wep_count[i] > 0) && ((Wl_pool[wi_index] - wep_count[i]) >= 0) ) {
1990                                 Wl_pool[wi_index] -= wep_count[i];
1991                         } else {
1992                                 // not enough weapons in pool
1993                                 // TEMP HACK: FRED doesn't fill in a weapons pool if there are no starting wings... so
1994                                 //            add to the pool.  This should be fixed.
1995                                 if ( Wss_num_wings <= 0 ) {
1996                                         wl_add_index_to_list(wi_index);
1997                                 } else {
1998         
1999                                         if ( (Wl_pool[wi_index] <= 0) || (wep_count[i] == 0) ) {
2000                                                 // fresh out of this weapon, pick an alternate pool weapon if we can
2001                                                 int wep_pool_index, wep_precedence_index, new_wi_index = -1;
2002                                                 for ( wep_pool_index = 0; wep_pool_index < MAX_WEAPON_TYPES; wep_pool_index++ ) {
2003
2004                                                         if ( Wl_pool[wep_pool_index] <= 0 ) {
2005                                                                 continue;
2006                                                         }
2007
2008                                                         // AL 3-31-98: Only pick another primary if primary, etc
2009                                                         if ( Weapon_info[wi_index].subtype != Weapon_info[wep_pool_index].subtype ) {
2010                                                                 continue;
2011                                                         }
2012
2013                                                         if ( !weapon_allowed_for_game_type(Ship_info[ship_class].allowed_weapons[wep_pool_index]) ) {
2014                                                                 continue;
2015                                                         }
2016
2017
2018                                                         for ( wep_precedence_index = 0; wep_precedence_index < Num_player_weapon_precedence; wep_precedence_index++ ) {
2019                                                                 if ( wep_pool_index == Player_weapon_precedence[wep_precedence_index] ) {
2020                                                                         new_wi_index = wep_pool_index;
2021                                                                         break;
2022                                                                 }
2023                                                         }
2024
2025                                                         if ( new_wi_index >= 0 ) {
2026                                                                 break;
2027                                                         }
2028                                                 }
2029
2030                                                 if ( new_wi_index >= 0 ) {
2031                                                         wep[i] = new_wi_index;
2032                                                         wi_index = new_wi_index;
2033                                                 }
2034                                         }
2035
2036                                         int new_wep_count = wep_count[i];
2037                                         if ( Weapon_info[wi_index].subtype == WP_MISSILE ) {
2038                                                 int secondary_bank_index;
2039                                                 secondary_bank_index = i-3;
2040                                                 if ( secondary_bank_index < 0 ) {
2041                                                         Int3();
2042                                                         secondary_bank_index = 0;
2043                                                 }
2044                                                 new_wep_count = wl_calc_missile_fit(wi_index, Ship_info[ship_class].secondary_bank_ammo_capacity[secondary_bank_index]);
2045                                         }
2046
2047                                         wep_count[i] = min(new_wep_count, Wl_pool[wi_index]);
2048                                         Assert(wep_count[i] >= 0);
2049                                         Wl_pool[wi_index] -= wep_count[i];
2050                                         if ( wep_count[i] <= 0 ) {
2051                                                 wep[i] = -1;
2052                                         }
2053                                 }
2054                         }
2055                 }
2056         }
2057 }
2058
2059 // Init the weapons portion of Wss_slots[] and the ui data in Wl_slots[]
2060 // NOTE: it is assumed that Wl_pool[] has been initialized, and Wss_slots[].ship_class is correctly set
2061 void wl_fill_slots()
2062 {
2063         int i, j;       
2064         int wep[MAX_WL_WEAPONS];
2065         int wep_count[MAX_WL_WEAPONS];
2066         
2067         for ( i = 0; i < MAX_WSS_SLOTS; i++ ) {
2068                 if ( Wss_slots[i].ship_class < 0 ){
2069                         continue;
2070                 }
2071
2072                 // get the weapons info that should be on ship by default
2073                 wl_get_default_weapons(Wss_slots[i].ship_class, i, wep, wep_count);
2074                 wl_remove_weps_from_pool(wep, wep_count, Wss_slots[i].ship_class);
2075
2076                 // copy to Wss_slots[]
2077                 for ( j = 0; j < MAX_WL_WEAPONS; j++ ) {
2078                         Wss_slots[i].wep[j] = wep[j];
2079                         Wss_slots[i].wep_count[j] = wep_count[j];
2080                 }
2081         }       
2082 }
2083
2084 // set up the primary and secondary icons lists that hold the weapons the player can choose from
2085 void wl_init_icon_lists()
2086 {
2087         int i;
2088
2089         Plist_start = 0;                // offset into Plist[]
2090         Slist_start = 0;
2091
2092         Plist_size = 0;         // number of active elements in Plist[]
2093         Slist_size = 0;
2094
2095         for ( i = 0; i < MAX_WEAPON_TYPES; i++ ) {
2096                 Plist[i] = -1;
2097                 Slist[i] = -1;
2098         }
2099
2100         for ( i = 0; i < MAX_WEAPON_TYPES; i++ ) {
2101                 if ( Wl_pool[i] > 0 ) {
2102                         if ( Weapon_info[i].subtype == WP_MISSILE ) {
2103                                 Slist[Slist_size++] = i;
2104                         } else {
2105                                 Plist[Plist_size++] = i;
2106                         }
2107                 }
2108         }
2109 }
2110
2111 // initialize team specific weapon select data structures
2112 void weapon_select_init_team(int team_num)
2113 {
2114         Wl_icons = Wl_icons_teams[team_num];
2115         ss_set_team_pointers(team_num);
2116
2117         wl_init_pool(&Team_data[team_num]);
2118         wl_init_icon_lists();
2119         wl_init_ship_class_data();
2120
2121         wl_load_all_icons();
2122         wl_load_all_anims();
2123
2124         wl_fill_slots();
2125 }
2126
2127 // close out what weapon_select_init_team() set up but only when we are not acutally
2128 // in the weapon select screen
2129 void weapon_select_close_team()
2130 {
2131         if (Weapon_select_open)
2132                 return;
2133
2134         wl_unload_all_anim_instances();
2135         wl_unload_all_anims();
2136 }
2137
2138 // This init is called even before the weapons loadout screen is entered.  It is called when the
2139 // briefing state is entered.
2140 void weapon_select_common_init()
2141 {
2142         int idx;
2143         
2144         if((Game_mode & GM_MULTIPLAYER) && (Netgame.type_flags & NG_TYPE_TEAM)){
2145                 // initialize for all teams
2146                 for(idx=0;idx<MULTI_TS_MAX_TEAMS;idx++){
2147                         weapon_select_init_team(idx);
2148                 }
2149
2150                 // re-initialize for me specifically
2151                 weapon_select_init_team(Common_team);
2152         } else {        
2153                 // initialize for my own team
2154                 weapon_select_init_team(Common_team);
2155         }
2156
2157         wl_reset_selected_slot();
2158         wl_reset_carried_icon();
2159         wl_maybe_reset_selected_weapon_class();
2160 }
2161
2162 // ---------------------------------------------------------------------------------
2163 // weapon_select_init() is called to load the bitmaps and set up the mask regions for
2164 //      the weapon loadout screen.  common_select_init() is called to load the animations
2165 // and bitmaps which are in common with the ship select and briefing screens.
2166 //
2167 // The Weapon_select_open flag is set to 1 when weapon_select_init() completes successfully
2168 //
2169 void weapon_select_init()
2170 {
2171         common_set_interface_palette("WeaponPalette");
2172         common_flash_button_init();
2173
2174         // for multiplayer, change the state in my netplayer structure
2175         if ( Game_mode & GM_MULTIPLAYER )
2176                 Net_player->state = NETPLAYER_STATE_WEAPON_SELECT;
2177
2178         ship_stop_animation();
2179         stop_weapon_animation();
2180         Weapon_anim_class = -1;
2181
2182         set_active_ui(&Weapon_ui_window);
2183         Current_screen = ON_WEAPON_SELECT;
2184         Last_wl_ship_class = -1;
2185
2186         wl_maybe_reset_selected_slot();
2187         wl_set_disabled_weapons(Wss_slots[Selected_wl_slot].ship_class);
2188
2189         help_overlay_set_state(WL_OVERLAY,0);
2190
2191         if ( Weapon_select_open ) {
2192                 wl_maybe_reset_selected_weapon_class();
2193                 wl_start_slot_animation(Selected_wl_slot);
2194                 common_buttons_maybe_reload(&Weapon_ui_window); // AL 11-21-97: this is necessary since we may returning from the hotkey
2195                                                                                                                                                 // screen, which can release common button bitmaps.
2196                 common_reset_buttons();
2197                 nprintf(("Alan","weapon_select_init() returning without doing anything\n"));
2198
2199                 // if we're in multiplayer always select the player's ship
2200                 wl_reset_selected_slot();
2201
2202                 return;
2203         }
2204
2205         nprintf(("Alan","entering weapon_select_init()\n"));
2206         common_select_init();
2207         
2208         WeaponSelectMaskBitmap = bm_load(Wl_loadout_select_mask[gr_screen.res]);
2209         if (WeaponSelectMaskBitmap < 0) {
2210                 if (gr_screen.res == GR_640) {
2211                         Error(LOCATION,"Could not load in 'weaponloadout-m'!");
2212                 } else if (gr_screen.res == GR_1024) {
2213                         Error(LOCATION,"Could not load in '2_weaponloadout-m'!");
2214                 } else {
2215                         Error(LOCATION,"Could not load in 'weaponloadout-m'!");
2216                 }
2217         }
2218
2219         Weaponselect_mask_w = -1;
2220         Weaponselect_mask_h = -1;
2221
2222         // get a pointer to bitmap by using bm_lock()
2223         WeaponSelectMaskPtr = bm_lock(WeaponSelectMaskBitmap, 8, BMP_AABITMAP);
2224         WeaponSelectMaskData = (ubyte*)WeaponSelectMaskPtr->data;
2225         Assert(WeaponSelectMaskData != NULL);
2226         bm_get_info(WeaponSelectMaskBitmap, &Weaponselect_mask_w, &Weaponselect_mask_h);
2227
2228
2229         // Set up the mask regions
2230    // initialize the different regions of the menu that will react when the mouse moves over it
2231         Num_weapon_select_regions = 0;
2232         
2233         snazzy_menu_add_region(&Weapon_select_region[Num_weapon_select_regions++], "",  COMMON_BRIEFING_REGION,                         0);
2234         snazzy_menu_add_region(&Weapon_select_region[Num_weapon_select_regions++], "",  COMMON_SS_REGION,                                               0);
2235         snazzy_menu_add_region(&Weapon_select_region[Num_weapon_select_regions++], "",  COMMON_WEAPON_REGION,                           0);
2236         snazzy_menu_add_region(&Weapon_select_region[Num_weapon_select_regions++], "",  COMMON_COMMIT_REGION,                           0);
2237         snazzy_menu_add_region(&Weapon_select_region[Num_weapon_select_regions++], "",  COMMON_HELP_REGION,                                     0);
2238         snazzy_menu_add_region(&Weapon_select_region[Num_weapon_select_regions++], "",  COMMON_OPTIONS_REGION,                          0);
2239
2240         snazzy_menu_add_region(&Weapon_select_region[Num_weapon_select_regions++], "",  WING_0_SHIP_0,          0);
2241         snazzy_menu_add_region(&Weapon_select_region[Num_weapon_select_regions++], "",  WING_0_SHIP_1,          0);
2242         snazzy_menu_add_region(&Weapon_select_region[Num_weapon_select_regions++], "",  WING_0_SHIP_2,          0);
2243         snazzy_menu_add_region(&Weapon_select_region[Num_weapon_select_regions++], "",  WING_0_SHIP_3,          0);
2244         snazzy_menu_add_region(&Weapon_select_region[Num_weapon_select_regions++], "",  WING_1_SHIP_0,          0);
2245         snazzy_menu_add_region(&Weapon_select_region[Num_weapon_select_regions++], "",  WING_1_SHIP_1,          0);
2246         snazzy_menu_add_region(&Weapon_select_region[Num_weapon_select_regions++], "",  WING_1_SHIP_2,          0);
2247         snazzy_menu_add_region(&Weapon_select_region[Num_weapon_select_regions++], "",  WING_1_SHIP_3,          0);
2248         snazzy_menu_add_region(&Weapon_select_region[Num_weapon_select_regions++], "",  WING_2_SHIP_0,          0);
2249         snazzy_menu_add_region(&Weapon_select_region[Num_weapon_select_regions++], "",  WING_2_SHIP_1,          0);
2250         snazzy_menu_add_region(&Weapon_select_region[Num_weapon_select_regions++], "",  WING_2_SHIP_2,          0);
2251         snazzy_menu_add_region(&Weapon_select_region[Num_weapon_select_regions++], "",  WING_2_SHIP_3,          0);
2252
2253         snazzy_menu_add_region(&Weapon_select_region[Num_weapon_select_regions++], "",  ICON_PRIMARY_0,         0);
2254         snazzy_menu_add_region(&Weapon_select_region[Num_weapon_select_regions++], "",  ICON_PRIMARY_1,         0);
2255         snazzy_menu_add_region(&Weapon_select_region[Num_weapon_select_regions++], "",  ICON_PRIMARY_2,         0);
2256         snazzy_menu_add_region(&Weapon_select_region[Num_weapon_select_regions++], "",  ICON_PRIMARY_3,         0);
2257         snazzy_menu_add_region(&Weapon_select_region[Num_weapon_select_regions++], "",  ICON_SECONDARY_0,               0);
2258         snazzy_menu_add_region(&Weapon_select_region[Num_weapon_select_regions++], "",  ICON_SECONDARY_1,               0);
2259         snazzy_menu_add_region(&Weapon_select_region[Num_weapon_select_regions++], "",  ICON_SECONDARY_2,               0);
2260         snazzy_menu_add_region(&Weapon_select_region[Num_weapon_select_regions++], "",  ICON_SECONDARY_3,               0);
2261
2262         snazzy_menu_add_region(&Weapon_select_region[Num_weapon_select_regions++], "",  ICON_SHIP_PRIMARY_0,            0);
2263         snazzy_menu_add_region(&Weapon_select_region[Num_weapon_select_regions++], "",  ICON_SHIP_PRIMARY_1,            0);
2264         snazzy_menu_add_region(&Weapon_select_region[Num_weapon_select_regions++], "",  ICON_SHIP_PRIMARY_2,            0);
2265         snazzy_menu_add_region(&Weapon_select_region[Num_weapon_select_regions++], "",  ICON_SHIP_SECONDARY_0,          0);
2266         snazzy_menu_add_region(&Weapon_select_region[Num_weapon_select_regions++], "",  ICON_SHIP_SECONDARY_1,          0);
2267         snazzy_menu_add_region(&Weapon_select_region[Num_weapon_select_regions++], "",  ICON_SHIP_SECONDARY_2,          0);
2268         snazzy_menu_add_region(&Weapon_select_region[Num_weapon_select_regions++], "",  ICON_SHIP_SECONDARY_3,          0);
2269
2270         // init common UI
2271         Weapon_ui_window.create( 0, 0, gr_screen.max_w, gr_screen.max_h, 0 );
2272
2273         if(Game_mode & GM_MULTIPLAYER){
2274                 Weapon_ui_window.set_mask_bmap(Wl_mask_multi[gr_screen.res]);
2275         } else {
2276                 Weapon_ui_window.set_mask_bmap(Wl_mask_single[gr_screen.res]);
2277         }
2278
2279         // initialize background bitmap
2280         if(Game_mode & GM_MULTIPLAYER) {
2281                 Weapon_select_background_bitmap = bm_load(Weapon_select_multi_background_fname[gr_screen.res]);
2282         } else {
2283                 Weapon_select_background_bitmap = bm_load(Weapon_select_background_fname[gr_screen.res]);
2284         }
2285
2286         Weapon_ui_window.tooltip_handler = wl_tooltip_handler;
2287         common_buttons_init(&Weapon_ui_window);
2288         weapon_buttons_init();
2289         Weapon_select_open = 1;
2290
2291         // if we're in multiplayer always select the player's ship
2292         wl_reset_selected_slot();
2293 }
2294
2295 // ----------------------------------------------------------------
2296 // wl_dump_carried_icon()
2297 void wl_dump_carried_icon()
2298 {
2299         if ( wl_icon_being_carried() ) {
2300                 // Add back into the weapon pool
2301                 if ( Carried_wl_icon.from_bank >= 0 ) {
2302                         // return to list
2303                         wl_drop(Carried_wl_icon.from_bank, -1, -1, Carried_wl_icon.weapon_class, Carried_wl_icon.from_slot);
2304                 } else {
2305                         if ( wl_carried_icon_moved() ) {
2306                                 gamesnd_play_iface(SND_ICON_DROP);
2307                         }                       
2308                 }
2309
2310                 wl_reset_carried_icon();
2311         }
2312 }
2313
2314 // ----------------------------------------------------------------
2315 // drop_icon_on_slot()
2316 //
2317 // Drop the Carried_wl_icon onto the specified slot.  The slot numbering is:
2318 //
2319 // 0->2: primary weapons
2320 // 3-6: secondary weapons
2321 //
2322 // These are the slots that exist beside the overhead view of the ship.
2323 // on the weapons loadout screen.
2324 //
2325 int drop_icon_on_slot(int bank_num)
2326 {
2327         if ( Selected_wl_slot == -1 ) {
2328                 return 0;
2329         }
2330
2331         if(Game_mode & GM_MULTIPLAYER){
2332                 if(multi_ts_disabled_slot(Selected_wl_slot)){
2333                         return 0;
2334                 }
2335         } else {
2336                 if ( ss_disabled_slot( Selected_wl_slot ) ){
2337                         return 0;
2338                 }
2339         }
2340
2341         // check if slot exists
2342         if ( Wss_slots[Selected_wl_slot].wep_count[bank_num] < 0 ) {
2343                 return 0;
2344         }
2345
2346         if ( !wl_carried_icon_moved() ) {
2347                 wl_reset_carried_icon();
2348                 return 0;
2349         }
2350
2351         wl_drop(Carried_wl_icon.from_bank, Carried_wl_icon.weapon_class, bank_num, -1, Selected_wl_slot);
2352         return 1;
2353 }
2354
2355 // ----------------------------------------------------------------
2356 // maybe_drop_icon_on_slot()
2357 //
2358 void maybe_drop_icon_on_slot(int bank_num)
2359 {
2360         int dropped=0;
2361         if ( !mouse_down(MOUSE_LEFT_BUTTON) ) {
2362                 if ( wl_icon_being_carried() )
2363                         dropped = drop_icon_on_slot(bank_num);
2364
2365                 if ( dropped ) {
2366                         wl_reset_carried_icon();
2367                 }
2368         }
2369 }               
2370
2371 // ----------------------------------------------------------------
2372 // wl_check_for_stopped_ship_anims()
2373 //
2374 void wl_check_for_stopped_ship_anims()
2375 {
2376         int i;
2377         anim_instance *ai;
2378         for ( i = 0; i < MAX_SHIP_TYPES; i++ ) {
2379                 ai = Wl_ships[i].anim_instance;
2380                 if ( ai != NULL ) {
2381                         if ( !anim_playing(ai) ) {
2382                                 Wl_ships[i].anim_instance = NULL;
2383                         }
2384                 }
2385         }
2386 }
2387
2388 // ---------------------------------------------------------------------------------
2389 // do_mouse_over_list_weapon()
2390 //
2391 void do_mouse_over_list_weapon(int index)
2392 {
2393         Hot_weapon_icon = index;
2394
2395         int region_index = ICON_PRIMARY_0+index;
2396         if ( index > 3 ) {
2397                 region_index = ICON_SECONDARY_0 + (index - 4);
2398         }
2399         
2400         if ( Wl_mouse_down_on_region != region_index ){
2401                 return;
2402         }
2403
2404         if ( mouse_down(MOUSE_LEFT_BUTTON) )
2405                 wl_pick_icon_from_list(index);
2406 }
2407
2408 // ---------------------------------------------------------------------------------
2409 // do_mouse_over_ship_weapon()
2410 //
2411 //
2412 // input: index -> bank index on ship (0..6)
2413 //
2414 // returns: 
2415 //          0 -> icon was not dropped on a slot
2416 //                              1 -> icon was dropped on a slot
2417 int do_mouse_over_ship_weapon(int index)
2418 {
2419         int dropped_on_slot, is_moved, mx, my;
2420
2421         dropped_on_slot = 0;
2422         Assert(Selected_wl_slot >= 0);
2423
2424         if ( ss_disabled_slot( Selected_wl_slot ) )
2425                 return 0;
2426
2427         Hot_weapon_bank_icon = index;   // bank icon will be drawn highlighted
2428
2429         if ( mouse_down(MOUSE_LEFT_BUTTON) ) {
2430                 if ( Wl_mouse_down_on_region == (ICON_SHIP_PRIMARY_0+index) ){
2431                         pick_from_ship_slot(index);
2432                 }
2433         } else {
2434                 int was_carried = wl_icon_being_carried();
2435                 maybe_drop_icon_on_slot(index);
2436                 if ( was_carried && !wl_icon_being_carried() ) {
2437                         mouse_get_pos( &mx, &my );
2438                         if ( Carried_wl_icon.from_x != mx || Carried_wl_icon.from_y != my) {
2439                                 dropped_on_slot = 1;
2440                         }
2441                 }
2442         }
2443
2444         // set Hot_weapon_bank if a droppable icon is being held over a slot that
2445         // can accept that icon
2446         is_moved = 0;
2447         mouse_get_pos( &mx, &my );
2448         if ( Carried_wl_icon.from_x != mx || Carried_wl_icon.from_y != my) {
2449                 is_moved = 1;
2450         }
2451
2452         if ( wl_icon_being_carried() && is_moved ) {
2453                 if ( Weapon_info[Carried_wl_icon.weapon_class].subtype != WP_MISSILE ) {
2454                         if ( (index < 3) && (Wss_slots[Selected_wl_slot].wep_count[index] >= 0) ) 
2455                                 Hot_weapon_bank = index;
2456                 } else {
2457                         if ( index >= 3 && ( Wss_slots[Selected_wl_slot].wep_count[index] >= 0) ) 
2458                                 Hot_weapon_bank = index;
2459                 }
2460         }
2461
2462         return dropped_on_slot;
2463 }
2464
2465
2466 // maybe flash a button if player hasn't done anything for a while
2467 void wl_maybe_flash_button()
2468 {
2469         if ( common_flash_bright() ) {
2470                 // commit button
2471                 if ( Common_buttons[Current_screen-1][gr_screen.res][3].button.button_hilighted() ) {
2472                         common_flash_button_init();
2473                 } else {
2474                         Common_buttons[Current_screen-1][gr_screen.res][3].button.draw_forced(1);
2475                 }
2476         }
2477 }
2478
2479
2480 void weapon_select_render(float frametime)
2481 {
2482         if ( !Background_playing ) {
2483                 gr_set_bitmap(Weapon_select_background_bitmap, GR_ALPHABLEND_NONE, GR_BITBLT_MODE_NORMAL, 1.0f, -1, -1);
2484                 gr_bitmap(0, 0);
2485         }
2486
2487         anim_render_all(0, frametime);
2488         anim_render_all(ON_SHIP_SELECT, frametime);
2489 }
2490
2491 // draw the weapon description text
2492 // this wipes in
2493 void wl_render_weapon_desc(float frametime)
2494 {
2495 #ifndef MAKE_FS1
2496         int *weapon_desc_coords;
2497         int *weapon_title_coords;
2498
2499         // retrieve the correct set of text coordinates
2500         if (Game_mode & GM_MULTIPLAYER) {
2501                 weapon_desc_coords = Wl_new_weapon_desc_coords_multi[gr_screen.res];
2502                 weapon_title_coords = Wl_new_weapon_title_coords_multi[gr_screen.res];
2503         } else {
2504                 weapon_desc_coords = Wl_new_weapon_desc_coords[gr_screen.res];
2505                 weapon_title_coords = Wl_new_weapon_title_coords[gr_screen.res];
2506         }
2507
2508         // render the normal version of the weapom desc
2509         char bright_char[WEAPON_DESC_MAX_LINES];                        // one bright char per line
2510         if (!Weapon_desc_wipe_done) {
2511                 // draw mid-wipe version
2512                 // decide which char is last (and bright)
2513                 int bright_char_index = (int)(Weapon_desc_wipe_time_elapsed * WEAPON_DESC_MAX_LENGTH / WEAPON_DESC_WIPE_TIME);
2514                 int i, w, h, curr_len;
2515                 
2516                 // draw weapon title (above weapon anim)
2517                 for (i=0; i<2; i++) {
2518                         curr_len = strlen(Weapon_desc_lines[i]);
2519
2520                         if (bright_char_index < curr_len) {
2521                                 // save bright char and plunk in some nulls to shorten string
2522                                 bright_char[i] = Weapon_desc_lines[i][bright_char_index];
2523                                 Weapon_desc_lines[i][bright_char_index] = '\0';
2524
2525                                 // draw the strings
2526                                 gr_set_color_fast(&Color_white);                        
2527                                 gr_string(weapon_title_coords[0], weapon_title_coords[1]+(10*i), Weapon_desc_lines[i]);
2528
2529                                 // draw the bright letters
2530                                 gr_set_color_fast(&Color_bright_white);
2531                                 gr_get_string_size(&w, &h, Weapon_desc_lines[i], curr_len);
2532                                 gr_printf(weapon_title_coords[0]+w, weapon_title_coords[1]+(10*i), "%c", bright_char[i]);
2533
2534                                 // restore the bright char to the string
2535                                 Weapon_desc_lines[i][bright_char_index] = bright_char[i];
2536
2537                         } else {
2538                                 // draw the string
2539                                 gr_set_color_fast(&Color_white);
2540                                 gr_string(weapon_title_coords[0], weapon_title_coords[1]+(10*i), Weapon_desc_lines[i]);
2541                         }
2542                 }
2543
2544                 // draw weapon desc (below weapon anim)
2545                 for (i=2; i<WEAPON_DESC_MAX_LINES; i++) {
2546                         curr_len = strlen(Weapon_desc_lines[i]);
2547
2548                         if (bright_char_index < curr_len) {
2549                                 // save bright char and plunk in some nulls to shorten string
2550                                 bright_char[i] = Weapon_desc_lines[i][bright_char_index];
2551                                 Weapon_desc_lines[i][bright_char_index] = '\0';
2552
2553                                 // draw the string
2554                                 gr_set_color_fast(&Color_white);
2555                                 gr_string(weapon_desc_coords[0], weapon_desc_coords[1]+(10*(i-2)), Weapon_desc_lines[i]);
2556
2557                                 // draw the bright letters
2558                                 gr_set_color_fast(&Color_bright_white);
2559                                 gr_get_string_size(&w, &h, Weapon_desc_lines[i], curr_len);
2560                                 gr_printf(weapon_desc_coords[0]+w, weapon_desc_coords[1]+(10*(i-2)), "%c", bright_char[i]);
2561
2562                                 // restore the bright char to the string
2563                                 Weapon_desc_lines[i][bright_char_index] = bright_char[i];
2564
2565                         } else {
2566                                 // draw the string
2567                                 gr_set_color_fast(&Color_white);
2568                                 gr_string(weapon_desc_coords[0], weapon_desc_coords[1]+(10*(i-2)), Weapon_desc_lines[i]);
2569                         }
2570                 }
2571
2572                 // update time
2573                 Weapon_desc_wipe_time_elapsed += frametime;
2574                 if (Weapon_desc_wipe_time_elapsed >= WEAPON_DESC_WIPE_TIME) {
2575                         // wipe is done,set flag and stop sound
2576                         Weapon_desc_wipe_done = 1;
2577                 }
2578
2579         } else {
2580
2581                 // draw full version
2582                 // FIXME - change to use a for loop 
2583                 gr_set_color_fast(&Color_white);
2584                 gr_string(weapon_title_coords[0], weapon_title_coords[1], Weapon_desc_lines[0]);
2585                 gr_string(weapon_title_coords[0], weapon_title_coords[1] + 10, Weapon_desc_lines[1]);
2586                 gr_string(weapon_desc_coords[0], weapon_desc_coords[1], Weapon_desc_lines[2]);
2587                 gr_string(weapon_desc_coords[0], weapon_desc_coords[1] + 10, Weapon_desc_lines[3]);
2588                 gr_string(weapon_desc_coords[0], weapon_desc_coords[1] + 20, Weapon_desc_lines[4]);
2589                 gr_string(weapon_desc_coords[0], weapon_desc_coords[1] + 30, Weapon_desc_lines[5]);
2590         }
2591 #endif
2592 }
2593
2594
2595
2596 // re-inits wiping vars and causes the current text to wipe in again
2597 void wl_weapon_desc_start_wipe()
2598 {
2599 #ifndef MAKE_FS1
2600         int currchar_src = 0, currline_dest = 2, currchar_dest = 0, i;
2601         int w, h;
2602         int title_len = strlen(Weapon_info[Selected_wl_class].title);
2603
2604         // init wipe vars
2605         Weapon_desc_wipe_time_elapsed = 0.0f;
2606         Weapon_desc_wipe_done = 0;
2607
2608         // break title into two lines if too long
2609         strcpy(Weapon_desc_lines[0], Weapon_info[Selected_wl_class].title);
2610         gr_get_string_size(&w, &h, Weapon_info[Selected_wl_class].title, title_len);
2611         if (w > Weapon_title_max_width[gr_screen.res]) {
2612                 // split
2613                 currchar_src = (int)(((float)title_len / (float)w) * Weapon_title_max_width[gr_screen.res]);                    // char to start space search at
2614                 while (Weapon_desc_lines[0][currchar_src] != ' ') {
2615                         currchar_src--;
2616                         if (currchar_src <= 0) {
2617                                 currchar_src = title_len;
2618                                 break;
2619                         }
2620                 }
2621
2622                 Weapon_desc_lines[0][currchar_src] = '\0';                                                                              // shorten line 0
2623                 strcpy(Weapon_desc_lines[1], &(Weapon_desc_lines[0][currchar_src+1]));          // copy remainder into line 1
2624         } else {
2625                 // entire title in line 0, thus line 1 is empty
2626                 Weapon_desc_lines[1][0] = '\0';
2627         }
2628         
2629         // break current description into lines (break at the /n's)
2630         currchar_src = 0;
2631         while (Weapon_info[Selected_wl_class].desc[currchar_src] != '\0') {
2632                 if (Weapon_info[Selected_wl_class].desc[currchar_src] == '\n') {
2633                         // break here
2634                         if (currchar_src != 0) {                                        // protect against leading /n's
2635                                 Weapon_desc_lines[currline_dest][currchar_dest] = '\0';
2636                                 currline_dest++;
2637                                 currchar_dest = 0;
2638                         }
2639                 } else {
2640                         // straight copy
2641                         Weapon_desc_lines[currline_dest][currchar_dest] = Weapon_info[Selected_wl_class].desc[currchar_src];
2642                         currchar_dest++;
2643                 }
2644
2645                 currchar_src++;
2646
2647                 Assert(currline_dest < WEAPON_DESC_MAX_LINES);
2648                 Assert(currchar_dest < WEAPON_DESC_MAX_LENGTH);
2649         }
2650
2651         // wrap up the line processing
2652         Weapon_desc_lines[currline_dest][currchar_dest] = '\0';
2653         for (i=currline_dest+1; i<WEAPON_DESC_MAX_LINES; i++) {
2654                 Weapon_desc_lines[i][0] = '\0';
2655         }
2656 #endif
2657 }
2658
2659
2660
2661 // ---------------------------------------------------------------------------------
2662 // weapon_select_do() is called once per frame while in the weapon loadout screen.  
2663 //
2664 // Calls to common_ functions are made for those functions which are common to the
2665 // ship select and briefing screens.
2666 //
2667 void weapon_select_do(float frametime)
2668 {
2669         int k, wl_choice, snazzy_action;
2670         //char buf[256];
2671
2672         if ( !Weapon_select_open )
2673                 weapon_select_init();
2674
2675         wl_choice = snazzy_menu_do(WeaponSelectMaskData, Weaponselect_mask_w, Weaponselect_mask_h, Num_weapon_select_regions, Weapon_select_region, &snazzy_action, 0);
2676
2677         if ( wl_choice >= 0 ) {
2678                 if ( snazzy_action == SNAZZY_CLICKED ) {
2679                         nprintf(("Alan","got one\n"));
2680                 } 
2681         }
2682
2683         Hot_wl_slot = -1;
2684         Hot_weapon_icon = -1;
2685         Hot_weapon_bank = -1;
2686         Hot_weapon_bank_icon = -1;
2687
2688         k = common_select_do(frametime);
2689         
2690         if ( help_overlay_active(WL_OVERLAY) ) {
2691                 if ( Weapon_anim_class >= 0 && Wl_icons[Weapon_anim_class].anim_instance ) {
2692                         anim_pause(Wl_icons[Weapon_anim_class].anim_instance);
2693                 }
2694         }
2695         else {
2696                 if ( Weapon_anim_class >= 0 && Wl_icons[Weapon_anim_class].anim_instance ) {
2697                         anim_unpause(Wl_icons[Weapon_anim_class].anim_instance);
2698                 }
2699         }
2700
2701         // Check common keypresses
2702         common_check_keys(k);
2703
2704         if ( Mouse_down_last_frame ) {
2705                 Wl_mouse_down_on_region = wl_choice;
2706         }
2707
2708         if ( wl_choice > -1 ) {
2709                 switch(wl_choice) {
2710                         case ICON_PRIMARY_0:
2711                                 do_mouse_over_list_weapon(0);
2712                                 break;
2713                         case ICON_PRIMARY_1:
2714                                 do_mouse_over_list_weapon(1);
2715                                 break;
2716                         case ICON_PRIMARY_2:
2717                                 do_mouse_over_list_weapon(2);
2718                                 break;
2719                         case ICON_PRIMARY_3:
2720                                 do_mouse_over_list_weapon(3);
2721                                 break;
2722                         case ICON_SECONDARY_0:
2723                                 do_mouse_over_list_weapon(4);
2724                                 break;
2725                         case ICON_SECONDARY_1:
2726                                 do_mouse_over_list_weapon(5);
2727                                 break;
2728                         case ICON_SECONDARY_2:
2729                                 do_mouse_over_list_weapon(6);
2730                                 break;
2731                         case ICON_SECONDARY_3:
2732                                 do_mouse_over_list_weapon(7);
2733                                 break;
2734                         case ICON_SHIP_PRIMARY_0:
2735                                 if ( do_mouse_over_ship_weapon(0) )
2736                                         wl_choice = -1;
2737                                 break;
2738                         case ICON_SHIP_PRIMARY_1:
2739                                 if ( do_mouse_over_ship_weapon(1) )
2740                                         wl_choice = -1;
2741                                 break;
2742                         case ICON_SHIP_PRIMARY_2:
2743                                 if ( do_mouse_over_ship_weapon(2) )
2744                                         wl_choice = -1;
2745                                 break;
2746                         case ICON_SHIP_SECONDARY_0:
2747                                 if ( do_mouse_over_ship_weapon(3) )
2748                                         wl_choice = -1;
2749                                 break;
2750                         case ICON_SHIP_SECONDARY_1:
2751                                 if ( do_mouse_over_ship_weapon(4) )
2752                                         wl_choice = -1;
2753                                 break;
2754                         case ICON_SHIP_SECONDARY_2:
2755                                 if ( do_mouse_over_ship_weapon(5) )
2756                                         wl_choice = -1;
2757                                 break;
2758                         case ICON_SHIP_SECONDARY_3:
2759                                 if ( do_mouse_over_ship_weapon(6) )
2760                                         wl_choice = -1;
2761                                 break;
2762                         case WING_0_SHIP_0:
2763                                 Hot_wl_slot = 0;
2764                                 break;
2765                         case WING_0_SHIP_1:
2766                                 Hot_wl_slot = 1;
2767                                 break;
2768                         case WING_0_SHIP_2:
2769                                 Hot_wl_slot = 2;
2770                                 break;
2771                         case WING_0_SHIP_3:
2772                                 Hot_wl_slot = 3;
2773                                 break;
2774                         case WING_1_SHIP_0:
2775                                 Hot_wl_slot = 4;
2776                                 break;
2777                         case WING_1_SHIP_1:
2778                                 Hot_wl_slot = 5;
2779                                 break;
2780                         case WING_1_SHIP_2:
2781                                 Hot_wl_slot = 6;
2782                                 break;
2783                         case WING_1_SHIP_3:
2784                                 Hot_wl_slot = 7;
2785                                 break;
2786                         case WING_2_SHIP_0:
2787                                 Hot_wl_slot = 8;
2788                                 break;
2789                         case WING_2_SHIP_1:
2790                                 Hot_wl_slot = 9;
2791                                 break;
2792                         case WING_2_SHIP_2:
2793                                 Hot_wl_slot = 10;
2794                                 break;
2795                         case WING_2_SHIP_3:
2796                                 Hot_wl_slot = 11;
2797                                 break;
2798
2799                         default:
2800                                 break;
2801                 }       // end switch
2802         }
2803
2804         if ( !mouse_down(MOUSE_LEFT_BUTTON) )  {
2805                 wl_dump_carried_icon();
2806         }
2807
2808         // Check for a mouse click on buttons
2809         common_check_buttons();
2810         weapon_check_buttons();
2811
2812         // Check for the mouse clicks over a region
2813         if ( wl_choice > -1 && snazzy_action == SNAZZY_CLICKED ) {
2814                 switch (wl_choice) {
2815                                 case ICON_PRIMARY_0:
2816                                         maybe_select_new_weapon(0);
2817                                         break;
2818                                 case ICON_PRIMARY_1:
2819                                         maybe_select_new_weapon(1);
2820                                         break;
2821                                 case ICON_PRIMARY_2:
2822                                         maybe_select_new_weapon(2);
2823                                         break;
2824                                 case ICON_PRIMARY_3:
2825                                         maybe_select_new_weapon(3);
2826                                         break;
2827                                 case ICON_SECONDARY_0:
2828                                         maybe_select_new_weapon(4);
2829                                         break;
2830                                 case ICON_SECONDARY_1:
2831                                         maybe_select_new_weapon(5);
2832                                         break;
2833                                 case ICON_SECONDARY_2:
2834                                         maybe_select_new_weapon(6);
2835                                         break;
2836                                 case ICON_SECONDARY_3:
2837                                         maybe_select_new_weapon(7);
2838                                         break;
2839                                 case ICON_SHIP_PRIMARY_0:
2840                                         maybe_select_new_ship_weapon(0);
2841                                         break;
2842                                 case ICON_SHIP_PRIMARY_1:
2843                                         maybe_select_new_ship_weapon(1);
2844                                         break;
2845                                 case ICON_SHIP_PRIMARY_2:
2846                                         maybe_select_new_ship_weapon(2);
2847                                         break;
2848                                 case ICON_SHIP_SECONDARY_0:
2849                                         maybe_select_new_ship_weapon(3);
2850                                         break;
2851                                 case ICON_SHIP_SECONDARY_1:
2852                                         maybe_select_new_ship_weapon(4);
2853                                         break;
2854                                 case ICON_SHIP_SECONDARY_2:
2855                                         maybe_select_new_ship_weapon(5);
2856                                         break;
2857                                 case ICON_SHIP_SECONDARY_3:
2858                                         maybe_select_new_ship_weapon(6);
2859                                         break;
2860                                 case WING_0_SHIP_0:
2861                                         maybe_select_wl_slot(0,0);
2862                                         break;
2863                                 case WING_0_SHIP_1:
2864                                         maybe_select_wl_slot(0,1);
2865                                         break;
2866                                 case WING_0_SHIP_2:
2867                                         maybe_select_wl_slot(0,2);
2868                                         break;
2869                                 case WING_0_SHIP_3:
2870                                         maybe_select_wl_slot(0,3);
2871                                         break;
2872                                 case WING_1_SHIP_0:
2873                                         maybe_select_wl_slot(1,0);
2874                                         break;
2875                                 case WING_1_SHIP_1:
2876                                         maybe_select_wl_slot(1,1);
2877                                         break;
2878                                 case WING_1_SHIP_2:
2879                                         maybe_select_wl_slot(1,2);
2880                                         break;
2881                                 case WING_1_SHIP_3:
2882                                         maybe_select_wl_slot(1,3);
2883                                         break;
2884                                 case WING_2_SHIP_0:
2885                                         maybe_select_wl_slot(2,0);
2886                                         break;
2887                                 case WING_2_SHIP_1:
2888                                         maybe_select_wl_slot(2,1);
2889                                         break;
2890                                 case WING_2_SHIP_2:
2891                                         maybe_select_wl_slot(2,2);
2892                                         break;
2893                                 case WING_2_SHIP_3:
2894                                         maybe_select_wl_slot(2,3);
2895                                         break;
2896
2897                                 default:
2898                                         break;
2899
2900                         }       // end switch
2901                 }
2902
2903         gr_reset_clip();
2904
2905         if ( Weapon_anim_class != -1 && ( Selected_wl_class == Weapon_anim_class ) ) {
2906                 wl_icon_info *icon;
2907                 Assert(Selected_wl_class >= 0 && Selected_wl_class < MAX_WEAPON_TYPES );
2908                 if ( Weapon_anim_class != Selected_wl_class ) 
2909                         start_weapon_animation(Selected_wl_class);      
2910
2911                 Assert(Weapon_anim_class == Selected_wl_class);
2912                 icon = &Wl_icons[Selected_wl_class];
2913                 if ( icon->anim_instance ) {
2914                         if ( icon->anim_instance->frame_num == icon->anim_instance->stop_at ) {
2915                                 anim_play_struct aps;
2916                                 int *weapon_ani_coords;
2917
2918                                 // get the correct weapon animations coords
2919                                 if (Game_mode & GM_MULTIPLAYER) {
2920                                         weapon_ani_coords = Wl_weapon_ani_coords_multi[gr_screen.res];
2921                                 } else {
2922                                         weapon_ani_coords = Wl_weapon_ani_coords[gr_screen.res];
2923                                 }
2924
2925                                 anim_release_render_instance(icon->anim_instance);
2926                                 anim_play_init(&aps, icon->anim, weapon_ani_coords[0], weapon_ani_coords[1]);
2927                                 aps.start_at = WEAPON_ANIM_LOOP_FRAME-1;
2928                                 aps.screen_id = ON_WEAPON_SELECT;
2929                                 aps.framerate_independent = 1;
2930                                 aps.skip_frames = 0;
2931                                 icon->anim_instance = anim_play(&aps);
2932                         }
2933                 }
2934         }
2935
2936         weapon_select_render(frametime);
2937         if ( !Background_playing ) {            
2938                 Weapon_ui_window.draw();
2939                 wl_redraw_pressed_buttons();
2940                 draw_wl_icons();
2941                 anim_render_all(ON_WEAPON_SELECT, frametime);
2942                 wl_check_for_stopped_ship_anims();
2943                 wl_render_overhead_view(frametime);
2944                 wl_draw_ship_weapons(Selected_wl_slot);
2945                 for ( int i = 0; i < MAX_WING_BLOCKS; i++ ) {
2946                         draw_wing_block(i, Hot_wl_slot, Selected_wl_slot, -1);
2947                 }
2948                 common_render_selected_screen_button();
2949         }
2950
2951         // maybe blit the multiplayer "locked" button
2952         if((Game_mode & GM_MULTIPLAYER) && multi_ts_is_locked()){
2953                 Buttons[gr_screen.res][WL_BUTTON_MULTI_LOCK].button.draw_forced(2);
2954         }
2955
2956
2957         if ( wl_icon_being_carried() ) {
2958                 int mx, my, sx, sy;
2959                 Assert(Carried_wl_icon.weapon_class < MAX_WEAPON_TYPES);
2960                 mouse_get_pos( &mx, &my );
2961                 sx = mx + Wl_delta_x;
2962                 sy = my + Wl_delta_y;
2963
2964
2965                 if ( Wl_icons[Carried_wl_icon.weapon_class].can_use > 0) {
2966                         gr_set_color_fast(&Color_blue);
2967                         gr_set_bitmap(Wl_icons[Carried_wl_icon.weapon_class].icon_bmaps[WEAPON_ICON_FRAME_SELECTED], GR_ALPHABLEND_NONE, GR_BITBLT_MODE_NORMAL, 1.0f, -1, -1);
2968                         gr_bitmap(sx, sy);
2969                 }
2970
2971                 // draw number to prevent it from disappearing on clicks
2972                 if ( Carried_wl_icon.from_bank >= MAX_WL_PRIMARY ) {
2973                         if ( mx == Carried_wl_icon.from_x && my == Carried_wl_icon.from_y ) {
2974                                 int num_missiles = Wss_slots[Carried_wl_icon.from_slot].wep_count[Carried_wl_icon.from_bank];
2975                                 //sprintf(buf, "%d", num_missiles);
2976                                 //gr_set_color_fast(&Color_white);
2977
2978                                 //int x_offset = wl_fury_missile_offset_hack(Carried_wl_icon.weapon_class, num_missiles);
2979                                 //gr_string(sx-19-x_offset, sy+8, buf);
2980
2981
2982                                 wl_render_icon_count(num_missiles, Wl_bank_coords[gr_screen.res][Carried_wl_icon.from_bank][0], Wl_bank_coords[gr_screen.res][Carried_wl_icon.from_bank][1]);
2983                         }
2984                 }
2985
2986                 // check so see if this is really a legal weapon to carry away
2987                 if ( !Wl_icons[Carried_wl_icon.weapon_class].can_use ) {
2988                         int diffx, diffy;
2989                         diffx = abs(Carried_wl_icon.from_x-mx);
2990                         diffy = abs(Carried_wl_icon.from_y-my);
2991                         if ( (diffx > 2) || (diffy > 2) ) {
2992                                 int ship_class = Wss_slots[Selected_wl_slot].ship_class;
2993                                 wl_pause_anim();
2994                                 if (Lcl_gr) {
2995                                         // might have to get weapon name translation
2996                                         char display_name[128];
2997                                         strncpy(display_name, Weapon_info[Carried_wl_icon.weapon_class].name, 128);
2998                                         lcl_translate_wep_name(display_name);
2999                                         popup(PF_USE_AFFIRMATIVE_ICON, 1, POPUP_OK, XSTR( "A %s is unable to carry %s weaponry", 633), Ship_info[ship_class].name, display_name);
3000                                 } else {
3001                                         popup(PF_USE_AFFIRMATIVE_ICON, 1, POPUP_OK, XSTR( "A %s is unable to carry %s weaponry", 633), Ship_info[ship_class].name, Weapon_info[Carried_wl_icon.weapon_class].name);
3002                                 }
3003                                 wl_unpause_anim();
3004                                 wl_dump_carried_icon();
3005                         }
3006                 }
3007         }
3008
3009         if ( Weapon_anim_class != Selected_wl_class ) {
3010                 start_weapon_animation(Selected_wl_class);
3011         }
3012
3013         // render weapon description text
3014         wl_render_weapon_desc(frametime);
3015
3016         wl_maybe_flash_button();
3017
3018         // should render the chatbox as close to the end as possible so it overlaps all controls
3019         if(!Background_playing){
3020
3021                 // render some extra stuff in multiplayer
3022                 if(Game_mode & GM_MULTIPLAYER){                         
3023                         // render the chatbox
3024                         chatbox_render();
3025
3026                         // draw tooltips
3027                         Weapon_ui_window.draw_tooltip();
3028
3029                         // render the status indicator for the voice system
3030                         multi_common_voice_display_status();
3031
3032                         // blit the "ships/players" locked button
3033                         // multi_ts_blit_locked_button();
3034                 }
3035         }
3036
3037         // blit help overlay if active
3038         help_overlay_maybe_blit(WL_OVERLAY);
3039         gr_flip();      
3040
3041         // If the commit button was pressed, do the commit button actions.  Done at the end of the
3042         // loop so there isn't a skip in the animation (since ship_create() can take a long time if
3043         // the ship model is not in memory
3044         if ( Commit_pressed ) {
3045                 if(Game_mode & GM_MULTIPLAYER){
3046                         multi_ts_commit_pressed();                      
3047                 } else {
3048                         commit_pressed();
3049                 }               
3050                 Commit_pressed = 0;
3051         }
3052 }
3053
3054 // -------------------------------------------------------------------------------
3055 // weapon_select_close() will free the bitmap slot and memory that was allocated
3056 // to store the mask bitmap.
3057 //
3058 // Weapon_select_open is cleared when this function completes.
3059 //
3060 void weapon_select_close()
3061 {
3062         if ( !Weapon_select_open ) {
3063                 nprintf(("Alan","weapon_select_close() returning without doing anything\n"));
3064                 return;
3065         }
3066
3067         nprintf(("Alan", "Entering weapon_select_close()\n"));
3068
3069         stop_weapon_animation();
3070
3071         // done with the bitmaps, so unlock it
3072         bm_unlock(WeaponSelectMaskBitmap);
3073
3074         Weapon_ui_window.destroy();
3075
3076 #ifdef MAKE_FS1
3077         common_free_interface_palette();
3078 #endif
3079
3080         // unload bitmaps
3081         help_overlay_unload(WL_OVERLAY);
3082
3083         bm_unload(WeaponSelectMaskBitmap);
3084
3085         wl_unload_icons();
3086         wl_unload_all_anim_instances();
3087         wl_unload_all_anims();
3088
3089         Selected_wl_class = -1;
3090         Weapon_select_open = 0;
3091 }
3092
3093
3094 // ------------------------------------------------------------------------
3095 //      wl_render_icon_count()
3096 //              renders the number next to the weapon icon
3097 //
3098 // input:       x,y                     =>              x,y screen position OF THE ICON (NOT where u want the text, 
3099 //                                                                              this is calculated to prevent overlapping)
3100 //                              num                     =>              the actual count to be printed
3101 //
3102 void wl_render_icon_count(int num, int x, int y)
3103 {
3104         char buf[32];
3105         int num_w, num_h;
3106         int number_to_draw = (num > 1000) ? 999 : num;          // cap count @ 999
3107         Assert(number_to_draw >= 0);
3108
3109         sprintf(buf, "%d", number_to_draw);
3110         gr_get_string_size(&num_w, &num_h, buf, strlen(buf));
3111
3112         // render
3113         gr_set_color_fast(&Color_white);
3114 #ifdef MAKE_FS1
3115         // strict left align
3116         gr_string(x-21, y+8, buf);
3117 #else
3118         gr_string(x-num_w-4, y+8, buf);
3119 #endif
3120 }
3121
3122
3123 // ------------------------------------------------------------------------
3124 //      wl_render_icon()
3125 //
3126 // input:       index                   =>              index into Wl_icons[], identifying which weapon to draw
3127 //                              x,y                     =>              x,y screen position to draw icon at
3128 //                              num                     =>              count for weapon
3129 //                              draw_num_flag =>        0 if not to draw count for weapon, nonzero otherwise
3130 //                              hot_mask                =>              value that should match Hot_weapon_icon to show mouse is over
3131 //                              hot_bank_mask =>        value that should match Hot_weapon_bank_icon to show mouse is over
3132 //                              select_mask     =>              value that should match Selected_wl_class to show icon is selected
3133 //
3134 void wl_render_icon(int index, int x, int y, int num, int draw_num_flag, int hot_mask, int hot_bank_mask, int select_mask)
3135 {
3136         int                             bitmap_id;
3137         wl_icon_info    *icon;
3138
3139         if ( Selected_wl_slot == -1 )
3140                 return;
3141
3142         icon = &Wl_icons[index];
3143
3144         if ( icon->icon_bmaps[0] == -1 ) {
3145                 wl_load_icons(index);
3146         }
3147                 
3148         // assume default bitmap is to be used
3149         bitmap_id = icon->icon_bmaps[WEAPON_ICON_FRAME_NORMAL]; // normal frame
3150
3151         if ( bitmap_id < 0 ) {
3152                 Int3();
3153                 return;
3154         }
3155
3156         // next check if ship has mouse over it
3157         if ( !wl_icon_being_carried() ) {
3158                 if ( Hot_weapon_icon > -1 && Hot_weapon_icon == hot_mask )
3159                         bitmap_id = icon->icon_bmaps[WEAPON_ICON_FRAME_HOT];
3160
3161                 if ( Hot_weapon_bank_icon > -1 && Hot_weapon_bank_icon == hot_bank_mask )
3162                         bitmap_id = icon->icon_bmaps[WEAPON_ICON_FRAME_HOT];
3163         }
3164
3165         // if icon is selected
3166         if ( Selected_wl_class > -1 ) {
3167                 if ( Selected_wl_class == select_mask)
3168                         bitmap_id = icon->icon_bmaps[WEAPON_ICON_FRAME_SELECTED];       // selected icon
3169         }
3170
3171         // if icon is disabled
3172         if ( !icon->can_use ) {
3173                 bitmap_id = icon->icon_bmaps[WEAPON_ICON_FRAME_DISABLED];       // disabled icon
3174         }
3175
3176         gr_set_color_fast(&Color_blue);
3177         gr_set_bitmap(bitmap_id, GR_ALPHABLEND_NONE, GR_BITBLT_MODE_NORMAL, 1.0f, -1, -1);
3178         gr_bitmap(x, y);
3179
3180         // draw the number of the item
3181         // now, right justified
3182         if ( draw_num_flag != 0 ) {             
3183 #ifdef MAKE_FS1
3184                 // add a little extra text padding for secondary weapons
3185                 if ( hot_mask > 3 ) {
3186                         wl_render_icon_count(num, x-9, y);
3187                 } else {
3188                         wl_render_icon_count(num, x, y);
3189                 }
3190 #else
3191                 wl_render_icon_count(num, x, y);
3192 #endif
3193         }
3194 }
3195
3196 // ------------------------------------------------------------------------
3197 //      wl_draw_ship_weapons()
3198 //
3199 // Draw the icons for the weapons that are currently on the selected ship
3200 //
3201 // input:       slot_num                =>              Slot to draw weapons for
3202 //
3203 void wl_draw_ship_weapons(int index)
3204 {
3205         int             i;
3206         int             *wep, *wep_count;
3207
3208         if ( index == -1 )
3209                 return;
3210
3211         Assert(index >= 0 && index < MAX_WSS_SLOTS);
3212         wep = Wss_slots[index].wep;
3213         wep_count = Wss_slots[index].wep_count;
3214
3215         for ( i = 0; i < MAX_WL_WEAPONS; i++ ) {
3216
3217                 if ( Carried_wl_icon.from_bank == i && Carried_wl_icon.from_slot == index ) {
3218                         continue;
3219                 }
3220
3221                 if ( (wep[i] != -1) && (wep_count[i] > 0) ) {
3222                         int x_offset = wl_fury_missile_offset_hack(wep[i], wep_count[i]);
3223                         x_offset =0;
3224                         wl_render_icon( wep[i], Wl_bank_coords[gr_screen.res][i][0]+x_offset, Wl_bank_coords[gr_screen.res][i][1], wep_count[i], Wl_bank_count_draw_flags[i], -1, i, wep[i]);
3225                 }
3226         }
3227 }
3228
3229 // ------------------------------------------------------------------------
3230 //      draw_wl_icon_with_number()
3231 //
3232 // input:       list_count                      =>              list position on screen (0-7)
3233 //                              weapon_class            =>              class of weapon
3234 //
3235 void draw_wl_icon_with_number(int list_count, int weapon_class)
3236 {
3237         Assert( list_count >= 0 && list_count < 8 );    
3238
3239         wl_render_icon(weapon_class, Wl_weapon_icon_coords[gr_screen.res][list_count][0], Wl_weapon_icon_coords[gr_screen.res][list_count][1],
3240                                            Wl_pool[weapon_class], 1, list_count, -1, weapon_class);
3241 }
3242
3243 // ------------------------------------------------------------------------
3244 //      draw_wl_icons()
3245 //
3246 // Draw the weapon icons that are available
3247 void draw_wl_icons()
3248 {
3249         int i, count;
3250
3251         count=0;
3252         for ( i = Plist_start; i < Plist_size; i++ ) {
3253                 draw_wl_icon_with_number(count, Plist[i]);
3254                 if ( ++count > 3 )
3255                         break;
3256         }
3257
3258         count=0;
3259         for ( i = Slist_start; i < Slist_size; i++ ) {
3260                 draw_wl_icon_with_number(count+4, Slist[i]);
3261                 if ( ++count > 3 )
3262                         break;
3263         }
3264 }
3265
3266 // ------------------------------------------------------------------------
3267 // wl_pick_icon_from_list()
3268 //
3269 // determine if an icon from the scrollable weapon list can be picked up 
3270 // (for drag and drop).  It calculates the difference in x & y between the icon
3271 // and the mouse, so we can move the icon with the mouse in a realistic way
3272 // input: index (0..7) 
3273 void wl_pick_icon_from_list(int index)
3274 {
3275         int weapon_class, mx, my;
3276
3277         // if this is a multiplayer game and the player is an observer, he can never pick any weapons up
3278         if((Game_mode & GM_MULTIPLAYER) && (Net_player->flags & NETINFO_FLAG_OBSERVER)){
3279                 return;
3280         }
3281
3282         // if a weapon is being carried, do nothing
3283         if ( wl_icon_being_carried() ) {
3284                 return;
3285         }
3286
3287         if ( index < 4 ) {
3288                 weapon_class = Plist[Plist_start+index];
3289         } else {
3290                 weapon_class = Slist[Slist_start+index-4];
3291         }
3292
3293         // there isn't a weapon there at all!
3294         if ( weapon_class < 0 ) 
3295                 return;
3296
3297         // no weapons left of that class
3298         if ( Wl_pool[weapon_class] <= 0 ) {
3299                 return;
3300         }
3301
3302 /*
3303         // some are available, but weapon cannot be used on current ship class
3304         if ( !Wl_icons[weapon_class].can_use ) {
3305                 wl_pause_anim();
3306
3307                 int ship_class = Wss_slots[Selected_wl_slot].ship_class;
3308                 popup(PF_USE_AFFIRMATIVE_ICON, 1, POPUP_OK, "A %s is unable to carry %s weaponry", Ship_info[ship_class].name, Weapon_info[weapon_class].name);
3309
3310                 wl_unpause_anim();
3311                 return;
3312         }
3313 */
3314
3315         wl_set_carried_icon(-1, -1, weapon_class);
3316         common_flash_button_init();
3317
3318         mouse_get_pos( &mx, &my );
3319         Wl_delta_x = Wl_weapon_icon_coords[gr_screen.res][index][0] - mx;
3320         Wl_delta_y = Wl_weapon_icon_coords[gr_screen.res][index][1] - my;
3321 }
3322
3323 // ------------------------------------------------------------------------
3324 //      pick_from_ship_slot()
3325 //
3326 // input: num   ->      index into shipb banks (0..2 primary, 3..6 secondary)
3327 void pick_from_ship_slot(int num)
3328 {
3329         int mx, my, *wep, *wep_count;
3330                 
3331         Assert(num < 7);
3332
3333         if ( Selected_wl_slot == -1 )
3334                 return;
3335
3336         if ( wl_icon_being_carried() )
3337                 return;
3338
3339         if ( ss_disabled_slot( Selected_wl_slot ) )
3340                 return;
3341
3342         wep = Wss_slots[Selected_wl_slot].wep;
3343         wep_count = Wss_slots[Selected_wl_slot].wep_count;
3344
3345         // check if a weapon even exists in that slot
3346         if ( (wep[num] < 0) || (wep_count[num] <= 0) ) {
3347                 return;
3348         }
3349
3350         Assert(Wl_icons[wep[num]].can_use);
3351
3352         wl_set_carried_icon(num, Selected_wl_slot, wep[num]);
3353         common_flash_button_init();
3354                         
3355         mouse_get_pos( &mx, &my );
3356
3357         int x_offset = wl_fury_missile_offset_hack(wep[num], wep_count[num]);
3358         Wl_delta_x = Wl_bank_coords[gr_screen.res][num][0] - mx + x_offset;
3359         Wl_delta_y = Wl_bank_coords[gr_screen.res][num][1] - my;
3360
3361         Carried_wl_icon.from_x = mx;
3362         Carried_wl_icon.from_y = my;
3363 }
3364
3365 // determine if this slot has no weapons
3366 int wl_slots_all_empty(wss_unit *slot)
3367 {
3368         int                     i;
3369
3370         for ( i = 0; i < MAX_WL_WEAPONS; i++ ) {
3371                 if ( (slot->wep_count[i] > 0) && (slot->wep[i] >= 0) ) 
3372                         return 0;
3373         }
3374
3375         return 1;
3376 }
3377
3378 // ------------------------------------------------------------------------
3379 //      wl_update_ship_weapons()
3380 //
3381 // Change a ship's weapons based on the information contained in the
3382 // Weapon_data[] structure that is filled in during weapon loadout
3383 //
3384 // returns: -1  =>      if the playre ship has no weapons
3385 //                              0       =>      function finished without errors        
3386 int wl_update_ship_weapons(int objnum, wss_unit *slot )
3387 {
3388         // AL 11-15-97: Ensure that the player ship hasn't removed all 
3389         //                                       weapons from their ship.  This will cause a warning to appear.
3390         if ( objnum == OBJ_INDEX(Player_obj) && Weapon_select_open ) {
3391                 if ( wl_slots_all_empty(slot) ) {
3392                         return -1;
3393                 }
3394         }
3395
3396         wl_bash_ship_weapons(&Ships[Objects[objnum].instance].weapons, slot);
3397         return 0;
3398 }
3399
3400
3401 // ------------------------------------------------------------------------
3402 //      wl_update_parse_object_weapons()
3403 //
3404 // Set the Pilot subsystem of a parse_object to the weapons that are setup
3405 // for the wing_block,wing_slot ship
3406 //
3407 // input:       pobjp   =>      pointer to parse object that references Pilot subsystem
3408 //
3409 void wl_update_parse_object_weapons(p_object *pobjp, wss_unit *slot)
3410 {
3411         int                             i,      j, sidx, pilot_index, max_count;
3412         subsys_status   *ss;
3413         ship_info               *sip;
3414
3415         Assert(slot->ship_class >= 0);
3416         sip = &Ship_info[slot->ship_class];
3417
3418         pilot_index = wl_get_pilot_subsys_index(pobjp);
3419
3420         if ( pilot_index == -1 ) 
3421                 return;
3422                                                 
3423         ss = &Subsys_status[pilot_index];
3424
3425         for ( i = 0; i < MAX_PRIMARY_BANKS; i++ ) {
3426                 ss->primary_banks[i] = -1;              
3427         }
3428
3429         for ( i = 0; i < MAX_SECONDARY_BANKS; i++ ) {
3430                 ss->secondary_banks[i] = -1;            
3431         }
3432
3433         j = 0;
3434         for ( i = 0; i < MAX_WL_PRIMARY; i++ ) {
3435                 if ( (slot->wep_count[i] > 0) && (slot->wep[i] >= 0) ) {
3436                         ss->primary_banks[j] = slot->wep[i];
3437                         j++;
3438                 }
3439         }
3440
3441         j = 0;
3442         for ( i = 0; i < MAX_WL_SECONDARY; i++ ) {
3443                 sidx = i+MAX_WL_PRIMARY;
3444                 if ( (slot->wep_count[sidx] > 0) && (slot->wep[sidx] >= 0) ) {
3445                         ss->secondary_banks[j] = slot->wep[sidx];
3446                         // Important: the secondary_ammo[] value is a percentage of max capacity!
3447                         max_count = wl_calc_missile_fit(slot->wep[sidx], Ship_info[slot->ship_class].secondary_bank_ammo_capacity[j]);
3448                         ss->secondary_ammo[j] = fl2i( i2fl(slot->wep_count[sidx]) / max_count * 100.0f + 0.5f);
3449                         j++;
3450                 }
3451         }
3452 }
3453
3454 // ------------------------------------------------------------------------
3455 // stop_weapon_animation()
3456 //
3457 // Stop the current weapon animation from playing.
3458 //
3459 void stop_weapon_animation()
3460 {
3461         if ( Weapon_anim_class < 0 )
3462                 return;
3463
3464         if ( anim_playing(Wl_icons[Weapon_anim_class].anim_instance) )
3465                 anim_release_render_instance(Wl_icons[Weapon_anim_class].anim_instance);
3466
3467         Wl_icons[Weapon_anim_class].anim_instance = NULL;
3468         Weapon_anim_class = -1;
3469 }
3470
3471 // ------------------------------------------------------------------------
3472 // start_weapon_animation()
3473 //
3474 // Start the current weapon animation from playing.
3475 //
3476 void start_weapon_animation(int weapon_class) 
3477 {
3478         wl_icon_info    *icon;
3479         int *weapon_ani_coords;
3480
3481         if ( weapon_class < 0 )
3482                 return;
3483
3484         if ( weapon_class == Weapon_anim_class ) 
3485                 return;
3486
3487         // get the correct weapon animations coords
3488         if (Game_mode & GM_MULTIPLAYER) {
3489                 weapon_ani_coords = Wl_weapon_ani_coords_multi[gr_screen.res];
3490         } else {
3491                 weapon_ani_coords = Wl_weapon_ani_coords[gr_screen.res];
3492         }
3493
3494         icon = &Wl_icons[weapon_class];
3495
3496         // stop current animation playing
3497         stop_weapon_animation();
3498
3499         // see if we need to load in the animation from disk
3500         if ( icon->anim == NULL ) {
3501                 wl_load_anim(weapon_class);
3502                 /*
3503                 icon->anim = anim_load(Weapon_info[weapon_class].anim_filename, 1);
3504                 if ( icon->anim == NULL ) {
3505                         Int3(); // could not open the weapon animation
3506                         return;
3507                 }
3508                 */
3509         }
3510
3511         // see if we need to get an instance
3512         if ( icon->anim_instance == NULL ) {
3513                 anim_play_struct aps;
3514
3515                 anim_play_init(&aps, icon->anim, weapon_ani_coords[0], weapon_ani_coords[1]);
3516                 aps.screen_id = ON_WEAPON_SELECT;
3517                 aps.framerate_independent = 1;
3518                 aps.skip_frames = 0;
3519                 icon->anim_instance = anim_play(&aps);
3520                 gamesnd_play_iface(SND_WEAPON_ANIM_START);
3521         }
3522
3523         Weapon_anim_class = weapon_class;
3524
3525         // start the text wipe
3526         wl_weapon_desc_start_wipe();
3527 }
3528
3529 // reset the weapons loadout to the defaults in the mission
3530 void wl_reset_to_defaults()
3531 {
3532         // don't reset of weapons pool in multiplayer
3533         if(Game_mode & GM_MULTIPLAYER){
3534                 return;
3535         }
3536
3537         wl_init_pool(&Team_data[Common_team]);
3538         wl_init_icon_lists();
3539         wl_fill_slots();
3540         wl_reset_selected_slot();
3541         wl_reset_carried_icon();
3542         wl_maybe_reset_selected_weapon_class();
3543 }
3544
3545 // Bash ship weapons, based on what is stored in the stored weapons loadout
3546 // NOTE: Wss_slots[] is assumed to be correctly set
3547 void wl_bash_ship_weapons(ship_weapon *swp, wss_unit *slot)
3548 {
3549         int             i, j, sidx;
3550         
3551         for ( i = 0; i < MAX_PRIMARY_BANKS; i++ ) {
3552                 swp->primary_bank_weapons[i] = -1;              
3553         }
3554
3555         for ( i = 0; i < MAX_SECONDARY_BANKS; i++ ) {
3556                 swp->secondary_bank_weapons[i] = -1;            
3557         }
3558
3559         j = 0;
3560         for ( i = 0; i < MAX_WL_PRIMARY; i++ ) {
3561                 if ( (slot->wep_count[i] > 0) && (slot->wep[i] >= 0) ) {
3562                         swp->primary_bank_weapons[j] = slot->wep[i];                    
3563                         j++;
3564                 }
3565         }
3566         swp->num_primary_banks = j;
3567
3568         j = 0;
3569         for ( i = 0; i < MAX_WL_SECONDARY; i++ ) {
3570                 sidx = i+MAX_WL_PRIMARY;
3571                 if ( (slot->wep_count[sidx] > 0) && (slot->wep[sidx] >= 0) ) {
3572                         swp->secondary_bank_weapons[j] = slot->wep[sidx];
3573                         swp->secondary_bank_ammo[j] = slot->wep_count[sidx];
3574                         j++;
3575                 }
3576         }
3577         swp->num_secondary_banks = j;
3578 }
3579
3580 // utility function for swapping two banks
3581 void wl_swap_weapons(int ship_slot, int from_bank, int to_bank)
3582 {
3583         wss_unit        *slot;
3584         int     tmp;
3585         slot = &Wss_slots[ship_slot];
3586
3587         if ( from_bank == to_bank ) {
3588                 return;
3589         }
3590
3591         // swap weapon type
3592         tmp = slot->wep[from_bank];
3593         slot->wep[from_bank] = slot->wep[to_bank];
3594         slot->wep[to_bank] = tmp;
3595
3596         // swap weapon count
3597         tmp = slot->wep_count[from_bank];
3598         slot->wep_count[from_bank] = slot->wep_count[to_bank];
3599         slot->wep_count[to_bank] = tmp;
3600 }
3601
3602 // utility function used to put back overflow into the weapons pool
3603 void wl_saturate_bank(int ship_slot, int bank)
3604 {
3605         wss_unit        *slot;
3606         int             max_count, overflow;
3607
3608         slot = &Wss_slots[ship_slot];
3609
3610         if ( (slot->wep[bank] < 0) || (slot->wep_count <= 0) ) {
3611                 return;
3612         }
3613
3614         max_count = wl_calc_missile_fit(slot->wep[bank], Ship_info[slot->ship_class].secondary_bank_ammo_capacity[bank-3]);
3615         overflow = slot->wep_count[bank] - max_count;
3616         if ( overflow > 0 ) {
3617                 slot->wep_count[bank] -= overflow;
3618                 // add overflow back to pool
3619                 Wl_pool[slot->wep[bank]] += overflow;
3620         }
3621 }
3622
3623 // exit: 0 -> no data changed
3624 //                      1 -> data changed
3625 //       sound => gets filled with sound id to play
3626 int wl_swap_slot_slot(int from_bank, int to_bank, int ship_slot, int *sound)
3627 {
3628         wss_unit        *slot;
3629         slot = &Wss_slots[ship_slot];
3630
3631         if ( slot->ship_class == -1 ) {
3632                 Int3(); // should not be possible
3633                 return 0;
3634         }
3635         
3636         // do nothing if swapping with self
3637         if ( from_bank == to_bank ) {
3638                 *sound=SND_ICON_DROP_ON_WING;
3639                 return 0;       // no update
3640         }
3641
3642         // ensure that source bank exists and has something to pick from
3643         if ( slot->wep[from_bank] == -1 || slot->wep_count[from_bank] <= 0 ) {
3644                 return 0;
3645         }
3646
3647         // ensure that the dest bank exists
3648         if ( slot->wep_count[to_bank] < 0 ) {
3649                 return 0;
3650         }
3651
3652         // ensure that the banks are both of the same class
3653         if ( (IS_BANK_PRIMARY(from_bank) && IS_BANK_SECONDARY(to_bank)) || (IS_BANK_SECONDARY(from_bank) && IS_BANK_PRIMARY(to_bank)) ) {
3654                 // put from_bank back into list
3655                 Wl_pool[slot->wep[from_bank]] += slot->wep_count[from_bank];;           // return to list
3656                 slot->wep[from_bank] = -1;                                                                                                              // remove from slot
3657                 slot->wep_count[from_bank] = 0;
3658                 *sound=SND_ICON_DROP;
3659                 return 1;
3660         }
3661
3662         // case 1: primaries (easy)
3663         if ( IS_BANK_PRIMARY(from_bank) && IS_BANK_PRIMARY(to_bank) ) {
3664                 wl_swap_weapons(ship_slot, from_bank, to_bank);
3665                 *sound=SND_ICON_DROP_ON_WING;
3666                 return 1;
3667         }
3668
3669         // case 2: secondaries (harder)
3670         if ( IS_BANK_SECONDARY(from_bank) && IS_BANK_SECONDARY(to_bank) ) {
3671
3672                 // case 2a: secondaries are the same type
3673                 if ( slot->wep[from_bank] == slot->wep[to_bank] ) {
3674                         int dest_max, dest_can_fit, source_can_give;
3675                         dest_max = wl_calc_missile_fit(slot->wep[to_bank], Ship_info[slot->ship_class].secondary_bank_ammo_capacity[to_bank-3]);
3676
3677                         dest_can_fit = dest_max - slot->wep_count[to_bank];
3678
3679                         if ( dest_can_fit <= 0 ) {
3680                                 // dest bank is already full.. nothing to do here
3681                                 return 0;
3682                         }
3683
3684                         // see how much source can give
3685                         source_can_give = min(dest_can_fit, slot->wep_count[from_bank]);
3686
3687                         if ( source_can_give > 0 ) {                    
3688                                 slot->wep_count[to_bank] += source_can_give;            // add to dest
3689                                 slot->wep_count[from_bank] -= source_can_give;  // take from source
3690                                 *sound=SND_ICON_DROP_ON_WING;
3691                                 return 1;
3692                         } else {
3693                                 return 0;
3694                         }
3695                 }
3696
3697                 // case 2b: secondaries are different types
3698                 if ( slot->wep[from_bank] != slot->wep[to_bank] ) {
3699
3700                         // swap 'em 
3701                         wl_swap_weapons(ship_slot, from_bank, to_bank);
3702
3703                         // put back some on list if required
3704                         wl_saturate_bank(ship_slot, from_bank);
3705                         wl_saturate_bank(ship_slot, to_bank);
3706                         *sound=SND_ICON_DROP_ON_WING;
3707                         return 1;
3708                 }
3709         }
3710
3711         Int3();         // should never get here
3712         return 0;
3713 }
3714
3715 // exit: 0 -> no data changed
3716 //                      1 -> data changed
3717 //       sound => gets filled with sound id to play
3718 int wl_dump_to_list(int from_bank, int to_list, int ship_slot, int *sound)
3719 {
3720         wss_unit        *slot;
3721         slot = &Wss_slots[ship_slot];
3722
3723         // ensure that source bank exists and has something to pick from
3724         if ( slot->wep[from_bank] == -1 || slot->wep_count[from_bank] <= 0 ) {
3725                 return 0;
3726         }
3727
3728         // put weapon bank to the list
3729         Wl_pool[to_list] += slot->wep_count[from_bank];                 // return to list
3730         slot->wep[from_bank] = -1;                                                                              // remove from slot
3731         slot->wep_count[from_bank] = 0;
3732         *sound=SND_ICON_DROP;
3733
3734         return 1;
3735 }
3736
3737 // exit: 0 -> no data changed
3738 //                      1 -> data changed
3739 //       sound => gets filled with sound id to play
3740 int wl_grab_from_list(int from_list, int to_bank, int ship_slot, int *sound)
3741 {
3742         wss_unit        *slot;
3743         slot = &Wss_slots[ship_slot];
3744         int max_fit;
3745
3746         // ensure that the banks are both of the same class
3747         if ( (IS_LIST_PRIMARY(from_list) && IS_BANK_SECONDARY(to_bank)) || (IS_LIST_SECONDARY(from_list) && IS_BANK_PRIMARY(to_bank)) ) {
3748                 // do nothing
3749                 *sound=SND_ICON_DROP;
3750                 return 0;
3751         }
3752
3753         // ensure that dest bank exists
3754         if ( slot->wep_count[to_bank] < 0 ) {
3755                 *sound=SND_ICON_DROP;
3756                 return 0;
3757         }
3758
3759         // bank should be empty:
3760         Assert(slot->wep_count[to_bank] == 0);
3761         Assert(slot->wep[to_bank] < 0);
3762
3763         // ensure that pool has weapon
3764         if ( Wl_pool[from_list] <= 0 ) {
3765                 return 0;
3766         }
3767
3768         // find how much dest bank can fit
3769         if ( to_bank < MAX_WL_PRIMARY ) {
3770                 max_fit = 1;
3771         } else {
3772                 max_fit = wl_calc_missile_fit(from_list, Ship_info[slot->ship_class].secondary_bank_ammo_capacity[to_bank-MAX_WL_PRIMARY]);
3773         }
3774
3775         // take weapon from list
3776         if ( Wl_pool[from_list] < max_fit ) {
3777                 max_fit = Wl_pool[from_list];
3778         }
3779         Wl_pool[from_list] -= max_fit;
3780
3781         // put on the slot
3782         slot->wep[to_bank] = from_list;
3783         slot->wep_count[to_bank] = max_fit;
3784
3785         *sound=SND_ICON_DROP_ON_WING;
3786         return 1;
3787 }
3788
3789 // exit: 0 -> no data changed
3790 //                      1 -> data changed
3791 //       sound => gets filled with sound id to play
3792 int wl_swap_list_slot(int from_list, int to_bank, int ship_slot, int *sound)
3793 {
3794         wss_unit        *slot;
3795         slot = &Wss_slots[ship_slot];
3796         int max_fit;
3797
3798         // ensure that the banks are both of the same class
3799         if ( (IS_LIST_PRIMARY(from_list) && IS_BANK_SECONDARY(to_bank)) || (IS_LIST_SECONDARY(from_list) && IS_BANK_PRIMARY(to_bank)) ) {
3800                 // do nothing
3801                 *sound=SND_ICON_DROP;
3802                 return 0;
3803         }
3804
3805         // ensure that dest bank exists
3806         if ( slot->wep_count[to_bank] < 0 ) {
3807                 return 0;
3808         }
3809
3810         // bank should have something in it
3811         Assert(slot->wep_count[to_bank] > 0);
3812         Assert(slot->wep[to_bank] >= 0);
3813
3814         // ensure that pool has weapon
3815         if ( Wl_pool[from_list] <= 0 ) {
3816                 return 0;
3817         }
3818
3819         // dump slot weapon back into list
3820         Wl_pool[slot->wep[to_bank]] += slot->wep_count[to_bank];
3821         slot->wep_count[to_bank] = 0;
3822         slot->wep[to_bank] = -1;
3823
3824         // put weapon on ship from list
3825         
3826         // find how much dest bank can fit
3827         if ( to_bank < MAX_WL_PRIMARY ) {
3828                 max_fit = 1;
3829         } else {
3830                 max_fit = wl_calc_missile_fit(from_list, Ship_info[slot->ship_class].secondary_bank_ammo_capacity[to_bank-MAX_WL_PRIMARY]);
3831         }
3832
3833         // take weapon from list
3834         if ( Wl_pool[from_list] < max_fit ) {
3835                 max_fit = Wl_pool[from_list];
3836         }
3837         Wl_pool[from_list] -= max_fit;
3838
3839         // put on the slot
3840         slot->wep[to_bank] = from_list;
3841         slot->wep_count[to_bank] = max_fit;
3842
3843         *sound=SND_ICON_DROP_ON_WING;
3844         return 1;
3845 }
3846
3847 // update any interface data that may be dependent on Wss_slots[] 
3848 void wl_synch_interface()
3849 {
3850 }
3851
3852 void wl_apply(int mode,int from_bank,int from_list,int to_bank,int to_list,int ship_slot,int player_index)
3853 {
3854         int update=0;
3855         int sound=-1;
3856         net_player *pl;
3857
3858         // get the appropriate net player
3859         if(Game_mode & GM_MULTIPLAYER){
3860                 if(player_index == -1){
3861                         pl = Net_player;
3862                 } else {
3863                         pl = &Net_players[player_index];
3864                 }
3865         } else {
3866                 pl = NULL;
3867         }
3868
3869         switch(mode){
3870         case WSS_SWAP_SLOT_SLOT:
3871                 update = wl_swap_slot_slot(from_bank, to_bank, ship_slot, &sound);
3872                 break;
3873         case WSS_DUMP_TO_LIST:
3874                 update = wl_dump_to_list(from_bank, to_list, ship_slot, &sound);
3875                 break;
3876         case WSS_GRAB_FROM_LIST:
3877                 update = wl_grab_from_list(from_list, to_bank, ship_slot, &sound);
3878                 break;
3879         case WSS_SWAP_LIST_SLOT:
3880                 update = wl_swap_list_slot(from_list, to_bank, ship_slot, &sound);
3881                 break;
3882         }
3883
3884         // only play this sound if the move was done locally (by the host in other words)
3885         if ( (sound >= 0) && (player_index == -1) ) {   
3886                 gamesnd_play_iface(sound);      
3887         }       
3888
3889         if ( update ) {
3890                 if ( MULTIPLAYER_HOST ) {
3891                         int size;
3892                         ubyte wss_data[MAX_PACKET_SIZE-20];
3893
3894                         size = store_wss_data(wss_data, MAX_PACKET_SIZE-20,sound,player_index);                 
3895                         Assert(pl != NULL);
3896                         send_wss_update_packet(pl->p_info.team,wss_data, size);
3897                 }
3898
3899                 if(Game_mode & GM_MULTIPLAYER){
3900                         Assert(pl != NULL);
3901
3902                         // if the pool we're using has changed, synch stuff up
3903                         if(pl->p_info.team == Net_player->p_info.team){
3904                                 wl_synch_interface();                   
3905                         }
3906                 } else {
3907                         wl_synch_interface();
3908                 }
3909         }               
3910 }
3911
3912 void wl_drop(int from_bank,int from_list,int to_bank,int to_list, int ship_slot, int player_index)
3913 {
3914         int mode;
3915         net_player *pl;
3916
3917         // get the appropriate net player
3918         if(Game_mode & GM_MULTIPLAYER){
3919                 if(player_index == -1){
3920                         pl = Net_player;
3921                 } else {
3922                         pl = &Net_players[player_index];
3923                 }
3924         } else {
3925                 pl = NULL;
3926         }
3927
3928         common_flash_button_init();
3929         if ( !(Game_mode & GM_MULTIPLAYER) || MULTIPLAYER_HOST ) {
3930                 if((Game_mode & GM_MULTIPLAYER) && (Netgame.type_flags & NG_TYPE_TEAM)){
3931                         // set the global pointers to the right pools
3932                         ss_set_team_pointers(pl->p_info.team);
3933                 }
3934
3935
3936                 mode = wss_get_mode(from_bank, from_list, to_bank, to_list, ship_slot);
3937                 if ( mode >= 0 ) {
3938                         wl_apply(mode, from_bank, from_list, to_bank, to_list, ship_slot, player_index);
3939                 }               
3940
3941                 if((Game_mode & GM_MULTIPLAYER) && (Netgame.type_flags & NG_TYPE_TEAM)){
3942                         // set the global pointers to the right pools
3943                         ss_set_team_pointers(Net_player->p_info.team);
3944                 }
3945         } else {
3946                 send_wss_request_packet(Net_player->player_id, from_bank, from_list, to_bank, to_list, ship_slot, -1, WSS_WEAPON_SELECT);
3947         }
3948 }
3949