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