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