]> icculus.org git repositories - taylor/freespace2.git/blob - src/network/stand_server.cpp
make multi log use port number in filename for standalone
[taylor/freespace2.git] / src / network / stand_server.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 the
6  * source.
7  *
8 */
9
10
11 #include "pstypes.h"
12 #include "osregistry.h"
13 #include "multi_options.h"
14 #include "gamesequence.h"
15 #include "timer.h"
16 #include "version.h"
17 #include "multi.h"
18 #include "stand_gui.h"
19 #include "multi_pmsg.h"
20 #include "multi_endgame.h"
21 #include "multimsgs.h"
22 #include "multiutil.h"
23 #include "freespace.h"
24 #include "missiongoals.h"
25 #include "cmdline.h"
26 #include "multi_kick.h"
27
28 #include <libwebsockets.h>
29 #include <string>
30 #include <vector>
31
32
33
34 static std::string Standalone_debug_state = "";
35 static std::string Standalone_ping_str;
36 static std::string Standalone_player_info;
37 static std::string Standalone_message;
38 static std::string Standalone_pinfo_active_player;
39 static std::string Standalone_mission_name = "";
40 static std::string Standalone_mission_time = "";
41 static std::string Standalone_netgame_info;
42 static std::string Standalone_mission_goals = "";
43 static std::string Standalone_popup_title;
44 static std::string Standalone_popup_field1 = "";
45 static std::string Standalone_popup_field2 = "";
46 static float Standalone_fps = 0.0f;
47
48 #define STANDALONE_MAX_BAN              50
49 static std::vector<std::string> Standalone_ban_list;
50
51 #define STD_STATS_UPDATE_TIME           500             // ms between updating player stats
52 #define STD_NG_UPDATE_TIME                      1500    // ms between updating netgame information
53 #define STD_PING_UPDATE_TIME            1000    // ms between updating pings
54 #define STD_FPS_UPDATE_TIME                     250             // ms between fps updates
55
56 static int Standalone_stats_stamp = -1;
57 static int Standalone_ng_stamp = -1;
58 static int Standalone_ping_stamp = -1;
59 static int Standalone_fps_stamp = -1;
60
61 static int Standalone_update_flags = 0;
62
63 #define STD_UFLAG_DEBUG_STATE           (1<<0)
64 #define STD_UFLAG_TITLE                         (1<<1)
65 #define STD_UFLAG_CONN                          (1<<2)
66 #define STD_UFLAG_RESET                         (1<<3)
67
68 #define STD_UFLAG_SERVER_NAME           (1<<4)
69 #define STD_UFLAG_HOST_PASS                     (1<<5)
70 #define STD_UFLAG_SET_PING                      (1<<6)
71
72 #define STD_UFLAG_FPS                           (1<<7)
73 #define STD_UFLAG_MISSION_NAME          (1<<8)
74 #define STD_UFLAG_MISSION_TIME          (1<<9)
75 #define STD_UFLAG_NETGAME_INFO          (1<<10)
76 #define STD_UFLAG_MISSION_GOALS         (1<<11)
77
78 #define STD_UFLAG_PLAYER_INFO           (1<<12)
79
80 #define STD_UFLAG_S_MESSAGE                     (1<<13)
81
82 #define STD_UFLAG_POPUP                         (1<<14)         // DO NOT INCLUDE IN STD_UFLAG_ALL!!
83
84 #define STD_UFLAG_GENERAL                       (STD_UFLAG_DEBUG_STATE|STD_UFLAG_TITLE|STD_UFLAG_RESET)
85 #define STD_UFLAG_TAB_SERVER            (STD_UFLAG_SERVER_NAME|STD_UFLAG_HOST_PASS|STD_UFLAG_CONN|STD_UFLAG_SET_PING)
86 #define STD_UFLAG_TAB_MULTI                     (STD_UFLAG_FPS|STD_UFLAG_MISSION_NAME|STD_UFLAG_MISSION_TIME|STD_UFLAG_NETGAME_INFO|STD_UFLAG_MISSION_GOALS)
87 #define STD_UFLAG_TAB_PLAYER            (STD_UFLAG_PLAYER_INFO)
88 #define STD_UFLAG_TAB_GS                        (STD_UFLAG_S_MESSAGE)
89
90 #define STD_UFLAG_ALL                           (STD_UFLAG_GENERAL|STD_UFLAG_TAB_SERVER|STD_UFLAG_TAB_MULTI|STD_UFLAG_TAB_PLAYER|STD_UFLAG_TAB_GS)
91
92
93 static lws_context *stand_context = NULL;
94
95
96 static int callback_http(struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len)
97 {
98         bool try_reuse = false;
99
100         switch (reason) {
101                 case LWS_CALLBACK_HTTP: {
102                         if (len < 1) {
103                                 lws_return_http_status(wsi, HTTP_STATUS_BAD_REQUEST, NULL);
104                                 try_reuse = true;
105
106                                 break;
107                         }
108
109                         int     ret = lws_serve_http_file(wsi, "./standalone.html", "text/html", NULL, 0);
110
111                         if ( (ret < 0) || ((ret > 0) && lws_http_transaction_completed(wsi)) ) {
112                                 // error or can't reuse connection, close the socket
113                                 return -1;
114                         }
115
116                         break;
117                 }
118
119                 case LWS_CALLBACK_HTTP_BODY_COMPLETION: {
120                         lws_return_http_status(wsi, HTTP_STATUS_OK, NULL);
121                         try_reuse = true;
122
123                         break;
124                 }
125
126                 case LWS_CALLBACK_HTTP_FILE_COMPLETION: {
127                         try_reuse = true;
128
129                         break;
130                 }
131
132                 default:
133                         break;
134         }
135
136         if (try_reuse) {
137                 if (lws_http_transaction_completed(wsi)) {
138                         return -1;
139                 }
140         }
141
142         return 0;
143 }
144
145 static int callback_standalone(struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len)
146 {
147         #define MAX_BUF_SIZE    1050
148         unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + MAX_BUF_SIZE + LWS_SEND_BUFFER_POST_PADDING];
149         unsigned char *p = &buf[LWS_SEND_BUFFER_PRE_PADDING];
150         int rval;
151         int size;
152
153         switch (reason) {
154                 case LWS_CALLBACK_ESTABLISHED: {
155                         std_reset_standalone_gui();
156
157                         break;
158                 }
159
160                 case LWS_CALLBACK_SERVER_WRITEABLE: {
161                         // RESET: *must* come first
162                         if (Standalone_update_flags & STD_UFLAG_RESET) {
163                                 size = SDL_snprintf((char *)p, MAX_BUF_SIZE, "reset");
164
165                                 rval = lws_write(wsi, p, size, LWS_WRITE_TEXT);
166
167                                 if (rval < size) {
168                                         lwsl_err("ERROR sending reset command!\n");
169                                         return -1;
170                                 }
171
172                                 Standalone_update_flags &= ~STD_UFLAG_RESET;
173                         }
174
175                         // general messages
176                         if (Standalone_update_flags & STD_UFLAG_TITLE) {
177                                 size = SDL_snprintf((char *)p, 64, "T:%s %d.%02d.%02d", XSTR("FreeSpace Standalone", 935), FS_VERSION_MAJOR, FS_VERSION_MINOR, FS_VERSION_BUILD);
178
179                                 rval = lws_write(wsi, p, size, LWS_WRITE_TEXT);
180
181                                 if (rval < size) {
182                                         lwsl_err("ERROR sending title string!\n");
183                                         return -1;
184                                 }
185
186                                 Standalone_update_flags &= ~STD_UFLAG_TITLE;
187                         }
188
189                         if (Standalone_update_flags & STD_UFLAG_DEBUG_STATE) {
190                                 size = SDL_snprintf((char *)p, 32, "D:%s", Standalone_debug_state.c_str());
191
192                                 rval = lws_write(wsi, p, size, LWS_WRITE_TEXT);
193
194                                 if (rval < size) {
195                                         lwsl_err("ERROR sending debug state!\n");
196                                         return -1;
197                                 }
198
199                                 Standalone_update_flags &= ~STD_UFLAG_DEBUG_STATE;
200                         }
201
202                         if (Standalone_update_flags & STD_UFLAG_POPUP) {
203                                 if ( !Standalone_popup_title.empty() ) {
204                                         size = SDL_snprintf((char *)p, MAX_BUF_SIZE, "popup %s;%s;%s", Standalone_popup_title.c_str(), Standalone_popup_field1.c_str(), Standalone_popup_field2.c_str());
205                                 } else {
206                                         size = SDL_snprintf((char *)p, MAX_BUF_SIZE, "popup ");
207                                 }
208
209                                 rval = lws_write(wsi, p, size, LWS_WRITE_TEXT);
210
211                                 if (rval < size) {
212                                         lwsl_err("ERROR sending popup!\n");
213                                         return -1;
214                                 }
215
216                                 Standalone_update_flags &= ~STD_UFLAG_POPUP;
217                         }
218
219                         // server tab
220                         if (Standalone_update_flags & STD_UFLAG_SERVER_NAME) {
221                                 size = SDL_snprintf((char *)p, MAX_GAMENAME_LEN, "S:name %s", Netgame.name);
222
223                                 rval = lws_write(wsi, p, size, LWS_WRITE_TEXT);
224
225                                 if (rval < size) {
226                                         lwsl_err("ERROR sending server name!\n");
227                                         return -1;
228                                 }
229
230                                 Standalone_update_flags &= ~STD_UFLAG_SERVER_NAME;
231                         }
232
233                         if (Standalone_update_flags & STD_UFLAG_HOST_PASS) {
234                                 size = SDL_snprintf((char *)p, STD_PASSWD_LEN, "S:pass %s", Multi_options_g.std_passwd);
235
236                                 rval = lws_write(wsi, p, size, LWS_WRITE_TEXT);
237
238                                 if (rval < size) {
239                                         lwsl_err("ERROR sending host password!\n");
240                                         return -1;
241                                 }
242
243                                 Standalone_update_flags &= ~STD_UFLAG_HOST_PASS;
244                         }
245
246                         if (Standalone_update_flags & STD_UFLAG_CONN) {
247                                 std::string conn_str;
248                                 char ip_address[60];
249
250                                 conn_str.reserve(1024);
251
252                                 for (int i = 0; i < MAX_PLAYERS; i++) {
253                                         net_player *np = &Net_players[i];
254
255                                         if ( MULTI_CONNECTED((*np)) && (Net_player != np) ) {
256                                                 conn_str.append(np->player->callsign);
257                                                 conn_str.append(",");
258
259                                                 psnet_addr_to_string(ip_address, SDL_arraysize(ip_address), &np->p_info.addr);
260                                                 conn_str.append(ip_address);
261                                                 conn_str.append(",");
262
263                                                 if (np->s_info.ping.ping_avg > -1) {
264                                                         if (np->s_info.ping.ping_avg >= 1000) {
265                                                                 SDL_snprintf(ip_address, SDL_arraysize(ip_address), "%s", XSTR("> 1 sec", 914));
266                                                         } else {
267                                                                 SDL_snprintf(ip_address, SDL_arraysize(ip_address), "%d%s", np->s_info.ping.ping_avg, XSTR(" ms", 915));
268                                                         }
269                                                 }
270
271                                                 conn_str.append(";");
272                                         }
273                                 }
274
275                                 SDL_assert(conn_str.length() < 1024);
276
277                                 size = SDL_snprintf((char *)p, MAX_BUF_SIZE, "S:conn %s", conn_str.c_str());
278
279                                 rval = lws_write(wsi, p, size, LWS_WRITE_TEXT);
280
281                                 if (rval < size) {
282                                         lwsl_err("ERROR sending connetions!\n");
283                                         return -1;
284                                 }
285
286                                 Standalone_update_flags &= ~STD_UFLAG_CONN;
287                         }
288
289                         if ( (Standalone_update_flags & STD_UFLAG_SET_PING) && !Standalone_ping_str.empty() ) {
290                                 SDL_assert(Standalone_ping_str.length() < 1024);
291
292                                 size = SDL_snprintf((char *)p, MAX_BUF_SIZE, "S:ping %s", Standalone_ping_str.c_str());
293
294                                 rval = lws_write(wsi, p, size, LWS_WRITE_TEXT);
295
296                                 if (rval < size) {
297                                         lwsl_err("ERROR sending conn ping!\n");
298                                         return -1;
299                                 }
300
301                                 Standalone_ping_str.clear();
302                                 Standalone_update_flags &= ~ STD_UFLAG_SET_PING;
303                         }
304
305                         // multi-player tab
306                         if (Standalone_update_flags & STD_UFLAG_MISSION_NAME) {
307                                 size = SDL_snprintf((char *)p, MAX_BUF_SIZE, "M:name %s", Standalone_mission_name.c_str());
308
309                                 rval = lws_write(wsi, p, size, LWS_WRITE_TEXT);
310
311                                 if (rval < size) {
312                                         lwsl_err("ERROR sending mission name!\n");
313                                         return -1;
314                                 }
315
316                                 Standalone_update_flags &= ~STD_UFLAG_MISSION_NAME;
317                         }
318
319                         if (Standalone_update_flags & STD_UFLAG_MISSION_TIME) {
320                                 size = SDL_snprintf((char *)p, MAX_BUF_SIZE, "M:time %s", Standalone_mission_time.c_str());
321
322                                 rval = lws_write(wsi, p, size, LWS_WRITE_TEXT);
323
324                                 if (rval < size) {
325                                         lwsl_err("ERROR sending mission time!\n");
326                                         return -1;
327                                 }
328
329                                 Standalone_update_flags &= ~STD_UFLAG_MISSION_TIME;
330                         }
331
332                         if (Standalone_update_flags & STD_UFLAG_NETGAME_INFO) {
333                                 size = SDL_snprintf((char *)p, MAX_BUF_SIZE, "M:info %s", Standalone_netgame_info.c_str());
334
335                                 rval = lws_write(wsi, p, size, LWS_WRITE_TEXT);
336
337                                 if (rval < size) {
338                                         lwsl_err("ERROR sending netgame info!\n");
339                                         return -1;
340                                 }
341
342                                 Standalone_update_flags &= ~STD_UFLAG_NETGAME_INFO;
343                         }
344
345                         if (Standalone_update_flags & STD_UFLAG_FPS) {
346                                 size = SDL_snprintf((char *)p, MAX_BUF_SIZE, "M:fps %.1f", Standalone_fps);
347
348                                 rval = lws_write(wsi, p, size, LWS_WRITE_TEXT);
349
350                                 if (rval < size) {
351                                         lwsl_err("ERROR sending fps!\n");
352                                         return -1;
353                                 }
354
355                                 Standalone_update_flags &= ~STD_UFLAG_FPS;
356                         }
357
358                         if ( (Standalone_update_flags & STD_UFLAG_MISSION_GOALS) && !Standalone_mission_goals.empty() ) {
359                                 size = SDL_snprintf((char *)p, MAX_BUF_SIZE, "M:goal %s", Standalone_mission_goals.c_str());
360
361                                 rval = lws_write(wsi, p, size, LWS_WRITE_TEXT);
362
363                                 if (rval < size) {
364                                         lwsl_err("ERROR sending mission goals!\n");
365                                         return -1;
366                                 }
367
368                                 Standalone_mission_goals.clear();
369                                 Standalone_update_flags &= ~STD_UFLAG_MISSION_GOALS;
370                         }
371
372                         // player tab
373                         if ( (Standalone_update_flags & STD_UFLAG_PLAYER_INFO) && !Standalone_player_info.empty() ) {
374                                 size = SDL_snprintf((char *)p, MAX_BUF_SIZE, "P:info %s", Standalone_player_info.c_str());
375
376                                 rval = lws_write(wsi, p, size, LWS_WRITE_TEXT);
377
378                                 if (rval < size) {
379                                         lwsl_err("ERROR sending player info!\n");
380                                         return -1;
381                                 }
382
383                                 Standalone_player_info.clear();
384                                 Standalone_update_flags &= ~STD_UFLAG_PLAYER_INFO;
385                         }
386
387                         // god stuff tab
388                         if ( (Standalone_update_flags & STD_UFLAG_S_MESSAGE) && !Standalone_message.empty() ) {
389                                 size = SDL_snprintf((char *)p, MAX_BUF_SIZE, "G:mesg %s", Standalone_message.c_str());
390
391                                 rval = lws_write(wsi, p, size, LWS_WRITE_TEXT);
392
393                                 if (rval < size) {
394                                         lwsl_err("ERROR sending chat message!\n");
395                                         return -1;
396                                 }
397
398                                 Standalone_message.clear();
399                                 Standalone_update_flags &= ~STD_UFLAG_S_MESSAGE;
400                         }
401
402                         break;
403                 }
404
405                 case LWS_CALLBACK_RECEIVE: {
406                         if (in != NULL && len > 0) {
407                                 const char *msg = (const char *)in;
408                                 char mtype = msg[0];
409
410                                 if ( !SDL_strcmp(msg, "shutdown") ) {
411                                         gameseq_post_event(GS_EVENT_QUIT_GAME);
412                                         return -1;
413                                 }
414
415                                 // server tab
416                                 if (mtype == 'S') {
417                                         if (len >= 7) {
418                                                 if ( !SDL_strncmp(msg+2, "name ", 5) ) {
419                                                         SDL_strlcpy(Netgame.name, msg+7, SDL_arraysize(Netgame.name));
420                                                         SDL_strlcpy(Multi_options_g.std_pname, Netgame.name, SDL_arraysize(Multi_options_g.std_pname));
421                                                 } else if ( !SDL_strncmp(msg+2, "pass ", 5) ) {
422                                                         SDL_strlcpy(Multi_options_g.std_passwd, msg+7, SDL_arraysize(Multi_options_g.std_passwd));
423                                                 } else if ( !SDL_strncmp(msg+2, "kick ", 5) ) {
424                                                         char ip_string[60];
425
426                                                         for (int i = 0; i < MAX_PLAYERS; i++) {
427                                                                 if ( MULTI_CONNECTED(Net_players[i]) ) {
428                                                                         psnet_addr_to_string(ip_string, SDL_arraysize(ip_string), &Net_players[i].p_info.addr);
429
430                                                                         if ( !SDL_strcmp(msg+7, ip_string) ) {
431                                                                                 multi_kick_player(i, 0);
432
433                                                                                 break;
434                                                                         }
435                                                                 }
436                                                         }
437                                                 }
438                                         }
439                                 }
440                                 // multi-player tab
441                                 else if (mtype == 'M') {
442                                         if (len >= 6) {
443                                                 if ( !SDL_strncmp(msg+2, "fps ", 4) ) {
444                                                         int fps = SDL_atoi(msg+6);
445
446                                                         Multi_options_g.std_framecap = fps;
447                                                 }
448                                         }
449                                 }
450                                 // player info tab
451                                 else if (mtype == 'P') {
452                                         if (len >= 7) {
453                                                 if ( !SDL_strncmp(msg+2, "info ", 5) ) {
454                                                         int i;
455
456                                                         for (i = 0; i < MAX_PLAYERS; i++) {
457                                                                 net_player *np = &Net_players[i];
458
459                                                                 if ( MULTI_CONNECTED((*np)) && (Net_player != np) ) {
460                                                                         if ( !SDL_strcmp(msg+7, np->player->callsign) ) {
461                                                                                 Standalone_pinfo_active_player = msg+7;
462                                                                                 std_pinfo_display_player_info(np);
463
464                                                                                 break;
465                                                                         }
466                                                                 }
467                                                         }
468
469                                                         if (i == MAX_PLAYERS) {
470                                                                 Standalone_pinfo_active_player.clear();
471                                                         }
472                                                 }
473                                         }
474                                 }
475                                 // god stuff tab
476                                 else if (mtype == 'G') {
477                                         if (len >= 7) {
478                                                 if ( !SDL_strncmp(msg+2, "smsg ", 5) ) {
479                                                         char txt[256];
480
481                                                         SDL_strlcpy(txt, msg+7, SDL_arraysize(txt));
482
483                                                         if (SDL_strlen(txt) > 0) {
484                                                                 send_game_chat_packet(Net_player, txt, MULTI_MSG_ALL, NULL);
485
486                                                                 std_add_chat_text(txt, MY_NET_PLAYER_NUM, 1);
487                                                         }
488                                                 } else if ( !SDL_strcmp(msg+2, "mrefresh") ) {
489                                                         if (MULTI_IS_TRACKER_GAME) {
490                                                                 cf_delete(MULTI_VALID_MISSION_FILE, CF_TYPE_DATA);
491
492                                                                 multi_update_valid_missions();
493                                                         }
494                                                 }
495                                         }
496                                 }
497                         }
498
499                         break;
500                 }
501
502                 default:
503                         break;
504         }
505
506         return 0;
507 }
508
509 static struct lws_protocols stand_protocols[] = {
510         {
511                 "http-only",
512                 callback_http,
513                 0,
514                 0
515         },
516         {
517                 "standalone",
518                 callback_standalone,
519                 0,
520                 0
521         },
522         // terminator
523         {
524                 NULL,
525                 NULL,
526                 0,
527                 0
528         }
529 };
530
531
532
533
534 void std_deinit_standalone()
535 {
536         if (stand_context) {
537                 lws_cancel_service(stand_context);
538                 lws_context_destroy(stand_context);
539                 stand_context = NULL;
540         }
541 }
542
543 void std_init_standalone()
544 {
545         struct lws_context_creation_info info;
546
547         if (stand_context) {
548                 return;
549         }
550
551         SDL_zero(info);
552
553         info.port = Multi_options_g.port;
554
555         info.protocols = stand_protocols;
556
557         info.gid = -1;
558         info.uid = -1;
559
560         info.ka_time = 0;
561         info.ka_probes = 0;
562         info.ka_interval = 0;
563
564         stand_context = lws_create_context(&info);
565
566         if (stand_context == NULL) {
567                 Error(LOCATION, "Unable to initialize standalone server!");
568         }
569
570         atexit(std_deinit_standalone);
571
572         // turn off all sound and music
573         Cmdline_freespace_no_sound = 1;
574         Cmdline_freespace_no_music = 1;
575
576         std_reset_standalone_gui();
577
578         std_multi_update_netgame_info_controls();
579 }
580
581 void std_do_gui_frame()
582 {
583         // maybe update selected player stats
584         if ( ((Standalone_stats_stamp == -1) || timestamp_elapsed(Standalone_stats_stamp)) && !Standalone_pinfo_active_player.empty() ) {
585                 Standalone_stats_stamp = timestamp(STD_STATS_UPDATE_TIME);
586
587                 for (int i = 0; i < MAX_PLAYERS; i++) {
588                         net_player *np = &Net_players[i];
589
590                         if ( MULTI_CONNECTED((*np)) && (Net_player != np) ) {
591                                 if ( !SDL_strcmp(Standalone_pinfo_active_player.c_str(), np->player->callsign) ) {
592                                         std_pinfo_display_player_info(np);
593
594                                         break;
595                                 }
596                         }
597                 }
598         }
599
600         // maybe update netgame info
601         if ( (Standalone_ng_stamp == -1) || timestamp_elapsed(Standalone_ng_stamp) ) {
602                 Standalone_ng_stamp = timestamp(STD_NG_UPDATE_TIME);
603
604                 std_multi_update_netgame_info_controls();
605         }
606
607         // update connection ping times
608         if ( ((Standalone_ping_stamp == -1) || timestamp_elapsed(Standalone_ping_stamp)) && !Standalone_ping_str.empty() ) {
609                 Standalone_ping_stamp = timestamp(STD_PING_UPDATE_TIME);
610                 Standalone_update_flags |= STD_UFLAG_SET_PING;
611         }
612
613         if (Standalone_update_flags) {
614                 lws_callback_on_writable_all_protocol(stand_context, &stand_protocols[1]);
615         }
616
617         lws_service(stand_context, 0);
618 }
619
620 void std_debug_set_standalone_state_string(const char *str)
621 {
622         Standalone_debug_state = str;
623
624         Standalone_update_flags |= STD_UFLAG_DEBUG_STATE;
625 }
626
627 void std_connect_set_gamename(const char *name)
628 {
629         if (name == NULL) {
630                 // if a permanent name exists, use that instead of the default
631                 if ( SDL_strlen(Multi_options_g.std_pname) ) {
632                         SDL_strlcpy(Netgame.name, Multi_options_g.std_pname, SDL_arraysize(Netgame.name));
633                 } else {
634                         SDL_strlcpy(Netgame.name, XSTR("Standalone Server", 916), SDL_arraysize(Netgame.name));
635                 }
636         } else {
637                 SDL_strlcpy(Netgame.name, name, SDL_arraysize(Netgame.name));
638         }
639
640         Standalone_update_flags |= STD_UFLAG_SERVER_NAME;
641 }
642
643 int std_connect_set_connect_count()
644 {
645         int count = 0;
646
647         for (int i = 0; i < MAX_PLAYERS; i++) {
648                 if (MULTI_CONNECTED(Net_players[i]) && (Net_player != &Net_players[i]) ) {
649                         count++;
650                 }
651         }
652
653         return count;
654 }
655
656 void std_add_player(net_player *p)
657 {
658         Standalone_update_flags |= STD_UFLAG_CONN;
659
660         // check to see if this guy is the host
661         std_connect_set_host_connect_status();
662 }
663
664 int std_remove_player(net_player *p)
665 {
666         int count;
667
668         Standalone_update_flags |= STD_UFLAG_CONN;
669
670         // update the host connect count
671         std_connect_set_host_connect_status();
672
673         // update the currently connected players
674         count = std_connect_set_connect_count();
675
676         if (count == 0) {
677                 multi_quit_game(PROMPT_NONE);
678                 return 1;
679         }
680
681         return 0;
682 }
683
684 void std_update_player_ping(net_player *p)
685 {
686         char ip_address[60];
687
688         if (p->s_info.ping.ping_avg > -1) {
689                 psnet_addr_to_string(ip_address, SDL_arraysize(ip_address), &p->p_info.addr);
690
691                 // only add it if address isn't already queued up
692                 if (Standalone_ping_str.find(ip_address) == std::string::npos) {
693                         Standalone_ping_str.append(ip_address);
694
695                         if (p->s_info.ping.ping_avg > 1000) {
696                                 SDL_snprintf(ip_address, SDL_arraysize(ip_address), ",%s;", XSTR("> 1 sec", 914));
697                         } else {
698                                 SDL_snprintf(ip_address, SDL_arraysize(ip_address), ",%d%s;", p->s_info.ping.ping_avg, XSTR(" ms", 915));
699                         }
700
701                         Standalone_ping_str.append(ip_address);
702                 }
703         }
704 }
705
706 void std_pinfo_display_player_info(net_player *p)
707 {
708         char sml_ping[30];
709
710         Standalone_player_info.clear();
711         Standalone_player_info.reserve(256);
712
713         // ship type
714         Standalone_player_info.append(Ship_info[p->p_info.ship_class].name);
715         Standalone_player_info.append(";");
716
717         // avg ping time
718         if (p->s_info.ping.ping_avg > 1000) {
719                 SDL_snprintf(sml_ping, SDL_arraysize(sml_ping), "%s", XSTR("> 1 sec", 914));
720         } else {
721                 SDL_snprintf(sml_ping, SDL_arraysize(sml_ping), "%d%s", p->s_info.ping.ping_avg, XSTR(" ms", 915));
722         }
723
724         Standalone_player_info.append(sml_ping);
725         Standalone_player_info.append(";");
726
727         scoring_struct *ptr = &p->player->stats;
728
729         // all-time stats
730         SDL_snprintf(sml_ping, SDL_arraysize(sml_ping), "%d", ptr->p_shots_fired);
731         Standalone_player_info.append(sml_ping);
732         Standalone_player_info.append(",");
733         SDL_snprintf(sml_ping, SDL_arraysize(sml_ping), "%d", ptr->p_shots_hit);
734         Standalone_player_info.append(sml_ping);
735         Standalone_player_info.append(",");
736         SDL_snprintf(sml_ping, SDL_arraysize(sml_ping), "%d", ptr->p_bonehead_hits);
737         Standalone_player_info.append(sml_ping);
738         Standalone_player_info.append(",");
739         SDL_snprintf(sml_ping, SDL_arraysize(sml_ping), "%d", ptr->p_shots_fired ? (int)(100.0f * ((float)ptr->p_shots_hit / (float)ptr->p_shots_fired)) : 0);
740         Standalone_player_info.append(sml_ping);
741         Standalone_player_info.append(",");
742         SDL_snprintf(sml_ping, SDL_arraysize(sml_ping), "%d", ptr->p_shots_fired ? (int)(100.0f * ((float)ptr->p_bonehead_hits / (float)ptr->p_shots_fired)) : 0);
743         Standalone_player_info.append(sml_ping);
744         Standalone_player_info.append(",");
745         SDL_snprintf(sml_ping, SDL_arraysize(sml_ping), "%d", ptr->s_shots_fired);
746         Standalone_player_info.append(sml_ping);
747         Standalone_player_info.append(",");
748         SDL_snprintf(sml_ping, SDL_arraysize(sml_ping), "%d", ptr->s_shots_hit);
749         Standalone_player_info.append(sml_ping);
750         Standalone_player_info.append(",");
751         SDL_snprintf(sml_ping, SDL_arraysize(sml_ping), "%d", ptr->s_bonehead_hits);
752         Standalone_player_info.append(sml_ping);
753         Standalone_player_info.append(",");
754         SDL_snprintf(sml_ping, SDL_arraysize(sml_ping), "%d", ptr->s_shots_fired ? (int)(100.0f * ((float)ptr->s_shots_hit / (float)ptr->s_shots_fired)) : 0);
755         Standalone_player_info.append(sml_ping);
756         Standalone_player_info.append(",");
757         SDL_snprintf(sml_ping, SDL_arraysize(sml_ping), "%d", ptr->s_shots_fired ? (int)(100.0f * ((float)ptr->s_bonehead_hits / (float)ptr->s_shots_fired)) : 0);
758         Standalone_player_info.append(sml_ping);
759         Standalone_player_info.append(",");
760         SDL_snprintf(sml_ping, SDL_arraysize(sml_ping), "%d", ptr->assists);
761         Standalone_player_info.append(sml_ping);
762         Standalone_player_info.append(";");     // <- end of block
763
764         // mission stats
765         SDL_snprintf(sml_ping, SDL_arraysize(sml_ping), "%d", ptr->mp_shots_fired);
766         Standalone_player_info.append(sml_ping);
767         Standalone_player_info.append(",");
768         SDL_snprintf(sml_ping, SDL_arraysize(sml_ping), "%d", ptr->mp_shots_hit);
769         Standalone_player_info.append(sml_ping);
770         Standalone_player_info.append(",");
771         SDL_snprintf(sml_ping, SDL_arraysize(sml_ping), "%d", ptr->mp_bonehead_hits);
772         Standalone_player_info.append(sml_ping);
773         Standalone_player_info.append(",");
774         SDL_snprintf(sml_ping, SDL_arraysize(sml_ping), "%d", ptr->mp_shots_fired ? (int)(100.0f * ((float)ptr->mp_shots_hit / (float)ptr->mp_shots_fired)) : 0);
775         Standalone_player_info.append(sml_ping);
776         Standalone_player_info.append(",");
777         SDL_snprintf(sml_ping, SDL_arraysize(sml_ping), "%d", ptr->mp_shots_fired ? (int)(100.0f * ((float)ptr->mp_bonehead_hits / (float)ptr->mp_shots_fired)) : 0);
778         Standalone_player_info.append(sml_ping);
779         Standalone_player_info.append(",");
780         SDL_snprintf(sml_ping, SDL_arraysize(sml_ping), "%d", ptr->ms_shots_fired);
781         Standalone_player_info.append(sml_ping);
782         Standalone_player_info.append(",");
783         SDL_snprintf(sml_ping, SDL_arraysize(sml_ping), "%d", ptr->ms_shots_hit);
784         Standalone_player_info.append(sml_ping);
785         Standalone_player_info.append(",");
786         SDL_snprintf(sml_ping, SDL_arraysize(sml_ping), "%d", ptr->ms_bonehead_hits);
787         Standalone_player_info.append(sml_ping);
788         Standalone_player_info.append(",");
789         SDL_snprintf(sml_ping, SDL_arraysize(sml_ping), "%d", ptr->ms_shots_fired ? (int)(100.0f * ((float)ptr->ms_shots_hit / (float)ptr->ms_shots_fired)) : 0);
790         Standalone_player_info.append(sml_ping);
791         Standalone_player_info.append(",");
792         SDL_snprintf(sml_ping, SDL_arraysize(sml_ping), "%d", ptr->ms_shots_fired ? (int)(100.0f * ((float)ptr->ms_bonehead_hits / (float)ptr->ms_shots_fired)) : 0);
793         Standalone_player_info.append(sml_ping);
794         Standalone_player_info.append(",");
795         SDL_snprintf(sml_ping, SDL_arraysize(sml_ping), "%d", ptr->m_assists);
796         Standalone_player_info.append(sml_ping);
797
798         Standalone_update_flags |= STD_UFLAG_PLAYER_INFO;
799 }
800
801 void std_add_chat_text(const char *text, int player_index, int add_id)
802 {
803         char id[32];
804
805         if ( (player_index < 0) || (player_index >= MAX_PLAYERS) ) {
806                 return;
807         }
808
809         // format the chat text nicely
810         if (add_id) {
811                 if ( MULTI_STANDALONE(Net_players[player_index]) ) {
812                         SDL_snprintf(id, SDL_arraysize(id), XSTR("<SERVER> %s", 924), "");
813                 } else {
814                         SDL_snprintf(id, SDL_arraysize(id), "%s: ", Net_players[player_index].player->callsign);
815                 }
816
817                 Standalone_message.append(id);
818         }
819
820         Standalone_message.append(text);
821         Standalone_message.append("\n");
822
823         Standalone_update_flags |= STD_UFLAG_S_MESSAGE;
824 }
825
826 void std_reset_timestamps()
827 {
828         // reset the stats update stamp
829         Standalone_stats_stamp = timestamp(STD_STATS_UPDATE_TIME);
830
831         // reset the netgame controls update timestamp
832         Standalone_ng_stamp = timestamp(STD_NG_UPDATE_TIME);
833
834         // reset the ping update stamp
835         Standalone_ping_stamp = timestamp(STD_PING_UPDATE_TIME);
836
837         // reset fps update stamp
838         Standalone_fps_stamp = timestamp(STD_FPS_UPDATE_TIME);
839 }
840
841 void std_add_ban(const char *name)
842 {
843         if ( (name == NULL) || !SDL_strlen(name) ) {
844                 return;
845         }
846
847         if (Standalone_ban_list.size() >= STANDALONE_MAX_BAN) {
848                 return;
849         }
850
851         Standalone_ban_list.push_back(name);
852 }
853
854 int std_player_is_banned(const char *name)
855 {
856         if ( Standalone_ban_list.empty() ) {
857                 return 0;
858         }
859
860         for (size_t i = 0; i < Standalone_ban_list.size(); i++) {
861                 if ( !SDL_strcasecmp(name, Standalone_ban_list[i].c_str()) ) {
862                         return 1;
863                 }
864         }
865
866         return 0;
867 }
868
869 int std_is_host_passwd()
870 {
871         return (SDL_strlen(Multi_options_g.std_passwd) > 0) ? 1 : 0;
872 }
873
874 void std_multi_set_standalone_mission_name(const char *mission_name)
875 {
876         Standalone_mission_name = mission_name;
877         Standalone_update_flags |= STD_UFLAG_MISSION_NAME;
878 }
879
880 void std_multi_set_standalone_missiontime(float mission_time)
881 {
882         char txt[80];
883         char timestr[50];
884         fix m_time = fl2f(mission_time);
885
886         // format the time string and set the text
887         game_format_time(m_time, timestr, SDL_arraysize(timestr));
888         SDL_snprintf(txt, SDL_arraysize(txt), "%s  :  %.1f", timestr, mission_time);
889
890         Standalone_mission_time = txt;
891         Standalone_update_flags |= STD_UFLAG_MISSION_TIME;
892 }
893
894 void std_multi_update_netgame_info_controls()
895 {
896         char nginfo[50];
897
898         SDL_snprintf(nginfo, SDL_arraysize(nginfo), "%d,%d,%d,%d", Netgame.max_players, Netgame.options.max_observers, Netgame.security, Netgame.respawn);
899
900         Standalone_netgame_info = nginfo;
901         Standalone_update_flags |= STD_UFLAG_NETGAME_INFO;
902 }
903
904 void std_set_standalone_fps(float fps)
905 {
906         if ( (Standalone_fps_stamp == -1) || timestamp_elapsed(Standalone_fps_stamp) ) {
907                 Standalone_fps_stamp = timestamp(STD_FPS_UPDATE_TIME);
908
909                 Standalone_fps = fps;
910                 Standalone_update_flags |= STD_UFLAG_FPS;
911         }
912 }
913
914 void std_multi_setup_goal_tree()
915 {
916         std::string primary;
917         std::string secondary;
918         std::string bonus;
919         std::string status;
920
921         Standalone_mission_goals.clear();
922
923         for (int i = 0; i < Num_goals; i++) {
924                 switch (Mission_goals[i].satisfied) {
925                         case GOAL_FAILED: {
926                                 status = "f ";
927                                 break;
928                         }
929
930                         case GOAL_COMPLETE: {
931                                 status = "c ";
932                                 break;
933                         }
934
935                         case GOAL_INCOMPLETE:
936                         default: {
937                                 status = "i ";
938                                 break;
939                         }
940                 }
941
942                 switch (Mission_goals[i].type & GOAL_TYPE_MASK) {
943                         case PRIMARY_GOAL: {
944                                 primary.append(status);
945                                 primary.append(Mission_goals[i].name);
946                                 primary.append(",");
947
948                                 break;
949                         }
950
951                         case SECONDARY_GOAL: {
952                                 secondary.append(status);
953                                 secondary.append(Mission_goals[i].name);
954                                 secondary.append(",");
955
956                                 break;
957                         }
958
959                         case BONUS_GOAL: {
960                                 bonus.append(status);
961                                 bonus.append(Mission_goals[i].name);
962                                 bonus.append(",");
963
964                                 break;
965                         }
966
967                         default:
968                                 break;
969                 }
970         }
971
972         if ( primary.empty() ) {
973                 Standalone_mission_goals.append("i none");
974         } else {
975                 Standalone_mission_goals.append(primary.substr(0, primary.size()-1));
976         }
977
978         Standalone_mission_goals.append(";");
979
980         if ( secondary.empty() ) {
981                 Standalone_mission_goals.append("i none");
982         } else {
983                 Standalone_mission_goals.append(secondary.substr(0, secondary.size()-1));
984         }
985
986         Standalone_mission_goals.append(";");
987
988         if ( bonus.empty() ) {
989                 Standalone_mission_goals.append("i none");
990         } else {
991                 Standalone_mission_goals.append(bonus.substr(0, bonus.size()-1));
992         }
993
994         Standalone_update_flags |= STD_UFLAG_MISSION_GOALS;
995 }
996
997 void std_multi_add_goals()
998 {
999         std_multi_setup_goal_tree();
1000 }
1001
1002 void std_multi_update_goals()
1003 {
1004         std_multi_setup_goal_tree();
1005 }
1006
1007 void std_reset_standalone_gui()
1008 {
1009         Standalone_stats_stamp = -1;
1010         Standalone_ng_stamp = -1;
1011         Standalone_ping_stamp = -1;
1012         Standalone_fps_stamp = -1;
1013
1014         Standalone_ping_str.clear();
1015         Standalone_player_info.clear();
1016         Standalone_message.clear();
1017         Standalone_pinfo_active_player.clear();
1018         Standalone_mission_name = "";
1019         Standalone_mission_time = "";
1020         Standalone_netgame_info.clear();
1021         Standalone_popup_title.clear();
1022         Standalone_popup_field1 = "";
1023         Standalone_popup_field2 = "";
1024
1025         std_set_standalone_fps(0.0f);
1026         std_multi_set_standalone_missiontime(0.0f);
1027
1028         Standalone_update_flags |= STD_UFLAG_ALL;
1029 }
1030
1031 void std_create_gen_dialog(const char *title)
1032 {
1033         Standalone_popup_title = title;
1034 }
1035
1036 void std_destroy_gen_dialog()
1037 {
1038         Standalone_popup_title.clear();
1039
1040         Standalone_update_flags |= STD_UFLAG_POPUP;
1041 }
1042
1043 void std_gen_set_text(const char *str, int field_num)
1044 {
1045         switch (field_num) {
1046                 case 0:
1047                         Standalone_popup_title = str;
1048                         break;
1049
1050                 case 1:
1051                         Standalone_popup_field1 = str;
1052                         break;
1053
1054                 case 2:
1055                         Standalone_popup_field2 = str;
1056                         break;
1057
1058                 default:
1059                         break;
1060         }
1061
1062         Standalone_update_flags |= STD_UFLAG_POPUP;
1063 }
1064
1065 void std_connect_set_host_connect_status()
1066 {
1067 }