2 * $Logfile: /Freespace2/code/Network/multi_pmsg.cpp $
8 * Revision 1.1 2002/05/03 03:28:10 root
12 * 7 3/10/99 6:50p Dave
13 * Changed the way we buffer packets for all clients. Optimized turret
14 * fired packets. Did some weapon firing optimizations.
16 * 6 3/09/99 6:24p Dave
17 * More work on object update revamping. Identified several sources of
18 * unnecessary bandwidth.
20 * 5 2/24/99 2:25p Dave
21 * Fixed up chatbox bugs. Made squad war reporting better. Fixed a respawn
22 * bug for dogfight more.
24 * 4 11/19/98 8:03a Dave
25 * Full support for D3-style reliable sockets. Revamped packet lag/loss
26 * system, made it receiver side and at the lowest possible level.
28 * 3 11/17/98 11:12a Dave
29 * Removed player identification by address. Now assign explicit id #'s.
31 * 2 10/07/98 10:53a Dave
34 * 1 10/07/98 10:50a Dave
36 * 18 6/13/98 9:32p Mike
37 * Kill last character in file which caused "Find in Files" to report the
38 * file as "not a text file."
40 * 17 6/13/98 6:01p Hoffoss
41 * Externalized all new (or forgot to be added) strings to all the code.
43 * 16 6/13/98 3:19p Hoffoss
44 * NOX()ed out a bunch of strings that shouldn't be translated.
46 * 15 5/13/98 6:54p Dave
47 * More sophistication to PXO interface. Changed respawn checking so
48 * there's no window for desynchronization between the server and the
51 * 14 5/08/98 5:05p Dave
52 * Go to the join game screen when quitting multiplayer. Fixed mission
53 * text chat bugs. Put mission type symbols on the create game list.
54 * Started updating standalone gui controls.
56 * 13 5/02/98 5:38p Dave
57 * Put in new tracker API code. Put in ship information on mp team select
58 * screen. Make standalone server name permanent. Fixed standalone server
61 * 12 4/23/98 6:18p Dave
62 * Store ETS values between respawns. Put kick feature in the text
63 * messaging system. Fixed text messaging system so that it doesn't
64 * process or trigger ship controls. Other UI fixes.
66 * 11 4/22/98 4:59p Allender
67 * new multiplayer dead popup. big changes to the comm menu system for
68 * team vs. team. Start of debriefing stuff for team vs. team Make form
69 * on my wing work with individual ships who have high priority orders
71 * 10 4/16/98 6:35p Dave
72 * Display more informative prompt when typing in a text message.
74 * 9 4/06/98 10:24p Dave
75 * Fixed up Netgame.respawn for the standalone case.
77 * 8 4/05/98 3:30p Dave
78 * Print netplayer messages in brighter green on the hud, with
79 * accompanying sound. Echo netplayer messages on sending machine. Fixed
80 * standalone sequencing bug where host never get the "enter mission"
83 * 7 4/04/98 4:22p Dave
84 * First rev of UDP reliable sockets is done. Seems to work well if not
87 * 6 4/03/98 1:03a Dave
88 * First pass at unreliable guaranteed delivery packets.
90 * 5 4/02/98 5:50p Dave
91 * Put in support for standard comm messages to get sent to netplayers as
92 * well as ai ships. Make critical button presses not get evaluated on the
95 * 4 4/01/98 5:56p Dave
96 * Fixed a messaging bug which caused msg_all mode in multiplayer not to
97 * work. Compile out a host of multiplayer options not available in the
100 * 3 3/27/98 11:57a Dave
101 * Put in expression checking for text messages.
103 * 2 3/25/98 2:16p Dave
104 * Select random default image for newly created pilots. Fixed several
105 * multi-pause messaging bugs. Begin work on online help for multiplayer
108 * 1 3/19/98 5:04p Dave
115 #include "multimsgs.h"
116 #include "multiutil.h"
117 #include "multi_pmsg.h"
118 #include "multi_kick.h"
121 #include "hudmessage.h"
122 #include "hudsquadmsg.h"
127 // ----------------------------------------------------------------------------------
128 // MULTI MESSAGING DEFINES/VARS
131 // if a key is down less than this time, fire up the test messaging system, otherwise fire up the voice messaging system
132 #define MULTI_MSG_KEYDOWN_WAIT 325 // in ms
134 // sound to play before displaying incoming text messages in-mission
135 #define MULTI_MSG_TEXT_SOUND SND_CUE_VOICE
137 // max length of a string we'll allow players to send
138 #define MULTI_MSG_MAX_LEN 75
140 // current message processing mode
141 int Multi_msg_mode = MULTI_MSG_NONE;
143 // timestamp for timing keydown
144 int Multi_msg_stamp = -1;
146 // flag indicating if there is _still_ a key down for the current message mode
147 int Multi_msg_repeat_flag = 0;
149 // timestamp set when we leave messaging mode, use to keep eating keys for a short period of time
150 int Multi_msg_eat_stamp = -1;
152 // text message input vars
153 int Multi_msg_text_enter = 0;
154 char Multi_msg_text[MULTI_MSG_MAX_TEXT_LEN+1];
156 // command defines - all these commands must be followed by a ":" so that we can easily tokenize and recognize
157 // it as a command instead of a word. they also must be immediately at the beginning of a text string
158 // SO : kick dave would not work
159 // kick: dave would work
160 // Finally, if no command is found but there is a ":", it uses the text before the : as an expression to
161 // lookup players to route the text to
162 #define MULTI_MSG_CMD_COUNT 1 // # of commands
163 #define MULTI_MSG_CMD_KICK 0 // kick command
166 char *Multi_msg_commands[MULTI_MSG_CMD_COUNT] = { // commands themselves
171 // process an entered line of text and maybe perform a command on it (return 1 if an action was performed, 0 if not)
172 int multi_msg_check_command(char *str);
174 // perform the passed command (MULTI_MSG_CMD_* define) with the passed string argument
175 void multi_msg_perform_command(int command,char *param);
178 // ----------------------------------------------------------------------------------
179 // MULTI MESSAGING FUNCTIONS
182 // called when a messaging key has been detected as being pressed
183 void multi_msg_key_down(int mode)
185 // keep eating keys for a short period of time
186 if((Multi_msg_eat_stamp != -1) && !timestamp_elapsed(Multi_msg_eat_stamp)){
190 // if our player flags are marked as being in msg mode, don't do anything
191 if(Player->flags & PLAYER_FLAGS_MSG_MODE){
195 // if there already is a keydown
196 if(Multi_msg_mode != MULTI_MSG_NONE){
197 // if it is the same as the current mode, set the "still down" flag
198 if((mode == Multi_msg_mode) && !Multi_msg_text_enter){
199 Multi_msg_repeat_flag = 1;
206 // otherwise set the message mode and set the timestamp
207 Multi_msg_mode = mode;
208 Multi_msg_repeat_flag = 1;
209 Multi_msg_stamp = timestamp(MULTI_MSG_KEYDOWN_WAIT);
212 // returns true when messaging system has determined that we should be messaging with voice
213 int multi_msg_voice_record()
215 return ((Multi_msg_mode != MULTI_MSG_NONE) && timestamp_elapsed(Multi_msg_stamp) && Multi_msg_repeat_flag && !Multi_msg_text_enter) ? 1 : 0;
218 // general processing function to do things like timing keydown, etc. call from multi_do_frame()
219 void multi_msg_process()
221 // keep eating keys for a short period of time
222 if((Multi_msg_eat_stamp != -1) && timestamp_elapsed(Multi_msg_eat_stamp)){
223 Multi_msg_eat_stamp = -1;
227 // if we don't currently have a valid mode set, don't do anything
228 if(Multi_msg_mode == MULTI_MSG_NONE){
232 // if the key has been released
233 if(!Multi_msg_repeat_flag && (Multi_msg_stamp != -1) && !Multi_msg_text_enter){
234 // if the timestamp had not yet elapsed, fire up the text messaging system
235 // this is the equivalent of a (TAP)
236 if(!timestamp_elapsed(Multi_msg_stamp) && !Multi_msg_text_enter){
237 // fire up text messaging system here
238 Multi_msg_text_enter = 1;
239 memset(Multi_msg_text,0,MULTI_MSG_MAX_TEXT_LEN+1);
241 Multi_msg_mode = MULTI_MSG_NONE;
242 Multi_msg_stamp = -1;
246 // unset the repeat flag every frame
247 Multi_msg_repeat_flag = 0;
250 // get the current messaging mode
253 return Multi_msg_mode;
256 // return 0 or 1 if in text chat mode or not
257 int multi_msg_text_mode()
259 return Multi_msg_text_enter;
262 // process a text string entered by the local player
263 void multi_msg_eval_text_msg()
267 // if its a 0 length string, don't do anything
268 if(strlen(Multi_msg_text) <= 0){
272 // evaluate any special commands here
273 if(multi_msg_check_command(Multi_msg_text)){
277 // get the player if in MSG_TARGET mode
278 if(Multi_msg_mode == MULTI_MSG_TARGET){
279 if(Player_ai->target_objnum != -1){
280 player_index = multi_find_player_by_object(&Objects[Player_ai->target_objnum]);
281 if(player_index != -1){
282 // send the chat packet
283 send_game_chat_packet(Net_player, Multi_msg_text, Multi_msg_mode, &Net_players[player_index]);
285 // echo the message locally
286 multi_msg_display_mission_text(Multi_msg_text, MY_NET_PLAYER_NUM);
292 // send the chat packet
293 send_game_chat_packet(Net_player, Multi_msg_text, Multi_msg_mode, NULL);
295 // echo the message locally
296 multi_msg_display_mission_text(Multi_msg_text, MY_NET_PLAYER_NUM);
300 // maybe process a keypress in text messaging mode, return true if the key was processed
301 int multi_msg_text_process(int k)
305 // keep eating keys for a short period of time
306 if((Multi_msg_eat_stamp != -1) && !timestamp_elapsed(Multi_msg_eat_stamp)){
310 // if we're not in text message mode, return 0
311 if(!Multi_msg_text_enter){
316 // cancel the message
318 multi_msg_text_flush();
323 multi_msg_eval_text_msg();
324 multi_msg_text_flush();
329 if(strlen(Multi_msg_text) > 0){
330 Multi_msg_text[strlen(Multi_msg_text)-1] = '\0';
334 // ignore these individual keys
335 case KEY_LSHIFT + KEY_SHIFTED:
336 case KEY_RSHIFT + KEY_SHIFTED:
337 case KEY_LALT + KEY_SHIFTED:
338 case KEY_RALT + KEY_SHIFTED:
339 case KEY_LCTRL + KEY_SHIFTED:
340 case KEY_RCTRL + KEY_SHIFTED:
343 // stick other printable characters onto the text
345 // if we're not already at the maximum length
346 if(strlen(Multi_msg_text) < MULTI_MSG_MAX_LEN){
347 str[0] = (char)key_to_ascii(k);
349 strcat(Multi_msg_text,str);
357 // return 0 or 1 if there is multi text to be rendered (filling in txt if necessary)
358 int multi_msg_message_text(char *txt)
360 // if we're not in text message mode, return 0
361 if(!Multi_msg_text_enter){
365 // put the target of the message at the front of the string
366 switch(Multi_msg_mode){
367 // messaging all players
369 strcpy(txt,XSTR("ALL : ",694));
372 // messaging friendly players
373 case MULTI_MSG_FRIENDLY:
374 strcpy(txt,XSTR("FRIENDLY : ",695));
377 // messaging hostile players
378 case MULTI_MSG_HOSTILE:
379 strcpy(txt,XSTR("HOSTILE : ",696));
382 // messaging targeted ship
383 case MULTI_MSG_TARGET:
384 strcpy(txt,XSTR("TARGET : ",697));
390 strcat(txt,Multi_msg_text);
395 // display ingame,inmission message text
396 void multi_msg_display_mission_text(char *msg,int player_index)
398 // play a cue voice sound
399 snd_play(&Snds[MULTI_MSG_TEXT_SOUND]);
401 if(MULTI_STANDALONE(Net_players[player_index])){
402 HUD_sourced_printf(HUD_SOURCE_NETPLAYER,"%s %s",XSTR("<SERVER>",698),msg);
404 HUD_sourced_printf(HUD_SOURCE_NETPLAYER,"%s : %s",Net_players[player_index].player->callsign,msg);
408 // if the passed net_player's callsign matches the reg expression of the passed expr
409 int multi_msg_matches_expr(net_player *player,char *expr)
411 char callsign[CALLSIGN_LEN+1];
414 // some error checking
415 if((player == NULL) || (expr == NULL) || (strlen(expr) <= 0)){
419 // get the completely lowercase callsign
420 memset(callsign,0,CALLSIGN_LEN+1);
421 len = strlen(player->player->callsign);
422 for(idx=0;idx<len;idx++){
423 callsign[idx] = (char)tolower(player->player->callsign[idx]);
426 // see if this guy's callsign matches the expr
428 for(idx=0;idx<len;idx++){
429 // look for non-matching characters
430 if(callsign[idx] != expr[idx]){
439 // if text input mode is active, clear it
440 void multi_msg_text_flush()
442 Multi_msg_text_enter = 0;
443 Multi_msg_mode = MULTI_MSG_NONE;
444 Multi_msg_stamp = -1;
446 // keep eating keys for a short period of time and unset any used control bits
447 Multi_msg_eat_stamp = timestamp(350);
448 control_config_clear_used_status();
453 // -----------------------------------------------------------------------------------
454 // MULTI MESSAGE COMMAND FUNCTIONS
457 // process an entered line of text and maybe perform a command on it (return 1 if an action was performed, 0 if not)
458 int multi_msg_check_command(char *str)
461 char *prefix,*predicate,param[MULTI_MSG_MAX_TEXT_LEN+1];
464 if(strstr(str,":") == NULL){
468 // try and find a command prefix
470 prefix = strtok(str,":");
475 // get all the text after the message
477 predicate = strtok(NULL, NOX("\n\0"));
478 if(predicate == NULL){
482 // store the text as the actual parameter
483 strcpy(param,predicate);
484 drop_leading_white_space(param);
486 // go through all existing commands and see what we can do
487 for(idx=0;idx<MULTI_MSG_CMD_COUNT;idx++){
488 if(!stricmp(prefix,Multi_msg_commands[idx])){
489 // perform the command
490 multi_msg_perform_command(idx,param);
497 // apply the results as a general expression, if we're in message all mode
498 if(Multi_msg_mode == MULTI_MSG_ALL){
499 strcpy(Multi_msg_text,param);
501 // send the chat packet
502 send_game_chat_packet(Net_player, Multi_msg_text, MULTI_MSG_EXPR,NULL, prefix);
504 // echo the message locally
505 multi_msg_display_mission_text(Multi_msg_text, MY_NET_PLAYER_NUM);
511 // no commands performed
515 // perform the passed command (MULTI_MSG_CMD_* define) with the passed string argument
516 void multi_msg_perform_command(int command,char *param)
518 // we may eventually want to split each of these cases into its own function to make things neater
521 case MULTI_MSG_CMD_KICK:
522 int np_index = multi_find_player_by_callsign(param);
524 multi_kick_player(np_index);
531 // -----------------------------------------------------------------------------------
532 // MULTI SQUADMATE MESSAGING FUNCTIONS
537 char *Multi_msg_subsys_name[SUBSYSTEM_MAX] = {
553 // display a squadmsg order directed towards _me_
554 void multi_msg_show_squadmsg(net_player *source,int command,ushort target_sig,int subsys_type)
556 char hud_string[255];
557 char temp_string[100];
562 memset(hud_string,0,255);
563 memset(temp_string,0,100);
565 // add the message header
566 sprintf(hud_string,XSTR("ORDER FROM <%s> : ",699),source->player->callsign);
568 // get the target obj if possible
570 target_obj = multi_get_network_object(target_sig);
574 // add the command specific text
577 case ATTACK_TARGET_ITEM :
578 if((target_obj != NULL) && (target_obj->type == OBJ_SHIP)){
579 sprintf(temp_string,XSTR("Attack %s",700),Ships[target_obj->instance].ship_name);
580 strcat(hud_string,temp_string);
587 case DISABLE_TARGET_ITEM:
588 if((target_obj != NULL) && (target_obj->type == OBJ_SHIP)){
589 sprintf(temp_string,XSTR("Disable %s",701),Ships[target_obj->instance].ship_name);
590 strcat(hud_string,temp_string);
597 case PROTECT_TARGET_ITEM:
598 if((target_obj != NULL) && (target_obj->type == OBJ_SHIP)){
599 sprintf(temp_string,XSTR("Protect %s",702),Ships[target_obj->instance].ship_name);
600 strcat(hud_string,temp_string);
607 case IGNORE_TARGET_ITEM:
608 if((target_obj != NULL) && (target_obj->type == OBJ_SHIP)){
609 sprintf(temp_string,XSTR("Ignore %s",703),Ships[target_obj->instance].ship_name);
610 strcat(hud_string,temp_string);
617 case DISARM_TARGET_ITEM:
618 if((target_obj != NULL) && (target_obj->type == OBJ_SHIP)){
619 sprintf(temp_string,XSTR("Disarm %s",704),Ships[target_obj->instance].ship_name);
620 strcat(hud_string,temp_string);
626 // disable subsystem on my target
627 case DISABLE_SUBSYSTEM_ITEM:
628 if((target_obj != NULL) && (target_obj->type == OBJ_SHIP) && (subsys_type != -1) && (subsys_type != 0)){
629 sprintf(temp_string,XSTR("Disable subsystem %s on %s",705),Multi_msg_subsys_name[subsys_type],Ships[target_obj->instance].ship_name);
630 strcat(hud_string,temp_string);
638 strcat(hud_string,XSTR("Form on my wing",706));
643 strcat(hud_string,XSTR("Cover me",707));
647 case ENGAGE_ENEMY_ITEM:
648 strcat(hud_string,XSTR("Engage enemy!",708));
658 HUD_printf(hud_string);
662 // evaluate if the given netplayer exists in the passed wingnum
663 int multi_msg_player_in_wing(int wingnum,net_player *pl)
667 // if this guy doesn't have a valid ship, bail
668 if((pl->player->objnum == -1) || (Objects[pl->player->objnum].type != OBJ_SHIP)){
672 // look through all ships in the wing
673 for(idx=0;idx<Wings[wingnum].current_count;idx++){
674 // if we found a match
675 if(Wings[wingnum].ship_index[idx] == Objects[pl->player->objnum].instance){
683 // evaluate if the given netplayer is flying the passed shipnum
684 int multi_msg_player_in_ship(int shipnum,net_player *pl)
686 // if we found a matching ship
687 if((pl->player->objnum != -1) && (Objects[pl->player->objnum].type == OBJ_SHIP) && (shipnum == Objects[pl->player->objnum].instance)){
691 // not a matching ship
695 // send a squadmsg packet to a player
696 void multi_msg_send_squadmsg_packet(net_player *target,net_player *source,int command,ushort net_sig,int subsys_type)
702 Assert(source != NULL);
703 Assert(target != NULL);
704 if((source == NULL) || (target == NULL)){
709 BUILD_HEADER(SQUADMSG_PLAYER);
711 // add the command and targeting data
714 // add the id of the guy sending the order
715 ADD_DATA(source->player_id);
720 // targeted subsytem (or -1 if none)
721 s_val = (char)subsys_type;
724 // send to the player
725 multi_io_send_reliable(target, data, packet_size);
728 // evaluate if a wing SQUADMATE MESSAGE command should be sent to a player
729 // return 0 if at least one ai ship got the order, 1 if only players
730 int multi_msg_eval_wing_squadmsg(int wingnum,int command,ai_info *aif, int player_num)
737 // get the index of the sender
739 player_num = MY_NET_PLAYER_NUM;
741 // get the target information
742 if(aif->target_objnum == -1){
745 net_sig = Objects[aif->target_objnum].net_signature;
748 if((aif->targeted_subsys == NULL) || (aif->targeted_subsys->system_info == NULL)){
751 subsys_type = aif->targeted_subsys->system_info->type;
754 // go through all netplayers and find all matched
755 sent_count = Wings[wingnum].current_count;
756 for(idx=0;idx<MAX_PLAYERS;idx++){
757 if(MULTI_CONNECTED(Net_players[idx]) && !MULTI_STANDALONE(Net_players[idx])){
758 // if he is in the wing, send him the message
759 if(multi_msg_player_in_wing(wingnum,&Net_players[idx])){
760 // if this was the sender himself, just decrement the count
761 if(idx == player_num){
766 // if its me, just display locally
767 if(&Net_players[idx] == Net_player){
768 multi_msg_show_squadmsg(&Net_players[player_num],command,net_sig,subsys_type);
771 // otherwise send it to who is supposed to get it
773 multi_msg_send_squadmsg_packet(&Net_players[idx],&Net_players[player_num],command,net_sig,subsys_type);
780 // if all the ships which got the message were players, return 1
784 // evaluate if a ship SQUADMATE MESSAGE command should be sent to a player
785 // return 0 if not sent to a netplayer, 1 if it was
786 int multi_msg_eval_ship_squadmsg(int shipnum,int command,ai_info *aif, int player_num)
792 // get the index of the sender
793 if ( player_num == -1 )
794 player_num = MY_NET_PLAYER_NUM;
796 // get the target information
797 if(aif->target_objnum == -1){
800 net_sig = Objects[aif->target_objnum].net_signature;
803 if((aif->targeted_subsys == NULL) || (aif->targeted_subsys->system_info == NULL)){
806 subsys_type = aif->targeted_subsys->system_info->type;
809 // go through all netplayers and find all matched
810 for(idx=0;idx<MAX_PLAYERS;idx++){
811 if(MULTI_CONNECTED(Net_players[idx]) && !MULTI_STANDALONE(Net_players[idx]) && (idx != player_num)){
812 // if he is in the ship, send him the message
813 if(multi_msg_player_in_ship(shipnum,&Net_players[idx])){
814 // if its me, just display locall
815 if(&Net_players[idx] == Net_player){
816 multi_msg_show_squadmsg(&Net_players[player_num],command,net_sig,subsys_type);
820 // otherwise send it to who is supposed to get it
822 multi_msg_send_squadmsg_packet(&Net_players[idx],&Net_players[player_num],command,net_sig,subsys_type);
829 // this will let the messaging system show a response to the sender of the packet
833 // process incoming squadmate messaging info
834 void multi_msg_process_squadmsg_packet(unsigned char *data, header *hinfo)
841 int offset = HEADER_LENGTH;
843 // get all packet data
850 // determine who the order is from
851 source_index = find_player_id(source_id);
852 if(source_index == -1){
853 nprintf(("Network","Received squadmsg order packet from unknown player!!\n"));
857 // display the squadmessage somehow
858 multi_msg_show_squadmsg(&Net_players[source_index],command,net_sig,(int)s_val);