]> git.xonotic.org Git - xonotic/darkplaces.git/blob - csprogs.c
sys: work around incomplete POSIX support in MacOS
[xonotic/darkplaces.git] / csprogs.c
1 /*
2 Copyright (C) 2006-2021 DarkPlaces contributors
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 */
20
21 #include "quakedef.h"
22 #include "progsvm.h"
23 #include "clprogdefs.h"
24 #include "csprogs.h"
25 #include "cl_collision.h"
26 #include "snd_main.h"
27 #include "clvm_cmds.h"
28 #include "prvm_cmds.h"
29
30 //============================================================================
31 // Client prog handling
32 //[515]: omg !!! optimize it ! a lot of hacks here and there also :P
33
34 #define CSQC_RETURNVAL  prog->globals.fp[OFS_RETURN]
35
36 void CL_VM_PreventInformationLeaks(void)
37 {
38         prvm_prog_t *prog = CLVM_prog;
39
40         if(!prog->loaded)
41                 return;
42
43         VM_ClearTraceGlobals(prog);
44         PRVM_clientglobalfloat(trace_networkentity) = 0;
45 }
46
47
48 /** Previous DP versions declined to load csprogs if it lacked any of:
49  * CSQC_Init, CSQC_InputEvent, CSQC_UpdateView, CSQC_ConsoleCommand
50  * whereas in FTE and QSS-based engines the minimum is either CSQC_UpdateView
51  * or CSQC_DrawHud (only called in CSQC_SIMPLE aka hud-only mode)
52  * and the other funcs are optional, so we now behave the same here.
53  */
54 static void CL_CheckRequiredFuncs(prvm_prog_t *prog, const char *filename)
55 {
56         if (PRVM_ED_FindFunction(prog, "CSQC_UpdateView"))
57                 return;
58         else if (PRVM_ED_FindFunction(prog, "CSQC_DrawHud"))
59                 prog->flag |= PRVM_CSQC_SIMPLE;
60         else
61                 prog->error_cmd("%s: no CSQC_UpdateView (EXT_CSQC) or CSQC_DrawHud (CSQC_SIMPLE) function found in %s", prog->name, filename);
62 }
63
64 #define CL_REQFIELDS (sizeof(cl_reqfields) / sizeof(prvm_required_field_t))
65
66 prvm_required_field_t cl_reqfields[] =
67 {
68 #define PRVM_DECLARE_serverglobalfloat(x)
69 #define PRVM_DECLARE_serverglobalvector(x)
70 #define PRVM_DECLARE_serverglobalstring(x)
71 #define PRVM_DECLARE_serverglobaledict(x)
72 #define PRVM_DECLARE_serverglobalfunction(x)
73 #define PRVM_DECLARE_clientglobalfloat(x)
74 #define PRVM_DECLARE_clientglobalvector(x)
75 #define PRVM_DECLARE_clientglobalstring(x)
76 #define PRVM_DECLARE_clientglobaledict(x)
77 #define PRVM_DECLARE_clientglobalfunction(x)
78 #define PRVM_DECLARE_menuglobalfloat(x)
79 #define PRVM_DECLARE_menuglobalvector(x)
80 #define PRVM_DECLARE_menuglobalstring(x)
81 #define PRVM_DECLARE_menuglobaledict(x)
82 #define PRVM_DECLARE_menuglobalfunction(x)
83 #define PRVM_DECLARE_serverfieldfloat(x)
84 #define PRVM_DECLARE_serverfieldvector(x)
85 #define PRVM_DECLARE_serverfieldstring(x)
86 #define PRVM_DECLARE_serverfieldedict(x)
87 #define PRVM_DECLARE_serverfieldfunction(x)
88 #define PRVM_DECLARE_clientfieldfloat(x) {ev_float, #x },
89 #define PRVM_DECLARE_clientfieldvector(x) {ev_vector, #x },
90 #define PRVM_DECLARE_clientfieldstring(x) {ev_string, #x },
91 #define PRVM_DECLARE_clientfieldedict(x) {ev_entity, #x },
92 #define PRVM_DECLARE_clientfieldfunction(x) {ev_function, #x },
93 #define PRVM_DECLARE_menufieldfloat(x)
94 #define PRVM_DECLARE_menufieldvector(x)
95 #define PRVM_DECLARE_menufieldstring(x)
96 #define PRVM_DECLARE_menufieldedict(x)
97 #define PRVM_DECLARE_menufieldfunction(x)
98 #define PRVM_DECLARE_serverfunction(x)
99 #define PRVM_DECLARE_clientfunction(x)
100 #define PRVM_DECLARE_menufunction(x)
101 #define PRVM_DECLARE_field(x)
102 #define PRVM_DECLARE_global(x)
103 #define PRVM_DECLARE_function(x)
104 #include "prvm_offsets.h"
105 #undef PRVM_DECLARE_serverglobalfloat
106 #undef PRVM_DECLARE_serverglobalvector
107 #undef PRVM_DECLARE_serverglobalstring
108 #undef PRVM_DECLARE_serverglobaledict
109 #undef PRVM_DECLARE_serverglobalfunction
110 #undef PRVM_DECLARE_clientglobalfloat
111 #undef PRVM_DECLARE_clientglobalvector
112 #undef PRVM_DECLARE_clientglobalstring
113 #undef PRVM_DECLARE_clientglobaledict
114 #undef PRVM_DECLARE_clientglobalfunction
115 #undef PRVM_DECLARE_menuglobalfloat
116 #undef PRVM_DECLARE_menuglobalvector
117 #undef PRVM_DECLARE_menuglobalstring
118 #undef PRVM_DECLARE_menuglobaledict
119 #undef PRVM_DECLARE_menuglobalfunction
120 #undef PRVM_DECLARE_serverfieldfloat
121 #undef PRVM_DECLARE_serverfieldvector
122 #undef PRVM_DECLARE_serverfieldstring
123 #undef PRVM_DECLARE_serverfieldedict
124 #undef PRVM_DECLARE_serverfieldfunction
125 #undef PRVM_DECLARE_clientfieldfloat
126 #undef PRVM_DECLARE_clientfieldvector
127 #undef PRVM_DECLARE_clientfieldstring
128 #undef PRVM_DECLARE_clientfieldedict
129 #undef PRVM_DECLARE_clientfieldfunction
130 #undef PRVM_DECLARE_menufieldfloat
131 #undef PRVM_DECLARE_menufieldvector
132 #undef PRVM_DECLARE_menufieldstring
133 #undef PRVM_DECLARE_menufieldedict
134 #undef PRVM_DECLARE_menufieldfunction
135 #undef PRVM_DECLARE_serverfunction
136 #undef PRVM_DECLARE_clientfunction
137 #undef PRVM_DECLARE_menufunction
138 #undef PRVM_DECLARE_field
139 #undef PRVM_DECLARE_global
140 #undef PRVM_DECLARE_function
141 };
142
143 #define CL_REQGLOBALS (sizeof(cl_reqglobals) / sizeof(prvm_required_field_t))
144
145 prvm_required_field_t cl_reqglobals[] =
146 {
147 #define PRVM_DECLARE_serverglobalfloat(x)
148 #define PRVM_DECLARE_serverglobalvector(x)
149 #define PRVM_DECLARE_serverglobalstring(x)
150 #define PRVM_DECLARE_serverglobaledict(x)
151 #define PRVM_DECLARE_serverglobalfunction(x)
152 #define PRVM_DECLARE_clientglobalfloat(x) {ev_float, #x},
153 #define PRVM_DECLARE_clientglobalvector(x) {ev_vector, #x},
154 #define PRVM_DECLARE_clientglobalstring(x) {ev_string, #x},
155 #define PRVM_DECLARE_clientglobaledict(x) {ev_entity, #x},
156 #define PRVM_DECLARE_clientglobalfunction(x) {ev_function, #x},
157 #define PRVM_DECLARE_menuglobalfloat(x)
158 #define PRVM_DECLARE_menuglobalvector(x)
159 #define PRVM_DECLARE_menuglobalstring(x)
160 #define PRVM_DECLARE_menuglobaledict(x)
161 #define PRVM_DECLARE_menuglobalfunction(x)
162 #define PRVM_DECLARE_serverfieldfloat(x)
163 #define PRVM_DECLARE_serverfieldvector(x)
164 #define PRVM_DECLARE_serverfieldstring(x)
165 #define PRVM_DECLARE_serverfieldedict(x)
166 #define PRVM_DECLARE_serverfieldfunction(x)
167 #define PRVM_DECLARE_clientfieldfloat(x)
168 #define PRVM_DECLARE_clientfieldvector(x)
169 #define PRVM_DECLARE_clientfieldstring(x)
170 #define PRVM_DECLARE_clientfieldedict(x)
171 #define PRVM_DECLARE_clientfieldfunction(x)
172 #define PRVM_DECLARE_menufieldfloat(x)
173 #define PRVM_DECLARE_menufieldvector(x)
174 #define PRVM_DECLARE_menufieldstring(x)
175 #define PRVM_DECLARE_menufieldedict(x)
176 #define PRVM_DECLARE_menufieldfunction(x)
177 #define PRVM_DECLARE_serverfunction(x)
178 #define PRVM_DECLARE_clientfunction(x)
179 #define PRVM_DECLARE_menufunction(x)
180 #define PRVM_DECLARE_field(x)
181 #define PRVM_DECLARE_global(x)
182 #define PRVM_DECLARE_function(x)
183 #include "prvm_offsets.h"
184 #undef PRVM_DECLARE_serverglobalfloat
185 #undef PRVM_DECLARE_serverglobalvector
186 #undef PRVM_DECLARE_serverglobalstring
187 #undef PRVM_DECLARE_serverglobaledict
188 #undef PRVM_DECLARE_serverglobalfunction
189 #undef PRVM_DECLARE_clientglobalfloat
190 #undef PRVM_DECLARE_clientglobalvector
191 #undef PRVM_DECLARE_clientglobalstring
192 #undef PRVM_DECLARE_clientglobaledict
193 #undef PRVM_DECLARE_clientglobalfunction
194 #undef PRVM_DECLARE_menuglobalfloat
195 #undef PRVM_DECLARE_menuglobalvector
196 #undef PRVM_DECLARE_menuglobalstring
197 #undef PRVM_DECLARE_menuglobaledict
198 #undef PRVM_DECLARE_menuglobalfunction
199 #undef PRVM_DECLARE_serverfieldfloat
200 #undef PRVM_DECLARE_serverfieldvector
201 #undef PRVM_DECLARE_serverfieldstring
202 #undef PRVM_DECLARE_serverfieldedict
203 #undef PRVM_DECLARE_serverfieldfunction
204 #undef PRVM_DECLARE_clientfieldfloat
205 #undef PRVM_DECLARE_clientfieldvector
206 #undef PRVM_DECLARE_clientfieldstring
207 #undef PRVM_DECLARE_clientfieldedict
208 #undef PRVM_DECLARE_clientfieldfunction
209 #undef PRVM_DECLARE_menufieldfloat
210 #undef PRVM_DECLARE_menufieldvector
211 #undef PRVM_DECLARE_menufieldstring
212 #undef PRVM_DECLARE_menufieldedict
213 #undef PRVM_DECLARE_menufieldfunction
214 #undef PRVM_DECLARE_serverfunction
215 #undef PRVM_DECLARE_clientfunction
216 #undef PRVM_DECLARE_menufunction
217 #undef PRVM_DECLARE_field
218 #undef PRVM_DECLARE_global
219 #undef PRVM_DECLARE_function
220 };
221
222 void CL_VM_UpdateDmgGlobals (int dmg_take, int dmg_save, vec3_t dmg_origin)
223 {
224         prvm_prog_t *prog = CLVM_prog;
225
226         if(prog->loaded)
227         {
228                 PRVM_clientglobalfloat(dmg_take) = dmg_take;
229                 PRVM_clientglobalfloat(dmg_save) = dmg_save;
230                 VectorCopy(dmg_origin, PRVM_clientglobalvector(dmg_origin));
231         }
232 }
233
234 void CSQC_UpdateNetworkTimes(double newtime, double oldtime)
235 {
236         prvm_prog_t *prog = CLVM_prog;
237
238         if(!prog->loaded)
239                 return;
240
241         PRVM_clientglobalfloat(servertime) = newtime;
242         PRVM_clientglobalfloat(serverprevtime) = oldtime;
243         PRVM_clientglobalfloat(serverdeltatime) = newtime - oldtime;
244 }
245
246 //[515]: set globals before calling R_UpdateView, WEIRD CRAP
247 static void CSQC_SetGlobals (double frametime)
248 {
249         vec3_t pmove_org;
250         prvm_prog_t *prog = CLVM_prog;
251
252         PRVM_clientglobalfloat(time) = cl.time;
253         PRVM_clientglobalfloat(cltime) = host.realtime; // Spike named it that way.
254         PRVM_clientglobalfloat(frametime) = frametime;
255         PRVM_clientglobalfloat(servercommandframe) = cls.servermovesequence;
256         PRVM_clientglobalfloat(clientcommandframe) = cl.movecmd[0].sequence;
257         VectorCopy(cl.viewangles, PRVM_clientglobalvector(input_angles));
258         // // FIXME: this actually belongs into getinputstate().. [12/17/2007 Black]
259         PRVM_clientglobalfloat(input_buttons) = cl.movecmd[0].buttons;
260         VectorSet(PRVM_clientglobalvector(input_movevalues), cl.movecmd[0].forwardmove, cl.movecmd[0].sidemove, cl.movecmd[0].upmove);
261         VectorCopy(cl.csqc_vieworiginfromengine, cl.csqc_vieworigin);
262         VectorCopy(cl.csqc_viewanglesfromengine, cl.csqc_viewangles);
263
264         // LadyHavoc: Spike says not to do this, but without pmove_org the
265         // CSQC is useless as it can't alter the view origin without
266         // completely replacing it
267         Matrix4x4_OriginFromMatrix(&cl.entities[cl.viewentity].render.matrix, pmove_org);
268         VectorCopy(pmove_org, PRVM_clientglobalvector(pmove_org));
269         VectorCopy(cl.movement_velocity, PRVM_clientglobalvector(pmove_vel));
270         PRVM_clientglobalfloat(pmove_onground) = cl.onground;
271         PRVM_clientglobalfloat(pmove_inwater) = cl.inwater;
272
273         VectorCopy(cl.viewangles, PRVM_clientglobalvector(view_angles));
274         VectorCopy(cl.punchangle, PRVM_clientglobalvector(view_punchangle));
275         VectorCopy(cl.punchvector, PRVM_clientglobalvector(view_punchvector));
276         PRVM_clientglobalfloat(maxclients) = cl.maxclients;
277
278         PRVM_clientglobalfloat(player_localentnum) = cl.viewentity;
279
280         CSQC_R_RecalcView();
281 }
282
283 void CSQC_Predraw (prvm_edict_t *ed)
284 {
285         prvm_prog_t *prog = CLVM_prog;
286         int b;
287         if(!PRVM_clientedictfunction(ed, predraw))
288                 return;
289         b = PRVM_clientglobaledict(self);
290         PRVM_clientglobaledict(self) = PRVM_EDICT_TO_PROG(ed);
291         prog->ExecuteProgram(prog, PRVM_clientedictfunction(ed, predraw), "CSQC_Predraw: NULL function\n");
292         PRVM_clientglobaledict(self) = b;
293 }
294
295 void CSQC_Think (prvm_edict_t *ed)
296 {
297         prvm_prog_t *prog = CLVM_prog;
298         int b;
299         if(PRVM_clientedictfunction(ed, think))
300         if(PRVM_clientedictfloat(ed, nextthink) && PRVM_clientedictfloat(ed, nextthink) <= PRVM_clientglobalfloat(time))
301         {
302                 PRVM_clientedictfloat(ed, nextthink) = 0;
303                 b = PRVM_clientglobaledict(self);
304                 PRVM_clientglobaledict(self) = PRVM_EDICT_TO_PROG(ed);
305                 prog->ExecuteProgram(prog, PRVM_clientedictfunction(ed, think), "CSQC_Think: NULL function\n");
306                 PRVM_clientglobaledict(self) = b;
307         }
308 }
309
310 extern cvar_t cl_noplayershadow;
311 qbool CSQC_AddRenderEdict(prvm_edict_t *ed, int edictnum)
312 {
313         prvm_prog_t *prog = CLVM_prog;
314         int renderflags;
315         int c;
316         float scale;
317         entity_render_t *entrender;
318         model_t *model;
319         prvm_vec3_t modellight_origin;
320
321         model = CL_GetModelFromEdict(ed);
322         if (!model)
323                 return false;
324
325         if (edictnum)
326         {
327                 if (r_refdef.scene.numentities >= r_refdef.scene.maxentities)
328                         return false;
329                 entrender = cl.csqcrenderentities + edictnum;
330                 r_refdef.scene.entities[r_refdef.scene.numentities++] = entrender;
331                 entrender->entitynumber = edictnum + MAX_EDICTS;
332                 //entrender->shadertime = 0; // shadertime was set by spawn()
333                 entrender->flags = 0;
334                 entrender->effects = 0;
335                 entrender->alpha = 1;
336                 entrender->scale = 1;
337                 VectorSet(entrender->colormod, 1, 1, 1);
338                 VectorSet(entrender->glowmod, 1, 1, 1);
339                 entrender->allowdecals = true;
340         }
341         else
342         {
343                 entrender = CL_NewTempEntity(0);
344                 if (!entrender)
345                         return false;
346         }
347
348         entrender->userwavefunc_param[0] = PRVM_clientedictfloat(ed, userwavefunc_param0);
349         entrender->userwavefunc_param[1] = PRVM_clientedictfloat(ed, userwavefunc_param1);
350         entrender->userwavefunc_param[2] = PRVM_clientedictfloat(ed, userwavefunc_param2);
351         entrender->userwavefunc_param[3] = PRVM_clientedictfloat(ed, userwavefunc_param3);
352
353         entrender->model = model;
354         entrender->skinnum = (int)PRVM_clientedictfloat(ed, skin);
355         entrender->effects |= entrender->model->effects;
356         renderflags = (int)PRVM_clientedictfloat(ed, renderflags);
357         entrender->alpha = PRVM_clientedictfloat(ed, alpha);
358         entrender->scale = scale = PRVM_clientedictfloat(ed, scale);
359         VectorCopy(PRVM_clientedictvector(ed, colormod), entrender->colormod);
360         VectorCopy(PRVM_clientedictvector(ed, glowmod), entrender->glowmod);
361         if(PRVM_clientedictfloat(ed, effects))  entrender->effects |= (int)PRVM_clientedictfloat(ed, effects);
362         if (!entrender->alpha)
363                 entrender->alpha = 1.0f;
364         if (!entrender->scale)
365                 entrender->scale = scale = 1.0f;
366         if (!VectorLength2(entrender->colormod))
367                 VectorSet(entrender->colormod, 1, 1, 1);
368         if (!VectorLength2(entrender->glowmod))
369                 VectorSet(entrender->glowmod, 1, 1, 1);
370
371         // LadyHavoc: use the CL_GetTagMatrix function on self to ensure consistent behavior (duplicate code would be bad)
372         // this also sets the custommodellight_origin for us
373         CL_GetTagMatrix(prog, &entrender->matrix, ed, 0, modellight_origin);
374         VectorCopy(modellight_origin, entrender->custommodellight_origin);
375
376         // set up the animation data
377         VM_GenerateFrameGroupBlend(prog, ed->priv.server->framegroupblend, ed);
378         VM_FrameBlendFromFrameGroupBlend(ed->priv.server->frameblend, ed->priv.server->framegroupblend, model, cl.time);
379         VM_UpdateEdictSkeleton(prog, ed, model, ed->priv.server->frameblend);
380         if (PRVM_clientedictfloat(ed, shadertime)) // hack for csprogs.dat files that do not set shadertime, leaves the value at entity spawn time
381                 entrender->shadertime = PRVM_clientedictfloat(ed, shadertime);
382
383         // transparent offset
384         if (renderflags & RF_USETRANSPARENTOFFSET)
385                 entrender->transparent_offset = PRVM_clientglobalfloat(transparent_offset);
386
387         // model light
388         if (renderflags & RF_MODELLIGHT)
389         {
390                 if (PRVM_clientedictvector(ed, modellight_ambient)) VectorCopy(PRVM_clientedictvector(ed, modellight_ambient), entrender->custommodellight_ambient); else VectorClear(entrender->custommodellight_ambient);
391                 if (PRVM_clientedictvector(ed, modellight_diffuse)) VectorCopy(PRVM_clientedictvector(ed, modellight_diffuse), entrender->custommodellight_diffuse); else VectorClear(entrender->custommodellight_diffuse);
392                 if (PRVM_clientedictvector(ed, modellight_dir))     VectorCopy(PRVM_clientedictvector(ed, modellight_dir), entrender->custommodellight_lightdir);    else VectorClear(entrender->custommodellight_lightdir);
393                 entrender->flags |= RENDER_CUSTOMIZEDMODELLIGHT;
394         }
395
396         if(renderflags)
397         {
398                 if(renderflags & RF_VIEWMODEL) entrender->flags |= RENDER_VIEWMODEL | RENDER_NODEPTHTEST;
399                 if(renderflags & RF_EXTERNALMODEL) entrender->flags |= RENDER_EXTERIORMODEL;
400                 if(renderflags & RF_WORLDOBJECT) entrender->flags |= RENDER_WORLDOBJECT;
401                 if(renderflags & RF_DEPTHHACK) entrender->flags |= RENDER_NODEPTHTEST;
402                 if(renderflags & RF_ADDITIVE) entrender->flags |= RENDER_ADDITIVE;
403                 if(renderflags & RF_DYNAMICMODELLIGHT) entrender->flags |= RENDER_DYNAMICMODELLIGHT;
404         }
405
406         c = (int)PRVM_clientedictfloat(ed, colormap);
407         if (c <= 0)
408                 CL_SetEntityColormapColors(entrender, -1);
409         else if (c <= cl.maxclients && cl.scores != NULL)
410                 CL_SetEntityColormapColors(entrender, cl.scores[c-1].colors);
411         else
412                 CL_SetEntityColormapColors(entrender, c);
413
414         entrender->flags &= ~(RENDER_SHADOW | RENDER_LIGHT | RENDER_NOSELFSHADOW);
415         // either fullbright or lit
416         if(!r_fullbright.integer)
417         {
418                 if (!(entrender->effects & EF_FULLBRIGHT) && !(renderflags & RF_FULLBRIGHT))
419                         entrender->flags |= RENDER_LIGHT;
420         }
421         // hide player shadow during intermission or nehahra movie
422         if (!(entrender->effects & (EF_NOSHADOW | EF_ADDITIVE | EF_NODEPTHTEST))
423          &&  (entrender->alpha >= 1)
424          && !(renderflags & RF_NOSHADOW)
425          && !(entrender->flags & RENDER_VIEWMODEL)
426          && (!(entrender->flags & RENDER_EXTERIORMODEL) || (!cl.intermission && cls.protocol != PROTOCOL_NEHAHRAMOVIE && !cl_noplayershadow.integer)))
427                 entrender->flags |= RENDER_SHADOW;
428         if (entrender->flags & RENDER_VIEWMODEL)
429                 entrender->flags |= RENDER_NOSELFSHADOW;
430         if (entrender->effects & EF_NOSELFSHADOW)
431                 entrender->flags |= RENDER_NOSELFSHADOW;
432         if (entrender->effects & EF_NODEPTHTEST)
433                 entrender->flags |= RENDER_NODEPTHTEST;
434         if (entrender->effects & EF_ADDITIVE)
435                 entrender->flags |= RENDER_ADDITIVE;
436         if (entrender->effects & EF_DOUBLESIDED)
437                 entrender->flags |= RENDER_DOUBLESIDED;
438         if (entrender->effects & EF_DYNAMICMODELLIGHT)
439                 entrender->flags |= RENDER_DYNAMICMODELLIGHT;
440
441         // make the other useful stuff
442         memcpy(entrender->framegroupblend, ed->priv.server->framegroupblend, sizeof(ed->priv.server->framegroupblend));
443         CL_UpdateRenderEntity(entrender);
444
445         // override animation data with full control
446         memcpy(entrender->frameblend, ed->priv.server->frameblend, sizeof(ed->priv.server->frameblend));
447         if (ed->priv.server->skeleton.relativetransforms)
448                 entrender->skeleton = &ed->priv.server->skeleton;
449         else
450                 entrender->skeleton = NULL;
451
452         return true;
453 }
454
455 // 0 = keydown, key, character (EXT_CSQC)
456 // 1 = keyup, key, character (EXT_CSQC)
457 // 2 = mousemove relative, x, y (EXT_CSQC)
458 // 3 = mousemove absolute, x, y (DP_CSQC)
459 qbool CL_VM_InputEvent (int eventtype, float x, float y)
460 {
461         prvm_prog_t *prog = CLVM_prog;
462         qbool r;
463
464         if(!prog->loaded)
465                 return false;
466
467         if (!PRVM_clientfunction(CSQC_InputEvent))
468                 r = false;
469         else
470         {
471                 PRVM_clientglobalfloat(time) = cl.time;
472                 PRVM_clientglobaledict(self) = cl.csqc_server2csqcentitynumber[cl.playerentity];
473                 PRVM_G_FLOAT(OFS_PARM0) = eventtype;
474                 PRVM_G_FLOAT(OFS_PARM1) = x; // key or x
475                 PRVM_G_FLOAT(OFS_PARM2) = y; // ascii or y
476                 prog->ExecuteProgram(prog, PRVM_clientfunction(CSQC_InputEvent), "QC function CSQC_InputEvent is missing");
477                 r = CSQC_RETURNVAL != 0;
478         }
479         return r;
480 }
481
482 extern r_refdef_view_t csqc_original_r_refdef_view;
483 extern r_refdef_view_t csqc_main_r_refdef_view;
484 qbool CL_VM_UpdateView (double frametime)
485 {
486         prvm_prog_t *prog = CLVM_prog;
487         vec3_t emptyvector;
488         emptyvector[0] = 0;
489         emptyvector[1] = 0;
490         emptyvector[2] = 0;
491 //      vec3_t oldangles;
492
493         if(!prog->loaded)
494                 return false;
495
496         R_TimeReport("pre-UpdateView");
497
498         csqc_original_r_refdef_view = r_refdef.view;
499         csqc_main_r_refdef_view = r_refdef.view;
500         //VectorCopy(cl.viewangles, oldangles);
501         PRVM_clientglobalfloat(time) = cl.time;
502         PRVM_clientglobaledict(self) = cl.csqc_server2csqcentitynumber[cl.playerentity];
503         CSQC_SetGlobals(frametime);
504         // clear renderable entity and light lists to prevent crashes if the
505         // CSQC_UpdateView function does not call R_ClearScene as it should
506         r_refdef.scene.numentities = 0;
507         r_refdef.scene.numlights = 0;
508         // polygonbegin without draw2d arg has to guess
509         prog->polygonbegin_guess2d = false;
510         // free memory for resources that are no longer referenced
511         PRVM_GarbageCollection(prog);
512         // pass in width and height and menu/focus state as parameters (EXT_CSQC_1)
513         if (csqc_lowres.integer)
514         {
515                 PRVM_G_FLOAT(OFS_PARM0) = vid_conwidth.value;
516                 PRVM_G_FLOAT(OFS_PARM1) = vid_conheight.value;
517         }
518         else
519         {
520                 PRVM_G_FLOAT(OFS_PARM0) = vid.mode.width;
521                 PRVM_G_FLOAT(OFS_PARM1) = vid.mode.height;
522         }
523         /*
524          * This should be fine for now but FTEQW uses flags for keydest
525          * and checks that an array called "eyeoffset" is 0
526          *
527          * Just a note in case there's compatibility problems later
528          */
529         PRVM_G_FLOAT(OFS_PARM2) = key_dest == key_game;
530         prog->ExecuteProgram(prog, PRVM_clientfunction(CSQC_UpdateView), "QC function CSQC_UpdateView is missing");
531         //VectorCopy(oldangles, cl.viewangles);
532         // Dresk : Reset Dmg Globals Here
533         CL_VM_UpdateDmgGlobals(0, 0, emptyvector);
534         r_refdef.view = csqc_main_r_refdef_view;
535         R_RenderView_UpdateViewVectors(); // we have to do this, as we undid the scene render doing this for us
536
537         R_TimeReport("UpdateView");
538         return true;
539 }
540
541 void CL_VM_DrawHud(double frametime)
542 {
543         prvm_prog_t *prog = CLVM_prog;
544
545         R_TimeReport("pre-DrawHud");
546
547         PRVM_clientglobalfloat(time) = cl.time;
548         PRVM_clientglobaledict(self) = cl.csqc_server2csqcentitynumber[cl.playerentity];
549         CSQC_SetGlobals(frametime);
550
551         PRVM_GarbageCollection(prog);
552
553         // width and height parameters are virtual in CSQC_SIMPLE engines
554         VectorSet(PRVM_G_VECTOR(OFS_PARM0), vid_conwidth.integer, vid_conheight.integer, 0);
555         PRVM_G_FLOAT(OFS_PARM1) = sb_showscores;
556         prog->ExecuteProgram(prog, PRVM_clientfunction(CSQC_DrawHud), "QC function CSQC_DrawHud is missing");
557
558         if (PRVM_clientfunction(CSQC_DrawScores))
559         {
560                 VectorSet(PRVM_G_VECTOR(OFS_PARM0), vid_conwidth.integer, vid_conheight.integer, 0);
561                 PRVM_G_FLOAT(OFS_PARM1) = sb_showscores;
562                 if (key_dest != key_menu)
563                         prog->ExecuteProgram(prog, PRVM_clientfunction(CSQC_DrawScores), "QC function CSQC_DrawScores is missing");
564         }
565         else if (sb_showscores || (cl.stats[STAT_HEALTH] <= 0 && cl_deathscoreboard.integer))
566                 if (!cl.islocalgame) // LadyHavoc: changed to draw the deathmatch overlays in any multiplayer mode
567                         Sbar_DeathmatchOverlay ();
568
569         R_TimeReport("DrawHud");
570 }
571
572
573 qbool CL_VM_ConsoleCommand(const char *text, size_t textlen)
574 {
575         prvm_prog_t *prog = CLVM_prog;
576         return PRVM_ConsoleCommand(prog, text, textlen, &prog->funcoffsets.CSQC_ConsoleCommand, false, cl.csqc_server2csqcentitynumber[cl.playerentity], cl.time, "QC function CSQC_ConsoleCommand is missing");
577 }
578
579 qbool CL_VM_Parse_TempEntity (void)
580 {
581         prvm_prog_t *prog = CLVM_prog;
582         int t;
583         qbool r = false;
584
585         if(!prog->loaded)
586                 return false;
587
588         if(PRVM_clientfunction(CSQC_Parse_TempEntity))
589         {
590                 t = cl_message.readcount;
591                 PRVM_clientglobalfloat(time) = cl.time;
592                 PRVM_clientglobaledict(self) = cl.csqc_server2csqcentitynumber[cl.playerentity];
593                 prog->ExecuteProgram(prog, PRVM_clientfunction(CSQC_Parse_TempEntity), "QC function CSQC_Parse_TempEntity is missing");
594                 r = CSQC_RETURNVAL != 0;
595                 if(!r)
596                 {
597                         cl_message.readcount = t;
598                         cl_message.badread = false;
599                 }
600         }
601         return r;
602 }
603
604 void CL_VM_Parse_StuffCmd(const char *msg, size_t msg_len)
605 {
606         prvm_prog_t *prog = CLVM_prog;
607         int restorevm_tempstringsbuf_cursize;
608
609         if(msg[0] == 'c')
610         if(msg[1] == 's')
611         if(msg[2] == 'q')
612         if(msg[3] == 'c')
613         {
614                 // if this is setting a csqc variable, deprotect csqc_progcrc
615                 // temporarily so that it can be set by the cvar command,
616                 // and then reprotect it afterwards
617                 int crcflags = csqc_progcrc.flags;
618                 csqc_progcrc.flags &= ~CF_READONLY;
619                 csqc_progsize.flags &= ~CF_READONLY;
620                 Cmd_ExecuteString(cmd_local, msg, msg_len, src_local, true);
621                 csqc_progcrc.flags = csqc_progsize.flags = crcflags;
622                 return;
623         }
624
625         if(!prog->loaded)
626         {
627                 Cbuf_AddText(cmd_local, msg);
628                 return;
629         }
630
631         if(PRVM_clientfunction(CSQC_Parse_StuffCmd))
632         {
633                 PRVM_clientglobalfloat(time) = cl.time;
634                 PRVM_clientglobaledict(self) = cl.csqc_server2csqcentitynumber[cl.playerentity];
635                 restorevm_tempstringsbuf_cursize = prog->tempstringsbuf.cursize;
636                 PRVM_G_INT(OFS_PARM0) = PRVM_SetTempString(prog, msg, msg_len);
637                 prog->ExecuteProgram(prog, PRVM_clientfunction(CSQC_Parse_StuffCmd), "QC function CSQC_Parse_StuffCmd is missing");
638                 prog->tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize;
639         }
640         else
641                 Cbuf_AddText(cmd_local, msg);
642 }
643
644 static void CL_VM_Parse_Print(const char *msg, size_t msg_len)
645 {
646         prvm_prog_t *prog = CLVM_prog;
647         int restorevm_tempstringsbuf_cursize;
648         PRVM_clientglobalfloat(time) = cl.time;
649         PRVM_clientglobaledict(self) = cl.csqc_server2csqcentitynumber[cl.playerentity];
650         restorevm_tempstringsbuf_cursize = prog->tempstringsbuf.cursize;
651         PRVM_G_INT(OFS_PARM0) = PRVM_SetTempString(prog, msg, msg_len);
652         prog->ExecuteProgram(prog, PRVM_clientfunction(CSQC_Parse_Print), "QC function CSQC_Parse_Print is missing");
653         prog->tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize;
654 }
655
656 void CSQC_AddPrintText(const char *msg, size_t msg_len)
657 {
658         prvm_prog_t *prog = CLVM_prog;
659         char *start = cl.csqc_printtextbuf + cl.csqc_printtextbuf_len;
660         size_t writebytes = min(msg_len + 1, MAX_INPUTLINE - cl.csqc_printtextbuf_len);
661
662         if(prog->loaded && PRVM_clientfunction(CSQC_Parse_Print))
663         {
664                 if(msg[msg_len - 1] != '\n' && msg[msg_len - 1] != '\r')
665                 {
666                         if(cl.csqc_printtextbuf_len + msg_len + 1 >= MAX_INPUTLINE)
667                         {
668                                 CL_VM_Parse_Print(cl.csqc_printtextbuf, cl.csqc_printtextbuf_len);
669                                 cl.csqc_printtextbuf[0] = '\0';
670                                 cl.csqc_printtextbuf_len = 0;
671                         }
672                         else
673                         {
674                                 memcpy(start, msg, writebytes);
675                                 cl.csqc_printtextbuf_len += msg_len;
676                         }
677                         return;
678                 }
679                 memcpy(start, msg, writebytes);
680                 cl.csqc_printtextbuf_len += msg_len;
681                 CL_VM_Parse_Print(cl.csqc_printtextbuf, cl.csqc_printtextbuf_len);
682                 cl.csqc_printtextbuf[0] = '\0';
683                 cl.csqc_printtextbuf_len = 0;
684         }
685         else
686                 Con_Print(msg);
687 }
688
689 void CL_VM_Parse_CenterPrint(const char *msg, size_t msg_len)
690 {
691         prvm_prog_t *prog = CLVM_prog;
692         int restorevm_tempstringsbuf_cursize;
693
694         if(prog->loaded && PRVM_clientfunction(CSQC_Parse_CenterPrint))
695         {
696                 PRVM_clientglobalfloat(time) = cl.time;
697                 PRVM_clientglobaledict(self) = cl.csqc_server2csqcentitynumber[cl.playerentity];
698                 restorevm_tempstringsbuf_cursize = prog->tempstringsbuf.cursize;
699                 PRVM_G_INT(OFS_PARM0) = PRVM_SetTempString(prog, msg, msg_len);
700                 prog->ExecuteProgram(prog, PRVM_clientfunction(CSQC_Parse_CenterPrint), "QC function CSQC_Parse_CenterPrint is missing");
701                 prog->tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize;
702         }
703         else
704                 SCR_CenterPrint(msg);
705 }
706
707 void CL_VM_UpdateIntermissionState (int intermission)
708 {
709         prvm_prog_t *prog = CLVM_prog;
710
711         if(prog->loaded)
712                 PRVM_clientglobalfloat(intermission) = intermission;
713 }
714 void CL_VM_UpdateShowingScoresState (int showingscores)
715 {
716         prvm_prog_t *prog = CLVM_prog;
717
718         if(prog->loaded)
719                 PRVM_clientglobalfloat(sb_showscores) = showingscores;
720 }
721 qbool CL_VM_Event_Sound(int sound_num, float fvolume, int channel, float attenuation, int ent, vec3_t pos, int flags, float speed)
722 {
723         prvm_prog_t *prog = CLVM_prog;
724         qbool r = false;
725
726         if(prog->loaded)
727         {
728                 if(PRVM_clientfunction(CSQC_Event_Sound))
729                 {
730                         PRVM_clientglobalfloat(time) = cl.time;
731                         PRVM_clientglobaledict(self) = cl.csqc_server2csqcentitynumber[cl.playerentity];
732                         PRVM_G_FLOAT(OFS_PARM0) = ent;
733                         PRVM_G_FLOAT(OFS_PARM1) = CHAN_ENGINE2USER(channel);
734                         PRVM_G_INT(OFS_PARM2) = PRVM_SetTempString(prog, cl.sound_name[sound_num], strlen(cl.sound_name[sound_num]));
735                         PRVM_G_FLOAT(OFS_PARM3) = fvolume;
736                         PRVM_G_FLOAT(OFS_PARM4) = attenuation;
737                         VectorCopy(pos, PRVM_G_VECTOR(OFS_PARM5) );
738                         PRVM_G_FLOAT(OFS_PARM6) = speed * 100.0f;
739                         PRVM_G_FLOAT(OFS_PARM7) = flags; // flags
740                         prog->ExecuteProgram(prog, PRVM_clientfunction(CSQC_Event_Sound), "QC function CSQC_Event_Sound is missing");
741                         r = CSQC_RETURNVAL != 0;
742                 }
743         }
744
745         return r;
746 }
747 static void CL_VM_UpdateCoopDeathmatchGlobals (int gametype)
748 {
749         prvm_prog_t *prog = CLVM_prog;
750         // Avoid global names for clean(er) coding
751         int localcoop;
752         int localdeathmatch;
753
754         if(prog->loaded)
755         {
756                 if(gametype == GAME_COOP)
757                 {
758                         localcoop = 1;
759                         localdeathmatch = 0;
760                 }
761                 else if(gametype == GAME_DEATHMATCH)
762                 {
763                         localcoop = 0;
764                         localdeathmatch = 1;
765                 }
766                 else
767                 {
768                         // How did the ServerInfo send an unknown gametype?
769                         // Better just assign the globals as 0...
770                         localcoop = 0;
771                         localdeathmatch = 0;
772                 }
773                 PRVM_clientglobalfloat(coop) = localcoop;
774                 PRVM_clientglobalfloat(deathmatch) = localdeathmatch;
775         }
776 }
777 #if 0
778 static float CL_VM_Event (float event)          //[515]: needed ? I'd say "YES", but don't know for what :D
779 {
780         prvm_prog_t *prog = CLVM_prog;
781         float r = 0;
782
783         if(!prog->loaded)
784                 return 0;
785
786         if(PRVM_clientfunction(CSQC_Event))
787         {
788                 PRVM_clientglobalfloat(time) = cl.time;
789                 PRVM_clientglobaledict(self) = cl.csqc_server2csqcentitynumber[cl.playerentity];
790                 PRVM_G_FLOAT(OFS_PARM0) = event;
791                 prog->ExecuteProgram(prog, PRVM_clientfunction(CSQC_Event), "QC function CSQC_Event is missing");
792                 r = CSQC_RETURNVAL;
793         }
794         return r;
795 }
796 #endif
797
798 void CSQC_ReadEntities (void)
799 {
800         prvm_prog_t *prog = CLVM_prog;
801         unsigned short entnum, oldself, realentnum;
802
803         if(!prog->loaded)
804         {
805                 Host_Error ("CSQC_ReadEntities: CSQC is not loaded");
806                 return;
807         }
808
809         PRVM_clientglobalfloat(time) = cl.time;
810         oldself = PRVM_clientglobaledict(self);
811         while(1)
812         {
813                 entnum = MSG_ReadShort(&cl_message);
814                 if(!entnum || cl_message.badread)
815                         break;
816                 realentnum = entnum & 0x7FFF;
817                 PRVM_clientglobaledict(self) = cl.csqc_server2csqcentitynumber[realentnum];
818                 if(entnum & 0x8000)
819                 {
820                         if(PRVM_clientglobaledict(self))
821                         {
822                                 prog->ExecuteProgram(prog, PRVM_clientfunction(CSQC_Ent_Remove), "QC function CSQC_Ent_Remove is missing");
823                                 cl.csqc_server2csqcentitynumber[realentnum] = 0;
824                         }
825                         else
826                         {
827                                 // LadyHavoc: removing an entity that is already gone on
828                                 // the csqc side is possible for legitimate reasons (such
829                                 // as a repeat of the remove message), so no warning is
830                                 // needed
831                                 //Con_Printf("Bad csqc_server2csqcentitynumber map\n"); //[515]: never happens ?
832                         }
833                 }
834                 else
835                 {
836                         if(!PRVM_clientglobaledict(self))
837                         {
838                                 if(!PRVM_clientfunction(CSQC_Ent_Spawn))
839                                 {
840                                         prvm_edict_t    *ed;
841                                         ed = PRVM_ED_Alloc(prog);
842                                         PRVM_clientedictfloat(ed, entnum) = realentnum;
843                                         PRVM_clientglobaledict(self) = cl.csqc_server2csqcentitynumber[realentnum] = PRVM_EDICT_TO_PROG(ed);
844                                 }
845                                 else
846                                 {
847                                         // entity( float entnum ) CSQC_Ent_Spawn;
848                                         // the qc function should set entnum, too (this way it also can return world [2/1/2008 Andreas]
849                                         PRVM_G_FLOAT(OFS_PARM0) = (float) realentnum;
850                                         // make sure no one gets wrong ideas
851                                         PRVM_clientglobaledict(self) = 0;
852                                         prog->ExecuteProgram(prog, PRVM_clientfunction(CSQC_Ent_Spawn), "QC function CSQC_Ent_Spawn is missing");
853                                         PRVM_clientglobaledict(self) = cl.csqc_server2csqcentitynumber[realentnum] = PRVM_EDICT( PRVM_G_INT( OFS_RETURN ) );
854                                 }
855                                 PRVM_G_FLOAT(OFS_PARM0) = 1;
856                                 prog->ExecuteProgram(prog, PRVM_clientfunction(CSQC_Ent_Update), "QC function CSQC_Ent_Update is missing");
857                         }
858                         else {
859                                 PRVM_G_FLOAT(OFS_PARM0) = 0;
860                                 prog->ExecuteProgram(prog, PRVM_clientfunction(CSQC_Ent_Update), "QC function CSQC_Ent_Update is missing");
861                         }
862                 }
863         }
864         PRVM_clientglobaledict(self) = oldself;
865 }
866
867 static void CLVM_begin_increase_edicts(prvm_prog_t *prog)
868 {
869         // links don't survive the transition, so unlink everything
870         World_UnlinkAll(&cl.world);
871 }
872
873 static void CLVM_end_increase_edicts(prvm_prog_t *prog)
874 {
875         int i;
876         prvm_edict_t *ent;
877
878         // link every entity except world
879         for (i = 1, ent = prog->edicts;i < prog->num_edicts;i++, ent++)
880                 if (!ent->free)
881                         CL_LinkEdict(ent);
882 }
883
884 static void CLVM_init_edict(prvm_prog_t *prog, prvm_edict_t *e)
885 {
886         int edictnum = PRVM_NUM_FOR_EDICT(e);
887         entity_render_t *entrender;
888         CL_ExpandCSQCRenderEntities(edictnum);
889         entrender = cl.csqcrenderentities + edictnum;
890         e->priv.server->move = false; // don't move on first frame
891         memset(entrender, 0, sizeof(*entrender));
892         entrender->shadertime = cl.time;
893 }
894
895 static void CLVM_free_edict(prvm_prog_t *prog, prvm_edict_t *ed)
896 {
897         entity_render_t *entrender = cl.csqcrenderentities + PRVM_NUM_FOR_EDICT(ed);
898         R_DecalSystem_Reset(&entrender->decalsystem);
899         memset(entrender, 0, sizeof(*entrender));
900         World_UnlinkEdict(ed);
901         memset(ed->fields.fp, 0, prog->entityfields * sizeof(prvm_vec_t));
902         VM_RemoveEdictSkeleton(prog, ed);
903 #ifdef USEODE
904         World_Physics_RemoveFromEntity(&cl.world, ed);
905         World_Physics_RemoveJointFromEntity(&cl.world, ed);
906 #endif
907 }
908
909 static void CLVM_count_edicts(prvm_prog_t *prog)
910 {
911         int             i;
912         prvm_edict_t    *ent;
913         int             active = 0, models = 0, solid = 0;
914
915         for (i=0 ; i<prog->num_edicts ; i++)
916         {
917                 ent = PRVM_EDICT_NUM(i);
918                 if (ent->free)
919                         continue;
920                 active++;
921                 if (PRVM_clientedictfloat(ent, solid))
922                         solid++;
923                 if (PRVM_clientedictstring(ent, model))
924                         models++;
925         }
926
927         Con_Printf("num_edicts:%3i\n", prog->num_edicts);
928         Con_Printf("active    :%3i\n", active);
929         Con_Printf("view      :%3i\n", models);
930         Con_Printf("touch     :%3i\n", solid);
931 }
932
933 static qbool CLVM_load_edict(prvm_prog_t *prog, prvm_edict_t *ent)
934 {
935         return true;
936 }
937
938 // returns true if the packet is valid, false if end of file is reached
939 // used for dumping the CSQC download into demo files
940 qbool MakeDownloadPacket(const char *filename, unsigned char *data, size_t len, int crc, int cnt, sizebuf_t *buf, int protocol)
941 {
942         int packetsize = buf->maxsize - 7; // byte short long
943         int npackets = ((int)len + packetsize - 1) / (packetsize);
944         char vabuf[1024];
945
946         if(protocol == PROTOCOL_QUAKEWORLD)
947                 return false; // CSQC can't run in QW anyway
948
949         SZ_Clear(buf);
950         if(cnt == 0)
951         {
952                 MSG_WriteByte(buf, svc_stufftext);
953                 MSG_WriteString(buf, va(vabuf, sizeof(vabuf), "\ncl_downloadbegin %lu %s\n", (unsigned long)len, filename));
954                 return true;
955         }
956         else if(cnt >= 1 && cnt <= npackets)
957         {
958                 unsigned long thispacketoffset = (cnt - 1) * packetsize;
959                 int thispacketsize = (int)len - thispacketoffset;
960                 if(thispacketsize > packetsize)
961                         thispacketsize = packetsize;
962
963                 MSG_WriteByte(buf, svc_downloaddata);
964                 MSG_WriteLong(buf, thispacketoffset);
965                 MSG_WriteShort(buf, thispacketsize);
966                 SZ_Write(buf, data + thispacketoffset, thispacketsize);
967
968                 return true;
969         }
970         else if(cnt == npackets + 1)
971         {
972                 MSG_WriteByte(buf, svc_stufftext);
973                 MSG_WriteString(buf, va(vabuf, sizeof(vabuf), "\ncl_downloadfinished %lu %d\n", (unsigned long)len, crc));
974                 return true;
975         }
976         return false;
977 }
978
979 extern cvar_t csqc_usedemoprogs;
980 void CL_VM_Init (void)
981 {
982         prvm_prog_t *prog = CLVM_prog;
983         const char* csprogsfn = NULL;
984         unsigned char *csprogsdata = NULL;
985         fs_offset_t csprogsdatasize = 0;
986         int csprogsdatacrc, requiredcrc;
987         int requiredsize;
988         char vabuf[1024];
989
990         // reset csqc_progcrc after reading it, so that changing servers doesn't
991         // expect csqc on the next server
992         requiredcrc = csqc_progcrc.integer;
993         requiredsize = csqc_progsize.integer;
994         Cvar_SetValueQuick(&csqc_progcrc, -1);
995         Cvar_SetValueQuick(&csqc_progsize, -1);
996
997         // if the server is not requesting a csprogs, then we're done here
998         if (requiredcrc < 0)
999                 return;
1000
1001         // see if the requested csprogs.dat file matches the requested crc
1002         if (!cls.demoplayback || csqc_usedemoprogs.integer)
1003         {
1004                 csprogsfn = va(vabuf, sizeof(vabuf), "dlcache/%s.%i.%i", csqc_progname.string, requiredsize, requiredcrc);
1005                 if(cls.caughtcsprogsdata && cls.caughtcsprogsdatasize == requiredsize && CRC_Block(cls.caughtcsprogsdata, (size_t)cls.caughtcsprogsdatasize) == requiredcrc)
1006                 {
1007                         Con_DPrintf("Using buffered \"%s\"\n", csprogsfn);
1008                         csprogsdata = cls.caughtcsprogsdata;
1009                         csprogsdatasize = cls.caughtcsprogsdatasize;
1010                         cls.caughtcsprogsdata = NULL;
1011                         cls.caughtcsprogsdatasize = 0;
1012                 }
1013                 else
1014                 {
1015                         Con_DPrintf("Not using buffered \"%s\" (buffered: %p, %d)\n", csprogsfn, (void *)cls.caughtcsprogsdata, (int) cls.caughtcsprogsdatasize);
1016                         csprogsdata = FS_LoadFile(csprogsfn, tempmempool, true, &csprogsdatasize);
1017                 }
1018         }
1019         if (!csprogsdata)
1020         {
1021                 csprogsfn = csqc_progname.string;
1022                 csprogsdata = FS_LoadFile(csprogsfn, tempmempool, true, &csprogsdatasize);
1023         }
1024         if (csprogsdata)
1025         {
1026                 csprogsdatacrc = CRC_Block(csprogsdata, (size_t)csprogsdatasize);
1027                 if (csprogsdatacrc != requiredcrc || csprogsdatasize != requiredsize)
1028                 {
1029                         if (cls.demoplayback)
1030                         {
1031                                 Con_Printf(CON_WARN "Warning: 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);
1032                                 // Mem_Free(csprogsdata);
1033                                 // return;
1034                                 // We WANT to continue here, and play the demo with different csprogs!
1035                                 // After all, this is just a warning. Sure things may go wrong from here.
1036                         }
1037                         else
1038                         {
1039                                 Mem_Free(csprogsdata);
1040                                 CL_DisconnectEx(false, "Your %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);
1041                                 return;
1042                         }
1043                 }
1044         }
1045         else
1046         {
1047                 if (requiredcrc >= 0)
1048                         CL_DisconnectEx(false, CON_ERROR "CL_VM_Init: %s requires CSQC, but \"%s\" wasn't found\n", cls.demoplayback ? "demo" : "server", csqc_progname.string);
1049                 return;
1050         }
1051
1052         PRVM_Prog_Init(prog, cmd_local);
1053
1054         // allocate the mempools
1055         prog->progs_mempool = Mem_AllocPool(csqc_progname.string, 0, NULL);
1056         prog->edictprivate_size = 0; // no private struct used
1057         prog->name = "client";
1058         prog->num_edicts = 1;
1059         prog->max_edicts = 512;
1060         prog->limit_edicts = CL_MAX_EDICTS;
1061         prog->reserved_edicts = 0;
1062         prog->edictprivate_size = sizeof(edict_engineprivate_t);
1063         // TODO: add a shared extension string #define and add real support for csqc extension strings [12/5/2007 Black]
1064         prog->extensionstring = vm_sv_extensions;
1065         prog->builtins = vm_cl_builtins;
1066         prog->numbuiltins = vm_cl_numbuiltins;
1067
1068         // all callbacks must be defined (pointers are not checked before calling)
1069         prog->begin_increase_edicts = CLVM_begin_increase_edicts;
1070         prog->end_increase_edicts   = CLVM_end_increase_edicts;
1071         prog->init_edict            = CLVM_init_edict;
1072         prog->free_edict            = CLVM_free_edict;
1073         prog->count_edicts          = CLVM_count_edicts;
1074         prog->load_edict            = CLVM_load_edict;
1075         prog->init_cmd              = CLVM_init_cmd;
1076         prog->reset_cmd             = CLVM_reset_cmd;
1077         prog->error_cmd             = Host_Error;
1078         prog->ExecuteProgram        = CLVM_ExecuteProgram;
1079
1080         PRVM_Prog_Load(prog, csprogsfn, csprogsdata, csprogsdatasize, CL_CheckRequiredFuncs, CL_REQFIELDS, cl_reqfields, CL_REQGLOBALS, cl_reqglobals);
1081
1082         if (!prog->loaded)
1083         {
1084                 Mem_Free(csprogsdata);
1085                 Host_Error("CSQC %s failed to load\n", csprogsfn);
1086         }
1087
1088         if(cls.demorecording)
1089         {
1090                 if(cls.demo_lastcsprogssize != csprogsdatasize || cls.demo_lastcsprogscrc != csprogsdatacrc)
1091                 {
1092                         int i;
1093                         static char buf[NET_MAXMESSAGE];
1094                         sizebuf_t sb;
1095                         unsigned char *demobuf; fs_offset_t demofilesize;
1096
1097                         sb.data = (unsigned char *) buf;
1098                         sb.maxsize = sizeof(buf);
1099                         i = 0;
1100
1101                         CL_CutDemo(&demobuf, &demofilesize);
1102                         while(MakeDownloadPacket(csqc_progname.string, csprogsdata, (size_t)csprogsdatasize, csprogsdatacrc, i++, &sb, cls.protocol))
1103                                 CL_WriteDemoMessage(&sb);
1104                         CL_PasteDemo(&demobuf, &demofilesize);
1105
1106                         cls.demo_lastcsprogssize = csprogsdatasize;
1107                         cls.demo_lastcsprogscrc = csprogsdatacrc;
1108                 }
1109         }
1110         Mem_Free(csprogsdata);
1111
1112         // check if OP_STATE animation is possible in this dat file
1113         if (prog->fieldoffsets.nextthink >= 0 && prog->fieldoffsets.frame >= 0 && prog->fieldoffsets.think >= 0 && prog->globaloffsets.self >= 0)
1114                 prog->flag |= PRVM_OP_STATE;
1115
1116         // set time
1117         PRVM_clientglobalfloat(time) = cl.time;
1118         PRVM_clientglobaledict(self) = 0;
1119
1120         PRVM_clientglobalstring(mapname) = PRVM_SetEngineString(prog, cl.worldbasename);
1121         PRVM_clientglobalfloat(player_localnum) = cl.realplayerentity - 1;
1122         PRVM_clientglobalfloat(player_localentnum) = cl.viewentity;
1123
1124         // set map description (use world entity 0)
1125         PRVM_clientedictstring(prog->edicts, message) = PRVM_SetEngineString(prog, cl.worldmessage);
1126         VectorCopy(cl.world.mins, PRVM_clientedictvector(prog->edicts, mins));
1127         VectorCopy(cl.world.maxs, PRVM_clientedictvector(prog->edicts, maxs));
1128         VectorCopy(cl.world.mins, PRVM_clientedictvector(prog->edicts, absmin));
1129         VectorCopy(cl.world.maxs, PRVM_clientedictvector(prog->edicts, absmax));
1130         PRVM_clientedictfloat(prog->edicts, solid) = SOLID_BSP;
1131         PRVM_clientedictfloat(prog->edicts, modelindex) = 1;
1132         PRVM_clientedictstring(prog->edicts, model) = PRVM_SetEngineString(prog, cl.worldmodel->name);
1133
1134         // call the prog init if it exists
1135         if (PRVM_clientfunction(CSQC_Init))
1136         {
1137                 PRVM_G_FLOAT(OFS_PARM0) = 1.0f; // CSQC_SIMPLE engines always pass 0, FTE always passes 1
1138                 // always include "DarkPlaces" so it can be recognised when gamename doesn't include it
1139                 PRVM_G_INT(OFS_PARM1) = PRVM_SetEngineString(prog, va(vabuf, sizeof(vabuf), "DarkPlaces %s", gamename));
1140                 PRVM_G_FLOAT(OFS_PARM2) = 1.0f; // TODO DP versions...
1141                 prog->ExecuteProgram(prog, PRVM_clientfunction(CSQC_Init), "QC function CSQC_Init is missing");
1142         }
1143
1144         // Once CSQC_Init was called, we consider csqc code fully initialized.
1145         prog->inittime = host.realtime;
1146
1147         cl.csqc_vidvars.drawcrosshair = false;
1148         cl.csqc_vidvars.drawenginesbar = false;
1149
1150         // Update Coop and Deathmatch Globals (at this point the client knows them from ServerInfo)
1151         CL_VM_UpdateCoopDeathmatchGlobals(cl.gametype);
1152 }
1153
1154 void CL_VM_ShutDown (void)
1155 {
1156         prvm_prog_t *prog = CLVM_prog;
1157         Cmd_ClearCSQCCommands(cmd_local);
1158
1159         //Cvar_SetValueQuick(&csqc_progcrc, -1);
1160         //Cvar_SetValueQuick(&csqc_progsize, -1);
1161         if (prog->loaded)
1162         {
1163                 PRVM_clientglobalfloat(time) = cl.time;
1164                 PRVM_clientglobaledict(self) = 0;
1165                 if (PRVM_clientfunction(CSQC_Shutdown))
1166                         prog->ExecuteProgram(prog, PRVM_clientfunction(CSQC_Shutdown), "QC function CSQC_Shutdown is missing");
1167         }
1168         PRVM_Prog_Reset(prog);
1169         Con_DPrint("CSQC ^1unloaded\n");
1170 }
1171
1172 qbool CL_VM_GetEntitySoundOrigin(int entnum, vec3_t out)
1173 {
1174         prvm_prog_t *prog = CLVM_prog;
1175         prvm_edict_t *ed;
1176         model_t *mod;
1177         matrix4x4_t matrix;
1178         qbool r = 0;
1179
1180         ed = PRVM_EDICT_NUM(entnum - MAX_EDICTS);
1181
1182         if(!ed->free)
1183         {
1184                 mod = CL_GetModelFromEdict(ed);
1185                 VectorCopy(PRVM_clientedictvector(ed, origin), out);
1186                 if(CL_GetTagMatrix(prog, &matrix, ed, 0, NULL) == 0)
1187                         Matrix4x4_OriginFromMatrix(&matrix, out);
1188                 if (mod && mod->soundfromcenter)
1189                         VectorMAMAM(1.0f, out, 0.5f, mod->normalmins, 0.5f, mod->normalmaxs, out);
1190                 r = 1;
1191         }
1192
1193         return r;
1194 }
1195
1196 qbool CL_VM_TransformView(int entnum, matrix4x4_t *viewmatrix, mplane_t *clipplane, vec3_t visorigin)
1197 {
1198         prvm_prog_t *prog = CLVM_prog;
1199         qbool ret = false;
1200         prvm_edict_t *ed;
1201         vec3_t forward, left, up, origin, ang;
1202         matrix4x4_t mat, matq;
1203
1204         ed = PRVM_EDICT_NUM(entnum);
1205         // camera:
1206         //   camera_transform
1207         if(PRVM_clientedictfunction(ed, camera_transform))
1208         {
1209                 ret = true;
1210                 if(viewmatrix && clipplane && visorigin)
1211                 {
1212                         Matrix4x4_ToVectors(viewmatrix, forward, left, up, origin);
1213                         AnglesFromVectors(ang, forward, up, false);
1214                         PRVM_clientglobalfloat(time) = cl.time;
1215                         PRVM_clientglobaledict(self) = entnum;
1216                         VectorCopy(origin, PRVM_G_VECTOR(OFS_PARM0));
1217                         VectorCopy(ang, PRVM_G_VECTOR(OFS_PARM1));
1218                         VectorCopy(forward, PRVM_clientglobalvector(v_forward));
1219                         VectorScale(left, -1, PRVM_clientglobalvector(v_right));
1220                         VectorCopy(up, PRVM_clientglobalvector(v_up));
1221                         VectorCopy(origin, PRVM_clientglobalvector(trace_endpos));
1222                         prog->ExecuteProgram(prog, PRVM_clientedictfunction(ed, camera_transform), "QC function e.camera_transform is missing");
1223                         VectorCopy(PRVM_G_VECTOR(OFS_RETURN), origin);
1224                         VectorCopy(PRVM_clientglobalvector(v_forward), forward);
1225                         VectorScale(PRVM_clientglobalvector(v_right), -1, left);
1226                         VectorCopy(PRVM_clientglobalvector(v_up), up);
1227                         VectorCopy(PRVM_clientglobalvector(trace_endpos), visorigin);
1228                         Matrix4x4_Invert_Full(&mat, viewmatrix);
1229                         Matrix4x4_FromVectors(viewmatrix, forward, left, up, origin);
1230                         Matrix4x4_Concat(&matq, viewmatrix, &mat);
1231                         Matrix4x4_TransformPositivePlane(&matq, clipplane->normal[0], clipplane->normal[1], clipplane->normal[2], clipplane->dist, clipplane->normal_and_dist);
1232                 }
1233         }
1234
1235         return ret;
1236 }
1237
1238 int CL_VM_GetViewEntity(void)
1239 {
1240         if(cl.csqc_server2csqcentitynumber[cl.viewentity])
1241                 return cl.csqc_server2csqcentitynumber[cl.viewentity] + MAX_EDICTS;
1242         return cl.viewentity;
1243 }