2c5d185fd52a36350072d2289b91895e820de5fd
[xonotic/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
23 int current_skill;
24 cvar_t sv_cheats = {0, "sv_cheats", "0", "enables cheat commands in any game, and cheat impulses in dpmod"};
25 cvar_t rcon_password = {0, "rcon_password", "", "password to authenticate rcon commands"};
26 cvar_t rcon_address = {0, "rcon_address", "", "server address to send rcon commands to (when not connected to a server)"};
27 qboolean allowcheats = false;
28
29 /*
30 ==================
31 Host_Quit_f
32 ==================
33 */
34
35 void Host_Quit_f (void)
36 {
37         Sys_Quit ();
38 }
39
40
41 /*
42 ==================
43 Host_Status_f
44 ==================
45 */
46 void Host_Status_f (void)
47 {
48         client_t *client;
49         int seconds, minutes, hours = 0, j, players;
50         void (*print) (const char *fmt, ...);
51
52         if (cmd_source == src_command)
53         {
54                 if (!sv.active)
55                 {
56                         Cmd_ForwardToServer ();
57                         return;
58                 }
59                 print = Con_Printf;
60         }
61         else
62                 print = SV_ClientPrintf;
63
64         for (players = 0, j = 0;j < svs.maxclients;j++)
65                 if (svs.clients[j].active)
66                         players++;
67         print ("host:     %s\n", Cvar_VariableString ("hostname"));
68         print ("version:  %s build %s\n", gamename, buildstring);
69         print ("protocol: %i (%s)\n", Protocol_NumberForEnum(sv.protocol), Protocol_NameForEnum(sv.protocol));
70         print ("map:      %s\n", sv.name);
71         print ("players:  %i active (%i max)\n\n", players, svs.maxclients);
72         for (j = 0, client = svs.clients;j < svs.maxclients;j++, client++)
73         {
74                 if (!client->active)
75                         continue;
76                 seconds = (int)(realtime - client->connecttime);
77                 minutes = seconds / 60;
78                 if (minutes)
79                 {
80                         seconds -= (minutes * 60);
81                         hours = minutes / 60;
82                         if (hours)
83                                 minutes -= (hours * 60);
84                 }
85                 else
86                         hours = 0;
87                 print ("#%-2u %-16.16s  %3i  %2i:%02i:%02i\n", j+1, client->name, (int)client->edict->fields.server->frags, hours, minutes, seconds);
88                 print ("   %s\n", client->netconnection ? client->netconnection->address : "botclient");
89         }
90 }
91
92
93 /*
94 ==================
95 Host_God_f
96
97 Sets client to godmode
98 ==================
99 */
100 void Host_God_f (void)
101 {
102         if (cmd_source == src_command)
103         {
104                 Cmd_ForwardToServer ();
105                 return;
106         }
107
108         if (!allowcheats)
109         {
110                 SV_ClientPrint("No cheats allowed, use sv_cheats 1 and restart level to enable.\n");
111                 return;
112         }
113
114         host_client->edict->fields.server->flags = (int)host_client->edict->fields.server->flags ^ FL_GODMODE;
115         if (!((int)host_client->edict->fields.server->flags & FL_GODMODE) )
116                 SV_ClientPrint("godmode OFF\n");
117         else
118                 SV_ClientPrint("godmode ON\n");
119 }
120
121 void Host_Notarget_f (void)
122 {
123         if (cmd_source == src_command)
124         {
125                 Cmd_ForwardToServer ();
126                 return;
127         }
128
129         if (!allowcheats)
130         {
131                 SV_ClientPrint("No cheats allowed, use sv_cheats 1 and restart level to enable.\n");
132                 return;
133         }
134
135         host_client->edict->fields.server->flags = (int)host_client->edict->fields.server->flags ^ FL_NOTARGET;
136         if (!((int)host_client->edict->fields.server->flags & FL_NOTARGET) )
137                 SV_ClientPrint("notarget OFF\n");
138         else
139                 SV_ClientPrint("notarget ON\n");
140 }
141
142 qboolean noclip_anglehack;
143
144 void Host_Noclip_f (void)
145 {
146         if (cmd_source == src_command)
147         {
148                 Cmd_ForwardToServer ();
149                 return;
150         }
151
152         if (!allowcheats)
153         {
154                 SV_ClientPrint("No cheats allowed, use sv_cheats 1 and restart level to enable.\n");
155                 return;
156         }
157
158         if (host_client->edict->fields.server->movetype != MOVETYPE_NOCLIP)
159         {
160                 noclip_anglehack = true;
161                 host_client->edict->fields.server->movetype = MOVETYPE_NOCLIP;
162                 SV_ClientPrint("noclip ON\n");
163         }
164         else
165         {
166                 noclip_anglehack = false;
167                 host_client->edict->fields.server->movetype = MOVETYPE_WALK;
168                 SV_ClientPrint("noclip OFF\n");
169         }
170 }
171
172 /*
173 ==================
174 Host_Fly_f
175
176 Sets client to flymode
177 ==================
178 */
179 void Host_Fly_f (void)
180 {
181         if (cmd_source == src_command)
182         {
183                 Cmd_ForwardToServer ();
184                 return;
185         }
186
187         if (!allowcheats)
188         {
189                 SV_ClientPrint("No cheats allowed, use sv_cheats 1 and restart level to enable.\n");
190                 return;
191         }
192
193         if (host_client->edict->fields.server->movetype != MOVETYPE_FLY)
194         {
195                 host_client->edict->fields.server->movetype = MOVETYPE_FLY;
196                 SV_ClientPrint("flymode ON\n");
197         }
198         else
199         {
200                 host_client->edict->fields.server->movetype = MOVETYPE_WALK;
201                 SV_ClientPrint("flymode OFF\n");
202         }
203 }
204
205
206 /*
207 ==================
208 Host_Ping_f
209
210 ==================
211 */
212 void Host_Ping_f (void)
213 {
214         int i;
215         client_t *client;
216         void (*print) (const char *fmt, ...);
217
218         if (cmd_source == src_command)
219         {
220                 if (!sv.active)
221                 {
222                         Cmd_ForwardToServer ();
223                         return;
224                 }
225                 print = Con_Printf;
226         }
227         else
228                 print = SV_ClientPrintf;
229
230         print("Client ping times:\n");
231         for (i = 0, client = svs.clients;i < svs.maxclients;i++, client++)
232         {
233                 if (!client->active)
234                         continue;
235                 print("%4i %s\n", (int)floor(client->ping*1000+0.5), client->name);
236         }
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         if (cmd_source != src_command)
267                 return;
268
269         cls.demonum = -1;               // stop demo loop in case this fails
270
271         CL_Disconnect ();
272         Host_ShutdownServer();
273
274         // remove console or menu
275         key_dest = key_game;
276         key_consoleactive = 0;
277
278         svs.serverflags = 0;                    // haven't completed an episode yet
279         allowcheats = sv_cheats.integer != 0;
280         strcpy(level, Cmd_Argv(1));
281         SV_SpawnServer(level);
282         if (sv.active && cls.state == ca_disconnected)
283                 CL_EstablishConnection("local:1");
284 }
285
286 /*
287 ==================
288 Host_Changelevel_f
289
290 Goes to a new map, taking all clients along
291 ==================
292 */
293 void Host_Changelevel_f (void)
294 {
295         char level[MAX_QPATH];
296
297         if (Cmd_Argc() != 2)
298         {
299                 Con_Print("changelevel <levelname> : continue game on a new level\n");
300                 return;
301         }
302         if (cls.demoplayback)
303         {
304                 Con_Print("Only the server may changelevel\n");
305                 return;
306         }
307         // HACKHACKHACK
308         if (!sv.active) {
309                 Host_Map_f();
310                 return;
311         }
312         if (cmd_source != src_command)
313                 return;
314
315         // remove console or menu
316         key_dest = key_game;
317         key_consoleactive = 0;
318
319         SV_VM_Begin();
320         SV_SaveSpawnparms ();
321         SV_VM_End();
322         allowcheats = sv_cheats.integer != 0;
323         strcpy(level, Cmd_Argv(1));
324         SV_SpawnServer(level);
325         if (sv.active && cls.state == ca_disconnected)
326                 CL_EstablishConnection("local:1");
327 }
328
329 /*
330 ==================
331 Host_Restart_f
332
333 Restarts the current server for a dead player
334 ==================
335 */
336 void Host_Restart_f (void)
337 {
338         char mapname[MAX_QPATH];
339
340         if (Cmd_Argc() != 1)
341         {
342                 Con_Print("restart : restart current level\n");
343                 return;
344         }
345         if (!sv.active)
346         {
347                 Con_Print("Only the server may restart\n");
348                 return;
349         }
350         if (cmd_source != src_command)
351                 return;
352
353         // remove console or menu
354         key_dest = key_game;
355         key_consoleactive = 0;
356
357         allowcheats = sv_cheats.integer != 0;
358         strcpy(mapname, sv.name);
359         SV_SpawnServer(mapname);
360         if (sv.active && cls.state == ca_disconnected)
361                 CL_EstablishConnection("local:1");
362 }
363
364 /*
365 ==================
366 Host_Reconnect_f
367
368 This command causes the client to wait for the signon messages again.
369 This is sent just before a server changes levels
370 ==================
371 */
372 void Host_Reconnect_f (void)
373 {
374         if (cmd_source == src_command)
375         {
376                 Con_Print("reconnect is not valid from the console\n");
377                 return;
378         }
379         if (Cmd_Argc() != 1)
380         {
381                 Con_Print("reconnect : wait for signon messages again\n");
382                 return;
383         }
384         if (!cls.signon)
385         {
386                 //Con_Print("reconnect: no signon, ignoring reconnect\n");
387                 return;
388         }
389         cls.signon = 0;         // need new connection messages
390 }
391
392 /*
393 =====================
394 Host_Connect_f
395
396 User command to connect to server
397 =====================
398 */
399 void Host_Connect_f (void)
400 {
401         if (Cmd_Argc() != 2)
402         {
403                 Con_Print("connect <serveraddress> : connect to a multiplayer game\n");
404                 return;
405         }
406         CL_EstablishConnection(Cmd_Argv(1));
407 }
408
409
410 /*
411 ===============================================================================
412
413 LOAD / SAVE GAME
414
415 ===============================================================================
416 */
417
418 #define SAVEGAME_VERSION        5
419
420 /*
421 ===============
422 Host_SavegameComment
423
424 Writes a SAVEGAME_COMMENT_LENGTH character comment describing the current
425 ===============
426 */
427 void Host_SavegameComment (char *text)
428 {
429         int             i;
430         char    kills[20];
431
432         for (i=0 ; i<SAVEGAME_COMMENT_LENGTH ; i++)
433                 text[i] = ' ';
434         // LordHavoc: added min() to prevent overflow
435         memcpy (text, cl.levelname, min(strlen(cl.levelname), SAVEGAME_COMMENT_LENGTH));
436         sprintf (kills,"kills:%3i/%3i", cl.stats[STAT_MONSTERS], cl.stats[STAT_TOTALMONSTERS]);
437         memcpy (text+22, kills, strlen(kills));
438         // convert space to _ to make stdio happy
439         // LordHavoc: convert control characters to _ as well
440         for (i=0 ; i<SAVEGAME_COMMENT_LENGTH ; i++)
441                 if (text[i] <= ' ')
442                         text[i] = '_';
443         text[SAVEGAME_COMMENT_LENGTH] = '\0';
444 }
445
446
447 /*
448 ===============
449 Host_Savegame_f
450 ===============
451 */
452 void Host_Savegame_f (void)
453 {
454         char    name[MAX_QPATH];
455         qfile_t *f;
456         int             i;
457         char    comment[SAVEGAME_COMMENT_LENGTH+1];
458
459         if (cmd_source != src_command)
460                 return;
461
462         if (cls.state != ca_connected || !sv.active)
463         {
464                 Con_Print("Not playing a local game.\n");
465                 return;
466         }
467
468         if (cl.intermission)
469         {
470                 Con_Print("Can't save in intermission.\n");
471                 return;
472         }
473
474         for (i = 0;i < svs.maxclients;i++)
475         {
476                 if (svs.clients[i].active)
477                 {
478                         if (i > 0)
479                         {
480                                 Con_Print("Can't save multiplayer games.\n");
481                                 return;
482                         }
483                         if (svs.clients[i].edict->fields.server->deadflag)
484                         {
485                                 Con_Print("Can't savegame with a dead player\n");
486                                 return;
487                         }
488                 }
489         }
490
491         if (Cmd_Argc() != 2)
492         {
493                 Con_Print("save <savename> : save a game\n");
494                 return;
495         }
496
497         if (strstr(Cmd_Argv(1), ".."))
498         {
499                 Con_Print("Relative pathnames are not allowed.\n");
500                 return;
501         }
502
503         strlcpy (name, Cmd_Argv(1), sizeof (name));
504         FS_DefaultExtension (name, ".sav", sizeof (name));
505
506         Con_Printf("Saving game to %s...\n", name);
507         f = FS_Open (name, "wb", false, false);
508         if (!f)
509         {
510                 Con_Print("ERROR: couldn't open.\n");
511                 return;
512         }
513
514         FS_Printf(f, "%i\n", SAVEGAME_VERSION);
515         Host_SavegameComment (comment);
516         FS_Printf(f, "%s\n", comment);
517         for (i=0 ; i<NUM_SPAWN_PARMS ; i++)
518                 FS_Printf(f, "%f\n", svs.clients[0].spawn_parms[i]);
519         FS_Printf(f, "%d\n", current_skill);
520         FS_Printf(f, "%s\n", sv.name);
521         FS_Printf(f, "%f\n",sv.time);
522
523         // write the light styles
524         for (i=0 ; i<MAX_LIGHTSTYLES ; i++)
525         {
526                 if (sv.lightstyles[i][0])
527                         FS_Printf(f, "%s\n", sv.lightstyles[i]);
528                 else
529                         FS_Print(f,"m\n");
530         }
531
532         SV_VM_Begin();
533
534         PRVM_ED_WriteGlobals (f);
535         for (i=0 ; i<prog->num_edicts ; i++)
536                 PRVM_ED_Write (f, PRVM_EDICT_NUM(i));
537
538         SV_VM_End();
539
540         FS_Close (f);
541         Con_Print("done.\n");
542 }
543
544
545 /*
546 ===============
547 Host_Loadgame_f
548 ===============
549 */
550 void Host_Loadgame_f (void)
551 {
552         char filename[MAX_QPATH];
553         char mapname[MAX_QPATH];
554         float time;
555         const char *start;
556         const char *t;
557         const char *oldt;
558         char *text;
559         prvm_edict_t *ent;
560         int i;
561         int entnum;
562         int version;
563         float spawn_parms[NUM_SPAWN_PARMS];
564
565         if (cmd_source != src_command)
566                 return;
567
568         if (Cmd_Argc() != 2)
569         {
570                 Con_Print("load <savename> : load a game\n");
571                 return;
572         }
573
574         strcpy (filename, Cmd_Argv(1));
575         FS_DefaultExtension (filename, ".sav", sizeof (filename));
576
577         Con_Printf("Loading game from %s...\n", filename);
578
579         cls.demonum = -1;               // stop demo loop in case this fails
580
581         t = text = (char *)FS_LoadFile (filename, tempmempool, false, NULL);
582         if (!text)
583         {
584                 Con_Print("ERROR: couldn't open.\n");
585                 return;
586         }
587
588         // version
589         COM_ParseToken(&t, false);
590         version = atoi(com_token);
591         if (version != SAVEGAME_VERSION)
592         {
593                 Mem_Free(text);
594                 Con_Printf("Savegame is version %i, not %i\n", version, SAVEGAME_VERSION);
595                 return;
596         }
597
598         // description
599         // this is a little hard to parse, as : is a separator in COM_ParseToken,
600         // so use the console parser instead
601         COM_ParseTokenConsole(&t);
602
603         for (i = 0;i < NUM_SPAWN_PARMS;i++)
604         {
605                 COM_ParseToken(&t, false);
606                 spawn_parms[i] = atof(com_token);
607         }
608         // skill
609         COM_ParseToken(&t, false);
610 // this silliness is so we can load 1.06 save files, which have float skill values
611         current_skill = (int)(atof(com_token) + 0.5);
612         Cvar_SetValue ("skill", (float)current_skill);
613
614         // mapname
615         COM_ParseToken(&t, false);
616         strcpy (mapname, com_token);
617
618         // time
619         COM_ParseToken(&t, false);
620         time = atof(com_token);
621
622         allowcheats = sv_cheats.integer != 0;
623
624         SV_SpawnServer (mapname);
625         if (!sv.active)
626         {
627                 Mem_Free(text);
628                 Con_Print("Couldn't load map\n");
629                 return;
630         }
631         sv.paused = true;               // pause until all clients connect
632         sv.loadgame = true;
633
634 // load the light styles
635
636         for (i = 0;i < MAX_LIGHTSTYLES;i++)
637         {
638                 // light style
639                 oldt = t;
640                 COM_ParseToken(&t, false);
641                 // if this is a 64 lightstyle savegame produced by Quake, stop now
642                 // we have to check this because darkplaces saves 256 lightstyle savegames
643                 if (com_token[0] == '{')
644                 {
645                         t = oldt;
646                         break;
647                 }
648                 strlcpy(sv.lightstyles[i], com_token, sizeof(sv.lightstyles[i]));
649         }
650
651         // now skip everything before the first opening brace
652         // (this is for forward compatibility, so that older versions (at
653         // least ones with this fix) can load savegames with extra data before the
654         // first brace, as might be produced by a later engine version)
655         for(;;)
656         {
657                 oldt = t;
658                 COM_ParseToken(&t, false);
659                 if (com_token[0] == '{')
660                 {
661                         t = oldt;
662                         break;
663                 }
664         }
665
666 // load the edicts out of the savegame file
667         SV_VM_Begin();
668         // -1 is the globals
669         entnum = -1;
670         for (;;)
671         {
672                 start = t;
673                 while (COM_ParseToken(&t, false))
674                         if (!strcmp(com_token, "}"))
675                                 break;
676                 if (!COM_ParseToken(&start, false))
677                 {
678                         // end of file
679                         break;
680                 }
681                 if (strcmp(com_token,"{"))
682                 {
683                         Mem_Free(text);
684                         Host_Error ("First token isn't a brace");
685                 }
686
687                 if (entnum == -1)
688                 {
689                         // parse the global vars
690                         PRVM_ED_ParseGlobals (start);
691                 }
692                 else
693                 {
694                         // parse an edict
695                         if (entnum >= MAX_EDICTS)
696                         {
697                                 Mem_Free(text);
698                                 Host_Error("Host_PerformLoadGame: too many edicts in save file (reached MAX_EDICTS %i)", MAX_EDICTS);
699                         }
700                         while (entnum >= prog->max_edicts)
701                                 //SV_IncreaseEdicts();
702                                 PRVM_MEM_IncreaseEdicts();
703                         ent = PRVM_EDICT_NUM(entnum);
704                         memset (ent->fields.server, 0, prog->progs->entityfields * 4);
705                         ent->priv.server->free = false;
706                         PRVM_ED_ParseEdict (start, ent);
707
708                         // link it into the bsp tree
709                         if (!ent->priv.server->free)
710                                 SV_LinkEdict (ent, false);
711                 }
712
713                 entnum++;
714         }
715
716         prog->num_edicts = entnum;
717         sv.time = time;
718
719         for (i = 0;i < NUM_SPAWN_PARMS;i++)
720                 svs.clients[0].spawn_parms[i] = spawn_parms[i];
721
722         SV_VM_End();
723
724         // make sure we're connected to loopback
725         if (cls.state == ca_disconnected || !(cls.state == ca_connected && cls.netcon != NULL && LHNETADDRESS_GetAddressType(&cls.netcon->peeraddress) == LHNETADDRESSTYPE_LOOP))
726                 CL_EstablishConnection("local:1");
727 }
728
729 //============================================================================
730
731 /*
732 ======================
733 Host_Name_f
734 ======================
735 */
736 cvar_t cl_name = {CVAR_SAVE, "_cl_name", "player", "internal storage cvar for current player name (changed by name command)"};
737 void Host_Name_f (void)
738 {
739         int i, j;
740         char newName[sizeof(host_client->name)];
741
742         if (Cmd_Argc () == 1)
743         {
744                 Con_Printf("\"name\" is \"%s\"\n", cl_name.string);
745                 return;
746         }
747
748         if (Cmd_Argc () == 2)
749                 strlcpy (newName, Cmd_Argv(1), sizeof (newName));
750         else
751                 strlcpy (newName, Cmd_Args(), sizeof (newName));
752
753         for (i = 0, j = 0;newName[i];i++)
754                 if (newName[i] != '\r' && newName[i] != '\n')
755                         newName[j++] = newName[i];
756         newName[j] = 0;
757
758         if (cmd_source == src_command)
759         {
760                 Cvar_Set ("_cl_name", newName);
761                 if (cls.state == ca_connected)
762                         Cmd_ForwardToServer ();
763                 return;
764         }
765
766         if (sv.time < host_client->nametime)
767         {
768                 SV_ClientPrintf("You can't change name more than once every 5 seconds!\n");
769                 return;
770         }
771
772         host_client->nametime = sv.time + 5;
773
774         // point the string back at updateclient->name to keep it safe
775         strlcpy (host_client->name, newName, sizeof (host_client->name));
776         host_client->edict->fields.server->netname = PRVM_SetEngineString(host_client->name);
777         if (strcmp(host_client->old_name, host_client->name))
778         {
779                 if (host_client->spawned)
780                         SV_BroadcastPrintf("%s changed name to %s\n", host_client->old_name, host_client->name);
781                 strcpy(host_client->old_name, host_client->name);
782                 // send notification to all clients
783                 MSG_WriteByte (&sv.reliable_datagram, svc_updatename);
784                 MSG_WriteByte (&sv.reliable_datagram, host_client - svs.clients);
785                 MSG_WriteString (&sv.reliable_datagram, host_client->name);
786         }
787 }
788
789 /*
790 ======================
791 Host_Playermodel_f
792 ======================
793 */
794 cvar_t cl_playermodel = {CVAR_SAVE, "_cl_playermodel", "", "internal storage cvar for current player model in Nexuiz (changed by playermodel command)"};
795 // the old cl_playermodel in cl_main has been renamed to __cl_playermodel
796 void Host_Playermodel_f (void)
797 {
798         int i, j;
799         char newPath[sizeof(host_client->playermodel)];
800
801         if (Cmd_Argc () == 1)
802         {
803                 Con_Printf("\"playermodel\" is \"%s\"\n", cl_playermodel.string);
804                 return;
805         }
806
807         if (Cmd_Argc () == 2)
808                 strlcpy (newPath, Cmd_Argv(1), sizeof (newPath));
809         else
810                 strlcpy (newPath, Cmd_Args(), sizeof (newPath));
811
812         for (i = 0, j = 0;newPath[i];i++)
813                 if (newPath[i] != '\r' && newPath[i] != '\n')
814                         newPath[j++] = newPath[i];
815         newPath[j] = 0;
816
817         if (cmd_source == src_command)
818         {
819                 Cvar_Set ("_cl_playermodel", newPath);
820                 if (cls.state == ca_connected)
821                         Cmd_ForwardToServer ();
822                 return;
823         }
824
825         /*
826         if (sv.time < host_client->nametime)
827         {
828                 SV_ClientPrintf("You can't change playermodel more than once every 5 seconds!\n");
829                 return;
830         }
831
832         host_client->nametime = sv.time + 5;
833         */
834
835         // point the string back at updateclient->name to keep it safe
836         strlcpy (host_client->playermodel, newPath, sizeof (host_client->playermodel));
837         if( eval_playermodel )
838                 PRVM_GETEDICTFIELDVALUE(host_client->edict, eval_playermodel)->string = PRVM_SetEngineString(host_client->playermodel);
839         if (strcmp(host_client->old_model, host_client->playermodel))
840         {
841                 strcpy(host_client->old_model, host_client->playermodel);
842                 /*// send notification to all clients
843                 MSG_WriteByte (&sv.reliable_datagram, svc_updatepmodel);
844                 MSG_WriteByte (&sv.reliable_datagram, host_client - svs.clients);
845                 MSG_WriteString (&sv.reliable_datagram, host_client->playermodel);*/
846         }
847 }
848
849 /*
850 ======================
851 Host_Playerskin_f
852 ======================
853 */
854 cvar_t cl_playerskin = {CVAR_SAVE, "_cl_playerskin", "", "internal storage cvar for current player skin in Nexuiz (changed by playerskin command)"};
855 void Host_Playerskin_f (void)
856 {
857         int i, j;
858         char newPath[sizeof(host_client->playerskin)];
859
860         if (Cmd_Argc () == 1)
861         {
862                 Con_Printf("\"playerskin\" is \"%s\"\n", cl_playerskin.string);
863                 return;
864         }
865
866         if (Cmd_Argc () == 2)
867                 strlcpy (newPath, Cmd_Argv(1), sizeof (newPath));
868         else
869                 strlcpy (newPath, Cmd_Args(), sizeof (newPath));
870
871         for (i = 0, j = 0;newPath[i];i++)
872                 if (newPath[i] != '\r' && newPath[i] != '\n')
873                         newPath[j++] = newPath[i];
874         newPath[j] = 0;
875
876         if (cmd_source == src_command)
877         {
878                 Cvar_Set ("_cl_playerskin", newPath);
879                 if (cls.state == ca_connected)
880                         Cmd_ForwardToServer ();
881                 return;
882         }
883
884         /*
885         if (sv.time < host_client->nametime)
886         {
887                 SV_ClientPrintf("You can't change playermodel more than once every 5 seconds!\n");
888                 return;
889         }
890
891         host_client->nametime = sv.time + 5;
892         */
893
894         // point the string back at updateclient->name to keep it safe
895         strlcpy (host_client->playerskin, newPath, sizeof (host_client->playerskin));
896         if( eval_playerskin )
897                 PRVM_GETEDICTFIELDVALUE(host_client->edict, eval_playerskin)->string = PRVM_SetEngineString(host_client->playerskin);
898         if (strcmp(host_client->old_skin, host_client->playerskin))
899         {
900                 if (host_client->spawned)
901                         SV_BroadcastPrintf("%s changed skin to %s\n", host_client->name, host_client->playerskin);
902                 strcpy(host_client->old_skin, host_client->playerskin);
903                 /*// send notification to all clients
904                 MSG_WriteByte (&sv.reliable_datagram, svc_updatepskin);
905                 MSG_WriteByte (&sv.reliable_datagram, host_client - svs.clients);
906                 MSG_WriteString (&sv.reliable_datagram, host_client->playerskin);*/
907         }
908 }
909
910 void Host_Version_f (void)
911 {
912         Con_Printf("Version: %s build %s\n", gamename, buildstring);
913 }
914
915 void Host_Say(qboolean teamonly)
916 {
917         client_t *save;
918         int j, quoted;
919         const char *p1;
920         char *p2;
921         // LordHavoc: long say messages
922         char text[1024];
923         qboolean fromServer = false;
924
925         if (cmd_source == src_command)
926         {
927                 if (cls.state == ca_dedicated)
928                 {
929                         fromServer = true;
930                         teamonly = false;
931                 }
932                 else
933                 {
934                         Cmd_ForwardToServer ();
935                         return;
936                 }
937         }
938
939         if (Cmd_Argc () < 2)
940                 return;
941
942         if (!teamplay.integer)
943                 teamonly = false;
944
945 // turn on color set 1
946         p1 = Cmd_Args();
947         quoted = false;
948         if (*p1 == '\"')
949         {
950                 quoted = true;
951                 p1++;
952         }
953         if (!fromServer)
954                 dpsnprintf (text, sizeof(text), "%c%s" STRING_COLOR_DEFAULT_STR ": %s", 1, host_client->name, p1);
955         else
956                 dpsnprintf (text, sizeof(text), "%c<%s" STRING_COLOR_DEFAULT_STR "> %s", 1, hostname.string, p1);
957         p2 = text + strlen(text);
958         while ((const char *)p2 > (const char *)text && (p2[-1] == '\r' || p2[-1] == '\n' || (p2[-1] == '\"' && quoted)))
959         {
960                 if (p2[-1] == '\"' && quoted)
961                         quoted = false;
962                 p2[-1] = 0;
963                 p2--;
964         }
965         strlcat(text, "\n", sizeof(text));
966
967         // note: save is not a valid edict if fromServer is true
968         save = host_client;
969         for (j = 0, host_client = svs.clients;j < svs.maxclients;j++, host_client++)
970                 if (host_client->spawned && (!teamonly || host_client->edict->fields.server->team == save->edict->fields.server->team))
971                         SV_ClientPrint(text);
972         host_client = save;
973
974         if (cls.state == ca_dedicated)
975                 Con_Print(&text[1]);
976 }
977
978
979 void Host_Say_f(void)
980 {
981         Host_Say(false);
982 }
983
984
985 void Host_Say_Team_f(void)
986 {
987         Host_Say(true);
988 }
989
990
991 void Host_Tell_f(void)
992 {
993         client_t *save;
994         int j;
995         const char *p1, *p2;
996         char text[MAX_INPUTLINE]; // LordHavoc: FIXME: temporary buffer overflow fix (was 64)
997         qboolean fromServer = false;
998
999         if (cmd_source == src_command)
1000         {
1001                 if (cls.state == ca_dedicated)
1002                         fromServer = true;
1003                 else
1004                 {
1005                         Cmd_ForwardToServer ();
1006                         return;
1007                 }
1008         }
1009
1010         if (Cmd_Argc () < 3)
1011                 return;
1012
1013         if (!fromServer)
1014                 sprintf (text, "%s: ", host_client->name);
1015         else
1016                 sprintf (text, "<%s> ", hostname.string);
1017
1018         p1 = Cmd_Args();
1019         p2 = p1 + strlen(p1);
1020         // remove the target name
1021         while (p1 < p2 && *p1 != ' ')
1022                 p1++;
1023         while (p1 < p2 && *p1 == ' ')
1024                 p1++;
1025         // remove trailing newlines
1026         while (p2 > p1 && (p2[-1] == '\n' || p2[-1] == '\r'))
1027                 p2--;
1028         // remove quotes if present
1029         if (*p1 == '"')
1030         {
1031                 p1++;
1032                 if (p2[-1] == '"')
1033                         p2--;
1034                 else if (fromServer)
1035                         Con_Print("Host_Tell: missing end quote\n");
1036                 else
1037                         SV_ClientPrint("Host_Tell: missing end quote\n");
1038         }
1039         while (p2 > p1 && (p2[-1] == '\n' || p2[-1] == '\r'))
1040                 p2--;
1041         for (j = (int)strlen(text);j < (int)(sizeof(text) - 2) && p1 < p2;)
1042                 text[j++] = *p1++;
1043         text[j++] = '\n';
1044         text[j++] = 0;
1045
1046         save = host_client;
1047         for (j = 0, host_client = svs.clients;j < svs.maxclients;j++, host_client++)
1048                 if (host_client->spawned && !strcasecmp(host_client->name, Cmd_Argv(1)))
1049                         SV_ClientPrint(text);
1050         host_client = save;
1051 }
1052
1053
1054 /*
1055 ==================
1056 Host_Color_f
1057 ==================
1058 */
1059 cvar_t cl_color = {CVAR_SAVE, "_cl_color", "0", "internal storage cvar for current player colors (changed by color command)"};
1060 void Host_Color_f(void)
1061 {
1062         int             top, bottom;
1063         int             playercolor;
1064         mfunction_t *f;
1065         func_t  SV_ChangeTeam;
1066
1067         if (Cmd_Argc() == 1)
1068         {
1069                 Con_Printf("\"color\" is \"%i %i\"\n", cl_color.integer >> 4, cl_color.integer & 15);
1070                 Con_Print("color <0-15> [0-15]\n");
1071                 return;
1072         }
1073
1074         if (Cmd_Argc() == 2)
1075                 top = bottom = atoi(Cmd_Argv(1));
1076         else
1077         {
1078                 top = atoi(Cmd_Argv(1));
1079                 bottom = atoi(Cmd_Argv(2));
1080         }
1081
1082         top &= 15;
1083         // LordHavoc: allow skin colormaps 14 and 15 (was 13)
1084         if (top > 15)
1085                 top = 15;
1086         bottom &= 15;
1087         // LordHavoc: allow skin colormaps 14 and 15 (was 13)
1088         if (bottom > 15)
1089                 bottom = 15;
1090
1091         playercolor = top*16 + bottom;
1092
1093         if (cmd_source == src_command)
1094         {
1095                 Cvar_SetValue ("_cl_color", playercolor);
1096                 if (cls.state == ca_connected)
1097                         Cmd_ForwardToServer ();
1098                 return;
1099         }
1100
1101         if (host_client->edict && (f = PRVM_ED_FindFunction ("SV_ChangeTeam")) && (SV_ChangeTeam = (func_t)(f - prog->functions)))
1102         {
1103                 Con_DPrint("Calling SV_ChangeTeam\n");
1104                 prog->globals.server->time = sv.time;
1105                 prog->globals.generic[OFS_PARM0] = playercolor;
1106                 prog->globals.server->self = PRVM_EDICT_TO_PROG(host_client->edict);
1107                 PRVM_ExecuteProgram (SV_ChangeTeam, "QC function SV_ChangeTeam is missing");
1108         }
1109         else
1110         {
1111                 prvm_eval_t *val;
1112                 if (host_client->edict)
1113                 {
1114                         if ((val = PRVM_GETEDICTFIELDVALUE(host_client->edict, eval_clientcolors)))
1115                                 val->_float = playercolor;
1116                         host_client->edict->fields.server->team = bottom + 1;
1117                 }
1118                 host_client->colors = playercolor;
1119                 if (host_client->old_colors != host_client->colors)
1120                 {
1121                         host_client->old_colors = host_client->colors;
1122                         // send notification to all clients
1123                         MSG_WriteByte (&sv.reliable_datagram, svc_updatecolors);
1124                         MSG_WriteByte (&sv.reliable_datagram, host_client - svs.clients);
1125                         MSG_WriteByte (&sv.reliable_datagram, host_client->colors);
1126                 }
1127         }
1128 }
1129
1130 cvar_t cl_rate = {CVAR_SAVE, "_cl_rate", "10000", "internal storage cvar for current rate (changed by rate command)"};
1131 void Host_Rate_f(void)
1132 {
1133         int rate;
1134
1135         if (Cmd_Argc() != 2)
1136         {
1137                 Con_Printf("\"rate\" is \"%i\"\n", cl_rate.integer);
1138                 Con_Print("rate <500-25000>\n");
1139                 return;
1140         }
1141
1142         rate = atoi(Cmd_Argv(1));
1143
1144         if (cmd_source == src_command)
1145         {
1146                 Cvar_SetValue ("_cl_rate", bound(NET_MINRATE, rate, NET_MAXRATE));
1147                 if (cls.state == ca_connected)
1148                         Cmd_ForwardToServer ();
1149                 return;
1150         }
1151
1152         host_client->rate = rate;
1153 }
1154
1155 /*
1156 ==================
1157 Host_Kill_f
1158 ==================
1159 */
1160 void Host_Kill_f (void)
1161 {
1162         if (cmd_source == src_command)
1163         {
1164                 Cmd_ForwardToServer ();
1165                 return;
1166         }
1167
1168         if (host_client->edict->fields.server->health <= 0)
1169         {
1170                 SV_ClientPrint("Can't suicide -- already dead!\n");
1171                 return;
1172         }
1173
1174         prog->globals.server->time = sv.time;
1175         prog->globals.server->self = PRVM_EDICT_TO_PROG(host_client->edict);
1176         PRVM_ExecuteProgram (prog->globals.server->ClientKill, "QC function ClientKill is missing");
1177 }
1178
1179
1180 /*
1181 ==================
1182 Host_Pause_f
1183 ==================
1184 */
1185 void Host_Pause_f (void)
1186 {
1187
1188         if (cmd_source == src_command)
1189         {
1190                 Cmd_ForwardToServer ();
1191                 return;
1192         }
1193         if (!pausable.integer)
1194                 SV_ClientPrint("Pause not allowed.\n");
1195         else
1196         {
1197                 sv.paused ^= 1;
1198                 SV_BroadcastPrintf("%s %spaused the game\n", host_client->name, sv.paused ? "" : "un");
1199                 // send notification to all clients
1200                 MSG_WriteByte(&sv.reliable_datagram, svc_setpause);
1201                 MSG_WriteByte(&sv.reliable_datagram, sv.paused);
1202         }
1203 }
1204
1205 /*
1206 ======================
1207 Host_PModel_f
1208 LordHavoc: only supported for Nehahra, I personally think this is dumb, but Mindcrime won't listen.
1209 LordHavoc: correction, Mindcrime will be removing pmodel in the future, but it's still stuck here for compatibility.
1210 ======================
1211 */
1212 cvar_t cl_pmodel = {CVAR_SAVE, "_cl_pmodel", "0", "internal storage cvar for current player model number in nehahra (changed by pmodel command)"};
1213 static void Host_PModel_f (void)
1214 {
1215         int i;
1216         prvm_eval_t *val;
1217
1218         if (Cmd_Argc () == 1)
1219         {
1220                 Con_Printf("\"pmodel\" is \"%s\"\n", cl_pmodel.string);
1221                 return;
1222         }
1223         i = atoi(Cmd_Argv(1));
1224
1225         if (cmd_source == src_command)
1226         {
1227                 if (cl_pmodel.integer == i)
1228                         return;
1229                 Cvar_SetValue ("_cl_pmodel", i);
1230                 if (cls.state == ca_connected)
1231                         Cmd_ForwardToServer ();
1232                 return;
1233         }
1234
1235         if (host_client->edict && (val = PRVM_GETEDICTFIELDVALUE(host_client->edict, eval_pmodel)))
1236                 val->_float = i;
1237 }
1238
1239 //===========================================================================
1240
1241
1242 /*
1243 ==================
1244 Host_PreSpawn_f
1245 ==================
1246 */
1247 void Host_PreSpawn_f (void)
1248 {
1249         if (cmd_source == src_command)
1250         {
1251                 Con_Print("prespawn is not valid from the console\n");
1252                 return;
1253         }
1254
1255         if (host_client->spawned)
1256         {
1257                 Con_Print("prespawn not valid -- already spawned\n");
1258                 return;
1259         }
1260
1261         if (host_client->netconnection)
1262         {
1263                 SZ_Write (&host_client->netconnection->message, sv.signon.data, sv.signon.cursize);
1264                 MSG_WriteByte (&host_client->netconnection->message, svc_signonnum);
1265                 MSG_WriteByte (&host_client->netconnection->message, 2);
1266         }
1267
1268         // reset the name change timer because the client will send name soon
1269         host_client->nametime = 0;
1270 }
1271
1272 /*
1273 ==================
1274 Host_Spawn_f
1275 ==================
1276 */
1277 void Host_Spawn_f (void)
1278 {
1279         int i;
1280         client_t *client;
1281         func_t RestoreGame;
1282         mfunction_t *f;
1283         int stats[MAX_CL_STATS];
1284
1285         if (cmd_source == src_command)
1286         {
1287                 Con_Print("spawn is not valid from the console\n");
1288                 return;
1289         }
1290
1291         if (host_client->spawned)
1292         {
1293                 Con_Print("Spawn not valid -- already spawned\n");
1294                 return;
1295         }
1296
1297         // reset name change timer again because they might want to change name
1298         // again in the first 5 seconds after connecting
1299         host_client->nametime = 0;
1300
1301         // LordHavoc: moved this above the QC calls at FrikaC's request
1302         // LordHavoc: commented this out
1303         //if (host_client->netconnection)
1304         //      SZ_Clear (&host_client->netconnection->message);
1305
1306         // run the entrance script
1307         if (sv.loadgame)
1308         {
1309                 // loaded games are fully initialized already
1310                 // if this is the last client to be connected, unpause
1311                 sv.paused = false;
1312
1313                 if ((f = PRVM_ED_FindFunction ("RestoreGame")))
1314                 if ((RestoreGame = (func_t)(f - prog->functions)))
1315                 {
1316                         Con_DPrint("Calling RestoreGame\n");
1317                         prog->globals.server->time = sv.time;
1318                         prog->globals.server->self = PRVM_EDICT_TO_PROG(host_client->edict);
1319                         PRVM_ExecuteProgram (RestoreGame, "QC function RestoreGame is missing");
1320                 }
1321         }
1322         else
1323         {
1324                 //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);
1325
1326                 // copy spawn parms out of the client_t
1327                 for (i=0 ; i< NUM_SPAWN_PARMS ; i++)
1328                         (&prog->globals.server->parm1)[i] = host_client->spawn_parms[i];
1329
1330                 // call the spawn function
1331                 host_client->clientconnectcalled = true;
1332                 prog->globals.server->time = sv.time;
1333                 prog->globals.server->self = PRVM_EDICT_TO_PROG(host_client->edict);
1334                 PRVM_ExecuteProgram (prog->globals.server->ClientConnect, "QC function ClientConnect is missing");
1335
1336                 if ((Sys_DoubleTime() - host_client->connecttime) <= sv.time)
1337                         Con_Printf("%s entered the game\n", host_client->name);
1338
1339                 PRVM_ExecuteProgram (prog->globals.server->PutClientInServer, "QC function PutClientInServer is missing");
1340         }
1341
1342         if (!host_client->netconnection)
1343                 return;
1344
1345         // send time of update
1346         MSG_WriteByte (&host_client->netconnection->message, svc_time);
1347         MSG_WriteFloat (&host_client->netconnection->message, sv.time);
1348
1349         // send all current names, colors, and frag counts
1350         for (i = 0, client = svs.clients;i < svs.maxclients;i++, client++)
1351         {
1352                 if (!client->active)
1353                         continue;
1354                 MSG_WriteByte (&host_client->netconnection->message, svc_updatename);
1355                 MSG_WriteByte (&host_client->netconnection->message, i);
1356                 MSG_WriteString (&host_client->netconnection->message, client->name);
1357                 MSG_WriteByte (&host_client->netconnection->message, svc_updatefrags);
1358                 MSG_WriteByte (&host_client->netconnection->message, i);
1359                 MSG_WriteShort (&host_client->netconnection->message, client->frags);
1360                 MSG_WriteByte (&host_client->netconnection->message, svc_updatecolors);
1361                 MSG_WriteByte (&host_client->netconnection->message, i);
1362                 MSG_WriteByte (&host_client->netconnection->message, client->colors);
1363         }
1364
1365         // send all current light styles
1366         for (i=0 ; i<MAX_LIGHTSTYLES ; i++)
1367         {
1368                 if (sv.lightstyles[i][0])
1369                 {
1370                         MSG_WriteByte (&host_client->netconnection->message, svc_lightstyle);
1371                         MSG_WriteByte (&host_client->netconnection->message, (char)i);
1372                         MSG_WriteString (&host_client->netconnection->message, sv.lightstyles[i]);
1373                 }
1374         }
1375
1376         // send some stats
1377         MSG_WriteByte (&host_client->netconnection->message, svc_updatestat);
1378         MSG_WriteByte (&host_client->netconnection->message, STAT_TOTALSECRETS);
1379         MSG_WriteLong (&host_client->netconnection->message, prog->globals.server->total_secrets);
1380
1381         MSG_WriteByte (&host_client->netconnection->message, svc_updatestat);
1382         MSG_WriteByte (&host_client->netconnection->message, STAT_TOTALMONSTERS);
1383         MSG_WriteLong (&host_client->netconnection->message, prog->globals.server->total_monsters);
1384
1385         MSG_WriteByte (&host_client->netconnection->message, svc_updatestat);
1386         MSG_WriteByte (&host_client->netconnection->message, STAT_SECRETS);
1387         MSG_WriteLong (&host_client->netconnection->message, prog->globals.server->found_secrets);
1388
1389         MSG_WriteByte (&host_client->netconnection->message, svc_updatestat);
1390         MSG_WriteByte (&host_client->netconnection->message, STAT_MONSTERS);
1391         MSG_WriteLong (&host_client->netconnection->message, prog->globals.server->killed_monsters);
1392
1393         // send a fixangle
1394         // Never send a roll angle, because savegames can catch the server
1395         // in a state where it is expecting the client to correct the angle
1396         // and it won't happen if the game was just loaded, so you wind up
1397         // with a permanent head tilt
1398         MSG_WriteByte (&host_client->netconnection->message, svc_setangle);
1399         MSG_WriteAngle (&host_client->netconnection->message, host_client->edict->fields.server->angles[0], sv.protocol);
1400         MSG_WriteAngle (&host_client->netconnection->message, host_client->edict->fields.server->angles[1], sv.protocol);
1401         MSG_WriteAngle (&host_client->netconnection->message, 0, sv.protocol);
1402
1403         SV_WriteClientdataToMessage (host_client, host_client->edict, &host_client->netconnection->message, stats);
1404
1405         MSG_WriteByte (&host_client->netconnection->message, svc_signonnum);
1406         MSG_WriteByte (&host_client->netconnection->message, 3);
1407 }
1408
1409 /*
1410 ==================
1411 Host_Begin_f
1412 ==================
1413 */
1414 void Host_Begin_f (void)
1415 {
1416         if (cmd_source == src_command)
1417         {
1418                 Con_Print("begin is not valid from the console\n");
1419                 return;
1420         }
1421
1422         host_client->spawned = true;
1423 }
1424
1425 //===========================================================================
1426
1427
1428 /*
1429 ==================
1430 Host_Kick_f
1431
1432 Kicks a user off of the server
1433 ==================
1434 */
1435 void Host_Kick_f (void)
1436 {
1437         char *who;
1438         const char *message = NULL;
1439         client_t *save;
1440         int i;
1441         qboolean byNumber = false;
1442
1443         if (cmd_source != src_command || !sv.active)
1444                 return;
1445
1446         SV_VM_Begin();
1447         save = host_client;
1448
1449         if (Cmd_Argc() > 2 && strcmp(Cmd_Argv(1), "#") == 0)
1450         {
1451                 i = atof(Cmd_Argv(2)) - 1;
1452                 if (i < 0 || i >= svs.maxclients || !(host_client = svs.clients + i)->active)
1453                         return;
1454                 byNumber = true;
1455         }
1456         else
1457         {
1458                 for (i = 0, host_client = svs.clients;i < svs.maxclients;i++, host_client++)
1459                 {
1460                         if (!host_client->active)
1461                                 continue;
1462                         if (strcasecmp(host_client->name, Cmd_Argv(1)) == 0)
1463                                 break;
1464                 }
1465         }
1466
1467         if (i < svs.maxclients)
1468         {
1469                 if (cmd_source == src_command)
1470                 {
1471                         if (cls.state == ca_dedicated)
1472                                 who = "Console";
1473                         else
1474                                 who = cl_name.string;
1475                 }
1476                 else
1477                         who = save->name;
1478
1479                 // can't kick yourself!
1480                 if (host_client == save)
1481                         return;
1482
1483                 if (Cmd_Argc() > 2)
1484                 {
1485                         message = Cmd_Args();
1486                         COM_ParseToken(&message, false);
1487                         if (byNumber)
1488                         {
1489                                 message++;                                                      // skip the #
1490                                 while (*message == ' ')                         // skip white space
1491                                         message++;
1492                                 message += strlen(Cmd_Argv(2)); // skip the number
1493                         }
1494                         while (*message && *message == ' ')
1495                                 message++;
1496                 }
1497                 if (message)
1498                         SV_ClientPrintf("Kicked by %s: %s\n", who, message);
1499                 else
1500                         SV_ClientPrintf("Kicked by %s\n", who);
1501                 SV_DropClient (false); // kicked
1502         }
1503
1504         host_client = save;
1505         SV_VM_End();
1506 }
1507
1508 /*
1509 ===============================================================================
1510
1511 DEBUGGING TOOLS
1512
1513 ===============================================================================
1514 */
1515
1516 /*
1517 ==================
1518 Host_Give_f
1519 ==================
1520 */
1521 void Host_Give_f (void)
1522 {
1523         const char *t;
1524         int v;
1525         prvm_eval_t *val;
1526
1527         if (cmd_source == src_command)
1528         {
1529                 Cmd_ForwardToServer ();
1530                 return;
1531         }
1532
1533         if (!allowcheats)
1534         {
1535                 SV_ClientPrint("No cheats allowed, use sv_cheats 1 and restart level to enable.\n");
1536                 return;
1537         }
1538
1539         t = Cmd_Argv(1);
1540         v = atoi (Cmd_Argv(2));
1541
1542         switch (t[0])
1543         {
1544         case '0':
1545         case '1':
1546         case '2':
1547         case '3':
1548         case '4':
1549         case '5':
1550         case '6':
1551         case '7':
1552         case '8':
1553         case '9':
1554                 // MED 01/04/97 added hipnotic give stuff
1555                 if (gamemode == GAME_HIPNOTIC)
1556                 {
1557                         if (t[0] == '6')
1558                         {
1559                                 if (t[1] == 'a')
1560                                         host_client->edict->fields.server->items = (int)host_client->edict->fields.server->items | HIT_PROXIMITY_GUN;
1561                                 else
1562                                         host_client->edict->fields.server->items = (int)host_client->edict->fields.server->items | IT_GRENADE_LAUNCHER;
1563                         }
1564                         else if (t[0] == '9')
1565                                 host_client->edict->fields.server->items = (int)host_client->edict->fields.server->items | HIT_LASER_CANNON;
1566                         else if (t[0] == '0')
1567                                 host_client->edict->fields.server->items = (int)host_client->edict->fields.server->items | HIT_MJOLNIR;
1568                         else if (t[0] >= '2')
1569                                 host_client->edict->fields.server->items = (int)host_client->edict->fields.server->items | (IT_SHOTGUN << (t[0] - '2'));
1570                 }
1571                 else
1572                 {
1573                         if (t[0] >= '2')
1574                                 host_client->edict->fields.server->items = (int)host_client->edict->fields.server->items | (IT_SHOTGUN << (t[0] - '2'));
1575                 }
1576                 break;
1577
1578         case 's':
1579                 if (gamemode == GAME_ROGUE && (val = PRVM_GETEDICTFIELDVALUE(host_client->edict, eval_ammo_shells1)))
1580                         val->_float = v;
1581
1582                 host_client->edict->fields.server->ammo_shells = v;
1583                 break;
1584         case 'n':
1585                 if (gamemode == GAME_ROGUE)
1586                 {
1587                         if ((val = PRVM_GETEDICTFIELDVALUE(host_client->edict, eval_ammo_nails1)))
1588                         {
1589                                 val->_float = v;
1590                                 if (host_client->edict->fields.server->weapon <= IT_LIGHTNING)
1591                                         host_client->edict->fields.server->ammo_nails = v;
1592                         }
1593                 }
1594                 else
1595                 {
1596                         host_client->edict->fields.server->ammo_nails = v;
1597                 }
1598                 break;
1599         case 'l':
1600                 if (gamemode == GAME_ROGUE)
1601                 {
1602                         val = PRVM_GETEDICTFIELDVALUE(host_client->edict, eval_ammo_lava_nails);
1603                         if (val)
1604                         {
1605                                 val->_float = v;
1606                                 if (host_client->edict->fields.server->weapon > IT_LIGHTNING)
1607                                         host_client->edict->fields.server->ammo_nails = v;
1608                         }
1609                 }
1610                 break;
1611         case 'r':
1612                 if (gamemode == GAME_ROGUE)
1613                 {
1614                         val = PRVM_GETEDICTFIELDVALUE(host_client->edict, eval_ammo_rockets1);
1615                         if (val)
1616                         {
1617                                 val->_float = v;
1618                                 if (host_client->edict->fields.server->weapon <= IT_LIGHTNING)
1619                                         host_client->edict->fields.server->ammo_rockets = v;
1620                         }
1621                 }
1622                 else
1623                 {
1624                         host_client->edict->fields.server->ammo_rockets = v;
1625                 }
1626                 break;
1627         case 'm':
1628                 if (gamemode == GAME_ROGUE)
1629                 {
1630                         val = PRVM_GETEDICTFIELDVALUE(host_client->edict, eval_ammo_multi_rockets);
1631                         if (val)
1632                         {
1633                                 val->_float = v;
1634                                 if (host_client->edict->fields.server->weapon > IT_LIGHTNING)
1635                                         host_client->edict->fields.server->ammo_rockets = v;
1636                         }
1637                 }
1638                 break;
1639         case 'h':
1640                 host_client->edict->fields.server->health = v;
1641                 break;
1642         case 'c':
1643                 if (gamemode == GAME_ROGUE)
1644                 {
1645                         val = PRVM_GETEDICTFIELDVALUE(host_client->edict, eval_ammo_cells1);
1646                         if (val)
1647                         {
1648                                 val->_float = v;
1649                                 if (host_client->edict->fields.server->weapon <= IT_LIGHTNING)
1650                                         host_client->edict->fields.server->ammo_cells = v;
1651                         }
1652                 }
1653                 else
1654                 {
1655                         host_client->edict->fields.server->ammo_cells = v;
1656                 }
1657                 break;
1658         case 'p':
1659                 if (gamemode == GAME_ROGUE)
1660                 {
1661                         val = PRVM_GETEDICTFIELDVALUE(host_client->edict, eval_ammo_plasma);
1662                         if (val)
1663                         {
1664                                 val->_float = v;
1665                                 if (host_client->edict->fields.server->weapon > IT_LIGHTNING)
1666                                         host_client->edict->fields.server->ammo_cells = v;
1667                         }
1668                 }
1669                 break;
1670         }
1671 }
1672
1673 prvm_edict_t    *FindViewthing (void)
1674 {
1675         int             i;
1676         prvm_edict_t    *e;
1677
1678         for (i=0 ; i<prog->num_edicts ; i++)
1679         {
1680                 e = PRVM_EDICT_NUM(i);
1681                 if (!strcmp (PRVM_GetString(e->fields.server->classname), "viewthing"))
1682                         return e;
1683         }
1684         Con_Print("No viewthing on map\n");
1685         return NULL;
1686 }
1687
1688 /*
1689 ==================
1690 Host_Viewmodel_f
1691 ==================
1692 */
1693 void Host_Viewmodel_f (void)
1694 {
1695         prvm_edict_t    *e;
1696         model_t *m;
1697
1698         if (!sv.active)
1699                 return;
1700
1701         SV_VM_Begin();
1702         e = FindViewthing ();
1703         SV_VM_End();
1704         if (!e)
1705                 return;
1706
1707         m = Mod_ForName (Cmd_Argv(1), false, true, false);
1708         if (!m || !m->loaded || !m->Draw)
1709         {
1710                 Con_Printf("viewmodel: can't load %s\n", Cmd_Argv(1));
1711                 return;
1712         }
1713
1714         e->fields.server->frame = 0;
1715         cl.model_precache[(int)e->fields.server->modelindex] = m;
1716 }
1717
1718 /*
1719 ==================
1720 Host_Viewframe_f
1721 ==================
1722 */
1723 void Host_Viewframe_f (void)
1724 {
1725         prvm_edict_t    *e;
1726         int             f;
1727         model_t *m;
1728
1729         if (!sv.active)
1730                 return;
1731
1732         SV_VM_Begin();
1733         e = FindViewthing ();
1734         SV_VM_End();
1735         if (!e)
1736                 return;
1737         m = cl.model_precache[(int)e->fields.server->modelindex];
1738
1739         f = atoi(Cmd_Argv(1));
1740         if (f >= m->numframes)
1741                 f = m->numframes-1;
1742
1743         e->fields.server->frame = f;
1744 }
1745
1746
1747 void PrintFrameName (model_t *m, int frame)
1748 {
1749         if (m->animscenes)
1750                 Con_Printf("frame %i: %s\n", frame, m->animscenes[frame].name);
1751         else
1752                 Con_Printf("frame %i\n", frame);
1753 }
1754
1755 /*
1756 ==================
1757 Host_Viewnext_f
1758 ==================
1759 */
1760 void Host_Viewnext_f (void)
1761 {
1762         prvm_edict_t    *e;
1763         model_t *m;
1764
1765         if (!sv.active)
1766                 return;
1767
1768         SV_VM_Begin();
1769         e = FindViewthing ();
1770         SV_VM_End();
1771         if (!e)
1772                 return;
1773         m = cl.model_precache[(int)e->fields.server->modelindex];
1774
1775         e->fields.server->frame = e->fields.server->frame + 1;
1776         if (e->fields.server->frame >= m->numframes)
1777                 e->fields.server->frame = m->numframes - 1;
1778
1779         PrintFrameName (m, e->fields.server->frame);
1780 }
1781
1782 /*
1783 ==================
1784 Host_Viewprev_f
1785 ==================
1786 */
1787 void Host_Viewprev_f (void)
1788 {
1789         prvm_edict_t    *e;
1790         model_t *m;
1791
1792         if (!sv.active)
1793                 return;
1794
1795         SV_VM_Begin();
1796         e = FindViewthing ();
1797         SV_VM_End();
1798         if (!e)
1799                 return;
1800
1801         m = cl.model_precache[(int)e->fields.server->modelindex];
1802
1803         e->fields.server->frame = e->fields.server->frame - 1;
1804         if (e->fields.server->frame < 0)
1805                 e->fields.server->frame = 0;
1806
1807         PrintFrameName (m, e->fields.server->frame);
1808 }
1809
1810 /*
1811 ===============================================================================
1812
1813 DEMO LOOP CONTROL
1814
1815 ===============================================================================
1816 */
1817
1818
1819 /*
1820 ==================
1821 Host_Startdemos_f
1822 ==================
1823 */
1824 void Host_Startdemos_f (void)
1825 {
1826         int             i, c;
1827
1828         if (cls.state == ca_dedicated || COM_CheckParm("-listen") || COM_CheckParm("-benchmark") || COM_CheckParm("-demo") || COM_CheckParm("-demolooponly"))
1829                 return;
1830
1831         c = Cmd_Argc() - 1;
1832         if (c > MAX_DEMOS)
1833         {
1834                 Con_Printf("Max %i demos in demoloop\n", MAX_DEMOS);
1835                 c = MAX_DEMOS;
1836         }
1837         Con_Printf("%i demo(s) in loop\n", c);
1838
1839         for (i=1 ; i<c+1 ; i++)
1840                 strlcpy (cls.demos[i-1], Cmd_Argv(i), sizeof (cls.demos[i-1]));
1841
1842         // LordHavoc: clear the remaining slots
1843         for (;i <= MAX_DEMOS;i++)
1844                 cls.demos[i-1][0] = 0;
1845
1846         if (!sv.active && cls.demonum != -1 && !cls.demoplayback)
1847         {
1848                 cls.demonum = 0;
1849                 CL_NextDemo ();
1850         }
1851         else
1852                 cls.demonum = -1;
1853 }
1854
1855
1856 /*
1857 ==================
1858 Host_Demos_f
1859
1860 Return to looping demos
1861 ==================
1862 */
1863 void Host_Demos_f (void)
1864 {
1865         if (cls.state == ca_dedicated)
1866                 return;
1867         if (cls.demonum == -1)
1868                 cls.demonum = 1;
1869         CL_Disconnect_f ();
1870         CL_NextDemo ();
1871 }
1872
1873 /*
1874 ==================
1875 Host_Stopdemo_f
1876
1877 Return to looping demos
1878 ==================
1879 */
1880 void Host_Stopdemo_f (void)
1881 {
1882         if (!cls.demoplayback)
1883                 return;
1884         CL_Disconnect ();
1885         Host_ShutdownServer ();
1886 }
1887
1888 void Host_SendCvar_f (void)
1889 {
1890         int             i;
1891         cvar_t  *c;
1892         client_t *old;
1893
1894         if(Cmd_Argc() != 2)
1895                 return;
1896         if(!(c = Cvar_FindVar(Cmd_Argv(1))))
1897                 return;
1898         if (cls.state != ca_dedicated)
1899                 Cmd_ForwardStringToServer(va("sentcvar %s %s\n", c->name, c->string));
1900         if(!sv.active)// || !SV_ParseClientCommandQC)
1901                 return;
1902
1903         old = host_client;
1904         if (cls.state != ca_dedicated)
1905                 i = 1;
1906         else
1907                 i = 0;
1908         for(;i<svs.maxclients;i++)
1909                 if(svs.clients[i].active && svs.clients[i].netconnection)
1910                 {
1911                         host_client = &svs.clients[i];
1912                         Host_ClientCommands(va("sendcvar %s\n", c->name));
1913                 }
1914         host_client = old;
1915 }
1916
1917 static void MaxPlayers_f(void)
1918 {
1919         int n;
1920
1921         if (Cmd_Argc() != 2)
1922         {
1923                 Con_Printf("\"maxplayers\" is \"%u\"\n", svs.maxclients);
1924                 return;
1925         }
1926
1927         if (sv.active)
1928         {
1929                 Con_Print("maxplayers can not be changed while a server is running.\n");
1930                 return;
1931         }
1932
1933         n = atoi(Cmd_Argv(1));
1934         n = bound(1, n, MAX_SCOREBOARD);
1935         Con_Printf("\"maxplayers\" set to \"%u\"\n", n);
1936
1937         if (svs.clients)
1938                 Mem_Free(svs.clients);
1939         svs.maxclients = n;
1940         svs.clients = (client_t *)Mem_Alloc(sv_mempool, sizeof(client_t) * svs.maxclients);
1941         if (n == 1)
1942                 Cvar_Set ("deathmatch", "0");
1943         else
1944                 Cvar_Set ("deathmatch", "1");
1945 }
1946
1947 //=============================================================================
1948
1949 // QuakeWorld commands
1950
1951 char emodel_name[] =
1952         { 'e' ^ 0xff, 'm' ^ 0xff, 'o' ^ 0xff, 'd' ^ 0xff, 'e' ^ 0xff, 'l' ^ 0xff, 0 };
1953 char pmodel_name[] =
1954         { 'p' ^ 0xff, 'm' ^ 0xff, 'o' ^ 0xff, 'd' ^ 0xff, 'e' ^ 0xff, 'l' ^ 0xff, 0 };
1955 char prespawn_name[] =
1956         { 'p'^0xff, 'r'^0xff, 'e'^0xff, 's'^0xff, 'p'^0xff, 'a'^0xff, 'w'^0xff, 'n'^0xff,
1957                 ' '^0xff, '%'^0xff, 'i'^0xff, ' '^0xff, '0'^0xff, ' '^0xff, '%'^0xff, 'i'^0xff, 0 };
1958 char modellist_name[] =
1959         { 'm'^0xff, 'o'^0xff, 'd'^0xff, 'e'^0xff, 'l'^0xff, 'l'^0xff, 'i'^0xff, 's'^0xff, 't'^0xff,
1960                 ' '^0xff, '%'^0xff, 'i'^0xff, ' '^0xff, '%'^0xff, 'i'^0xff, 0 };
1961 char soundlist_name[] =
1962         { 's'^0xff, 'o'^0xff, 'u'^0xff, 'n'^0xff, 'd'^0xff, 'l'^0xff, 'i'^0xff, 's'^0xff, 't'^0xff,
1963                 ' '^0xff, '%'^0xff, 'i'^0xff, ' '^0xff, '%'^0xff, 'i'^0xff, 0 };
1964
1965 /*
1966 =====================
1967 Host_Rcon_f
1968
1969   Send the rest of the command line over as
1970   an unconnected command.
1971 =====================
1972 */
1973 void Host_Rcon_f (void) // credit: taken from QuakeWorld
1974 {
1975         lhnetaddress_t to;
1976         lhnetsocket_t *mysocket;
1977
1978         if (!rcon_password.string)
1979         {
1980                 Con_Printf ("You must set rcon_password before issuing an rcon command.\n");
1981                 return;
1982         }
1983
1984         if (cls.netcon)
1985                 to = cls.netcon->peeraddress;
1986         else
1987         {
1988                 if (!rcon_address.integer || !rcon_address.string[0])
1989                 {
1990                         Con_Printf ("You must either be connected, or set the rcon_address cvar to issue rcon commands\n");
1991                         return;
1992                 }
1993                 LHNETADDRESS_FromString(&to, rcon_address.string, sv_netport.integer);
1994         }
1995         mysocket = NetConn_ChooseClientSocketForAddress(&to);
1996         if (mysocket)
1997         {
1998                 // simply put together the rcon packet and send it
1999                 NetConn_WriteString(mysocket, va("\377\377\377\377rcon %s %s", rcon_password.string, Cmd_Args()), &to);
2000         }
2001 }
2002
2003 /*
2004 ====================
2005 Host_Packet_f
2006
2007 packet <destination> <contents>
2008
2009 Contents allows \n escape character
2010 ====================
2011 */
2012 void Host_Packet_f (void) // credit: taken from QuakeWorld
2013 {
2014         char send[2048];
2015         int i, l;
2016         const char *in;
2017         char *out;
2018         lhnetaddress_t address;
2019         lhnetsocket_t *mysocket;
2020
2021         if (Cmd_Argc() != 3)
2022         {
2023                 Con_Printf ("packet <destination> <contents>\n");
2024                 return;
2025         }
2026
2027         if (!LHNETADDRESS_FromString (&address, Cmd_Argv(1), sv_netport.integer))
2028         {
2029                 Con_Printf ("Bad address\n");
2030                 return;
2031         }
2032
2033         in = Cmd_Argv(2);
2034         out = send+4;
2035         send[0] = send[1] = send[2] = send[3] = 0xff;
2036
2037         l = strlen (in);
2038         for (i=0 ; i<l ; i++)
2039         {
2040                 if (out >= send + sizeof(send) - 1)
2041                         break;
2042                 if (in[i] == '\\' && in[i+1] == 'n')
2043                 {
2044                         *out++ = '\n';
2045                         i++;
2046                 }
2047                 else
2048                         *out++ = in[i];
2049         }
2050         *out = 0;
2051
2052         mysocket = NetConn_ChooseClientSocketForAddress(&address);
2053         if (mysocket)
2054                 NetConn_WriteString(mysocket, send, &address);
2055 }
2056
2057 //=============================================================================
2058
2059 /*
2060 ==================
2061 Host_InitCommands
2062 ==================
2063 */
2064 void Host_InitCommands (void)
2065 {
2066         Cmd_AddCommand ("status", Host_Status_f, "print server status information");
2067         Cmd_AddCommand ("quit", Host_Quit_f, "quit the game");
2068         if (gamemode == GAME_NEHAHRA)
2069         {
2070                 Cmd_AddCommand ("max", Host_God_f, "god mode (invulnerability)");
2071                 Cmd_AddCommand ("monster", Host_Notarget_f, "notarget mode (monsters do not see you)");
2072                 Cmd_AddCommand ("scrag", Host_Fly_f, "fly mode (flight)");
2073                 Cmd_AddCommand ("wraith", Host_Noclip_f, "noclip mode (flight without collisions, move through walls)");
2074                 Cmd_AddCommand ("gimme", Host_Give_f, "alter inventory");
2075         }
2076         else
2077         {
2078                 Cmd_AddCommand ("god", Host_God_f, "god mode (invulnerability)");
2079                 Cmd_AddCommand ("notarget", Host_Notarget_f, "notarget mode (monsters do not see you)");
2080                 Cmd_AddCommand ("fly", Host_Fly_f, "fly mode (flight)");
2081                 Cmd_AddCommand ("noclip", Host_Noclip_f, "noclip mode (flight without collisions, move through walls)");
2082                 Cmd_AddCommand ("give", Host_Give_f, "alter inventory");
2083         }
2084         Cmd_AddCommand ("map", Host_Map_f, "kick everyone off the server and start a new level");
2085         Cmd_AddCommand ("restart", Host_Restart_f, "restart current level");
2086         Cmd_AddCommand ("changelevel", Host_Changelevel_f, "change to another level, bringing along all connected clients");
2087         Cmd_AddCommand ("connect", Host_Connect_f, "connect to a server by IP address or hostname");
2088         Cmd_AddCommand ("reconnect", Host_Reconnect_f, "reset signon level in preparation for a new level (do not use)");
2089         Cmd_AddCommand ("version", Host_Version_f, "print engine version");
2090         Cmd_AddCommand ("say", Host_Say_f, "send a chat message to everyone on the server");
2091         Cmd_AddCommand ("say_team", Host_Say_Team_f, "send a chat message to your team on the server");
2092         Cmd_AddCommand ("tell", Host_Tell_f, "send a chat message to only one person on the server");
2093         Cmd_AddCommand ("kill", Host_Kill_f, "die instantly");
2094         Cmd_AddCommand ("pause", Host_Pause_f, "pause the game (if the server allows pausing)");
2095         Cmd_AddCommand ("kick", Host_Kick_f, "kick a player off the server by number or name");
2096         Cmd_AddCommand ("ping", Host_Ping_f, "print ping times of all players on the server");
2097         Cmd_AddCommand ("load", Host_Loadgame_f, "load a saved game file");
2098         Cmd_AddCommand ("save", Host_Savegame_f, "save the game to a file");
2099
2100         Cmd_AddCommand ("startdemos", Host_Startdemos_f, "start playing back the selected demos sequentially (used at end of startup script)");
2101         Cmd_AddCommand ("demos", Host_Demos_f, "restart looping demos defined by the last startdemos command");
2102         Cmd_AddCommand ("stopdemo", Host_Stopdemo_f, "stop playing or recording demo (like stop command) and return to looping demos");
2103
2104         Cmd_AddCommand ("viewmodel", Host_Viewmodel_f, "change model of viewthing entity in current level");
2105         Cmd_AddCommand ("viewframe", Host_Viewframe_f, "change animation frame of viewthing entity in current level");
2106         Cmd_AddCommand ("viewnext", Host_Viewnext_f, "change to next animation frame of viewthing entity in current level");
2107         Cmd_AddCommand ("viewprev", Host_Viewprev_f, "change to previous animation frame of viewthing entity in current level");
2108
2109         Cvar_RegisterVariable (&cl_name);
2110         Cmd_AddCommand ("name", Host_Name_f, "change your player name");
2111         Cvar_RegisterVariable (&cl_color);
2112         Cmd_AddCommand ("color", Host_Color_f, "change your player shirt and pants colors");
2113         Cvar_RegisterVariable (&cl_rate);
2114         Cmd_AddCommand ("rate", Host_Rate_f, "change your network connection speed");
2115         if (gamemode == GAME_NEHAHRA)
2116         {
2117                 Cvar_RegisterVariable (&cl_pmodel);
2118                 Cmd_AddCommand ("pmodel", Host_PModel_f, "change your player model choice (Nehahra specific)");
2119         }
2120
2121         // BLACK: This isnt game specific anymore (it was GAME_NEXUIZ at first)
2122         Cvar_RegisterVariable (&cl_playermodel);
2123         Cmd_AddCommand ("playermodel", Host_Playermodel_f, "change your player model");
2124         Cvar_RegisterVariable (&cl_playerskin);
2125         Cmd_AddCommand ("playerskin", Host_Playerskin_f, "change your player skin number");
2126
2127         Cmd_AddCommand ("prespawn", Host_PreSpawn_f, "signon 1 (client acknowledges that server information has been received)");
2128         Cmd_AddCommand ("spawn", Host_Spawn_f, "signon 2 (client has sent player information, and is asking server to send scoreboard rankings)");
2129         Cmd_AddCommand ("begin", 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)");
2130         Cmd_AddCommand ("maxplayers", MaxPlayers_f, "sets limit on how many players (or bots) may be connected to the server at once");
2131
2132         Cmd_AddCommand ("sendcvar", Host_SendCvar_f, "sends the value of a cvar to the server as a sentcvar command, for use by QuakeC");       // By [515]
2133
2134         Cvar_RegisterVariable (&rcon_password);
2135         Cvar_RegisterVariable (&rcon_address);
2136         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)");
2137         Cmd_AddCommand ("packet", Host_Packet_f, "send a packet to the specified address:port containing a text string");
2138
2139         Cvar_RegisterVariable(&sv_cheats);
2140 }
2141