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