]> icculus.org git repositories - taylor/freespace2.git/blob - src/missionui/missionweaponchoice.cpp
warnings (clang): unused-but-set-variable
[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 const char *Wl_mask_single[GR_NUM_RESOLUTIONS] = {
561         "weaponloadout-m",
562         "2_weaponloadout-m"
563 };
564
565 static const char *Wl_mask_multi[GR_NUM_RESOLUTIONS] = {
566         "weaponloadoutmulti-m",
567         "2_weaponloadoutmulti-m"
568 };
569
570 static const char *Wl_loadout_select_mask[GR_NUM_RESOLUTIONS] = {
571         "weaponloadout-m",
572         "2_weaponloadout-m"
573 };
574
575
576 static const 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 const 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         const 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(const 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 ( !SDL_strncasecmp(Weapon_info[weapon_class].name, NOX("fury"), 4) ) {
944                 return 3;
945         }
946
947         return 0;
948 }
949
950 const char *wl_tooltip_handler(const char *str)
951 {
952         if (Selected_wl_class < 0)
953                 return NULL;
954
955         if (!SDL_strcasecmp(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 ( !SDL_strcasecmp(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                                 SDL_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         SDL_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         SDL_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                 SDL_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         SDL_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         SDL_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         ship_weapon     *swp;
1834
1835         SDL_assert(ship_index >= 0);
1836
1837         swp = &Ships[ship_index].weapons;
1838
1839         for ( i = 0; i < swp->num_primary_banks; i++ ) {
1840                 wep[i] = swp->primary_bank_weapons[i];
1841                 wep_count[i] = 1;
1842                 if ( wep[i] == -1 ) {
1843                         wep_count[i] = 0;
1844                 }
1845         }
1846
1847         for ( i = 0; i < swp->num_secondary_banks; i++ ) {
1848                 wep[i+MAX_WL_PRIMARY] = swp->secondary_bank_weapons[i];
1849                 wep_count[i+MAX_WL_PRIMARY] = swp->secondary_bank_ammo[i];
1850                 if ( wep[i+MAX_WL_PRIMARY] == -1 ) {
1851                         wep_count[i+MAX_WL_PRIMARY] = 0;
1852                 }
1853         }
1854 }
1855
1856 // set wep and wep_count from a ship which sits in the ship_arrivals[] list at index sa_index
1857 void wl_get_parseobj_weapons(int sa_index, int ship_class, int *wep, int *wep_count)
1858 {
1859         int                             i,      pilot_index;
1860         subsys_status   *ss;
1861         ship_info               *sip;
1862         p_object                        *pobjp;
1863
1864         pobjp = &ship_arrivals[sa_index];
1865         sip = &Ship_info[ship_class];
1866
1867         pilot_index = wl_get_pilot_subsys_index(pobjp);
1868
1869         if ( pilot_index == -1 )
1870                 return;
1871
1872         ss = &Subsys_status[pilot_index];
1873
1874         if ( ss->primary_banks[0] != SUBSYS_STATUS_NO_CHANGE ) {
1875                 for ( i=0; i < MAX_PRIMARY_BANKS; i++ ) {
1876                         wep[i] = ss->primary_banks[i];          
1877                 } // end for
1878         }
1879
1880         if ( ss->secondary_banks[0] != SUBSYS_STATUS_NO_CHANGE ) {
1881                 for ( i=0; i < MAX_SECONDARY_BANKS; i++ ) {
1882                         wep[i+MAX_WL_PRIMARY] = ss->secondary_banks[i]; 
1883                 } // end for
1884         }
1885
1886         // ammo counts could still be modified
1887         for ( i=0; i < MAX_SECONDARY_BANKS; i++ ) {
1888                 if ( wep[i+MAX_WL_PRIMARY] >= 0 ) {
1889                         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));
1890                 }
1891         }
1892 }
1893
1894 // ensure that there aren't any bogus weapons assigned by default
1895 void wl_cull_illegal_weapons(int ship_class, int *wep, int *wep_count)
1896 {
1897         int i;
1898         for ( i=0; i < MAX_WL_WEAPONS; i++ ) {
1899                 if ( wep[i] < 0 ) {
1900                         continue;
1901                 }
1902
1903                 if ( !weapon_allowed_for_game_type(Ship_info[ship_class].allowed_weapons[wep[i]]) ) {
1904 //                      wep[i] = -1;
1905                         wep_count[i] = 0;
1906                 }
1907         }
1908 }
1909
1910 // get the weapons info that should be on ship by default
1911 void wl_get_default_weapons(int ship_class, int slot_num, int *wep, int *wep_count)
1912 {
1913         int original_ship_class, i;
1914
1915         SDL_assert(slot_num >= 0 && slot_num < MAX_WSS_SLOTS);
1916
1917         // clear out wep and wep_count
1918         for ( i = 0; i < MAX_WL_WEAPONS; i++ ) {
1919                 wep[i] = -1;
1920                 wep_count[i] = -1;
1921         }
1922
1923         if ( ship_class < 0 )
1924                 return;
1925
1926         original_ship_class = ss_return_original_ship_class(slot_num);
1927
1928         if ( original_ship_class != ship_class ) {
1929                 wl_get_ship_class_weapons(ship_class, wep, wep_count);
1930         } else {
1931                 int sa_index;   // ship arrival index
1932                 sa_index = ss_return_saindex(slot_num);
1933
1934                 if ( sa_index >= 0 ) {
1935                         // still a parse object
1936                         wl_get_ship_class_weapons(ship_class, wep, wep_count);
1937                         wl_get_parseobj_weapons(sa_index, ship_class, wep, wep_count);
1938                 } else {
1939                         // ship has been created
1940 //                      wl_get_ship_weapons(slot_num/4, slot_num%4, wep, wep_count);
1941                         int ship_index = -1;
1942                         p_object *pobjp;
1943                         ss_return_ship(slot_num/4, slot_num%4, &ship_index, &pobjp);
1944                         SDL_assert(ship_index != -1);
1945                         wl_get_ship_weapons(ship_index, wep, wep_count);
1946                 }
1947         }
1948
1949         // ensure that there aren't any bogus weapons assigned by default
1950         wl_cull_illegal_weapons(ship_class, wep, wep_count);    
1951 }
1952
1953 // function to add a weapon_class to ui lists
1954 void wl_add_index_to_list(int wi_index)
1955 {
1956         int i;
1957         if ( Weapon_info[wi_index].subtype == WP_MISSILE ) {
1958
1959                 for ( i=0; i<Slist_size; i++ ) {
1960                         if ( Slist[i] == wi_index )
1961                                 break;
1962                 }
1963
1964                 if ( i == Slist_size ) 
1965                         Slist[Slist_size++] = wi_index;
1966
1967         } else {
1968                 for ( i=0; i<Plist_size; i++ ) {
1969                         if ( Plist[i] == wi_index )
1970                                 break;
1971                 }
1972
1973                 if ( i == Plist_size ) 
1974                         Plist[Plist_size++] = wi_index;
1975         }
1976 }
1977
1978 // remove the weapons specificed by wep[] and wep_count[] from Wl_pool[].
1979 void wl_remove_weps_from_pool(int *wep, int *wep_count, int ship_class)
1980 {
1981         int i, wi_index;
1982
1983         for ( i = 0; i < MAX_WL_WEAPONS; i++ ) {
1984                 wi_index = wep[i];
1985                 if ( wi_index >= 0 ) {
1986                         if ( (wep_count[i] > 0) && ((Wl_pool[wi_index] - wep_count[i]) >= 0) ) {
1987                                 Wl_pool[wi_index] -= wep_count[i];
1988                         } else {
1989                                 // not enough weapons in pool
1990                                 // TEMP HACK: FRED doesn't fill in a weapons pool if there are no starting wings... so
1991                                 //            add to the pool.  This should be fixed.
1992                                 if ( Wss_num_wings <= 0 ) {
1993                                         wl_add_index_to_list(wi_index);
1994                                 } else {
1995         
1996                                         if ( (Wl_pool[wi_index] <= 0) || (wep_count[i] == 0) ) {
1997                                                 // fresh out of this weapon, pick an alternate pool weapon if we can
1998                                                 int wep_pool_index, wep_precedence_index, new_wi_index = -1;
1999                                                 for ( wep_pool_index = 0; wep_pool_index < MAX_WEAPON_TYPES; wep_pool_index++ ) {
2000
2001                                                         if ( Wl_pool[wep_pool_index] <= 0 ) {
2002                                                                 continue;
2003                                                         }
2004
2005                                                         // AL 3-31-98: Only pick another primary if primary, etc
2006                                                         if ( Weapon_info[wi_index].subtype != Weapon_info[wep_pool_index].subtype ) {
2007                                                                 continue;
2008                                                         }
2009
2010                                                         if ( !weapon_allowed_for_game_type(Ship_info[ship_class].allowed_weapons[wep_pool_index]) ) {
2011                                                                 continue;
2012                                                         }
2013
2014
2015                                                         for ( wep_precedence_index = 0; wep_precedence_index < Num_player_weapon_precedence; wep_precedence_index++ ) {
2016                                                                 if ( wep_pool_index == Player_weapon_precedence[wep_precedence_index] ) {
2017                                                                         new_wi_index = wep_pool_index;
2018                                                                         break;
2019                                                                 }
2020                                                         }
2021
2022                                                         if ( new_wi_index >= 0 ) {
2023                                                                 break;
2024                                                         }
2025                                                 }
2026
2027                                                 if ( new_wi_index >= 0 ) {
2028                                                         wep[i] = new_wi_index;
2029                                                         wi_index = new_wi_index;
2030                                                 }
2031                                         }
2032
2033                                         int new_wep_count = wep_count[i];
2034                                         if ( Weapon_info[wi_index].subtype == WP_MISSILE ) {
2035                                                 int secondary_bank_index;
2036                                                 secondary_bank_index = i-3;
2037                                                 if ( secondary_bank_index < 0 ) {
2038                                                         Int3();
2039                                                         secondary_bank_index = 0;
2040                                                 }
2041                                                 new_wep_count = wl_calc_missile_fit(wi_index, Ship_info[ship_class].secondary_bank_ammo_capacity[secondary_bank_index]);
2042                                         }
2043
2044                                         wep_count[i] = min(new_wep_count, Wl_pool[wi_index]);
2045                                         SDL_assert(wep_count[i] >= 0);
2046                                         Wl_pool[wi_index] -= wep_count[i];
2047                                         if ( wep_count[i] <= 0 ) {
2048                                                 wep[i] = -1;
2049                                         }
2050                                 }
2051                         }
2052                 }
2053         }
2054 }
2055
2056 // Init the weapons portion of Wss_slots[] and the ui data in Wl_slots[]
2057 // NOTE: it is assumed that Wl_pool[] has been initialized, and Wss_slots[].ship_class is correctly set
2058 void wl_fill_slots()
2059 {
2060         int i, j;       
2061         int wep[MAX_WL_WEAPONS];
2062         int wep_count[MAX_WL_WEAPONS];
2063         
2064         for ( i = 0; i < MAX_WSS_SLOTS; i++ ) {
2065                 if ( Wss_slots[i].ship_class < 0 ){
2066                         continue;
2067                 }
2068
2069                 // get the weapons info that should be on ship by default
2070                 wl_get_default_weapons(Wss_slots[i].ship_class, i, wep, wep_count);
2071                 wl_remove_weps_from_pool(wep, wep_count, Wss_slots[i].ship_class);
2072
2073                 // copy to Wss_slots[]
2074                 for ( j = 0; j < MAX_WL_WEAPONS; j++ ) {
2075                         Wss_slots[i].wep[j] = wep[j];
2076                         Wss_slots[i].wep_count[j] = wep_count[j];
2077                 }
2078         }       
2079 }
2080
2081 // set up the primary and secondary icons lists that hold the weapons the player can choose from
2082 void wl_init_icon_lists()
2083 {
2084         int i;
2085
2086         Plist_start = 0;                // offset into Plist[]
2087         Slist_start = 0;
2088
2089         Plist_size = 0;         // number of active elements in Plist[]
2090         Slist_size = 0;
2091
2092         for ( i = 0; i < MAX_WEAPON_TYPES; i++ ) {
2093                 Plist[i] = -1;
2094                 Slist[i] = -1;
2095         }
2096
2097         for ( i = 0; i < MAX_WEAPON_TYPES; i++ ) {
2098                 if ( Wl_pool[i] > 0 ) {
2099                         if ( Weapon_info[i].subtype == WP_MISSILE ) {
2100                                 Slist[Slist_size++] = i;
2101                         } else {
2102                                 Plist[Plist_size++] = i;
2103                         }
2104                 }
2105         }
2106 }
2107
2108 // initialize team specific weapon select data structures
2109 void weapon_select_init_team(int team_num)
2110 {
2111         Wl_icons = Wl_icons_teams[team_num];
2112         ss_set_team_pointers(team_num);
2113
2114         wl_init_pool(&Team_data[team_num]);
2115         wl_init_icon_lists();
2116         wl_init_ship_class_data();
2117
2118         wl_load_all_icons();
2119         wl_load_all_anims();
2120
2121         wl_fill_slots();
2122 }
2123
2124 // close out what weapon_select_init_team() set up but only when we are not acutally
2125 // in the weapon select screen
2126 void weapon_select_close_team()
2127 {
2128         if (Weapon_select_open)
2129                 return;
2130
2131         wl_unload_all_anim_instances();
2132         wl_unload_all_anims();
2133 }
2134
2135 // This init is called even before the weapons loadout screen is entered.  It is called when the
2136 // briefing state is entered.
2137 void weapon_select_common_init()
2138 {
2139         int idx;
2140         
2141         if((Game_mode & GM_MULTIPLAYER) && (Netgame.type_flags & NG_TYPE_TEAM)){
2142                 // initialize for all teams
2143                 for(idx=0;idx<MULTI_TS_MAX_TEAMS;idx++){
2144                         weapon_select_init_team(idx);
2145                 }
2146
2147                 // re-initialize for me specifically
2148                 weapon_select_init_team(Common_team);
2149         } else {        
2150                 // initialize for my own team
2151                 weapon_select_init_team(Common_team);
2152         }
2153
2154         wl_reset_selected_slot();
2155         wl_reset_carried_icon();
2156         wl_maybe_reset_selected_weapon_class();
2157 }
2158
2159 // ---------------------------------------------------------------------------------
2160 // weapon_select_init() is called to load the bitmaps and set up the mask regions for
2161 //      the weapon loadout screen.  common_select_init() is called to load the animations
2162 // and bitmaps which are in common with the ship select and briefing screens.
2163 //
2164 // The Weapon_select_open flag is set to 1 when weapon_select_init() completes successfully
2165 //
2166 void weapon_select_init()
2167 {
2168         common_set_interface_palette("WeaponPalette");
2169         common_flash_button_init();
2170
2171         // for multiplayer, change the state in my netplayer structure
2172         if ( Game_mode & GM_MULTIPLAYER )
2173                 Net_player->state = NETPLAYER_STATE_WEAPON_SELECT;
2174
2175         ship_stop_animation();
2176         stop_weapon_animation();
2177         Weapon_anim_class = -1;
2178
2179         set_active_ui(&Weapon_ui_window);
2180         Current_screen = ON_WEAPON_SELECT;
2181         Last_wl_ship_class = -1;
2182
2183         wl_maybe_reset_selected_slot();
2184         wl_set_disabled_weapons(Wss_slots[Selected_wl_slot].ship_class);
2185
2186         help_overlay_set_state(WL_OVERLAY,0);
2187
2188         if ( Weapon_select_open ) {
2189                 wl_maybe_reset_selected_weapon_class();
2190                 wl_start_slot_animation(Selected_wl_slot);
2191                 common_buttons_maybe_reload(&Weapon_ui_window); // AL 11-21-97: this is necessary since we may returning from the hotkey
2192                                                                                                                                                 // screen, which can release common button bitmaps.
2193                 common_reset_buttons();
2194                 nprintf(("Alan","weapon_select_init() returning without doing anything\n"));
2195
2196                 // if we're in multiplayer always select the player's ship
2197                 wl_reset_selected_slot();
2198
2199                 return;
2200         }
2201
2202         nprintf(("Alan","entering weapon_select_init()\n"));
2203         common_select_init();
2204         
2205         WeaponSelectMaskBitmap = bm_load(Wl_loadout_select_mask[gr_screen.res]);
2206         if (WeaponSelectMaskBitmap < 0) {
2207                 if (gr_screen.res == GR_640) {
2208                         Error(LOCATION,"Could not load in 'weaponloadout-m'!");
2209                 } else if (gr_screen.res == GR_1024) {
2210                         Error(LOCATION,"Could not load in '2_weaponloadout-m'!");
2211                 } else {
2212                         Error(LOCATION,"Could not load in 'weaponloadout-m'!");
2213                 }
2214         }
2215
2216         Weaponselect_mask_w = -1;
2217         Weaponselect_mask_h = -1;
2218
2219         // get a pointer to bitmap by using bm_lock()
2220         WeaponSelectMaskPtr = bm_lock(WeaponSelectMaskBitmap, 8, BMP_AABITMAP);
2221         WeaponSelectMaskData = (ubyte*)WeaponSelectMaskPtr->data;
2222         SDL_assert(WeaponSelectMaskData != NULL);
2223         bm_get_info(WeaponSelectMaskBitmap, &Weaponselect_mask_w, &Weaponselect_mask_h);
2224
2225
2226         // Set up the mask regions
2227    // initialize the different regions of the menu that will react when the mouse moves over it
2228         Num_weapon_select_regions = 0;
2229         
2230         snazzy_menu_add_region(&Weapon_select_region[Num_weapon_select_regions++], "",  COMMON_BRIEFING_REGION,                         0);
2231         snazzy_menu_add_region(&Weapon_select_region[Num_weapon_select_regions++], "",  COMMON_SS_REGION,                                               0);
2232         snazzy_menu_add_region(&Weapon_select_region[Num_weapon_select_regions++], "",  COMMON_WEAPON_REGION,                           0);
2233         snazzy_menu_add_region(&Weapon_select_region[Num_weapon_select_regions++], "",  COMMON_COMMIT_REGION,                           0);
2234         snazzy_menu_add_region(&Weapon_select_region[Num_weapon_select_regions++], "",  COMMON_HELP_REGION,                                     0);
2235         snazzy_menu_add_region(&Weapon_select_region[Num_weapon_select_regions++], "",  COMMON_OPTIONS_REGION,                          0);
2236
2237         snazzy_menu_add_region(&Weapon_select_region[Num_weapon_select_regions++], "",  WING_0_SHIP_0,          0);
2238         snazzy_menu_add_region(&Weapon_select_region[Num_weapon_select_regions++], "",  WING_0_SHIP_1,          0);
2239         snazzy_menu_add_region(&Weapon_select_region[Num_weapon_select_regions++], "",  WING_0_SHIP_2,          0);
2240         snazzy_menu_add_region(&Weapon_select_region[Num_weapon_select_regions++], "",  WING_0_SHIP_3,          0);
2241         snazzy_menu_add_region(&Weapon_select_region[Num_weapon_select_regions++], "",  WING_1_SHIP_0,          0);
2242         snazzy_menu_add_region(&Weapon_select_region[Num_weapon_select_regions++], "",  WING_1_SHIP_1,          0);
2243         snazzy_menu_add_region(&Weapon_select_region[Num_weapon_select_regions++], "",  WING_1_SHIP_2,          0);
2244         snazzy_menu_add_region(&Weapon_select_region[Num_weapon_select_regions++], "",  WING_1_SHIP_3,          0);
2245         snazzy_menu_add_region(&Weapon_select_region[Num_weapon_select_regions++], "",  WING_2_SHIP_0,          0);
2246         snazzy_menu_add_region(&Weapon_select_region[Num_weapon_select_regions++], "",  WING_2_SHIP_1,          0);
2247         snazzy_menu_add_region(&Weapon_select_region[Num_weapon_select_regions++], "",  WING_2_SHIP_2,          0);
2248         snazzy_menu_add_region(&Weapon_select_region[Num_weapon_select_regions++], "",  WING_2_SHIP_3,          0);
2249
2250         snazzy_menu_add_region(&Weapon_select_region[Num_weapon_select_regions++], "",  ICON_PRIMARY_0,         0);
2251         snazzy_menu_add_region(&Weapon_select_region[Num_weapon_select_regions++], "",  ICON_PRIMARY_1,         0);
2252         snazzy_menu_add_region(&Weapon_select_region[Num_weapon_select_regions++], "",  ICON_PRIMARY_2,         0);
2253         snazzy_menu_add_region(&Weapon_select_region[Num_weapon_select_regions++], "",  ICON_PRIMARY_3,         0);
2254         snazzy_menu_add_region(&Weapon_select_region[Num_weapon_select_regions++], "",  ICON_SECONDARY_0,               0);
2255         snazzy_menu_add_region(&Weapon_select_region[Num_weapon_select_regions++], "",  ICON_SECONDARY_1,               0);
2256         snazzy_menu_add_region(&Weapon_select_region[Num_weapon_select_regions++], "",  ICON_SECONDARY_2,               0);
2257         snazzy_menu_add_region(&Weapon_select_region[Num_weapon_select_regions++], "",  ICON_SECONDARY_3,               0);
2258
2259         snazzy_menu_add_region(&Weapon_select_region[Num_weapon_select_regions++], "",  ICON_SHIP_PRIMARY_0,            0);
2260         snazzy_menu_add_region(&Weapon_select_region[Num_weapon_select_regions++], "",  ICON_SHIP_PRIMARY_1,            0);
2261         snazzy_menu_add_region(&Weapon_select_region[Num_weapon_select_regions++], "",  ICON_SHIP_PRIMARY_2,            0);
2262         snazzy_menu_add_region(&Weapon_select_region[Num_weapon_select_regions++], "",  ICON_SHIP_SECONDARY_0,          0);
2263         snazzy_menu_add_region(&Weapon_select_region[Num_weapon_select_regions++], "",  ICON_SHIP_SECONDARY_1,          0);
2264         snazzy_menu_add_region(&Weapon_select_region[Num_weapon_select_regions++], "",  ICON_SHIP_SECONDARY_2,          0);
2265         snazzy_menu_add_region(&Weapon_select_region[Num_weapon_select_regions++], "",  ICON_SHIP_SECONDARY_3,          0);
2266
2267         // init common UI
2268         Weapon_ui_window.create( 0, 0, gr_screen.max_w, gr_screen.max_h, 0 );
2269
2270         if(Game_mode & GM_MULTIPLAYER){
2271                 Weapon_ui_window.set_mask_bmap(Wl_mask_multi[gr_screen.res]);
2272         } else {
2273                 Weapon_ui_window.set_mask_bmap(Wl_mask_single[gr_screen.res]);
2274         }
2275
2276         // initialize background bitmap
2277         if(Game_mode & GM_MULTIPLAYER) {
2278                 Weapon_select_background_bitmap = bm_load(Weapon_select_multi_background_fname[gr_screen.res]);
2279         } else {
2280                 Weapon_select_background_bitmap = bm_load(Weapon_select_background_fname[gr_screen.res]);
2281         }
2282
2283         Weapon_ui_window.tooltip_handler = wl_tooltip_handler;
2284         common_buttons_init(&Weapon_ui_window);
2285         weapon_buttons_init();
2286         Weapon_select_open = 1;
2287
2288         // if we're in multiplayer always select the player's ship
2289         wl_reset_selected_slot();
2290 }
2291
2292 // ----------------------------------------------------------------
2293 // wl_dump_carried_icon()
2294 void wl_dump_carried_icon()
2295 {
2296         if ( wl_icon_being_carried() ) {
2297                 // Add back into the weapon pool
2298                 if ( Carried_wl_icon.from_bank >= 0 ) {
2299                         // return to list
2300                         wl_drop(Carried_wl_icon.from_bank, -1, -1, Carried_wl_icon.weapon_class, Carried_wl_icon.from_slot);
2301                 } else {
2302                         if ( wl_carried_icon_moved() ) {
2303                                 gamesnd_play_iface(SND_ICON_DROP);
2304                         }                       
2305                 }
2306
2307                 wl_reset_carried_icon();
2308         }
2309 }
2310
2311 // ----------------------------------------------------------------
2312 // drop_icon_on_slot()
2313 //
2314 // Drop the Carried_wl_icon onto the specified slot.  The slot numbering is:
2315 //
2316 // 0->2: primary weapons
2317 // 3-6: secondary weapons
2318 //
2319 // These are the slots that exist beside the overhead view of the ship.
2320 // on the weapons loadout screen.
2321 //
2322 int drop_icon_on_slot(int bank_num)
2323 {
2324         if ( Selected_wl_slot == -1 ) {
2325                 return 0;
2326         }
2327
2328         if(Game_mode & GM_MULTIPLAYER){
2329                 if(multi_ts_disabled_slot(Selected_wl_slot)){
2330                         return 0;
2331                 }
2332         } else {
2333                 if ( ss_disabled_slot( Selected_wl_slot ) ){
2334                         return 0;
2335                 }
2336         }
2337
2338         // check if slot exists
2339         if ( Wss_slots[Selected_wl_slot].wep_count[bank_num] < 0 ) {
2340                 return 0;
2341         }
2342
2343         if ( !wl_carried_icon_moved() ) {
2344                 wl_reset_carried_icon();
2345                 return 0;
2346         }
2347
2348         wl_drop(Carried_wl_icon.from_bank, Carried_wl_icon.weapon_class, bank_num, -1, Selected_wl_slot);
2349         return 1;
2350 }
2351
2352 // ----------------------------------------------------------------
2353 // maybe_drop_icon_on_slot()
2354 //
2355 void maybe_drop_icon_on_slot(int bank_num)
2356 {
2357         int dropped=0;
2358         if ( !mouse_down(MOUSE_LEFT_BUTTON) ) {
2359                 if ( wl_icon_being_carried() )
2360                         dropped = drop_icon_on_slot(bank_num);
2361
2362                 if ( dropped ) {
2363                         wl_reset_carried_icon();
2364                 }
2365         }
2366 }               
2367
2368 // ----------------------------------------------------------------
2369 // wl_check_for_stopped_ship_anims()
2370 //
2371 void wl_check_for_stopped_ship_anims()
2372 {
2373         int i;
2374         anim_instance *ai;
2375         for ( i = 0; i < MAX_SHIP_TYPES; i++ ) {
2376                 ai = Wl_ships[i].anim_instance;
2377                 if ( ai != NULL ) {
2378                         if ( !anim_playing(ai) ) {
2379                                 Wl_ships[i].anim_instance = NULL;
2380                         }
2381                 }
2382         }
2383 }
2384
2385 // ---------------------------------------------------------------------------------
2386 // do_mouse_over_list_weapon()
2387 //
2388 void do_mouse_over_list_weapon(int index)
2389 {
2390         Hot_weapon_icon = index;
2391
2392         int region_index = ICON_PRIMARY_0+index;
2393         if ( index > 3 ) {
2394                 region_index = ICON_SECONDARY_0 + (index - 4);
2395         }
2396         
2397         if ( Wl_mouse_down_on_region != region_index ){
2398                 return;
2399         }
2400
2401         if ( mouse_down(MOUSE_LEFT_BUTTON) )
2402                 wl_pick_icon_from_list(index);
2403 }
2404
2405 // ---------------------------------------------------------------------------------
2406 // do_mouse_over_ship_weapon()
2407 //
2408 //
2409 // input: index -> bank index on ship (0..6)
2410 //
2411 // returns: 
2412 //          0 -> icon was not dropped on a slot
2413 //                              1 -> icon was dropped on a slot
2414 int do_mouse_over_ship_weapon(int index)
2415 {
2416         int dropped_on_slot, is_moved, mx, my;
2417
2418         dropped_on_slot = 0;
2419         SDL_assert(Selected_wl_slot >= 0);
2420
2421         if ( ss_disabled_slot( Selected_wl_slot ) )
2422                 return 0;
2423
2424         Hot_weapon_bank_icon = index;   // bank icon will be drawn highlighted
2425
2426         if ( mouse_down(MOUSE_LEFT_BUTTON) ) {
2427                 if ( Wl_mouse_down_on_region == (ICON_SHIP_PRIMARY_0+index) ){
2428                         pick_from_ship_slot(index);
2429                 }
2430         } else {
2431                 int was_carried = wl_icon_being_carried();
2432                 maybe_drop_icon_on_slot(index);
2433                 if ( was_carried && !wl_icon_being_carried() ) {
2434                         mouse_get_pos( &mx, &my );
2435                         if ( Carried_wl_icon.from_x != mx || Carried_wl_icon.from_y != my) {
2436                                 dropped_on_slot = 1;
2437                         }
2438                 }
2439         }
2440
2441         // set Hot_weapon_bank if a droppable icon is being held over a slot that
2442         // can accept that icon
2443         is_moved = 0;
2444         mouse_get_pos( &mx, &my );
2445         if ( Carried_wl_icon.from_x != mx || Carried_wl_icon.from_y != my) {
2446                 is_moved = 1;
2447         }
2448
2449         if ( wl_icon_being_carried() && is_moved ) {
2450                 if ( Weapon_info[Carried_wl_icon.weapon_class].subtype != WP_MISSILE ) {
2451                         if ( (index < 3) && (Wss_slots[Selected_wl_slot].wep_count[index] >= 0) ) 
2452                                 Hot_weapon_bank = index;
2453                 } else {
2454                         if ( index >= 3 && ( Wss_slots[Selected_wl_slot].wep_count[index] >= 0) ) 
2455                                 Hot_weapon_bank = index;
2456                 }
2457         }
2458
2459         return dropped_on_slot;
2460 }
2461
2462
2463 // maybe flash a button if player hasn't done anything for a while
2464 void wl_maybe_flash_button()
2465 {
2466         if ( common_flash_bright() ) {
2467                 // commit button
2468                 if ( Common_buttons[Current_screen-1][gr_screen.res][3].button.button_hilighted() ) {
2469                         common_flash_button_init();
2470                 } else {
2471                         Common_buttons[Current_screen-1][gr_screen.res][3].button.draw_forced(1);
2472                 }
2473         }
2474 }
2475
2476
2477 void weapon_select_render(float frametime)
2478 {
2479         if ( !Background_playing ) {
2480                 gr_set_bitmap(Weapon_select_background_bitmap, GR_ALPHABLEND_NONE, GR_BITBLT_MODE_NORMAL, 1.0f, -1, -1);
2481                 gr_bitmap(0, 0);
2482         }
2483
2484         anim_render_all(0, frametime);
2485         anim_render_all(ON_SHIP_SELECT, frametime);
2486 }
2487
2488 // draw the weapon description text
2489 // this wipes in
2490 void wl_render_weapon_desc(float frametime)
2491 {
2492 #ifndef MAKE_FS1
2493         int *weapon_desc_coords;
2494         int *weapon_title_coords;
2495
2496         // retrieve the correct set of text coordinates
2497         if (Game_mode & GM_MULTIPLAYER) {
2498                 weapon_desc_coords = Wl_new_weapon_desc_coords_multi[gr_screen.res];
2499                 weapon_title_coords = Wl_new_weapon_title_coords_multi[gr_screen.res];
2500         } else {
2501                 weapon_desc_coords = Wl_new_weapon_desc_coords[gr_screen.res];
2502                 weapon_title_coords = Wl_new_weapon_title_coords[gr_screen.res];
2503         }
2504
2505         // render the normal version of the weapom desc
2506         char bright_char[WEAPON_DESC_MAX_LINES];                        // one bright char per line
2507         if (!Weapon_desc_wipe_done) {
2508                 // draw mid-wipe version
2509                 // decide which char is last (and bright)
2510                 int bright_char_index = (int)(Weapon_desc_wipe_time_elapsed * WEAPON_DESC_MAX_LENGTH / WEAPON_DESC_WIPE_TIME);
2511                 int i, w, h, curr_len;
2512                 
2513                 // draw weapon title (above weapon anim)
2514                 for (i=0; i<2; i++) {
2515                         curr_len = strlen(Weapon_desc_lines[i]);
2516
2517                         if (bright_char_index < curr_len) {
2518                                 // save bright char and plunk in some nulls to shorten string
2519                                 bright_char[i] = Weapon_desc_lines[i][bright_char_index];
2520                                 Weapon_desc_lines[i][bright_char_index] = '\0';
2521
2522                                 // draw the strings
2523                                 gr_set_color_fast(&Color_white);                        
2524                                 gr_string(weapon_title_coords[0], weapon_title_coords[1]+(10*i), Weapon_desc_lines[i]);
2525
2526                                 // draw the bright letters
2527                                 gr_set_color_fast(&Color_bright_white);
2528                                 gr_get_string_size(&w, &h, Weapon_desc_lines[i], curr_len);
2529                                 gr_printf(weapon_title_coords[0]+w, weapon_title_coords[1]+(10*i), "%c", bright_char[i]);
2530
2531                                 // restore the bright char to the string
2532                                 Weapon_desc_lines[i][bright_char_index] = bright_char[i];
2533
2534                         } else {
2535                                 // draw the string
2536                                 gr_set_color_fast(&Color_white);
2537                                 gr_string(weapon_title_coords[0], weapon_title_coords[1]+(10*i), Weapon_desc_lines[i]);
2538                         }
2539                 }
2540
2541                 // draw weapon desc (below weapon anim)
2542                 for (i=2; i<WEAPON_DESC_MAX_LINES; i++) {
2543                         curr_len = strlen(Weapon_desc_lines[i]);
2544
2545                         if (bright_char_index < curr_len) {
2546                                 // save bright char and plunk in some nulls to shorten string
2547                                 bright_char[i] = Weapon_desc_lines[i][bright_char_index];
2548                                 Weapon_desc_lines[i][bright_char_index] = '\0';
2549
2550                                 // draw the string
2551                                 gr_set_color_fast(&Color_white);
2552                                 gr_string(weapon_desc_coords[0], weapon_desc_coords[1]+(10*(i-2)), Weapon_desc_lines[i]);
2553
2554                                 // draw the bright letters
2555                                 gr_set_color_fast(&Color_bright_white);
2556                                 gr_get_string_size(&w, &h, Weapon_desc_lines[i], curr_len);
2557                                 gr_printf(weapon_desc_coords[0]+w, weapon_desc_coords[1]+(10*(i-2)), "%c", bright_char[i]);
2558
2559                                 // restore the bright char to the string
2560                                 Weapon_desc_lines[i][bright_char_index] = bright_char[i];
2561
2562                         } else {
2563                                 // draw the string
2564                                 gr_set_color_fast(&Color_white);
2565                                 gr_string(weapon_desc_coords[0], weapon_desc_coords[1]+(10*(i-2)), Weapon_desc_lines[i]);
2566                         }
2567                 }
2568
2569                 // update time
2570                 Weapon_desc_wipe_time_elapsed += frametime;
2571                 if (Weapon_desc_wipe_time_elapsed >= WEAPON_DESC_WIPE_TIME) {
2572                         // wipe is done,set flag and stop sound
2573                         Weapon_desc_wipe_done = 1;
2574                 }
2575
2576         } else {
2577
2578                 // draw full version
2579                 // FIXME - change to use a for loop 
2580                 gr_set_color_fast(&Color_white);
2581                 gr_string(weapon_title_coords[0], weapon_title_coords[1], Weapon_desc_lines[0]);
2582                 gr_string(weapon_title_coords[0], weapon_title_coords[1] + 10, Weapon_desc_lines[1]);
2583                 gr_string(weapon_desc_coords[0], weapon_desc_coords[1], Weapon_desc_lines[2]);
2584                 gr_string(weapon_desc_coords[0], weapon_desc_coords[1] + 10, Weapon_desc_lines[3]);
2585                 gr_string(weapon_desc_coords[0], weapon_desc_coords[1] + 20, Weapon_desc_lines[4]);
2586                 gr_string(weapon_desc_coords[0], weapon_desc_coords[1] + 30, Weapon_desc_lines[5]);
2587         }
2588 #endif
2589 }
2590
2591
2592
2593 // re-inits wiping vars and causes the current text to wipe in again
2594 void wl_weapon_desc_start_wipe()
2595 {
2596 #ifndef MAKE_FS1
2597         int currchar_src = 0, currline_dest = 2, currchar_dest = 0, i;
2598         int w, h;
2599         int title_len = strlen(Weapon_info[Selected_wl_class].title);
2600
2601         // init wipe vars
2602         Weapon_desc_wipe_time_elapsed = 0.0f;
2603         Weapon_desc_wipe_done = 0;
2604
2605         // break title into two lines if too long
2606         strcpy(Weapon_desc_lines[0], Weapon_info[Selected_wl_class].title);
2607         gr_get_string_size(&w, &h, Weapon_info[Selected_wl_class].title, title_len);
2608         if (w > Weapon_title_max_width[gr_screen.res]) {
2609                 // split
2610                 currchar_src = (int)(((float)title_len / (float)w) * Weapon_title_max_width[gr_screen.res]);                    // char to start space search at
2611                 while (Weapon_desc_lines[0][currchar_src] != ' ') {
2612                         currchar_src--;
2613                         if (currchar_src <= 0) {
2614                                 currchar_src = title_len;
2615                                 break;
2616                         }
2617                 }
2618
2619                 Weapon_desc_lines[0][currchar_src] = '\0';                                                                              // shorten line 0
2620                 strcpy(Weapon_desc_lines[1], &(Weapon_desc_lines[0][currchar_src+1]));          // copy remainder into line 1
2621         } else {
2622                 // entire title in line 0, thus line 1 is empty
2623                 Weapon_desc_lines[1][0] = '\0';
2624         }
2625         
2626         // break current description into lines (break at the /n's)
2627         currchar_src = 0;
2628         while (Weapon_info[Selected_wl_class].desc[currchar_src] != '\0') {
2629                 if (Weapon_info[Selected_wl_class].desc[currchar_src] == '\n') {
2630                         // break here
2631                         if (currchar_src != 0) {                                        // protect against leading /n's
2632                                 Weapon_desc_lines[currline_dest][currchar_dest] = '\0';
2633                                 currline_dest++;
2634                                 currchar_dest = 0;
2635                         }
2636                 } else {
2637                         // straight copy
2638                         Weapon_desc_lines[currline_dest][currchar_dest] = Weapon_info[Selected_wl_class].desc[currchar_src];
2639                         currchar_dest++;
2640                 }
2641
2642                 currchar_src++;
2643
2644                 SDL_assert(currline_dest < WEAPON_DESC_MAX_LINES);
2645                 SDL_assert(currchar_dest < WEAPON_DESC_MAX_LENGTH);
2646         }
2647
2648         // wrap up the line processing
2649         Weapon_desc_lines[currline_dest][currchar_dest] = '\0';
2650         for (i=currline_dest+1; i<WEAPON_DESC_MAX_LINES; i++) {
2651                 Weapon_desc_lines[i][0] = '\0';
2652         }
2653 #endif
2654 }
2655
2656
2657
2658 // ---------------------------------------------------------------------------------
2659 // weapon_select_do() is called once per frame while in the weapon loadout screen.  
2660 //
2661 // Calls to common_ functions are made for those functions which are common to the
2662 // ship select and briefing screens.
2663 //
2664 void weapon_select_do(float frametime)
2665 {
2666         int k, wl_choice, snazzy_action;
2667         //char buf[256];
2668
2669         if ( !Weapon_select_open )
2670                 weapon_select_init();
2671
2672         wl_choice = snazzy_menu_do(WeaponSelectMaskData, Weaponselect_mask_w, Weaponselect_mask_h, Num_weapon_select_regions, Weapon_select_region, &snazzy_action, 0);
2673
2674         if ( wl_choice >= 0 ) {
2675                 if ( snazzy_action == SNAZZY_CLICKED ) {
2676                         nprintf(("Alan","got one\n"));
2677                 } 
2678         }
2679
2680         Hot_wl_slot = -1;
2681         Hot_weapon_icon = -1;
2682         Hot_weapon_bank = -1;
2683         Hot_weapon_bank_icon = -1;
2684
2685         k = common_select_do(frametime);
2686         
2687         if ( help_overlay_active(WL_OVERLAY) ) {
2688                 if ( Weapon_anim_class >= 0 && Wl_icons[Weapon_anim_class].anim_instance ) {
2689                         anim_pause(Wl_icons[Weapon_anim_class].anim_instance);
2690                 }
2691         }
2692         else {
2693                 if ( Weapon_anim_class >= 0 && Wl_icons[Weapon_anim_class].anim_instance ) {
2694                         anim_unpause(Wl_icons[Weapon_anim_class].anim_instance);
2695                 }
2696         }
2697
2698         // Check common keypresses
2699         common_check_keys(k);
2700
2701         if ( Mouse_down_last_frame ) {
2702                 Wl_mouse_down_on_region = wl_choice;
2703         }
2704
2705         if ( wl_choice > -1 ) {
2706                 switch(wl_choice) {
2707                         case ICON_PRIMARY_0:
2708                                 do_mouse_over_list_weapon(0);
2709                                 break;
2710                         case ICON_PRIMARY_1:
2711                                 do_mouse_over_list_weapon(1);
2712                                 break;
2713                         case ICON_PRIMARY_2:
2714                                 do_mouse_over_list_weapon(2);
2715                                 break;
2716                         case ICON_PRIMARY_3:
2717                                 do_mouse_over_list_weapon(3);
2718                                 break;
2719                         case ICON_SECONDARY_0:
2720                                 do_mouse_over_list_weapon(4);
2721                                 break;
2722                         case ICON_SECONDARY_1:
2723                                 do_mouse_over_list_weapon(5);
2724                                 break;
2725                         case ICON_SECONDARY_2:
2726                                 do_mouse_over_list_weapon(6);
2727                                 break;
2728                         case ICON_SECONDARY_3:
2729                                 do_mouse_over_list_weapon(7);
2730                                 break;
2731                         case ICON_SHIP_PRIMARY_0:
2732                                 if ( do_mouse_over_ship_weapon(0) )
2733                                         wl_choice = -1;
2734                                 break;
2735                         case ICON_SHIP_PRIMARY_1:
2736                                 if ( do_mouse_over_ship_weapon(1) )
2737                                         wl_choice = -1;
2738                                 break;
2739                         case ICON_SHIP_PRIMARY_2:
2740                                 if ( do_mouse_over_ship_weapon(2) )
2741                                         wl_choice = -1;
2742                                 break;
2743                         case ICON_SHIP_SECONDARY_0:
2744                                 if ( do_mouse_over_ship_weapon(3) )
2745                                         wl_choice = -1;
2746                                 break;
2747                         case ICON_SHIP_SECONDARY_1:
2748                                 if ( do_mouse_over_ship_weapon(4) )
2749                                         wl_choice = -1;
2750                                 break;
2751                         case ICON_SHIP_SECONDARY_2:
2752                                 if ( do_mouse_over_ship_weapon(5) )
2753                                         wl_choice = -1;
2754                                 break;
2755                         case ICON_SHIP_SECONDARY_3:
2756                                 if ( do_mouse_over_ship_weapon(6) )
2757                                         wl_choice = -1;
2758                                 break;
2759                         case WING_0_SHIP_0:
2760                                 Hot_wl_slot = 0;
2761                                 break;
2762                         case WING_0_SHIP_1:
2763                                 Hot_wl_slot = 1;
2764                                 break;
2765                         case WING_0_SHIP_2:
2766                                 Hot_wl_slot = 2;
2767                                 break;
2768                         case WING_0_SHIP_3:
2769                                 Hot_wl_slot = 3;
2770                                 break;
2771                         case WING_1_SHIP_0:
2772                                 Hot_wl_slot = 4;
2773                                 break;
2774                         case WING_1_SHIP_1:
2775                                 Hot_wl_slot = 5;
2776                                 break;
2777                         case WING_1_SHIP_2:
2778                                 Hot_wl_slot = 6;
2779                                 break;
2780                         case WING_1_SHIP_3:
2781                                 Hot_wl_slot = 7;
2782                                 break;
2783                         case WING_2_SHIP_0:
2784                                 Hot_wl_slot = 8;
2785                                 break;
2786                         case WING_2_SHIP_1:
2787                                 Hot_wl_slot = 9;
2788                                 break;
2789                         case WING_2_SHIP_2:
2790                                 Hot_wl_slot = 10;
2791                                 break;
2792                         case WING_2_SHIP_3:
2793                                 Hot_wl_slot = 11;
2794                                 break;
2795
2796                         default:
2797                                 break;
2798                 }       // end switch
2799         }
2800
2801         if ( !mouse_down(MOUSE_LEFT_BUTTON) )  {
2802                 wl_dump_carried_icon();
2803         }
2804
2805         // Check for a mouse click on buttons
2806         common_check_buttons();
2807         weapon_check_buttons();
2808
2809         // Check for the mouse clicks over a region
2810         if ( wl_choice > -1 && snazzy_action == SNAZZY_CLICKED ) {
2811                 switch (wl_choice) {
2812                                 case ICON_PRIMARY_0:
2813                                         maybe_select_new_weapon(0);
2814                                         break;
2815                                 case ICON_PRIMARY_1:
2816                                         maybe_select_new_weapon(1);
2817                                         break;
2818                                 case ICON_PRIMARY_2:
2819                                         maybe_select_new_weapon(2);
2820                                         break;
2821                                 case ICON_PRIMARY_3:
2822                                         maybe_select_new_weapon(3);
2823                                         break;
2824                                 case ICON_SECONDARY_0:
2825                                         maybe_select_new_weapon(4);
2826                                         break;
2827                                 case ICON_SECONDARY_1:
2828                                         maybe_select_new_weapon(5);
2829                                         break;
2830                                 case ICON_SECONDARY_2:
2831                                         maybe_select_new_weapon(6);
2832                                         break;
2833                                 case ICON_SECONDARY_3:
2834                                         maybe_select_new_weapon(7);
2835                                         break;
2836                                 case ICON_SHIP_PRIMARY_0:
2837                                         maybe_select_new_ship_weapon(0);
2838                                         break;
2839                                 case ICON_SHIP_PRIMARY_1:
2840                                         maybe_select_new_ship_weapon(1);
2841                                         break;
2842                                 case ICON_SHIP_PRIMARY_2:
2843                                         maybe_select_new_ship_weapon(2);
2844                                         break;
2845                                 case ICON_SHIP_SECONDARY_0:
2846                                         maybe_select_new_ship_weapon(3);
2847                                         break;
2848                                 case ICON_SHIP_SECONDARY_1:
2849                                         maybe_select_new_ship_weapon(4);
2850                                         break;
2851                                 case ICON_SHIP_SECONDARY_2:
2852                                         maybe_select_new_ship_weapon(5);
2853                                         break;
2854                                 case ICON_SHIP_SECONDARY_3:
2855                                         maybe_select_new_ship_weapon(6);
2856                                         break;
2857                                 case WING_0_SHIP_0:
2858                                         maybe_select_wl_slot(0,0);
2859                                         break;
2860                                 case WING_0_SHIP_1:
2861                                         maybe_select_wl_slot(0,1);
2862                                         break;
2863                                 case WING_0_SHIP_2:
2864                                         maybe_select_wl_slot(0,2);
2865                                         break;
2866                                 case WING_0_SHIP_3:
2867                                         maybe_select_wl_slot(0,3);
2868                                         break;
2869                                 case WING_1_SHIP_0:
2870                                         maybe_select_wl_slot(1,0);
2871                                         break;
2872                                 case WING_1_SHIP_1:
2873                                         maybe_select_wl_slot(1,1);
2874                                         break;
2875                                 case WING_1_SHIP_2:
2876                                         maybe_select_wl_slot(1,2);
2877                                         break;
2878                                 case WING_1_SHIP_3:
2879                                         maybe_select_wl_slot(1,3);
2880                                         break;
2881                                 case WING_2_SHIP_0:
2882                                         maybe_select_wl_slot(2,0);
2883                                         break;
2884                                 case WING_2_SHIP_1:
2885                                         maybe_select_wl_slot(2,1);
2886                                         break;
2887                                 case WING_2_SHIP_2:
2888                                         maybe_select_wl_slot(2,2);
2889                                         break;
2890                                 case WING_2_SHIP_3:
2891                                         maybe_select_wl_slot(2,3);
2892                                         break;
2893
2894                                 default:
2895                                         break;
2896
2897                         }       // end switch
2898                 }
2899
2900         gr_reset_clip();
2901
2902         if ( Weapon_anim_class != -1 && ( Selected_wl_class == Weapon_anim_class ) ) {
2903                 wl_icon_info *icon;
2904                 SDL_assert(Selected_wl_class >= 0 && Selected_wl_class < MAX_WEAPON_TYPES );
2905                 if ( Weapon_anim_class != Selected_wl_class ) 
2906                         start_weapon_animation(Selected_wl_class);      
2907
2908                 SDL_assert(Weapon_anim_class == Selected_wl_class);
2909                 icon = &Wl_icons[Selected_wl_class];
2910                 if ( icon->anim_instance ) {
2911                         if ( icon->anim_instance->frame_num == icon->anim_instance->stop_at ) {
2912                                 anim_play_struct aps;
2913                                 int *weapon_ani_coords;
2914
2915                                 // get the correct weapon animations coords
2916                                 if (Game_mode & GM_MULTIPLAYER) {
2917                                         weapon_ani_coords = Wl_weapon_ani_coords_multi[gr_screen.res];
2918                                 } else {
2919                                         weapon_ani_coords = Wl_weapon_ani_coords[gr_screen.res];
2920                                 }
2921
2922                                 anim_release_render_instance(icon->anim_instance);
2923                                 anim_play_init(&aps, icon->anim, weapon_ani_coords[0], weapon_ani_coords[1]);
2924                                 aps.start_at = WEAPON_ANIM_LOOP_FRAME-1;
2925                                 aps.screen_id = ON_WEAPON_SELECT;
2926                                 aps.framerate_independent = 1;
2927                                 aps.skip_frames = 0;
2928                                 icon->anim_instance = anim_play(&aps);
2929                         }
2930                 }
2931         }
2932
2933         weapon_select_render(frametime);
2934         if ( !Background_playing ) {            
2935                 Weapon_ui_window.draw();
2936                 wl_redraw_pressed_buttons();
2937                 draw_wl_icons();
2938                 anim_render_all(ON_WEAPON_SELECT, frametime);
2939                 wl_check_for_stopped_ship_anims();
2940                 wl_render_overhead_view(frametime);
2941                 wl_draw_ship_weapons(Selected_wl_slot);
2942                 for ( int i = 0; i < MAX_WING_BLOCKS; i++ ) {
2943                         draw_wing_block(i, Hot_wl_slot, Selected_wl_slot, -1);
2944                 }
2945                 common_render_selected_screen_button();
2946         }
2947
2948         // maybe blit the multiplayer "locked" button
2949         if((Game_mode & GM_MULTIPLAYER) && multi_ts_is_locked()){
2950                 Buttons[gr_screen.res][WL_BUTTON_MULTI_LOCK].button.draw_forced(2);
2951         }
2952
2953
2954         if ( wl_icon_being_carried() ) {
2955                 int mx, my, sx, sy;
2956                 SDL_assert(Carried_wl_icon.weapon_class < MAX_WEAPON_TYPES);
2957                 mouse_get_pos( &mx, &my );
2958                 sx = mx + Wl_delta_x;
2959                 sy = my + Wl_delta_y;
2960
2961
2962                 if ( Wl_icons[Carried_wl_icon.weapon_class].can_use > 0) {
2963                         gr_set_color_fast(&Color_blue);
2964                         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);
2965                         gr_bitmap(sx, sy);
2966                 }
2967
2968                 // draw number to prevent it from disappearing on clicks
2969                 if ( Carried_wl_icon.from_bank >= MAX_WL_PRIMARY ) {
2970                         if ( mx == Carried_wl_icon.from_x && my == Carried_wl_icon.from_y ) {
2971                                 int num_missiles = Wss_slots[Carried_wl_icon.from_slot].wep_count[Carried_wl_icon.from_bank];
2972                                 //sprintf(buf, "%d", num_missiles);
2973                                 //gr_set_color_fast(&Color_white);
2974
2975                                 //int x_offset = wl_fury_missile_offset_hack(Carried_wl_icon.weapon_class, num_missiles);
2976                                 //gr_string(sx-19-x_offset, sy+8, buf);
2977
2978
2979                                 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]);
2980                         }
2981                 }
2982
2983                 // check so see if this is really a legal weapon to carry away
2984                 if ( !Wl_icons[Carried_wl_icon.weapon_class].can_use ) {
2985                         int diffx, diffy;
2986                         diffx = abs(Carried_wl_icon.from_x-mx);
2987                         diffy = abs(Carried_wl_icon.from_y-my);
2988                         if ( (diffx > 2) || (diffy > 2) ) {
2989                                 int ship_class = Wss_slots[Selected_wl_slot].ship_class;
2990                                 wl_pause_anim();
2991                                 if (Lcl_gr) {
2992                                         // might have to get weapon name translation
2993                                         char display_name[128];
2994                                         strncpy(display_name, Weapon_info[Carried_wl_icon.weapon_class].name, 128);
2995                                         lcl_translate_wep_name(display_name);
2996                                         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);
2997                                 } else {
2998                                         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);
2999                                 }
3000                                 wl_unpause_anim();
3001                                 wl_dump_carried_icon();
3002                         }
3003                 }
3004         }
3005
3006         if ( Weapon_anim_class != Selected_wl_class ) {
3007                 start_weapon_animation(Selected_wl_class);
3008         }
3009
3010         // render weapon description text
3011         wl_render_weapon_desc(frametime);
3012
3013         wl_maybe_flash_button();
3014
3015         // should render the chatbox as close to the end as possible so it overlaps all controls
3016         if(!Background_playing){
3017
3018                 // render some extra stuff in multiplayer
3019                 if(Game_mode & GM_MULTIPLAYER){                         
3020                         // render the chatbox
3021                         chatbox_render();
3022
3023                         // draw tooltips
3024                         Weapon_ui_window.draw_tooltip();
3025
3026                         // render the status indicator for the voice system
3027                         multi_common_voice_display_status();
3028
3029                         // blit the "ships/players" locked button
3030                         // multi_ts_blit_locked_button();
3031                 }
3032         }
3033
3034         // blit help overlay if active
3035         help_overlay_maybe_blit(WL_OVERLAY);
3036         gr_flip();      
3037
3038         // If the commit button was pressed, do the commit button actions.  Done at the end of the
3039         // loop so there isn't a skip in the animation (since ship_create() can take a long time if
3040         // the ship model is not in memory
3041         if ( Commit_pressed ) {
3042                 if(Game_mode & GM_MULTIPLAYER){
3043                         multi_ts_commit_pressed();                      
3044                 } else {
3045                         commit_pressed();
3046                 }               
3047                 Commit_pressed = 0;
3048         }
3049 }
3050
3051 // -------------------------------------------------------------------------------
3052 // weapon_select_close() will free the bitmap slot and memory that was allocated
3053 // to store the mask bitmap.
3054 //
3055 // Weapon_select_open is cleared when this function completes.
3056 //
3057 void weapon_select_close()
3058 {
3059         if ( !Weapon_select_open ) {
3060                 nprintf(("Alan","weapon_select_close() returning without doing anything\n"));
3061                 return;
3062         }
3063
3064         nprintf(("Alan", "Entering weapon_select_close()\n"));
3065
3066         stop_weapon_animation();
3067
3068         // done with the bitmaps, so unlock it
3069         bm_unlock(WeaponSelectMaskBitmap);
3070
3071         Weapon_ui_window.destroy();
3072
3073 #ifdef MAKE_FS1
3074         common_free_interface_palette();
3075 #endif
3076
3077         // unload bitmaps
3078         help_overlay_unload(WL_OVERLAY);
3079
3080         bm_unload(WeaponSelectMaskBitmap);
3081
3082         wl_unload_icons();
3083         wl_unload_all_anim_instances();
3084         wl_unload_all_anims();
3085
3086         Selected_wl_class = -1;
3087         Weapon_select_open = 0;
3088 }
3089
3090
3091 // ------------------------------------------------------------------------
3092 //      wl_render_icon_count()
3093 //              renders the number next to the weapon icon
3094 //
3095 // input:       x,y                     =>              x,y screen position OF THE ICON (NOT where u want the text, 
3096 //                                                                              this is calculated to prevent overlapping)
3097 //                              num                     =>              the actual count to be printed
3098 //
3099 void wl_render_icon_count(int num, int x, int y)
3100 {
3101         char buf[32];
3102         int num_w, num_h;
3103         int number_to_draw = (num > 1000) ? 999 : num;          // cap count @ 999
3104         SDL_assert(number_to_draw >= 0);
3105
3106         sprintf(buf, "%d", number_to_draw);
3107         gr_get_string_size(&num_w, &num_h, buf, strlen(buf));
3108
3109         // render
3110         gr_set_color_fast(&Color_white);
3111 #ifdef MAKE_FS1
3112         // strict left align
3113         gr_string(x-21, y+8, buf);
3114 #else
3115         gr_string(x-num_w-4, y+8, buf);
3116 #endif
3117 }
3118
3119
3120 // ------------------------------------------------------------------------
3121 //      wl_render_icon()
3122 //
3123 // input:       index                   =>              index into Wl_icons[], identifying which weapon to draw
3124 //                              x,y                     =>              x,y screen position to draw icon at
3125 //                              num                     =>              count for weapon
3126 //                              draw_num_flag =>        0 if not to draw count for weapon, nonzero otherwise
3127 //                              hot_mask                =>              value that should match Hot_weapon_icon to show mouse is over
3128 //                              hot_bank_mask =>        value that should match Hot_weapon_bank_icon to show mouse is over
3129 //                              select_mask     =>              value that should match Selected_wl_class to show icon is selected
3130 //
3131 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)
3132 {
3133         int                             bitmap_id;
3134         wl_icon_info    *icon;
3135
3136         if ( Selected_wl_slot == -1 )
3137                 return;
3138
3139         icon = &Wl_icons[index];
3140
3141         if ( icon->icon_bmaps[0] == -1 ) {
3142                 wl_load_icons(index);
3143         }
3144                 
3145         // assume default bitmap is to be used
3146         bitmap_id = icon->icon_bmaps[WEAPON_ICON_FRAME_NORMAL]; // normal frame
3147
3148         if ( bitmap_id < 0 ) {
3149                 Int3();
3150                 return;
3151         }
3152
3153         // next check if ship has mouse over it
3154         if ( !wl_icon_being_carried() ) {
3155                 if ( Hot_weapon_icon > -1 && Hot_weapon_icon == hot_mask )
3156                         bitmap_id = icon->icon_bmaps[WEAPON_ICON_FRAME_HOT];
3157
3158                 if ( Hot_weapon_bank_icon > -1 && Hot_weapon_bank_icon == hot_bank_mask )
3159                         bitmap_id = icon->icon_bmaps[WEAPON_ICON_FRAME_HOT];
3160         }
3161
3162         // if icon is selected
3163         if ( Selected_wl_class > -1 ) {
3164                 if ( Selected_wl_class == select_mask)
3165                         bitmap_id = icon->icon_bmaps[WEAPON_ICON_FRAME_SELECTED];       // selected icon
3166         }
3167
3168         // if icon is disabled
3169         if ( !icon->can_use ) {
3170                 bitmap_id = icon->icon_bmaps[WEAPON_ICON_FRAME_DISABLED];       // disabled icon
3171         }
3172
3173         gr_set_color_fast(&Color_blue);
3174         gr_set_bitmap(bitmap_id, GR_ALPHABLEND_NONE, GR_BITBLT_MODE_NORMAL, 1.0f, -1, -1);
3175         gr_bitmap(x, y);
3176
3177         // draw the number of the item
3178         // now, right justified
3179         if ( draw_num_flag != 0 ) {             
3180 #ifdef MAKE_FS1
3181                 // add a little extra text padding for secondary weapons
3182                 if ( hot_mask > 3 ) {
3183                         wl_render_icon_count(num, x-9, y);
3184                 } else {
3185                         wl_render_icon_count(num, x, y);
3186                 }
3187 #else
3188                 wl_render_icon_count(num, x, y);
3189 #endif
3190         }
3191 }
3192
3193 // ------------------------------------------------------------------------
3194 //      wl_draw_ship_weapons()
3195 //
3196 // Draw the icons for the weapons that are currently on the selected ship
3197 //
3198 // input:       slot_num                =>              Slot to draw weapons for
3199 //
3200 void wl_draw_ship_weapons(int index)
3201 {
3202         int             i;
3203         int             *wep, *wep_count;
3204
3205         if ( index == -1 )
3206                 return;
3207
3208         SDL_assert(index >= 0 && index < MAX_WSS_SLOTS);
3209         wep = Wss_slots[index].wep;
3210         wep_count = Wss_slots[index].wep_count;
3211
3212         for ( i = 0; i < MAX_WL_WEAPONS; i++ ) {
3213
3214                 if ( Carried_wl_icon.from_bank == i && Carried_wl_icon.from_slot == index ) {
3215                         continue;
3216                 }
3217
3218                 if ( (wep[i] != -1) && (wep_count[i] > 0) ) {
3219                         int x_offset = wl_fury_missile_offset_hack(wep[i], wep_count[i]);
3220                         x_offset =0;
3221                         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]);
3222                 }
3223         }
3224 }
3225
3226 // ------------------------------------------------------------------------
3227 //      draw_wl_icon_with_number()
3228 //
3229 // input:       list_count                      =>              list position on screen (0-7)
3230 //                              weapon_class            =>              class of weapon
3231 //
3232 void draw_wl_icon_with_number(int list_count, int weapon_class)
3233 {
3234         SDL_assert( list_count >= 0 && list_count < 8 );        
3235
3236         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],
3237                                            Wl_pool[weapon_class], 1, list_count, -1, weapon_class);
3238 }
3239
3240 // ------------------------------------------------------------------------
3241 //      draw_wl_icons()
3242 //
3243 // Draw the weapon icons that are available
3244 void draw_wl_icons()
3245 {
3246         int i, count;
3247
3248         count=0;
3249         for ( i = Plist_start; i < Plist_size; i++ ) {
3250                 draw_wl_icon_with_number(count, Plist[i]);
3251                 if ( ++count > 3 )
3252                         break;
3253         }
3254
3255         count=0;
3256         for ( i = Slist_start; i < Slist_size; i++ ) {
3257                 draw_wl_icon_with_number(count+4, Slist[i]);
3258                 if ( ++count > 3 )
3259                         break;
3260         }
3261 }
3262
3263 // ------------------------------------------------------------------------
3264 // wl_pick_icon_from_list()
3265 //
3266 // determine if an icon from the scrollable weapon list can be picked up 
3267 // (for drag and drop).  It calculates the difference in x & y between the icon
3268 // and the mouse, so we can move the icon with the mouse in a realistic way
3269 // input: index (0..7) 
3270 void wl_pick_icon_from_list(int index)
3271 {
3272         int weapon_class, mx, my;
3273
3274         // if this is a multiplayer game and the player is an observer, he can never pick any weapons up
3275         if((Game_mode & GM_MULTIPLAYER) && (Net_player->flags & NETINFO_FLAG_OBSERVER)){
3276                 return;
3277         }
3278
3279         // if a weapon is being carried, do nothing
3280         if ( wl_icon_being_carried() ) {
3281                 return;
3282         }
3283
3284         if ( index < 4 ) {
3285                 weapon_class = Plist[Plist_start+index];
3286         } else {
3287                 weapon_class = Slist[Slist_start+index-4];
3288         }
3289
3290         // there isn't a weapon there at all!
3291         if ( weapon_class < 0 ) 
3292                 return;
3293
3294         // no weapons left of that class
3295         if ( Wl_pool[weapon_class] <= 0 ) {
3296                 return;
3297         }
3298
3299 /*
3300         // some are available, but weapon cannot be used on current ship class
3301         if ( !Wl_icons[weapon_class].can_use ) {
3302                 wl_pause_anim();
3303
3304                 int ship_class = Wss_slots[Selected_wl_slot].ship_class;
3305                 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);
3306
3307                 wl_unpause_anim();
3308                 return;
3309         }
3310 */
3311
3312         wl_set_carried_icon(-1, -1, weapon_class);
3313         common_flash_button_init();
3314
3315         mouse_get_pos( &mx, &my );
3316         Wl_delta_x = Wl_weapon_icon_coords[gr_screen.res][index][0] - mx;
3317         Wl_delta_y = Wl_weapon_icon_coords[gr_screen.res][index][1] - my;
3318 }
3319
3320 // ------------------------------------------------------------------------
3321 //      pick_from_ship_slot()
3322 //
3323 // input: num   ->      index into shipb banks (0..2 primary, 3..6 secondary)
3324 void pick_from_ship_slot(int num)
3325 {
3326         int mx, my, *wep, *wep_count;
3327                 
3328         SDL_assert(num < 7);
3329
3330         if ( Selected_wl_slot == -1 )
3331                 return;
3332
3333         if ( wl_icon_being_carried() )
3334                 return;
3335
3336         if ( ss_disabled_slot( Selected_wl_slot ) )
3337                 return;
3338
3339         wep = Wss_slots[Selected_wl_slot].wep;
3340         wep_count = Wss_slots[Selected_wl_slot].wep_count;
3341
3342         // check if a weapon even exists in that slot
3343         if ( (wep[num] < 0) || (wep_count[num] <= 0) ) {
3344                 return;
3345         }
3346
3347         SDL_assert(Wl_icons[wep[num]].can_use);
3348
3349         wl_set_carried_icon(num, Selected_wl_slot, wep[num]);
3350         common_flash_button_init();
3351                         
3352         mouse_get_pos( &mx, &my );
3353
3354         int x_offset = wl_fury_missile_offset_hack(wep[num], wep_count[num]);
3355         Wl_delta_x = Wl_bank_coords[gr_screen.res][num][0] - mx + x_offset;
3356         Wl_delta_y = Wl_bank_coords[gr_screen.res][num][1] - my;
3357
3358         Carried_wl_icon.from_x = mx;
3359         Carried_wl_icon.from_y = my;
3360 }
3361
3362 // determine if this slot has no weapons
3363 int wl_slots_all_empty(wss_unit *slot)
3364 {
3365         int                     i;
3366
3367         for ( i = 0; i < MAX_WL_WEAPONS; i++ ) {
3368                 if ( (slot->wep_count[i] > 0) && (slot->wep[i] >= 0) ) 
3369                         return 0;
3370         }
3371
3372         return 1;
3373 }
3374
3375 // ------------------------------------------------------------------------
3376 //      wl_update_ship_weapons()
3377 //
3378 // Change a ship's weapons based on the information contained in the
3379 // Weapon_data[] structure that is filled in during weapon loadout
3380 //
3381 // returns: -1  =>      if the playre ship has no weapons
3382 //                              0       =>      function finished without errors        
3383 int wl_update_ship_weapons(int objnum, wss_unit *slot )
3384 {
3385         // AL 11-15-97: Ensure that the player ship hasn't removed all 
3386         //                                       weapons from their ship.  This will cause a warning to appear.
3387         if ( objnum == OBJ_INDEX(Player_obj) && Weapon_select_open ) {
3388                 if ( wl_slots_all_empty(slot) ) {
3389                         return -1;
3390                 }
3391         }
3392
3393         wl_bash_ship_weapons(&Ships[Objects[objnum].instance].weapons, slot);
3394         return 0;
3395 }
3396
3397
3398 // ------------------------------------------------------------------------
3399 //      wl_update_parse_object_weapons()
3400 //
3401 // Set the Pilot subsystem of a parse_object to the weapons that are setup
3402 // for the wing_block,wing_slot ship
3403 //
3404 // input:       pobjp   =>      pointer to parse object that references Pilot subsystem
3405 //
3406 void wl_update_parse_object_weapons(p_object *pobjp, wss_unit *slot)
3407 {
3408         int                             i,      j, sidx, pilot_index, max_count;
3409         subsys_status   *ss;
3410
3411         SDL_assert(slot->ship_class >= 0);
3412
3413         pilot_index = wl_get_pilot_subsys_index(pobjp);
3414
3415         if ( pilot_index == -1 ) 
3416                 return;
3417                                                 
3418         ss = &Subsys_status[pilot_index];
3419
3420         for ( i = 0; i < MAX_PRIMARY_BANKS; i++ ) {
3421                 ss->primary_banks[i] = -1;              
3422         }
3423
3424         for ( i = 0; i < MAX_SECONDARY_BANKS; i++ ) {
3425                 ss->secondary_banks[i] = -1;            
3426         }
3427
3428         j = 0;
3429         for ( i = 0; i < MAX_WL_PRIMARY; i++ ) {
3430                 if ( (slot->wep_count[i] > 0) && (slot->wep[i] >= 0) ) {
3431                         ss->primary_banks[j] = slot->wep[i];
3432                         j++;
3433                 }
3434         }
3435
3436         j = 0;
3437         for ( i = 0; i < MAX_WL_SECONDARY; i++ ) {
3438                 sidx = i+MAX_WL_PRIMARY;
3439                 if ( (slot->wep_count[sidx] > 0) && (slot->wep[sidx] >= 0) ) {
3440                         ss->secondary_banks[j] = slot->wep[sidx];
3441                         // Important: the secondary_ammo[] value is a percentage of max capacity!
3442                         max_count = wl_calc_missile_fit(slot->wep[sidx], Ship_info[slot->ship_class].secondary_bank_ammo_capacity[j]);
3443                         ss->secondary_ammo[j] = fl2i( i2fl(slot->wep_count[sidx]) / max_count * 100.0f + 0.5f);
3444                         j++;
3445                 }
3446         }
3447 }
3448
3449 // ------------------------------------------------------------------------
3450 // stop_weapon_animation()
3451 //
3452 // Stop the current weapon animation from playing.
3453 //
3454 void stop_weapon_animation()
3455 {
3456         if ( Weapon_anim_class < 0 )
3457                 return;
3458
3459         if ( anim_playing(Wl_icons[Weapon_anim_class].anim_instance) )
3460                 anim_release_render_instance(Wl_icons[Weapon_anim_class].anim_instance);
3461
3462         Wl_icons[Weapon_anim_class].anim_instance = NULL;
3463         Weapon_anim_class = -1;
3464 }
3465
3466 // ------------------------------------------------------------------------
3467 // start_weapon_animation()
3468 //
3469 // Start the current weapon animation from playing.
3470 //
3471 void start_weapon_animation(int weapon_class) 
3472 {
3473         wl_icon_info    *icon;
3474         int *weapon_ani_coords;
3475
3476         if ( weapon_class < 0 )
3477                 return;
3478
3479         if ( weapon_class == Weapon_anim_class ) 
3480                 return;
3481
3482         // get the correct weapon animations coords
3483         if (Game_mode & GM_MULTIPLAYER) {
3484                 weapon_ani_coords = Wl_weapon_ani_coords_multi[gr_screen.res];
3485         } else {
3486                 weapon_ani_coords = Wl_weapon_ani_coords[gr_screen.res];
3487         }
3488
3489         icon = &Wl_icons[weapon_class];
3490
3491         // stop current animation playing
3492         stop_weapon_animation();
3493
3494         // see if we need to load in the animation from disk
3495         if ( icon->anim == NULL ) {
3496                 wl_load_anim(weapon_class);
3497                 /*
3498                 icon->anim = anim_load(Weapon_info[weapon_class].anim_filename, 1);
3499                 if ( icon->anim == NULL ) {
3500                         Int3(); // could not open the weapon animation
3501                         return;
3502                 }
3503                 */
3504         }
3505
3506         // see if we need to get an instance
3507         if ( icon->anim_instance == NULL ) {
3508                 anim_play_struct aps;
3509
3510                 anim_play_init(&aps, icon->anim, weapon_ani_coords[0], weapon_ani_coords[1]);
3511                 aps.screen_id = ON_WEAPON_SELECT;
3512                 aps.framerate_independent = 1;
3513                 aps.skip_frames = 0;
3514                 icon->anim_instance = anim_play(&aps);
3515                 gamesnd_play_iface(SND_WEAPON_ANIM_START);
3516         }
3517
3518         Weapon_anim_class = weapon_class;
3519
3520         // start the text wipe
3521         wl_weapon_desc_start_wipe();
3522 }
3523
3524 // reset the weapons loadout to the defaults in the mission
3525 void wl_reset_to_defaults()
3526 {
3527         // don't reset of weapons pool in multiplayer
3528         if(Game_mode & GM_MULTIPLAYER){
3529                 return;
3530         }
3531
3532         wl_init_pool(&Team_data[Common_team]);
3533         wl_init_icon_lists();
3534         wl_fill_slots();
3535         wl_reset_selected_slot();
3536         wl_reset_carried_icon();
3537         wl_maybe_reset_selected_weapon_class();
3538 }
3539
3540 // Bash ship weapons, based on what is stored in the stored weapons loadout
3541 // NOTE: Wss_slots[] is assumed to be correctly set
3542 void wl_bash_ship_weapons(ship_weapon *swp, wss_unit *slot)
3543 {
3544         int             i, j, sidx;
3545         
3546         for ( i = 0; i < MAX_PRIMARY_BANKS; i++ ) {
3547                 swp->primary_bank_weapons[i] = -1;              
3548         }
3549
3550         for ( i = 0; i < MAX_SECONDARY_BANKS; i++ ) {
3551                 swp->secondary_bank_weapons[i] = -1;            
3552         }
3553
3554         j = 0;
3555         for ( i = 0; i < MAX_WL_PRIMARY; i++ ) {
3556                 if ( (slot->wep_count[i] > 0) && (slot->wep[i] >= 0) ) {
3557                         swp->primary_bank_weapons[j] = slot->wep[i];                    
3558                         j++;
3559                 }
3560         }
3561         swp->num_primary_banks = j;
3562
3563         j = 0;
3564         for ( i = 0; i < MAX_WL_SECONDARY; i++ ) {
3565                 sidx = i+MAX_WL_PRIMARY;
3566                 if ( (slot->wep_count[sidx] > 0) && (slot->wep[sidx] >= 0) ) {
3567                         swp->secondary_bank_weapons[j] = slot->wep[sidx];
3568                         swp->secondary_bank_ammo[j] = slot->wep_count[sidx];
3569                         j++;
3570                 }
3571         }
3572         swp->num_secondary_banks = j;
3573 }
3574
3575 // utility function for swapping two banks
3576 void wl_swap_weapons(int ship_slot, int from_bank, int to_bank)
3577 {
3578         wss_unit        *slot;
3579         int     tmp;
3580         slot = &Wss_slots[ship_slot];
3581
3582         if ( from_bank == to_bank ) {
3583                 return;
3584         }
3585
3586         // swap weapon type
3587         tmp = slot->wep[from_bank];
3588         slot->wep[from_bank] = slot->wep[to_bank];
3589         slot->wep[to_bank] = tmp;
3590
3591         // swap weapon count
3592         tmp = slot->wep_count[from_bank];
3593         slot->wep_count[from_bank] = slot->wep_count[to_bank];
3594         slot->wep_count[to_bank] = tmp;
3595 }
3596
3597 // utility function used to put back overflow into the weapons pool
3598 void wl_saturate_bank(int ship_slot, int bank)
3599 {
3600         wss_unit        *slot;
3601         int             max_count, overflow;
3602
3603         slot = &Wss_slots[ship_slot];
3604
3605         if ( (slot->wep[bank] < 0) || (slot->wep_count <= 0) ) {
3606                 return;
3607         }
3608
3609         max_count = wl_calc_missile_fit(slot->wep[bank], Ship_info[slot->ship_class].secondary_bank_ammo_capacity[bank-3]);
3610         overflow = slot->wep_count[bank] - max_count;
3611         if ( overflow > 0 ) {
3612                 slot->wep_count[bank] -= overflow;
3613                 // add overflow back to pool
3614                 Wl_pool[slot->wep[bank]] += overflow;
3615         }
3616 }
3617
3618 // exit: 0 -> no data changed
3619 //                      1 -> data changed
3620 //       sound => gets filled with sound id to play
3621 int wl_swap_slot_slot(int from_bank, int to_bank, int ship_slot, int *sound)
3622 {
3623         wss_unit        *slot;
3624         slot = &Wss_slots[ship_slot];
3625
3626         if ( slot->ship_class == -1 ) {
3627                 Int3(); // should not be possible
3628                 return 0;
3629         }
3630         
3631         // do nothing if swapping with self
3632         if ( from_bank == to_bank ) {
3633                 *sound=SND_ICON_DROP_ON_WING;
3634                 return 0;       // no update
3635         }
3636
3637         // ensure that source bank exists and has something to pick from
3638         if ( slot->wep[from_bank] == -1 || slot->wep_count[from_bank] <= 0 ) {
3639                 return 0;
3640         }
3641
3642         // ensure that the dest bank exists
3643         if ( slot->wep_count[to_bank] < 0 ) {
3644                 return 0;
3645         }
3646
3647         // ensure that the banks are both of the same class
3648         if ( (IS_BANK_PRIMARY(from_bank) && IS_BANK_SECONDARY(to_bank)) || (IS_BANK_SECONDARY(from_bank) && IS_BANK_PRIMARY(to_bank)) ) {
3649                 // put from_bank back into list
3650                 Wl_pool[slot->wep[from_bank]] += slot->wep_count[from_bank];;           // return to list
3651                 slot->wep[from_bank] = -1;                                                                                                              // remove from slot
3652                 slot->wep_count[from_bank] = 0;
3653                 *sound=SND_ICON_DROP;
3654                 return 1;
3655         }
3656
3657         // case 1: primaries (easy)
3658         if ( IS_BANK_PRIMARY(from_bank) && IS_BANK_PRIMARY(to_bank) ) {
3659                 wl_swap_weapons(ship_slot, from_bank, to_bank);
3660                 *sound=SND_ICON_DROP_ON_WING;
3661                 return 1;
3662         }
3663
3664         // case 2: secondaries (harder)
3665         if ( IS_BANK_SECONDARY(from_bank) && IS_BANK_SECONDARY(to_bank) ) {
3666
3667                 // case 2a: secondaries are the same type
3668                 if ( slot->wep[from_bank] == slot->wep[to_bank] ) {
3669                         int dest_max, dest_can_fit, source_can_give;
3670                         dest_max = wl_calc_missile_fit(slot->wep[to_bank], Ship_info[slot->ship_class].secondary_bank_ammo_capacity[to_bank-3]);
3671
3672                         dest_can_fit = dest_max - slot->wep_count[to_bank];
3673
3674                         if ( dest_can_fit <= 0 ) {
3675                                 // dest bank is already full.. nothing to do here
3676                                 return 0;
3677                         }
3678
3679                         // see how much source can give
3680                         source_can_give = min(dest_can_fit, slot->wep_count[from_bank]);
3681
3682                         if ( source_can_give > 0 ) {                    
3683                                 slot->wep_count[to_bank] += source_can_give;            // add to dest
3684                                 slot->wep_count[from_bank] -= source_can_give;  // take from source
3685                                 *sound=SND_ICON_DROP_ON_WING;
3686                                 return 1;
3687                         } else {
3688                                 return 0;
3689                         }
3690                 }
3691
3692                 // case 2b: secondaries are different types
3693                 if ( slot->wep[from_bank] != slot->wep[to_bank] ) {
3694
3695                         // swap 'em 
3696                         wl_swap_weapons(ship_slot, from_bank, to_bank);
3697
3698                         // put back some on list if required
3699                         wl_saturate_bank(ship_slot, from_bank);
3700                         wl_saturate_bank(ship_slot, to_bank);
3701                         *sound=SND_ICON_DROP_ON_WING;
3702                         return 1;
3703                 }
3704         }
3705
3706         Int3();         // should never get here
3707         return 0;
3708 }
3709
3710 // exit: 0 -> no data changed
3711 //                      1 -> data changed
3712 //       sound => gets filled with sound id to play
3713 int wl_dump_to_list(int from_bank, int to_list, int ship_slot, int *sound)
3714 {
3715         wss_unit        *slot;
3716         slot = &Wss_slots[ship_slot];
3717
3718         // ensure that source bank exists and has something to pick from
3719         if ( slot->wep[from_bank] == -1 || slot->wep_count[from_bank] <= 0 ) {
3720                 return 0;
3721         }
3722
3723         // put weapon bank to the list
3724         Wl_pool[to_list] += slot->wep_count[from_bank];                 // return to list
3725         slot->wep[from_bank] = -1;                                                                              // remove from slot
3726         slot->wep_count[from_bank] = 0;
3727         *sound=SND_ICON_DROP;
3728
3729         return 1;
3730 }
3731
3732 // exit: 0 -> no data changed
3733 //                      1 -> data changed
3734 //       sound => gets filled with sound id to play
3735 int wl_grab_from_list(int from_list, int to_bank, int ship_slot, int *sound)
3736 {
3737         wss_unit        *slot;
3738         slot = &Wss_slots[ship_slot];
3739         int max_fit;
3740
3741         // ensure that the banks are both of the same class
3742         if ( (IS_LIST_PRIMARY(from_list) && IS_BANK_SECONDARY(to_bank)) || (IS_LIST_SECONDARY(from_list) && IS_BANK_PRIMARY(to_bank)) ) {
3743                 // do nothing
3744                 *sound=SND_ICON_DROP;
3745                 return 0;
3746         }
3747
3748         // ensure that dest bank exists
3749         if ( slot->wep_count[to_bank] < 0 ) {
3750                 *sound=SND_ICON_DROP;
3751                 return 0;
3752         }
3753
3754         // bank should be empty:
3755         SDL_assert(slot->wep_count[to_bank] == 0);
3756         SDL_assert(slot->wep[to_bank] < 0);
3757
3758         // ensure that pool has weapon
3759         if ( Wl_pool[from_list] <= 0 ) {
3760                 return 0;
3761         }
3762
3763         // find how much dest bank can fit
3764         if ( to_bank < MAX_WL_PRIMARY ) {
3765                 max_fit = 1;
3766         } else {
3767                 max_fit = wl_calc_missile_fit(from_list, Ship_info[slot->ship_class].secondary_bank_ammo_capacity[to_bank-MAX_WL_PRIMARY]);
3768         }
3769
3770         // take weapon from list
3771         if ( Wl_pool[from_list] < max_fit ) {
3772                 max_fit = Wl_pool[from_list];
3773         }
3774         Wl_pool[from_list] -= max_fit;
3775
3776         // put on the slot
3777         slot->wep[to_bank] = from_list;
3778         slot->wep_count[to_bank] = max_fit;
3779
3780         *sound=SND_ICON_DROP_ON_WING;
3781         return 1;
3782 }
3783
3784 // exit: 0 -> no data changed
3785 //                      1 -> data changed
3786 //       sound => gets filled with sound id to play
3787 int wl_swap_list_slot(int from_list, int to_bank, int ship_slot, int *sound)
3788 {
3789         wss_unit        *slot;
3790         slot = &Wss_slots[ship_slot];
3791         int max_fit;
3792
3793         // ensure that the banks are both of the same class
3794         if ( (IS_LIST_PRIMARY(from_list) && IS_BANK_SECONDARY(to_bank)) || (IS_LIST_SECONDARY(from_list) && IS_BANK_PRIMARY(to_bank)) ) {
3795                 // do nothing
3796                 *sound=SND_ICON_DROP;
3797                 return 0;
3798         }
3799
3800         // ensure that dest bank exists
3801         if ( slot->wep_count[to_bank] < 0 ) {
3802                 return 0;
3803         }
3804
3805         // bank should have something in it
3806         SDL_assert(slot->wep_count[to_bank] > 0);
3807         SDL_assert(slot->wep[to_bank] >= 0);
3808
3809         // ensure that pool has weapon
3810         if ( Wl_pool[from_list] <= 0 ) {
3811                 return 0;
3812         }
3813
3814         // dump slot weapon back into list
3815         Wl_pool[slot->wep[to_bank]] += slot->wep_count[to_bank];
3816         slot->wep_count[to_bank] = 0;
3817         slot->wep[to_bank] = -1;
3818
3819         // put weapon on ship from list
3820         
3821         // find how much dest bank can fit
3822         if ( to_bank < MAX_WL_PRIMARY ) {
3823                 max_fit = 1;
3824         } else {
3825                 max_fit = wl_calc_missile_fit(from_list, Ship_info[slot->ship_class].secondary_bank_ammo_capacity[to_bank-MAX_WL_PRIMARY]);
3826         }
3827
3828         // take weapon from list
3829         if ( Wl_pool[from_list] < max_fit ) {
3830                 max_fit = Wl_pool[from_list];
3831         }
3832         Wl_pool[from_list] -= max_fit;
3833
3834         // put on the slot
3835         slot->wep[to_bank] = from_list;
3836         slot->wep_count[to_bank] = max_fit;
3837
3838         *sound=SND_ICON_DROP_ON_WING;
3839         return 1;
3840 }
3841
3842 // update any interface data that may be dependent on Wss_slots[] 
3843 void wl_synch_interface()
3844 {
3845 }
3846
3847 void wl_apply(int mode,int from_bank,int from_list,int to_bank,int to_list,int ship_slot,int player_index)
3848 {
3849         int update=0;
3850         int sound=-1;
3851         net_player *pl;
3852
3853         // get the appropriate net player
3854         if(Game_mode & GM_MULTIPLAYER){
3855                 if(player_index == -1){
3856                         pl = Net_player;
3857                 } else {
3858                         pl = &Net_players[player_index];
3859                 }
3860         } else {
3861                 pl = NULL;
3862         }
3863
3864         switch(mode){
3865         case WSS_SWAP_SLOT_SLOT:
3866                 update = wl_swap_slot_slot(from_bank, to_bank, ship_slot, &sound);
3867                 break;
3868         case WSS_DUMP_TO_LIST:
3869                 update = wl_dump_to_list(from_bank, to_list, ship_slot, &sound);
3870                 break;
3871         case WSS_GRAB_FROM_LIST:
3872                 update = wl_grab_from_list(from_list, to_bank, ship_slot, &sound);
3873                 break;
3874         case WSS_SWAP_LIST_SLOT:
3875                 update = wl_swap_list_slot(from_list, to_bank, ship_slot, &sound);
3876                 break;
3877         }
3878
3879         // only play this sound if the move was done locally (by the host in other words)
3880         if ( (sound >= 0) && (player_index == -1) ) {   
3881                 gamesnd_play_iface(sound);      
3882         }       
3883
3884         if ( update ) {
3885                 if ( MULTIPLAYER_HOST ) {
3886                         int size;
3887                         ubyte wss_data[MAX_PACKET_SIZE-20];
3888
3889                         size = store_wss_data(wss_data, MAX_PACKET_SIZE-20,sound,player_index);                 
3890                         SDL_assert(pl != NULL);
3891                         send_wss_update_packet(pl->p_info.team,wss_data, size);
3892                 }
3893
3894                 if(Game_mode & GM_MULTIPLAYER){
3895                         SDL_assert(pl != NULL);
3896
3897                         // if the pool we're using has changed, synch stuff up
3898                         if(pl->p_info.team == Net_player->p_info.team){
3899                                 wl_synch_interface();                   
3900                         }
3901                 } else {
3902                         wl_synch_interface();
3903                 }
3904         }               
3905 }
3906
3907 void wl_drop(int from_bank,int from_list,int to_bank,int to_list, int ship_slot, int player_index)
3908 {
3909         int mode;
3910         net_player *pl;
3911
3912         // get the appropriate net player
3913         if(Game_mode & GM_MULTIPLAYER){
3914                 if(player_index == -1){
3915                         pl = Net_player;
3916                 } else {
3917                         pl = &Net_players[player_index];
3918                 }
3919         } else {
3920                 pl = NULL;
3921         }
3922
3923         common_flash_button_init();
3924         if ( !(Game_mode & GM_MULTIPLAYER) || MULTIPLAYER_HOST ) {
3925                 if((Game_mode & GM_MULTIPLAYER) && (Netgame.type_flags & NG_TYPE_TEAM)){
3926                         // set the global pointers to the right pools
3927                         ss_set_team_pointers(pl->p_info.team);
3928                 }
3929
3930
3931                 mode = wss_get_mode(from_bank, from_list, to_bank, to_list, ship_slot);
3932                 if ( mode >= 0 ) {
3933                         wl_apply(mode, from_bank, from_list, to_bank, to_list, ship_slot, player_index);
3934                 }               
3935
3936                 if((Game_mode & GM_MULTIPLAYER) && (Netgame.type_flags & NG_TYPE_TEAM)){
3937                         // set the global pointers to the right pools
3938                         ss_set_team_pointers(Net_player->p_info.team);
3939                 }
3940         } else {
3941                 send_wss_request_packet(Net_player->player_id, from_bank, from_list, to_bank, to_list, ship_slot, -1, WSS_WEAPON_SELECT);
3942         }
3943 }
3944