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