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