]> icculus.org git repositories - divverent/darkplaces.git/blob - clvm_cmds.c
prints entity number of MOVETYPE_PUSH entities without models (this is mostly useful...
[divverent/darkplaces.git] / clvm_cmds.c
1 #include "prvm_cmds.h"
2 #include "csprogs.h"
3 #include "cl_collision.h"
4
5 //============================================================================
6 // Client
7 //[515]: unsolved PROBLEMS
8 //- finish player physics code (cs_runplayerphysics)
9 //- fix R_AddDynamicLight
10 //- EntWasFreed ?
11 //- RF_DEPTHHACK is not like it should be
12 //- add builtin that sets cl.viewangles instead of reading "input_angles" global
13 //- finish lines support for R_Polygon***
14 //- insert selecttraceline into traceline somehow
15
16 //4 feature darkplaces csqc: add builtin to clientside qc for reading triangles of model meshes (useful to orient a ui along a triangle of a model mesh)
17 //4 feature darkplaces csqc: add builtins to clientside qc for gl calls
18
19 #ifndef PF_WARNING
20 #define PF_WARNING(s) do{Con_Printf(s);PRVM_PrintState();return;}while(0)
21 #endif
22
23 //[515]: really need new list ?
24 char *vm_cl_extensions =
25 "DP_CON_SET "
26 "DP_CON_SETA "
27 "DP_CON_STARTMAP "
28 "DP_EF_ADDITIVE "
29 "DP_EF_BLUE "
30 "DP_EF_FLAME "
31 "DP_EF_FULLBRIGHT "
32 "DP_EF_NODEPTHTEST "
33 "DP_EF_NODRAW "
34 "DP_EF_NOSHADOW "
35 "DP_EF_RED "
36 "DP_EF_STARDUST "
37 "DP_ENT_ALPHA "
38 "DP_ENT_CUSTOMCOLORMAP "
39 "DP_ENT_GLOW "
40 "DP_ENT_SCALE "
41 "DP_GFX_EXTERNALTEXTURES "
42 "DP_GFX_FOG "
43 "DP_GFX_QUAKE3MODELTAGS "
44 "DP_GFX_SKINFILES "
45 "DP_GFX_SKYBOX "
46 "DP_HALFLIFE_MAP "
47 "DP_HALFLIFE_MAP_CVAR "
48 "DP_HALFLIFE_SPRITE "
49 "DP_INPUTBUTTONS "
50 "DP_LITSPRITES "
51 "DP_LITSUPPORT "
52 "DP_MONSTERWALK "
53 "DP_MOVETYPEBOUNCEMISSILE "
54 "DP_MOVETYPEFOLLOW "
55 "DP_QC_CHANGEPITCH "
56 "DP_QC_COPYENTITY "
57 "DP_QC_CVAR_STRING "
58 "DP_QC_ETOS "
59 "DP_QC_FINDCHAIN "
60 "DP_QC_FINDCHAINFLAGS "
61 "DP_QC_FINDCHAINFLOAT "
62 "DP_QC_FINDFLAGS "
63 "DP_QC_FINDFLOAT "
64 "DP_QC_FS_SEARCH " // Black: same as in the menu qc
65 "DP_QC_GETLIGHT "
66 "DP_QC_GETSURFACE "
67 "DP_QC_GETTAGINFO "
68 "DP_QC_MINMAXBOUND "
69 "DP_QC_MULTIPLETEMPSTRINGS "
70 "DP_QC_RANDOMVEC "
71 "DP_QC_SINCOSSQRTPOW "
72 //"DP_QC_STRINGBUFFERS "        //[515]: not needed ?
73 "DP_QC_TRACEBOX "
74 //"DP_QC_TRACETOSS "
75 "DP_QC_TRACE_MOVETYPE_HITMODEL "
76 "DP_QC_TRACE_MOVETYPE_WORLDONLY "
77 "DP_QC_VECTORVECTORS "
78 "DP_QUAKE2_MODEL "
79 "DP_QUAKE2_SPRITE "
80 "DP_QUAKE3_MAP "
81 "DP_QUAKE3_MODEL "
82 "DP_REGISTERCVAR "
83 "DP_SND_DIRECTIONLESSATTNNONE "
84 "DP_SND_FAKETRACKS "
85 "DP_SND_OGGVORBIS "
86 "DP_SND_STEREOWAV "
87 "DP_SOLIDCORPSE "
88 "DP_SPRITE32 "
89 "DP_SV_EFFECT "
90 "DP_SV_ROTATINGBMODEL "
91 "DP_SV_SLOWMO "
92 "DP_TE_BLOOD "
93 "DP_TE_BLOODSHOWER "
94 "DP_TE_CUSTOMFLASH "
95 "DP_TE_EXPLOSIONRGB "
96 "DP_TE_FLAMEJET "
97 "DP_TE_PARTICLECUBE "
98 "DP_TE_PARTICLERAIN "
99 "DP_TE_PARTICLESNOW "
100 "DP_TE_PLASMABURN "
101 "DP_TE_QUADEFFECTS1 "
102 "DP_TE_SMALLFLASH "
103 "DP_TE_SPARK "
104 "DP_TE_STANDARDEFFECTBUILTINS "
105 "EXT_BITSHIFT "
106 "EXT_CSQC "
107 "FRIK_FILE "
108 "KRIMZON_SV_PARSECLIENTCOMMAND "
109 "NEH_CMD_PLAY2 "
110 "NXQ_GFX_LETTERBOX "
111 "PRYDON_CLIENTCURSOR "
112 "TENEBRAE_GFX_DLIGHTS "
113 "TW_SV_STEPCONTROL "
114 "NEXUIZ_PLAYERMODEL "
115 "NEXUIZ_PLAYERSKIN "
116 ;
117
118 sfx_t *S_FindName(const char *name);
119 void PF_registercvar (void);
120 int Sbar_GetPlayer (int index);
121 void Sbar_SortFrags (void);
122 void CL_FindNonSolidLocation(const vec3_t in, vec3_t out, vec_t radius);
123 void CL_ExpandCSQCEntities(int num);
124 void CSQC_RelinkAllEntities (int drawmask);
125 void CSQC_RelinkCSQCEntities (void);
126 char *Key_GetBind (int key);
127
128
129
130
131
132 // #1 void(vector ang) makevectors
133 void VM_CL_makevectors (void)
134 {
135         VM_SAFEPARMCOUNT(1, VM_CL_makevectors);
136         AngleVectors (PRVM_G_VECTOR(OFS_PARM0), prog->globals.client->v_forward, prog->globals.client->v_right, prog->globals.client->v_up);
137 }
138
139 // #2 void(entity e, vector o) setorigin
140 void VM_CL_setorigin (void)
141 {
142         prvm_edict_t    *e;
143         float   *org;
144
145         e = PRVM_G_EDICT(OFS_PARM0);
146         if (e == prog->edicts)
147                 PF_WARNING("setorigin: can not modify world entity\n");
148         if (e->priv.required->free)
149                 PF_WARNING("setorigin: can not modify free entity\n");
150         org = PRVM_G_VECTOR(OFS_PARM1);
151         VectorCopy (org, e->fields.client->origin);
152 }
153
154 // #3 void(entity e, string m) setmodel
155 void VM_CL_setmodel (void)
156 {
157         prvm_edict_t    *e;
158         const char              *m;
159         struct model_s  *mod;
160         int                             i;
161
162         VM_SAFEPARMCOUNT(2, VM_CL_setmodel);
163
164         e = PRVM_G_EDICT(OFS_PARM0);
165         m = PRVM_G_STRING(OFS_PARM1);
166         for(i=0;i<MAX_MODELS;i++)
167                 if(!cl.csqc_model_precache[i])
168                         break;
169                 else
170                 if(!strcmp(cl.csqc_model_precache[i]->name, m))
171                 {
172                         e->fields.client->model = PRVM_SetEngineString(cl.csqc_model_precache[i]->name);
173                         e->fields.client->modelindex = -(i+1);
174                         return;
175                 }
176
177         for (i=0, mod = cl.model_precache[0] ; i < MAX_MODELS ; i++, mod = cl.model_precache[i])
178                 if(mod)
179                 if(!strcmp(mod->name, m))
180                 {
181                         e->fields.client->model = PRVM_SetEngineString(mod->name);
182                         e->fields.client->modelindex = i;
183                         return;
184                 }
185         e->fields.client->modelindex = 0;
186         e->fields.client->model = 0;
187 }
188
189 // #4 void(entity e, vector min, vector max) setsize
190 void VM_CL_setsize (void)
191 {
192         prvm_edict_t    *e;
193         float                   *min, *max;
194         VM_SAFEPARMCOUNT(3, VM_CL_setsize);
195
196         e = PRVM_G_EDICT(OFS_PARM0);
197         if (e == prog->edicts)
198                 PF_WARNING("setsize: can not modify world entity\n");
199         if (e->priv.server->free)
200                 PF_WARNING("setsize: can not modify free entity\n");
201         min = PRVM_G_VECTOR(OFS_PARM1);
202         max = PRVM_G_VECTOR(OFS_PARM2);
203
204         VectorCopy (min, e->fields.client->mins);
205         VectorCopy (max, e->fields.client->maxs);
206         VectorSubtract (max, min, e->fields.client->size);
207 }
208
209 // #8 void(entity e, float chan, string samp) sound
210 void VM_CL_sound (void)
211 {
212         const char                      *sample;
213         int                                     channel;
214         prvm_edict_t            *entity;
215         int                             volume;
216         float                           attenuation;
217
218         VM_SAFEPARMCOUNT(5, VM_CL_sound);
219
220         entity = PRVM_G_EDICT(OFS_PARM0);
221         channel = (int)PRVM_G_FLOAT(OFS_PARM1);
222         sample = PRVM_G_STRING(OFS_PARM2);
223         volume = (int)(PRVM_G_FLOAT(OFS_PARM3)*255.0f);
224         attenuation = PRVM_G_FLOAT(OFS_PARM4);
225
226         if (volume < 0 || volume > 255)
227                 PF_WARNING("VM_CL_sound: volume must be in range 0-1\n");
228
229         if (attenuation < 0 || attenuation > 4)
230                 PF_WARNING("VM_CL_sound: attenuation must be in range 0-4\n");
231
232         if (channel < 0 || channel > 7)
233                 PF_WARNING("VM_CL_sound: channel must be in range 0-7\n");
234
235         S_StartSound(32768 + PRVM_NUM_FOR_EDICT(entity), channel, S_FindName(sample), entity->fields.client->origin, volume, attenuation);
236 }
237
238 // #14 entity() spawn
239 void VM_CL_spawn (void)
240 {
241         prvm_edict_t *ed;
242         ed = PRVM_ED_Alloc();
243         ed->fields.client->entnum = PRVM_NUM_FOR_EDICT(ed);     //[515]: not needed any more ?
244         VM_RETURN_EDICT(ed);
245 }
246
247 // #16 float(vector v1, vector v2, float tryents) traceline
248 void VM_CL_traceline (void)
249 {
250         float   *v1, *v2;
251         trace_t trace;
252         int             ent;
253
254         v1 = PRVM_G_VECTOR(OFS_PARM0);
255         v2 = PRVM_G_VECTOR(OFS_PARM1);
256
257         trace = CL_TraceBox(v1, vec3_origin, vec3_origin, v2, 1, &ent, 1, false);
258
259         prog->globals.client->trace_allsolid = trace.allsolid;
260         prog->globals.client->trace_startsolid = trace.startsolid;
261         prog->globals.client->trace_fraction = trace.fraction;
262         prog->globals.client->trace_inwater = trace.inwater;
263         prog->globals.client->trace_inopen = trace.inopen;
264         VectorCopy (trace.endpos, prog->globals.client->trace_endpos);
265         VectorCopy (trace.plane.normal, prog->globals.client->trace_plane_normal);
266         prog->globals.client->trace_plane_dist =  trace.plane.dist;
267         if (ent)
268                 prog->globals.client->trace_ent = ent;
269         else
270                 prog->globals.client->trace_ent = PRVM_EDICT_TO_PROG(prog->edicts);
271 }
272
273 // #19 void(string s) precache_sound
274 void VM_CL_precache_sound (void)
275 {
276         const char *n;
277         VM_SAFEPARMCOUNT(1, VM_CL_precache_sound);
278         n = PRVM_G_STRING(OFS_PARM0);
279         S_PrecacheSound(n, true, false);
280 }
281
282 // #20 void(string s) precache_model
283 void VM_CL_precache_model (void)
284 {
285         const char      *name;
286         int                     i;
287         model_t         *m;
288
289         VM_SAFEPARMCOUNT(1, VM_CL_precache_model);
290
291         name = PRVM_G_STRING(OFS_PARM0);
292         for(i=1;i<MAX_MODELS;i++)
293                 if(!cl.csqc_model_precache[i])
294                 {
295                         i = 0;
296                         break;
297                 }
298                 else
299                 if(!strcmp(cl.csqc_model_precache[i]->name, name))
300                 {
301                         i = -(i+1);
302                         break;
303                 }
304         if(i)
305         {
306                 PRVM_G_FLOAT(OFS_RETURN) = i;
307                 return;
308         }
309         PRVM_G_FLOAT(OFS_RETURN) = 0;
310         m = Mod_ForName(name, false, false, false);
311         if(m && m->loaded)
312         {
313                 for(i=1;i<MAX_MODELS;i++)
314                         if(!cl.csqc_model_precache[i])
315                                 break;
316                 if(i == MAX_MODELS)
317                         PF_WARNING("VM_CL_precache_model: no free models\n");
318                 cl.csqc_model_precache[i] = (model_t*)m;
319                 PRVM_G_FLOAT(OFS_RETURN) = -(i+1);
320                 return;
321         }
322         Con_Printf("VM_CL_precache_model: model \"%s\" not found\n", name);
323 }
324
325 int CSQC_EntitiesInBox (vec3_t mins, vec3_t maxs, int maxlist, prvm_edict_t **list)
326 {
327         prvm_edict_t    *ent;
328         int                             i, k;
329
330         ent = PRVM_NEXT_EDICT(prog->edicts);
331         for(k=0,i=1; i<prog->num_edicts ;i++, ent = PRVM_NEXT_EDICT(ent))
332         {
333                 if (ent->priv.required->free)
334                         continue;
335 //              VectorAdd(ent->fields.client->origin, ent->fields.client->mins, ent->fields.client->absmin);
336 //              VectorAdd(ent->fields.client->origin, ent->fields.client->maxs, ent->fields.client->absmax);
337                 if(BoxesOverlap(mins, maxs, ent->fields.client->absmin, ent->fields.client->absmax))
338                         list[k++] = ent;
339         }
340         return k;
341 }
342
343 // #22 entity(vector org, float rad) findradius
344 void VM_CL_findradius (void)
345 {
346         prvm_edict_t    *ent, *chain;
347         vec_t                   radius, radius2;
348         vec3_t                  org, eorg, mins, maxs;
349         int                             i, numtouchedicts;
350         prvm_edict_t    *touchedicts[MAX_EDICTS];
351
352         chain = (prvm_edict_t *)prog->edicts;
353
354         VectorCopy(PRVM_G_VECTOR(OFS_PARM0), org);
355         radius = PRVM_G_FLOAT(OFS_PARM1);
356         radius2 = radius * radius;
357
358         mins[0] = org[0] - (radius + 1);
359         mins[1] = org[1] - (radius + 1);
360         mins[2] = org[2] - (radius + 1);
361         maxs[0] = org[0] + (radius + 1);
362         maxs[1] = org[1] + (radius + 1);
363         maxs[2] = org[2] + (radius + 1);
364         numtouchedicts = CSQC_EntitiesInBox(mins, maxs, MAX_EDICTS, touchedicts);
365         if (numtouchedicts > MAX_EDICTS)
366         {
367                 // this never happens   //[515]: for what then ?
368                 Con_Printf("CSQC_EntitiesInBox returned %i edicts, max was %i\n", numtouchedicts, MAX_EDICTS);
369                 numtouchedicts = MAX_EDICTS;
370         }
371         for (i = 0;i < numtouchedicts;i++)
372         {
373                 ent = touchedicts[i];
374                 // Quake did not return non-solid entities but darkplaces does
375                 // (note: this is the reason you can't blow up fallen zombies)
376                 if (ent->fields.client->solid == SOLID_NOT && !sv_gameplayfix_blowupfallenzombies.integer)
377                         continue;
378                 // LordHavoc: compare against bounding box rather than center so it
379                 // doesn't miss large objects, and use DotProduct instead of Length
380                 // for a major speedup
381                 VectorSubtract(org, ent->fields.client->origin, eorg);
382                 if (sv_gameplayfix_findradiusdistancetobox.integer)
383                 {
384                         eorg[0] -= bound(ent->fields.client->mins[0], eorg[0], ent->fields.client->maxs[0]);
385                         eorg[1] -= bound(ent->fields.client->mins[1], eorg[1], ent->fields.client->maxs[1]);
386                         eorg[2] -= bound(ent->fields.client->mins[2], eorg[2], ent->fields.client->maxs[2]);
387                 }
388                 else
389                         VectorMAMAM(1, eorg, 0.5f, ent->fields.client->mins, 0.5f, ent->fields.client->maxs, eorg);
390                 if (DotProduct(eorg, eorg) < radius2)
391                 {
392                         ent->fields.client->chain = PRVM_EDICT_TO_PROG(chain);
393                         chain = ent;
394                 }
395         }
396
397         VM_RETURN_EDICT(chain);
398 }
399
400 // #34 float() droptofloor
401 void VM_CL_droptofloor (void)
402 {
403         prvm_edict_t            *ent;
404         vec3_t                          end;
405         trace_t                         trace;
406         int                                     i;
407
408         // assume failure if it returns early
409         PRVM_G_FLOAT(OFS_RETURN) = 0;
410
411         ent = PRVM_PROG_TO_EDICT(prog->globals.client->self);
412         if (ent == prog->edicts)
413                 PF_WARNING("droptofloor: can not modify world entity\n");
414         if (ent->priv.server->free)
415                 PF_WARNING("droptofloor: can not modify free entity\n");
416
417         VectorCopy (ent->fields.client->origin, end);
418         end[2] -= 256;
419
420         trace = CL_TraceBox(ent->fields.client->origin, ent->fields.client->mins, ent->fields.client->maxs, end, 1, &i, 1, false);
421
422         if (trace.fraction != 1)
423         {
424                 VectorCopy (trace.endpos, ent->fields.client->origin);
425                 ent->fields.client->flags = (int)ent->fields.client->flags | FL_ONGROUND;
426 //              ent->fields.client->groundentity = PRVM_EDICT_TO_PROG(trace.ent);
427                 PRVM_G_FLOAT(OFS_RETURN) = 1;
428                 // if support is destroyed, keep suspended (gross hack for floating items in various maps)
429 //              ent->priv.server->suspendedinairflag = true;
430         }
431 }
432
433 // #35 void(float style, string value) lightstyle
434 void VM_CL_lightstyle (void)
435 {
436         int                     i;
437         const char      *c;
438
439         VM_SAFEPARMCOUNT(2, VM_CL_lightstyle);
440
441         i = (int)PRVM_G_FLOAT(OFS_PARM0);
442         c = PRVM_G_STRING(OFS_PARM1);
443         if (i >= cl.max_lightstyle)
444                 PF_WARNING("VM_CL_lightstyle >= MAX_LIGHTSTYLES\n");
445         strlcpy (cl.lightstyle[i].map,  MSG_ReadString(), sizeof (cl.lightstyle[i].map));
446         cl.lightstyle[i].map[MAX_STYLESTRING - 1] = 0;
447         cl.lightstyle[i].length = (int)strlen(cl.lightstyle[i].map);
448 }
449
450 // #40 float(entity e) checkbottom
451 void VM_CL_checkbottom (void)
452 {
453         static int              cs_yes, cs_no;
454         prvm_edict_t    *ent;
455         vec3_t                  mins, maxs, start, stop;
456         trace_t                 trace;
457         int                             x, y, hit;
458         float                   mid, bottom;
459
460         VM_SAFEPARMCOUNT(1, VM_CL_checkbottom);
461         ent = PRVM_G_EDICT(OFS_PARM0);
462         PRVM_G_FLOAT(OFS_RETURN) = 0;
463
464         VectorAdd (ent->fields.client->origin, ent->fields.client->mins, mins);
465         VectorAdd (ent->fields.client->origin, ent->fields.client->maxs, maxs);
466
467 // if all of the points under the corners are solid world, don't bother
468 // with the tougher checks
469 // the corners must be within 16 of the midpoint
470         start[2] = mins[2] - 1;
471         for     (x=0 ; x<=1 ; x++)
472                 for     (y=0 ; y<=1 ; y++)
473                 {
474                         start[0] = x ? maxs[0] : mins[0];
475                         start[1] = y ? maxs[1] : mins[1];
476                         if (!(CL_PointSuperContents(start) & (SUPERCONTENTS_SOLID | SUPERCONTENTS_BODY)))
477                                 goto realcheck;
478                 }
479
480         cs_yes++;
481         PRVM_G_FLOAT(OFS_RETURN) = true;
482         return;         // we got out easy
483
484 realcheck:
485         cs_no++;
486 //
487 // check it for real...
488 //
489         start[2] = mins[2];
490
491 // the midpoint must be within 16 of the bottom
492         start[0] = stop[0] = (mins[0] + maxs[0])*0.5;
493         start[1] = stop[1] = (mins[1] + maxs[1])*0.5;
494         stop[2] = start[2] - 2*sv_stepheight.value;
495         trace = CL_TraceBox (start, vec3_origin, vec3_origin, stop, 1, &hit, 1, true);
496
497         if (trace.fraction == 1.0)
498                 return;
499
500         mid = bottom = trace.endpos[2];
501
502 // the corners must be within 16 of the midpoint
503         for     (x=0 ; x<=1 ; x++)
504                 for     (y=0 ; y<=1 ; y++)
505                 {
506                         start[0] = stop[0] = x ? maxs[0] : mins[0];
507                         start[1] = stop[1] = y ? maxs[1] : mins[1];
508
509                         trace = CL_TraceBox (start, vec3_origin, vec3_origin, stop, 1, &hit, 1, true);
510
511                         if (trace.fraction != 1.0 && trace.endpos[2] > bottom)
512                                 bottom = trace.endpos[2];
513                         if (trace.fraction == 1.0 || mid - trace.endpos[2] > sv_stepheight.value)
514                                 return;
515                 }
516
517         cs_yes++;
518         PRVM_G_FLOAT(OFS_RETURN) = true;
519 }
520
521 // #41 float(vector v) pointcontents
522 void VM_CL_pointcontents (void)
523 {
524         VM_SAFEPARMCOUNT(1, VM_CL_pointcontents);
525         PRVM_G_FLOAT(OFS_RETURN) = Mod_Q1BSP_NativeContentsFromSuperContents(NULL, CL_PointSuperContents(PRVM_G_VECTOR(OFS_PARM0)));
526 }
527
528 // #48 void(vector o, vector d, float color, float count) particle
529 void VM_CL_particle (void)
530 {
531         float   *org, *dir;
532         int             count;
533         unsigned char   color;
534         VM_SAFEPARMCOUNT(4, VM_CL_particle);
535
536         org = PRVM_G_VECTOR(OFS_PARM0);
537         dir = PRVM_G_VECTOR(OFS_PARM1);
538         color = (int)PRVM_G_FLOAT(OFS_PARM2);
539         count = (int)PRVM_G_FLOAT(OFS_PARM3);
540         CL_ParticleEffect(EFFECT_SVC_PARTICLE, count, org, org, dir, dir, NULL, color);
541 }
542
543 // #49 void(entity ent, float ideal_yaw, float speed_yaw) ChangeYaw
544 void VM_CL_changeyaw (void)
545 {
546         prvm_edict_t    *ent;
547         float                   ideal, current, move, speed;
548         VM_SAFEPARMCOUNT(3, VM_CL_changeyaw);
549
550         ent = PRVM_G_EDICT(OFS_PARM0);
551         if (ent == prog->edicts)
552                 PF_WARNING("changeyaw: can not modify world entity\n");
553         if (ent->priv.server->free)
554                 PF_WARNING("changeyaw: can not modify free entity\n");
555         current = ANGLEMOD(ent->fields.client->angles[1]);
556         ideal = PRVM_G_FLOAT(OFS_PARM1);
557         speed = PRVM_G_FLOAT(OFS_PARM2);
558
559         if (current == ideal)
560                 return;
561         move = ideal - current;
562         if (ideal > current)
563         {
564                 if (move >= 180)
565                         move = move - 360;
566         }
567         else
568         {
569                 if (move <= -180)
570                         move = move + 360;
571         }
572         if (move > 0)
573         {
574                 if (move > speed)
575                         move = speed;
576         }
577         else
578         {
579                 if (move < -speed)
580                         move = -speed;
581         }
582
583         ent->fields.client->angles[1] = ANGLEMOD (current + move);
584 }
585
586 // #63 void(entity ent, float ideal_pitch, float speed_pitch) changepitch (DP_QC_CHANGEPITCH)
587 void VM_CL_changepitch (void)
588 {
589         prvm_edict_t            *ent;
590         float                           ideal, current, move, speed;
591         VM_SAFEPARMCOUNT(3, VM_CL_changepitch);
592
593         ent = PRVM_G_EDICT(OFS_PARM0);
594         if (ent == prog->edicts)
595                 PF_WARNING("changepitch: can not modify world entity\n");
596         if (ent->priv.server->free)
597                 PF_WARNING("changepitch: can not modify free entity\n");
598         current = ANGLEMOD( ent->fields.client->angles[0] );
599         ideal = PRVM_G_FLOAT(OFS_PARM1);
600         speed = PRVM_G_FLOAT(OFS_PARM2);
601
602         if (current == ideal)
603                 return;
604         move = ideal - current;
605         if (ideal > current)
606         {
607                 if (move >= 180)
608                         move = move - 360;
609         }
610         else
611         {
612                 if (move <= -180)
613                         move = move + 360;
614         }
615         if (move > 0)
616         {
617                 if (move > speed)
618                         move = speed;
619         }
620         else
621         {
622                 if (move < -speed)
623                         move = -speed;
624         }
625
626         ent->fields.client->angles[0] = ANGLEMOD (current + move);
627 }
628
629 // #64 void(entity e, entity ignore) tracetoss (DP_QC_TRACETOSS)
630 void VM_CL_tracetoss (void)
631 {
632 /*      trace_t trace;
633         prvm_edict_t    *ent;
634         prvm_edict_t    *ignore;
635
636         ent = PRVM_G_EDICT(OFS_PARM0);
637         if (ent == prog->edicts)
638                 PF_WARNING("tracetoss: can not use world entity\n");
639         ignore = PRVM_G_EDICT(OFS_PARM1);
640
641 //FIXME
642         trace = SV_Trace_Toss (ent, ignore);
643
644         prog->globals.server->trace_allsolid = trace.allsolid;
645         prog->globals.server->trace_startsolid = trace.startsolid;
646         prog->globals.server->trace_fraction = trace.fraction;
647         prog->globals.server->trace_inwater = trace.inwater;
648         prog->globals.server->trace_inopen = trace.inopen;
649         VectorCopy (trace.endpos, prog->globals.server->trace_endpos);
650         VectorCopy (trace.plane.normal, prog->globals.server->trace_plane_normal);
651         prog->globals.server->trace_plane_dist =  trace.plane.dist;
652         if (trace.ent)
653                 prog->globals.server->trace_ent = PRVM_EDICT_TO_PROG(trace.ent);
654         else
655                 prog->globals.server->trace_ent = PRVM_EDICT_TO_PROG(prog->edicts);
656 */
657 }
658
659 // #74 void(vector pos, string samp, float vol, float atten) ambientsound
660 void VM_CL_ambientsound (void)
661 {
662         float   *f;
663         sfx_t   *s;
664         VM_SAFEPARMCOUNT(4, VM_CL_ambientsound);
665         s = S_FindName(PRVM_G_STRING(OFS_PARM0));
666         f = PRVM_G_VECTOR(OFS_PARM1);
667         S_StaticSound (s, f, PRVM_G_FLOAT(OFS_PARM2), PRVM_G_FLOAT(OFS_PARM3)*64);
668 }
669
670 // #90 void(vector v1, vector min, vector max, vector v2, float nomonsters, entity forent) tracebox (DP_QC_TRACEBOX)
671 void VM_CL_tracebox (void)
672 {
673         float   *v1, *v2, *m1, *m2;
674         trace_t trace;
675         int             ent;
676
677         v1 = PRVM_G_VECTOR(OFS_PARM0);
678         m1 = PRVM_G_VECTOR(OFS_PARM1);
679         m2 = PRVM_G_VECTOR(OFS_PARM2);
680         v2 = PRVM_G_VECTOR(OFS_PARM3);
681
682         trace = CL_TraceBox(v1, m1, m2, v2, 1, &ent, 1, false);
683
684         prog->globals.client->trace_allsolid = trace.allsolid;
685         prog->globals.client->trace_startsolid = trace.startsolid;
686         prog->globals.client->trace_fraction = trace.fraction;
687         prog->globals.client->trace_inwater = trace.inwater;
688         prog->globals.client->trace_inopen = trace.inopen;
689         VectorCopy (trace.endpos, prog->globals.client->trace_endpos);
690         VectorCopy (trace.plane.normal, prog->globals.client->trace_plane_normal);
691         prog->globals.client->trace_plane_dist =  trace.plane.dist;
692         if (ent)
693                 prog->globals.client->trace_ent = ent;
694         else
695                 prog->globals.client->trace_ent = PRVM_EDICT_TO_PROG(prog->edicts);
696 }
697
698 // #92 vector(vector org) getlight (DP_QC_GETLIGHT)
699 void VM_CL_getlight (void)
700 {
701         vec3_t ambientcolor, diffusecolor, diffusenormal;
702         vec_t *p;
703
704         VM_SAFEPARMCOUNT(1, VM_CL_getlight);
705
706         p = PRVM_G_VECTOR(OFS_PARM0);
707         VectorClear(ambientcolor);
708         VectorClear(diffusecolor);
709         VectorClear(diffusenormal);
710         if (cl.worldmodel && cl.worldmodel->brush.LightPoint)
711                 cl.worldmodel->brush.LightPoint(cl.worldmodel, p, ambientcolor, diffusecolor, diffusenormal);
712         VectorMA(ambientcolor, 0.5, diffusecolor, PRVM_G_VECTOR(OFS_RETURN));
713 }
714
715
716 //============================================================================
717 //[515]: SCENE MANAGER builtins
718 void V_CalcRefdef (void);//view.c
719 void CSQC_R_ClearScreen (void);//gl_rmain.c
720 void CSQC_R_RenderScene (void);//gl_rmain.c
721 void CSQC_AddEntity (int n);//csprogs.c
722 void CSQC_ClearCSQCEntities (void);
723
724 matrix4x4_t csqc_listenermatrix;
725 qboolean csqc_usecsqclistener = false, csqc_frame = false;//[515]: per-frame
726 qboolean csqc_onground;
727
728 static void CSQC_R_RecalcView (void)
729 {
730         extern matrix4x4_t viewmodelmatrix;
731         viewmodelmatrix = identitymatrix;
732         r_refdef.viewentitymatrix = identitymatrix;
733         Matrix4x4_CreateFromQuakeEntity(&r_refdef.viewentitymatrix, csqc_origin[0], csqc_origin[1], csqc_origin[2], csqc_angles[0], csqc_angles[1], csqc_angles[2], 1);
734         Matrix4x4_CreateFromQuakeEntity(&viewmodelmatrix, csqc_origin[0], csqc_origin[1], csqc_origin[2], csqc_angles[0], csqc_angles[1], csqc_angles[2], 0.3);
735 }
736
737 //#300 void() clearscene (EXT_CSQC)
738 void VM_R_ClearScene (void)
739 {
740         VM_SAFEPARMCOUNT(0, VM_R_ClearScene);
741 //      CSQC_R_RecalcView();
742         if(csqc_frame)
743                 CSQC_ClearCSQCEntities();
744         CSQC_R_ClearScreen();
745 }
746
747 //#301 void(float mask) addentities (EXT_CSQC)
748 void VM_R_AddEntities (void)
749 {
750         VM_SAFEPARMCOUNT(1, VM_R_AddEntities);
751         csqc_drawmask = (int)PRVM_G_FLOAT(OFS_PARM0);
752 }
753
754 //#302 void(entity ent) addentity (EXT_CSQC)
755 void VM_R_AddEntity (void)
756 {
757         VM_SAFEPARMCOUNT(1, VM_R_AddEntity);
758         CSQC_AddEntity(PRVM_NUM_FOR_EDICT(PRVM_G_EDICT(OFS_PARM0)));
759 }
760
761 //#303 float(float property, ...) setproperty (EXT_CSQC)
762 void VM_R_SetView (void)
763 {
764         int             c;
765         float   *f;
766         float   k;
767
768         if(prog->argc < 2)
769                 VM_SAFEPARMCOUNT(2, VM_R_SetView);
770
771         c = (int)PRVM_G_FLOAT(OFS_PARM0);
772         f = PRVM_G_VECTOR(OFS_PARM1);
773         k = PRVM_G_FLOAT(OFS_PARM1);
774
775         switch(c)
776         {
777         case VF_MIN:                    r_refdef.x = (int)f[0];
778                                                         r_refdef.y = (int)f[1];
779                                                         break;
780         case VF_MIN_X:                  r_refdef.x = (int)k;
781                                                         break;
782         case VF_MIN_Y:                  r_refdef.y = (int)k;
783                                                         break;
784         case VF_SIZE:                   r_refdef.width = (int)f[0];
785                                                         r_refdef.height = (int)f[1];
786                                                         break;
787         case VF_SIZE_Y:                 r_refdef.width = (int)k;
788                                                         break;
789         case VF_SIZE_X:                 r_refdef.height = (int)k;
790                                                         break;
791         case VF_VIEWPORT:               r_refdef.x = (int)f[0];
792                                                         r_refdef.y = (int)f[1];
793                                                         f = PRVM_G_VECTOR(OFS_PARM2);
794                                                         r_refdef.width = (int)f[0];
795                                                         r_refdef.height = (int)f[1];
796                                                         break;
797         case VF_FOV:                    //r_refdef.fov_x = f[0]; // FIXME!
798                                                         //r_refdef.fov_y = f[1]; // FIXME!
799                                                         break;
800         case VF_FOVX:                   //r_refdef.fov_x = k; // FIXME!
801                                                         break;
802         case VF_FOVY:                   //r_refdef.fov_y = k; // FIXME!
803                                                         break;
804         case VF_ORIGIN:                 VectorCopy(f, csqc_origin);
805                                                         CSQC_R_RecalcView();
806                                                         break;
807         case VF_ORIGIN_X:               csqc_origin[0] = k;
808                                                         CSQC_R_RecalcView();
809                                                         break;
810         case VF_ORIGIN_Y:               csqc_origin[1] = k;
811                                                         CSQC_R_RecalcView();
812                                                         break;
813         case VF_ORIGIN_Z:               csqc_origin[2] = k;
814                                                         CSQC_R_RecalcView();
815                                                         break;
816         case VF_ANGLES:                 VectorCopy(f, csqc_angles);
817                                                         CSQC_R_RecalcView();
818                                                         break;
819         case VF_ANGLES_X:               csqc_angles[0] = k;
820                                                         CSQC_R_RecalcView();
821                                                         break;
822         case VF_ANGLES_Y:               csqc_angles[1] = k;
823                                                         CSQC_R_RecalcView();
824                                                         break;
825         case VF_ANGLES_Z:               csqc_angles[2] = k;
826                                                         CSQC_R_RecalcView();
827                                                         break;
828         case VF_DRAWWORLD:              cl.csqc_vidvars.drawworld = k;
829                                                         break;
830         case VF_DRAWENGINESBAR: cl.csqc_vidvars.drawenginesbar = k;
831                                                         break;
832         case VF_DRAWCROSSHAIR:  cl.csqc_vidvars.drawcrosshair = k;
833                                                         break;
834
835         case VF_CL_VIEWANGLES:  VectorCopy(f, cl.viewangles);
836                                                         break;
837         case VF_CL_VIEWANGLES_X:cl.viewangles[0] = k;
838                                                         break;
839         case VF_CL_VIEWANGLES_Y:cl.viewangles[1] = k;
840                                                         break;
841         case VF_CL_VIEWANGLES_Z:cl.viewangles[2] = k;
842                                                         break;
843
844         default:                                Con_Printf("VM_R_SetView : unknown parm %i\n", c);
845                                                         PRVM_G_FLOAT(OFS_RETURN) = 0;
846                                                         return;
847         }
848         PRVM_G_FLOAT(OFS_RETURN) = 1;
849 }
850
851 //#304 void() renderscene (EXT_CSQC)
852 void VM_R_RenderScene (void) //#134
853 {
854         VM_SAFEPARMCOUNT(0, VM_R_RenderScene);
855
856         if(csqc_frame)
857         {
858                 CSQC_RelinkCSQCEntities();
859                 CSQC_RelinkAllEntities(csqc_drawmask);
860         }
861
862         CSQC_R_RenderScene();
863 }
864
865 //#305 void(vector org, float radius, vector lightcolours) adddynamiclight (EXT_CSQC)
866 void VM_R_AddDynamicLight (void)
867 {
868         float           *pos, *col;
869         matrix4x4_t     tempmatrix;
870         VM_SAFEPARMCOUNT(3, VM_R_AddDynamicLight);
871
872         pos = PRVM_G_VECTOR(OFS_PARM0);
873         col = PRVM_G_VECTOR(OFS_PARM2);
874         Matrix4x4_CreateTranslate(&tempmatrix, pos[0], pos[1], pos[2]);
875         CL_AllocDlight(NULL, &tempmatrix, PRVM_G_FLOAT(OFS_PARM1), col[0], col[1], col[2], 500, 0, 0, -1, true, 1, 0.25, 0.25, 1, 1, LIGHTFLAG_NORMALMODE | LIGHTFLAG_REALTIMEMODE);
876         //CL_AllocDlight(NULL, &tempmatrix, PRVM_G_FLOAT(OFS_PARM1), col[0], col[1], col[2], 500, 0.2, 0, -1, true, 1, 0.25, 1, 0, 0, LIGHTFLAG_NORMALMODE | LIGHTFLAG_REALTIMEMODE);
877 }
878
879 //============================================================================
880
881 //#310 vector (vector v) cs_unproject (EXT_CSQC)
882 void VM_CL_unproject (void)
883 {
884         float   *f;
885         vec3_t  temp;
886
887         VM_SAFEPARMCOUNT(1, VM_CL_unproject);
888         f = PRVM_G_VECTOR(OFS_PARM0);
889         VectorSet(temp, f[2], f[0] * f[2] * -r_refdef.frustum_x * 2.0 / r_refdef.width, f[1] * f[2] * -r_refdef.frustum_y * 2.0 / r_refdef.height);
890         Matrix4x4_Transform(&r_refdef.viewentitymatrix, temp, PRVM_G_VECTOR(OFS_RETURN));
891 }
892
893 //#311 vector (vector v) cs_project (EXT_CSQC)
894 void VM_CL_project (void)
895 {
896         float   *f;
897         vec3_t  v;
898         matrix4x4_t m;
899
900         VM_SAFEPARMCOUNT(1, VM_CL_project);
901         f = PRVM_G_VECTOR(OFS_PARM0);
902         Matrix4x4_Invert_Simple(&m, &r_refdef.viewentitymatrix);
903         Matrix4x4_Transform(&m, f, v);
904         VectorSet(PRVM_G_VECTOR(OFS_RETURN), v[1]/v[0]/-r_refdef.frustum_x*0.5*r_refdef.width, v[2]/v[0]/-r_refdef.frustum_y*r_refdef.height*0.5, v[0]);
905 }
906
907 //#330 float(float stnum) getstatf (EXT_CSQC)
908 void VM_CL_getstatf (void)
909 {
910         int i;
911         union
912         {
913                 float f;
914                 int l;
915         }dat;
916         VM_SAFEPARMCOUNT(1, VM_CL_getstatf);
917         i = (int)PRVM_G_FLOAT(OFS_PARM0);
918         if(i < 0 || i >= MAX_CL_STATS)
919         {
920                 Con_Printf("VM_CL_getstatf: index>=MAX_CL_STATS or index<0\n");
921                 return;
922         }
923         dat.l = cl.stats[i];
924         PRVM_G_FLOAT(OFS_RETURN) =  dat.f;
925 }
926
927 //#331 float(float stnum) getstati (EXT_CSQC)
928 void VM_CL_getstati (void)
929 {
930         int i, index;
931         VM_SAFEPARMCOUNT(1, VM_CL_getstati);
932         index = (int)PRVM_G_FLOAT(OFS_PARM0);
933
934         if(index < 0 || index >= MAX_CL_STATS)
935         {
936                 Con_Printf("VM_CL_getstati: index>=MAX_CL_STATS or index<0\n");
937                 return;
938         }
939         i = cl.stats[index];
940         PRVM_G_FLOAT(OFS_RETURN) = i;
941 }
942
943 //#332 string(float firststnum) getstats (EXT_CSQC)
944 void VM_CL_getstats (void)
945 {
946         int i;
947         char *t;
948         VM_SAFEPARMCOUNT(1, VM_CL_getstats);
949         i = (int)PRVM_G_FLOAT(OFS_PARM0);
950         if(i < 0 || i > MAX_CL_STATS-4)
951         {
952                 Con_Printf("VM_CL_getstats: index>MAX_CL_STATS-4 or index<0\n");
953                 return;
954         }
955         t = VM_GetTempString();
956         strlcpy(t, (char*)&cl.stats[i], 16);
957         PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(t);
958 }
959
960 //#333 void(entity e, float mdlindex) setmodelindex (EXT_CSQC)
961 void VM_CL_setmodelindex (void)
962 {
963         int                             i;
964         prvm_edict_t    *t;
965         struct model_s  *m;
966
967         VM_SAFEPARMCOUNT(2, VM_CL_setmodelindex);
968
969         t = PRVM_G_EDICT(OFS_PARM0);
970         i = (int)PRVM_G_FLOAT(OFS_PARM1);
971
972         t->fields.client->model = 0;
973         t->fields.client->modelindex = 0;
974
975         if(!i)
976                 return;
977         if(i<0)
978         {
979                 i = -(i+1);
980                 if(i >= MAX_MODELS)
981                         PF_WARNING("VM_CL_setmodelindex >= MAX_MODELS\n");
982                 m = cl.csqc_model_precache[i];
983         }
984         else
985                 if(i >= MAX_MODELS)
986                         PF_WARNING("VM_CL_setmodelindex >= MAX_MODELS\n");
987                 else
988                         m = cl.model_precache[i];
989         if(!m)
990                 PF_WARNING("VM_CL_setmodelindex: null model\n");
991         t->fields.client->model = PRVM_SetEngineString(m->name);
992         t->fields.client->modelindex = i;
993 }
994
995 //#334 string(float mdlindex) modelnameforindex (EXT_CSQC)
996 void VM_CL_modelnameforindex (void)
997 {
998         int i;
999
1000         VM_SAFEPARMCOUNT(1, VM_CL_modelnameforindex);
1001
1002         PRVM_G_INT(OFS_RETURN) = 0;
1003         i = (int)PRVM_G_FLOAT(OFS_PARM0);
1004         if(i<0)
1005         {
1006                 i = -(i+1);
1007                 if(i >= MAX_MODELS)
1008                         PF_WARNING("VM_CL_modelnameforindex >= MAX_MODELS\n");
1009                 if(cl.csqc_model_precache[i])
1010                         PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(cl.csqc_model_precache[i]->name);
1011                 return;
1012         }
1013         if(i >= MAX_MODELS)
1014                 PF_WARNING("VM_CL_modelnameforindex >= MAX_MODELS\n");
1015         if(cl.model_precache[i])
1016                 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(cl.model_precache[i]->name);
1017 }
1018
1019 //#335 float(string effectname) particleeffectnum (EXT_CSQC)
1020 void VM_CL_particleeffectnum (void)
1021 {
1022         const char      *n;
1023         int                     i;
1024         VM_SAFEPARMCOUNT(1, VM_CL_particleeffectnum);
1025         n = PRVM_G_STRING(OFS_PARM0);
1026         i = CL_ParticleEffectIndexForName(n);
1027         if (i == 0)
1028                 i = -1;
1029         PRVM_G_FLOAT(OFS_RETURN) = i;
1030 }
1031
1032 void CSQC_ParseBeam (int ent, vec3_t start, vec3_t end, model_t *m, int lightning)
1033 {
1034         int             i;
1035         beam_t  *b;
1036
1037         // override any beam with the same entity
1038         for (i = 0, b = cl.beams;i < cl.max_beams;i++, b++)
1039         {
1040                 if (b->entity == ent && ent)
1041                 {
1042                         //b->entity = ent;
1043                         b->lightning = lightning;
1044                         b->relativestartvalid = (ent && cl.csqcentities[ent].state_current.active) ? 2 : 0;
1045                         b->model = m;
1046                         b->endtime = cl.time + 0.2;
1047                         VectorCopy (start, b->start);
1048                         VectorCopy (end, b->end);
1049                         return;
1050                 }
1051         }
1052
1053         // find a free beam
1054         for (i = 0, b = cl.beams;i < cl.max_beams;i++, b++)
1055         {
1056                 if (!b->model || b->endtime < cl.time)
1057                 {
1058                         b->entity = ent;
1059                         b->lightning = lightning;
1060                         b->relativestartvalid = (ent && cl.csqcentities[ent].state_current.active) ? 2 : 0;
1061                         b->model = m;
1062                         b->endtime = cl.time + 0.2;
1063                         VectorCopy (start, b->start);
1064                         VectorCopy (end, b->end);
1065                         return;
1066                 }
1067         }
1068         Con_Print("beam list overflow!\n");
1069 }
1070
1071 // #336 void(entity ent, float effectnum, vector start, vector end[, float color]) trailparticles (EXT_CSQC)
1072 void VM_CL_trailparticles (void)
1073 {
1074         int                             i, entnum;
1075         float                   *start, *end;
1076         prvm_edict_t    *t;
1077         VM_SAFEPARMCOUNT(4, VM_CL_trailparticles);
1078
1079         t = PRVM_G_EDICT(OFS_PARM0);
1080         entnum  = PRVM_NUM_FOR_EDICT(t);
1081         i               = (int)PRVM_G_FLOAT(OFS_PARM1);
1082         start   = PRVM_G_VECTOR(OFS_PARM2);
1083         end             = PRVM_G_VECTOR(OFS_PARM3);
1084
1085         if (entnum >= MAX_EDICTS)
1086         {
1087                 Con_Printf("CSQC_ParseBeam: invalid entity number %i\n", entnum);
1088                 return;
1089         }
1090         if (entnum >= cl.max_csqcentities)
1091                 CL_ExpandCSQCEntities(entnum);
1092
1093         CL_ParticleEffect(i, VectorDistance(start, end), start, end, t->fields.client->velocity, t->fields.client->velocity, &cl.csqcentities[entnum], (int)PRVM_G_FLOAT(OFS_PARM4));
1094 }
1095
1096 //#337 void(float effectnum, vector origin, vector dir, float count) pointparticles (EXT_CSQC)
1097 void VM_CL_pointparticles (void)
1098 {
1099         int                     i, n;
1100         float           *f, *v;
1101         VM_SAFEPARMCOUNT(4, VM_CL_pointparticles);
1102         i = (int)PRVM_G_FLOAT(OFS_PARM0);
1103         f = PRVM_G_VECTOR(OFS_PARM1);
1104         v = PRVM_G_VECTOR(OFS_PARM2);
1105         n = (int)PRVM_G_FLOAT(OFS_PARM3);
1106         CL_ParticleEffect(i, n, f, f, v, v, NULL, 0);
1107 }
1108
1109 //#338 void(string s) cprint (EXT_CSQC)
1110 void VM_CL_centerprint (void)
1111 {
1112         char s[VM_STRINGTEMP_LENGTH];
1113         if(prog->argc < 1)
1114                 VM_SAFEPARMCOUNT(1, VM_CL_centerprint);
1115         VM_VarString(0, s, sizeof(s));
1116         SCR_CenterPrint(s);
1117 }
1118
1119 //#342 string(float keynum) getkeybind (EXT_CSQC)
1120 void VM_CL_getkeybind (void)
1121 {
1122         int i;
1123
1124         VM_SAFEPARMCOUNT(1, VM_CL_getkeybind);
1125         i = (int)PRVM_G_FLOAT(OFS_PARM0);
1126         PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(Key_GetBind(i));
1127 }
1128
1129 //#343 void(float usecursor) setcursormode (EXT_CSQC)
1130 void VM_CL_setcursormode (void)
1131 {
1132         VM_SAFEPARMCOUNT(1, VM_CL_setcursormode);
1133         cl.csqc_wantsmousemove = PRVM_G_FLOAT(OFS_PARM0);
1134         cl_ignoremousemove = true;
1135 }
1136
1137 //#345 float(float framenum) getinputstate (EXT_CSQC)
1138 void VM_CL_getinputstate (void)
1139 {
1140         int i, frame;
1141         VM_SAFEPARMCOUNT(1, VM_CL_getinputstate);
1142         frame = (int)PRVM_G_FLOAT(OFS_PARM0);
1143         for (i = 0;i < cl.movement_numqueue;i++)
1144                 if (cl.movement_queue[i].sequence == frame)
1145                 {
1146                         VectorCopy(cl.movement_queue[i].viewangles, prog->globals.client->input_angles);
1147                         //prog->globals.client->input_buttons = cl.movement_queue[i].//FIXME
1148                         VectorCopy(cl.movement_queue[i].move, prog->globals.client->input_movevalues);
1149                         prog->globals.client->input_timelength = cl.movement_queue[i].frametime;
1150                         if(cl.movement_queue[i].crouch)
1151                         {
1152                                 VectorCopy(cl.playercrouchmins, prog->globals.client->pmove_mins);
1153                                 VectorCopy(cl.playercrouchmaxs, prog->globals.client->pmove_maxs);
1154                         }
1155                         else
1156                         {
1157                                 VectorCopy(cl.playerstandmins, prog->globals.client->pmove_mins);
1158                                 VectorCopy(cl.playerstandmaxs, prog->globals.client->pmove_maxs);
1159                         }
1160                 }
1161 }
1162
1163 //#346 void(float sens) setsensitivityscaler (EXT_CSQC)
1164 void VM_CL_setsensitivityscale (void)
1165 {
1166         VM_SAFEPARMCOUNT(1, VM_CL_setsensitivityscale);
1167         cl.sensitivityscale = PRVM_G_FLOAT(OFS_PARM0);
1168 }
1169
1170 //#347 void() runstandardplayerphysics (EXT_CSQC)
1171 void VM_CL_runplayerphysics (void)
1172 {
1173 }
1174
1175 //#348 string(float playernum, string keyname) getplayerkeyvalue (EXT_CSQC)
1176 void VM_CL_getplayerkey (void)
1177 {
1178         int                     i;
1179         char            t[128];
1180         const char      *c;
1181         char            *temp;
1182
1183         VM_SAFEPARMCOUNT(2, VM_CL_getplayerkey);
1184
1185         i = (int)PRVM_G_FLOAT(OFS_PARM0);
1186         c = PRVM_G_STRING(OFS_PARM1);
1187         PRVM_G_INT(OFS_RETURN) = OFS_NULL;
1188         Sbar_SortFrags();
1189
1190         i = Sbar_GetPlayer(i);
1191         if(i < 0)
1192                 return;
1193
1194         t[0] = 0;
1195
1196         if(!strcasecmp(c, "name"))
1197                 strcpy(t, cl.scores[i].name);
1198         else
1199                 if(!strcasecmp(c, "frags"))
1200                         sprintf(t, "%i", cl.scores[i].frags);
1201 //      else
1202 //              if(!strcasecmp(c, "ping"))
1203 //                      sprintf(t, "%i", cl.scores[i].ping);
1204 //      else
1205 //              if(!strcasecmp(c, "entertime"))
1206 //                      sprintf(t, "%f", cl.scores[i].entertime);
1207         else
1208                 if(!strcasecmp(c, "colors"))
1209                         sprintf(t, "%i", cl.scores[i].colors);
1210         else
1211                 if(!strcasecmp(c, "topcolor"))
1212                         sprintf(t, "%i", cl.scores[i].colors & 0xf0);
1213         else
1214                 if(!strcasecmp(c, "bottomcolor"))
1215                         sprintf(t, "%i", (cl.scores[i].colors &15)<<4);
1216         else
1217                 if(!strcasecmp(c, "viewentity"))
1218                         sprintf(t, "%i", i+1);
1219         if(!t[0])
1220                 return;
1221         temp = VM_GetTempString();
1222         strcpy(temp, t);
1223         PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(temp);
1224 }
1225
1226 //#349 float() isdemo (EXT_CSQC)
1227 void VM_CL_isdemo (void)
1228 {
1229         PRVM_G_FLOAT(OFS_RETURN) = cls.demoplayback;
1230 }
1231
1232 //#351 void(vector origin, vector forward, vector right, vector up) SetListener (EXT_CSQC)
1233 void VM_CL_setlistener (void)
1234 {
1235         VM_SAFEPARMCOUNT(4, VM_CL_setlistener);
1236         Matrix4x4_FromVectors(&csqc_listenermatrix, PRVM_G_VECTOR(OFS_PARM1), PRVM_G_VECTOR(OFS_PARM2), PRVM_G_VECTOR(OFS_PARM3), PRVM_G_VECTOR(OFS_PARM0));
1237         csqc_usecsqclistener = true;    //use csqc listener at this frame
1238 }
1239
1240 //#352 void(string cmdname) registercommand (EXT_CSQC)
1241 void VM_CL_registercmd (void)
1242 {
1243         char *t;
1244         VM_SAFEPARMCOUNT(1, VM_CL_registercmd);
1245         if(!Cmd_Exists(PRVM_G_STRING(OFS_PARM0)))
1246         {
1247                 t = (char *)Z_Malloc(strlen(PRVM_G_STRING(OFS_PARM0))+1);
1248                 strcpy(t, PRVM_G_STRING(OFS_PARM0));
1249                 Cmd_AddCommand(t, NULL, "console command created by QuakeC");
1250         }
1251         else
1252                 Cmd_AddCommand(PRVM_G_STRING(OFS_PARM0), NULL, "console command created by QuakeC");
1253
1254 }
1255
1256 //#354 float() playernum (EXT_CSQC)
1257 void VM_CL_playernum (void)
1258 {
1259         int i, k;
1260
1261         VM_SAFEPARMCOUNT(0, VM_CL_playernum);
1262
1263         for(i=k=0 ; i<cl.maxclients ; i++)
1264                 if(cl.scores[i].name[0])
1265                         k++;
1266         PRVM_G_FLOAT(OFS_RETURN) = k;
1267 }
1268
1269 //#355 float() cl_onground (EXT_CSQC)
1270 void VM_CL_onground (void)
1271 {
1272         PRVM_G_FLOAT(OFS_RETURN) = csqc_onground;
1273 }
1274
1275 //#360 float() readbyte (EXT_CSQC)
1276 void VM_CL_ReadByte (void)
1277 {
1278         PRVM_G_FLOAT(OFS_RETURN) = MSG_ReadByte();
1279 }
1280
1281 //#361 float() readchar (EXT_CSQC)
1282 void VM_CL_ReadChar (void)
1283 {
1284         PRVM_G_FLOAT(OFS_RETURN) = MSG_ReadChar();
1285 }
1286
1287 //#362 float() readshort (EXT_CSQC)
1288 void VM_CL_ReadShort (void)
1289 {
1290         PRVM_G_FLOAT(OFS_RETURN) = MSG_ReadShort();
1291 }
1292
1293 //#363 float() readlong (EXT_CSQC)
1294 void VM_CL_ReadLong (void)
1295 {
1296         PRVM_G_FLOAT(OFS_RETURN) = MSG_ReadLong();
1297 }
1298
1299 //#364 float() readcoord (EXT_CSQC)
1300 void VM_CL_ReadCoord (void)
1301 {
1302         PRVM_G_FLOAT(OFS_RETURN) = MSG_ReadCoord(cls.protocol);
1303 }
1304
1305 //#365 float() readangle (EXT_CSQC)
1306 void VM_CL_ReadAngle (void)
1307 {
1308         PRVM_G_FLOAT(OFS_RETURN) = MSG_ReadAngle(cls.protocol);
1309 }
1310
1311 //#366 string() readstring (EXT_CSQC)
1312 void VM_CL_ReadString (void)
1313 {
1314         char *t, *s;
1315         t = VM_GetTempString();
1316         s = MSG_ReadString();
1317         PRVM_G_INT(OFS_RETURN) = 0;
1318         if(s)
1319         {
1320                 strcpy(t, s);
1321                 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(t);
1322         }
1323 }
1324
1325 //#367 float() readfloat (EXT_CSQC)
1326 void VM_CL_ReadFloat (void)
1327 {
1328         PRVM_G_FLOAT(OFS_RETURN) = MSG_ReadFloat();
1329 }
1330
1331 //=================================================================//
1332
1333 // #404 void(vector org, string modelname, float startframe, float endframe, float framerate) effect (DP_SV_EFFECT)
1334 void VM_CL_effect (void)
1335 {
1336         VM_SAFEPARMCOUNT(5, VM_CL_effect);
1337         CL_Effect(PRVM_G_VECTOR(OFS_PARM0), (int)PRVM_G_FLOAT(OFS_PARM1), (int)PRVM_G_FLOAT(OFS_PARM2), (int)PRVM_G_FLOAT(OFS_PARM3), PRVM_G_FLOAT(OFS_PARM4));
1338 }
1339
1340 // #405 void(vector org, vector velocity, float howmany) te_blood (DP_TE_BLOOD)
1341 void VM_CL_te_blood (void)
1342 {
1343         float   *pos;
1344         vec3_t  pos2;
1345         VM_SAFEPARMCOUNT(3, VM_CL_te_blood);
1346         if (PRVM_G_FLOAT(OFS_PARM2) < 1)
1347                 return;
1348         pos = PRVM_G_VECTOR(OFS_PARM0);
1349         CL_FindNonSolidLocation(pos, pos2, 4);
1350         CL_ParticleEffect(EFFECT_TE_BLOOD, PRVM_G_FLOAT(OFS_PARM2), pos2, pos2, PRVM_G_VECTOR(OFS_PARM1), PRVM_G_VECTOR(OFS_PARM1), NULL, 0);
1351 }
1352
1353 // #406 void(vector mincorner, vector maxcorner, float explosionspeed, float howmany) te_bloodshower (DP_TE_BLOODSHOWER)
1354 void VM_CL_te_bloodshower (void)
1355 {
1356         vec_t speed;
1357         vec3_t vel1, vel2;
1358         VM_SAFEPARMCOUNT(4, VM_CL_te_bloodshower);
1359         if (PRVM_G_FLOAT(OFS_PARM3) < 1)
1360                 return;
1361         speed = PRVM_G_FLOAT(OFS_PARM2);
1362         vel1[0] = -speed;
1363         vel1[1] = -speed;
1364         vel1[2] = -speed;
1365         vel2[0] = speed;
1366         vel2[1] = speed;
1367         vel2[2] = speed;
1368         CL_ParticleEffect(EFFECT_TE_BLOOD, PRVM_G_FLOAT(OFS_PARM3), PRVM_G_VECTOR(OFS_PARM0), PRVM_G_VECTOR(OFS_PARM1), vel1, vel2, NULL, 0);
1369 }
1370
1371 // #407 void(vector org, vector color) te_explosionrgb (DP_TE_EXPLOSIONRGB)
1372 void VM_CL_te_explosionrgb (void)
1373 {
1374         float           *pos;
1375         vec3_t          pos2;
1376         matrix4x4_t     tempmatrix;
1377         VM_SAFEPARMCOUNT(2, VM_CL_te_explosionrgb);
1378         pos = PRVM_G_VECTOR(OFS_PARM0);
1379         CL_FindNonSolidLocation(pos, pos2, 10);
1380         CL_ParticleExplosion(pos2);
1381         Matrix4x4_CreateTranslate(&tempmatrix, pos2[0], pos2[1], pos2[2]);
1382         CL_AllocDlight(NULL, &tempmatrix, 350, PRVM_G_VECTOR(OFS_PARM1)[0], PRVM_G_VECTOR(OFS_PARM1)[1], PRVM_G_VECTOR(OFS_PARM1)[2], 700, 0.5, 0, -1, true, 1, 0.25, 0.25, 1, 1, LIGHTFLAG_NORMALMODE | LIGHTFLAG_REALTIMEMODE);
1383 }
1384
1385 // #408 void(vector mincorner, vector maxcorner, vector vel, float howmany, float color, float gravityflag, float randomveljitter) te_particlecube (DP_TE_PARTICLECUBE)
1386 void VM_CL_te_particlecube (void)
1387 {
1388         VM_SAFEPARMCOUNT(7, VM_CL_te_particlecube);
1389         CL_ParticleCube(PRVM_G_VECTOR(OFS_PARM0), PRVM_G_VECTOR(OFS_PARM1), PRVM_G_VECTOR(OFS_PARM2), (int)PRVM_G_FLOAT(OFS_PARM3), (int)PRVM_G_FLOAT(OFS_PARM4), PRVM_G_FLOAT(OFS_PARM5), PRVM_G_FLOAT(OFS_PARM6));
1390 }
1391
1392 // #409 void(vector mincorner, vector maxcorner, vector vel, float howmany, float color) te_particlerain (DP_TE_PARTICLERAIN)
1393 void VM_CL_te_particlerain (void)
1394 {
1395         VM_SAFEPARMCOUNT(5, VM_CL_te_particlerain);
1396         CL_ParticleRain(PRVM_G_VECTOR(OFS_PARM0), PRVM_G_VECTOR(OFS_PARM1), PRVM_G_VECTOR(OFS_PARM2), (int)PRVM_G_FLOAT(OFS_PARM3), (int)PRVM_G_FLOAT(OFS_PARM4), 0);
1397 }
1398
1399 // #410 void(vector mincorner, vector maxcorner, vector vel, float howmany, float color) te_particlesnow (DP_TE_PARTICLESNOW)
1400 void VM_CL_te_particlesnow (void)
1401 {
1402         VM_SAFEPARMCOUNT(5, VM_CL_te_particlesnow);
1403         CL_ParticleRain(PRVM_G_VECTOR(OFS_PARM0), PRVM_G_VECTOR(OFS_PARM1), PRVM_G_VECTOR(OFS_PARM2), (int)PRVM_G_FLOAT(OFS_PARM3), (int)PRVM_G_FLOAT(OFS_PARM4), 1);
1404 }
1405
1406 // #411 void(vector org, vector vel, float howmany) te_spark
1407 void VM_CL_te_spark (void)
1408 {
1409         float           *pos;
1410         vec3_t          pos2;
1411         VM_SAFEPARMCOUNT(3, VM_CL_te_spark);
1412
1413         pos = PRVM_G_VECTOR(OFS_PARM0);
1414         CL_FindNonSolidLocation(pos, pos2, 4);
1415         CL_ParticleEffect(EFFECT_TE_SPARK, PRVM_G_FLOAT(OFS_PARM2), pos2, pos2, PRVM_G_VECTOR(OFS_PARM1), PRVM_G_VECTOR(OFS_PARM1), NULL, 0);
1416 }
1417
1418 // #412 void(vector org) te_gunshotquad (DP_QUADEFFECTS1)
1419 void VM_CL_te_gunshotquad (void)
1420 {
1421         float           *pos;
1422         vec3_t          pos2;
1423         VM_SAFEPARMCOUNT(1, VM_CL_te_gunshotquad);
1424
1425         pos = PRVM_G_VECTOR(OFS_PARM0);
1426         CL_FindNonSolidLocation(pos, pos2, 4);
1427         CL_ParticleEffect(EFFECT_TE_GUNSHOTQUAD, 1, pos2, pos2, vec3_origin, vec3_origin, NULL, 0);
1428 }
1429
1430 // #413 void(vector org) te_spikequad (DP_QUADEFFECTS1)
1431 void VM_CL_te_spikequad (void)
1432 {
1433         float           *pos;
1434         vec3_t          pos2;
1435         int                     rnd;
1436         VM_SAFEPARMCOUNT(1, VM_CL_te_spikequad);
1437
1438         pos = PRVM_G_VECTOR(OFS_PARM0);
1439         CL_FindNonSolidLocation(pos, pos2, 4);
1440         CL_ParticleEffect(EFFECT_TE_SPIKEQUAD, 1, pos2, pos2, vec3_origin, vec3_origin, NULL, 0);
1441         if (rand() % 5)                 S_StartSound(-1, 0, cl.sfx_tink1, pos2, 1, 1);
1442         else
1443         {
1444                 rnd = rand() & 3;
1445                 if (rnd == 1)           S_StartSound(-1, 0, cl.sfx_ric1, pos2, 1, 1);
1446                 else if (rnd == 2)      S_StartSound(-1, 0, cl.sfx_ric2, pos2, 1, 1);
1447                 else                            S_StartSound(-1, 0, cl.sfx_ric3, pos2, 1, 1);
1448         }
1449 }
1450
1451 // #414 void(vector org) te_superspikequad (DP_QUADEFFECTS1)
1452 void VM_CL_te_superspikequad (void)
1453 {
1454         float           *pos;
1455         vec3_t          pos2;
1456         int                     rnd;
1457         VM_SAFEPARMCOUNT(1, VM_CL_te_superspikequad);
1458
1459         pos = PRVM_G_VECTOR(OFS_PARM0);
1460         CL_FindNonSolidLocation(pos, pos2, 4);
1461         CL_ParticleEffect(EFFECT_TE_SUPERSPIKEQUAD, 1, pos2, pos2, vec3_origin, vec3_origin, NULL, 0);
1462         if (rand() % 5)                 S_StartSound(-1, 0, cl.sfx_tink1, pos, 1, 1);
1463         else
1464         {
1465                 rnd = rand() & 3;
1466                 if (rnd == 1)           S_StartSound(-1, 0, cl.sfx_ric1, pos2, 1, 1);
1467                 else if (rnd == 2)      S_StartSound(-1, 0, cl.sfx_ric2, pos2, 1, 1);
1468                 else                            S_StartSound(-1, 0, cl.sfx_ric3, pos2, 1, 1);
1469         }
1470 }
1471
1472 // #415 void(vector org) te_explosionquad (DP_QUADEFFECTS1)
1473 void VM_CL_te_explosionquad (void)
1474 {
1475         float           *pos;
1476         vec3_t          pos2;
1477         VM_SAFEPARMCOUNT(1, VM_CL_te_explosionquad);
1478
1479         pos = PRVM_G_VECTOR(OFS_PARM0);
1480         CL_FindNonSolidLocation(pos, pos2, 10);
1481         CL_ParticleEffect(EFFECT_TE_EXPLOSIONQUAD, 1, pos2, pos2, vec3_origin, vec3_origin, NULL, 0);
1482         S_StartSound(-1, 0, cl.sfx_r_exp3, pos2, 1, 1);
1483 }
1484
1485 // #416 void(vector org) te_smallflash (DP_TE_SMALLFLASH)
1486 void VM_CL_te_smallflash (void)
1487 {
1488         float           *pos;
1489         vec3_t          pos2;
1490         VM_SAFEPARMCOUNT(1, VM_CL_te_smallflash);
1491
1492         pos = PRVM_G_VECTOR(OFS_PARM0);
1493         CL_FindNonSolidLocation(pos, pos2, 10);
1494         CL_ParticleEffect(EFFECT_TE_SMALLFLASH, 1, pos2, pos2, vec3_origin, vec3_origin, NULL, 0);
1495 }
1496
1497 // #417 void(vector org, float radius, float lifetime, vector color) te_customflash (DP_TE_CUSTOMFLASH)
1498 void VM_CL_te_customflash (void)
1499 {
1500         float           *pos;
1501         vec3_t          pos2;
1502         matrix4x4_t     tempmatrix;
1503         VM_SAFEPARMCOUNT(4, VM_CL_te_customflash);
1504
1505         pos = PRVM_G_VECTOR(OFS_PARM0);
1506         CL_FindNonSolidLocation(pos, pos2, 4);
1507         Matrix4x4_CreateTranslate(&tempmatrix, pos2[0], pos2[1], pos2[2]);
1508         CL_AllocDlight(NULL, &tempmatrix, PRVM_G_FLOAT(OFS_PARM1), PRVM_G_VECTOR(OFS_PARM3)[0], PRVM_G_VECTOR(OFS_PARM3)[1], PRVM_G_VECTOR(OFS_PARM3)[2], PRVM_G_FLOAT(OFS_PARM1) / PRVM_G_FLOAT(OFS_PARM2), PRVM_G_FLOAT(OFS_PARM2), 0, -1, true, 1, 0.25, 1, 1, 1, LIGHTFLAG_NORMALMODE | LIGHTFLAG_REALTIMEMODE);
1509 }
1510
1511 // #418 void(vector org) te_gunshot (DP_TE_STANDARDEFFECTBUILTINS)
1512 void VM_CL_te_gunshot (void)
1513 {
1514         float           *pos;
1515         vec3_t          pos2;
1516         VM_SAFEPARMCOUNT(1, VM_CL_te_gunshot);
1517
1518         pos = PRVM_G_VECTOR(OFS_PARM0);
1519         CL_FindNonSolidLocation(pos, pos2, 4);
1520         CL_ParticleEffect(EFFECT_TE_GUNSHOT, 1, pos2, pos2, vec3_origin, vec3_origin, NULL, 0);
1521 }
1522
1523 // #419 void(vector org) te_spike (DP_TE_STANDARDEFFECTBUILTINS)
1524 void VM_CL_te_spike (void)
1525 {
1526         float           *pos;
1527         vec3_t          pos2;
1528         int                     rnd;
1529         VM_SAFEPARMCOUNT(1, VM_CL_te_spike);
1530
1531         pos = PRVM_G_VECTOR(OFS_PARM0);
1532         CL_FindNonSolidLocation(pos, pos2, 4);
1533         CL_ParticleEffect(EFFECT_TE_SPIKE, 1, pos2, pos2, vec3_origin, vec3_origin, NULL, 0);
1534         if (rand() % 5)                 S_StartSound(-1, 0, cl.sfx_tink1, pos2, 1, 1);
1535         else
1536         {
1537                 rnd = rand() & 3;
1538                 if (rnd == 1)           S_StartSound(-1, 0, cl.sfx_ric1, pos2, 1, 1);
1539                 else if (rnd == 2)      S_StartSound(-1, 0, cl.sfx_ric2, pos2, 1, 1);
1540                 else                            S_StartSound(-1, 0, cl.sfx_ric3, pos2, 1, 1);
1541         }
1542 }
1543
1544 // #420 void(vector org) te_superspike (DP_TE_STANDARDEFFECTBUILTINS)
1545 void VM_CL_te_superspike (void)
1546 {
1547         float           *pos;
1548         vec3_t          pos2;
1549         int                     rnd;
1550         VM_SAFEPARMCOUNT(1, VM_CL_te_superspike);
1551
1552         pos = PRVM_G_VECTOR(OFS_PARM0);
1553         CL_FindNonSolidLocation(pos, pos2, 4);
1554         CL_ParticleEffect(EFFECT_TE_SUPERSPIKE, 1, pos2, pos2, vec3_origin, vec3_origin, NULL, 0);
1555         if (rand() % 5)                 S_StartSound(-1, 0, cl.sfx_tink1, pos2, 1, 1);
1556         else
1557         {
1558                 rnd = rand() & 3;
1559                 if (rnd == 1)           S_StartSound(-1, 0, cl.sfx_ric1, pos2, 1, 1);
1560                 else if (rnd == 2)      S_StartSound(-1, 0, cl.sfx_ric2, pos2, 1, 1);
1561                 else                            S_StartSound(-1, 0, cl.sfx_ric3, pos2, 1, 1);
1562         }
1563 }
1564
1565 // #421 void(vector org) te_explosion (DP_TE_STANDARDEFFECTBUILTINS)
1566 void VM_CL_te_explosion (void)
1567 {
1568         float           *pos;
1569         vec3_t          pos2;
1570         VM_SAFEPARMCOUNT(1, VM_CL_te_explosion);
1571
1572         pos = PRVM_G_VECTOR(OFS_PARM0);
1573         CL_FindNonSolidLocation(pos, pos2, 10);
1574         CL_ParticleEffect(EFFECT_TE_EXPLOSION, 1, pos2, pos2, vec3_origin, vec3_origin, NULL, 0);
1575         S_StartSound(-1, 0, cl.sfx_r_exp3, pos2, 1, 1);
1576 }
1577
1578 // #422 void(vector org) te_tarexplosion (DP_TE_STANDARDEFFECTBUILTINS)
1579 void VM_CL_te_tarexplosion (void)
1580 {
1581         float           *pos;
1582         vec3_t          pos2;
1583         VM_SAFEPARMCOUNT(1, VM_CL_te_tarexplosion);
1584
1585         pos = PRVM_G_VECTOR(OFS_PARM0);
1586         CL_FindNonSolidLocation(pos, pos2, 10);
1587         CL_ParticleEffect(EFFECT_TE_TAREXPLOSION, 1, pos2, pos2, vec3_origin, vec3_origin, NULL, 0);
1588         S_StartSound(-1, 0, cl.sfx_r_exp3, pos2, 1, 1);
1589 }
1590
1591 // #423 void(vector org) te_wizspike (DP_TE_STANDARDEFFECTBUILTINS)
1592 void VM_CL_te_wizspike (void)
1593 {
1594         float           *pos;
1595         vec3_t          pos2;
1596         VM_SAFEPARMCOUNT(1, VM_CL_te_wizspike);
1597
1598         pos = PRVM_G_VECTOR(OFS_PARM0);
1599         CL_FindNonSolidLocation(pos, pos2, 4);
1600         CL_ParticleEffect(EFFECT_TE_WIZSPIKE, 1, pos2, pos2, vec3_origin, vec3_origin, NULL, 0);
1601         S_StartSound(-1, 0, cl.sfx_wizhit, pos2, 1, 1);
1602 }
1603
1604 // #424 void(vector org) te_knightspike (DP_TE_STANDARDEFFECTBUILTINS)
1605 void VM_CL_te_knightspike (void)
1606 {
1607         float           *pos;
1608         vec3_t          pos2;
1609         VM_SAFEPARMCOUNT(1, VM_CL_te_knightspike);
1610
1611         pos = PRVM_G_VECTOR(OFS_PARM0);
1612         CL_FindNonSolidLocation(pos, pos2, 4);
1613         CL_ParticleEffect(EFFECT_TE_KNIGHTSPIKE, 1, pos2, pos2, vec3_origin, vec3_origin, NULL, 0);
1614         S_StartSound(-1, 0, cl.sfx_knighthit, pos2, 1, 1);
1615 }
1616
1617 // #425 void(vector org) te_lavasplash (DP_TE_STANDARDEFFECTBUILTINS)
1618 void VM_CL_te_lavasplash (void)
1619 {
1620         VM_SAFEPARMCOUNT(1, VM_CL_te_lavasplash);
1621         CL_ParticleEffect(EFFECT_TE_LAVASPLASH, 1, PRVM_G_VECTOR(OFS_PARM0), PRVM_G_VECTOR(OFS_PARM0), vec3_origin, vec3_origin, NULL, 0);
1622 }
1623
1624 // #426 void(vector org) te_teleport (DP_TE_STANDARDEFFECTBUILTINS)
1625 void VM_CL_te_teleport (void)
1626 {
1627         VM_SAFEPARMCOUNT(1, VM_CL_te_teleport);
1628         CL_ParticleEffect(EFFECT_TE_TELEPORT, 1, PRVM_G_VECTOR(OFS_PARM0), PRVM_G_VECTOR(OFS_PARM0), vec3_origin, vec3_origin, NULL, 0);
1629 }
1630
1631 // #427 void(vector org, float colorstart, float colorlength) te_explosion2 (DP_TE_STANDARDEFFECTBUILTINS)
1632 void VM_CL_te_explosion2 (void)
1633 {
1634         float           *pos;
1635         vec3_t          pos2, color;
1636         matrix4x4_t     tempmatrix;
1637         int                     colorStart, colorLength;
1638         unsigned char           *tempcolor;
1639         VM_SAFEPARMCOUNT(3, VM_CL_te_explosion2);
1640
1641         pos = PRVM_G_VECTOR(OFS_PARM0);
1642         colorStart = (int)PRVM_G_FLOAT(OFS_PARM1);
1643         colorLength = (int)PRVM_G_FLOAT(OFS_PARM2);
1644         CL_FindNonSolidLocation(pos, pos2, 10);
1645         CL_ParticleExplosion2(pos2, colorStart, colorLength);
1646         tempcolor = (unsigned char *)&palette_complete[(rand()%colorLength) + colorStart];
1647         color[0] = tempcolor[0] * (2.0f / 255.0f);
1648         color[1] = tempcolor[1] * (2.0f / 255.0f);
1649         color[2] = tempcolor[2] * (2.0f / 255.0f);
1650         Matrix4x4_CreateTranslate(&tempmatrix, pos2[0], pos2[1], pos2[2]);
1651         CL_AllocDlight(NULL, &tempmatrix, 350, color[0], color[1], color[2], 700, 0.5, 0, -1, true, 1, 0.25, 0.25, 1, 1, LIGHTFLAG_NORMALMODE | LIGHTFLAG_REALTIMEMODE);
1652         S_StartSound(-1, 0, cl.sfx_r_exp3, pos2, 1, 1);
1653 }
1654
1655
1656 static void VM_CL_NewBeam (int ent, float *start, float *end, model_t *m, qboolean lightning)
1657 {
1658         beam_t  *b;
1659         int             i;
1660
1661         if (ent >= cl.max_csqcentities)
1662                 CL_ExpandCSQCEntities(ent);
1663
1664         // override any beam with the same entity
1665         for (i = 0, b = cl.beams;i < cl.max_beams;i++, b++)
1666         {
1667                 if (b->entity == ent && ent)
1668                 {
1669                         //b->entity = ent;
1670                         b->lightning = lightning;
1671                         b->relativestartvalid = (ent && cl.csqcentities[ent].state_current.active) ? 2 : 0;
1672                         b->model = m;
1673                         b->endtime = cl.time + 0.2;
1674                         VectorCopy (start, b->start);
1675                         VectorCopy (end, b->end);
1676                         return;
1677                 }
1678         }
1679
1680         // find a free beam
1681         for (i = 0, b = cl.beams;i < cl.max_beams;i++, b++)
1682         {
1683                 if (!b->model || b->endtime < cl.time)
1684                 {
1685                         b->entity = ent;
1686                         b->lightning = lightning;
1687                         b->relativestartvalid = (ent && cl.csqcentities[ent].state_current.active) ? 2 : 0;
1688                         b->model = m;
1689                         b->endtime = cl.time + 0.2;
1690                         VectorCopy (start, b->start);
1691                         VectorCopy (end, b->end);
1692                         return;
1693                 }
1694         }
1695         Con_Print("beam list overflow!\n");
1696 }
1697
1698 // #428 void(entity own, vector start, vector end) te_lightning1 (DP_TE_STANDARDEFFECTBUILTINS)
1699 void VM_CL_te_lightning1 (void)
1700 {
1701         VM_SAFEPARMCOUNT(3, VM_CL_te_lightning1);
1702         VM_CL_NewBeam(PRVM_G_EDICTNUM(OFS_PARM0), PRVM_G_VECTOR(OFS_PARM1), PRVM_G_VECTOR(OFS_PARM2), cl.model_bolt, true);
1703 }
1704
1705 // #429 void(entity own, vector start, vector end) te_lightning2 (DP_TE_STANDARDEFFECTBUILTINS)
1706 void VM_CL_te_lightning2 (void)
1707 {
1708         VM_SAFEPARMCOUNT(3, VM_CL_te_lightning2);
1709         VM_CL_NewBeam(PRVM_G_EDICTNUM(OFS_PARM0), PRVM_G_VECTOR(OFS_PARM1), PRVM_G_VECTOR(OFS_PARM2), cl.model_bolt2, true);
1710 }
1711
1712 // #430 void(entity own, vector start, vector end) te_lightning3 (DP_TE_STANDARDEFFECTBUILTINS)
1713 void VM_CL_te_lightning3 (void)
1714 {
1715         VM_SAFEPARMCOUNT(3, VM_CL_te_lightning3);
1716         VM_CL_NewBeam(PRVM_G_EDICTNUM(OFS_PARM0), PRVM_G_VECTOR(OFS_PARM1), PRVM_G_VECTOR(OFS_PARM2), cl.model_bolt3, false);
1717 }
1718
1719 // #431 void(entity own, vector start, vector end) te_beam (DP_TE_STANDARDEFFECTBUILTINS)
1720 void VM_CL_te_beam (void)
1721 {
1722         VM_SAFEPARMCOUNT(3, VM_CL_te_beam);
1723         VM_CL_NewBeam(PRVM_G_EDICTNUM(OFS_PARM0), PRVM_G_VECTOR(OFS_PARM1), PRVM_G_VECTOR(OFS_PARM2), cl.model_beam, false);
1724 }
1725
1726 // #433 void(vector org) te_plasmaburn (DP_TE_PLASMABURN)
1727 void VM_CL_te_plasmaburn (void)
1728 {
1729         float           *pos;
1730         vec3_t          pos2;
1731         VM_SAFEPARMCOUNT(1, VM_CL_te_plasmaburn);
1732
1733         pos = PRVM_G_VECTOR(OFS_PARM0);
1734         CL_FindNonSolidLocation(pos, pos2, 4);
1735         CL_ParticleEffect(EFFECT_TE_PLASMABURN, 1, pos2, pos2, vec3_origin, vec3_origin, NULL, 0);
1736 }
1737
1738
1739 //====================================================================
1740 //DP_QC_GETSURFACE
1741
1742 extern void clippointtosurface(model_t *model, msurface_t *surface, vec3_t p, vec3_t out);
1743 static model_t *cl_getmodel(prvm_edict_t *ed)
1744 {
1745         int modelindex;
1746         model_t *model = NULL;
1747         if (!ed || ed->priv.server->free)
1748                 return NULL;
1749         modelindex = (int)ed->fields.client->modelindex;
1750         if(!modelindex)
1751                 return NULL;
1752         if(modelindex<0)
1753         {
1754                 modelindex = -(modelindex+1);
1755                 if(modelindex < MAX_MODELS)
1756                         model = cl.csqc_model_precache[modelindex];
1757         }
1758         else
1759         {
1760                 if(modelindex < MAX_MODELS)
1761                         model = cl.model_precache[modelindex];
1762         }
1763         return model;
1764 }
1765
1766 static msurface_t *cl_getsurface(model_t *model, int surfacenum)
1767 {
1768         if (surfacenum < 0 || surfacenum >= model->nummodelsurfaces)
1769                 return NULL;
1770         return model->data_surfaces + surfacenum + model->firstmodelsurface;
1771 }
1772
1773 // #434 float(entity e, float s) getsurfacenumpoints
1774 void VM_CL_getsurfacenumpoints(void)
1775 {
1776         model_t *model = cl_getmodel(PRVM_G_EDICT(OFS_PARM0));
1777         msurface_t *surface;
1778         // return 0 if no such surface
1779         if (!model || !(surface = cl_getsurface(model, (int)PRVM_G_FLOAT(OFS_PARM1))))
1780         {
1781                 PRVM_G_FLOAT(OFS_RETURN) = 0;
1782                 return;
1783         }
1784
1785         // note: this (incorrectly) assumes it is a simple polygon
1786         PRVM_G_FLOAT(OFS_RETURN) = surface->num_vertices;
1787 }
1788
1789 // #435 vector(entity e, float s, float n) getsurfacepoint
1790 void VM_CL_getsurfacepoint(void)
1791 {
1792         prvm_edict_t *ed;
1793         model_t *model;
1794         msurface_t *surface;
1795         int pointnum;
1796         VectorClear(PRVM_G_VECTOR(OFS_RETURN));
1797         ed = PRVM_G_EDICT(OFS_PARM0);
1798         if (!(model = cl_getmodel(ed)) || !(surface = cl_getsurface(model, (int)PRVM_G_FLOAT(OFS_PARM1))))
1799                 return;
1800         // note: this (incorrectly) assumes it is a simple polygon
1801         pointnum = (int)PRVM_G_FLOAT(OFS_PARM2);
1802         if (pointnum < 0 || pointnum >= surface->num_vertices)
1803                 return;
1804         // FIXME: implement rotation/scaling
1805         VectorAdd(&(model->surfmesh.data_vertex3f + 3 * surface->num_firstvertex)[pointnum * 3], ed->fields.client->origin, PRVM_G_VECTOR(OFS_RETURN));
1806 }
1807
1808 // #436 vector(entity e, float s) getsurfacenormal
1809 void VM_CL_getsurfacenormal(void)
1810 {
1811         model_t *model;
1812         msurface_t *surface;
1813         vec3_t normal;
1814         VectorClear(PRVM_G_VECTOR(OFS_RETURN));
1815         if (!(model = cl_getmodel(PRVM_G_EDICT(OFS_PARM0))) || !(surface = cl_getsurface(model, (int)PRVM_G_FLOAT(OFS_PARM1))))
1816                 return;
1817         // FIXME: implement rotation/scaling
1818         // note: this (incorrectly) assumes it is a simple polygon
1819         // note: this only returns the first triangle, so it doesn't work very
1820         // well for curved surfaces or arbitrary meshes
1821         TriangleNormal((model->surfmesh.data_vertex3f + 3 * surface->num_firstvertex), (model->surfmesh.data_vertex3f + 3 * surface->num_firstvertex) + 3, (model->surfmesh.data_vertex3f + 3 * surface->num_firstvertex) + 6, normal);
1822         VectorNormalize(normal);
1823         VectorCopy(normal, PRVM_G_VECTOR(OFS_RETURN));
1824 }
1825
1826 // #437 string(entity e, float s) getsurfacetexture
1827 void VM_CL_getsurfacetexture(void)
1828 {
1829         model_t *model;
1830         msurface_t *surface;
1831         PRVM_G_INT(OFS_RETURN) = 0;
1832         if (!(model = cl_getmodel(PRVM_G_EDICT(OFS_PARM0))) || !(surface = cl_getsurface(model, (int)PRVM_G_FLOAT(OFS_PARM1))))
1833                 return;
1834         PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(surface->texture->name);
1835 }
1836
1837 // #438 float(entity e, vector p) getsurfacenearpoint
1838 void VM_CL_getsurfacenearpoint(void)
1839 {
1840         int surfacenum, best;
1841         vec3_t clipped, p;
1842         vec_t dist, bestdist;
1843         prvm_edict_t *ed;
1844         model_t *model = NULL;
1845         msurface_t *surface;
1846         vec_t *point;
1847         PRVM_G_FLOAT(OFS_RETURN) = -1;
1848         ed = PRVM_G_EDICT(OFS_PARM0);
1849         if(!(model = cl_getmodel(ed)) || !model->num_surfaces)
1850                 return;
1851
1852         // FIXME: implement rotation/scaling
1853         point = PRVM_G_VECTOR(OFS_PARM1);
1854         VectorSubtract(point, ed->fields.client->origin, p);
1855         best = -1;
1856         bestdist = 1000000000;
1857         for (surfacenum = 0;surfacenum < model->nummodelsurfaces;surfacenum++)
1858         {
1859                 surface = model->data_surfaces + surfacenum + model->firstmodelsurface;
1860                 // first see if the nearest point on the surface's box is closer than the previous match
1861                 clipped[0] = bound(surface->mins[0], p[0], surface->maxs[0]) - p[0];
1862                 clipped[1] = bound(surface->mins[1], p[1], surface->maxs[1]) - p[1];
1863                 clipped[2] = bound(surface->mins[2], p[2], surface->maxs[2]) - p[2];
1864                 dist = VectorLength2(clipped);
1865                 if (dist < bestdist)
1866                 {
1867                         // it is, check the nearest point on the actual geometry
1868                         clippointtosurface(model, surface, p, clipped);
1869                         VectorSubtract(clipped, p, clipped);
1870                         dist += VectorLength2(clipped);
1871                         if (dist < bestdist)
1872                         {
1873                                 // that's closer too, store it as the best match
1874                                 best = surfacenum;
1875                                 bestdist = dist;
1876                         }
1877                 }
1878         }
1879         PRVM_G_FLOAT(OFS_RETURN) = best;
1880 }
1881
1882 // #439 vector(entity e, float s, vector p) getsurfaceclippedpoint
1883 void VM_CL_getsurfaceclippedpoint(void)
1884 {
1885         prvm_edict_t *ed;
1886         model_t *model;
1887         msurface_t *surface;
1888         vec3_t p, out;
1889         VectorClear(PRVM_G_VECTOR(OFS_RETURN));
1890         ed = PRVM_G_EDICT(OFS_PARM0);
1891         if (!(model = cl_getmodel(ed)) || !(surface = cl_getsurface(model, (int)PRVM_G_FLOAT(OFS_PARM1))))
1892                 return;
1893         // FIXME: implement rotation/scaling
1894         VectorSubtract(PRVM_G_VECTOR(OFS_PARM2), ed->fields.client->origin, p);
1895         clippointtosurface(model, surface, p, out);
1896         // FIXME: implement rotation/scaling
1897         VectorAdd(out, ed->fields.client->origin, PRVM_G_VECTOR(OFS_RETURN));
1898 }
1899
1900 // #443 void(entity e, entity tagentity, string tagname) setattachment
1901 void VM_CL_setattachment (void)
1902 {
1903         prvm_edict_t *e = PRVM_G_EDICT(OFS_PARM0);
1904         prvm_edict_t *tagentity = PRVM_G_EDICT(OFS_PARM1);
1905         const char *tagname = PRVM_G_STRING(OFS_PARM2);
1906         prvm_eval_t *v;
1907         int modelindex;
1908         model_t *model;
1909
1910         if (e == prog->edicts)
1911                 PF_WARNING("setattachment: can not modify world entity\n");
1912         if (e->priv.server->free)
1913                 PF_WARNING("setattachment: can not modify free entity\n");
1914
1915         if (tagentity == NULL)
1916                 tagentity = prog->edicts;
1917
1918         v = PRVM_GETEDICTFIELDVALUE(e, csqc_fieldoff_tag_entity);
1919         if (v)
1920                 v->edict = PRVM_EDICT_TO_PROG(tagentity);
1921
1922         v = PRVM_GETEDICTFIELDVALUE(e, csqc_fieldoff_tag_index);
1923         if (v)
1924                 v->_float = 0;
1925         if (tagentity != NULL && tagentity != prog->edicts && tagname && tagname[0])
1926         {
1927                 modelindex = (int)tagentity->fields.client->modelindex;
1928                 model = NULL;
1929
1930                 if(modelindex)
1931                 {
1932                         if(modelindex<0)
1933                         {
1934                                 modelindex = -(modelindex+1);
1935                                 if(modelindex < MAX_MODELS)
1936                                         model = cl.csqc_model_precache[modelindex];
1937                         }
1938                         else
1939                                 if(modelindex < MAX_MODELS)
1940                                         model = cl.model_precache[modelindex];
1941                 }
1942
1943                 if (model)
1944                 {
1945                         v->_float = Mod_Alias_GetTagIndexForName(model, (int)tagentity->fields.client->skin, tagname);
1946                         if (v->_float == 0)
1947                                 Con_DPrintf("setattachment(edict %i, edict %i, string \"%s\"): tried to find tag named \"%s\" on entity %i (model \"%s\") but could not find it\n", PRVM_NUM_FOR_EDICT(e), PRVM_NUM_FOR_EDICT(tagentity), tagname, tagname, PRVM_NUM_FOR_EDICT(tagentity), model->name);
1948                 }
1949                 else
1950                         Con_DPrintf("setattachment(edict %i, edict %i, string \"%s\"): tried to find tag named \"%s\" on entity %i but it has no model\n", PRVM_NUM_FOR_EDICT(e), PRVM_NUM_FOR_EDICT(tagentity), tagname, tagname, PRVM_NUM_FOR_EDICT(tagentity));
1951         }
1952 }
1953
1954 /////////////////////////////////////////
1955 // DP_MD3_TAGINFO extension coded by VorteX
1956
1957 int CL_GetTagIndex (prvm_edict_t *e, const char *tagname)
1958 {
1959         int i;
1960         model_t *m;
1961
1962         i = (int)e->fields.client->modelindex;
1963
1964         if(!i)
1965                 return -1;
1966         if(i<0)
1967         {
1968                 i = -(i+1);
1969                 if(i >= MAX_MODELS)
1970                         return -1;
1971                 m = cl.csqc_model_precache[i];
1972         }
1973         else
1974                 if(i >= MAX_MODELS)
1975                         return -1;
1976                 else
1977                         m = cl.model_precache[i];
1978
1979         return Mod_Alias_GetTagIndexForName(m, (int)e->fields.client->skin, tagname);
1980 };
1981
1982 // Warnings/errors code:
1983 // 0 - normal (everything all-right)
1984 // 1 - world entity
1985 // 2 - free entity
1986 // 3 - null or non-precached model
1987 // 4 - no tags with requested index
1988 // 5 - runaway loop at attachment chain
1989 extern cvar_t cl_bob;
1990 extern cvar_t cl_bobcycle;
1991 extern cvar_t cl_bobup;
1992 int CL_GetTagMatrix (matrix4x4_t *out, prvm_edict_t *ent, int tagindex)
1993 {
1994         prvm_eval_t *val;
1995         int modelindex, reqframe, attachloop, i;
1996         matrix4x4_t entitymatrix, tagmatrix, attachmatrix;
1997         prvm_edict_t *attachent;
1998         model_t *model;
1999
2000         *out = identitymatrix; // warnings and errors return identical matrix
2001
2002         if (ent == prog->edicts)
2003                 return 1;
2004         if (ent->priv.server->free)
2005                 return 2;
2006
2007         modelindex = (int)ent->fields.client->modelindex;
2008
2009         if(!modelindex)
2010                 return 3;
2011         if(modelindex<0)
2012         {
2013                 modelindex = -(modelindex+1);
2014                 if(modelindex >= MAX_MODELS)
2015                         return 3;
2016                 model = cl.csqc_model_precache[modelindex];
2017         }
2018         else
2019                 if(modelindex >= MAX_MODELS)
2020                         return 3;
2021                 else
2022                         model = cl.model_precache[modelindex];
2023
2024         if (ent->fields.client->frame >= 0 && ent->fields.client->frame < model->numframes && model->animscenes)
2025                 reqframe = model->animscenes[(int)ent->fields.client->frame].firstframe;
2026         else
2027                 reqframe = 0; // if model has wrong frame, engine automatically switches to model first frame
2028
2029         // get initial tag matrix
2030         if (tagindex)
2031         {
2032                 int ret = Mod_Alias_GetTagMatrix(model, reqframe, tagindex - 1, &tagmatrix);
2033                 if (ret)
2034                         return ret;
2035         }
2036         else
2037                 tagmatrix = identitymatrix;
2038
2039         if ((val = PRVM_GETEDICTFIELDVALUE(ent, csqc_fieldoff_tag_entity)) && val->edict)
2040         { // DP_GFX_QUAKE3MODELTAGS, scan all chain and stop on unattached entity
2041                 attachloop = 0;
2042                 do
2043                 {
2044                         attachent = PRVM_EDICT_NUM(val->edict); // to this it entity our entity is attached
2045                         val = PRVM_GETEDICTFIELDVALUE(ent, csqc_fieldoff_tag_index);
2046
2047                         model = NULL;
2048                         i = (int)attachent->fields.client->modelindex;
2049                         if(i<0)
2050                         {
2051                                 i = -(i+1);
2052                                 if(i < MAX_MODELS)
2053                                         model = cl.csqc_model_precache[i];
2054                         }
2055                         else
2056                                 if(i < MAX_MODELS)
2057                                         model = cl.model_precache[i];
2058
2059                         if (model && val->_float >= 1 && model->animscenes && attachent->fields.client->frame >= 0 && attachent->fields.client->frame < model->numframes)
2060                                 Mod_Alias_GetTagMatrix(model, model->animscenes[(int)attachent->fields.client->frame].firstframe, (int)val->_float - 1, &attachmatrix);
2061                         else
2062                                 attachmatrix = identitymatrix;
2063
2064                         // apply transformation by child entity matrix
2065                         val = PRVM_GETEDICTFIELDVALUE(ent, csqc_fieldoff_scale);
2066                         if (val->_float == 0)
2067                                 val->_float = 1;
2068                         Matrix4x4_CreateFromQuakeEntity(&entitymatrix, ent->fields.client->origin[0], ent->fields.client->origin[1], ent->fields.client->origin[2], -ent->fields.client->angles[0], ent->fields.client->angles[1], ent->fields.client->angles[2], val->_float);
2069                         Matrix4x4_Concat(out, &entitymatrix, &tagmatrix);
2070                         out->m[0][3] = entitymatrix.m[0][3] + val->_float*(entitymatrix.m[0][0]*tagmatrix.m[0][3] + entitymatrix.m[0][1]*tagmatrix.m[1][3] + entitymatrix.m[0][2]*tagmatrix.m[2][3]);
2071                         out->m[1][3] = entitymatrix.m[1][3] + val->_float*(entitymatrix.m[1][0]*tagmatrix.m[0][3] + entitymatrix.m[1][1]*tagmatrix.m[1][3] + entitymatrix.m[1][2]*tagmatrix.m[2][3]);
2072                         out->m[2][3] = entitymatrix.m[2][3] + val->_float*(entitymatrix.m[2][0]*tagmatrix.m[0][3] + entitymatrix.m[2][1]*tagmatrix.m[1][3] + entitymatrix.m[2][2]*tagmatrix.m[2][3]);
2073                         Matrix4x4_Copy(&tagmatrix, out);
2074
2075                         // finally transformate by matrix of tag on parent entity
2076                         Matrix4x4_Concat(out, &attachmatrix, &tagmatrix);
2077                         out->m[0][3] = attachmatrix.m[0][3] + attachmatrix.m[0][0]*tagmatrix.m[0][3] + attachmatrix.m[0][1]*tagmatrix.m[1][3] + attachmatrix.m[0][2]*tagmatrix.m[2][3];
2078                         out->m[1][3] = attachmatrix.m[1][3] + attachmatrix.m[1][0]*tagmatrix.m[0][3] + attachmatrix.m[1][1]*tagmatrix.m[1][3] + attachmatrix.m[1][2]*tagmatrix.m[2][3];
2079                         out->m[2][3] = attachmatrix.m[2][3] + attachmatrix.m[2][0]*tagmatrix.m[0][3] + attachmatrix.m[2][1]*tagmatrix.m[1][3] + attachmatrix.m[2][2]*tagmatrix.m[2][3];
2080                         Matrix4x4_Copy(&tagmatrix, out);
2081
2082                         ent = attachent;
2083                         attachloop += 1;
2084                         if (attachloop > 255) // prevent runaway looping
2085                                 return 5;
2086                 }
2087                 while ((val = PRVM_GETEDICTFIELDVALUE(ent, csqc_fieldoff_tag_entity)) && val->edict);
2088         }
2089
2090         // normal or RENDER_VIEWMODEL entity (or main parent entity on attach chain)
2091         val = PRVM_GETEDICTFIELDVALUE(ent, csqc_fieldoff_scale);
2092         if (val->_float == 0)
2093                 val->_float = 1;
2094         // Alias models have inverse pitch, bmodels can't have tags, so don't check for modeltype...
2095         Matrix4x4_CreateFromQuakeEntity(&entitymatrix, ent->fields.client->origin[0], ent->fields.client->origin[1], ent->fields.client->origin[2], -ent->fields.client->angles[0], ent->fields.client->angles[1], ent->fields.client->angles[2], val->_float);
2096         Matrix4x4_Concat(out, &entitymatrix, &tagmatrix);
2097         out->m[0][3] = entitymatrix.m[0][3] + val->_float*(entitymatrix.m[0][0]*tagmatrix.m[0][3] + entitymatrix.m[0][1]*tagmatrix.m[1][3] + entitymatrix.m[0][2]*tagmatrix.m[2][3]);
2098         out->m[1][3] = entitymatrix.m[1][3] + val->_float*(entitymatrix.m[1][0]*tagmatrix.m[0][3] + entitymatrix.m[1][1]*tagmatrix.m[1][3] + entitymatrix.m[1][2]*tagmatrix.m[2][3]);
2099         out->m[2][3] = entitymatrix.m[2][3] + val->_float*(entitymatrix.m[2][0]*tagmatrix.m[0][3] + entitymatrix.m[2][1]*tagmatrix.m[1][3] + entitymatrix.m[2][2]*tagmatrix.m[2][3]);
2100
2101         if ((val = PRVM_GETEDICTFIELDVALUE(ent, csqc_fieldoff_renderflags)) && (RF_VIEWMODEL & (int)val->_float))
2102         {// RENDER_VIEWMODEL magic
2103                 Matrix4x4_Copy(&tagmatrix, out);
2104
2105                 val = PRVM_GETEDICTFIELDVALUE(ent, csqc_fieldoff_scale);
2106                 if (val->_float == 0)
2107                         val->_float = 1;
2108
2109                 Matrix4x4_CreateFromQuakeEntity(&entitymatrix, csqc_origin[0], csqc_origin[1], csqc_origin[2], csqc_angles[0], csqc_angles[1], csqc_angles[2], val->_float);
2110                 Matrix4x4_Concat(out, &entitymatrix, &tagmatrix);
2111                 out->m[0][3] = entitymatrix.m[0][3] + val->_float*(entitymatrix.m[0][0]*tagmatrix.m[0][3] + entitymatrix.m[0][1]*tagmatrix.m[1][3] + entitymatrix.m[0][2]*tagmatrix.m[2][3]);
2112                 out->m[1][3] = entitymatrix.m[1][3] + val->_float*(entitymatrix.m[1][0]*tagmatrix.m[0][3] + entitymatrix.m[1][1]*tagmatrix.m[1][3] + entitymatrix.m[1][2]*tagmatrix.m[2][3]);
2113                 out->m[2][3] = entitymatrix.m[2][3] + val->_float*(entitymatrix.m[2][0]*tagmatrix.m[0][3] + entitymatrix.m[2][1]*tagmatrix.m[1][3] + entitymatrix.m[2][2]*tagmatrix.m[2][3]);
2114
2115                 /*
2116                 // Cl_bob, ported from rendering code
2117                 if (ent->fields.client->health > 0 && cl_bob.value && cl_bobcycle.value)
2118                 {
2119                         double bob, cycle;
2120                         // LordHavoc: this code is *weird*, but not replacable (I think it
2121                         // should be done in QC on the server, but oh well, quake is quake)
2122                         // LordHavoc: figured out bobup: the time at which the sin is at 180
2123                         // degrees (which allows lengthening or squishing the peak or valley)
2124                         cycle = sv.time/cl_bobcycle.value;
2125                         cycle -= (int)cycle;
2126                         if (cycle < cl_bobup.value)
2127                                 cycle = sin(M_PI * cycle / cl_bobup.value);
2128                         else
2129                                 cycle = sin(M_PI + M_PI * (cycle-cl_bobup.value)/(1.0 - cl_bobup.value));
2130                         // bob is proportional to velocity in the xy plane
2131                         // (don't count Z, or jumping messes it up)
2132                         bob = sqrt(ent->fields.client->velocity[0]*ent->fields.client->velocity[0] + ent->fields.client->velocity[1]*ent->fields.client->velocity[1])*cl_bob.value;
2133                         bob = bob*0.3 + bob*0.7*cycle;
2134                         out->m[2][3] += bound(-7, bob, 4);
2135                 }
2136                 */
2137         }
2138         return 0;
2139 }
2140
2141 // #451 float(entity ent, string tagname) gettagindex (DP_QC_GETTAGINFO)
2142 void VM_CL_gettagindex (void)
2143 {
2144         prvm_edict_t *ent = PRVM_G_EDICT(OFS_PARM0);
2145         const char *tag_name = PRVM_G_STRING(OFS_PARM1);
2146         int modelindex, tag_index;
2147
2148         if (ent == prog->edicts)
2149                 PF_WARNING("gettagindex: can't affect world entity\n");
2150         if (ent->priv.server->free)
2151                 PF_WARNING("gettagindex: can't affect free entity\n");
2152
2153         modelindex = (int)ent->fields.client->modelindex;
2154         if(modelindex < 0)
2155                 modelindex = -(modelindex+1);
2156         tag_index = 0;
2157         if (modelindex <= 0 || modelindex >= MAX_MODELS)
2158                 Con_DPrintf("gettagindex(entity #%i): null or non-precached model\n", PRVM_NUM_FOR_EDICT(ent));
2159         else
2160         {
2161                 tag_index = CL_GetTagIndex(ent, tag_name);
2162                 if (tag_index == 0)
2163                         Con_DPrintf("gettagindex(entity #%i): tag \"%s\" not found\n", PRVM_NUM_FOR_EDICT(ent), tag_name);
2164         }
2165         PRVM_G_FLOAT(OFS_RETURN) = tag_index;
2166 }
2167
2168 // #452 vector(entity ent, float tagindex) gettaginfo (DP_QC_GETTAGINFO)
2169 void VM_CL_gettaginfo (void)
2170 {
2171         prvm_edict_t *e = PRVM_G_EDICT(OFS_PARM0);
2172         int tagindex = (int)PRVM_G_FLOAT(OFS_PARM1);
2173         matrix4x4_t tag_matrix;
2174         int returncode;
2175
2176         returncode = CL_GetTagMatrix(&tag_matrix, e, tagindex);
2177         Matrix4x4_ToVectors(&tag_matrix, prog->globals.client->v_forward, prog->globals.client->v_right, prog->globals.client->v_up, PRVM_G_VECTOR(OFS_RETURN));
2178
2179         switch(returncode)
2180         {
2181                 case 1:
2182                         PF_WARNING("gettagindex: can't affect world entity\n");
2183                         break;
2184                 case 2:
2185                         PF_WARNING("gettagindex: can't affect free entity\n");
2186                         break;
2187                 case 3:
2188                         Con_DPrintf("CL_GetTagMatrix(entity #%i): null or non-precached model\n", PRVM_NUM_FOR_EDICT(e));
2189                         break;
2190                 case 4:
2191                         Con_DPrintf("CL_GetTagMatrix(entity #%i): model has no tag with requested index %i\n", PRVM_NUM_FOR_EDICT(e), tagindex);
2192                         break;
2193                 case 5:
2194                         Con_DPrintf("CL_GetTagMatrix(entity #%i): runaway loop at attachment chain\n", PRVM_NUM_FOR_EDICT(e));
2195                         break;
2196         }
2197 }
2198
2199 //=================================================
2200 //[515]: here goes test/unfinished/etc.
2201
2202 //[515]: check if it is what it should be
2203 void VM_WasFreed (void)
2204 {
2205         prvm_edict_t    *e;
2206         VM_SAFEPARMCOUNT(1, VM_WasFreed);
2207
2208         e = PRVM_G_EDICT(OFS_PARM0);
2209         if (!e->priv.required->free || (e->priv.required->free && (e->priv.required->freetime < 2 || (*prog->time - e->priv.required->freetime) > 0.5 )))
2210                 PRVM_G_FLOAT(OFS_RETURN) = false;
2211         else
2212                 PRVM_G_FLOAT(OFS_RETURN) = true;
2213 }
2214
2215 void VM_CL_select_cube (void)
2216 {
2217         int             i;
2218         int             chain_of;
2219         float   *mins2, *maxs2;
2220         prvm_edict_t    *ent, *chain;
2221         vec3_t  mins1, maxs1;
2222
2223         VM_SAFEPARMCOUNT(2, VM_CL_select_cube);
2224
2225         // is the same like !(prog->flag & PRVM_FE_CHAIN) - even if the operator precedence is another
2226         if(!prog->flag & PRVM_FE_CHAIN)
2227                 PRVM_ERROR("VM_findchain: %s doesnt have a chain field !\n", PRVM_NAME);
2228
2229         chain_of = PRVM_ED_FindField("chain")->ofs;
2230         chain = prog->edicts;
2231
2232         mins2 = PRVM_G_VECTOR(OFS_PARM0);
2233         maxs2 = PRVM_G_VECTOR(OFS_PARM1);
2234
2235         ent = PRVM_NEXT_EDICT(prog->edicts);
2236         for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
2237         {
2238                 if (ent->priv.required->free)
2239                         continue;
2240                 VectorCopy(ent->fields.client->origin, mins1);
2241                 VectorAdd(mins1, ent->fields.client->maxs, maxs1);
2242                 VectorAdd(mins1, ent->fields.client->mins, mins1);
2243                 if (mins1[0] > maxs2[0] || mins1[1] > maxs2[1] || mins1[2] > maxs2[2])
2244                         continue;
2245                 if (maxs1[0] < mins2[0] || maxs1[1] < mins2[1] || maxs1[2] < mins2[2])
2246                         continue;
2247                 PRVM_E_INT(ent,chain_of) = PRVM_NUM_FOR_EDICT(chain);
2248                 chain = ent;
2249         }
2250
2251         VM_RETURN_EDICT(chain);
2252 }
2253
2254 void VM_CL_select_super (void)
2255 {
2256 /*      int             i;
2257         int             chain_of;
2258         float   *v[8];
2259         prvm_edict_t    *ent, *chain;
2260         vec3_t  mins1, maxs1;
2261
2262         VM_SAFEPARMCOUNT(8, VM_findchain);
2263         for(i=0;i<8;i++)
2264                 v[i] = PRVM_G_VECTOR(OFS_PARM0+i*3);
2265
2266         // is the same like !(prog->flag & PRVM_FE_CHAIN) - even if the operator precedence is another
2267         if(!prog->flag & PRVM_FE_CHAIN)
2268                 PRVM_ERROR("VM_findchain: %s doesnt have a chain field !\n", PRVM_NAME);
2269
2270         chain_of = PRVM_ED_FindField("chain")->ofs;
2271         chain = prog->edicts;
2272
2273         mins2 = PRVM_G_VECTOR(OFS_PARM0);
2274         maxs2 = PRVM_G_VECTOR(OFS_PARM1);
2275
2276         ent = PRVM_NEXT_EDICT(prog->edicts);
2277         for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
2278         {
2279                 if (ent->priv.required->free)
2280                         continue;
2281                 VectorCopy(ent->fields.client->origin, mins1);
2282                 VectorAdd(mins1, ent->fields.client->maxs, maxs1);
2283                 VectorAdd(mins1, ent->fields.client->mins, mins1);
2284                 if (mins1[0] > maxs2[0] || mins1[1] > maxs2[1] || mins1[2] > maxs2[2])
2285                         continue;
2286                 if (maxs1[0] < mins2[0] || maxs1[1] < mins2[1] || maxs1[2] < mins2[2])
2287                         continue;
2288                 PRVM_E_INT(ent,chain_of) = PRVM_NUM_FOR_EDICT(chain);
2289                 chain = ent;
2290         }
2291
2292         VM_RETURN_EDICT(chain);*/
2293 }
2294
2295 static int Is_Text_Color (char c, char t)
2296 {
2297         int a = 0;
2298         char c2 = c - (c & 128);
2299         char t2 = t - (t & 128);
2300
2301         if(c != '^' && c2 != '^')               return 0;
2302         if(t >= '0' && t <= '9')                a = 1;
2303         if(t2 >= '0' && t2 <= '9')              a = 1;
2304 /*      if(t >= 'A' && t <= 'Z')                a = 2;
2305         if(t2 >= 'A' && t2 <= 'Z')              a = 2;
2306
2307         if(a == 1 && scr_colortext.integer > 0)
2308                 return 1;
2309         if(a == 2 && scr_multifonts.integer > 0)
2310                 return 2;
2311 */
2312         return a;
2313 }
2314
2315 void VM_uncolorstring (void) //#170
2316 {
2317         const char      *in;
2318         char            *out;
2319         int                     k = 0, i = 0;
2320
2321         VM_SAFEPARMCOUNT(1, VM_uncolorstring);
2322         in = PRVM_G_STRING(OFS_PARM0);
2323         if(!in)
2324                 PRVM_ERROR ("VM_uncolorstring: %s: NULL\n", PRVM_NAME);
2325         VM_CheckEmptyString (in);
2326         out = VM_GetTempString();
2327
2328         while (in[k])
2329         {
2330                 if(in[k+1])
2331                 if(Is_Text_Color(in[k], in[k+1]) == 1/* || (in[k] == '&' && in[k+1] == 'r')*/)
2332                 {
2333                         k += 2;
2334                         continue;
2335                 }
2336                 out[i] = in[k];
2337                 ++k;
2338                 ++i;
2339         }
2340 }
2341
2342 void VM_CL_selecttraceline (void)
2343 {
2344         float   *v1, *v2;
2345         int             ent, ignore, csqcents;
2346
2347         v1 = PRVM_G_VECTOR(OFS_PARM0);
2348         v2 = PRVM_G_VECTOR(OFS_PARM1);
2349         ignore = (int)PRVM_G_FLOAT(OFS_PARM2);
2350         csqcents = (int)PRVM_G_FLOAT(OFS_PARM3);
2351         ent = 0;
2352
2353         if((csqcents && ignore > cl.num_csqcentities) || (!csqcents && ignore > cl.num_entities))
2354         {
2355                 Con_Printf("VM_CL_selecttraceline: out of entities\n");
2356                 return;
2357         }
2358         else
2359                 if(csqcents)
2360                         prog->globals.client->trace_fraction = CL_SelectTraceLine(v1, v2, prog->globals.client->trace_endpos, prog->globals.client->trace_plane_normal, &prog->globals.client->trace_ent, &cl.csqcentities[ignore].render, csqcents);
2361                 else
2362                         prog->globals.client->trace_fraction = CL_SelectTraceLine(v1, v2, prog->globals.client->trace_endpos, prog->globals.client->trace_plane_normal, &ent, &cl.entities[ignore].render, csqcents);
2363         PRVM_G_FLOAT(OFS_RETURN) = ent;
2364 }
2365
2366 void VM_charindex (void)
2367 {
2368         const char *s;
2369         s = PRVM_G_STRING(OFS_PARM0);
2370         if(!s)
2371                 return;
2372         if((unsigned)PRVM_G_FLOAT(OFS_PARM1) > strlen(s))
2373                 return;
2374         PRVM_G_FLOAT(OFS_RETURN) = (unsigned char)s[(int)PRVM_G_FLOAT(OFS_PARM1)];
2375 }
2376
2377 //#223 string(float c, ...) chr2str (FTE_STRINGS)
2378 void VM_chr2str (void)
2379 {
2380         char    *t;
2381         int             i;
2382         t = VM_GetTempString();
2383         for(i=0;i<prog->argc;i++)
2384                 t[i] = (unsigned char)PRVM_G_FLOAT(OFS_PARM0+i*3);
2385         t[i] = 0;
2386         PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(t);
2387 }
2388
2389 //#228 float(string s1, string s2, float len) strncmp (FTE_STRINGS)
2390 void VM_strncmp (void)
2391 {
2392         const char *s1, *s2;
2393         VM_SAFEPARMCOUNT(1, VM_strncmp);
2394         s1 = PRVM_G_STRING(OFS_PARM0);
2395         s2 = PRVM_G_STRING(OFS_PARM1);
2396         PRVM_G_FLOAT(OFS_RETURN) = strncmp(s1, s2, (size_t)PRVM_G_FLOAT(OFS_PARM2));
2397 }
2398
2399 //============================================================================
2400 //============================================================================
2401
2402 prvm_builtin_t vm_cl_builtins[] = {
2403 0,  // to be consistent with the old vm
2404 VM_CL_makevectors,                      // #1 void(vector ang) makevectors
2405 VM_CL_setorigin,                        // #2 void(entity e, vector o) setorigin
2406 VM_CL_setmodel,                         // #3 void(entity e, string m) setmodel
2407 VM_CL_setsize,                          // #4 void(entity e, vector min, vector max) setsize
2408 0,
2409 VM_break,                                       // #6 void() break
2410 VM_random,                                      // #7 float() random
2411 VM_CL_sound,                            // #8 void(entity e, float chan, string samp) sound
2412 VM_normalize,                           // #9 vector(vector v) normalize
2413 VM_error,                                       // #10 void(string e) error
2414 VM_objerror,                            // #11 void(string e) objerror
2415 VM_vlen,                                        // #12 float(vector v) vlen
2416 VM_vectoyaw,                            // #13 float(vector v) vectoyaw
2417 VM_CL_spawn,                            // #14 entity() spawn
2418 VM_remove,                                      // #15 void(entity e) remove
2419 VM_CL_traceline,                        // #16 float(vector v1, vector v2, float tryents) traceline
2420 0,
2421 VM_find,                                        // #18 entity(entity start, .string fld, string match) find
2422 VM_CL_precache_sound,           // #19 void(string s) precache_sound
2423 VM_CL_precache_model,           // #20 void(string s) precache_model
2424 0,
2425 VM_CL_findradius,                       // #22 entity(vector org, float rad) findradius
2426 0,
2427 0,
2428 VM_dprint,                                      // #25 void(string s) dprint
2429 VM_ftos,                                        // #26 void(string s) ftos
2430 VM_vtos,                                        // #27 void(string s) vtos
2431 VM_coredump,                            // #28 void() coredump
2432 VM_traceon,                                     // #29 void() traceon
2433 VM_traceoff,                            // #30 void() traceoff
2434 VM_eprint,                                      // #31 void(entity e) eprint
2435 0,
2436 NULL,                                           // #33
2437 VM_CL_droptofloor,                      // #34 float() droptofloor
2438 VM_CL_lightstyle,                       // #35 void(float style, string value) lightstyle
2439 VM_rint,                                        // #36 float(float v) rint
2440 VM_floor,                                       // #37 float(float v) floor
2441 VM_ceil,                                        // #38 float(float v) ceil
2442 NULL,                                           // #39
2443 VM_CL_checkbottom,                      // #40 float(entity e) checkbottom
2444 VM_CL_pointcontents,            // #41 float(vector v) pointcontents
2445 NULL,                                           // #42
2446 VM_fabs,                                        // #43 float(float f) fabs
2447 0,
2448 VM_cvar,                                        // #45 float(string s) cvar
2449 VM_localcmd,                            // #46 void(string s) localcmd
2450 VM_nextent,                                     // #47 entity(entity e) nextent
2451 VM_CL_particle,                         // #48 void(vector o, vector d, float color, float count) particle
2452 VM_CL_changeyaw,                        // #49 void(entity ent, float ideal_yaw, float speed_yaw) ChangeYaw
2453 NULL,                                           // #50
2454 VM_vectoangles,                         // #51 vector(vector v) vectoangles
2455 0,                      // #52 void(float to, float f) WriteByte
2456 0,                      // #53 void(float to, float f) WriteChar
2457 0,                      // #54 void(float to, float f) WriteShort
2458 0,                      // #55 void(float to, float f) WriteLong
2459 0,                      // #56 void(float to, float f) WriteCoord
2460 0,                      // #57 void(float to, float f) WriteAngle
2461 0,                      // #58 void(float to, string s) WriteString
2462 0,
2463 VM_sin,                                         // #60 float(float f) sin (DP_QC_SINCOSSQRTPOW)
2464 VM_cos,                                         // #61 float(float f) cos (DP_QC_SINCOSSQRTPOW)
2465 VM_sqrt,                                        // #62 float(float f) sqrt (DP_QC_SINCOSSQRTPOW)
2466 VM_CL_changepitch,                      // #63 void(entity ent, float ideal_pitch, float speed_pitch) changepitch (DP_QC_CHANGEPITCH)
2467 VM_CL_tracetoss,                        // #64 void(entity e, entity ignore) tracetoss (DP_QC_TRACETOSS)
2468 VM_etos,                                        // #65 string(entity ent) etos (DP_QC_ETOS)
2469 NULL,                                           // #66
2470 0,                                                              // #67
2471 0,                                                              // #68
2472 0,                                                              // #69
2473 0,                                                              // #70
2474 NULL,                                           // #71
2475 VM_cvar_set,                            // #72 void(string var, string val) cvar_set
2476 0,                                                              // #73
2477 VM_CL_ambientsound,                     // #74 void(vector pos, string samp, float vol, float atten) ambientsound
2478 VM_CL_precache_model,           // #75 string(string s) precache_model2
2479 VM_CL_precache_sound,           // #76 string(string s) precache_sound2
2480 0,                                                              // #77
2481 VM_chr,                                         // #78
2482 NULL,                                           // #79
2483 NULL,                                           // #80
2484 VM_stof,                                        // #81 float(string s) stof (FRIK_FILE)
2485 NULL,                                           // #82
2486 NULL,                                           // #83
2487 NULL,                                           // #84
2488 NULL,                                           // #85
2489 NULL,                                           // #86
2490 NULL,                                           // #87
2491 NULL,                                           // #88
2492 NULL,                                           // #89
2493 VM_CL_tracebox,                         // #90 void(vector v1, vector min, vector max, vector v2, float nomonsters, entity forent) tracebox (DP_QC_TRACEBOX)
2494 VM_randomvec,                           // #91 vector() randomvec (DP_QC_RANDOMVEC)
2495 VM_CL_getlight,                         // #92 vector(vector org) getlight (DP_QC_GETLIGHT)
2496 PF_registercvar,                        // #93 float(string name, string value) registercvar (DP_REGISTERCVAR)
2497 VM_min,                                         // #94 float(float a, floats) min (DP_QC_MINMAXBOUND)
2498 VM_max,                                         // #95 float(float a, floats) max (DP_QC_MINMAXBOUND)
2499 VM_bound,                                       // #96 float(float minimum, float val, float maximum) bound (DP_QC_MINMAXBOUND)
2500 VM_pow,                                         // #97 float(float f, float f) pow (DP_QC_SINCOSSQRTPOW)
2501 VM_findfloat,                           // #98 entity(entity start, .float fld, float match) findfloat (DP_QC_FINDFLOAT)
2502 VM_checkextension,                      // #99 float(string s) checkextension (the basis of the extension system)
2503 NULL,                                           // #100
2504 NULL,                                           // #101
2505 NULL,                                           // #102
2506 NULL,                                           // #103
2507 NULL,                                           // #104
2508 NULL,                                           // #105
2509 NULL,                                           // #106
2510 NULL,                                           // #107
2511 NULL,                                           // #108
2512 NULL,                                           // #109
2513 VM_fopen,                                       // #110 float(string filename, float mode) fopen (FRIK_FILE)
2514 VM_fclose,                                      // #111 void(float fhandle) fclose (FRIK_FILE)
2515 VM_fgets,                                       // #112 string(float fhandle) fgets (FRIK_FILE)
2516 VM_fputs,                                       // #113 void(float fhandle, string s) fputs (FRIK_FILE)
2517 VM_strlen,                                      // #114 float(string s) strlen (FRIK_FILE)
2518 VM_strcat,                                      // #115 string(string s1, string s2) strcat (FRIK_FILE)
2519 VM_substring,                           // #116 string(string s, float start, float length) substring (FRIK_FILE)
2520 VM_stov,                                        // #117 vector(string) stov (FRIK_FILE)
2521 VM_strzone,                                     // #118 string(string s) strzone (FRIK_FILE)
2522 VM_strunzone,                           // #119 void(string s) strunzone (FRIK_FILE)
2523
2524 e10, e10, e10, e10, e10, e10, e10, e10,         // #120-199
2525 e10,    //#200-209
2526 0,      //#210
2527 0,      //#211
2528 0,      //#212
2529 0,      //#213
2530 0,      //#214
2531 0,      //#215
2532 0,      //#216
2533 0,      //#217
2534 VM_bitshift,                            //#218 float(float number, float quantity) bitshift (EXT_BITSHIFT)
2535 0,      //#219
2536 0,      //#220
2537 0,      //#221
2538 VM_charindex,                           //#222 float(string str, float ofs) str2chr (FTE_STRINGS)
2539 VM_chr2str,                                     //#223 string(float c, ...) chr2str (FTE_STRINGS)
2540 0,      //#224
2541 0,      //#225
2542 0,      //#226
2543 0,      //#227
2544 VM_strncmp,                                     //#228 float(string s1, string s2, float len) strncmp (FTE_STRINGS)
2545 0,
2546 e10, e10, e10, e10, e10, e10, e10,      // #230-299
2547
2548 //======CSQC start=======//
2549 //3d world (buffer/buffering) operations
2550 VM_R_ClearScene,                        //#300 void() clearscene (EXT_CSQC)
2551 VM_R_AddEntities,                       //#301 void(float mask) addentities (EXT_CSQC)
2552 VM_R_AddEntity,                         //#302 void(entity ent) addentity (EXT_CSQC)
2553 VM_R_SetView,                           //#303 float(float property, ...) setproperty (EXT_CSQC)
2554 VM_R_RenderScene,                       //#304 void() renderscene (EXT_CSQC)
2555 VM_R_AddDynamicLight,           //#305 void(vector org, float radius, vector lightcolours) adddynamiclight (EXT_CSQC)
2556 VM_R_PolygonBegin,                      //#306 void(string texturename, float flag[, float is2d, float lines]) R_BeginPolygon
2557 VM_R_PolygonVertex,                     //#307 void(vector org, vector texcoords, vector rgb, float alpha) R_PolygonVertex
2558 VM_R_PolygonEnd,                        //#308 void() R_EndPolygon
2559 0,                      //#309
2560
2561 //maths stuff that uses the current view settings
2562 VM_CL_unproject,                        //#310 vector (vector v) cs_unproject (EXT_CSQC)
2563 VM_CL_project,                          //#311 vector (vector v) cs_project (EXT_CSQC)
2564 0,                      //#312
2565 0,                      //#313
2566 0,                      //#314
2567
2568 //2d (immediate) operations
2569 VM_drawline,                            //#315 void(float width, vector pos1, vector pos2, float flag) drawline (EXT_CSQC)
2570 VM_iscachedpic,                         //#316 float(string name) iscachedpic (EXT_CSQC)
2571 VM_precache_pic,                        //#317 string(string name, float trywad) precache_pic (EXT_CSQC)
2572 VM_getimagesize,                        //#318 vector(string picname) draw_getimagesize (EXT_CSQC)
2573 VM_freepic,                                     //#319 void(string name) freepic (EXT_CSQC)
2574 VM_drawcharacter,                       //#320 float(vector position, float character, vector scale, vector rgb, float alpha, float flag) drawcharacter (EXT_CSQC)
2575 VM_drawstring,                          //#321 float(vector position, string text, vector scale, vector rgb, float alpha, float flag) drawstring (EXT_CSQC)
2576 VM_drawpic,                                     //#322 float(vector position, string pic, vector size, vector rgb, float alpha, float flag) drawpic (EXT_CSQC)
2577 VM_drawfill,                            //#323 float(vector position, vector size, vector rgb, float alpha, float flag) drawfill (EXT_CSQC)
2578 VM_drawsetcliparea,                     //#324 void(float x, float y, float width, float height) drawsetcliparea
2579 VM_drawresetcliparea,           //#325 void(void) drawresetcliparea
2580 0,                      //#326
2581 0,                      //#327
2582 0,                      //#328
2583 0,                      //#329
2584
2585 VM_CL_getstatf,                         //#330 float(float stnum) getstatf (EXT_CSQC)
2586 VM_CL_getstati,                         //#331 float(float stnum) getstati (EXT_CSQC)
2587 VM_CL_getstats,                         //#332 string(float firststnum) getstats (EXT_CSQC)
2588 VM_CL_setmodelindex,            //#333 void(entity e, float mdlindex) setmodelindex (EXT_CSQC)
2589 VM_CL_modelnameforindex,        //#334 string(float mdlindex) modelnameforindex (EXT_CSQC)
2590 VM_CL_particleeffectnum,        //#335 float(string effectname) particleeffectnum (EXT_CSQC)
2591 VM_CL_trailparticles,           //#336 void(entity ent, float effectnum, vector start, vector end) trailparticles (EXT_CSQC)
2592 VM_CL_pointparticles,           //#337 void(float effectnum, vector origin [, vector dir, float count]) pointparticles (EXT_CSQC)
2593 VM_CL_centerprint,                      //#338 void(string s) cprint (EXT_CSQC)
2594 VM_print,                                       //#339 void(string s) print (EXT_CSQC)
2595 VM_keynumtostring,                      //#340 string(float keynum) keynumtostring (EXT_CSQC)
2596 VM_stringtokeynum,                      //#341 float(string keyname) stringtokeynum (EXT_CSQC)
2597 VM_CL_getkeybind,                       //#342 string(float keynum) getkeybind (EXT_CSQC)
2598 VM_CL_setcursormode,            //#343 void(float usecursor) setcursormode (EXT_CSQC)
2599 VM_getmousepos,                         //#344 vector() getmousepos (EXT_CSQC)
2600 VM_CL_getinputstate,            //#345 float(float framenum) getinputstate (EXT_CSQC)
2601 VM_CL_setsensitivityscale,      //#346 void(float sens) setsensitivityscaler (EXT_CSQC)
2602 VM_CL_runplayerphysics,         //#347 void() runstandardplayerphysics (EXT_CSQC)
2603 VM_CL_getplayerkey,                     //#348 string(float playernum, string keyname) getplayerkeyvalue (EXT_CSQC)
2604 VM_CL_isdemo,                           //#349 float() isdemo (EXT_CSQC)
2605 VM_isserver,                            //#350 float() isserver (EXT_CSQC)
2606 VM_CL_setlistener,                      //#351 void(vector origin, vector forward, vector right, vector up) SetListener (EXT_CSQC)
2607 VM_CL_registercmd,                      //#352 void(string cmdname) registercommand (EXT_CSQC)
2608 VM_WasFreed,                            //#353 float(entity ent) wasfreed (EXT_CSQC) (should be availabe on server too)
2609 VM_CL_playernum,                        //#354 float() playernum
2610 VM_CL_onground,                         //#355 float() cl_onground (EXT_CSQC)
2611 VM_charindex,                           //#356 float(string s, float num) charindex
2612 VM_CL_selecttraceline,          //#357 float(vector start, vector end, float ignore, float csqcents) selecttraceline
2613 0,                      //#358
2614 0,                      //#359
2615 VM_CL_ReadByte,                         //#360 float() readbyte (EXT_CSQC)
2616 VM_CL_ReadChar,                         //#361 float() readchar (EXT_CSQC)
2617 VM_CL_ReadShort,                        //#362 float() readshort (EXT_CSQC)
2618 VM_CL_ReadLong,                         //#363 float() readlong (EXT_CSQC)
2619 VM_CL_ReadCoord,                        //#364 float() readcoord (EXT_CSQC)
2620 VM_CL_ReadAngle,                        //#365 float() readangle (EXT_CSQC)
2621 VM_CL_ReadString,                       //#366 string() readstring (EXT_CSQC)
2622 VM_CL_ReadFloat,                        //#367 float() readfloat (EXT_CSQC)
2623 0,                      //#368
2624 0,                      //#369
2625 0,                      //#370
2626 0,                      //#371
2627 0,                      //#372
2628 0,                      //#373
2629 0,                      //#374
2630 0,                      //#375
2631 0,                      //#376
2632 0,                      //#377
2633 0,                      //#378
2634 0,                      //#379
2635 0,                      //#380
2636 0,                      //#381
2637 0,                      //#382
2638 0,                      //#383
2639 0,                      //#384
2640 0,                      //#385
2641 0,                      //#386
2642 0,                      //#387
2643 0,                      //#388
2644 0,                      //#389
2645 0,                      //#390
2646 0,                      //#391
2647 0,                      //#392
2648 0,                      //#393
2649 0,                      //#394
2650 0,                      //#395
2651 0,                      //#396
2652 0,                      //#397
2653 0,                      //#398
2654 0,                      //#399
2655 //=========CSQC end========//
2656
2657 VM_copyentity,                                  // #400 void(entity from, entity to) copyentity (DP_QC_COPYENTITY)
2658 0,
2659 VM_findchain,                                   // #402 entity(.string fld, string match) findchain (DP_QC_FINDCHAIN)
2660 VM_findchainfloat,                              // #403 entity(.float fld, float match) findchainfloat (DP_QC_FINDCHAINFLOAT)
2661 VM_CL_effect,                                   // #404 void(vector org, string modelname, float startframe, float endframe, float framerate) effect (DP_SV_EFFECT)
2662 VM_CL_te_blood,                                 // #405 void(vector org, vector velocity, float howmany) te_blood (DP_TE_BLOOD)
2663 VM_CL_te_bloodshower,                   // #406 void(vector mincorner, vector maxcorner, float explosionspeed, float howmany) te_bloodshower (DP_TE_BLOODSHOWER)
2664 VM_CL_te_explosionrgb,                  // #407 void(vector org, vector color) te_explosionrgb (DP_TE_EXPLOSIONRGB)
2665 VM_CL_te_particlecube,                  // #408 void(vector mincorner, vector maxcorner, vector vel, float howmany, float color, float gravityflag, float randomveljitter) te_particlecube (DP_TE_PARTICLECUBE)
2666 VM_CL_te_particlerain,                  // #409 void(vector mincorner, vector maxcorner, vector vel, float howmany, float color) te_particlerain (DP_TE_PARTICLERAIN)
2667 VM_CL_te_particlesnow,                  // #410 void(vector mincorner, vector maxcorner, vector vel, float howmany, float color) te_particlesnow (DP_TE_PARTICLESNOW)
2668 VM_CL_te_spark,                                 // #411 void(vector org, vector vel, float howmany) te_spark (DP_TE_SPARK)
2669 VM_CL_te_gunshotquad,                   // #412 void(vector org) te_gunshotquad (DP_QUADEFFECTS1)
2670 VM_CL_te_spikequad,                             // #413 void(vector org) te_spikequad (DP_QUADEFFECTS1)
2671 VM_CL_te_superspikequad,                // #414 void(vector org) te_superspikequad (DP_QUADEFFECTS1)
2672 VM_CL_te_explosionquad,                 // #415 void(vector org) te_explosionquad (DP_QUADEFFECTS1)
2673 VM_CL_te_smallflash,                    // #416 void(vector org) te_smallflash (DP_TE_SMALLFLASH)
2674 VM_CL_te_customflash,                   // #417 void(vector org, float radius, float lifetime, vector color) te_customflash (DP_TE_CUSTOMFLASH)
2675 VM_CL_te_gunshot,                               // #418 void(vector org) te_gunshot (DP_TE_STANDARDEFFECTBUILTINS)
2676 VM_CL_te_spike,                                 // #419 void(vector org) te_spike (DP_TE_STANDARDEFFECTBUILTINS)
2677 VM_CL_te_superspike,                    // #420 void(vector org) te_superspike (DP_TE_STANDARDEFFECTBUILTINS)
2678 VM_CL_te_explosion,                             // #421 void(vector org) te_explosion (DP_TE_STANDARDEFFECTBUILTINS)
2679 VM_CL_te_tarexplosion,                  // #422 void(vector org) te_tarexplosion (DP_TE_STANDARDEFFECTBUILTINS)
2680 VM_CL_te_wizspike,                              // #423 void(vector org) te_wizspike (DP_TE_STANDARDEFFECTBUILTINS)
2681 VM_CL_te_knightspike,                   // #424 void(vector org) te_knightspike (DP_TE_STANDARDEFFECTBUILTINS)
2682 VM_CL_te_lavasplash,                    // #425 void(vector org) te_lavasplash (DP_TE_STANDARDEFFECTBUILTINS)
2683 VM_CL_te_teleport,                              // #426 void(vector org) te_teleport (DP_TE_STANDARDEFFECTBUILTINS)
2684 VM_CL_te_explosion2,                    // #427 void(vector org, float colorstart, float colorlength) te_explosion2 (DP_TE_STANDARDEFFECTBUILTINS)
2685 VM_CL_te_lightning1,                    // #428 void(entity own, vector start, vector end) te_lightning1 (DP_TE_STANDARDEFFECTBUILTINS)
2686 VM_CL_te_lightning2,                    // #429 void(entity own, vector start, vector end) te_lightning2 (DP_TE_STANDARDEFFECTBUILTINS)
2687 VM_CL_te_lightning3,                    // #430 void(entity own, vector start, vector end) te_lightning3 (DP_TE_STANDARDEFFECTBUILTINS)
2688 VM_CL_te_beam,                                  // #431 void(entity own, vector start, vector end) te_beam (DP_TE_STANDARDEFFECTBUILTINS)
2689 VM_vectorvectors,                               // #432 void(vector dir) vectorvectors (DP_QC_VECTORVECTORS)
2690 VM_CL_te_plasmaburn,                    // #433 void(vector org) te_plasmaburn (DP_TE_PLASMABURN)
2691 VM_CL_getsurfacenumpoints,              // #434 float(entity e, float s) getsurfacenumpoints (DP_QC_GETSURFACE)
2692 VM_CL_getsurfacepoint,                  // #435 vector(entity e, float s, float n) getsurfacepoint (DP_QC_GETSURFACE)
2693 VM_CL_getsurfacenormal,                 // #436 vector(entity e, float s) getsurfacenormal (DP_QC_GETSURFACE)
2694 VM_CL_getsurfacetexture,                // #437 string(entity e, float s) getsurfacetexture (DP_QC_GETSURFACE)
2695 VM_CL_getsurfacenearpoint,              // #438 float(entity e, vector p) getsurfacenearpoint (DP_QC_GETSURFACE)
2696 VM_CL_getsurfaceclippedpoint,   // #439 vector(entity e, float s, vector p) getsurfaceclippedpoint (DP_QC_GETSURFACE)
2697 0,                                                                      // #440
2698 VM_tokenize,                            // #441 float(string s) tokenize (KRIMZON_SV_PARSECLIENTCOMMAND)
2699 VM_argv,                                        // #442 string(float n) argv (KRIMZON_SV_PARSECLIENTCOMMAND)
2700 VM_CL_setattachment,            // #443 void(entity e, entity tagentity, string tagname) setattachment (DP_GFX_QUAKE3MODELTAGS)
2701 VM_search_begin,                        // #444 float(string pattern, float caseinsensitive, float quiet) search_begin (DP_FS_SEARCH)
2702 VM_search_end,                          // #445 void(float handle) search_end (DP_FS_SEARCH)
2703 VM_search_getsize,                      // #446 float(float handle) search_getsize (DP_FS_SEARCH)
2704 VM_search_getfilename,          // #447 string(float handle, float num) search_getfilename (DP_FS_SEARCH)
2705 VM_cvar_string,                         // #448 string(string s) cvar_string (DP_QC_CVAR_STRING)
2706 VM_findflags,                           // #449 entity(entity start, .float fld, float match) findflags (DP_QC_FINDFLAGS)
2707 VM_findchainflags,                      // #450 entity(.float fld, float match) findchainflags (DP_QC_FINDCHAINFLAGS)
2708 VM_CL_gettagindex,                      // #451 float(entity ent, string tagname) gettagindex (DP_QC_GETTAGINFO)
2709 VM_CL_gettaginfo,                       // #452 vector(entity ent, float tagindex) gettaginfo (DP_QC_GETTAGINFO)
2710 0,                                                              // #453
2711 0,                                                              // #454
2712 0,                                                              // #455
2713 NULL,                                           // #456
2714 NULL,                                           // #457
2715 NULL,                                           // #458
2716 NULL,                                           // #459
2717 VM_buf_create,                          // #460 float() buf_create (DP_QC_STRINGBUFFERS)
2718 VM_buf_del,                                     // #461 void(float bufhandle) buf_del (DP_QC_STRINGBUFFERS)
2719 VM_buf_getsize,                         // #462 float(float bufhandle) buf_getsize (DP_QC_STRINGBUFFERS)
2720 VM_buf_copy,                            // #463 void(float bufhandle_from, float bufhandle_to) buf_copy (DP_QC_STRINGBUFFERS)
2721 VM_buf_sort,                            // #464 void(float bufhandle, float sortpower, float backward) buf_sort (DP_QC_STRINGBUFFERS)
2722 VM_buf_implode,                         // #465 string(float bufhandle, string glue) buf_implode (DP_QC_STRINGBUFFERS)
2723 VM_bufstr_get,                          // #466 string(float bufhandle, float string_index) bufstr_get (DP_QC_STRINGBUFFERS)
2724 VM_bufstr_set,                          // #467 void(float bufhandle, float string_index, string str) bufstr_set (DP_QC_STRINGBUFFERS)
2725 VM_bufstr_add,                          // #468 float(float bufhandle, string str, float order) bufstr_add (DP_QC_STRINGBUFFERS)
2726 VM_bufstr_free,                         // #469 void(float bufhandle, float string_index) bufstr_free (DP_QC_STRINGBUFFERS)
2727 e10, e10, e10                   // #470-499 (LordHavoc)
2728 };
2729
2730 const int vm_cl_numbuiltins = sizeof(vm_cl_builtins) / sizeof(prvm_builtin_t);
2731
2732 void VM_CL_Cmd_Init(void)
2733 {
2734 }
2735
2736 void VM_CL_Cmd_Reset(void)
2737 {
2738 }
2739