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