changed all references to entity_render_t->frame to frame2 to eliminate
[xonotic/darkplaces.git] / csprogs.c
1 #include "quakedef.h"
2 #include "progsvm.h"
3 #include "clprogdefs.h"
4 #include "csprogs.h"
5 #include "cl_collision.h"
6
7 //============================================================================
8 // Client prog handling
9 //[515]: omg !!! optimize it ! a lot of hacks here and there also :P
10
11 #define CSQC_RETURNVAL  prog->globals.generic[OFS_RETURN]
12 #define CSQC_BEGIN              csqc_tmpprog=prog;prog=0;PRVM_SetProg(PRVM_CLIENTPROG);
13 #define CSQC_END                prog=csqc_tmpprog;
14 static prvm_prog_t *csqc_tmpprog;
15
16 //[515]: these are required funcs
17 static char *cl_required_func[] =
18 {
19         "CSQC_Init",
20         "CSQC_InputEvent",
21         "CSQC_UpdateView",
22         "CSQC_ConsoleCommand",
23         "CSQC_Shutdown"
24 };
25
26 static int cl_numrequiredfunc = sizeof(cl_required_func) / sizeof(char*);
27
28 void CL_VM_Error (const char *format, ...) DP_FUNC_PRINTF(1);
29 void CL_VM_Error (const char *format, ...)      //[515]: hope it will be never executed =)
30 {
31         char errorstring[4096];
32         va_list argptr;
33
34         va_start (argptr, format);
35         dpvsnprintf (errorstring, sizeof(errorstring), format, argptr);
36         va_end (argptr);
37 //      Con_Printf( "CL_VM_Error: %s\n", errorstring );
38
39         PRVM_Crash();
40         cl.csqc_loaded = false;
41
42         Cvar_SetValueQuick(&csqc_progcrc, -1);
43         Cvar_SetValueQuick(&csqc_progsize, -1);
44
45 //      Host_AbortCurrentFrame();       //[515]: hmmm... if server says it needs csqc then client MUST disconnect
46         Host_Error(va("CL_VM_Error: %s", errorstring));
47 }
48
49 //[515]: set globals before calling R_UpdateView, WEIRD CRAP
50 static void CSQC_SetGlobals (void)
51 {
52         CSQC_BEGIN
53                 prog->globals.client->time = cl.time;
54                 prog->globals.client->frametime = max(0, cl.time - cl.oldtime);
55                 prog->globals.client->servercommandframe = cls.servermovesequence;
56                 prog->globals.client->clientcommandframe = cl.movecmd[0].sequence;
57                 VectorCopy(cl.viewangles, prog->globals.client->input_angles);
58                 VectorCopy(cl.viewangles, cl.csqc_angles);
59                 prog->globals.client->input_buttons = cl.movecmd[0].buttons;
60                 VectorSet(prog->globals.client->input_movevalues, cl.movecmd[0].forwardmove, cl.movecmd[0].sidemove, cl.movecmd[0].upmove);
61                 //VectorCopy(cl.movement_origin, cl.csqc_origin);
62                 Matrix4x4_OriginFromMatrix(&cl.entities[cl.viewentity].render.matrix, cl.csqc_origin);
63                 VectorCopy(cl.csqc_origin, prog->globals.client->pmove_org);
64                 prog->globals.client->maxclients = cl.maxclients;
65                 //VectorCopy(cl.movement_velocity, prog->globals.client->pmove_vel);
66                 VectorCopy(cl.velocity, prog->globals.client->pmove_vel);
67         CSQC_END
68 }
69
70 void CSQC_Predraw (prvm_edict_t *ed)
71 {
72         int b;
73         if(!ed->fields.client->predraw)
74                 return;
75         b = prog->globals.client->self;
76         prog->globals.client->self = PRVM_EDICT_TO_PROG(ed);
77         PRVM_ExecuteProgram(ed->fields.client->predraw, "CSQC_Predraw: NULL function\n");
78         prog->globals.client->self = b;
79 }
80
81 void CSQC_Think (prvm_edict_t *ed)
82 {
83         int b;
84         if(ed->fields.client->think)
85         if(ed->fields.client->nextthink && ed->fields.client->nextthink <= prog->globals.client->time)
86         {
87                 ed->fields.client->nextthink = 0;
88                 b = prog->globals.client->self;
89                 prog->globals.client->self = PRVM_EDICT_TO_PROG(ed);
90                 PRVM_ExecuteProgram(ed->fields.client->think, "CSQC_Think: NULL function\n");
91                 prog->globals.client->self = b;
92         }
93 }
94
95 extern cvar_t cl_noplayershadow;
96 qboolean CSQC_AddRenderEdict(prvm_edict_t *ed)
97 {
98         int renderflags;
99         float scale;
100         prvm_eval_t *val;
101         entity_t *e;
102         model_t *model;
103         matrix4x4_t tagmatrix, matrix2;
104
105         model = CL_GetModelFromEdict(ed);
106         if (!model)
107                 return false;
108
109         e = CL_NewTempEntity();
110         if (!e)
111                 return false;
112
113         e->render.model = model;
114         e->render.colormap = (int)ed->fields.client->colormap;
115         e->render.skinnum = (int)ed->fields.client->skin;
116         e->render.effects |= e->render.model->effects;
117         scale = 1;
118         renderflags = 0;
119         if((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.renderflags)) && val->_float)     renderflags = (int)val->_float;
120         if((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.alpha)) && val->_float)           e->render.alpha = val->_float;
121         if((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.scale)) && val->_float)           e->render.scale = scale = val->_float;
122         if((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.colormod)) && VectorLength2(val->vector)) VectorCopy(val->vector, e->render.colormod);
123         if((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.effects)) && val->_float) e->render.effects |= (int)val->_float;
124         if((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.tag_entity)) && val->edict)
125         {
126                 int tagentity;
127                 int tagindex = 0;
128                 tagentity = val->edict;
129                 if((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.tag_index)) && val->_float)
130                         tagindex = (int)val->_float;
131                 // FIXME: calculate tag matrix
132                 Matrix4x4_CreateIdentity(&tagmatrix);
133         }
134         else
135                 Matrix4x4_CreateIdentity(&tagmatrix);
136
137         if (renderflags & RF_USEAXIS)
138         {
139                 vec3_t left;
140                 VectorNegate(prog->globals.client->v_right, left);
141                 Matrix4x4_FromVectors(&matrix2, prog->globals.client->v_forward, left, prog->globals.client->v_up, ed->fields.client->origin);
142                 Matrix4x4_Scale(&matrix2, scale, 1);
143         }
144         else
145         {
146                 vec3_t angles;
147                 VectorCopy(ed->fields.client->angles, angles);
148                 // if model is alias, reverse pitch direction
149                 if (e->render.model->type == mod_alias)
150                         angles[0] = -angles[0];
151
152                 // set up the render matrix
153                 Matrix4x4_CreateFromQuakeEntity(&matrix2, ed->fields.client->origin[0], ed->fields.client->origin[1], ed->fields.client->origin[2], angles[0], angles[1], angles[2], scale);
154         }
155
156         // set up the animation data
157         // self.frame is the interpolation target (new frame)
158         // self.frame1time is the animation base time for the interpolation target
159         // self.frame2 is the interpolation start (previous frame)
160         // self.frame2time is the animation base time for the interpolation start
161         e->render.frame1 = e->render.frame2 = ed->fields.client->frame;
162         if ((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.frame2))) e->render.frame2 = val->_float;
163         if ((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.frame1time))) e->render.frame2time = val->_float;
164         if ((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.frame2time))) e->render.frame1time = val->_float;
165         if ((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.lerpfrac))) e->render.framelerp = val->_float;
166
167         // concat the matrices to make the entity relative to its tag
168         Matrix4x4_Concat(&e->render.matrix, &tagmatrix, &matrix2);
169         // make the other useful stuff
170         CL_UpdateRenderEntity(&e->render);
171
172         if(renderflags)
173         {
174                 if(renderflags & RF_VIEWMODEL)  e->render.flags |= RENDER_VIEWMODEL;
175                 if(renderflags & RF_EXTERNALMODEL)e->render.flags |= RENDER_EXTERIORMODEL;
176                 if(renderflags & RF_DEPTHHACK)  e->render.effects |= EF_NODEPTHTEST;
177                 if(renderflags & RF_ADDITIVE)           e->render.effects |= EF_ADDITIVE;
178         }
179
180         // either fullbright or lit
181         if (!(e->render.effects & EF_FULLBRIGHT) && !r_fullbright.integer)
182                 e->render.flags |= RENDER_LIGHT;
183         // hide player shadow during intermission or nehahra movie
184         if (!(e->render.effects & (EF_NOSHADOW | EF_ADDITIVE | EF_NODEPTHTEST))
185          &&  (e->render.alpha >= 1)
186          && !(e->render.flags & RENDER_VIEWMODEL)
187          && (!(e->render.flags & RENDER_EXTERIORMODEL) || (!cl.intermission && cls.protocol != PROTOCOL_NEHAHRAMOVIE && !cl_noplayershadow.integer)))
188                 e->render.flags |= RENDER_SHADOW;
189         if (e->render.flags & RENDER_VIEWMODEL)
190                 e->render.flags |= RENDER_NOSELFSHADOW;
191
192         return true;
193 }
194
195 qboolean CL_VM_InputEvent (qboolean pressed, int key)
196 {
197         qboolean r;
198         if(!cl.csqc_loaded)
199                 return false;
200         CSQC_BEGIN
201                 prog->globals.client->time = cl.time;
202                 PRVM_G_FLOAT(OFS_PARM0) = pressed;
203                 PRVM_G_FLOAT(OFS_PARM1) = key;
204                 PRVM_ExecuteProgram(prog->funcoffsets.CSQC_InputEvent, "QC function CSQC_InputEvent is missing");
205                 r = CSQC_RETURNVAL;
206         CSQC_END
207         return r;
208 }
209
210 qboolean CL_VM_UpdateView (void)
211 {
212 //      vec3_t oldangles;
213         if(!cl.csqc_loaded)
214                 return false;
215         CSQC_BEGIN
216                 //VectorCopy(cl.viewangles, oldangles);
217                 prog->globals.client->time = cl.time;
218                 CSQC_SetGlobals();
219                 // clear renderable entity and light lists to prevent crashes if the
220                 // CSQC_UpdateView function does not call R_ClearScene as it should
221                 r_refdef.numentities = 0;
222                 r_refdef.numlights = 0;
223                 PRVM_ExecuteProgram(prog->funcoffsets.CSQC_UpdateView, "QC function CSQC_UpdateView is missing");
224                 //VectorCopy(oldangles, cl.viewangles);
225         CSQC_END
226         return true;
227 }
228
229 extern sizebuf_t vm_tempstringsbuf;
230 qboolean CL_VM_ConsoleCommand (const char *cmd)
231 {
232         int restorevm_tempstringsbuf_cursize;
233         qboolean r;
234         if(!cl.csqc_loaded)
235                 return false;
236         CSQC_BEGIN
237                 prog->globals.client->time = cl.time;
238                 restorevm_tempstringsbuf_cursize = vm_tempstringsbuf.cursize;
239                 PRVM_G_INT(OFS_PARM0) = PRVM_SetTempString(cmd);
240                 PRVM_ExecuteProgram(prog->funcoffsets.CSQC_ConsoleCommand, "QC function CSQC_ConsoleCommand is missing");
241                 vm_tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize;
242                 r = CSQC_RETURNVAL;
243         CSQC_END
244         return r;
245 }
246
247 qboolean CL_VM_Parse_TempEntity (void)
248 {
249         int                     t;
250         qboolean        r = false;
251         if(!cl.csqc_loaded)
252                 return false;
253         CSQC_BEGIN
254         if(prog->funcoffsets.CSQC_Parse_TempEntity)
255         {
256                 t = msg_readcount;
257                 prog->globals.client->time = cl.time;
258                 PRVM_ExecuteProgram(prog->funcoffsets.CSQC_Parse_TempEntity, "QC function CSQC_Parse_TempEntity is missing");
259                 r = CSQC_RETURNVAL;
260                 if(!r)
261                 {
262                         msg_readcount = t;
263                         msg_badread = false;
264                 }
265         }
266         CSQC_END
267         return r;
268 }
269
270 void CL_VM_Parse_StuffCmd (const char *msg)
271 {
272         int restorevm_tempstringsbuf_cursize;
273         if(msg[0] == 'c')
274         if(msg[1] == 's')
275         if(msg[2] == 'q')
276         if(msg[3] == 'c')
277         {
278                 // if this is setting a csqc variable, deprotect csqc_progcrc
279                 // temporarily so that it can be set by the cvar command,
280                 // and then reprotect it afterwards
281                 int crcflags = csqc_progcrc.flags;
282                 int sizeflags = csqc_progcrc.flags;
283                 csqc_progcrc.flags &= ~CVAR_READONLY;
284                 csqc_progsize.flags &= ~CVAR_READONLY;
285                 Cmd_ExecuteString (msg, src_command);
286                 csqc_progcrc.flags = crcflags;
287                 csqc_progsize.flags = sizeflags;
288                 return;
289         }
290         if(!cl.csqc_loaded)
291         {
292                 Cbuf_AddText(msg);
293                 return;
294         }
295         CSQC_BEGIN
296         if(prog->funcoffsets.CSQC_Parse_StuffCmd)
297         {
298                 prog->globals.client->time = cl.time;
299                 restorevm_tempstringsbuf_cursize = vm_tempstringsbuf.cursize;
300                 PRVM_G_INT(OFS_PARM0) = PRVM_SetTempString(msg);
301                 PRVM_ExecuteProgram(prog->funcoffsets.CSQC_Parse_StuffCmd, "QC function CSQC_Parse_StuffCmd is missing");
302                 vm_tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize;
303         }
304         else
305                 Cbuf_AddText(msg);
306         CSQC_END
307 }
308
309 static void CL_VM_Parse_Print (const char *msg)
310 {
311         int restorevm_tempstringsbuf_cursize;
312         prog->globals.client->time = cl.time;
313         restorevm_tempstringsbuf_cursize = vm_tempstringsbuf.cursize;
314         PRVM_G_INT(OFS_PARM0) = PRVM_SetTempString(msg);
315         PRVM_ExecuteProgram(prog->funcoffsets.CSQC_Parse_Print, "QC function CSQC_Parse_Print is missing");
316         vm_tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize;
317 }
318
319 void CSQC_AddPrintText (const char *msg)
320 {
321         size_t i;
322         if(!cl.csqc_loaded)
323         {
324                 Con_Print(msg);
325                 return;
326         }
327         CSQC_BEGIN
328         if(prog->funcoffsets.CSQC_Parse_Print)
329         {
330                 // FIXME: is this bugged?
331                 i = strlen(msg)-1;
332                 if(msg[i] != '\n' && msg[i] != '\r')
333                 {
334                         if(strlen(cl.csqc_printtextbuf)+i >= MAX_INPUTLINE)
335                         {
336                                 CL_VM_Parse_Print(cl.csqc_printtextbuf);
337                                 cl.csqc_printtextbuf[0] = 0;
338                         }
339                         else
340                                 strlcat(cl.csqc_printtextbuf, msg, MAX_INPUTLINE);
341                         return;
342                 }
343                 strlcat(cl.csqc_printtextbuf, msg, MAX_INPUTLINE);
344                 CL_VM_Parse_Print(cl.csqc_printtextbuf);
345                 cl.csqc_printtextbuf[0] = 0;
346         }
347         else
348                 Con_Print(msg);
349         CSQC_END
350 }
351
352 void CL_VM_Parse_CenterPrint (const char *msg)
353 {
354         int restorevm_tempstringsbuf_cursize;
355         if(!cl.csqc_loaded)
356         {
357                 SCR_CenterPrint((char*)msg);
358                 return;
359         }
360         CSQC_BEGIN
361         if(prog->funcoffsets.CSQC_Parse_CenterPrint)
362         {
363                 prog->globals.client->time = cl.time;
364                 restorevm_tempstringsbuf_cursize = vm_tempstringsbuf.cursize;
365                 PRVM_G_INT(OFS_PARM0) = PRVM_SetTempString(msg);
366                 PRVM_ExecuteProgram(prog->funcoffsets.CSQC_Parse_CenterPrint, "QC function CSQC_Parse_CenterPrint is missing");
367                 vm_tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize;
368         }
369         else
370                 SCR_CenterPrint((char*)msg);
371         CSQC_END
372 }
373
374 float CL_VM_Event (float event)         //[515]: needed ? I'd say "YES", but don't know for what :D
375 {
376         float r = 0;
377         if(!cl.csqc_loaded)
378                 return 0;
379         CSQC_BEGIN
380         if(prog->funcoffsets.CSQC_Event)
381         {
382                 prog->globals.client->time = cl.time;
383                 PRVM_G_FLOAT(OFS_PARM0) = event;
384                 PRVM_ExecuteProgram(prog->funcoffsets.CSQC_Event, "QC function CSQC_Event is missing");
385                 r = CSQC_RETURNVAL;
386         }
387         CSQC_END
388         return r;
389 }
390
391 void CSQC_ReadEntities (void)
392 {
393         unsigned short entnum, oldself, realentnum;
394         CSQC_BEGIN
395                 prog->globals.client->time = cl.time;
396                 oldself = prog->globals.client->self;
397                 while(1)
398                 {
399                         entnum = MSG_ReadShort();
400                         if(!entnum || msg_badread)
401                                 return;
402                         realentnum = entnum & 0x7FFF;
403                         prog->globals.client->self = cl.csqc_server2csqcentitynumber[realentnum];
404                         if(entnum & 0x8000)
405                         {
406                                 if(prog->globals.client->self)
407                                 {
408                                         PRVM_ExecuteProgram(prog->funcoffsets.CSQC_Ent_Remove, "QC function CSQC_Ent_Remove is missing");
409                                         cl.csqc_server2csqcentitynumber[realentnum] = 0;
410                                 }
411                                 else
412                                         Con_Printf("Smth bad happens in csqc...\n");    //[515]: never happens ?
413                         }
414                         else
415                         {
416                                 if(!prog->globals.client->self)
417                                 {
418                                         prvm_edict_t    *ed;
419                                         ed = PRVM_ED_Alloc();
420                                         ed->fields.client->entnum = realentnum;
421                                         prog->globals.client->self = cl.csqc_server2csqcentitynumber[realentnum] = PRVM_EDICT_TO_PROG(ed);
422                                         PRVM_G_FLOAT(OFS_PARM0) = 1;
423                                 }
424                                 else
425                                         PRVM_G_FLOAT(OFS_PARM0) = 0;
426                                 PRVM_ExecuteProgram(prog->funcoffsets.CSQC_Ent_Update, "QC function CSQC_Ent_Update is missing");
427                         }
428                 }
429                 prog->globals.client->self = oldself;
430         CSQC_END
431 }
432
433 void CL_VM_CB_BeginIncreaseEdicts(void)
434 {
435         int i;
436         prvm_edict_t *ent;
437
438         // links don't survive the transition, so unlink everything
439         for (i = 0, ent = prog->edicts;i < prog->max_edicts;i++, ent++)
440         {
441                 if (!ent->priv.server->free)
442                         World_UnlinkEdict(prog->edicts + i);
443                 memset(&ent->priv.server->areagrid, 0, sizeof(ent->priv.server->areagrid));
444         }
445         World_Clear(&cl.world);
446 }
447
448 void CL_VM_CB_EndIncreaseEdicts(void)
449 {
450         int i;
451         prvm_edict_t *ent;
452
453         // link every entity except world
454         for (i = 1, ent = prog->edicts;i < prog->max_edicts;i++, ent++)
455                 if (!ent->priv.server->free)
456                         CL_LinkEdict(ent);
457 }
458
459 void CL_VM_CB_InitEdict(prvm_edict_t *e)
460 {
461         e->priv.server->move = false; // don't move on first frame
462 }
463
464 void CL_VM_CB_FreeEdict(prvm_edict_t *ed)
465 {
466         World_UnlinkEdict(ed);
467         memset(ed->fields.client, 0, sizeof(*ed->fields.client));
468 }
469
470 void CL_VM_CB_CountEdicts(void)
471 {
472         int             i;
473         prvm_edict_t    *ent;
474         int             active = 0, models = 0, solid = 0;
475
476         for (i=0 ; i<prog->num_edicts ; i++)
477         {
478                 ent = PRVM_EDICT_NUM(i);
479                 if (ent->priv.server->free)
480                         continue;
481                 active++;
482                 if (ent->fields.client->solid)
483                         solid++;
484                 if (ent->fields.client->model)
485                         models++;
486         }
487
488         Con_Printf("num_edicts:%3i\n", prog->num_edicts);
489         Con_Printf("active    :%3i\n", active);
490         Con_Printf("view      :%3i\n", models);
491         Con_Printf("touch     :%3i\n", solid);
492 }
493
494 qboolean CL_VM_CB_LoadEdict(prvm_edict_t *ent)
495 {
496         return true;
497 }
498
499 void Cmd_ClearCsqcFuncs (void);
500
501 void CL_VM_Init (void)
502 {
503         unsigned char *csprogsdata;
504         fs_offset_t csprogsdatasize;
505         int csprogsdatacrc, requiredcrc;
506         int requiredsize;
507
508         // reset csqc_progcrc after reading it, so that changing servers doesn't
509         // expect csqc on the next server
510         requiredcrc = csqc_progcrc.integer;
511         requiredsize = csqc_progsize.integer;
512         Cvar_SetValueQuick(&csqc_progcrc, -1);
513         Cvar_SetValueQuick(&csqc_progsize, -1);
514
515         // if the server is not requesting a csprogs, then we're done here
516         if (requiredcrc < 0)
517                 return;
518
519         // see if the requested csprogs.dat file matches the requested crc
520         csprogsdatacrc = -1;
521         csprogsdata = FS_LoadFile(va("dlcache/%s.%i.%i", csqc_progname.string, requiredsize, requiredcrc), tempmempool, true, &csprogsdatasize);
522         if (!csprogsdata)
523                 csprogsdata = FS_LoadFile(csqc_progname.string, tempmempool, true, &csprogsdatasize);
524         if (csprogsdata)
525         {
526                 csprogsdatacrc = CRC_Block(csprogsdata, csprogsdatasize);
527                 Mem_Free(csprogsdata);
528                 if (csprogsdatacrc != requiredcrc || csprogsdatasize != requiredsize)
529                 {
530                         if (cls.demoplayback)
531                         {
532                                 Con_Printf("^1Warning: Your %s is not the same version as the demo was recorded with (CRC/size are %i/%i but should be %i/%i)\n", csqc_progname.string, csprogsdatacrc, (int)csprogsdatasize, requiredcrc, requiredsize);
533                                 return;
534                         }
535                         else
536                         {
537                                 Con_Printf("^1Your %s is not the same version as the server (CRC is %i/%i but should be %i/%i)\n", csqc_progname.string, csprogsdatacrc, (int)csprogsdatasize, requiredcrc, requiredsize);
538                                 CL_Disconnect();
539                                 return;
540                         }
541                 }
542         }
543         else
544         {
545                 if (requiredcrc >= 0)
546                 {
547                         if (cls.demoplayback)
548                                 Con_Printf("CL_VM_Init: demo requires CSQC, but \"%s\" wasn't found\n", csqc_progname.string);
549                         else
550                                 Con_Printf("CL_VM_Init: server requires CSQC, but \"%s\" wasn't found\n", csqc_progname.string);
551                         CL_Disconnect();
552                 }
553                 return;
554         }
555
556         PRVM_Begin;
557         PRVM_InitProg(PRVM_CLIENTPROG);
558
559         // allocate the mempools
560         prog->progs_mempool = Mem_AllocPool(csqc_progname.string, 0, NULL);
561         prog->headercrc = CL_PROGHEADER_CRC;
562         prog->edictprivate_size = 0; // no private struct used
563         prog->name = CL_NAME;
564         prog->num_edicts = 1;
565         prog->max_edicts = 512;
566         prog->limit_edicts = CL_MAX_EDICTS;
567         prog->reserved_edicts = 0;
568         prog->edictprivate_size = sizeof(edict_engineprivate_t);
569         prog->extensionstring = vm_sv_extensions;
570         prog->builtins = vm_cl_builtins;
571         prog->numbuiltins = vm_cl_numbuiltins;
572         prog->begin_increase_edicts = CL_VM_CB_BeginIncreaseEdicts;
573         prog->end_increase_edicts = CL_VM_CB_EndIncreaseEdicts;
574         prog->init_edict = CL_VM_CB_InitEdict;
575         prog->free_edict = CL_VM_CB_FreeEdict;
576         prog->count_edicts = CL_VM_CB_CountEdicts;
577         prog->load_edict = CL_VM_CB_LoadEdict;
578         prog->init_cmd = VM_CL_Cmd_Init;
579         prog->reset_cmd = VM_CL_Cmd_Reset;
580         prog->error_cmd = CL_VM_Error;
581
582         PRVM_LoadProgs(csqc_progname.string, cl_numrequiredfunc, cl_required_func, 0, NULL, 0, NULL);
583
584         if (!prog->loaded)
585         {
586                 CL_VM_Error("CSQC ^2failed to load\n");
587                 if(!sv.active)
588                         CL_Disconnect();
589                 return;
590         }
591
592         Con_Printf("CSQC ^5loaded (crc=%i, size=%i)\n", csprogsdatacrc, (int)csprogsdatasize);
593
594         // check if OP_STATE animation is possible in this dat file
595         if (prog->fieldoffsets.nextthink >= 0 && prog->fieldoffsets.frame >= 0 && prog->fieldoffsets.think >= 0 && prog->globaloffsets.self >= 0)
596                 prog->flag |= PRVM_OP_STATE;
597
598         // set time
599         prog->globals.client->time = cl.time;
600
601         prog->globals.client->mapname = PRVM_SetEngineString(cl.worldmodel->name);
602         prog->globals.client->player_localentnum = cl.playerentity;
603
604         // call the prog init
605         PRVM_ExecuteProgram(prog->funcoffsets.CSQC_Init, "QC function CSQC_Init is missing");
606
607         PRVM_End;
608         cl.csqc_loaded = true;
609
610         cl.csqc_vidvars.drawcrosshair = false;
611         cl.csqc_vidvars.drawenginesbar = false;
612 }
613
614 void CL_VM_ShutDown (void)
615 {
616         Cmd_ClearCsqcFuncs();
617         Cvar_SetValueQuick(&csqc_progcrc, -1);
618         Cvar_SetValueQuick(&csqc_progsize, -1);
619         if(!cl.csqc_loaded)
620                 return;
621         CSQC_BEGIN
622                 prog->globals.client->time = cl.time;
623                 PRVM_ExecuteProgram(prog->funcoffsets.CSQC_Shutdown, "QC function CSQC_Shutdown is missing");
624                 PRVM_ResetProg();
625         CSQC_END
626         Con_Print("CSQC ^1unloaded\n");
627         cl.csqc_loaded = false;
628 }