]> icculus.org git repositories - divverent/darkplaces.git/blob - prvm_cmds.c
use only the latest ping time, not averaged
[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         PRVM_G_INT(OFS_RETURN) = PRVM_AllocString(strlen(in) + 1, &out);
1690         strcpy(out, in);
1691 }
1692
1693 /*
1694 =========
1695 VM_strunzone
1696
1697 strunzone(string s)
1698 =========
1699 */
1700 //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!!!)
1701 void VM_strunzone(void)
1702 {
1703         VM_SAFEPARMCOUNT(1,VM_strunzone);
1704         PRVM_FreeString(PRVM_G_INT(OFS_PARM0));
1705 }
1706
1707 /*
1708 =========
1709 VM_command (used by client and menu)
1710
1711 clientcommand(float client, string s) (for client and menu)
1712 =========
1713 */
1714 //void(entity e, string s) clientcommand = #440; // executes a command string as if it came from the specified client
1715 //this function originally written by KrimZon, made shorter by LordHavoc
1716 void VM_clcommand (void)
1717 {
1718         client_t *temp_client;
1719         int i;
1720
1721         VM_SAFEPARMCOUNT(2,VM_clcommand);
1722
1723         i = PRVM_G_FLOAT(OFS_PARM0);
1724         if (!sv.active  || i < 0 || i >= svs.maxclients || !svs.clients[i].active)
1725         {
1726                 Con_Printf("VM_clientcommand: %s: invalid client/server is not active !\n", PRVM_NAME);
1727                 return;
1728         }
1729
1730         temp_client = host_client;
1731         host_client = svs.clients + i;
1732         Cmd_ExecuteString (PRVM_G_STRING(OFS_PARM1), src_client);
1733         host_client = temp_client;
1734 }
1735
1736
1737 /*
1738 =========
1739 VM_tokenize
1740
1741 float tokenize(string s)
1742 =========
1743 */
1744 //float(string s) tokenize = #441; // takes apart a string into individal words (access them with argv), returns how many
1745 //this function originally written by KrimZon, made shorter by LordHavoc
1746 //20040203: rewritten by LordHavoc (no longer uses allocations)
1747 int num_tokens = 0;
1748 char *tokens[256], tokenbuf[4096];
1749 void VM_tokenize (void)
1750 {
1751         int pos;
1752         const char *p;
1753
1754         VM_SAFEPARMCOUNT(1,VM_tokenize);
1755
1756         p = PRVM_G_STRING(OFS_PARM0);
1757
1758         num_tokens = 0;
1759         pos = 0;
1760         while(COM_ParseToken(&p, false))
1761         {
1762                 if (num_tokens >= (int)(sizeof(tokens)/sizeof(tokens[0])))
1763                         break;
1764                 if (pos + strlen(com_token) + 1 > sizeof(tokenbuf))
1765                         break;
1766                 tokens[num_tokens++] = tokenbuf + pos;
1767                 strcpy(tokenbuf + pos, com_token);
1768                 pos += strlen(com_token) + 1;
1769         }
1770
1771         PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
1772 }
1773
1774 //string(float n) argv = #442; // returns a word from the tokenized string (returns nothing for an invalid index)
1775 //this function originally written by KrimZon, made shorter by LordHavoc
1776 void VM_argv (void)
1777 {
1778         int token_num;
1779
1780         VM_SAFEPARMCOUNT(1,VM_argv);
1781
1782         token_num = PRVM_G_FLOAT(OFS_PARM0);
1783
1784         if (token_num >= 0 && token_num < num_tokens)
1785                 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(tokens[token_num]);
1786         else
1787                 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(NULL);
1788 }
1789
1790 /*
1791 //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)
1792 void PF_setattachment (void)
1793 {
1794         prvm_edict_t *e = PRVM_G_EDICT(OFS_PARM0);
1795         prvm_edict_t *tagentity = PRVM_G_EDICT(OFS_PARM1);
1796         char *tagname = PRVM_G_STRING(OFS_PARM2);
1797         prvm_eval_t *v;
1798         int i, modelindex;
1799         model_t *model;
1800
1801         if (tagentity == NULL)
1802                 tagentity = prog->edicts;
1803
1804         v = PRVM_GETEDICTFIELDVALUE(e, eval_tag_entity);
1805         if (v)
1806                 fields.server->edict = PRVM_EDICT_TO_PROG(tagentity);
1807
1808         v = PRVM_GETEDICTFIELDVALUE(e, eval_tag_index);
1809         if (v)
1810                 fields.server->_float = 0;
1811         if (tagentity != NULL && tagentity != prog->edicts && tagname && tagname[0])
1812         {
1813                 modelindex = (int)tagentity->fields.server->modelindex;
1814                 if (modelindex >= 0 && modelindex < MAX_MODELS)
1815                 {
1816                         model = sv.models[modelindex];
1817                         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)
1818                                 for (i = 0;i < model->data_overridetagnamesforskin[(unsigned int)tagentity->fields.server->skin].num_overridetagnames;i++)
1819                                         if (!strcmp(tagname, model->data_overridetagnamesforskin[(unsigned int)tagentity->fields.server->skin].data_overridetagnames[i].name))
1820                                                 fields.server->_float = i + 1;
1821                         // FIXME: use a model function to get tag info (need to handle skeletal)
1822                         if (fields.server->_float == 0 && model->num_tags)
1823                                 for (i = 0;i < model->num_tags;i++)
1824                                         if (!strcmp(tagname, model->data_tags[i].name))
1825                                                 fields.server->_float = i + 1;
1826                         if (fields.server->_float == 0)
1827                                 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);
1828                 }
1829                 else
1830                         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));
1831         }
1832 }*/
1833
1834 /*
1835 =========
1836 VM_isserver
1837
1838 float   isserver()
1839 =========
1840 */
1841 void VM_isserver(void)
1842 {
1843         VM_SAFEPARMCOUNT(0,VM_serverstate);
1844
1845         PRVM_G_FLOAT(OFS_RETURN) = sv.active;
1846 }
1847
1848 /*
1849 =========
1850 VM_clientcount
1851
1852 float   clientcount()
1853 =========
1854 */
1855 void VM_clientcount(void)
1856 {
1857         VM_SAFEPARMCOUNT(0,VM_clientcount);
1858
1859         PRVM_G_FLOAT(OFS_RETURN) = svs.maxclients;
1860 }
1861
1862 /*
1863 =========
1864 VM_clientstate
1865
1866 float   clientstate()
1867 =========
1868 */
1869 void VM_clientstate(void)
1870 {
1871         VM_SAFEPARMCOUNT(0,VM_clientstate);
1872
1873         PRVM_G_FLOAT(OFS_RETURN) = cls.state;
1874 }
1875
1876 /*
1877 =========
1878 VM_getostype
1879
1880 float   getostype(void)
1881 =========
1882 */ // not used at the moment -> not included in the common list
1883 void VM_getostype(void)
1884 {
1885         VM_SAFEPARMCOUNT(0,VM_getostype);
1886
1887         /*
1888         OS_WINDOWS
1889         OS_LINUX
1890         OS_MAC - not supported
1891         */
1892
1893 #ifdef _WIN32
1894         PRVM_G_FLOAT(OFS_RETURN) = 0;
1895 #elif defined _MAC
1896         PRVM_G_FLOAT(OFS_RETURN) = 2;
1897 #else
1898         PRVM_G_FLOAT(OFS_RETURN) = 1;
1899 #endif
1900 }
1901
1902 /*
1903 =========
1904 VM_getmousepos
1905
1906 vector  getmousepos()
1907 =========
1908 */
1909 void VM_getmousepos(void)
1910 {
1911
1912         VM_SAFEPARMCOUNT(0,VM_getmousepos);
1913
1914         PRVM_G_VECTOR(OFS_RETURN)[0] = in_mouse_x * vid_conwidth.integer / vid.width;
1915         PRVM_G_VECTOR(OFS_RETURN)[1] = in_mouse_y * vid_conheight.integer / vid.height;
1916         PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
1917 }
1918
1919 /*
1920 =========
1921 VM_gettime
1922
1923 float   gettime(void)
1924 =========
1925 */
1926 void VM_gettime(void)
1927 {
1928         VM_SAFEPARMCOUNT(0,VM_gettime);
1929
1930         PRVM_G_FLOAT(OFS_RETURN) = (float) *prog->time;
1931 }
1932
1933 /*
1934 =========
1935 VM_loadfromdata
1936
1937 loadfromdata(string data)
1938 =========
1939 */
1940 void VM_loadfromdata(void)
1941 {
1942         VM_SAFEPARMCOUNT(1,VM_loadentsfromfile);
1943
1944         PRVM_ED_LoadFromFile(PRVM_G_STRING(OFS_PARM0));
1945 }
1946
1947 /*
1948 ========================
1949 VM_parseentitydata
1950
1951 parseentitydata(entity ent, string data)
1952 ========================
1953 */
1954 void VM_parseentitydata(void)
1955 {
1956         prvm_edict_t *ent;
1957         const char *data;
1958
1959         VM_SAFEPARMCOUNT(2, VM_parseentitydata);
1960
1961     // get edict and test it
1962         ent = PRVM_G_EDICT(OFS_PARM0);
1963         if (ent->priv.required->free)
1964                 PRVM_ERROR ("VM_parseentitydata: %s: Can only set already spawned entities (entity %i is free)!\n", PRVM_NAME, PRVM_NUM_FOR_EDICT(ent));
1965
1966         data = PRVM_G_STRING(OFS_PARM1);
1967
1968     // parse the opening brace
1969         if (!COM_ParseToken(&data, false) || com_token[0] != '{' )
1970                 PRVM_ERROR ("VM_parseentitydata: %s: Couldn't parse entity data:\n%s\n", PRVM_NAME, data );
1971
1972         PRVM_ED_ParseEdict (data, ent);
1973 }
1974
1975 /*
1976 =========
1977 VM_loadfromfile
1978
1979 loadfromfile(string file)
1980 =========
1981 */
1982 void VM_loadfromfile(void)
1983 {
1984         const char *filename;
1985         qbyte *data;
1986
1987         VM_SAFEPARMCOUNT(1,VM_loadfromfile);
1988
1989         filename = PRVM_G_STRING(OFS_PARM0);
1990         // .. is parent directory on many platforms
1991         // / is parent directory on Amiga
1992         // : is root of drive on Amiga (also used as a directory separator on Mac, but / works there too, so that's a bad idea)
1993         // \ is a windows-ism (so it's naughty to use it, / works on all platforms)
1994         if ((filename[0] == '.' && filename[1] == '.') || filename[0] == '/' || strrchr(filename, ':') || strrchr(filename, '\\'))
1995         {
1996                 Con_Printf("VM_loadfromfile: %s dangerous or non-portable filename \"%s\" not allowed. (contains : or \\ or begins with .. or /)\n", PRVM_NAME, filename);
1997                 PRVM_G_FLOAT(OFS_RETURN) = -4;
1998                 return;
1999         }
2000
2001         // not conform with VM_fopen
2002         data = FS_LoadFile(filename, tempmempool, false);
2003         if (data == NULL)
2004                 PRVM_G_FLOAT(OFS_RETURN) = -1;
2005
2006         PRVM_ED_LoadFromFile(data);
2007
2008         if(data)
2009                 Mem_Free(data);
2010 }
2011
2012
2013 /*
2014 =========
2015 VM_modulo
2016
2017 float   mod(float val, float m)
2018 =========
2019 */
2020 void VM_modulo(void)
2021 {
2022         int val, m;
2023         VM_SAFEPARMCOUNT(2,VM_module);
2024
2025         val = (int) PRVM_G_FLOAT(OFS_PARM0);
2026         m       = (int) PRVM_G_FLOAT(OFS_PARM1);
2027
2028         PRVM_G_FLOAT(OFS_RETURN) = (float) (val % m);
2029 }
2030
2031 void VM_Search_Init(void)
2032 {
2033         memset(VM_SEARCHLIST,0,sizeof(fssearch_t*[MAX_VMSEARCHES]));
2034 }
2035
2036 void VM_Search_Reset(void)
2037 {
2038         int i;
2039         // reset the fssearch list
2040         for(i = 0; i < MAX_VMSEARCHES; i++)
2041                 if(VM_SEARCHLIST[i])
2042                         FS_FreeSearch(VM_SEARCHLIST[i]);
2043         memset(VM_SEARCHLIST,0,sizeof(fssearch_t*[MAX_VMSEARCHES]));
2044 }
2045
2046 /*
2047 =========
2048 VM_search_begin
2049
2050 float search_begin(string pattern, float caseinsensitive, float quiet)
2051 =========
2052 */
2053 void VM_search_begin(void)
2054 {
2055         int handle;
2056         const char *pattern;
2057         int caseinsens, quiet;
2058
2059         VM_SAFEPARMCOUNT(3, VM_search_begin);
2060
2061         pattern = PRVM_G_STRING(OFS_PARM0);
2062
2063         VM_CheckEmptyString(pattern);
2064
2065         caseinsens = PRVM_G_FLOAT(OFS_PARM1);
2066         quiet = PRVM_G_FLOAT(OFS_PARM2);
2067
2068         for(handle = 0; handle < MAX_VMSEARCHES; handle++)
2069                 if(!VM_SEARCHLIST[handle])
2070                         break;
2071
2072         if(handle >= MAX_VMSEARCHES)
2073         {
2074                 Con_Printf("VM_search_begin: %s ran out of search handles (%i)\n", PRVM_NAME, MAX_VMSEARCHES);
2075                 PRVM_G_FLOAT(OFS_RETURN) = -2;
2076                 return;
2077         }
2078
2079         if(!(VM_SEARCHLIST[handle] = FS_Search(pattern,caseinsens, quiet)))
2080                 PRVM_G_FLOAT(OFS_RETURN) = -1;
2081         else
2082                 PRVM_G_FLOAT(OFS_RETURN) = handle;
2083 }
2084
2085 /*
2086 =========
2087 VM_search_end
2088
2089 void    search_end(float handle)
2090 =========
2091 */
2092 void VM_search_end(void)
2093 {
2094         int handle;
2095         VM_SAFEPARMCOUNT(1, VM_search_end);
2096
2097         handle = PRVM_G_FLOAT(OFS_PARM0);
2098
2099         if(handle < 0 || handle >= MAX_VMSEARCHES)
2100         {
2101                 Con_Printf("VM_search_end: invalid handle %i used in %s\n", handle, PRVM_NAME);
2102                 return;
2103         }
2104         if(VM_SEARCHLIST[handle] == NULL)
2105         {
2106                 Con_Printf("VM_search_end: no such handle %i in %s\n", handle, PRVM_NAME);
2107                 return;
2108         }
2109
2110         FS_FreeSearch(VM_SEARCHLIST[handle]);
2111         VM_SEARCHLIST[handle] = NULL;
2112 }
2113
2114 /*
2115 =========
2116 VM_search_getsize
2117
2118 float   search_getsize(float handle)
2119 =========
2120 */
2121 void VM_search_getsize(void)
2122 {
2123         int handle;
2124         VM_SAFEPARMCOUNT(1, VM_M_search_getsize);
2125
2126         handle = PRVM_G_FLOAT(OFS_PARM0);
2127
2128         if(handle < 0 || handle >= MAX_VMSEARCHES)
2129         {
2130                 Con_Printf("VM_search_getsize: invalid handle %i used in %s\n", handle, PRVM_NAME);
2131                 return;
2132         }
2133         if(VM_SEARCHLIST[handle] == NULL)
2134         {
2135                 Con_Printf("VM_search_getsize: no such handle %i in %s\n", handle, PRVM_NAME);
2136                 return;
2137         }
2138
2139         PRVM_G_FLOAT(OFS_RETURN) = VM_SEARCHLIST[handle]->numfilenames;
2140 }
2141
2142 /*
2143 =========
2144 VM_search_getfilename
2145
2146 string  search_getfilename(float handle, float num)
2147 =========
2148 */
2149 void VM_search_getfilename(void)
2150 {
2151         int handle, filenum;
2152         char *tmp;
2153         VM_SAFEPARMCOUNT(2, VM_search_getfilename);
2154
2155         handle = PRVM_G_FLOAT(OFS_PARM0);
2156         filenum = PRVM_G_FLOAT(OFS_PARM1);
2157
2158         if(handle < 0 || handle >= MAX_VMSEARCHES)
2159         {
2160                 Con_Printf("VM_search_getfilename: invalid handle %i used in %s\n", handle, PRVM_NAME);
2161                 return;
2162         }
2163         if(VM_SEARCHLIST[handle] == NULL)
2164         {
2165                 Con_Printf("VM_search_getfilename: no such handle %i in %s\n", handle, PRVM_NAME);
2166                 return;
2167         }
2168         if(filenum < 0 || filenum >= VM_SEARCHLIST[handle]->numfilenames)
2169         {
2170                 Con_Printf("VM_search_getfilename: invalid filenum %i in %s\n", filenum, PRVM_NAME);
2171                 return;
2172         }
2173
2174         tmp = VM_GetTempString();
2175         strcpy(tmp, VM_SEARCHLIST[handle]->filenames[filenum]);
2176
2177         PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(tmp);
2178 }
2179
2180 /*
2181 =========
2182 VM_chr
2183
2184 string  chr(float ascii)
2185 =========
2186 */
2187 void VM_chr(void)
2188 {
2189         char *tmp;
2190         VM_SAFEPARMCOUNT(1, VM_chr);
2191
2192         tmp = VM_GetTempString();
2193         tmp[0] = (unsigned char) PRVM_G_FLOAT(OFS_PARM0);
2194         tmp[1] = 0;
2195
2196         PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(tmp);
2197 }
2198
2199 //=============================================================================
2200 // Draw builtins (client & menu)
2201
2202 /*
2203 =========
2204 VM_iscachedpic
2205
2206 float   iscachedpic(string pic)
2207 =========
2208 */
2209 void VM_iscachedpic(void)
2210 {
2211         VM_SAFEPARMCOUNT(1,VM_iscachedpic);
2212
2213         // drawq hasnt such a function, thus always return true
2214         PRVM_G_FLOAT(OFS_RETURN) = false;
2215 }
2216
2217 /*
2218 =========
2219 VM_precache_pic
2220
2221 string  precache_pic(string pic)
2222 =========
2223 */
2224 void VM_precache_pic(void)
2225 {
2226         const char      *s;
2227
2228         VM_SAFEPARMCOUNT(1, VM_precache_pic);
2229
2230         s = PRVM_G_STRING(OFS_PARM0);
2231         PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
2232
2233         if(!s)
2234                 PRVM_ERROR ("VM_precache_pic: %s: NULL\n", PRVM_NAME);
2235
2236         VM_CheckEmptyString (s);
2237
2238         // AK Draw_CachePic is supposed to always return a valid pointer
2239         if( Draw_CachePic(s, false)->tex == r_texture_notexture )
2240                 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(NULL);
2241 }
2242
2243 /*
2244 =========
2245 VM_freepic
2246
2247 freepic(string s)
2248 =========
2249 */
2250 void VM_freepic(void)
2251 {
2252         const char *s;
2253
2254         VM_SAFEPARMCOUNT(1,VM_freepic);
2255
2256         s = PRVM_G_STRING(OFS_PARM0);
2257
2258         if(!s)
2259                 PRVM_ERROR ("VM_freepic: %s: NULL\n");
2260
2261         VM_CheckEmptyString (s);
2262
2263         Draw_FreePic(s);
2264 }
2265
2266 /*
2267 =========
2268 VM_drawcharacter
2269
2270 float   drawcharacter(vector position, float character, vector scale, vector rgb, float alpha, float flag)
2271 =========
2272 */
2273 void VM_drawcharacter(void)
2274 {
2275         float *pos,*scale,*rgb;
2276         char   character;
2277         int flag;
2278         VM_SAFEPARMCOUNT(6,VM_drawcharacter);
2279
2280         character = (char) PRVM_G_FLOAT(OFS_PARM1);
2281         if(character == 0)
2282         {
2283                 Con_Printf("VM_drawcharacter: %s passed null character !\n",PRVM_NAME);
2284                 PRVM_G_FLOAT(OFS_RETURN) = -1;
2285                 return;
2286         }
2287
2288         pos = PRVM_G_VECTOR(OFS_PARM0);
2289         scale = PRVM_G_VECTOR(OFS_PARM2);
2290         rgb = PRVM_G_VECTOR(OFS_PARM3);
2291         flag = (int)PRVM_G_FLOAT(OFS_PARM5);
2292
2293         if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2294         {
2295                 Con_Printf("VM_drawcharacter: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2296                 PRVM_G_FLOAT(OFS_RETURN) = -2;
2297                 return;
2298         }
2299
2300         if(pos[2] || scale[2])
2301                 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")));
2302
2303         if(!scale[0] || !scale[1])
2304         {
2305                 Con_Printf("VM_drawcharacter: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
2306                 PRVM_G_FLOAT(OFS_RETURN) = -3;
2307                 return;
2308         }
2309
2310         DrawQ_String (pos[0], pos[1], &character, 1, scale[0], scale[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM4), flag);
2311         PRVM_G_FLOAT(OFS_RETURN) = 1;
2312 }
2313
2314 /*
2315 =========
2316 VM_drawstring
2317
2318 float   drawstring(vector position, string text, vector scale, vector rgb, float alpha, float flag)
2319 =========
2320 */
2321 void VM_drawstring(void)
2322 {
2323         float *pos,*scale,*rgb;
2324         const char  *string;
2325         int flag;
2326         VM_SAFEPARMCOUNT(6,VM_drawstring);
2327
2328         string = PRVM_G_STRING(OFS_PARM1);
2329         if(!string)
2330         {
2331                 Con_Printf("VM_drawstring: %s passed null string !\n",PRVM_NAME);
2332                 PRVM_G_FLOAT(OFS_RETURN) = -1;
2333                 return;
2334         }
2335
2336         //VM_CheckEmptyString(string); Why should it be checked - perhaps the menu wants to support the precolored letters, too?
2337
2338         pos = PRVM_G_VECTOR(OFS_PARM0);
2339         scale = PRVM_G_VECTOR(OFS_PARM2);
2340         rgb = PRVM_G_VECTOR(OFS_PARM3);
2341         flag = (int)PRVM_G_FLOAT(OFS_PARM5);
2342
2343         if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2344         {
2345                 Con_Printf("VM_drawstring: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2346                 PRVM_G_FLOAT(OFS_RETURN) = -2;
2347                 return;
2348         }
2349
2350         if(!scale[0] || !scale[1])
2351         {
2352                 Con_Printf("VM_drawstring: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
2353                 PRVM_G_FLOAT(OFS_RETURN) = -3;
2354                 return;
2355         }
2356
2357         if(pos[2] || scale[2])
2358                 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")));
2359
2360         DrawQ_String (pos[0], pos[1], string, 0, scale[0], scale[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM4), flag);
2361         PRVM_G_FLOAT(OFS_RETURN) = 1;
2362 }
2363 /*
2364 =========
2365 VM_drawpic
2366
2367 float   drawpic(vector position, string pic, vector size, vector rgb, float alpha, float flag)
2368 =========
2369 */
2370 void VM_drawpic(void)
2371 {
2372         const char *pic;
2373         float *size, *pos, *rgb;
2374         int flag;
2375
2376         VM_SAFEPARMCOUNT(6,VM_drawpic);
2377
2378         pic = PRVM_G_STRING(OFS_PARM1);
2379
2380         if(!pic)
2381         {
2382                 Con_Printf("VM_drawpic: %s passed null picture name !\n", PRVM_NAME);
2383                 PRVM_G_FLOAT(OFS_RETURN) = -1;
2384                 return;
2385         }
2386
2387         VM_CheckEmptyString (pic);
2388
2389         // is pic cached ? no function yet for that
2390         if(!1)
2391         {
2392                 Con_Printf("VM_drawpic: %s: %s not cached !\n", PRVM_NAME, pic);
2393                 PRVM_G_FLOAT(OFS_RETURN) = -4;
2394                 return;
2395         }
2396
2397         pos = PRVM_G_VECTOR(OFS_PARM0);
2398         size = PRVM_G_VECTOR(OFS_PARM2);
2399         rgb = PRVM_G_VECTOR(OFS_PARM3);
2400         flag = (int) PRVM_G_FLOAT(OFS_PARM5);
2401
2402         if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2403         {
2404                 Con_Printf("VM_drawstring: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2405                 PRVM_G_FLOAT(OFS_RETURN) = -2;
2406                 return;
2407         }
2408
2409         if(pos[2] || size[2])
2410                 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")));
2411
2412         DrawQ_Pic(pos[0], pos[1], pic, size[0], size[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM4), flag);
2413         PRVM_G_FLOAT(OFS_RETURN) = 1;
2414 }
2415
2416 /*
2417 =========
2418 VM_drawfill
2419
2420 float drawfill(vector position, vector size, vector rgb, float alpha, float flag)
2421 =========
2422 */
2423 void VM_drawfill(void)
2424 {
2425         float *size, *pos, *rgb;
2426         int flag;
2427
2428         VM_SAFEPARMCOUNT(5,VM_drawfill);
2429
2430
2431         pos = PRVM_G_VECTOR(OFS_PARM0);
2432         size = PRVM_G_VECTOR(OFS_PARM1);
2433         rgb = PRVM_G_VECTOR(OFS_PARM2);
2434         flag = (int) PRVM_G_FLOAT(OFS_PARM4);
2435
2436         if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2437         {
2438                 Con_Printf("VM_drawstring: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2439                 PRVM_G_FLOAT(OFS_RETURN) = -2;
2440                 return;
2441         }
2442
2443         if(pos[2] || size[2])
2444                 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")));
2445
2446         DrawQ_Pic(pos[0], pos[1], 0, size[0], size[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM3), flag);
2447         PRVM_G_FLOAT(OFS_RETURN) = 1;
2448 }
2449
2450 /*
2451 =========
2452 VM_drawsetcliparea
2453
2454 drawsetcliparea(float x, float y, float width, float height)
2455 =========
2456 */
2457 void VM_drawsetcliparea(void)
2458 {
2459         float x,y,w,h;
2460         VM_SAFEPARMCOUNT(4,VM_drawsetcliparea);
2461
2462         x = bound(0, PRVM_G_FLOAT(OFS_PARM0), vid_conwidth.integer);
2463         y = bound(0, PRVM_G_FLOAT(OFS_PARM1), vid_conheight.integer);
2464         w = bound(0, PRVM_G_FLOAT(OFS_PARM2) + PRVM_G_FLOAT(OFS_PARM0) - x, (vid_conwidth.integer  - x));
2465         h = bound(0, PRVM_G_FLOAT(OFS_PARM3) + PRVM_G_FLOAT(OFS_PARM1) - y, (vid_conheight.integer - y));
2466
2467         DrawQ_SetClipArea(x, y, w, h);
2468 }
2469
2470 /*
2471 =========
2472 VM_drawresetcliparea
2473
2474 drawresetcliparea()
2475 =========
2476 */
2477 void VM_drawresetcliparea(void)
2478 {
2479         VM_SAFEPARMCOUNT(0,VM_drawresetcliparea);
2480
2481         DrawQ_ResetClipArea();
2482 }
2483
2484 /*
2485 =========
2486 VM_getimagesize
2487
2488 vector  getimagesize(string pic)
2489 =========
2490 */
2491 void VM_getimagesize(void)
2492 {
2493         const char *p;
2494         cachepic_t *pic;
2495
2496         VM_SAFEPARMCOUNT(1,VM_getimagesize);
2497
2498         p = PRVM_G_STRING(OFS_PARM0);
2499
2500         if(!p)
2501                 PRVM_ERROR("VM_getimagepos: %s passed null picture name !\n", PRVM_NAME);
2502
2503         VM_CheckEmptyString (p);
2504
2505         pic = Draw_CachePic (p, false);
2506
2507         PRVM_G_VECTOR(OFS_RETURN)[0] = pic->width;
2508         PRVM_G_VECTOR(OFS_RETURN)[1] = pic->height;
2509         PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
2510 }
2511
2512 // CL_Video interface functions
2513
2514 /*
2515 ========================
2516 VM_cin_open
2517
2518 float cin_open(string file, string name)
2519 ========================
2520 */
2521 void VM_cin_open( void )
2522 {
2523         const char *file;
2524         const char *name;
2525
2526         VM_SAFEPARMCOUNT( 2, VM_cin_open );
2527
2528         file = PRVM_G_STRING( OFS_PARM0 );
2529         name = PRVM_G_STRING( OFS_PARM1 );
2530
2531         VM_CheckEmptyString( file );
2532     VM_CheckEmptyString( name );
2533
2534         if( CL_OpenVideo( file, name, MENUOWNER ) )
2535                 PRVM_G_FLOAT( OFS_RETURN ) = 1;
2536         else
2537                 PRVM_G_FLOAT( OFS_RETURN ) = 0;
2538 }
2539
2540 /*
2541 ========================
2542 VM_cin_close
2543
2544 void cin_close(string name)
2545 ========================
2546 */
2547 void VM_cin_close( void )
2548 {
2549         const char *name;
2550
2551         VM_SAFEPARMCOUNT( 1, VM_cin_close );
2552
2553         name = PRVM_G_STRING( OFS_PARM0 );
2554         VM_CheckEmptyString( name );
2555
2556         CL_CloseVideo( CL_GetVideo( name ) );
2557 }
2558
2559 /*
2560 ========================
2561 VM_cin_setstate
2562 void cin_setstate(string name, float type)
2563 ========================
2564 */
2565 void VM_cin_setstate( void )
2566 {
2567         const char *name;
2568         clvideostate_t  state;
2569         clvideo_t               *video;
2570
2571         VM_SAFEPARMCOUNT( 2, VM_cin_netstate );
2572
2573         name = PRVM_G_STRING( OFS_PARM0 );
2574         VM_CheckEmptyString( name );
2575
2576         state = PRVM_G_FLOAT( OFS_PARM1 );
2577
2578         video = CL_GetVideo( name );
2579         if( video && state > CLVIDEO_UNUSED && state < CLVIDEO_STATECOUNT )
2580                 CL_SetVideoState( video, state );
2581 }
2582
2583 /*
2584 ========================
2585 VM_cin_getstate
2586
2587 float cin_getstate(string name)
2588 ========================
2589 */
2590 void VM_cin_getstate( void )
2591 {
2592         const char *name;
2593         clvideo_t               *video;
2594
2595         VM_SAFEPARMCOUNT( 1, VM_cin_getstate );
2596
2597         name = PRVM_G_STRING( OFS_PARM0 );
2598         VM_CheckEmptyString( name );
2599
2600         video = CL_GetVideo( name );
2601         if( video )
2602                 PRVM_G_FLOAT( OFS_RETURN ) = (int)video->state;
2603         else
2604                 PRVM_G_FLOAT( OFS_RETURN ) = 0;
2605 }
2606
2607 /*
2608 ========================
2609 VM_cin_restart
2610
2611 void cin_restart(string name)
2612 ========================
2613 */
2614 void VM_cin_restart( void )
2615 {
2616         const char *name;
2617         clvideo_t               *video;
2618
2619         VM_SAFEPARMCOUNT( 1, VM_cin_restart );
2620
2621         name = PRVM_G_STRING( OFS_PARM0 );
2622         VM_CheckEmptyString( name );
2623
2624         video = CL_GetVideo( name );
2625         if( video )
2626                 CL_RestartVideo( video );
2627 }
2628
2629 ////////////////////////////////////////
2630 // AltString functions
2631 ////////////////////////////////////////
2632
2633 /*
2634 ========================
2635 VM_altstr_count
2636
2637 float altstr_count(string)
2638 ========================
2639 */
2640 void VM_altstr_count( void )
2641 {
2642         const char *altstr, *pos;
2643         int     count;
2644
2645         VM_SAFEPARMCOUNT( 1, VM_altstr_count );
2646
2647         altstr = PRVM_G_STRING( OFS_PARM0 );
2648         //VM_CheckEmptyString( altstr );
2649
2650         for( count = 0, pos = altstr ; *pos ; pos++ ) {
2651                 if( *pos == '\\' ) {
2652                         if( !*++pos ) {
2653                                 break;
2654                         }
2655                 } else if( *pos == '\'' ) {
2656                         count++;
2657                 }
2658         }
2659
2660         PRVM_G_FLOAT( OFS_RETURN ) = (float) (count / 2);
2661 }
2662
2663 /*
2664 ========================
2665 VM_altstr_prepare
2666
2667 string altstr_prepare(string)
2668 ========================
2669 */
2670 void VM_altstr_prepare( void )
2671 {
2672         char *outstr, *out;
2673         const char *instr, *in;
2674         int size;
2675
2676         VM_SAFEPARMCOUNT( 1, VM_altstr_prepare );
2677
2678         instr = PRVM_G_STRING( OFS_PARM0 );
2679         //VM_CheckEmptyString( instr );
2680         outstr = VM_GetTempString();
2681
2682         for( out = outstr, in = instr, size = VM_STRINGTEMP_LENGTH - 1 ; size && *in ; size--, in++, out++ )
2683                 if( *in == '\'' ) {
2684                         *out++ = '\\';
2685                         *out = '\'';
2686                         size--;
2687                 } else
2688                         *out = *in;
2689         *out = 0;
2690
2691         PRVM_G_INT( OFS_RETURN ) = PRVM_SetEngineString( outstr );
2692 }
2693
2694 /*
2695 ========================
2696 VM_altstr_get
2697
2698 string altstr_get(string, float)
2699 ========================
2700 */
2701 void VM_altstr_get( void )
2702 {
2703         const char *altstr, *pos;
2704         char *outstr, *out;
2705         int count, size;
2706
2707         VM_SAFEPARMCOUNT( 2, VM_altstr_get );
2708
2709         altstr = PRVM_G_STRING( OFS_PARM0 );
2710         //VM_CheckEmptyString( altstr );
2711
2712         count = PRVM_G_FLOAT( OFS_PARM1 );
2713         count = count * 2 + 1;
2714
2715         for( pos = altstr ; *pos && count ; pos++ )
2716                 if( *pos == '\\' ) {
2717                         if( !*++pos )
2718                                 break;
2719                 } else if( *pos == '\'' )
2720                         count--;
2721
2722         if( !*pos ) {
2723                 PRVM_G_INT( OFS_RETURN ) = PRVM_SetEngineString( NULL );
2724                 return;
2725         }
2726
2727     outstr = VM_GetTempString();
2728         for( out = outstr, size = VM_STRINGTEMP_LENGTH - 1 ; size && *pos ; size--, pos++, out++ )
2729                 if( *pos == '\\' ) {
2730                         if( !*++pos )
2731                                 break;
2732                         *out = *pos;
2733                         size--;
2734                 } else if( *pos == '\'' )
2735                         break;
2736                 else
2737                         *out = *pos;
2738
2739         *out = 0;
2740         PRVM_G_INT( OFS_RETURN ) = PRVM_SetEngineString( outstr );
2741 }
2742
2743 /*
2744 ========================
2745 VM_altstr_set
2746
2747 string altstr_set(string altstr, float num, string set)
2748 ========================
2749 */
2750 void VM_altstr_set( void )
2751 {
2752     int num;
2753         const char *altstr, *str;
2754         const char *in;
2755         char *outstr, *out;
2756
2757         VM_SAFEPARMCOUNT( 3, VM_altstr_set );
2758
2759         altstr = PRVM_G_STRING( OFS_PARM0 );
2760         //VM_CheckEmptyString( altstr );
2761
2762         num = PRVM_G_FLOAT( OFS_PARM1 );
2763
2764         str = PRVM_G_STRING( OFS_PARM2 );
2765         //VM_CheckEmptyString( str );
2766
2767         outstr = out = VM_GetTempString();
2768         for( num = num * 2 + 1, in = altstr; *in && num; *out++ = *in++ )
2769                 if( *in == '\\' ) {
2770                         if( !*++in ) {
2771                                 break;
2772                         }
2773                 } else if( *in == '\'' ) {
2774                         num--;
2775                 }
2776
2777         if( !in ) {
2778                 PRVM_G_INT( OFS_RETURN ) = PRVM_SetEngineString( altstr );
2779                 return;
2780         }
2781         // copy set in
2782         for( ; *str; *out++ = *str++ );
2783         // now jump over the old content
2784         for( ; *in ; in++ )
2785                 if( *in == '\'' || (*in == '\\' && !*++in) )
2786                         break;
2787
2788         if( !in ) {
2789                 PRVM_G_INT( OFS_RETURN ) = PRVM_SetEngineString( NULL );
2790                 return;
2791         }
2792
2793         strcpy( out, in );
2794         PRVM_G_INT( OFS_RETURN ) = PRVM_SetEngineString( outstr );
2795 }
2796
2797 /*
2798 ========================
2799 VM_altstr_ins
2800 insert after num
2801 string  altstr_ins(string altstr, float num, string set)
2802 ========================
2803 */
2804 void VM_altstr_ins(void)
2805 {
2806         int num;
2807         const char *setstr;
2808         const char *set;
2809         const char *instr;
2810         const char *in;
2811         char *outstr;
2812         char *out;
2813
2814         in = instr = PRVM_G_STRING( OFS_PARM0 );
2815         num = PRVM_G_FLOAT( OFS_PARM1 );
2816         set = setstr = PRVM_G_STRING( OFS_PARM2 );
2817
2818         out = outstr = VM_GetTempString();
2819         for( num = num * 2 + 2 ; *in && num > 0 ; *out++ = *in++ )
2820                 if( *in == '\\' ) {
2821                         if( !*++in ) {
2822                                 break;
2823                         }
2824                 } else if( *in == '\'' ) {
2825                         num--;
2826                 }
2827
2828         *out++ = '\'';
2829         for( ; *set ; *out++ = *set++ );
2830         *out++ = '\'';
2831
2832         strcpy( out, in );
2833         PRVM_G_INT( OFS_RETURN ) = PRVM_SetEngineString( outstr );
2834 }
2835
2836 void VM_Cmd_Init(void)
2837 {
2838         // only init the stuff for the current prog
2839         VM_Files_Init();
2840         VM_Search_Init();
2841 }
2842
2843 void VM_Cmd_Reset(void)
2844 {
2845         CL_PurgeOwner( MENUOWNER );
2846         VM_Search_Reset();
2847         VM_Files_CloseAll();
2848 }
2849
2850