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