]> icculus.org git repositories - divverent/darkplaces.git/blob - csprogs.c
some cleanup of the GLSL shader, no longer uses #ifdef variable declarations because...
[divverent/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 = 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 = 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 = 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 = ed->fields.client->colormap;
207         e->state_current.effects = ed->fields.client->effects;
208         e->state_current.frame = ed->fields.client->frame;
209         e->state_current.skin = ed->fields.client->skin;
210
211         if((val = PRVM_GETEDICTFIELDVALUE(ed, csqc_fieldoff_alpha)) && val->_float)             e->state_current.alpha = val->_float*255;
212         if((val = PRVM_GETEDICTFIELDVALUE(ed, csqc_fieldoff_scale)) && val->_float)             e->state_current.scale = val->_float*16;
213         if((val = PRVM_GETEDICTFIELDVALUE(ed, csqc_fieldoff_colormod)) && VectorLength2(val->vector))   VectorScale(val->vector, 32, e->state_current.colormod);
214         if((val = PRVM_GETEDICTFIELDVALUE(ed, csqc_fieldoff_effects)) && val->_float)   e->state_current.effects = 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 = 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         i = strlen(msg)-1;
408         if(msg[i] != '\n' && msg[i] != '\r')
409         {
410                 if(strlen(csqc_printtextbuf)+i >= CSQC_PRINTBUFFERLEN)
411                 {
412                         CL_VM_Parse_Print(csqc_printtextbuf);
413                         csqc_printtextbuf[0] = 0;
414                 }
415                 else
416                         strcat(csqc_printtextbuf, msg);
417                 return;
418         }
419         strcat(csqc_printtextbuf, msg);
420         CL_VM_Parse_Print(csqc_printtextbuf);
421         csqc_printtextbuf[0] = 0;
422 }
423
424 void CL_VM_Parse_CenterPrint (const char *msg)
425 {
426         if(!csqc_loaded || !CSQC_Parse_CenterPrint)
427         {
428                 SCR_CenterPrint((char*)msg);
429                 return;
430         }
431         CSQC_BEGIN
432                 *prog->time = cl.time;
433                 PRVM_G_INT(OFS_PARM0) = PRVM_SetEngineString(msg);
434                 PRVM_ExecuteProgram ((func_t)(CSQC_Parse_CenterPrint - prog->functions), CL_F_PARSE_CENTERPRINT);
435         CSQC_END
436 }
437
438 float CL_VM_Event (float event)         //[515]: needed ? I'd say "YES", but don't know for what :D
439 {
440         float r;
441         if(!csqc_loaded || !CSQC_Event)
442                 return 0;
443         CSQC_BEGIN
444                 *prog->time = cl.time;
445                 PRVM_G_FLOAT(OFS_PARM0) = event;
446                 PRVM_ExecuteProgram ((func_t)(CSQC_Event - prog->functions), CL_F_EVENT);
447                 r = CSQC_RETURNVAL;
448         CSQC_END
449         return r;
450 }
451
452 void CSQC_ReadEntities (void)
453 {
454         unsigned short entnum, oldself, realentnum;
455         CSQC_BEGIN
456                 *prog->time = cl.time;
457                 oldself = prog->globals.client->self;
458                 while(1)
459                 {
460                         entnum = MSG_ReadShort();
461                         if(!entnum)
462                                 return;
463                         realentnum = entnum & 0x7FFF;
464                         prog->globals.client->self = csqc_sv2csqcents[realentnum];
465                         if(entnum & 0x8000)
466                         {
467                                 if(prog->globals.client->self)
468                                 {
469                                         PRVM_ExecuteProgram((func_t)(CSQC_Ent_Remove - prog->functions), CL_F_ENT_REMOVE);
470                                         csqc_sv2csqcents[realentnum] = 0;
471                                 }
472                                 else
473                                         Con_Printf("Smth bad happens in csqc...\n");    //[515]: never happens ?
474                         }
475                         else
476                         {
477                                 if(!prog->globals.client->self)
478                                 {
479                                         prvm_edict_t    *ed;
480                                         ed = PRVM_ED_Alloc();
481                                         ed->fields.client->entnum = realentnum;
482                                         prog->globals.client->self = csqc_sv2csqcents[realentnum] = PRVM_EDICT_TO_PROG(ed);
483                                         PRVM_G_FLOAT(OFS_PARM0) = 1;
484                                 }
485                                 else
486                                         PRVM_G_FLOAT(OFS_PARM0) = 0;
487                                 PRVM_ExecuteProgram((func_t)(CSQC_Ent_Update - prog->functions), CL_F_ENT_UPDATE);
488                         }
489                 }
490                 prog->globals.client->self = oldself;
491         CSQC_END
492 }
493
494 void Cmd_ClearCsqcFuncs (void);
495
496 void CL_VM_Init (void)
497 {
498         entity_t *ent;
499
500         csqc_loaded = false;
501         memset(cl.csqc_model_precache, 0, sizeof(cl.csqc_model_precache));
502         memset(&cl.csqc_vidvars, true, sizeof(csqc_vidvars_t));
503
504         if(!FS_FileExists(csqc_progname.string))
505         {
506                 if(!sv.active && csqc_progcrc.integer)
507                 {
508                         Con_Printf("CL_VM_Init: server requires CSQC, but \"%s\" wasn't found\n", csqc_progname.string);
509                         CL_Disconnect();
510                 }
511                 return;
512         }
513         else
514                 if(!sv.active && !csqc_progcrc.integer) //[515]: because cheaters may use csqc while server didn't allowed it !
515                 {
516                         Con_Printf("CL_VM_Init: server didn't sent CSQC crc, so CSQC is disabled\n");
517                         return;
518                 }
519
520         PRVM_Begin;
521         PRVM_InitProg(PRVM_CLIENTPROG);
522
523         // allocate the mempools
524         prog->progs_mempool = Mem_AllocPool(csqc_progname.string, 0, NULL);
525         prog->headercrc = CL_PROGHEADER_CRC;
526         prog->edictprivate_size = 0; // no private struct used
527         prog->name = csqc_progname.string;
528         prog->num_edicts = 1;
529         prog->limit_edicts = CL_MAX_EDICTS;
530         prog->extensionstring = vm_cl_extensions;
531         prog->builtins = vm_cl_builtins;
532         prog->numbuiltins = vm_cl_numbuiltins;
533         prog->init_cmd = VM_CL_Cmd_Init;
534         prog->reset_cmd = VM_CL_Cmd_Reset;
535         prog->error_cmd = CL_VM_Error;
536
537         PRVM_LoadProgs(csqc_progname.string, cl_numrequiredfunc, cl_required_func, 0, NULL);
538
539         if(!sv.active && !cls.demoplayback && prog->filecrc != (unsigned short)csqc_progcrc.integer)
540         {
541                 Con_Printf("^1Your CSQC version differs from server's one (%i!=%i)\n", prog->filecrc, csqc_progcrc.integer);
542                 PRVM_ResetProg();
543                 CL_Disconnect();
544                 return;
545         }
546
547         if(prog->loaded)
548         {
549                 Cvar_SetValueQuick(&csqc_progcrc, prog->filecrc);
550                 Con_Printf("CSQC ^5loaded (crc=%i)\n", csqc_progcrc.integer);
551         }
552         else
553         {
554                 CL_VM_Error("CSQC ^2failed to load\n");
555                 if(!sv.active)
556                         CL_Disconnect();
557                 return;
558         }
559
560         csqc_mempool = Mem_AllocPool("CSQC", 0, NULL);
561
562         //[515]: optional fields & funcs
563         CL_VM_FindEdictFieldOffsets();
564
565         // set time
566         *prog->time = cl.time;
567         csqc_frametime = 0;
568
569         prog->globals.client->mapname = PRVM_SetEngineString(cl.worldmodel->name);
570         prog->globals.client->player_localentnum = cl.playerentity;
571
572         // call the prog init
573         PRVM_ExecuteProgram((func_t) (PRVM_ED_FindFunction(CL_F_INIT) - prog->functions), CL_F_INIT);
574
575         PRVM_End;
576         csqc_loaded = true;
577
578         csqc_sv2csqcents = Mem_Alloc(csqc_mempool, MAX_EDICTS*sizeof(unsigned short));
579         memset(csqc_sv2csqcents, 0, MAX_EDICTS*sizeof(unsigned short));
580
581         cl.csqc_vidvars.drawcrosshair = false;
582         cl.csqc_vidvars.drawenginesbar = false;
583
584         // local state
585         ent = &cl.csqcentities[0];
586         // entire entity array was cleared, so just fill in a few fields
587         ent->state_current.active = true;
588         ent->render.model = cl.worldmodel = cl.model_precache[1];
589         ent->render.scale = 1; // some of the renderer still relies on scale
590         ent->render.alpha = 1;
591         ent->render.flags = RENDER_SHADOW | RENDER_LIGHT;
592         Matrix4x4_CreateFromQuakeEntity(&ent->render.matrix, 0, 0, 0, 0, 0, 0, 1);
593         Matrix4x4_Invert_Simple(&ent->render.inversematrix, &ent->render.matrix);
594         CL_BoundingBoxForEntity(&ent->render);
595 }
596
597 void CL_VM_ShutDown (void)
598 {
599         Cmd_ClearCsqcFuncs();
600         Cvar_SetValueQuick(&csqc_progcrc, 0);
601         if(!csqc_loaded)
602                 return;
603         CSQC_BEGIN
604                 *prog->time = cl.time;
605                 PRVM_ExecuteProgram((func_t) (PRVM_ED_FindFunction(CL_F_SHUTDOWN) - prog->functions), CL_F_SHUTDOWN);
606                 PRVM_ResetProg();
607         CSQC_END
608         Con_Print("CSQC ^1unloaded\n");
609         csqc_loaded = false;
610         Mem_FreePool(&csqc_mempool);
611 }