fix compile error where no extern was defined for ode_dll
[xonotic/darkplaces.git] / prvm_cmds.c
1 // AK
2 // Basically every vm builtin cmd should be in here.
3 // All 3 builtin and extension lists can be found here
4 // cause large (I think they will) parts are from pr_cmds the same copyright like in pr_cmds
5 // also applies here
6
7 #include "quakedef.h"
8
9 #include "prvm_cmds.h"
10 #include "libcurl.h"
11 #include <time.h>
12
13 #include "cl_collision.h"
14 #include "clvm_cmds.h"
15 #include "csprogs.h"
16 #include "ft2.h"
17 #include "mdfour.h"
18
19 extern cvar_t prvm_backtraceforwarnings;
20 extern dllhandle_t ode_dll;
21
22 // LordHavoc: changed this to NOT use a return statement, so that it can be used in functions that must return a value
23 void VM_Warning(prvm_prog_t *prog, const char *fmt, ...)
24 {
25         va_list argptr;
26         char msg[MAX_INPUTLINE];
27         static double recursive = -1;
28
29         va_start(argptr,fmt);
30         dpvsnprintf(msg,sizeof(msg),fmt,argptr);
31         va_end(argptr);
32
33         Con_Print(msg);
34
35         // TODO: either add a cvar/cmd to control the state dumping or replace some of the calls with Con_Printf [9/13/2006 Black]
36         if(prvm_backtraceforwarnings.integer && recursive != realtime) // NOTE: this compares to the time, just in case if PRVM_PrintState causes a Host_Error and keeps recursive set
37         {
38                 recursive = realtime;
39                 PRVM_PrintState(prog);
40                 recursive = -1;
41         }
42 }
43
44
45 //============================================================================
46 // Common
47
48 // TODO DONE: move vm_files and vm_fssearchlist to prvm_prog_t struct
49 // TODO: move vm_files and vm_fssearchlist back [9/13/2006 Black]
50 // TODO: (move vm_files and vm_fssearchlist to prvm_prog_t struct again) [2007-01-23 LordHavoc]
51 // TODO: will this war ever end? [2007-01-23 LordHavoc]
52
53 void VM_CheckEmptyString(prvm_prog_t *prog, const char *s)
54 {
55         if (ISWHITESPACE(s[0]))
56                 prog->error_cmd("%s: Bad string", prog->name);
57 }
58
59 void VM_GenerateFrameGroupBlend(prvm_prog_t *prog, framegroupblend_t *framegroupblend, const prvm_edict_t *ed)
60 {
61         // self.frame is the interpolation target (new frame)
62         // self.frame1time is the animation base time for the interpolation target
63         // self.frame2 is the interpolation start (previous frame)
64         // self.frame2time is the animation base time for the interpolation start
65         // self.lerpfrac is the interpolation strength for self.frame2
66         // self.lerpfrac3 is the interpolation strength for self.frame3
67         // self.lerpfrac4 is the interpolation strength for self.frame4
68         // pitch angle on a player model where the animator set up 5 sets of
69         // animations and the csqc simply lerps between sets)
70         framegroupblend[0].frame = (int) PRVM_gameedictfloat(ed, frame     );
71         framegroupblend[1].frame = (int) PRVM_gameedictfloat(ed, frame2    );
72         framegroupblend[2].frame = (int) PRVM_gameedictfloat(ed, frame3    );
73         framegroupblend[3].frame = (int) PRVM_gameedictfloat(ed, frame4    );
74         framegroupblend[0].start =       PRVM_gameedictfloat(ed, frame1time);
75         framegroupblend[1].start =       PRVM_gameedictfloat(ed, frame2time);
76         framegroupblend[2].start =       PRVM_gameedictfloat(ed, frame3time);
77         framegroupblend[3].start =       PRVM_gameedictfloat(ed, frame4time);
78         framegroupblend[1].lerp  =       PRVM_gameedictfloat(ed, lerpfrac  );
79         framegroupblend[2].lerp  =       PRVM_gameedictfloat(ed, lerpfrac3 );
80         framegroupblend[3].lerp  =       PRVM_gameedictfloat(ed, lerpfrac4 );
81         // assume that the (missing) lerpfrac1 is whatever remains after lerpfrac2+lerpfrac3+lerpfrac4 are summed
82         framegroupblend[0].lerp = 1 - framegroupblend[1].lerp - framegroupblend[2].lerp - framegroupblend[3].lerp;
83 }
84
85 // LordHavoc: quite tempting to break apart this function to reuse the
86 //            duplicated code, but I suspect it is better for performance
87 //            this way
88 void VM_FrameBlendFromFrameGroupBlend(frameblend_t *frameblend, const framegroupblend_t *framegroupblend, const dp_model_t *model)
89 {
90         int sub2, numframes, f, i, k;
91         int isfirstframegroup = true;
92         int nolerp;
93         double sublerp, lerp, d;
94         const animscene_t *scene;
95         const framegroupblend_t *g;
96         frameblend_t *blend = frameblend;
97
98         memset(blend, 0, MAX_FRAMEBLENDS * sizeof(*blend));
99
100         if (!model || !model->surfmesh.isanimated)
101         {
102                 blend[0].lerp = 1;
103                 return;
104         }
105
106         nolerp = (model->type == mod_sprite) ? !r_lerpsprites.integer : !r_lerpmodels.integer;
107         numframes = model->numframes;
108         for (k = 0, g = framegroupblend;k < MAX_FRAMEGROUPBLENDS;k++, g++)
109         {
110                 f = g->frame;
111                 if ((unsigned int)f >= (unsigned int)numframes)
112                 {
113                         if (developer_extra.integer)
114                                 Con_DPrintf("VM_FrameBlendFromFrameGroupBlend: no such frame %d in model %s\n", f, model->name);
115                         f = 0;
116                 }
117                 d = lerp = g->lerp;
118                 if (lerp <= 0)
119                         continue;
120                 if (nolerp)
121                 {
122                         if (isfirstframegroup)
123                         {
124                                 d = lerp = 1;
125                                 isfirstframegroup = false;
126                         }
127                         else
128                                 continue;
129                 }
130                 if (model->animscenes)
131                 {
132                         scene = model->animscenes + f;
133                         f = scene->firstframe;
134                         if (scene->framecount > 1)
135                         {
136                                 // this code path is only used on .zym models and torches
137                                 sublerp = scene->framerate * (cl.time - g->start);
138                                 f = (int) floor(sublerp);
139                                 sublerp -= f;
140                                 sub2 = f + 1;
141                                 if (sublerp < (1.0 / 65536.0f))
142                                         sublerp = 0;
143                                 if (sublerp > (65535.0f / 65536.0f))
144                                         sublerp = 1;
145                                 if (nolerp)
146                                         sublerp = 0;
147                                 if (scene->loop)
148                                 {
149                                         f = (f % scene->framecount);
150                                         sub2 = (sub2 % scene->framecount);
151                                 }
152                                 f = bound(0, f, (scene->framecount - 1)) + scene->firstframe;
153                                 sub2 = bound(0, sub2, (scene->framecount - 1)) + scene->firstframe;
154                                 d = sublerp * lerp;
155                                 // two framelerps produced from one animation
156                                 if (d > 0)
157                                 {
158                                         for (i = 0;i < MAX_FRAMEBLENDS;i++)
159                                         {
160                                                 if (blend[i].lerp <= 0 || blend[i].subframe == sub2)
161                                                 {
162                                                         blend[i].subframe = sub2;
163                                                         blend[i].lerp += d;
164                                                         break;
165                                                 }
166                                         }
167                                 }
168                                 d = (1 - sublerp) * lerp;
169                         }
170                 }
171                 if (d > 0)
172                 {
173                         for (i = 0;i < MAX_FRAMEBLENDS;i++)
174                         {
175                                 if (blend[i].lerp <= 0 || blend[i].subframe == f)
176                                 {
177                                         blend[i].subframe = f;
178                                         blend[i].lerp += d;
179                                         break;
180                                 }
181                         }
182                 }
183         }
184 }
185
186 void VM_UpdateEdictSkeleton(prvm_prog_t *prog, prvm_edict_t *ed, const dp_model_t *edmodel, const frameblend_t *frameblend)
187 {
188         if (ed->priv.server->skeleton.model != edmodel)
189         {
190                 VM_RemoveEdictSkeleton(prog, ed);
191                 ed->priv.server->skeleton.model = edmodel;
192         }
193         if (!ed->priv.server->skeleton.model || !ed->priv.server->skeleton.model->num_bones)
194         {
195                 if(ed->priv.server->skeleton.relativetransforms)
196                         Mem_Free(ed->priv.server->skeleton.relativetransforms);
197                 ed->priv.server->skeleton.relativetransforms = NULL;
198                 return;
199         }
200
201         {
202                 int skeletonindex = -1;
203                 skeleton_t *skeleton;
204                 skeletonindex = (int)PRVM_gameedictfloat(ed, skeletonindex) - 1;
205                 if (skeletonindex >= 0 && skeletonindex < MAX_EDICTS && (skeleton = prog->skeletons[skeletonindex]) && skeleton->model->num_bones == ed->priv.server->skeleton.model->num_bones)
206                 {
207                         // custom skeleton controlled by the game (FTE_CSQC_SKELETONOBJECTS)
208                         if (!ed->priv.server->skeleton.relativetransforms)
209                                 ed->priv.server->skeleton.relativetransforms = (matrix4x4_t *)Mem_Alloc(prog->progs_mempool, ed->priv.server->skeleton.model->num_bones * sizeof(matrix4x4_t));
210                         memcpy(ed->priv.server->skeleton.relativetransforms, skeleton->relativetransforms, ed->priv.server->skeleton.model->num_bones * sizeof(matrix4x4_t));
211                 }
212                 else
213                 {
214                         if(ed->priv.server->skeleton.relativetransforms)
215                                 Mem_Free(ed->priv.server->skeleton.relativetransforms);
216                         ed->priv.server->skeleton.relativetransforms = NULL;
217                 }
218         }
219 }
220
221 void VM_RemoveEdictSkeleton(prvm_prog_t *prog, prvm_edict_t *ed)
222 {
223         if (ed->priv.server->skeleton.relativetransforms)
224                 Mem_Free(ed->priv.server->skeleton.relativetransforms);
225         memset(&ed->priv.server->skeleton, 0, sizeof(ed->priv.server->skeleton));
226 }
227
228
229
230
231 //============================================================================
232 //BUILT-IN FUNCTIONS
233
234 void VM_VarString(prvm_prog_t *prog, int first, char *out, int outlength)
235 {
236         int i;
237         const char *s;
238         char *outend;
239
240         outend = out + outlength - 1;
241         for (i = first;i < prog->argc && out < outend;i++)
242         {
243                 s = PRVM_G_STRING((OFS_PARM0+i*3));
244                 while (out < outend && *s)
245                         *out++ = *s++;
246         }
247         *out++ = 0;
248 }
249
250 /*
251 =================
252 VM_checkextension
253
254 returns true if the extension is supported by the server
255
256 checkextension(extensionname)
257 =================
258 */
259
260 // kind of helper function
261 static qboolean checkextension(prvm_prog_t *prog, const char *name)
262 {
263         int len;
264         const char *e, *start;
265         len = (int)strlen(name);
266
267         for (e = prog->extensionstring;*e;e++)
268         {
269                 while (*e == ' ')
270                         e++;
271                 if (!*e)
272                         break;
273                 start = e;
274                 while (*e && *e != ' ')
275                         e++;
276                 if ((e - start) == len && !strncasecmp(start, name, len))
277                 {
278                         // special sheck for ODE
279                         if (!strncasecmp("DP_PHYSICS_ODE", name, 14))
280                         {
281 #ifdef ODE_DYNAMIC
282                                 return ode_dll ? true : false;
283 #else
284 #ifdef ODE_STATIC
285                                 return true;
286 #else
287                                 return false;
288 #endif
289 #endif
290                         }
291
292                         // special sheck for d0_blind_id
293                         if (!strcasecmp("DP_CRYPTO", name))
294                                 return Crypto_Available();
295                         if (!strcasecmp("DP_QC_DIGEST_SHA256", name))
296                                 return Crypto_Available();
297
298                         return true;
299                 }
300         }
301         return false;
302 }
303
304 void VM_checkextension(prvm_prog_t *prog)
305 {
306         VM_SAFEPARMCOUNT(1,VM_checkextension);
307
308         PRVM_G_FLOAT(OFS_RETURN) = checkextension(prog, PRVM_G_STRING(OFS_PARM0));
309 }
310
311 /*
312 =================
313 VM_error
314
315 This is a TERMINAL error, which will kill off the entire prog.
316 Dumps self.
317
318 error(value)
319 =================
320 */
321 void VM_error(prvm_prog_t *prog)
322 {
323         prvm_edict_t    *ed;
324         char string[VM_STRINGTEMP_LENGTH];
325
326         VM_VarString(prog, 0, string, sizeof(string));
327         Con_Printf("======%s ERROR in %s:\n%s\n", prog->name, PRVM_GetString(prog, prog->xfunction->s_name), string);
328         ed = PRVM_PROG_TO_EDICT(PRVM_allglobaledict(self));
329         PRVM_ED_Print(prog, ed, NULL);
330
331         prog->error_cmd("%s: Program error in function %s:\n%s\nTip: read above for entity information\n", prog->name, PRVM_GetString(prog, prog->xfunction->s_name), string);
332 }
333
334 /*
335 =================
336 VM_objerror
337
338 Dumps out self, then an error message.  The program is aborted and self is
339 removed, but the level can continue.
340
341 objerror(value)
342 =================
343 */
344 void VM_objerror(prvm_prog_t *prog)
345 {
346         prvm_edict_t    *ed;
347         char string[VM_STRINGTEMP_LENGTH];
348
349         VM_VarString(prog, 0, string, sizeof(string));
350         Con_Printf("======OBJECT ERROR======\n"); // , prog->name, PRVM_GetString(prog->xfunction->s_name), string); // or include them? FIXME
351         ed = PRVM_PROG_TO_EDICT(PRVM_allglobaledict(self));
352         PRVM_ED_Print(prog, ed, NULL);
353         PRVM_ED_Free (prog, ed);
354         Con_Printf("%s OBJECT ERROR in %s:\n%s\nTip: read above for entity information\n", prog->name, PRVM_GetString(prog, prog->xfunction->s_name), string);
355 }
356
357 /*
358 =================
359 VM_print
360
361 print to console
362
363 print(...[string])
364 =================
365 */
366 void VM_print(prvm_prog_t *prog)
367 {
368         char string[VM_STRINGTEMP_LENGTH];
369
370         VM_VarString(prog, 0, string, sizeof(string));
371         Con_Print(string);
372 }
373
374 /*
375 =================
376 VM_bprint
377
378 broadcast print to everyone on server
379
380 bprint(...[string])
381 =================
382 */
383 void VM_bprint(prvm_prog_t *prog)
384 {
385         char string[VM_STRINGTEMP_LENGTH];
386
387         if(!sv.active)
388         {
389                 VM_Warning(prog, "VM_bprint: game is not server(%s) !\n", prog->name);
390                 return;
391         }
392
393         VM_VarString(prog, 0, string, sizeof(string));
394         SV_BroadcastPrint(string);
395 }
396
397 /*
398 =================
399 VM_sprint (menu & client but only if server.active == true)
400
401 single print to a specific client
402
403 sprint(float clientnum,...[string])
404 =================
405 */
406 void VM_sprint(prvm_prog_t *prog)
407 {
408         client_t        *client;
409         int                     clientnum;
410         char string[VM_STRINGTEMP_LENGTH];
411
412         VM_SAFEPARMCOUNTRANGE(1, 8, VM_sprint);
413
414         //find client for this entity
415         clientnum = (int)PRVM_G_FLOAT(OFS_PARM0);
416         if (!sv.active  || clientnum < 0 || clientnum >= svs.maxclients || !svs.clients[clientnum].active)
417         {
418                 VM_Warning(prog, "VM_sprint: %s: invalid client or server is not active !\n", prog->name);
419                 return;
420         }
421
422         client = svs.clients + clientnum;
423         if (!client->netconnection)
424                 return;
425
426         VM_VarString(prog, 1, string, sizeof(string));
427         MSG_WriteChar(&client->netconnection->message,svc_print);
428         MSG_WriteString(&client->netconnection->message, string);
429 }
430
431 /*
432 =================
433 VM_centerprint
434
435 single print to the screen
436
437 centerprint(value)
438 =================
439 */
440 void VM_centerprint(prvm_prog_t *prog)
441 {
442         char string[VM_STRINGTEMP_LENGTH];
443
444         VM_SAFEPARMCOUNTRANGE(1, 8, VM_centerprint);
445         VM_VarString(prog, 0, string, sizeof(string));
446         SCR_CenterPrint(string);
447 }
448
449 /*
450 =================
451 VM_normalize
452
453 vector normalize(vector)
454 =================
455 */
456 void VM_normalize(prvm_prog_t *prog)
457 {
458         float   *value1;
459         vec3_t  newvalue;
460         double  f;
461
462         VM_SAFEPARMCOUNT(1,VM_normalize);
463
464         value1 = PRVM_G_VECTOR(OFS_PARM0);
465
466         f = VectorLength2(value1);
467         if (f)
468         {
469                 f = 1.0 / sqrt(f);
470                 VectorScale(value1, f, newvalue);
471         }
472         else
473                 VectorClear(newvalue);
474
475         VectorCopy (newvalue, PRVM_G_VECTOR(OFS_RETURN));
476 }
477
478 /*
479 =================
480 VM_vlen
481
482 scalar vlen(vector)
483 =================
484 */
485 void VM_vlen(prvm_prog_t *prog)
486 {
487         VM_SAFEPARMCOUNT(1,VM_vlen);
488         PRVM_G_FLOAT(OFS_RETURN) = VectorLength(PRVM_G_VECTOR(OFS_PARM0));
489 }
490
491 /*
492 =================
493 VM_vectoyaw
494
495 float vectoyaw(vector)
496 =================
497 */
498 void VM_vectoyaw(prvm_prog_t *prog)
499 {
500         float   *value1;
501         float   yaw;
502
503         VM_SAFEPARMCOUNT(1,VM_vectoyaw);
504
505         value1 = PRVM_G_VECTOR(OFS_PARM0);
506
507         if (value1[1] == 0 && value1[0] == 0)
508                 yaw = 0;
509         else
510         {
511                 yaw = (int) (atan2(value1[1], value1[0]) * 180 / M_PI);
512                 if (yaw < 0)
513                         yaw += 360;
514         }
515
516         PRVM_G_FLOAT(OFS_RETURN) = yaw;
517 }
518
519
520 /*
521 =================
522 VM_vectoangles
523
524 vector vectoangles(vector[, vector])
525 =================
526 */
527 void VM_vectoangles(prvm_prog_t *prog)
528 {
529         VM_SAFEPARMCOUNTRANGE(1, 2,VM_vectoangles);
530
531         AnglesFromVectors(PRVM_G_VECTOR(OFS_RETURN), PRVM_G_VECTOR(OFS_PARM0), prog->argc >= 2 ? PRVM_G_VECTOR(OFS_PARM1) : NULL, true);
532 }
533
534 /*
535 =================
536 VM_random
537
538 Returns a number from 0<= num < 1
539
540 float random()
541 =================
542 */
543 void VM_random(prvm_prog_t *prog)
544 {
545         VM_SAFEPARMCOUNT(0,VM_random);
546
547         PRVM_G_FLOAT(OFS_RETURN) = lhrandom(0, 1);
548 }
549
550 /*
551 =========
552 VM_localsound
553
554 localsound(string sample)
555 =========
556 */
557 void VM_localsound(prvm_prog_t *prog)
558 {
559         const char *s;
560
561         VM_SAFEPARMCOUNT(1,VM_localsound);
562
563         s = PRVM_G_STRING(OFS_PARM0);
564
565         if(!S_LocalSound (s))
566         {
567                 PRVM_G_FLOAT(OFS_RETURN) = -4;
568                 VM_Warning(prog, "VM_localsound: Failed to play %s for %s !\n", s, prog->name);
569                 return;
570         }
571
572         PRVM_G_FLOAT(OFS_RETURN) = 1;
573 }
574
575 /*
576 =================
577 VM_break
578
579 break()
580 =================
581 */
582 void VM_break(prvm_prog_t *prog)
583 {
584         prog->error_cmd("%s: break statement", prog->name);
585 }
586
587 //============================================================================
588
589 /*
590 =================
591 VM_localcmd
592
593 Sends text over to the client's execution buffer
594
595 [localcmd (string, ...) or]
596 cmd (string, ...)
597 =================
598 */
599 void VM_localcmd(prvm_prog_t *prog)
600 {
601         char string[VM_STRINGTEMP_LENGTH];
602         VM_SAFEPARMCOUNTRANGE(1, 8, VM_localcmd);
603         VM_VarString(prog, 0, string, sizeof(string));
604         Cbuf_AddText(string);
605 }
606
607 static qboolean PRVM_Cvar_ReadOk(const char *string)
608 {
609         cvar_t *cvar;
610         cvar = Cvar_FindVar(string);
611         return ((cvar) && ((cvar->flags & CVAR_PRIVATE) == 0));
612 }
613
614 /*
615 =================
616 VM_cvar
617
618 float cvar (string)
619 =================
620 */
621 void VM_cvar(prvm_prog_t *prog)
622 {
623         char string[VM_STRINGTEMP_LENGTH];
624         VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar);
625         VM_VarString(prog, 0, string, sizeof(string));
626         VM_CheckEmptyString(prog, string);
627         PRVM_G_FLOAT(OFS_RETURN) = PRVM_Cvar_ReadOk(string) ? Cvar_VariableValue(string) : 0;
628 }
629
630 /*
631 =================
632 VM_cvar
633
634 float cvar_type (string)
635 float CVAR_TYPEFLAG_EXISTS = 1;
636 float CVAR_TYPEFLAG_SAVED = 2;
637 float CVAR_TYPEFLAG_PRIVATE = 4;
638 float CVAR_TYPEFLAG_ENGINE = 8;
639 float CVAR_TYPEFLAG_HASDESCRIPTION = 16;
640 float CVAR_TYPEFLAG_READONLY = 32;
641 =================
642 */
643 void VM_cvar_type(prvm_prog_t *prog)
644 {
645         char string[VM_STRINGTEMP_LENGTH];
646         cvar_t *cvar;
647         int ret;
648
649         VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar);
650         VM_VarString(prog, 0, string, sizeof(string));
651         VM_CheckEmptyString(prog, string);
652         cvar = Cvar_FindVar(string);
653
654
655         if(!cvar)
656         {
657                 PRVM_G_FLOAT(OFS_RETURN) = 0;
658                 return; // CVAR_TYPE_NONE
659         }
660
661         ret = 1; // CVAR_EXISTS
662         if(cvar->flags & CVAR_SAVE)
663                 ret |= 2; // CVAR_TYPE_SAVED
664         if(cvar->flags & CVAR_PRIVATE)
665                 ret |= 4; // CVAR_TYPE_PRIVATE
666         if(!(cvar->flags & CVAR_ALLOCATED))
667                 ret |= 8; // CVAR_TYPE_ENGINE
668         if(cvar->description != cvar_dummy_description)
669                 ret |= 16; // CVAR_TYPE_HASDESCRIPTION
670         if(cvar->flags & CVAR_READONLY)
671                 ret |= 32; // CVAR_TYPE_READONLY
672         
673         PRVM_G_FLOAT(OFS_RETURN) = ret;
674 }
675
676 /*
677 =================
678 VM_cvar_string
679
680 const string    VM_cvar_string (string, ...)
681 =================
682 */
683 void VM_cvar_string(prvm_prog_t *prog)
684 {
685         char string[VM_STRINGTEMP_LENGTH];
686         VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar_string);
687         VM_VarString(prog, 0, string, sizeof(string));
688         VM_CheckEmptyString(prog, string);
689         PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, PRVM_Cvar_ReadOk(string) ? Cvar_VariableString(string) : "");
690 }
691
692
693 /*
694 ========================
695 VM_cvar_defstring
696
697 const string    VM_cvar_defstring (string, ...)
698 ========================
699 */
700 void VM_cvar_defstring(prvm_prog_t *prog)
701 {
702         char string[VM_STRINGTEMP_LENGTH];
703         VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar_defstring);
704         VM_VarString(prog, 0, string, sizeof(string));
705         VM_CheckEmptyString(prog, string);
706         PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, Cvar_VariableDefString(string));
707 }
708
709 /*
710 ========================
711 VM_cvar_defstring
712
713 const string    VM_cvar_description (string, ...)
714 ========================
715 */
716 void VM_cvar_description(prvm_prog_t *prog)
717 {
718         char string[VM_STRINGTEMP_LENGTH];
719         VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar_description);
720         VM_VarString(prog, 0, string, sizeof(string));
721         VM_CheckEmptyString(prog, string);
722         PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, Cvar_VariableDescription(string));
723 }
724 /*
725 =================
726 VM_cvar_set
727
728 void cvar_set (string,string, ...)
729 =================
730 */
731 void VM_cvar_set(prvm_prog_t *prog)
732 {
733         const char *name;
734         char string[VM_STRINGTEMP_LENGTH];
735         VM_SAFEPARMCOUNTRANGE(2,8,VM_cvar_set);
736         VM_VarString(prog, 1, string, sizeof(string));
737         name = PRVM_G_STRING(OFS_PARM0);
738         VM_CheckEmptyString(prog, name);
739         Cvar_Set(name, string);
740 }
741
742 /*
743 =========
744 VM_dprint
745
746 dprint(...[string])
747 =========
748 */
749 void VM_dprint(prvm_prog_t *prog)
750 {
751         char string[VM_STRINGTEMP_LENGTH];
752         VM_SAFEPARMCOUNTRANGE(1, 8, VM_dprint);
753         VM_VarString(prog, 0, string, sizeof(string));
754 #if 1
755         Con_DPrintf("%s", string);
756 #else
757         Con_DPrintf("%s: %s", prog->name, string);
758 #endif
759 }
760
761 /*
762 =========
763 VM_ftos
764
765 string  ftos(float)
766 =========
767 */
768
769 void VM_ftos(prvm_prog_t *prog)
770 {
771         float v;
772         char s[128];
773
774         VM_SAFEPARMCOUNT(1, VM_ftos);
775
776         v = PRVM_G_FLOAT(OFS_PARM0);
777
778         if ((float)((int)v) == v)
779                 dpsnprintf(s, sizeof(s), "%i", (int)v);
780         else
781                 dpsnprintf(s, sizeof(s), "%f", v);
782         PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, s);
783 }
784
785 /*
786 =========
787 VM_fabs
788
789 float   fabs(float)
790 =========
791 */
792
793 void VM_fabs(prvm_prog_t *prog)
794 {
795         float   v;
796
797         VM_SAFEPARMCOUNT(1,VM_fabs);
798
799         v = PRVM_G_FLOAT(OFS_PARM0);
800         PRVM_G_FLOAT(OFS_RETURN) = fabs(v);
801 }
802
803 /*
804 =========
805 VM_vtos
806
807 string  vtos(vector)
808 =========
809 */
810
811 void VM_vtos(prvm_prog_t *prog)
812 {
813         char s[512];
814
815         VM_SAFEPARMCOUNT(1,VM_vtos);
816
817         dpsnprintf (s, sizeof(s), "'%5.1f %5.1f %5.1f'", PRVM_G_VECTOR(OFS_PARM0)[0], PRVM_G_VECTOR(OFS_PARM0)[1], PRVM_G_VECTOR(OFS_PARM0)[2]);
818         PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, s);
819 }
820
821 /*
822 =========
823 VM_etos
824
825 string  etos(entity)
826 =========
827 */
828
829 void VM_etos(prvm_prog_t *prog)
830 {
831         char s[128];
832
833         VM_SAFEPARMCOUNT(1, VM_etos);
834
835         dpsnprintf (s, sizeof(s), "entity %i", PRVM_G_EDICTNUM(OFS_PARM0));
836         PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, s);
837 }
838
839 /*
840 =========
841 VM_stof
842
843 float stof(...[string])
844 =========
845 */
846 void VM_stof(prvm_prog_t *prog)
847 {
848         char string[VM_STRINGTEMP_LENGTH];
849         VM_SAFEPARMCOUNTRANGE(1, 8, VM_stof);
850         VM_VarString(prog, 0, string, sizeof(string));
851         PRVM_G_FLOAT(OFS_RETURN) = atof(string);
852 }
853
854 /*
855 ========================
856 VM_itof
857
858 float itof(intt ent)
859 ========================
860 */
861 void VM_itof(prvm_prog_t *prog)
862 {
863         VM_SAFEPARMCOUNT(1, VM_itof);
864         PRVM_G_FLOAT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
865 }
866
867 /*
868 ========================
869 VM_ftoe
870
871 entity ftoe(float num)
872 ========================
873 */
874 void VM_ftoe(prvm_prog_t *prog)
875 {
876         int ent;
877         VM_SAFEPARMCOUNT(1, VM_ftoe);
878
879         ent = (int)PRVM_G_FLOAT(OFS_PARM0);
880         if (ent < 0 || ent >= prog->max_edicts || PRVM_PROG_TO_EDICT(ent)->priv.required->free)
881                 ent = 0; // return world instead of a free or invalid entity
882
883         PRVM_G_INT(OFS_RETURN) = ent;
884 }
885
886 /*
887 ========================
888 VM_etof
889
890 float etof(entity ent)
891 ========================
892 */
893 void VM_etof(prvm_prog_t *prog)
894 {
895         VM_SAFEPARMCOUNT(1, VM_etof);
896         PRVM_G_FLOAT(OFS_RETURN) = PRVM_G_EDICTNUM(OFS_PARM0);
897 }
898
899 /*
900 =========
901 VM_strftime
902
903 string strftime(float uselocaltime, string[, string ...])
904 =========
905 */
906 void VM_strftime(prvm_prog_t *prog)
907 {
908         time_t t;
909 #if _MSC_VER >= 1400
910         struct tm tm;
911         int tmresult;
912 #else
913         struct tm *tm;
914 #endif
915         char fmt[VM_STRINGTEMP_LENGTH];
916         char result[VM_STRINGTEMP_LENGTH];
917         VM_SAFEPARMCOUNTRANGE(2, 8, VM_strftime);
918         VM_VarString(prog, 1, fmt, sizeof(fmt));
919         t = time(NULL);
920 #if _MSC_VER >= 1400
921         if (PRVM_G_FLOAT(OFS_PARM0))
922                 tmresult = localtime_s(&tm, &t);
923         else
924                 tmresult = gmtime_s(&tm, &t);
925         if (!tmresult)
926 #else
927         if (PRVM_G_FLOAT(OFS_PARM0))
928                 tm = localtime(&t);
929         else
930                 tm = gmtime(&t);
931         if (!tm)
932 #endif
933         {
934                 PRVM_G_INT(OFS_RETURN) = 0;
935                 return;
936         }
937 #if _MSC_VER >= 1400
938         strftime(result, sizeof(result), fmt, &tm);
939 #else
940         strftime(result, sizeof(result), fmt, tm);
941 #endif
942         PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, result);
943 }
944
945 /*
946 =========
947 VM_spawn
948
949 entity spawn()
950 =========
951 */
952
953 void VM_spawn(prvm_prog_t *prog)
954 {
955         prvm_edict_t    *ed;
956         VM_SAFEPARMCOUNT(0, VM_spawn);
957         prog->xfunction->builtinsprofile += 20;
958         ed = PRVM_ED_Alloc(prog);
959         VM_RETURN_EDICT(ed);
960 }
961
962 /*
963 =========
964 VM_remove
965
966 remove(entity e)
967 =========
968 */
969
970 void VM_remove(prvm_prog_t *prog)
971 {
972         prvm_edict_t    *ed;
973         prog->xfunction->builtinsprofile += 20;
974
975         VM_SAFEPARMCOUNT(1, VM_remove);
976
977         ed = PRVM_G_EDICT(OFS_PARM0);
978         if( PRVM_NUM_FOR_EDICT(ed) <= prog->reserved_edicts )
979         {
980                 if (developer.integer > 0)
981                         VM_Warning(prog, "VM_remove: tried to remove the null entity or a reserved entity!\n" );
982         }
983         else if( ed->priv.required->free )
984         {
985                 if (developer.integer > 0)
986                         VM_Warning(prog, "VM_remove: tried to remove an already freed entity!\n" );
987         }
988         else
989                 PRVM_ED_Free (prog, ed);
990 }
991
992 /*
993 =========
994 VM_find
995
996 entity  find(entity start, .string field, string match)
997 =========
998 */
999
1000 void VM_find(prvm_prog_t *prog)
1001 {
1002         int             e;
1003         int             f;
1004         const char      *s, *t;
1005         prvm_edict_t    *ed;
1006
1007         VM_SAFEPARMCOUNT(3,VM_find);
1008
1009         e = PRVM_G_EDICTNUM(OFS_PARM0);
1010         f = PRVM_G_INT(OFS_PARM1);
1011         s = PRVM_G_STRING(OFS_PARM2);
1012
1013         // LordHavoc: apparently BloodMage does a find(world, weaponmodel, "") and
1014         // expects it to find all the monsters, so we must be careful to support
1015         // searching for ""
1016
1017         for (e++ ; e < prog->num_edicts ; e++)
1018         {
1019                 prog->xfunction->builtinsprofile++;
1020                 ed = PRVM_EDICT_NUM(e);
1021                 if (ed->priv.required->free)
1022                         continue;
1023                 t = PRVM_E_STRING(ed,f);
1024                 if (!t)
1025                         t = "";
1026                 if (!strcmp(t,s))
1027                 {
1028                         VM_RETURN_EDICT(ed);
1029                         return;
1030                 }
1031         }
1032
1033         VM_RETURN_EDICT(prog->edicts);
1034 }
1035
1036 /*
1037 =========
1038 VM_findfloat
1039
1040   entity        findfloat(entity start, .float field, float match)
1041   entity        findentity(entity start, .entity field, entity match)
1042 =========
1043 */
1044 // LordHavoc: added this for searching float, int, and entity reference fields
1045 void VM_findfloat(prvm_prog_t *prog)
1046 {
1047         int             e;
1048         int             f;
1049         float   s;
1050         prvm_edict_t    *ed;
1051
1052         VM_SAFEPARMCOUNT(3,VM_findfloat);
1053
1054         e = PRVM_G_EDICTNUM(OFS_PARM0);
1055         f = PRVM_G_INT(OFS_PARM1);
1056         s = PRVM_G_FLOAT(OFS_PARM2);
1057
1058         for (e++ ; e < prog->num_edicts ; e++)
1059         {
1060                 prog->xfunction->builtinsprofile++;
1061                 ed = PRVM_EDICT_NUM(e);
1062                 if (ed->priv.required->free)
1063                         continue;
1064                 if (PRVM_E_FLOAT(ed,f) == s)
1065                 {
1066                         VM_RETURN_EDICT(ed);
1067                         return;
1068                 }
1069         }
1070
1071         VM_RETURN_EDICT(prog->edicts);
1072 }
1073
1074 /*
1075 =========
1076 VM_findchain
1077
1078 entity  findchain(.string field, string match)
1079 =========
1080 */
1081 // chained search for strings in entity fields
1082 // entity(.string field, string match) findchain = #402;
1083 void VM_findchain(prvm_prog_t *prog)
1084 {
1085         int             i;
1086         int             f;
1087         const char      *s, *t;
1088         prvm_edict_t    *ent, *chain;
1089         int chainfield;
1090
1091         VM_SAFEPARMCOUNTRANGE(2,3,VM_findchain);
1092
1093         if(prog->argc == 3)
1094                 chainfield = PRVM_G_INT(OFS_PARM2);
1095         else
1096                 chainfield = prog->fieldoffsets.chain;
1097         if (chainfield < 0)
1098                 prog->error_cmd("VM_findchain: %s doesnt have the specified chain field !", prog->name);
1099
1100         chain = prog->edicts;
1101
1102         f = PRVM_G_INT(OFS_PARM0);
1103         s = PRVM_G_STRING(OFS_PARM1);
1104
1105         // LordHavoc: apparently BloodMage does a find(world, weaponmodel, "") and
1106         // expects it to find all the monsters, so we must be careful to support
1107         // searching for ""
1108
1109         ent = PRVM_NEXT_EDICT(prog->edicts);
1110         for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
1111         {
1112                 prog->xfunction->builtinsprofile++;
1113                 if (ent->priv.required->free)
1114                         continue;
1115                 t = PRVM_E_STRING(ent,f);
1116                 if (!t)
1117                         t = "";
1118                 if (strcmp(t,s))
1119                         continue;
1120
1121                 PRVM_EDICTFIELDEDICT(ent,chainfield) = PRVM_NUM_FOR_EDICT(chain);
1122                 chain = ent;
1123         }
1124
1125         VM_RETURN_EDICT(chain);
1126 }
1127
1128 /*
1129 =========
1130 VM_findchainfloat
1131
1132 entity  findchainfloat(.string field, float match)
1133 entity  findchainentity(.string field, entity match)
1134 =========
1135 */
1136 // LordHavoc: chained search for float, int, and entity reference fields
1137 // entity(.string field, float match) findchainfloat = #403;
1138 void VM_findchainfloat(prvm_prog_t *prog)
1139 {
1140         int             i;
1141         int             f;
1142         float   s;
1143         prvm_edict_t    *ent, *chain;
1144         int chainfield;
1145
1146         VM_SAFEPARMCOUNTRANGE(2, 3, VM_findchainfloat);
1147
1148         if(prog->argc == 3)
1149                 chainfield = PRVM_G_INT(OFS_PARM2);
1150         else
1151                 chainfield = prog->fieldoffsets.chain;
1152         if (chainfield < 0)
1153                 prog->error_cmd("VM_findchain: %s doesnt have the specified chain field !", prog->name);
1154
1155         chain = (prvm_edict_t *)prog->edicts;
1156
1157         f = PRVM_G_INT(OFS_PARM0);
1158         s = PRVM_G_FLOAT(OFS_PARM1);
1159
1160         ent = PRVM_NEXT_EDICT(prog->edicts);
1161         for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
1162         {
1163                 prog->xfunction->builtinsprofile++;
1164                 if (ent->priv.required->free)
1165                         continue;
1166                 if (PRVM_E_FLOAT(ent,f) != s)
1167                         continue;
1168
1169                 PRVM_EDICTFIELDEDICT(ent,chainfield) = PRVM_EDICT_TO_PROG(chain);
1170                 chain = ent;
1171         }
1172
1173         VM_RETURN_EDICT(chain);
1174 }
1175
1176 /*
1177 ========================
1178 VM_findflags
1179
1180 entity  findflags(entity start, .float field, float match)
1181 ========================
1182 */
1183 // LordHavoc: search for flags in float fields
1184 void VM_findflags(prvm_prog_t *prog)
1185 {
1186         int             e;
1187         int             f;
1188         int             s;
1189         prvm_edict_t    *ed;
1190
1191         VM_SAFEPARMCOUNT(3, VM_findflags);
1192
1193
1194         e = PRVM_G_EDICTNUM(OFS_PARM0);
1195         f = PRVM_G_INT(OFS_PARM1);
1196         s = (int)PRVM_G_FLOAT(OFS_PARM2);
1197
1198         for (e++ ; e < prog->num_edicts ; e++)
1199         {
1200                 prog->xfunction->builtinsprofile++;
1201                 ed = PRVM_EDICT_NUM(e);
1202                 if (ed->priv.required->free)
1203                         continue;
1204                 if (!PRVM_E_FLOAT(ed,f))
1205                         continue;
1206                 if ((int)PRVM_E_FLOAT(ed,f) & s)
1207                 {
1208                         VM_RETURN_EDICT(ed);
1209                         return;
1210                 }
1211         }
1212
1213         VM_RETURN_EDICT(prog->edicts);
1214 }
1215
1216 /*
1217 ========================
1218 VM_findchainflags
1219
1220 entity  findchainflags(.float field, float match)
1221 ========================
1222 */
1223 // LordHavoc: chained search for flags in float fields
1224 void VM_findchainflags(prvm_prog_t *prog)
1225 {
1226         int             i;
1227         int             f;
1228         int             s;
1229         prvm_edict_t    *ent, *chain;
1230         int chainfield;
1231
1232         VM_SAFEPARMCOUNTRANGE(2, 3, VM_findchainflags);
1233
1234         if(prog->argc == 3)
1235                 chainfield = PRVM_G_INT(OFS_PARM2);
1236         else
1237                 chainfield = prog->fieldoffsets.chain;
1238         if (chainfield < 0)
1239                 prog->error_cmd("VM_findchain: %s doesnt have the specified chain field !", prog->name);
1240
1241         chain = (prvm_edict_t *)prog->edicts;
1242
1243         f = PRVM_G_INT(OFS_PARM0);
1244         s = (int)PRVM_G_FLOAT(OFS_PARM1);
1245
1246         ent = PRVM_NEXT_EDICT(prog->edicts);
1247         for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
1248         {
1249                 prog->xfunction->builtinsprofile++;
1250                 if (ent->priv.required->free)
1251                         continue;
1252                 if (!PRVM_E_FLOAT(ent,f))
1253                         continue;
1254                 if (!((int)PRVM_E_FLOAT(ent,f) & s))
1255                         continue;
1256
1257                 PRVM_EDICTFIELDEDICT(ent,chainfield) = PRVM_EDICT_TO_PROG(chain);
1258                 chain = ent;
1259         }
1260
1261         VM_RETURN_EDICT(chain);
1262 }
1263
1264 /*
1265 =========
1266 VM_precache_sound
1267
1268 string  precache_sound (string sample)
1269 =========
1270 */
1271 void VM_precache_sound(prvm_prog_t *prog)
1272 {
1273         const char *s;
1274
1275         VM_SAFEPARMCOUNT(1, VM_precache_sound);
1276
1277         s = PRVM_G_STRING(OFS_PARM0);
1278         PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
1279         //VM_CheckEmptyString(prog, s);
1280
1281         if(snd_initialized.integer && !S_PrecacheSound(s, true, true))
1282         {
1283                 VM_Warning(prog, "VM_precache_sound: Failed to load %s for %s\n", s, prog->name);
1284                 return;
1285         }
1286 }
1287
1288 /*
1289 =================
1290 VM_precache_file
1291
1292 returns the same string as output
1293
1294 does nothing, only used by qcc to build .pak archives
1295 =================
1296 */
1297 void VM_precache_file(prvm_prog_t *prog)
1298 {
1299         VM_SAFEPARMCOUNT(1,VM_precache_file);
1300         // precache_file is only used to copy files with qcc, it does nothing
1301         PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
1302 }
1303
1304 /*
1305 =========
1306 VM_coredump
1307
1308 coredump()
1309 =========
1310 */
1311 void VM_coredump(prvm_prog_t *prog)
1312 {
1313         VM_SAFEPARMCOUNT(0,VM_coredump);
1314
1315         Cbuf_AddText("prvm_edicts ");
1316         Cbuf_AddText(prog->name);
1317         Cbuf_AddText("\n");
1318 }
1319
1320 /*
1321 =========
1322 VM_stackdump
1323
1324 stackdump()
1325 =========
1326 */
1327 void VM_stackdump(prvm_prog_t *prog)
1328 {
1329         VM_SAFEPARMCOUNT(0, VM_stackdump);
1330
1331         PRVM_StackTrace(prog);
1332 }
1333
1334 /*
1335 =========
1336 VM_crash
1337
1338 crash()
1339 =========
1340 */
1341
1342 void VM_crash(prvm_prog_t *prog)
1343 {
1344         VM_SAFEPARMCOUNT(0, VM_crash);
1345
1346         prog->error_cmd("Crash called by %s",prog->name);
1347 }
1348
1349 /*
1350 =========
1351 VM_traceon
1352
1353 traceon()
1354 =========
1355 */
1356 void VM_traceon(prvm_prog_t *prog)
1357 {
1358         VM_SAFEPARMCOUNT(0,VM_traceon);
1359
1360         prog->trace = true;
1361 }
1362
1363 /*
1364 =========
1365 VM_traceoff
1366
1367 traceoff()
1368 =========
1369 */
1370 void VM_traceoff(prvm_prog_t *prog)
1371 {
1372         VM_SAFEPARMCOUNT(0,VM_traceoff);
1373
1374         prog->trace = false;
1375 }
1376
1377 /*
1378 =========
1379 VM_eprint
1380
1381 eprint(entity e)
1382 =========
1383 */
1384 void VM_eprint(prvm_prog_t *prog)
1385 {
1386         VM_SAFEPARMCOUNT(1,VM_eprint);
1387
1388         PRVM_ED_PrintNum (prog, PRVM_G_EDICTNUM(OFS_PARM0), NULL);
1389 }
1390
1391 /*
1392 =========
1393 VM_rint
1394
1395 float   rint(float)
1396 =========
1397 */
1398 void VM_rint(prvm_prog_t *prog)
1399 {
1400         float f;
1401         VM_SAFEPARMCOUNT(1,VM_rint);
1402
1403         f = PRVM_G_FLOAT(OFS_PARM0);
1404         if (f > 0)
1405                 PRVM_G_FLOAT(OFS_RETURN) = floor(f + 0.5);
1406         else
1407                 PRVM_G_FLOAT(OFS_RETURN) = ceil(f - 0.5);
1408 }
1409
1410 /*
1411 =========
1412 VM_floor
1413
1414 float   floor(float)
1415 =========
1416 */
1417 void VM_floor(prvm_prog_t *prog)
1418 {
1419         VM_SAFEPARMCOUNT(1,VM_floor);
1420
1421         PRVM_G_FLOAT(OFS_RETURN) = floor(PRVM_G_FLOAT(OFS_PARM0));
1422 }
1423
1424 /*
1425 =========
1426 VM_ceil
1427
1428 float   ceil(float)
1429 =========
1430 */
1431 void VM_ceil(prvm_prog_t *prog)
1432 {
1433         VM_SAFEPARMCOUNT(1,VM_ceil);
1434
1435         PRVM_G_FLOAT(OFS_RETURN) = ceil(PRVM_G_FLOAT(OFS_PARM0));
1436 }
1437
1438
1439 /*
1440 =============
1441 VM_nextent
1442
1443 entity  nextent(entity)
1444 =============
1445 */
1446 void VM_nextent(prvm_prog_t *prog)
1447 {
1448         int             i;
1449         prvm_edict_t    *ent;
1450
1451         VM_SAFEPARMCOUNT(1, VM_nextent);
1452
1453         i = PRVM_G_EDICTNUM(OFS_PARM0);
1454         while (1)
1455         {
1456                 prog->xfunction->builtinsprofile++;
1457                 i++;
1458                 if (i == prog->num_edicts)
1459                 {
1460                         VM_RETURN_EDICT(prog->edicts);
1461                         return;
1462                 }
1463                 ent = PRVM_EDICT_NUM(i);
1464                 if (!ent->priv.required->free)
1465                 {
1466                         VM_RETURN_EDICT(ent);
1467                         return;
1468                 }
1469         }
1470 }
1471
1472 //=============================================================================
1473
1474 /*
1475 ==============
1476 VM_changelevel
1477 server and menu
1478
1479 changelevel(string map)
1480 ==============
1481 */
1482 void VM_changelevel(prvm_prog_t *prog)
1483 {
1484         char vabuf[1024];
1485         VM_SAFEPARMCOUNT(1, VM_changelevel);
1486
1487         if(!sv.active)
1488         {
1489                 VM_Warning(prog, "VM_changelevel: game is not server (%s)\n", prog->name);
1490                 return;
1491         }
1492
1493 // make sure we don't issue two changelevels
1494         if (svs.changelevel_issued)
1495                 return;
1496         svs.changelevel_issued = true;
1497
1498         Cbuf_AddText(va(vabuf, sizeof(vabuf), "changelevel %s\n",PRVM_G_STRING(OFS_PARM0)));
1499 }
1500
1501 /*
1502 =========
1503 VM_sin
1504
1505 float   sin(float)
1506 =========
1507 */
1508 void VM_sin(prvm_prog_t *prog)
1509 {
1510         VM_SAFEPARMCOUNT(1,VM_sin);
1511         PRVM_G_FLOAT(OFS_RETURN) = sin(PRVM_G_FLOAT(OFS_PARM0));
1512 }
1513
1514 /*
1515 =========
1516 VM_cos
1517 float   cos(float)
1518 =========
1519 */
1520 void VM_cos(prvm_prog_t *prog)
1521 {
1522         VM_SAFEPARMCOUNT(1,VM_cos);
1523         PRVM_G_FLOAT(OFS_RETURN) = cos(PRVM_G_FLOAT(OFS_PARM0));
1524 }
1525
1526 /*
1527 =========
1528 VM_sqrt
1529
1530 float   sqrt(float)
1531 =========
1532 */
1533 void VM_sqrt(prvm_prog_t *prog)
1534 {
1535         VM_SAFEPARMCOUNT(1,VM_sqrt);
1536         PRVM_G_FLOAT(OFS_RETURN) = sqrt(PRVM_G_FLOAT(OFS_PARM0));
1537 }
1538
1539 /*
1540 =========
1541 VM_asin
1542
1543 float   asin(float)
1544 =========
1545 */
1546 void VM_asin(prvm_prog_t *prog)
1547 {
1548         VM_SAFEPARMCOUNT(1,VM_asin);
1549         PRVM_G_FLOAT(OFS_RETURN) = asin(PRVM_G_FLOAT(OFS_PARM0));
1550 }
1551
1552 /*
1553 =========
1554 VM_acos
1555 float   acos(float)
1556 =========
1557 */
1558 void VM_acos(prvm_prog_t *prog)
1559 {
1560         VM_SAFEPARMCOUNT(1,VM_acos);
1561         PRVM_G_FLOAT(OFS_RETURN) = acos(PRVM_G_FLOAT(OFS_PARM0));
1562 }
1563
1564 /*
1565 =========
1566 VM_atan
1567 float   atan(float)
1568 =========
1569 */
1570 void VM_atan(prvm_prog_t *prog)
1571 {
1572         VM_SAFEPARMCOUNT(1,VM_atan);
1573         PRVM_G_FLOAT(OFS_RETURN) = atan(PRVM_G_FLOAT(OFS_PARM0));
1574 }
1575
1576 /*
1577 =========
1578 VM_atan2
1579 float   atan2(float,float)
1580 =========
1581 */
1582 void VM_atan2(prvm_prog_t *prog)
1583 {
1584         VM_SAFEPARMCOUNT(2,VM_atan2);
1585         PRVM_G_FLOAT(OFS_RETURN) = atan2(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1586 }
1587
1588 /*
1589 =========
1590 VM_tan
1591 float   tan(float)
1592 =========
1593 */
1594 void VM_tan(prvm_prog_t *prog)
1595 {
1596         VM_SAFEPARMCOUNT(1,VM_tan);
1597         PRVM_G_FLOAT(OFS_RETURN) = tan(PRVM_G_FLOAT(OFS_PARM0));
1598 }
1599
1600 /*
1601 =================
1602 VM_randomvec
1603
1604 Returns a vector of length < 1 and > 0
1605
1606 vector randomvec()
1607 =================
1608 */
1609 void VM_randomvec(prvm_prog_t *prog)
1610 {
1611         vec3_t temp;
1612         VM_SAFEPARMCOUNT(0, VM_randomvec);
1613         VectorRandom(temp);
1614         VectorCopy(temp, PRVM_G_VECTOR(OFS_RETURN));
1615 }
1616
1617 //=============================================================================
1618
1619 /*
1620 =========
1621 VM_registercvar
1622
1623 float   registercvar (string name, string value[, float flags])
1624 =========
1625 */
1626 void VM_registercvar(prvm_prog_t *prog)
1627 {
1628         const char *name, *value;
1629         int     flags;
1630
1631         VM_SAFEPARMCOUNTRANGE(2, 3, VM_registercvar);
1632
1633         name = PRVM_G_STRING(OFS_PARM0);
1634         value = PRVM_G_STRING(OFS_PARM1);
1635         flags = prog->argc >= 3 ? (int)PRVM_G_FLOAT(OFS_PARM2) : 0;
1636         PRVM_G_FLOAT(OFS_RETURN) = 0;
1637
1638         if(flags > CVAR_MAXFLAGSVAL)
1639                 return;
1640
1641 // first check to see if it has already been defined
1642         if (Cvar_FindVar (name))
1643                 return;
1644
1645 // check for overlap with a command
1646         if (Cmd_Exists (name))
1647         {
1648                 VM_Warning(prog, "VM_registercvar: %s is a command\n", name);
1649                 return;
1650         }
1651
1652         Cvar_Get(name, value, flags, NULL);
1653
1654         PRVM_G_FLOAT(OFS_RETURN) = 1; // success
1655 }
1656
1657
1658 /*
1659 =================
1660 VM_min
1661
1662 returns the minimum of two supplied floats
1663
1664 float min(float a, float b, ...[float])
1665 =================
1666 */
1667 void VM_min(prvm_prog_t *prog)
1668 {
1669         VM_SAFEPARMCOUNTRANGE(2, 8, VM_min);
1670         // LordHavoc: 3+ argument enhancement suggested by FrikaC
1671         if (prog->argc >= 3)
1672         {
1673                 int i;
1674                 float f = PRVM_G_FLOAT(OFS_PARM0);
1675                 for (i = 1;i < prog->argc;i++)
1676                         if (f > PRVM_G_FLOAT((OFS_PARM0+i*3)))
1677                                 f = PRVM_G_FLOAT((OFS_PARM0+i*3));
1678                 PRVM_G_FLOAT(OFS_RETURN) = f;
1679         }
1680         else
1681                 PRVM_G_FLOAT(OFS_RETURN) = min(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1682 }
1683
1684 /*
1685 =================
1686 VM_max
1687
1688 returns the maximum of two supplied floats
1689
1690 float   max(float a, float b, ...[float])
1691 =================
1692 */
1693 void VM_max(prvm_prog_t *prog)
1694 {
1695         VM_SAFEPARMCOUNTRANGE(2, 8, VM_max);
1696         // LordHavoc: 3+ argument enhancement suggested by FrikaC
1697         if (prog->argc >= 3)
1698         {
1699                 int i;
1700                 float f = PRVM_G_FLOAT(OFS_PARM0);
1701                 for (i = 1;i < prog->argc;i++)
1702                         if (f < PRVM_G_FLOAT((OFS_PARM0+i*3)))
1703                                 f = PRVM_G_FLOAT((OFS_PARM0+i*3));
1704                 PRVM_G_FLOAT(OFS_RETURN) = f;
1705         }
1706         else
1707                 PRVM_G_FLOAT(OFS_RETURN) = max(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1708 }
1709
1710 /*
1711 =================
1712 VM_bound
1713
1714 returns number bounded by supplied range
1715
1716 float   bound(float min, float value, float max)
1717 =================
1718 */
1719 void VM_bound(prvm_prog_t *prog)
1720 {
1721         VM_SAFEPARMCOUNT(3,VM_bound);
1722         PRVM_G_FLOAT(OFS_RETURN) = bound(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1), PRVM_G_FLOAT(OFS_PARM2));
1723 }
1724
1725 /*
1726 =================
1727 VM_pow
1728
1729 returns a raised to power b
1730
1731 float   pow(float a, float b)
1732 =================
1733 */
1734 void VM_pow(prvm_prog_t *prog)
1735 {
1736         VM_SAFEPARMCOUNT(2,VM_pow);
1737         PRVM_G_FLOAT(OFS_RETURN) = pow(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1738 }
1739
1740 void VM_log(prvm_prog_t *prog)
1741 {
1742         VM_SAFEPARMCOUNT(1,VM_log);
1743         PRVM_G_FLOAT(OFS_RETURN) = log(PRVM_G_FLOAT(OFS_PARM0));
1744 }
1745
1746 void VM_Files_Init(prvm_prog_t *prog)
1747 {
1748         int i;
1749         for (i = 0;i < PRVM_MAX_OPENFILES;i++)
1750                 prog->openfiles[i] = NULL;
1751 }
1752
1753 void VM_Files_CloseAll(prvm_prog_t *prog)
1754 {
1755         int i;
1756         for (i = 0;i < PRVM_MAX_OPENFILES;i++)
1757         {
1758                 if (prog->openfiles[i])
1759                         FS_Close(prog->openfiles[i]);
1760                 prog->openfiles[i] = NULL;
1761         }
1762 }
1763
1764 static qfile_t *VM_GetFileHandle(prvm_prog_t *prog, int index)
1765 {
1766         if (index < 0 || index >= PRVM_MAX_OPENFILES)
1767         {
1768                 Con_Printf("VM_GetFileHandle: invalid file handle %i used in %s\n", index, prog->name);
1769                 return NULL;
1770         }
1771         if (prog->openfiles[index] == NULL)
1772         {
1773                 Con_Printf("VM_GetFileHandle: no such file handle %i (or file has been closed) in %s\n", index, prog->name);
1774                 return NULL;
1775         }
1776         return prog->openfiles[index];
1777 }
1778
1779 /*
1780 =========
1781 VM_fopen
1782
1783 float   fopen(string filename, float mode)
1784 =========
1785 */
1786 // float(string filename, float mode) fopen = #110;
1787 // opens a file inside quake/gamedir/data/ (mode is FILE_READ, FILE_APPEND, or FILE_WRITE),
1788 // returns fhandle >= 0 if successful, or fhandle < 0 if unable to open file for any reason
1789 void VM_fopen(prvm_prog_t *prog)
1790 {
1791         int filenum, mode;
1792         const char *modestring, *filename;
1793         char vabuf[1024];
1794
1795         VM_SAFEPARMCOUNT(2,VM_fopen);
1796
1797         for (filenum = 0;filenum < PRVM_MAX_OPENFILES;filenum++)
1798                 if (prog->openfiles[filenum] == NULL)
1799                         break;
1800         if (filenum >= PRVM_MAX_OPENFILES)
1801         {
1802                 PRVM_G_FLOAT(OFS_RETURN) = -2;
1803                 VM_Warning(prog, "VM_fopen: %s ran out of file handles (%i)\n", prog->name, PRVM_MAX_OPENFILES);
1804                 return;
1805         }
1806         filename = PRVM_G_STRING(OFS_PARM0);
1807         mode = (int)PRVM_G_FLOAT(OFS_PARM1);
1808         switch(mode)
1809         {
1810         case 0: // FILE_READ
1811                 modestring = "rb";
1812                 prog->openfiles[filenum] = FS_OpenVirtualFile(va(vabuf, sizeof(vabuf), "data/%s", filename), false);
1813                 if (prog->openfiles[filenum] == NULL)
1814                         prog->openfiles[filenum] = FS_OpenVirtualFile(va(vabuf, sizeof(vabuf), "%s", filename), false);
1815                 break;
1816         case 1: // FILE_APPEND
1817                 modestring = "a";
1818                 prog->openfiles[filenum] = FS_OpenRealFile(va(vabuf, sizeof(vabuf), "data/%s", filename), modestring, false);
1819                 break;
1820         case 2: // FILE_WRITE
1821                 modestring = "w";
1822                 prog->openfiles[filenum] = FS_OpenRealFile(va(vabuf, sizeof(vabuf), "data/%s", filename), modestring, false);
1823                 break;
1824         default:
1825                 PRVM_G_FLOAT(OFS_RETURN) = -3;
1826                 VM_Warning(prog, "VM_fopen: %s: no such mode %i (valid: 0 = read, 1 = append, 2 = write)\n", prog->name, mode);
1827                 return;
1828         }
1829
1830         if (prog->openfiles[filenum] == NULL)
1831         {
1832                 PRVM_G_FLOAT(OFS_RETURN) = -1;
1833                 if (developer_extra.integer)
1834                         VM_Warning(prog, "VM_fopen: %s: %s mode %s failed\n", prog->name, filename, modestring);
1835         }
1836         else
1837         {
1838                 PRVM_G_FLOAT(OFS_RETURN) = filenum;
1839                 if (developer_extra.integer)
1840                         Con_DPrintf("VM_fopen: %s: %s mode %s opened as #%i\n", prog->name, filename, modestring, filenum);
1841                 prog->openfiles_origin[filenum] = PRVM_AllocationOrigin(prog);
1842         }
1843 }
1844
1845 /*
1846 =========
1847 VM_fclose
1848
1849 fclose(float fhandle)
1850 =========
1851 */
1852 //void(float fhandle) fclose = #111; // closes a file
1853 void VM_fclose(prvm_prog_t *prog)
1854 {
1855         int filenum;
1856
1857         VM_SAFEPARMCOUNT(1,VM_fclose);
1858
1859         filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1860         if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
1861         {
1862                 VM_Warning(prog, "VM_fclose: invalid file handle %i used in %s\n", filenum, prog->name);
1863                 return;
1864         }
1865         if (prog->openfiles[filenum] == NULL)
1866         {
1867                 VM_Warning(prog, "VM_fclose: no such file handle %i (or file has been closed) in %s\n", filenum, prog->name);
1868                 return;
1869         }
1870         FS_Close(prog->openfiles[filenum]);
1871         prog->openfiles[filenum] = NULL;
1872         if(prog->openfiles_origin[filenum])
1873                 PRVM_Free((char *)prog->openfiles_origin[filenum]);
1874         if (developer_extra.integer)
1875                 Con_DPrintf("VM_fclose: %s: #%i closed\n", prog->name, filenum);
1876 }
1877
1878 /*
1879 =========
1880 VM_fgets
1881
1882 string  fgets(float fhandle)
1883 =========
1884 */
1885 //string(float fhandle) fgets = #112; // reads a line of text from the file and returns as a tempstring
1886 void VM_fgets(prvm_prog_t *prog)
1887 {
1888         int c, end;
1889         char string[VM_STRINGTEMP_LENGTH];
1890         int filenum;
1891
1892         VM_SAFEPARMCOUNT(1,VM_fgets);
1893
1894         // set the return value regardless of any possible errors
1895         PRVM_G_INT(OFS_RETURN) = OFS_NULL;
1896
1897         filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1898         if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
1899         {
1900                 VM_Warning(prog, "VM_fgets: invalid file handle %i used in %s\n", filenum, prog->name);
1901                 return;
1902         }
1903         if (prog->openfiles[filenum] == NULL)
1904         {
1905                 VM_Warning(prog, "VM_fgets: no such file handle %i (or file has been closed) in %s\n", filenum, prog->name);
1906                 return;
1907         }
1908         end = 0;
1909         for (;;)
1910         {
1911                 c = FS_Getc(prog->openfiles[filenum]);
1912                 if (c == '\r' || c == '\n' || c < 0)
1913                         break;
1914                 if (end < VM_STRINGTEMP_LENGTH - 1)
1915                         string[end++] = c;
1916         }
1917         string[end] = 0;
1918         // remove \n following \r
1919         if (c == '\r')
1920         {
1921                 c = FS_Getc(prog->openfiles[filenum]);
1922                 if (c != '\n')
1923                         FS_UnGetc(prog->openfiles[filenum], (unsigned char)c);
1924         }
1925         if (developer_extra.integer)
1926                 Con_DPrintf("fgets: %s: %s\n", prog->name, string);
1927         if (c >= 0 || end)
1928                 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, string);
1929 }
1930
1931 /*
1932 =========
1933 VM_fputs
1934
1935 fputs(float fhandle, string s)
1936 =========
1937 */
1938 //void(float fhandle, string s) fputs = #113; // writes a line of text to the end of the file
1939 void VM_fputs(prvm_prog_t *prog)
1940 {
1941         int stringlength;
1942         char string[VM_STRINGTEMP_LENGTH];
1943         int filenum;
1944
1945         VM_SAFEPARMCOUNT(2,VM_fputs);
1946
1947         filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1948         if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
1949         {
1950                 VM_Warning(prog, "VM_fputs: invalid file handle %i used in %s\n", filenum, prog->name);
1951                 return;
1952         }
1953         if (prog->openfiles[filenum] == NULL)
1954         {
1955                 VM_Warning(prog, "VM_fputs: no such file handle %i (or file has been closed) in %s\n", filenum, prog->name);
1956                 return;
1957         }
1958         VM_VarString(prog, 1, string, sizeof(string));
1959         if ((stringlength = (int)strlen(string)))
1960                 FS_Write(prog->openfiles[filenum], string, stringlength);
1961         if (developer_extra.integer)
1962                 Con_DPrintf("fputs: %s: %s\n", prog->name, string);
1963 }
1964
1965 /*
1966 =========
1967 VM_writetofile
1968
1969         writetofile(float fhandle, entity ent)
1970 =========
1971 */
1972 void VM_writetofile(prvm_prog_t *prog)
1973 {
1974         prvm_edict_t * ent;
1975         qfile_t *file;
1976
1977         VM_SAFEPARMCOUNT(2, VM_writetofile);
1978
1979         file = VM_GetFileHandle(prog, (int)PRVM_G_FLOAT(OFS_PARM0));
1980         if( !file )
1981         {
1982                 VM_Warning(prog, "VM_writetofile: invalid or closed file handle\n");
1983                 return;
1984         }
1985
1986         ent = PRVM_G_EDICT(OFS_PARM1);
1987         if(ent->priv.required->free)
1988         {
1989                 VM_Warning(prog, "VM_writetofile: %s: entity %i is free !\n", prog->name, PRVM_NUM_FOR_EDICT(ent));
1990                 return;
1991         }
1992
1993         PRVM_ED_Write (prog, file, ent);
1994 }
1995
1996 // KrimZon - DP_QC_ENTITYDATA
1997 /*
1998 =========
1999 VM_numentityfields
2000
2001 float() numentityfields
2002 Return the number of entity fields - NOT offsets
2003 =========
2004 */
2005 void VM_numentityfields(prvm_prog_t *prog)
2006 {
2007         PRVM_G_FLOAT(OFS_RETURN) = prog->numfielddefs;
2008 }
2009
2010 // KrimZon - DP_QC_ENTITYDATA
2011 /*
2012 =========
2013 VM_entityfieldname
2014
2015 string(float fieldnum) entityfieldname
2016 Return name of the specified field as a string, or empty if the field is invalid (warning)
2017 =========
2018 */
2019 void VM_entityfieldname(prvm_prog_t *prog)
2020 {
2021         ddef_t *d;
2022         int i = (int)PRVM_G_FLOAT(OFS_PARM0);
2023         
2024         if (i < 0 || i >= prog->numfielddefs)
2025         {
2026         VM_Warning(prog, "VM_entityfieldname: %s: field index out of bounds\n", prog->name);
2027         PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, "");
2028                 return;
2029         }
2030         
2031         d = &prog->fielddefs[i];
2032         PRVM_G_INT(OFS_RETURN) = d->s_name; // presuming that s_name points to a string already
2033 }
2034
2035 // KrimZon - DP_QC_ENTITYDATA
2036 /*
2037 =========
2038 VM_entityfieldtype
2039
2040 float(float fieldnum) entityfieldtype
2041 =========
2042 */
2043 void VM_entityfieldtype(prvm_prog_t *prog)
2044 {
2045         ddef_t *d;
2046         int i = (int)PRVM_G_FLOAT(OFS_PARM0);
2047         
2048         if (i < 0 || i >= prog->numfielddefs)
2049         {
2050                 VM_Warning(prog, "VM_entityfieldtype: %s: field index out of bounds\n", prog->name);
2051                 PRVM_G_FLOAT(OFS_RETURN) = -1.0;
2052                 return;
2053         }
2054         
2055         d = &prog->fielddefs[i];
2056         PRVM_G_FLOAT(OFS_RETURN) = (float)d->type;
2057 }
2058
2059 // KrimZon - DP_QC_ENTITYDATA
2060 /*
2061 =========
2062 VM_getentityfieldstring
2063
2064 string(float fieldnum, entity ent) getentityfieldstring
2065 =========
2066 */
2067 void VM_getentityfieldstring(prvm_prog_t *prog)
2068 {
2069         // put the data into a string
2070         ddef_t *d;
2071         int type, j;
2072         int *v;
2073         prvm_edict_t * ent;
2074         int i = (int)PRVM_G_FLOAT(OFS_PARM0);
2075         char valuebuf[MAX_INPUTLINE];
2076         
2077         if (i < 0 || i >= prog->numfielddefs)
2078         {
2079         VM_Warning(prog, "VM_entityfielddata: %s: field index out of bounds\n", prog->name);
2080                 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, "");
2081                 return;
2082         }
2083         
2084         d = &prog->fielddefs[i];
2085         
2086         // get the entity
2087         ent = PRVM_G_EDICT(OFS_PARM1);
2088         if(ent->priv.required->free)
2089         {
2090                 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, "");
2091                 VM_Warning(prog, "VM_entityfielddata: %s: entity %i is free !\n", prog->name, PRVM_NUM_FOR_EDICT(ent));
2092                 return;
2093         }
2094         v = (int *)((char *)ent->fields.vp + d->ofs*4);
2095         
2096         // if it's 0 or blank, return an empty string
2097         type = d->type & ~DEF_SAVEGLOBAL;
2098         for (j=0 ; j<prvm_type_size[type] ; j++)
2099                 if (v[j])
2100                         break;
2101         if (j == prvm_type_size[type])
2102         {
2103                 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, "");
2104                 return;
2105         }
2106                 
2107         PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, PRVM_UglyValueString(prog, (etype_t)d->type, (prvm_eval_t *)v, valuebuf, sizeof(valuebuf)));
2108 }
2109
2110 // KrimZon - DP_QC_ENTITYDATA
2111 /*
2112 =========
2113 VM_putentityfieldstring
2114
2115 float(float fieldnum, entity ent, string s) putentityfieldstring
2116 =========
2117 */
2118 void VM_putentityfieldstring(prvm_prog_t *prog)
2119 {
2120         ddef_t *d;
2121         prvm_edict_t * ent;
2122         int i = (int)PRVM_G_FLOAT(OFS_PARM0);
2123
2124         if (i < 0 || i >= prog->numfielddefs)
2125         {
2126         VM_Warning(prog, "VM_entityfielddata: %s: field index out of bounds\n", prog->name);
2127                 PRVM_G_FLOAT(OFS_RETURN) = 0.0f;
2128                 return;
2129         }
2130
2131         d = &prog->fielddefs[i];
2132
2133         // get the entity
2134         ent = PRVM_G_EDICT(OFS_PARM1);
2135         if(ent->priv.required->free)
2136         {
2137                 VM_Warning(prog, "VM_entityfielddata: %s: entity %i is free !\n", prog->name, PRVM_NUM_FOR_EDICT(ent));
2138                 PRVM_G_FLOAT(OFS_RETURN) = 0.0f;
2139                 return;
2140         }
2141
2142         // parse the string into the value
2143         PRVM_G_FLOAT(OFS_RETURN) = ( PRVM_ED_ParseEpair(prog, ent, d, PRVM_G_STRING(OFS_PARM2), false) ) ? 1.0f : 0.0f;
2144 }
2145
2146 /*
2147 =========
2148 VM_strlen
2149
2150 float   strlen(string s)
2151 =========
2152 */
2153 //float(string s) strlen = #114; // returns how many characters are in a string
2154 void VM_strlen(prvm_prog_t *prog)
2155 {
2156         VM_SAFEPARMCOUNT(1,VM_strlen);
2157
2158         //PRVM_G_FLOAT(OFS_RETURN) = strlen(PRVM_G_STRING(OFS_PARM0));
2159         PRVM_G_FLOAT(OFS_RETURN) = u8_strlen(PRVM_G_STRING(OFS_PARM0));
2160 }
2161
2162 // DRESK - Decolorized String
2163 /*
2164 =========
2165 VM_strdecolorize
2166
2167 string  strdecolorize(string s)
2168 =========
2169 */
2170 // string (string s) strdecolorize = #472; // returns the passed in string with color codes stripped
2171 void VM_strdecolorize(prvm_prog_t *prog)
2172 {
2173         char szNewString[VM_STRINGTEMP_LENGTH];
2174         const char *szString;
2175
2176         // Prepare Strings
2177         VM_SAFEPARMCOUNT(1,VM_strdecolorize);
2178         szString = PRVM_G_STRING(OFS_PARM0);
2179         COM_StringDecolorize(szString, 0, szNewString, sizeof(szNewString), TRUE);
2180         PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, szNewString);
2181 }
2182
2183 // DRESK - String Length (not counting color codes)
2184 /*
2185 =========
2186 VM_strlennocol
2187
2188 float   strlennocol(string s)
2189 =========
2190 */
2191 // float(string s) strlennocol = #471; // returns how many characters are in a string not including color codes
2192 // For example, ^2Dresk returns a length of 5
2193 void VM_strlennocol(prvm_prog_t *prog)
2194 {
2195         const char *szString;
2196         int nCnt;
2197
2198         VM_SAFEPARMCOUNT(1,VM_strlennocol);
2199
2200         szString = PRVM_G_STRING(OFS_PARM0);
2201
2202         //nCnt = COM_StringLengthNoColors(szString, 0, NULL);
2203         nCnt = u8_COM_StringLengthNoColors(szString, 0, NULL);
2204
2205         PRVM_G_FLOAT(OFS_RETURN) = nCnt;
2206 }
2207
2208 // DRESK - String to Uppercase and Lowercase
2209 /*
2210 =========
2211 VM_strtolower
2212
2213 string  strtolower(string s)
2214 =========
2215 */
2216 // string (string s) strtolower = #480; // returns passed in string in lowercase form
2217 void VM_strtolower(prvm_prog_t *prog)
2218 {
2219         char szNewString[VM_STRINGTEMP_LENGTH];
2220         const char *szString;
2221
2222         // Prepare Strings
2223         VM_SAFEPARMCOUNT(1,VM_strtolower);
2224         szString = PRVM_G_STRING(OFS_PARM0);
2225
2226         COM_ToLowerString(szString, szNewString, sizeof(szNewString) );
2227
2228         PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, szNewString);
2229 }
2230
2231 /*
2232 =========
2233 VM_strtoupper
2234
2235 string  strtoupper(string s)
2236 =========
2237 */
2238 // string (string s) strtoupper = #481; // returns passed in string in uppercase form
2239 void VM_strtoupper(prvm_prog_t *prog)
2240 {
2241         char szNewString[VM_STRINGTEMP_LENGTH];
2242         const char *szString;
2243
2244         // Prepare Strings
2245         VM_SAFEPARMCOUNT(1,VM_strtoupper);
2246         szString = PRVM_G_STRING(OFS_PARM0);
2247
2248         COM_ToUpperString(szString, szNewString, sizeof(szNewString) );
2249
2250         PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, szNewString);
2251 }
2252
2253 /*
2254 =========
2255 VM_strcat
2256
2257 string strcat(string,string,...[string])
2258 =========
2259 */
2260 //string(string s1, string s2) strcat = #115;
2261 // concatenates two strings (for example "abc", "def" would return "abcdef")
2262 // and returns as a tempstring
2263 void VM_strcat(prvm_prog_t *prog)
2264 {
2265         char s[VM_STRINGTEMP_LENGTH];
2266         VM_SAFEPARMCOUNTRANGE(1, 8, VM_strcat);
2267
2268         VM_VarString(prog, 0, s, sizeof(s));
2269         PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, s);
2270 }
2271
2272 /*
2273 =========
2274 VM_substring
2275
2276 string  substring(string s, float start, float length)
2277 =========
2278 */
2279 // string(string s, float start, float length) substring = #116;
2280 // returns a section of a string as a tempstring
2281 void VM_substring(prvm_prog_t *prog)
2282 {
2283         int start, length;
2284         int u_slength = 0, u_start;
2285         size_t u_length;
2286         const char *s;
2287         char string[VM_STRINGTEMP_LENGTH];
2288
2289         VM_SAFEPARMCOUNT(3,VM_substring);
2290
2291         /*
2292         s = PRVM_G_STRING(OFS_PARM0);
2293         start = (int)PRVM_G_FLOAT(OFS_PARM1);
2294         length = (int)PRVM_G_FLOAT(OFS_PARM2);
2295         slength = strlen(s);
2296
2297         if (start < 0) // FTE_STRINGS feature
2298                 start += slength;
2299         start = bound(0, start, slength);
2300
2301         if (length < 0) // FTE_STRINGS feature
2302                 length += slength - start + 1;
2303         maxlen = min((int)sizeof(string) - 1, slength - start);
2304         length = bound(0, length, maxlen);
2305
2306         memcpy(string, s + start, length);
2307         string[length] = 0;
2308         PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, string);
2309         */
2310         
2311         s = PRVM_G_STRING(OFS_PARM0);
2312         start = (int)PRVM_G_FLOAT(OFS_PARM1);
2313         length = (int)PRVM_G_FLOAT(OFS_PARM2);
2314
2315         if (start < 0) // FTE_STRINGS feature
2316         {
2317                 u_slength = u8_strlen(s);
2318                 start += u_slength;
2319                 start = bound(0, start, u_slength);
2320         }
2321
2322         if (length < 0) // FTE_STRINGS feature
2323         {
2324                 if (!u_slength) // it's not calculated when it's not needed above
2325                         u_slength = u8_strlen(s);
2326                 length += u_slength - start + 1;
2327         }
2328                 
2329         // positive start, positive length
2330         u_start = u8_byteofs(s, start, NULL);
2331         if (u_start < 0)
2332         {
2333                 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, "");
2334                 return;
2335         }
2336         u_length = u8_bytelen(s + u_start, length);
2337         if (u_length >= sizeof(string)-1)
2338                 u_length = sizeof(string)-1;
2339         
2340         memcpy(string, s + u_start, u_length);
2341         string[u_length] = 0;
2342         PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, string);
2343 }
2344
2345 /*
2346 =========
2347 VM_strreplace
2348
2349 string(string search, string replace, string subject) strreplace = #484;
2350 =========
2351 */
2352 // replaces all occurrences of search with replace in the string subject, and returns the result
2353 void VM_strreplace(prvm_prog_t *prog)
2354 {
2355         int i, j, si;
2356         const char *search, *replace, *subject;
2357         char string[VM_STRINGTEMP_LENGTH];
2358         int search_len, replace_len, subject_len;
2359
2360         VM_SAFEPARMCOUNT(3,VM_strreplace);
2361
2362         search = PRVM_G_STRING(OFS_PARM0);
2363         replace = PRVM_G_STRING(OFS_PARM1);
2364         subject = PRVM_G_STRING(OFS_PARM2);
2365
2366         search_len = (int)strlen(search);
2367         replace_len = (int)strlen(replace);
2368         subject_len = (int)strlen(subject);
2369
2370         si = 0;
2371         for (i = 0; i <= subject_len - search_len; i++)
2372         {
2373                 for (j = 0; j < search_len; j++) // thus, i+j < subject_len
2374                         if (subject[i+j] != search[j])
2375                                 break;
2376                 if (j == search_len)
2377                 {
2378                         // NOTE: if search_len == 0, we always hit THIS case, and never the other
2379                         // found it at offset 'i'
2380                         for (j = 0; j < replace_len && si < (int)sizeof(string) - 1; j++)
2381                                 string[si++] = replace[j];
2382                         if(search_len > 0)
2383                         {
2384                                 i += search_len - 1;
2385                         }
2386                         else
2387                         {
2388                                 // the above would subtract 1 from i... so we
2389                                 // don't do that, but instead output the next
2390                                 // char
2391                                 if (si < (int)sizeof(string) - 1)
2392                                         string[si++] = subject[i];
2393                         }
2394                 }
2395                 else
2396                 {
2397                         // in THIS case, we know search_len > 0, thus i < subject_len
2398                         // not found
2399                         if (si < (int)sizeof(string) - 1)
2400                                 string[si++] = subject[i];
2401                 }
2402         }
2403         // remaining chars (these cannot match)
2404         for (; i < subject_len; i++)
2405                 if (si < (int)sizeof(string) - 1)
2406                         string[si++] = subject[i];
2407         string[si] = '\0';
2408
2409         PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, string);
2410 }
2411
2412 /*
2413 =========
2414 VM_strireplace
2415
2416 string(string search, string replace, string subject) strireplace = #485;
2417 =========
2418 */
2419 // case-insensitive version of strreplace
2420 void VM_strireplace(prvm_prog_t *prog)
2421 {
2422         int i, j, si;
2423         const char *search, *replace, *subject;
2424         char string[VM_STRINGTEMP_LENGTH];
2425         int search_len, replace_len, subject_len;
2426
2427         VM_SAFEPARMCOUNT(3,VM_strreplace);
2428
2429         search = PRVM_G_STRING(OFS_PARM0);
2430         replace = PRVM_G_STRING(OFS_PARM1);
2431         subject = PRVM_G_STRING(OFS_PARM2);
2432
2433         search_len = (int)strlen(search);
2434         replace_len = (int)strlen(replace);
2435         subject_len = (int)strlen(subject);
2436
2437         si = 0;
2438         for (i = 0; i <= subject_len - search_len; i++)
2439         {
2440                 for (j = 0; j < search_len; j++) // thus, i+j < subject_len
2441                         if (tolower(subject[i+j]) != tolower(search[j]))
2442                                 break;
2443                 if (j == search_len)
2444                 {
2445                         // NOTE: if search_len == 0, we always hit THIS case, and never the other
2446                         // found it at offset 'i'
2447                         for (j = 0; j < replace_len && si < (int)sizeof(string) - 1; j++)
2448                                 string[si++] = replace[j];
2449                         if(search_len > 0)
2450                         {
2451                                 i += search_len - 1;
2452                         }
2453                         else
2454                         {
2455                                 // the above would subtract 1 from i... so we
2456                                 // don't do that, but instead output the next
2457                                 // char
2458                                 if (si < (int)sizeof(string) - 1)
2459                                         string[si++] = subject[i];
2460                         }
2461                 }
2462                 else
2463                 {
2464                         // in THIS case, we know search_len > 0, thus i < subject_len
2465                         // not found
2466                         if (si < (int)sizeof(string) - 1)
2467                                 string[si++] = subject[i];
2468                 }
2469         }
2470         // remaining chars (these cannot match)
2471         for (; i < subject_len; i++)
2472                 if (si < (int)sizeof(string) - 1)
2473                         string[si++] = subject[i];
2474         string[si] = '\0';
2475
2476         PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, string);
2477 }
2478
2479 /*
2480 =========
2481 VM_stov
2482
2483 vector  stov(string s)
2484 =========
2485 */
2486 //vector(string s) stov = #117; // returns vector value from a string
2487 void VM_stov(prvm_prog_t *prog)
2488 {
2489         char string[VM_STRINGTEMP_LENGTH];
2490
2491         VM_SAFEPARMCOUNT(1,VM_stov);
2492
2493         VM_VarString(prog, 0, string, sizeof(string));
2494         Math_atov(string, PRVM_G_VECTOR(OFS_RETURN));
2495 }
2496
2497 /*
2498 =========
2499 VM_strzone
2500
2501 string  strzone(string s)
2502 =========
2503 */
2504 //string(string s, ...) strzone = #118; // makes a copy of a string into the string zone and returns it, this is often used to keep around a tempstring for longer periods of time (tempstrings are replaced often)
2505 void VM_strzone(prvm_prog_t *prog)
2506 {
2507         char *out;
2508         char string[VM_STRINGTEMP_LENGTH];
2509         size_t alloclen;
2510
2511         VM_SAFEPARMCOUNT(1,VM_strzone);
2512
2513         VM_VarString(prog, 0, string, sizeof(string));
2514         alloclen = strlen(string) + 1;
2515         PRVM_G_INT(OFS_RETURN) = PRVM_AllocString(prog, alloclen, &out);
2516         memcpy(out, string, alloclen);
2517 }
2518
2519 /*
2520 =========
2521 VM_strunzone
2522
2523 strunzone(string s)
2524 =========
2525 */
2526 //void(string s) strunzone = #119; // removes a copy of a string from the string zone (you can not use that string again or it may crash!!!)
2527 void VM_strunzone(prvm_prog_t *prog)
2528 {
2529         VM_SAFEPARMCOUNT(1,VM_strunzone);
2530         PRVM_FreeString(prog, PRVM_G_INT(OFS_PARM0));
2531 }
2532
2533 /*
2534 =========
2535 VM_command (used by client and menu)
2536
2537 clientcommand(float client, string s) (for client and menu)
2538 =========
2539 */
2540 //void(entity e, string s) clientcommand = #440; // executes a command string as if it came from the specified client
2541 //this function originally written by KrimZon, made shorter by LordHavoc
2542 void VM_clcommand (prvm_prog_t *prog)
2543 {
2544         client_t *temp_client;
2545         int i;
2546
2547         VM_SAFEPARMCOUNT(2,VM_clcommand);
2548
2549         i = (int)PRVM_G_FLOAT(OFS_PARM0);
2550         if (!sv.active  || i < 0 || i >= svs.maxclients || !svs.clients[i].active)
2551         {
2552                 VM_Warning(prog, "VM_clientcommand: %s: invalid client/server is not active !\n", prog->name);
2553                 return;
2554         }
2555
2556         temp_client = host_client;
2557         host_client = svs.clients + i;
2558         Cmd_ExecuteString (PRVM_G_STRING(OFS_PARM1), src_client, true);
2559         host_client = temp_client;
2560 }
2561
2562
2563 /*
2564 =========
2565 VM_tokenize
2566
2567 float tokenize(string s)
2568 =========
2569 */
2570 //float(string s) tokenize = #441; // takes apart a string into individal words (access them with argv), returns how many
2571 //this function originally written by KrimZon, made shorter by LordHavoc
2572 //20040203: rewritten by LordHavoc (no longer uses allocations)
2573 static int num_tokens = 0;
2574 static int tokens[VM_STRINGTEMP_LENGTH / 2];
2575 static int tokens_startpos[VM_STRINGTEMP_LENGTH / 2];
2576 static int tokens_endpos[VM_STRINGTEMP_LENGTH / 2];
2577 static char tokenize_string[VM_STRINGTEMP_LENGTH];
2578 void VM_tokenize (prvm_prog_t *prog)
2579 {
2580         const char *p;
2581
2582         VM_SAFEPARMCOUNT(1,VM_tokenize);
2583
2584         strlcpy(tokenize_string, PRVM_G_STRING(OFS_PARM0), sizeof(tokenize_string));
2585         p = tokenize_string;
2586
2587         num_tokens = 0;
2588         for(;;)
2589         {
2590                 if (num_tokens >= (int)(sizeof(tokens)/sizeof(tokens[0])))
2591                         break;
2592
2593                 // skip whitespace here to find token start pos
2594                 while(*p && ISWHITESPACE(*p))
2595                         ++p;
2596
2597                 tokens_startpos[num_tokens] = p - tokenize_string;
2598                 if(!COM_ParseToken_VM_Tokenize(&p, false))
2599                         break;
2600                 tokens_endpos[num_tokens] = p - tokenize_string;
2601                 tokens[num_tokens] = PRVM_SetTempString(prog, com_token);
2602                 ++num_tokens;
2603         }
2604
2605         PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
2606 }
2607
2608 //float(string s) tokenize = #514; // takes apart a string into individal words (access them with argv), returns how many
2609 void VM_tokenize_console (prvm_prog_t *prog)
2610 {
2611         const char *p;
2612
2613         VM_SAFEPARMCOUNT(1,VM_tokenize);
2614
2615         strlcpy(tokenize_string, PRVM_G_STRING(OFS_PARM0), sizeof(tokenize_string));
2616         p = tokenize_string;
2617
2618         num_tokens = 0;
2619         for(;;)
2620         {
2621                 if (num_tokens >= (int)(sizeof(tokens)/sizeof(tokens[0])))
2622                         break;
2623
2624                 // skip whitespace here to find token start pos
2625                 while(*p && ISWHITESPACE(*p))
2626                         ++p;
2627
2628                 tokens_startpos[num_tokens] = p - tokenize_string;
2629                 if(!COM_ParseToken_Console(&p))
2630                         break;
2631                 tokens_endpos[num_tokens] = p - tokenize_string;
2632                 tokens[num_tokens] = PRVM_SetTempString(prog, com_token);
2633                 ++num_tokens;
2634         }
2635
2636         PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
2637 }
2638
2639 /*
2640 =========
2641 VM_tokenizebyseparator
2642
2643 float tokenizebyseparator(string s, string separator1, ...)
2644 =========
2645 */
2646 //float(string s, string separator1, ...) tokenizebyseparator = #479; // takes apart a string into individal words (access them with argv), returns how many
2647 //this function returns the token preceding each instance of a separator (of
2648 //which there can be multiple), and the text following the last separator
2649 //useful for parsing certain kinds of data like IP addresses
2650 //example:
2651 //numnumbers = tokenizebyseparator("10.1.2.3", ".");
2652 //returns 4 and the tokens "10" "1" "2" "3".
2653 void VM_tokenizebyseparator (prvm_prog_t *prog)
2654 {
2655         int j, k;
2656         int numseparators;
2657         int separatorlen[7];
2658         const char *separators[7];
2659         const char *p, *p0;
2660         const char *token;
2661         char tokentext[MAX_INPUTLINE];
2662
2663         VM_SAFEPARMCOUNTRANGE(2, 8,VM_tokenizebyseparator);
2664
2665         strlcpy(tokenize_string, PRVM_G_STRING(OFS_PARM0), sizeof(tokenize_string));
2666         p = tokenize_string;
2667
2668         numseparators = 0;
2669         for (j = 1;j < prog->argc;j++)
2670         {
2671                 // skip any blank separator strings
2672                 const char *s = PRVM_G_STRING(OFS_PARM0+j*3);
2673                 if (!s[0])
2674                         continue;
2675                 separators[numseparators] = s;
2676                 separatorlen[numseparators] = strlen(s);
2677                 numseparators++;
2678         }
2679
2680         num_tokens = 0;
2681         j = 0;
2682
2683         while (num_tokens < (int)(sizeof(tokens)/sizeof(tokens[0])))
2684         {
2685                 token = tokentext + j;
2686                 tokens_startpos[num_tokens] = p - tokenize_string;
2687                 p0 = p;
2688                 while (*p)
2689                 {
2690                         for (k = 0;k < numseparators;k++)
2691                         {
2692                                 if (!strncmp(p, separators[k], separatorlen[k]))
2693                                 {
2694                                         p += separatorlen[k];
2695                                         break;
2696                                 }
2697                         }
2698                         if (k < numseparators)
2699                                 break;
2700                         if (j < (int)sizeof(tokentext)-1)
2701                                 tokentext[j++] = *p;
2702                         p++;
2703                         p0 = p;
2704                 }
2705                 tokens_endpos[num_tokens] = p0 - tokenize_string;
2706                 if (j >= (int)sizeof(tokentext))
2707                         break;
2708                 tokentext[j++] = 0;
2709                 tokens[num_tokens++] = PRVM_SetTempString(prog, token);
2710                 if (!*p)
2711                         break;
2712         }
2713
2714         PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
2715 }
2716
2717 //string(float n) argv = #442; // returns a word from the tokenized string (returns nothing for an invalid index)
2718 //this function originally written by KrimZon, made shorter by LordHavoc
2719 void VM_argv (prvm_prog_t *prog)
2720 {
2721         int token_num;
2722
2723         VM_SAFEPARMCOUNT(1,VM_argv);
2724
2725         token_num = (int)PRVM_G_FLOAT(OFS_PARM0);
2726
2727         if(token_num < 0)
2728                 token_num += num_tokens;
2729
2730         if (token_num >= 0 && token_num < num_tokens)
2731                 PRVM_G_INT(OFS_RETURN) = tokens[token_num];
2732         else
2733                 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
2734 }
2735
2736 //float(float n) argv_start_index = #515; // returns the start index of a token
2737 void VM_argv_start_index (prvm_prog_t *prog)
2738 {
2739         int token_num;
2740
2741         VM_SAFEPARMCOUNT(1,VM_argv);
2742
2743         token_num = (int)PRVM_G_FLOAT(OFS_PARM0);
2744
2745         if(token_num < 0)
2746                 token_num += num_tokens;
2747
2748         if (token_num >= 0 && token_num < num_tokens)
2749                 PRVM_G_FLOAT(OFS_RETURN) = tokens_startpos[token_num];
2750         else
2751                 PRVM_G_FLOAT(OFS_RETURN) = -1;
2752 }
2753
2754 //float(float n) argv_end_index = #516; // returns the end index of a token
2755 void VM_argv_end_index (prvm_prog_t *prog)
2756 {
2757         int token_num;
2758
2759         VM_SAFEPARMCOUNT(1,VM_argv);
2760
2761         token_num = (int)PRVM_G_FLOAT(OFS_PARM0);
2762
2763         if(token_num < 0)
2764                 token_num += num_tokens;
2765
2766         if (token_num >= 0 && token_num < num_tokens)
2767                 PRVM_G_FLOAT(OFS_RETURN) = tokens_endpos[token_num];
2768         else
2769                 PRVM_G_FLOAT(OFS_RETURN) = -1;
2770 }
2771
2772 /*
2773 =========
2774 VM_isserver
2775
2776 float   isserver()
2777 =========
2778 */
2779 void VM_isserver(prvm_prog_t *prog)
2780 {
2781         VM_SAFEPARMCOUNT(0,VM_serverstate);
2782
2783         PRVM_G_FLOAT(OFS_RETURN) = sv.active && (svs.maxclients > 1 || cls.state == ca_dedicated);
2784 }
2785
2786 /*
2787 =========
2788 VM_clientcount
2789
2790 float   clientcount()
2791 =========
2792 */
2793 void VM_clientcount(prvm_prog_t *prog)
2794 {
2795         VM_SAFEPARMCOUNT(0,VM_clientcount);
2796
2797         PRVM_G_FLOAT(OFS_RETURN) = svs.maxclients;
2798 }
2799
2800 /*
2801 =========
2802 VM_clientstate
2803
2804 float   clientstate()
2805 =========
2806 */
2807 void VM_clientstate(prvm_prog_t *prog)
2808 {
2809         VM_SAFEPARMCOUNT(0,VM_clientstate);
2810
2811
2812         switch( cls.state ) {
2813                 case ca_uninitialized:
2814                 case ca_dedicated:
2815                         PRVM_G_FLOAT(OFS_RETURN) = 0;
2816                         break;
2817                 case ca_disconnected:
2818                         PRVM_G_FLOAT(OFS_RETURN) = 1;
2819                         break;
2820                 case ca_connected:
2821                         PRVM_G_FLOAT(OFS_RETURN) = 2;
2822                         break;
2823                 default:
2824                         // should never be reached!
2825                         break;
2826         }
2827 }
2828
2829 /*
2830 =========
2831 VM_getostype
2832
2833 float   getostype(prvm_prog_t *prog)
2834 =========
2835 */ // not used at the moment -> not included in the common list
2836 void VM_getostype(prvm_prog_t *prog)
2837 {
2838         VM_SAFEPARMCOUNT(0,VM_getostype);
2839
2840         /*
2841         OS_WINDOWS
2842         OS_LINUX
2843         OS_MAC - not supported
2844         */
2845
2846 #ifdef WIN32
2847         PRVM_G_FLOAT(OFS_RETURN) = 0;
2848 #elif defined(MACOSX)
2849         PRVM_G_FLOAT(OFS_RETURN) = 2;
2850 #else
2851         PRVM_G_FLOAT(OFS_RETURN) = 1;
2852 #endif
2853 }
2854
2855 /*
2856 =========
2857 VM_gettime
2858
2859 float   gettime(prvm_prog_t *prog)
2860 =========
2861 */
2862 float CDAudio_GetPosition(void);
2863 void VM_gettime(prvm_prog_t *prog)
2864 {
2865         int timer_index;
2866
2867         VM_SAFEPARMCOUNTRANGE(0,1,VM_gettime);
2868
2869         if(prog->argc == 0)
2870         {
2871                 PRVM_G_FLOAT(OFS_RETURN) = (float) realtime;
2872         }
2873         else
2874         {
2875                 timer_index = (int) PRVM_G_FLOAT(OFS_PARM0);
2876                 switch(timer_index)
2877                 {
2878                         case 0: // GETTIME_FRAMESTART
2879                                 PRVM_G_FLOAT(OFS_RETURN) = realtime;
2880                                 break;
2881                         case 1: // GETTIME_REALTIME
2882                                 PRVM_G_FLOAT(OFS_RETURN) = Sys_DirtyTime();
2883                                 break;
2884                         case 2: // GETTIME_HIRES
2885                                 PRVM_G_FLOAT(OFS_RETURN) = (Sys_DirtyTime() - host_dirtytime);
2886                                 break;
2887                         case 3: // GETTIME_UPTIME
2888                                 PRVM_G_FLOAT(OFS_RETURN) = realtime;
2889                                 break;
2890                         case 4: // GETTIME_CDTRACK
2891                                 PRVM_G_FLOAT(OFS_RETURN) = CDAudio_GetPosition();
2892                                 break;
2893                         default:
2894                                 VM_Warning(prog, "VM_gettime: %s: unsupported timer specified, returning realtime\n", prog->name);
2895                                 PRVM_G_FLOAT(OFS_RETURN) = realtime;
2896                                 break;
2897                 }
2898         }
2899 }
2900
2901 /*
2902 =========
2903 VM_getsoundtime
2904
2905 float   getsoundtime(prvm_prog_t *prog)
2906 =========
2907 */
2908
2909 void VM_getsoundtime (prvm_prog_t *prog)
2910 {
2911         int entnum, entchannel;
2912         VM_SAFEPARMCOUNT(2,VM_getsoundtime);
2913
2914         if (prog == SVVM_prog)
2915                 entnum = PRVM_NUM_FOR_EDICT(PRVM_G_EDICT(OFS_PARM0));
2916         else if (prog == CLVM_prog)
2917                 entnum = MAX_EDICTS + PRVM_NUM_FOR_EDICT(PRVM_G_EDICT(OFS_PARM0));
2918         else
2919         {
2920                 VM_Warning(prog, "VM_getsoundtime: %s: not supported on this progs\n", prog->name);
2921                 PRVM_G_FLOAT(OFS_RETURN) = -1;
2922                 return;
2923         }
2924         entchannel = (int)PRVM_G_FLOAT(OFS_PARM1);
2925         entchannel = CHAN_USER2ENGINE(entchannel);
2926         if (!IS_CHAN(entchannel))
2927                 VM_Warning(prog, "VM_getsoundtime: %s: bad channel %i\n", prog->name, entchannel);
2928         PRVM_G_FLOAT(OFS_RETURN) = (float)S_GetEntChannelPosition(entnum, entchannel);
2929 }
2930
2931 /*
2932 =========
2933 VM_GetSoundLen
2934
2935 string  soundlength (string sample)
2936 =========
2937 */
2938 void VM_soundlength (prvm_prog_t *prog)
2939 {
2940         const char *s;
2941
2942         VM_SAFEPARMCOUNT(1, VM_soundlength);
2943
2944         s = PRVM_G_STRING(OFS_PARM0);
2945         PRVM_G_FLOAT(OFS_RETURN) = S_SoundLength(s);
2946 }
2947
2948 /*
2949 =========
2950 VM_loadfromdata
2951
2952 loadfromdata(string data)
2953 =========
2954 */
2955 void VM_loadfromdata(prvm_prog_t *prog)
2956 {
2957         VM_SAFEPARMCOUNT(1,VM_loadentsfromfile);
2958
2959         PRVM_ED_LoadFromFile(prog, PRVM_G_STRING(OFS_PARM0));
2960 }
2961
2962 /*
2963 ========================
2964 VM_parseentitydata
2965
2966 parseentitydata(entity ent, string data)
2967 ========================
2968 */
2969 void VM_parseentitydata(prvm_prog_t *prog)
2970 {
2971         prvm_edict_t *ent;
2972         const char *data;
2973
2974         VM_SAFEPARMCOUNT(2, VM_parseentitydata);
2975
2976         // get edict and test it
2977         ent = PRVM_G_EDICT(OFS_PARM0);
2978         if (ent->priv.required->free)
2979                 prog->error_cmd("VM_parseentitydata: %s: Can only set already spawned entities (entity %i is free)!", prog->name, PRVM_NUM_FOR_EDICT(ent));
2980
2981         data = PRVM_G_STRING(OFS_PARM1);
2982
2983         // parse the opening brace
2984         if (!COM_ParseToken_Simple(&data, false, false, true) || com_token[0] != '{' )
2985                 prog->error_cmd("VM_parseentitydata: %s: Couldn't parse entity data:\n%s", prog->name, data );
2986
2987         PRVM_ED_ParseEdict (prog, data, ent);
2988 }
2989
2990 /*
2991 =========
2992 VM_loadfromfile
2993
2994 loadfromfile(string file)
2995 =========
2996 */
2997 void VM_loadfromfile(prvm_prog_t *prog)
2998 {
2999         const char *filename;
3000         char *data;
3001
3002         VM_SAFEPARMCOUNT(1,VM_loadfromfile);
3003
3004         filename = PRVM_G_STRING(OFS_PARM0);
3005         if (FS_CheckNastyPath(filename, false))
3006         {
3007                 PRVM_G_FLOAT(OFS_RETURN) = -4;
3008                 VM_Warning(prog, "VM_loadfromfile: %s dangerous or non-portable filename \"%s\" not allowed. (contains : or \\ or begins with .. or /)\n", prog->name, filename);
3009                 return;
3010         }
3011
3012         // not conform with VM_fopen
3013         data = (char *)FS_LoadFile(filename, tempmempool, false, NULL);
3014         if (data == NULL)
3015                 PRVM_G_FLOAT(OFS_RETURN) = -1;
3016
3017         PRVM_ED_LoadFromFile(prog, data);
3018
3019         if(data)
3020                 Mem_Free(data);
3021 }
3022
3023
3024 /*
3025 =========
3026 VM_modulo
3027
3028 float   mod(float val, float m)
3029 =========
3030 */
3031 void VM_modulo(prvm_prog_t *prog)
3032 {
3033         int val, m;
3034         VM_SAFEPARMCOUNT(2,VM_module);
3035
3036         val = (int) PRVM_G_FLOAT(OFS_PARM0);
3037         m       = (int) PRVM_G_FLOAT(OFS_PARM1);
3038
3039         PRVM_G_FLOAT(OFS_RETURN) = (float) (val % m);
3040 }
3041
3042 static void VM_Search_Init(prvm_prog_t *prog)
3043 {
3044         int i;
3045         for (i = 0;i < PRVM_MAX_OPENSEARCHES;i++)
3046                 prog->opensearches[i] = NULL;
3047 }
3048
3049 static void VM_Search_Reset(prvm_prog_t *prog)
3050 {
3051         int i;
3052         // reset the fssearch list
3053         for(i = 0; i < PRVM_MAX_OPENSEARCHES; i++)
3054         {
3055                 if(prog->opensearches[i])
3056                         FS_FreeSearch(prog->opensearches[i]);
3057                 prog->opensearches[i] = NULL;
3058         }
3059 }
3060
3061 /*
3062 =========
3063 VM_search_begin
3064
3065 float search_begin(string pattern, float caseinsensitive, float quiet)
3066 =========
3067 */
3068 void VM_search_begin(prvm_prog_t *prog)
3069 {
3070         int handle;
3071         const char *pattern;
3072         int caseinsens, quiet;
3073
3074         VM_SAFEPARMCOUNT(3, VM_search_begin);
3075
3076         pattern = PRVM_G_STRING(OFS_PARM0);
3077
3078         VM_CheckEmptyString(prog, pattern);
3079
3080         caseinsens = (int)PRVM_G_FLOAT(OFS_PARM1);
3081         quiet = (int)PRVM_G_FLOAT(OFS_PARM2);
3082
3083         for(handle = 0; handle < PRVM_MAX_OPENSEARCHES; handle++)
3084                 if(!prog->opensearches[handle])
3085                         break;
3086
3087         if(handle >= PRVM_MAX_OPENSEARCHES)
3088         {
3089                 PRVM_G_FLOAT(OFS_RETURN) = -2;
3090                 VM_Warning(prog, "VM_search_begin: %s ran out of search handles (%i)\n", prog->name, PRVM_MAX_OPENSEARCHES);
3091                 return;
3092         }
3093
3094         if(!(prog->opensearches[handle] = FS_Search(pattern,caseinsens, quiet)))
3095                 PRVM_G_FLOAT(OFS_RETURN) = -1;
3096         else
3097         {
3098                 prog->opensearches_origin[handle] = PRVM_AllocationOrigin(prog);
3099                 PRVM_G_FLOAT(OFS_RETURN) = handle;
3100         }
3101 }
3102
3103 /*
3104 =========
3105 VM_search_end
3106
3107 void    search_end(float handle)
3108 =========
3109 */
3110 void VM_search_end(prvm_prog_t *prog)
3111 {
3112         int handle;
3113         VM_SAFEPARMCOUNT(1, VM_search_end);
3114
3115         handle = (int)PRVM_G_FLOAT(OFS_PARM0);
3116
3117         if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
3118         {
3119                 VM_Warning(prog, "VM_search_end: invalid handle %i used in %s\n", handle, prog->name);
3120                 return;
3121         }
3122         if(prog->opensearches[handle] == NULL)
3123         {
3124                 VM_Warning(prog, "VM_search_end: no such handle %i in %s\n", handle, prog->name);
3125                 return;
3126         }
3127
3128         FS_FreeSearch(prog->opensearches[handle]);
3129         prog->opensearches[handle] = NULL;
3130         if(prog->opensearches_origin[handle])
3131                 PRVM_Free((char *)prog->opensearches_origin[handle]);
3132 }
3133
3134 /*
3135 =========
3136 VM_search_getsize
3137
3138 float   search_getsize(float handle)
3139 =========
3140 */
3141 void VM_search_getsize(prvm_prog_t *prog)
3142 {
3143         int handle;
3144         VM_SAFEPARMCOUNT(1, VM_M_search_getsize);
3145
3146         handle = (int)PRVM_G_FLOAT(OFS_PARM0);
3147
3148         if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
3149         {
3150                 VM_Warning(prog, "VM_search_getsize: invalid handle %i used in %s\n", handle, prog->name);
3151                 return;
3152         }
3153         if(prog->opensearches[handle] == NULL)
3154         {
3155                 VM_Warning(prog, "VM_search_getsize: no such handle %i in %s\n", handle, prog->name);
3156                 return;
3157         }
3158
3159         PRVM_G_FLOAT(OFS_RETURN) = prog->opensearches[handle]->numfilenames;
3160 }
3161
3162 /*
3163 =========
3164 VM_search_getfilename
3165
3166 string  search_getfilename(float handle, float num)
3167 =========
3168 */
3169 void VM_search_getfilename(prvm_prog_t *prog)
3170 {
3171         int handle, filenum;
3172         VM_SAFEPARMCOUNT(2, VM_search_getfilename);
3173
3174         handle = (int)PRVM_G_FLOAT(OFS_PARM0);
3175         filenum = (int)PRVM_G_FLOAT(OFS_PARM1);
3176
3177         if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
3178         {
3179                 VM_Warning(prog, "VM_search_getfilename: invalid handle %i used in %s\n", handle, prog->name);
3180                 return;
3181         }
3182         if(prog->opensearches[handle] == NULL)
3183         {
3184                 VM_Warning(prog, "VM_search_getfilename: no such handle %i in %s\n", handle, prog->name);
3185                 return;
3186         }
3187         if(filenum < 0 || filenum >= prog->opensearches[handle]->numfilenames)
3188         {
3189                 VM_Warning(prog, "VM_search_getfilename: invalid filenum %i in %s\n", filenum, prog->name);
3190                 return;
3191         }
3192
3193         PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, prog->opensearches[handle]->filenames[filenum]);
3194 }
3195
3196 /*
3197 =========
3198 VM_chr
3199
3200 string  chr(float ascii)
3201 =========
3202 */
3203 void VM_chr(prvm_prog_t *prog)
3204 {
3205         /*
3206         char tmp[2];
3207         VM_SAFEPARMCOUNT(1, VM_chr);
3208
3209         tmp[0] = (unsigned char) PRVM_G_FLOAT(OFS_PARM0);
3210         tmp[1] = 0;
3211
3212         PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, tmp);
3213         */
3214         
3215         char tmp[8];
3216         int len;
3217         VM_SAFEPARMCOUNT(1, VM_chr);
3218
3219         len = u8_fromchar((Uchar)PRVM_G_FLOAT(OFS_PARM0), tmp, sizeof(tmp));
3220         tmp[len] = 0;
3221         PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, tmp);
3222 }
3223
3224 //=============================================================================
3225 // Draw builtins (client & menu)
3226
3227 /*
3228 =========
3229 VM_iscachedpic
3230
3231 float   iscachedpic(string pic)
3232 =========
3233 */
3234 void VM_iscachedpic(prvm_prog_t *prog)
3235 {
3236         VM_SAFEPARMCOUNT(1,VM_iscachedpic);
3237
3238         // drawq hasnt such a function, thus always return true
3239         PRVM_G_FLOAT(OFS_RETURN) = false;
3240 }
3241
3242 /*
3243 =========
3244 VM_precache_pic
3245
3246 string  precache_pic(string pic)
3247 =========
3248 */
3249 #define PRECACHE_PIC_FROMWAD 1 /* FTEQW, not supported here */
3250 #define PRECACHE_PIC_NOTPERSISTENT 2
3251 //#define PRECACHE_PIC_NOCLAMP 4
3252 #define PRECACHE_PIC_MIPMAP 8
3253 void VM_precache_pic(prvm_prog_t *prog)
3254 {
3255         const char      *s;
3256         int flags = 0;
3257
3258         VM_SAFEPARMCOUNTRANGE(1, 2, VM_precache_pic);
3259
3260         s = PRVM_G_STRING(OFS_PARM0);
3261         PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
3262         VM_CheckEmptyString(prog, s);
3263
3264         if(prog->argc >= 2)
3265         {
3266                 int f = PRVM_G_FLOAT(OFS_PARM1);
3267                 if(f & PRECACHE_PIC_NOTPERSISTENT)
3268                         flags |= CACHEPICFLAG_NOTPERSISTENT;
3269                 //if(f & PRECACHE_PIC_NOCLAMP)
3270                 //      flags |= CACHEPICFLAG_NOCLAMP;
3271                 if(f & PRECACHE_PIC_MIPMAP)
3272                         flags |= CACHEPICFLAG_MIPMAP;
3273         }
3274
3275         // AK Draw_CachePic is supposed to always return a valid pointer
3276         if( Draw_CachePic_Flags(s, flags)->tex == r_texture_notexture )
3277                 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
3278 }
3279
3280 /*
3281 =========
3282 VM_freepic
3283
3284 freepic(string s)
3285 =========
3286 */
3287 void VM_freepic(prvm_prog_t *prog)
3288 {
3289         const char *s;
3290
3291         VM_SAFEPARMCOUNT(1,VM_freepic);
3292
3293         s = PRVM_G_STRING(OFS_PARM0);
3294         VM_CheckEmptyString(prog, s);
3295
3296         Draw_FreePic(s);
3297 }
3298
3299 static void getdrawfontscale(prvm_prog_t *prog, float *sx, float *sy)
3300 {
3301         vec3_t v;
3302         *sx = *sy = 1;
3303         VectorCopy(PRVM_drawglobalvector(drawfontscale), v);
3304         if(VectorLength2(v) > 0)
3305         {
3306                 *sx = v[0];
3307                 *sy = v[1];
3308         }
3309 }
3310
3311 static dp_font_t *getdrawfont(prvm_prog_t *prog)
3312 {
3313         int f = (int) PRVM_drawglobalfloat(drawfont);
3314         if(f < 0 || f >= dp_fonts.maxsize)
3315                 return FONT_DEFAULT;
3316         return &dp_fonts.f[f];
3317 }
3318
3319 /*
3320 =========
3321 VM_drawcharacter
3322
3323 float   drawcharacter(vector position, float character, vector scale, vector rgb, float alpha, float flag)
3324 =========
3325 */
3326 void VM_drawcharacter(prvm_prog_t *prog)
3327 {
3328         float *pos,*scale,*rgb;
3329         char   character;
3330         int flag;
3331         float sx, sy;
3332         VM_SAFEPARMCOUNT(6,VM_drawcharacter);
3333
3334         character = (char) PRVM_G_FLOAT(OFS_PARM1);
3335         if(character == 0)
3336         {
3337                 PRVM_G_FLOAT(OFS_RETURN) = -1;
3338                 VM_Warning(prog, "VM_drawcharacter: %s passed null character !\n",prog->name);
3339                 return;
3340         }
3341
3342         pos = PRVM_G_VECTOR(OFS_PARM0);
3343         scale = PRVM_G_VECTOR(OFS_PARM2);
3344         rgb = PRVM_G_VECTOR(OFS_PARM3);
3345         flag = (int)PRVM_G_FLOAT(OFS_PARM5);
3346
3347         if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3348         {
3349                 PRVM_G_FLOAT(OFS_RETURN) = -2;
3350                 VM_Warning(prog, "VM_drawcharacter: %s: wrong DRAWFLAG %i !\n",prog->name,flag);
3351                 return;
3352         }
3353
3354         if(pos[2] || scale[2])
3355                 VM_Warning(prog, "VM_drawcharacter: z value%c from %s discarded\n",(pos[2] && scale[2]) ? 's' : 0,((pos[2] && scale[2]) ? "pos and scale" : (pos[2] ? "pos" : "scale")));
3356
3357         if(!scale[0] || !scale[1])
3358         {
3359                 PRVM_G_FLOAT(OFS_RETURN) = -3;
3360                 VM_Warning(prog, "VM_drawcharacter: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
3361                 return;
3362         }
3363
3364         getdrawfontscale(prog, &sx, &sy);
3365         DrawQ_String_Scale(pos[0], pos[1], &character, 1, scale[0], scale[1], sx, sy, rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM4), flag, NULL, true, getdrawfont(prog));
3366         PRVM_G_FLOAT(OFS_RETURN) = 1;
3367 }
3368
3369 /*
3370 =========
3371 VM_drawstring
3372
3373 float   drawstring(vector position, string text, vector scale, vector rgb, float alpha[, float flag])
3374 =========
3375 */
3376 void VM_drawstring(prvm_prog_t *prog)
3377 {
3378         float *pos,*scale,*rgb;
3379         const char  *string;
3380         int flag = 0;
3381         float sx, sy;
3382         VM_SAFEPARMCOUNTRANGE(5,6,VM_drawstring);
3383
3384         string = PRVM_G_STRING(OFS_PARM1);
3385         pos = PRVM_G_VECTOR(OFS_PARM0);
3386         scale = PRVM_G_VECTOR(OFS_PARM2);
3387         rgb = PRVM_G_VECTOR(OFS_PARM3);
3388         if (prog->argc >= 6)
3389                 flag = (int)PRVM_G_FLOAT(OFS_PARM5);
3390
3391         if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3392         {
3393                 PRVM_G_FLOAT(OFS_RETURN) = -2;
3394                 VM_Warning(prog, "VM_drawstring: %s: wrong DRAWFLAG %i !\n",prog->name,flag);
3395                 return;
3396         }
3397
3398         if(!scale[0] || !scale[1])
3399         {
3400                 PRVM_G_FLOAT(OFS_RETURN) = -3;
3401                 VM_Warning(prog, "VM_drawstring: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
3402                 return;
3403         }
3404
3405         if(pos[2] || scale[2])
3406                 VM_Warning(prog, "VM_drawstring: z value%s from %s discarded\n",(pos[2] && scale[2]) ? "s" : " ",((pos[2] && scale[2]) ? "pos and scale" : (pos[2] ? "pos" : "scale")));
3407
3408         getdrawfontscale(prog, &sx, &sy);
3409         DrawQ_String_Scale(pos[0], pos[1], string, 0, scale[0], scale[1], sx, sy, rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM4), flag, NULL, true, getdrawfont(prog));
3410         //Font_DrawString(pos[0], pos[1], string, 0, scale[0], scale[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM4), flag, NULL, true);
3411         PRVM_G_FLOAT(OFS_RETURN) = 1;
3412 }
3413
3414 /*
3415 =========
3416 VM_drawcolorcodedstring
3417
3418 float   drawcolorcodedstring(vector position, string text, vector scale, float alpha, float flag)
3419 /
3420 float   drawcolorcodedstring(vector position, string text, vector scale, vector rgb, float alpha, float flag)
3421 =========
3422 */
3423 void VM_drawcolorcodedstring(prvm_prog_t *prog)
3424 {
3425         float *pos, *scale;
3426         const char  *string;
3427         int flag;
3428         vec3_t rgb;
3429         float sx, sy, alpha;
3430
3431         VM_SAFEPARMCOUNTRANGE(5,6,VM_drawcolorcodedstring);
3432
3433         if (prog->argc == 6) // full 6 parms, like normal drawstring
3434         {
3435                 pos = PRVM_G_VECTOR(OFS_PARM0);
3436                 string = PRVM_G_STRING(OFS_PARM1);
3437                 scale = PRVM_G_VECTOR(OFS_PARM2);
3438                 VectorCopy(PRVM_G_VECTOR(OFS_PARM3), rgb); 
3439                 alpha = PRVM_G_FLOAT(OFS_PARM4);
3440                 flag = (int)PRVM_G_FLOAT(OFS_PARM5);
3441         }
3442         else
3443         {
3444                 pos = PRVM_G_VECTOR(OFS_PARM0);
3445                 string = PRVM_G_STRING(OFS_PARM1);
3446                 scale = PRVM_G_VECTOR(OFS_PARM2);
3447                 rgb[0] = 1.0;
3448                 rgb[1] = 1.0;
3449                 rgb[2] = 1.0;
3450                 alpha = PRVM_G_FLOAT(OFS_PARM3);
3451                 flag = (int)PRVM_G_FLOAT(OFS_PARM4);
3452         }
3453
3454         if(flag < DRAWFLAG_NORMAL || flag >= DRAWFLAG_NUMFLAGS)
3455         {
3456                 PRVM_G_FLOAT(OFS_RETURN) = -2;
3457                 VM_Warning(prog, "VM_drawcolorcodedstring: %s: wrong DRAWFLAG %i !\n",prog->name,flag);
3458                 return;
3459         }
3460
3461         if(!scale[0] || !scale[1])
3462         {
3463                 PRVM_G_FLOAT(OFS_RETURN) = -3;
3464                 VM_Warning(prog, "VM_drawcolorcodedstring: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
3465                 return;
3466         }
3467
3468         if(pos[2] || scale[2])
3469                 VM_Warning(prog, "VM_drawcolorcodedstring: z value%s from %s discarded\n",(pos[2] && scale[2]) ? "s" : " ",((pos[2] && scale[2]) ? "pos and scale" : (pos[2] ? "pos" : "scale")));
3470
3471         getdrawfontscale(prog, &sx, &sy);
3472         DrawQ_String_Scale(pos[0], pos[1], string, 0, scale[0], scale[1], sx, sy, rgb[0], rgb[1], rgb[2], alpha, flag, NULL, false, getdrawfont(prog));
3473         if (prog->argc == 6) // also return vector of last color
3474                 VectorCopy(DrawQ_Color, PRVM_G_VECTOR(OFS_RETURN));
3475         else
3476                 PRVM_G_FLOAT(OFS_RETURN) = 1;
3477 }
3478 /*
3479 =========
3480 VM_stringwidth
3481
3482 float   stringwidth(string text, float allowColorCodes, float size)
3483 =========
3484 */
3485 void VM_stringwidth(prvm_prog_t *prog)
3486 {
3487         const char  *string;
3488         float *szv;
3489         float mult; // sz is intended font size so we can later add freetype support, mult is font size multiplier in pixels per character cell
3490         int colors;
3491         float sx, sy;
3492         size_t maxlen = 0;
3493         VM_SAFEPARMCOUNTRANGE(2,3,VM_drawstring);
3494
3495         getdrawfontscale(prog, &sx, &sy);
3496         if(prog->argc == 3)
3497         {
3498                 szv = PRVM_G_VECTOR(OFS_PARM2);
3499                 mult = 1;
3500         }
3501         else
3502         {
3503                 // we want the width for 8x8 font size, divided by 8
3504                 static float defsize[] = {8, 8};
3505                 szv = defsize;
3506                 mult = 0.125;
3507                 // to make sure snapping is turned off, ALWAYS use a nontrivial scale in this case
3508                 if(sx >= 0.9 && sx <= 1.1)
3509                 {
3510                         mult *= 2;
3511                         sx /= 2;
3512                         sy /= 2;
3513                 }
3514         }
3515
3516         string = PRVM_G_STRING(OFS_PARM0);
3517         colors = (int)PRVM_G_FLOAT(OFS_PARM1);
3518
3519         PRVM_G_FLOAT(OFS_RETURN) = DrawQ_TextWidth_UntilWidth_TrackColors_Scale(string, &maxlen, szv[0], szv[1], sx, sy, NULL, !colors, getdrawfont(prog), 1000000000) * mult;
3520 /*
3521         if(prog->argc == 3)
3522         {
3523                 mult = sz = PRVM_G_FLOAT(OFS_PARM2);
3524         }
3525         else
3526         {
3527                 sz = 8;
3528                 mult = 1;
3529         }
3530
3531         string = PRVM_G_STRING(OFS_PARM0);
3532         colors = (int)PRVM_G_FLOAT(OFS_PARM1);
3533
3534         PRVM_G_FLOAT(OFS_RETURN) = DrawQ_TextWidth(string, 0, !colors, getdrawfont()) * mult; // 1x1 characters, don't actually draw
3535 */
3536 }
3537
3538 /*
3539 =========
3540 VM_findfont
3541
3542 float findfont(string s)
3543 =========
3544 */
3545
3546 static float getdrawfontnum(const char *fontname)
3547 {
3548         int i;
3549
3550         for(i = 0; i < dp_fonts.maxsize; ++i)
3551                 if(!strcmp(dp_fonts.f[i].title, fontname))
3552                         return i;
3553         return -1;
3554 }
3555
3556 void VM_findfont(prvm_prog_t *prog)
3557 {
3558         VM_SAFEPARMCOUNT(1,VM_findfont);
3559         PRVM_G_FLOAT(OFS_RETURN) = getdrawfontnum(PRVM_G_STRING(OFS_PARM0));
3560 }
3561
3562 /*
3563 =========
3564 VM_loadfont
3565
3566 float loadfont(string fontname, string fontmaps, string sizes, float slot)
3567 =========
3568 */
3569
3570 void VM_loadfont(prvm_prog_t *prog)
3571 {
3572         const char *fontname, *filelist, *sizes, *c, *cm;
3573         char mainfont[MAX_QPATH];
3574         int i, numsizes;
3575         float sz, scale, voffset;
3576         dp_font_t *f;
3577
3578         VM_SAFEPARMCOUNTRANGE(3,6,VM_loadfont);
3579
3580         fontname = PRVM_G_STRING(OFS_PARM0);
3581         if (!fontname[0])
3582                 fontname = "default";
3583
3584         filelist = PRVM_G_STRING(OFS_PARM1);
3585         if (!filelist[0])
3586                 filelist = "gfx/conchars";
3587
3588         sizes = PRVM_G_STRING(OFS_PARM2);
3589         if (!sizes[0])
3590                 sizes = "10";
3591
3592         // find a font
3593         f = NULL;
3594         if (prog->argc >= 4)
3595         {
3596                 i = PRVM_G_FLOAT(OFS_PARM3);
3597                 if (i >= 0 && i < dp_fonts.maxsize)
3598                 {
3599                         f = &dp_fonts.f[i];
3600                         strlcpy(f->title, fontname, sizeof(f->title)); // replace name
3601                 }
3602         }
3603         if (!f)
3604                 f = FindFont(fontname, true);
3605         if (!f)
3606         {
3607                 PRVM_G_FLOAT(OFS_RETURN) = -1;
3608                 return; // something go wrong
3609         }
3610
3611         memset(f->fallbacks, 0, sizeof(f->fallbacks));
3612         memset(f->fallback_faces, 0, sizeof(f->fallback_faces));
3613
3614         // first font is handled "normally"
3615         c = strchr(filelist, ':');
3616         cm = strchr(filelist, ',');
3617         if(c && (!cm || c < cm))
3618                 f->req_face = atoi(c+1);
3619         else
3620         {
3621                 f->req_face = 0;
3622                 c = cm;
3623         }
3624         if(!c || (c - filelist) > MAX_QPATH)
3625                 strlcpy(mainfont, filelist, sizeof(mainfont));
3626         else
3627         {
3628                 memcpy(mainfont, filelist, c - filelist);
3629                 mainfont[c - filelist] = 0;
3630         }
3631
3632         // handle fallbacks
3633         for(i = 0; i < MAX_FONT_FALLBACKS; ++i)
3634         {
3635                 c = strchr(filelist, ',');
3636                 if(!c)
3637                         break;
3638                 filelist = c + 1;
3639                 if(!*filelist)
3640                         break;
3641                 c = strchr(filelist, ':');
3642                 cm = strchr(filelist, ',');
3643                 if(c && (!cm || c < cm))
3644                         f->fallback_faces[i] = atoi(c+1);
3645                 else
3646                 {
3647                         f->fallback_faces[i] = 0; // f->req_face; could make it stick to the default-font's face index
3648                         c = cm;
3649                 }
3650                 if(!c || (c-filelist) > MAX_QPATH)
3651                 {
3652                         strlcpy(f->fallbacks[i], filelist, sizeof(mainfont));
3653                 }
3654                 else
3655                 {
3656                         memcpy(f->fallbacks[i], filelist, c - filelist);
3657                         f->fallbacks[i][c - filelist] = 0;
3658                 }
3659         }
3660
3661         // handle sizes
3662         for(i = 0; i < MAX_FONT_SIZES; ++i)
3663                 f->req_sizes[i] = -1;
3664         for (numsizes = 0,c = sizes;;)
3665         {
3666                 if (!COM_ParseToken_VM_Tokenize(&c, 0))
3667                         break;
3668                 sz = atof(com_token);
3669                 // detect crap size
3670                 if (sz < 0.001f || sz > 1000.0f)
3671                 {
3672                         VM_Warning(prog, "VM_loadfont: crap size %s", com_token);
3673                         continue;
3674                 }
3675                 // check overflow
3676                 if (numsizes == MAX_FONT_SIZES)
3677                 {
3678                         VM_Warning(prog, "VM_loadfont: MAX_FONT_SIZES = %i exceeded", MAX_FONT_SIZES);
3679                         break;
3680                 }
3681                 f->req_sizes[numsizes] = sz;
3682                 numsizes++;
3683         }
3684
3685         // additional scale/hoffset parms
3686         scale = 1;
3687         voffset = 0;
3688         if (prog->argc >= 5)
3689         {
3690                 scale = PRVM_G_FLOAT(OFS_PARM4);
3691                 if (scale <= 0)
3692                         scale = 1;
3693         }
3694         if (prog->argc >= 6)
3695                 voffset = PRVM_G_FLOAT(OFS_PARM5);
3696
3697         // load
3698         LoadFont(true, mainfont, f, scale, voffset);
3699
3700         // return index of loaded font
3701         PRVM_G_FLOAT(OFS_RETURN) = (f - dp_fonts.f);
3702 }
3703
3704 /*
3705 =========
3706 VM_drawpic
3707
3708 float   drawpic(vector position, string pic, vector size, vector rgb, float alpha, float flag)
3709 =========
3710 */
3711 void VM_drawpic(prvm_prog_t *prog)
3712 {
3713         const char *picname;
3714         float *size, *pos, *rgb;
3715         int flag = 0;
3716
3717         VM_SAFEPARMCOUNTRANGE(5,6,VM_drawpic);
3718
3719         picname = PRVM_G_STRING(OFS_PARM1);
3720         VM_CheckEmptyString(prog, picname);
3721
3722         // is pic cached ? no function yet for that
3723         if(!1)
3724         {
3725                 PRVM_G_FLOAT(OFS_RETURN) = -4;
3726                 VM_Warning(prog, "VM_drawpic: %s: %s not cached !\n", prog->name, picname);
3727                 return;
3728         }
3729
3730         pos = PRVM_G_VECTOR(OFS_PARM0);
3731         size = PRVM_G_VECTOR(OFS_PARM2);
3732         rgb = PRVM_G_VECTOR(OFS_PARM3);
3733         if (prog->argc >= 6)
3734                 flag = (int) PRVM_G_FLOAT(OFS_PARM5);
3735
3736         if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3737         {
3738                 PRVM_G_FLOAT(OFS_RETURN) = -2;
3739                 VM_Warning(prog, "VM_drawpic: %s: wrong DRAWFLAG %i !\n",prog->name,flag);
3740                 return;
3741         }
3742
3743         if(pos[2] || size[2])
3744                 VM_Warning(prog, "VM_drawpic: z value%s from %s discarded\n",(pos[2] && size[2]) ? "s" : " ",((pos[2] && size[2]) ? "pos and size" : (pos[2] ? "pos" : "size")));
3745
3746         DrawQ_Pic(pos[0], pos[1], Draw_CachePic_Flags (picname, CACHEPICFLAG_NOTPERSISTENT), size[0], size[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM4), flag);
3747         PRVM_G_FLOAT(OFS_RETURN) = 1;
3748 }
3749 /*
3750 =========
3751 VM_drawrotpic
3752
3753 float   drawrotpic(vector position, string pic, vector size, vector org, float angle, vector rgb, float alpha, float flag)
3754 =========
3755 */
3756 void VM_drawrotpic(prvm_prog_t *prog)
3757 {
3758         const char *picname;
3759         float *size, *pos, *org, *rgb;
3760         int flag;
3761
3762         VM_SAFEPARMCOUNT(8,VM_drawrotpic);
3763
3764         picname = PRVM_G_STRING(OFS_PARM1);
3765         VM_CheckEmptyString(prog, picname);
3766
3767         // is pic cached ? no function yet for that
3768         if(!1)
3769         {
3770                 PRVM_G_FLOAT(OFS_RETURN) = -4;
3771                 VM_Warning(prog, "VM_drawrotpic: %s: %s not cached !\n", prog->name, picname);
3772                 return;
3773         }
3774
3775         pos = PRVM_G_VECTOR(OFS_PARM0);
3776         size = PRVM_G_VECTOR(OFS_PARM2);
3777         org = PRVM_G_VECTOR(OFS_PARM3);
3778         rgb = PRVM_G_VECTOR(OFS_PARM5);
3779         flag = (int) PRVM_G_FLOAT(OFS_PARM7);
3780
3781         if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3782         {
3783                 PRVM_G_FLOAT(OFS_RETURN) = -2;
3784                 VM_Warning(prog, "VM_drawrotpic: %s: wrong DRAWFLAG %i !\n",prog->name,flag);
3785                 return;
3786         }
3787
3788         if(pos[2] || size[2] || org[2])
3789                 VM_Warning(prog, "VM_drawrotpic: z value from pos/size/org discarded\n");
3790
3791         DrawQ_RotPic(pos[0], pos[1], Draw_CachePic_Flags(picname, CACHEPICFLAG_NOTPERSISTENT), size[0], size[1], org[0], org[1], PRVM_G_FLOAT(OFS_PARM4), rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM6), flag);
3792         PRVM_G_FLOAT(OFS_RETURN) = 1;
3793 }
3794 /*
3795 =========
3796 VM_drawsubpic
3797
3798 float   drawsubpic(vector position, vector size, string pic, vector srcPos, vector srcSize, vector rgb, float alpha, float flag)
3799
3800 =========
3801 */
3802 void VM_drawsubpic(prvm_prog_t *prog)
3803 {
3804         const char *picname;
3805         float *size, *pos, *rgb, *srcPos, *srcSize, alpha;
3806         int flag;
3807
3808         VM_SAFEPARMCOUNT(8,VM_drawsubpic);
3809
3810         picname = PRVM_G_STRING(OFS_PARM2);
3811         VM_CheckEmptyString(prog, picname);
3812
3813         // is pic cached ? no function yet for that
3814         if(!1)
3815         {
3816                 PRVM_G_FLOAT(OFS_RETURN) = -4;
3817                 VM_Warning(prog, "VM_drawsubpic: %s: %s not cached !\n", prog->name, picname);
3818                 return;
3819         }
3820
3821         pos = PRVM_G_VECTOR(OFS_PARM0);
3822         size = PRVM_G_VECTOR(OFS_PARM1);
3823         srcPos = PRVM_G_VECTOR(OFS_PARM3);
3824         srcSize = PRVM_G_VECTOR(OFS_PARM4);
3825         rgb = PRVM_G_VECTOR(OFS_PARM5);
3826         alpha = PRVM_G_FLOAT(OFS_PARM6);
3827         flag = (int) PRVM_G_FLOAT(OFS_PARM7);
3828
3829         if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3830         {
3831                 PRVM_G_FLOAT(OFS_RETURN) = -2;
3832                 VM_Warning(prog, "VM_drawsubpic: %s: wrong DRAWFLAG %i !\n",prog->name,flag);
3833                 return;
3834         }
3835
3836         if(pos[2] || size[2])
3837                 VM_Warning(prog, "VM_drawsubpic: z value%s from %s discarded\n",(pos[2] && size[2]) ? "s" : " ",((pos[2] && size[2]) ? "pos and size" : (pos[2] ? "pos" : "size")));
3838
3839         DrawQ_SuperPic(pos[0], pos[1], Draw_CachePic_Flags (picname, CACHEPICFLAG_NOTPERSISTENT),
3840                 size[0], size[1],
3841                 srcPos[0],              srcPos[1],              rgb[0], rgb[1], rgb[2], alpha,
3842                 srcPos[0] + srcSize[0], srcPos[1],              rgb[0], rgb[1], rgb[2], alpha,
3843                 srcPos[0],              srcPos[1] + srcSize[1], rgb[0], rgb[1], rgb[2], alpha,
3844                 srcPos[0] + srcSize[0], srcPos[1] + srcSize[1], rgb[0], rgb[1], rgb[2], alpha,
3845                 flag);
3846         PRVM_G_FLOAT(OFS_RETURN) = 1;
3847 }
3848
3849 /*
3850 =========
3851 VM_drawfill
3852
3853 float drawfill(vector position, vector size, vector rgb, float alpha, float flag)
3854 =========
3855 */
3856 void VM_drawfill(prvm_prog_t *prog)
3857 {
3858         float *size, *pos, *rgb;
3859         int flag;
3860
3861         VM_SAFEPARMCOUNT(5,VM_drawfill);
3862
3863
3864         pos = PRVM_G_VECTOR(OFS_PARM0);
3865         size = PRVM_G_VECTOR(OFS_PARM1);
3866         rgb = PRVM_G_VECTOR(OFS_PARM2);
3867         flag = (int) PRVM_G_FLOAT(OFS_PARM4);
3868
3869         if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3870         {
3871                 PRVM_G_FLOAT(OFS_RETURN) = -2;
3872                 VM_Warning(prog, "VM_drawfill: %s: wrong DRAWFLAG %i !\n",prog->name,flag);
3873                 return;
3874         }
3875
3876         if(pos[2] || size[2])
3877                 VM_Warning(prog, "VM_drawfill: z value%s from %s discarded\n",(pos[2] && size[2]) ? "s" : " ",((pos[2] && size[2]) ? "pos and size" : (pos[2] ? "pos" : "size")));
3878
3879         DrawQ_Fill(pos[0], pos[1], size[0], size[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM3), flag);
3880         PRVM_G_FLOAT(OFS_RETURN) = 1;
3881 }
3882
3883 /*
3884 =========
3885 VM_drawsetcliparea
3886
3887 drawsetcliparea(float x, float y, float width, float height)
3888 =========
3889 */
3890 void VM_drawsetcliparea(prvm_prog_t *prog)
3891 {
3892         float x,y,w,h;
3893         VM_SAFEPARMCOUNT(4,VM_drawsetcliparea);
3894
3895         x = bound(0, PRVM_G_FLOAT(OFS_PARM0), vid_conwidth.integer);
3896         y = bound(0, PRVM_G_FLOAT(OFS_PARM1), vid_conheight.integer);
3897         w = bound(0, PRVM_G_FLOAT(OFS_PARM2) + PRVM_G_FLOAT(OFS_PARM0) - x, (vid_conwidth.integer  - x));
3898         h = bound(0, PRVM_G_FLOAT(OFS_PARM3) + PRVM_G_FLOAT(OFS_PARM1) - y, (vid_conheight.integer - y));
3899
3900         DrawQ_SetClipArea(x, y, w, h);
3901 }
3902
3903 /*
3904 =========
3905 VM_drawresetcliparea
3906
3907 drawresetcliparea()
3908 =========
3909 */
3910 void VM_drawresetcliparea(prvm_prog_t *prog)
3911 {
3912         VM_SAFEPARMCOUNT(0,VM_drawresetcliparea);
3913
3914         DrawQ_ResetClipArea();
3915 }
3916
3917 /*
3918 =========
3919 VM_getimagesize
3920
3921 vector  getimagesize(string pic)
3922 =========
3923 */
3924 void VM_getimagesize(prvm_prog_t *prog)
3925 {
3926         const char *p;
3927         cachepic_t *pic;
3928
3929         VM_SAFEPARMCOUNT(1,VM_getimagesize);
3930
3931         p = PRVM_G_STRING(OFS_PARM0);
3932         VM_CheckEmptyString(prog, p);
3933
3934         pic = Draw_CachePic_Flags (p, CACHEPICFLAG_NOTPERSISTENT);
3935         if( pic->tex == r_texture_notexture )
3936         {
3937                 PRVM_G_VECTOR(OFS_RETURN)[0] = 0;
3938                 PRVM_G_VECTOR(OFS_RETURN)[1] = 0;
3939         }
3940         else
3941         {
3942                 PRVM_G_VECTOR(OFS_RETURN)[0] = pic->width;
3943                 PRVM_G_VECTOR(OFS_RETURN)[1] = pic->height;
3944         }
3945         PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
3946 }
3947
3948 /*
3949 =========
3950 VM_keynumtostring
3951
3952 string keynumtostring(float keynum)
3953 =========
3954 */
3955 void VM_keynumtostring (prvm_prog_t *prog)
3956 {
3957         char tinystr[2];
3958         VM_SAFEPARMCOUNT(1, VM_keynumtostring);
3959
3960         PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, Key_KeynumToString((int)PRVM_G_FLOAT(OFS_PARM0), tinystr, sizeof(tinystr)));
3961 }
3962
3963 /*
3964 =========
3965 VM_findkeysforcommand
3966
3967 string  findkeysforcommand(string command, float bindmap)
3968
3969 the returned string is an altstring
3970 =========
3971 */
3972 #define FKFC_NUMKEYS 5
3973 void M_FindKeysForCommand(const char *command, int *keys);
3974 void VM_findkeysforcommand(prvm_prog_t *prog)
3975 {
3976         const char *cmd;
3977         char ret[VM_STRINGTEMP_LENGTH];
3978         int keys[FKFC_NUMKEYS];
3979         int i;
3980         int bindmap;
3981         char vabuf[1024];
3982
3983         VM_SAFEPARMCOUNTRANGE(1, 2, VM_findkeysforcommand);
3984
3985         cmd = PRVM_G_STRING(OFS_PARM0);
3986         if(prog->argc == 2)
3987                 bindmap = bound(-1, PRVM_G_FLOAT(OFS_PARM1), MAX_BINDMAPS-1);
3988         else
3989                 bindmap = 0; // consistent to "bind"
3990
3991         VM_CheckEmptyString(prog, cmd);
3992
3993         Key_FindKeysForCommand(cmd, keys, FKFC_NUMKEYS, bindmap);
3994
3995         ret[0] = 0;
3996         for(i = 0; i < FKFC_NUMKEYS; i++)
3997                 strlcat(ret, va(vabuf, sizeof(vabuf), " \'%i\'", keys[i]), sizeof(ret));
3998
3999         PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, ret);
4000 }
4001
4002 /*
4003 =========
4004 VM_stringtokeynum
4005
4006 float stringtokeynum(string key)
4007 =========
4008 */
4009 void VM_stringtokeynum (prvm_prog_t *prog)
4010 {
4011         VM_SAFEPARMCOUNT( 1, VM_keynumtostring );
4012
4013         PRVM_G_FLOAT(OFS_RETURN) = Key_StringToKeynum(PRVM_G_STRING(OFS_PARM0));
4014 }
4015
4016 /*
4017 =========
4018 VM_getkeybind
4019
4020 string getkeybind(float key, float bindmap)
4021 =========
4022 */
4023 void VM_getkeybind (prvm_prog_t *prog)
4024 {
4025         int bindmap;
4026         VM_SAFEPARMCOUNTRANGE(1, 2, VM_CL_getkeybind);
4027         if(prog->argc == 2)
4028                 bindmap = bound(-1, PRVM_G_FLOAT(OFS_PARM1), MAX_BINDMAPS-1);
4029         else
4030                 bindmap = 0; // consistent to "bind"
4031
4032         PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, Key_GetBind((int)PRVM_G_FLOAT(OFS_PARM0), bindmap));
4033 }
4034
4035 /*
4036 =========
4037 VM_setkeybind
4038
4039 float setkeybind(float key, string cmd, float bindmap)
4040 =========
4041 */
4042 void VM_setkeybind (prvm_prog_t *prog)
4043 {
4044         int bindmap;
4045         VM_SAFEPARMCOUNTRANGE(2, 3, VM_CL_setkeybind);
4046         if(prog->argc == 3)
4047                 bindmap = bound(-1, PRVM_G_FLOAT(OFS_PARM2), MAX_BINDMAPS-1);
4048         else
4049                 bindmap = 0; // consistent to "bind"
4050
4051         PRVM_G_FLOAT(OFS_RETURN) = 0;
4052         if(Key_SetBinding((int)PRVM_G_FLOAT(OFS_PARM0), bindmap, PRVM_G_STRING(OFS_PARM1)))
4053                 PRVM_G_FLOAT(OFS_RETURN) = 1;
4054 }
4055
4056 /*
4057 =========
4058 VM_getbindmap
4059
4060 vector getbindmaps()
4061 =========
4062 */
4063 void VM_getbindmaps (prvm_prog_t *prog)
4064 {
4065         int fg, bg;
4066         VM_SAFEPARMCOUNT(0, VM_CL_getbindmap);
4067         Key_GetBindMap(&fg, &bg);
4068         PRVM_G_VECTOR(OFS_RETURN)[0] = fg;
4069         PRVM_G_VECTOR(OFS_RETURN)[1] = bg;
4070         PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
4071 }
4072
4073 /*
4074 =========
4075 VM_setbindmap
4076
4077 float setbindmaps(vector bindmap)
4078 =========
4079 */
4080 void VM_setbindmaps (prvm_prog_t *prog)
4081 {
4082         VM_SAFEPARMCOUNT(1, VM_CL_setbindmap);
4083         PRVM_G_FLOAT(OFS_RETURN) = 0;
4084         if(PRVM_G_VECTOR(OFS_PARM0)[2] == 0)
4085                 if(Key_SetBindMap((int)PRVM_G_VECTOR(OFS_PARM0)[0], (int)PRVM_G_VECTOR(OFS_PARM0)[1]))
4086                         PRVM_G_FLOAT(OFS_RETURN) = 1;
4087 }
4088
4089 // CL_Video interface functions
4090
4091 /*
4092 ========================
4093 VM_cin_open
4094
4095 float cin_open(string file, string name)
4096 ========================
4097 */
4098 void VM_cin_open(prvm_prog_t *prog)
4099 {
4100         const char *file;
4101         const char *name;
4102
4103         VM_SAFEPARMCOUNT( 2, VM_cin_open );
4104
4105         file = PRVM_G_STRING( OFS_PARM0 );
4106         name = PRVM_G_STRING( OFS_PARM1 );
4107
4108         VM_CheckEmptyString(prog,  file );
4109     VM_CheckEmptyString(prog,  name );
4110
4111         if( CL_OpenVideo( file, name, MENUOWNER, "" ) )
4112                 PRVM_G_FLOAT( OFS_RETURN ) = 1;
4113         else
4114                 PRVM_G_FLOAT( OFS_RETURN ) = 0;
4115 }
4116
4117 /*
4118 ========================
4119 VM_cin_close
4120
4121 void cin_close(string name)
4122 ========================
4123 */
4124 void VM_cin_close(prvm_prog_t *prog)
4125 {
4126         const char *name;
4127
4128         VM_SAFEPARMCOUNT( 1, VM_cin_close );
4129
4130         name = PRVM_G_STRING( OFS_PARM0 );
4131         VM_CheckEmptyString(prog,  name );
4132
4133         CL_CloseVideo( CL_GetVideoByName( name ) );
4134 }
4135
4136 /*
4137 ========================
4138 VM_cin_setstate
4139 void cin_setstate(string name, float type)
4140 ========================
4141 */
4142 void VM_cin_setstate(prvm_prog_t *prog)
4143 {
4144         const char *name;
4145         clvideostate_t  state;
4146         clvideo_t               *video;
4147
4148         VM_SAFEPARMCOUNT( 2, VM_cin_netstate );
4149
4150         name = PRVM_G_STRING( OFS_PARM0 );
4151         VM_CheckEmptyString(prog,  name );
4152
4153         state = (clvideostate_t)((int)PRVM_G_FLOAT( OFS_PARM1 ));
4154
4155         video = CL_GetVideoByName( name );
4156         if( video && state > CLVIDEO_UNUSED && state < CLVIDEO_STATECOUNT )
4157                 CL_SetVideoState( video, state );
4158 }
4159
4160 /*
4161 ========================
4162 VM_cin_getstate
4163
4164 float cin_getstate(string name)
4165 ========================
4166 */
4167 void VM_cin_getstate(prvm_prog_t *prog)
4168 {
4169         const char *name;
4170         clvideo_t               *video;
4171
4172         VM_SAFEPARMCOUNT( 1, VM_cin_getstate );
4173
4174         name = PRVM_G_STRING( OFS_PARM0 );
4175         VM_CheckEmptyString(prog,  name );
4176
4177         video = CL_GetVideoByName( name );
4178         if( video )
4179                 PRVM_G_FLOAT( OFS_RETURN ) = (int)video->state;
4180         else
4181                 PRVM_G_FLOAT( OFS_RETURN ) = 0;
4182 }
4183
4184 /*
4185 ========================
4186 VM_cin_restart
4187
4188 void cin_restart(string name)
4189 ========================
4190 */
4191 void VM_cin_restart(prvm_prog_t *prog)
4192 {
4193         const char *name;
4194         clvideo_t               *video;
4195
4196         VM_SAFEPARMCOUNT( 1, VM_cin_restart );
4197
4198         name = PRVM_G_STRING( OFS_PARM0 );
4199         VM_CheckEmptyString(prog,  name );
4200
4201         video = CL_GetVideoByName( name );
4202         if( video )
4203                 CL_RestartVideo( video );
4204 }
4205
4206 /*
4207 ========================
4208 VM_gecko_create
4209
4210 float[bool] gecko_create( string name )
4211 ========================
4212 */
4213 void VM_gecko_create(prvm_prog_t *prog) {
4214         // REMOVED
4215         PRVM_G_FLOAT( OFS_RETURN ) = 0;
4216 }
4217
4218 /*
4219 ========================
4220 VM_gecko_destroy
4221
4222 void gecko_destroy( string name )
4223 ========================
4224 */
4225 void VM_gecko_destroy(prvm_prog_t *prog) {
4226         // REMOVED
4227 }
4228
4229 /*
4230 ========================
4231 VM_gecko_navigate
4232
4233 void gecko_navigate( string name, string URI )
4234 ========================
4235 */
4236 void VM_gecko_navigate(prvm_prog_t *prog) {
4237         // REMOVED
4238 }
4239
4240 /*
4241 ========================
4242 VM_gecko_keyevent
4243
4244 float[bool] gecko_keyevent( string name, float key, float eventtype ) 
4245 ========================
4246 */
4247 void VM_gecko_keyevent(prvm_prog_t *prog) {
4248         // REMOVED
4249         PRVM_G_FLOAT( OFS_RETURN ) = 0;
4250 }
4251
4252 /*
4253 ========================
4254 VM_gecko_movemouse
4255
4256 void gecko_mousemove( string name, float x, float y )
4257 ========================
4258 */
4259 void VM_gecko_movemouse(prvm_prog_t *prog) {
4260         // REMOVED
4261 }
4262
4263
4264 /*
4265 ========================
4266 VM_gecko_resize
4267
4268 void gecko_resize( string name, float w, float h )
4269 ========================
4270 */
4271 void VM_gecko_resize(prvm_prog_t *prog) {
4272         // REMOVED
4273 }
4274
4275
4276 /*
4277 ========================
4278 VM_gecko_get_texture_extent
4279
4280 vector gecko_get_texture_extent( string name )
4281 ========================
4282 */
4283 void VM_gecko_get_texture_extent(prvm_prog_t *prog) {
4284         // REMOVED
4285         PRVM_G_VECTOR(OFS_RETURN)[0] = 0;
4286         PRVM_G_VECTOR(OFS_RETURN)[1] = 0;
4287 }
4288
4289
4290
4291 /*
4292 ==============
4293 VM_makevectors
4294
4295 Writes new values for v_forward, v_up, and v_right based on angles
4296 void makevectors(vector angle)
4297 ==============
4298 */
4299 void VM_makevectors (prvm_prog_t *prog)
4300 {
4301         VM_SAFEPARMCOUNT(1, VM_makevectors);
4302         AngleVectors(PRVM_G_VECTOR(OFS_PARM0), PRVM_gameglobalvector(v_forward), PRVM_gameglobalvector(v_right), PRVM_gameglobalvector(v_up));
4303 }
4304
4305 /*
4306 ==============
4307 VM_vectorvectors
4308
4309 Writes new values for v_forward, v_up, and v_right based on the given forward vector
4310 vectorvectors(vector)
4311 ==============
4312 */
4313 void VM_vectorvectors (prvm_prog_t *prog)
4314 {
4315         VM_SAFEPARMCOUNT(1, VM_vectorvectors);
4316         VectorNormalize2(PRVM_G_VECTOR(OFS_PARM0), PRVM_gameglobalvector(v_forward));
4317         VectorVectors(PRVM_gameglobalvector(v_forward), PRVM_gameglobalvector(v_right), PRVM_gameglobalvector(v_up));
4318 }
4319
4320 /*
4321 ========================
4322 VM_drawline
4323
4324 void drawline(float width, vector pos1, vector pos2, vector rgb, float alpha, float flags)
4325 ========================
4326 */
4327 void VM_drawline (prvm_prog_t *prog)
4328 {
4329         float   *c1, *c2, *rgb;
4330         float   alpha, width;
4331         unsigned char   flags;
4332
4333         VM_SAFEPARMCOUNT(6, VM_drawline);
4334         width   = PRVM_G_FLOAT(OFS_PARM0);
4335         c1              = PRVM_G_VECTOR(OFS_PARM1);
4336         c2              = PRVM_G_VECTOR(OFS_PARM2);
4337         rgb             = PRVM_G_VECTOR(OFS_PARM3);
4338         alpha   = PRVM_G_FLOAT(OFS_PARM4);
4339         flags   = (int)PRVM_G_FLOAT(OFS_PARM5);
4340         DrawQ_Line(width, c1[0], c1[1], c2[0], c2[1], rgb[0], rgb[1], rgb[2], alpha, flags);
4341 }
4342
4343 // float(float number, float quantity) bitshift (EXT_BITSHIFT)
4344 void VM_bitshift (prvm_prog_t *prog)
4345 {
4346         int n1, n2;
4347         VM_SAFEPARMCOUNT(2, VM_bitshift);
4348
4349         n1 = (int)fabs((float)((int)PRVM_G_FLOAT(OFS_PARM0)));
4350         n2 = (int)PRVM_G_FLOAT(OFS_PARM1);
4351         if(!n1)
4352                 PRVM_G_FLOAT(OFS_RETURN) = n1;
4353         else
4354         if(n2 < 0)
4355                 PRVM_G_FLOAT(OFS_RETURN) = (n1 >> -n2);
4356         else
4357                 PRVM_G_FLOAT(OFS_RETURN) = (n1 << n2);
4358 }
4359
4360 ////////////////////////////////////////
4361 // AltString functions
4362 ////////////////////////////////////////
4363
4364 /*
4365 ========================
4366 VM_altstr_count
4367
4368 float altstr_count(string)
4369 ========================
4370 */
4371 void VM_altstr_count(prvm_prog_t *prog)
4372 {
4373         const char *altstr, *pos;
4374         int     count;
4375
4376         VM_SAFEPARMCOUNT( 1, VM_altstr_count );
4377
4378         altstr = PRVM_G_STRING( OFS_PARM0 );
4379         //VM_CheckEmptyString(prog,  altstr );
4380
4381         for( count = 0, pos = altstr ; *pos ; pos++ ) {
4382                 if( *pos == '\\' ) {
4383                         if( !*++pos ) {
4384                                 break;
4385                         }
4386                 } else if( *pos == '\'' ) {
4387                         count++;
4388                 }
4389         }
4390
4391         PRVM_G_FLOAT( OFS_RETURN ) = (float) (count / 2);
4392 }
4393
4394 /*
4395 ========================
4396 VM_altstr_prepare
4397
4398 string altstr_prepare(string)
4399 ========================
4400 */
4401 void VM_altstr_prepare(prvm_prog_t *prog)
4402 {
4403         char *out;
4404         const char *instr, *in;
4405         int size;
4406         char outstr[VM_STRINGTEMP_LENGTH];
4407
4408         VM_SAFEPARMCOUNT( 1, VM_altstr_prepare );
4409
4410         instr = PRVM_G_STRING( OFS_PARM0 );
4411
4412         for( out = outstr, in = instr, size = sizeof(outstr) - 1 ; size && *in ; size--, in++, out++ )
4413                 if( *in == '\'' ) {
4414                         *out++ = '\\';
4415                         *out = '\'';
4416                         size--;
4417                 } else
4418                         *out = *in;
4419         *out = 0;
4420
4421         PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString(prog,  outstr );
4422 }
4423
4424 /*
4425 ========================
4426 VM_altstr_get
4427
4428 string altstr_get(string, float)
4429 ========================
4430 */
4431 void VM_altstr_get(prvm_prog_t *prog)
4432 {
4433         const char *altstr, *pos;
4434         char *out;
4435         int count, size;
4436         char outstr[VM_STRINGTEMP_LENGTH];
4437
4438         VM_SAFEPARMCOUNT( 2, VM_altstr_get );
4439
4440         altstr = PRVM_G_STRING( OFS_PARM0 );
4441
4442         count = (int)PRVM_G_FLOAT( OFS_PARM1 );
4443         count = count * 2 + 1;
4444
4445         for( pos = altstr ; *pos && count ; pos++ )
4446                 if( *pos == '\\' ) {
4447                         if( !*++pos )
4448                                 break;
4449                 } else if( *pos == '\'' )
4450                         count--;
4451
4452         if( !*pos ) {
4453                 PRVM_G_INT( OFS_RETURN ) = 0;
4454                 return;
4455         }
4456
4457         for( out = outstr, size = sizeof(outstr) - 1 ; size && *pos ; size--, pos++, out++ )
4458                 if( *pos == '\\' ) {
4459                         if( !*++pos )
4460                                 break;
4461                         *out = *pos;
4462                         size--;
4463                 } else if( *pos == '\'' )
4464                         break;
4465                 else
4466                         *out = *pos;
4467
4468         *out = 0;
4469         PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString(prog,  outstr );
4470 }
4471
4472 /*
4473 ========================
4474 VM_altstr_set
4475
4476 string altstr_set(string altstr, float num, string set)
4477 ========================
4478 */
4479 void VM_altstr_set(prvm_prog_t *prog)
4480 {
4481     int num;
4482         const char *altstr, *str;
4483         const char *in;
4484         char *out;
4485         char outstr[VM_STRINGTEMP_LENGTH];
4486
4487         VM_SAFEPARMCOUNT( 3, VM_altstr_set );
4488
4489         altstr = PRVM_G_STRING( OFS_PARM0 );
4490
4491         num = (int)PRVM_G_FLOAT( OFS_PARM1 );
4492
4493         str = PRVM_G_STRING( OFS_PARM2 );
4494
4495         out = outstr;
4496         for( num = num * 2 + 1, in = altstr; *in && num; *out++ = *in++ )