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