]> icculus.org git repositories - taylor/freespace2.git/blob - src/network/multi_dogfight.cpp
safer strings using SDL string functions
[taylor/freespace2.git] / src / network / multi_dogfight.cpp
1 /*
2  * Copyright (C) Volition, Inc. 1999.  All rights reserved.
3  *
4  * All source code herein is the property of Volition, Inc. You may not sell 
5  * or otherwise commercially exploit the source or things you created based on
6  * the source.
7  */
8
9 /*
10  * $Logfile: /Freespace2/code/Network/multi_dogfight.cpp $
11  * $Revision$
12  * $Date$
13  * $Author$
14  * 
15  * $Log$
16  * Revision 1.4  2004/09/20 01:31:44  theoddone33
17  * GCC 3.4 fixes.
18  *
19  * Revision 1.3  2002/06/09 04:41:23  relnev
20  * added copyright header
21  *
22  * Revision 1.2  2002/05/07 03:16:47  theoddone33
23  * The Great Newline Fix
24  *
25  * Revision 1.1.1.1  2002/05/03 03:28:10  root
26  * Initial import.
27  *
28  * 
29  * 11    8/18/99 11:31a Jefff
30  * mission title on kill matrix
31  * 
32  * 10    8/17/99 9:55a Dave
33  * Fixed dogfight scoring problem.
34  * 
35  * 9     8/06/99 12:29a Dave
36  * Multiple bug fixes.
37  * 
38  * 8     5/03/99 8:32p Dave
39  * New version of multi host options screen.
40  * 
41  * 7     4/12/99 10:07p Dave
42  * Made network startup more forgiving. Added checkmarks to dogfight
43  * screen for players who hit commit.
44  * 
45  * 6     4/12/99 2:22p Dave
46  * More checks for dogfight stats.
47  * 
48  * 5     2/24/99 3:40p Dave
49  * Fixed problem where ships were becoming traitor all the time.
50  * 
51  * 4     2/24/99 2:25p Dave
52  * Fixed up chatbox bugs. Made squad war reporting better. Fixed a respawn
53  * bug for dogfight more.
54  * 
55  * 3     2/23/99 8:11p Dave
56  * Tidied up dogfight mode. Fixed TvT ship type problems for alpha wing.
57  * Small pass over todolist items.
58  * 
59  * 2     2/23/99 2:29p Dave
60  * First run of oldschool dogfight mode. 
61  *   
62  * $NoKeywords: $
63  */
64
65 #include "multi.h"
66 #include "multiutil.h"
67 #include "multi_log.h"
68 #include "bmpman.h"
69 #include "object.h"
70 #include "systemvars.h"
71 #include "freespace.h"
72 #include "key.h"
73 #include "missionscreencommon.h"
74 #include "eventmusic.h"
75 #include "gamesnd.h"
76 #include "multiui.h"
77 #include "chatbox.h"
78 #include "ui.h"
79 #include "font.h"
80 #include "multi_dogfight.h"
81 #include "alphacolors.h"
82
83 // ----------------------------------------------------------------------------------------------------
84 // MULTI DOGFIGHT DEFINES/VARS
85 //
86
87 // interface stuff
88 UI_WINDOW Multi_df_window;
89
90 #define NUM_MULTI_DF_BUTTONS                    1
91 #define ACCEPT_BUTTON                                   0
92
93 ui_button_info Multi_df_buttons[GR_NUM_RESOLUTIONS][NUM_MULTI_DF_BUTTONS] = {
94         { // GR_640
95                 // accept
96                 ui_button_info("CB_05a",        571,    425,    578,    413,    5),
97         },      
98         { // GR_1024
99                 // accept
100                 ui_button_info("2_CB_05a",      914,    681,    914,    660,    5),
101         }
102 };
103
104 int Multi_df_background_bitmap = -1;
105 const char *Multi_df_background_fname[GR_NUM_RESOLUTIONS] = {
106         "KillMatrix",
107         "2_KillMatrix"
108 };
109 const char *Multi_df_mask_fname[GR_NUM_RESOLUTIONS] = {
110         "KillMatrix-m",
111         "2_KillMatrix-m"
112 };
113
114 // coord 3 is max width
115 static int Kill_matrix_title_coords[GR_NUM_RESOLUTIONS][3] = {
116         {       // GR_640
117                 19, 118, 172
118         },
119         {       // GR_1024
120                 33, 194, 272
121         }
122 };
123
124 // display area coords
125 int Multi_df_display_coords[GR_NUM_RESOLUTIONS][4] = {
126         { // GR_640
127                 43, 133, 569, 269
128         },
129         { // GR_1024
130                 60, 213, 919, 429
131         }
132 };
133
134 #define MULTI_DF_TOTAL_ADJUST                           5
135
136 // "check" icon coords
137 int Multi_df_check_coords[GR_NUM_RESOLUTIONS] = {
138         // GR_640
139         28,     
140
141         // GR_1024
142         45      
143 };
144
145 // players when the screen started - we need to store this explicity so that even after players leave, we can display the full kill matrix
146 typedef struct multi_df_score {
147         char callsign[CALLSIGN_LEN+1];          // callsign for this guy        
148         scoring_struct stats;                                   // stats for the guy    
149         int np_index;                                                           // absolute index into the netplayers array
150 } multi_df_score;
151 multi_df_score Multi_df_score[MAX_PLAYERS];
152 int Multi_df_score_count = 0;
153
154
155 // ----------------------------------------------------------------------------------------------------
156 // MULTI DOGFIGHT FORWARD DECLARATIONS
157 //
158
159 // process button presses
160 void multi_df_process_buttons();
161
162 // button was pressed
163 void multi_df_button_pressed(int button);
164
165 // setup kill matrix data
166 void multi_df_setup_kill_matrix();
167
168 // blit the kill matrix
169 void multi_df_blit_kill_matrix();
170
171 // stuff a string representing the # of kills, player X had on player Y (where X and Y are indices into Multi_df_score)
172 // returns the # of kills
173 int multi_df_stuff_kills(char *kills, const int max_klen, int player_x, int player_y);
174
175
176 // ----------------------------------------------------------------------------------------------------
177 // MULTI DOGFIGHT FUNCTIONS
178 //
179
180 // call once per level just before entering the mission
181 void multi_df_level_pre_enter()
182 {                       
183         int idx;
184
185         // if we're not in dogfight mode, do nothing
186         if(!(Netgame.type_flags & NG_TYPE_DOGFIGHT)){
187                 return;
188         }
189
190         // go through all player ships and make them hostile
191         for(idx=0; idx<MAX_PLAYERS; idx++){
192                 if(MULTI_CONNECTED(Net_players[idx]) && !MULTI_STANDALONE(Net_players[idx]) && !MULTI_OBSERVER(Net_players[idx]) && (Net_players[idx].player != NULL) && (Net_players[idx].player->objnum >= 0) && (Objects[Net_players[idx].player->objnum].type == OBJ_SHIP)){
193                         Ships[Objects[Net_players[idx].player->objnum].instance].team = TEAM_TRAITOR;
194                 }
195         }
196
197         // 
198 }
199
200 // evaluate a kill in dogfight by a netplayer
201 void multi_df_eval_kill(net_player *killer, object *dead_obj)
202 {
203         int dead_index = -1;
204         
205         // if we're not in dogfight mode, do nothing
206         if(!(Netgame.type_flags & NG_TYPE_DOGFIGHT)){
207                 return;
208         }
209
210         // sanity checks
211         if((killer == NULL) || (dead_obj == NULL) || (killer->player == NULL)){
212                 return;
213         }
214         
215         // try and find the dead player
216         dead_index = multi_find_player_by_object(dead_obj);
217         if(dead_index < 0){
218                 return;
219         }
220         SDL_assert(dead_index < MAX_PLAYERS);
221         if(dead_index == NET_PLAYER_INDEX(killer)){
222                 return;
223         }
224
225         // update his kills
226         killer->player->stats.m_dogfight_kills[dead_index]++;
227 }
228
229 // debrief
230 void multi_df_debrief_init()
231 {
232         // no longer is mission
233         Game_mode &= ~(GM_IN_MISSION);  
234         game_flush();
235
236         // call scoring level close for my stats.  Needed for award_init.  The stats will
237         // be backed out if used chooses to replace them.
238         scoring_level_close();
239
240         // multiplayer debriefing stuff
241         multi_debrief_init();
242
243         // close down any old instances of the chatbox
244         chatbox_close();
245
246         // create the new one
247         chatbox_create();
248
249         // always play success music
250         common_music_init(SCORE_DEBRIEF_SUCCESS);
251
252         // setup kill matrix
253         multi_df_setup_kill_matrix();
254
255         UI_WINDOW *w;
256         ui_button_info *b;
257         int idx;
258
259         // load background bitmap
260         Multi_df_background_bitmap = bm_load(Multi_df_background_fname[gr_screen.res]);
261         SDL_assert(Multi_df_background_bitmap);
262
263         // create the UI window
264         Multi_df_window.create(0, 0, gr_screen.max_w, gr_screen.max_h, 0);
265         Multi_df_window.set_mask_bmap(Multi_df_mask_fname[gr_screen.res]);
266         
267         // initialize the control buttons
268         for (idx=0; idx<NUM_MULTI_DF_BUTTONS; idx++) {
269                 b = &Multi_df_buttons[gr_screen.res][idx];
270
271                 // create the button
272                 b->button.create(&Multi_df_window, NULL, b->x, b->y, 60, 30, 1, 1);
273                 
274                 // set its highlight action
275                 b->button.set_highlight_action(common_play_highlight_sound);
276
277                 // set its animation bitmaps
278                 b->button.set_bmaps(b->filename);
279
280                 // link the mask hotspot
281                 b->button.link_hotspot(b->hotspot);
282         }               
283
284         // add some text
285         w = &Multi_df_window;   
286         w->add_XSTR("Accept", 1035, Multi_df_buttons[gr_screen.res][ACCEPT_BUTTON].xt, Multi_df_buttons[gr_screen.res][ACCEPT_BUTTON].yt, &Multi_df_buttons[gr_screen.res][ACCEPT_BUTTON].button, UI_XSTR_COLOR_PINK);
287 }
288
289 // do frame
290 void multi_df_debrief_do()
291 {
292         int k, new_k;
293         char buf[256];
294         
295         k = chatbox_process();  
296         new_k = Multi_df_window.process(k, 0);  
297
298         // process keypresses
299         switch(new_k){
300         case SDLK_ESCAPE:
301                 multi_debrief_esc_hit();
302                 break;
303         }
304
305         // process buttons      
306         multi_df_process_buttons();
307
308         // music stuff
309         common_music_do();
310
311         // process debriefing details
312         multi_debrief_do_frame();
313
314         // draw the background
315         GR_MAYBE_CLEAR_RES(Multi_df_background_bitmap);
316         if (Multi_df_background_bitmap >= 0) {
317                 gr_set_bitmap(Multi_df_background_bitmap, GR_ALPHABLEND_NONE, GR_BITBLT_MODE_NORMAL, 1.0f, -1, -1);
318                 gr_bitmap(0, 0);
319         } 
320
321         // draw the window
322         Multi_df_window.draw(); 
323
324         // kill matrix
325         multi_df_blit_kill_matrix();
326
327         // render the chatbox
328         chatbox_render();
329
330         // draw the mission title
331         SDL_strlcpy(buf, The_mission.name, sizeof(buf));
332         gr_force_fit_string(buf, 255, Kill_matrix_title_coords[gr_screen.res][2]);
333         gr_set_color_fast(&Color_bright_white);
334         gr_string(Kill_matrix_title_coords[gr_screen.res][0], Kill_matrix_title_coords[gr_screen.res][1], buf);
335
336         // flip
337         gr_flip();
338 }
339
340 // close
341 void multi_df_debrief_close()
342 {
343         int idx;
344
345         // shutdown the chatbox
346         chatbox_close();
347
348         // if stats weren't accepted, backout my own stats
349         if (multi_debrief_stats_accept_code() != 1) {           
350                 // if stats weren't accepted, backout my own stats
351                 if (multi_debrief_stats_accept_code() != 1) {
352                         if(MULTIPLAYER_MASTER){
353                                 for(idx=0; idx<MAX_PLAYERS; idx++){
354                                         if(MULTI_CONNECTED(Net_players[idx]) && !MULTI_STANDALONE(Net_players[idx]) && !MULTI_PERM_OBSERVER(Net_players[idx]) && (Net_players[idx].player != NULL)){
355                                                 scoring_backout_accept(&Net_players[idx].player->stats);
356                                         }
357                                 }
358                         } else {
359                                 scoring_backout_accept( &Player->stats );
360                         }
361                 }
362         }
363
364         // music stuff
365         common_music_close();
366 }
367
368
369 // ----------------------------------------------------------------------------------------------------
370 // MULTI DOGFIGHT FORWARD DEFINITIONS
371 //
372
373 // process button presses
374 void multi_df_process_buttons()
375 {
376         int idx;
377
378         for(idx=0; idx<NUM_MULTI_DF_BUTTONS; idx++){
379                 if(Multi_df_buttons[gr_screen.res][idx].button.pressed()){
380                         multi_df_button_pressed(idx);
381                         break;
382                 }
383         }
384 }
385
386 // button was pressed
387 void multi_df_button_pressed(int button)
388 {
389         switch(button){
390         case ACCEPT_BUTTON:
391                 multi_debrief_accept_hit();
392                 break;
393         }
394 }
395
396 // setup kill matrix data
397 void multi_df_setup_kill_matrix()
398 {
399         int idx, s_idx;
400         multi_df_score *s;
401
402         Multi_df_score_count = 0;
403
404         // add players as necessary
405         for(idx=0; idx<MAX_PLAYERS; idx++){
406                 if(MULTI_CONNECTED(Net_players[idx]) && !MULTI_STANDALONE(Net_players[idx]) && !MULTI_PERM_OBSERVER(Net_players[idx]) && (Net_players[idx].player != NULL)){
407                         // stuff data for this guy
408                         s = &Multi_df_score[Multi_df_score_count++];
409
410                         ml_printf("Dogfight debrief stats for %s", Net_players[idx].player->callsign);
411                         for(s_idx=0; s_idx<MAX_PLAYERS; s_idx++){
412                                 ml_printf("%d", Net_players[idx].player->stats.m_dogfight_kills[s_idx]);
413                         }
414
415                         s->stats = Net_players[idx].player->stats;
416                         SDL_strlcpy(s->callsign, Net_players[idx].player->callsign, sizeof(s->callsign));
417                         s->np_index = idx;
418                 }
419         }
420 }
421
422 // blit the kill matrix
423 void multi_df_blit_kill_matrix()
424 {
425         int idx, s_idx, str_len;
426         int cx, cy;
427         char squashed_string[CALLSIGN_LEN+1] = "";
428
429         // max width of an individual item, and the text that can be in that item
430         float max_item_width = ((float)Multi_df_display_coords[gr_screen.res][2] - 40.0f) / (float)(Multi_df_score_count + 1);
431         float max_text_width = max_item_width * 0.8f;
432
433         // start x for the top bar (one item to the right)
434         int top_x_start = Multi_df_display_coords[gr_screen.res][0] + (int)max_item_width;
435         int top_y_start = Multi_df_display_coords[gr_screen.res][1];    
436
437         // start x for the side bar
438         int side_x_start = Multi_df_display_coords[gr_screen.res][0];
439         int side_y_start = Multi_df_display_coords[gr_screen.res][1] + 10;
440
441         // draw the top bar
442         cx = top_x_start;
443         cy = top_y_start;
444         for(idx=0; idx<Multi_df_score_count; idx++){            
445                 // force the string to fit nicely
446                 SDL_strlcpy(squashed_string, Multi_df_score[idx].callsign, sizeof(squashed_string));
447                 gr_force_fit_string(squashed_string, CALLSIGN_LEN, (int)max_text_width);
448                 gr_get_string_size(&str_len, NULL, squashed_string);
449
450                 // set color and blit the string                
451                 SDL_assert(Multi_df_score[idx].np_index >= 0);
452                 if(Multi_df_score[idx].np_index >= 0){
453                         gr_set_color_fast(Color_netplayer[Multi_df_score[idx].np_index]);
454                 }
455                 gr_string(cx + (int)((max_item_width - (float)str_len)/2.0f), cy, squashed_string);
456
457                 // next spot
458                 cx += (int)max_item_width;
459         }
460
461         // draw the rest of the scoreboard      
462         cx = side_x_start;
463         cy = side_y_start;
464         int row_total;
465         for(idx=0; idx<Multi_df_score_count; idx++){            
466                 // draw a check if necessary
467                 if(!MULTI_CONNECTED(Net_players[Multi_df_score[idx].np_index]) || (Net_players[Multi_df_score[idx].np_index].state == NETPLAYER_STATE_DEBRIEF_ACCEPT) || (Net_players[Multi_df_score[idx].np_index].state == NETPLAYER_STATE_DEBRIEF_REPLAY)){
468                         if(Multi_common_icons[MICON_VALID] != -1){
469                                 gr_set_bitmap(Multi_common_icons[MICON_VALID], GR_ALPHABLEND_NONE, GR_BITBLT_MODE_NORMAL, 1.0f, -1, -1);
470                                 gr_bitmap(Multi_df_check_coords[gr_screen.res], cy);
471                         }
472                 }
473
474                 // draw the name
475                 cx = Multi_df_display_coords[gr_screen.res][0];
476                 SDL_strlcpy(squashed_string, Multi_df_score[idx].callsign, sizeof(squashed_string));
477                 gr_force_fit_string(squashed_string, CALLSIGN_LEN, (int)max_text_width);
478                 gr_get_string_size(&str_len, NULL, squashed_string);            
479                 SDL_assert(Multi_df_score[idx].np_index >= 0);
480                 if(Multi_df_score[idx].np_index >= 0){
481                         gr_set_color_fast(Color_netplayer[Multi_df_score[idx].np_index]);
482                 }
483                 gr_string(cx, cy, squashed_string);
484
485                 cx = top_x_start;
486                 row_total = 0;
487                 for(s_idx=0; s_idx<Multi_df_score_count; s_idx++){
488                         // stuff the string to be displayed and select the proper display color
489                         if(s_idx == idx){
490                                 SDL_strlcpy(squashed_string, "-", sizeof(squashed_string));
491                                 gr_set_color_fast(&Color_grey);
492                         } else {
493                                 row_total += multi_df_stuff_kills(squashed_string, sizeof(squashed_string), idx, s_idx);
494                                 SDL_assert(Multi_df_score[idx].np_index >= 0);
495                                 if(Multi_df_score[idx].np_index >= 0){
496                                         gr_set_color_fast(Color_netplayer[Multi_df_score[idx].np_index]);
497                                 }                               
498                         }                                               
499
500                         // draw the string
501                         gr_force_fit_string(squashed_string, CALLSIGN_LEN, (int)max_text_width);
502                         gr_get_string_size(&str_len, NULL, squashed_string);
503                         gr_string(cx + (int)((max_item_width - (float)str_len)/2.0f), cy, squashed_string);
504
505                         // next spot
506                         cx += (int)max_item_width;
507                 }
508
509                 // draw the row total
510                 gr_set_color_fast(Color_netplayer[Multi_df_score[idx].np_index]);
511                 SDL_snprintf(squashed_string, sizeof(squashed_string), "(%d)", row_total);
512                 gr_get_string_size(&str_len, NULL, squashed_string);
513                 gr_string(Multi_df_display_coords[gr_screen.res][0] + Multi_df_display_coords[gr_screen.res][2] - (MULTI_DF_TOTAL_ADJUST + str_len), cy, squashed_string);
514
515                 cy += 10;
516         }
517
518         /*
519         // blit totals
520         int column_total;
521         cx = top_x_start;
522         cy += 3;
523         for(idx=0; idx<Multi_df_score_count; idx++){
524                 // add up this column
525                 column_total = 0;
526                 for(s_idx=0; s_idx<Multi_df_score_count; s_idx++){
527                         // add                  
528                         column_total += Multi_df_score[idx].stats.m_dogfight_kills[s_idx];
529                 }
530
531                 // draw
532                 sprintf(squashed_string, "%d", column_total);
533                 gr_set_color_fast(Color_netplayer[Multi_df_score[idx].np_index]);
534                 gr_string(cx, cy, squashed_string);
535
536                 // next item
537                 cx += (int)max_item_width;
538         }
539         */
540 }
541
542 // stuff a string representing the # of kills, player X had on player Y (where X and Y are indices into Multi_df_score)
543 // returns the # of kills
544 int multi_df_stuff_kills(char *kills, const int max_klen, int player_x, int player_y)
545 {
546         multi_df_score *s = &Multi_df_score[player_x];
547         SDL_strlcpy(kills, "", max_klen);
548         
549         SDL_snprintf(kills, sizeof(max_klen), "%d", s->stats.m_dogfight_kills[Multi_df_score[player_y].np_index]);
550         return s->stats.m_dogfight_kills[Multi_df_score[player_y].np_index];
551 }