]> icculus.org git repositories - divverent/darkplaces.git/blob - host_cmd.c
querycounters LOVE to be reset ;)
[divverent/darkplaces.git] / host_cmd.c
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 */
20
21 #include "quakedef.h"
22 #include "sv_demo.h"
23 #include "image.h"
24
25 int current_skill;
26 cvar_t sv_cheats = {0, "sv_cheats", "0", "enables cheat commands in any game, and cheat impulses in dpmod"};
27 cvar_t sv_adminnick = {CVAR_SAVE, "sv_adminnick", "", "nick name to use for admin messages instead of host name"};
28 cvar_t rcon_password = {CVAR_PRIVATE, "rcon_password", "", "password to authenticate rcon commands"};
29 cvar_t rcon_address = {0, "rcon_address", "", "server address to send rcon commands to (when not connected to a server)"};
30 cvar_t team = {CVAR_USERINFO | CVAR_SAVE, "team", "none", "QW team (4 character limit, example: blue)"};
31 cvar_t skin = {CVAR_USERINFO | CVAR_SAVE, "skin", "", "QW player skin name (example: base)"};
32 cvar_t noaim = {CVAR_USERINFO | CVAR_SAVE, "noaim", "1", "QW option to disable vertical autoaim"};
33 qboolean allowcheats = false;
34
35 extern qboolean host_shuttingdown;
36
37 /*
38 ==================
39 Host_Quit_f
40 ==================
41 */
42
43 void Host_Quit_f (void)
44 {
45         if(host_shuttingdown)
46                 Con_Printf("shutting down already!\n");
47         else
48                 Sys_Quit (0);
49 }
50
51 /*
52 ==================
53 Host_Status_f
54 ==================
55 */
56 void Host_Status_f (void)
57 {
58         client_t *client;
59         int seconds, minutes, hours = 0, j, players;
60         void (*print) (const char *fmt, ...);
61
62         if (cmd_source == src_command)
63         {
64                 // if running a client, try to send over network so the client's status report parser will see the report
65                 if (cls.state == ca_connected)
66                 {
67                         Cmd_ForwardToServer ();
68                         return;
69                 }
70                 print = Con_Printf;
71         }
72         else
73                 print = SV_ClientPrintf;
74
75         if (!sv.active)
76                 return;
77
78         for (players = 0, j = 0;j < svs.maxclients;j++)
79                 if (svs.clients[j].active)
80                         players++;
81         print ("host:     %s\n", Cvar_VariableString ("hostname"));
82         print ("version:  %s build %s\n", gamename, buildstring);
83         print ("protocol: %i (%s)\n", Protocol_NumberForEnum(sv.protocol), Protocol_NameForEnum(sv.protocol));
84         print ("map:      %s\n", sv.name);
85         print ("timing:   %s\n", Host_TimingReport());
86         print ("players:  %i active (%i max)\n\n", players, svs.maxclients);
87         for (j = 0, client = svs.clients;j < svs.maxclients;j++, client++)
88         {
89                 if (!client->active)
90                         continue;
91                 seconds = (int)(realtime - client->connecttime);
92                 minutes = seconds / 60;
93                 if (minutes)
94                 {
95                         seconds -= (minutes * 60);
96                         hours = minutes / 60;
97                         if (hours)
98                                 minutes -= (hours * 60);
99                 }
100                 else
101                         hours = 0;
102                 print ("#%-3u %-16.16s  %3i  %2i:%02i:%02i\n", j+1, client->name, client->frags, hours, minutes, seconds);
103                 print ("   %s\n", client->netconnection ? client->netconnection->address : "botclient");
104         }
105 }
106
107
108 /*
109 ==================
110 Host_God_f
111
112 Sets client to godmode
113 ==================
114 */
115 void Host_God_f (void)
116 {
117         if (!allowcheats)
118         {
119                 SV_ClientPrint("No cheats allowed, use sv_cheats 1 and restart level to enable.\n");
120                 return;
121         }
122
123         host_client->edict->fields.server->flags = (int)host_client->edict->fields.server->flags ^ FL_GODMODE;
124         if (!((int)host_client->edict->fields.server->flags & FL_GODMODE) )
125                 SV_ClientPrint("godmode OFF\n");
126         else
127                 SV_ClientPrint("godmode ON\n");
128 }
129
130 void Host_Notarget_f (void)
131 {
132         if (!allowcheats)
133         {
134                 SV_ClientPrint("No cheats allowed, use sv_cheats 1 and restart level to enable.\n");
135                 return;
136         }
137
138         host_client->edict->fields.server->flags = (int)host_client->edict->fields.server->flags ^ FL_NOTARGET;
139         if (!((int)host_client->edict->fields.server->flags & FL_NOTARGET) )
140                 SV_ClientPrint("notarget OFF\n");
141         else
142                 SV_ClientPrint("notarget ON\n");
143 }
144
145 qboolean noclip_anglehack;
146
147 void Host_Noclip_f (void)
148 {
149         if (!allowcheats)
150         {
151                 SV_ClientPrint("No cheats allowed, use sv_cheats 1 and restart level to enable.\n");
152                 return;
153         }
154
155         if (host_client->edict->fields.server->movetype != MOVETYPE_NOCLIP)
156         {
157                 noclip_anglehack = true;
158                 host_client->edict->fields.server->movetype = MOVETYPE_NOCLIP;
159                 SV_ClientPrint("noclip ON\n");
160         }
161         else
162         {
163                 noclip_anglehack = false;
164                 host_client->edict->fields.server->movetype = MOVETYPE_WALK;
165                 SV_ClientPrint("noclip OFF\n");
166         }
167 }
168
169 /*
170 ==================
171 Host_Fly_f
172
173 Sets client to flymode
174 ==================
175 */
176 void Host_Fly_f (void)
177 {
178         if (!allowcheats)
179         {
180                 SV_ClientPrint("No cheats allowed, use sv_cheats 1 and restart level to enable.\n");
181                 return;
182         }
183
184         if (host_client->edict->fields.server->movetype != MOVETYPE_FLY)
185         {
186                 host_client->edict->fields.server->movetype = MOVETYPE_FLY;
187                 SV_ClientPrint("flymode ON\n");
188         }
189         else
190         {
191                 host_client->edict->fields.server->movetype = MOVETYPE_WALK;
192                 SV_ClientPrint("flymode OFF\n");
193         }
194 }
195
196
197 /*
198 ==================
199 Host_Ping_f
200
201 ==================
202 */
203 void Host_Pings_f (void); // called by Host_Ping_f
204 void Host_Ping_f (void)
205 {
206         int i;
207         client_t *client;
208         void (*print) (const char *fmt, ...);
209
210         if (cmd_source == src_command)
211         {
212                 // if running a client, try to send over network so the client's ping report parser will see the report
213                 if (cls.state == ca_connected)
214                 {
215                         Cmd_ForwardToServer ();
216                         return;
217                 }
218                 print = Con_Printf;
219         }
220         else
221                 print = SV_ClientPrintf;
222
223         if (!sv.active)
224                 return;
225
226         print("Client ping times:\n");
227         for (i = 0, client = svs.clients;i < svs.maxclients;i++, client++)
228         {
229                 if (!client->active)
230                         continue;
231                 print("%4i %s\n", bound(0, (int)floor(client->ping*1000+0.5), 9999), client->name);
232         }
233
234         // now call the Pings command also, which will send a report that contains packet loss for the scoreboard (as well as a simpler ping report)
235         // actually, don't, it confuses old clients (resulting in "unknown command pingplreport" flooding the console)
236         //Host_Pings_f();
237 }
238
239 /*
240 ===============================================================================
241
242 SERVER TRANSITIONS
243
244 ===============================================================================
245 */
246
247 /*
248 ======================
249 Host_Map_f
250
251 handle a
252 map <servername>
253 command from the console.  Active clients are kicked off.
254 ======================
255 */
256 void Host_Map_f (void)
257 {
258         char level[MAX_QPATH];
259
260         if (Cmd_Argc() != 2)
261         {
262                 Con_Print("map <levelname> : start a new game (kicks off all players)\n");
263                 return;
264         }
265
266         cls.demonum = -1;               // stop demo loop in case this fails
267
268         CL_Disconnect ();
269         Host_ShutdownServer();
270
271         // remove menu
272         key_dest = key_game;
273
274         svs.serverflags = 0;                    // haven't completed an episode yet
275         allowcheats = sv_cheats.integer != 0;
276         strlcpy(level, Cmd_Argv(1), sizeof(level));
277         SV_SpawnServer(level);
278         if (sv.active && cls.state == ca_disconnected)
279                 CL_EstablishConnection("local:1");
280 }
281
282 /*
283 ==================
284 Host_Changelevel_f
285
286 Goes to a new map, taking all clients along
287 ==================
288 */
289 void Host_Changelevel_f (void)
290 {
291         char level[MAX_QPATH];
292
293         if (Cmd_Argc() != 2)
294         {
295                 Con_Print("changelevel <levelname> : continue game on a new level\n");
296                 return;
297         }
298         // HACKHACKHACK
299         if (!sv.active) {
300                 Host_Map_f();
301                 return;
302         }
303
304         // remove menu
305         key_dest = key_game;
306
307         SV_VM_Begin();
308         SV_SaveSpawnparms ();
309         SV_VM_End();
310         allowcheats = sv_cheats.integer != 0;
311         strlcpy(level, Cmd_Argv(1), sizeof(level));
312         SV_SpawnServer(level);
313         if (sv.active && cls.state == ca_disconnected)
314                 CL_EstablishConnection("local:1");
315 }
316
317 /*
318 ==================
319 Host_Restart_f
320
321 Restarts the current server for a dead player
322 ==================
323 */
324 void Host_Restart_f (void)
325 {
326         char mapname[MAX_QPATH];
327
328         if (Cmd_Argc() != 1)
329         {
330                 Con_Print("restart : restart current level\n");
331                 return;
332         }
333         if (!sv.active)
334         {
335                 Con_Print("Only the server may restart\n");
336                 return;
337         }
338
339         // remove menu
340         key_dest = key_game;
341
342         allowcheats = sv_cheats.integer != 0;
343         strlcpy(mapname, sv.name, sizeof(mapname));
344         SV_SpawnServer(mapname);
345         if (sv.active && cls.state == ca_disconnected)
346                 CL_EstablishConnection("local:1");
347 }
348
349 /*
350 ==================
351 Host_Reconnect_f
352
353 This command causes the client to wait for the signon messages again.
354 This is sent just before a server changes levels
355 ==================
356 */
357 void Host_Reconnect_f (void)
358 {
359         char temp[128];
360         // if not connected, reconnect to the most recent server
361         if (!cls.netcon)
362         {
363                 // if we have connected to a server recently, the userinfo
364                 // will still contain its IP address, so get the address...
365                 InfoString_GetValue(cls.userinfo, "*ip", temp, sizeof(temp));
366                 if (temp[0])
367                         CL_EstablishConnection(temp);
368                 else
369                         Con_Printf("Reconnect to what server?  (you have not connected to a server yet)\n");
370                 return;
371         }
372         // if connected, do something based on protocol
373         if (cls.protocol == PROTOCOL_QUAKEWORLD)
374         {
375                 // quakeworld can just re-login
376                 if (cls.qw_downloadmemory)  // don't change when downloading
377                         return;
378
379                 S_StopAllSounds();
380
381                 if (cls.state == ca_connected && cls.signon < SIGNONS)
382                 {
383                         Con_Printf("reconnecting...\n");
384                         MSG_WriteChar(&cls.netcon->message, qw_clc_stringcmd);
385                         MSG_WriteString(&cls.netcon->message, "new");
386                 }
387         }
388         else
389         {
390                 // netquake uses reconnect on level changes (silly)
391                 if (Cmd_Argc() != 1)
392                 {
393                         Con_Print("reconnect : wait for signon messages again\n");
394                         return;
395                 }
396                 if (!cls.signon)
397                 {
398                         Con_Print("reconnect: no signon, ignoring reconnect\n");
399                         return;
400                 }
401                 cls.signon = 0;         // need new connection messages
402         }
403 }
404
405 /*
406 =====================
407 Host_Connect_f
408
409 User command to connect to server
410 =====================
411 */
412 void Host_Connect_f (void)
413 {
414         if (Cmd_Argc() != 2)
415         {
416                 Con_Print("connect <serveraddress> : connect to a multiplayer game\n");
417                 return;
418         }
419         CL_EstablishConnection(Cmd_Argv(1));
420 }
421
422
423 /*
424 ===============================================================================
425
426 LOAD / SAVE GAME
427
428 ===============================================================================
429 */
430
431 #define SAVEGAME_VERSION        5
432
433 /*
434 ===============
435 Host_Savegame_f
436 ===============
437 */
438 void Host_Savegame_f (void)
439 {
440         char    name[MAX_QPATH];
441         qfile_t *f;
442         int             i;
443         char    comment[SAVEGAME_COMMENT_LENGTH+1];
444
445         if (!sv.active)
446         {
447                 Con_Print("Can't save - no server running.\n");
448                 return;
449         }
450
451         if (cl.islocalgame)
452         {
453                 // singleplayer checks
454                 if (cl.intermission)
455                 {
456                         Con_Print("Can't save in intermission.\n");
457                         return;
458                 }
459
460                 if (svs.clients[0].active && svs.clients[0].edict->fields.server->deadflag)
461                 {
462                         Con_Print("Can't savegame with a dead player\n");
463                         return;
464                 }
465         }
466         else
467                 Con_Print("Warning: saving a multiplayer game may have strange results when restored (to properly resume, all players must join in the same player slots and then the game can be reloaded).\n");
468
469         if (Cmd_Argc() != 2)
470         {
471                 Con_Print("save <savename> : save a game\n");
472                 return;
473         }
474
475         if (strstr(Cmd_Argv(1), ".."))
476         {
477                 Con_Print("Relative pathnames are not allowed.\n");
478                 return;
479         }
480
481         strlcpy (name, Cmd_Argv(1), sizeof (name));
482         FS_DefaultExtension (name, ".sav", sizeof (name));
483
484         Con_Printf("Saving game to %s...\n", name);
485         f = FS_Open (name, "wb", false, false);
486         if (!f)
487         {
488                 Con_Print("ERROR: couldn't open.\n");
489                 return;
490         }
491
492         SV_VM_Begin();
493
494         FS_Printf(f, "%i\n", SAVEGAME_VERSION);
495
496         memset(comment, 0, sizeof(comment));
497         sprintf(comment, "%-21s kills:%3i/%3i", PRVM_GetString(prog->edicts->fields.server->message), (int)prog->globals.server->killed_monsters, (int)prog->globals.server->total_monsters);
498         // convert space to _ to make stdio happy
499         // LordHavoc: convert control characters to _ as well
500         for (i=0 ; i<SAVEGAME_COMMENT_LENGTH ; i++)
501                 if (comment[i] <= ' ')
502                         comment[i] = '_';
503         comment[SAVEGAME_COMMENT_LENGTH] = '\0';
504
505         FS_Printf(f, "%s\n", comment);
506         for (i=0 ; i<NUM_SPAWN_PARMS ; i++)
507                 FS_Printf(f, "%f\n", svs.clients[0].spawn_parms[i]);
508         FS_Printf(f, "%d\n", current_skill);
509         FS_Printf(f, "%s\n", sv.name);
510         FS_Printf(f, "%f\n",sv.time);
511
512         // write the light styles
513         for (i=0 ; i<MAX_LIGHTSTYLES ; i++)
514         {
515                 if (sv.lightstyles[i][0])
516                         FS_Printf(f, "%s\n", sv.lightstyles[i]);
517                 else
518                         FS_Print(f,"m\n");
519         }
520
521         PRVM_ED_WriteGlobals (f);
522         for (i=0 ; i<prog->num_edicts ; i++)
523                 PRVM_ED_Write (f, PRVM_EDICT_NUM(i));
524
525         SV_VM_End();
526
527         FS_Close (f);
528         Con_Print("done.\n");
529 }
530
531
532 /*
533 ===============
534 Host_Loadgame_f
535 ===============
536 */
537 void Host_Loadgame_f (void)
538 {
539         char filename[MAX_QPATH];
540         char mapname[MAX_QPATH];
541         float time;
542         const char *start;
543         const char *t;
544         const char *oldt;
545         char *text;
546         prvm_edict_t *ent;
547         int i;
548         int entnum;
549         int version;
550         float spawn_parms[NUM_SPAWN_PARMS];
551
552         if (Cmd_Argc() != 2)
553         {
554                 Con_Print("load <savename> : load a game\n");
555                 return;
556         }
557
558         strlcpy (filename, Cmd_Argv(1), sizeof(filename));
559         FS_DefaultExtension (filename, ".sav", sizeof (filename));
560
561         Con_Printf("Loading game from %s...\n", filename);
562
563         cls.demonum = -1;               // stop demo loop in case this fails
564
565         t = text = (char *)FS_LoadFile (filename, tempmempool, false, NULL);
566         if (!text)
567         {
568                 Con_Print("ERROR: couldn't open.\n");
569                 return;
570         }
571
572         // version
573         COM_ParseToken_Simple(&t, false, false);
574         version = atoi(com_token);
575         if (version != SAVEGAME_VERSION)
576         {
577                 Mem_Free(text);
578                 Con_Printf("Savegame is version %i, not %i\n", version, SAVEGAME_VERSION);
579                 return;
580         }
581
582         // description
583         COM_ParseToken_Simple(&t, false, false);
584
585         for (i = 0;i < NUM_SPAWN_PARMS;i++)
586         {
587                 COM_ParseToken_Simple(&t, false, false);
588                 spawn_parms[i] = atof(com_token);
589         }
590         // skill
591         COM_ParseToken_Simple(&t, false, false);
592 // this silliness is so we can load 1.06 save files, which have float skill values
593         current_skill = (int)(atof(com_token) + 0.5);
594         Cvar_SetValue ("skill", (float)current_skill);
595
596         // mapname
597         COM_ParseToken_Simple(&t, false, false);
598         strlcpy (mapname, com_token, sizeof(mapname));
599
600         // time
601         COM_ParseToken_Simple(&t, false, false);
602         time = atof(com_token);
603
604         allowcheats = sv_cheats.integer != 0;
605
606         SV_SpawnServer (mapname);
607         if (!sv.active)
608         {
609                 Mem_Free(text);
610                 Con_Print("Couldn't load map\n");
611                 return;
612         }
613         sv.paused = true;               // pause until all clients connect
614         sv.loadgame = true;
615
616 // load the light styles
617
618         for (i = 0;i < MAX_LIGHTSTYLES;i++)
619         {
620                 // light style
621                 oldt = t;
622                 COM_ParseToken_Simple(&t, false, false);
623                 // if this is a 64 lightstyle savegame produced by Quake, stop now
624                 // we have to check this because darkplaces saves 256 lightstyle savegames
625                 if (com_token[0] == '{')
626                 {
627                         t = oldt;
628                         break;
629                 }
630                 strlcpy(sv.lightstyles[i], com_token, sizeof(sv.lightstyles[i]));
631         }
632
633         // now skip everything before the first opening brace
634         // (this is for forward compatibility, so that older versions (at
635         // least ones with this fix) can load savegames with extra data before the
636         // first brace, as might be produced by a later engine version)
637         for(;;)
638         {
639                 oldt = t;
640                 COM_ParseToken_Simple(&t, false, false);
641                 if (com_token[0] == '{')
642                 {
643                         t = oldt;
644                         break;
645                 }
646         }
647
648 // load the edicts out of the savegame file
649         SV_VM_Begin();
650         // -1 is the globals
651         entnum = -1;
652         for (;;)
653         {
654                 start = t;
655                 while (COM_ParseToken_Simple(&t, false, false))
656                         if (!strcmp(com_token, "}"))
657                                 break;
658                 if (!COM_ParseToken_Simple(&start, false, false))
659                 {
660                         // end of file
661                         break;
662                 }
663                 if (strcmp(com_token,"{"))
664                 {
665                         Mem_Free(text);
666                         Host_Error ("First token isn't a brace");
667                 }
668
669                 if (entnum == -1)
670                 {
671                         // parse the global vars
672                         PRVM_ED_ParseGlobals (start);
673                 }
674                 else
675                 {
676                         // parse an edict
677                         if (entnum >= MAX_EDICTS)
678                         {
679                                 Mem_Free(text);
680                                 Host_Error("Host_PerformLoadGame: too many edicts in save file (reached MAX_EDICTS %i)", MAX_EDICTS);
681                         }
682                         while (entnum >= prog->max_edicts)
683                                 PRVM_MEM_IncreaseEdicts();
684                         ent = PRVM_EDICT_NUM(entnum);
685                         memset (ent->fields.server, 0, prog->progs->entityfields * 4);
686                         ent->priv.server->free = false;
687                         PRVM_ED_ParseEdict (start, ent);
688
689                         // link it into the bsp tree
690                         if (!ent->priv.server->free)
691                                 SV_LinkEdict (ent, false);
692                 }
693
694                 entnum++;
695         }
696         Mem_Free(text);
697
698         prog->num_edicts = entnum;
699         sv.time = time;
700
701         for (i = 0;i < NUM_SPAWN_PARMS;i++)
702                 svs.clients[0].spawn_parms[i] = spawn_parms[i];
703
704         SV_VM_End();
705
706         // make sure we're connected to loopback
707         if (sv.active && cls.state == ca_disconnected)
708                 CL_EstablishConnection("local:1");
709 }
710
711 //============================================================================
712
713 /*
714 ======================
715 Host_Name_f
716 ======================
717 */
718 cvar_t cl_name = {CVAR_SAVE | CVAR_NQUSERINFOHACK, "_cl_name", "player", "internal storage cvar for current player name (changed by name command)"};
719 void Host_Name_f (void)
720 {
721         int i, j;
722         qboolean valid_colors;
723         char newName[sizeof(host_client->name)];
724
725         if (Cmd_Argc () == 1)
726         {
727                 Con_Printf("\"name\" is \"%s\"\n", cl_name.string);
728                 return;
729         }
730
731         if (Cmd_Argc () == 2)
732                 strlcpy (newName, Cmd_Argv(1), sizeof (newName));
733         else
734                 strlcpy (newName, Cmd_Args(), sizeof (newName));
735
736         if (cmd_source == src_command)
737         {
738                 Cvar_Set ("_cl_name", newName);
739                 return;
740         }
741
742         if (realtime < host_client->nametime)
743         {
744                 SV_ClientPrintf("You can't change name more than once every 5 seconds!\n");
745                 return;
746         }
747
748         host_client->nametime = realtime + 5;
749
750         // point the string back at updateclient->name to keep it safe
751         strlcpy (host_client->name, newName, sizeof (host_client->name));
752
753         for (i = 0, j = 0;host_client->name[i];i++)
754                 if (host_client->name[i] != '\r' && host_client->name[i] != '\n')
755                         host_client->name[j++] = host_client->name[i];
756         host_client->name[j] = 0;
757
758         COM_StringLengthNoColors(host_client->name, 0, &valid_colors);
759         if(!valid_colors) // NOTE: this also proves the string is not empty, as "" is a valid colored string
760         {
761                 size_t l;
762                 l = strlen(host_client->name);
763                 if(l < sizeof(host_client->name) - 1)
764                 {
765                         // duplicate the color tag to escape it
766                         host_client->name[i] = STRING_COLOR_TAG;
767                         host_client->name[i+1] = 0;
768                         //Con_DPrintf("abuse detected, adding another trailing color tag\n");
769                 }
770                 else
771                 {
772                         // remove the last character to fix the color code
773                         host_client->name[l-1] = 0;
774                         //Con_DPrintf("abuse detected, removing a trailing color tag\n");
775                 }
776         }
777
778         // find the last color tag offset and decide if we need to add a reset tag
779         for (i = 0, j = -1;host_client->name[i];i++)
780         {
781                 if (host_client->name[i] == STRING_COLOR_TAG)
782                 {
783                         if (host_client->name[i+1] >= '0' && host_client->name[i+1] <= '9')
784                         {
785                                 j = i;
786                                 // if this happens to be a reset  tag then we don't need one
787                                 if (host_client->name[i+1] == '0' + STRING_COLOR_DEFAULT)
788                                         j = -1;
789                                 i++;
790                                 continue;
791                         }
792                         if (host_client->name[i+1] == STRING_COLOR_TAG)
793                         {
794                                 i++;
795                                 continue;
796                         }
797                 }
798         }
799         // does not end in the default color string, so add it
800         if (j >= 0 && strlen(host_client->name) < sizeof(host_client->name) - 2)
801                 memcpy(host_client->name + strlen(host_client->name), STRING_COLOR_DEFAULT_STR, strlen(STRING_COLOR_DEFAULT_STR) + 1);
802
803         host_client->edict->fields.server->netname = PRVM_SetEngineString(host_client->name);
804         if (strcmp(host_client->old_name, host_client->name))
805         {
806                 if (host_client->spawned)
807                         SV_BroadcastPrintf("%s changed name to %s\n", host_client->old_name, host_client->name);
808                 strlcpy(host_client->old_name, host_client->name, sizeof(host_client->old_name));
809                 // send notification to all clients
810                 MSG_WriteByte (&sv.reliable_datagram, svc_updatename);
811                 MSG_WriteByte (&sv.reliable_datagram, host_client - svs.clients);
812                 MSG_WriteString (&sv.reliable_datagram, host_client->name);
813                 SV_WriteNetnameIntoDemo(host_client);
814         }
815 }
816
817 /*
818 ======================
819 Host_Playermodel_f
820 ======================
821 */
822 cvar_t cl_playermodel = {CVAR_SAVE | CVAR_NQUSERINFOHACK, "_cl_playermodel", "", "internal storage cvar for current player model in Nexuiz (changed by playermodel command)"};
823 // the old cl_playermodel in cl_main has been renamed to __cl_playermodel
824 void Host_Playermodel_f (void)
825 {
826         int i, j;
827         char newPath[sizeof(host_client->playermodel)];
828
829         if (Cmd_Argc () == 1)
830         {
831                 Con_Printf("\"playermodel\" is \"%s\"\n", cl_playermodel.string);
832                 return;
833         }
834
835         if (Cmd_Argc () == 2)
836                 strlcpy (newPath, Cmd_Argv(1), sizeof (newPath));
837         else
838                 strlcpy (newPath, Cmd_Args(), sizeof (newPath));
839
840         for (i = 0, j = 0;newPath[i];i++)
841                 if (newPath[i] != '\r' && newPath[i] != '\n')
842                         newPath[j++] = newPath[i];
843         newPath[j] = 0;
844
845         if (cmd_source == src_command)
846         {
847                 Cvar_Set ("_cl_playermodel", newPath);
848                 return;
849         }
850
851         /*
852         if (realtime < host_client->nametime)
853         {
854                 SV_ClientPrintf("You can't change playermodel more than once every 5 seconds!\n");
855                 return;
856         }
857
858         host_client->nametime = realtime + 5;
859         */
860
861         // point the string back at updateclient->name to keep it safe
862         strlcpy (host_client->playermodel, newPath, sizeof (host_client->playermodel));
863         if( prog->fieldoffsets.playermodel >= 0 )
864                 PRVM_EDICTFIELDVALUE(host_client->edict, prog->fieldoffsets.playermodel)->string = PRVM_SetEngineString(host_client->playermodel);
865         if (strcmp(host_client->old_model, host_client->playermodel))
866         {
867                 strlcpy(host_client->old_model, host_client->playermodel, sizeof(host_client->old_model));
868                 /*// send notification to all clients
869                 MSG_WriteByte (&sv.reliable_datagram, svc_updatepmodel);
870                 MSG_WriteByte (&sv.reliable_datagram, host_client - svs.clients);
871                 MSG_WriteString (&sv.reliable_datagram, host_client->playermodel);*/
872         }
873 }
874
875 /*
876 ======================
877 Host_Playerskin_f
878 ======================
879 */
880 cvar_t cl_playerskin = {CVAR_SAVE | CVAR_NQUSERINFOHACK, "_cl_playerskin", "", "internal storage cvar for current player skin in Nexuiz (changed by playerskin command)"};
881 void Host_Playerskin_f (void)
882 {
883         int i, j;
884         char newPath[sizeof(host_client->playerskin)];
885
886         if (Cmd_Argc () == 1)
887         {
888                 Con_Printf("\"playerskin\" is \"%s\"\n", cl_playerskin.string);
889                 return;
890         }
891
892         if (Cmd_Argc () == 2)
893                 strlcpy (newPath, Cmd_Argv(1), sizeof (newPath));
894         else
895                 strlcpy (newPath, Cmd_Args(), sizeof (newPath));
896
897         for (i = 0, j = 0;newPath[i];i++)
898                 if (newPath[i] != '\r' && newPath[i] != '\n')
899                         newPath[j++] = newPath[i];
900         newPath[j] = 0;
901
902         if (cmd_source == src_command)
903         {
904                 Cvar_Set ("_cl_playerskin", newPath);
905                 return;
906         }
907
908         /*
909         if (realtime < host_client->nametime)
910         {
911                 SV_ClientPrintf("You can't change playermodel more than once every 5 seconds!\n");
912                 return;
913         }
914
915         host_client->nametime = realtime + 5;
916         */
917
918         // point the string back at updateclient->name to keep it safe
919         strlcpy (host_client->playerskin, newPath, sizeof (host_client->playerskin));
920         if( prog->fieldoffsets.playerskin >= 0 )
921                 PRVM_EDICTFIELDVALUE(host_client->edict, prog->fieldoffsets.playerskin)->string = PRVM_SetEngineString(host_client->playerskin);
922         if (strcmp(host_client->old_skin, host_client->playerskin))
923         {
924                 //if (host_client->spawned)
925                 //      SV_BroadcastPrintf("%s changed skin to %s\n", host_client->name, host_client->playerskin);
926                 strlcpy(host_client->old_skin, host_client->playerskin, sizeof(host_client->old_skin));
927                 /*// send notification to all clients
928                 MSG_WriteByte (&sv.reliable_datagram, svc_updatepskin);
929                 MSG_WriteByte (&sv.reliable_datagram, host_client - svs.clients);
930                 MSG_WriteString (&sv.reliable_datagram, host_client->playerskin);*/
931         }
932 }
933
934 void Host_Version_f (void)
935 {
936         Con_Printf("Version: %s build %s\n", gamename, buildstring);
937 }
938
939 void Host_Say(qboolean teamonly)
940 {
941         client_t *save;
942         int j, quoted;
943         const char *p1;
944         char *p2;
945         // LordHavoc: long say messages
946         char text[1024];
947         qboolean fromServer = false;
948
949         if (cmd_source == src_command)
950         {
951                 if (cls.state == ca_dedicated)
952                 {
953                         fromServer = true;
954                         teamonly = false;
955                 }
956                 else
957                 {
958                         Cmd_ForwardToServer ();
959                         return;
960                 }
961         }
962
963         if (Cmd_Argc () < 2)
964                 return;
965
966         if (!teamplay.integer)
967                 teamonly = false;
968
969         p1 = Cmd_Args();
970         quoted = false;
971         if (*p1 == '\"')
972         {
973                 quoted = true;
974                 p1++;
975         }
976         // note this uses the chat prefix \001
977         if (!fromServer && !teamonly)
978                 dpsnprintf (text, sizeof(text), "\001%s: %s", host_client->name, p1);
979         else if (!fromServer && teamonly)
980                 dpsnprintf (text, sizeof(text), "\001(%s): %s", host_client->name, p1);
981         else if(*(sv_adminnick.string))
982                 dpsnprintf (text, sizeof(text), "\001<%s> %s", sv_adminnick.string, p1);
983         else
984                 dpsnprintf (text, sizeof(text), "\001<%s> %s", hostname.string, p1);
985         p2 = text + strlen(text);
986         while ((const char *)p2 > (const char *)text && (p2[-1] == '\r' || p2[-1] == '\n' || (p2[-1] == '\"' && quoted)))
987         {
988                 if (p2[-1] == '\"' && quoted)
989                         quoted = false;
990                 p2[-1] = 0;
991                 p2--;
992         }
993         strlcat(text, "\n", sizeof(text));
994
995         // note: save is not a valid edict if fromServer is true
996         save = host_client;
997         for (j = 0, host_client = svs.clients;j < svs.maxclients;j++, host_client++)
998                 if (host_client->active && (!teamonly || host_client->edict->fields.server->team == save->edict->fields.server->team))
999                         SV_ClientPrint(text);
1000         host_client = save;
1001
1002         if (cls.state == ca_dedicated)
1003                 Con_Print(&text[1]);
1004 }
1005
1006
1007 void Host_Say_f(void)
1008 {
1009         Host_Say(false);
1010 }
1011
1012
1013 void Host_Say_Team_f(void)
1014 {
1015         Host_Say(true);
1016 }
1017
1018
1019 void Host_Tell_f(void)
1020 {
1021         const char *playername_start = NULL;
1022         size_t playername_length = 0;
1023         int playernumber = 0;
1024         client_t *save;
1025         int j;
1026         const char *p1, *p2;
1027         char text[MAX_INPUTLINE]; // LordHavoc: FIXME: temporary buffer overflow fix (was 64)
1028         qboolean fromServer = false;
1029
1030         if (cmd_source == src_command)
1031         {
1032                 if (cls.state == ca_dedicated)
1033                         fromServer = true;
1034                 else
1035                 {
1036                         Cmd_ForwardToServer ();
1037                         return;
1038                 }
1039         }
1040
1041         if (Cmd_Argc () < 2)
1042                 return;
1043
1044         // note this uses the chat prefix \001
1045         if (!fromServer)
1046                 sprintf (text, "\001%s tells you: ", host_client->name);
1047         else if(*(sv_adminnick.string))
1048                 sprintf (text, "\001<%s tells you> ", sv_adminnick.string);
1049         else
1050                 sprintf (text, "\001<%s tells you> ", hostname.string);
1051
1052         p1 = Cmd_Args();
1053         p2 = p1 + strlen(p1);
1054         // remove the target name
1055         while (p1 < p2 && *p1 == ' ')
1056                 p1++;
1057         if(*p1 == '#')
1058         {
1059                 ++p1;
1060                 while (p1 < p2 && *p1 == ' ')
1061                         p1++;
1062                 while (p1 < p2 && isdigit(*p1))
1063                 {
1064                         playernumber = playernumber * 10 + (*p1 - '0');
1065                         p1++;
1066                 }
1067                 --playernumber;
1068         }
1069         else if(*p1 == '"')
1070         {
1071                 ++p1;
1072                 playername_start = p1;
1073                 while (p1 < p2 && *p1 != '"')
1074                         p1++;
1075                 playername_length = p1 - playername_start;
1076                 if(p1 < p2)
1077                         p1++;
1078         }
1079         else
1080         {
1081                 playername_start = p1;
1082                 while (p1 < p2 && *p1 != ' ')
1083                         p1++;
1084                 playername_length = p1 - playername_start;
1085         }
1086         while (p1 < p2 && *p1 == ' ')
1087                 p1++;
1088         if(playername_start)
1089         {
1090                 // set playernumber to the right client
1091                 char namebuf[128];
1092                 if(playername_length >= sizeof(namebuf))
1093                 {
1094                         if (fromServer)
1095                                 Con_Print("Host_Tell: too long player name/ID\n");
1096                         else
1097                                 SV_ClientPrint("Host_Tell: too long player name/ID\n");
1098                         return;
1099                 }
1100                 memcpy(namebuf, playername_start, playername_length);
1101                 namebuf[playername_length] = 0;
1102                 for (playernumber = 0; playernumber < svs.maxclients; playernumber++)
1103                 {
1104                         if (!svs.clients[playernumber].active)
1105                                 continue;
1106                         if (strcasecmp(svs.clients[playernumber].name, namebuf) == 0)
1107                                 break;
1108                 }
1109         }
1110         if(playernumber < 0 || playernumber >= svs.maxclients || !(svs.clients[playernumber].active))
1111         {
1112                 if (fromServer)
1113                         Con_Print("Host_Tell: invalid player name/ID\n");
1114                 else
1115                         SV_ClientPrint("Host_Tell: invalid player name/ID\n");
1116                 return;
1117         }
1118         // remove trailing newlines
1119         while (p2 > p1 && (p2[-1] == '\n' || p2[-1] == '\r'))
1120                 p2--;
1121         // remove quotes if present
1122         if (*p1 == '"')
1123         {
1124                 p1++;
1125                 if (p2[-1] == '"')
1126                         p2--;
1127                 else if (fromServer)
1128                         Con_Print("Host_Tell: missing end quote\n");
1129                 else
1130                         SV_ClientPrint("Host_Tell: missing end quote\n");
1131         }
1132         while (p2 > p1 && (p2[-1] == '\n' || p2[-1] == '\r'))
1133                 p2--;
1134         if(p1 == p2)
1135                 return; // empty say
1136         for (j = (int)strlen(text);j < (int)(sizeof(text) - 2) && p1 < p2;)
1137                 text[j++] = *p1++;
1138         text[j++] = '\n';
1139         text[j++] = 0;
1140
1141         save = host_client;
1142         host_client = svs.clients + playernumber;
1143         SV_ClientPrint(text);
1144         host_client = save;
1145 }
1146
1147
1148 /*
1149 ==================
1150 Host_Color_f
1151 ==================
1152 */
1153 cvar_t cl_color = {CVAR_SAVE | CVAR_NQUSERINFOHACK, "_cl_color", "0", "internal storage cvar for current player colors (changed by color command)"};
1154 void Host_Color(int changetop, int changebottom)
1155 {
1156         int top, bottom, playercolor;
1157
1158         // get top and bottom either from the provided values or the current values
1159         // (allows changing only top or bottom, or both at once)
1160         top = changetop >= 0 ? changetop : (cl_color.integer >> 4);
1161         bottom = changebottom >= 0 ? changebottom : cl_color.integer;
1162
1163         top &= 15;
1164         bottom &= 15;
1165         // LordHavoc: allowing skin colormaps 14 and 15 by commenting this out
1166         //if (top > 13)
1167         //      top = 13;
1168         //if (bottom > 13)
1169         //      bottom = 13;
1170
1171         playercolor = top*16 + bottom;
1172
1173         if (cmd_source == src_command)
1174         {
1175                 Cvar_SetValueQuick(&cl_color, playercolor);
1176                 return;
1177         }
1178
1179         if (cls.protocol == PROTOCOL_QUAKEWORLD)
1180                 return;
1181
1182         if (host_client->edict && prog->funcoffsets.SV_ChangeTeam)
1183         {
1184                 Con_DPrint("Calling SV_ChangeTeam\n");
1185                 prog->globals.server->time = sv.time;
1186                 prog->globals.generic[OFS_PARM0] = playercolor;
1187                 prog->globals.server->self = PRVM_EDICT_TO_PROG(host_client->edict);
1188                 PRVM_ExecuteProgram(prog->funcoffsets.SV_ChangeTeam, "QC function SV_ChangeTeam is missing");
1189         }
1190         else
1191         {
1192                 prvm_eval_t *val;
1193                 if (host_client->edict)
1194                 {
1195                         if ((val = PRVM_EDICTFIELDVALUE(host_client->edict, prog->fieldoffsets.clientcolors)))
1196                                 val->_float = playercolor;
1197                         host_client->edict->fields.server->team = bottom + 1;
1198                 }
1199                 host_client->colors = playercolor;
1200                 if (host_client->old_colors != host_client->colors)
1201                 {
1202                         host_client->old_colors = host_client->colors;
1203                         // send notification to all clients
1204                         MSG_WriteByte (&sv.reliable_datagram, svc_updatecolors);
1205                         MSG_WriteByte (&sv.reliable_datagram, host_client - svs.clients);
1206                         MSG_WriteByte (&sv.reliable_datagram, host_client->colors);
1207                 }
1208         }
1209 }
1210
1211 void Host_Color_f(void)
1212 {
1213         int             top, bottom;
1214
1215         if (Cmd_Argc() == 1)
1216         {
1217                 Con_Printf("\"color\" is \"%i %i\"\n", cl_color.integer >> 4, cl_color.integer & 15);
1218                 Con_Print("color <0-15> [0-15]\n");
1219                 return;
1220         }
1221
1222         if (Cmd_Argc() == 2)
1223                 top = bottom = atoi(Cmd_Argv(1));
1224         else
1225         {
1226                 top = atoi(Cmd_Argv(1));
1227                 bottom = atoi(Cmd_Argv(2));
1228         }
1229         Host_Color(top, bottom);
1230 }
1231
1232 void Host_TopColor_f(void)
1233 {
1234         if (Cmd_Argc() == 1)
1235         {
1236                 Con_Printf("\"topcolor\" is \"%i\"\n", (cl_color.integer >> 4) & 15);
1237                 Con_Print("topcolor <0-15>\n");
1238                 return;
1239         }
1240
1241         Host_Color(atoi(Cmd_Argv(1)), -1);
1242 }
1243
1244 void Host_BottomColor_f(void)
1245 {
1246         if (Cmd_Argc() == 1)
1247         {
1248                 Con_Printf("\"bottomcolor\" is \"%i\"\n", cl_color.integer & 15);
1249                 Con_Print("bottomcolor <0-15>\n");
1250                 return;
1251         }
1252
1253         Host_Color(-1, atoi(Cmd_Argv(1)));
1254 }
1255
1256 cvar_t cl_rate = {CVAR_SAVE | CVAR_NQUSERINFOHACK, "_cl_rate", "20000", "internal storage cvar for current rate (changed by rate command)"};
1257 void Host_Rate_f(void)
1258 {
1259         int rate;
1260
1261         if (Cmd_Argc() != 2)
1262         {
1263                 Con_Printf("\"rate\" is \"%i\"\n", cl_rate.integer);
1264                 Con_Print("rate <bytespersecond>\n");
1265                 return;
1266         }
1267
1268         rate = atoi(Cmd_Argv(1));
1269
1270         if (cmd_source == src_command)
1271         {
1272                 Cvar_SetValue ("_cl_rate", max(NET_MINRATE, rate));
1273                 return;
1274         }
1275
1276         host_client->rate = rate;
1277 }
1278
1279 /*
1280 ==================
1281 Host_Kill_f
1282 ==================
1283 */
1284 void Host_Kill_f (void)
1285 {
1286         if (host_client->edict->fields.server->health <= 0)
1287         {
1288                 SV_ClientPrint("Can't suicide -- already dead!\n");
1289                 return;
1290         }
1291
1292         prog->globals.server->time = sv.time;
1293         prog->globals.server->self = PRVM_EDICT_TO_PROG(host_client->edict);
1294         PRVM_ExecuteProgram (prog->globals.server->ClientKill, "QC function ClientKill is missing");
1295 }
1296
1297
1298 /*
1299 ==================
1300 Host_Pause_f
1301 ==================
1302 */
1303 void Host_Pause_f (void)
1304 {
1305         if (!pausable.integer)
1306                 SV_ClientPrint("Pause not allowed.\n");
1307         else
1308         {
1309                 sv.paused ^= 1;
1310                 SV_BroadcastPrintf("%s %spaused the game\n", host_client->name, sv.paused ? "" : "un");
1311                 // send notification to all clients
1312                 MSG_WriteByte(&sv.reliable_datagram, svc_setpause);
1313                 MSG_WriteByte(&sv.reliable_datagram, sv.paused);
1314         }
1315 }
1316
1317 /*
1318 ======================
1319 Host_PModel_f
1320 LordHavoc: only supported for Nehahra, I personally think this is dumb, but Mindcrime won't listen.
1321 LordHavoc: correction, Mindcrime will be removing pmodel in the future, but it's still stuck here for compatibility.
1322 ======================
1323 */
1324 cvar_t cl_pmodel = {CVAR_SAVE | CVAR_NQUSERINFOHACK, "_cl_pmodel", "0", "internal storage cvar for current player model number in nehahra (changed by pmodel command)"};
1325 static void Host_PModel_f (void)
1326 {
1327         int i;
1328         prvm_eval_t *val;
1329
1330         if (Cmd_Argc () == 1)
1331         {
1332                 Con_Printf("\"pmodel\" is \"%s\"\n", cl_pmodel.string);
1333                 return;
1334         }
1335         i = atoi(Cmd_Argv(1));
1336
1337         if (cmd_source == src_command)
1338         {
1339                 if (cl_pmodel.integer == i)
1340                         return;
1341                 Cvar_SetValue ("_cl_pmodel", i);
1342                 if (cls.state == ca_connected)
1343                         Cmd_ForwardToServer ();
1344                 return;
1345         }
1346
1347         if (host_client->edict && (val = PRVM_EDICTFIELDVALUE(host_client->edict, prog->fieldoffsets.pmodel)))
1348                 val->_float = i;
1349 }
1350
1351 //===========================================================================
1352
1353
1354 /*
1355 ==================
1356 Host_PreSpawn_f
1357 ==================
1358 */
1359 void Host_PreSpawn_f (void)
1360 {
1361         if (host_client->spawned)
1362         {
1363                 Con_Print("prespawn not valid -- already spawned\n");
1364                 return;
1365         }
1366
1367         if (host_client->netconnection)
1368         {
1369                 SZ_Write (&host_client->netconnection->message, sv.signon.data, sv.signon.cursize);
1370                 MSG_WriteByte (&host_client->netconnection->message, svc_signonnum);
1371                 MSG_WriteByte (&host_client->netconnection->message, 2);
1372                 host_client->sendsignon = 0;            // enable unlimited sends again
1373         }
1374
1375         // reset the name change timer because the client will send name soon
1376         host_client->nametime = 0;
1377 }
1378
1379 /*
1380 ==================
1381 Host_Spawn_f
1382 ==================
1383 */
1384 void Host_Spawn_f (void)
1385 {
1386         int i;
1387         client_t *client;
1388         int stats[MAX_CL_STATS];
1389
1390         if (host_client->spawned)
1391         {
1392                 Con_Print("Spawn not valid -- already spawned\n");
1393                 return;
1394         }
1395
1396         // reset name change timer again because they might want to change name
1397         // again in the first 5 seconds after connecting
1398         host_client->nametime = 0;
1399
1400         // LordHavoc: moved this above the QC calls at FrikaC's request
1401         // LordHavoc: commented this out
1402         //if (host_client->netconnection)
1403         //      SZ_Clear (&host_client->netconnection->message);
1404
1405         // run the entrance script
1406         if (sv.loadgame)
1407         {
1408                 // loaded games are fully initialized already
1409                 if (prog->funcoffsets.RestoreGame)
1410                 {
1411                         Con_DPrint("Calling RestoreGame\n");
1412                         prog->globals.server->time = sv.time;
1413                         prog->globals.server->self = PRVM_EDICT_TO_PROG(host_client->edict);
1414                         PRVM_ExecuteProgram(prog->funcoffsets.RestoreGame, "QC function RestoreGame is missing");
1415                 }
1416         }
1417         else
1418         {
1419                 //Con_Printf("Host_Spawn_f: host_client->edict->netname = %s, host_client->edict->netname = %s, host_client->name = %s\n", PRVM_GetString(host_client->edict->fields.server->netname), PRVM_GetString(host_client->edict->fields.server->netname), host_client->name);
1420
1421                 // copy spawn parms out of the client_t
1422                 for (i=0 ; i< NUM_SPAWN_PARMS ; i++)
1423                         (&prog->globals.server->parm1)[i] = host_client->spawn_parms[i];
1424
1425                 // call the spawn function
1426                 host_client->clientconnectcalled = true;
1427                 prog->globals.server->time = sv.time;
1428                 prog->globals.server->self = PRVM_EDICT_TO_PROG(host_client->edict);
1429                 PRVM_ExecuteProgram (prog->globals.server->ClientConnect, "QC function ClientConnect is missing");
1430
1431                 if (cls.state == ca_dedicated)
1432                         Con_Printf("%s connected\n", host_client->name);
1433
1434                 PRVM_ExecuteProgram (prog->globals.server->PutClientInServer, "QC function PutClientInServer is missing");
1435         }
1436
1437         if (!host_client->netconnection)
1438                 return;
1439
1440         // send time of update
1441         MSG_WriteByte (&host_client->netconnection->message, svc_time);
1442         MSG_WriteFloat (&host_client->netconnection->message, sv.time);
1443
1444         // send all current names, colors, and frag counts
1445         for (i = 0, client = svs.clients;i < svs.maxclients;i++, client++)
1446         {
1447                 if (!client->active)
1448                         continue;
1449                 MSG_WriteByte (&host_client->netconnection->message, svc_updatename);
1450                 MSG_WriteByte (&host_client->netconnection->message, i);
1451                 MSG_WriteString (&host_client->netconnection->message, client->name);
1452                 MSG_WriteByte (&host_client->netconnection->message, svc_updatefrags);
1453                 MSG_WriteByte (&host_client->netconnection->message, i);
1454                 MSG_WriteShort (&host_client->netconnection->message, client->frags);
1455                 MSG_WriteByte (&host_client->netconnection->message, svc_updatecolors);
1456                 MSG_WriteByte (&host_client->netconnection->message, i);
1457                 MSG_WriteByte (&host_client->netconnection->message, client->colors);
1458         }
1459
1460         // send all current light styles
1461         for (i=0 ; i<MAX_LIGHTSTYLES ; i++)
1462         {
1463                 if (sv.lightstyles[i][0])
1464                 {
1465                         MSG_WriteByte (&host_client->netconnection->message, svc_lightstyle);
1466                         MSG_WriteByte (&host_client->netconnection->message, (char)i);
1467                         MSG_WriteString (&host_client->netconnection->message, sv.lightstyles[i]);
1468                 }
1469         }
1470
1471         // send some stats
1472         MSG_WriteByte (&host_client->netconnection->message, svc_updatestat);
1473         MSG_WriteByte (&host_client->netconnection->message, STAT_TOTALSECRETS);
1474         MSG_WriteLong (&host_client->netconnection->message, (int)prog->globals.server->total_secrets);
1475
1476         MSG_WriteByte (&host_client->netconnection->message, svc_updatestat);
1477         MSG_WriteByte (&host_client->netconnection->message, STAT_TOTALMONSTERS);
1478         MSG_WriteLong (&host_client->netconnection->message, (int)prog->globals.server->total_monsters);
1479
1480         MSG_WriteByte (&host_client->netconnection->message, svc_updatestat);
1481         MSG_WriteByte (&host_client->netconnection->message, STAT_SECRETS);
1482         MSG_WriteLong (&host_client->netconnection->message, (int)prog->globals.server->found_secrets);
1483
1484         MSG_WriteByte (&host_client->netconnection->message, svc_updatestat);
1485         MSG_WriteByte (&host_client->netconnection->message, STAT_MONSTERS);
1486         MSG_WriteLong (&host_client->netconnection->message, (int)prog->globals.server->killed_monsters);
1487
1488         // send a fixangle
1489         // Never send a roll angle, because savegames can catch the server
1490         // in a state where it is expecting the client to correct the angle
1491         // and it won't happen if the game was just loaded, so you wind up
1492         // with a permanent head tilt
1493         if (sv.loadgame)
1494         {
1495                 MSG_WriteByte (&host_client->netconnection->message, svc_setangle);
1496                 MSG_WriteAngle (&host_client->netconnection->message, host_client->edict->fields.server->v_angle[0], sv.protocol);
1497                 MSG_WriteAngle (&host_client->netconnection->message, host_client->edict->fields.server->v_angle[1], sv.protocol);
1498                 MSG_WriteAngle (&host_client->netconnection->message, 0, sv.protocol);
1499         }
1500         else
1501         {
1502                 MSG_WriteByte (&host_client->netconnection->message, svc_setangle);
1503                 MSG_WriteAngle (&host_client->netconnection->message, host_client->edict->fields.server->angles[0], sv.protocol);
1504                 MSG_WriteAngle (&host_client->netconnection->message, host_client->edict->fields.server->angles[1], sv.protocol);
1505                 MSG_WriteAngle (&host_client->netconnection->message, 0, sv.protocol);
1506         }
1507
1508         SV_WriteClientdataToMessage (host_client, host_client->edict, &host_client->netconnection->message, stats);
1509
1510         MSG_WriteByte (&host_client->netconnection->message, svc_signonnum);
1511         MSG_WriteByte (&host_client->netconnection->message, 3);
1512 }
1513
1514 /*
1515 ==================
1516 Host_Begin_f
1517 ==================
1518 */
1519 void Host_Begin_f (void)
1520 {
1521         host_client->spawned = true;
1522
1523         // LordHavoc: note: this code also exists in SV_DropClient
1524         if (sv.loadgame)
1525         {
1526                 int i;
1527                 for (i = 0;i < svs.maxclients;i++)
1528                         if (svs.clients[i].active && !svs.clients[i].spawned)
1529                                 break;
1530                 if (i == svs.maxclients)
1531                 {
1532                         Con_Printf("Loaded game, everyone rejoined - unpausing\n");
1533                         sv.paused = sv.loadgame = false; // we're basically done with loading now
1534                 }
1535         }
1536 }
1537
1538 //===========================================================================
1539
1540
1541 /*
1542 ==================
1543 Host_Kick_f
1544
1545 Kicks a user off of the server
1546 ==================
1547 */
1548 void Host_Kick_f (void)
1549 {
1550         char *who;
1551         const char *message = NULL;
1552         client_t *save;
1553         int i;
1554         qboolean byNumber = false;
1555
1556         if (!sv.active)
1557                 return;
1558
1559         SV_VM_Begin();
1560         save = host_client;
1561
1562         if (Cmd_Argc() > 2 && strcmp(Cmd_Argv(1), "#") == 0)
1563         {
1564                 i = (int)(atof(Cmd_Argv(2)) - 1);
1565                 if (i < 0 || i >= svs.maxclients || !(host_client = svs.clients + i)->active)
1566                         return;
1567                 byNumber = true;
1568         }
1569         else
1570         {
1571                 for (i = 0, host_client = svs.clients;i < svs.maxclients;i++, host_client++)
1572                 {
1573                         if (!host_client->active)
1574                                 continue;
1575                         if (strcasecmp(host_client->name, Cmd_Argv(1)) == 0)
1576                                 break;
1577                 }
1578         }
1579
1580         if (i < svs.maxclients)
1581         {
1582                 if (cmd_source == src_command)
1583                 {
1584                         if (cls.state == ca_dedicated)
1585                                 who = "Console";
1586                         else
1587                                 who = cl_name.string;
1588                 }
1589                 else
1590                         who = save->name;
1591
1592                 // can't kick yourself!
1593                 if (host_client == save)
1594                         return;
1595
1596                 if (Cmd_Argc() > 2)
1597                 {
1598                         message = Cmd_Args();
1599                         COM_ParseToken_Simple(&message, false, false);
1600                         if (byNumber)
1601                         {
1602                                 message++;                                                      // skip the #
1603                                 while (*message == ' ')                         // skip white space
1604                                         message++;
1605                                 message += strlen(Cmd_Argv(2)); // skip the number
1606                         }
1607                         while (*message && *message == ' ')
1608                                 message++;
1609                 }
1610                 if (message)
1611                         SV_ClientPrintf("Kicked by %s: %s\n", who, message);
1612                 else
1613                         SV_ClientPrintf("Kicked by %s\n", who);
1614                 SV_DropClient (false); // kicked
1615         }
1616
1617         host_client = save;
1618         SV_VM_End();
1619 }
1620
1621 /*
1622 ===============================================================================
1623
1624 DEBUGGING TOOLS
1625
1626 ===============================================================================
1627 */
1628
1629 /*
1630 ==================
1631 Host_Give_f
1632 ==================
1633 */
1634 void Host_Give_f (void)
1635 {
1636         const char *t;
1637         int v;
1638         prvm_eval_t *val;
1639
1640         if (!allowcheats)
1641         {
1642                 SV_ClientPrint("No cheats allowed, use sv_cheats 1 and restart level to enable.\n");
1643                 return;
1644         }
1645
1646         t = Cmd_Argv(1);
1647         v = atoi (Cmd_Argv(2));
1648
1649         switch (t[0])
1650         {
1651         case '0':
1652         case '1':
1653         case '2':
1654         case '3':
1655         case '4':
1656         case '5':
1657         case '6':
1658         case '7':
1659         case '8':
1660         case '9':
1661                 // MED 01/04/97 added hipnotic give stuff
1662                 if (gamemode == GAME_HIPNOTIC)
1663                 {
1664                         if (t[0] == '6')
1665                         {
1666                                 if (t[1] == 'a')
1667                                         host_client->edict->fields.server->items = (int)host_client->edict->fields.server->items | HIT_PROXIMITY_GUN;
1668                                 else
1669                                         host_client->edict->fields.server->items = (int)host_client->edict->fields.server->items | IT_GRENADE_LAUNCHER;
1670                         }
1671                         else if (t[0] == '9')
1672                                 host_client->edict->fields.server->items = (int)host_client->edict->fields.server->items | HIT_LASER_CANNON;
1673                         else if (t[0] == '0')
1674                                 host_client->edict->fields.server->items = (int)host_client->edict->fields.server->items | HIT_MJOLNIR;
1675                         else if (t[0] >= '2')
1676                                 host_client->edict->fields.server->items = (int)host_client->edict->fields.server->items | (IT_SHOTGUN << (t[0] - '2'));
1677                 }
1678                 else
1679                 {
1680                         if (t[0] >= '2')
1681                                 host_client->edict->fields.server->items = (int)host_client->edict->fields.server->items | (IT_SHOTGUN << (t[0] - '2'));
1682                 }
1683                 break;
1684
1685         case 's':
1686                 if (gamemode == GAME_ROGUE && (val = PRVM_EDICTFIELDVALUE(host_client->edict, prog->fieldoffsets.ammo_shells1)))
1687                         val->_float = v;
1688
1689                 host_client->edict->fields.server->ammo_shells = v;
1690                 break;
1691         case 'n':
1692                 if (gamemode == GAME_ROGUE)
1693                 {
1694                         if ((val = PRVM_EDICTFIELDVALUE(host_client->edict, prog->fieldoffsets.ammo_nails1)))
1695                         {
1696                                 val->_float = v;
1697                                 if (host_client->edict->fields.server->weapon <= IT_LIGHTNING)
1698                                         host_client->edict->fields.server->ammo_nails = v;
1699                         }
1700                 }
1701                 else
1702                 {
1703                         host_client->edict->fields.server->ammo_nails = v;
1704                 }
1705                 break;
1706         case 'l':
1707                 if (gamemode == GAME_ROGUE)
1708                 {
1709                         val = PRVM_EDICTFIELDVALUE(host_client->edict, prog->fieldoffsets.ammo_lava_nails);
1710                         if (val)
1711                         {
1712                                 val->_float = v;
1713                                 if (host_client->edict->fields.server->weapon > IT_LIGHTNING)
1714                                         host_client->edict->fields.server->ammo_nails = v;
1715                         }
1716                 }
1717                 break;
1718         case 'r':
1719                 if (gamemode == GAME_ROGUE)
1720                 {
1721                         val = PRVM_EDICTFIELDVALUE(host_client->edict, prog->fieldoffsets.ammo_rockets1);
1722                         if (val)
1723                         {
1724                                 val->_float = v;
1725                                 if (host_client->edict->fields.server->weapon <= IT_LIGHTNING)
1726                                         host_client->edict->fields.server->ammo_rockets = v;
1727                         }
1728                 }
1729                 else
1730                 {
1731                         host_client->edict->fields.server->ammo_rockets = v;
1732                 }
1733                 break;
1734         case 'm':
1735                 if (gamemode == GAME_ROGUE)
1736                 {
1737                         val = PRVM_EDICTFIELDVALUE(host_client->edict, prog->fieldoffsets.ammo_multi_rockets);
1738                         if (val)
1739                         {
1740                                 val->_float = v;
1741                                 if (host_client->edict->fields.server->weapon > IT_LIGHTNING)
1742                                         host_client->edict->fields.server->ammo_rockets = v;
1743                         }
1744                 }
1745                 break;
1746         case 'h':
1747                 host_client->edict->fields.server->health = v;
1748                 break;
1749         case 'c':
1750                 if (gamemode == GAME_ROGUE)
1751                 {
1752                         val = PRVM_EDICTFIELDVALUE(host_client->edict, prog->fieldoffsets.ammo_cells1);
1753                         if (val)
1754                         {
1755                                 val->_float = v;
1756                                 if (host_client->edict->fields.server->weapon <= IT_LIGHTNING)
1757                                         host_client->edict->fields.server->ammo_cells = v;
1758                         }
1759                 }
1760                 else
1761                 {
1762                         host_client->edict->fields.server->ammo_cells = v;
1763                 }
1764                 break;
1765         case 'p':
1766                 if (gamemode == GAME_ROGUE)
1767                 {
1768                         val = PRVM_EDICTFIELDVALUE(host_client->edict, prog->fieldoffsets.ammo_plasma);
1769                         if (val)
1770                         {
1771                                 val->_float = v;
1772                                 if (host_client->edict->fields.server->weapon > IT_LIGHTNING)
1773                                         host_client->edict->fields.server->ammo_cells = v;
1774                         }
1775                 }
1776                 break;
1777         }
1778 }
1779
1780 prvm_edict_t    *FindViewthing (void)
1781 {
1782         int             i;
1783         prvm_edict_t    *e;
1784
1785         for (i=0 ; i<prog->num_edicts ; i++)
1786         {
1787                 e = PRVM_EDICT_NUM(i);
1788                 if (!strcmp (PRVM_GetString(e->fields.server->classname), "viewthing"))
1789                         return e;
1790         }
1791         Con_Print("No viewthing on map\n");
1792         return NULL;
1793 }
1794
1795 /*
1796 ==================
1797 Host_Viewmodel_f
1798 ==================
1799 */
1800 void Host_Viewmodel_f (void)
1801 {
1802         prvm_edict_t    *e;
1803         model_t *m;
1804
1805         if (!sv.active)
1806                 return;
1807
1808         SV_VM_Begin();
1809         e = FindViewthing ();
1810         SV_VM_End();
1811         if (!e)
1812                 return;
1813
1814         m = Mod_ForName (Cmd_Argv(1), false, true, false);
1815         if (!m || !m->loaded || !m->Draw)
1816         {
1817                 Con_Printf("viewmodel: can't load %s\n", Cmd_Argv(1));
1818                 return;
1819         }
1820
1821         e->fields.server->frame = 0;
1822         cl.model_precache[(int)e->fields.server->modelindex] = m;
1823 }
1824
1825 /*
1826 ==================
1827 Host_Viewframe_f
1828 ==================
1829 */
1830 void Host_Viewframe_f (void)
1831 {
1832         prvm_edict_t    *e;
1833         int             f;
1834         model_t *m;
1835
1836         if (!sv.active)
1837                 return;
1838
1839         SV_VM_Begin();
1840         e = FindViewthing ();
1841         SV_VM_End();
1842         if (!e)
1843                 return;
1844         m = cl.model_precache[(int)e->fields.server->modelindex];
1845
1846         f = atoi(Cmd_Argv(1));
1847         if (f >= m->numframes)
1848                 f = m->numframes-1;
1849
1850         e->fields.server->frame = f;
1851 }
1852
1853
1854 void PrintFrameName (model_t *m, int frame)
1855 {
1856         if (m->animscenes)
1857                 Con_Printf("frame %i: %s\n", frame, m->animscenes[frame].name);
1858         else
1859                 Con_Printf("frame %i\n", frame);
1860 }
1861
1862 /*
1863 ==================
1864 Host_Viewnext_f
1865 ==================
1866 */
1867 void Host_Viewnext_f (void)
1868 {
1869         prvm_edict_t    *e;
1870         model_t *m;
1871
1872         if (!sv.active)
1873                 return;
1874
1875         SV_VM_Begin();
1876         e = FindViewthing ();
1877         SV_VM_End();
1878         if (!e)
1879                 return;
1880         m = cl.model_precache[(int)e->fields.server->modelindex];
1881
1882         e->fields.server->frame = e->fields.server->frame + 1;
1883         if (e->fields.server->frame >= m->numframes)
1884                 e->fields.server->frame = m->numframes - 1;
1885
1886         PrintFrameName (m, (int)e->fields.server->frame);
1887 }
1888
1889 /*
1890 ==================
1891 Host_Viewprev_f
1892 ==================
1893 */
1894 void Host_Viewprev_f (void)
1895 {
1896         prvm_edict_t    *e;
1897         model_t *m;
1898
1899         if (!sv.active)
1900                 return;
1901
1902         SV_VM_Begin();
1903         e = FindViewthing ();
1904         SV_VM_End();
1905         if (!e)
1906                 return;
1907
1908         m = cl.model_precache[(int)e->fields.server->modelindex];
1909
1910         e->fields.server->frame = e->fields.server->frame - 1;
1911         if (e->fields.server->frame < 0)
1912                 e->fields.server->frame = 0;
1913
1914         PrintFrameName (m, (int)e->fields.server->frame);
1915 }
1916
1917 /*
1918 ===============================================================================
1919
1920 DEMO LOOP CONTROL
1921
1922 ===============================================================================
1923 */
1924
1925
1926 /*
1927 ==================
1928 Host_Startdemos_f
1929 ==================
1930 */
1931 void Host_Startdemos_f (void)
1932 {
1933         int             i, c;
1934
1935         if (cls.state == ca_dedicated || COM_CheckParm("-listen") || COM_CheckParm("-benchmark") || COM_CheckParm("-demo") || COM_CheckParm("-capturedemo"))
1936                 return;
1937
1938         c = Cmd_Argc() - 1;
1939         if (c > MAX_DEMOS)
1940         {
1941                 Con_Printf("Max %i demos in demoloop\n", MAX_DEMOS);
1942                 c = MAX_DEMOS;
1943         }
1944         Con_Printf("%i demo(s) in loop\n", c);
1945
1946         for (i=1 ; i<c+1 ; i++)
1947                 strlcpy (cls.demos[i-1], Cmd_Argv(i), sizeof (cls.demos[i-1]));
1948
1949         // LordHavoc: clear the remaining slots
1950         for (;i <= MAX_DEMOS;i++)
1951                 cls.demos[i-1][0] = 0;
1952
1953         if (!sv.active && cls.demonum != -1 && !cls.demoplayback)
1954         {
1955                 cls.demonum = 0;
1956                 CL_NextDemo ();
1957         }
1958         else
1959                 cls.demonum = -1;
1960 }
1961
1962
1963 /*
1964 ==================
1965 Host_Demos_f
1966
1967 Return to looping demos
1968 ==================
1969 */
1970 void Host_Demos_f (void)
1971 {
1972         if (cls.state == ca_dedicated)
1973                 return;
1974         if (cls.demonum == -1)
1975                 cls.demonum = 1;
1976         CL_Disconnect_f ();
1977         CL_NextDemo ();
1978 }
1979
1980 /*
1981 ==================
1982 Host_Stopdemo_f
1983
1984 Return to looping demos
1985 ==================
1986 */
1987 void Host_Stopdemo_f (void)
1988 {
1989         if (!cls.demoplayback)
1990                 return;
1991         CL_Disconnect ();
1992         Host_ShutdownServer ();
1993 }
1994
1995 void Host_SendCvar_f (void)
1996 {
1997         int             i;
1998         cvar_t  *c;
1999         const char *cvarname;
2000         client_t *old;
2001
2002         if(Cmd_Argc() != 2)
2003                 return;
2004         cvarname = Cmd_Argv(1);
2005         if (cls.state == ca_connected)
2006         {
2007                 c = Cvar_FindVar(cvarname);
2008                 // LordHavoc: if there is no such cvar or if it is private, send a
2009                 // reply indicating that it has no value
2010                 if(!c || (c->flags & CVAR_PRIVATE))
2011                         Cmd_ForwardStringToServer(va("sentcvar %s", cvarname));
2012                 else
2013                         Cmd_ForwardStringToServer(va("sentcvar %s \"%s\"", c->name, c->string));
2014                 return;
2015         }
2016         if(!sv.active)// || !prog->funcoffsets.SV_ParseClientCommand)
2017                 return;
2018
2019         old = host_client;
2020         if (cls.state != ca_dedicated)
2021                 i = 1;
2022         else
2023                 i = 0;
2024         for(;i<svs.maxclients;i++)
2025                 if(svs.clients[i].active && svs.clients[i].netconnection)
2026                 {
2027                         host_client = &svs.clients[i];
2028                         Host_ClientCommands(va("sendcvar %s\n", cvarname));
2029                 }
2030         host_client = old;
2031 }
2032
2033 static void MaxPlayers_f(void)
2034 {
2035         int n;
2036
2037         if (Cmd_Argc() != 2)
2038         {
2039                 Con_Printf("\"maxplayers\" is \"%u\"\n", svs.maxclients);
2040                 return;
2041         }
2042
2043         if (sv.active)
2044         {
2045                 Con_Print("maxplayers can not be changed while a server is running.\n");
2046                 return;
2047         }
2048
2049         n = atoi(Cmd_Argv(1));
2050         n = bound(1, n, MAX_SCOREBOARD);
2051         Con_Printf("\"maxplayers\" set to \"%u\"\n", n);
2052
2053         if (svs.clients)
2054                 Mem_Free(svs.clients);
2055         svs.maxclients = n;
2056         svs.clients = (client_t *)Mem_Alloc(sv_mempool, sizeof(client_t) * svs.maxclients);
2057         if (n == 1)
2058                 Cvar_Set ("deathmatch", "0");
2059         else
2060                 Cvar_Set ("deathmatch", "1");
2061 }
2062
2063 //=============================================================================
2064
2065 // QuakeWorld commands
2066
2067 /*
2068 =====================
2069 Host_Rcon_f
2070
2071   Send the rest of the command line over as
2072   an unconnected command.
2073 =====================
2074 */
2075 void Host_Rcon_f (void) // credit: taken from QuakeWorld
2076 {
2077         int i;
2078         lhnetaddress_t to;
2079         lhnetsocket_t *mysocket;
2080
2081         if (!rcon_password.string || !rcon_password.string[0])
2082         {
2083                 Con_Printf ("You must set rcon_password before issuing an rcon command.\n");
2084                 return;
2085         }
2086
2087         for (i = 0;rcon_password.string[i];i++)
2088         {
2089                 if (rcon_password.string[i] <= ' ')
2090                 {
2091                         Con_Printf("rcon_password is not allowed to have any whitespace.\n");
2092                         return;
2093                 }
2094         }
2095
2096         if (cls.netcon)
2097                 to = cls.netcon->peeraddress;
2098         else
2099         {
2100                 if (!rcon_address.string[0])
2101                 {
2102                         Con_Printf ("You must either be connected, or set the rcon_address cvar to issue rcon commands\n");
2103                         return;
2104                 }
2105                 LHNETADDRESS_FromString(&to, rcon_address.string, sv_netport.integer);
2106         }
2107         mysocket = NetConn_ChooseClientSocketForAddress(&to);
2108         if (mysocket)
2109         {
2110                 // simply put together the rcon packet and send it
2111                 NetConn_WriteString(mysocket, va("\377\377\377\377rcon %s %s", rcon_password.string, Cmd_Args()), &to);
2112         }
2113 }
2114
2115 /*
2116 ====================
2117 Host_User_f
2118
2119 user <name or userid>
2120
2121 Dump userdata / masterdata for a user
2122 ====================
2123 */
2124 void Host_User_f (void) // credit: taken from QuakeWorld
2125 {
2126         int             uid;
2127         int             i;
2128
2129         if (Cmd_Argc() != 2)
2130         {
2131                 Con_Printf ("Usage: user <username / userid>\n");
2132                 return;
2133         }
2134
2135         uid = atoi(Cmd_Argv(1));
2136
2137         for (i = 0;i < cl.maxclients;i++)
2138         {
2139                 if (!cl.scores[i].name[0])
2140                         continue;
2141                 if (cl.scores[i].qw_userid == uid || !strcasecmp(cl.scores[i].name, Cmd_Argv(1)))
2142                 {
2143                         InfoString_Print(cl.scores[i].qw_userinfo);
2144                         return;
2145                 }
2146         }
2147         Con_Printf ("User not in server.\n");
2148 }
2149
2150 /*
2151 ====================
2152 Host_Users_f
2153
2154 Dump userids for all current players
2155 ====================
2156 */
2157 void Host_Users_f (void) // credit: taken from QuakeWorld
2158 {
2159         int             i;
2160         int             c;
2161
2162         c = 0;
2163         Con_Printf ("userid frags name\n");
2164         Con_Printf ("------ ----- ----\n");
2165         for (i = 0;i < cl.maxclients;i++)
2166         {
2167                 if (cl.scores[i].name[0])
2168                 {
2169                         Con_Printf ("%6i %4i %s\n", cl.scores[i].qw_userid, cl.scores[i].frags, cl.scores[i].name);
2170                         c++;
2171                 }
2172         }
2173
2174         Con_Printf ("%i total users\n", c);
2175 }
2176
2177 /*
2178 ==================
2179 Host_FullServerinfo_f
2180
2181 Sent by server when serverinfo changes
2182 ==================
2183 */
2184 // TODO: shouldn't this be a cvar instead?
2185 void Host_FullServerinfo_f (void) // credit: taken from QuakeWorld
2186 {
2187         char temp[512];
2188         if (Cmd_Argc() != 2)
2189         {
2190                 Con_Printf ("usage: fullserverinfo <complete info string>\n");
2191                 return;
2192         }
2193
2194         strlcpy (cl.qw_serverinfo, Cmd_Argv(1), sizeof(cl.qw_serverinfo));
2195         InfoString_GetValue(cl.qw_serverinfo, "teamplay", temp, sizeof(temp));
2196         cl.qw_teamplay = atoi(temp);
2197 }
2198
2199 /*
2200 ==================
2201 Host_FullInfo_f
2202
2203 Allow clients to change userinfo
2204 ==================
2205 Casey was here :)
2206 */
2207 void Host_FullInfo_f (void) // credit: taken from QuakeWorld
2208 {
2209         char key[512];
2210         char value[512];
2211         char *o;
2212         const char *s;
2213
2214         if (Cmd_Argc() != 2)
2215         {
2216                 Con_Printf ("fullinfo <complete info string>\n");
2217                 return;
2218         }
2219
2220         s = Cmd_Argv(1);
2221         if (*s == '\\')
2222                 s++;
2223         while (*s)
2224         {
2225                 o = key;
2226                 while (*s && *s != '\\')
2227                         *o++ = *s++;
2228                 *o = 0;
2229
2230                 if (!*s)
2231                 {
2232                         Con_Printf ("MISSING VALUE\n");
2233                         return;
2234                 }
2235
2236                 o = value;
2237                 s++;
2238                 while (*s && *s != '\\')
2239                         *o++ = *s++;
2240                 *o = 0;
2241
2242                 if (*s)
2243                         s++;
2244
2245                 CL_SetInfo(key, value, false, false, false, false);
2246         }
2247 }
2248
2249 /*
2250 ==================
2251 CL_SetInfo_f
2252
2253 Allow clients to change userinfo
2254 ==================
2255 */
2256 void Host_SetInfo_f (void) // credit: taken from QuakeWorld
2257 {
2258         if (Cmd_Argc() == 1)
2259         {
2260                 InfoString_Print(cls.userinfo);
2261                 return;
2262         }
2263         if (Cmd_Argc() != 3)
2264         {
2265                 Con_Printf ("usage: setinfo [ <key> <value> ]\n");
2266                 return;
2267         }
2268         CL_SetInfo(Cmd_Argv(1), Cmd_Argv(2), true, false, false, false);
2269 }
2270
2271 /*
2272 ====================
2273 Host_Packet_f
2274
2275 packet <destination> <contents>
2276
2277 Contents allows \n escape character
2278 ====================
2279 */
2280 void Host_Packet_f (void) // credit: taken from QuakeWorld
2281 {
2282         char send[2048];
2283         int i, l;
2284         const char *in;
2285         char *out;
2286         lhnetaddress_t address;
2287         lhnetsocket_t *mysocket;
2288
2289         if (Cmd_Argc() != 3)
2290         {
2291                 Con_Printf ("packet <destination> <contents>\n");
2292                 return;
2293         }
2294
2295         if (!LHNETADDRESS_FromString (&address, Cmd_Argv(1), sv_netport.integer))
2296         {
2297                 Con_Printf ("Bad address\n");
2298                 return;
2299         }
2300
2301         in = Cmd_Argv(2);
2302         out = send+4;
2303         send[0] = send[1] = send[2] = send[3] = 0xff;
2304
2305         l = (int)strlen (in);
2306         for (i=0 ; i<l ; i++)
2307         {
2308                 if (out >= send + sizeof(send) - 1)
2309                         break;
2310                 if (in[i] == '\\' && in[i+1] == 'n')
2311                 {
2312                         *out++ = '\n';
2313                         i++;
2314                 }
2315                 else if (in[i] == '\\' && in[i+1] == '0')
2316                 {
2317                         *out++ = '\0';
2318                         i++;
2319                 }
2320                 else if (in[i] == '\\' && in[i+1] == 't')
2321                 {
2322                         *out++ = '\t';
2323                         i++;
2324                 }
2325                 else if (in[i] == '\\' && in[i+1] == 'r')
2326                 {
2327                         *out++ = '\r';
2328                         i++;
2329                 }
2330                 else if (in[i] == '\\' && in[i+1] == '"')
2331                 {
2332                         *out++ = '\"';
2333                         i++;
2334                 }
2335                 else
2336                         *out++ = in[i];
2337         }
2338
2339         mysocket = NetConn_ChooseClientSocketForAddress(&address);
2340         if (mysocket)
2341                 NetConn_Write(mysocket, send, out - send, &address);
2342 }
2343
2344 /*
2345 ====================
2346 Host_Pings_f
2347
2348 Send back ping and packet loss update for all current players to this player
2349 ====================
2350 */
2351 void Host_Pings_f (void)
2352 {
2353         int             i, j, ping, packetloss;
2354         char temp[128];
2355
2356         if (!host_client->netconnection)
2357                 return;
2358
2359         if (sv.protocol != PROTOCOL_QUAKEWORLD)
2360         {
2361                 MSG_WriteByte(&host_client->netconnection->message, svc_stufftext);
2362                 MSG_WriteUnterminatedString(&host_client->netconnection->message, "pingplreport");
2363         }
2364         for (i = 0;i < svs.maxclients;i++)
2365         {
2366                 packetloss = 0;
2367                 if (svs.clients[i].netconnection)
2368                         for (j = 0;j < NETGRAPH_PACKETS;j++)
2369                                 if (svs.clients[i].netconnection->incoming_unreliablesize[j] == NETGRAPH_LOSTPACKET)
2370                                         packetloss++;
2371                 packetloss = packetloss * 100 / NETGRAPH_PACKETS;
2372                 ping = (int)floor(svs.clients[i].ping*1000+0.5);
2373                 ping = bound(0, ping, 9999);
2374                 if (sv.protocol == PROTOCOL_QUAKEWORLD)
2375                 {
2376                         // send qw_svc_updateping and qw_svc_updatepl messages
2377                         MSG_WriteByte(&host_client->netconnection->message, qw_svc_updateping);
2378                         MSG_WriteShort(&host_client->netconnection->message, ping);
2379                         MSG_WriteByte(&host_client->netconnection->message, qw_svc_updatepl);
2380                         MSG_WriteByte(&host_client->netconnection->message, packetloss);
2381                 }
2382                 else
2383                 {
2384                         // write the string into the packet as multiple unterminated strings to avoid needing a local buffer
2385                         dpsnprintf(temp, sizeof(temp), " %d %d", ping, packetloss);
2386                         MSG_WriteUnterminatedString(&host_client->netconnection->message, temp);
2387                 }
2388         }
2389         if (sv.protocol != PROTOCOL_QUAKEWORLD)
2390                 MSG_WriteString(&host_client->netconnection->message, "\n");
2391 }
2392
2393 void Host_PingPLReport_f(void)
2394 {
2395         int i;
2396         int l = Cmd_Argc();
2397         if (l > cl.maxclients)
2398                 l = cl.maxclients;
2399         for (i = 0;i < l;i++)
2400         {
2401                 cl.scores[i].qw_ping = atoi(Cmd_Argv(1+i*2));
2402                 cl.scores[i].qw_packetloss = atoi(Cmd_Argv(1+i*2+1));
2403         }
2404 }
2405
2406 //=============================================================================
2407
2408 /*
2409 ==================
2410 Host_InitCommands
2411 ==================
2412 */
2413 void Host_InitCommands (void)
2414 {
2415         dpsnprintf(cls.userinfo, sizeof(cls.userinfo), "\\name\\player\\team\\none\\topcolor\\0\\bottomcolor\\0\\rate\\10000\\msg\\1\\noaim\\1\\*ver\\dp");
2416
2417         Cmd_AddCommand_WithClientCommand ("status", Host_Status_f, Host_Status_f, "print server status information");
2418         Cmd_AddCommand ("quit", Host_Quit_f, "quit the game");
2419         if (gamemode == GAME_NEHAHRA)
2420         {
2421                 Cmd_AddCommand_WithClientCommand ("max", NULL, Host_God_f, "god mode (invulnerability)");
2422                 Cmd_AddCommand_WithClientCommand ("monster", NULL, Host_Notarget_f, "notarget mode (monsters do not see you)");
2423                 Cmd_AddCommand_WithClientCommand ("scrag", NULL, Host_Fly_f, "fly mode (flight)");
2424                 Cmd_AddCommand_WithClientCommand ("wraith", NULL, Host_Noclip_f, "noclip mode (flight without collisions, move through walls)");
2425                 Cmd_AddCommand_WithClientCommand ("gimme", NULL, Host_Give_f, "alter inventory");
2426         }
2427         else
2428         {
2429                 Cmd_AddCommand_WithClientCommand ("god", NULL, Host_God_f, "god mode (invulnerability)");
2430                 Cmd_AddCommand_WithClientCommand ("notarget", NULL, Host_Notarget_f, "notarget mode (monsters do not see you)");
2431                 Cmd_AddCommand_WithClientCommand ("fly", NULL, Host_Fly_f, "fly mode (flight)");
2432                 Cmd_AddCommand_WithClientCommand ("noclip", NULL, Host_Noclip_f, "noclip mode (flight without collisions, move through walls)");
2433                 Cmd_AddCommand_WithClientCommand ("give", NULL, Host_Give_f, "alter inventory");
2434         }
2435         Cmd_AddCommand ("map", Host_Map_f, "kick everyone off the server and start a new level");
2436         Cmd_AddCommand ("restart", Host_Restart_f, "restart current level");
2437         Cmd_AddCommand ("changelevel", Host_Changelevel_f, "change to another level, bringing along all connected clients");
2438         Cmd_AddCommand ("connect", Host_Connect_f, "connect to a server by IP address or hostname");
2439         Cmd_AddCommand ("reconnect", Host_Reconnect_f, "reconnect to the last server you were on, or resets a quakeworld connection (do not use if currently playing on a netquake server)");
2440         Cmd_AddCommand ("version", Host_Version_f, "print engine version");
2441         Cmd_AddCommand_WithClientCommand ("say", Host_Say_f, Host_Say_f, "send a chat message to everyone on the server");
2442         Cmd_AddCommand_WithClientCommand ("say_team", Host_Say_Team_f, Host_Say_Team_f, "send a chat message to your team on the server");
2443         Cmd_AddCommand_WithClientCommand ("tell", Host_Tell_f, Host_Tell_f, "send a chat message to only one person on the server");
2444         Cmd_AddCommand_WithClientCommand ("kill", NULL, Host_Kill_f, "die instantly");
2445         Cmd_AddCommand_WithClientCommand ("pause", NULL, Host_Pause_f, "pause the game (if the server allows pausing)");
2446         Cmd_AddCommand ("kick", Host_Kick_f, "kick a player off the server by number or name");
2447         Cmd_AddCommand_WithClientCommand ("ping", Host_Ping_f, Host_Ping_f, "print ping times of all players on the server");
2448         Cmd_AddCommand ("load", Host_Loadgame_f, "load a saved game file");
2449         Cmd_AddCommand ("save", Host_Savegame_f, "save the game to a file");
2450
2451         Cmd_AddCommand ("startdemos", Host_Startdemos_f, "start playing back the selected demos sequentially (used at end of startup script)");
2452         Cmd_AddCommand ("demos", Host_Demos_f, "restart looping demos defined by the last startdemos command");
2453         Cmd_AddCommand ("stopdemo", Host_Stopdemo_f, "stop playing or recording demo (like stop command) and return to looping demos");
2454
2455         Cmd_AddCommand ("viewmodel", Host_Viewmodel_f, "change model of viewthing entity in current level");
2456         Cmd_AddCommand ("viewframe", Host_Viewframe_f, "change animation frame of viewthing entity in current level");
2457         Cmd_AddCommand ("viewnext", Host_Viewnext_f, "change to next animation frame of viewthing entity in current level");
2458         Cmd_AddCommand ("viewprev", Host_Viewprev_f, "change to previous animation frame of viewthing entity in current level");
2459
2460         Cvar_RegisterVariable (&cl_name);
2461         Cmd_AddCommand_WithClientCommand ("name", Host_Name_f, Host_Name_f, "change your player name");
2462         Cvar_RegisterVariable (&cl_color);
2463         Cmd_AddCommand_WithClientCommand ("color", Host_Color_f, Host_Color_f, "change your player shirt and pants colors");
2464         Cvar_RegisterVariable (&cl_rate);
2465         Cmd_AddCommand_WithClientCommand ("rate", Host_Rate_f, Host_Rate_f, "change your network connection speed");
2466         if (gamemode == GAME_NEHAHRA)
2467         {
2468                 Cvar_RegisterVariable (&cl_pmodel);
2469                 Cmd_AddCommand_WithClientCommand ("pmodel", Host_PModel_f, Host_PModel_f, "change your player model choice (Nehahra specific)");
2470         }
2471
2472         // BLACK: This isnt game specific anymore (it was GAME_NEXUIZ at first)
2473         Cvar_RegisterVariable (&cl_playermodel);
2474         Cmd_AddCommand_WithClientCommand ("playermodel", Host_Playermodel_f, Host_Playermodel_f, "change your player model");
2475         Cvar_RegisterVariable (&cl_playerskin);
2476         Cmd_AddCommand_WithClientCommand ("playerskin", Host_Playerskin_f, Host_Playerskin_f, "change your player skin number");
2477
2478         Cmd_AddCommand_WithClientCommand ("prespawn", NULL, Host_PreSpawn_f, "signon 1 (client acknowledges that server information has been received)");
2479         Cmd_AddCommand_WithClientCommand ("spawn", NULL, Host_Spawn_f, "signon 2 (client has sent player information, and is asking server to send scoreboard rankings)");
2480         Cmd_AddCommand_WithClientCommand ("begin", NULL, Host_Begin_f, "signon 3 (client asks server to start sending entities, and will go to signon 4 (playing) when the first entity update is received)");
2481         Cmd_AddCommand ("maxplayers", MaxPlayers_f, "sets limit on how many players (or bots) may be connected to the server at once");
2482
2483         Cmd_AddCommand ("sendcvar", Host_SendCvar_f, "sends the value of a cvar to the server as a sentcvar command, for use by QuakeC");
2484
2485         Cvar_RegisterVariable (&rcon_password);
2486         Cvar_RegisterVariable (&rcon_address);
2487         Cmd_AddCommand ("rcon", Host_Rcon_f, "sends a command to the server console (if your rcon_password matches the server's rcon_password), or to the address specified by rcon_address when not connected (again rcon_password must match the server's)");
2488         Cmd_AddCommand ("user", Host_User_f, "prints additional information about a player number or name on the scoreboard");
2489         Cmd_AddCommand ("users", Host_Users_f, "prints additional information about all players on the scoreboard");
2490         Cmd_AddCommand ("fullserverinfo", Host_FullServerinfo_f, "internal use only, sent by server to client to update client's local copy of serverinfo string");
2491         Cmd_AddCommand ("fullinfo", Host_FullInfo_f, "allows client to modify their userinfo");
2492         Cmd_AddCommand ("setinfo", Host_SetInfo_f, "modifies your userinfo");
2493         Cmd_AddCommand ("packet", Host_Packet_f, "send a packet to the specified address:port containing a text string");
2494         Cmd_AddCommand ("topcolor", Host_TopColor_f, "QW command to set top color without changing bottom color");
2495         Cmd_AddCommand ("bottomcolor", Host_BottomColor_f, "QW command to set bottom color without changing top color");
2496
2497         Cmd_AddCommand_WithClientCommand ("pings", NULL, Host_Pings_f, "command sent by clients to request updated ping and packetloss of players on scoreboard (originally from QW, but also used on NQ servers)");
2498         Cmd_AddCommand ("pingplreport", Host_PingPLReport_f, "command sent by server containing client ping and packet loss values for scoreboard, triggered by pings command from client (not used by QW servers)");
2499
2500         Cmd_AddCommand ("fixtrans", Image_FixTransparentPixels_f, "change alpha-zero pixels in an image file to sensible values, and write out a new TGA (warning: SLOW)");
2501
2502         Cvar_RegisterVariable (&team);
2503         Cvar_RegisterVariable (&skin);
2504         Cvar_RegisterVariable (&noaim);
2505
2506         Cvar_RegisterVariable(&sv_cheats);
2507         Cvar_RegisterVariable(&sv_adminnick);
2508 }
2509