3 #include "clprogdefs.h"
5 #include "cl_collision.h"
10 //============================================================================
11 // Client prog handling
12 //[515]: omg !!! optimize it ! a lot of hacks here and there also :P
14 #define CSQC_RETURNVAL prog->globals.generic[OFS_RETURN]
15 #define CSQC_BEGIN csqc_tmpprog=prog;prog=0;PRVM_SetProg(PRVM_CLIENTPROG);
16 #define CSQC_END prog=csqc_tmpprog;
18 static prvm_prog_t *csqc_tmpprog;
20 void CL_VM_PreventInformationLeaks(void)
25 VM_ClearTraceGlobals();
26 PRVM_clientglobalfloat(trace_networkentity) = 0;
30 //[515]: these are required funcs
31 static const char *cl_required_func[] =
36 "CSQC_ConsoleCommand",
39 static int cl_numrequiredfunc = sizeof(cl_required_func) / sizeof(char*);
41 #define CL_REQFIELDS (sizeof(cl_reqfields) / sizeof(prvm_required_field_t))
43 prvm_required_field_t cl_reqfields[] =
45 #define PRVM_DECLARE_serverglobalfloat(x)
46 #define PRVM_DECLARE_serverglobalvector(x)
47 #define PRVM_DECLARE_serverglobalstring(x)
48 #define PRVM_DECLARE_serverglobaledict(x)
49 #define PRVM_DECLARE_serverglobalfunction(x)
50 #define PRVM_DECLARE_clientglobalfloat(x)
51 #define PRVM_DECLARE_clientglobalvector(x)
52 #define PRVM_DECLARE_clientglobalstring(x)
53 #define PRVM_DECLARE_clientglobaledict(x)
54 #define PRVM_DECLARE_clientglobalfunction(x)
55 #define PRVM_DECLARE_menuglobalfloat(x)
56 #define PRVM_DECLARE_menuglobalvector(x)
57 #define PRVM_DECLARE_menuglobalstring(x)
58 #define PRVM_DECLARE_menuglobaledict(x)
59 #define PRVM_DECLARE_menuglobalfunction(x)
60 #define PRVM_DECLARE_serverfieldfloat(x)
61 #define PRVM_DECLARE_serverfieldvector(x)
62 #define PRVM_DECLARE_serverfieldstring(x)
63 #define PRVM_DECLARE_serverfieldedict(x)
64 #define PRVM_DECLARE_serverfieldfunction(x)
65 #define PRVM_DECLARE_clientfieldfloat(x) {ev_float, #x },
66 #define PRVM_DECLARE_clientfieldvector(x) {ev_vector, #x },
67 #define PRVM_DECLARE_clientfieldstring(x) {ev_string, #x },
68 #define PRVM_DECLARE_clientfieldedict(x) {ev_entity, #x },
69 #define PRVM_DECLARE_clientfieldfunction(x) {ev_function, #x },
70 #define PRVM_DECLARE_menufieldfloat(x)
71 #define PRVM_DECLARE_menufieldvector(x)
72 #define PRVM_DECLARE_menufieldstring(x)
73 #define PRVM_DECLARE_menufieldedict(x)
74 #define PRVM_DECLARE_menufieldfunction(x)
75 #define PRVM_DECLARE_serverfunction(x)
76 #define PRVM_DECLARE_clientfunction(x)
77 #define PRVM_DECLARE_menufunction(x)
78 #define PRVM_DECLARE_field(x)
79 #define PRVM_DECLARE_global(x)
80 #define PRVM_DECLARE_function(x)
81 #include "prvm_offsets.h"
82 #undef PRVM_DECLARE_serverglobalfloat
83 #undef PRVM_DECLARE_serverglobalvector
84 #undef PRVM_DECLARE_serverglobalstring
85 #undef PRVM_DECLARE_serverglobaledict
86 #undef PRVM_DECLARE_serverglobalfunction
87 #undef PRVM_DECLARE_clientglobalfloat
88 #undef PRVM_DECLARE_clientglobalvector
89 #undef PRVM_DECLARE_clientglobalstring
90 #undef PRVM_DECLARE_clientglobaledict
91 #undef PRVM_DECLARE_clientglobalfunction
92 #undef PRVM_DECLARE_menuglobalfloat
93 #undef PRVM_DECLARE_menuglobalvector
94 #undef PRVM_DECLARE_menuglobalstring
95 #undef PRVM_DECLARE_menuglobaledict
96 #undef PRVM_DECLARE_menuglobalfunction
97 #undef PRVM_DECLARE_serverfieldfloat
98 #undef PRVM_DECLARE_serverfieldvector
99 #undef PRVM_DECLARE_serverfieldstring
100 #undef PRVM_DECLARE_serverfieldedict
101 #undef PRVM_DECLARE_serverfieldfunction
102 #undef PRVM_DECLARE_clientfieldfloat
103 #undef PRVM_DECLARE_clientfieldvector
104 #undef PRVM_DECLARE_clientfieldstring
105 #undef PRVM_DECLARE_clientfieldedict
106 #undef PRVM_DECLARE_clientfieldfunction
107 #undef PRVM_DECLARE_menufieldfloat
108 #undef PRVM_DECLARE_menufieldvector
109 #undef PRVM_DECLARE_menufieldstring
110 #undef PRVM_DECLARE_menufieldedict
111 #undef PRVM_DECLARE_menufieldfunction
112 #undef PRVM_DECLARE_serverfunction
113 #undef PRVM_DECLARE_clientfunction
114 #undef PRVM_DECLARE_menufunction
115 #undef PRVM_DECLARE_field
116 #undef PRVM_DECLARE_global
117 #undef PRVM_DECLARE_function
120 #define CL_REQGLOBALS (sizeof(cl_reqglobals) / sizeof(prvm_required_field_t))
122 prvm_required_field_t cl_reqglobals[] =
124 #define PRVM_DECLARE_serverglobalfloat(x)
125 #define PRVM_DECLARE_serverglobalvector(x)
126 #define PRVM_DECLARE_serverglobalstring(x)
127 #define PRVM_DECLARE_serverglobaledict(x)
128 #define PRVM_DECLARE_serverglobalfunction(x)
129 #define PRVM_DECLARE_clientglobalfloat(x) {ev_float, #x},
130 #define PRVM_DECLARE_clientglobalvector(x) {ev_vector, #x},
131 #define PRVM_DECLARE_clientglobalstring(x) {ev_string, #x},
132 #define PRVM_DECLARE_clientglobaledict(x) {ev_entity, #x},
133 #define PRVM_DECLARE_clientglobalfunction(x) {ev_function, #x},
134 #define PRVM_DECLARE_menuglobalfloat(x)
135 #define PRVM_DECLARE_menuglobalvector(x)
136 #define PRVM_DECLARE_menuglobalstring(x)
137 #define PRVM_DECLARE_menuglobaledict(x)
138 #define PRVM_DECLARE_menuglobalfunction(x)
139 #define PRVM_DECLARE_serverfieldfloat(x)
140 #define PRVM_DECLARE_serverfieldvector(x)
141 #define PRVM_DECLARE_serverfieldstring(x)
142 #define PRVM_DECLARE_serverfieldedict(x)
143 #define PRVM_DECLARE_serverfieldfunction(x)
144 #define PRVM_DECLARE_clientfieldfloat(x)
145 #define PRVM_DECLARE_clientfieldvector(x)
146 #define PRVM_DECLARE_clientfieldstring(x)
147 #define PRVM_DECLARE_clientfieldedict(x)
148 #define PRVM_DECLARE_clientfieldfunction(x)
149 #define PRVM_DECLARE_menufieldfloat(x)
150 #define PRVM_DECLARE_menufieldvector(x)
151 #define PRVM_DECLARE_menufieldstring(x)
152 #define PRVM_DECLARE_menufieldedict(x)
153 #define PRVM_DECLARE_menufieldfunction(x)
154 #define PRVM_DECLARE_serverfunction(x)
155 #define PRVM_DECLARE_clientfunction(x)
156 #define PRVM_DECLARE_menufunction(x)
157 #define PRVM_DECLARE_field(x)
158 #define PRVM_DECLARE_global(x)
159 #define PRVM_DECLARE_function(x)
160 #include "prvm_offsets.h"
161 #undef PRVM_DECLARE_serverglobalfloat
162 #undef PRVM_DECLARE_serverglobalvector
163 #undef PRVM_DECLARE_serverglobalstring
164 #undef PRVM_DECLARE_serverglobaledict
165 #undef PRVM_DECLARE_serverglobalfunction
166 #undef PRVM_DECLARE_clientglobalfloat
167 #undef PRVM_DECLARE_clientglobalvector
168 #undef PRVM_DECLARE_clientglobalstring
169 #undef PRVM_DECLARE_clientglobaledict
170 #undef PRVM_DECLARE_clientglobalfunction
171 #undef PRVM_DECLARE_menuglobalfloat
172 #undef PRVM_DECLARE_menuglobalvector
173 #undef PRVM_DECLARE_menuglobalstring
174 #undef PRVM_DECLARE_menuglobaledict
175 #undef PRVM_DECLARE_menuglobalfunction
176 #undef PRVM_DECLARE_serverfieldfloat
177 #undef PRVM_DECLARE_serverfieldvector
178 #undef PRVM_DECLARE_serverfieldstring
179 #undef PRVM_DECLARE_serverfieldedict
180 #undef PRVM_DECLARE_serverfieldfunction
181 #undef PRVM_DECLARE_clientfieldfloat
182 #undef PRVM_DECLARE_clientfieldvector
183 #undef PRVM_DECLARE_clientfieldstring
184 #undef PRVM_DECLARE_clientfieldedict
185 #undef PRVM_DECLARE_clientfieldfunction
186 #undef PRVM_DECLARE_menufieldfloat
187 #undef PRVM_DECLARE_menufieldvector
188 #undef PRVM_DECLARE_menufieldstring
189 #undef PRVM_DECLARE_menufieldedict
190 #undef PRVM_DECLARE_menufieldfunction
191 #undef PRVM_DECLARE_serverfunction
192 #undef PRVM_DECLARE_clientfunction
193 #undef PRVM_DECLARE_menufunction
194 #undef PRVM_DECLARE_field
195 #undef PRVM_DECLARE_global
196 #undef PRVM_DECLARE_function
199 void CL_VM_Error (const char *format, ...) DP_FUNC_PRINTF(1);
200 void CL_VM_Error (const char *format, ...) //[515]: hope it will be never executed =)
202 char errorstring[4096];
205 va_start (argptr, format);
206 dpvsnprintf (errorstring, sizeof(errorstring), format, argptr);
208 // Con_Printf( "CL_VM_Error: %s\n", errorstring );
211 cl.csqc_loaded = false;
213 Cvar_SetValueQuick(&csqc_progcrc, -1);
214 Cvar_SetValueQuick(&csqc_progsize, -1);
216 // Host_AbortCurrentFrame(); //[515]: hmmm... if server says it needs csqc then client MUST disconnect
217 Host_Error("CL_VM_Error: %s", errorstring);
219 void CL_VM_UpdateDmgGlobals (int dmg_take, int dmg_save, vec3_t dmg_origin)
224 PRVM_clientglobalfloat(dmg_take) = dmg_take;
225 PRVM_clientglobalfloat(dmg_save) = dmg_save;
226 VectorCopy(dmg_origin, PRVM_clientglobalvector(dmg_origin));
231 void CSQC_UpdateNetworkTimes(double newtime, double oldtime)
236 PRVM_clientglobalfloat(servertime) = newtime;
237 PRVM_clientglobalfloat(serverprevtime) = oldtime;
238 PRVM_clientglobalfloat(serverdeltatime) = newtime - oldtime;
242 //[515]: set globals before calling R_UpdateView, WEIRD CRAP
243 void CSQC_R_RecalcView (void);
244 static void CSQC_SetGlobals (void)
247 PRVM_clientglobalfloat(time) = cl.time;
248 PRVM_clientglobalfloat(frametime) = max(0, cl.time - cl.oldtime);
249 PRVM_clientglobalfloat(servercommandframe) = cls.servermovesequence;
250 PRVM_clientglobalfloat(clientcommandframe) = cl.movecmd[0].sequence;
251 VectorCopy(cl.viewangles, PRVM_clientglobalvector(input_angles));
252 // // FIXME: this actually belongs into getinputstate().. [12/17/2007 Black]
253 PRVM_clientglobalfloat(input_buttons) = cl.movecmd[0].buttons;
254 VectorSet(PRVM_clientglobalvector(input_movevalues), cl.movecmd[0].forwardmove, cl.movecmd[0].sidemove, cl.movecmd[0].upmove);
255 VectorCopy(cl.csqc_vieworiginfromengine, cl.csqc_vieworigin);
256 VectorCopy(cl.csqc_viewanglesfromengine, cl.csqc_viewangles);
258 // LordHavoc: Spike says not to do this, but without pmove_org the
259 // CSQC is useless as it can't alter the view origin without
260 // completely replacing it
261 Matrix4x4_OriginFromMatrix(&cl.entities[cl.viewentity].render.matrix, PRVM_clientglobalvector(pmove_org));
262 VectorCopy(cl.movement_velocity, PRVM_clientglobalvector(pmove_vel));
263 PRVM_clientglobalfloat(pmove_onground) = cl.onground;
264 PRVM_clientglobalfloat(pmove_inwater) = cl.inwater;
266 VectorCopy(cl.viewangles, PRVM_clientglobalvector(view_angles));
267 VectorCopy(cl.punchangle, PRVM_clientglobalvector(view_punchangle));
268 VectorCopy(cl.punchvector, PRVM_clientglobalvector(view_punchvector));
269 PRVM_clientglobalfloat(maxclients) = cl.maxclients;
275 void CSQC_Predraw (prvm_edict_t *ed)
278 if(!PRVM_clientedictfunction(ed, predraw))
280 b = PRVM_clientglobaledict(self);
281 PRVM_clientglobaledict(self) = PRVM_EDICT_TO_PROG(ed);
282 PRVM_ExecuteProgram(PRVM_clientedictfunction(ed, predraw), "CSQC_Predraw: NULL function\n");
283 PRVM_clientglobaledict(self) = b;
286 void CSQC_Think (prvm_edict_t *ed)
289 if(PRVM_clientedictfunction(ed, think))
290 if(PRVM_clientedictfloat(ed, nextthink) && PRVM_clientedictfloat(ed, nextthink) <= PRVM_clientglobalfloat(time))
292 PRVM_clientedictfloat(ed, nextthink) = 0;
293 b = PRVM_clientglobaledict(self);
294 PRVM_clientglobaledict(self) = PRVM_EDICT_TO_PROG(ed);
295 PRVM_ExecuteProgram(PRVM_clientedictfunction(ed, think), "CSQC_Think: NULL function\n");
296 PRVM_clientglobaledict(self) = b;
300 extern cvar_t cl_noplayershadow;
301 extern cvar_t r_equalize_entities_fullbright;
302 qboolean CSQC_AddRenderEdict(prvm_edict_t *ed, int edictnum)
307 entity_render_t *entrender;
310 model = CL_GetModelFromEdict(ed);
316 if (r_refdef.scene.numentities >= r_refdef.scene.maxentities)
318 entrender = cl.csqcrenderentities + edictnum;
319 r_refdef.scene.entities[r_refdef.scene.numentities++] = entrender;
320 entrender->entitynumber = edictnum + MAX_EDICTS;
321 //entrender->shadertime = 0; // shadertime was set by spawn()
322 entrender->flags = 0;
323 entrender->alpha = 1;
324 entrender->scale = 1;
325 VectorSet(entrender->colormod, 1, 1, 1);
326 VectorSet(entrender->glowmod, 1, 1, 1);
327 entrender->allowdecals = true;
331 entrender = CL_NewTempEntity(0);
336 entrender->userwavefunc_param[0] = PRVM_clientedictfloat(ed, userwavefunc_param0);
337 entrender->userwavefunc_param[1] = PRVM_clientedictfloat(ed, userwavefunc_param1);
338 entrender->userwavefunc_param[2] = PRVM_clientedictfloat(ed, userwavefunc_param2);
339 entrender->userwavefunc_param[3] = PRVM_clientedictfloat(ed, userwavefunc_param3);
341 entrender->model = model;
342 entrender->skinnum = (int)PRVM_clientedictfloat(ed, skin);
343 entrender->effects |= entrender->model->effects;
344 renderflags = (int)PRVM_clientedictfloat(ed, renderflags);
345 entrender->alpha = PRVM_clientedictfloat(ed, alpha);
346 entrender->scale = scale = PRVM_clientedictfloat(ed, scale);
347 VectorCopy(PRVM_clientedictvector(ed, colormod), entrender->colormod);
348 VectorCopy(PRVM_clientedictvector(ed, glowmod), entrender->glowmod);
349 if(PRVM_clientedictfloat(ed, effects)) entrender->effects |= (int)PRVM_clientedictfloat(ed, effects);
350 if (!entrender->alpha)
351 entrender->alpha = 1.0f;
352 if (!entrender->scale)
353 entrender->scale = scale = 1.0f;
354 if (!VectorLength2(entrender->colormod))
355 VectorSet(entrender->colormod, 1, 1, 1);
356 if (!VectorLength2(entrender->glowmod))
357 VectorSet(entrender->glowmod, 1, 1, 1);
359 // LordHavoc: use the CL_GetTagMatrix function on self to ensure consistent behavior (duplicate code would be bad)
360 CL_GetTagMatrix(&entrender->matrix, ed, 0);
362 // set up the animation data
363 VM_GenerateFrameGroupBlend(ed->priv.server->framegroupblend, ed);
364 VM_FrameBlendFromFrameGroupBlend(ed->priv.server->frameblend, ed->priv.server->framegroupblend, model);
365 VM_UpdateEdictSkeleton(ed, model, ed->priv.server->frameblend);
366 if (PRVM_clientedictfloat(ed, shadertime)) // hack for csprogs.dat files that do not set shadertime, leaves the value at entity spawn time
367 entrender->shadertime = PRVM_clientedictfloat(ed, shadertime);
369 // transparent offset
370 if (renderflags & RF_USETRANSPARENTOFFSET)
371 entrender->transparent_offset = PRVM_clientglobalfloat(transparent_offset);
375 if(renderflags & RF_VIEWMODEL) entrender->flags |= RENDER_VIEWMODEL | RENDER_NODEPTHTEST;
376 if(renderflags & RF_EXTERNALMODEL)entrender->flags |= RENDER_EXTERIORMODEL;
377 if(renderflags & RF_NOCULL) entrender->flags |= RENDER_NOCULL;
378 if(renderflags & RF_DEPTHHACK) entrender->flags |= RENDER_NODEPTHTEST;
379 if(renderflags & RF_ADDITIVE) entrender->flags |= RENDER_ADDITIVE;
382 c = (int)PRVM_clientedictfloat(ed, colormap);
384 CL_SetEntityColormapColors(entrender, -1);
385 else if (c <= cl.maxclients && cl.scores != NULL)
386 CL_SetEntityColormapColors(entrender, cl.scores[c-1].colors);
388 CL_SetEntityColormapColors(entrender, c);
390 entrender->flags &= ~(RENDER_SHADOW | RENDER_LIGHT | RENDER_NOSELFSHADOW);
391 // either fullbright or lit
392 if(!r_fullbright.integer)
394 if (!(entrender->effects & EF_FULLBRIGHT) && !(renderflags & RF_FULLBRIGHT))
395 entrender->flags |= RENDER_LIGHT;
396 else if(r_equalize_entities_fullbright.integer)
397 entrender->flags |= RENDER_LIGHT | RENDER_EQUALIZE;
399 // hide player shadow during intermission or nehahra movie
400 if (!(entrender->effects & (EF_NOSHADOW | EF_ADDITIVE | EF_NODEPTHTEST))
401 && (entrender->alpha >= 1)
402 && !(renderflags & RF_NOSHADOW)
403 && !(entrender->flags & RENDER_VIEWMODEL)
404 && (!(entrender->flags & RENDER_EXTERIORMODEL) || (!cl.intermission && cls.protocol != PROTOCOL_NEHAHRAMOVIE && !cl_noplayershadow.integer)))
405 entrender->flags |= RENDER_SHADOW;
406 if (entrender->flags & RENDER_VIEWMODEL)
407 entrender->flags |= RENDER_NOSELFSHADOW;
408 if (entrender->effects & EF_NOSELFSHADOW)
409 entrender->flags |= RENDER_NOSELFSHADOW;
410 if (entrender->effects & EF_NODEPTHTEST)
411 entrender->flags |= RENDER_NODEPTHTEST;
412 if (entrender->effects & EF_ADDITIVE)
413 entrender->flags |= RENDER_ADDITIVE;
414 if (entrender->effects & EF_DOUBLESIDED)
415 entrender->flags |= RENDER_DOUBLESIDED;
417 // make the other useful stuff
418 memcpy(entrender->framegroupblend, ed->priv.server->framegroupblend, sizeof(ed->priv.server->framegroupblend));
419 CL_UpdateRenderEntity(entrender);
420 // override animation data with full control
421 memcpy(entrender->frameblend, ed->priv.server->frameblend, sizeof(ed->priv.server->frameblend));
422 if (ed->priv.server->skeleton.relativetransforms)
423 entrender->skeleton = &ed->priv.server->skeleton;
425 entrender->skeleton = NULL;
430 qboolean CL_VM_InputEvent (qboolean down, int key, int ascii)
438 if (!PRVM_clientfunction(CSQC_InputEvent))
442 PRVM_clientglobalfloat(time) = cl.time;
443 PRVM_clientglobaledict(self) = cl.csqc_server2csqcentitynumber[cl.playerentity];
444 PRVM_G_FLOAT(OFS_PARM0) = !down; // 0 is down, 1 is up
445 PRVM_G_FLOAT(OFS_PARM1) = key;
446 PRVM_G_FLOAT(OFS_PARM2) = ascii;
447 PRVM_ExecuteProgram(PRVM_clientfunction(CSQC_InputEvent), "QC function CSQC_InputEvent is missing");
448 r = CSQC_RETURNVAL != 0;
454 qboolean CL_VM_UpdateView (void)
463 R_TimeReport("pre-UpdateView");
465 //VectorCopy(cl.viewangles, oldangles);
466 PRVM_clientglobalfloat(time) = cl.time;
467 PRVM_clientglobaledict(self) = cl.csqc_server2csqcentitynumber[cl.playerentity];
469 // clear renderable entity and light lists to prevent crashes if the
470 // CSQC_UpdateView function does not call R_ClearScene as it should
471 r_refdef.scene.numentities = 0;
472 r_refdef.scene.numlights = 0;
473 // pass in width and height as parameters (EXT_CSQC_1)
474 PRVM_G_FLOAT(OFS_PARM0) = vid.width;
475 PRVM_G_FLOAT(OFS_PARM1) = vid.height;
476 PRVM_ExecuteProgram(PRVM_clientfunction(CSQC_UpdateView), "QC function CSQC_UpdateView is missing");
477 //VectorCopy(oldangles, cl.viewangles);
478 // Dresk : Reset Dmg Globals Here
479 CL_VM_UpdateDmgGlobals(0, 0, emptyvector);
481 R_TimeReport("UpdateView");
485 extern sizebuf_t vm_tempstringsbuf;
486 qboolean CL_VM_ConsoleCommand (const char *cmd)
488 int restorevm_tempstringsbuf_cursize;
493 if (PRVM_clientfunction(CSQC_ConsoleCommand))
495 PRVM_clientglobalfloat(time) = cl.time;
496 PRVM_clientglobaledict(self) = cl.csqc_server2csqcentitynumber[cl.playerentity];
497 restorevm_tempstringsbuf_cursize = vm_tempstringsbuf.cursize;
498 PRVM_G_INT(OFS_PARM0) = PRVM_SetTempString(cmd);
499 PRVM_ExecuteProgram(PRVM_clientfunction(CSQC_ConsoleCommand), "QC function CSQC_ConsoleCommand is missing");
500 vm_tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize;
501 r = CSQC_RETURNVAL != 0;
507 qboolean CL_VM_Parse_TempEntity (void)
514 if(PRVM_clientfunction(CSQC_Parse_TempEntity))
517 PRVM_clientglobalfloat(time) = cl.time;
518 PRVM_clientglobaledict(self) = cl.csqc_server2csqcentitynumber[cl.playerentity];
519 PRVM_ExecuteProgram(PRVM_clientfunction(CSQC_Parse_TempEntity), "QC function CSQC_Parse_TempEntity is missing");
520 r = CSQC_RETURNVAL != 0;
531 void CL_VM_Parse_StuffCmd (const char *msg)
533 int restorevm_tempstringsbuf_cursize;
539 // if this is setting a csqc variable, deprotect csqc_progcrc
540 // temporarily so that it can be set by the cvar command,
541 // and then reprotect it afterwards
542 int crcflags = csqc_progcrc.flags;
543 int sizeflags = csqc_progcrc.flags;
544 csqc_progcrc.flags &= ~CVAR_READONLY;
545 csqc_progsize.flags &= ~CVAR_READONLY;
546 Cmd_ExecuteString (msg, src_command);
547 csqc_progcrc.flags = crcflags;
548 csqc_progsize.flags = sizeflags;
553 if(!strncmp(msg, "curl --clear_autodownload\ncurl --pak --forthismap --as ", 55))
555 // special handling for map download commands
556 // run these commands IMMEDIATELY, instead of waiting for a client frame
557 // that way, there is no black screen when playing back demos
558 // I know this is a really ugly hack, but I can't think of any better way
559 // FIXME find the actual CAUSE of this, and make demo playback WAIT
560 // until all maps are loaded, then remove this hack
562 char buf[MAX_INPUTLINE];
575 if(l > sizeof(buf) - 1)
577 strlcpy(buf, p, l + 1); // strlcpy needs a + 1 as it includes the newline!
579 Cmd_ExecuteString(buf, src_command);
583 ++p; // skip the newline and continue
585 break; // end of string or overflow
587 Cmd_ExecuteString("curl --clear_autodownload", src_command); // don't inhibit CSQC loading
597 if(PRVM_clientfunction(CSQC_Parse_StuffCmd))
599 PRVM_clientglobalfloat(time) = cl.time;
600 PRVM_clientglobaledict(self) = cl.csqc_server2csqcentitynumber[cl.playerentity];
601 restorevm_tempstringsbuf_cursize = vm_tempstringsbuf.cursize;
602 PRVM_G_INT(OFS_PARM0) = PRVM_SetTempString(msg);
603 PRVM_ExecuteProgram(PRVM_clientfunction(CSQC_Parse_StuffCmd), "QC function CSQC_Parse_StuffCmd is missing");
604 vm_tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize;
611 static void CL_VM_Parse_Print (const char *msg)
613 int restorevm_tempstringsbuf_cursize;
614 PRVM_clientglobalfloat(time) = cl.time;
615 PRVM_clientglobaledict(self) = cl.csqc_server2csqcentitynumber[cl.playerentity];
616 restorevm_tempstringsbuf_cursize = vm_tempstringsbuf.cursize;
617 PRVM_G_INT(OFS_PARM0) = PRVM_SetTempString(msg);
618 PRVM_ExecuteProgram(PRVM_clientfunction(CSQC_Parse_Print), "QC function CSQC_Parse_Print is missing");
619 vm_tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize;
622 void CSQC_AddPrintText (const char *msg)
631 if(PRVM_clientfunction(CSQC_Parse_Print))
633 // FIXME: is this bugged?
635 if(msg[i] != '\n' && msg[i] != '\r')
637 if(strlen(cl.csqc_printtextbuf)+i >= MAX_INPUTLINE)
639 CL_VM_Parse_Print(cl.csqc_printtextbuf);
640 cl.csqc_printtextbuf[0] = 0;
643 strlcat(cl.csqc_printtextbuf, msg, MAX_INPUTLINE);
646 strlcat(cl.csqc_printtextbuf, msg, MAX_INPUTLINE);
647 CL_VM_Parse_Print(cl.csqc_printtextbuf);
648 cl.csqc_printtextbuf[0] = 0;
655 void CL_VM_Parse_CenterPrint (const char *msg)
657 int restorevm_tempstringsbuf_cursize;
660 SCR_CenterPrint(msg);
664 if(PRVM_clientfunction(CSQC_Parse_CenterPrint))
666 PRVM_clientglobalfloat(time) = cl.time;
667 PRVM_clientglobaledict(self) = cl.csqc_server2csqcentitynumber[cl.playerentity];
668 restorevm_tempstringsbuf_cursize = vm_tempstringsbuf.cursize;
669 PRVM_G_INT(OFS_PARM0) = PRVM_SetTempString(msg);
670 PRVM_ExecuteProgram(PRVM_clientfunction(CSQC_Parse_CenterPrint), "QC function CSQC_Parse_CenterPrint is missing");
671 vm_tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize;
674 SCR_CenterPrint(msg);
678 void CL_VM_UpdateIntermissionState (int intermission)
683 PRVM_clientglobalfloat(intermission) = intermission;
687 void CL_VM_UpdateShowingScoresState (int showingscores)
692 PRVM_clientglobalfloat(sb_showscores) = showingscores;
696 qboolean CL_VM_Event_Sound(int sound_num, float volume, int channel, float attenuation, int ent, vec3_t pos)
702 if(PRVM_clientfunction(CSQC_Event_Sound))
704 PRVM_clientglobalfloat(time) = cl.time;
705 PRVM_clientglobaledict(self) = cl.csqc_server2csqcentitynumber[cl.playerentity];
706 PRVM_G_FLOAT(OFS_PARM0) = ent;
707 PRVM_G_FLOAT(OFS_PARM1) = CHAN_ENGINE2USER(channel);
708 PRVM_G_INT(OFS_PARM2) = PRVM_SetTempString(cl.sound_name[sound_num] );
709 PRVM_G_FLOAT(OFS_PARM3) = volume;
710 PRVM_G_FLOAT(OFS_PARM4) = attenuation;
711 VectorCopy(pos, PRVM_G_VECTOR(OFS_PARM5) );
712 PRVM_G_FLOAT(OFS_PARM6) = 0; // pitch shift not supported yet
713 PRVM_G_FLOAT(OFS_PARM7) = 0; // flags - none can come in at this point yet
714 PRVM_ExecuteProgram(PRVM_clientfunction(CSQC_Event_Sound), "QC function CSQC_Event_Sound is missing");
715 r = CSQC_RETURNVAL != 0;
722 void CL_VM_UpdateCoopDeathmatchGlobals (int gametype)
724 // Avoid global names for clean(er) coding
730 if(gametype == GAME_COOP)
736 if(gametype == GAME_DEATHMATCH)
743 // How did the ServerInfo send an unknown gametype?
744 // Better just assign the globals as 0...
749 PRVM_clientglobalfloat(coop) = localcoop;
750 PRVM_clientglobalfloat(deathmatch) = localdeathmatch;
754 float CL_VM_Event (float event) //[515]: needed ? I'd say "YES", but don't know for what :D
760 if(PRVM_clientfunction(CSQC_Event))
762 PRVM_clientglobalfloat(time) = cl.time;
763 PRVM_clientglobaledict(self) = cl.csqc_server2csqcentitynumber[cl.playerentity];
764 PRVM_G_FLOAT(OFS_PARM0) = event;
765 PRVM_ExecuteProgram(PRVM_clientfunction(CSQC_Event), "QC function CSQC_Event is missing");
772 void CSQC_ReadEntities (void)
774 unsigned short entnum, oldself, realentnum;
777 Host_Error ("CSQC_ReadEntities: CSQC is not loaded");
782 PRVM_clientglobalfloat(time) = cl.time;
783 oldself = PRVM_clientglobaledict(self);
786 entnum = MSG_ReadShort();
787 if(!entnum || msg_badread)
789 realentnum = entnum & 0x7FFF;
790 PRVM_clientglobaledict(self) = cl.csqc_server2csqcentitynumber[realentnum];
793 if(PRVM_clientglobaledict(self))
795 PRVM_ExecuteProgram(PRVM_clientfunction(CSQC_Ent_Remove), "QC function CSQC_Ent_Remove is missing");
796 cl.csqc_server2csqcentitynumber[realentnum] = 0;
800 // LordHavoc: removing an entity that is already gone on
801 // the csqc side is possible for legitimate reasons (such
802 // as a repeat of the remove message), so no warning is
804 //Con_Printf("Bad csqc_server2csqcentitynumber map\n"); //[515]: never happens ?
809 if(!PRVM_clientglobaledict(self))
811 if(!PRVM_clientfunction(CSQC_Ent_Spawn))
814 ed = PRVM_ED_Alloc();
815 PRVM_clientedictfloat(ed, entnum) = realentnum;
816 PRVM_clientglobaledict(self) = cl.csqc_server2csqcentitynumber[realentnum] = PRVM_EDICT_TO_PROG(ed);
820 // entity( float entnum ) CSQC_Ent_Spawn;
821 // the qc function should set entnum, too (this way it also can return world [2/1/2008 Andreas]
822 PRVM_G_FLOAT(OFS_PARM0) = (float) realentnum;
823 // make sure no one gets wrong ideas
824 PRVM_clientglobaledict(self) = 0;
825 PRVM_ExecuteProgram(PRVM_clientfunction(CSQC_Ent_Spawn), "QC function CSQC_Ent_Spawn is missing");
826 PRVM_clientglobaledict(self) = cl.csqc_server2csqcentitynumber[realentnum] = PRVM_EDICT( PRVM_G_INT( OFS_RETURN ) );
828 PRVM_G_FLOAT(OFS_PARM0) = 1;
829 PRVM_ExecuteProgram(PRVM_clientfunction(CSQC_Ent_Update), "QC function CSQC_Ent_Update is missing");
832 PRVM_G_FLOAT(OFS_PARM0) = 0;
833 PRVM_ExecuteProgram(PRVM_clientfunction(CSQC_Ent_Update), "QC function CSQC_Ent_Update is missing");
837 PRVM_clientglobaledict(self) = oldself;
841 void CL_VM_CB_BeginIncreaseEdicts(void)
843 // links don't survive the transition, so unlink everything
844 World_UnlinkAll(&cl.world);
847 void CL_VM_CB_EndIncreaseEdicts(void)
852 // link every entity except world
853 for (i = 1, ent = prog->edicts;i < prog->num_edicts;i++, ent++)
854 if (!ent->priv.server->free)
858 void CL_VM_CB_InitEdict(prvm_edict_t *e)
860 int edictnum = PRVM_NUM_FOR_EDICT(e);
861 entity_render_t *entrender;
862 CL_ExpandCSQCRenderEntities(edictnum);
863 entrender = cl.csqcrenderentities + edictnum;
864 e->priv.server->move = false; // don't move on first frame
865 memset(entrender, 0, sizeof(*entrender));
866 entrender->shadertime = cl.time;
869 extern void R_DecalSystem_Reset(decalsystem_t *decalsystem);
871 void CL_VM_CB_FreeEdict(prvm_edict_t *ed)
873 entity_render_t *entrender = cl.csqcrenderentities + PRVM_NUM_FOR_EDICT(ed);
874 R_DecalSystem_Reset(&entrender->decalsystem);
875 memset(entrender, 0, sizeof(*entrender));
876 World_UnlinkEdict(ed);
877 memset(ed->fields.vp, 0, prog->entityfields * 4);
878 VM_RemoveEdictSkeleton(ed);
879 World_Physics_RemoveFromEntity(&cl.world, ed);
880 World_Physics_RemoveJointFromEntity(&cl.world, ed);
883 void CL_VM_CB_CountEdicts(void)
887 int active = 0, models = 0, solid = 0;
889 for (i=0 ; i<prog->num_edicts ; i++)
891 ent = PRVM_EDICT_NUM(i);
892 if (ent->priv.server->free)
895 if (PRVM_clientedictfloat(ent, solid))
897 if (PRVM_clientedictstring(ent, model))
901 Con_Printf("num_edicts:%3i\n", prog->num_edicts);
902 Con_Printf("active :%3i\n", active);
903 Con_Printf("view :%3i\n", models);
904 Con_Printf("touch :%3i\n", solid);
907 qboolean CL_VM_CB_LoadEdict(prvm_edict_t *ent)
912 void Cmd_ClearCsqcFuncs (void);
914 // returns true if the packet is valid, false if end of file is reached
915 // used for dumping the CSQC download into demo files
916 qboolean MakeDownloadPacket(const char *filename, unsigned char *data, size_t len, int crc, int cnt, sizebuf_t *buf, int protocol)
918 int packetsize = buf->maxsize - 7; // byte short long
919 int npackets = (len + packetsize - 1) / (packetsize);
921 if(protocol == PROTOCOL_QUAKEWORLD)
922 return false; // CSQC can't run in QW anyway
927 MSG_WriteByte(buf, svc_stufftext);
928 MSG_WriteString(buf, va("\ncl_downloadbegin %lu %s\n", (unsigned long)len, filename));
931 else if(cnt >= 1 && cnt <= npackets)
933 unsigned long thispacketoffset = (cnt - 1) * packetsize;
934 int thispacketsize = len - thispacketoffset;
935 if(thispacketsize > packetsize)
936 thispacketsize = packetsize;
938 MSG_WriteByte(buf, svc_downloaddata);
939 MSG_WriteLong(buf, thispacketoffset);
940 MSG_WriteShort(buf, thispacketsize);
941 SZ_Write(buf, data + thispacketoffset, thispacketsize);
945 else if(cnt == npackets + 1)
947 MSG_WriteByte(buf, svc_stufftext);
948 MSG_WriteString(buf, va("\ncl_downloadfinished %lu %d\n", (unsigned long)len, crc));
954 void CL_VM_Init (void)
956 const char* csprogsfn;
957 unsigned char *csprogsdata;
958 fs_offset_t csprogsdatasize;
959 int csprogsdatacrc, requiredcrc;
962 // reset csqc_progcrc after reading it, so that changing servers doesn't
963 // expect csqc on the next server
964 requiredcrc = csqc_progcrc.integer;
965 requiredsize = csqc_progsize.integer;
966 Cvar_SetValueQuick(&csqc_progcrc, -1);
967 Cvar_SetValueQuick(&csqc_progsize, -1);
969 // if the server is not requesting a csprogs, then we're done here
973 // see if the requested csprogs.dat file matches the requested crc
975 csprogsfn = va("dlcache/%s.%i.%i", csqc_progname.string, requiredsize, requiredcrc);
976 csprogsdata = FS_LoadFile(csprogsfn, tempmempool, true, &csprogsdatasize);
979 csprogsfn = csqc_progname.string;
980 csprogsdata = FS_LoadFile(csprogsfn, tempmempool, true, &csprogsdatasize);
984 csprogsdatacrc = CRC_Block(csprogsdata, (size_t)csprogsdatasize);
985 if (csprogsdatacrc != requiredcrc || csprogsdatasize != requiredsize)
987 if (cls.demoplayback)
989 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);
990 // Mem_Free(csprogsdata);
992 // We WANT to continue here, and play the demo with different csprogs!
993 // After all, this is just a warning. Sure things may go wrong from here.
997 Mem_Free(csprogsdata);
998 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);
1006 if (requiredcrc >= 0)
1008 if (cls.demoplayback)
1009 Con_Printf("CL_VM_Init: demo requires CSQC, but \"%s\" wasn't found\n", csqc_progname.string);
1011 Con_Printf("CL_VM_Init: server requires CSQC, but \"%s\" wasn't found\n", csqc_progname.string);
1018 PRVM_InitProg(PRVM_CLIENTPROG);
1020 // allocate the mempools
1021 prog->progs_mempool = Mem_AllocPool(csqc_progname.string, 0, NULL);
1022 prog->edictprivate_size = 0; // no private struct used
1023 prog->name = CL_NAME;
1024 prog->num_edicts = 1;
1025 prog->max_edicts = 512;
1026 prog->limit_edicts = CL_MAX_EDICTS;
1027 prog->reserved_edicts = 0;
1028 prog->edictprivate_size = sizeof(edict_engineprivate_t);
1029 // TODO: add a shared extension string #define and add real support for csqc extension strings [12/5/2007 Black]
1030 prog->extensionstring = vm_sv_extensions;
1031 prog->builtins = vm_cl_builtins;
1032 prog->numbuiltins = vm_cl_numbuiltins;
1033 prog->begin_increase_edicts = CL_VM_CB_BeginIncreaseEdicts;
1034 prog->end_increase_edicts = CL_VM_CB_EndIncreaseEdicts;
1035 prog->init_edict = CL_VM_CB_InitEdict;
1036 prog->free_edict = CL_VM_CB_FreeEdict;
1037 prog->count_edicts = CL_VM_CB_CountEdicts;
1038 prog->load_edict = CL_VM_CB_LoadEdict;
1039 prog->init_cmd = VM_CL_Cmd_Init;
1040 prog->reset_cmd = VM_CL_Cmd_Reset;
1041 prog->error_cmd = CL_VM_Error;
1042 prog->ExecuteProgram = CLVM_ExecuteProgram;
1044 PRVM_LoadProgs(csprogsfn, cl_numrequiredfunc, cl_required_func, CL_REQFIELDS, cl_reqfields, CL_REQGLOBALS, cl_reqglobals);
1048 CL_VM_Error("CSQC %s ^2failed to load\n", csprogsfn);
1051 Mem_Free(csprogsdata);
1055 Con_DPrintf("CSQC %s ^5loaded (crc=%i, size=%i)\n", csprogsfn, csprogsdatacrc, (int)csprogsdatasize);
1057 if(cls.demorecording)
1059 if(cls.demo_lastcsprogssize != csprogsdatasize || cls.demo_lastcsprogscrc != csprogsdatacrc)
1062 static char buf[NET_MAXMESSAGE];
1064 unsigned char *demobuf; fs_offset_t demofilesize;
1066 sb.data = (unsigned char *) buf;
1067 sb.maxsize = sizeof(buf);
1070 CL_CutDemo(&demobuf, &demofilesize);
1071 while(MakeDownloadPacket(csqc_progname.string, csprogsdata, (size_t)csprogsdatasize, csprogsdatacrc, i++, &sb, cls.protocol))
1072 CL_WriteDemoMessage(&sb);
1073 CL_PasteDemo(&demobuf, &demofilesize);
1075 cls.demo_lastcsprogssize = csprogsdatasize;
1076 cls.demo_lastcsprogscrc = csprogsdatacrc;
1079 Mem_Free(csprogsdata);
1081 // check if OP_STATE animation is possible in this dat file
1082 if (prog->fieldoffsets.nextthink >= 0 && prog->fieldoffsets.frame >= 0 && prog->fieldoffsets.think >= 0 && prog->globaloffsets.self >= 0)
1083 prog->flag |= PRVM_OP_STATE;
1086 PRVM_clientglobalfloat(time) = cl.time;
1087 PRVM_clientglobaledict(self) = 0;
1089 PRVM_clientglobalstring(mapname) = PRVM_SetEngineString(cl.worldname);
1090 PRVM_clientglobalfloat(player_localentnum) = cl.playerentity;
1092 // set map description (use world entity 0)
1093 PRVM_clientedictstring(prog->edicts, message) = PRVM_SetEngineString(cl.worldmessage);
1094 VectorCopy(cl.world.mins, PRVM_clientedictvector(prog->edicts, mins));
1095 VectorCopy(cl.world.maxs, PRVM_clientedictvector(prog->edicts, maxs));
1097 // call the prog init
1098 PRVM_ExecuteProgram(PRVM_clientfunction(CSQC_Init), "QC function CSQC_Init is missing");
1101 cl.csqc_loaded = true;
1103 cl.csqc_vidvars.drawcrosshair = false;
1104 cl.csqc_vidvars.drawenginesbar = false;
1106 // Update Coop and Deathmatch Globals (at this point the client knows them from ServerInfo)
1107 CL_VM_UpdateCoopDeathmatchGlobals(cl.gametype);
1110 void CL_VM_ShutDown (void)
1112 Cmd_ClearCsqcFuncs();
1113 //Cvar_SetValueQuick(&csqc_progcrc, -1);
1114 //Cvar_SetValueQuick(&csqc_progsize, -1);
1118 PRVM_clientglobalfloat(time) = cl.time;
1119 PRVM_clientglobaledict(self) = 0;
1120 if (PRVM_clientfunction(CSQC_Shutdown))
1121 PRVM_ExecuteProgram(PRVM_clientfunction(CSQC_Shutdown), "QC function CSQC_Shutdown is missing");
1124 Con_DPrint("CSQC ^1unloaded\n");
1125 cl.csqc_loaded = false;
1128 qboolean CL_VM_GetEntitySoundOrigin(int entnum, vec3_t out)
1137 // FIXME consider attachments here!
1139 ed = PRVM_EDICT_NUM(entnum - MAX_EDICTS);
1141 if(!ed->priv.required->free)
1143 mod = CL_GetModelFromEdict(ed);
1144 VectorCopy(PRVM_clientedictvector(ed, origin), out);
1145 if(CL_GetTagMatrix (&matrix, ed, 0) == 0)
1146 Matrix4x4_OriginFromMatrix(&matrix, out);
1147 if (mod && mod->soundfromcenter)
1148 VectorMAMAM(1.0f, out, 0.5f, mod->normalmins, 0.5f, mod->normalmaxs, out);
1157 qboolean CL_VM_TransformView(int entnum, matrix4x4_t *viewmatrix, mplane_t *clipplane, vec3_t visorigin)
1159 qboolean ret = false;
1161 vec3_t forward, left, up, origin, ang;
1162 matrix4x4_t mat, matq;
1165 ed = PRVM_EDICT_NUM(entnum);
1168 if(PRVM_clientedictfunction(ed, camera_transform))
1171 if(viewmatrix || clipplane || visorigin)
1173 Matrix4x4_ToVectors(viewmatrix, forward, left, up, origin);
1174 AnglesFromVectors(ang, forward, up, false);
1175 PRVM_clientglobalfloat(time) = cl.time;
1176 PRVM_clientglobaledict(self) = entnum;
1177 VectorCopy(origin, PRVM_G_VECTOR(OFS_PARM0));
1178 VectorCopy(ang, PRVM_G_VECTOR(OFS_PARM1));
1179 VectorCopy(forward, PRVM_clientglobalvector(v_forward));
1180 VectorScale(left, -1, PRVM_clientglobalvector(v_right));
1181 VectorCopy(up, PRVM_clientglobalvector(v_up));
1182 VectorCopy(origin, PRVM_clientglobalvector(trace_endpos));
1183 PRVM_ExecuteProgram(PRVM_clientedictfunction(ed, camera_transform), "QC function e.camera_transform is missing");
1184 VectorCopy(PRVM_G_VECTOR(OFS_RETURN), origin);
1185 VectorCopy(PRVM_clientglobalvector(v_forward), forward);
1186 VectorScale(PRVM_clientglobalvector(v_right), -1, left);
1187 VectorCopy(PRVM_clientglobalvector(v_up), up);
1188 VectorCopy(PRVM_clientglobalvector(trace_endpos), visorigin);
1189 Matrix4x4_Invert_Full(&mat, viewmatrix);
1190 Matrix4x4_FromVectors(viewmatrix, forward, left, up, origin);
1191 Matrix4x4_Concat(&matq, viewmatrix, &mat);
1192 Matrix4x4_TransformPositivePlane(&matq, clipplane->normal[0], clipplane->normal[1], clipplane->normal[2], clipplane->dist, &clipplane->normal[0]);