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