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