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