]> icculus.org git repositories - divverent/darkplaces.git/blob - host_cmd.c
Linux GLX, a lot of code shrinkage/cleanup, assembly support.
[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 extern cvar_t   pausable;
24
25 int     current_skill;
26
27 void Mod_Print (void);
28
29 /*
30 ==================
31 Host_Quit_f
32 ==================
33 */
34
35 // LordHavoc: didn't like it asking me if I wanted to quit
36 //extern void M_Menu_Quit_f (void);
37
38 void Host_Quit_f (void)
39 {
40         /*
41         if (key_dest != key_console && cls.state != ca_dedicated)
42         {
43                 M_Menu_Quit_f ();
44                 return;
45         }
46         */
47         CL_Disconnect ();
48         Host_ShutdownServer(false);             
49
50         Sys_Quit ();
51 }
52
53
54 /*
55 ==================
56 Host_Status_f
57 ==================
58 */
59 void Host_Status_f (void)
60 {
61         client_t        *client;
62         int                     seconds;
63         int                     minutes;
64         int                     hours = 0;
65         int                     j;
66         void            (*print) (char *fmt, ...);
67         
68         if (cmd_source == src_command)
69         {
70                 if (!sv.active)
71                 {
72                         Cmd_ForwardToServer ();
73                         return;
74                 }
75                 print = Con_Printf;
76         }
77         else
78                 print = SV_ClientPrintf;
79
80         print ("host:    %s\n", Cvar_VariableString ("hostname"));
81         print ("version: %4.2f\n", VERSION);
82         if (tcpipAvailable)
83                 print ("tcp/ip:  %s\n", my_tcpip_address);
84         if (ipxAvailable)
85                 print ("ipx:     %s\n", my_ipx_address);
86         print ("map:     %s\n", sv.name);
87         print ("players: %i active (%i max)\n\n", net_activeconnections, svs.maxclients);
88         for (j=0, client = svs.clients ; j<svs.maxclients ; j++, client++)
89         {
90                 if (!client->active)
91                         continue;
92                 seconds = (int)(net_time - client->netconnection->connecttime);
93                 minutes = seconds / 60;
94                 if (minutes)
95                 {
96                         seconds -= (minutes * 60);
97                         hours = minutes / 60;
98                         if (hours)
99                                 minutes -= (hours * 60);
100                 }
101                 else
102                         hours = 0;
103                 print ("#%-2u %-16.16s  %3i  %2i:%02i:%02i\n", j+1, client->name, (int)client->edict->v.frags, hours, minutes, seconds);
104                 print ("   %s\n", client->netconnection->address);
105         }
106 }
107
108
109 /*
110 ==================
111 Host_God_f
112
113 Sets client to godmode
114 ==================
115 */
116 void Host_God_f (void)
117 {
118         if (cmd_source == src_command)
119         {
120                 Cmd_ForwardToServer ();
121                 return;
122         }
123
124         if (pr_global_struct->deathmatch)
125                 return;
126
127         sv_player->v.flags = (int)sv_player->v.flags ^ FL_GODMODE;
128         if (!((int)sv_player->v.flags & FL_GODMODE) )
129                 SV_ClientPrintf ("godmode OFF\n");
130         else
131                 SV_ClientPrintf ("godmode ON\n");
132 }
133
134 void Host_Notarget_f (void)
135 {
136         if (cmd_source == src_command)
137         {
138                 Cmd_ForwardToServer ();
139                 return;
140         }
141
142         if (pr_global_struct->deathmatch)
143                 return;
144
145         sv_player->v.flags = (int)sv_player->v.flags ^ FL_NOTARGET;
146         if (!((int)sv_player->v.flags & FL_NOTARGET) )
147                 SV_ClientPrintf ("notarget OFF\n");
148         else
149                 SV_ClientPrintf ("notarget ON\n");
150 }
151
152 qboolean noclip_anglehack;
153
154 void Host_Noclip_f (void)
155 {
156         if (cmd_source == src_command)
157         {
158                 Cmd_ForwardToServer ();
159                 return;
160         }
161
162         if (pr_global_struct->deathmatch)
163                 return;
164
165         if (sv_player->v.movetype != MOVETYPE_NOCLIP)
166         {
167                 noclip_anglehack = true;
168                 sv_player->v.movetype = MOVETYPE_NOCLIP;
169                 SV_ClientPrintf ("noclip ON\n");
170         }
171         else
172         {
173                 noclip_anglehack = false;
174                 sv_player->v.movetype = MOVETYPE_WALK;
175                 SV_ClientPrintf ("noclip OFF\n");
176         }
177 }
178
179 /*
180 ==================
181 Host_Fly_f
182
183 Sets client to flymode
184 ==================
185 */
186 void Host_Fly_f (void)
187 {
188         if (cmd_source == src_command)
189         {
190                 Cmd_ForwardToServer ();
191                 return;
192         }
193
194         if (pr_global_struct->deathmatch)
195                 return;
196
197         if (sv_player->v.movetype != MOVETYPE_FLY)
198         {
199                 sv_player->v.movetype = MOVETYPE_FLY;
200                 SV_ClientPrintf ("flymode ON\n");
201         }
202         else
203         {
204                 sv_player->v.movetype = MOVETYPE_WALK;
205                 SV_ClientPrintf ("flymode OFF\n");
206         }
207 }
208
209
210 /*
211 ==================
212 Host_Ping_f
213
214 ==================
215 */
216 void Host_Ping_f (void)
217 {
218         int             i, j;
219         float   total;
220         client_t        *client;
221         
222         if (cmd_source == src_command)
223         {
224                 Cmd_ForwardToServer ();
225                 return;
226         }
227
228         SV_ClientPrintf ("Client ping times:\n");
229         for (i=0, client = svs.clients ; i<svs.maxclients ; i++, client++)
230         {
231                 if (!client->active)
232                         continue;
233                 total = 0;
234                 for (j=0 ; j<NUM_PING_TIMES ; j++)
235                         total+=client->ping_times[j];
236                 total /= NUM_PING_TIMES;
237                 SV_ClientPrintf ("%4i %s\n", (int)(total*1000), client->name);
238         }
239 }
240
241 /*
242 ===============================================================================
243
244 SERVER TRANSITIONS
245
246 ===============================================================================
247 */
248
249
250 /*
251 ======================
252 Host_Map_f
253
254 handle a 
255 map <servername>
256 command from the console.  Active clients are kicked off.
257 ======================
258 */
259 void Host_Map_f (void)
260 {
261         int             i;
262         char    name[MAX_QPATH];
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(false);             
271
272         key_dest = key_game;                    // remove console or menu
273         SCR_BeginLoadingPlaque ();
274
275         cls.mapstring[0] = 0;
276         for (i=0 ; i<Cmd_Argc() ; i++)
277         {
278                 strcat (cls.mapstring, Cmd_Argv(i));
279                 strcat (cls.mapstring, " ");
280         }
281         strcat (cls.mapstring, "\n");
282
283         svs.serverflags = 0;                    // haven't completed an episode yet
284         strcpy (name, Cmd_Argv(1));
285         SV_SpawnServer (name);
286         if (!sv.active)
287                 return;
288         
289         if (cls.state != ca_dedicated)
290         {
291                 strcpy (cls.spawnparms, "");
292
293                 for (i=2 ; i<Cmd_Argc() ; i++)
294                 {
295                         strcat (cls.spawnparms, Cmd_Argv(i));
296                         strcat (cls.spawnparms, " ");
297                 }
298                 
299                 Cmd_ExecuteString ("connect local", src_command);
300         }       
301 }
302
303 /*
304 ==================
305 Host_Changelevel_f
306
307 Goes to a new map, taking all clients along
308 ==================
309 */
310 void Host_Changelevel_f (void)
311 {
312         char    level[MAX_QPATH];
313
314         if (Cmd_Argc() != 2)
315         {
316                 Con_Printf ("changelevel <levelname> : continue game on a new level\n");
317                 return;
318         }
319         if (!sv.active || cls.demoplayback)
320         {
321                 Con_Printf ("Only the server may changelevel\n");
322                 return;
323         }
324         SV_SaveSpawnparms ();
325         strcpy (level, Cmd_Argv(1));
326         SV_SpawnServer (level);
327 }
328
329 /*
330 ==================
331 Host_Restart_f
332
333 Restarts the current server for a dead player
334 ==================
335 */
336 void Host_Restart_f (void)
337 {
338         char    mapname[MAX_QPATH];
339
340         if (cls.demoplayback || !sv.active)
341                 return;
342
343         if (cmd_source != src_command)
344                 return;
345         strcpy (mapname, sv.name);      // must copy out, because it gets cleared
346                                                                 // in sv_spawnserver
347         SV_SpawnServer (mapname);
348 }
349
350 /*
351 ==================
352 Host_Reconnect_f
353
354 This command causes the client to wait for the signon messages again.
355 This is sent just before a server changes levels
356 ==================
357 */
358 void Host_Reconnect_f (void)
359 {
360         SCR_BeginLoadingPlaque ();
361         cls.signon = 0;         // need new connection messages
362 }
363
364 /*
365 =====================
366 Host_Connect_f
367
368 User command to connect to server
369 =====================
370 */
371 void Host_Connect_f (void)
372 {
373         char    name[MAX_QPATH];
374         
375         cls.demonum = -1;               // stop demo loop in case this fails
376         if (cls.demoplayback)
377         {
378                 CL_StopPlayback ();
379                 CL_Disconnect ();
380         }
381         strcpy (name, Cmd_Argv(1));
382         CL_EstablishConnection (name);
383         Host_Reconnect_f ();
384 }
385
386
387 /*
388 ===============================================================================
389
390 LOAD / SAVE GAME
391
392 ===============================================================================
393 */
394
395 #define SAVEGAME_VERSION        5
396
397 /*
398 ===============
399 Host_SavegameComment
400
401 Writes a SAVEGAME_COMMENT_LENGTH character comment describing the current 
402 ===============
403 */
404 void Host_SavegameComment (char *text)
405 {
406         int             i;
407         char    kills[20];
408
409         for (i=0 ; i<SAVEGAME_COMMENT_LENGTH ; i++)
410                 text[i] = ' ';
411         memcpy (text, cl.levelname, strlen(cl.levelname));
412         sprintf (kills,"kills:%3i/%3i", cl.stats[STAT_MONSTERS], cl.stats[STAT_TOTALMONSTERS]);
413         memcpy (text+22, kills, strlen(kills));
414 // convert space to _ to make stdio happy
415         for (i=0 ; i<SAVEGAME_COMMENT_LENGTH ; i++)
416                 if (text[i] == ' ')
417                         text[i] = '_';
418         text[SAVEGAME_COMMENT_LENGTH] = '\0';
419 }
420
421
422 /*
423 ===============
424 Host_Savegame_f
425 ===============
426 */
427 void Host_Savegame_f (void)
428 {
429         char    name[256];
430         FILE    *f;
431         int             i;
432         char    comment[SAVEGAME_COMMENT_LENGTH+1];
433
434         if (cmd_source != src_command)
435                 return;
436
437         if (!sv.active)
438         {
439                 Con_Printf ("Not playing a local game.\n");
440                 return;
441         }
442
443         if (cl.intermission)
444         {
445                 Con_Printf ("Can't save in intermission.\n");
446                 return;
447         }
448
449         if (svs.maxclients != 1)
450         {
451                 Con_Printf ("Can't save multiplayer games.\n");
452                 return;
453         }
454
455         if (Cmd_Argc() != 2)
456         {
457                 Con_Printf ("save <savename> : save a game\n");
458                 return;
459         }
460
461         if (strstr(Cmd_Argv(1), ".."))
462         {
463                 Con_Printf ("Relative pathnames are not allowed.\n");
464                 return;
465         }
466                 
467         for (i=0 ; i<svs.maxclients ; i++)
468         {
469                 if (svs.clients[i].active && (svs.clients[i].edict->v.health <= 0) )
470                 {
471                         Con_Printf ("Can't savegame with a dead player\n");
472                         return;
473                 }
474         }
475
476         sprintf (name, "%s/%s", com_gamedir, Cmd_Argv(1));
477         COM_DefaultExtension (name, ".sav");
478         
479         Con_Printf ("Saving game to %s...\n", name);
480         f = fopen (name, "w");
481         if (!f)
482         {
483                 Con_Printf ("ERROR: couldn't open.\n");
484                 return;
485         }
486         
487         fprintf (f, "%i\n", SAVEGAME_VERSION);
488         Host_SavegameComment (comment);
489         fprintf (f, "%s\n", comment);
490         for (i=0 ; i<NUM_SPAWN_PARMS ; i++)
491                 fprintf (f, "%f\n", svs.clients->spawn_parms[i]);
492         fprintf (f, "%d\n", current_skill);
493         fprintf (f, "%s\n", sv.name);
494         fprintf (f, "%f\n",sv.time);
495
496 // write the light styles
497
498         for (i=0 ; i<MAX_LIGHTSTYLES ; i++)
499         {
500                 if (sv.lightstyles[i])
501                         fprintf (f, "%s\n", sv.lightstyles[i]);
502                 else
503                         fprintf (f,"m\n");
504         }
505
506
507         ED_WriteGlobals (f);
508         for (i=0 ; i<sv.num_edicts ; i++)
509         {
510                 ED_Write (f, EDICT_NUM(i));
511                 fflush (f);
512         }
513         fclose (f);
514         Con_Printf ("done.\n");
515 }
516
517
518 /*
519 ===============
520 Host_Loadgame_f
521 ===============
522 */
523 void Host_Loadgame_f (void)
524 {
525         char    name[MAX_OSPATH];
526         FILE    *f;
527         char    mapname[MAX_QPATH];
528         float   time, tfloat;
529         char    str[32768], *start;
530         int             i, r;
531         edict_t *ent;
532         int             entnum;
533         int             version;
534         float                   spawn_parms[NUM_SPAWN_PARMS];
535
536         if (cmd_source != src_command)
537                 return;
538
539         if (Cmd_Argc() != 2)
540         {
541                 Con_Printf ("load <savename> : load a game\n");
542                 return;
543         }
544
545         cls.demonum = -1;               // stop demo loop in case this fails
546
547         sprintf (name, "%s/%s", com_gamedir, Cmd_Argv(1));
548         COM_DefaultExtension (name, ".sav");
549         
550 // we can't call SCR_BeginLoadingPlaque, because too much stack space has
551 // been used.  The menu calls it before stuffing loadgame command
552 //      SCR_BeginLoadingPlaque ();
553
554         Con_Printf ("Loading game from %s...\n", name);
555         f = fopen (name, "r");
556         if (!f)
557         {
558                 Con_Printf ("ERROR: couldn't open.\n");
559                 return;
560         }
561
562         fscanf (f, "%i\n", &version);
563         if (version != SAVEGAME_VERSION)
564         {
565                 fclose (f);
566                 Con_Printf ("Savegame is version %i, not %i\n", version, SAVEGAME_VERSION);
567                 return;
568         }
569         fscanf (f, "%s\n", str);
570         for (i=0 ; i<NUM_SPAWN_PARMS ; i++)
571                 fscanf (f, "%f\n", &spawn_parms[i]);
572 // this silliness is so we can load 1.06 save files, which have float skill values
573         fscanf (f, "%f\n", &tfloat);
574         current_skill = (int)(tfloat + 0.1);
575         Cvar_SetValue ("skill", (float)current_skill);
576
577         fscanf (f, "%s\n",mapname);
578         fscanf (f, "%f\n",&time);
579
580         CL_Disconnect_f ();
581         
582         SV_SpawnServer (mapname);
583         if (!sv.active)
584         {
585                 Con_Printf ("Couldn't load map\n");
586                 return;
587         }
588         sv.paused = true;               // pause until all clients connect
589         sv.loadgame = true;
590
591 // load the light styles
592
593         for (i=0 ; i<MAX_LIGHTSTYLES ; i++)
594         {
595                 fscanf (f, "%s\n", str);
596                 sv.lightstyles[i] = Hunk_Alloc (strlen(str)+1);
597                 strcpy (sv.lightstyles[i], str);
598         }
599
600 // load the edicts out of the savegame file
601         entnum = -1;            // -1 is the globals
602         while (!feof(f))
603         {
604                 for (i=0 ; i<sizeof(str)-1 ; i++)
605                 {
606                         r = fgetc (f);
607                         if (r == EOF || !r)
608                                 break;
609                         str[i] = r;
610                         if (r == '}')
611                         {
612                                 i++;
613                                 break;
614                         }
615                 }
616                 if (i == sizeof(str)-1)
617                         Sys_Error ("Loadgame buffer overflow");
618                 str[i] = 0;
619                 start = str;
620                 start = COM_Parse(str);
621                 if (!com_token[0])
622                         break;          // end of file
623                 if (strcmp(com_token,"{"))
624                         Sys_Error ("First token isn't a brace");
625                         
626                 if (entnum == -1)
627                 {       // parse the global vars
628                         ED_ParseGlobals (start);
629                 }
630                 else
631                 {       // parse an edict
632
633                         ent = EDICT_NUM(entnum);
634                         memset (&ent->v, 0, progs->entityfields * 4);
635                         ent->free = false;
636                         ED_ParseEdict (start, ent);
637         
638                 // link it into the bsp tree
639                         if (!ent->free)
640                                 SV_LinkEdict (ent, false);
641                 }
642
643                 entnum++;
644         }
645         
646         sv.num_edicts = entnum;
647         sv.time = time;
648
649         fclose (f);
650
651         for (i=0 ; i<NUM_SPAWN_PARMS ; i++)
652                 svs.clients->spawn_parms[i] = spawn_parms[i];
653
654         if (cls.state != ca_dedicated)
655         {
656                 CL_EstablishConnection ("local");
657                 Host_Reconnect_f ();
658         }
659 }
660
661 //============================================================================
662
663 /*
664 ======================
665 Host_Name_f
666 ======================
667 */
668 void Host_Name_f (void)
669 {
670         char    *newName;
671
672         if (Cmd_Argc () == 1)
673         {
674                 Con_Printf ("\"name\" is \"%s\"\n", cl_name.string);
675                 return;
676         }
677         if (Cmd_Argc () == 2)
678                 newName = Cmd_Argv(1);  
679         else
680                 newName = Cmd_Args();
681         newName[15] = 0;
682
683         if (cmd_source == src_command)
684         {
685                 if (strcmp(cl_name.string, newName) == 0)
686                         return;
687                 Cvar_Set ("_cl_name", newName);
688                 if (cls.state == ca_connected)
689                         Cmd_ForwardToServer ();
690                 return;
691         }
692
693         if (host_client->name[0] && strcmp(host_client->name, "unconnected") )
694                 if (strcmp(host_client->name, newName) != 0)
695                         Con_Printf ("%s renamed to %s\n", host_client->name, newName);
696         strcpy (host_client->name, newName);
697         host_client->edict->v.netname = host_client->name - pr_strings;
698         
699 // send notification to all clients
700         
701         MSG_WriteByte (&sv.reliable_datagram, svc_updatename);
702         MSG_WriteByte (&sv.reliable_datagram, host_client - svs.clients);
703         MSG_WriteString (&sv.reliable_datagram, host_client->name);
704 }
705
706         
707 void Host_Version_f (void)
708 {
709         Con_Printf ("Version %4.2f\n", VERSION);
710         Con_Printf ("Exe: "__TIME__" "__DATE__"\n");
711 }
712
713 void Host_Say(qboolean teamonly)
714 {
715         client_t *client;
716         client_t *save;
717         int             j;
718         char    *p;
719         // LordHavoc: 256 char say messages
720         unsigned char   text[256];
721         qboolean        fromServer = false;
722
723         if (cmd_source == src_command)
724         {
725                 if (cls.state == ca_dedicated)
726                 {
727                         fromServer = true;
728                         teamonly = false;
729                 }
730                 else
731                 {
732                         Cmd_ForwardToServer ();
733                         return;
734                 }
735         }
736
737         if (Cmd_Argc () < 2)
738                 return;
739
740         save = host_client;
741
742         p = Cmd_Args();
743 // remove quotes if present
744         if (*p == '"')
745         {
746                 p++;
747                 p[strlen(p)-1] = 0;
748         }
749
750 // turn on color set 1
751         if (!fromServer)
752                 sprintf (text, "%c%s: ", 1, save->name);
753         else
754                 sprintf (text, "%c<%s> ", 1, hostname.string);
755
756         j = sizeof(text) - 2 - strlen(text);  // -2 for /n and null terminator
757         if (strlen(p) > j)
758                 p[j] = 0;
759
760         strcat (text, p);
761         strcat (text, "\n");
762
763         for (j = 0, client = svs.clients; j < svs.maxclients; j++, client++)
764         {
765                 if (!client || !client->active || !client->spawned)
766                         continue;
767                 if (teamplay.value && teamonly && client->edict->v.team != save->edict->v.team)
768                         continue;
769                 host_client = client;
770                 SV_ClientPrintf("%s", text);
771         }
772         host_client = save;
773
774         Sys_Printf("%s", &text[1]);
775 }
776
777
778 void Host_Say_f(void)
779 {
780         Host_Say(false);
781 }
782
783
784 void Host_Say_Team_f(void)
785 {
786         Host_Say(true);
787 }
788
789
790 void Host_Tell_f(void)
791 {
792         client_t *client;
793         client_t *save;
794         int             j;
795         char    *p;
796         char    text[64];
797
798         if (cmd_source == src_command)
799         {
800                 Cmd_ForwardToServer ();
801                 return;
802         }
803
804         if (Cmd_Argc () < 3)
805                 return;
806
807         strcpy(text, host_client->name);
808         strcat(text, ": ");
809
810         p = Cmd_Args();
811
812 // remove quotes if present
813         if (*p == '"')
814         {
815                 p++;
816                 p[strlen(p)-1] = 0;
817         }
818
819 // check length & truncate if necessary
820         j = sizeof(text) - 2 - strlen(text);  // -2 for /n and null terminator
821         if (strlen(p) > j)
822                 p[j] = 0;
823
824         strcat (text, p);
825         strcat (text, "\n");
826
827         save = host_client;
828         for (j = 0, client = svs.clients; j < svs.maxclients; j++, client++)
829         {
830                 if (!client->active || !client->spawned)
831                         continue;
832                 if (Q_strcasecmp(client->name, Cmd_Argv(1)))
833                         continue;
834                 host_client = client;
835                 SV_ClientPrintf("%s", text);
836                 break;
837         }
838         host_client = save;
839 }
840
841
842 /*
843 ==================
844 Host_Color_f
845 ==================
846 */
847 void Host_Color_f(void)
848 {
849         int             top, bottom;
850         int             playercolor;
851         
852         if (Cmd_Argc() == 1)
853         {
854                 Con_Printf ("\"color\" is \"%i %i\"\n", ((int)cl_color.value) >> 4, ((int)cl_color.value) & 0x0f);
855                 Con_Printf ("color <0-13> [0-13]\n");
856                 return;
857         }
858
859         if (Cmd_Argc() == 2)
860                 top = bottom = atoi(Cmd_Argv(1));
861         else
862         {
863                 top = atoi(Cmd_Argv(1));
864                 bottom = atoi(Cmd_Argv(2));
865         }
866         
867         top &= 15;
868         // LordHavoc: allow skin colormaps 14 and 15 (was 13)
869         if (top > 15)
870                 top = 15;
871         bottom &= 15;
872         // LordHavoc: allow skin colormaps 14 and 15 (was 13)
873         if (bottom > 15)
874                 bottom = 15;
875         
876         playercolor = top*16 + bottom;
877
878         if (cmd_source == src_command)
879         {
880                 Cvar_SetValue ("_cl_color", playercolor);
881                 if (cls.state == ca_connected)
882                         Cmd_ForwardToServer ();
883                 return;
884         }
885
886         host_client->colors = playercolor;
887         host_client->edict->v.team = bottom + 1;
888
889 // send notification to all clients
890         MSG_WriteByte (&sv.reliable_datagram, svc_updatecolors);
891         MSG_WriteByte (&sv.reliable_datagram, host_client - svs.clients);
892         MSG_WriteByte (&sv.reliable_datagram, host_client->colors);
893 }
894
895 /*
896 ==================
897 Host_Kill_f
898 ==================
899 */
900 void Host_Kill_f (void)
901 {
902         if (cmd_source == src_command)
903         {
904                 Cmd_ForwardToServer ();
905                 return;
906         }
907
908         if (sv_player->v.health <= 0)
909         {
910                 SV_ClientPrintf ("Can't suicide -- allready dead!\n");
911                 return;
912         }
913         
914         pr_global_struct->time = sv.time;
915         pr_global_struct->self = EDICT_TO_PROG(sv_player);
916         PR_ExecuteProgram (pr_global_struct->ClientKill);
917 }
918
919
920 /*
921 ==================
922 Host_Pause_f
923 ==================
924 */
925 void Host_Pause_f (void)
926 {
927         
928         if (cmd_source == src_command)
929         {
930                 Cmd_ForwardToServer ();
931                 return;
932         }
933         if (!pausable.value)
934                 SV_ClientPrintf ("Pause not allowed.\n");
935         else
936         {
937                 sv.paused ^= 1;
938
939                 if (sv.paused)
940                 {
941                         SV_BroadcastPrintf ("%s paused the game\n", pr_strings + sv_player->v.netname);
942                 }
943                 else
944                 {
945                         SV_BroadcastPrintf ("%s unpaused the game\n",pr_strings + sv_player->v.netname);
946                 }
947
948         // send notification to all clients
949                 MSG_WriteByte (&sv.reliable_datagram, svc_setpause);
950                 MSG_WriteByte (&sv.reliable_datagram, sv.paused);
951         }
952 }
953
954 //===========================================================================
955
956
957 /*
958 ==================
959 Host_PreSpawn_f
960 ==================
961 */
962 void Host_PreSpawn_f (void)
963 {
964         if (cmd_source == src_command)
965         {
966                 Con_Printf ("prespawn is not valid from the console\n");
967                 return;
968         }
969
970         if (host_client->spawned)
971         {
972                 Con_Printf ("prespawn not valid -- allready spawned\n");
973                 return;
974         }
975         
976         SZ_Write (&host_client->message, sv.signon.data, sv.signon.cursize);
977         MSG_WriteByte (&host_client->message, svc_signonnum);
978         MSG_WriteByte (&host_client->message, 2);
979         host_client->sendsignon = true;
980 }
981
982 dfunction_t *ED_FindFunction (char *name);
983
984 /*
985 ==================
986 Host_Spawn_f
987 ==================
988 */
989 void Host_Spawn_f (void)
990 {
991         int             i;
992         client_t        *client;
993         edict_t *ent;
994         func_t RestoreGame;
995         dfunction_t *f;
996
997         if (cmd_source == src_command)
998         {
999                 Con_Printf ("spawn is not valid from the console\n");
1000                 return;
1001         }
1002
1003         if (host_client->spawned)
1004         {
1005                 Con_Printf ("Spawn not valid -- allready spawned\n");
1006                 return;
1007         }
1008
1009 // run the entrance script
1010         if (sv.loadgame)
1011         {       // loaded games are fully inited allready
1012                 // if this is the last client to be connected, unpause
1013                 sv.paused = false;
1014
1015                 if ((f = ED_FindFunction ("RestoreGame")))
1016                 if ((RestoreGame = (func_t)(f - pr_functions)))
1017                 {
1018                         Con_DPrintf("Calling RestoreGame\n");
1019                         pr_global_struct->time = sv.time;
1020                         pr_global_struct->self = EDICT_TO_PROG(sv_player);
1021                         PR_ExecuteProgram (RestoreGame);
1022                 }
1023         }
1024         else
1025         {
1026                 eval_t *val;
1027                 // set up the edict
1028                 ent = host_client->edict;
1029
1030                 memset (&ent->v, 0, progs->entityfields * 4);
1031                 ent->v.colormap = NUM_FOR_EDICT(ent);
1032                 ent->v.team = (host_client->colors & 15) + 1;
1033                 ent->v.netname = host_client->name - pr_strings;
1034                 if ((val = GETEDICTFIELDVALUE(host_client->edict, eval_pmodel)))
1035                         val->_float = host_client->pmodel;
1036
1037                 // copy spawn parms out of the client_t
1038
1039                 for (i=0 ; i< NUM_SPAWN_PARMS ; i++)
1040                         (&pr_global_struct->parm1)[i] = host_client->spawn_parms[i];
1041
1042                 // call the spawn function
1043
1044                 pr_global_struct->time = sv.time;
1045                 pr_global_struct->self = EDICT_TO_PROG(sv_player);
1046                 PR_ExecuteProgram (pr_global_struct->ClientConnect);
1047
1048                 if ((Sys_FloatTime() - host_client->netconnection->connecttime) <= sv.time)
1049                         Sys_Printf ("%s entered the game\n", host_client->name);
1050
1051                 PR_ExecuteProgram (pr_global_struct->PutClientInServer);        
1052         }
1053
1054
1055 // send all current names, colors, and frag counts
1056         SZ_Clear (&host_client->message);
1057
1058 // send time of update
1059         MSG_WriteByte (&host_client->message, svc_time);
1060         MSG_WriteFloat (&host_client->message, sv.time);
1061
1062         for (i=0, client = svs.clients ; i<svs.maxclients ; i++, client++)
1063         {
1064                 MSG_WriteByte (&host_client->message, svc_updatename);
1065                 MSG_WriteByte (&host_client->message, i);
1066                 MSG_WriteString (&host_client->message, client->name);
1067                 MSG_WriteByte (&host_client->message, svc_updatefrags);
1068                 MSG_WriteByte (&host_client->message, i);
1069                 MSG_WriteShort (&host_client->message, client->old_frags);
1070                 MSG_WriteByte (&host_client->message, svc_updatecolors);
1071                 MSG_WriteByte (&host_client->message, i);
1072                 MSG_WriteByte (&host_client->message, client->colors);
1073         }
1074         
1075 // send all current light styles
1076         for (i=0 ; i<MAX_LIGHTSTYLES ; i++)
1077         {
1078                 MSG_WriteByte (&host_client->message, svc_lightstyle);
1079                 MSG_WriteByte (&host_client->message, (char)i);
1080                 MSG_WriteString (&host_client->message, sv.lightstyles[i]);
1081         }
1082
1083 //
1084 // send some stats
1085 //
1086         MSG_WriteByte (&host_client->message, svc_updatestat);
1087         MSG_WriteByte (&host_client->message, STAT_TOTALSECRETS);
1088         MSG_WriteLong (&host_client->message, pr_global_struct->total_secrets);
1089
1090         MSG_WriteByte (&host_client->message, svc_updatestat);
1091         MSG_WriteByte (&host_client->message, STAT_TOTALMONSTERS);
1092         MSG_WriteLong (&host_client->message, pr_global_struct->total_monsters);
1093
1094         MSG_WriteByte (&host_client->message, svc_updatestat);
1095         MSG_WriteByte (&host_client->message, STAT_SECRETS);
1096         MSG_WriteLong (&host_client->message, pr_global_struct->found_secrets);
1097
1098         MSG_WriteByte (&host_client->message, svc_updatestat);
1099         MSG_WriteByte (&host_client->message, STAT_MONSTERS);
1100         MSG_WriteLong (&host_client->message, pr_global_struct->killed_monsters);
1101
1102 //
1103 // send a fixangle
1104 // Never send a roll angle, because savegames can catch the server
1105 // in a state where it is expecting the client to correct the angle
1106 // and it won't happen if the game was just loaded, so you wind up
1107 // with a permanent head tilt
1108         ent = EDICT_NUM( 1 + (host_client - svs.clients) );
1109         MSG_WriteByte (&host_client->message, svc_setangle);
1110         for (i=0 ; i < 2 ; i++)
1111                 MSG_WriteAngle (&host_client->message, ent->v.angles[i] );
1112         MSG_WriteAngle (&host_client->message, 0 );
1113
1114         SV_WriteClientdataToMessage (sv_player, &host_client->message);
1115
1116         MSG_WriteByte (&host_client->message, svc_signonnum);
1117         MSG_WriteByte (&host_client->message, 3);
1118         host_client->sendsignon = true;
1119 }
1120
1121 /*
1122 ==================
1123 Host_Begin_f
1124 ==================
1125 */
1126 void Host_Begin_f (void)
1127 {
1128         if (cmd_source == src_command)
1129         {
1130                 Con_Printf ("begin is not valid from the console\n");
1131                 return;
1132         }
1133
1134         host_client->spawned = true;
1135 }
1136
1137 //===========================================================================
1138
1139
1140 /*
1141 ==================
1142 Host_Kick_f
1143
1144 Kicks a user off of the server
1145 ==================
1146 */
1147 void Host_Kick_f (void)
1148 {
1149         char            *who;
1150         char            *message = NULL;
1151         client_t        *save;
1152         int                     i;
1153         qboolean        byNumber = false;
1154
1155         if (cmd_source == src_command)
1156         {
1157                 if (!sv.active)
1158                 {
1159                         Cmd_ForwardToServer ();
1160                         return;
1161                 }
1162         }
1163         else if (pr_global_struct->deathmatch)
1164                 return;
1165
1166         save = host_client;
1167
1168         if (Cmd_Argc() > 2 && strcmp(Cmd_Argv(1), "#") == 0)
1169         {
1170                 i = atof(Cmd_Argv(2)) - 1;
1171                 if (i < 0 || i >= svs.maxclients)
1172                         return;
1173                 if (!svs.clients[i].active)
1174                         return;
1175                 host_client = &svs.clients[i];
1176                 byNumber = true;
1177         }
1178         else
1179         {
1180                 for (i = 0, host_client = svs.clients; i < svs.maxclients; i++, host_client++)
1181                 {
1182                         if (!host_client->active)
1183                                 continue;
1184                         if (Q_strcasecmp(host_client->name, Cmd_Argv(1)) == 0)
1185                                 break;
1186                 }
1187         }
1188
1189         if (i < svs.maxclients)
1190         {
1191                 if (cmd_source == src_command)
1192                         if (cls.state == ca_dedicated)
1193                                 who = "Console";
1194                         else
1195                                 who = cl_name.string;
1196                 else
1197                         who = save->name;
1198
1199                 // can't kick yourself!
1200                 if (host_client == save)
1201                         return;
1202
1203                 if (Cmd_Argc() > 2)
1204                 {
1205                         message = COM_Parse(Cmd_Args());
1206                         if (byNumber)
1207                         {
1208                                 message++;                                                      // skip the #
1209                                 while (*message == ' ')                         // skip white space
1210                                         message++;
1211                                 message += strlen(Cmd_Argv(2)); // skip the number
1212                         }
1213                         while (*message && *message == ' ')
1214                                 message++;
1215                 }
1216                 if (message)
1217                         SV_ClientPrintf ("Kicked by %s: %s\n", who, message);
1218                 else
1219                         SV_ClientPrintf ("Kicked by %s\n", who);
1220                 SV_DropClient (false);
1221         }
1222
1223         host_client = save;
1224 }
1225
1226 /*
1227 ===============================================================================
1228
1229 DEBUGGING TOOLS
1230
1231 ===============================================================================
1232 */
1233
1234 /*
1235 ==================
1236 Host_Give_f
1237 ==================
1238 */
1239 void Host_Give_f (void)
1240 {
1241         char    *t;
1242         int             v;
1243         eval_t  *val;
1244
1245         if (cmd_source == src_command)
1246         {
1247                 Cmd_ForwardToServer ();
1248                 return;
1249         }
1250
1251         if (pr_global_struct->deathmatch)
1252                 return;
1253
1254         t = Cmd_Argv(1);
1255         v = atoi (Cmd_Argv(2));
1256         
1257         switch (t[0])
1258         {
1259    case '0':
1260    case '1':
1261    case '2':
1262    case '3':
1263    case '4':
1264    case '5':
1265    case '6':
1266    case '7':
1267    case '8':
1268    case '9':
1269       // MED 01/04/97 added hipnotic give stuff
1270       if (hipnotic)
1271       {
1272          if (t[0] == '6')
1273          {
1274             if (t[1] == 'a')
1275                sv_player->v.items = (int)sv_player->v.items | HIT_PROXIMITY_GUN;
1276             else
1277                sv_player->v.items = (int)sv_player->v.items | IT_GRENADE_LAUNCHER;
1278          }
1279          else if (t[0] == '9')
1280             sv_player->v.items = (int)sv_player->v.items | HIT_LASER_CANNON;
1281          else if (t[0] == '0')
1282             sv_player->v.items = (int)sv_player->v.items | HIT_MJOLNIR;
1283          else if (t[0] >= '2')
1284             sv_player->v.items = (int)sv_player->v.items | (IT_SHOTGUN << (t[0] - '2'));
1285       }
1286       else
1287       {
1288          if (t[0] >= '2')
1289             sv_player->v.items = (int)sv_player->v.items | (IT_SHOTGUN << (t[0] - '2'));
1290       }
1291                 break;
1292         
1293     case 's':
1294                 if (rogue)
1295                 {
1296                     if ((val = GETEDICTFIELDVALUE(sv_player, eval_ammo_shells1)))
1297                             val->_float = v;
1298                 }
1299
1300         sv_player->v.ammo_shells = v;
1301         break;          
1302     case 'n':
1303                 if (rogue)
1304                 {
1305                     if ((val = GETEDICTFIELDVALUE(sv_player, eval_ammo_nails1)))
1306                         {
1307                             val->_float = v;
1308                                 if (sv_player->v.weapon <= IT_LIGHTNING)
1309                                         sv_player->v.ammo_nails = v;
1310                         }
1311                 }
1312                 else
1313                 {
1314                         sv_player->v.ammo_nails = v;
1315                 }
1316         break;          
1317     case 'l':
1318                 if (rogue)
1319                 {
1320                         val = GETEDICTFIELDVALUE(sv_player, eval_ammo_lava_nails);
1321                         if (val)
1322                         {
1323                                 val->_float = v;
1324                                 if (sv_player->v.weapon > IT_LIGHTNING)
1325                                         sv_player->v.ammo_nails = v;
1326                         }
1327                 }
1328         break;
1329     case 'r':
1330                 if (rogue)
1331                 {
1332                         val = GETEDICTFIELDVALUE(sv_player, eval_ammo_rockets1);
1333                         if (val)
1334                         {
1335                                 val->_float = v;
1336                                 if (sv_player->v.weapon <= IT_LIGHTNING)
1337                                         sv_player->v.ammo_rockets = v;
1338                         }
1339                 }
1340                 else
1341                 {
1342                         sv_player->v.ammo_rockets = v;
1343                 }
1344         break;          
1345     case 'm':
1346                 if (rogue)
1347                 {
1348                         val = GETEDICTFIELDVALUE(sv_player, eval_ammo_multi_rockets);
1349                         if (val)
1350                         {
1351                                 val->_float = v;
1352                                 if (sv_player->v.weapon > IT_LIGHTNING)
1353                                         sv_player->v.ammo_rockets = v;
1354                         }
1355                 }
1356         break;          
1357     case 'h':
1358         sv_player->v.health = v;
1359         break;          
1360     case 'c':
1361                 if (rogue)
1362                 {
1363                         val = GETEDICTFIELDVALUE(sv_player, eval_ammo_cells1);
1364                         if (val)
1365                         {
1366                                 val->_float = v;
1367                                 if (sv_player->v.weapon <= IT_LIGHTNING)
1368                                         sv_player->v.ammo_cells = v;
1369                         }
1370                 }
1371                 else
1372                 {
1373                         sv_player->v.ammo_cells = v;
1374                 }
1375         break;          
1376     case 'p':
1377                 if (rogue)
1378                 {
1379                         val = GETEDICTFIELDVALUE(sv_player, eval_ammo_plasma);
1380                         if (val)
1381                         {
1382                                 val->_float = v;
1383                                 if (sv_player->v.weapon > IT_LIGHTNING)
1384                                         sv_player->v.ammo_cells = v;
1385                         }
1386                 }
1387         break;          
1388     }
1389 }
1390
1391 edict_t *FindViewthing (void)
1392 {
1393         int             i;
1394         edict_t *e;
1395         
1396         for (i=0 ; i<sv.num_edicts ; i++)
1397         {
1398                 e = EDICT_NUM(i);
1399                 if ( !strcmp (pr_strings + e->v.classname, "viewthing") )
1400                         return e;
1401         }
1402         Con_Printf ("No viewthing on map\n");
1403         return NULL;
1404 }
1405
1406 /*
1407 ==================
1408 Host_Viewmodel_f
1409 ==================
1410 */
1411 void Host_Viewmodel_f (void)
1412 {
1413         edict_t *e;
1414         model_t *m;
1415
1416         e = FindViewthing ();
1417         if (!e)
1418                 return;
1419
1420         m = Mod_ForName (Cmd_Argv(1), false);
1421         if (!m)
1422         {
1423                 Con_Printf ("Can't load %s\n", Cmd_Argv(1));
1424                 return;
1425         }
1426         
1427         e->v.frame = 0;
1428         cl.model_precache[(int)e->v.modelindex] = m;
1429 }
1430
1431 /*
1432 ==================
1433 Host_Viewframe_f
1434 ==================
1435 */
1436 void Host_Viewframe_f (void)
1437 {
1438         edict_t *e;
1439         int             f;
1440         model_t *m;
1441
1442         e = FindViewthing ();
1443         if (!e)
1444                 return;
1445         m = cl.model_precache[(int)e->v.modelindex];
1446
1447         f = atoi(Cmd_Argv(1));
1448         if (f >= m->numframes)
1449                 f = m->numframes-1;
1450
1451         e->v.frame = f;         
1452 }
1453
1454
1455 void PrintFrameName (model_t *m, int frame)
1456 {
1457         aliashdr_t                      *hdr;
1458         maliasframedesc_t       *pframedesc;
1459
1460         hdr = (aliashdr_t *)Mod_Extradata (m);
1461         if (!hdr)
1462                 return;
1463         pframedesc = &hdr->frames[frame];
1464         
1465         Con_Printf ("frame %i: %s\n", frame, pframedesc->name);
1466 }
1467
1468 /*
1469 ==================
1470 Host_Viewnext_f
1471 ==================
1472 */
1473 void Host_Viewnext_f (void)
1474 {
1475         edict_t *e;
1476         model_t *m;
1477         
1478         e = FindViewthing ();
1479         if (!e)
1480                 return;
1481         m = cl.model_precache[(int)e->v.modelindex];
1482
1483         e->v.frame = e->v.frame + 1;
1484         if (e->v.frame >= m->numframes)
1485                 e->v.frame = m->numframes - 1;
1486
1487         PrintFrameName (m, e->v.frame);         
1488 }
1489
1490 /*
1491 ==================
1492 Host_Viewprev_f
1493 ==================
1494 */
1495 void Host_Viewprev_f (void)
1496 {
1497         edict_t *e;
1498         model_t *m;
1499
1500         e = FindViewthing ();
1501         if (!e)
1502                 return;
1503
1504         m = cl.model_precache[(int)e->v.modelindex];
1505
1506         e->v.frame = e->v.frame - 1;
1507         if (e->v.frame < 0)
1508                 e->v.frame = 0;
1509
1510         PrintFrameName (m, e->v.frame);         
1511 }
1512
1513 /*
1514 ===============================================================================
1515
1516 DEMO LOOP CONTROL
1517
1518 ===============================================================================
1519 */
1520
1521
1522 /*
1523 ==================
1524 Host_Startdemos_f
1525 ==================
1526 */
1527 void Host_Startdemos_f (void)
1528 {
1529         int             i, c;
1530
1531         if (cls.state == ca_dedicated)
1532         {
1533                 if (!sv.active)
1534                         Cbuf_AddText ("map start\n");
1535                 return;
1536         }
1537
1538         c = Cmd_Argc() - 1;
1539         if (c > MAX_DEMOS)
1540         {
1541                 Con_Printf ("Max %i demos in demoloop\n", MAX_DEMOS);
1542                 c = MAX_DEMOS;
1543         }
1544         Con_Printf ("%i demo(s) in loop\n", c);
1545
1546         for (i=1 ; i<c+1 ; i++)
1547                 strncpy (cls.demos[i-1], Cmd_Argv(i), sizeof(cls.demos[0])-1);
1548
1549         if (!sv.active && cls.demonum != -1 && !cls.demoplayback)
1550         {
1551                 cls.demonum = 0;
1552                 CL_NextDemo ();
1553         }
1554         else
1555                 cls.demonum = -1;
1556 }
1557
1558
1559 /*
1560 ==================
1561 Host_Demos_f
1562
1563 Return to looping demos
1564 ==================
1565 */
1566 void Host_Demos_f (void)
1567 {
1568         if (cls.state == ca_dedicated)
1569                 return;
1570         if (cls.demonum == -1)
1571                 cls.demonum = 1;
1572         CL_Disconnect_f ();
1573         CL_NextDemo ();
1574 }
1575
1576 /*
1577 ==================
1578 Host_Stopdemo_f
1579
1580 Return to looping demos
1581 ==================
1582 */
1583 void Host_Stopdemo_f (void)
1584 {
1585         if (cls.state == ca_dedicated)
1586                 return;
1587         if (!cls.demoplayback)
1588                 return;
1589         CL_StopPlayback ();
1590         CL_Disconnect ();
1591 }
1592
1593 //=============================================================================
1594
1595 /*
1596 ==================
1597 Host_InitCommands
1598 ==================
1599 */
1600 void Host_InitCommands (void)
1601 {
1602         Cmd_AddCommand ("status", Host_Status_f);
1603         Cmd_AddCommand ("quit", Host_Quit_f);
1604         if (nehahra)
1605         {
1606                 Cmd_AddCommand ("max", Host_God_f);
1607                 Cmd_AddCommand ("monster", Host_Notarget_f);
1608                 Cmd_AddCommand ("scrag", Host_Fly_f);
1609                 Cmd_AddCommand ("wraith", Host_Noclip_f);
1610                 Cmd_AddCommand ("gimme", Host_Give_f);
1611         }
1612         else
1613         {
1614                 Cmd_AddCommand ("god", Host_God_f);
1615                 Cmd_AddCommand ("notarget", Host_Notarget_f);
1616                 Cmd_AddCommand ("fly", Host_Fly_f);
1617                 Cmd_AddCommand ("noclip", Host_Noclip_f);
1618                 Cmd_AddCommand ("give", Host_Give_f);
1619         }
1620         Cmd_AddCommand ("map", Host_Map_f);
1621         Cmd_AddCommand ("restart", Host_Restart_f);
1622         Cmd_AddCommand ("changelevel", Host_Changelevel_f);
1623         Cmd_AddCommand ("connect", Host_Connect_f);
1624         Cmd_AddCommand ("reconnect", Host_Reconnect_f);
1625         Cmd_AddCommand ("name", Host_Name_f);
1626         Cmd_AddCommand ("version", Host_Version_f);
1627         Cmd_AddCommand ("say", Host_Say_f);
1628         Cmd_AddCommand ("say_team", Host_Say_Team_f);
1629         Cmd_AddCommand ("tell", Host_Tell_f);
1630         Cmd_AddCommand ("color", Host_Color_f);
1631         Cmd_AddCommand ("kill", Host_Kill_f);
1632         Cmd_AddCommand ("pause", Host_Pause_f);
1633         Cmd_AddCommand ("spawn", Host_Spawn_f);
1634         Cmd_AddCommand ("begin", Host_Begin_f);
1635         Cmd_AddCommand ("prespawn", Host_PreSpawn_f);
1636         Cmd_AddCommand ("kick", Host_Kick_f);
1637         Cmd_AddCommand ("ping", Host_Ping_f);
1638         Cmd_AddCommand ("load", Host_Loadgame_f);
1639         Cmd_AddCommand ("save", Host_Savegame_f);
1640
1641         Cmd_AddCommand ("startdemos", Host_Startdemos_f);
1642         Cmd_AddCommand ("demos", Host_Demos_f);
1643         Cmd_AddCommand ("stopdemo", Host_Stopdemo_f);
1644
1645         Cmd_AddCommand ("viewmodel", Host_Viewmodel_f);
1646         Cmd_AddCommand ("viewframe", Host_Viewframe_f);
1647         Cmd_AddCommand ("viewnext", Host_Viewnext_f);
1648         Cmd_AddCommand ("viewprev", Host_Viewprev_f);
1649
1650         Cmd_AddCommand ("mcache", Mod_Print);
1651 }