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