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