added DP_QC_TOKENIZEBYSEPARATOR extension
[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         size_t nCnt;
1758         int nPos;
1759         int nFillPos;
1760         int bFinished;
1761                 nPos = 0;
1762                 nFillPos = 0;
1763                 nCnt = 0;
1764                 bFinished = 0;
1765
1766         // Prepare Strings
1767         VM_SAFEPARMCOUNT(1,VM_strdecolorize);
1768         szString = PRVM_G_STRING(OFS_PARM0);
1769
1770         while(!bFinished)
1771         { // Traverse through String
1772                 if( szString[nPos] == '\n' || szString[nPos] == '\r' || szString[nPos] <= 0)
1773                 { // String End Found
1774                         szNewString[nFillPos++] = szString[nPos];
1775                         bFinished = 1;
1776                 }
1777                 else
1778                 if( szString[nPos] == STRING_COLOR_TAG)
1779                 { // Color Code Located
1780                         if( szString[nPos + 1] == STRING_COLOR_TAG)
1781                         { // Valid Characters to Include
1782                                 szNewString[nFillPos++] = szString[nPos];
1783                                 nPos = nPos + 1;
1784                                 szNewString[nFillPos++] = szString[nPos];
1785                         }
1786                         else
1787                         if( szString[nPos + 1] >= '0' && szString[nPos + 1] <= '9' )
1788                         { // Color Code Found; Increment Position
1789                                 nPos = nPos + 1;
1790                         }
1791                         else
1792                         { // Unknown Color Code; Include
1793                                 szNewString[nFillPos++] = szString[nPos];
1794                                 nPos = nPos + 1;
1795                         }
1796                 }
1797                 else
1798                         // Include Character
1799                         szNewString[nFillPos++] = szString[nPos];
1800
1801                         // Increment Position
1802                         nPos = nPos + 1;
1803         }
1804
1805         PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(szNewString);
1806 }
1807
1808 // DRESK - String Length (not counting color codes)
1809 /*
1810 =========
1811 VM_strlennocol
1812
1813 float   strlennocol(string s)
1814 =========
1815 */
1816 // float(string s) strlennocol = #471; // returns how many characters are in a string not including color codes
1817 // For example, ^2Dresk returns a length of 5
1818 void VM_strlennocol(void)
1819 {
1820         const char *szString;
1821         size_t nCnt;
1822         int nPos;
1823         int bFinished;
1824                 nPos = 0;
1825                 nCnt = 0;
1826                 bFinished = 0;
1827
1828         VM_SAFEPARMCOUNT(1,VM_strlennocol);
1829
1830         szString = PRVM_G_STRING(OFS_PARM0);
1831
1832         while(!bFinished)
1833         { // Count Characters
1834                 // SV_BroadcastPrintf("Position '%d'; Character '%c'; Length '%d'\n", nPos, szString[nPos], nCnt);
1835
1836                 if( szString[nPos] == '\n' || szString[nPos] == '\r' || szString[nPos] <= 0)
1837                 { // String End Found
1838                         // SV_BroadcastPrintf("Found End of String at '%d'\n", nPos);
1839                         bFinished = 1;
1840                 }
1841                 else
1842                 if( szString[nPos] == STRING_COLOR_TAG)
1843                 { // Color Code Located
1844                         if( szString[nPos + 1] == STRING_COLOR_TAG)
1845                         { // Increment Length; Skip Color Code
1846                                 nCnt = nCnt + 1;
1847                                 nPos = nPos + 1;
1848                         }
1849                         else
1850                         if( szString[nPos + 1] >= '0' && szString[nPos + 1] <= '9' )
1851                         { // Color Code Found; Increment Position
1852                                 // SV_BroadcastPrintf("Found Color Codes at '%d'\n", nPos);
1853                                 nPos = nPos + 1;
1854                         }
1855                         else
1856                         { // Unknown Color Code; Increment Length!
1857                                 nPos = nPos + 1;
1858                                 nCnt = nCnt + 1;
1859                         }
1860                 }
1861                 else
1862                         // Increment String Length
1863                         nCnt = nCnt + 1;
1864
1865                 // Increment Position
1866                 nPos = nPos + 1;
1867         }
1868         PRVM_G_FLOAT(OFS_RETURN) = nCnt;
1869 }
1870
1871 /*
1872 =========
1873 VM_strcat
1874
1875 string strcat(string,string,...[string])
1876 =========
1877 */
1878 //string(string s1, string s2) strcat = #115;
1879 // concatenates two strings (for example "abc", "def" would return "abcdef")
1880 // and returns as a tempstring
1881 void VM_strcat(void)
1882 {
1883         char s[VM_STRINGTEMP_LENGTH];
1884         VM_SAFEPARMCOUNTRANGE(1, 8, VM_strcat);
1885
1886         VM_VarString(0, s, sizeof(s));
1887         PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(s);
1888 }
1889
1890 /*
1891 =========
1892 VM_substring
1893
1894 string  substring(string s, float start, float length)
1895 =========
1896 */
1897 // string(string s, float start, float length) substring = #116;
1898 // returns a section of a string as a tempstring
1899 void VM_substring(void)
1900 {
1901         int i, start, length;
1902         const char *s;
1903         char string[VM_STRINGTEMP_LENGTH];
1904
1905         VM_SAFEPARMCOUNT(3,VM_substring);
1906
1907         s = PRVM_G_STRING(OFS_PARM0);
1908         start = (int)PRVM_G_FLOAT(OFS_PARM1);
1909         length = (int)PRVM_G_FLOAT(OFS_PARM2);
1910         for (i = 0;i < start && *s;i++, s++);
1911         for (i = 0;i < (int)sizeof(string) - 1 && *s && i < length;i++, s++)
1912                 string[i] = *s;
1913         string[i] = 0;
1914         PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string);
1915 }
1916
1917 /*
1918 =========
1919 VM_stov
1920
1921 vector  stov(string s)
1922 =========
1923 */
1924 //vector(string s) stov = #117; // returns vector value from a string
1925 void VM_stov(void)
1926 {
1927         char string[VM_STRINGTEMP_LENGTH];
1928
1929         VM_SAFEPARMCOUNT(1,VM_stov);
1930
1931         VM_VarString(0, string, sizeof(string));
1932         Math_atov(string, PRVM_G_VECTOR(OFS_RETURN));
1933 }
1934
1935 /*
1936 =========
1937 VM_strzone
1938
1939 string  strzone(string s)
1940 =========
1941 */
1942 //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)
1943 void VM_strzone(void)
1944 {
1945         char *out;
1946         char string[VM_STRINGTEMP_LENGTH];
1947         size_t alloclen;
1948
1949         VM_SAFEPARMCOUNT(1,VM_strzone);
1950
1951         VM_VarString(0, string, sizeof(string));
1952         alloclen = strlen(string) + 1;
1953         PRVM_G_INT(OFS_RETURN) = PRVM_AllocString(alloclen, &out);
1954         memcpy(out, string, alloclen);
1955 }
1956
1957 /*
1958 =========
1959 VM_strunzone
1960
1961 strunzone(string s)
1962 =========
1963 */
1964 //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!!!)
1965 void VM_strunzone(void)
1966 {
1967         VM_SAFEPARMCOUNT(1,VM_strunzone);
1968         PRVM_FreeString(PRVM_G_INT(OFS_PARM0));
1969 }
1970
1971 /*
1972 =========
1973 VM_command (used by client and menu)
1974
1975 clientcommand(float client, string s) (for client and menu)
1976 =========
1977 */
1978 //void(entity e, string s) clientcommand = #440; // executes a command string as if it came from the specified client
1979 //this function originally written by KrimZon, made shorter by LordHavoc
1980 void VM_clcommand (void)
1981 {
1982         client_t *temp_client;
1983         int i;
1984
1985         VM_SAFEPARMCOUNT(2,VM_clcommand);
1986
1987         i = (int)PRVM_G_FLOAT(OFS_PARM0);
1988         if (!sv.active  || i < 0 || i >= svs.maxclients || !svs.clients[i].active)
1989         {
1990                 VM_Warning("VM_clientcommand: %s: invalid client/server is not active !\n", PRVM_NAME);
1991                 return;
1992         }
1993
1994         temp_client = host_client;
1995         host_client = svs.clients + i;
1996         Cmd_ExecuteString (PRVM_G_STRING(OFS_PARM1), src_client);
1997         host_client = temp_client;
1998 }
1999
2000
2001 /*
2002 =========
2003 VM_tokenize
2004
2005 float tokenize(string s)
2006 =========
2007 */
2008 //float(string s) tokenize = #441; // takes apart a string into individal words (access them with argv), returns how many
2009 //this function originally written by KrimZon, made shorter by LordHavoc
2010 //20040203: rewritten by LordHavoc (no longer uses allocations)
2011 int num_tokens = 0;
2012 int tokens[256];
2013 void VM_tokenize (void)
2014 {
2015         const char *p;
2016
2017         VM_SAFEPARMCOUNT(1,VM_tokenize);
2018
2019         p = PRVM_G_STRING(OFS_PARM0);
2020
2021         num_tokens = 0;
2022         while(COM_ParseToken(&p, false))
2023         {
2024                 if (num_tokens >= (int)(sizeof(tokens)/sizeof(tokens[0])))
2025                         break;
2026                 tokens[num_tokens++] = PRVM_SetTempString(com_token);
2027         }
2028
2029         PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
2030 }
2031
2032 /*
2033 =========
2034 VM_tokenizebyseparator
2035
2036 float tokenizebyseparator(string s, string separator1, ...)
2037 =========
2038 */
2039 //float(string s, string separator1, ...) tokenizebyseparator = #479; // takes apart a string into individal words (access them with argv), returns how many
2040 //this function returns the token preceding each instance of a separator (of
2041 //which there can be multiple), and the text following the last separator
2042 //useful for parsing certain kinds of data like IP addresses
2043 //example:
2044 //numnumbers = tokenizebyseparator("10.1.2.3", ".");
2045 //returns 4 and the tokens "10" "1" "2" "3".
2046 void VM_tokenizebyseparator (void)
2047 {
2048         int j, k;
2049         int numseparators;
2050         int separatorlen[7];
2051         const char *separators[7];
2052         const char *p;
2053         char tokentext[MAX_INPUTLINE];
2054
2055         VM_SAFEPARMCOUNTRANGE(2, 8,VM_tokenizebyseparator);
2056
2057         p = PRVM_G_STRING(OFS_PARM0);
2058
2059         numseparators = 0;;
2060         for (j = 1;j < prog->argc;j++)
2061         {
2062                 // skip any blank separator strings
2063                 if (!PRVM_G_STRING(OFS_PARM0 + j)[0])
2064                         continue;
2065                 separators[numseparators] = PRVM_G_STRING(OFS_PARM0 + j);
2066                 separatorlen[numseparators] = strlen(separators[numseparators]);
2067                 numseparators++;
2068         }
2069
2070         num_tokens = 0;
2071         for (num_tokens = 0;num_tokens < (int)(sizeof(tokens)/sizeof(tokens[0]));num_tokens++)
2072         {
2073                 while (*p)
2074                 {
2075                         for (k = 0;k < numseparators;k++)
2076                         {
2077                                 if (!strncmp(p, separators[k], separatorlen[k]))
2078                                 {
2079                                         p += separatorlen[k];
2080                                         break;
2081                                 }
2082                         }
2083                         if (k < numseparators)
2084                                 break;
2085                         if (j < (int)sizeof(tokentext[MAX_INPUTLINE]-1))
2086                                 tokentext[j++] = *p;
2087                         p++;
2088                 }
2089                 tokentext[j] = 0;
2090                 tokens[num_tokens] = PRVM_SetTempString(tokentext);
2091                 if (!*p)
2092                         break;
2093         }
2094
2095         PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
2096 }
2097
2098 //string(float n) argv = #442; // returns a word from the tokenized string (returns nothing for an invalid index)
2099 //this function originally written by KrimZon, made shorter by LordHavoc
2100 void VM_argv (void)
2101 {
2102         int token_num;
2103
2104         VM_SAFEPARMCOUNT(1,VM_argv);
2105
2106         token_num = (int)PRVM_G_FLOAT(OFS_PARM0);
2107
2108         if (token_num >= 0 && token_num < num_tokens)
2109                 PRVM_G_INT(OFS_RETURN) = tokens[token_num];
2110         else
2111                 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
2112 }
2113
2114 /*
2115 =========
2116 VM_isserver
2117
2118 float   isserver()
2119 =========
2120 */
2121 void VM_isserver(void)
2122 {
2123         VM_SAFEPARMCOUNT(0,VM_serverstate);
2124
2125         PRVM_G_FLOAT(OFS_RETURN) = sv.active && (svs.maxclients > 1 || cls.state == ca_dedicated);
2126 }
2127
2128 /*
2129 =========
2130 VM_clientcount
2131
2132 float   clientcount()
2133 =========
2134 */
2135 void VM_clientcount(void)
2136 {
2137         VM_SAFEPARMCOUNT(0,VM_clientcount);
2138
2139         PRVM_G_FLOAT(OFS_RETURN) = svs.maxclients;
2140 }
2141
2142 /*
2143 =========
2144 VM_clientstate
2145
2146 float   clientstate()
2147 =========
2148 */
2149 void VM_clientstate(void)
2150 {
2151         VM_SAFEPARMCOUNT(0,VM_clientstate);
2152
2153         PRVM_G_FLOAT(OFS_RETURN) = cls.state;
2154 }
2155
2156 /*
2157 =========
2158 VM_getostype
2159
2160 float   getostype(void)
2161 =========
2162 */ // not used at the moment -> not included in the common list
2163 void VM_getostype(void)
2164 {
2165         VM_SAFEPARMCOUNT(0,VM_getostype);
2166
2167         /*
2168         OS_WINDOWS
2169         OS_LINUX
2170         OS_MAC - not supported
2171         */
2172
2173 #ifdef WIN32
2174         PRVM_G_FLOAT(OFS_RETURN) = 0;
2175 #elif defined(MACOSX)
2176         PRVM_G_FLOAT(OFS_RETURN) = 2;
2177 #else
2178         PRVM_G_FLOAT(OFS_RETURN) = 1;
2179 #endif
2180 }
2181
2182 /*
2183 =========
2184 VM_getmousepos
2185
2186 vector  getmousepos()
2187 =========
2188 */
2189 void VM_getmousepos(void)
2190 {
2191
2192         VM_SAFEPARMCOUNT(0,VM_getmousepos);
2193
2194         PRVM_G_VECTOR(OFS_RETURN)[0] = in_mouse_x * vid_conwidth.integer / vid.width;
2195         PRVM_G_VECTOR(OFS_RETURN)[1] = in_mouse_y * vid_conheight.integer / vid.height;
2196         PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
2197 }
2198
2199 /*
2200 =========
2201 VM_gettime
2202
2203 float   gettime(void)
2204 =========
2205 */
2206 void VM_gettime(void)
2207 {
2208         VM_SAFEPARMCOUNT(0,VM_gettime);
2209
2210         PRVM_G_FLOAT(OFS_RETURN) = (float) realtime;
2211 }
2212
2213 /*
2214 =========
2215 VM_loadfromdata
2216
2217 loadfromdata(string data)
2218 =========
2219 */
2220 void VM_loadfromdata(void)
2221 {
2222         VM_SAFEPARMCOUNT(1,VM_loadentsfromfile);
2223
2224         PRVM_ED_LoadFromFile(PRVM_G_STRING(OFS_PARM0));
2225 }
2226
2227 /*
2228 ========================
2229 VM_parseentitydata
2230
2231 parseentitydata(entity ent, string data)
2232 ========================
2233 */
2234 void VM_parseentitydata(void)
2235 {
2236         prvm_edict_t *ent;
2237         const char *data;
2238
2239         VM_SAFEPARMCOUNT(2, VM_parseentitydata);
2240
2241     // get edict and test it
2242         ent = PRVM_G_EDICT(OFS_PARM0);
2243         if (ent->priv.required->free)
2244                 PRVM_ERROR ("VM_parseentitydata: %s: Can only set already spawned entities (entity %i is free)!", PRVM_NAME, PRVM_NUM_FOR_EDICT(ent));
2245
2246         data = PRVM_G_STRING(OFS_PARM1);
2247
2248     // parse the opening brace
2249         if (!COM_ParseTokenConsole(&data) || com_token[0] != '{' )
2250                 PRVM_ERROR ("VM_parseentitydata: %s: Couldn't parse entity data:\n%s", PRVM_NAME, data );
2251
2252         PRVM_ED_ParseEdict (data, ent);
2253 }
2254
2255 /*
2256 =========
2257 VM_loadfromfile
2258
2259 loadfromfile(string file)
2260 =========
2261 */
2262 void VM_loadfromfile(void)
2263 {
2264         const char *filename;
2265         char *data;
2266
2267         VM_SAFEPARMCOUNT(1,VM_loadfromfile);
2268
2269         filename = PRVM_G_STRING(OFS_PARM0);
2270         if (FS_CheckNastyPath(filename, false))
2271         {
2272                 PRVM_G_FLOAT(OFS_RETURN) = -4;
2273                 VM_Warning("VM_loadfromfile: %s dangerous or non-portable filename \"%s\" not allowed. (contains : or \\ or begins with .. or /)\n", PRVM_NAME, filename);
2274                 return;
2275         }
2276
2277         // not conform with VM_fopen
2278         data = (char *)FS_LoadFile(filename, tempmempool, false, NULL);
2279         if (data == NULL)
2280                 PRVM_G_FLOAT(OFS_RETURN) = -1;
2281
2282         PRVM_ED_LoadFromFile(data);
2283
2284         if(data)
2285                 Mem_Free(data);
2286 }
2287
2288
2289 /*
2290 =========
2291 VM_modulo
2292
2293 float   mod(float val, float m)
2294 =========
2295 */
2296 void VM_modulo(void)
2297 {
2298         int val, m;
2299         VM_SAFEPARMCOUNT(2,VM_module);
2300
2301         val = (int) PRVM_G_FLOAT(OFS_PARM0);
2302         m       = (int) PRVM_G_FLOAT(OFS_PARM1);
2303
2304         PRVM_G_FLOAT(OFS_RETURN) = (float) (val % m);
2305 }
2306
2307 void VM_Search_Init(void)
2308 {
2309         int i;
2310         for (i = 0;i < PRVM_MAX_OPENSEARCHES;i++)
2311                 prog->opensearches[i] = NULL;
2312 }
2313
2314 void VM_Search_Reset(void)
2315 {
2316         int i;
2317         // reset the fssearch list
2318         for(i = 0; i < PRVM_MAX_OPENSEARCHES; i++)
2319         {
2320                 if(prog->opensearches[i])
2321                         FS_FreeSearch(prog->opensearches[i]);
2322                 prog->opensearches[i] = NULL;
2323         }
2324 }
2325
2326 /*
2327 =========
2328 VM_search_begin
2329
2330 float search_begin(string pattern, float caseinsensitive, float quiet)
2331 =========
2332 */
2333 void VM_search_begin(void)
2334 {
2335         int handle;
2336         const char *pattern;
2337         int caseinsens, quiet;
2338
2339         VM_SAFEPARMCOUNT(3, VM_search_begin);
2340
2341         pattern = PRVM_G_STRING(OFS_PARM0);
2342
2343         VM_CheckEmptyString(pattern);
2344
2345         caseinsens = (int)PRVM_G_FLOAT(OFS_PARM1);
2346         quiet = (int)PRVM_G_FLOAT(OFS_PARM2);
2347
2348         for(handle = 0; handle < PRVM_MAX_OPENSEARCHES; handle++)
2349                 if(!prog->opensearches[handle])
2350                         break;
2351
2352         if(handle >= PRVM_MAX_OPENSEARCHES)
2353         {
2354                 PRVM_G_FLOAT(OFS_RETURN) = -2;
2355                 VM_Warning("VM_search_begin: %s ran out of search handles (%i)\n", PRVM_NAME, PRVM_MAX_OPENSEARCHES);
2356                 return;
2357         }
2358
2359         if(!(prog->opensearches[handle] = FS_Search(pattern,caseinsens, quiet)))
2360                 PRVM_G_FLOAT(OFS_RETURN) = -1;
2361         else
2362                 PRVM_G_FLOAT(OFS_RETURN) = handle;
2363 }
2364
2365 /*
2366 =========
2367 VM_search_end
2368
2369 void    search_end(float handle)
2370 =========
2371 */
2372 void VM_search_end(void)
2373 {
2374         int handle;
2375         VM_SAFEPARMCOUNT(1, VM_search_end);
2376
2377         handle = (int)PRVM_G_FLOAT(OFS_PARM0);
2378
2379         if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
2380         {
2381                 VM_Warning("VM_search_end: invalid handle %i used in %s\n", handle, PRVM_NAME);
2382                 return;
2383         }
2384         if(prog->opensearches[handle] == NULL)
2385         {
2386                 VM_Warning("VM_search_end: no such handle %i in %s\n", handle, PRVM_NAME);
2387                 return;
2388         }
2389
2390         FS_FreeSearch(prog->opensearches[handle]);
2391         prog->opensearches[handle] = NULL;
2392 }
2393
2394 /*
2395 =========
2396 VM_search_getsize
2397
2398 float   search_getsize(float handle)
2399 =========
2400 */
2401 void VM_search_getsize(void)
2402 {
2403         int handle;
2404         VM_SAFEPARMCOUNT(1, VM_M_search_getsize);
2405
2406         handle = (int)PRVM_G_FLOAT(OFS_PARM0);
2407
2408         if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
2409         {
2410                 VM_Warning("VM_search_getsize: invalid handle %i used in %s\n", handle, PRVM_NAME);
2411                 return;
2412         }
2413         if(prog->opensearches[handle] == NULL)
2414         {
2415                 VM_Warning("VM_search_getsize: no such handle %i in %s\n", handle, PRVM_NAME);
2416                 return;
2417         }
2418
2419         PRVM_G_FLOAT(OFS_RETURN) = prog->opensearches[handle]->numfilenames;
2420 }
2421
2422 /*
2423 =========
2424 VM_search_getfilename
2425
2426 string  search_getfilename(float handle, float num)
2427 =========
2428 */
2429 void VM_search_getfilename(void)
2430 {
2431         int handle, filenum;
2432         VM_SAFEPARMCOUNT(2, VM_search_getfilename);
2433
2434         handle = (int)PRVM_G_FLOAT(OFS_PARM0);
2435         filenum = (int)PRVM_G_FLOAT(OFS_PARM1);
2436
2437         if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
2438         {
2439                 VM_Warning("VM_search_getfilename: invalid handle %i used in %s\n", handle, PRVM_NAME);
2440                 return;
2441         }
2442         if(prog->opensearches[handle] == NULL)
2443         {
2444                 VM_Warning("VM_search_getfilename: no such handle %i in %s\n", handle, PRVM_NAME);
2445                 return;
2446         }
2447         if(filenum < 0 || filenum >= prog->opensearches[handle]->numfilenames)
2448         {
2449                 VM_Warning("VM_search_getfilename: invalid filenum %i in %s\n", filenum, PRVM_NAME);
2450                 return;
2451         }
2452
2453         PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog->opensearches[handle]->filenames[filenum]);
2454 }
2455
2456 /*
2457 =========
2458 VM_chr
2459
2460 string  chr(float ascii)
2461 =========
2462 */
2463 void VM_chr(void)
2464 {
2465         char tmp[2];
2466         VM_SAFEPARMCOUNT(1, VM_chr);
2467
2468         tmp[0] = (unsigned char) PRVM_G_FLOAT(OFS_PARM0);
2469         tmp[1] = 0;
2470
2471         PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(tmp);
2472 }
2473
2474 //=============================================================================
2475 // Draw builtins (client & menu)
2476
2477 /*
2478 =========
2479 VM_iscachedpic
2480
2481 float   iscachedpic(string pic)
2482 =========
2483 */
2484 void VM_iscachedpic(void)
2485 {
2486         VM_SAFEPARMCOUNT(1,VM_iscachedpic);
2487
2488         // drawq hasnt such a function, thus always return true
2489         PRVM_G_FLOAT(OFS_RETURN) = false;
2490 }
2491
2492 /*
2493 =========
2494 VM_precache_pic
2495
2496 string  precache_pic(string pic)
2497 =========
2498 */
2499 void VM_precache_pic(void)
2500 {
2501         const char      *s;
2502
2503         VM_SAFEPARMCOUNT(1, VM_precache_pic);
2504
2505         s = PRVM_G_STRING(OFS_PARM0);
2506         PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
2507         VM_CheckEmptyString (s);
2508
2509         // AK Draw_CachePic is supposed to always return a valid pointer
2510         if( Draw_CachePic(s, false)->tex == r_texture_notexture )
2511                 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
2512 }
2513
2514 /*
2515 =========
2516 VM_freepic
2517
2518 freepic(string s)
2519 =========
2520 */
2521 void VM_freepic(void)
2522 {
2523         const char *s;
2524
2525         VM_SAFEPARMCOUNT(1,VM_freepic);
2526
2527         s = PRVM_G_STRING(OFS_PARM0);
2528         VM_CheckEmptyString (s);
2529
2530         Draw_FreePic(s);
2531 }
2532
2533 /*
2534 =========
2535 VM_drawcharacter
2536
2537 float   drawcharacter(vector position, float character, vector scale, vector rgb, float alpha, float flag)
2538 =========
2539 */
2540 void VM_drawcharacter(void)
2541 {
2542         float *pos,*scale,*rgb;
2543         char   character;
2544         int flag;
2545         VM_SAFEPARMCOUNT(6,VM_drawcharacter);
2546
2547         character = (char) PRVM_G_FLOAT(OFS_PARM1);
2548         if(character == 0)
2549         {
2550                 PRVM_G_FLOAT(OFS_RETURN) = -1;
2551                 VM_Warning("VM_drawcharacter: %s passed null character !\n",PRVM_NAME);
2552                 return;
2553         }
2554
2555         pos = PRVM_G_VECTOR(OFS_PARM0);
2556         scale = PRVM_G_VECTOR(OFS_PARM2);
2557         rgb = PRVM_G_VECTOR(OFS_PARM3);
2558         flag = (int)PRVM_G_FLOAT(OFS_PARM5);
2559
2560         if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2561         {
2562                 PRVM_G_FLOAT(OFS_RETURN) = -2;
2563                 VM_Warning("VM_drawcharacter: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2564                 return;
2565         }
2566
2567         if(pos[2] || scale[2])
2568                 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")));
2569
2570         if(!scale[0] || !scale[1])
2571         {
2572                 PRVM_G_FLOAT(OFS_RETURN) = -3;
2573                 VM_Warning("VM_drawcharacter: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
2574                 return;
2575         }
2576
2577         DrawQ_String (pos[0], pos[1], &character, 1, scale[0], scale[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM4), flag);
2578         PRVM_G_FLOAT(OFS_RETURN) = 1;
2579 }
2580
2581 /*
2582 =========
2583 VM_drawstring
2584
2585 float   drawstring(vector position, string text, vector scale, vector rgb, float alpha, float flag)
2586 =========
2587 */
2588 void VM_drawstring(void)
2589 {
2590         float *pos,*scale,*rgb;
2591         const char  *string;
2592         int flag;
2593         VM_SAFEPARMCOUNT(6,VM_drawstring);
2594
2595         string = PRVM_G_STRING(OFS_PARM1);
2596         pos = PRVM_G_VECTOR(OFS_PARM0);
2597         scale = PRVM_G_VECTOR(OFS_PARM2);
2598         rgb = PRVM_G_VECTOR(OFS_PARM3);
2599         flag = (int)PRVM_G_FLOAT(OFS_PARM5);
2600
2601         if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2602         {
2603                 PRVM_G_FLOAT(OFS_RETURN) = -2;
2604                 VM_Warning("VM_drawstring: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2605                 return;
2606         }
2607
2608         if(!scale[0] || !scale[1])
2609         {
2610                 PRVM_G_FLOAT(OFS_RETURN) = -3;
2611                 VM_Warning("VM_drawstring: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
2612                 return;
2613         }
2614
2615         if(pos[2] || scale[2])
2616                 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")));
2617
2618         DrawQ_String (pos[0], pos[1], string, 0, scale[0], scale[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM4), flag);
2619         PRVM_G_FLOAT(OFS_RETURN) = 1;
2620 }
2621 /*
2622 =========
2623 VM_drawpic
2624
2625 float   drawpic(vector position, string pic, vector size, vector rgb, float alpha, float flag)
2626 =========
2627 */
2628 void VM_drawpic(void)
2629 {
2630         const char *picname;
2631         float *size, *pos, *rgb;
2632         int flag;
2633
2634         VM_SAFEPARMCOUNT(6,VM_drawpic);
2635
2636         picname = PRVM_G_STRING(OFS_PARM1);
2637         VM_CheckEmptyString (picname);
2638
2639         // is pic cached ? no function yet for that
2640         if(!1)
2641         {
2642                 PRVM_G_FLOAT(OFS_RETURN) = -4;
2643                 VM_Warning("VM_drawpic: %s: %s not cached !\n", PRVM_NAME, picname);
2644                 return;
2645         }
2646
2647         pos = PRVM_G_VECTOR(OFS_PARM0);
2648         size = PRVM_G_VECTOR(OFS_PARM2);
2649         rgb = PRVM_G_VECTOR(OFS_PARM3);
2650         flag = (int) PRVM_G_FLOAT(OFS_PARM5);
2651
2652         if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2653         {
2654                 PRVM_G_FLOAT(OFS_RETURN) = -2;
2655                 VM_Warning("VM_drawpic: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2656                 return;
2657         }
2658
2659         if(pos[2] || size[2])
2660                 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")));
2661
2662         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);
2663         PRVM_G_FLOAT(OFS_RETURN) = 1;
2664 }
2665
2666 /*
2667 =========
2668 VM_drawfill
2669
2670 float drawfill(vector position, vector size, vector rgb, float alpha, float flag)
2671 =========
2672 */
2673 void VM_drawfill(void)
2674 {
2675         float *size, *pos, *rgb;
2676         int flag;
2677
2678         VM_SAFEPARMCOUNT(5,VM_drawfill);
2679
2680
2681         pos = PRVM_G_VECTOR(OFS_PARM0);
2682         size = PRVM_G_VECTOR(OFS_PARM1);
2683         rgb = PRVM_G_VECTOR(OFS_PARM2);
2684         flag = (int) PRVM_G_FLOAT(OFS_PARM4);
2685
2686         if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2687         {
2688                 PRVM_G_FLOAT(OFS_RETURN) = -2;
2689                 VM_Warning("VM_drawfill: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2690                 return;
2691         }
2692
2693         if(pos[2] || size[2])
2694                 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")));
2695
2696         DrawQ_Pic(pos[0], pos[1], NULL, size[0], size[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM3), flag);
2697         PRVM_G_FLOAT(OFS_RETURN) = 1;
2698 }
2699
2700 /*
2701 =========
2702 VM_drawsetcliparea
2703
2704 drawsetcliparea(float x, float y, float width, float height)
2705 =========
2706 */
2707 void VM_drawsetcliparea(void)
2708 {
2709         float x,y,w,h;
2710         VM_SAFEPARMCOUNT(4,VM_drawsetcliparea);
2711
2712         x = bound(0, PRVM_G_FLOAT(OFS_PARM0), vid_conwidth.integer);
2713         y = bound(0, PRVM_G_FLOAT(OFS_PARM1), vid_conheight.integer);
2714         w = bound(0, PRVM_G_FLOAT(OFS_PARM2) + PRVM_G_FLOAT(OFS_PARM0) - x, (vid_conwidth.integer  - x));
2715         h = bound(0, PRVM_G_FLOAT(OFS_PARM3) + PRVM_G_FLOAT(OFS_PARM1) - y, (vid_conheight.integer - y));
2716
2717         DrawQ_SetClipArea(x, y, w, h);
2718 }
2719
2720 /*
2721 =========
2722 VM_drawresetcliparea
2723
2724 drawresetcliparea()
2725 =========
2726 */
2727 void VM_drawresetcliparea(void)
2728 {
2729         VM_SAFEPARMCOUNT(0,VM_drawresetcliparea);
2730
2731         DrawQ_ResetClipArea();
2732 }
2733
2734 /*
2735 =========
2736 VM_getimagesize
2737
2738 vector  getimagesize(string pic)
2739 =========
2740 */
2741 void VM_getimagesize(void)
2742 {
2743         const char *p;
2744         cachepic_t *pic;
2745
2746         VM_SAFEPARMCOUNT(1,VM_getimagesize);
2747
2748         p = PRVM_G_STRING(OFS_PARM0);
2749         VM_CheckEmptyString (p);
2750
2751         pic = Draw_CachePic (p, false);
2752
2753         PRVM_G_VECTOR(OFS_RETURN)[0] = pic->width;
2754         PRVM_G_VECTOR(OFS_RETURN)[1] = pic->height;
2755         PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
2756 }
2757
2758 /*
2759 =========
2760 VM_keynumtostring
2761
2762 string keynumtostring(float keynum)
2763 =========
2764 */
2765 void VM_keynumtostring (void)
2766 {
2767         VM_SAFEPARMCOUNT(1, VM_keynumtostring);
2768
2769         PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(Key_KeynumToString((int)PRVM_G_FLOAT(OFS_PARM0)));
2770 }
2771
2772 /*
2773 =========
2774 VM_stringtokeynum
2775
2776 float stringtokeynum(string key)
2777 =========
2778 */
2779 void VM_stringtokeynum (void)
2780 {
2781         VM_SAFEPARMCOUNT( 1, VM_keynumtostring );
2782
2783         PRVM_G_INT(OFS_RETURN) = Key_StringToKeynum(PRVM_G_STRING(OFS_PARM0));
2784 }
2785
2786 // CL_Video interface functions
2787
2788 /*
2789 ========================
2790 VM_cin_open
2791
2792 float cin_open(string file, string name)
2793 ========================
2794 */
2795 void VM_cin_open( void )
2796 {
2797         const char *file;
2798         const char *name;
2799
2800         VM_SAFEPARMCOUNT( 2, VM_cin_open );
2801
2802         file = PRVM_G_STRING( OFS_PARM0 );
2803         name = PRVM_G_STRING( OFS_PARM1 );
2804
2805         VM_CheckEmptyString( file );
2806     VM_CheckEmptyString( name );
2807
2808         if( CL_OpenVideo( file, name, MENUOWNER ) )
2809                 PRVM_G_FLOAT( OFS_RETURN ) = 1;
2810         else
2811                 PRVM_G_FLOAT( OFS_RETURN ) = 0;
2812 }
2813
2814 /*
2815 ========================
2816 VM_cin_close
2817
2818 void cin_close(string name)
2819 ========================
2820 */
2821 void VM_cin_close( void )
2822 {
2823         const char *name;
2824
2825         VM_SAFEPARMCOUNT( 1, VM_cin_close );
2826
2827         name = PRVM_G_STRING( OFS_PARM0 );
2828         VM_CheckEmptyString( name );
2829
2830         CL_CloseVideo( CL_GetVideoByName( name ) );
2831 }
2832
2833 /*
2834 ========================
2835 VM_cin_setstate
2836 void cin_setstate(string name, float type)
2837 ========================
2838 */
2839 void VM_cin_setstate( void )
2840 {
2841         const char *name;
2842         clvideostate_t  state;
2843         clvideo_t               *video;
2844
2845         VM_SAFEPARMCOUNT( 2, VM_cin_netstate );
2846
2847         name = PRVM_G_STRING( OFS_PARM0 );
2848         VM_CheckEmptyString( name );
2849
2850         state = (clvideostate_t)((int)PRVM_G_FLOAT( OFS_PARM1 ));
2851
2852         video = CL_GetVideoByName( name );
2853         if( video && state > CLVIDEO_UNUSED && state < CLVIDEO_STATECOUNT )
2854                 CL_SetVideoState( video, state );
2855 }
2856
2857 /*
2858 ========================
2859 VM_cin_getstate
2860
2861 float cin_getstate(string name)
2862 ========================
2863 */
2864 void VM_cin_getstate( void )
2865 {
2866         const char *name;
2867         clvideo_t               *video;
2868
2869         VM_SAFEPARMCOUNT( 1, VM_cin_getstate );
2870
2871         name = PRVM_G_STRING( OFS_PARM0 );
2872         VM_CheckEmptyString( name );
2873
2874         video = CL_GetVideoByName( name );
2875         if( video )
2876                 PRVM_G_FLOAT( OFS_RETURN ) = (int)video->state;
2877         else
2878                 PRVM_G_FLOAT( OFS_RETURN ) = 0;
2879 }
2880
2881 /*
2882 ========================
2883 VM_cin_restart
2884
2885 void cin_restart(string name)
2886 ========================
2887 */
2888 void VM_cin_restart( void )
2889 {
2890         const char *name;
2891         clvideo_t               *video;
2892
2893         VM_SAFEPARMCOUNT( 1, VM_cin_restart );
2894
2895         name = PRVM_G_STRING( OFS_PARM0 );
2896         VM_CheckEmptyString( name );
2897
2898         video = CL_GetVideoByName( name );
2899         if( video )
2900                 CL_RestartVideo( video );
2901 }
2902
2903 /*
2904 ==============
2905 VM_makevectors
2906
2907 Writes new values for v_forward, v_up, and v_right based on angles
2908 void makevectors(vector angle)
2909 ==============
2910 */
2911 void VM_makevectors (void)
2912 {
2913         prvm_eval_t *valforward, *valright, *valup;
2914         valforward = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_forward);
2915         valright = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_right);
2916         valup = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_up);
2917         if (!valforward || !valright || !valup)
2918         {
2919                 VM_Warning("makevectors: could not find v_forward, v_right, or v_up global variables\n");
2920                 return;
2921         }
2922         VM_SAFEPARMCOUNT(1, VM_makevectors);
2923         AngleVectors (PRVM_G_VECTOR(OFS_PARM0), valforward->vector, valright->vector, valup->vector);
2924 }
2925
2926 /*
2927 ==============
2928 VM_vectorvectors
2929
2930 Writes new values for v_forward, v_up, and v_right based on the given forward vector
2931 vectorvectors(vector)
2932 ==============
2933 */
2934 void VM_vectorvectors (void)
2935 {
2936         prvm_eval_t *valforward, *valright, *valup;
2937         valforward = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_forward);
2938         valright = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_right);
2939         valup = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_up);
2940         if (!valforward || !valright || !valup)
2941         {
2942                 VM_Warning("vectorvectors: could not find v_forward, v_right, or v_up global variables\n");
2943                 return;
2944         }
2945         VM_SAFEPARMCOUNT(1, VM_vectorvectors);
2946         VectorNormalize2(PRVM_G_VECTOR(OFS_PARM0), valforward->vector);
2947         VectorVectors(valforward->vector, valright->vector, valup->vector);
2948 }
2949
2950 /*
2951 ========================
2952 VM_drawline
2953
2954 void drawline(float width, vector pos1, vector pos2, vector rgb, float alpha, float flags)
2955 ========================
2956 */
2957 void VM_drawline (void)
2958 {
2959         float   *c1, *c2, *rgb;
2960         float   alpha, width;
2961         unsigned char   flags;
2962
2963         VM_SAFEPARMCOUNT(6, VM_drawline);
2964         width   = PRVM_G_FLOAT(OFS_PARM0);
2965         c1              = PRVM_G_VECTOR(OFS_PARM1);
2966         c2              = PRVM_G_VECTOR(OFS_PARM2);
2967         rgb             = PRVM_G_VECTOR(OFS_PARM3);
2968         alpha   = PRVM_G_FLOAT(OFS_PARM4);
2969         flags   = (int)PRVM_G_FLOAT(OFS_PARM5);
2970         DrawQ_Line(width, c1[0], c1[1], c2[0], c2[1], rgb[0], rgb[1], rgb[2], alpha, flags);
2971 }
2972
2973
2974
2975
2976
2977 // float(float number, float quantity) bitshift (EXT_BITSHIFT)
2978 void VM_bitshift (void)
2979 {
2980         int n1, n2;
2981         VM_SAFEPARMCOUNT(2, VM_bitshift);
2982
2983         n1 = (int)fabs((int)PRVM_G_FLOAT(OFS_PARM0));
2984         n2 = (int)PRVM_G_FLOAT(OFS_PARM1);
2985         if(!n1)
2986                 PRVM_G_FLOAT(OFS_RETURN) = n1;
2987         else
2988         if(n2 < 0)
2989                 PRVM_G_FLOAT(OFS_RETURN) = (n1 >> -n2);
2990         else
2991                 PRVM_G_FLOAT(OFS_RETURN) = (n1 << n2);
2992 }
2993
2994 ////////////////////////////////////////
2995 // AltString functions
2996 ////////////////////////////////////////
2997
2998 /*
2999 ========================
3000 VM_altstr_count
3001
3002 float altstr_count(string)
3003 ========================
3004 */
3005 void VM_altstr_count( void )
3006 {
3007         const char *altstr, *pos;
3008         int     count;
3009
3010         VM_SAFEPARMCOUNT( 1, VM_altstr_count );
3011
3012         altstr = PRVM_G_STRING( OFS_PARM0 );
3013         //VM_CheckEmptyString( altstr );
3014
3015         for( count = 0, pos = altstr ; *pos ; pos++ ) {
3016                 if( *pos == '\\' ) {
3017                         if( !*++pos ) {
3018                                 break;
3019                         }
3020                 } else if( *pos == '\'' ) {
3021                         count++;
3022                 }
3023         }
3024
3025         PRVM_G_FLOAT( OFS_RETURN ) = (float) (count / 2);
3026 }
3027
3028 /*
3029 ========================
3030 VM_altstr_prepare
3031
3032 string altstr_prepare(string)
3033 ========================
3034 */
3035 void VM_altstr_prepare( void )
3036 {
3037         char *out;
3038         const char *instr, *in;
3039         int size;
3040         char outstr[VM_STRINGTEMP_LENGTH];
3041
3042         VM_SAFEPARMCOUNT( 1, VM_altstr_prepare );
3043
3044         instr = PRVM_G_STRING( OFS_PARM0 );
3045
3046         for( out = outstr, in = instr, size = sizeof(outstr) - 1 ; size && *in ; size--, in++, out++ )
3047                 if( *in == '\'' ) {
3048                         *out++ = '\\';
3049                         *out = '\'';
3050                         size--;
3051                 } else
3052                         *out = *in;
3053         *out = 0;
3054
3055         PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
3056 }
3057
3058 /*
3059 ========================
3060 VM_altstr_get
3061
3062 string altstr_get(string, float)
3063 ========================
3064 */
3065 void VM_altstr_get( void )
3066 {
3067         const char *altstr, *pos;
3068         char *out;
3069         int count, size;
3070         char outstr[VM_STRINGTEMP_LENGTH];
3071
3072         VM_SAFEPARMCOUNT( 2, VM_altstr_get );
3073
3074         altstr = PRVM_G_STRING( OFS_PARM0 );
3075
3076         count = (int)PRVM_G_FLOAT( OFS_PARM1 );
3077         count = count * 2 + 1;
3078
3079         for( pos = altstr ; *pos && count ; pos++ )
3080                 if( *pos == '\\' ) {
3081                         if( !*++pos )
3082                                 break;
3083                 } else if( *pos == '\'' )
3084                         count--;
3085
3086         if( !*pos ) {
3087                 PRVM_G_INT( OFS_RETURN ) = 0;
3088                 return;
3089         }
3090
3091         for( out = outstr, size = sizeof(outstr) - 1 ; size && *pos ; size--, pos++, out++ )
3092                 if( *pos == '\\' ) {
3093                         if( !*++pos )
3094                                 break;
3095                         *out = *pos;
3096                         size--;
3097                 } else if( *pos == '\'' )
3098                         break;
3099                 else
3100                         *out = *pos;
3101
3102         *out = 0;
3103         PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
3104 }
3105
3106 /*
3107 ========================
3108 VM_altstr_set
3109
3110 string altstr_set(string altstr, float num, string set)
3111 ========================
3112 */
3113 void VM_altstr_set( void )
3114 {
3115     int num;
3116         const char *altstr, *str;
3117         const char *in;
3118         char *out;
3119         char outstr[VM_STRINGTEMP_LENGTH];
3120
3121         VM_SAFEPARMCOUNT( 3, VM_altstr_set );
3122
3123         altstr = PRVM_G_STRING( OFS_PARM0 );
3124
3125         num = (int)PRVM_G_FLOAT( OFS_PARM1 );
3126
3127         str = PRVM_G_STRING( OFS_PARM2 );
3128
3129         out = outstr;
3130         for( num = num * 2 + 1, in = altstr; *in && num; *out++ = *in++ )
3131                 if( *in == '\\' ) {
3132                         if( !*++in ) {
3133                                 break;
3134                         }
3135                 } else if( *in == '\'' ) {
3136                         num--;
3137                 }
3138
3139         // copy set in
3140         for( ; *str; *out++ = *str++ );
3141         // now jump over the old content
3142         for( ; *in ; in++ )
3143                 if( *in == '\'' || (*in == '\\' && !*++in) )
3144                         break;
3145
3146         strlcpy(out, in, outstr + sizeof(outstr) - out);
3147         PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
3148 }
3149
3150 /*
3151 ========================
3152 VM_altstr_ins
3153 insert after num
3154 string  altstr_ins(string altstr, float num, string set)
3155 ========================
3156 */
3157 void VM_altstr_ins(void)
3158 {
3159         int num;
3160         const char *setstr;
3161         const char *set;
3162         const char *instr;
3163         const char *in;
3164         char *out;
3165         char outstr[VM_STRINGTEMP_LENGTH];
3166
3167         VM_SAFEPARMCOUNT(3, VM_altstr_ins);
3168
3169         in = instr = PRVM_G_STRING( OFS_PARM0 );
3170         num = (int)PRVM_G_FLOAT( OFS_PARM1 );
3171         set = setstr = PRVM_G_STRING( OFS_PARM2 );
3172
3173         out = outstr;
3174         for( num = num * 2 + 2 ; *in && num > 0 ; *out++ = *in++ )
3175                 if( *in == '\\' ) {
3176                         if( !*++in ) {
3177                                 break;
3178                         }
3179                 } else if( *in == '\'' ) {
3180                         num--;
3181                 }
3182
3183         *out++ = '\'';
3184         for( ; *set ; *out++ = *set++ );
3185         *out++ = '\'';
3186
3187         strlcpy(out, in, outstr + sizeof(outstr) - out);
3188         PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
3189 }
3190
3191
3192 ////////////////////////////////////////
3193 // BufString functions
3194 ////////////////////////////////////////
3195 //[515]: string buffers support
3196 #define MAX_QCSTR_BUFFERS 128
3197 #define MAX_QCSTR_STRINGS 1024
3198
3199 typedef struct
3200 {
3201         int             num_strings;
3202         char    *strings[MAX_QCSTR_STRINGS];
3203 }qcstrbuffer_t;
3204
3205 // FIXME: move stringbuffers to prog_t to allow multiple progs!
3206 static qcstrbuffer_t    *qcstringbuffers[MAX_QCSTR_BUFFERS];
3207 static int                              num_qcstringbuffers;
3208 static int                              buf_sortpower;
3209
3210 #define BUFSTR_BUFFER(a) (a>=MAX_QCSTR_BUFFERS) ? NULL : (qcstringbuffers[a])
3211 #define BUFSTR_ISFREE(a) (a<MAX_QCSTR_BUFFERS&&qcstringbuffers[a]&&qcstringbuffers[a]->num_strings<=0) ? 1 : 0
3212
3213 static int BufStr_FindFreeBuffer (void)
3214 {
3215         int     i;
3216         if(num_qcstringbuffers == MAX_QCSTR_BUFFERS)
3217                 return -1;
3218         for(i=0;i<MAX_QCSTR_BUFFERS;i++)
3219                 if(!qcstringbuffers[i])
3220                 {
3221                         qcstringbuffers[i] = (qcstrbuffer_t *)Z_Malloc(sizeof(qcstrbuffer_t));
3222                         memset(qcstringbuffers[i], 0, sizeof(qcstrbuffer_t));
3223                         return i;
3224                 }
3225         return -1;
3226 }
3227
3228 static void BufStr_ClearBuffer (int index)
3229 {
3230         qcstrbuffer_t   *b = qcstringbuffers[index];
3231         int                             i;
3232
3233         if(b)
3234         {
3235                 if(b->num_strings > 0)
3236                 {
3237                         for(i=0;i<b->num_strings;i++)
3238                                 if(b->strings[i])
3239                                         Z_Free(b->strings[i]);
3240                         num_qcstringbuffers--;
3241                 }
3242                 Z_Free(qcstringbuffers[index]);
3243                 qcstringbuffers[index] = NULL;
3244         }
3245 }
3246
3247 static int BufStr_FindFreeString (qcstrbuffer_t *b)
3248 {
3249         int                             i;
3250         for(i=0;i<b->num_strings;i++)
3251                 if(!b->strings[i] || !b->strings[i][0])
3252                         return i;
3253         if(i == MAX_QCSTR_STRINGS)      return -1;
3254         else                                            return i;
3255 }
3256
3257 static int BufStr_SortStringsUP (const void *in1, const void *in2)
3258 {
3259         const char *a, *b;
3260         a = *((const char **) in1);
3261         b = *((const char **) in2);
3262         if(!a[0])       return 1;
3263         if(!b[0])       return -1;
3264         return strncmp(a, b, buf_sortpower);
3265 }
3266
3267 static int BufStr_SortStringsDOWN (const void *in1, const void *in2)
3268 {
3269         const char *a, *b;
3270         a = *((const char **) in1);
3271         b = *((const char **) in2);
3272         if(!a[0])       return 1;
3273         if(!b[0])       return -1;
3274         return strncmp(b, a, buf_sortpower);
3275 }
3276
3277 /*
3278 ========================
3279 VM_buf_create
3280 creates new buffer, and returns it's index, returns -1 if failed
3281 float buf_create(void) = #460;
3282 ========================
3283 */
3284 void VM_buf_create (void)
3285 {
3286         int i;
3287         VM_SAFEPARMCOUNT(0, VM_buf_create);
3288         i = BufStr_FindFreeBuffer();
3289         if(i >= 0)
3290                 num_qcstringbuffers++;
3291         //else
3292                 //Con_Printf("VM_buf_create: buffers overflow in %s\n", PRVM_NAME);
3293         PRVM_G_FLOAT(OFS_RETURN) = i;
3294 }
3295
3296 /*
3297 ========================
3298 VM_buf_del
3299 deletes buffer and all strings in it
3300 void buf_del(float bufhandle) = #461;
3301 ========================
3302 */
3303 void VM_buf_del (void)
3304 {
3305         VM_SAFEPARMCOUNT(1, VM_buf_del);
3306         if(BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0)))
3307                 BufStr_ClearBuffer((int)PRVM_G_FLOAT(OFS_PARM0));
3308         else
3309         {
3310                 VM_Warning("VM_buf_del: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3311                 return;
3312         }
3313 }
3314
3315 /*
3316 ========================
3317 VM_buf_getsize
3318 how many strings are stored in buffer
3319 float buf_getsize(float bufhandle) = #462;
3320 ========================
3321 */
3322 void VM_buf_getsize (void)
3323 {
3324         qcstrbuffer_t   *b;
3325         VM_SAFEPARMCOUNT(1, VM_buf_getsize);
3326
3327         b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3328         if(!b)
3329         {
3330                 PRVM_G_FLOAT(OFS_RETURN) = -1;
3331                 VM_Warning("VM_buf_getsize: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3332                 return;
3333         }
3334         else
3335                 PRVM_G_FLOAT(OFS_RETURN) = b->num_strings;
3336 }
3337
3338 /*
3339 ========================
3340 VM_buf_copy
3341 copy all content from one buffer to another, make sure it exists
3342 void buf_copy(float bufhandle_from, float bufhandle_to) = #463;
3343 ========================
3344 */
3345 void VM_buf_copy (void)
3346 {
3347         qcstrbuffer_t   *b1, *b2;
3348         int                             i;
3349         VM_SAFEPARMCOUNT(2, VM_buf_copy);
3350
3351         b1 = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3352         if(!b1)
3353         {
3354                 VM_Warning("VM_buf_copy: invalid source buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3355                 return;
3356         }
3357         i = (int)PRVM_G_FLOAT(OFS_PARM1);
3358         if(i == (int)PRVM_G_FLOAT(OFS_PARM0))
3359         {
3360                 VM_Warning("VM_buf_copy: source == destination (%i) in %s\n", i, PRVM_NAME);
3361                 return;
3362         }
3363         b2 = BUFSTR_BUFFER(i);
3364         if(!b2)
3365         {
3366                 VM_Warning("VM_buf_copy: invalid destination buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM1), PRVM_NAME);
3367                 return;
3368         }
3369
3370         BufStr_ClearBuffer(i);
3371         qcstringbuffers[i] = (qcstrbuffer_t *)Z_Malloc(sizeof(qcstrbuffer_t));
3372         memset(qcstringbuffers[i], 0, sizeof(qcstrbuffer_t));
3373         b2->num_strings = b1->num_strings;
3374
3375         for(i=0;i<b1->num_strings;i++)
3376                 if(b1->strings[i] && b1->strings[i][0])
3377                 {
3378                         size_t stringlen;
3379                         stringlen = strlen(b1->strings[i]) + 1;
3380                         b2->strings[i] = (char *)Z_Malloc(stringlen);
3381                         if(!b2->strings[i])
3382                         {
3383                                 VM_Warning("VM_buf_copy: not enough memory for buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM1), PRVM_NAME);
3384                                 break;
3385                         }
3386                         memcpy(b2->strings[i], b1->strings[i], stringlen);
3387                 }
3388 }
3389
3390 /*
3391 ========================
3392 VM_buf_sort
3393 sort buffer by beginnings of strings (sortpower defaults it's lenght)
3394 "backward == TRUE" means that sorting goes upside-down
3395 void buf_sort(float bufhandle, float sortpower, float backward) = #464;
3396 ========================
3397 */
3398 void VM_buf_sort (void)
3399 {
3400         qcstrbuffer_t   *b;
3401         int                             i;
3402         VM_SAFEPARMCOUNT(3, VM_buf_sort);
3403
3404         b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3405         if(!b)
3406         {
3407                 VM_Warning("VM_buf_sort: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3408                 return;
3409         }
3410         if(b->num_strings <= 0)
3411         {
3412                 VM_Warning("VM_buf_sort: tried to sort empty buffer %i in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3413                 return;
3414         }
3415         buf_sortpower = (int)PRVM_G_FLOAT(OFS_PARM1);
3416         if(buf_sortpower <= 0)
3417                 buf_sortpower = 99999999;
3418
3419         if(!PRVM_G_FLOAT(OFS_PARM2))
3420                 qsort(b->strings, b->num_strings, sizeof(char*), BufStr_SortStringsUP);
3421         else
3422                 qsort(b->strings, b->num_strings, sizeof(char*), BufStr_SortStringsDOWN);
3423
3424         for(i=b->num_strings-1;i>=0;i--)        //[515]: delete empty lines
3425                 if(b->strings)
3426                 {
3427                         if(b->strings[i][0])
3428                                 break;
3429                         else
3430                         {
3431                                 Z_Free(b->strings[i]);
3432                                 --b->num_strings;
3433                                 b->strings[i] = NULL;
3434                         }
3435                 }
3436                 else
3437                         --b->num_strings;
3438 }
3439
3440 /*
3441 ========================
3442 VM_buf_implode
3443 concantenates all buffer string into one with "glue" separator and returns it as tempstring
3444 string buf_implode(float bufhandle, string glue) = #465;
3445 ========================
3446 */
3447 void VM_buf_implode (void)
3448 {
3449         qcstrbuffer_t   *b;
3450         char                    k[VM_STRINGTEMP_LENGTH];
3451         const char              *sep;
3452         int                             i;
3453         size_t                  l;
3454         VM_SAFEPARMCOUNT(2, VM_buf_implode);
3455
3456         b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3457         PRVM_G_INT(OFS_RETURN) = OFS_NULL;
3458         if(!b)
3459         {
3460                 VM_Warning("VM_buf_implode: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3461                 return;
3462         }
3463         if(!b->num_strings)
3464                 return;
3465         sep = PRVM_G_STRING(OFS_PARM1);
3466         k[0] = 0;
3467         for(l=i=0;i<b->num_strings;i++)
3468                 if(b->strings[i])
3469                 {
3470                         l += (i > 0 ? strlen(sep) : 0) + strlen(b->strings[i]);
3471                         if (l >= sizeof(k) - 1)
3472                                 break;
3473                         strlcat(k, sep, sizeof(k));
3474                         strlcat(k, b->strings[i], sizeof(k));
3475                 }
3476         PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(k);
3477 }
3478
3479 /*
3480 ========================
3481 VM_bufstr_get
3482 get a string from buffer, returns tempstring, dont str_unzone it!
3483 string bufstr_get(float bufhandle, float string_index) = #465;
3484 ========================
3485 */
3486 void VM_bufstr_get (void)
3487 {
3488         qcstrbuffer_t   *b;
3489         int                             strindex;
3490         VM_SAFEPARMCOUNT(2, VM_bufstr_get);
3491
3492         PRVM_G_INT(OFS_RETURN) = OFS_NULL;
3493         b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3494         if(!b)
3495         {
3496                 VM_Warning("VM_bufstr_get: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), 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_get: invalid string index %i used in %s\n", strindex, PRVM_NAME);
3503                 return;
3504         }
3505         if(b->num_strings <= strindex)
3506                 return;
3507         if(b->strings[strindex])
3508                 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(b->strings[strindex]);
3509 }
3510
3511 /*
3512 ========================
3513 VM_bufstr_set
3514 copies a string into selected slot of buffer
3515 void bufstr_set(float bufhandle, float string_index, string str) = #466;
3516 ========================
3517 */
3518 void VM_bufstr_set (void)
3519 {
3520         int                             bufindex, strindex;
3521         qcstrbuffer_t   *b;
3522         const char              *news;
3523         size_t                  alloclen;
3524
3525         VM_SAFEPARMCOUNT(3, VM_bufstr_set);
3526
3527         bufindex = (int)PRVM_G_FLOAT(OFS_PARM0);
3528         b = BUFSTR_BUFFER(bufindex);
3529         if(!b)
3530         {
3531                 VM_Warning("VM_bufstr_set: invalid buffer %i used in %s\n", bufindex, PRVM_NAME);
3532                 return;
3533         }
3534         strindex = (int)PRVM_G_FLOAT(OFS_PARM1);
3535         if(strindex < 0 || strindex > MAX_QCSTR_STRINGS)
3536         {
3537                 VM_Warning("VM_bufstr_set: invalid string index %i used in %s\n", strindex, PRVM_NAME);
3538                 return;
3539         }
3540         news = PRVM_G_STRING(OFS_PARM2);
3541         if(b->strings[strindex])
3542                 Z_Free(b->strings[strindex]);
3543         alloclen = strlen(news) + 1;
3544         b->strings[strindex] = (char *)Z_Malloc(alloclen);
3545         memcpy(b->strings[strindex], news, alloclen);
3546 }
3547
3548 /*
3549 ========================
3550 VM_bufstr_add
3551 adds string to buffer in nearest free slot and returns it
3552 "order == TRUE" means that string will be added after last "full" slot
3553 float bufstr_add(float bufhandle, string str, float order) = #467;
3554 ========================
3555 */
3556 void VM_bufstr_add (void)
3557 {
3558         int                             bufindex, order, strindex;
3559         qcstrbuffer_t   *b;
3560         const char              *string;
3561         size_t                  alloclen;
3562
3563         VM_SAFEPARMCOUNT(3, VM_bufstr_add);
3564
3565         bufindex = (int)PRVM_G_FLOAT(OFS_PARM0);
3566         b = BUFSTR_BUFFER(bufindex);
3567         PRVM_G_FLOAT(OFS_RETURN) = -1;
3568         if(!b)
3569         {
3570                 VM_Warning("VM_bufstr_add: invalid buffer %i used in %s\n", bufindex, PRVM_NAME);
3571                 return;
3572         }
3573         string = PRVM_G_STRING(OFS_PARM1);
3574         order = (int)PRVM_G_FLOAT(OFS_PARM2);
3575         if(order)
3576                 strindex = b->num_strings;
3577         else
3578         {
3579                 strindex = BufStr_FindFreeString(b);
3580                 if(strindex < 0)
3581                 {
3582                         VM_Warning("VM_bufstr_add: buffer %i has no free string slots in %s\n", bufindex, PRVM_NAME);
3583                         return;
3584                 }
3585         }
3586
3587         while(b->num_strings <= strindex)
3588         {
3589                 if(b->num_strings == MAX_QCSTR_STRINGS)
3590                 {
3591                         VM_Warning("VM_bufstr_add: buffer %i has no free string slots in %s\n", bufindex, PRVM_NAME);
3592                         return;
3593                 }
3594                 b->strings[b->num_strings] = NULL;
3595                 b->num_strings++;
3596         }
3597         if(b->strings[strindex])
3598                 Z_Free(b->strings[strindex]);
3599         alloclen = strlen(string) + 1;
3600         b->strings[strindex] = (char *)Z_Malloc(alloclen);
3601         memcpy(b->strings[strindex], string, alloclen);
3602         PRVM_G_FLOAT(OFS_RETURN) = strindex;
3603 }
3604
3605 /*
3606 ========================
3607 VM_bufstr_free
3608 delete string from buffer
3609 void bufstr_free(float bufhandle, float string_index) = #468;
3610 ========================
3611 */
3612 void VM_bufstr_free (void)
3613 {
3614         int                             i;
3615         qcstrbuffer_t   *b;
3616         VM_SAFEPARMCOUNT(2, VM_bufstr_free);
3617
3618         b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3619         if(!b)
3620         {
3621                 VM_Warning("VM_bufstr_free: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3622                 return;
3623         }
3624         i = (int)PRVM_G_FLOAT(OFS_PARM1);
3625         if(i < 0 || i > MAX_QCSTR_STRINGS)
3626         {
3627                 VM_Warning("VM_bufstr_free: invalid string index %i used in %s\n", i, PRVM_NAME);
3628                 return;
3629         }
3630         if(b->strings[i])
3631                 Z_Free(b->strings[i]);
3632         b->strings[i] = NULL;
3633         if(i+1 == b->num_strings)
3634                 --b->num_strings;
3635 }
3636
3637 //=============
3638
3639 /*
3640 ==============
3641 VM_changeyaw
3642
3643 This was a major timewaster in progs, so it was converted to C
3644 ==============
3645 */
3646 void VM_changeyaw (void)
3647 {
3648         prvm_edict_t            *ent;
3649         float           ideal, current, move, speed;
3650
3651         // this is called (VERY HACKISHLY) by SV_MoveToGoal, so it can not use any
3652         // parameters because they are the parameters to SV_MoveToGoal, not this
3653         //VM_SAFEPARMCOUNT(0, VM_changeyaw);
3654
3655         ent = PRVM_PROG_TO_EDICT(PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict);
3656         if (ent == prog->edicts)
3657         {
3658                 VM_Warning("changeyaw: can not modify world entity\n");
3659                 return;
3660         }
3661         if (ent->priv.server->free)
3662         {
3663                 VM_Warning("changeyaw: can not modify free entity\n");
3664                 return;
3665         }
3666         if (prog->fieldoffsets.angles < 0 || prog->fieldoffsets.ideal_yaw < 0 || prog->fieldoffsets.yaw_speed < 0)
3667         {
3668                 VM_Warning("changeyaw: angles, ideal_yaw, or yaw_speed field(s) not found\n");
3669                 return;
3670         }
3671         current = ANGLEMOD(PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.angles)->vector[1]);
3672         ideal = PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.ideal_yaw)->_float;
3673         speed = PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.yaw_speed)->_float;
3674
3675         if (current == ideal)
3676                 return;
3677         move = ideal - current;
3678         if (ideal > current)
3679         {
3680                 if (move >= 180)
3681                         move = move - 360;
3682         }
3683         else
3684         {
3685                 if (move <= -180)
3686                         move = move + 360;
3687         }
3688         if (move > 0)
3689         {
3690                 if (move > speed)
3691                         move = speed;
3692         }
3693         else
3694         {
3695                 if (move < -speed)
3696                         move = -speed;
3697         }
3698
3699         PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.angles)->vector[1] = ANGLEMOD (current + move);
3700 }
3701
3702 /*
3703 ==============
3704 VM_changepitch
3705 ==============
3706 */
3707 void VM_changepitch (void)
3708 {
3709         prvm_edict_t            *ent;
3710         float           ideal, current, move, speed;
3711
3712         VM_SAFEPARMCOUNT(1, VM_changepitch);
3713
3714         ent = PRVM_G_EDICT(OFS_PARM0);
3715         if (ent == prog->edicts)
3716         {
3717                 VM_Warning("changepitch: can not modify world entity\n");
3718                 return;
3719         }
3720         if (ent->priv.server->free)
3721         {
3722                 VM_Warning("changepitch: can not modify free entity\n");
3723                 return;
3724         }
3725         if (prog->fieldoffsets.angles < 0 || prog->fieldoffsets.idealpitch < 0 || prog->fieldoffsets.pitch_speed < 0)
3726         {
3727                 VM_Warning("changepitch: angles, idealpitch, or pitch_speed field(s) not found\n");
3728                 return;
3729         }
3730         current = ANGLEMOD(PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.angles)->vector[0]);
3731         ideal = PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.idealpitch)->_float;
3732         speed = PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.pitch_speed)->_float;
3733
3734         if (current == ideal)
3735                 return;
3736         move = ideal - current;
3737         if (ideal > current)
3738         {
3739                 if (move >= 180)
3740                         move = move - 360;
3741         }
3742         else
3743         {
3744                 if (move <= -180)
3745                         move = move + 360;
3746         }
3747         if (move > 0)
3748         {
3749                 if (move > speed)
3750                         move = speed;
3751         }
3752         else
3753         {
3754                 if (move < -speed)
3755                         move = -speed;
3756         }
3757
3758         PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.angles)->vector[0] = ANGLEMOD (current + move);
3759 }
3760
3761
3762 static int Is_Text_Color (char c, char t)
3763 {
3764         int a = 0;
3765         char c2 = c - (c & 128);
3766         char t2 = t - (t & 128);
3767
3768         if(c != STRING_COLOR_TAG && c2 != STRING_COLOR_TAG)             return 0;
3769         if(t >= '0' && t <= '9')                a = 1;
3770         if(t2 >= '0' && t2 <= '9')              a = 1;
3771 /*      if(t >= 'A' && t <= 'Z')                a = 2;
3772         if(t2 >= 'A' && t2 <= 'Z')              a = 2;
3773
3774         if(a == 1 && scr_colortext.integer > 0)
3775                 return 1;
3776         if(a == 2 && scr_multifonts.integer > 0)
3777                 return 2;
3778 */
3779         return a;
3780 }
3781
3782 void VM_uncolorstring (void)
3783 {
3784         const char      *in;
3785         char            out[VM_STRINGTEMP_LENGTH];
3786         int                     k = 0, i = 0;
3787
3788         VM_SAFEPARMCOUNT(1, VM_uncolorstring);
3789         in = PRVM_G_STRING(OFS_PARM0);
3790         VM_CheckEmptyString (in);
3791
3792         while (in[k])
3793         {
3794                 if(in[k+1])
3795                 if(Is_Text_Color(in[k], in[k+1]) == 1/* || (in[k] == '&' && in[k+1] == 'r')*/)
3796                 {
3797                         k += 2;
3798                         continue;
3799                 }
3800                 out[i] = in[k];
3801                 ++k;
3802                 ++i;
3803         }
3804         PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(out);
3805 }
3806
3807 //#222 string(string s, float index) str2chr (FTE_STRINGS)
3808 void VM_str2chr (void)
3809 {
3810         const char *s;
3811         VM_SAFEPARMCOUNT(2, VM_str2chr);
3812         s = PRVM_G_STRING(OFS_PARM0);
3813         if((unsigned)PRVM_G_FLOAT(OFS_PARM1) > strlen(s))
3814                 return;
3815         PRVM_G_FLOAT(OFS_RETURN) = (unsigned char)s[(int)PRVM_G_FLOAT(OFS_PARM1)];
3816 }
3817