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