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