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