]> icculus.org git repositories - taylor/freespace2.git/blob - src/network/multi_ingame.cpp
added copyright header
[taylor/freespace2.git] / src / network / multi_ingame.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/Network/multi_ingame.cpp $
11  * $Revision$
12  * $Date$
13  * $Author$
14  *
15  * $Log$
16  * Revision 1.3  2002/06/09 04:41:23  relnev
17  * added copyright header
18  *
19  * Revision 1.2  2002/06/02 04:26:34  relnev
20  * warning cleanup
21  *
22  * Revision 1.1.1.1  2002/05/03 03:28:10  root
23  * Initial import.
24  *
25  * 
26  * 20    8/03/99 11:02p Dave
27  * Maybe fixed sync problems in multiplayer.
28  * 
29  * 19    7/30/99 7:01p Dave
30  * Dogfight escort gauge. Fixed up laser rendering in Glide.
31  * 
32  * 18    7/28/99 5:33p Dave
33  * Nailed the missing stats bug to the wall. Problem was optimized build
34  * and using GET_DATA() with array elements. BLECH.
35  * 
36  * 17    7/26/99 6:07p Dave
37  * Removed some warnings.
38  * 
39  * 16    7/26/99 5:50p Dave
40  * Revised ingame join. Better? We'll see....
41  * 
42  * 15    7/24/99 5:48p Jefff
43  * converted to new UI stuff -- added 1024 support
44  * 
45  * 14    7/08/99 10:53a Dave
46  * New multiplayer interpolation scheme. Not 100% done yet, but still
47  * better than the old way.
48  * 
49  * 13    4/21/99 6:15p Dave
50  * Did some serious housecleaning in the beam code. Made it ready to go
51  * for anti-fighter "pulse" weapons. Fixed collision pair creation. Added
52  * a handy macro for recalculating collision pairs for a given object.
53  * 
54  * 12    3/10/99 6:50p Dave
55  * Changed the way we buffer packets for all clients. Optimized turret
56  * fired packets. Did some weapon firing optimizations.
57  * 
58  * 11    3/09/99 6:24p Dave
59  * More work on object update revamping. Identified several sources of
60  * unnecessary bandwidth.
61  * 
62  * 10    3/08/99 7:03p Dave
63  * First run of new object update system. Looks very promising.
64  * 
65  * 9     3/01/99 7:39p Dave
66  * Added prioritizing ship respawns. Also fixed respawns in TvT so teams
67  * don't mix respawn points.
68  * 
69  * 8     1/30/99 5:08p Dave
70  * More new hi-res stuff.Support for nice D3D textures.
71  * 
72  * 7     1/27/99 9:56a Dave
73  * Temporary checkin of beam weapons for Dan to make cool sounds.
74  * 
75  * 6     11/19/98 8:03a Dave
76  * Full support for D3-style reliable sockets. Revamped packet lag/loss
77  * system, made it receiver side and at the lowest possible level.
78  * 
79  * 5     11/17/98 11:12a Dave
80  * Removed player identification by address. Now assign explicit id #'s.
81  * 
82  * 4     11/05/98 5:55p Dave
83  * Big pass at reducing #includes
84  * 
85  * 3     10/13/98 9:29a Dave
86  * Started neatening up freespace.h. Many variables renamed and
87  * reorganized. Added AlphaColors.[h,cpp]
88  * 
89  * 2     10/07/98 10:53a Dave
90  * Initial checkin.
91  * 
92  * 1     10/07/98 10:50a Dave
93  * 
94  * 83    9/18/98 4:54p Dave
95  * Fixed ingame ship select icon problem.
96  * 
97  * 82    9/18/98 4:23p Dave
98  * Reversed the logic on a bogus assert.
99  * 
100  * 81    9/18/98 3:13p Allender
101  * actually delete ships from wings when ingame joining.  Shoudl get over
102  * problem were too many ships are presend in the mission since we don't
103  * delete unused ships until after ingame join is completed
104  * 
105  * 80    9/15/98 1:59p Dave
106  * Fixed bonehead mistake calling multi_endgame_ending()
107  * 
108  * 79    9/11/98 4:14p Dave
109  * Fixed file checksumming of < file_size. Put in more verbose kicking and
110  * PXO stats store reporting.
111  * 
112  * 78    9/11/98 2:10p Allender
113  * base temporary parse_object's ai_goals to -1 so that we don't try and
114  * free sepxressions that shouldn't be freed
115  * 
116  * 77    8/07/98 10:16a Allender
117  * use obj_set_flags for the COULD_BE_PLAYER flag
118  * 
119  * 76    7/24/98 9:27a Dave
120  * Tidied up endgame sequencing by removing several old flags and
121  * standardizing _all_ endgame stuff with a single function call.
122  * 
123  * 75    6/30/98 2:17p Dave
124  * Revised object update system. Removed updates for all weapons. Put
125  * button info back into control info packet.
126  * 
127  * 74    6/13/98 6:01p Hoffoss
128  * Externalized all new (or forgot to be added) strings to all the code.
129  * 
130  * 73    6/13/98 3:18p Hoffoss
131  * NOX()ed out a bunch of strings that shouldn't be translated.
132  * 
133  * 72    6/10/98 2:56p Dave
134  * Substantial changes to reduce bandwidth and latency problems.
135  * 
136  * 71    6/04/98 11:46a Dave
137  * Drastically reduce size/rate of client control info update packets. Put
138  * in rate limiting for object updating from server.
139  * 
140  * 70    5/26/98 11:54a Allender
141  * fix multiplayer problems and sexpression crash
142  * 
143  * 69    5/25/98 1:33a Allender
144  * fixed timestamp problem for client update packets, small fix for ingame
145  * validation code
146  * 
147  * 68    5/24/98 9:17p Allender
148  * commented out test code to help rectify bogus player objects when
149  * ingame joining
150  * 
151  * 67    5/23/98 3:16p Allender
152  * work on object update packet optimizations (a new updating system).
153  * Don't allow medals/promotions/badges when playing singple player
154  * missions through the simulator
155  * 
156  * 66    5/22/98 9:35p Dave
157  * Put in channel based support for PXO. Put in "shutdown" button for
158  * standalone. UI tweaks for TvT
159  * 
160  * 65    5/21/98 10:03p Allender
161  * add secondary ammo counts to ingame join packets
162  * 
163  * 64    5/21/98 1:52a Dave
164  * Remove obsolete command line functions. Reduce shield explosion packets
165  * drastically. Tweak PXO screen even more. Fix file xfer system so that
166  * we can guarantee file uniqueness.
167  * 
168  * 63    5/21/98 12:14a Allender
169  * fix ingame join problems
170  * 
171  * 62    5/20/98 3:25p Allender
172  * ingame join changes (which probably won't make the final version).
173  * Added RAS code into psnet
174  * 
175  * 61    5/19/98 11:36p Allender
176  * fixed very nasty mask problem with ingame joiner marking player objects
177  * incorrectly.  Named ingame joiner ship and observer ship unique names
178  * 
179  * 60    5/19/98 8:35p Dave
180  * Revamp PXO channel listing system. Send campaign goals/events to
181  * clients for evaluation. Made lock button pressable on all screens. 
182  * 
183  * 59    5/18/98 12:41a Allender
184  * fixed subsystem problems on clients (i.e. not reporting properly on
185  * damage indicator).  Fixed ingame join problem with respawns.  minor
186  * comm menu stuff
187  * 
188  * 58    5/15/98 4:12p Allender
189  * removed redbook code.  Put back in ingame join timer.  Major fixups for
190  * stats in multiplayer.  Pass correct score, medals, etc when leaving
191  * game.  Be sure clients display medals, badges, etc.
192  * 
193  * 57    5/15/98 1:44p Allender
194  * initialize hotkeys when entering the mission ingame joining
195  * 
196  * 56    5/11/98 4:33p Allender
197  * fixed ingame join problems -- started to work on new object updating
198  * code (currently ifdef'ed out)
199  * 
200  * 55    5/09/98 4:31p Chad
201  * Fixed weapon recharge rate problem and fixed weapon link status problem
202  * for ingame joiners.
203  * 
204  * 54    5/08/98 11:21a Allender
205  * fix ingame join trouble.  Small messaging fix.  Enable collisions for
206  * friendlies again
207  * 
208  * 53    5/03/98 7:04p Dave
209  * Make team vs. team work mores smoothly with standalone. Change how host
210  * interacts with standalone for picking missions. Put in a time limit for
211  * ingame join ship select. Fix ingame join ship select screen for Vasudan
212  * ship icons.
213  * 
214  * 52    5/03/98 2:52p Dave
215  * Removed multiplayer furball mode.
216  * 
217  * 51    5/02/98 1:47a Dave
218  * Make sure ingame joiners know how many respawns they have left. Tidy up
219  * some multiui stuff.
220  * 
221  * 50    5/01/98 4:11p Comet
222  * Fixed ship_ets bug. I think.
223  * 
224  * 49    4/30/98 12:49a Allender
225  * change ship type and weapons of any player wing ship, not just ones
226  * that players currently occupy
227  * 
228  * 48    4/29/98 9:36p Allender
229  * ingame join tweaks.  added network message for countermeasures
230  * 
231  * 47    4/29/98 12:28p Chad
232  * set packet size when selecting a player's ingame choice
233  * 
234  * 46    4/25/98 2:02p Dave
235  * Put in multiplayer context help screens. Reworked ingame join ship
236  * select screen. Fixed places where network timestamps get hosed.
237  * 
238  * 45    4/23/98 11:52p Allender
239  * make homing weapons send their homing object.  Fixed ingame joiners so
240  * they bash ship types and weapons correctly when joining
241  * 
242  * 44    4/23/98 1:49a Allender
243  * major rearm/repair fixes for multiplayer.  Fixed respawning of AI ships
244  * to not respawn until 5 seconds after they die.  Send escort information
245  * to ingame joiners
246  * 
247  * 43    4/22/98 5:53p Dave
248  * Large reworking of endgame sequencing. Updated multi host options
249  * screen for new artwork. Put in checks for host or team captains leaving
250  * midgame.
251  * 
252  * 42    4/22/98 4:59p Allender
253  * new multiplayer dead popup.  big changes to the comm menu system for
254  * team vs. team.  Start of debriefing stuff for team vs. team  Make form
255  * on my wing work with individual ships who have high priority orders
256  * 
257  * 41    4/21/98 11:56p Dave
258  * Put in player deaths statskeeping. Use arrow keys in the ingame join
259  * ship select screen. Don't quit the game if in the debriefing and server
260  * leaves.
261  * 
262  * 40    4/20/98 12:40a Allender
263  * fixed nasty problem where network read code was not reentrant.  minor
264  * UI tweaks.  ingame joiners now get netgame info correctly.
265  * 
266  * 39    4/07/98 5:42p Dave
267  * Put in support for ui display of voice system status (recording,
268  * playing back, etc). Make sure main hall music is stopped before
269  * entering a multiplayer game via ingame join.
270  * 
271  * 38    4/06/98 6:47p Allender
272  * be sure that ingame joiner has reasonable current primary and secondary
273  * banks
274  * 
275  * 37    4/06/98 12:33a Allender
276  * ingame joiners need to unmark all player ships as could be players
277  * before getting ship information from host.  Don't send count with
278  * countermeasure fired packet
279  * 
280  * 36    4/04/98 4:22p Dave
281  * First rev of UDP reliable sockets is done. Seems to work well if not
282  * overly burdened.
283  * 
284  * 35    4/04/98 3:55p Allender
285  * made ingame join send packet to all other clients in the game when in
286  * game joiner selects his ship
287  * 
288  * 34    4/03/98 1:03a Dave
289  * First pass at unreliable guaranteed delivery packets.
290  * 
291  * 33    4/02/98 5:36p John
292  * Made the level paging occur at the same time as the level is read in.
293  * 
294  * 32    3/31/98 4:03p Allender
295  * some ingame join fixed -- make sure ingame joiners have parse objects
296  * for all other players
297  * 
298  * 31    3/24/98 5:12p Allender
299  * ingame join packet sequencing
300  *
301  * $NoKeywords: $
302  */
303
304 #include <limits.h>
305
306 #include "ai.h"
307 #include "object.h"
308 #include "ship.h"
309 #include "multi.h"
310 #include "multiutil.h"
311 #include "multimsgs.h"
312 #include "multiui.h"
313 #include "missionparse.h"
314 #include "freespace.h"
315 #include "gamesequence.h"
316 #include "2d.h"
317 #include "ui.h"
318 #include "key.h"
319 #include "gamesnd.h"
320 #include "linklist.h"
321 #include "multi_ingame.h"
322 #include "missionscreencommon.h"
323 #include "popup.h"
324 #include "bmpman.h"
325 #include "mouse.h"
326 #include "multi_observer.h"
327 #include "multi_xfer.h"
328 #include "multi_kick.h"
329 #include "missiongoals.h"
330 #include "mainhallmenu.h"
331 #include "stats.h"
332 #include "multiteamselect.h"
333 #include "missionweaponchoice.h"
334 #include "multi_endgame.h"
335 #include "hudescort.h"
336 #include "hudshield.h"
337 #include "objcollide.h"
338 #include "missionhotkey.h"
339 #include "multi_campaign.h"
340 #include "multi_obj.h"
341 #include "alphacolors.h"
342 #include "timer.h"
343
344 // --------------------------------------------------------------------------------------------------
345 // DAVE's BIGASS INGAME JOIN WARNING/DISCLAIMER
346 //
347 // Ingame joining is another delicate system. Although not as delicate as server transfer, it will
348 // help to take as many precautions as possible when handling ingame joins. Please be sure to follow
349 // all the same rules as explained in multi_strans.h
350 //
351 // --------------------------------------------------------------------------------------------------
352
353 // --------------------------------------------------------------------------------------------------
354 // INGAME JOIN DESCRIPTION
355 //
356 // 1.) Joiner sends a JOIN packet to the server
357 // 2.) If the server accepts him, he receives an ACCEPT packet in return
358 // 3.) The client then moves into the INGAME_SYNC state to begin receiving data from the server
359 // 4.) The first thing he does on this screen is send his filesig packet to the server. At which 
360 //     point the server will either let him in or deny him. There are no file transfers ingame.
361 // 5.) The server calls multi_handle_ingame_joiners() once per frame, through multi_do_frame()
362 // 6.) After verifiying or kicking the player because of his file signature, the server tells the
363 //     player to load the mission
364 // 7.) When the mission is loaded, the server, sends a netgame update to the client
365 // 8.) Without waiting, the server then begins sending data ship packets to the player
366 // 9.) Upon confirmation of receiving these packets, the server sends wing data packets
367 // 10.) Upon completion of this, the server sends respawn point packets
368 // 11.) Upon completion of this, the server sends a post briefing data block packet containing ship class and 
369 //      weapon information
370 // 12.) After this, the server sends a player settings packet (to all players for good measure)
371 // 13.) At this point, the server sends a jump into mission packet
372 // 14.) Upon receipt of this packet, the client moves into the ingame ship select state
373 // 15.) The first thing the client does in this state is load the mission data (textures, etc)
374 // 16.) The player is presented with a list of ships he can choose from. He selects one and sends
375 //      an INGAME_SHIP_REQUEST to the server. 
376 // 17.) The server checks to see if this request is acceptable and sends an INGAME_SHIP_REQUEST back
377 //      with the appropriate data.
378 // 18.) If the client received an affirmative, he selects the ship and jumps into the mission, otherwise
379 //      he removes it from the list and tries for another ship
380 // --------------------------------------------------------------------------------------------------
381
382
383 LOCAL   int     Ingame_ships_deleted = 0;
384 LOCAL   int     Ingame_ships_to_delete[MAX_SHIPS];
385
386
387 // --------------------------------------------------------------------------------------------------
388 // INGAME JOIN FORWARD DECLARATIONS
389 //
390
391 void multi_ingame_send_ship_update(net_player *p);
392
393 void multi_ingame_join_check_buttons();
394 void multi_ingame_join_button_pressed(int n);
395
396
397
398 // --------------------------------------------------------------------------------------------------
399 // INGAME JOIN COMMON DEFINITIONS
400 //
401
402
403 // --------------------------------------------------------------------------------------------------
404 // INGAME JOIN SERVER FUNCTIONS
405 //
406
407 // called on the server to process ingame joiners and move them through the motions of ingame joining
408 void multi_handle_ingame_joiners()
409 {
410         int idx;
411
412         Assert( MULTIPLAYER_MASTER );
413
414         // if my ingame joining flag isn't set, then don't do anything.
415         if ( !(Netgame.flags & NG_FLAG_INGAME_JOINING) ){
416                 return;
417         }
418
419         // traverse through all the players
420         for(idx = 0; idx<MAX_PLAYERS; idx++){
421                 // only process ingame joiners
422                 if( !(Net_players[idx].flags & NETINFO_FLAG_RELIABLE_CONNECTED) || !(Net_players[idx].flags & NETINFO_FLAG_INGAME_JOIN)){
423                         continue;
424                 }
425                 
426                 // if we're waiting for players to receive files, then check on their status
427                 if(Net_players[idx].s_info.ingame_join_flags & INGAME_JOIN_FLAG_FILE_XFER){                                             
428                         switch(multi_xfer_get_status(Net_players[idx].s_info.xfer_handle)){
429                         // if it has successfully completed, set his ok flag
430                         case MULTI_XFER_SUCCESS :
431                                 // set his ok flag
432                                 Net_players[idx].flags |= NETINFO_FLAG_MISSION_OK;
433
434                                 // release the xfer instance handle
435                                 multi_xfer_release_handle(Net_players[idx].s_info.xfer_handle);
436                                 Net_players[idx].s_info.xfer_handle = -1;
437                                 break;
438                         // if it has failed or timed-out, kick the player
439                         case MULTI_XFER_TIMEDOUT:
440                         case MULTI_XFER_FAIL:
441                                 // release the xfer handle
442                                 multi_xfer_release_handle(Net_players[idx].s_info.xfer_handle);
443                                 Net_players[idx].s_info.xfer_handle = -1;
444                                                 
445                                 // kick the loser
446                                 multi_kick_player(idx, 0, KICK_REASON_BAD_XFER);
447                                 break;
448                         }                                               
449                 }               
450                 
451                 // if the player has verified his file signature then send him the packet to load the mission and mark this down
452                 if((Net_players[idx].flags & NETINFO_FLAG_MISSION_OK) && !(Net_players[idx].s_info.ingame_join_flags & INGAME_JOIN_FLAG_LOADING_MISSION)){
453                         // send the netgame update here as well
454                         send_netgame_update_packet(&Net_players[idx]);
455                         send_netplayer_update_packet(&Net_players[idx]);
456
457                         // send the packet and mark it down
458                         send_netplayer_load_packet(&Net_players[idx]);                  
459                         Net_players[idx].s_info.ingame_join_flags |= INGAME_JOIN_FLAG_LOADING_MISSION;                                          
460                 }
461
462                 // once he has finished loading the mission, start sending him ship data packets and mark this down
463                 if((Net_players[idx].state == NETPLAYER_STATE_MISSION_LOADED) && !(Net_players[idx].s_info.ingame_join_flags & INGAME_JOIN_FLAG_SENDING_SHIPS)){
464                         int i;
465
466                         // send the packet and mark it down
467                         for (i = 0; i < Num_teams; i++ ) {
468                                 if(Game_mode & GM_STANDALONE_SERVER){
469                                         send_wss_slots_data_packet(i, 1, &Net_players[idx], 0);
470                                 } else {
471                                         send_wss_slots_data_packet(i, 1, &Net_players[idx]);
472                                 }
473                         }
474
475                         // mark the netgame as a critical stage in ingame joining so that I don't evaluate mission event/
476                         // goals, ship arrivals, etc.
477                         Netgame.flags |= NG_FLAG_INGAME_JOINING_CRITICAL;
478
479                         // send the packet and mark it down
480                         send_ingame_ships_packet(&Net_players[idx]);
481                         Net_players[idx].s_info.ingame_join_flags |= INGAME_JOIN_FLAG_SENDING_SHIPS;
482                 }
483
484                 // once he has finished receiving the ship data, start sending him wing data and mark this down
485                 /*
486                 if((Net_players[idx].state == NETPLAYER_STATE_INGAME_SHIPS) && !(Net_players[idx].s_info.ingame_join_flags & INGAME_JOIN_FLAG_SENDING_WINGS)){
487                         // setup the list of wings to send
488                         Net_players[idx].s_info.wing_index = 0;
489                         Net_players[idx].s_info.wing_index_backup = 0;
490
491                         // send the packet and mark it down
492                         send_ingame_wings_packet(&Net_players[idx]);
493                         Net_players[idx].s_info.ingame_join_flags |= INGAME_JOIN_FLAG_SENDING_WINGS;
494                 }
495                 */
496
497                 // once he has received the respawn packet, send him the player settings for all the players in the game and mark this down
498                 if((Net_players[idx].state == NETPLAYER_STATE_INGAME_SHIPS) && !(Net_players[idx].s_info.ingame_join_flags & INGAME_JOIN_FLAG_SENDING_POST)){
499                         // reset the critical ingame joining flag so that I as server, will start evaluating mission
500                         // things again
501                         Netgame.flags &= ~NG_FLAG_INGAME_JOINING_CRITICAL;
502
503                         // send the packet and mark it down
504                         if(Game_mode & GM_STANDALONE_SERVER){
505                                 send_post_sync_data_packet(&Net_players[idx],0);
506                         } else {
507                                 send_post_sync_data_packet(&Net_players[idx]);
508                         }
509                         Net_players[idx].s_info.ingame_join_flags |= INGAME_JOIN_FLAG_SENDING_POST;
510                 }                               
511
512                 // once the settings have been received, send him the jump into mission packet and mark this down. now the joiner
513                 // moves into the ship select state (ingame)
514                 if((Net_players[idx].state == NETPLAYER_STATE_POST_DATA_ACK) && !(Net_players[idx].s_info.ingame_join_flags & INGAME_JOIN_FLAG_PICK_SHIP)){                     
515                         // if this guy is an obsever, create his observer object and be done!
516                         if(Net_players[idx].flags & NETINFO_FLAG_OBSERVER){
517                                 multi_obs_create_observer(&Net_players[idx]);
518                                 Net_players[idx].flags &= ~(NETINFO_FLAG_INGAME_JOIN);
519                                 Netgame.flags &= ~(NG_FLAG_INGAME_JOINING_CRITICAL | NG_FLAG_INGAME_JOINING);
520                         }
521                 
522                         // send the packet and mark it down
523                         send_jump_into_mission_packet(&Net_players[idx]);
524                         Net_players[idx].s_info.ingame_join_flags |= INGAME_JOIN_FLAG_PICK_SHIP;
525                 }
526
527                 // check to see if his timestamp for ship update (hull, shields, etc) has popped. If so, send some info and reset
528                 if(timestamp_elapsed(Net_players[idx].s_info.last_full_update_time)){
529                         // send the ships
530                         multi_ingame_send_ship_update(&Net_players[idx]);
531
532                         // reset the timestamp
533                         Net_players[idx].s_info.last_full_update_time = timestamp(INGAME_SHIP_UPDATE_TIME);
534                 }
535
536                 // once he has received the weapon state packet, send him the player settings for all the players in the game and mark this down
537                 if(!(Net_players[idx].s_info.ingame_join_flags & INGAME_JOIN_FLAG_SENDING_SETS)){                       
538                         // send the packet and mark it down
539                         // this will update _ALL_ players in the game which is important
540                         send_player_settings_packet();
541                         send_player_settings_packet( &Net_players[idx] );               // send directly so he gets the packet
542                 
543                         Net_players[idx].s_info.ingame_join_flags |= INGAME_JOIN_FLAG_SENDING_SETS;
544                 }               
545         }
546 }
547
548 // the final step for an ingame joining observer - create my observer object, unflag myself as joining and jump into mission
549 void multi_ingame_observer_finish()
550 {
551         // create my local observer object
552         multi_obs_create_observer_client();
553
554         // unflag myself as being an ingame joiner
555         Net_player->flags &= ~(NETINFO_FLAG_INGAME_JOIN);
556
557         // set my state to be in-mission
558         Net_player->state = NETPLAYER_STATE_IN_MISSION;
559         send_netplayer_update_packet();
560
561         // jump into the game
562         gameseq_post_event(GS_EVENT_ENTER_GAME);
563 }
564
565 // --------------------------------------------------------------------------------------------------
566 // INGAME DATA SYNC SCREEN 
567 //
568
569 // mission sync screen init function for ingame joining
570 void multi_ingame_sync_init()
571 {       
572         // if we couldn't get the file signature correctly. send some bogus values
573         multi_get_mission_checksum(Game_current_mission_filename);
574         
575         // everyone should re-initialize these 
576         init_multiplayer_stats();
577
578         // reset all sequencing info
579         multi_oo_reset_sequencing();
580
581         // send the file signature to the host for possible mission file transfer
582         strcpy(Netgame.mission_name,Game_current_mission_filename);
583         send_file_sig_packet(Multi_current_file_checksum,Multi_current_file_length);
584         
585         Ingame_ships_deleted = 0;
586 }
587
588 // mission sync screen do function for ingame joining
589 void multi_ingame_sync_do()
590 {       
591 }
592
593 // mission sync screen do function for ingame joining
594 void multi_ingame_sync_close()
595 {
596 }
597
598
599 // --------------------------------------------------------------------------------------------------
600 // INGAME SHIP SELECT SCREEN 
601 //
602 static char *Multi_ingame_join_bitmap_fname[GR_NUM_RESOLUTIONS] = {
603         "MultiIngame",                          // GR_640
604         "2_MultiIngame"                 // GR_1024
605 };
606
607 static char *Multi_ingame_join_bitmap_mask_fname[GR_NUM_RESOLUTIONS] = {
608         "MultiIngame-M",                        // GR_640
609         "2_MultiIngame-M"                       // GR_1024
610 };
611
612
613 // button defs
614 #define MULTI_INGAME_JOIN_NUM_BUTTONS       2
615 #define MIJ_CANCEL              0
616 #define MIJ_JOIN                        1
617
618 ui_button_info Multi_ingame_join_buttons[GR_NUM_RESOLUTIONS][MULTI_INGAME_JOIN_NUM_BUTTONS] = {
619         { // GR_640
620                 ui_button_info( "MIB_00",       532,    434,    510,    413,    0 ),                                            // cancel
621                 ui_button_info( "MIB_01",       572,    428,    585,    413,    1 ),                                            // join
622         },
623         { // GR_1024
624                 ui_button_info( "2_MIB_00",     851,    695,    916,    685,    0 ),                                            // cancel
625                 ui_button_info( "2_MIB_01",     916,    685,    950,    665,    1 ),                                            // join
626         }
627 };
628
629 #define MULTI_INGAME_JOIN_NUM_TEXT                      8
630
631 UI_XSTR Multi_ingame_join_text[GR_NUM_RESOLUTIONS][MULTI_INGAME_JOIN_NUM_TEXT] = {
632         { // GR_640             
633                 {"Cancel",                                                      387,    510,    413,    UI_XSTR_COLOR_PINK, -1, &Multi_ingame_join_buttons[GR_640][MIJ_CANCEL].button}, 
634                 {"Join",                                                                1303,   585,    413,    UI_XSTR_COLOR_PINK, -1, &Multi_ingame_join_buttons[GR_640][MIJ_JOIN].button},
635                 {"Select Ship",                                 317,    39,     6,              UI_XSTR_COLOR_PINK, -1, NULL},
636                 {"name",                                                                1423,   39,     28,     UI_XSTR_COLOR_GREEN, -1, NULL},
637                 {"class",                                                       1424,   145,    28,     UI_XSTR_COLOR_GREEN,    -1, NULL},
638                 {"status",                                                      1425,   214,    28,     UI_XSTR_COLOR_GREEN,    -1, NULL},
639                 {"primary",                                                     1426,   295,    28,     UI_XSTR_COLOR_GREEN, -1, NULL},
640                 {"secondary",                                           1427,   440,    28,     UI_XSTR_COLOR_GREEN, -1, NULL}
641         },
642         { // GR_1024            
643                 {"Cancel",                                                      387,    843,    665,    UI_XSTR_COLOR_PINK, -1, &Multi_ingame_join_buttons[GR_1024][MIJ_CANCEL].button},        
644                 {"Join",                                                                1303,   950,    665,    UI_XSTR_COLOR_PINK, -1, &Multi_ingame_join_buttons[GR_1024][MIJ_JOIN].button},
645                 {"Select Ship",                                 317,    63,     14,     UI_XSTR_COLOR_PINK, -1, NULL},
646                 {"name",                                                                1423,   63,     45,     UI_XSTR_COLOR_GREEN, -1, NULL},
647                 {"class",                                                       1424,   233,    45,     UI_XSTR_COLOR_GREEN,    -1, NULL},
648                 {"status",                                                      1425,   343,    45,     UI_XSTR_COLOR_GREEN,    -1, NULL},
649                 {"primary",                                                     1426,   473,    45,     UI_XSTR_COLOR_GREEN, -1, NULL},
650                 {"secondary",                                           1427,   704,    45,     UI_XSTR_COLOR_GREEN, -1, NULL}
651         }
652 };
653
654 #define MI_FIELD_X              0
655 #define MI_FIELD_Y              1
656 #define MI_FIELD_W              2
657 #define MI_FIELD_H              3
658
659 static int Mi_width[GR_NUM_RESOLUTIONS] = { 
660         569,            // GR_640
661         910             // GR_1024
662 };
663
664 static int Mi_height[GR_NUM_RESOLUTIONS] = {
665         339,            // GR_640
666         542             // GR_1024
667 };
668
669 static int Mi_spacing[GR_NUM_RESOLUTIONS] = {
670         30,
671         48
672 };
673
674 static int Mi_name_field[GR_NUM_RESOLUTIONS][4] = {
675         // GR_640
676         {
677                 33,                     // x
678                 49,                     // y
679                 100,                    // width
680                 339                     // height
681         },
682         // GR_1024
683         {
684                 53,                     // x
685                 78,                     // y
686                 160,                    // width
687                 542                     // height
688         }
689 };
690
691 static int Mi_class_field[GR_NUM_RESOLUTIONS][4] = {
692         // GR_640
693         {
694                 140,                    // x
695                 49,                     // y
696                 59,                     // width
697                 339                     // height
698         },
699         // GR_1024
700         {
701                 224,                    // x
702                 78,                     // y
703                 94,                     // width
704                 542                     // height
705         }
706 };
707
708 static int Mi_status_field[GR_NUM_RESOLUTIONS][4] = {
709         // GR_640
710         {
711                 209,                    // x
712                 49,                     // y
713                 69,                     // width
714                 339                     // height
715         },
716         // GR_1024
717         {
718                 334,                    // x
719                 78,                     // y
720                 110,                    // width
721                 542                     // height
722         }
723 };
724
725 static int Mi_primary_field[GR_NUM_RESOLUTIONS][4] = {
726         // GR_640
727         {
728                 287,                    // x
729                 49,                     // y
730                 145,                    // width
731                 339                     // height
732         },
733         // GR_1024
734         {
735                 459,                    // x
736                 78,                     // y
737                 232,                    // width
738                 542                     // height
739         }
740 };
741
742 static int Mi_secondary_field[GR_NUM_RESOLUTIONS][4] = {
743         // GR_640
744         {
745                 441,                    // x
746                 49,                     // y
747                 145,                    // width
748                 339                     // height
749         },
750         // GR_1024
751         {
752                 706,                    // x
753                 78,                     // y
754                 232,                    // width
755                 542                     // height
756         }
757 };
758
759 // for timing a player out
760 static int Multi_ingame_timer_coords[GR_NUM_RESOLUTIONS][2] = {
761         {
762                 // GR_640
763                 26,
764                 411
765         },
766         {
767                 // GR_1024
768                 42,
769                 658
770         }
771 };
772
773 //#define MULTI_INGAME_TIME_LEFT_X                      26
774 //#define MULTI_INGAME_TIME_LEFT_Y                      411
775
776 #define MULTI_INGAME_TIME_SECONDS               (1000 * 15)
777 LOCAL int Ingame_time_left;
778
779 // uses MULTI_JOIN_REFRESH_TIME as its timestamp
780 UI_WINDOW Multi_ingame_window;                                                                                  // the window object for the join screen
781 UI_BUTTON Multi_ingame_select_button;                                                                   // for selecting list items
782 int Multi_ingame_bitmap;                                                                                                        // the background bitmap
783
784 // ship class icons
785 #define MULTI_INGAME_MAX_SHIP_ICONS                     40
786 typedef struct is_icon {
787         int bmaps[NUM_ICON_FRAMES];
788         int ship_class;
789 } is_icon;
790 is_icon Multi_ingame_ship_icon[MULTI_INGAME_MAX_SHIP_ICONS];
791 int Multi_ingame_num_ship_icons;
792
793 // # of available ships (also == the # currently being displayed)
794 int Multi_ingame_num_avail;
795
796 // signatures for each of the available ships
797 ushort Multi_ingame_ship_sigs[MAX_PLAYERS];
798
799 // net signature of the ship we've requested to grab as an ingame joiner (waiting for server response if >= 0)
800 ushort Multi_ingame_join_sig;
801
802 // the index into the list of the ship currently selected
803 int Multi_ingame_ship_selected;
804
805 // temporary stuff - used only until we come up with a more permanent interface for this screen
806 #define MAX_INGAME_SHIPS 50
807 #define INGAME_FINAL_TIMEOUT 4000
808
809 ushort Ingame_ship_signatures[MAX_INGAME_SHIPS];
810
811 //XSTR:ON
812
813 // local variables to hold ship/obj/ai information for the joiner.  We need to
814 // create a bogus ship so that packets that the joiner receives during his join
815 // have valid Player_ship, Player_obj, and Player_ai to work with
816 int Ingame_shipnum;
817
818 // display the available ships (OF_COULD_BE_PLAYER flagged)
819 void multi_ingame_join_display_avail();
820
821 // try and scroll the selected ship up
822 void multi_ingame_scroll_select_up();
823
824 // try and scroll the selected ship down
825 void multi_ingame_scroll_select_down();
826
827 // handle all timeout details
828 void multi_ingame_handle_timeout();
829
830 int multi_ingame_get_ship_class_icon(int ship_class)
831 {
832         int idx;
833
834         // lookup through all available ship icons
835         for(idx=0;idx<Multi_ingame_num_ship_icons;idx++){
836                 if(Multi_ingame_ship_icon[idx].ship_class == ship_class){
837                         return idx;
838                 }
839         }
840
841         // couldn't find it 
842         return -1;
843 }
844
845 void multi_ingame_load_icons()
846 {
847         int first_frame, num_frames, idx, s_idx;
848
849         // zero out the icon handles    
850         for(idx=0;idx<MULTI_INGAME_MAX_SHIP_ICONS;idx++){
851                 Multi_ingame_ship_icon[idx].ship_class = -1;            
852                 for(s_idx=0;s_idx<NUM_ICON_FRAMES;s_idx++){
853                         Multi_ingame_ship_icon[idx].bmaps[s_idx] = -1;          
854                 }
855         }               
856         Multi_ingame_num_ship_icons = 0;
857
858         // traverse through all ship types
859         for(idx=0;idx<MAX_SHIP_TYPES;idx++){
860                 // if there is a valid icon for this ship
861                 if((strlen(Ship_info[idx].icon_filename) > 0) && (Multi_ingame_num_ship_icons < MULTI_INGAME_MAX_SHIP_ICONS)){
862                         // set the ship class
863                         Multi_ingame_ship_icon[Multi_ingame_num_ship_icons].ship_class = idx;
864
865                         // load in the animation frames for the icon    
866                         first_frame = bm_load_animation(Ship_info[idx].icon_filename, &num_frames);
867                         if ( first_frame == -1 ) {
868                                 Int3(); // Could not load in icon frames.. get Dave
869                         }       
870                         for ( s_idx = 0; s_idx < num_frames; s_idx++ ) {
871                                 Multi_ingame_ship_icon[Multi_ingame_num_ship_icons].bmaps[s_idx] = first_frame+s_idx;
872                         }
873
874                         Multi_ingame_num_ship_icons++;
875                 }
876         }
877 }
878
879 void multi_ingame_unload_icons()
880 {
881         int idx,s_idx;
882
883         // unload all the bitmaps
884         for(idx=0;idx<Multi_ingame_num_ship_icons;idx++){
885                 for(s_idx=0;s_idx<NUM_ICON_FRAMES;s_idx++){
886                         if(Multi_ingame_ship_icon[idx].bmaps[s_idx] != -1){
887                                 bm_release(Multi_ingame_ship_icon[idx].bmaps[s_idx]);
888                                 Multi_ingame_ship_icon[idx].bmaps[s_idx] = -1;
889                         }
890                 }
891         }
892 }
893
894 // ingame join ship selection screen init
895 void multi_ingame_select_init()
896 {
897         /// int objnum, wingnum_save,idx, goals_save;
898         // ushort net_signature;
899         int idx;
900
901         Multi_ingame_join_sig = 0;
902         Net_player->player->objnum = -1;
903
904         // create a ship, then find a ship to copy crucial information from.  Save and restore the wing
905         // number to be safe.
906         /*
907         wingnum_save = Player_start_pobject.wingnum;
908         net_signature = Player_start_pobject.net_signature;
909         goals_save = Player_start_pobject.ai_goals;
910         Player_start_pobject.wingnum = -1;
911         Player_start_pobject.net_signature = 0;
912         Player_start_pobject.ai_goals = -1;
913         objnum = parse_create_object( &Player_start_pobject );
914         Player_start_pobject.wingnum = wingnum_save;
915         Player_start_pobject.net_signature = net_signature;
916         Player_start_pobject.ai_goals = goals_save;
917
918         if ( objnum == -1 ) {
919                 nprintf(("Network", "Bailing ingame join because unable to create parse object player ship\n"));
920                 multi_quit_game(PROMPT_NONE, MULTI_END_NOTIFY_NONE, MULTI_END_ERROR_INGAME_SHIP);
921                 return;
922         }
923
924         // make it invalid
925         Player_obj = &Objects[objnum];
926         Player_obj->net_signature = 0;                                          
927         Player_ship = &Ships[Player_obj->instance];
928         strcpy(Player_ship->ship_name, NOX("JIP Ship"));
929         Player_ai = &Ai_info[Player_ship->ai_index];
930         */
931
932         // load the temp ship icons
933         multi_ingame_load_icons();
934
935         // blast all the ingame ship signatures
936         memset(Multi_ingame_ship_sigs,0,sizeof(ushort) * MAX_PLAYERS);
937
938         // the index into the list of the ship currently selected
939         Multi_ingame_ship_selected = -1;
940
941         // initialize the time he has left to select a ship
942         Ingame_time_left = timestamp(MULTI_INGAME_TIME_SECONDS);
943
944         // initialize GUI data  
945
946         // create the interface window
947         Multi_ingame_window.create(0,0,gr_screen.max_w,gr_screen.max_h,0);
948         Multi_ingame_window.set_mask_bmap(Multi_ingame_join_bitmap_mask_fname[gr_screen.res]);
949
950         // load the background bitmap
951         Multi_ingame_bitmap = bm_load(Multi_ingame_join_bitmap_fname[gr_screen.res]);
952         if(Multi_ingame_bitmap < 0)
953                 Error(LOCATION, "Couldn't load background bitmap for ingame join");     
954         
955         // create the interface buttons
956         for(idx=0; idx<MULTI_INGAME_JOIN_NUM_BUTTONS; idx++) {
957                 // create the object
958                 Multi_ingame_join_buttons[gr_screen.res][idx].button.create(&Multi_ingame_window, "", Multi_ingame_join_buttons[gr_screen.res][idx].x, Multi_ingame_join_buttons[gr_screen.res][idx].y, 1, 1, 0, 1);
959
960                 // set the sound to play when highlighted
961                 Multi_ingame_join_buttons[gr_screen.res][idx].button.set_highlight_action(common_play_highlight_sound);
962
963                 // set the ani for the button
964                 Multi_ingame_join_buttons[gr_screen.res][idx].button.set_bmaps(Multi_ingame_join_buttons[gr_screen.res][idx].filename);
965
966                 // set the hotspot
967                 Multi_ingame_join_buttons[gr_screen.res][idx].button.link_hotspot(Multi_ingame_join_buttons[gr_screen.res][idx].hotspot);
968         }       
969         
970         // create all xstrs
971         for(idx=0; idx<MULTI_INGAME_JOIN_NUM_TEXT; idx++) {
972                 Multi_ingame_window.add_XSTR(&Multi_ingame_join_text[gr_screen.res][idx]);
973         }
974
975         // create the list item select button
976         Multi_ingame_select_button.create(&Multi_ingame_window, "", Mi_name_field[gr_screen.res][MI_FIELD_X], Mi_name_field[gr_screen.res][MI_FIELD_Y], Mi_width[gr_screen.res], Mi_height[gr_screen.res], 0, 1);
977         Multi_ingame_select_button.hide();                      
978
979         // load freespace stuff
980         // JAS: Code to do paging used to be here.
981 }
982
983 // process all ship list related details
984 void multi_ingame_ship_list_process()
985 {
986         int select_index,y;
987
988         // if we currently don't have any ships selected, but we've got items on the list, select the first one
989         if((Multi_ingame_ship_selected == -1) && (Multi_ingame_num_avail > 0)){
990                 gamesnd_play_iface(SND_USER_SELECT);
991                 Multi_ingame_ship_selected = 0;
992         }
993
994         // if we currently have a ship selected, but it disappears, select the next ship (is possible0
995         if((Multi_ingame_ship_selected >= 0) && (Multi_ingame_ship_selected >= Multi_ingame_num_avail)){
996                 gamesnd_play_iface(SND_USER_SELECT);
997                 Multi_ingame_ship_selected = Multi_ingame_num_avail-1;
998         }
999         
1000         // if the player clicked on the select button, see if the selection has changed
1001         if(Multi_ingame_select_button.pressed()){
1002                 Multi_ingame_select_button.get_mouse_pos(NULL,&y);
1003                 select_index = y / Mi_spacing[gr_screen.res];
1004
1005                 // if we've selected a valid item
1006                 if((select_index >= 0) && (select_index < Multi_ingame_num_avail)){
1007                         // if we're not selected the same item, play a sound
1008                         if(Multi_ingame_ship_selected != select_index){
1009                                 gamesnd_play_iface(SND_USER_SELECT);
1010                         }
1011
1012                         // select the item
1013                         Multi_ingame_ship_selected = select_index;
1014                 }
1015         }
1016 }
1017
1018
1019 // determines if a button was pressed, and acts accordingly
1020 void multi_ingame_join_check_buttons()
1021 {
1022         int idx;
1023         for(idx=0; idx<MULTI_INGAME_JOIN_NUM_BUTTONS; idx++) {
1024                 // we only really need to check for one button pressed at a time,
1025                 // so we can break after finding one.
1026                 if(Multi_ingame_join_buttons[gr_screen.res][idx].button.pressed()) {
1027                         multi_ingame_join_button_pressed(idx);
1028                         break;
1029                 }
1030         }
1031 }
1032
1033 // a button was pressed, so make it do its thing
1034 // this is the "acting accordingly" part
1035 void multi_ingame_join_button_pressed(int n)
1036 {
1037         switch(n) {
1038         case MIJ_CANCEL:
1039                 multi_quit_game(PROMPT_CLIENT);
1040                 break;
1041         case MIJ_JOIN:
1042                 // don't do further processing if the game is paused
1043                 if ( Netgame.game_state == NETGAME_STATE_PAUSED )
1044                         return;
1045
1046                 if(Multi_ingame_join_sig == 0) {
1047                         // if he has a valid ship selected
1048                         if(Multi_ingame_ship_selected >= 0) {
1049                                 gamesnd_play_iface(SND_USER_SELECT);
1050                         
1051                                 // select the sig of this ship and send a request for it
1052                                 Multi_ingame_join_sig = Multi_ingame_ship_sigs[Multi_ingame_ship_selected];
1053                                 
1054                                 // send a request to the
1055                                 send_ingame_ship_request_packet(INGAME_SR_REQUEST,Multi_ingame_join_sig);
1056                         } else {
1057                                 gamesnd_play_iface(SND_GENERAL_FAIL);
1058                         }
1059                 } else {
1060                         gamesnd_play_iface(SND_GENERAL_FAIL);
1061                 }
1062
1063                 break;
1064         default:
1065                 // how the hell did this happen?
1066                 Int3();
1067         }
1068 }
1069
1070
1071 // ingame join ship selection screen do
1072 void multi_ingame_select_do()
1073 {       
1074         int k = Multi_ingame_window.process();
1075
1076         // process any keypresses
1077         switch(k){
1078         case KEY_ESC :
1079                 multi_quit_game(PROMPT_CLIENT);         
1080                 break;
1081
1082         case KEY_UP:
1083                 multi_ingame_scroll_select_up();
1084                 break;
1085
1086         case KEY_DOWN:
1087                 multi_ingame_scroll_select_down();
1088                 break;
1089         }       
1090
1091         // process button presses
1092         // multi_ingame_process_buttons();
1093         multi_ingame_join_check_buttons();
1094         
1095         // process any ship list related events
1096         multi_ingame_ship_list_process();       
1097         
1098         // draw the background, etc
1099         gr_reset_clip();        
1100         GR_MAYBE_CLEAR_RES(Multi_ingame_bitmap);
1101         if(Multi_ingame_bitmap != -1){
1102                 gr_set_bitmap(Multi_ingame_bitmap);
1103                 gr_bitmap(0,0);
1104         }
1105         Multi_ingame_window.draw();
1106
1107         // handle all timeout details. blitting, etc
1108         multi_ingame_handle_timeout();
1109
1110         // display the available ships
1111         multi_ingame_join_display_avail();              
1112
1113         // flip the buffer
1114         gr_flip();      
1115 }
1116
1117 // ingame join ship select close
1118 void multi_ingame_select_close()
1119 {       
1120         // unload any bitmaps
1121         if(!bm_unload(Multi_ingame_bitmap)){
1122                 nprintf(("General","WARNING : could not unload background bitmap %s\n",Multi_ingame_join_bitmap_fname[gr_screen.res]));
1123         }       
1124
1125         // unload all the ship class icons
1126         multi_ingame_unload_icons();
1127         
1128         // destroy the UI_WINDOW
1129         Multi_ingame_window.destroy();  
1130
1131         // stop main hall music
1132         main_hall_stop_music(); 
1133 }
1134
1135 // display an individual ships information, starting at the indicated y pixel value
1136 void multi_ingame_join_display_ship(object *objp,int y_start)
1137 {
1138         int icon_num,idx;
1139         ship_info *sip;
1140         int y_spacing;
1141         ship_weapon *wp;
1142
1143         sip = &Ship_info[Ships[objp->instance].ship_info_index];
1144         
1145         // blit the ship name itself
1146         gr_set_color_fast(&Color_normal);
1147         gr_string(Mi_name_field[gr_screen.res][MI_FIELD_X],y_start+10, Ships[objp->instance].ship_name);
1148         
1149         // blit the ship class icon
1150         icon_num = multi_ingame_get_ship_class_icon(Ships[objp->instance].ship_info_index);
1151         if(icon_num != -1){
1152                 gr_set_bitmap(Multi_ingame_ship_icon[icon_num].bmaps[0]);
1153                 gr_bitmap(Mi_class_field[gr_screen.res][MI_FIELD_X] + 15, y_start);
1154         }
1155         
1156         gr_set_color_fast(&Color_bright);
1157         wp = &Ships[objp->instance].weapons;
1158         
1159         // blit the ship's primary weapons      
1160         y_spacing = (Mi_spacing[gr_screen.res] - (wp->num_primary_banks * 10)) / 2;
1161         for(idx=0;idx<wp->num_primary_banks;idx++){
1162                 gr_string(Mi_primary_field[gr_screen.res][MI_FIELD_X], y_start + y_spacing + (idx * 10), Weapon_info[wp->primary_bank_weapons[idx]].name);
1163         }
1164
1165         // blit the ship's secondary weapons    
1166         y_spacing = (Mi_spacing[gr_screen.res] - (wp->num_secondary_banks * 10)) / 2;
1167         for(idx=0;idx<wp->num_secondary_banks;idx++){
1168                 gr_string(Mi_secondary_field[gr_screen.res][MI_FIELD_X], y_start + y_spacing + (idx * 10), Weapon_info[wp->secondary_bank_weapons[idx]].name);
1169         }       
1170
1171         // blit the shield/hull integrity
1172         hud_shield_show_mini(objp, Mi_status_field[gr_screen.res][MI_FIELD_X] + 15, y_start + 3,5,7);
1173 }
1174
1175 // display the available ships (OF_COULD_BE_PLAYER flagged)
1176 void multi_ingame_join_display_avail()
1177 {               
1178         ship_obj *moveup;       
1179
1180         // recalculate this # every frame
1181         Multi_ingame_num_avail = 0;     
1182
1183         // display a background highlight rectangle for any selected lines
1184         if(Multi_ingame_ship_selected != -1){           
1185                 int y_start = (Mi_name_field[gr_screen.res][MI_FIELD_Y] + (Multi_ingame_ship_selected * Mi_spacing[gr_screen.res]));            
1186
1187                 // draw the border
1188                 gr_set_color_fast(&Color_bright_blue);
1189                 gr_line(Mi_name_field[gr_screen.res][MI_FIELD_X]-1,y_start-1, (Mi_name_field[gr_screen.res][MI_FIELD_X]-1) + (Mi_width[gr_screen.res]+2),y_start-1);
1190                 gr_line(Mi_name_field[gr_screen.res][MI_FIELD_X]-1,y_start + Mi_spacing[gr_screen.res] - 2, (Mi_name_field[gr_screen.res][MI_FIELD_X]-1) + (Mi_width[gr_screen.res]+2),y_start + Mi_spacing[gr_screen.res] - 2);
1191                 gr_line(Mi_name_field[gr_screen.res][MI_FIELD_X]-1,y_start, Mi_name_field[gr_screen.res][MI_FIELD_X]-1, y_start + Mi_spacing[gr_screen.res] - 2);
1192                 gr_line((Mi_name_field[gr_screen.res][MI_FIELD_X]-1) + (Mi_width[gr_screen.res]+2), y_start,(Mi_name_field[gr_screen.res][MI_FIELD_X]-1) + (Mi_width[gr_screen.res]+2),y_start + Mi_spacing[gr_screen.res] - 2);
1193         }
1194
1195         moveup = GET_FIRST(&Ship_obj_list);     
1196         while(moveup != END_OF_LIST(&Ship_obj_list)){
1197                 if( !(Ships[Objects[moveup->objnum].instance].flags & (SF_DYING|SF_DEPARTING)) && (Objects[moveup->objnum].flags & OF_COULD_BE_PLAYER) ) {
1198                         // display the ship
1199                         multi_ingame_join_display_ship(&Objects[moveup->objnum],Mi_name_field[gr_screen.res][MI_FIELD_Y] + (Multi_ingame_num_avail * Mi_spacing[gr_screen.res]));
1200
1201                         // set the ship signature
1202                         Multi_ingame_ship_sigs[Multi_ingame_num_avail] = Objects[moveup->objnum].net_signature;
1203                         
1204                         // inc the # available
1205                         Multi_ingame_num_avail++;
1206                 }
1207                 moveup = GET_NEXT(moveup);
1208         }               
1209 }
1210
1211 // try and scroll the selected ship up
1212 void multi_ingame_scroll_select_up()
1213 {
1214         if(Multi_ingame_ship_selected > 0){
1215                 gamesnd_play_iface(SND_USER_SELECT);
1216                 Multi_ingame_ship_selected--;
1217         } else {
1218                 gamesnd_play_iface(SND_GENERAL_FAIL);
1219         }       
1220 }
1221
1222 // try and scroll the selected ship down
1223 void multi_ingame_scroll_select_down()
1224 {
1225         if(Multi_ingame_ship_selected < (Multi_ingame_num_avail - 1)){
1226                 gamesnd_play_iface(SND_USER_SELECT);
1227                 Multi_ingame_ship_selected++;
1228         } else {
1229                 gamesnd_play_iface(SND_GENERAL_FAIL);
1230         }
1231 }
1232
1233 // handle all timeout details
1234 void multi_ingame_handle_timeout()
1235 {
1236         /*
1237         // uncomment this block to disable the timer
1238         gr_set_color_fast(&Color_bright_red);
1239         gr_string(Multi_ingame_timer_coords[gr_screen.res][0], Multi_ingame_timer_coords[gr_screen.res][1], "Timer disabled!!");
1240         return;
1241         */
1242
1243         // if we've timed out, leave the game
1244         if( timestamp_elapsed(Ingame_time_left) ) {
1245                 multi_quit_game(PROMPT_NONE, MULTI_END_NOTIFY_INGAME_TIMEOUT, MULTI_END_ERROR_NONE);
1246                 return;
1247         }
1248
1249         // otherwise, blit how much time we have left
1250         int time_left = timestamp_until(Ingame_time_left) / 1000;
1251         char tl_string[100];
1252         gr_set_color_fast(&Color_bright);
1253         memset(tl_string,0,100);
1254         sprintf(tl_string,XSTR("Time remaining : %d s\n",682),time_left);       
1255         gr_string(Multi_ingame_timer_coords[gr_screen.res][0], Multi_ingame_timer_coords[gr_screen.res][1], tl_string);
1256 }
1257
1258
1259 // --------------------------------------------------------------------------------------------------
1260 // PACKET HANDLER functions
1261 // these are also defined in multimsgs.h, but the implementations are in the module for the sake of convenience
1262 //
1263
1264 #define INGAME_PACKET_SLOP              75                              // slop value used for packets to ingame joiner
1265
1266 void process_ingame_ships_packet( ubyte *data, header *hinfo )
1267 {
1268         int offset, sflags, oflags, team, j;
1269         ubyte p_type;
1270         ushort net_signature;   
1271         short wing_data;        
1272         int team_val, slot_index, idx;
1273         char ship_name[255] = "";
1274         object *objp;
1275         int net_sig_modify;
1276
1277         // go through the ship obj list and delete everything. YEAH
1278         if(!Ingame_ships_deleted){
1279                 int idx;
1280
1281                 // no player object
1282                 Player_obj = NULL;
1283                 Player_ship = NULL;
1284                 Player_ai = NULL;
1285
1286                 // delete all ships
1287                 for(idx=0; idx<MAX_SHIPS; idx++){
1288                         if((Ships[idx].objnum >= 0) && (Ships[idx].objnum < MAX_OBJECTS)){
1289                                 obj_delete(Ships[idx].objnum);
1290                         }
1291                 }
1292
1293                 Ingame_ships_deleted = 1;
1294         }
1295
1296         offset = HEADER_LENGTH;
1297
1298         // go
1299         GET_DATA( p_type );     
1300         while ( p_type == INGAME_SHIP_NEXT ) {
1301                 p_object *p_objp;
1302                 int ship_num, objnum;
1303
1304                 GET_STRING( ship_name );
1305                 GET_DATA( net_signature );
1306                 GET_DATA( sflags );
1307                 GET_DATA( oflags );
1308                 GET_DATA( team );               
1309                 GET_DATA( wing_data );
1310                 net_sig_modify = 0;
1311                 if(wing_data >= 0){
1312                         GET_DATA(Wings[wing_data].current_wave);                        
1313                         net_sig_modify = Wings[wing_data].current_wave - 1;
1314                 }
1315
1316                 // lookup ship in the original ships array
1317                 p_objp = mission_parse_get_original_ship(net_signature);
1318                 if(p_objp == NULL){
1319                         // if this ship is part of wing not on its current wave, look for its "original" by subtracting out wave #
1320                         p_objp = mission_parse_get_arrival_ship((ushort)(net_signature - (ushort)net_sig_modify));
1321                 }
1322                 if(p_objp == NULL){
1323                         Int3();
1324                         nprintf(("Network", "Couldn't find ship %s in either arrival list or in mission"));
1325                         multi_quit_game(PROMPT_NONE, MULTI_END_NOTIFY_NONE, MULTI_END_ERROR_INGAME_BOGUS);
1326                         return;
1327                 }
1328
1329                 // go ahead and create the parse object.  Set the network signature of this guy before
1330                 // creation
1331                 // multi_set_network_signature( net_signature, MULTI_SIG_SHIP );
1332                 objnum = parse_create_object( p_objp );
1333                 ship_num = Objects[objnum].instance;
1334                 Objects[objnum].flags = oflags;
1335                 Objects[objnum].net_signature = net_signature;
1336
1337                 // assign any common data
1338                 strcpy(Ships[ship_num].ship_name, ship_name);
1339                 Ships[ship_num].flags = sflags;
1340                 Ships[ship_num].team = team;
1341                 Ships[ship_num].wingnum = (int)wing_data;                               
1342
1343                 GET_DATA( p_type );
1344         }
1345
1346         PACKET_SET_SIZE();
1347
1348         // if we have reached the end of the list and change our network state
1349         if ( p_type == INGAME_SHIP_LIST_EOL ) {         
1350                 // merge all created list
1351                 obj_merge_created_list();
1352
1353                 // fixup player ship stuff
1354                 for(idx=0; idx<MAX_SHIPS; idx++){
1355                         if(Ships[idx].objnum < 0){      
1356                                 continue;
1357                         }
1358
1359                         // get the team and slot.  Team will be -1 when it isn't a part of player wing.  So, if
1360                         // not -1, then be sure we have a valid slot, then change the ship type, etc.
1361                         objp = &Objects[Ships[idx].objnum];             
1362                         multi_ts_get_team_and_slot(Ships[idx].ship_name, &team_val, &slot_index);
1363                         if ( team_val != -1 ) {
1364                                 Assert( slot_index != -1 );
1365
1366                                 // change the ship type and the weapons
1367                                 change_ship_type(objp->instance, Wss_slots_teams[team_val][slot_index].ship_class);
1368                                 wl_bash_ship_weapons(&Ships[idx].weapons, &Wss_slots_teams[team_val][slot_index]);
1369         
1370                                 // Be sure to mark this ship as as a could_be_player
1371                                 obj_set_flags( objp, objp->flags | OF_COULD_BE_PLAYER );
1372                                 objp->flags &= ~OF_PLAYER_SHIP;
1373                         }
1374
1375                         // if this is a player ship, make sure we find out who's it is and set their objnum accordingly
1376                         if(team_val != -1){
1377                                 for( j = 0; j < MAX_PLAYERS; j++){
1378                                         if(MULTI_CONNECTED(Net_players[j]) && (Net_players[j].player->objnum == Objects[Ships[idx].objnum].net_signature)) {                                            
1379                                                 // nprintf(("Network", "Making %s ship for %s\n", Ships[shipnum].ship_name, Net_players[j].player->callsign));
1380                                                 multi_assign_player_ship( j, objp, Ships[idx].ship_info_index );
1381                                                 objp->flags |= OF_PLAYER_SHIP;
1382                                                 objp->flags &= ~OF_COULD_BE_PLAYER;
1383                                                 break;
1384                                         }
1385                                 }
1386                         }
1387                 }
1388
1389                 // notify the server that we're all good.
1390                 Net_player->state = NETPLAYER_STATE_INGAME_SHIPS;
1391                 send_netplayer_update_packet();
1392
1393                 // add some mission sync text
1394                 multi_common_add_text(XSTR("Ships packet ack (ingame)\n",683));
1395         }
1396 }
1397
1398 void send_ingame_ships_packet(net_player *player)
1399 {
1400         ubyte data[MAX_PACKET_SIZE];
1401         ubyte p_type;
1402         ship_obj *so;
1403         int packet_size;
1404         short wing_data;
1405
1406         BUILD_HEADER( SHIPS_INGAME_PACKET );
1407
1408         // essentially, we are going to send a list of ship names to the joiner for ships that are not
1409         // in wings.  The joiner will take the list, create any ships which should be created, and delete all
1410         // other ships after the list is sent.
1411         for ( so = GET_FIRST(&Ship_obj_list); so != END_OF_LIST(&Ship_obj_list); so = GET_NEXT(so) ) {
1412                 ship *shipp;
1413
1414                 shipp = &Ships[Objects[so->objnum].instance];
1415
1416                 // skip all wings.
1417                 // if ( shipp->wingnum != -1 ){
1418                         // continue;
1419                 // }
1420
1421                 if ( Objects[so->objnum].net_signature == STANDALONE_SHIP_SIG ){
1422                         continue;
1423                 }
1424
1425                 //  add the ship name and other information such as net signature, ship and object(?) flags.
1426                 p_type = INGAME_SHIP_NEXT;
1427                 ADD_DATA( p_type );
1428                 ADD_STRING( shipp->ship_name );
1429                 ADD_DATA( Objects[so->objnum].net_signature );
1430                 ADD_DATA( shipp->flags );
1431                 ADD_DATA( Objects[so->objnum].flags );
1432                 ADD_DATA( shipp->team );
1433                 wing_data = (short)shipp->wingnum;
1434                 ADD_DATA(wing_data);
1435                 if(wing_data >= 0){
1436                         ADD_DATA(Wings[wing_data].current_wave);
1437                 }
1438
1439                 // don't send anymore data if we are getting close to the maximum size of this packet.  Send it off and
1440                 // keep going
1441                 if ( packet_size > (MAX_PACKET_SIZE - INGAME_PACKET_SLOP) ) {
1442                         p_type = INGAME_SHIP_LIST_EOP;
1443                         ADD_DATA( p_type );                     
1444                         multi_io_send_reliable(player, data, packet_size);
1445                         BUILD_HEADER( SHIPS_INGAME_PACKET );
1446                 }
1447         }
1448
1449         // end of the ship list!!!
1450         p_type = INGAME_SHIP_LIST_EOL;
1451         ADD_DATA( p_type );     
1452         multi_io_send_reliable(player, data, packet_size);
1453 }
1454
1455 void process_ingame_wings_packet( ubyte *data, header *hinfo )
1456 {
1457         Int3();
1458 }
1459 /*
1460 // code to process the wing data from a server.
1461 void process_ingame_wings_packet( ubyte *data, header *hinfo )
1462 {
1463         int offset, wingnum;
1464         ubyte p_type, what;
1465
1466         offset = HEADER_LENGTH;
1467
1468         GET_DATA( p_type );
1469
1470         // p_type tells us whether to stop or not
1471         while ( p_type == INGAME_WING_NEXT ) {
1472                 wing *wingp;
1473
1474                 // get the wingnum and a pointer to it.  The game stores data for all wings always, so this
1475                 // is perfectly valid
1476                 GET_DATA( wingnum );
1477                 wingp = &Wings[wingnum];
1478
1479                 GET_DATA( what );
1480                 if ( what == INGAME_WING_NOT_ARRIVED ) {
1481                         Assert( wingp->total_arrived_count == 0 );                      // this had better be true!!!
1482                 } else if ( what == INGAME_WING_DEPARTED ) {
1483                         // mark the wing as gone.  if it isn't, it soon will be.  Maybe we should send more information
1484                         // about these wings later (like total_arrived_count, etc), but we will see.
1485                         wingp->flags |= WF_WING_GONE;
1486                 } else {
1487                         int total_arrived_count, current_count, current_wave, i, j;
1488                         ushort signature;
1489                         int shipnum;
1490
1491                         // the wing is present in the mission on the server.  Get the crucial information about the
1492                         // wing.  Then get the ships for this wing in order on the client machine
1493                         GET_DATA( total_arrived_count );
1494                         GET_DATA( current_count );
1495                         GET_DATA( current_wave );
1496
1497                         Assert( current_wave > 0 );
1498                         Assert( total_arrived_count > 0 );
1499
1500                         // for this wing, strip it down to nothing.  Let the parse object ocde recreate the
1501                         // wing from the parse objects, then bash any weapons, etc for player wings.  We need
1502                         // to do this because we might actually wind up with > MAX_SHIPS_PER_WING if we
1503                         // don't delete them all first, and have a > 0 threshold, and are on something other
1504                         // than the first wave.  Only do this for non-player wings.
1505
1506                         nprintf(("Network", "Clearing %s -- %d ships\n", wingp->name, wingp->current_count));
1507                         for ( i = 0; i < wingp->current_count; i++ ) {
1508                                 int index, objnum;
1509
1510                                 index = wingp->ship_index[i];
1511                                 Assert( index != -1 );
1512                                 objnum = Ships[index].objnum;
1513                                 Assert( objnum != -1 );
1514
1515                                 // delete the object since we are filling the wing again anyway.
1516                                 obj_delete( objnum );
1517                                 Objects[objnum].net_signature = 0;                              // makes this object "invalid" until dead.
1518                                 if ( Objects[objnum].type == OBJ_GHOST ) {
1519                                         nprintf(("Network", "Marking ghost objnum %d as dead\n", objnum));
1520                                         Objects[objnum].flags |= OF_SHOULD_BE_DEAD;
1521                                 }
1522                                 Ingame_ships_to_delete[index] = 0;              // be sure that this guy doesn't get deleted, since we already deleted it
1523                                 wingp->ship_index[i] = -1;
1524                         }
1525                         wingp->current_count = 0;
1526                         wingp->total_arrived_count = 0;
1527
1528                         // now, recreate all the ships needed
1529                         for (i = 0; i < current_count; i++ ) {
1530                                 int which_one, team, slot_index, specific_instance;;
1531                                 ship *shipp;
1532                                 object *objp;
1533
1534                                 GET_DATA( signature );
1535
1536                                 // assign which_one to be the given signature - wing's base signature.  This let's us
1537                                 // know which ship to create (i.e. the total_arrivel_count);
1538                                 which_one = signature - wingp->net_signature;
1539                                 Assert( (which_one >= 0) && (which_one < (wingp->net_signature + (wingp->wave_count*wingp->num_waves))) );
1540                                 wingp->total_arrived_count = (ushort)which_one;
1541
1542                                 // determine which ship in the ahip arrival list this guy is.  It is a 0 based index
1543                                 specific_instance = which_one % wingp->wave_count;
1544
1545                                 // call parse_wing_create_ships making sure that we only ever create 1 ship at a time.  We don't
1546                                 // want parse_wing_create_ships() to assign network signature either.  We will directly
1547                                 // assign it here.
1548
1549                                 wingp->current_wave = 0;                                                // make it the first wave.  Ensures that ships don't get removed off the list
1550                                 parse_wing_create_ships( wingp, 1, 1, specific_instance );
1551                                 shipnum = wingp->ship_index[wingp->current_count-1];
1552                                 Ingame_ships_to_delete[shipnum] = 0;                    // "unmark" this ship so it doesn't get deleted.
1553
1554                                 // kind of stupid, but bash the name since it won't get recreated properly from
1555                                 // the parse_wing_create_ships call.
1556                                 shipp = &Ships[shipnum];
1557                                 sprintf(shipp->ship_name, NOX("%s %d"), wingp->name, which_one + 1);
1558                                 nprintf(("Network", "Created %s\n", shipp->ship_name));
1559
1560                                 objp = &Objects[shipp->objnum];
1561                                 objp->net_signature = (ushort)(wingp->net_signature + which_one);
1562
1563                                 // get the team and slot.  Team will be -1 when it isn't a part of player wing.  So, if
1564                                 // not -1, then be sure we have a valid slot, then change the ship type, etc.
1565                                 multi_ts_get_team_and_slot(shipp->ship_name, &team, &slot_index);
1566                                 if ( team != -1 ) {
1567                                         Assert( slot_index != -1 );
1568
1569                                         // change the ship type and the weapons
1570                                         change_ship_type(objp->instance, Wss_slots_teams[team][slot_index].ship_class);
1571                                         wl_bash_ship_weapons(&shipp->weapons,&Wss_slots_teams[team][slot_index]);
1572
1573                                         // Be sure to mark this ship as as a could_be_player
1574                                         obj_set_flags( objp, objp->flags | OF_COULD_BE_PLAYER );
1575                                         objp->flags &= ~OF_PLAYER_SHIP;
1576                                 }
1577
1578                                 // if this is a player ship, make sure we find out who's it is and set their objnum accordingly
1579                                 for( j = 0; j < MAX_PLAYERS; j++){
1580                                         if(MULTI_CONNECTED(Net_players[j]) && (Net_players[j].player->objnum == signature)) {
1581                                                 Assert( team != -1 );           // to help trap errors!!!
1582                                                 nprintf(("Network", "Making %s ship for %s\n", Ships[shipnum].ship_name, Net_players[j].player->callsign));
1583                                                 multi_assign_player_ship( j, objp, Ships[shipnum].ship_info_index );
1584                                                 objp->flags |= OF_PLAYER_SHIP;
1585                                                 objp->flags &= ~OF_COULD_BE_PLAYER;
1586                                                 break;
1587                                         }
1588                                 }
1589                         }
1590
1591
1592                         // we will have no ships in any wings at this point (we didn't create any when we loaded the
1593                         // mission).  Set the current wave of this wing to be 1 less than was passed in since this value
1594                         // will get incremented in parse_wing_create_ships;
1595                         wingp->current_wave = current_wave;
1596                         wingp->total_arrived_count = total_arrived_count;
1597                 }
1598
1599                 GET_DATA( p_type );
1600         }
1601
1602    PACKET_SET_SIZE();
1603
1604         // if we have reached the end of the list change our network state
1605         if ( p_type == INGAME_WING_LIST_EOL ) {
1606                 Net_player->state = NETPLAYER_STATE_INGAME_WINGS;
1607                 send_netplayer_update_packet();
1608
1609                 // add some mission sync text
1610                 multi_common_add_text(XSTR("Wings packet (ingame)\n",684));
1611         }
1612         
1613 }
1614
1615 // function to send information about wings.  We need to send enough information to let the client
1616 // construct or reconstruct any wings in the mission.  We will rely on the fact that the host wing array
1617 // will exactly match the client wing array (in terms of number, and wing names)
1618 void send_ingame_wings_packet( net_player *player )
1619 {
1620         ubyte data[MAX_PACKET_SIZE];
1621         ubyte p_type;
1622         int packet_size, i;
1623         ubyte what;
1624
1625         BUILD_HEADER( WINGS_INGAME_PACKET );
1626
1627         // iterate through the wings list
1628         for ( i = 0; i < num_wings; i++ ) {
1629                 wing *wingp;
1630
1631                 wingp = &Wings[i];
1632
1633                 p_type = INGAME_WING_NEXT;
1634                 ADD_DATA( p_type );
1635
1636                 ADD_DATA( i );
1637
1638                 // add wing data that the client needs.  There are several conditions to send to clients:
1639                 //
1640                 // 1. wing hasn't arrived -- total_arrived_count will be 0
1641                 // 2. wing is done (or currently departing)
1642                 // 3. wing is present (any wave, any number of ships).
1643                 //
1644                 // 1 and 2 are easy to handle.  (3) is the hardest.
1645                 if ( wingp->total_arrived_count == 0 ) {
1646                         what = INGAME_WING_NOT_ARRIVED;
1647                         ADD_DATA( what );
1648                 } else if ( wingp->flags & (WF_WING_GONE | WF_WING_DEPARTING) ) {
1649                         what = INGAME_WING_DEPARTED;
1650                         ADD_DATA( what );
1651                 } else {
1652                         int j;
1653
1654                         // include to code to possibly send more wing data here in this part of the if/else
1655                         // chain.  We can do this because MAX_WINGS * 8 (8 being the number of byte for a minimum
1656                         // description of a wing) is always less than MAX_PACKET_SIZE.  Checking here ensures that
1657                         // we have enough space for *this* wing in the packet, and not the largest wing.  The
1658                         // formula below looks at number of ships in the wing, the name length, length of the signature,
1659                         // and the size of the bytes added before the ship names.  32 accounts for a little slop
1660                         if ( packet_size > (MAX_PACKET_SIZE - (wingp->current_count * (NAME_LENGTH+2) + 32)) ) {
1661                                 p_type = INGAME_WING_LIST_EOP;
1662                                 ADD_DATA( p_type );                             
1663                                 multi_io_send_reliable(player, data, packet_size);
1664                                 BUILD_HEADER( WINGS_INGAME_PACKET );
1665                         }
1666                         what = INGAME_WING_PRESENT;
1667                         ADD_DATA( what );
1668                         ADD_DATA( wingp->total_arrived_count );
1669                         ADD_DATA( wingp->current_count );
1670                         ADD_DATA( wingp->current_wave );
1671
1672                         // add the ship name and net signature of all ships currently in the wing.
1673                         for ( j = 0; j < wingp->current_count; j++ ) {
1674                                 ship *shipp;
1675
1676                                 shipp = &Ships[wingp->ship_index[j]];
1677                                 //ADD_STRING( shipp->ship_name );
1678                                 ADD_DATA( Objects[shipp->objnum].net_signature );
1679                         }
1680                 }
1681
1682         }
1683
1684         p_type = INGAME_WING_LIST_EOL;
1685         ADD_DATA( p_type );
1686         
1687         multi_io_send_reliable(player, data, packet_size);
1688 }
1689 */
1690
1691 // send a request or a reply regarding ingame join ship choice
1692 void send_ingame_ship_request_packet(int code,int rdata,net_player *pl)
1693 {
1694         ubyte data[MAX_PACKET_SIZE],val;
1695         ship *shipp;
1696         int i, packet_size = 0;
1697         ushort signature;
1698         p_object *pobj;
1699
1700         // add the data
1701         BUILD_HEADER(INGAME_SHIP_REQUEST);
1702
1703         // add the code
1704         ADD_DATA(code);
1705         
1706         // add any code specific data
1707         switch(code){
1708         case INGAME_SR_REQUEST:
1709                 // add the net signature of the ship we're requesting
1710                 signature = (ushort)rdata;
1711                 ADD_DATA( signature );
1712                 break;
1713         case INGAME_SR_CONFIRM:
1714                 // get a pointer to the ship
1715                 shipp = &Ships[Objects[rdata].instance];
1716
1717                 // add the most recent position and orientation for the requested ship
1718                 ADD_DATA(Objects[rdata].pos);
1719                 ADD_ORIENT(Objects[rdata].orient);
1720                 ADD_DATA( Missiontime );
1721
1722                 // add the # of respawns this ship has left
1723                 pobj = mission_parse_get_arrival_ship( Objects[rdata].net_signature );
1724                 Assert(pobj != NULL);
1725                 ADD_DATA(pobj->respawn_count);
1726
1727                 // add the ships ets settings
1728                 val = (ubyte)shipp->weapon_recharge_index;
1729                 ADD_DATA(val);
1730                 val = (ubyte)shipp->shield_recharge_index;
1731                 ADD_DATA(val);
1732                 val = (ubyte)shipp->engine_recharge_index;
1733                 ADD_DATA(val);
1734
1735                 // add current primary and secondary banks, and add link status
1736                 val = (ubyte)shipp->weapons.current_primary_bank;
1737                 ADD_DATA(val);
1738                 val = (ubyte)shipp->weapons.current_secondary_bank;
1739                 ADD_DATA(val);          
1740
1741                 // add the current ammo count for secondary banks;
1742                 val = (ubyte)shipp->weapons.num_secondary_banks;                // for sanity checking
1743                 ADD_DATA(val);
1744                 for ( i = 0; i < shipp->weapons.num_secondary_banks; i++ ) {
1745                         Assert( shipp->weapons.secondary_bank_ammo[i] < UCHAR_MAX );
1746                         val = (ubyte)shipp->weapons.secondary_bank_ammo[i];
1747                         ADD_DATA(val);
1748                 }
1749
1750                 // add the link status of weapons
1751                 // primary link status  
1752                 val = 0;
1753                 if(shipp->flags & SF_PRIMARY_LINKED){
1754                         val |= (1<<0);
1755                 }
1756                 if(shipp->flags & SF_SECONDARY_DUAL_FIRE){
1757                         val |= (1<<1);
1758                 }
1759                 ADD_DATA(val);          
1760                 break;
1761         }
1762
1763         // send the packet
1764         if(Net_player->flags & NETINFO_FLAG_AM_MASTER){
1765                 Assert(pl != NULL);             
1766                 multi_io_send_reliable(pl, data, packet_size);
1767         } else {                
1768                 multi_io_send_reliable(Net_player, data, packet_size);
1769         }
1770
1771         // if this is a confirm to a player -- send data to the other players in the game telling them
1772         if ( (code == INGAME_SR_CONFIRM) && (Net_player->flags & NETINFO_FLAG_AM_MASTER) ) {
1773                 int i, player_num;
1774
1775                 player_num = NET_PLAYER_NUM(pl);
1776                 code = INGAME_PLAYER_CHOICE;
1777                 BUILD_HEADER(INGAME_SHIP_REQUEST);
1778                 ADD_DATA(code);
1779                 ADD_DATA(player_num);
1780                 ADD_DATA(Objects[rdata].net_signature);
1781                 for (i = 0; i < MAX_PLAYERS; i++ ) {
1782                         if(MULTI_CONNECTED(Net_players[i]) && (&Net_players[i] != Net_player) && (i != player_num) ) {                          
1783                                 multi_io_send_reliable(&Net_players[i], data, packet_size);
1784                         }
1785                 }
1786         }
1787 }
1788
1789 // function to validate all players in the game according to their team select index.  If discrepancies
1790 // are found, this function should be able to fix them up.
1791 void multi_ingame_validate_players()
1792 {
1793         int i;
1794
1795         for ( i = 0; i < MAX_PLAYERS; i++ ) {
1796                 if( MULTI_CONNECTED(Net_players[i]) && (Net_player != &Net_players[i]) && !MULTI_STANDALONE(Net_players[i]) ) {
1797                         char *ship_name;
1798                         int shipnum, objnum, player_objnum;
1799
1800                         player_objnum = Net_players[i].player->objnum;
1801                         if ( (Objects[player_objnum].type != OBJ_SHIP) || (Objects[player_objnum].type != OBJ_GHOST) ) {
1802                                 Int3();
1803                         }
1804
1805                         ship_name = multi_ts_get_shipname( Net_players[i].p_info.team, Net_players[i].p_info.ship_index );
1806                         Assert( ship_name != NULL );
1807                         shipnum = ship_name_lookup( ship_name );
1808                         if ( shipnum == -1 ) {
1809                                 // ship could be respawning
1810                                 continue;
1811                         }
1812                         objnum = Ships[shipnum].objnum;
1813                         Assert( objnum != -1 );
1814
1815                         // if this guy's objnum isn't a ship, then it should proably be a ghost!!
1816                         if ( Objects[objnum].type == OBJ_SHIP ) {
1817                                 if ( objnum != Net_players[i].player->objnum ) {
1818                                         Int3();
1819                                         Net_players[i].player->objnum = objnum;
1820                                 }
1821                         } else {
1822                                 Assert( Objects[objnum].type == OBJ_GHOST );
1823                         }
1824                 }
1825         }
1826 }
1827
1828 // process an ingame ship request packet
1829 void process_ingame_ship_request_packet(ubyte *data, header *hinfo)
1830 {
1831         int code;
1832         object *objp;
1833         int offset = HEADER_LENGTH;
1834         int team, slot_index, i;
1835         uint respawn_count;
1836         ubyte val, num_secondary_banks;
1837         p_object *pobj;
1838
1839         // get the code
1840         GET_DATA(code);
1841
1842         switch(code){
1843         // a request for a ship from an ingame joiner
1844         case INGAME_SR_REQUEST:                 
1845                 int player_num;         
1846                 ushort sig_request;
1847
1848                 // lookup the player and make sure he doesn't already have an objnum (along with possible error conditions)
1849                 GET_DATA(sig_request);
1850                 PACKET_SET_SIZE();
1851                         
1852                 player_num = find_player_id(hinfo->id); 
1853                 if(player_num == -1){
1854                         nprintf(("Network","Received ingame ship request packet from unknown player!!\n"));             
1855                         break;
1856                 }
1857                 
1858                 // make sure this player doesn't already have an object
1859                 Assert(MULTI_CONNECTED(Net_players[player_num]));
1860                 if(Net_players[player_num].player->objnum != -1){
1861                         send_ingame_ship_request_packet(INGAME_SR_DENY,0,&Net_players[player_num]);
1862                         break;
1863                 }
1864                 
1865                 // try and find the object
1866                 objp = NULL;
1867                 objp = multi_get_network_object(sig_request);
1868                 if(objp == NULL || !(objp->flags & OF_COULD_BE_PLAYER)){
1869                         send_ingame_ship_request_packet(INGAME_SR_DENY,0,&Net_players[player_num]);
1870                         break;
1871                 }               
1872
1873                 // Assign the player this objnum and ack him
1874                 Net_players[player_num].player->objnum = OBJ_INDEX(objp);
1875                 Net_players[player_num].state = NETPLAYER_STATE_IN_MISSION;                   // since he'll do this anyway...
1876                 Net_players[player_num].flags &= ~(NETINFO_FLAG_INGAME_JOIN);
1877                 multi_assign_player_ship( player_num, objp, Ships[objp->instance].ship_info_index );
1878
1879                 // update his ets and link status stuff
1880                 multi_server_update_player_weapons(&Net_players[player_num],&Ships[objp->instance]);
1881
1882                 objp->flags &= ~(OF_COULD_BE_PLAYER);
1883                 objp->flags |= OF_PLAYER_SHIP;
1884
1885                 // send a player settings packet to update all other players of this guy's final choices
1886                 send_player_settings_packet();
1887
1888                 // initialize datarate limiting for this guy
1889                 multi_oo_rate_init(&Net_players[player_num]);
1890                 
1891                 // ack him
1892                 send_ingame_ship_request_packet(INGAME_SR_CONFIRM,OBJ_INDEX(objp),&Net_players[player_num]);
1893
1894                 // clear my ingame join flag so that others may join
1895                 Netgame.flags &= ~NG_FLAG_INGAME_JOINING;
1896
1897                 // clear his net stats
1898                 scoring_level_init( &(Net_players[player_num].player->stats) );
1899                 break;
1900
1901                 // a denial for the ship we requested from the server
1902         case INGAME_SR_DENY :
1903                 PACKET_SET_SIZE();
1904
1905                 // set this to -1 so we can pick again
1906                 Multi_ingame_join_sig = 0;
1907
1908                 // display a popup
1909                 popup(PF_BODY_BIG | PF_USE_AFFIRMATIVE_ICON,1,POPUP_OK,XSTR("You have been denied the requested ship",686));
1910                 break;
1911
1912         // a confirmation that we can use the selected ship
1913         case INGAME_SR_CONFIRM:
1914                 // object *temp_objp;
1915
1916                 // delete the ship this ingame joiner was using.  Unassign Player_obj so that this object
1917                 // doesn't become a ghost.
1918                 // temp_objp = Player_obj;
1919                 // Player_obj = NULL;
1920                 // obj_delete( OBJ_INDEX(temp_objp) );
1921
1922                 // get the object itself
1923                 objp = multi_get_network_object(Multi_ingame_join_sig);
1924                 Assert(objp != NULL);
1925
1926                 // get its most recent position and orientation
1927                 GET_DATA(objp->pos);
1928                 GET_ORIENT(objp->orient);
1929                 GET_DATA( Missiontime );
1930                 GET_DATA( respawn_count );
1931                                 
1932                 // tell the server I'm in the mission
1933                 Net_player->state = NETPLAYER_STATE_IN_MISSION;
1934                 send_netplayer_update_packet();
1935
1936                 // setup our object                             
1937                 Net_player->player->objnum = OBJ_INDEX(objp);                   
1938                 Player_obj = objp;
1939                 Player_obj->flags &= ~(OF_COULD_BE_PLAYER);
1940                 Player_obj->flags |= OF_PLAYER_SHIP;
1941                 multi_assign_player_ship( MY_NET_PLAYER_NUM, objp, Ships[objp->instance].ship_info_index );
1942
1943                 // must change the ship type and weapons.  An ingame joiner know about the default class
1944                 // and weapons for a ship, but these could have changed.
1945                 multi_ts_get_team_and_slot(Player_ship->ship_name, &team, &slot_index);
1946                 Assert( team != -1 );
1947                 Assert( slot_index != -1 );
1948                 change_ship_type(objp->instance, Wss_slots_teams[team][slot_index].ship_class);
1949                 wl_bash_ship_weapons(&Player_ship->weapons,&Wss_slots_teams[team][slot_index]);
1950
1951                 // get the parse object for it and assign the respawn count
1952                 pobj = mission_parse_get_arrival_ship( objp->net_signature );
1953                 Assert(pobj != NULL);
1954                 pobj->respawn_count = respawn_count;
1955
1956                 // get the ships ets settings
1957                 GET_DATA(val);
1958                 Player_ship->weapon_recharge_index = val;
1959                 GET_DATA(val);
1960                 Player_ship->shield_recharge_index = val;
1961                 GET_DATA(val);
1962                 Player_ship->engine_recharge_index = val;               
1963
1964                 // get current primary and secondary banks, and add link status
1965                 GET_DATA(val);
1966                 Player_ship->weapons.current_primary_bank = val;
1967                 GET_DATA(val);
1968                 Player_ship->weapons.current_secondary_bank = val;                              
1969
1970                 // secondary bank ammo data
1971                 GET_DATA( num_secondary_banks );
1972                 Assert( num_secondary_banks == Player_ship->weapons.num_secondary_banks );
1973                 for ( i = 0; i < Player_ship->weapons.num_secondary_banks; i++ ) {
1974                         GET_DATA(val);
1975                         Player_ship->weapons.secondary_bank_ammo[i] = val;
1976                 }
1977
1978
1979                 // get the link status of weapons
1980                 GET_DATA(val);
1981                 if(val & (1<<0)){
1982                         Player_ship->flags |= SF_PRIMARY_LINKED;
1983                 }
1984                 if(val & (1<<1)){
1985                         Player_ship->flags |= SF_SECONDARY_DUAL_FIRE;
1986                 }               
1987                 PACKET_SET_SIZE();                                      
1988
1989                 // be sure that this ships current primary/secondary weapons are valid.  Easiest is to just
1990                 // bash the values to 0!
1991                 /*
1992                 if ( Player_ship->weapons.current_primary_bank == -1 )
1993                         Player_ship->weapons.current_primary_bank = 0;
1994                 if ( Player_ship->weapons.current_secondary_bank == -1 )
1995                         Player_ship->weapons.current_secondary_bank = 0;
1996                 */
1997                 
1998                 Net_player->flags &= ~(NETINFO_FLAG_INGAME_JOIN);
1999
2000                 // clear all object collision pairs, then add myself to the list
2001                 extern void obj_reset_all_collisions();
2002                 obj_reset_all_collisions();
2003                 // obj_reset_pairs();
2004                 // obj_add_pairs( OBJ_INDEX(Player_obj) );
2005
2006                 mission_hotkey_set_defaults();
2007
2008                 //multi_ingame_validate_players();
2009
2010                 // jump into the mission 
2011                 // NOTE : we check this flag because its possible that the player could have received an endgame packet in the same
2012                 //        frame as getting this confirmation. In that case, he should be quitting to the main menu. We must not make
2013                 //        him continue on into the mission
2014                 if(!multi_endgame_ending()){
2015                         gameseq_post_event(GS_EVENT_ENTER_GAME);                                                                
2016                 } 
2017                 break;
2018
2019         case INGAME_PLAYER_CHOICE: {
2020                 int player_num;
2021                 ushort net_signature;
2022                 object *objp;
2023
2024                 // get the player number of this guy, and the net signature of the ship he has chosen
2025                 GET_DATA(player_num);
2026                 GET_DATA(net_signature);
2027                 PACKET_SET_SIZE();
2028
2029                 objp = multi_get_network_object(net_signature);
2030                 if ( objp == NULL ) {
2031                         // bogus!!!  couldn't find the object -- we cannot connect his -- this is really bad!!!
2032                         nprintf(("Network", "Couldn't find ship for ingame joiner %s\n", Net_players[player_num].player->callsign));
2033                         break;
2034                 }
2035                 objp->flags |= OF_PLAYER_SHIP;
2036                 objp->flags &= ~OF_COULD_BE_PLAYER;
2037
2038                 multi_assign_player_ship( player_num, objp, Ships[objp->instance].ship_info_index );
2039
2040                 break;
2041                 }
2042         }
2043 }
2044
2045
2046 // --------------------------------------------------------------------------------------------------
2047 // INGAME JOIN FORWARD DEFINITIONS
2048 //
2049
2050 void multi_ingame_send_ship_update(net_player *p)
2051 {
2052         ship_obj *moveup;
2053         
2054         // get the first object on the list
2055         moveup = GET_FIRST(&Ship_obj_list);
2056         
2057         // go through the list and send all ships which are mark as OF_COULD_BE_PLAYER
2058         while(moveup!=END_OF_LIST(&Ship_obj_list)){
2059                 if(Objects[moveup->objnum].flags & OF_COULD_BE_PLAYER){
2060                         // send the update
2061                         send_ingame_ship_update_packet(p,&Ships[Objects[moveup->objnum].instance]);
2062                 }
2063
2064                 // move to the next item
2065                 moveup = GET_NEXT(moveup);
2066         }
2067 }
2068
2069 // for now, I guess we'll just send hull and shield % values
2070 void send_ingame_ship_update_packet(net_player *p,ship *sp)
2071 {
2072         ubyte data[MAX_PACKET_SIZE];
2073         object *objp;
2074         int idx;
2075         int packet_size = 0;
2076         float f_tmp;
2077
2078         BUILD_HEADER(INGAME_SHIP_UPDATE);
2079         
2080         // just send net signature, shield and hull percentages
2081         objp = &Objects[sp->objnum];
2082         ADD_DATA(objp->net_signature);
2083         ADD_DATA(objp->flags);
2084         ADD_DATA(objp->hull_strength);
2085         
2086         // shield percentages
2087         for(idx=0; idx<MAX_SHIELD_SECTIONS; idx++){
2088                 f_tmp = objp->shields[idx];
2089                 ADD_DATA(f_tmp);
2090         }
2091         
2092         multi_io_send_reliable(p, data, packet_size);
2093 }
2094
2095 void process_ingame_ship_update_packet(ubyte *data, header *hinfo)
2096 {
2097         int offset;
2098         float garbage;
2099         int flags;
2100         int idx;
2101         ushort net_sig;
2102         object *lookup;
2103         float f_tmp;
2104         
2105         offset = HEADER_LENGTH;
2106         // get the net sig for the ship and do a lookup
2107         GET_DATA(net_sig);
2108         GET_DATA(flags);
2109    
2110         // get the object
2111         lookup = multi_get_network_object(net_sig);
2112         if(lookup == NULL){
2113                 // read in garbage values if we can't find the ship
2114                 nprintf(("Network","Got ingame ship update for unknown object\n"));
2115                 GET_DATA(garbage);
2116                 for(idx=0;idx<MAX_SHIELD_SECTIONS;idx++){
2117                         GET_DATA(garbage);
2118                 }
2119
2120                 PACKET_SET_SIZE();
2121                 return;
2122         }
2123         // otherwise read in the ship values
2124         lookup->flags = flags;
2125         GET_DATA(lookup->hull_strength);
2126         for(idx=0;idx<MAX_SHIELD_SECTIONS;idx++){
2127                 GET_DATA(f_tmp);
2128                 lookup->shields[idx] = f_tmp;
2129         }
2130
2131         PACKET_SET_SIZE();
2132 }
2133