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