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