]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - csprogs.c
f6758470c34f3fc3738a0e85c573f2fe5a1d3664
[xonotic/darkplaces.git] / csprogs.c
1 #include "quakedef.h"
2 #include "progsvm.h"
3 #include "clprogdefs.h"
4 #include "csprogs.h"
5
6 //============================================================================
7 // Client prog handling
8 //[515]: omg !!! optimize it ! a lot of hacks here and there also :P
9
10 #define CSQC_RETURNVAL  prog->globals.generic[OFS_RETURN]
11 #define CSQC_BEGIN              csqc_tmpprog=prog;prog=0;PRVM_SetProg(PRVM_CLIENTPROG);
12 #define CSQC_END                prog=csqc_tmpprog;
13 static prvm_prog_t *csqc_tmpprog;
14
15 //[515]: these are required funcs
16 #define CL_F_INIT                               "CSQC_Init"
17 #define CL_F_INPUTEVENT                 "CSQC_InputEvent"
18 #define CL_F_UPDATEVIEW                 "CSQC_UpdateView"
19 #define CL_F_CONSOLECOMMAND             "CSQC_ConsoleCommand"
20 #define CL_F_SHUTDOWN                   "CSQC_Shutdown"
21
22 //[515]: these are optional
23 #define CL_F_PARSE_TEMPENTITY   "CSQC_Parse_TempEntity" //[515]: very helpfull when you want to create your own particles/decals/etc for effects that allready exist
24 #define CL_F_PARSE_STUFFCMD             "CSQC_Parse_StuffCmd"
25 #define CL_F_PARSE_PRINT                "CSQC_Parse_Print"
26 #define CL_F_PARSE_CENTERPRINT  "CSQC_Parse_CenterPrint"
27 #define CL_F_ENT_UPDATE                 "CSQC_Ent_Update"
28 #define CL_F_ENT_REMOVE                 "CSQC_Ent_Remove"
29 #define CL_F_EVENT                              "CSQC_Event"    //[515]: engine call this for its own needs
30                                                                                                 //so csqc can do some things according to what engine it's running on
31                                                                                                 //example: to say about edicts increase, whatever...
32
33 #define CSQC_PRINTBUFFERLEN             8192    //[515]: enough ?
34
35 static char *cl_required_func[] =
36 {
37         CL_F_INIT,
38         CL_F_INPUTEVENT,
39         CL_F_UPDATEVIEW,
40         CL_F_CONSOLECOMMAND,
41         CL_F_SHUTDOWN
42 };
43
44 static int cl_numrequiredfunc = sizeof(cl_required_func) / sizeof(char*);
45
46 unsigned int                    csqc_drawmask = 0;
47 static char                             *csqc_printtextbuf = NULL;
48 static unsigned short   *csqc_sv2csqcents;      //[515]: server entities numbers on client side. FIXME : make pointers instead of numbers ?
49
50 static mfunction_t      *CSQC_Parse_TempEntity;
51 static mfunction_t      *CSQC_Parse_StuffCmd;
52 static mfunction_t      *CSQC_Parse_Print;
53 static mfunction_t      *CSQC_Parse_CenterPrint;
54 static mfunction_t      *CSQC_Ent_Update;
55 static mfunction_t      *CSQC_Ent_Remove;
56 static mfunction_t      *CSQC_Event;
57
58 static int csqc_fieldoff_alpha;
59 static int csqc_fieldoff_colormod;
60 static int csqc_fieldoff_effects;
61 int csqc_fieldoff_scale;
62 int csqc_fieldoff_renderflags;
63 int csqc_fieldoff_tag_entity;
64 int csqc_fieldoff_tag_index;
65 int csqc_fieldoff_dphitcontentsmask;
66
67 qboolean csqc_loaded = false;
68
69 vec3_t csqc_origin, csqc_angles;
70 static double csqc_frametime = 0;
71 int csqc_buttons;
72
73 static mempool_t *csqc_mempool;
74
75 static void CL_VM_FindEdictFieldOffsets (void)
76 {
77         csqc_fieldoff_alpha                     = PRVM_ED_FindFieldOffset("alpha");
78         csqc_fieldoff_scale                     = PRVM_ED_FindFieldOffset("scale");
79         csqc_fieldoff_colormod          = PRVM_ED_FindFieldOffset("colormod");
80         csqc_fieldoff_renderflags       = PRVM_ED_FindFieldOffset("renderflags");
81         csqc_fieldoff_effects           = PRVM_ED_FindFieldOffset("effects");
82         csqc_fieldoff_tag_entity        = PRVM_ED_FindFieldOffset("tag_entity");
83         csqc_fieldoff_tag_index         = PRVM_ED_FindFieldOffset("tag_index");
84
85         CSQC_Parse_TempEntity           = PRVM_ED_FindFunction (CL_F_PARSE_TEMPENTITY);
86         CSQC_Parse_StuffCmd                     = PRVM_ED_FindFunction (CL_F_PARSE_STUFFCMD);
87         CSQC_Parse_Print                        = PRVM_ED_FindFunction (CL_F_PARSE_PRINT);
88         CSQC_Parse_CenterPrint          = PRVM_ED_FindFunction (CL_F_PARSE_CENTERPRINT);
89         CSQC_Ent_Update                         = PRVM_ED_FindFunction (CL_F_ENT_UPDATE);
90         CSQC_Ent_Remove                         = PRVM_ED_FindFunction (CL_F_ENT_REMOVE);
91         CSQC_Event                                      = PRVM_ED_FindFunction (CL_F_EVENT);
92
93         if(CSQC_Parse_Print)
94         {
95                 csqc_printtextbuf = (char *)Mem_Alloc(csqc_mempool, CSQC_PRINTBUFFERLEN);
96                 csqc_printtextbuf[0] = 0;
97         }
98 }
99
100 void CL_VM_Error (const char *format, ...)      //[515]: hope it will be never executed =)
101 {
102         char errorstring[4096];
103         va_list argptr;
104
105         va_start (argptr, format);
106         dpvsnprintf (errorstring, sizeof(errorstring), format, argptr);
107         va_end (argptr);
108 //      Con_Printf( "CL_VM_Error: %s\n", errorstring );
109
110         PRVM_Crash();
111         csqc_loaded = false;
112         Mem_FreePool(&csqc_mempool);
113
114         Cvar_SetValueQuick(&csqc_progcrc, 0);
115
116 //      Host_AbortCurrentFrame();       //[515]: hmmm... if server says it needs csqc then client MUST disconnect
117         Host_Error(va("CL_VM_Error: %s", errorstring));
118 }
119
120 //[515]: set globals before calling R_UpdateView, WEIRD CRAP
121 static void CSQC_SetGlobals (void)
122 {
123         //extern cvar_t sv_accelerate, sv_friction, sv_gravity, sv_stopspeed, sv_maxspeed;
124
125         CSQC_BEGIN
126                 *prog->time = cl.time;
127                 prog->globals.client->frametime = cl.time - csqc_frametime;
128                 csqc_frametime = cl.time;
129                 prog->globals.client->servercommandframe = cl.servermovesequence;
130                 prog->globals.client->clientcommandframe = cl.movemessages;
131                 VectorCopy(cl.viewangles, prog->globals.client->input_angles);
132                 VectorCopy(cl.viewangles, csqc_angles);
133                 prog->globals.client->input_buttons = csqc_buttons;
134                 VectorSet(prog->globals.client->input_movevalues, cl.cmd.forwardmove, cl.cmd.sidemove, cl.cmd.upmove);
135                 //VectorCopy(cl.movement_origin, csqc_origin);
136                 VectorCopy(cl.entities[cl.viewentity].render.origin, csqc_origin);
137                 VectorCopy(csqc_origin, prog->globals.client->pmove_org);
138                 prog->globals.client->maxclients = cl.maxclients;
139                 //VectorCopy(cl.movement_velocity, prog->globals.client->pmove_vel);
140                 VectorCopy(cl.velocity, prog->globals.client->pmove_vel);
141         CSQC_END
142 }
143
144 static void CSQC_Predraw (prvm_edict_t *ed)
145 {
146         int b;
147         if(!ed->fields.client->predraw)
148                 return;
149         b = prog->globals.client->self;
150         prog->globals.client->self = PRVM_EDICT_TO_PROG(ed);
151         PRVM_ExecuteProgram(ed->fields.client->predraw, "CSQC_Predraw: NULL function\n");
152         prog->globals.client->self = b;
153 }
154
155 static void CSQC_Think (prvm_edict_t *ed)
156 {
157         int b;
158         if(ed->fields.client->think)
159         if(ed->fields.client->nextthink && ed->fields.client->nextthink <= *prog->time)
160         {
161                 ed->fields.client->nextthink = 0;
162                 b = prog->globals.client->self;
163                 prog->globals.client->self = PRVM_EDICT_TO_PROG(ed);
164                 PRVM_ExecuteProgram(ed->fields.client->think, "CSQC_Think: NULL function\n");
165                 prog->globals.client->self = b;
166         }
167 }
168
169 //[515]: weird too
170 static qboolean CSQC_EdictToEntity (prvm_edict_t *ed, entity_t *e)
171 {
172         int i;
173         prvm_eval_t *val;
174
175         i = (int)ed->fields.client->modelindex;
176         e->state_current.modelindex = 0;
177         if(i >= MAX_MODELS || i <= -MAX_MODELS) //[515]: make work as error ?
178         {
179                 Con_Print("CSQC_EdictToEntity: modelindex >= MAX_MODELS\n");
180                 ed->fields.client->modelindex = 0;
181         }
182         else
183                 e->state_current.modelindex = i;
184         if(!e->state_current.modelindex)
185                 return false;
186
187         e->state_current.time = cl.time;
188
189         i = 0;
190         if((val = PRVM_GETEDICTFIELDVALUE(ed, csqc_fieldoff_renderflags)) && val->_float)
191         {
192                 i = (int)val->_float;
193                 if(i & RF_VIEWMODEL)    e->state_current.flags |= RENDER_VIEWMODEL;
194                 if(i & RF_EXTERNALMODEL)e->state_current.flags |= RENDER_EXTERIORMODEL;
195                 if(i & RF_DEPTHHACK)    e->state_current.effects |= EF_NODEPTHTEST;
196                 if(i & RF_ADDITIVE)             e->state_current.effects |= EF_ADDITIVE;
197         }
198
199         if(i & RF_USEAXIS)      //FIXME!!!
200                 VectorCopy(ed->fields.client->angles, e->persistent.newangles);
201         else
202                 VectorCopy(ed->fields.client->angles, e->persistent.newangles);
203
204         VectorCopy(ed->fields.client->origin, e->persistent.neworigin);
205         VectorCopy(ed->fields.client->origin, e->state_current.origin);
206         e->state_current.colormap = (int)ed->fields.client->colormap;
207         e->state_current.effects = (int)ed->fields.client->effects;
208         e->state_current.frame = (int)ed->fields.client->frame;
209         e->state_current.skin = (int)ed->fields.client->skin;
210
211         if((val = PRVM_GETEDICTFIELDVALUE(ed, csqc_fieldoff_alpha)) && val->_float)             e->state_current.alpha = (int)(val->_float*255);
212         if((val = PRVM_GETEDICTFIELDVALUE(ed, csqc_fieldoff_scale)) && val->_float)             e->state_current.scale = (int)(val->_float*16);
213         if((val = PRVM_GETEDICTFIELDVALUE(ed, csqc_fieldoff_colormod)) && VectorLength2(val->vector))   {int j;for (j = 0;j < 3;j++) e->state_current.colormod[j] = (unsigned char)bound(0, val->vector[j] * 32.0f, 255);}
214         if((val = PRVM_GETEDICTFIELDVALUE(ed, csqc_fieldoff_effects)) && val->_float)   e->state_current.effects = (int)val->_float;
215         if((val = PRVM_GETEDICTFIELDVALUE(ed, csqc_fieldoff_tag_entity)) && val->edict)
216         {
217                 e->state_current.tagentity = val->edict;
218                 if((val = PRVM_GETEDICTFIELDVALUE(ed, csqc_fieldoff_tag_index)) && val->_float)
219                         e->state_current.tagindex = (int)val->_float;
220         }
221
222         return true;
223 }
224
225 void CSQC_ClearCSQCEntities (void)
226 {
227         memset(cl.csqcentities_active, 0, sizeof(cl.csqcentities_active));
228         cl.num_csqcentities = 0;
229         csqc_drawmask = 0;
230 }
231
232 void CL_ExpandCSQCEntities (int num);
233
234 void CSQC_RelinkCSQCEntities (void)
235 {
236         int                     i;
237         entity_t        *e;
238         prvm_edict_t *ed;
239
240         *prog->time = cl.time;
241         for(i=1;i<prog->num_edicts;i++)
242         {
243                 if(i >= cl.max_csqcentities)
244                         CL_ExpandCSQCEntities(i);
245
246                 e = &cl.csqcentities[i];
247                 ed = &prog->edicts[i];
248                 if(ed->priv.required->free)
249                 {
250                         e->state_current.active = false;
251                         cl.csqcentities_active[i] = false;
252                         continue;
253                 }
254                 VectorAdd(ed->fields.client->origin, ed->fields.client->mins, ed->fields.client->absmin);
255                 VectorAdd(ed->fields.client->origin, ed->fields.client->maxs, ed->fields.client->absmax);
256                 CSQC_Think(ed);
257                 if(ed->priv.required->free)
258                 {
259                         e->state_current.active = false;
260                         cl.csqcentities_active[i] = false;
261                         continue;
262                 }
263                 CSQC_Predraw(ed);
264                 if(ed->priv.required->free)
265                 {
266                         e->state_current.active = false;
267                         cl.csqcentities_active[i] = false;
268                         continue;
269                 }
270                 if(!cl.csqcentities_active[i])
271                 if(!((int)ed->fields.client->drawmask & csqc_drawmask))
272                         continue;
273
274                 e->state_previous       = e->state_current;
275                 e->state_current        = defaultstate;
276                 if((cl.csqcentities_active[i] = CSQC_EdictToEntity(ed, e)))
277                 {
278                         if(!e->state_current.active)
279                         {
280                                 if(!e->state_previous.active)
281                                         VectorCopy(ed->fields.client->origin, e->persistent.trail_origin);//[515]: hack to make good gibs =/
282                                 e->state_previous = e->state_current;
283                                 e->state_current.active = true;
284                         }
285                         e->persistent.lerpdeltatime = 0;//prog->globals.client->frametime;
286                         cl.num_csqcentities++;
287                 }
288         }
289 }
290
291 //[515]: omfg... it's all weird =/
292 void CSQC_AddEntity (int n)
293 {
294         cl.csqcentities_active[n] = true;
295 }
296
297 qboolean CL_VM_InputEvent (qboolean pressed, int key)
298 {
299         qboolean r;
300         if(!csqc_loaded)
301                 return false;
302         CSQC_BEGIN
303                 *prog->time = cl.time;
304                 PRVM_G_FLOAT(OFS_PARM0) = pressed;
305                 PRVM_G_FLOAT(OFS_PARM1) = key;
306                 PRVM_ExecuteProgram (prog->globals.client->CSQC_InputEvent, CL_F_INPUTEVENT);
307                 r = CSQC_RETURNVAL;
308         CSQC_END
309         return r;
310 }
311
312 qboolean CL_VM_UpdateView (void)
313 {
314 //      vec3_t oldangles;
315         if(!csqc_loaded)
316                 return false;
317         CSQC_BEGIN
318                 //VectorCopy(cl.viewangles, oldangles);
319                 *prog->time = cl.time;
320                 CSQC_SetGlobals();
321                 csqc_drawmask = 0;
322                 cl.num_csqcentities = 0;
323                 PRVM_ExecuteProgram (prog->globals.client->CSQC_UpdateView, CL_F_UPDATEVIEW);
324                 //VectorCopy(oldangles, cl.viewangles);
325         CSQC_END
326         csqc_frame = false;
327         return true;
328 }
329
330 qboolean CL_VM_ConsoleCommand (const char *cmd)
331 {
332         qboolean r;
333         if(!csqc_loaded)
334                 return false;
335         CSQC_BEGIN
336                 *prog->time = cl.time;
337                 PRVM_G_INT(OFS_PARM0) = PRVM_SetEngineString(cmd);
338                 PRVM_ExecuteProgram (prog->globals.client->CSQC_ConsoleCommand, CL_F_CONSOLECOMMAND);
339                 r = CSQC_RETURNVAL;
340         CSQC_END
341         return r;
342 }
343
344 qboolean CL_VM_Parse_TempEntity (void)
345 {
346         int                     t;
347         qboolean        r;
348         if(!csqc_loaded || !CSQC_Parse_TempEntity)
349                 return false;
350         t = msg_readcount;
351         CSQC_BEGIN
352                 *prog->time = cl.time;
353                 PRVM_ExecuteProgram ((func_t)(CSQC_Parse_TempEntity - prog->functions), CL_F_PARSE_TEMPENTITY);
354                 r = CSQC_RETURNVAL;
355         CSQC_END
356         if(!r)
357         {
358                 msg_readcount = t;
359                 msg_badread = false;
360         }
361         return r;
362 }
363
364 void CL_VM_Parse_StuffCmd (const char *msg)
365 {
366         if(!csqc_loaded)        //[515]: add more here
367         if(msg[0] == 'c')
368         if(msg[1] == 's')
369         if(msg[2] == 'q')
370         if(msg[3] == 'c')
371         {
372                 Cvar_SetQuick(&csqc_progcrc, "0");
373                 csqc_progcrc.flags = 0;
374                 Cmd_ExecuteString (msg, src_command);
375                 csqc_progcrc.flags = CVAR_READONLY;
376                 return;
377         }
378         if(!csqc_loaded || !CSQC_Parse_StuffCmd)
379         {
380                 Cbuf_AddText(msg);
381                 return;
382         }
383         CSQC_BEGIN
384                 *prog->time = cl.time;
385                 PRVM_G_INT(OFS_PARM0) = PRVM_SetEngineString(msg);
386                 PRVM_ExecuteProgram ((func_t)(CSQC_Parse_StuffCmd - prog->functions), CL_F_PARSE_STUFFCMD);
387         CSQC_END
388 }
389
390 static void CL_VM_Parse_Print (const char *msg)
391 {
392         CSQC_BEGIN
393                 *prog->time = cl.time;
394                 PRVM_G_INT(OFS_PARM0) = PRVM_SetEngineString(msg);
395                 PRVM_ExecuteProgram ((func_t)(CSQC_Parse_Print - prog->functions), CL_F_PARSE_PRINT);
396         CSQC_END
397 }
398
399 void CSQC_AddPrintText (const char *msg)
400 {
401         size_t i;
402         if(!csqc_loaded || !CSQC_Parse_Print)
403         {
404                 Con_Print(msg);
405                 return;
406         }
407         // FIXME: is this bugged?
408         i = strlen(msg)-1;
409         if(msg[i] != '\n' && msg[i] != '\r')
410         {
411                 if(strlen(csqc_printtextbuf)+i >= CSQC_PRINTBUFFERLEN)
412                 {
413                         CL_VM_Parse_Print(csqc_printtextbuf);
414                         csqc_printtextbuf[0] = 0;
415                 }
416                 else
417                         strcat(csqc_printtextbuf, msg);
418                 return;
419         }
420         strcat(csqc_printtextbuf, msg);
421         CL_VM_Parse_Print(csqc_printtextbuf);
422         csqc_printtextbuf[0] = 0;
423 }
424
425 void CL_VM_Parse_CenterPrint (const char *msg)
426 {
427         if(!csqc_loaded || !CSQC_Parse_CenterPrint)
428         {
429                 SCR_CenterPrint((char*)msg);
430                 return;
431         }
432         CSQC_BEGIN
433                 *prog->time = cl.time;
434                 PRVM_G_INT(OFS_PARM0) = PRVM_SetEngineString(msg);
435                 PRVM_ExecuteProgram ((func_t)(CSQC_Parse_CenterPrint - prog->functions), CL_F_PARSE_CENTERPRINT);
436         CSQC_END
437 }
438
439 float CL_VM_Event (float event)         //[515]: needed ? I'd say "YES", but don't know for what :D
440 {
441         float r;
442         if(!csqc_loaded || !CSQC_Event)
443                 return 0;
444         CSQC_BEGIN
445                 *prog->time = cl.time;
446                 PRVM_G_FLOAT(OFS_PARM0) = event;
447                 PRVM_ExecuteProgram ((func_t)(CSQC_Event - prog->functions), CL_F_EVENT);
448                 r = CSQC_RETURNVAL;
449         CSQC_END
450         return r;
451 }
452
453 void CSQC_ReadEntities (void)
454 {
455         unsigned short entnum, oldself, realentnum;
456         CSQC_BEGIN
457                 *prog->time = cl.time;
458                 oldself = prog->globals.client->self;
459                 while(1)
460                 {
461                         entnum = MSG_ReadShort();
462                         if(!entnum)
463                                 return;
464                         realentnum = entnum & 0x7FFF;
465                         prog->globals.client->self = csqc_sv2csqcents[realentnum];
466                         if(entnum & 0x8000)
467                         {
468                                 if(prog->globals.client->self)
469                                 {
470                                         PRVM_ExecuteProgram((func_t)(CSQC_Ent_Remove - prog->functions), CL_F_ENT_REMOVE);
471                                         csqc_sv2csqcents[realentnum] = 0;
472                                 }
473                                 else
474                                         Con_Printf("Smth bad happens in csqc...\n");    //[515]: never happens ?
475                         }
476                         else
477                         {
478                                 if(!prog->globals.client->self)
479                                 {
480                                         prvm_edict_t    *ed;
481                                         ed = PRVM_ED_Alloc();
482                                         ed->fields.client->entnum = realentnum;
483                                         prog->globals.client->self = csqc_sv2csqcents[realentnum] = PRVM_EDICT_TO_PROG(ed);
484                                         PRVM_G_FLOAT(OFS_PARM0) = 1;
485                                 }
486                                 else
487                                         PRVM_G_FLOAT(OFS_PARM0) = 0;
488                                 PRVM_ExecuteProgram((func_t)(CSQC_Ent_Update - prog->functions), CL_F_ENT_UPDATE);
489                         }
490                 }
491                 prog->globals.client->self = oldself;
492         CSQC_END
493 }
494
495 void Cmd_ClearCsqcFuncs (void);
496
497 void CL_VM_Init (void)
498 {
499         entity_t *ent;
500
501         csqc_loaded = false;
502         memset(cl.csqc_model_precache, 0, sizeof(cl.csqc_model_precache));
503         memset(&cl.csqc_vidvars, true, sizeof(csqc_vidvars_t));
504
505         if(!FS_FileExists(csqc_progname.string))
506         {
507                 if(!sv.active && csqc_progcrc.integer)
508                 {
509                         Con_Printf("CL_VM_Init: server requires CSQC, but \"%s\" wasn't found\n", csqc_progname.string);
510                         CL_Disconnect();
511                 }
512                 return;
513         }
514         else
515                 if(!sv.active && !csqc_progcrc.integer) //[515]: because cheaters may use csqc while server didn't allowed it !
516                 {
517                         Con_Printf("CL_VM_Init: server didn't sent CSQC crc, so CSQC is disabled\n");
518                         return;
519                 }
520
521         PRVM_Begin;
522         PRVM_InitProg(PRVM_CLIENTPROG);
523
524         // allocate the mempools
525         prog->progs_mempool = Mem_AllocPool(csqc_progname.string, 0, NULL);
526         prog->headercrc = CL_PROGHEADER_CRC;
527         prog->edictprivate_size = 0; // no private struct used
528         prog->name = csqc_progname.string;
529         prog->num_edicts = 1;
530         prog->limit_edicts = CL_MAX_EDICTS;
531         prog->extensionstring = vm_cl_extensions;
532         prog->builtins = vm_cl_builtins;
533         prog->numbuiltins = vm_cl_numbuiltins;
534         prog->init_cmd = VM_CL_Cmd_Init;
535         prog->reset_cmd = VM_CL_Cmd_Reset;
536         prog->error_cmd = CL_VM_Error;
537
538         PRVM_LoadProgs(csqc_progname.string, cl_numrequiredfunc, cl_required_func, 0, NULL);
539
540         if(!sv.active && !cls.demoplayback && prog->filecrc != (unsigned short)csqc_progcrc.integer)
541         {
542                 Con_Printf("^1Your CSQC version differs from server's one (%i!=%i)\n", prog->filecrc, csqc_progcrc.integer);
543                 PRVM_ResetProg();
544                 CL_Disconnect();
545                 return;
546         }
547
548         if(prog->loaded)
549         {
550                 Cvar_SetValueQuick(&csqc_progcrc, prog->filecrc);
551                 Con_Printf("CSQC ^5loaded (crc=%i)\n", csqc_progcrc.integer);
552         }
553         else
554         {
555                 CL_VM_Error("CSQC ^2failed to load\n");
556                 if(!sv.active)
557                         CL_Disconnect();
558                 return;
559         }
560
561         csqc_mempool = Mem_AllocPool("CSQC", 0, NULL);
562
563         //[515]: optional fields & funcs
564         CL_VM_FindEdictFieldOffsets();
565
566         // set time
567         *prog->time = cl.time;
568         csqc_frametime = 0;
569
570         prog->globals.client->mapname = PRVM_SetEngineString(cl.worldmodel->name);
571         prog->globals.client->player_localentnum = cl.playerentity;
572
573         // call the prog init
574         PRVM_ExecuteProgram((func_t) (PRVM_ED_FindFunction(CL_F_INIT) - prog->functions), CL_F_INIT);
575
576         PRVM_End;
577         csqc_loaded = true;
578
579         csqc_sv2csqcents = (unsigned short *)Mem_Alloc(csqc_mempool, MAX_EDICTS*sizeof(unsigned short));
580         memset(csqc_sv2csqcents, 0, MAX_EDICTS*sizeof(unsigned short));
581
582         cl.csqc_vidvars.drawcrosshair = false;
583         cl.csqc_vidvars.drawenginesbar = false;
584
585         // local state
586         ent = &cl.csqcentities[0];
587         // entire entity array was cleared, so just fill in a few fields
588         ent->state_current.active = true;
589         ent->render.model = cl.worldmodel = cl.model_precache[1];
590         ent->render.scale = 1; // some of the renderer still relies on scale
591         ent->render.alpha = 1;
592         ent->render.flags = RENDER_SHADOW | RENDER_LIGHT;
593         Matrix4x4_CreateFromQuakeEntity(&ent->render.matrix, 0, 0, 0, 0, 0, 0, 1);
594         Matrix4x4_Invert_Simple(&ent->render.inversematrix, &ent->render.matrix);
595         CL_BoundingBoxForEntity(&ent->render);
596 }
597
598 void CL_VM_ShutDown (void)
599 {
600         Cmd_ClearCsqcFuncs();
601         Cvar_SetValueQuick(&csqc_progcrc, 0);
602         if(!csqc_loaded)
603                 return;
604         CSQC_BEGIN
605                 *prog->time = cl.time;
606                 PRVM_ExecuteProgram((func_t) (PRVM_ED_FindFunction(CL_F_SHUTDOWN) - prog->functions), CL_F_SHUTDOWN);
607                 PRVM_ResetProg();
608         CSQC_END
609         Con_Print("CSQC ^1unloaded\n");
610         csqc_loaded = false;
611         Mem_FreePool(&csqc_mempool);
612 }