]> icculus.org git repositories - divverent/darkplaces.git/blob - prvm_cmds.c
zym models now support TraceBox calls
[divverent/darkplaces.git] / prvm_cmds.c
1 // AK
2 // Basically every vm builtin cmd should be in here.
3 // All 3 builtin and extension lists can be found here
4 // cause large (I think they will) parts are from pr_cmds the same copyright like in pr_cmds
5 // also applies here
6
7 #include "prvm_cmds.h"
8
9 //============================================================================
10 // Common
11
12 // temp string handling
13 // LordHavoc: added this to semi-fix the problem of using many ftos calls in a print
14 #define VM_STRINGTEMP_BUFFERS 16
15 #define VM_STRINGTEMP_LENGTH 4096
16 static char vm_string_temp[VM_STRINGTEMP_BUFFERS][VM_STRINGTEMP_LENGTH];
17 static int vm_string_tempindex = 0;
18
19 // qc file handling
20 #define MAX_VMFILES             256
21 #define MAX_PRVMFILES   MAX_VMFILES * PRVM_MAXPROGS
22 #define VM_FILES ((qfile_t**)(vm_files + PRVM_GetProgNr() * MAX_VMFILES))
23
24 qfile_t *vm_files[MAX_PRVMFILES];
25
26 // qc fs search handling
27 #define MAX_VMSEARCHES 128
28 #define TOTAL_VMSEARCHES MAX_VMSEARCHES * PRVM_MAXPROGS
29 #define VM_SEARCHLIST ((fssearch_t**)(vm_fssearchlist + PRVM_GetProgNr() * MAX_VMSEARCHES))
30
31 fssearch_t *vm_fssearchlist[TOTAL_VMSEARCHES];
32
33 char *VM_GetTempString(void)
34 {
35         char *s;
36         s = vm_string_temp[vm_string_tempindex];
37         vm_string_tempindex = (vm_string_tempindex + 1) % VM_STRINGTEMP_BUFFERS;
38         return s;
39 }
40
41 void VM_CheckEmptyString (const char *s)
42 {
43         if (s[0] <= ' ')
44                 PRVM_ERROR ("%s: Bad string", PRVM_NAME);
45 }
46
47 //============================================================================
48 //BUILT-IN FUNCTIONS
49
50 void VM_VarString(int first, char *out, int outlength)
51 {
52         int i;
53         const char *s;
54         char *outend;
55
56         outend = out + outlength - 1;
57         for (i = first;i < prog->argc && out < outend;i++)
58         {
59                 s = PRVM_G_STRING((OFS_PARM0+i*3));
60                 while (out < outend && *s)
61                         *out++ = *s++;
62         }
63         *out++ = 0;
64 }
65
66 /*
67 =================
68 VM_checkextension
69
70 returns true if the extension is supported by the server
71
72 checkextension(extensionname)
73 =================
74 */
75
76 // kind of helper function
77 static qboolean checkextension(const char *name)
78 {
79         int len;
80         char *e, *start;
81         len = strlen(name);
82
83         for (e = prog->extensionstring;*e;e++)
84         {
85                 while (*e == ' ')
86                         e++;
87                 if (!*e)
88                         break;
89                 start = e;
90                 while (*e && *e != ' ')
91                         e++;
92                 if (e - start == len)
93                         if (!strncasecmp(start, name, len))
94                         {
95                                 return true;
96                         }
97         }
98         return false;
99 }
100
101 void VM_checkextension (void)
102 {
103         VM_SAFEPARMCOUNT(1,VM_checkextension);
104
105         PRVM_G_FLOAT(OFS_RETURN) = checkextension(PRVM_G_STRING(OFS_PARM0));
106 }
107
108 /*
109 =================
110 VM_error
111
112 This is a TERMINAL error, which will kill off the entire prog.
113 Dumps self.
114
115 error(value)
116 =================
117 */
118 void VM_error (void)
119 {
120         prvm_edict_t    *ed;
121         char string[VM_STRINGTEMP_LENGTH];
122
123         VM_VarString(0, string, sizeof(string));
124         Con_Printf("======%S ERROR in %s:\n%s\n", PRVM_NAME, PRVM_GetString(prog->xfunction->s_name), string);
125         if(prog->self)
126         {
127                 ed = PRVM_G_EDICT(prog->self->ofs);
128                 PRVM_ED_Print(ed);
129         }
130
131         PRVM_ERROR ("%s: Program error", PRVM_NAME);
132 }
133
134 /*
135 =================
136 VM_objerror
137
138 Dumps out self, then an error message.  The program is aborted and self is
139 removed, but the level can continue.
140
141 objerror(value)
142 =================
143 */
144 void VM_objerror (void)
145 {
146         prvm_edict_t    *ed;
147         char string[VM_STRINGTEMP_LENGTH];
148
149         VM_VarString(0, string, sizeof(string));
150         Con_Printf("======%s OBJECT ERROR in %s:\n%s\n", PRVM_NAME, PRVM_GetString(prog->xfunction->s_name), string);
151         if(prog->self)
152         {
153                 ed = PRVM_G_EDICT (prog->self->ofs);
154                 PRVM_ED_Print(ed);
155
156                 PRVM_ED_Free (ed);
157         }
158         else
159                 // objerror has to display the object fields -> else call
160                 PRVM_ERROR ("VM_objecterror: self not defined !\n");
161 }
162
163 /*
164 =================
165 VM_print (actually used only by client and menu)
166
167 print to console
168
169 print(string)
170 =================
171 */
172 void VM_print (void)
173 {
174         char string[VM_STRINGTEMP_LENGTH];
175
176         VM_VarString(0, string, sizeof(string));
177         Con_Print(string);
178 }
179
180 /*
181 =================
182 VM_bprint
183
184 broadcast print to everyone on server
185
186 bprint(...[string])
187 =================
188 */
189 void VM_bprint (void)
190 {
191         char string[VM_STRINGTEMP_LENGTH];
192
193         if(!sv.active)
194         {
195                 Con_Printf("VM_bprint: game is not server(%s) !\n", PRVM_NAME);
196                 return;
197         }
198
199         VM_VarString(0, string, sizeof(string));
200         SV_BroadcastPrint(string);
201 }
202
203 /*
204 =================
205 VM_sprint (menu & client but only if server.active == true)
206
207 single print to a specific client
208
209 sprint(float clientnum,...[string])
210 =================
211 */
212 void VM_sprint (void)
213 {
214         client_t        *client;
215         int                     clientnum;
216         char string[VM_STRINGTEMP_LENGTH];
217
218         //find client for this entity
219         clientnum = PRVM_G_FLOAT(OFS_PARM0);
220         if (!sv.active  || clientnum < 0 || clientnum >= svs.maxclients || !svs.clients[clientnum].active)
221         {
222                 Con_Printf("VM_sprint: %s: invalid client or server is not active !\n", PRVM_NAME);
223                 return;
224         }
225
226         client = svs.clients + clientnum;
227         VM_VarString(1, string, sizeof(string));
228         MSG_WriteChar(&client->message,svc_print);
229         MSG_WriteString(&client->message, string);
230 }
231
232 /*
233 =================
234 VM_centerprint
235
236 single print to the screen
237
238 centerprint(clientent, value)
239 =================
240 */
241 void VM_centerprint (void)
242 {
243         char string[VM_STRINGTEMP_LENGTH];
244
245         VM_VarString(0, string, sizeof(string));
246         SCR_CenterPrint(string);
247 }
248
249 /*
250 =================
251 VM_normalize
252
253 vector normalize(vector)
254 =================
255 */
256 void VM_normalize (void)
257 {
258         float   *value1;
259         vec3_t  newvalue;
260         float   new;
261
262         VM_SAFEPARMCOUNT(1,VM_normalize);
263
264         value1 = PRVM_G_VECTOR(OFS_PARM0);
265
266         new = value1[0] * value1[0] + value1[1] * value1[1] + value1[2]*value1[2];
267         new = sqrt(new);
268
269         if (new == 0)
270                 newvalue[0] = newvalue[1] = newvalue[2] = 0;
271         else
272         {
273                 new = 1/new;
274                 newvalue[0] = value1[0] * new;
275                 newvalue[1] = value1[1] * new;
276                 newvalue[2] = value1[2] * new;
277         }
278
279         VectorCopy (newvalue, PRVM_G_VECTOR(OFS_RETURN));
280 }
281
282 /*
283 =================
284 VM_vlen
285
286 scalar vlen(vector)
287 =================
288 */
289 void VM_vlen (void)
290 {
291         float   *value1;
292         float   new;
293
294         VM_SAFEPARMCOUNT(1,VM_vlen);
295
296         value1 = PRVM_G_VECTOR(OFS_PARM0);
297
298         new = value1[0] * value1[0] + value1[1] * value1[1] + value1[2]*value1[2];
299         new = sqrt(new);
300
301         PRVM_G_FLOAT(OFS_RETURN) = new;
302 }
303
304 /*
305 =================
306 VM_vectoyaw
307
308 float vectoyaw(vector)
309 =================
310 */
311 void VM_vectoyaw (void)
312 {
313         float   *value1;
314         float   yaw;
315
316         VM_SAFEPARMCOUNT(1,VM_vectoyaw);
317
318         value1 = PRVM_G_VECTOR(OFS_PARM0);
319
320         if (value1[1] == 0 && value1[0] == 0)
321                 yaw = 0;
322         else
323         {
324                 yaw = (int) (atan2(value1[1], value1[0]) * 180 / M_PI);
325                 if (yaw < 0)
326                         yaw += 360;
327         }
328
329         PRVM_G_FLOAT(OFS_RETURN) = yaw;
330 }
331
332
333 /*
334 =================
335 VM_vectoangles
336
337 vector vectoangles(vector)
338 =================
339 */
340 void VM_vectoangles (void)
341 {
342         float   *value1;
343         float   forward;
344         float   yaw, pitch;
345
346         VM_SAFEPARMCOUNT(1,VM_vectoangles);
347
348         value1 = PRVM_G_VECTOR(OFS_PARM0);
349
350         if (value1[1] == 0 && value1[0] == 0)
351         {
352                 yaw = 0;
353                 if (value1[2] > 0)
354                         pitch = 90;
355                 else
356                         pitch = 270;
357         }
358         else
359         {
360                 // LordHavoc: optimized a bit
361                 if (value1[0])
362                 {
363                         yaw = (atan2(value1[1], value1[0]) * 180 / M_PI);
364                         if (yaw < 0)
365                                 yaw += 360;
366                 }
367                 else if (value1[1] > 0)
368                         yaw = 90;
369                 else
370                         yaw = 270;
371
372                 forward = sqrt(value1[0]*value1[0] + value1[1]*value1[1]);
373                 pitch = (int) (atan2(value1[2], forward) * 180 / M_PI);
374                 if (pitch < 0)
375                         pitch += 360;
376         }
377
378         PRVM_G_FLOAT(OFS_RETURN+0) = pitch;
379         PRVM_G_FLOAT(OFS_RETURN+1) = yaw;
380         PRVM_G_FLOAT(OFS_RETURN+2) = 0;
381 }
382
383 /*
384 =================
385 VM_random
386
387 Returns a number from 0<= num < 1
388
389 float random()
390 =================
391 */
392 void VM_random (void)
393 {
394         VM_SAFEPARMCOUNT(0,VM_random);
395
396         PRVM_G_FLOAT(OFS_RETURN) = lhrandom(0, 1);
397 }
398
399 /*
400 =================
401 PF_sound
402
403 Each entity can have eight independant sound sources, like voice,
404 weapon, feet, etc.
405
406 Channel 0 is an auto-allocate channel, the others override anything
407 already running on that entity/channel pair.
408
409 An attenuation of 0 will play full volume everywhere in the level.
410 Larger attenuations will drop off.
411
412 =================
413 */
414 /*
415 void PF_sound (void)
416 {
417         char            *sample;
418         int                     channel;
419         edict_t         *entity;
420         int             volume;
421         float attenuation;
422
423         entity = G_EDICT(OFS_PARM0);
424         channel = G_FLOAT(OFS_PARM1);
425         sample = G_STRING(OFS_PARM2);
426         volume = G_FLOAT(OFS_PARM3) * 255;
427         attenuation = G_FLOAT(OFS_PARM4);
428
429         if (volume < 0 || volume > 255)
430                 Host_Error ("SV_StartSound: volume = %i", volume);
431
432         if (attenuation < 0 || attenuation > 4)
433                 Host_Error ("SV_StartSound: attenuation = %f", attenuation);
434
435         if (channel < 0 || channel > 7)
436                 Host_Error ("SV_StartSound: channel = %i", channel);
437
438         SV_StartSound (entity, channel, sample, volume, attenuation);
439 }
440 */
441
442 /*
443 =========
444 VM_localsound
445
446 localsound(string sample)
447 =========
448 */
449 void VM_localsound(void)
450 {
451         const char *s;
452
453         VM_SAFEPARMCOUNT(1,VM_localsound);
454
455         s = PRVM_G_STRING(OFS_PARM0);
456
457         if(!S_LocalSound (s))
458         {
459                 Con_Printf("VM_localsound: Failed to play %s for %s !\n", s, PRVM_NAME);
460                 PRVM_G_FLOAT(OFS_RETURN) = -4;
461                 return;
462         }
463
464         PRVM_G_FLOAT(OFS_RETURN) = 1;
465 }
466
467 /*
468 =================
469 VM_break
470
471 break()
472 =================
473 */
474 void VM_break (void)
475 {
476         PRVM_ERROR ("%s: break statement", PRVM_NAME);
477 }
478
479 //============================================================================
480
481 /*
482 =================
483 VM_localcmd
484
485 Sends text over to the client's execution buffer
486
487 [localcmd (string) or]
488 cmd (string)
489 =================
490 */
491 void VM_localcmd (void)
492 {
493         VM_SAFEPARMCOUNT(1,VM_localcmd);
494
495         Cbuf_AddText(PRVM_G_STRING(OFS_PARM0));
496 }
497
498 /*
499 =================
500 VM_cvar
501
502 float cvar (string)
503 =================
504 */
505 void VM_cvar (void)
506 {
507         VM_SAFEPARMCOUNT(1,VM_cvar);
508
509         PRVM_G_FLOAT(OFS_RETURN) = Cvar_VariableValue(PRVM_G_STRING(OFS_PARM0));
510 }
511
512 /*
513 =================
514 VM_cvar_string
515
516 const string    VM_cvar_string (string)
517 =================
518 */
519 void VM_cvar_string(void)
520 {
521         char *out;
522         const char *name;
523         const char *cvar_string;
524         VM_SAFEPARMCOUNT(1,VM_cvar_string);
525
526         name = PRVM_G_STRING(OFS_PARM0);
527
528         if(!name)
529                 PRVM_ERROR("VM_str_cvar: %s: null string\n", PRVM_NAME);
530
531         VM_CheckEmptyString(name);
532
533         out = VM_GetTempString();
534
535         cvar_string = Cvar_VariableString(name);
536
537         strcpy(out, cvar_string);
538
539         PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(out);
540 }
541
542 /*
543 =================
544 VM_cvar_set
545
546 void cvar_set (string,string)
547 =================
548 */
549 void VM_cvar_set (void)
550 {
551         VM_SAFEPARMCOUNT(2,VM_cvar_set);
552
553         Cvar_Set(PRVM_G_STRING(OFS_PARM0), PRVM_G_STRING(OFS_PARM1));
554 }
555
556 /*
557 =========
558 VM_dprint
559
560 dprint(...[string])
561 =========
562 */
563 void VM_dprint (void)
564 {
565         char string[VM_STRINGTEMP_LENGTH];
566         if (developer.integer)
567         {
568                 VM_VarString(0, string, sizeof(string));
569                 Con_Printf("%s: %s", PRVM_NAME, string);
570         }
571 }
572
573 /*
574 =========
575 VM_ftos
576
577 string  ftos(float)
578 =========
579 */
580
581 void VM_ftos (void)
582 {
583         float v;
584         char *s;
585
586         VM_SAFEPARMCOUNT(1, VM_ftos);
587
588         v = PRVM_G_FLOAT(OFS_PARM0);
589
590         s = VM_GetTempString();
591         if ((float)((int)v) == v)
592                 sprintf(s, "%i", (int)v);
593         else
594                 sprintf(s, "%f", v);
595         PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(s);
596 }
597
598 /*
599 =========
600 VM_fabs
601
602 float   fabs(float)
603 =========
604 */
605
606 void VM_fabs (void)
607 {
608         float   v;
609
610         VM_SAFEPARMCOUNT(1,VM_fabs);
611
612         v = PRVM_G_FLOAT(OFS_PARM0);
613         PRVM_G_FLOAT(OFS_RETURN) = fabs(v);
614 }
615
616 /*
617 =========
618 VM_vtos
619
620 string  vtos(vector)
621 =========
622 */
623
624 void VM_vtos (void)
625 {
626         char *s;
627
628         VM_SAFEPARMCOUNT(1,VM_vtos);
629
630         s = VM_GetTempString();
631         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]);
632         PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(s);
633 }
634
635 /*
636 =========
637 VM_etos
638
639 string  etos(entity)
640 =========
641 */
642
643 void VM_etos (void)
644 {
645         char *s;
646
647         VM_SAFEPARMCOUNT(1, VM_etos);
648
649         s = VM_GetTempString();
650         sprintf (s, "entity %i", PRVM_G_EDICTNUM(OFS_PARM0));
651         PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(s);
652 }
653
654 /*
655 =========
656 VM_stof
657
658 float stof(...[string])
659 =========
660 */
661 void VM_stof(void)
662 {
663         char string[VM_STRINGTEMP_LENGTH];
664         VM_VarString(0, string, sizeof(string));
665         PRVM_G_FLOAT(OFS_RETURN) = atof(string);
666 }
667
668 /*
669 ========================
670 VM_itof
671
672 float itof(intt ent)
673 ========================
674 */
675 void VM_itof(void)
676 {
677         VM_SAFEPARMCOUNT(1, VM_itof);
678         PRVM_G_FLOAT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
679 }
680
681 /*
682 ========================
683 VM_itoe
684
685 intt ftoi(float num)
686 ========================
687 */
688 void VM_ftoi(void)
689 {
690         int ent;
691         VM_SAFEPARMCOUNT(1, VM_ftoi);
692
693         ent = PRVM_G_FLOAT(OFS_PARM0);
694         if(PRVM_PROG_TO_EDICT(ent)->priv.required->free)
695                 PRVM_ERROR ("VM_ftoe: %s tried to access a freed entity (entity %i)!\n", PRVM_NAME, ent);
696
697         PRVM_G_INT(OFS_RETURN) = ent;
698 }
699
700 /*
701 =========
702 VM_spawn
703
704 entity spawn()
705 =========
706 */
707
708 void VM_spawn (void)
709 {
710         prvm_edict_t    *ed;
711         prog->xfunction->builtinsprofile += 20;
712         ed = PRVM_ED_Alloc();
713         VM_RETURN_EDICT(ed);
714 }
715
716 /*
717 =========
718 VM_remove
719
720 remove(entity e)
721 =========
722 */
723
724 void VM_remove (void)
725 {
726         prvm_edict_t    *ed;
727         prog->xfunction->builtinsprofile += 20;
728
729         VM_SAFEPARMCOUNT(1, VM_remove);
730
731         ed = PRVM_G_EDICT(OFS_PARM0);
732 //      if (ed == prog->edicts)
733 //              PRVM_ERROR ("remove: tried to remove world\n");
734 //      if (PRVM_NUM_FOR_EDICT(ed) <= sv.maxclients)
735 //              Host_Error("remove: tried to remove a client\n");
736         PRVM_ED_Free (ed);
737 }
738
739 /*
740 =========
741 VM_find
742
743 entity  find(entity start, .string field, string match)
744 =========
745 */
746
747 void VM_find (void)
748 {
749         int             e;
750         int             f;
751         const char      *s, *t;
752         prvm_edict_t    *ed;
753
754         VM_SAFEPARMCOUNT(3,VM_find);
755
756         e = PRVM_G_EDICTNUM(OFS_PARM0);
757         f = PRVM_G_INT(OFS_PARM1);
758         s = PRVM_G_STRING(OFS_PARM2);
759
760         if (!s || !s[0])
761         {
762                 // return reserved edict 0 (could be used for whatever the prog wants)
763                 VM_RETURN_EDICT(prog->edicts);
764                 return;
765         }
766
767         for (e++ ; e < prog->num_edicts ; e++)
768         {
769                 prog->xfunction->builtinsprofile++;
770                 ed = PRVM_EDICT_NUM(e);
771                 if (ed->priv.required->free)
772                         continue;
773                 t = PRVM_E_STRING(ed,f);
774                 if (!t)
775                         continue;
776                 if (!strcmp(t,s))
777                 {
778                         VM_RETURN_EDICT(ed);
779                         return;
780                 }
781         }
782
783         VM_RETURN_EDICT(prog->edicts);
784 }
785
786 /*
787 =========
788 VM_findfloat
789
790   entity        findfloat(entity start, .float field, float match)
791   entity        findentity(entity start, .entity field, entity match)
792 =========
793 */
794 // LordHavoc: added this for searching float, int, and entity reference fields
795 void VM_findfloat (void)
796 {
797         int             e;
798         int             f;
799         float   s;
800         prvm_edict_t    *ed;
801
802         VM_SAFEPARMCOUNT(3,VM_findfloat);
803
804         e = PRVM_G_EDICTNUM(OFS_PARM0);
805         f = PRVM_G_INT(OFS_PARM1);
806         s = PRVM_G_FLOAT(OFS_PARM2);
807
808         for (e++ ; e < prog->num_edicts ; e++)
809         {
810                 prog->xfunction->builtinsprofile++;
811                 ed = PRVM_EDICT_NUM(e);
812                 if (ed->priv.required->free)
813                         continue;
814                 if (PRVM_E_FLOAT(ed,f) == s)
815                 {
816                         VM_RETURN_EDICT(ed);
817                         return;
818                 }
819         }
820
821         VM_RETURN_EDICT(prog->edicts);
822 }
823
824 /*
825 =========
826 VM_findchain
827
828 entity  findchain(.string field, string match)
829 =========
830 */
831 int PRVM_ED_FindFieldOffset(const char *field);
832 // chained search for strings in entity fields
833 // entity(.string field, string match) findchain = #402;
834 void VM_findchain (void)
835 {
836         int             i;
837         int             f;
838         int             chain_of;
839         const char      *s, *t;
840         prvm_edict_t    *ent, *chain;
841
842         VM_SAFEPARMCOUNT(2,VM_findchain);
843
844         // is the same like !(prog->flag & PRVM_FE_CHAIN) - even if the operator precedence is another
845         if(!prog->flag & PRVM_FE_CHAIN)
846                 PRVM_ERROR("VM_findchain: %s doesnt have a chain field !\n", PRVM_NAME);
847
848         chain_of = PRVM_ED_FindFieldOffset ("chain");
849
850         chain = prog->edicts;
851
852         f = PRVM_G_INT(OFS_PARM0);
853         s = PRVM_G_STRING(OFS_PARM1);
854         if (!s || !s[0])
855         {
856                 VM_RETURN_EDICT(prog->edicts);
857                 return;
858         }
859
860         ent = PRVM_NEXT_EDICT(prog->edicts);
861         for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
862         {
863                 prog->xfunction->builtinsprofile++;
864                 if (ent->priv.required->free)
865                         continue;
866                 t = PRVM_E_STRING(ent,f);
867                 if (!t)
868                         continue;
869                 if (strcmp(t,s))
870                         continue;
871
872                 PRVM_E_INT(ent,chain_of) = PRVM_NUM_FOR_EDICT(chain);
873                 chain = ent;
874         }
875
876         VM_RETURN_EDICT(chain);
877 }
878
879 /*
880 =========
881 VM_findchainfloat
882
883 entity  findchainfloat(.string field, float match)
884 entity  findchainentity(.string field, entity match)
885 =========
886 */
887 // LordHavoc: chained search for float, int, and entity reference fields
888 // entity(.string field, float match) findchainfloat = #403;
889 void VM_findchainfloat (void)
890 {
891         int             i;
892         int             f;
893         int             chain_of;
894         float   s;
895         prvm_edict_t    *ent, *chain;
896
897         VM_SAFEPARMCOUNT(2, VM_findchainfloat);
898
899         if(!prog->flag & PRVM_FE_CHAIN)
900                 PRVM_ERROR("VM_findchainfloat: %s doesnt have a chain field !\n", PRVM_NAME);
901
902         chain_of = PRVM_ED_FindFieldOffset ("chain");
903
904         chain = (prvm_edict_t *)prog->edicts;
905
906         f = PRVM_G_INT(OFS_PARM0);
907         s = PRVM_G_FLOAT(OFS_PARM1);
908
909         ent = PRVM_NEXT_EDICT(prog->edicts);
910         for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
911         {
912                 prog->xfunction->builtinsprofile++;
913                 if (ent->priv.required->free)
914                         continue;
915                 if (PRVM_E_FLOAT(ent,f) != s)
916                         continue;
917
918                 PRVM_E_INT(ent,chain_of) = PRVM_EDICT_TO_PROG(chain);
919                 chain = ent;
920         }
921
922         VM_RETURN_EDICT(chain);
923 }
924
925 /*
926 =========
927 VM_precache_file
928
929 string  precache_file(string)
930 =========
931 */
932 void VM_precache_file (void)
933 {       // precache_file is only used to copy files with qcc, it does nothing
934         VM_SAFEPARMCOUNT(1,VM_precache_file);
935
936         PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
937 }
938
939 /*
940 =========
941 VM_preache_error
942
943 used instead of the other VM_precache_* functions in the builtin list
944 =========
945 */
946
947 void VM_precache_error (void)
948 {
949         PRVM_ERROR ("PF_Precache_*: Precache can only be done in spawn functions");
950 }
951
952 /*
953 =========
954 VM_precache_sound
955
956 string  precache_sound (string sample)
957 =========
958 */
959 void VM_precache_sound (void)
960 {
961         const char      *s;
962
963         VM_SAFEPARMCOUNT(1, VM_precache_sound);
964
965         s = PRVM_G_STRING(OFS_PARM0);
966         PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
967         VM_CheckEmptyString (s);
968
969         if(snd_initialized.integer && !S_PrecacheSound (s,true, true))
970                 Con_Printf("VM_precache_sound: Failed to load %s for %s\n", s, PRVM_NAME);
971 }
972
973 /*
974 =========
975 VM_coredump
976
977 coredump()
978 =========
979 */
980 void VM_coredump (void)
981 {
982         VM_SAFEPARMCOUNT(0,VM_coredump);
983
984         Cbuf_AddText("prvm_edicts ");
985         Cbuf_AddText(PRVM_NAME);
986         Cbuf_AddText("\n");
987 }
988
989 /*
990 =========
991 VM_stackdump
992
993 stackdump()
994 =========
995 */
996 void PRVM_StackTrace(void);
997 void VM_stackdump (void)
998 {
999         VM_SAFEPARMCOUNT(0, VM_stackdump);
1000
1001         PRVM_StackTrace();
1002 }
1003
1004 /*
1005 =========
1006 VM_crash
1007
1008 crash()
1009 =========
1010 */
1011
1012 void VM_crash(void)
1013 {
1014         VM_SAFEPARMCOUNT(0, VM_crash);
1015
1016         PRVM_ERROR("Crash called by %s\n",PRVM_NAME);
1017 }
1018
1019 /*
1020 =========
1021 VM_traceon
1022
1023 traceon()
1024 =========
1025 */
1026 void VM_traceon (void)
1027 {
1028         VM_SAFEPARMCOUNT(0,VM_traceon);
1029
1030         prog->trace = true;
1031 }
1032
1033 /*
1034 =========
1035 VM_traceoff
1036
1037 traceoff()
1038 =========
1039 */
1040 void VM_traceoff (void)
1041 {
1042         VM_SAFEPARMCOUNT(0,VM_traceoff);
1043
1044         prog->trace = false;
1045 }
1046
1047 /*
1048 =========
1049 VM_eprint
1050
1051 eprint(entity e)
1052 =========
1053 */
1054 void VM_eprint (void)
1055 {
1056         VM_SAFEPARMCOUNT(1,VM_eprint);
1057
1058         PRVM_ED_PrintNum (PRVM_G_EDICTNUM(OFS_PARM0));
1059 }
1060
1061 /*
1062 =========
1063 VM_rint
1064
1065 float   rint(float)
1066 =========
1067 */
1068 void VM_rint (void)
1069 {
1070         float   f;
1071
1072         VM_SAFEPARMCOUNT(1,VM_rint);
1073
1074         f = PRVM_G_FLOAT(OFS_PARM0);
1075         if (f > 0)
1076                 PRVM_G_FLOAT(OFS_RETURN) = (int)(f + 0.5);
1077         else
1078                 PRVM_G_FLOAT(OFS_RETURN) = (int)(f - 0.5);
1079 }
1080
1081 /*
1082 =========
1083 VM_floor
1084
1085 float   floor(float)
1086 =========
1087 */
1088 void VM_floor (void)
1089 {
1090         VM_SAFEPARMCOUNT(1,VM_floor);
1091
1092         PRVM_G_FLOAT(OFS_RETURN) = floor(PRVM_G_FLOAT(OFS_PARM0));
1093 }
1094
1095 /*
1096 =========
1097 VM_ceil
1098
1099 float   ceil(float)
1100 =========
1101 */
1102 void VM_ceil (void)
1103 {
1104         VM_SAFEPARMCOUNT(1,VM_ceil);
1105
1106         PRVM_G_FLOAT(OFS_RETURN) = ceil(PRVM_G_FLOAT(OFS_PARM0));
1107 }
1108
1109
1110 /*
1111 =============
1112 VM_nextent
1113
1114 entity  nextent(entity)
1115 =============
1116 */
1117 void VM_nextent (void)
1118 {
1119         int             i;
1120         prvm_edict_t    *ent;
1121
1122         i = PRVM_G_EDICTNUM(OFS_PARM0);
1123         while (1)
1124         {
1125                 prog->xfunction->builtinsprofile++;
1126                 i++;
1127                 if (i == prog->num_edicts)
1128                 {
1129                         VM_RETURN_EDICT(prog->edicts);
1130                         return;
1131                 }
1132                 ent = PRVM_EDICT_NUM(i);
1133                 if (!ent->priv.required->free)
1134                 {
1135                         VM_RETURN_EDICT(ent);
1136                         return;
1137                 }
1138         }
1139 }
1140
1141 /*
1142 ===============================================================================
1143 MESSAGE WRITING
1144
1145 used only for client and menu
1146 severs uses VM_SV_...
1147
1148 Write*(* data, float type, float to)
1149
1150 ===============================================================================
1151 */
1152
1153 #define MSG_BROADCAST   0               // unreliable to all
1154 #define MSG_ONE                 1               // reliable to one (msg_entity)
1155 #define MSG_ALL                 2               // reliable to all
1156 #define MSG_INIT                3               // write to the init string
1157
1158 sizebuf_t *VM_WriteDest (void)
1159 {
1160         int             dest;
1161         int             destclient;
1162
1163         if(!sv.active)
1164                 PRVM_ERROR("VM_WriteDest: game is not server (%s)\n", PRVM_NAME);
1165
1166         dest = G_FLOAT(OFS_PARM1);
1167         switch (dest)
1168         {
1169         case MSG_BROADCAST:
1170                 return &sv.datagram;
1171
1172         case MSG_ONE:
1173                 destclient = (int) PRVM_G_FLOAT(OFS_PARM2);
1174                 if (destclient < 0 || destclient >= svs.maxclients || !svs.clients[destclient].active)
1175                         PRVM_ERROR("VM_clientcommand: %s: invalid client !\n", PRVM_NAME);
1176
1177                 return &svs.clients[destclient].message;
1178
1179         case MSG_ALL:
1180                 return &sv.reliable_datagram;
1181
1182         case MSG_INIT:
1183                 return &sv.signon;
1184
1185         default:
1186                 PRVM_ERROR ("WriteDest: bad destination");
1187                 break;
1188         }
1189
1190         return NULL;
1191 }
1192
1193 void VM_WriteByte (void)
1194 {
1195         MSG_WriteByte (VM_WriteDest(), PRVM_G_FLOAT(OFS_PARM0));
1196 }
1197
1198 void VM_WriteChar (void)
1199 {
1200         MSG_WriteChar (VM_WriteDest(), PRVM_G_FLOAT(OFS_PARM0));
1201 }
1202
1203 void VM_WriteShort (void)
1204 {
1205         MSG_WriteShort (VM_WriteDest(), PRVM_G_FLOAT(OFS_PARM0));
1206 }
1207
1208 void VM_WriteLong (void)
1209 {
1210         MSG_WriteLong (VM_WriteDest(), PRVM_G_FLOAT(OFS_PARM0));
1211 }
1212
1213 void VM_WriteAngle (void)
1214 {
1215         MSG_WriteAngle (VM_WriteDest(), PRVM_G_FLOAT(OFS_PARM0), sv.protocol);
1216 }
1217
1218 void VM_WriteCoord (void)
1219 {
1220         MSG_WriteCoord (VM_WriteDest(), PRVM_G_FLOAT(OFS_PARM0), sv.protocol);
1221 }
1222
1223 void VM_WriteString (void)
1224 {
1225         MSG_WriteString (VM_WriteDest(), PRVM_G_STRING(OFS_PARM0));
1226 }
1227
1228 void VM_WriteEntity (void)
1229 {
1230         MSG_WriteShort (VM_WriteDest(), PRVM_G_EDICTNUM(OFS_PARM0));
1231 }
1232
1233 //=============================================================================
1234
1235 /*
1236 ==============
1237 VM_changelevel
1238 server and menu
1239
1240 changelevel(string map)
1241 ==============
1242 */
1243 void VM_changelevel (void)
1244 {
1245         const char      *s;
1246
1247         VM_SAFEPARMCOUNT(1, VM_changelevel);
1248
1249         if(!sv.active)
1250         {
1251                 Con_Printf("VM_changelevel: game is not server (%s)\n", PRVM_NAME);
1252                 return;
1253         }
1254
1255 // make sure we don't issue two changelevels
1256         if (svs.changelevel_issued)
1257                 return;
1258         svs.changelevel_issued = true;
1259
1260         s = PRVM_G_STRING(OFS_PARM0);
1261         Cbuf_AddText (va("changelevel %s\n",s));
1262 }
1263
1264 /*
1265 =========
1266 VM_sin
1267
1268 float   sin(float)
1269 =========
1270 */
1271 void VM_sin (void)
1272 {
1273         VM_SAFEPARMCOUNT(1,VM_sin);
1274         PRVM_G_FLOAT(OFS_RETURN) = sin(PRVM_G_FLOAT(OFS_PARM0));
1275 }
1276
1277 /*
1278 =========
1279 VM_cos
1280 float   cos(float)
1281 =========
1282 */
1283 void VM_cos (void)
1284 {
1285         VM_SAFEPARMCOUNT(1,VM_cos);
1286         PRVM_G_FLOAT(OFS_RETURN) = cos(PRVM_G_FLOAT(OFS_PARM0));
1287 }
1288
1289 /*
1290 =========
1291 VM_sqrt
1292
1293 float   sqrt(float)
1294 =========
1295 */
1296 void VM_sqrt (void)
1297 {
1298         VM_SAFEPARMCOUNT(1,VM_sqrt);
1299         PRVM_G_FLOAT(OFS_RETURN) = sqrt(PRVM_G_FLOAT(OFS_PARM0));
1300 }
1301
1302 /*
1303 =================
1304 VM_randomvec
1305
1306 Returns a vector of length < 1 and > 0
1307
1308 vector randomvec()
1309 =================
1310 */
1311 void VM_randomvec (void)
1312 {
1313         vec3_t          temp;
1314         //float         length;
1315
1316         VM_SAFEPARMCOUNT(0, VM_randomvec);
1317
1318         //// WTF ??
1319         do
1320         {
1321                 temp[0] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1322                 temp[1] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1323                 temp[2] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1324         }
1325         while (DotProduct(temp, temp) >= 1);
1326         VectorCopy (temp, PRVM_G_VECTOR(OFS_RETURN));
1327
1328         /*
1329         temp[0] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1330         temp[1] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1331         temp[2] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1332         // length returned always > 0
1333         length = (rand()&32766 + 1) * (1.0 / 32767.0) / VectorLength(temp);
1334         VectorScale(temp,length, temp);*/
1335         //VectorCopy(temp, PRVM_G_VECTOR(OFS_RETURN));
1336 }
1337
1338 //=============================================================================
1339
1340 /*
1341 =========
1342 VM_registercvar
1343
1344 float   registercvar (string name, string value, float flags)
1345 =========
1346 */
1347 void VM_registercvar (void)
1348 {
1349         const char *name, *value;
1350         int     flags;
1351
1352         VM_SAFEPARMCOUNT(3,VM_registercvar);
1353
1354         name = PRVM_G_STRING(OFS_PARM0);
1355         value = PRVM_G_STRING(OFS_PARM1);
1356         flags = PRVM_G_FLOAT(OFS_PARM2);
1357         PRVM_G_FLOAT(OFS_RETURN) = 0;
1358
1359         if(flags > CVAR_MAXFLAGSVAL)
1360                 return;
1361
1362 // first check to see if it has already been defined
1363         if (Cvar_FindVar (name))
1364                 return;
1365
1366 // check for overlap with a command
1367         if (Cmd_Exists (name))
1368         {
1369                 Con_Printf("VM_registercvar: %s is a command\n", name);
1370                 return;
1371         }
1372
1373         Cvar_Get(name, value, flags);
1374
1375         PRVM_G_FLOAT(OFS_RETURN) = 1; // success
1376 }
1377
1378 /*
1379 =================
1380 VM_min
1381
1382 returns the minimum of two supplied floats
1383
1384 float min(float a, float b, ...[float])
1385 =================
1386 */
1387 void VM_min (void)
1388 {
1389         // LordHavoc: 3+ argument enhancement suggested by FrikaC
1390         if (prog->argc == 2)
1391                 PRVM_G_FLOAT(OFS_RETURN) = min(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1392         else if (prog->argc >= 3)
1393         {
1394                 int i;
1395                 float f = PRVM_G_FLOAT(OFS_PARM0);
1396                 for (i = 1;i < prog->argc;i++)
1397                         if (PRVM_G_FLOAT((OFS_PARM0+i*3)) < f)
1398                                 f = PRVM_G_FLOAT((OFS_PARM0+i*3));
1399                 PRVM_G_FLOAT(OFS_RETURN) = f;
1400         }
1401         else
1402                 PRVM_ERROR("VM_min: %s must supply at least 2 floats\n", PRVM_NAME);
1403 }
1404
1405 /*
1406 =================
1407 VM_max
1408
1409 returns the maximum of two supplied floats
1410
1411 float   max(float a, float b, ...[float])
1412 =================
1413 */
1414 void VM_max (void)
1415 {
1416         // LordHavoc: 3+ argument enhancement suggested by FrikaC
1417         if (prog->argc == 2)
1418                 PRVM_G_FLOAT(OFS_RETURN) = max(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1419         else if (prog->argc >= 3)
1420         {
1421                 int i;
1422                 float f = PRVM_G_FLOAT(OFS_PARM0);
1423                 for (i = 1;i < prog->argc;i++)
1424                         if (PRVM_G_FLOAT((OFS_PARM0+i*3)) > f)
1425                                 f = PRVM_G_FLOAT((OFS_PARM0+i*3));
1426                 PRVM_G_FLOAT(OFS_RETURN) = f;
1427         }
1428         else
1429                 PRVM_ERROR("VM_max: %s must supply at least 2 floats\n", PRVM_NAME);
1430 }
1431
1432 /*
1433 =================
1434 VM_bound
1435
1436 returns number bounded by supplied range
1437
1438 float   bound(float min, float value, float max)
1439 =================
1440 */
1441 void VM_bound (void)
1442 {
1443         VM_SAFEPARMCOUNT(3,VM_bound);
1444         PRVM_G_FLOAT(OFS_RETURN) = bound(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1), PRVM_G_FLOAT(OFS_PARM2));
1445 }
1446
1447 /*
1448 =================
1449 VM_pow
1450
1451 returns a raised to power b
1452
1453 float   pow(float a, float b)
1454 =================
1455 */
1456 void VM_pow (void)
1457 {
1458         VM_SAFEPARMCOUNT(2,VM_pow);
1459         PRVM_G_FLOAT(OFS_RETURN) = pow(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1460 }
1461
1462 /*
1463 =================
1464 VM_copyentity
1465
1466 copies data from one entity to another
1467
1468 copyentity(entity src, entity dst)
1469 =================
1470 */
1471 void VM_copyentity (void)
1472 {
1473         prvm_edict_t *in, *out;
1474         VM_SAFEPARMCOUNT(2,VM_copyentity);
1475         in = PRVM_G_EDICT(OFS_PARM0);
1476         out = PRVM_G_EDICT(OFS_PARM1);
1477         memcpy(out->fields.vp, in->fields.vp, prog->progs->entityfields * 4);
1478 }
1479
1480 /*
1481 =================
1482 VM_setcolor
1483
1484 sets the color of a client and broadcasts the update to all connected clients
1485
1486 setcolor(clientent, value)
1487 =================
1488 */
1489 /*void PF_setcolor (void)
1490 {
1491         client_t *client;
1492         int entnum, i;
1493         eval_t *val;
1494
1495         entnum = G_EDICTNUM(OFS_PARM0);
1496         i = G_FLOAT(OFS_PARM1);
1497
1498         if (entnum < 1 || entnum > svs.maxclients || !svs.clients[entnum-1].active)
1499         {
1500                 Con_Print("tried to setcolor a non-client\n");
1501                 return;
1502         }
1503
1504         client = svs.clients + entnum-1;
1505         if ((val = GETEDICTFIELDVALUE(client->edict, eval_clientcolors)))
1506                 val->_float = i;
1507         client->colors = i;
1508         client->old_colors = i;
1509         client->edict->v->team = (i & 15) + 1;
1510
1511         MSG_WriteByte (&sv.reliable_datagram, svc_updatecolors);
1512         MSG_WriteByte (&sv.reliable_datagram, entnum - 1);
1513         MSG_WriteByte (&sv.reliable_datagram, i);
1514 }*/
1515
1516 void VM_Files_Init(void)
1517 {
1518         memset(VM_FILES, 0, sizeof(qfile_t*[MAX_VMFILES]));
1519 }
1520
1521 void VM_Files_CloseAll(void)
1522 {
1523         int i;
1524         for (i = 0;i < MAX_VMFILES;i++)
1525         {
1526                 if (VM_FILES[i])
1527                         FS_Close(VM_FILES[i]);
1528                 //VM_FILES[i] = NULL;
1529         }
1530         memset(VM_FILES,0,sizeof(qfile_t*[MAX_VMFILES])); // this should be faster (is it ?)
1531 }
1532
1533 qfile_t *VM_GetFileHandle( int index )
1534 {
1535         if (index < 0 || index >= MAX_VMFILES)
1536         {
1537                 Con_Printf("VM_GetFileHandle: invalid file handle %i used in %s\n", index, PRVM_NAME);
1538                 return NULL;
1539         }
1540         if (VM_FILES[index] == NULL)
1541         {
1542                 Con_Printf("VM_GetFileHandle: no such file handle %i (or file has been closed) in %s\n", index, PRVM_NAME);
1543                 return NULL;
1544         }
1545         return VM_FILES[index];
1546 }
1547
1548 /*
1549 =========
1550 VM_fopen
1551
1552 float   fopen(string filename, float mode)
1553 =========
1554 */
1555 // float(string filename, float mode) fopen = #110;
1556 // opens a file inside quake/gamedir/data/ (mode is FILE_READ, FILE_APPEND, or FILE_WRITE),
1557 // returns fhandle >= 0 if successful, or fhandle < 0 if unable to open file for any reason
1558 void VM_fopen(void)
1559 {
1560         int filenum, mode;
1561         const char *modestring, *filename;
1562
1563         VM_SAFEPARMCOUNT(2,VM_fopen);
1564
1565         for (filenum = 0;filenum < MAX_VMFILES;filenum++)
1566                 if (VM_FILES[filenum] == NULL)
1567                         break;
1568         if (filenum >= MAX_VMFILES)
1569         {
1570                 Con_Printf("VM_fopen: %s ran out of file handles (%i)\n", PRVM_NAME, MAX_VMFILES);
1571                 PRVM_G_FLOAT(OFS_RETURN) = -2;
1572                 return;
1573         }
1574         mode = PRVM_G_FLOAT(OFS_PARM1);
1575         switch(mode)
1576         {
1577         case 0: // FILE_READ
1578                 modestring = "rb";
1579                 break;
1580         case 1: // FILE_APPEND
1581                 modestring = "ab";
1582                 break;
1583         case 2: // FILE_WRITE
1584                 modestring = "wb";
1585                 break;
1586         default:
1587                 Con_Printf("VM_fopen: %s no such mode %i (valid: 0 = read, 1 = append, 2 = write)\n", PRVM_NAME, mode);
1588                 PRVM_G_FLOAT(OFS_RETURN) = -3;
1589                 return;
1590         }
1591         filename = PRVM_G_STRING(OFS_PARM0);
1592         // .. is parent directory on many platforms
1593         // / is parent directory on Amiga
1594         // : is root of drive on Amiga (also used as a directory separator on Mac, but / works there too, so that's a bad idea)
1595         // \ is a windows-ism (so it's naughty to use it, / works on all platforms)
1596         if ((filename[0] == '.' && filename[1] == '.') || filename[0] == '/' || strrchr(filename, ':') || strrchr(filename, '\\'))
1597         {
1598                 Con_Printf("VM_fopen: %s dangerous or non-portable filename \"%s\" not allowed. (contains : or \\ or begins with .. or /)\n", PRVM_NAME, filename);
1599                 PRVM_G_FLOAT(OFS_RETURN) = -4;
1600                 return;
1601         }
1602         VM_FILES[filenum] = FS_Open(va("data/%s", filename), modestring, false, false);
1603         if (VM_FILES[filenum] == NULL && mode == 0)
1604                 VM_FILES[filenum] = FS_Open(va("%s", filename), modestring, false, false);
1605
1606         if (VM_FILES[filenum] == NULL)
1607                 PRVM_G_FLOAT(OFS_RETURN) = -1;
1608         else
1609                 PRVM_G_FLOAT(OFS_RETURN) = filenum;
1610 }
1611
1612 /*
1613 =========
1614 VM_fclose
1615
1616 fclose(float fhandle)
1617 =========
1618 */
1619 //void(float fhandle) fclose = #111; // closes a file
1620 void VM_fclose(void)
1621 {
1622         int filenum;
1623
1624         VM_SAFEPARMCOUNT(1,VM_fclose);
1625
1626         filenum = PRVM_G_FLOAT(OFS_PARM0);
1627         if (filenum < 0 || filenum >= MAX_VMFILES)
1628         {
1629                 Con_Printf("VM_fclose: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1630                 return;
1631         }
1632         if (VM_FILES[filenum] == NULL)
1633         {
1634                 Con_Printf("VM_fclose: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1635                 return;
1636         }
1637         FS_Close(VM_FILES[filenum]);
1638         VM_FILES[filenum] = NULL;
1639 }
1640
1641 /*
1642 =========
1643 VM_fgets
1644
1645 string  fgets(float fhandle)
1646 =========
1647 */
1648 //string(float fhandle) fgets = #112; // reads a line of text from the file and returns as a tempstring
1649 void VM_fgets(void)
1650 {
1651         int c, end;
1652         static char string[VM_STRINGTEMP_LENGTH];
1653         int filenum;
1654
1655         VM_SAFEPARMCOUNT(1,VM_fgets);
1656
1657         filenum = PRVM_G_FLOAT(OFS_PARM0);
1658         if (filenum < 0 || filenum >= MAX_VMFILES)
1659         {
1660                 Con_Printf("VM_fgets: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1661                 return;
1662         }
1663         if (VM_FILES[filenum] == NULL)
1664         {
1665                 Con_Printf("VM_fgets: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1666                 return;
1667         }
1668         end = 0;
1669         for (;;)
1670         {
1671                 c = FS_Getc(VM_FILES[filenum]);
1672                 if (c == '\r' || c == '\n' || c < 0)
1673                         break;
1674                 if (end < VM_STRINGTEMP_LENGTH - 1)
1675                         string[end++] = c;
1676         }
1677         string[end] = 0;
1678         // remove \n following \r
1679         if (c == '\r')
1680         {
1681                 c = FS_Getc(VM_FILES[filenum]);
1682                 if (c != '\n')
1683                         FS_UnGetc(VM_FILES[filenum], (unsigned char)c);
1684         }
1685         if (developer.integer >= 3)
1686                 Con_Printf("fgets: %s: %s\n", PRVM_NAME, string);
1687         if (c >= 0 || end)
1688                 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(string);
1689         else
1690                 PRVM_G_INT(OFS_RETURN) = 0;
1691 }
1692
1693 /*
1694 =========
1695 VM_fputs
1696
1697 fputs(float fhandle, string s)
1698 =========
1699 */
1700 //void(float fhandle, string s) fputs = #113; // writes a line of text to the end of the file
1701 void VM_fputs(void)
1702 {
1703         int stringlength;
1704         char string[VM_STRINGTEMP_LENGTH];
1705         int filenum;
1706
1707         VM_SAFEPARMCOUNT(2,VM_fputs);
1708
1709         filenum = PRVM_G_FLOAT(OFS_PARM0);
1710         if (filenum < 0 || filenum >= MAX_VMFILES)
1711         {
1712                 Con_Printf("VM_fputs: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1713                 return;
1714         }
1715         if (VM_FILES[filenum] == NULL)
1716         {
1717                 Con_Printf("VM_fputs: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1718                 return;
1719         }
1720         VM_VarString(1, string, sizeof(string));
1721         if ((stringlength = strlen(string)))
1722                 FS_Write(VM_FILES[filenum], string, stringlength);
1723         if (developer.integer)
1724                 Con_Printf("fputs: %s: %s\n", PRVM_NAME, string);
1725 }
1726
1727 /*
1728 =========
1729 VM_strlen
1730
1731 float   strlen(string s)
1732 =========
1733 */
1734 //float(string s) strlen = #114; // returns how many characters are in a string
1735 void VM_strlen(void)
1736 {
1737         const char *s;
1738
1739         VM_SAFEPARMCOUNT(1,VM_strlen);
1740
1741         s = PRVM_G_STRING(OFS_PARM0);
1742         if (s)
1743                 PRVM_G_FLOAT(OFS_RETURN) = strlen(s);
1744         else
1745                 PRVM_G_FLOAT(OFS_RETURN) = 0;
1746 }
1747
1748 /*
1749 =========
1750 VM_strcat
1751
1752 string strcat(string,string,...[string])
1753 =========
1754 */
1755 //string(string s1, string s2) strcat = #115;
1756 // concatenates two strings (for example "abc", "def" would return "abcdef")
1757 // and returns as a tempstring
1758 void VM_strcat(void)
1759 {
1760         char *s;
1761
1762         if(prog->argc < 1)
1763                 PRVM_ERROR("VM_strcat wrong parameter count (min. 1 expected ) !\n");
1764
1765         s = VM_GetTempString();
1766         VM_VarString(0, s, VM_STRINGTEMP_LENGTH);
1767         PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(s);
1768 }
1769
1770 /*
1771 =========
1772 VM_substring
1773
1774 string  substring(string s, float start, float length)
1775 =========
1776 */
1777 // string(string s, float start, float length) substring = #116;
1778 // returns a section of a string as a tempstring
1779 void VM_substring(void)
1780 {
1781         int i, start, length;
1782         const char *s;
1783         char *string;
1784
1785         VM_SAFEPARMCOUNT(3,VM_substring);
1786
1787         string = VM_GetTempString();
1788         s = PRVM_G_STRING(OFS_PARM0);
1789         start = PRVM_G_FLOAT(OFS_PARM1);
1790         length = PRVM_G_FLOAT(OFS_PARM2);
1791         if (!s)
1792                 s = "";
1793         for (i = 0;i < start && *s;i++, s++);
1794         for (i = 0;i < VM_STRINGTEMP_LENGTH - 1 && *s && i < length;i++, s++)
1795                 string[i] = *s;
1796         string[i] = 0;
1797         PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(string);
1798 }
1799
1800 /*
1801 =========
1802 VM_stov
1803
1804 vector  stov(string s)
1805 =========
1806 */
1807 //vector(string s) stov = #117; // returns vector value from a string
1808 void VM_stov(void)
1809 {
1810         char string[VM_STRINGTEMP_LENGTH];
1811
1812         VM_SAFEPARMCOUNT(1,VM_stov);
1813
1814         VM_VarString(0, string, sizeof(string));
1815         Math_atov(string, PRVM_G_VECTOR(OFS_RETURN));
1816 }
1817
1818 /*
1819 =========
1820 VM_strzone
1821
1822 string  strzone(string s)
1823 =========
1824 */
1825 //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)
1826 void VM_strzone(void)
1827 {
1828         const char *in;
1829         char *out;
1830
1831         VM_SAFEPARMCOUNT(1,VM_strzone);
1832
1833         in = PRVM_G_STRING(OFS_PARM0);
1834         out = PRVM_AllocString(strlen(in) + 1);
1835         strcpy(out, in);
1836         PRVM_G_INT(OFS_RETURN) = PRVM_SetQCString(out);
1837 }
1838
1839 /*
1840 =========
1841 VM_strunzone
1842
1843 strunzone(string s)
1844 =========
1845 */
1846 //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!!!)
1847 void VM_strunzone(void)
1848 {
1849         VM_SAFEPARMCOUNT(1,VM_strunzone);
1850         PRVM_FreeString((char *)PRVM_G_STRING(OFS_PARM0));
1851 }
1852
1853 /*
1854 =========
1855 VM_command (used by client and menu)
1856
1857 clientcommand(float client, string s) (for client and menu)
1858 =========
1859 */
1860 //void(entity e, string s) clientcommand = #440; // executes a command string as if it came from the specified client
1861 //this function originally written by KrimZon, made shorter by LordHavoc
1862 void VM_clcommand (void)
1863 {
1864         client_t *temp_client;
1865         int i;
1866
1867         VM_SAFEPARMCOUNT(2,VM_clcommand);
1868
1869         i = PRVM_G_FLOAT(OFS_PARM0);
1870         if (!sv.active  || i < 0 || i >= svs.maxclients || !svs.clients[i].active)
1871         {
1872                 Con_Printf("VM_clientcommand: %s: invalid client/server is not active !\n", PRVM_NAME);
1873                 return;
1874         }
1875
1876         temp_client = host_client;
1877         host_client = svs.clients + i;
1878         Cmd_ExecuteString (PRVM_G_STRING(OFS_PARM1), src_client);
1879         host_client = temp_client;
1880 }
1881
1882
1883 /*
1884 =========
1885 VM_tokenize
1886
1887 float tokenize(string s)
1888 =========
1889 */
1890 //float(string s) tokenize = #441;
1891 // takes apart a string into individal words (access them with argv), returns how many
1892 // this function originally written by KrimZon, made shorter by LordHavoc
1893 static char **tokens = NULL;
1894 static int    max_tokens, num_tokens = 0;
1895 void VM_tokenize (void)
1896 {
1897         const char *p, *str;
1898
1899         VM_SAFEPARMCOUNT(1,VM_tokenize);
1900
1901         str = PRVM_G_STRING(OFS_PARM0);
1902
1903         if (tokens != NULL)
1904         {
1905                 int i;
1906                 for (i=0;i<num_tokens;i++)
1907                         Z_Free(tokens[i]);
1908                 Z_Free(tokens);
1909                 num_tokens = 0;
1910         }
1911
1912         tokens = Z_Malloc(strlen(str) * sizeof(char *));
1913         max_tokens = strlen(str);
1914
1915         for (p = str;COM_ParseToken(&p, false) && num_tokens < max_tokens;num_tokens++)
1916         {
1917                 tokens[num_tokens] = Z_Malloc(strlen(com_token) + 1);
1918                 strcpy(tokens[num_tokens], com_token);
1919         }
1920
1921         PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
1922 }
1923
1924 /*
1925 =========
1926 VM_argv
1927
1928 string argv(float n)
1929 =========
1930 */
1931 //string(float n) argv = #442;
1932 // returns a word from the tokenized string (returns nothing for an invalid index)
1933 // this function originally written by KrimZon, made shorter by LordHavoc
1934 void VM_argv (void)
1935 {
1936         int token_num;
1937
1938         VM_SAFEPARMCOUNT(1,VM_argv);
1939
1940         token_num = PRVM_G_FLOAT(OFS_PARM0);
1941         if (token_num >= 0 && token_num < num_tokens)
1942                 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(tokens[token_num]);
1943         else
1944                 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(NULL);
1945 }
1946
1947 /*
1948 //void(entity e, entity tagentity, string tagname) setattachment = #443; // attachs e to a tag on tagentity (note: use "" to attach to entity origin/angles instead of a tag)
1949 void PF_setattachment (void)
1950 {
1951         edict_t *e = G_EDICT(OFS_PARM0);
1952         edict_t *tagentity = G_EDICT(OFS_PARM1);
1953         char *tagname = G_STRING(OFS_PARM2);
1954         eval_t *v;
1955         int i, modelindex;
1956         model_t *model;
1957
1958         if (tagentity == NULL)
1959                 tagentity = sv.edicts;
1960
1961         v = GETEDICTFIELDVALUE(e, eval_tag_entity);
1962         if (v)
1963                 v->edict = EDICT_TO_PROG(tagentity);
1964
1965         v = GETEDICTFIELDVALUE(e, eval_tag_index);
1966         if (v)
1967                 v->_float = 0;
1968         if (tagentity != NULL && tagentity != sv.edicts && tagname && tagname[0])
1969         {
1970                 modelindex = (int)tagentity->v->modelindex;
1971                 if (modelindex >= 0 && modelindex < MAX_MODELS)
1972                 {
1973                         model = sv.models[modelindex];
1974                         if (model->data_overridetagnamesforskin && (unsigned int)tagentity->v->skin < (unsigned int)model->numskins && model->data_overridetagnamesforskin[(unsigned int)tagentity->v->skin].num_overridetagnames)
1975                                 for (i = 0;i < model->data_overridetagnamesforskin[(unsigned int)tagentity->v->skin].num_overridetagnames;i++)
1976                                         if (!strcmp(tagname, model->data_overridetagnamesforskin[(unsigned int)tagentity->v->skin].data_overridetagnames[i].name))
1977                                                 v->_float = i + 1;
1978                         // FIXME: use a model function to get tag info (need to handle skeletal)
1979                         if (v->_float == 0 && model->num_tags)
1980                                 for (i = 0;i < model->num_tags;i++)
1981                                         if (!strcmp(tagname, model->data_tags[i].name))
1982                                                 v->_float = i + 1;
1983                         if (v->_float == 0)
1984                                 Con_DPrintf("setattachment(edict %i, edict %i, string \"%s\"): tried to find tag named \"%s\" on entity %i (model \"%s\") but could not find it\n", NUM_FOR_EDICT(e), NUM_FOR_EDICT(tagentity), tagname, tagname, NUM_FOR_EDICT(tagentity), model->name);
1985                 }
1986                 else
1987                         Con_DPrintf("setattachment(edict %i, edict %i, string \"%s\"): tried to find tag named \"%s\" on entity %i but it has no model\n", NUM_FOR_EDICT(e), NUM_FOR_EDICT(tagentity), tagname, tagname, NUM_FOR_EDICT(tagentity));
1988         }
1989 }*/
1990
1991 /*
1992 =========
1993 VM_isserver
1994
1995 float   isserver()
1996 =========
1997 */
1998 void VM_isserver(void)
1999 {
2000         VM_SAFEPARMCOUNT(0,VM_serverstate);
2001
2002         PRVM_G_FLOAT(OFS_RETURN) = sv.active;
2003 }
2004
2005 /*
2006 =========
2007 VM_clientcount
2008
2009 float   clientcount()
2010 =========
2011 */
2012 void VM_clientcount(void)
2013 {
2014         VM_SAFEPARMCOUNT(0,VM_clientcount);
2015
2016         PRVM_G_FLOAT(OFS_RETURN) = svs.maxclients;
2017 }
2018
2019 /*
2020 =========
2021 VM_clientstate
2022
2023 float   clientstate()
2024 =========
2025 */
2026 void VM_clientstate(void)
2027 {
2028         VM_SAFEPARMCOUNT(0,VM_clientstate);
2029
2030         PRVM_G_FLOAT(OFS_RETURN) = cls.state;
2031 }
2032
2033 /*
2034 =========
2035 VM_getostype
2036
2037 float   getostype(void)
2038 =========
2039 */ // not used at the moment -> not included in the common list
2040 void VM_getostype(void)
2041 {
2042         VM_SAFEPARMCOUNT(0,VM_getostype);
2043
2044         /*
2045         OS_WINDOWS
2046         OS_LINUX
2047         OS_MAC - not supported
2048         */
2049
2050 #ifdef _WIN32
2051         PRVM_G_FLOAT(OFS_RETURN) = 0;
2052 #elif defined _MAC
2053         PRVM_G_FLOAT(OFS_RETURN) = 2;
2054 #else
2055         PRVM_G_FLOAT(OFS_RETURN) = 1;
2056 #endif
2057 }
2058
2059 /*
2060 =========
2061 VM_getmousepos
2062
2063 vector  getmousepos()
2064 =========
2065 */
2066 void VM_getmousepos(void)
2067 {
2068
2069         VM_SAFEPARMCOUNT(0,VM_getmousepos);
2070
2071         PRVM_G_VECTOR(OFS_RETURN)[0] = in_mouse_x * vid_conwidth.integer / vid.width;
2072         PRVM_G_VECTOR(OFS_RETURN)[1] = in_mouse_y * vid_conheight.integer / vid.height;
2073         PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
2074 }
2075
2076 /*
2077 =========
2078 VM_gettime
2079
2080 float   gettime(void)
2081 =========
2082 */
2083 void VM_gettime(void)
2084 {
2085         VM_SAFEPARMCOUNT(0,VM_gettime);
2086
2087         PRVM_G_FLOAT(OFS_RETURN) = (float) *prog->time;
2088 }
2089
2090 /*
2091 =========
2092 VM_loadfromdata
2093
2094 loadfromdata(string data)
2095 =========
2096 */
2097 void VM_loadfromdata(void)
2098 {
2099         VM_SAFEPARMCOUNT(1,VM_loadentsfromfile);
2100
2101         PRVM_ED_LoadFromFile(PRVM_G_STRING(OFS_PARM0));
2102 }
2103
2104 /*
2105 ========================
2106 VM_parseentitydata
2107
2108 parseentitydata(entity ent, string data)
2109 ========================
2110 */
2111 void VM_parseentitydata(void)
2112 {
2113         prvm_edict_t *ent;
2114         const char *data;
2115
2116         VM_SAFEPARMCOUNT(2, VM_parseentitydata);
2117
2118     // get edict and test it
2119         ent = PRVM_G_EDICT(OFS_PARM0);
2120         if (ent->priv.required->free)
2121                 PRVM_ERROR ("VM_parseentitydata: %s: Can only set already spawned entities (entity %i is free)!\n", PRVM_NAME, PRVM_NUM_FOR_EDICT(ent));
2122
2123         data = PRVM_G_STRING(OFS_PARM1);
2124
2125     // parse the opening brace
2126         if (!COM_ParseToken(&data, false) || com_token[0] != '{' )
2127                 PRVM_ERROR ("VM_parseentitydata: %s: Couldn't parse entity data:\n%s\n", PRVM_NAME, data );
2128
2129         PRVM_ED_ParseEdict (data, ent);
2130 }
2131
2132 /*
2133 =========
2134 VM_loadfromfile
2135
2136 loadfromfile(string file)
2137 =========
2138 */
2139 void VM_loadfromfile(void)
2140 {
2141         const char *filename;
2142         qbyte *data;
2143
2144         VM_SAFEPARMCOUNT(1,VM_loadfromfile);
2145
2146         filename = PRVM_G_STRING(OFS_PARM0);
2147         // .. is parent directory on many platforms
2148         // / is parent directory on Amiga
2149         // : is root of drive on Amiga (also used as a directory separator on Mac, but / works there too, so that's a bad idea)
2150         // \ is a windows-ism (so it's naughty to use it, / works on all platforms)
2151         if ((filename[0] == '.' && filename[1] == '.') || filename[0] == '/' || strrchr(filename, ':') || strrchr(filename, '\\'))
2152         {
2153                 Con_Printf("VM_loadfromfile: %s dangerous or non-portable filename \"%s\" not allowed. (contains : or \\ or begins with .. or /)\n", PRVM_NAME, filename);
2154                 PRVM_G_FLOAT(OFS_RETURN) = -4;
2155                 return;
2156         }
2157
2158         // not conform with VM_fopen
2159         data = FS_LoadFile(filename, tempmempool, false);
2160         if (data == NULL)
2161                 PRVM_G_FLOAT(OFS_RETURN) = -1;
2162
2163         PRVM_ED_LoadFromFile(data);
2164
2165         if(data)
2166                 Mem_Free(data);
2167 }
2168
2169
2170 /*
2171 =========
2172 VM_modulo
2173
2174 float   mod(float val, float m)
2175 =========
2176 */
2177 void VM_modulo(void)
2178 {
2179         int val, m;
2180         VM_SAFEPARMCOUNT(2,VM_module);
2181
2182         val = (int) PRVM_G_FLOAT(OFS_PARM0);
2183         m       = (int) PRVM_G_FLOAT(OFS_PARM1);
2184
2185         PRVM_G_FLOAT(OFS_RETURN) = (float) (val % m);
2186 }
2187
2188 void VM_Search_Init(void)
2189 {
2190         memset(VM_SEARCHLIST,0,sizeof(fssearch_t*[MAX_VMSEARCHES]));
2191 }
2192
2193 void VM_Search_Reset(void)
2194 {
2195         int i;
2196         // reset the fssearch list
2197         for(i = 0; i < MAX_VMSEARCHES; i++)
2198                 if(VM_SEARCHLIST[i])
2199                         FS_FreeSearch(VM_SEARCHLIST[i]);
2200         memset(VM_SEARCHLIST,0,sizeof(fssearch_t*[MAX_VMSEARCHES]));
2201 }
2202
2203 /*
2204 =========
2205 VM_search_begin
2206
2207 float search_begin(string pattern, float caseinsensitive, float quiet)
2208 =========
2209 */
2210 void VM_search_begin(void)
2211 {
2212         int handle;
2213         const char *pattern;
2214         int caseinsens, quiet;
2215
2216         VM_SAFEPARMCOUNT(3, VM_search_begin);
2217
2218         pattern = PRVM_G_STRING(OFS_PARM0);
2219
2220         VM_CheckEmptyString(pattern);
2221
2222         caseinsens = PRVM_G_FLOAT(OFS_PARM1);
2223         quiet = PRVM_G_FLOAT(OFS_PARM2);
2224
2225         for(handle = 0; handle < MAX_VMSEARCHES; handle++)
2226                 if(!VM_SEARCHLIST[handle])
2227                         break;
2228
2229         if(handle >= MAX_VMSEARCHES)
2230         {
2231                 Con_Printf("VM_search_begin: %s ran out of search handles (%i)\n", PRVM_NAME, MAX_VMSEARCHES);
2232                 PRVM_G_FLOAT(OFS_RETURN) = -2;
2233                 return;
2234         }
2235
2236         if(!(VM_SEARCHLIST[handle] = FS_Search(pattern,caseinsens, quiet)))
2237                 PRVM_G_FLOAT(OFS_RETURN) = -1;
2238         else
2239                 PRVM_G_FLOAT(OFS_RETURN) = handle;
2240 }
2241
2242 /*
2243 =========
2244 VM_search_end
2245
2246 void    search_end(float handle)
2247 =========
2248 */
2249 void VM_search_end(void)
2250 {
2251         int handle;
2252         VM_SAFEPARMCOUNT(1, VM_search_end);
2253
2254         handle = PRVM_G_FLOAT(OFS_PARM0);
2255
2256         if(handle < 0 || handle >= MAX_VMSEARCHES)
2257         {
2258                 Con_Printf("VM_search_end: invalid handle %i used in %s\n", handle, PRVM_NAME);
2259                 return;
2260         }
2261         if(VM_SEARCHLIST[handle] == NULL)
2262         {
2263                 Con_Printf("VM_search_end: no such handle %i in %s\n", handle, PRVM_NAME);
2264                 return;
2265         }
2266
2267         FS_FreeSearch(VM_SEARCHLIST[handle]);
2268         VM_SEARCHLIST[handle] = NULL;
2269 }
2270
2271 /*
2272 =========
2273 VM_search_getsize
2274
2275 float   search_getsize(float handle)
2276 =========
2277 */
2278 void VM_search_getsize(void)
2279 {
2280         int handle;
2281         VM_SAFEPARMCOUNT(1, VM_M_search_getsize);
2282
2283         handle = PRVM_G_FLOAT(OFS_PARM0);
2284
2285         if(handle < 0 || handle >= MAX_VMSEARCHES)
2286         {
2287                 Con_Printf("VM_search_getsize: invalid handle %i used in %s\n", handle, PRVM_NAME);
2288                 return;
2289         }
2290         if(VM_SEARCHLIST[handle] == NULL)
2291         {
2292                 Con_Printf("VM_search_getsize: no such handle %i in %s\n", handle, PRVM_NAME);
2293                 return;
2294         }
2295
2296         PRVM_G_FLOAT(OFS_RETURN) = VM_SEARCHLIST[handle]->numfilenames;
2297 }
2298
2299 /*
2300 =========
2301 VM_search_getfilename
2302
2303 string  search_getfilename(float handle, float num)
2304 =========
2305 */
2306 void VM_search_getfilename(void)
2307 {
2308         int handle, filenum;
2309         char *tmp;
2310         VM_SAFEPARMCOUNT(2, VM_search_getfilename);
2311
2312         handle = PRVM_G_FLOAT(OFS_PARM0);
2313         filenum = PRVM_G_FLOAT(OFS_PARM1);
2314
2315         if(handle < 0 || handle >= MAX_VMSEARCHES)
2316         {
2317                 Con_Printf("VM_search_getfilename: invalid handle %i used in %s\n", handle, PRVM_NAME);
2318                 return;
2319         }
2320         if(VM_SEARCHLIST[handle] == NULL)
2321         {
2322                 Con_Printf("VM_search_getfilename: no such handle %i in %s\n", handle, PRVM_NAME);
2323                 return;
2324         }
2325         if(filenum < 0 || filenum >= VM_SEARCHLIST[handle]->numfilenames)
2326         {
2327                 Con_Printf("VM_search_getfilename: invalid filenum %i in %s\n", filenum, PRVM_NAME);
2328                 return;
2329         }
2330
2331         tmp = VM_GetTempString();
2332         strcpy(tmp, VM_SEARCHLIST[handle]->filenames[filenum]);
2333
2334         PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(tmp);
2335 }
2336
2337 /*
2338 =========
2339 VM_chr
2340
2341 string  chr(float ascii)
2342 =========
2343 */
2344 void VM_chr(void)
2345 {
2346         char *tmp;
2347         VM_SAFEPARMCOUNT(1, VM_chr);
2348
2349         tmp = VM_GetTempString();
2350         tmp[0] = (unsigned char) PRVM_G_FLOAT(OFS_PARM0);
2351         tmp[1] = 0;
2352
2353         PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(tmp);
2354 }
2355
2356 //=============================================================================
2357 // Draw builtins (client & menu)
2358
2359 /*
2360 =========
2361 VM_iscachedpic
2362
2363 float   iscachedpic(string pic)
2364 =========
2365 */
2366 void VM_iscachedpic(void)
2367 {
2368         VM_SAFEPARMCOUNT(1,VM_iscachedpic);
2369
2370         // drawq hasnt such a function, thus always return true
2371         PRVM_G_FLOAT(OFS_RETURN) = false;
2372 }
2373
2374 /*
2375 =========
2376 VM_precache_pic
2377
2378 string  precache_pic(string pic)
2379 =========
2380 */
2381 void VM_precache_pic(void)
2382 {
2383         const char      *s;
2384
2385         VM_SAFEPARMCOUNT(1, VM_precache_pic);
2386
2387         s = PRVM_G_STRING(OFS_PARM0);
2388         PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
2389
2390         if(!s)
2391                 PRVM_ERROR ("VM_precache_pic: %s: NULL\n", PRVM_NAME);
2392
2393         VM_CheckEmptyString (s);
2394
2395         // AK Draw_CachePic is supposed to always return a valid pointer
2396         if( Draw_CachePic(s, false)->tex == r_texture_notexture )
2397                 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(NULL);
2398 }
2399
2400 /*
2401 =========
2402 VM_freepic
2403
2404 freepic(string s)
2405 =========
2406 */
2407 void VM_freepic(void)
2408 {
2409         const char *s;
2410
2411         VM_SAFEPARMCOUNT(1,VM_freepic);
2412
2413         s = PRVM_G_STRING(OFS_PARM0);
2414
2415         if(!s)
2416                 PRVM_ERROR ("VM_freepic: %s: NULL\n");
2417
2418         VM_CheckEmptyString (s);
2419
2420         Draw_FreePic(s);
2421 }
2422
2423 /*
2424 =========
2425 VM_drawcharacter
2426
2427 float   drawcharacter(vector position, float character, vector scale, vector rgb, float alpha, float flag)
2428 =========
2429 */
2430 void VM_drawcharacter(void)
2431 {
2432         float *pos,*scale,*rgb;
2433         char   character;
2434         int flag;
2435         VM_SAFEPARMCOUNT(6,VM_drawcharacter);
2436
2437         character = (char) PRVM_G_FLOAT(OFS_PARM1);
2438         if(character == 0)
2439         {
2440                 Con_Printf("VM_drawcharacter: %s passed null character !\n",PRVM_NAME);
2441                 PRVM_G_FLOAT(OFS_RETURN) = -1;
2442                 return;
2443         }
2444
2445         pos = PRVM_G_VECTOR(OFS_PARM0);
2446         scale = PRVM_G_VECTOR(OFS_PARM2);
2447         rgb = PRVM_G_VECTOR(OFS_PARM3);
2448         flag = (int)PRVM_G_FLOAT(OFS_PARM5);
2449
2450         if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2451         {
2452                 Con_Printf("VM_drawcharacter: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2453                 PRVM_G_FLOAT(OFS_RETURN) = -2;
2454                 return;
2455         }
2456
2457         if(pos[2] || scale[2])
2458                 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")));
2459
2460         if(!scale[0] || !scale[1])
2461         {
2462                 Con_Printf("VM_drawcharacter: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
2463                 PRVM_G_FLOAT(OFS_RETURN) = -3;
2464                 return;
2465         }
2466
2467         DrawQ_String (pos[0], pos[1], &character, 1, scale[0], scale[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM4), flag);
2468         PRVM_G_FLOAT(OFS_RETURN) = 1;
2469 }
2470
2471 /*
2472 =========
2473 VM_drawstring
2474
2475 float   drawstring(vector position, string text, vector scale, vector rgb, float alpha, float flag)
2476 =========
2477 */
2478 void VM_drawstring(void)
2479 {
2480         float *pos,*scale,*rgb;
2481         const char  *string;
2482         int flag;
2483         VM_SAFEPARMCOUNT(6,VM_drawstring);
2484
2485         string = PRVM_G_STRING(OFS_PARM1);
2486         if(!string)
2487         {
2488                 Con_Printf("VM_drawstring: %s passed null string !\n",PRVM_NAME);
2489                 PRVM_G_FLOAT(OFS_RETURN) = -1;
2490                 return;
2491         }
2492
2493         //VM_CheckEmptyString(string); Why should it be checked - perhaps the menu wants to support the precolored letters, too?
2494
2495         pos = PRVM_G_VECTOR(OFS_PARM0);
2496         scale = PRVM_G_VECTOR(OFS_PARM2);
2497         rgb = PRVM_G_VECTOR(OFS_PARM3);
2498         flag = (int)PRVM_G_FLOAT(OFS_PARM5);
2499
2500         if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2501         {
2502                 Con_Printf("VM_drawstring: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2503                 PRVM_G_FLOAT(OFS_RETURN) = -2;
2504                 return;
2505         }
2506
2507         if(!scale[0] || !scale[1])
2508         {
2509                 Con_Printf("VM_drawstring: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
2510                 PRVM_G_FLOAT(OFS_RETURN) = -3;
2511                 return;
2512         }
2513
2514         if(pos[2] || scale[2])
2515                 Con_Printf("VM_drawstring: z value%c from %s discarded\n",(pos[2] && scale[2]) ? 's' : 0,((pos[2] && scale[2]) ? "pos and scale" : (pos[2] ? "pos" : "scale")));
2516
2517         DrawQ_String (pos[0], pos[1], string, 0, scale[0], scale[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM4), flag);
2518         PRVM_G_FLOAT(OFS_RETURN) = 1;
2519 }
2520 /*
2521 =========
2522 VM_drawpic
2523
2524 float   drawpic(vector position, string pic, vector size, vector rgb, float alpha, float flag)
2525 =========
2526 */
2527 void VM_drawpic(void)
2528 {
2529         const char *pic;
2530         float *size, *pos, *rgb;
2531         int flag;
2532
2533         VM_SAFEPARMCOUNT(6,VM_drawpic);
2534
2535         pic = PRVM_G_STRING(OFS_PARM1);
2536
2537         if(!pic)
2538         {
2539                 Con_Printf("VM_drawpic: %s passed null picture name !\n", PRVM_NAME);
2540                 PRVM_G_FLOAT(OFS_RETURN) = -1;
2541                 return;
2542         }
2543
2544         VM_CheckEmptyString (pic);
2545
2546         // is pic cached ? no function yet for that
2547         if(!1)
2548         {
2549                 Con_Printf("VM_drawpic: %s: %s not cached !\n", PRVM_NAME, pic);
2550                 PRVM_G_FLOAT(OFS_RETURN) = -4;
2551                 return;
2552         }
2553
2554         pos = PRVM_G_VECTOR(OFS_PARM0);
2555         size = PRVM_G_VECTOR(OFS_PARM2);
2556         rgb = PRVM_G_VECTOR(OFS_PARM3);
2557         flag = (int) PRVM_G_FLOAT(OFS_PARM5);
2558
2559         if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2560         {
2561                 Con_Printf("VM_drawstring: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2562                 PRVM_G_FLOAT(OFS_RETURN) = -2;
2563                 return;
2564         }
2565
2566         if(pos[2] || size[2])
2567                 Con_Printf("VM_drawstring: z value%c from %s discarded\n",(pos[2] && size[2]) ? 's' : 0,((pos[2] && size[2]) ? "pos and size" : (pos[2] ? "pos" : "size")));
2568
2569         DrawQ_Pic(pos[0], pos[1], pic, size[0], size[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM4), flag);
2570         PRVM_G_FLOAT(OFS_RETURN) = 1;
2571 }
2572
2573 /*
2574 =========
2575 VM_drawfill
2576
2577 float drawfill(vector position, vector size, vector rgb, float alpha, float flag)
2578 =========
2579 */
2580 void VM_drawfill(void)
2581 {
2582         float *size, *pos, *rgb;
2583         int flag;
2584
2585         VM_SAFEPARMCOUNT(5,VM_drawfill);
2586
2587
2588         pos = PRVM_G_VECTOR(OFS_PARM0);
2589         size = PRVM_G_VECTOR(OFS_PARM1);
2590         rgb = PRVM_G_VECTOR(OFS_PARM2);
2591         flag = (int) PRVM_G_FLOAT(OFS_PARM4);
2592
2593         if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2594         {
2595                 Con_Printf("VM_drawstring: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2596                 PRVM_G_FLOAT(OFS_RETURN) = -2;
2597                 return;
2598         }
2599
2600         if(pos[2] || size[2])
2601                 Con_Printf("VM_drawstring: z value%c from %s discarded\n",(pos[2] && size[2]) ? 's' : 0,((pos[2] && size[2]) ? "pos and size" : (pos[2] ? "pos" : "size")));
2602
2603         DrawQ_Pic(pos[0], pos[1], 0, size[0], size[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM3), flag);
2604         PRVM_G_FLOAT(OFS_RETURN) = 1;
2605 }
2606
2607 /*
2608 =========
2609 VM_drawsetcliparea
2610
2611 drawsetcliparea(float x, float y, float width, float height)
2612 =========
2613 */
2614 void VM_drawsetcliparea(void)
2615 {
2616         float x,y,w,h;
2617         VM_SAFEPARMCOUNT(4,VM_drawsetcliparea);
2618
2619         x = bound(0, PRVM_G_FLOAT(OFS_PARM0), vid_conwidth.integer);
2620         y = bound(0, PRVM_G_FLOAT(OFS_PARM1), vid_conheight.integer);
2621         w = bound(0, PRVM_G_FLOAT(OFS_PARM2) + PRVM_G_FLOAT(OFS_PARM0) - x, (vid_conwidth.integer  - x));
2622         h = bound(0, PRVM_G_FLOAT(OFS_PARM3) + PRVM_G_FLOAT(OFS_PARM1) - y, (vid_conheight.integer - y));
2623
2624         DrawQ_SetClipArea(x, y, w, h);
2625 }
2626
2627 /*
2628 =========
2629 VM_drawresetcliparea
2630
2631 drawresetcliparea()
2632 =========
2633 */
2634 void VM_drawresetcliparea(void)
2635 {
2636         VM_SAFEPARMCOUNT(0,VM_drawresetcliparea);
2637
2638         DrawQ_ResetClipArea();
2639 }
2640
2641 /*
2642 =========
2643 VM_getimagesize
2644
2645 vector  getimagesize(string pic)
2646 =========
2647 */
2648 void VM_getimagesize(void)
2649 {
2650         const char *p;
2651         cachepic_t *pic;
2652
2653         VM_SAFEPARMCOUNT(1,VM_getimagesize);
2654
2655         p = PRVM_G_STRING(OFS_PARM0);
2656
2657         if(!p)
2658                 PRVM_ERROR("VM_getimagepos: %s passed null picture name !\n", PRVM_NAME);
2659
2660         VM_CheckEmptyString (p);
2661
2662         pic = Draw_CachePic (p, false);
2663
2664         PRVM_G_VECTOR(OFS_RETURN)[0] = pic->width;
2665         PRVM_G_VECTOR(OFS_RETURN)[1] = pic->height;
2666         PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
2667 }
2668
2669 // CL_Video interface functions
2670
2671 /*
2672 ========================
2673 VM_cin_open
2674
2675 float cin_open(string file, string name)
2676 ========================
2677 */
2678 void VM_cin_open( void )
2679 {
2680         const char *file;
2681         const char *name;
2682
2683         VM_SAFEPARMCOUNT( 2, VM_cin_open );
2684
2685         file = PRVM_G_STRING( OFS_PARM0 );
2686         name = PRVM_G_STRING( OFS_PARM1 );
2687
2688         VM_CheckEmptyString( file );
2689     VM_CheckEmptyString( name );
2690
2691         if( CL_OpenVideo( file, name, MENUOWNER ) )
2692                 PRVM_G_FLOAT( OFS_RETURN ) = 1;
2693         else
2694                 PRVM_G_FLOAT( OFS_RETURN ) = 0;
2695 }
2696
2697 /*
2698 ========================
2699 VM_cin_close
2700
2701 void cin_close(string name)
2702 ========================
2703 */
2704 void VM_cin_close( void )
2705 {
2706         const char *name;
2707
2708         VM_SAFEPARMCOUNT( 1, VM_cin_close );
2709
2710         name = PRVM_G_STRING( OFS_PARM0 );
2711         VM_CheckEmptyString( name );
2712
2713         CL_CloseVideo( CL_GetVideo( name ) );
2714 }
2715
2716 /*
2717 ========================
2718 VM_cin_setstate
2719 void cin_setstate(string name, float type)
2720 ========================
2721 */
2722 void VM_cin_setstate( void )
2723 {
2724         const char *name;
2725         clvideostate_t  state;
2726         clvideo_t               *video;
2727
2728         VM_SAFEPARMCOUNT( 2, VM_cin_netstate );
2729
2730         name = PRVM_G_STRING( OFS_PARM0 );
2731         VM_CheckEmptyString( name );
2732
2733         state = PRVM_G_FLOAT( OFS_PARM1 );
2734
2735         video = CL_GetVideo( name );
2736         if( video && state > CLVIDEO_UNUSED && state < CLVIDEO_STATECOUNT )
2737                 CL_SetVideoState( video, state );
2738 }
2739
2740 /*
2741 ========================
2742 VM_cin_getstate
2743
2744 float cin_getstate(string name)
2745 ========================
2746 */
2747 void VM_cin_getstate( void )
2748 {
2749         const char *name;
2750         clvideo_t               *video;
2751
2752         VM_SAFEPARMCOUNT( 1, VM_cin_getstate );
2753
2754         name = PRVM_G_STRING( OFS_PARM0 );
2755         VM_CheckEmptyString( name );
2756
2757         video = CL_GetVideo( name );
2758         if( video )
2759                 PRVM_G_FLOAT( OFS_RETURN ) = (int)video->state;
2760         else
2761                 PRVM_G_FLOAT( OFS_RETURN ) = 0;
2762 }
2763
2764 /*
2765 ========================
2766 VM_cin_restart
2767
2768 void cin_restart(string name)
2769 ========================
2770 */
2771 void VM_cin_restart( void )
2772 {
2773         const char *name;
2774         clvideo_t               *video;
2775
2776         VM_SAFEPARMCOUNT( 1, VM_cin_restart );
2777
2778         name = PRVM_G_STRING( OFS_PARM0 );
2779         VM_CheckEmptyString( name );
2780
2781         video = CL_GetVideo( name );
2782         if( video )
2783                 CL_RestartVideo( video );
2784 }
2785
2786 ////////////////////////////////////////
2787 // AltString functions
2788 ////////////////////////////////////////
2789
2790 /*
2791 ========================
2792 VM_altstr_count
2793
2794 float altstr_count(string)
2795 ========================
2796 */
2797 void VM_altstr_count( void )
2798 {
2799         const char *altstr, *pos;
2800         int     count;
2801
2802         VM_SAFEPARMCOUNT( 1, VM_altstr_count );
2803
2804         altstr = PRVM_G_STRING( OFS_PARM0 );
2805         //VM_CheckEmptyString( altstr );
2806
2807         for( count = 0, pos = altstr ; *pos ; pos++ ) {
2808                 if( *pos == '\\' ) {
2809                         if( !*++pos ) {
2810                                 break; 
2811                         }
2812                 } else if( *pos == '\'' ) {
2813                         count++;
2814                 }
2815         }
2816
2817         PRVM_G_FLOAT( OFS_RETURN ) = (float) (count / 2);
2818 }
2819
2820 /*
2821 ========================
2822 VM_altstr_prepare
2823
2824 string altstr_prepare(string)
2825 ========================
2826 */
2827 void VM_altstr_prepare( void )
2828 {
2829         char *outstr, *out;
2830         const char *instr, *in;
2831         int size;
2832
2833         VM_SAFEPARMCOUNT( 1, VM_altstr_prepare );
2834
2835         instr = PRVM_G_STRING( OFS_PARM0 );
2836         //VM_CheckEmptyString( instr );
2837         outstr = VM_GetTempString();
2838
2839         for( out = outstr, in = instr, size = VM_STRINGTEMP_LENGTH - 1 ; size && *in ; size--, in++, out++ )
2840                 if( *in == '\'' ) {
2841                         *out++ = '\\';
2842                         *out = '\'';
2843                         size--;
2844                 } else
2845                         *out = *in;
2846         *out = 0;
2847
2848         PRVM_G_INT( OFS_RETURN ) = PRVM_SetEngineString( outstr );
2849 }
2850
2851 /*
2852 ========================
2853 VM_altstr_get
2854
2855 string altstr_get(string, float)
2856 ========================
2857 */
2858 void VM_altstr_get( void )
2859 {
2860         const char *altstr, *pos;
2861         char *outstr, *out;
2862         int count, size;
2863
2864         VM_SAFEPARMCOUNT( 2, VM_altstr_get );
2865
2866         altstr = PRVM_G_STRING( OFS_PARM0 );
2867         //VM_CheckEmptyString( altstr );
2868
2869         count = PRVM_G_FLOAT( OFS_PARM1 );
2870         count = count * 2 + 1;
2871
2872         for( pos = altstr ; *pos && count ; pos++ )
2873                 if( *pos == '\\' ) {
2874                         if( !*++pos )
2875                                 break;
2876                 } else if( *pos == '\'' )
2877                         count--;
2878
2879         if( !*pos ) {
2880                 PRVM_G_INT( OFS_RETURN ) = PRVM_SetEngineString( NULL );
2881                 return;
2882         }
2883
2884     outstr = VM_GetTempString();
2885         for( out = outstr, size = VM_STRINGTEMP_LENGTH - 1 ; size && *pos ; size--, pos++, out++ )
2886                 if( *pos == '\\' ) {
2887                         if( !*++pos )
2888                                 break;
2889                         *out = *pos;
2890                         size--;
2891                 } else if( *pos == '\'' )
2892                         break;
2893                 else
2894                         *out = *pos;
2895
2896         *out = 0;
2897         PRVM_G_INT( OFS_RETURN ) = PRVM_SetEngineString( outstr );
2898 }
2899
2900 /*
2901 ========================
2902 VM_altstr_set
2903
2904 string altstr_set(string altstr, float num, string set)
2905 ========================
2906 */
2907 void VM_altstr_set( void )
2908 {
2909     int num;
2910         const char *altstr, *str;
2911         const char *in;
2912         char *outstr, *out;
2913
2914         VM_SAFEPARMCOUNT( 3, VM_altstr_set );
2915
2916         altstr = PRVM_G_STRING( OFS_PARM0 );
2917         //VM_CheckEmptyString( altstr );
2918
2919         num = PRVM_G_FLOAT( OFS_PARM1 );
2920
2921         str = PRVM_G_STRING( OFS_PARM2 );
2922         //VM_CheckEmptyString( str );
2923
2924         outstr = out = VM_GetTempString();
2925         for( num = num * 2 + 1, in = altstr; *in && num; *out++ = *in++ )
2926                 if( *in == '\\' ) {
2927                         if( !*++in ) {
2928                                 break;
2929                         }
2930                 } else if( *in == '\'' ) {
2931                         num--;
2932                 }
2933
2934         if( !in ) {
2935                 PRVM_G_INT( OFS_RETURN ) = PRVM_SetEngineString( altstr );
2936                 return;
2937         }
2938         // copy set in
2939         for( ; *str; *out++ = *str++ );
2940         // now jump over the old content
2941         for( ; *in ; in++ )
2942                 if( *in == '\'' || (*in == '\\' && !*++in) )
2943                         break;
2944
2945         if( !in ) {
2946                 PRVM_G_INT( OFS_RETURN ) = PRVM_SetEngineString( NULL );
2947                 return;
2948         }
2949
2950         strcpy( out, in );
2951         PRVM_G_INT( OFS_RETURN ) = PRVM_SetEngineString( outstr );
2952 }
2953
2954 /*
2955 ========================
2956 VM_altstr_ins
2957 insert after num
2958 string  altstr_ins(string altstr, float num, string set)
2959 ========================
2960 */
2961 void VM_altstr_ins(void)
2962 {
2963         int num;
2964         const char *setstr;
2965         const char *set;
2966         const char *instr;
2967         const char *in;
2968         char *outstr;
2969         char *out;
2970
2971         in = instr = PRVM_G_STRING( OFS_PARM0 );
2972         num = PRVM_G_FLOAT( OFS_PARM1 );
2973         set = setstr = PRVM_G_STRING( OFS_PARM2 );
2974
2975         out = outstr = VM_GetTempString();
2976         for( num = num * 2 + 2 ; *in && num > 0 ; *out++ = *in++ )
2977                 if( *in == '\\' ) {
2978                         if( !*++in ) {
2979                                 break;
2980                         }
2981                 } else if( *in == '\'' ) {
2982                         num--;
2983                 }
2984
2985         *out++ = '\'';
2986         for( ; *set ; *out++ = *set++ );
2987         *out++ = '\'';
2988
2989         strcpy( out, in );
2990         PRVM_G_INT( OFS_RETURN ) = PRVM_SetEngineString( outstr );
2991 }
2992
2993 void VM_Cmd_Init(void)
2994 {
2995         // only init the stuff for the current prog
2996         VM_Files_Init();
2997         VM_Search_Init();
2998 }
2999
3000 void VM_Cmd_Reset(void)
3001 {
3002         CL_PurgeOwner( MENUOWNER );
3003         VM_Search_Reset();
3004         VM_Files_CloseAll();
3005 }
3006
3007