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