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