]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - prvm_cmds.c
implemented QW skin support, it's broken on skins that are not the same
[xonotic/darkplaces.git] / prvm_cmds.c
1 // AK
2 // Basically every vm builtin cmd should be in here.
3 // All 3 builtin and extension lists can be found here
4 // cause large (I think they will) parts are from pr_cmds the same copyright like in pr_cmds
5 // also applies here
6
7 #include "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
1758         // Prepare Strings
1759         VM_SAFEPARMCOUNT(1,VM_strdecolorize);
1760         szString = PRVM_G_STRING(OFS_PARM0);
1761
1762         COM_StringDecolorize(szString, 0, szNewString, sizeof(szNewString), TRUE);
1763
1764         PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(szNewString);
1765 }
1766
1767 // DRESK - String Length (not counting color codes)
1768 /*
1769 =========
1770 VM_strlennocol
1771
1772 float   strlennocol(string s)
1773 =========
1774 */
1775 // float(string s) strlennocol = #471; // returns how many characters are in a string not including color codes
1776 // For example, ^2Dresk returns a length of 5
1777 void VM_strlennocol(void)
1778 {
1779         const char *szString;
1780         int nCnt;
1781
1782         VM_SAFEPARMCOUNT(1,VM_strlennocol);
1783
1784         szString = PRVM_G_STRING(OFS_PARM0);
1785
1786         nCnt = COM_StringLengthNoColors(szString, 0, NULL);
1787
1788         PRVM_G_FLOAT(OFS_RETURN) = nCnt;
1789 }
1790
1791 /*
1792 =========
1793 VM_strcat
1794
1795 string strcat(string,string,...[string])
1796 =========
1797 */
1798 //string(string s1, string s2) strcat = #115;
1799 // concatenates two strings (for example "abc", "def" would return "abcdef")
1800 // and returns as a tempstring
1801 void VM_strcat(void)
1802 {
1803         char s[VM_STRINGTEMP_LENGTH];
1804         VM_SAFEPARMCOUNTRANGE(1, 8, VM_strcat);
1805
1806         VM_VarString(0, s, sizeof(s));
1807         PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(s);
1808 }
1809
1810 /*
1811 =========
1812 VM_substring
1813
1814 string  substring(string s, float start, float length)
1815 =========
1816 */
1817 // string(string s, float start, float length) substring = #116;
1818 // returns a section of a string as a tempstring
1819 void VM_substring(void)
1820 {
1821         int i, start, length;
1822         const char *s;
1823         char string[VM_STRINGTEMP_LENGTH];
1824
1825         VM_SAFEPARMCOUNT(3,VM_substring);
1826
1827         s = PRVM_G_STRING(OFS_PARM0);
1828         start = (int)PRVM_G_FLOAT(OFS_PARM1);
1829         length = (int)PRVM_G_FLOAT(OFS_PARM2);
1830         for (i = 0;i < start && *s;i++, s++);
1831         for (i = 0;i < (int)sizeof(string) - 1 && *s && i < length;i++, s++)
1832                 string[i] = *s;
1833         string[i] = 0;
1834         PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string);
1835 }
1836
1837 /*
1838 =========
1839 VM_stov
1840
1841 vector  stov(string s)
1842 =========
1843 */
1844 //vector(string s) stov = #117; // returns vector value from a string
1845 void VM_stov(void)
1846 {
1847         char string[VM_STRINGTEMP_LENGTH];
1848
1849         VM_SAFEPARMCOUNT(1,VM_stov);
1850
1851         VM_VarString(0, string, sizeof(string));
1852         Math_atov(string, PRVM_G_VECTOR(OFS_RETURN));
1853 }
1854
1855 /*
1856 =========
1857 VM_strzone
1858
1859 string  strzone(string s)
1860 =========
1861 */
1862 //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)
1863 void VM_strzone(void)
1864 {
1865         char *out;
1866         char string[VM_STRINGTEMP_LENGTH];
1867         size_t alloclen;
1868
1869         VM_SAFEPARMCOUNT(1,VM_strzone);
1870
1871         VM_VarString(0, string, sizeof(string));
1872         alloclen = strlen(string) + 1;
1873         PRVM_G_INT(OFS_RETURN) = PRVM_AllocString(alloclen, &out);
1874         memcpy(out, string, alloclen);
1875 }
1876
1877 /*
1878 =========
1879 VM_strunzone
1880
1881 strunzone(string s)
1882 =========
1883 */
1884 //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!!!)
1885 void VM_strunzone(void)
1886 {
1887         VM_SAFEPARMCOUNT(1,VM_strunzone);
1888         PRVM_FreeString(PRVM_G_INT(OFS_PARM0));
1889 }
1890
1891 /*
1892 =========
1893 VM_command (used by client and menu)
1894
1895 clientcommand(float client, string s) (for client and menu)
1896 =========
1897 */
1898 //void(entity e, string s) clientcommand = #440; // executes a command string as if it came from the specified client
1899 //this function originally written by KrimZon, made shorter by LordHavoc
1900 void VM_clcommand (void)
1901 {
1902         client_t *temp_client;
1903         int i;
1904
1905         VM_SAFEPARMCOUNT(2,VM_clcommand);
1906
1907         i = (int)PRVM_G_FLOAT(OFS_PARM0);
1908         if (!sv.active  || i < 0 || i >= svs.maxclients || !svs.clients[i].active)
1909         {
1910                 VM_Warning("VM_clientcommand: %s: invalid client/server is not active !\n", PRVM_NAME);
1911                 return;
1912         }
1913
1914         temp_client = host_client;
1915         host_client = svs.clients + i;
1916         Cmd_ExecuteString (PRVM_G_STRING(OFS_PARM1), src_client);
1917         host_client = temp_client;
1918 }
1919
1920
1921 /*
1922 =========
1923 VM_tokenize
1924
1925 float tokenize(string s)
1926 =========
1927 */
1928 //float(string s) tokenize = #441; // takes apart a string into individal words (access them with argv), returns how many
1929 //this function originally written by KrimZon, made shorter by LordHavoc
1930 //20040203: rewritten by LordHavoc (no longer uses allocations)
1931 int num_tokens = 0;
1932 int tokens[256];
1933 void VM_tokenize (void)
1934 {
1935         const char *p;
1936
1937         VM_SAFEPARMCOUNT(1,VM_tokenize);
1938
1939         p = PRVM_G_STRING(OFS_PARM0);
1940
1941         num_tokens = 0;
1942         while(COM_ParseToken(&p, false))
1943         {
1944                 if (num_tokens >= (int)(sizeof(tokens)/sizeof(tokens[0])))
1945                         break;
1946                 tokens[num_tokens++] = PRVM_SetTempString(com_token);
1947         }
1948
1949         PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
1950 }
1951
1952 /*
1953 =========
1954 VM_tokenizebyseparator
1955
1956 float tokenizebyseparator(string s, string separator1, ...)
1957 =========
1958 */
1959 //float(string s, string separator1, ...) tokenizebyseparator = #479; // takes apart a string into individal words (access them with argv), returns how many
1960 //this function returns the token preceding each instance of a separator (of
1961 //which there can be multiple), and the text following the last separator
1962 //useful for parsing certain kinds of data like IP addresses
1963 //example:
1964 //numnumbers = tokenizebyseparator("10.1.2.3", ".");
1965 //returns 4 and the tokens "10" "1" "2" "3".
1966 void VM_tokenizebyseparator (void)
1967 {
1968         int j, k;
1969         int numseparators;
1970         int separatorlen[7];
1971         const char *separators[7];
1972         const char *p;
1973         char tokentext[MAX_INPUTLINE];
1974
1975         VM_SAFEPARMCOUNTRANGE(2, 8,VM_tokenizebyseparator);
1976
1977         p = PRVM_G_STRING(OFS_PARM0);
1978
1979         numseparators = 0;;
1980         for (j = 1;j < prog->argc;j++)
1981         {
1982                 // skip any blank separator strings
1983                 if (!PRVM_G_STRING(OFS_PARM0 + j)[0])
1984                         continue;
1985                 separators[numseparators] = PRVM_G_STRING(OFS_PARM0 + j);
1986                 separatorlen[numseparators] = strlen(separators[numseparators]);
1987                 numseparators++;
1988         }
1989
1990         num_tokens = 0;
1991         for (num_tokens = 0;num_tokens < (int)(sizeof(tokens)/sizeof(tokens[0]));num_tokens++)
1992         {
1993                 while (*p)
1994                 {
1995                         for (k = 0;k < numseparators;k++)
1996                         {
1997                                 if (!strncmp(p, separators[k], separatorlen[k]))
1998                                 {
1999                                         p += separatorlen[k];
2000                                         break;
2001                                 }
2002                         }
2003                         if (k < numseparators)
2004                                 break;
2005                         if (j < (int)sizeof(tokentext[MAX_INPUTLINE]-1))
2006                                 tokentext[j++] = *p;
2007                         p++;
2008                 }
2009                 tokentext[j] = 0;
2010                 tokens[num_tokens] = PRVM_SetTempString(tokentext);
2011                 if (!*p)
2012                         break;
2013         }
2014
2015         PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
2016 }
2017
2018 //string(float n) argv = #442; // returns a word from the tokenized string (returns nothing for an invalid index)
2019 //this function originally written by KrimZon, made shorter by LordHavoc
2020 void VM_argv (void)
2021 {
2022         int token_num;
2023
2024         VM_SAFEPARMCOUNT(1,VM_argv);
2025
2026         token_num = (int)PRVM_G_FLOAT(OFS_PARM0);
2027
2028         if (token_num >= 0 && token_num < num_tokens)
2029                 PRVM_G_INT(OFS_RETURN) = tokens[token_num];
2030         else
2031                 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
2032 }
2033
2034 /*
2035 =========
2036 VM_isserver
2037
2038 float   isserver()
2039 =========
2040 */
2041 void VM_isserver(void)
2042 {
2043         VM_SAFEPARMCOUNT(0,VM_serverstate);
2044
2045         PRVM_G_FLOAT(OFS_RETURN) = sv.active && (svs.maxclients > 1 || cls.state == ca_dedicated);
2046 }
2047
2048 /*
2049 =========
2050 VM_clientcount
2051
2052 float   clientcount()
2053 =========
2054 */
2055 void VM_clientcount(void)
2056 {
2057         VM_SAFEPARMCOUNT(0,VM_clientcount);
2058
2059         PRVM_G_FLOAT(OFS_RETURN) = svs.maxclients;
2060 }
2061
2062 /*
2063 =========
2064 VM_clientstate
2065
2066 float   clientstate()
2067 =========
2068 */
2069 void VM_clientstate(void)
2070 {
2071         VM_SAFEPARMCOUNT(0,VM_clientstate);
2072
2073         PRVM_G_FLOAT(OFS_RETURN) = cls.state;
2074 }
2075
2076 /*
2077 =========
2078 VM_getostype
2079
2080 float   getostype(void)
2081 =========
2082 */ // not used at the moment -> not included in the common list
2083 void VM_getostype(void)
2084 {
2085         VM_SAFEPARMCOUNT(0,VM_getostype);
2086
2087         /*
2088         OS_WINDOWS
2089         OS_LINUX
2090         OS_MAC - not supported
2091         */
2092
2093 #ifdef WIN32
2094         PRVM_G_FLOAT(OFS_RETURN) = 0;
2095 #elif defined(MACOSX)
2096         PRVM_G_FLOAT(OFS_RETURN) = 2;
2097 #else
2098         PRVM_G_FLOAT(OFS_RETURN) = 1;
2099 #endif
2100 }
2101
2102 /*
2103 =========
2104 VM_getmousepos
2105
2106 vector  getmousepos()
2107 =========
2108 */
2109 void VM_getmousepos(void)
2110 {
2111
2112         VM_SAFEPARMCOUNT(0,VM_getmousepos);
2113
2114         PRVM_G_VECTOR(OFS_RETURN)[0] = in_mouse_x * vid_conwidth.integer / vid.width;
2115         PRVM_G_VECTOR(OFS_RETURN)[1] = in_mouse_y * vid_conheight.integer / vid.height;
2116         PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
2117 }
2118
2119 /*
2120 =========
2121 VM_gettime
2122
2123 float   gettime(void)
2124 =========
2125 */
2126 void VM_gettime(void)
2127 {
2128         VM_SAFEPARMCOUNT(0,VM_gettime);
2129
2130         PRVM_G_FLOAT(OFS_RETURN) = (float) realtime;
2131 }
2132
2133 /*
2134 =========
2135 VM_loadfromdata
2136
2137 loadfromdata(string data)
2138 =========
2139 */
2140 void VM_loadfromdata(void)
2141 {
2142         VM_SAFEPARMCOUNT(1,VM_loadentsfromfile);
2143
2144         PRVM_ED_LoadFromFile(PRVM_G_STRING(OFS_PARM0));
2145 }
2146
2147 /*
2148 ========================
2149 VM_parseentitydata
2150
2151 parseentitydata(entity ent, string data)
2152 ========================
2153 */
2154 void VM_parseentitydata(void)
2155 {
2156         prvm_edict_t *ent;
2157         const char *data;
2158
2159         VM_SAFEPARMCOUNT(2, VM_parseentitydata);
2160
2161     // get edict and test it
2162         ent = PRVM_G_EDICT(OFS_PARM0);
2163         if (ent->priv.required->free)
2164                 PRVM_ERROR ("VM_parseentitydata: %s: Can only set already spawned entities (entity %i is free)!", PRVM_NAME, PRVM_NUM_FOR_EDICT(ent));
2165
2166         data = PRVM_G_STRING(OFS_PARM1);
2167
2168     // parse the opening brace
2169         if (!COM_ParseTokenConsole(&data) || com_token[0] != '{' )
2170                 PRVM_ERROR ("VM_parseentitydata: %s: Couldn't parse entity data:\n%s", PRVM_NAME, data );
2171
2172         PRVM_ED_ParseEdict (data, ent);
2173 }
2174
2175 /*
2176 =========
2177 VM_loadfromfile
2178
2179 loadfromfile(string file)
2180 =========
2181 */
2182 void VM_loadfromfile(void)
2183 {
2184         const char *filename;
2185         char *data;
2186
2187         VM_SAFEPARMCOUNT(1,VM_loadfromfile);
2188
2189         filename = PRVM_G_STRING(OFS_PARM0);
2190         if (FS_CheckNastyPath(filename, false))
2191         {
2192                 PRVM_G_FLOAT(OFS_RETURN) = -4;
2193                 VM_Warning("VM_loadfromfile: %s dangerous or non-portable filename \"%s\" not allowed. (contains : or \\ or begins with .. or /)\n", PRVM_NAME, filename);
2194                 return;
2195         }
2196
2197         // not conform with VM_fopen
2198         data = (char *)FS_LoadFile(filename, tempmempool, false, NULL);
2199         if (data == NULL)
2200                 PRVM_G_FLOAT(OFS_RETURN) = -1;
2201
2202         PRVM_ED_LoadFromFile(data);
2203
2204         if(data)
2205                 Mem_Free(data);
2206 }
2207
2208
2209 /*
2210 =========
2211 VM_modulo
2212
2213 float   mod(float val, float m)
2214 =========
2215 */
2216 void VM_modulo(void)
2217 {
2218         int val, m;
2219         VM_SAFEPARMCOUNT(2,VM_module);
2220
2221         val = (int) PRVM_G_FLOAT(OFS_PARM0);
2222         m       = (int) PRVM_G_FLOAT(OFS_PARM1);
2223
2224         PRVM_G_FLOAT(OFS_RETURN) = (float) (val % m);
2225 }
2226
2227 void VM_Search_Init(void)
2228 {
2229         int i;
2230         for (i = 0;i < PRVM_MAX_OPENSEARCHES;i++)
2231                 prog->opensearches[i] = NULL;
2232 }
2233
2234 void VM_Search_Reset(void)
2235 {
2236         int i;
2237         // reset the fssearch list
2238         for(i = 0; i < PRVM_MAX_OPENSEARCHES; i++)
2239         {
2240                 if(prog->opensearches[i])
2241                         FS_FreeSearch(prog->opensearches[i]);
2242                 prog->opensearches[i] = NULL;
2243         }
2244 }
2245
2246 /*
2247 =========
2248 VM_search_begin
2249
2250 float search_begin(string pattern, float caseinsensitive, float quiet)
2251 =========
2252 */
2253 void VM_search_begin(void)
2254 {
2255         int handle;
2256         const char *pattern;
2257         int caseinsens, quiet;
2258
2259         VM_SAFEPARMCOUNT(3, VM_search_begin);
2260
2261         pattern = PRVM_G_STRING(OFS_PARM0);
2262
2263         VM_CheckEmptyString(pattern);
2264
2265         caseinsens = (int)PRVM_G_FLOAT(OFS_PARM1);
2266         quiet = (int)PRVM_G_FLOAT(OFS_PARM2);
2267
2268         for(handle = 0; handle < PRVM_MAX_OPENSEARCHES; handle++)
2269                 if(!prog->opensearches[handle])
2270                         break;
2271
2272         if(handle >= PRVM_MAX_OPENSEARCHES)
2273         {
2274                 PRVM_G_FLOAT(OFS_RETURN) = -2;
2275                 VM_Warning("VM_search_begin: %s ran out of search handles (%i)\n", PRVM_NAME, PRVM_MAX_OPENSEARCHES);
2276                 return;
2277         }
2278
2279         if(!(prog->opensearches[handle] = FS_Search(pattern,caseinsens, quiet)))
2280                 PRVM_G_FLOAT(OFS_RETURN) = -1;
2281         else
2282                 PRVM_G_FLOAT(OFS_RETURN) = handle;
2283 }
2284
2285 /*
2286 =========
2287 VM_search_end
2288
2289 void    search_end(float handle)
2290 =========
2291 */
2292 void VM_search_end(void)
2293 {
2294         int handle;
2295         VM_SAFEPARMCOUNT(1, VM_search_end);
2296
2297         handle = (int)PRVM_G_FLOAT(OFS_PARM0);
2298
2299         if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
2300         {
2301                 VM_Warning("VM_search_end: invalid handle %i used in %s\n", handle, PRVM_NAME);
2302                 return;
2303         }
2304         if(prog->opensearches[handle] == NULL)
2305         {
2306                 VM_Warning("VM_search_end: no such handle %i in %s\n", handle, PRVM_NAME);
2307                 return;
2308         }
2309
2310         FS_FreeSearch(prog->opensearches[handle]);
2311         prog->opensearches[handle] = NULL;
2312 }
2313
2314 /*
2315 =========
2316 VM_search_getsize
2317
2318 float   search_getsize(float handle)
2319 =========
2320 */
2321 void VM_search_getsize(void)
2322 {
2323         int handle;
2324         VM_SAFEPARMCOUNT(1, VM_M_search_getsize);
2325
2326         handle = (int)PRVM_G_FLOAT(OFS_PARM0);
2327
2328         if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
2329         {
2330                 VM_Warning("VM_search_getsize: invalid handle %i used in %s\n", handle, PRVM_NAME);
2331                 return;
2332         }
2333         if(prog->opensearches[handle] == NULL)
2334         {
2335                 VM_Warning("VM_search_getsize: no such handle %i in %s\n", handle, PRVM_NAME);
2336                 return;
2337         }
2338
2339         PRVM_G_FLOAT(OFS_RETURN) = prog->opensearches[handle]->numfilenames;
2340 }
2341
2342 /*
2343 =========
2344 VM_search_getfilename
2345
2346 string  search_getfilename(float handle, float num)
2347 =========
2348 */
2349 void VM_search_getfilename(void)
2350 {
2351         int handle, filenum;
2352         VM_SAFEPARMCOUNT(2, VM_search_getfilename);
2353
2354         handle = (int)PRVM_G_FLOAT(OFS_PARM0);
2355         filenum = (int)PRVM_G_FLOAT(OFS_PARM1);
2356
2357         if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
2358         {
2359                 VM_Warning("VM_search_getfilename: invalid handle %i used in %s\n", handle, PRVM_NAME);
2360                 return;
2361         }
2362         if(prog->opensearches[handle] == NULL)
2363         {
2364                 VM_Warning("VM_search_getfilename: no such handle %i in %s\n", handle, PRVM_NAME);
2365                 return;
2366         }
2367         if(filenum < 0 || filenum >= prog->opensearches[handle]->numfilenames)
2368         {
2369                 VM_Warning("VM_search_getfilename: invalid filenum %i in %s\n", filenum, PRVM_NAME);
2370                 return;
2371         }
2372
2373         PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog->opensearches[handle]->filenames[filenum]);
2374 }
2375
2376 /*
2377 =========
2378 VM_chr
2379
2380 string  chr(float ascii)
2381 =========
2382 */
2383 void VM_chr(void)
2384 {
2385         char tmp[2];
2386         VM_SAFEPARMCOUNT(1, VM_chr);
2387
2388         tmp[0] = (unsigned char) PRVM_G_FLOAT(OFS_PARM0);
2389         tmp[1] = 0;
2390
2391         PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(tmp);
2392 }
2393
2394 //=============================================================================
2395 // Draw builtins (client & menu)
2396
2397 /*
2398 =========
2399 VM_iscachedpic
2400
2401 float   iscachedpic(string pic)
2402 =========
2403 */
2404 void VM_iscachedpic(void)
2405 {
2406         VM_SAFEPARMCOUNT(1,VM_iscachedpic);
2407
2408         // drawq hasnt such a function, thus always return true
2409         PRVM_G_FLOAT(OFS_RETURN) = false;
2410 }
2411
2412 /*
2413 =========
2414 VM_precache_pic
2415
2416 string  precache_pic(string pic)
2417 =========
2418 */
2419 void VM_precache_pic(void)
2420 {
2421         const char      *s;
2422
2423         VM_SAFEPARMCOUNT(1, VM_precache_pic);
2424
2425         s = PRVM_G_STRING(OFS_PARM0);
2426         PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
2427         VM_CheckEmptyString (s);
2428
2429         // AK Draw_CachePic is supposed to always return a valid pointer
2430         if( Draw_CachePic(s, false)->tex == r_texture_notexture )
2431                 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
2432 }
2433
2434 /*
2435 =========
2436 VM_freepic
2437
2438 freepic(string s)
2439 =========
2440 */
2441 void VM_freepic(void)
2442 {
2443         const char *s;
2444
2445         VM_SAFEPARMCOUNT(1,VM_freepic);
2446
2447         s = PRVM_G_STRING(OFS_PARM0);
2448         VM_CheckEmptyString (s);
2449
2450         Draw_FreePic(s);
2451 }
2452
2453 /*
2454 =========
2455 VM_drawcharacter
2456
2457 float   drawcharacter(vector position, float character, vector scale, vector rgb, float alpha, float flag)
2458 =========
2459 */
2460 void VM_drawcharacter(void)
2461 {
2462         float *pos,*scale,*rgb;
2463         char   character;
2464         int flag;
2465         VM_SAFEPARMCOUNT(6,VM_drawcharacter);
2466
2467         character = (char) PRVM_G_FLOAT(OFS_PARM1);
2468         if(character == 0)
2469         {
2470                 PRVM_G_FLOAT(OFS_RETURN) = -1;
2471                 VM_Warning("VM_drawcharacter: %s passed null character !\n",PRVM_NAME);
2472                 return;
2473         }
2474
2475         pos = PRVM_G_VECTOR(OFS_PARM0);
2476         scale = PRVM_G_VECTOR(OFS_PARM2);
2477         rgb = PRVM_G_VECTOR(OFS_PARM3);
2478         flag = (int)PRVM_G_FLOAT(OFS_PARM5);
2479
2480         if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2481         {
2482                 PRVM_G_FLOAT(OFS_RETURN) = -2;
2483                 VM_Warning("VM_drawcharacter: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2484                 return;
2485         }
2486
2487         if(pos[2] || scale[2])
2488                 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")));
2489
2490         if(!scale[0] || !scale[1])
2491         {
2492                 PRVM_G_FLOAT(OFS_RETURN) = -3;
2493                 VM_Warning("VM_drawcharacter: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
2494                 return;
2495         }
2496
2497         DrawQ_String (pos[0], pos[1], &character, 1, scale[0], scale[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM4), flag, NULL, true);
2498         PRVM_G_FLOAT(OFS_RETURN) = 1;
2499 }
2500
2501 /*
2502 =========
2503 VM_drawstring
2504
2505 float   drawstring(vector position, string text, vector scale, vector rgb, float alpha, float flag)
2506 =========
2507 */
2508 void VM_drawstring(void)
2509 {
2510         float *pos,*scale,*rgb;
2511         const char  *string;
2512         int flag;
2513         VM_SAFEPARMCOUNT(6,VM_drawstring);
2514
2515         string = PRVM_G_STRING(OFS_PARM1);
2516         pos = PRVM_G_VECTOR(OFS_PARM0);
2517         scale = PRVM_G_VECTOR(OFS_PARM2);
2518         rgb = PRVM_G_VECTOR(OFS_PARM3);
2519         flag = (int)PRVM_G_FLOAT(OFS_PARM5);
2520
2521         if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2522         {
2523                 PRVM_G_FLOAT(OFS_RETURN) = -2;
2524                 VM_Warning("VM_drawstring: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2525                 return;
2526         }
2527
2528         if(!scale[0] || !scale[1])
2529         {
2530                 PRVM_G_FLOAT(OFS_RETURN) = -3;
2531                 VM_Warning("VM_drawstring: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
2532                 return;
2533         }
2534
2535         if(pos[2] || scale[2])
2536                 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")));
2537
2538         DrawQ_String (pos[0], pos[1], string, 0, scale[0], scale[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM4), flag, NULL, true);
2539         PRVM_G_FLOAT(OFS_RETURN) = 1;
2540 }
2541 /*
2542 =========
2543 VM_drawpic
2544
2545 float   drawpic(vector position, string pic, vector size, vector rgb, float alpha, float flag)
2546 =========
2547 */
2548 void VM_drawpic(void)
2549 {
2550         const char *picname;
2551         float *size, *pos, *rgb;
2552         int flag;
2553
2554         VM_SAFEPARMCOUNT(6,VM_drawpic);
2555
2556         picname = PRVM_G_STRING(OFS_PARM1);
2557         VM_CheckEmptyString (picname);
2558
2559         // is pic cached ? no function yet for that
2560         if(!1)
2561         {
2562                 PRVM_G_FLOAT(OFS_RETURN) = -4;
2563                 VM_Warning("VM_drawpic: %s: %s not cached !\n", PRVM_NAME, picname);
2564                 return;
2565         }
2566
2567         pos = PRVM_G_VECTOR(OFS_PARM0);
2568         size = PRVM_G_VECTOR(OFS_PARM2);
2569         rgb = PRVM_G_VECTOR(OFS_PARM3);
2570         flag = (int) PRVM_G_FLOAT(OFS_PARM5);
2571
2572         if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2573         {
2574                 PRVM_G_FLOAT(OFS_RETURN) = -2;
2575                 VM_Warning("VM_drawpic: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2576                 return;
2577         }
2578
2579         if(pos[2] || size[2])
2580                 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")));
2581
2582         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);
2583         PRVM_G_FLOAT(OFS_RETURN) = 1;
2584 }
2585
2586 /*
2587 =========
2588 VM_drawfill
2589
2590 float drawfill(vector position, vector size, vector rgb, float alpha, float flag)
2591 =========
2592 */
2593 void VM_drawfill(void)
2594 {
2595         float *size, *pos, *rgb;
2596         int flag;
2597
2598         VM_SAFEPARMCOUNT(5,VM_drawfill);
2599
2600
2601         pos = PRVM_G_VECTOR(OFS_PARM0);
2602         size = PRVM_G_VECTOR(OFS_PARM1);
2603         rgb = PRVM_G_VECTOR(OFS_PARM2);
2604         flag = (int) PRVM_G_FLOAT(OFS_PARM4);
2605
2606         if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2607         {
2608                 PRVM_G_FLOAT(OFS_RETURN) = -2;
2609                 VM_Warning("VM_drawfill: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2610                 return;
2611         }
2612
2613         if(pos[2] || size[2])
2614                 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")));
2615
2616         DrawQ_Fill(pos[0], pos[1], size[0], size[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM3), flag);
2617         PRVM_G_FLOAT(OFS_RETURN) = 1;
2618 }
2619
2620 /*
2621 =========
2622 VM_drawsetcliparea
2623
2624 drawsetcliparea(float x, float y, float width, float height)
2625 =========
2626 */
2627 void VM_drawsetcliparea(void)
2628 {
2629         float x,y,w,h;
2630         VM_SAFEPARMCOUNT(4,VM_drawsetcliparea);
2631
2632         x = bound(0, PRVM_G_FLOAT(OFS_PARM0), vid_conwidth.integer);
2633         y = bound(0, PRVM_G_FLOAT(OFS_PARM1), vid_conheight.integer);
2634         w = bound(0, PRVM_G_FLOAT(OFS_PARM2) + PRVM_G_FLOAT(OFS_PARM0) - x, (vid_conwidth.integer  - x));
2635         h = bound(0, PRVM_G_FLOAT(OFS_PARM3) + PRVM_G_FLOAT(OFS_PARM1) - y, (vid_conheight.integer - y));
2636
2637         DrawQ_SetClipArea(x, y, w, h);
2638 }
2639
2640 /*
2641 =========
2642 VM_drawresetcliparea
2643
2644 drawresetcliparea()
2645 =========
2646 */
2647 void VM_drawresetcliparea(void)
2648 {
2649         VM_SAFEPARMCOUNT(0,VM_drawresetcliparea);
2650
2651         DrawQ_ResetClipArea();
2652 }
2653
2654 /*
2655 =========
2656 VM_getimagesize
2657
2658 vector  getimagesize(string pic)
2659 =========
2660 */
2661 void VM_getimagesize(void)
2662 {
2663         const char *p;
2664         cachepic_t *pic;
2665
2666         VM_SAFEPARMCOUNT(1,VM_getimagesize);
2667
2668         p = PRVM_G_STRING(OFS_PARM0);
2669         VM_CheckEmptyString (p);
2670
2671         pic = Draw_CachePic (p, false);
2672
2673         PRVM_G_VECTOR(OFS_RETURN)[0] = pic->width;
2674         PRVM_G_VECTOR(OFS_RETURN)[1] = pic->height;
2675         PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
2676 }
2677
2678 /*
2679 =========
2680 VM_keynumtostring
2681
2682 string keynumtostring(float keynum)
2683 =========
2684 */
2685 void VM_keynumtostring (void)
2686 {
2687         VM_SAFEPARMCOUNT(1, VM_keynumtostring);
2688
2689         PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(Key_KeynumToString((int)PRVM_G_FLOAT(OFS_PARM0)));
2690 }
2691
2692 /*
2693 =========
2694 VM_stringtokeynum
2695
2696 float stringtokeynum(string key)
2697 =========
2698 */
2699 void VM_stringtokeynum (void)
2700 {
2701         VM_SAFEPARMCOUNT( 1, VM_keynumtostring );
2702
2703         PRVM_G_INT(OFS_RETURN) = Key_StringToKeynum(PRVM_G_STRING(OFS_PARM0));
2704 }
2705
2706 // CL_Video interface functions
2707
2708 /*
2709 ========================
2710 VM_cin_open
2711
2712 float cin_open(string file, string name)
2713 ========================
2714 */
2715 void VM_cin_open( void )
2716 {
2717         const char *file;
2718         const char *name;
2719
2720         VM_SAFEPARMCOUNT( 2, VM_cin_open );
2721
2722         file = PRVM_G_STRING( OFS_PARM0 );
2723         name = PRVM_G_STRING( OFS_PARM1 );
2724
2725         VM_CheckEmptyString( file );
2726     VM_CheckEmptyString( name );
2727
2728         if( CL_OpenVideo( file, name, MENUOWNER ) )
2729                 PRVM_G_FLOAT( OFS_RETURN ) = 1;
2730         else
2731                 PRVM_G_FLOAT( OFS_RETURN ) = 0;
2732 }
2733
2734 /*
2735 ========================
2736 VM_cin_close
2737
2738 void cin_close(string name)
2739 ========================
2740 */
2741 void VM_cin_close( void )
2742 {
2743         const char *name;
2744
2745         VM_SAFEPARMCOUNT( 1, VM_cin_close );
2746
2747         name = PRVM_G_STRING( OFS_PARM0 );
2748         VM_CheckEmptyString( name );
2749
2750         CL_CloseVideo( CL_GetVideoByName( name ) );
2751 }
2752
2753 /*
2754 ========================
2755 VM_cin_setstate
2756 void cin_setstate(string name, float type)
2757 ========================
2758 */
2759 void VM_cin_setstate( void )
2760 {
2761         const char *name;
2762         clvideostate_t  state;
2763         clvideo_t               *video;
2764
2765         VM_SAFEPARMCOUNT( 2, VM_cin_netstate );
2766
2767         name = PRVM_G_STRING( OFS_PARM0 );
2768         VM_CheckEmptyString( name );
2769
2770         state = (clvideostate_t)((int)PRVM_G_FLOAT( OFS_PARM1 ));
2771
2772         video = CL_GetVideoByName( name );
2773         if( video && state > CLVIDEO_UNUSED && state < CLVIDEO_STATECOUNT )
2774                 CL_SetVideoState( video, state );
2775 }
2776
2777 /*
2778 ========================
2779 VM_cin_getstate
2780
2781 float cin_getstate(string name)
2782 ========================
2783 */
2784 void VM_cin_getstate( void )
2785 {
2786         const char *name;
2787         clvideo_t               *video;
2788
2789         VM_SAFEPARMCOUNT( 1, VM_cin_getstate );
2790
2791         name = PRVM_G_STRING( OFS_PARM0 );
2792         VM_CheckEmptyString( name );
2793
2794         video = CL_GetVideoByName( name );
2795         if( video )
2796                 PRVM_G_FLOAT( OFS_RETURN ) = (int)video->state;
2797         else
2798                 PRVM_G_FLOAT( OFS_RETURN ) = 0;
2799 }
2800
2801 /*
2802 ========================
2803 VM_cin_restart
2804
2805 void cin_restart(string name)
2806 ========================
2807 */
2808 void VM_cin_restart( void )
2809 {
2810         const char *name;
2811         clvideo_t               *video;
2812
2813         VM_SAFEPARMCOUNT( 1, VM_cin_restart );
2814
2815         name = PRVM_G_STRING( OFS_PARM0 );
2816         VM_CheckEmptyString( name );
2817
2818         video = CL_GetVideoByName( name );
2819         if( video )
2820                 CL_RestartVideo( video );
2821 }
2822
2823 /*
2824 ==============
2825 VM_makevectors
2826
2827 Writes new values for v_forward, v_up, and v_right based on angles
2828 void makevectors(vector angle)
2829 ==============
2830 */
2831 void VM_makevectors (void)
2832 {
2833         prvm_eval_t *valforward, *valright, *valup;
2834         valforward = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_forward);
2835         valright = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_right);
2836         valup = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_up);
2837         if (!valforward || !valright || !valup)
2838         {
2839                 VM_Warning("makevectors: could not find v_forward, v_right, or v_up global variables\n");
2840                 return;
2841         }
2842         VM_SAFEPARMCOUNT(1, VM_makevectors);
2843         AngleVectors (PRVM_G_VECTOR(OFS_PARM0), valforward->vector, valright->vector, valup->vector);
2844 }
2845
2846 /*
2847 ==============
2848 VM_vectorvectors
2849
2850 Writes new values for v_forward, v_up, and v_right based on the given forward vector
2851 vectorvectors(vector)
2852 ==============
2853 */
2854 void VM_vectorvectors (void)
2855 {
2856         prvm_eval_t *valforward, *valright, *valup;
2857         valforward = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_forward);
2858         valright = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_right);
2859         valup = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_up);
2860         if (!valforward || !valright || !valup)
2861         {
2862                 VM_Warning("vectorvectors: could not find v_forward, v_right, or v_up global variables\n");
2863                 return;
2864         }
2865         VM_SAFEPARMCOUNT(1, VM_vectorvectors);
2866         VectorNormalize2(PRVM_G_VECTOR(OFS_PARM0), valforward->vector);
2867         VectorVectors(valforward->vector, valright->vector, valup->vector);
2868 }
2869
2870 /*
2871 ========================
2872 VM_drawline
2873
2874 void drawline(float width, vector pos1, vector pos2, vector rgb, float alpha, float flags)
2875 ========================
2876 */
2877 void VM_drawline (void)
2878 {
2879         float   *c1, *c2, *rgb;
2880         float   alpha, width;
2881         unsigned char   flags;
2882
2883         VM_SAFEPARMCOUNT(6, VM_drawline);
2884         width   = PRVM_G_FLOAT(OFS_PARM0);
2885         c1              = PRVM_G_VECTOR(OFS_PARM1);
2886         c2              = PRVM_G_VECTOR(OFS_PARM2);
2887         rgb             = PRVM_G_VECTOR(OFS_PARM3);
2888         alpha   = PRVM_G_FLOAT(OFS_PARM4);
2889         flags   = (int)PRVM_G_FLOAT(OFS_PARM5);
2890         DrawQ_Line(width, c1[0], c1[1], c2[0], c2[1], rgb[0], rgb[1], rgb[2], alpha, flags);
2891 }
2892
2893
2894
2895
2896
2897 // float(float number, float quantity) bitshift (EXT_BITSHIFT)
2898 void VM_bitshift (void)
2899 {
2900         int n1, n2;
2901         VM_SAFEPARMCOUNT(2, VM_bitshift);
2902
2903         n1 = (int)fabs((int)PRVM_G_FLOAT(OFS_PARM0));
2904         n2 = (int)PRVM_G_FLOAT(OFS_PARM1);
2905         if(!n1)
2906                 PRVM_G_FLOAT(OFS_RETURN) = n1;
2907         else
2908         if(n2 < 0)
2909                 PRVM_G_FLOAT(OFS_RETURN) = (n1 >> -n2);
2910         else
2911                 PRVM_G_FLOAT(OFS_RETURN) = (n1 << n2);
2912 }
2913
2914 ////////////////////////////////////////
2915 // AltString functions
2916 ////////////////////////////////////////
2917
2918 /*
2919 ========================
2920 VM_altstr_count
2921
2922 float altstr_count(string)
2923 ========================
2924 */
2925 void VM_altstr_count( void )
2926 {
2927         const char *altstr, *pos;
2928         int     count;
2929
2930         VM_SAFEPARMCOUNT( 1, VM_altstr_count );
2931
2932         altstr = PRVM_G_STRING( OFS_PARM0 );
2933         //VM_CheckEmptyString( altstr );
2934
2935         for( count = 0, pos = altstr ; *pos ; pos++ ) {
2936                 if( *pos == '\\' ) {
2937                         if( !*++pos ) {
2938                                 break;
2939                         }
2940                 } else if( *pos == '\'' ) {
2941                         count++;
2942                 }
2943         }
2944
2945         PRVM_G_FLOAT( OFS_RETURN ) = (float) (count / 2);
2946 }
2947
2948 /*
2949 ========================
2950 VM_altstr_prepare
2951
2952 string altstr_prepare(string)
2953 ========================
2954 */
2955 void VM_altstr_prepare( void )
2956 {
2957         char *out;
2958         const char *instr, *in;
2959         int size;
2960         char outstr[VM_STRINGTEMP_LENGTH];
2961
2962         VM_SAFEPARMCOUNT( 1, VM_altstr_prepare );
2963
2964         instr = PRVM_G_STRING( OFS_PARM0 );
2965
2966         for( out = outstr, in = instr, size = sizeof(outstr) - 1 ; size && *in ; size--, in++, out++ )
2967                 if( *in == '\'' ) {
2968                         *out++ = '\\';
2969                         *out = '\'';
2970                         size--;
2971                 } else
2972                         *out = *in;
2973         *out = 0;
2974
2975         PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
2976 }
2977
2978 /*
2979 ========================
2980 VM_altstr_get
2981
2982 string altstr_get(string, float)
2983 ========================
2984 */
2985 void VM_altstr_get( void )
2986 {
2987         const char *altstr, *pos;
2988         char *out;
2989         int count, size;
2990         char outstr[VM_STRINGTEMP_LENGTH];
2991
2992         VM_SAFEPARMCOUNT( 2, VM_altstr_get );
2993
2994         altstr = PRVM_G_STRING( OFS_PARM0 );
2995
2996         count = (int)PRVM_G_FLOAT( OFS_PARM1 );
2997         count = count * 2 + 1;
2998
2999         for( pos = altstr ; *pos && count ; pos++ )
3000                 if( *pos == '\\' ) {
3001                         if( !*++pos )
3002                                 break;
3003                 } else if( *pos == '\'' )
3004                         count--;
3005
3006         if( !*pos ) {
3007                 PRVM_G_INT( OFS_RETURN ) = 0;
3008                 return;
3009         }
3010
3011         for( out = outstr, size = sizeof(outstr) - 1 ; size && *pos ; size--, pos++, out++ )
3012                 if( *pos == '\\' ) {
3013                         if( !*++pos )
3014                                 break;
3015                         *out = *pos;
3016                         size--;
3017                 } else if( *pos == '\'' )
3018                         break;
3019                 else
3020                         *out = *pos;
3021
3022         *out = 0;
3023         PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
3024 }
3025
3026 /*
3027 ========================
3028 VM_altstr_set
3029
3030 string altstr_set(string altstr, float num, string set)
3031 ========================
3032 */
3033 void VM_altstr_set( void )
3034 {
3035     int num;
3036         const char *altstr, *str;
3037         const char *in;
3038         char *out;
3039         char outstr[VM_STRINGTEMP_LENGTH];
3040
3041         VM_SAFEPARMCOUNT( 3, VM_altstr_set );
3042
3043         altstr = PRVM_G_STRING( OFS_PARM0 );
3044
3045         num = (int)PRVM_G_FLOAT( OFS_PARM1 );
3046
3047         str = PRVM_G_STRING( OFS_PARM2 );
3048
3049         out = outstr;
3050         for( num = num * 2 + 1, in = altstr; *in && num; *out++ = *in++ )
3051                 if( *in == '\\' ) {
3052                         if( !*++in ) {
3053                                 break;
3054                         }
3055                 } else if( *in == '\'' ) {
3056                         num--;
3057                 }
3058
3059         // copy set in
3060         for( ; *str; *out++ = *str++ );
3061         // now jump over the old content
3062         for( ; *in ; in++ )
3063                 if( *in == '\'' || (*in == '\\' && !*++in) )
3064                         break;
3065
3066         strlcpy(out, in, outstr + sizeof(outstr) - out);
3067         PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
3068 }
3069
3070 /*
3071 ========================
3072 VM_altstr_ins
3073 insert after num
3074 string  altstr_ins(string altstr, float num, string set)
3075 ========================
3076 */
3077 void VM_altstr_ins(void)
3078 {
3079         int num;
3080         const char *setstr;
3081         const char *set;
3082         const char *instr;
3083         const char *in;
3084         char *out;
3085         char outstr[VM_STRINGTEMP_LENGTH];
3086
3087         VM_SAFEPARMCOUNT(3, VM_altstr_ins);
3088
3089         in = instr = PRVM_G_STRING( OFS_PARM0 );
3090         num = (int)PRVM_G_FLOAT( OFS_PARM1 );
3091         set = setstr = PRVM_G_STRING( OFS_PARM2 );
3092
3093         out = outstr;
3094         for( num = num * 2 + 2 ; *in && num > 0 ; *out++ = *in++ )
3095                 if( *in == '\\' ) {
3096                         if( !*++in ) {
3097                                 break;
3098                         }
3099                 } else if( *in == '\'' ) {
3100                         num--;
3101                 }
3102
3103         *out++ = '\'';
3104         for( ; *set ; *out++ = *set++ );
3105         *out++ = '\'';
3106
3107         strlcpy(out, in, outstr + sizeof(outstr) - out);
3108         PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
3109 }
3110
3111
3112 ////////////////////////////////////////
3113 // BufString functions
3114 ////////////////////////////////////////
3115 //[515]: string buffers support
3116 #define MAX_QCSTR_BUFFERS 128
3117 #define MAX_QCSTR_STRINGS 1024
3118
3119 typedef struct
3120 {
3121         int             num_strings;
3122         char    *strings[MAX_QCSTR_STRINGS];
3123 }qcstrbuffer_t;
3124
3125 // FIXME: move stringbuffers to prog_t to allow multiple progs!
3126 static qcstrbuffer_t    *qcstringbuffers[MAX_QCSTR_BUFFERS];
3127 static int                              num_qcstringbuffers;
3128 static int                              buf_sortpower;
3129
3130 #define BUFSTR_BUFFER(a) (a>=MAX_QCSTR_BUFFERS) ? NULL : (qcstringbuffers[a])
3131 #define BUFSTR_ISFREE(a) (a<MAX_QCSTR_BUFFERS&&qcstringbuffers[a]&&qcstringbuffers[a]->num_strings<=0) ? 1 : 0
3132
3133 static int BufStr_FindFreeBuffer (void)
3134 {
3135         int     i;
3136         if(num_qcstringbuffers == MAX_QCSTR_BUFFERS)
3137                 return -1;
3138         for(i=0;i<MAX_QCSTR_BUFFERS;i++)
3139                 if(!qcstringbuffers[i])
3140                 {
3141                         qcstringbuffers[i] = (qcstrbuffer_t *)Z_Malloc(sizeof(qcstrbuffer_t));
3142                         memset(qcstringbuffers[i], 0, sizeof(qcstrbuffer_t));
3143                         return i;
3144                 }
3145         return -1;
3146 }
3147
3148 static void BufStr_ClearBuffer (int index)
3149 {
3150         qcstrbuffer_t   *b = qcstringbuffers[index];
3151         int                             i;
3152
3153         if(b)
3154         {
3155                 if(b->num_strings > 0)
3156                 {
3157                         for(i=0;i<b->num_strings;i++)
3158                                 if(b->strings[i])
3159                                         Z_Free(b->strings[i]);
3160                         num_qcstringbuffers--;
3161                 }
3162                 Z_Free(qcstringbuffers[index]);
3163                 qcstringbuffers[index] = NULL;
3164         }
3165 }
3166
3167 static int BufStr_FindFreeString (qcstrbuffer_t *b)
3168 {
3169         int                             i;
3170         for(i=0;i<b->num_strings;i++)
3171                 if(!b->strings[i] || !b->strings[i][0])
3172                         return i;
3173         if(i == MAX_QCSTR_STRINGS)      return -1;
3174         else                                            return i;
3175 }
3176
3177 static int BufStr_SortStringsUP (const void *in1, const void *in2)
3178 {
3179         const char *a, *b;
3180         a = *((const char **) in1);
3181         b = *((const char **) in2);
3182         if(!a[0])       return 1;
3183         if(!b[0])       return -1;
3184         return strncmp(a, b, buf_sortpower);
3185 }
3186
3187 static int BufStr_SortStringsDOWN (const void *in1, const void *in2)
3188 {
3189         const char *a, *b;
3190         a = *((const char **) in1);
3191         b = *((const char **) in2);
3192         if(!a[0])       return 1;
3193         if(!b[0])       return -1;
3194         return strncmp(b, a, buf_sortpower);
3195 }
3196
3197 /*
3198 ========================
3199 VM_buf_create
3200 creates new buffer, and returns it's index, returns -1 if failed
3201 float buf_create(void) = #460;
3202 ========================
3203 */
3204 void VM_buf_create (void)
3205 {
3206         int i;
3207         VM_SAFEPARMCOUNT(0, VM_buf_create);
3208         i = BufStr_FindFreeBuffer();
3209         if(i >= 0)
3210                 num_qcstringbuffers++;
3211         //else
3212                 //Con_Printf("VM_buf_create: buffers overflow in %s\n", PRVM_NAME);
3213         PRVM_G_FLOAT(OFS_RETURN) = i;
3214 }
3215
3216 /*
3217 ========================
3218 VM_buf_del
3219 deletes buffer and all strings in it
3220 void buf_del(float bufhandle) = #461;
3221 ========================
3222 */
3223 void VM_buf_del (void)
3224 {
3225         VM_SAFEPARMCOUNT(1, VM_buf_del);
3226         if(BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0)))
3227                 BufStr_ClearBuffer((int)PRVM_G_FLOAT(OFS_PARM0));
3228         else
3229         {
3230                 VM_Warning("VM_buf_del: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3231                 return;
3232         }
3233 }
3234
3235 /*
3236 ========================
3237 VM_buf_getsize
3238 how many strings are stored in buffer
3239 float buf_getsize(float bufhandle) = #462;
3240 ========================
3241 */
3242 void VM_buf_getsize (void)
3243 {
3244         qcstrbuffer_t   *b;
3245         VM_SAFEPARMCOUNT(1, VM_buf_getsize);
3246
3247         b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3248         if(!b)
3249         {
3250                 PRVM_G_FLOAT(OFS_RETURN) = -1;
3251                 VM_Warning("VM_buf_getsize: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3252                 return;
3253         }
3254         else
3255                 PRVM_G_FLOAT(OFS_RETURN) = b->num_strings;
3256 }
3257
3258 /*
3259 ========================
3260 VM_buf_copy
3261 copy all content from one buffer to another, make sure it exists
3262 void buf_copy(float bufhandle_from, float bufhandle_to) = #463;
3263 ========================
3264 */
3265 void VM_buf_copy (void)
3266 {
3267         qcstrbuffer_t   *b1, *b2;
3268         int                             i;
3269         VM_SAFEPARMCOUNT(2, VM_buf_copy);
3270
3271         b1 = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3272         if(!b1)
3273         {
3274                 VM_Warning("VM_buf_copy: invalid source buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3275                 return;
3276         }
3277         i = (int)PRVM_G_FLOAT(OFS_PARM1);
3278         if(i == (int)PRVM_G_FLOAT(OFS_PARM0))
3279         {
3280                 VM_Warning("VM_buf_copy: source == destination (%i) in %s\n", i, PRVM_NAME);
3281                 return;
3282         }
3283         b2 = BUFSTR_BUFFER(i);
3284         if(!b2)
3285         {
3286                 VM_Warning("VM_buf_copy: invalid destination buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM1), PRVM_NAME);
3287                 return;
3288         }
3289
3290         BufStr_ClearBuffer(i);
3291         qcstringbuffers[i] = (qcstrbuffer_t *)Z_Malloc(sizeof(qcstrbuffer_t));
3292         memset(qcstringbuffers[i], 0, sizeof(qcstrbuffer_t));
3293         b2->num_strings = b1->num_strings;
3294
3295         for(i=0;i<b1->num_strings;i++)
3296                 if(b1->strings[i] && b1->strings[i][0])
3297                 {
3298                         size_t stringlen;
3299                         stringlen = strlen(b1->strings[i]) + 1;
3300                         b2->strings[i] = (char *)Z_Malloc(stringlen);
3301                         if(!b2->strings[i])
3302                         {
3303                                 VM_Warning("VM_buf_copy: not enough memory for buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM1), PRVM_NAME);
3304                                 break;
3305                         }
3306                         memcpy(b2->strings[i], b1->strings[i], stringlen);
3307                 }
3308 }
3309
3310 /*
3311 ========================
3312 VM_buf_sort
3313 sort buffer by beginnings of strings (sortpower defaults it's lenght)
3314 "backward == TRUE" means that sorting goes upside-down
3315 void buf_sort(float bufhandle, float sortpower, float backward) = #464;
3316 ========================
3317 */
3318 void VM_buf_sort (void)
3319 {
3320         qcstrbuffer_t   *b;
3321         int                             i;
3322         VM_SAFEPARMCOUNT(3, VM_buf_sort);
3323
3324         b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3325         if(!b)
3326         {
3327                 VM_Warning("VM_buf_sort: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3328                 return;
3329         }
3330         if(b->num_strings <= 0)
3331         {
3332                 VM_Warning("VM_buf_sort: tried to sort empty buffer %i in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3333                 return;
3334         }
3335         buf_sortpower = (int)PRVM_G_FLOAT(OFS_PARM1);
3336         if(buf_sortpower <= 0)
3337                 buf_sortpower = 99999999;
3338
3339         if(!PRVM_G_FLOAT(OFS_PARM2))
3340                 qsort(b->strings, b->num_strings, sizeof(char*), BufStr_SortStringsUP);
3341         else
3342                 qsort(b->strings, b->num_strings, sizeof(char*), BufStr_SortStringsDOWN);
3343
3344         for(i=b->num_strings-1;i>=0;i--)        //[515]: delete empty lines
3345                 if(b->strings)
3346                 {
3347                         if(b->strings[i][0])
3348                                 break;
3349                         else
3350                         {
3351                                 Z_Free(b->strings[i]);
3352                                 --b->num_strings;
3353                                 b->strings[i] = NULL;
3354                         }
3355                 }
3356                 else
3357                         --b->num_strings;
3358 }
3359
3360 /*
3361 ========================
3362 VM_buf_implode
3363 concantenates all buffer string into one with "glue" separator and returns it as tempstring
3364 string buf_implode(float bufhandle, string glue) = #465;
3365 ========================
3366 */
3367 void VM_buf_implode (void)
3368 {
3369         qcstrbuffer_t   *b;
3370         char                    k[VM_STRINGTEMP_LENGTH];
3371         const char              *sep;
3372         int                             i;
3373         size_t                  l;
3374         VM_SAFEPARMCOUNT(2, VM_buf_implode);
3375
3376         b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3377         PRVM_G_INT(OFS_RETURN) = OFS_NULL;
3378         if(!b)
3379         {
3380                 VM_Warning("VM_buf_implode: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3381                 return;
3382         }
3383         if(!b->num_strings)
3384                 return;
3385         sep = PRVM_G_STRING(OFS_PARM1);
3386         k[0] = 0;
3387         for(l=i=0;i<b->num_strings;i++)
3388                 if(b->strings[i])
3389                 {
3390                         l += (i > 0 ? strlen(sep) : 0) + strlen(b->strings[i]);
3391                         if (l >= sizeof(k) - 1)
3392                                 break;
3393                         strlcat(k, sep, sizeof(k));
3394                         strlcat(k, b->strings[i], sizeof(k));
3395                 }
3396         PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(k);
3397 }
3398
3399 /*
3400 ========================
3401 VM_bufstr_get
3402 get a string from buffer, returns tempstring, dont str_unzone it!
3403 string bufstr_get(float bufhandle, float string_index) = #465;
3404 ========================
3405 */
3406 void VM_bufstr_get (void)
3407 {
3408         qcstrbuffer_t   *b;
3409         int                             strindex;
3410         VM_SAFEPARMCOUNT(2, VM_bufstr_get);
3411
3412         PRVM_G_INT(OFS_RETURN) = OFS_NULL;
3413         b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3414         if(!b)
3415         {
3416                 VM_Warning("VM_bufstr_get: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3417                 return;
3418         }
3419         strindex = (int)PRVM_G_FLOAT(OFS_PARM1);
3420         if(strindex < 0 || strindex > MAX_QCSTR_STRINGS)
3421         {
3422                 VM_Warning("VM_bufstr_get: invalid string index %i used in %s\n", strindex, PRVM_NAME);
3423                 return;
3424         }
3425         if(b->num_strings <= strindex)
3426                 return;
3427         if(b->strings[strindex])
3428                 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(b->strings[strindex]);
3429 }
3430
3431 /*
3432 ========================
3433 VM_bufstr_set
3434 copies a string into selected slot of buffer
3435 void bufstr_set(float bufhandle, float string_index, string str) = #466;
3436 ========================
3437 */
3438 void VM_bufstr_set (void)
3439 {
3440         int                             bufindex, strindex;
3441         qcstrbuffer_t   *b;
3442         const char              *news;
3443         size_t                  alloclen;
3444
3445         VM_SAFEPARMCOUNT(3, VM_bufstr_set);
3446
3447         bufindex = (int)PRVM_G_FLOAT(OFS_PARM0);
3448         b = BUFSTR_BUFFER(bufindex);
3449         if(!b)
3450         {
3451                 VM_Warning("VM_bufstr_set: invalid buffer %i used in %s\n", bufindex, PRVM_NAME);
3452                 return;
3453         }
3454         strindex = (int)PRVM_G_FLOAT(OFS_PARM1);
3455         if(strindex < 0 || strindex > MAX_QCSTR_STRINGS)
3456         {
3457                 VM_Warning("VM_bufstr_set: invalid string index %i used in %s\n", strindex, PRVM_NAME);
3458                 return;
3459         }
3460         news = PRVM_G_STRING(OFS_PARM2);
3461         if(b->strings[strindex])
3462                 Z_Free(b->strings[strindex]);
3463         alloclen = strlen(news) + 1;
3464         b->strings[strindex] = (char *)Z_Malloc(alloclen);
3465         memcpy(b->strings[strindex], news, alloclen);
3466 }
3467
3468 /*
3469 ========================
3470 VM_bufstr_add
3471 adds string to buffer in nearest free slot and returns it
3472 "order == TRUE" means that string will be added after last "full" slot
3473 float bufstr_add(float bufhandle, string str, float order) = #467;
3474 ========================
3475 */
3476 void VM_bufstr_add (void)
3477 {
3478         int                             bufindex, order, strindex;
3479         qcstrbuffer_t   *b;
3480         const char              *string;
3481         size_t                  alloclen;
3482
3483         VM_SAFEPARMCOUNT(3, VM_bufstr_add);
3484
3485         bufindex = (int)PRVM_G_FLOAT(OFS_PARM0);
3486         b = BUFSTR_BUFFER(bufindex);
3487         PRVM_G_FLOAT(OFS_RETURN) = -1;
3488         if(!b)
3489         {
3490                 VM_Warning("VM_bufstr_add: invalid buffer %i used in %s\n", bufindex, PRVM_NAME);
3491                 return;
3492         }
3493         string = PRVM_G_STRING(OFS_PARM1);
3494         order = (int)PRVM_G_FLOAT(OFS_PARM2);
3495         if(order)
3496                 strindex = b->num_strings;
3497         else
3498         {
3499                 strindex = BufStr_FindFreeString(b);
3500                 if(strindex < 0)
3501                 {
3502                         VM_Warning("VM_bufstr_add: buffer %i has no free string slots in %s\n", bufindex, PRVM_NAME);
3503                         return;
3504                 }
3505         }
3506
3507         while(b->num_strings <= strindex)
3508         {
3509                 if(b->num_strings == MAX_QCSTR_STRINGS)
3510                 {
3511                         VM_Warning("VM_bufstr_add: buffer %i has no free string slots in %s\n", bufindex, PRVM_NAME);
3512                         return;
3513                 }
3514                 b->strings[b->num_strings] = NULL;
3515                 b->num_strings++;
3516         }
3517         if(b->strings[strindex])
3518                 Z_Free(b->strings[strindex]);
3519         alloclen = strlen(string) + 1;
3520         b->strings[strindex] = (char *)Z_Malloc(alloclen);
3521         memcpy(b->strings[strindex], string, alloclen);
3522         PRVM_G_FLOAT(OFS_RETURN) = strindex;
3523 }
3524
3525 /*
3526 ========================
3527 VM_bufstr_free
3528 delete string from buffer
3529 void bufstr_free(float bufhandle, float string_index) = #468;
3530 ========================
3531 */
3532 void VM_bufstr_free (void)
3533 {
3534         int                             i;
3535         qcstrbuffer_t   *b;
3536         VM_SAFEPARMCOUNT(2, VM_bufstr_free);
3537
3538         b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3539         if(!b)
3540         {
3541                 VM_Warning("VM_bufstr_free: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3542                 return;
3543         }
3544         i = (int)PRVM_G_FLOAT(OFS_PARM1);
3545         if(i < 0 || i > MAX_QCSTR_STRINGS)
3546         {
3547                 VM_Warning("VM_bufstr_free: invalid string index %i used in %s\n", i, PRVM_NAME);
3548                 return;
3549         }
3550         if(b->strings[i])
3551                 Z_Free(b->strings[i]);
3552         b->strings[i] = NULL;
3553         if(i+1 == b->num_strings)
3554                 --b->num_strings;
3555 }
3556
3557 //=============
3558
3559 /*
3560 ==============
3561 VM_changeyaw
3562
3563 This was a major timewaster in progs, so it was converted to C
3564 ==============
3565 */
3566 void VM_changeyaw (void)
3567 {
3568         prvm_edict_t            *ent;
3569         float           ideal, current, move, speed;
3570
3571         // this is called (VERY HACKISHLY) by SV_MoveToGoal, so it can not use any
3572         // parameters because they are the parameters to SV_MoveToGoal, not this
3573         //VM_SAFEPARMCOUNT(0, VM_changeyaw);
3574
3575         ent = PRVM_PROG_TO_EDICT(PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict);
3576         if (ent == prog->edicts)
3577         {
3578                 VM_Warning("changeyaw: can not modify world entity\n");
3579                 return;
3580         }
3581         if (ent->priv.server->free)
3582         {
3583                 VM_Warning("changeyaw: can not modify free entity\n");
3584                 return;
3585         }
3586         if (prog->fieldoffsets.angles < 0 || prog->fieldoffsets.ideal_yaw < 0 || prog->fieldoffsets.yaw_speed < 0)
3587         {
3588                 VM_Warning("changeyaw: angles, ideal_yaw, or yaw_speed field(s) not found\n");
3589                 return;
3590         }
3591         current = ANGLEMOD(PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.angles)->vector[1]);
3592         ideal = PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.ideal_yaw)->_float;
3593         speed = PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.yaw_speed)->_float;
3594
3595         if (current == ideal)
3596                 return;
3597         move = ideal - current;
3598         if (ideal > current)
3599         {
3600                 if (move >= 180)
3601                         move = move - 360;
3602         }
3603         else
3604         {
3605                 if (move <= -180)
3606                         move = move + 360;
3607         }
3608         if (move > 0)
3609         {
3610                 if (move > speed)
3611                         move = speed;
3612         }
3613         else
3614         {
3615                 if (move < -speed)
3616                         move = -speed;
3617         }
3618
3619         PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.angles)->vector[1] = ANGLEMOD (current + move);
3620 }
3621
3622 /*
3623 ==============
3624 VM_changepitch
3625 ==============
3626 */
3627 void VM_changepitch (void)
3628 {
3629         prvm_edict_t            *ent;
3630         float           ideal, current, move, speed;
3631
3632         VM_SAFEPARMCOUNT(1, VM_changepitch);
3633
3634         ent = PRVM_G_EDICT(OFS_PARM0);
3635         if (ent == prog->edicts)
3636         {
3637                 VM_Warning("changepitch: can not modify world entity\n");
3638                 return;
3639         }
3640         if (ent->priv.server->free)
3641         {
3642                 VM_Warning("changepitch: can not modify free entity\n");
3643                 return;
3644         }
3645         if (prog->fieldoffsets.angles < 0 || prog->fieldoffsets.idealpitch < 0 || prog->fieldoffsets.pitch_speed < 0)
3646         {
3647                 VM_Warning("changepitch: angles, idealpitch, or pitch_speed field(s) not found\n");
3648                 return;
3649         }
3650         current = ANGLEMOD(PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.angles)->vector[0]);
3651         ideal = PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.idealpitch)->_float;
3652         speed = PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.pitch_speed)->_float;
3653
3654         if (current == ideal)
3655                 return;
3656         move = ideal - current;
3657         if (ideal > current)
3658         {
3659                 if (move >= 180)
3660                         move = move - 360;
3661         }
3662         else
3663         {
3664                 if (move <= -180)
3665                         move = move + 360;
3666         }
3667         if (move > 0)
3668         {
3669                 if (move > speed)
3670                         move = speed;
3671         }
3672         else
3673         {
3674                 if (move < -speed)
3675                         move = -speed;
3676         }
3677
3678         PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.angles)->vector[0] = ANGLEMOD (current + move);
3679 }
3680
3681
3682 static int Is_Text_Color (char c, char t)
3683 {
3684         int a = 0;
3685         char c2 = c - (c & 128);
3686         char t2 = t - (t & 128);
3687
3688         if(c != STRING_COLOR_TAG && c2 != STRING_COLOR_TAG)             return 0;
3689         if(t >= '0' && t <= '9')                a = 1;
3690         if(t2 >= '0' && t2 <= '9')              a = 1;
3691 /*      if(t >= 'A' && t <= 'Z')                a = 2;
3692         if(t2 >= 'A' && t2 <= 'Z')              a = 2;
3693
3694         if(a == 1 && scr_colortext.integer > 0)
3695                 return 1;
3696         if(a == 2 && scr_multifonts.integer > 0)
3697                 return 2;
3698 */
3699         return a;
3700 }
3701
3702 void VM_uncolorstring (void)
3703 {
3704         const char      *in;
3705         char            out[VM_STRINGTEMP_LENGTH];
3706         int                     k = 0, i = 0;
3707
3708         VM_SAFEPARMCOUNT(1, VM_uncolorstring);
3709         in = PRVM_G_STRING(OFS_PARM0);
3710         VM_CheckEmptyString (in);
3711
3712         while (in[k])
3713         {
3714                 if(in[k+1])
3715                 if(Is_Text_Color(in[k], in[k+1]) == 1/* || (in[k] == '&' && in[k+1] == 'r')*/)
3716                 {
3717                         k += 2;
3718                         continue;
3719                 }
3720                 out[i] = in[k];
3721                 ++k;
3722                 ++i;
3723         }
3724         PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(out);
3725 }
3726
3727 //#222 string(string s, float index) str2chr (FTE_STRINGS)
3728 void VM_str2chr (void)
3729 {
3730         const char *s;
3731         VM_SAFEPARMCOUNT(2, VM_str2chr);
3732         s = PRVM_G_STRING(OFS_PARM0);
3733         if((unsigned)PRVM_G_FLOAT(OFS_PARM1) > strlen(s))
3734                 return;
3735         PRVM_G_FLOAT(OFS_RETURN) = (unsigned char)s[(int)PRVM_G_FLOAT(OFS_PARM1)];
3736 }
3737
3738 //#223 string(float c, ...) chr2str (FTE_STRINGS)
3739 void VM_chr2str (void)
3740 {
3741         char    t[9];
3742         int             i;
3743         VM_SAFEPARMCOUNTRANGE(0, 8, VM_chr2str);
3744         for(i = 0;i < prog->argc && i < (int)sizeof(t) - 1;i++)
3745                 t[i] = (unsigned char)PRVM_G_FLOAT(OFS_PARM0+i*3);
3746         t[i] = 0;
3747         PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(t);
3748 }
3749
3750 //#228 float(string s1, string s2, float len) strncmp (FTE_STRINGS)
3751 void VM_strncmp (void)
3752 {
3753         const char *s1, *s2;
3754         VM_SAFEPARMCOUNT(1, VM_strncmp);
3755         s1 = PRVM_G_STRING(OFS_PARM0);
3756         s2 = PRVM_G_STRING(OFS_PARM1);
3757         PRVM_G_FLOAT(OFS_RETURN) = strncmp(s1, s2, (size_t)PRVM_G_FLOAT(OFS_PARM2));
3758 }
3759
3760 void VM_wasfreed (void)
3761 {
3762         VM_SAFEPARMCOUNT(1, VM_wasfreed);
3763         PRVM_G_FLOAT(OFS_RETURN) = PRVM_G_EDICT(OFS_PARM0)->priv.required->free;
3764 }
3765
3766 void VM_SetTraceGlobals(const trace_t *trace)
3767 {
3768         prvm_eval_t *val;
3769         if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_allsolid)))
3770                 val->_float = trace->allsolid;
3771         if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_startsolid)))
3772                 val->_float = trace->startsolid;
3773         if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_fraction)))
3774                 val->_float = trace->fraction;
3775         if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_inwater)))
3776                 val->_float = trace->inwater;
3777         if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_inopen)))
3778                 val->_float = trace->inopen;
3779         if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_endpos)))
3780                 VectorCopy(trace->endpos, val->vector);
3781         if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_plane_normal)))
3782                 VectorCopy(trace->plane.normal, val->vector);
3783         if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_plane_dist)))
3784                 val->_float = trace->plane.dist;
3785         if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_ent)))
3786                 val->edict = PRVM_EDICT_TO_PROG(trace->ent ? trace->ent : prog->edicts);
3787         if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dpstartcontents)))
3788                 val->_float = trace->startsupercontents;
3789         if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dphitcontents)))
3790                 val->_float = trace->hitsupercontents;
3791         if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dphitq3surfaceflags)))
3792                 val->_float = trace->hitq3surfaceflags;
3793         if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dphittexturename)))
3794                 val->string = trace->hittexture ? PRVM_SetTempString(trace->hittexture->name) : 0;
3795 }
3796
3797 //=============
3798
3799 void VM_Cmd_Init(void)
3800 {
3801         // only init the stuff for the current prog
3802         VM_Files_Init();
3803         VM_Search_Init();
3804 //      VM_BufStr_Init();
3805 }
3806
3807 void VM_Cmd_Reset(void)
3808 {
3809         CL_PurgeOwner( MENUOWNER );
3810         VM_Search_Reset();
3811         VM_Files_CloseAll();
3812 //      VM_BufStr_ShutDown();
3813 }
3814