]> icculus.org git repositories - divverent/darkplaces.git/blob - host.c
I think that this really should be in here
[divverent/darkplaces.git] / host.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 // host.c -- coordinates spawning and killing of local servers
21
22 #include "quakedef.h"
23
24 /*
25
26 A server can allways be started, even if the system started out as a client
27 to a remote system.
28
29 A client can NOT be started if the system started as a dedicated server.
30
31 Memory is cleared / released when a server or client begins, not when they end.
32
33 */
34
35 quakeparms_t host_parms;
36
37 qboolean        host_initialized;               // true if into command execution
38
39 double          host_frametime;
40 double          host_time;
41 double          realtime;                               // without any filtering or bounding
42 double          oldrealtime;                    // last frame run
43 int                     host_framecount;
44
45 int                     host_hunklevel;
46
47 int                     minimum_memory;
48
49 client_t        *host_client;                   // current client
50
51 jmp_buf         host_abortserver;
52
53 byte            *host_basepal;
54 //byte          *host_colormap;
55
56 cvar_t  host_framerate = {"host_framerate","0"};        // set for slow motion
57 cvar_t  host_speeds = {"host_speeds","0"};                      // set for running times
58 cvar_t  slowmo = {"slowmo", "1.0"};                                     // LordHavoc: framerate independent slowmo
59
60 cvar_t  sys_ticrate = {"sys_ticrate","0.05"};
61 cvar_t  serverprofile = {"serverprofile","0"};
62
63 cvar_t  fraglimit = {"fraglimit","0",false,true};
64 cvar_t  timelimit = {"timelimit","0",false,true};
65 cvar_t  teamplay = {"teamplay","0",false,true};
66
67 cvar_t  samelevel = {"samelevel","0"};
68 cvar_t  noexit = {"noexit","0",false,true};
69
70 cvar_t  developer = {"developer","0"};
71
72 cvar_t  skill = {"skill","1"};                                          // 0 - 3
73 cvar_t  deathmatch = {"deathmatch","0"};                        // 0, 1, or 2
74 cvar_t  coop = {"coop","0"};                    // 0 or 1
75
76 cvar_t  pausable = {"pausable","1"};
77
78 cvar_t  temp1 = {"temp1","0"};
79
80 cvar_t  timestamps = {"timestamps", "0", true};
81 cvar_t  timeformat = {"timeformat", "[%b %e %X] ", true};
82
83 /*
84 ================
85 Host_EndGame
86 ================
87 */
88 void Host_EndGame (char *message, ...)
89 {
90         va_list         argptr;
91         char            string[1024];
92         
93         va_start (argptr,message);
94         vsprintf (string,message,argptr);
95         va_end (argptr);
96         Con_DPrintf ("Host_EndGame: %s\n",string);
97         
98         if (sv.active)
99                 Host_ShutdownServer (false);
100
101         if (cls.state == ca_dedicated)
102                 Sys_Error ("Host_EndGame: %s\n",string);        // dedicated servers exit
103         
104         if (cls.demonum != -1)
105                 CL_NextDemo ();
106         else
107                 CL_Disconnect ();
108
109         longjmp (host_abortserver, 1);
110 }
111
112 /*
113 ================
114 Host_Error
115
116 This shuts down both the client and server
117 ================
118 */
119 void Host_Error (char *error, ...)
120 {
121         va_list         argptr;
122         char            string[1024];
123         static  qboolean inerror = false;
124         
125         if (inerror)
126                 Sys_Error ("Host_Error: recursively entered");
127         inerror = true;
128         
129         SCR_EndLoadingPlaque ();                // reenable screen updates
130
131         va_start (argptr,error);
132         vsprintf (string,error,argptr);
133         va_end (argptr);
134         Con_Printf ("Host_Error: %s\n",string);
135         
136         if (sv.active)
137                 Host_ShutdownServer (false);
138
139         if (cls.state == ca_dedicated)
140                 Sys_Error ("Host_Error: %s\n",string);  // dedicated servers exit
141
142         CL_Disconnect ();
143         cls.demonum = -1;
144
145         inerror = false;
146
147         longjmp (host_abortserver, 1);
148 }
149
150 /*
151 ================
152 Host_FindMaxClients
153 ================
154 */
155 void    Host_FindMaxClients (void)
156 {
157         int             i;
158
159         svs.maxclients = 1;
160                 
161         i = COM_CheckParm ("-dedicated");
162         if (i)
163         {
164                 cls.state = ca_dedicated;
165                 if (i != (com_argc - 1))
166                 {
167                         svs.maxclients = atoi (com_argv[i+1]);
168                 }
169                 else
170                         svs.maxclients = 8;
171         }
172         else
173                 cls.state = ca_disconnected;
174
175         i = COM_CheckParm ("-listen");
176         if (i)
177         {
178                 if (cls.state == ca_dedicated)
179                         Sys_Error ("Only one of -dedicated or -listen can be specified");
180                 if (i != (com_argc - 1))
181                         svs.maxclients = atoi (com_argv[i+1]);
182                 else
183                         svs.maxclients = 8;
184         }
185         if (svs.maxclients < 1)
186                 svs.maxclients = 8;
187         else if (svs.maxclients > MAX_SCOREBOARD)
188                 svs.maxclients = MAX_SCOREBOARD;
189
190         svs.maxclientslimit = svs.maxclients;
191         if (svs.maxclientslimit < MAX_SCOREBOARD) // LordHavoc: upped listen mode limit from 4 to MAX_SCOREBOARD
192                 svs.maxclientslimit = MAX_SCOREBOARD;
193         svs.clients = Hunk_AllocName (svs.maxclientslimit*sizeof(client_t), "clients");
194
195         if (svs.maxclients > 1)
196                 Cvar_SetValue ("deathmatch", 1.0);
197         else
198                 Cvar_SetValue ("deathmatch", 0.0);
199 }
200
201
202 /*
203 =======================
204 Host_InitLocal
205 ======================
206 */
207 void Host_InitLocal (void)
208 {
209         Host_InitCommands ();
210         
211         Cvar_RegisterVariable (&host_framerate);
212         Cvar_RegisterVariable (&host_speeds);
213         Cvar_RegisterVariable (&slowmo);
214
215         Cvar_RegisterVariable (&sys_ticrate);
216         Cvar_RegisterVariable (&serverprofile);
217
218         Cvar_RegisterVariable (&fraglimit);
219         Cvar_RegisterVariable (&timelimit);
220         Cvar_RegisterVariable (&teamplay);
221         Cvar_RegisterVariable (&samelevel);
222         Cvar_RegisterVariable (&noexit);
223         Cvar_RegisterVariable (&skill);
224         Cvar_RegisterVariable (&developer);
225         Cvar_RegisterVariable (&deathmatch);
226         Cvar_RegisterVariable (&coop);
227
228         Cvar_RegisterVariable (&pausable);
229
230         Cvar_RegisterVariable (&temp1);
231
232         Cvar_RegisterVariable (&timestamps);
233         Cvar_RegisterVariable (&timeformat);
234
235         Host_FindMaxClients ();
236         
237         host_time = 1.0;                // so a think at time 0 won't get called
238 }
239
240
241 /*
242 ===============
243 Host_WriteConfiguration
244
245 Writes key bindings and archived cvars to config.cfg
246 ===============
247 */
248 void Host_WriteConfiguration (void)
249 {
250         FILE    *f;
251
252 // dedicated servers initialize the host but don't parse and set the
253 // config.cfg cvars
254         if (host_initialized & !isDedicated)
255         {
256                 f = fopen (va("%s/config.cfg",com_gamedir), "w");
257                 if (!f)
258                 {
259                         Con_Printf ("Couldn't write config.cfg.\n");
260                         return;
261                 }
262                 
263                 Key_WriteBindings (f);
264                 Cvar_WriteVariables (f);
265
266                 fclose (f);
267         }
268 }
269
270
271 /*
272 =================
273 SV_ClientPrintf
274
275 Sends text across to be displayed 
276 FIXME: make this just a stuffed echo?
277 =================
278 */
279 void SV_ClientPrintf (char *fmt, ...)
280 {
281         va_list         argptr;
282         char            string[1024];
283         
284         va_start (argptr,fmt);
285         vsprintf (string, fmt,argptr);
286         va_end (argptr);
287         
288         MSG_WriteByte (&host_client->message, svc_print);
289         MSG_WriteString (&host_client->message, string);
290 }
291
292 /*
293 =================
294 SV_BroadcastPrintf
295
296 Sends text to all active clients
297 =================
298 */
299 void SV_BroadcastPrintf (char *fmt, ...)
300 {
301         va_list         argptr;
302         char            string[1024];
303         int                     i;
304         
305         va_start (argptr,fmt);
306         vsprintf (string, fmt,argptr);
307         va_end (argptr);
308         
309         for (i=0 ; i<svs.maxclients ; i++)
310                 if (svs.clients[i].active && svs.clients[i].spawned)
311                 {
312                         MSG_WriteByte (&svs.clients[i].message, svc_print);
313                         MSG_WriteString (&svs.clients[i].message, string);
314                 }
315 }
316
317 /*
318 =================
319 Host_ClientCommands
320
321 Send text over to the client to be executed
322 =================
323 */
324 void Host_ClientCommands (char *fmt, ...)
325 {
326         va_list         argptr;
327         char            string[1024];
328         
329         va_start (argptr,fmt);
330         vsprintf (string, fmt,argptr);
331         va_end (argptr);
332         
333         MSG_WriteByte (&host_client->message, svc_stufftext);
334         MSG_WriteString (&host_client->message, string);
335 }
336
337 /*
338 =====================
339 SV_DropClient
340
341 Called when the player is getting totally kicked off the host
342 if (crash = true), don't bother sending signofs
343 =====================
344 */
345 void SV_DropClient (qboolean crash)
346 {
347         int             saveSelf;
348         int             i;
349         client_t *client;
350
351         if (!crash)
352         {
353                 // send any final messages (don't check for errors)
354                 if (NET_CanSendMessage (host_client->netconnection))
355                 {
356                         MSG_WriteByte (&host_client->message, svc_disconnect);
357                         NET_SendMessage (host_client->netconnection, &host_client->message);
358                 }
359         
360                 if (host_client->edict && host_client->spawned)
361                 {
362                 // call the prog function for removing a client
363                 // this will set the body to a dead frame, among other things
364                         saveSelf = pr_global_struct->self;
365                         pr_global_struct->self = EDICT_TO_PROG(host_client->edict);
366                         PR_ExecuteProgram (pr_global_struct->ClientDisconnect);
367                         pr_global_struct->self = saveSelf;
368                 }
369
370                 Sys_Printf ("Client %s removed\n",host_client->name);
371         }
372
373 // break the net connection
374         NET_Close (host_client->netconnection);
375         host_client->netconnection = NULL;
376
377 // free the client (the body stays around)
378         host_client->active = false;
379         host_client->name[0] = 0;
380         host_client->old_frags = -999999;
381         net_activeconnections--;
382
383 // send notification to all clients
384         for (i=0, client = svs.clients ; i<svs.maxclients ; i++, client++)
385         {
386                 if (!client->active)
387                         continue;
388                 MSG_WriteByte (&client->message, svc_updatename);
389                 MSG_WriteByte (&client->message, host_client - svs.clients);
390                 MSG_WriteString (&client->message, "");
391                 MSG_WriteByte (&client->message, svc_updatefrags);
392                 MSG_WriteByte (&client->message, host_client - svs.clients);
393                 MSG_WriteShort (&client->message, 0);
394                 MSG_WriteByte (&client->message, svc_updatecolors);
395                 MSG_WriteByte (&client->message, host_client - svs.clients);
396                 MSG_WriteByte (&client->message, 0);
397         }
398 }
399
400 /*
401 ==================
402 Host_ShutdownServer
403
404 This only happens at the end of a game, not between levels
405 ==================
406 */
407 void Host_ShutdownServer(qboolean crash)
408 {
409         int             i;
410         int             count;
411         sizebuf_t       buf;
412         char            message[4];
413         double  start;
414
415         if (!sv.active)
416                 return;
417
418         sv.active = false;
419
420 // stop all client sounds immediately
421         if (cls.state == ca_connected)
422                 CL_Disconnect ();
423
424 // flush any pending messages - like the score!!!
425         start = Sys_FloatTime();
426         do
427         {
428                 count = 0;
429                 for (i=0, host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
430                 {
431                         if (host_client->active && host_client->message.cursize)
432                         {
433                                 if (NET_CanSendMessage (host_client->netconnection))
434                                 {
435                                         NET_SendMessage(host_client->netconnection, &host_client->message);
436                                         SZ_Clear (&host_client->message);
437                                 }
438                                 else
439                                 {
440                                         NET_GetMessage(host_client->netconnection);
441                                         count++;
442                                 }
443                         }
444                 }
445                 if ((Sys_FloatTime() - start) > 3.0)
446                         break;
447         }
448         while (count);
449
450 // make sure all the clients know we're disconnecting
451         buf.data = message;
452         buf.maxsize = 4;
453         buf.cursize = 0;
454         MSG_WriteByte(&buf, svc_disconnect);
455         count = NET_SendToAll(&buf, 5);
456         if (count)
457                 Con_Printf("Host_ShutdownServer: NET_SendToAll failed for %u clients\n", count);
458
459         for (i=0, host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
460                 if (host_client->active)
461                         SV_DropClient(crash);
462
463 //
464 // clear structures
465 //
466         memset (&sv, 0, sizeof(sv));
467         memset (svs.clients, 0, svs.maxclientslimit*sizeof(client_t));
468 }
469
470
471 /*
472 ================
473 Host_ClearMemory
474
475 This clears all the memory used by both the client and server, but does
476 not reinitialize anything.
477 ================
478 */
479 void Host_ClearMemory (void)
480 {
481         Con_DPrintf ("Clearing memory\n");
482         D_FlushCaches ();
483         Mod_ClearAll ();
484         if (host_hunklevel)
485                 Hunk_FreeToLowMark (host_hunklevel);
486
487         cls.signon = 0;
488         memset (&sv, 0, sizeof(sv));
489         memset (&cl, 0, sizeof(cl));
490 }
491
492
493 //============================================================================
494
495 /*
496 ===================
497 Host_FilterTime
498
499 Returns false if the time is too short to run a frame
500 ===================
501 */
502 qboolean Host_FilterTime (float time)
503 {
504         realtime += time;
505
506 //      if (!cls.timedemo && realtime - oldrealtime < (1.0 / 72.0))
507 //              return false;           // framerate is too high
508
509         host_frametime = (realtime - oldrealtime) * slowmo.value; // LordHavoc: slowmo cvar
510         oldrealtime = realtime;
511
512         if (host_framerate.value > 0)
513                 host_frametime = host_framerate.value;
514         else
515         {       // don't allow really long or short frames
516                 if (host_frametime > 0.1)
517                         host_frametime = 0.1;
518                 if (host_frametime < 0.001)
519                         host_frametime = 0.001;
520         }
521         
522         return true;
523 }
524
525
526 /*
527 ===================
528 Host_GetConsoleCommands
529
530 Add them exactly as if they had been typed at the console
531 ===================
532 */
533 void Host_GetConsoleCommands (void)
534 {
535         char    *cmd;
536
537         while (1)
538         {
539                 cmd = Sys_ConsoleInput ();
540                 if (!cmd)
541                         break;
542                 Cbuf_AddText (cmd);
543         }
544 }
545
546
547 /*
548 ==================
549 Host_ServerFrame
550
551 ==================
552 */
553 #ifdef FPS_20
554
555 void _Host_ServerFrame (void)
556 {
557 // run the world state  
558         pr_global_struct->frametime = host_frametime;
559
560 // read client messages
561         SV_RunClients ();
562         
563 // move things around and think
564 // always pause in single player if in console or menus
565         if (!sv.paused && (svs.maxclients > 1 || key_dest == key_game) )
566                 SV_Physics ();
567 }
568
569 void Host_ServerFrame (void)
570 {
571         float   save_host_frametime;
572         float   temp_host_frametime;
573         static float    host_serverframe_timevalue;
574
575 // run the world state  
576         pr_global_struct->frametime = host_frametime;
577
578 // set the time and clear the general datagram
579         SV_ClearDatagram ();
580                 
581 // check for new clients
582         SV_CheckForNewClients ();
583
584         temp_host_frametime = save_host_frametime = host_frametime;
585         // LordHavoc: the results of my attempts to mangle this code to process no more than sys_ticrate, 
586         // when I found that was too choppy, I changed it back to processing at least 20fps,
587         // I consider it a bit of a failure...  because I felt a little out of control in singleplayer
588         // (sliding around)
589         //if (host_serverframe_timevalue < -0.2) // don't let it get way out of range
590         //      host_serverframe_timevalue = -0.2;
591         //host_serverframe_timevalue += host_frametime;
592         // process frames (several if rendering is too slow to run well as a server)
593         while(temp_host_frametime > 0.0)
594         {
595                 host_frametime = temp_host_frametime;
596                 if (host_frametime > 0.05)
597                         host_frametime = 0.05;
598                 temp_host_frametime -= host_frametime;
599         //      host_serverframe_timevalue -= host_frametime;
600                 _Host_ServerFrame ();
601         }
602         host_frametime = save_host_frametime;
603
604 // send all messages to the clients
605         SV_SendClientMessages ();
606 // LordHavoc: sadly, this didn't look good to the person running the server in listen mode
607         /*
608 // wait until enough time has built up when the framerate exceeds sys_ticrate
609         if (host_serverframe_timevalue >= sys_ticrate.value)
610         //{
611         //      while(host_serverframe_timevalue >= sys_ticrate.value)
612         //              host_serverframe_timevalue -= sys_ticrate.value;
613 // send all messages to the clients
614                 SV_SendClientMessages ();
615         }
616         */
617 }
618
619 #else
620
621 double frametimetotal = 0, lastservertime = 0;
622 void Host_ServerFrame (void)
623 {
624         frametimetotal += host_frametime;
625         // LordHavoc: cap server at sys_ticrate in listen games
626         if (!isDedicated && svs.maxclients > 1 && ((realtime - lastservertime) < sys_ticrate.value))
627                 return;
628 // run the world state
629         pr_global_struct->frametime = frametimetotal;
630         frametimetotal = 0;
631 //      pr_global_struct->frametime = host_frametime;
632
633 // set the time and clear the general datagram
634         SV_ClearDatagram ();
635         
636 // check for new clients
637         SV_CheckForNewClients ();
638
639 // read client messages
640         SV_RunClients ();
641         
642 // move things around and think
643 // always pause in single player if in console or menus
644         if (!sv.paused && (svs.maxclients > 1 || key_dest == key_game) )
645                 SV_Physics ();
646
647 // send all messages to the clients
648         SV_SendClientMessages ();
649 }
650
651 #endif
652
653
654 /*
655 ==================
656 Host_Frame
657
658 Runs all active servers
659 ==================
660 */
661 void _Host_Frame (float time)
662 {
663         static double           time1 = 0;
664         static double           time2 = 0;
665         static double           time3 = 0;
666         int                     pass1, pass2, pass3;
667
668         if (setjmp (host_abortserver) )
669                 return;                 // something bad happened, or the server disconnected
670
671 // keep the random time dependent
672         rand ();
673         
674 // decide the simulation time
675         if (!Host_FilterTime (time))
676                 return;                 // don't run too fast, or packets will flood out
677                 
678 // get new key events
679         Sys_SendKeyEvents ();
680
681 // allow mice or other external controllers to add commands
682         IN_Commands ();
683
684 // process console commands
685         Cbuf_Execute ();
686
687         NET_Poll();
688
689 // if running the server locally, make intentions now
690         if (sv.active)
691                 CL_SendCmd ();
692         
693 //-------------------
694 //
695 // server operations
696 //
697 //-------------------
698
699 // check for commands typed to the host
700         Host_GetConsoleCommands ();
701         
702         if (sv.active)
703                 Host_ServerFrame ();
704
705 //-------------------
706 //
707 // client operations
708 //
709 //-------------------
710
711 // if running the server remotely, send intentions now after
712 // the incoming messages have been read
713         if (!sv.active)
714                 CL_SendCmd ();
715
716         host_time += host_frametime;
717
718 // fetch results from server
719         if (cls.state == ca_connected)
720         {
721                 CL_ReadFromServer ();
722         }
723
724 // update video
725         if (host_speeds.value)
726                 time1 = Sys_FloatTime ();
727                 
728         SCR_UpdateScreen ();
729
730         if (host_speeds.value)
731                 time2 = Sys_FloatTime ();
732                 
733 // update audio
734         if (cls.signon == SIGNONS)
735         {
736                 S_Update (r_origin, vpn, vright, vup);
737                 CL_DecayLights ();
738         }
739         else
740                 S_Update (vec3_origin, vec3_origin, vec3_origin, vec3_origin);
741         
742         CDAudio_Update();
743
744         if (host_speeds.value)
745         {
746                 pass1 = (time1 - time3)*1000;
747                 time3 = Sys_FloatTime ();
748                 pass2 = (time2 - time1)*1000;
749                 pass3 = (time3 - time2)*1000;
750                 Con_Printf ("%3i tot %3i server %3i gfx %3i snd\n",
751                                         pass1+pass2+pass3, pass1, pass2, pass3);
752         }
753         
754         host_framecount++;
755 }
756
757 void Host_Frame (float time)
758 {
759         double  time1, time2;
760         static double   timetotal;
761         static int              timecount;
762         int             i, c, m;
763
764         if (!serverprofile.value)
765         {
766                 _Host_Frame (time);
767                 return;
768         }
769         
770         time1 = Sys_FloatTime ();
771         _Host_Frame (time);
772         time2 = Sys_FloatTime ();       
773         
774         timetotal += time2 - time1;
775         timecount++;
776         
777         if (timecount < 1000)
778                 return;
779
780         m = timetotal*1000/timecount;
781         timecount = 0;
782         timetotal = 0;
783         c = 0;
784         for (i=0 ; i<svs.maxclients ; i++)
785         {
786                 if (svs.clients[i].active)
787                         c++;
788         }
789
790         Con_Printf ("serverprofile: %2i clients %2i msec\n",  c,  m);
791 }
792
793 //============================================================================
794
795
796 extern int vcrFile;
797 #define VCR_SIGNATURE   0x56435231
798 // "VCR1"
799
800 void Host_InitVCR (quakeparms_t *parms)
801 {
802         int             i, len, n;
803         char    *p;
804         
805         if (COM_CheckParm("-playback"))
806         {
807                 if (com_argc != 2)
808                         Sys_Error("No other parameters allowed with -playback\n");
809
810                 Sys_FileOpenRead("quake.vcr", &vcrFile);
811                 if (vcrFile == -1)
812                         Sys_Error("playback file not found\n");
813
814                 Sys_FileRead (vcrFile, &i, sizeof(int));
815                 if (i != VCR_SIGNATURE)
816                         Sys_Error("Invalid signature in vcr file\n");
817
818                 Sys_FileRead (vcrFile, &com_argc, sizeof(int));
819                 com_argv = malloc(com_argc * sizeof(char *));
820                 com_argv[0] = parms->argv[0];
821                 for (i = 0; i < com_argc; i++)
822                 {
823                         Sys_FileRead (vcrFile, &len, sizeof(int));
824                         p = malloc(len);
825                         Sys_FileRead (vcrFile, p, len);
826                         com_argv[i+1] = p;
827                 }
828                 com_argc++; /* add one for arg[0] */
829                 parms->argc = com_argc;
830                 parms->argv = com_argv;
831         }
832
833         if ( (n = COM_CheckParm("-record")) != 0)
834         {
835                 vcrFile = Sys_FileOpenWrite("quake.vcr");
836
837                 i = VCR_SIGNATURE;
838                 Sys_FileWrite(vcrFile, &i, sizeof(int));
839                 i = com_argc - 1;
840                 Sys_FileWrite(vcrFile, &i, sizeof(int));
841                 for (i = 1; i < com_argc; i++)
842                 {
843                         if (i == n)
844                         {
845                                 len = 10;
846                                 Sys_FileWrite(vcrFile, &len, sizeof(int));
847                                 Sys_FileWrite(vcrFile, "-playback", len);
848                                 continue;
849                         }
850                         len = strlen(com_argv[i]) + 1;
851                         Sys_FileWrite(vcrFile, &len, sizeof(int));
852                         Sys_FileWrite(vcrFile, com_argv[i], len);
853                 }
854         }
855         
856 }
857
858 /*
859 ====================
860 Host_Init
861 ====================
862 */
863 void Host_Init (quakeparms_t *parms)
864 {
865
866         if (standard_quake)
867                 minimum_memory = MINIMUM_MEMORY;
868         else
869                 minimum_memory = MINIMUM_MEMORY_LEVELPAK;
870
871         if (COM_CheckParm ("-minmemory"))
872                 parms->memsize = minimum_memory;
873
874         host_parms = *parms;
875
876         if (parms->memsize < minimum_memory)
877                 Sys_Error ("Only %4.1f megs of memory available, can't execute game", parms->memsize / (float)0x100000);
878
879         com_argc = parms->argc;
880         com_argv = parms->argv;
881
882         Memory_Init (parms->membase, parms->memsize);
883         Cbuf_Init ();
884         Cmd_Init ();    
885         V_Init ();
886         Chase_Init ();
887         Host_InitVCR (parms);
888         COM_Init (parms->basedir);
889         Host_InitLocal ();
890         W_LoadWadFile ("gfx.wad");
891         Key_Init ();
892         Con_Init ();    
893         M_Init ();      
894         PR_Init ();
895         Mod_Init ();
896         NET_Init ();
897         SV_Init ();
898
899         Con_Printf ("Exe: "__TIME__" "__DATE__"\n");
900         Con_Printf ("%4.1f megabyte heap\n",parms->memsize/ (1024*1024.0));
901         
902         R_InitTextures ();              // needed even for dedicated servers
903  
904         if (cls.state != ca_dedicated)
905         {
906                 host_basepal = (byte *)COM_LoadHunkFile ("gfx/palette.lmp", false);
907                 if (!host_basepal)
908                         Sys_Error ("Couldn't load gfx/palette.lmp");
909 //              host_colormap = (byte *)COM_LoadHunkFile ("gfx/colormap.lmp", false);
910 //              if (!host_colormap)
911 //                      Sys_Error ("Couldn't load gfx/colormap.lmp");
912
913 #ifndef _WIN32 // on non win32, mouse comes before video for security reasons
914                 IN_Init ();
915 #endif
916                 VID_Init (host_basepal);
917
918                 Draw_Init ();
919                 SCR_Init ();
920                 R_Init ();
921                 S_Init ();
922                 CDAudio_Init ();
923                 Sbar_Init ();
924                 CL_Init ();
925 #ifdef _WIN32 // on non win32, mouse comes before video for security reasons
926                 IN_Init ();
927 #endif
928         }
929
930         Cbuf_InsertText ("exec quake.rc\n");
931
932         Hunk_AllocName (0, "-HOST_HUNKLEVEL-");
933         host_hunklevel = Hunk_LowMark ();
934
935         host_initialized = true;
936         
937 //      Sys_Printf ("========Quake Initialized=========\n");    
938 //      printf("========Quake Initialized=========\n");
939 }
940
941
942 /*
943 ===============
944 Host_Shutdown
945
946 FIXME: this is a callback from Sys_Quit and Sys_Error.  It would be better
947 to run quit through here before the final handoff to the sys code.
948 ===============
949 */
950 void Host_Shutdown(void)
951 {
952         static qboolean isdown = false;
953         
954         if (isdown)
955         {
956                 printf ("recursive shutdown\n");
957                 return;
958         }
959         isdown = true;
960
961 // keep Con_Printf from trying to update the screen
962         scr_disabled_for_loading = true;
963
964         Host_WriteConfiguration (); 
965
966         CDAudio_Shutdown ();
967         NET_Shutdown ();
968         S_Shutdown();
969         IN_Shutdown ();
970
971         if (cls.state != ca_dedicated)
972         {
973                 VID_Shutdown();
974         }
975 }
976