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