8ba21cb727357a359f6d5146a7fc22db01a9a676
[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 = cl.movecmd[0].sequence;
71                 VectorCopy(cl.viewangles, prog->globals.client->input_angles);
72                 VectorCopy(cl.viewangles, csqc_angles);
73                 prog->globals.client->input_buttons = cl.movecmd[0].buttons;
74                 VectorSet(prog->globals.client->input_movevalues, cl.movecmd[0].forwardmove, cl.movecmd[0].sidemove, cl.movecmd[0].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 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->effects;
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         // set up the animation data
172         // self.frame is the interpolation target (new frame)
173         // self.frame1time is the animation base time for the interpolation target
174         // self.frame2 is the interpolation start (previous frame)
175         // self.frame2time is the animation base time for the interpolation start
176         e->render.frame1 = e->render.frame = ed->fields.client->frame;
177         if ((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.frame2))) e->render.frame1 = val->_float;
178         if ((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.frame1time))) e->render.frame2time = val->_float;
179         if ((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.frame2time))) e->render.frame1time = val->_float;
180         if ((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.lerpfrac))) e->render.framelerp = val->_float;
181
182         // concat the matrices to make the entity relative to its tag
183         Matrix4x4_Concat(&e->render.matrix, &tagmatrix, &matrix2);
184         // make the other useful stuff
185         CL_UpdateRenderEntity(&e->render);
186
187         if(renderflags)
188         {
189                 if(renderflags & RF_VIEWMODEL)  e->render.flags |= RENDER_VIEWMODEL;
190                 if(renderflags & RF_EXTERNALMODEL)e->render.flags |= RENDER_EXTERIORMODEL;
191                 if(renderflags & RF_DEPTHHACK)  e->render.effects |= EF_NODEPTHTEST;
192                 if(renderflags & RF_ADDITIVE)           e->render.effects |= EF_ADDITIVE;
193         }
194
195         // either fullbright or lit
196         if (!(e->render.effects & EF_FULLBRIGHT) && !r_fullbright.integer)
197                 e->render.flags |= RENDER_LIGHT;
198         // hide player shadow during intermission or nehahra movie
199         if (!(e->render.effects & (EF_NOSHADOW | EF_ADDITIVE | EF_NODEPTHTEST))
200          &&  (e->render.alpha >= 1)
201          && !(e->render.flags & RENDER_VIEWMODEL)
202          && (!(e->render.flags & RENDER_EXTERIORMODEL) || (!cl.intermission && cls.protocol != PROTOCOL_NEHAHRAMOVIE && !cl_noplayershadow.integer)))
203                 e->render.flags |= RENDER_SHADOW;
204         if (e->render.flags & RENDER_VIEWMODEL)
205                 e->render.flags |= RENDER_NOSELFSHADOW;
206
207         return true;
208 }
209
210 qboolean CL_VM_InputEvent (qboolean pressed, int key)
211 {
212         qboolean r;
213         if(!csqc_loaded)
214                 return false;
215         CSQC_BEGIN
216                 prog->globals.client->time = cl.time;
217                 PRVM_G_FLOAT(OFS_PARM0) = pressed;
218                 PRVM_G_FLOAT(OFS_PARM1) = key;
219                 PRVM_ExecuteProgram(prog->funcoffsets.CSQC_InputEvent, "QC function CSQC_InputEvent is missing");
220                 r = CSQC_RETURNVAL;
221         CSQC_END
222         return r;
223 }
224
225 qboolean CL_VM_UpdateView (void)
226 {
227 //      vec3_t oldangles;
228         if(!csqc_loaded)
229                 return false;
230         CSQC_BEGIN
231                 //VectorCopy(cl.viewangles, oldangles);
232                 prog->globals.client->time = cl.time;
233                 CSQC_SetGlobals();
234                 // clear renderable entity and light lists to prevent crashes if the
235                 // CSQC_UpdateView function does not call R_ClearScene as it should
236                 r_refdef.numentities = 0;
237                 r_refdef.numlights = 0;
238                 PRVM_ExecuteProgram(prog->funcoffsets.CSQC_UpdateView, "QC function CSQC_UpdateView is missing");
239                 //VectorCopy(oldangles, cl.viewangles);
240         CSQC_END
241         return true;
242 }
243
244 extern sizebuf_t vm_tempstringsbuf;
245 qboolean CL_VM_ConsoleCommand (const char *cmd)
246 {
247         int restorevm_tempstringsbuf_cursize;
248         qboolean r;
249         if(!csqc_loaded)
250                 return false;
251         CSQC_BEGIN
252                 prog->globals.client->time = cl.time;
253                 restorevm_tempstringsbuf_cursize = vm_tempstringsbuf.cursize;
254                 PRVM_G_INT(OFS_PARM0) = PRVM_SetTempString(cmd);
255                 PRVM_ExecuteProgram(prog->funcoffsets.CSQC_ConsoleCommand, "QC function CSQC_ConsoleCommand is missing");
256                 vm_tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize;
257                 r = CSQC_RETURNVAL;
258         CSQC_END
259         return r;
260 }
261
262 qboolean CL_VM_Parse_TempEntity (void)
263 {
264         int                     t;
265         qboolean        r = false;
266         if(!csqc_loaded)
267                 return false;
268         CSQC_BEGIN
269         if(prog->funcoffsets.CSQC_Parse_TempEntity)
270         {
271                 t = msg_readcount;
272                 prog->globals.client->time = cl.time;
273                 PRVM_ExecuteProgram(prog->funcoffsets.CSQC_Parse_TempEntity, "QC function CSQC_Parse_TempEntity is missing");
274                 r = CSQC_RETURNVAL;
275                 if(!r)
276                 {
277                         msg_readcount = t;
278                         msg_badread = false;
279                 }
280         }
281         CSQC_END
282         return r;
283 }
284
285 void CL_VM_Parse_StuffCmd (const char *msg)
286 {
287         int restorevm_tempstringsbuf_cursize;
288         if(msg[0] == 'c')
289         if(msg[1] == 's')
290         if(msg[2] == 'q')
291         if(msg[3] == 'c')
292         {
293                 // if this is setting a csqc variable, deprotect csqc_progcrc
294                 // temporarily so that it can be set by the cvar command,
295                 // and then reprotect it afterwards
296                 int crcflags = csqc_progcrc.flags;
297                 int sizeflags = csqc_progcrc.flags;
298                 csqc_progcrc.flags &= ~CVAR_READONLY;
299                 csqc_progsize.flags &= ~CVAR_READONLY;
300                 Cmd_ExecuteString (msg, src_command);
301                 csqc_progcrc.flags = crcflags;
302                 csqc_progsize.flags = sizeflags;
303                 return;
304         }
305         if(!csqc_loaded)
306         {
307                 Cbuf_AddText(msg);
308                 return;
309         }
310         CSQC_BEGIN
311         if(prog->funcoffsets.CSQC_Parse_StuffCmd)
312         {
313                 prog->globals.client->time = cl.time;
314                 restorevm_tempstringsbuf_cursize = vm_tempstringsbuf.cursize;
315                 PRVM_G_INT(OFS_PARM0) = PRVM_SetTempString(msg);
316                 PRVM_ExecuteProgram(prog->funcoffsets.CSQC_Parse_StuffCmd, "QC function CSQC_Parse_StuffCmd is missing");
317                 vm_tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize;
318         }
319         else
320                 Cbuf_AddText(msg);
321         CSQC_END
322 }
323
324 static void CL_VM_Parse_Print (const char *msg)
325 {
326         int restorevm_tempstringsbuf_cursize;
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_Print, "QC function CSQC_Parse_Print is missing");
331         vm_tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize;
332 }
333
334 void CSQC_AddPrintText (const char *msg)
335 {
336         size_t i;
337         if(!csqc_loaded)
338         {
339                 Con_Print(msg);
340                 return;
341         }
342         CSQC_BEGIN
343         if(prog->funcoffsets.CSQC_Parse_Print)
344         {
345                 // FIXME: is this bugged?
346                 i = strlen(msg)-1;
347                 if(msg[i] != '\n' && msg[i] != '\r')
348                 {
349                         if(strlen(csqc_printtextbuf)+i >= MAX_INPUTLINE)
350                         {
351                                 CL_VM_Parse_Print(csqc_printtextbuf);
352                                 csqc_printtextbuf[0] = 0;
353                         }
354                         else
355                                 strlcat(csqc_printtextbuf, msg, MAX_INPUTLINE);
356                         return;
357                 }
358                 strlcat(csqc_printtextbuf, msg, MAX_INPUTLINE);
359                 CL_VM_Parse_Print(csqc_printtextbuf);
360                 csqc_printtextbuf[0] = 0;
361         }
362         else
363                 Con_Print(msg);
364         CSQC_END
365 }
366
367 void CL_VM_Parse_CenterPrint (const char *msg)
368 {
369         int restorevm_tempstringsbuf_cursize;
370         if(!csqc_loaded)
371         {
372                 SCR_CenterPrint((char*)msg);
373                 return;
374         }
375         CSQC_BEGIN
376         if(prog->funcoffsets.CSQC_Parse_CenterPrint)
377         {
378                 prog->globals.client->time = cl.time;
379                 restorevm_tempstringsbuf_cursize = vm_tempstringsbuf.cursize;
380                 PRVM_G_INT(OFS_PARM0) = PRVM_SetTempString(msg);
381                 PRVM_ExecuteProgram(prog->funcoffsets.CSQC_Parse_CenterPrint, "QC function CSQC_Parse_CenterPrint is missing");
382                 vm_tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize;
383         }
384         else
385                 SCR_CenterPrint((char*)msg);
386         CSQC_END
387 }
388
389 float CL_VM_Event (float event)         //[515]: needed ? I'd say "YES", but don't know for what :D
390 {
391         float r = 0;
392         if(!csqc_loaded)
393                 return 0;
394         CSQC_BEGIN
395         if(prog->funcoffsets.CSQC_Event)
396         {
397                 prog->globals.client->time = cl.time;
398                 PRVM_G_FLOAT(OFS_PARM0) = event;
399                 PRVM_ExecuteProgram(prog->funcoffsets.CSQC_Event, "QC function CSQC_Event is missing");
400                 r = CSQC_RETURNVAL;
401         }
402         CSQC_END
403         return r;
404 }
405
406 void CSQC_ReadEntities (void)
407 {
408         unsigned short entnum, oldself, realentnum;
409         CSQC_BEGIN
410                 prog->globals.client->time = cl.time;
411                 oldself = prog->globals.client->self;
412                 while(1)
413                 {
414                         entnum = MSG_ReadShort();
415                         if(!entnum)
416                                 return;
417                         realentnum = entnum & 0x7FFF;
418                         prog->globals.client->self = csqc_sv2csqcents[realentnum];
419                         if(entnum & 0x8000)
420                         {
421                                 if(prog->globals.client->self)
422                                 {
423                                         PRVM_ExecuteProgram(prog->funcoffsets.CSQC_Ent_Remove, "QC function CSQC_Ent_Remove is missing");
424                                         csqc_sv2csqcents[realentnum] = 0;
425                                 }
426                                 else
427                                         Con_Printf("Smth bad happens in csqc...\n");    //[515]: never happens ?
428                         }
429                         else
430                         {
431                                 if(!prog->globals.client->self)
432                                 {
433                                         prvm_edict_t    *ed;
434                                         ed = PRVM_ED_Alloc();
435                                         ed->fields.client->entnum = realentnum;
436                                         prog->globals.client->self = csqc_sv2csqcents[realentnum] = PRVM_EDICT_TO_PROG(ed);
437                                         PRVM_G_FLOAT(OFS_PARM0) = 1;
438                                 }
439                                 else
440                                         PRVM_G_FLOAT(OFS_PARM0) = 0;
441                                 PRVM_ExecuteProgram(prog->funcoffsets.CSQC_Ent_Update, "QC function CSQC_Ent_Update is missing");
442                         }
443                 }
444                 prog->globals.client->self = oldself;
445         CSQC_END
446 }
447
448 void CL_VM_CB_BeginIncreaseEdicts(void)
449 {
450         int i;
451         prvm_edict_t *ent;
452
453         // links don't survive the transition, so unlink everything
454         for (i = 0, ent = prog->edicts;i < prog->max_edicts;i++, ent++)
455         {
456                 if (!ent->priv.server->free)
457                         World_UnlinkEdict(prog->edicts + i);
458                 memset(&ent->priv.server->areagrid, 0, sizeof(ent->priv.server->areagrid));
459         }
460         World_Clear(&cl.world);
461 }
462
463 void CL_VM_CB_EndIncreaseEdicts(void)
464 {
465         int i;
466         prvm_edict_t *ent;
467
468         // link every entity except world
469         for (i = 1, ent = prog->edicts;i < prog->max_edicts;i++, ent++)
470                 if (!ent->priv.server->free)
471                         CL_LinkEdict(ent);
472 }
473
474 void CL_VM_CB_InitEdict(prvm_edict_t *e)
475 {
476         e->priv.server->move = false; // don't move on first frame
477 }
478
479 void CL_VM_CB_FreeEdict(prvm_edict_t *ed)
480 {
481         World_UnlinkEdict(ed);
482         memset(ed->fields.client, 0, sizeof(*ed->fields.client));
483 }
484
485 void CL_VM_CB_CountEdicts(void)
486 {
487         int             i;
488         prvm_edict_t    *ent;
489         int             active = 0, models = 0, solid = 0;
490
491         for (i=0 ; i<prog->num_edicts ; i++)
492         {
493                 ent = PRVM_EDICT_NUM(i);
494                 if (ent->priv.server->free)
495                         continue;
496                 active++;
497                 if (ent->fields.client->solid)
498                         solid++;
499                 if (ent->fields.client->model)
500                         models++;
501         }
502
503         Con_Printf("num_edicts:%3i\n", prog->num_edicts);
504         Con_Printf("active    :%3i\n", active);
505         Con_Printf("view      :%3i\n", models);
506         Con_Printf("touch     :%3i\n", solid);
507 }
508
509 qboolean CL_VM_CB_LoadEdict(prvm_edict_t *ent)
510 {
511         return true;
512 }
513
514 void Cmd_ClearCsqcFuncs (void);
515
516 void CL_VM_Init (void)
517 {
518         unsigned char *csprogsdata;
519         fs_offset_t csprogsdatasize;
520         int csprogsdatacrc, requiredcrc;
521         int requiredsize;
522
523         // reset csqc_progcrc after reading it, so that changing servers doesn't
524         // expect csqc on the next server
525         requiredcrc = csqc_progcrc.integer;
526         requiredsize = csqc_progsize.integer;
527         Cvar_SetValueQuick(&csqc_progcrc, -1);
528         Cvar_SetValueQuick(&csqc_progsize, -1);
529
530         csqc_loaded = false;
531         memset(cl.csqc_model_precache, 0, sizeof(cl.csqc_model_precache));
532         memset(&cl.csqc_vidvars, true, sizeof(csqc_vidvars_t));
533
534         // if the server is not requesting a csprogs, then we're done here
535         if (requiredcrc < 0)
536                 return;
537
538         // see if the requested csprogs.dat file matches the requested crc
539         csprogsdatacrc = -1;
540         csprogsdata = FS_LoadFile(va("dlcache/%s.%i.%i", csqc_progname.string, requiredsize, requiredcrc), tempmempool, true, &csprogsdatasize);
541         if (!csprogsdata)
542                 csprogsdata = FS_LoadFile(csqc_progname.string, tempmempool, true, &csprogsdatasize);
543         if (csprogsdata)
544         {
545                 csprogsdatacrc = CRC_Block(csprogsdata, csprogsdatasize);
546                 Mem_Free(csprogsdata);
547                 if (csprogsdatacrc != requiredcrc || csprogsdatasize != requiredsize)
548                 {
549                         if (cls.demoplayback)
550                         {
551                                 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);
552                                 return;
553                         }
554                         else
555                         {
556                                 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);
557                                 CL_Disconnect();
558                                 return;
559                         }
560                 }
561         }
562         else
563         {
564                 if (requiredcrc >= 0)
565                 {
566                         if (cls.demoplayback)
567                                 Con_Printf("CL_VM_Init: demo requires CSQC, but \"%s\" wasn't found\n", csqc_progname.string);
568                         else
569                                 Con_Printf("CL_VM_Init: server requires CSQC, but \"%s\" wasn't found\n", csqc_progname.string);
570                         CL_Disconnect();
571                 }
572                 return;
573         }
574
575         PRVM_Begin;
576         PRVM_InitProg(PRVM_CLIENTPROG);
577
578         csqc_mempool = Mem_AllocPool("CSQC", 0, NULL);
579
580         // allocate the mempools
581         prog->progs_mempool = Mem_AllocPool(csqc_progname.string, 0, NULL);
582         prog->headercrc = CL_PROGHEADER_CRC;
583         prog->edictprivate_size = 0; // no private struct used
584         prog->name = CL_NAME;
585         prog->num_edicts = 1;
586         prog->max_edicts = 512;
587         prog->limit_edicts = CL_MAX_EDICTS;
588         prog->reserved_edicts = 0;
589         prog->edictprivate_size = sizeof(edict_engineprivate_t);
590         prog->extensionstring = vm_sv_extensions;
591         prog->builtins = vm_cl_builtins;
592         prog->numbuiltins = vm_cl_numbuiltins;
593         prog->begin_increase_edicts = CL_VM_CB_BeginIncreaseEdicts;
594         prog->end_increase_edicts = CL_VM_CB_EndIncreaseEdicts;
595         prog->init_edict = CL_VM_CB_InitEdict;
596         prog->free_edict = CL_VM_CB_FreeEdict;
597         prog->count_edicts = CL_VM_CB_CountEdicts;
598         prog->load_edict = CL_VM_CB_LoadEdict;
599         prog->init_cmd = VM_CL_Cmd_Init;
600         prog->reset_cmd = VM_CL_Cmd_Reset;
601         prog->error_cmd = CL_VM_Error;
602
603         PRVM_LoadProgs(csqc_progname.string, cl_numrequiredfunc, cl_required_func, 0, NULL, 0, NULL);
604
605         if (!prog->loaded)
606         {
607                 CL_VM_Error("CSQC ^2failed to load\n");
608                 if(!sv.active)
609                         CL_Disconnect();
610                 return;
611         }
612
613         Con_Printf("CSQC ^5loaded (crc=%i, size=%i)\n", csprogsdatacrc, (int)csprogsdatasize);
614
615         // check if OP_STATE animation is possible in this dat file
616         if (prog->fieldoffsets.nextthink >= 0 && prog->fieldoffsets.frame >= 0 && prog->fieldoffsets.think >= 0 && prog->globaloffsets.self >= 0)
617                 prog->flag |= PRVM_OP_STATE;
618
619         //[515]: optional fields & funcs
620         if(prog->funcoffsets.CSQC_Parse_Print)
621         {
622                 csqc_printtextbuf = (char *)Mem_Alloc(csqc_mempool, MAX_INPUTLINE);
623                 csqc_printtextbuf[0] = 0;
624         }
625
626         if (cl.worldmodel)
627         {
628                 VectorCopy(cl.worldmodel->normalmins, cl.world.areagrid_mins);
629                 VectorCopy(cl.worldmodel->normalmaxs, cl.world.areagrid_maxs);
630         }
631         else
632         {
633                 VectorSet(cl.world.areagrid_mins, -4096, -4096, -4096);
634                 VectorSet(cl.world.areagrid_maxs, 4096, 4096, 4096);
635         }
636         World_Clear(&cl.world);
637
638         // set time
639         prog->globals.client->time = cl.time;
640         csqc_frametime = 0;
641
642         prog->globals.client->mapname = PRVM_SetEngineString(cl.worldmodel->name);
643         prog->globals.client->player_localentnum = cl.playerentity;
644
645         // call the prog init
646         PRVM_ExecuteProgram(prog->funcoffsets.CSQC_Init, "QC function CSQC_Init is missing");
647
648         PRVM_End;
649         csqc_loaded = true;
650
651         csqc_sv2csqcents = (unsigned short *)Mem_Alloc(csqc_mempool, MAX_EDICTS*sizeof(unsigned short));
652         memset(csqc_sv2csqcents, 0, MAX_EDICTS*sizeof(unsigned short));
653
654         cl.csqc_vidvars.drawcrosshair = false;
655         cl.csqc_vidvars.drawenginesbar = false;
656 }
657
658 void CL_VM_ShutDown (void)
659 {
660         Cmd_ClearCsqcFuncs();
661         Cvar_SetValueQuick(&csqc_progcrc, -1);
662         Cvar_SetValueQuick(&csqc_progsize, -1);
663         if(!csqc_loaded)
664                 return;
665         CSQC_BEGIN
666                 prog->globals.client->time = cl.time;
667                 PRVM_ExecuteProgram(prog->funcoffsets.CSQC_Shutdown, "QC function CSQC_Shutdown is missing");
668                 PRVM_ResetProg();
669         CSQC_END
670         Con_Print("CSQC ^1unloaded\n");
671         csqc_loaded = false;
672         Mem_FreePool(&csqc_mempool);
673 }