fix RF_VIEWMODEL by using CL_GetTagMatrix on self rather than
[xonotic/darkplaces.git] / csprogs.c
1 #include "quakedef.h"
2 #include "progsvm.h"
3 #include "clprogdefs.h"
4 #include "csprogs.h"
5 #include "cl_collision.h"
6 #include "snd_main.h"
7 #include "clvm_cmds.h"
8 #include "prvm_cmds.h"
9
10 //============================================================================
11 // Client prog handling
12 //[515]: omg !!! optimize it ! a lot of hacks here and there also :P
13
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;
17
18 static prvm_prog_t *csqc_tmpprog;
19
20 void CL_VM_PreventInformationLeaks(void)
21 {
22         prvm_eval_t *val;
23         if(!cl.csqc_loaded)
24                 return;
25         CSQC_BEGIN
26                 VM_ClearTraceGlobals();
27                 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_networkentity)))
28                         val->_float = 0;
29         CSQC_END
30 }
31
32 //[515]: these are required funcs
33 static const char *cl_required_func[] =
34 {
35         "CSQC_Init",
36         "CSQC_InputEvent",
37         "CSQC_UpdateView",
38         "CSQC_ConsoleCommand",
39 };
40
41 static int cl_numrequiredfunc = sizeof(cl_required_func) / sizeof(char*);
42
43 void CL_VM_Error (const char *format, ...) DP_FUNC_PRINTF(1);
44 void CL_VM_Error (const char *format, ...)      //[515]: hope it will be never executed =)
45 {
46         char errorstring[4096];
47         va_list argptr;
48
49         va_start (argptr, format);
50         dpvsnprintf (errorstring, sizeof(errorstring), format, argptr);
51         va_end (argptr);
52 //      Con_Printf( "CL_VM_Error: %s\n", errorstring );
53
54         PRVM_Crash();
55         cl.csqc_loaded = false;
56
57         Cvar_SetValueQuick(&csqc_progcrc, -1);
58         Cvar_SetValueQuick(&csqc_progsize, -1);
59
60 //      Host_AbortCurrentFrame();       //[515]: hmmm... if server says it needs csqc then client MUST disconnect
61         Host_Error("CL_VM_Error: %s", errorstring);
62 }
63 void CL_VM_UpdateDmgGlobals (int dmg_take, int dmg_save, vec3_t dmg_origin)
64 {
65         prvm_eval_t *val;
66         if(cl.csqc_loaded)
67         {
68                 CSQC_BEGIN
69                 val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.dmg_take);
70                 if(val)
71                         val->_float = dmg_take;
72                 val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.dmg_save);
73                 if(val)
74                         val->_float = dmg_save;
75                 val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.dmg_origin);
76                 if(val)
77                 {
78                         val->vector[0] = dmg_origin[0];
79                         val->vector[1] = dmg_origin[1];
80                         val->vector[2] = dmg_origin[2];
81                 }
82                 CSQC_END
83         }
84 }
85
86 void CSQC_UpdateNetworkTimes(double newtime, double oldtime)
87 {
88         prvm_eval_t *val;
89         if(!cl.csqc_loaded)
90                 return;
91         CSQC_BEGIN
92         if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.servertime)))
93                 val->_float = newtime;
94         if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.serverprevtime)))
95                 val->_float = oldtime;
96         if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.serverdeltatime)))
97                 val->_float = newtime - oldtime;
98         CSQC_END
99 }
100
101 //[515]: set globals before calling R_UpdateView, WEIRD CRAP
102 static void CSQC_SetGlobals (void)
103 {
104         prvm_eval_t *val;
105         CSQC_BEGIN
106                 prog->globals.client->time = cl.time;
107                 prog->globals.client->frametime = max(0, cl.time - cl.oldtime);
108                 prog->globals.client->servercommandframe = cls.servermovesequence;
109                 prog->globals.client->clientcommandframe = cl.movecmd[0].sequence;
110                 VectorCopy(cl.viewangles, prog->globals.client->input_angles);
111                 VectorCopy(cl.viewangles, cl.csqc_angles);
112                 // // FIXME: this actually belongs into getinputstate().. [12/17/2007 Black]
113                 prog->globals.client->input_buttons = cl.movecmd[0].buttons;
114                 VectorSet(prog->globals.client->input_movevalues, cl.movecmd[0].forwardmove, cl.movecmd[0].sidemove, cl.movecmd[0].upmove);
115                 //VectorCopy(cl.movement_origin, cl.csqc_origin);
116                 Matrix4x4_OriginFromMatrix(&cl.entities[cl.viewentity].render.matrix, cl.csqc_origin);
117
118                 // LordHavoc: Spike says not to do this, but without pmove_org the
119                 // CSQC is useless as it can't alter the view origin without
120                 // completely replacing it
121                 VectorCopy(cl.csqc_origin, prog->globals.client->pmove_org);
122                 VectorCopy(cl.movement_velocity, prog->globals.client->pmove_vel);
123
124                 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.view_angles)))
125                         VectorCopy(cl.viewangles, val->vector);
126                 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.view_punchangle)))
127                         VectorCopy(cl.punchangle, val->vector);
128                 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.view_punchvector)))
129                         VectorCopy(cl.punchvector, val->vector);
130                 prog->globals.client->maxclients = cl.maxclients;
131         CSQC_END
132 }
133
134 void CSQC_Predraw (prvm_edict_t *ed)
135 {
136         int b;
137         if(!ed->fields.client->predraw)
138                 return;
139         b = prog->globals.client->self;
140         prog->globals.client->self = PRVM_EDICT_TO_PROG(ed);
141         PRVM_ExecuteProgram(ed->fields.client->predraw, "CSQC_Predraw: NULL function\n");
142         prog->globals.client->self = b;
143 }
144
145 void CSQC_Think (prvm_edict_t *ed)
146 {
147         int b;
148         if(ed->fields.client->think)
149         if(ed->fields.client->nextthink && ed->fields.client->nextthink <= prog->globals.client->time)
150         {
151                 ed->fields.client->nextthink = 0;
152                 b = prog->globals.client->self;
153                 prog->globals.client->self = PRVM_EDICT_TO_PROG(ed);
154                 PRVM_ExecuteProgram(ed->fields.client->think, "CSQC_Think: NULL function\n");
155                 prog->globals.client->self = b;
156         }
157 }
158
159 extern cvar_t cl_noplayershadow;
160 extern cvar_t r_equalize_entities_fullbright;
161 qboolean CSQC_AddRenderEdict(prvm_edict_t *ed, int edictnum)
162 {
163         int renderflags;
164         int c;
165         float scale;
166         prvm_eval_t *val;
167         entity_render_t *entrender;
168         dp_model_t *model;
169         matrix4x4_t tagmatrix, matrix2;
170
171         model = CL_GetModelFromEdict(ed);
172         if (!model)
173                 return false;
174
175         if (edictnum)
176         {
177                 if (r_refdef.scene.numentities >= r_refdef.scene.maxentities)
178                         return false;
179                 entrender = cl.csqcrenderentities + edictnum;
180                 r_refdef.scene.entities[r_refdef.scene.numentities++] = entrender;
181                 entrender->entitynumber = edictnum + MAX_EDICTS;
182                 //entrender->shadertime = 0; // shadertime was set by spawn()
183                 entrender->flags = 0;
184                 entrender->alpha = 1;
185                 entrender->scale = 1;
186                 VectorSet(entrender->colormod, 1, 1, 1);
187                 VectorSet(entrender->glowmod, 1, 1, 1);
188                 entrender->allowdecals = true;
189         }
190         else
191         {
192                 entrender = CL_NewTempEntity(0);
193                 if (!entrender)
194                         return false;
195         }
196
197         if((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.userwavefunc_param0)))    entrender->userwavefunc_param[0] = val->_float;
198         if((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.userwavefunc_param1)))    entrender->userwavefunc_param[1] = val->_float;
199         if((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.userwavefunc_param2)))    entrender->userwavefunc_param[2] = val->_float;
200         if((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.userwavefunc_param3)))    entrender->userwavefunc_param[3] = val->_float;
201
202         entrender->model = model;
203         entrender->skinnum = (int)ed->fields.client->skin;
204         entrender->effects |= entrender->model->effects;
205         scale = 1;
206         renderflags = 0;
207         if((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.renderflags)) && val->_float)     renderflags = (int)val->_float;
208         if((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.alpha)) && val->_float)           entrender->alpha = val->_float;
209         if((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.scale)) && val->_float)           entrender->scale = scale = val->_float;
210         if((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.colormod)) && VectorLength2(val->vector)) VectorCopy(val->vector, entrender->colormod);
211         if((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.glowmod)) && VectorLength2(val->vector))  VectorCopy(val->vector, entrender->glowmod);
212         if(ed->fields.client->effects)  entrender->effects |= (int)ed->fields.client->effects;
213         if (!VectorLength2(entrender->colormod))
214                 VectorSet(entrender->colormod, 1, 1, 1);
215         if (!VectorLength2(entrender->glowmod))
216                 VectorSet(entrender->glowmod, 1, 1, 1);
217
218         // LordHavoc: use the CL_GetTagMatrix function on self to ensure consistent behavior (duplicate code would be bad)
219         CL_GetTagMatrix(&tagmatrix, ed, 0);
220         if (renderflags & RF_USEAXIS)
221         {
222                 vec3_t left;
223                 VectorNegate(prog->globals.client->v_right, left);
224                 Matrix4x4_FromVectors(&matrix2, prog->globals.client->v_forward, left, prog->globals.client->v_up, ed->fields.client->origin);
225                 Matrix4x4_Scale(&matrix2, scale, 1);
226         }
227         else
228         {
229                 vec3_t angles;
230                 VectorCopy(ed->fields.client->angles, angles);
231                 // if model is alias, reverse pitch direction
232                 if (entrender->model->type == mod_alias)
233                         angles[0] = -angles[0];
234
235                 // set up the render matrix
236                 Matrix4x4_CreateFromQuakeEntity(&matrix2, ed->fields.client->origin[0], ed->fields.client->origin[1], ed->fields.client->origin[2], angles[0], angles[1], angles[2], scale);
237         }
238
239         // set up the animation data
240         VM_GenerateFrameGroupBlend(ed->priv.server->framegroupblend, ed);
241         VM_FrameBlendFromFrameGroupBlend(ed->priv.server->frameblend, ed->priv.server->framegroupblend, model);
242         VM_UpdateEdictSkeleton(ed, model, ed->priv.server->frameblend);
243         if ((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.shadertime))) entrender->shadertime = val->_float;
244
245         // concat the matrices to make the entity relative to its tag
246         Matrix4x4_Concat(&entrender->matrix, &tagmatrix, &matrix2);
247
248         // transparent offset
249         if ((renderflags & RF_USETRANSPARENTOFFSET) && (val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.transparent_offset)))
250                 entrender->transparent_offset = val->_float;
251
252         if(renderflags)
253         {
254                 if(renderflags & RF_VIEWMODEL)  entrender->flags |= RENDER_VIEWMODEL | RENDER_NODEPTHTEST;
255                 if(renderflags & RF_EXTERNALMODEL)entrender->flags |= RENDER_EXTERIORMODEL;
256                 if(renderflags & RF_NOCULL)             entrender->flags |= RENDER_NOCULL;
257                 if(renderflags & RF_DEPTHHACK)  entrender->flags |= RENDER_NODEPTHTEST;
258                 if(renderflags & RF_ADDITIVE)           entrender->flags |= RENDER_ADDITIVE;
259         }
260
261         c = (int)ed->fields.client->colormap;
262         if (c <= 0)
263                 CL_SetEntityColormapColors(entrender, -1);
264         else if (c <= cl.maxclients && cl.scores != NULL)
265                 CL_SetEntityColormapColors(entrender, cl.scores[c-1].colors);
266         else
267                 CL_SetEntityColormapColors(entrender, c);
268
269         entrender->flags &= ~(RENDER_SHADOW | RENDER_LIGHT | RENDER_NOSELFSHADOW);
270         // either fullbright or lit
271         if(!r_fullbright.integer)
272         {
273                 if (!(entrender->effects & EF_FULLBRIGHT) && !(renderflags & RF_FULLBRIGHT))
274                         entrender->flags |= RENDER_LIGHT;
275                 else if(r_equalize_entities_fullbright.integer)
276                         entrender->flags |= RENDER_LIGHT | RENDER_EQUALIZE;
277         }
278         // hide player shadow during intermission or nehahra movie
279         if (!(entrender->effects & (EF_NOSHADOW | EF_ADDITIVE | EF_NODEPTHTEST))
280          &&  (entrender->alpha >= 1)
281          && !(renderflags & RF_NOSHADOW)
282          && !(entrender->flags & RENDER_VIEWMODEL)
283          && (!(entrender->flags & RENDER_EXTERIORMODEL) || (!cl.intermission && cls.protocol != PROTOCOL_NEHAHRAMOVIE && !cl_noplayershadow.integer)))
284                 entrender->flags |= RENDER_SHADOW;
285         if (entrender->flags & RENDER_VIEWMODEL)
286                 entrender->flags |= RENDER_NOSELFSHADOW;
287         if (entrender->effects & EF_NOSELFSHADOW)
288                 entrender->flags |= RENDER_NOSELFSHADOW;
289         if (entrender->effects & EF_NODEPTHTEST)
290                 entrender->flags |= RENDER_NODEPTHTEST;
291         if (entrender->effects & EF_ADDITIVE)
292                 entrender->flags |= RENDER_ADDITIVE;
293         if (entrender->effects & EF_DOUBLESIDED)
294                 entrender->flags |= RENDER_DOUBLESIDED;
295
296         // make the other useful stuff
297         memcpy(entrender->framegroupblend, ed->priv.server->framegroupblend, sizeof(ed->priv.server->framegroupblend));
298         CL_UpdateRenderEntity(entrender);
299         // override animation data with full control
300         memcpy(entrender->frameblend, ed->priv.server->frameblend, sizeof(ed->priv.server->frameblend));
301         if (ed->priv.server->skeleton.relativetransforms)
302                 entrender->skeleton = &ed->priv.server->skeleton;
303         else
304                 entrender->skeleton = NULL;
305
306         return true;
307 }
308
309 qboolean CL_VM_InputEvent (qboolean down, int key, int ascii)
310 {
311         qboolean r;
312
313         if(!cl.csqc_loaded)
314                 return false;
315
316         CSQC_BEGIN
317                 if (!prog->funcoffsets.CSQC_InputEvent)
318                         r = false;
319                 else
320                 {
321                         prog->globals.client->time = cl.time;
322                         prog->globals.client->self = cl.csqc_server2csqcentitynumber[cl.playerentity];
323                         PRVM_G_FLOAT(OFS_PARM0) = !down; // 0 is down, 1 is up
324                         PRVM_G_FLOAT(OFS_PARM1) = key;
325                         PRVM_G_FLOAT(OFS_PARM2) = ascii;
326                         PRVM_ExecuteProgram(prog->funcoffsets.CSQC_InputEvent, "QC function CSQC_InputEvent is missing");
327                         r = CSQC_RETURNVAL != 0;
328                 }
329         CSQC_END
330         return r;
331 }
332
333 qboolean CL_VM_UpdateView (void)
334 {
335         vec3_t emptyvector;
336         emptyvector[0] = 0;
337         emptyvector[1] = 0;
338         emptyvector[2] = 0;
339 //      vec3_t oldangles;
340         if(!cl.csqc_loaded)
341                 return false;
342         R_TimeReport("pre-UpdateView");
343         CSQC_BEGIN
344                 //VectorCopy(cl.viewangles, oldangles);
345                 prog->globals.client->time = cl.time;
346                 prog->globals.client->self = cl.csqc_server2csqcentitynumber[cl.playerentity];
347                 CSQC_SetGlobals();
348                 // clear renderable entity and light lists to prevent crashes if the
349                 // CSQC_UpdateView function does not call R_ClearScene as it should
350                 r_refdef.scene.numentities = 0;
351                 r_refdef.scene.numlights = 0;
352                 // pass in width and height as parameters (EXT_CSQC_1)
353                 PRVM_G_FLOAT(OFS_PARM0) = vid.width;
354                 PRVM_G_FLOAT(OFS_PARM1) = vid.height;
355                 PRVM_ExecuteProgram(prog->funcoffsets.CSQC_UpdateView, "QC function CSQC_UpdateView is missing");
356                 //VectorCopy(oldangles, cl.viewangles);
357                 // Dresk : Reset Dmg Globals Here
358                 CL_VM_UpdateDmgGlobals(0, 0, emptyvector);
359         CSQC_END
360         R_TimeReport("UpdateView");
361         return true;
362 }
363
364 extern sizebuf_t vm_tempstringsbuf;
365 qboolean CL_VM_ConsoleCommand (const char *cmd)
366 {
367         int restorevm_tempstringsbuf_cursize;
368         qboolean r = false;
369         if(!cl.csqc_loaded)
370                 return false;
371         CSQC_BEGIN
372         if (prog->funcoffsets.CSQC_ConsoleCommand)
373         {
374                 prog->globals.client->time = cl.time;
375                 prog->globals.client->self = cl.csqc_server2csqcentitynumber[cl.playerentity];
376                 restorevm_tempstringsbuf_cursize = vm_tempstringsbuf.cursize;
377                 PRVM_G_INT(OFS_PARM0) = PRVM_SetTempString(cmd);
378                 PRVM_ExecuteProgram(prog->funcoffsets.CSQC_ConsoleCommand, "QC function CSQC_ConsoleCommand is missing");
379                 vm_tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize;
380                 r = CSQC_RETURNVAL != 0;
381         }
382         CSQC_END
383         return r;
384 }
385
386 qboolean CL_VM_Parse_TempEntity (void)
387 {
388         int                     t;
389         qboolean        r = false;
390         if(!cl.csqc_loaded)
391                 return false;
392         CSQC_BEGIN
393         if(prog->funcoffsets.CSQC_Parse_TempEntity)
394         {
395                 t = msg_readcount;
396                 prog->globals.client->time = cl.time;
397                 prog->globals.client->self = cl.csqc_server2csqcentitynumber[cl.playerentity];
398                 PRVM_ExecuteProgram(prog->funcoffsets.CSQC_Parse_TempEntity, "QC function CSQC_Parse_TempEntity is missing");
399                 r = CSQC_RETURNVAL != 0;
400                 if(!r)
401                 {
402                         msg_readcount = t;
403                         msg_badread = false;
404                 }
405         }
406         CSQC_END
407         return r;
408 }
409
410 void CL_VM_Parse_StuffCmd (const char *msg)
411 {
412         int restorevm_tempstringsbuf_cursize;
413         if(msg[0] == 'c')
414         if(msg[1] == 's')
415         if(msg[2] == 'q')
416         if(msg[3] == 'c')
417         {
418                 // if this is setting a csqc variable, deprotect csqc_progcrc
419                 // temporarily so that it can be set by the cvar command,
420                 // and then reprotect it afterwards
421                 int crcflags = csqc_progcrc.flags;
422                 int sizeflags = csqc_progcrc.flags;
423                 csqc_progcrc.flags &= ~CVAR_READONLY;
424                 csqc_progsize.flags &= ~CVAR_READONLY;
425                 Cmd_ExecuteString (msg, src_command);
426                 csqc_progcrc.flags = crcflags;
427                 csqc_progsize.flags = sizeflags;
428                 return;
429         }
430
431         if(cls.demoplayback)
432         if(!strncmp(msg, "curl --clear_autodownload\ncurl --pak --forthismap --as ", 55))
433         {
434                 // special handling for map download commands
435                 // run these commands IMMEDIATELY, instead of waiting for a client frame
436                 // that way, there is no black screen when playing back demos
437                 // I know this is a really ugly hack, but I can't think of any better way
438                 // FIXME find the actual CAUSE of this, and make demo playback WAIT
439                 // until all maps are loaded, then remove this hack
440
441                 char buf[MAX_INPUTLINE];
442                 const char *p, *q;
443                 size_t l;
444
445                 p = msg;
446
447                 for(;;)
448                 {
449                         q = strchr(p, '\n');
450                         if(q)
451                                 l = q - p;
452                         else
453                                 l = strlen(p);
454                         if(l > sizeof(buf) - 1)
455                                 l = sizeof(buf) - 1;
456                         strlcpy(buf, p, l + 1); // strlcpy needs a + 1 as it includes the newline!
457
458                         Cmd_ExecuteString(buf, src_command);
459
460                         p += l;
461                         if(*p == '\n')
462                                 ++p; // skip the newline and continue
463                         else
464                                 break; // end of string or overflow
465                 }
466                 Cmd_ExecuteString("curl --clear_autodownload", src_command); // don't inhibit CSQC loading
467                 return;
468         }
469
470         if(!cl.csqc_loaded)
471         {
472                 Cbuf_AddText(msg);
473                 return;
474         }
475         CSQC_BEGIN
476         if(prog->funcoffsets.CSQC_Parse_StuffCmd)
477         {
478                 prog->globals.client->time = cl.time;
479                 prog->globals.client->self = cl.csqc_server2csqcentitynumber[cl.playerentity];
480                 restorevm_tempstringsbuf_cursize = vm_tempstringsbuf.cursize;
481                 PRVM_G_INT(OFS_PARM0) = PRVM_SetTempString(msg);
482                 PRVM_ExecuteProgram(prog->funcoffsets.CSQC_Parse_StuffCmd, "QC function CSQC_Parse_StuffCmd is missing");
483                 vm_tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize;
484         }
485         else
486                 Cbuf_AddText(msg);
487         CSQC_END
488 }
489
490 static void CL_VM_Parse_Print (const char *msg)
491 {
492         int restorevm_tempstringsbuf_cursize;
493         prog->globals.client->time = cl.time;
494         prog->globals.client->self = cl.csqc_server2csqcentitynumber[cl.playerentity];
495         restorevm_tempstringsbuf_cursize = vm_tempstringsbuf.cursize;
496         PRVM_G_INT(OFS_PARM0) = PRVM_SetTempString(msg);
497         PRVM_ExecuteProgram(prog->funcoffsets.CSQC_Parse_Print, "QC function CSQC_Parse_Print is missing");
498         vm_tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize;
499 }
500
501 void CSQC_AddPrintText (const char *msg)
502 {
503         size_t i;
504         if(!cl.csqc_loaded)
505         {
506                 Con_Print(msg);
507                 return;
508         }
509         CSQC_BEGIN
510         if(prog->funcoffsets.CSQC_Parse_Print)
511         {
512                 // FIXME: is this bugged?
513                 i = strlen(msg)-1;
514                 if(msg[i] != '\n' && msg[i] != '\r')
515                 {
516                         if(strlen(cl.csqc_printtextbuf)+i >= MAX_INPUTLINE)
517                         {
518                                 CL_VM_Parse_Print(cl.csqc_printtextbuf);
519                                 cl.csqc_printtextbuf[0] = 0;
520                         }
521                         else
522                                 strlcat(cl.csqc_printtextbuf, msg, MAX_INPUTLINE);
523                         return;
524                 }
525                 strlcat(cl.csqc_printtextbuf, msg, MAX_INPUTLINE);
526                 CL_VM_Parse_Print(cl.csqc_printtextbuf);
527                 cl.csqc_printtextbuf[0] = 0;
528         }
529         else
530                 Con_Print(msg);
531         CSQC_END
532 }
533
534 void CL_VM_Parse_CenterPrint (const char *msg)
535 {
536         int restorevm_tempstringsbuf_cursize;
537         if(!cl.csqc_loaded)
538         {
539                 SCR_CenterPrint(msg);
540                 return;
541         }
542         CSQC_BEGIN
543         if(prog->funcoffsets.CSQC_Parse_CenterPrint)
544         {
545                 prog->globals.client->time = cl.time;
546                 prog->globals.client->self = cl.csqc_server2csqcentitynumber[cl.playerentity];
547                 restorevm_tempstringsbuf_cursize = vm_tempstringsbuf.cursize;
548                 PRVM_G_INT(OFS_PARM0) = PRVM_SetTempString(msg);
549                 PRVM_ExecuteProgram(prog->funcoffsets.CSQC_Parse_CenterPrint, "QC function CSQC_Parse_CenterPrint is missing");
550                 vm_tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize;
551         }
552         else
553                 SCR_CenterPrint(msg);
554         CSQC_END
555 }
556
557 void CL_VM_UpdateIntermissionState (int intermission)
558 {
559         prvm_eval_t *val;
560         if(cl.csqc_loaded)
561         {
562                 CSQC_BEGIN
563                 val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.intermission);
564                 if(val)
565                         val->_float = intermission;
566                 CSQC_END
567         }
568 }
569 void CL_VM_UpdateShowingScoresState (int showingscores)
570 {
571         prvm_eval_t *val;
572         if(cl.csqc_loaded)
573         {
574                 CSQC_BEGIN
575                 val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.sb_showscores);
576                 if(val)
577                         val->_float = showingscores;
578                 CSQC_END
579         }
580 }
581 qboolean CL_VM_Event_Sound(int sound_num, float volume, int channel, float attenuation, int ent, vec3_t pos)
582 {
583         qboolean r = false;
584         if(cl.csqc_loaded)
585         {
586                 CSQC_BEGIN
587                 if(prog->funcoffsets.CSQC_Event_Sound)
588                 {
589                         prog->globals.client->time = cl.time;
590                         prog->globals.client->self = cl.csqc_server2csqcentitynumber[cl.playerentity];
591                         PRVM_G_FLOAT(OFS_PARM0) = ent;
592                         PRVM_G_FLOAT(OFS_PARM1) = channel;
593                         PRVM_G_INT(OFS_PARM2) = PRVM_SetTempString(cl.sound_name[sound_num] );
594                         PRVM_G_FLOAT(OFS_PARM3) = volume;
595                         PRVM_G_FLOAT(OFS_PARM4) = attenuation;
596                         VectorCopy(pos, PRVM_G_VECTOR(OFS_PARM5) );
597                         PRVM_ExecuteProgram(prog->funcoffsets.CSQC_Event_Sound, "QC function CSQC_Event_Sound is missing");
598                         r = CSQC_RETURNVAL != 0;
599                 }
600                 CSQC_END
601         }
602
603         return r;
604 }
605 void CL_VM_UpdateCoopDeathmatchGlobals (int gametype)
606 {
607         // Avoid global names for clean(er) coding
608         int localcoop;
609         int localdeathmatch;
610
611         prvm_eval_t *val;
612         if(cl.csqc_loaded)
613         {
614                 if(gametype == GAME_COOP)
615                 {
616                         localcoop = 1;
617                         localdeathmatch = 0;
618                 }
619                 else
620                 if(gametype == GAME_DEATHMATCH)
621                 {
622                         localcoop = 0;
623                         localdeathmatch = 1;
624                 }
625                 else
626                 {
627                         // How did the ServerInfo send an unknown gametype?
628                         // Better just assign the globals as 0...
629                         localcoop = 0;
630                         localdeathmatch = 0;
631                 }
632                 CSQC_BEGIN
633                 val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.coop);
634                 if(val)
635                         val->_float = localcoop;
636                 val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.deathmatch);
637                 if(val)
638                         val->_float = localdeathmatch;
639                 CSQC_END
640         }
641 }
642 float CL_VM_Event (float event)         //[515]: needed ? I'd say "YES", but don't know for what :D
643 {
644         float r = 0;
645         if(!cl.csqc_loaded)
646                 return 0;
647         CSQC_BEGIN
648         if(prog->funcoffsets.CSQC_Event)
649         {
650                 prog->globals.client->time = cl.time;
651                 prog->globals.client->self = cl.csqc_server2csqcentitynumber[cl.playerentity];
652                 PRVM_G_FLOAT(OFS_PARM0) = event;
653                 PRVM_ExecuteProgram(prog->funcoffsets.CSQC_Event, "QC function CSQC_Event is missing");
654                 r = CSQC_RETURNVAL;
655         }
656         CSQC_END
657         return r;
658 }
659
660 void CSQC_ReadEntities (void)
661 {
662         unsigned short entnum, oldself, realentnum;
663         if(!cl.csqc_loaded)
664         {
665                 Host_Error ("CSQC_ReadEntities: CSQC is not loaded");
666                 return;
667         }
668
669         CSQC_BEGIN
670                 prog->globals.client->time = cl.time;
671                 oldself = prog->globals.client->self;
672                 while(1)
673                 {
674                         entnum = MSG_ReadShort();
675                         if(!entnum || msg_badread)
676                                 break;
677                         realentnum = entnum & 0x7FFF;
678                         prog->globals.client->self = cl.csqc_server2csqcentitynumber[realentnum];
679                         if(entnum & 0x8000)
680                         {
681                                 if(prog->globals.client->self)
682                                 {
683                                         PRVM_ExecuteProgram(prog->funcoffsets.CSQC_Ent_Remove, "QC function CSQC_Ent_Remove is missing");
684                                         cl.csqc_server2csqcentitynumber[realentnum] = 0;
685                                 }
686                                 else
687                                 {
688                                         // LordHavoc: removing an entity that is already gone on
689                                         // the csqc side is possible for legitimate reasons (such
690                                         // as a repeat of the remove message), so no warning is
691                                         // needed
692                                         //Con_Printf("Bad csqc_server2csqcentitynumber map\n"); //[515]: never happens ?
693                                 }
694                         }
695                         else
696                         {
697                                 if(!prog->globals.client->self)
698                                 {
699                                         if(!prog->funcoffsets.CSQC_Ent_Spawn)
700                                         {
701                                                 prvm_edict_t    *ed;
702                                                 ed = PRVM_ED_Alloc();
703                                                 ed->fields.client->entnum = realentnum;
704                                                 prog->globals.client->self = cl.csqc_server2csqcentitynumber[realentnum] = PRVM_EDICT_TO_PROG(ed);
705                                         }
706                                         else
707                                         {
708                                                 // entity( float entnum ) CSQC_Ent_Spawn;
709                                                 // the qc function should set entnum, too (this way it also can return world [2/1/2008 Andreas]
710                                                 PRVM_G_FLOAT(OFS_PARM0) = (float) realentnum;
711                                                 // make sure no one gets wrong ideas
712                                                 prog->globals.client->self = 0;
713                                                 PRVM_ExecuteProgram(prog->funcoffsets.CSQC_Ent_Spawn, "QC function CSQC_Ent_Spawn is missing");
714                                                 prog->globals.client->self = cl.csqc_server2csqcentitynumber[realentnum] = PRVM_EDICT( PRVM_G_INT( OFS_RETURN ) );
715                                         }
716                                         PRVM_G_FLOAT(OFS_PARM0) = 1;
717                                         PRVM_ExecuteProgram(prog->funcoffsets.CSQC_Ent_Update, "QC function CSQC_Ent_Update is missing");
718                                 }
719                                 else {
720                                         PRVM_G_FLOAT(OFS_PARM0) = 0;
721                                         PRVM_ExecuteProgram(prog->funcoffsets.CSQC_Ent_Update, "QC function CSQC_Ent_Update is missing");
722                                 }
723                         }
724                 }
725                 prog->globals.client->self = oldself;
726         CSQC_END
727 }
728
729 void CL_VM_CB_BeginIncreaseEdicts(void)
730 {
731         // links don't survive the transition, so unlink everything
732         World_UnlinkAll(&cl.world);
733 }
734
735 void CL_VM_CB_EndIncreaseEdicts(void)
736 {
737         int i;
738         prvm_edict_t *ent;
739
740         // link every entity except world
741         for (i = 1, ent = prog->edicts;i < prog->num_edicts;i++, ent++)
742                 if (!ent->priv.server->free)
743                         CL_LinkEdict(ent);
744 }
745
746 void CL_VM_CB_InitEdict(prvm_edict_t *e)
747 {
748         int edictnum = PRVM_NUM_FOR_EDICT(e);
749         entity_render_t *entrender;
750         CL_ExpandCSQCRenderEntities(edictnum);
751         entrender = cl.csqcrenderentities + edictnum;
752         e->priv.server->move = false; // don't move on first frame
753         memset(entrender, 0, sizeof(*entrender));
754         entrender->shadertime = cl.time;
755 }
756
757 extern void R_DecalSystem_Reset(decalsystem_t *decalsystem);
758
759 void CL_VM_CB_FreeEdict(prvm_edict_t *ed)
760 {
761         entity_render_t *entrender = cl.csqcrenderentities + PRVM_NUM_FOR_EDICT(ed);
762         R_DecalSystem_Reset(&entrender->decalsystem);
763         memset(entrender, 0, sizeof(*entrender));
764         World_UnlinkEdict(ed);
765         memset(ed->fields.client, 0, sizeof(*ed->fields.client));
766         VM_RemoveEdictSkeleton(ed);
767         World_Physics_RemoveFromEntity(&cl.world, ed);
768         World_Physics_RemoveJointFromEntity(&cl.world, ed);
769 }
770
771 void CL_VM_CB_CountEdicts(void)
772 {
773         int             i;
774         prvm_edict_t    *ent;
775         int             active = 0, models = 0, solid = 0;
776
777         for (i=0 ; i<prog->num_edicts ; i++)
778         {
779                 ent = PRVM_EDICT_NUM(i);
780                 if (ent->priv.server->free)
781                         continue;
782                 active++;
783                 if (ent->fields.client->solid)
784                         solid++;
785                 if (ent->fields.client->model)
786                         models++;
787         }
788
789         Con_Printf("num_edicts:%3i\n", prog->num_edicts);
790         Con_Printf("active    :%3i\n", active);
791         Con_Printf("view      :%3i\n", models);
792         Con_Printf("touch     :%3i\n", solid);
793 }
794
795 qboolean CL_VM_CB_LoadEdict(prvm_edict_t *ent)
796 {
797         return true;
798 }
799
800 void Cmd_ClearCsqcFuncs (void);
801
802 // returns true if the packet is valid, false if end of file is reached
803 // used for dumping the CSQC download into demo files
804 qboolean MakeDownloadPacket(const char *filename, unsigned char *data, size_t len, int crc, int cnt, sizebuf_t *buf, int protocol)
805 {
806         int packetsize = buf->maxsize - 7; // byte short long
807         int npackets = (len + packetsize - 1) / (packetsize);
808
809         if(protocol == PROTOCOL_QUAKEWORLD)
810                 return false; // CSQC can't run in QW anyway
811
812         SZ_Clear(buf);
813         if(cnt == 0)
814         {
815                 MSG_WriteByte(buf, svc_stufftext);
816                 MSG_WriteString(buf, va("\ncl_downloadbegin %lu %s\n", (unsigned long)len, filename));
817                 return true;
818         }
819         else if(cnt >= 1 && cnt <= npackets)
820         {
821                 unsigned long thispacketoffset = (cnt - 1) * packetsize;
822                 int thispacketsize = len - thispacketoffset;
823                 if(thispacketsize > packetsize)
824                         thispacketsize = packetsize;
825
826                 MSG_WriteByte(buf, svc_downloaddata);
827                 MSG_WriteLong(buf, thispacketoffset);
828                 MSG_WriteShort(buf, thispacketsize);
829                 SZ_Write(buf, data + thispacketoffset, thispacketsize);
830
831                 return true;
832         }
833         else if(cnt == npackets + 1)
834         {
835                 MSG_WriteByte(buf, svc_stufftext);
836                 MSG_WriteString(buf, va("\ncl_downloadfinished %lu %d\n", (unsigned long)len, crc));
837                 return true;
838         }
839         return false;
840 }
841
842 void CL_VM_Init (void)
843 {
844         const char* csprogsfn;
845         unsigned char *csprogsdata;
846         fs_offset_t csprogsdatasize;
847         int csprogsdatacrc, requiredcrc;
848         int requiredsize;
849         prvm_eval_t *val;
850
851         // reset csqc_progcrc after reading it, so that changing servers doesn't
852         // expect csqc on the next server
853         requiredcrc = csqc_progcrc.integer;
854         requiredsize = csqc_progsize.integer;
855         Cvar_SetValueQuick(&csqc_progcrc, -1);
856         Cvar_SetValueQuick(&csqc_progsize, -1);
857
858         // if the server is not requesting a csprogs, then we're done here
859         if (requiredcrc < 0)
860                 return;
861
862         // see if the requested csprogs.dat file matches the requested crc
863         csprogsdatacrc = -1;
864         csprogsfn = va("dlcache/%s.%i.%i", csqc_progname.string, requiredsize, requiredcrc);
865         csprogsdata = FS_LoadFile(csprogsfn, tempmempool, true, &csprogsdatasize);
866         if (!csprogsdata)
867         {
868                 csprogsfn = csqc_progname.string;
869                 csprogsdata = FS_LoadFile(csprogsfn, tempmempool, true, &csprogsdatasize);
870         }
871         if (csprogsdata)
872         {
873                 csprogsdatacrc = CRC_Block(csprogsdata, (size_t)csprogsdatasize);
874                 if (csprogsdatacrc != requiredcrc || csprogsdatasize != requiredsize)
875                 {
876                         if (cls.demoplayback)
877                         {
878                                 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);
879                                 // Mem_Free(csprogsdata);
880                                 // return;
881                                 // We WANT to continue here, and play the demo with different csprogs!
882                                 // After all, this is just a warning. Sure things may go wrong from here.
883                         }
884                         else
885                         {
886                                 Mem_Free(csprogsdata);
887                                 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);
888                                 CL_Disconnect();
889                                 return;
890                         }
891                 }
892         }
893         else
894         {
895                 if (requiredcrc >= 0)
896                 {
897                         if (cls.demoplayback)
898                                 Con_Printf("CL_VM_Init: demo requires CSQC, but \"%s\" wasn't found\n", csqc_progname.string);
899                         else
900                                 Con_Printf("CL_VM_Init: server requires CSQC, but \"%s\" wasn't found\n", csqc_progname.string);
901                         CL_Disconnect();
902                 }
903                 return;
904         }
905
906         PRVM_Begin;
907         PRVM_InitProg(PRVM_CLIENTPROG);
908
909         // allocate the mempools
910         prog->progs_mempool = Mem_AllocPool(csqc_progname.string, 0, NULL);
911         prog->headercrc = CL_PROGHEADER_CRC;
912         prog->edictprivate_size = 0; // no private struct used
913         prog->name = CL_NAME;
914         prog->num_edicts = 1;
915         prog->max_edicts = 512;
916         prog->limit_edicts = CL_MAX_EDICTS;
917         prog->reserved_edicts = 0;
918         prog->edictprivate_size = sizeof(edict_engineprivate_t);
919         // TODO: add a shared extension string #define and add real support for csqc extension strings [12/5/2007 Black]
920         prog->extensionstring = vm_sv_extensions;
921         prog->builtins = vm_cl_builtins;
922         prog->numbuiltins = vm_cl_numbuiltins;
923         prog->begin_increase_edicts = CL_VM_CB_BeginIncreaseEdicts;
924         prog->end_increase_edicts = CL_VM_CB_EndIncreaseEdicts;
925         prog->init_edict = CL_VM_CB_InitEdict;
926         prog->free_edict = CL_VM_CB_FreeEdict;
927         prog->count_edicts = CL_VM_CB_CountEdicts;
928         prog->load_edict = CL_VM_CB_LoadEdict;
929         prog->init_cmd = VM_CL_Cmd_Init;
930         prog->reset_cmd = VM_CL_Cmd_Reset;
931         prog->error_cmd = CL_VM_Error;
932         prog->ExecuteProgram = CLVM_ExecuteProgram;
933
934         PRVM_LoadProgs(csprogsfn, cl_numrequiredfunc, cl_required_func, 0, NULL, 0, NULL);
935
936         if (!prog->loaded)
937         {
938                 CL_VM_Error("CSQC %s ^2failed to load\n", csprogsfn);
939                 if(!sv.active)
940                         CL_Disconnect();
941                 Mem_Free(csprogsdata);
942                 return;
943         }
944
945         Con_DPrintf("CSQC %s ^5loaded (crc=%i, size=%i)\n", csprogsfn, csprogsdatacrc, (int)csprogsdatasize);
946
947         if(cls.demorecording)
948         {
949                 if(cls.demo_lastcsprogssize != csprogsdatasize || cls.demo_lastcsprogscrc != csprogsdatacrc)
950                 {
951                         int i;
952                         static char buf[NET_MAXMESSAGE];
953                         sizebuf_t sb;
954                         unsigned char *demobuf; fs_offset_t demofilesize;
955
956                         sb.data = (unsigned char *) buf;
957                         sb.maxsize = sizeof(buf);
958                         i = 0;
959
960                         CL_CutDemo(&demobuf, &demofilesize);
961                         while(MakeDownloadPacket(csqc_progname.string, csprogsdata, (size_t)csprogsdatasize, csprogsdatacrc, i++, &sb, cls.protocol))
962                                 CL_WriteDemoMessage(&sb);
963                         CL_PasteDemo(&demobuf, &demofilesize);
964
965                         cls.demo_lastcsprogssize = csprogsdatasize;
966                         cls.demo_lastcsprogscrc = csprogsdatacrc;
967                 }
968         }
969         Mem_Free(csprogsdata);
970
971         // check if OP_STATE animation is possible in this dat file
972         if (prog->fieldoffsets.nextthink >= 0 && prog->fieldoffsets.frame >= 0 && prog->fieldoffsets.think >= 0 && prog->globaloffsets.self >= 0)
973                 prog->flag |= PRVM_OP_STATE;
974
975         // set time
976         prog->globals.client->time = cl.time;
977         prog->globals.client->self = 0;
978
979         prog->globals.client->mapname = PRVM_SetEngineString(cl.worldname);
980         prog->globals.client->player_localentnum = cl.playerentity;
981
982         // set map description (use world entity 0)
983         val = PRVM_EDICTFIELDVALUE(prog->edicts, prog->fieldoffsets.message);
984         if(val)
985                 val->string = PRVM_SetEngineString(cl.worldmessage);
986         VectorCopy(cl.world.mins, prog->edicts->fields.client->mins);
987         VectorCopy(cl.world.maxs, prog->edicts->fields.client->maxs);
988
989         // call the prog init
990         PRVM_ExecuteProgram(prog->funcoffsets.CSQC_Init, "QC function CSQC_Init is missing");
991
992         PRVM_End;
993         cl.csqc_loaded = true;
994
995         cl.csqc_vidvars.drawcrosshair = false;
996         cl.csqc_vidvars.drawenginesbar = false;
997
998         // Update Coop and Deathmatch Globals (at this point the client knows them from ServerInfo)
999         CL_VM_UpdateCoopDeathmatchGlobals(cl.gametype);
1000 }
1001
1002 void CL_VM_ShutDown (void)
1003 {
1004         Cmd_ClearCsqcFuncs();
1005         //Cvar_SetValueQuick(&csqc_progcrc, -1);
1006         //Cvar_SetValueQuick(&csqc_progsize, -1);
1007         if(!cl.csqc_loaded)
1008                 return;
1009         CSQC_BEGIN
1010                 prog->globals.client->time = cl.time;
1011                 prog->globals.client->self = 0;
1012                 if (prog->funcoffsets.CSQC_Shutdown)
1013                         PRVM_ExecuteProgram(prog->funcoffsets.CSQC_Shutdown, "QC function CSQC_Shutdown is missing");
1014                 PRVM_ResetProg();
1015         CSQC_END
1016         Con_DPrint("CSQC ^1unloaded\n");
1017         cl.csqc_loaded = false;
1018 }
1019
1020 qboolean CL_VM_GetEntitySoundOrigin(int entnum, vec3_t out)
1021 {
1022         prvm_edict_t *ed;
1023         dp_model_t *mod;
1024         matrix4x4_t matrix;
1025         qboolean r = 0;
1026
1027         CSQC_BEGIN;
1028
1029         // FIXME consider attachments here!
1030
1031         ed = PRVM_EDICT_NUM(entnum - MAX_EDICTS);
1032
1033         if(!ed->priv.required->free)
1034         {
1035                 mod = CL_GetModelFromEdict(ed);
1036                 VectorCopy(ed->fields.client->origin, out);
1037                 if(CL_GetTagMatrix (&matrix, ed, 0) == 0)
1038                         Matrix4x4_OriginFromMatrix(&matrix, out);
1039                 if (mod && mod->soundfromcenter)
1040                         VectorMAMAM(1.0f, out, 0.5f, mod->normalmins, 0.5f, mod->normalmaxs, out);
1041                 r = 1;
1042         }
1043
1044         CSQC_END;
1045
1046         return r;
1047 }
1048
1049 qboolean CL_VM_TransformView(int entnum, matrix4x4_t *viewmatrix, mplane_t *clipplane, vec3_t visorigin)
1050 {
1051         qboolean ret = false;
1052         prvm_edict_t *ed;
1053         prvm_eval_t *val, *valforward, *valright, *valup, *valendpos;
1054         vec3_t forward, left, up, origin, ang;
1055         matrix4x4_t mat, matq;
1056
1057         CSQC_BEGIN
1058                 ed = PRVM_EDICT_NUM(entnum);
1059                 // camera:
1060                 //   camera_transform
1061                 if((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.camera_transform)) && val->function)
1062                 {
1063                         ret = true;
1064                         if(viewmatrix || clipplane || visorigin)
1065                         {
1066                                 valforward = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_forward);
1067                                 valright = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_right);
1068                                 valup = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_up);
1069                                 valendpos = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_endpos);
1070                                 if(valforward && valright && valup && valendpos)
1071                                 {
1072                                         Matrix4x4_ToVectors(viewmatrix, forward, left, up, origin);
1073                                         AnglesFromVectors(ang, forward, up, false);
1074                                         prog->globals.client->time = cl.time;
1075                                         prog->globals.client->self = entnum;
1076                                         VectorCopy(origin, PRVM_G_VECTOR(OFS_PARM0));
1077                                         VectorCopy(ang, PRVM_G_VECTOR(OFS_PARM1));
1078                                         VectorCopy(forward, valforward->vector);
1079                                         VectorScale(left, -1, valright->vector);
1080                                         VectorCopy(up, valup->vector);
1081                                         VectorCopy(origin, valendpos->vector);
1082                                         PRVM_ExecuteProgram(val->function, "QC function e.camera_transform is missing");
1083                                         VectorCopy(PRVM_G_VECTOR(OFS_RETURN), origin);
1084                                         VectorCopy(valforward->vector, forward);
1085                                         VectorScale(valright->vector, -1, left);
1086                                         VectorCopy(valup->vector, up);
1087                                         VectorCopy(valendpos->vector, visorigin);
1088                                         Matrix4x4_Invert_Full(&mat, viewmatrix);
1089                                         Matrix4x4_FromVectors(viewmatrix, forward, left, up, origin);
1090                                         Matrix4x4_Concat(&matq, viewmatrix, &mat);
1091                                         Matrix4x4_TransformPositivePlane(&matq, clipplane->normal[0], clipplane->normal[1], clipplane->normal[2], clipplane->dist, &clipplane->normal[0]);
1092                                 }
1093                         }
1094                 }
1095         CSQC_END
1096
1097         return ret;
1098 }