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