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