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