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
13 extern cvar_t prvm_backtraceforwarnings;
15 // LordHavoc: changed this to NOT use a return statement, so that it can be used in functions that must return a value
16 void VM_Warning(const char *fmt, ...)
19 char msg[MAX_INPUTLINE];
20 static double recursive = -1;
23 dpvsnprintf(msg,sizeof(msg),fmt,argptr);
28 // TODO: either add a cvar/cmd to control the state dumping or replace some of the calls with Con_Printf [9/13/2006 Black]
29 if(prvm_backtraceforwarnings.integer && recursive != realtime) // NOTE: this compares to the time, just in case if PRVM_PrintState causes a Host_Error and keeps recursive set
38 //============================================================================
41 // TODO DONE: move vm_files and vm_fssearchlist to prvm_prog_t struct
42 // TODO: move vm_files and vm_fssearchlist back [9/13/2006 Black]
43 // TODO: (move vm_files and vm_fssearchlist to prvm_prog_t struct again) [2007-01-23 LordHavoc]
44 // TODO: will this war ever end? [2007-01-23 LordHavoc]
46 void VM_CheckEmptyString (const char *s)
48 if (ISWHITESPACE(s[0]))
49 PRVM_ERROR ("%s: Bad string", PRVM_NAME);
52 //============================================================================
55 void VM_VarString(int first, char *out, int outlength)
61 outend = out + outlength - 1;
62 for (i = first;i < prog->argc && out < outend;i++)
64 s = PRVM_G_STRING((OFS_PARM0+i*3));
65 while (out < outend && *s)
75 returns true if the extension is supported by the server
77 checkextension(extensionname)
81 // kind of helper function
82 static qboolean checkextension(const char *name)
86 len = (int)strlen(name);
88 for (e = prog->extensionstring;*e;e++)
95 while (*e && *e != ' ')
97 if ((e - start) == len && !strncasecmp(start, name, len))
103 void VM_checkextension (void)
105 VM_SAFEPARMCOUNT(1,VM_checkextension);
107 PRVM_G_FLOAT(OFS_RETURN) = checkextension(PRVM_G_STRING(OFS_PARM0));
114 This is a TERMINAL error, which will kill off the entire prog.
123 char string[VM_STRINGTEMP_LENGTH];
125 VM_VarString(0, string, sizeof(string));
126 Con_Printf("======%s ERROR in %s:\n%s\n", PRVM_NAME, PRVM_GetString(prog->xfunction->s_name), string);
127 if (prog->globaloffsets.self >= 0)
129 ed = PRVM_PROG_TO_EDICT(PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict);
130 PRVM_ED_Print(ed, NULL);
133 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);
140 Dumps out self, then an error message. The program is aborted and self is
141 removed, but the level can continue.
146 void VM_objerror (void)
149 char string[VM_STRINGTEMP_LENGTH];
151 VM_VarString(0, string, sizeof(string));
152 Con_Printf("======OBJECT ERROR======\n"); // , PRVM_NAME, PRVM_GetString(prog->xfunction->s_name), string); // or include them? FIXME
153 if (prog->globaloffsets.self >= 0)
155 ed = PRVM_PROG_TO_EDICT(PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict);
156 PRVM_ED_Print(ed, NULL);
161 // objerror has to display the object fields -> else call
162 PRVM_ERROR ("VM_objecterror: self not defined !");
163 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);
177 char string[VM_STRINGTEMP_LENGTH];
179 VM_VarString(0, string, sizeof(string));
187 broadcast print to everyone on server
192 void VM_bprint (void)
194 char string[VM_STRINGTEMP_LENGTH];
198 VM_Warning("VM_bprint: game is not server(%s) !\n", PRVM_NAME);
202 VM_VarString(0, string, sizeof(string));
203 SV_BroadcastPrint(string);
208 VM_sprint (menu & client but only if server.active == true)
210 single print to a specific client
212 sprint(float clientnum,...[string])
215 void VM_sprint (void)
219 char string[VM_STRINGTEMP_LENGTH];
221 VM_SAFEPARMCOUNTRANGE(1, 8, VM_sprint);
223 //find client for this entity
224 clientnum = (int)PRVM_G_FLOAT(OFS_PARM0);
225 if (!sv.active || clientnum < 0 || clientnum >= svs.maxclients || !svs.clients[clientnum].active)
227 VM_Warning("VM_sprint: %s: invalid client or server is not active !\n", PRVM_NAME);
231 client = svs.clients + clientnum;
232 if (!client->netconnection)
235 VM_VarString(1, string, sizeof(string));
236 MSG_WriteChar(&client->netconnection->message,svc_print);
237 MSG_WriteString(&client->netconnection->message, string);
244 single print to the screen
249 void VM_centerprint (void)
251 char string[VM_STRINGTEMP_LENGTH];
253 VM_SAFEPARMCOUNTRANGE(1, 8, VM_centerprint);
254 VM_VarString(0, string, sizeof(string));
255 SCR_CenterPrint(string);
262 vector normalize(vector)
265 void VM_normalize (void)
271 VM_SAFEPARMCOUNT(1,VM_normalize);
273 value1 = PRVM_G_VECTOR(OFS_PARM0);
275 f = VectorLength2(value1);
279 VectorScale(value1, f, newvalue);
282 VectorClear(newvalue);
284 VectorCopy (newvalue, PRVM_G_VECTOR(OFS_RETURN));
296 VM_SAFEPARMCOUNT(1,VM_vlen);
297 PRVM_G_FLOAT(OFS_RETURN) = VectorLength(PRVM_G_VECTOR(OFS_PARM0));
304 float vectoyaw(vector)
307 void VM_vectoyaw (void)
312 VM_SAFEPARMCOUNT(1,VM_vectoyaw);
314 value1 = PRVM_G_VECTOR(OFS_PARM0);
316 if (value1[1] == 0 && value1[0] == 0)
320 yaw = (int) (atan2(value1[1], value1[0]) * 180 / M_PI);
325 PRVM_G_FLOAT(OFS_RETURN) = yaw;
333 vector vectoangles(vector[, vector])
336 void VM_vectoangles (void)
338 VM_SAFEPARMCOUNTRANGE(1, 2,VM_vectoangles);
340 AnglesFromVectors(PRVM_G_VECTOR(OFS_RETURN), PRVM_G_VECTOR(OFS_PARM0), prog->argc >= 2 ? PRVM_G_VECTOR(OFS_PARM1) : NULL, true);
347 Returns a number from 0<= num < 1
352 void VM_random (void)
354 VM_SAFEPARMCOUNT(0,VM_random);
356 PRVM_G_FLOAT(OFS_RETURN) = lhrandom(0, 1);
363 localsound(string sample)
366 void VM_localsound(void)
370 VM_SAFEPARMCOUNT(1,VM_localsound);
372 s = PRVM_G_STRING(OFS_PARM0);
374 if(!S_LocalSound (s))
376 PRVM_G_FLOAT(OFS_RETURN) = -4;
377 VM_Warning("VM_localsound: Failed to play %s for %s !\n", s, PRVM_NAME);
381 PRVM_G_FLOAT(OFS_RETURN) = 1;
393 PRVM_ERROR ("%s: break statement", PRVM_NAME);
396 //============================================================================
402 Sends text over to the client's execution buffer
404 [localcmd (string, ...) or]
408 void VM_localcmd (void)
410 char string[VM_STRINGTEMP_LENGTH];
411 VM_SAFEPARMCOUNTRANGE(1, 8, VM_localcmd);
412 VM_VarString(0, string, sizeof(string));
413 Cbuf_AddText(string);
416 static qboolean PRVM_Cvar_ReadOk(const char *string)
419 cvar = Cvar_FindVar(string);
420 return ((cvar) && ((cvar->flags & CVAR_PRIVATE) == 0));
432 char string[VM_STRINGTEMP_LENGTH];
433 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar);
434 VM_VarString(0, string, sizeof(string));
435 VM_CheckEmptyString(string);
436 PRVM_G_FLOAT(OFS_RETURN) = PRVM_Cvar_ReadOk(string) ? Cvar_VariableValue(string) : 0;
443 float cvar_type (string)
444 float CVAR_TYPEFLAG_EXISTS = 1;
445 float CVAR_TYPEFLAG_SAVED = 2;
446 float CVAR_TYPEFLAG_PRIVATE = 4;
447 float CVAR_TYPEFLAG_ENGINE = 8;
448 float CVAR_TYPEFLAG_HASDESCRIPTION = 16;
449 float CVAR_TYPEFLAG_READONLY = 32;
452 void VM_cvar_type (void)
454 char string[VM_STRINGTEMP_LENGTH];
458 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar);
459 VM_VarString(0, string, sizeof(string));
460 VM_CheckEmptyString(string);
461 cvar = Cvar_FindVar(string);
466 PRVM_G_FLOAT(OFS_RETURN) = 0;
467 return; // CVAR_TYPE_NONE
470 ret = 1; // CVAR_EXISTS
471 if(cvar->flags & CVAR_SAVE)
472 ret |= 2; // CVAR_TYPE_SAVED
473 if(cvar->flags & CVAR_PRIVATE)
474 ret |= 4; // CVAR_TYPE_PRIVATE
475 if(!(cvar->flags & CVAR_ALLOCATED))
476 ret |= 8; // CVAR_TYPE_ENGINE
477 if(cvar->description != cvar_dummy_description)
478 ret |= 16; // CVAR_TYPE_HASDESCRIPTION
479 if(cvar->flags & CVAR_READONLY)
480 ret |= 32; // CVAR_TYPE_READONLY
482 PRVM_G_FLOAT(OFS_RETURN) = ret;
489 const string VM_cvar_string (string, ...)
492 void VM_cvar_string(void)
494 char string[VM_STRINGTEMP_LENGTH];
495 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar_string);
496 VM_VarString(0, string, sizeof(string));
497 VM_CheckEmptyString(string);
498 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(PRVM_Cvar_ReadOk(string) ? Cvar_VariableString(string) : "");
503 ========================
506 const string VM_cvar_defstring (string, ...)
507 ========================
509 void VM_cvar_defstring (void)
511 char string[VM_STRINGTEMP_LENGTH];
512 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar_defstring);
513 VM_VarString(0, string, sizeof(string));
514 VM_CheckEmptyString(string);
515 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(Cvar_VariableDefString(string));
519 ========================
522 const string VM_cvar_description (string, ...)
523 ========================
525 void VM_cvar_description (void)
527 char string[VM_STRINGTEMP_LENGTH];
528 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar_description);
529 VM_VarString(0, string, sizeof(string));
530 VM_CheckEmptyString(string);
531 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(Cvar_VariableDescription(string));
537 void cvar_set (string,string, ...)
540 void VM_cvar_set (void)
543 char string[VM_STRINGTEMP_LENGTH];
544 VM_SAFEPARMCOUNTRANGE(2,8,VM_cvar_set);
545 VM_VarString(1, string, sizeof(string));
546 name = PRVM_G_STRING(OFS_PARM0);
547 VM_CheckEmptyString(name);
548 Cvar_Set(name, string);
558 void VM_dprint (void)
560 char string[VM_STRINGTEMP_LENGTH];
561 VM_SAFEPARMCOUNTRANGE(1, 8, VM_dprint);
562 if (developer.integer)
564 VM_VarString(0, string, sizeof(string));
566 Con_Printf("%s", string);
568 Con_Printf("%s: %s", PRVM_NAME, string);
586 VM_SAFEPARMCOUNT(1, VM_ftos);
588 v = PRVM_G_FLOAT(OFS_PARM0);
590 if ((float)((int)v) == v)
591 dpsnprintf(s, sizeof(s), "%i", (int)v);
593 dpsnprintf(s, sizeof(s), "%f", v);
594 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(s);
609 VM_SAFEPARMCOUNT(1,VM_fabs);
611 v = PRVM_G_FLOAT(OFS_PARM0);
612 PRVM_G_FLOAT(OFS_RETURN) = fabs(v);
627 VM_SAFEPARMCOUNT(1,VM_vtos);
629 dpsnprintf (s, sizeof(s), "'%5.1f %5.1f %5.1f'", PRVM_G_VECTOR(OFS_PARM0)[0], PRVM_G_VECTOR(OFS_PARM0)[1], PRVM_G_VECTOR(OFS_PARM0)[2]);
630 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(s);
645 VM_SAFEPARMCOUNT(1, VM_etos);
647 dpsnprintf (s, sizeof(s), "entity %i", PRVM_G_EDICTNUM(OFS_PARM0));
648 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(s);
655 float stof(...[string])
660 char string[VM_STRINGTEMP_LENGTH];
661 VM_SAFEPARMCOUNTRANGE(1, 8, VM_stof);
662 VM_VarString(0, string, sizeof(string));
663 PRVM_G_FLOAT(OFS_RETURN) = atof(string);
667 ========================
671 ========================
675 VM_SAFEPARMCOUNT(1, VM_itof);
676 PRVM_G_FLOAT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
680 ========================
683 entity ftoe(float num)
684 ========================
689 VM_SAFEPARMCOUNT(1, VM_ftoe);
691 ent = (int)PRVM_G_FLOAT(OFS_PARM0);
692 if (ent < 0 || ent >= MAX_EDICTS || PRVM_PROG_TO_EDICT(ent)->priv.required->free)
693 ent = 0; // return world instead of a free or invalid entity
695 PRVM_G_INT(OFS_RETURN) = ent;
699 ========================
702 float etof(entity ent)
703 ========================
707 VM_SAFEPARMCOUNT(1, VM_etof);
708 PRVM_G_FLOAT(OFS_RETURN) = PRVM_G_EDICTNUM(OFS_PARM0);
715 string strftime(float uselocaltime, string[, string ...])
718 void VM_strftime(void)
727 char fmt[VM_STRINGTEMP_LENGTH];
728 char result[VM_STRINGTEMP_LENGTH];
729 VM_SAFEPARMCOUNTRANGE(2, 8, VM_strftime);
730 VM_VarString(1, fmt, sizeof(fmt));
733 if (PRVM_G_FLOAT(OFS_PARM0))
734 tmresult = localtime_s(&tm, &t);
736 tmresult = gmtime_s(&tm, &t);
739 if (PRVM_G_FLOAT(OFS_PARM0))
746 PRVM_G_INT(OFS_RETURN) = 0;
750 strftime(result, sizeof(result), fmt, &tm);
752 strftime(result, sizeof(result), fmt, tm);
754 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(result);
768 VM_SAFEPARMCOUNT(0, VM_spawn);
769 prog->xfunction->builtinsprofile += 20;
770 ed = PRVM_ED_Alloc();
782 void VM_remove (void)
785 prog->xfunction->builtinsprofile += 20;
787 VM_SAFEPARMCOUNT(1, VM_remove);
789 ed = PRVM_G_EDICT(OFS_PARM0);
790 if( PRVM_NUM_FOR_EDICT(ed) <= prog->reserved_edicts )
792 if (developer.integer >= 1)
793 VM_Warning( "VM_remove: tried to remove the null entity or a reserved entity!\n" );
795 else if( ed->priv.required->free )
797 if (developer.integer >= 1)
798 VM_Warning( "VM_remove: tried to remove an already freed entity!\n" );
808 entity find(entity start, .string field, string match)
819 VM_SAFEPARMCOUNT(3,VM_find);
821 e = PRVM_G_EDICTNUM(OFS_PARM0);
822 f = PRVM_G_INT(OFS_PARM1);
823 s = PRVM_G_STRING(OFS_PARM2);
825 // LordHavoc: apparently BloodMage does a find(world, weaponmodel, "") and
826 // expects it to find all the monsters, so we must be careful to support
829 for (e++ ; e < prog->num_edicts ; e++)
831 prog->xfunction->builtinsprofile++;
832 ed = PRVM_EDICT_NUM(e);
833 if (ed->priv.required->free)
835 t = PRVM_E_STRING(ed,f);
845 VM_RETURN_EDICT(prog->edicts);
852 entity findfloat(entity start, .float field, float match)
853 entity findentity(entity start, .entity field, entity match)
856 // LordHavoc: added this for searching float, int, and entity reference fields
857 void VM_findfloat (void)
864 VM_SAFEPARMCOUNT(3,VM_findfloat);
866 e = PRVM_G_EDICTNUM(OFS_PARM0);
867 f = PRVM_G_INT(OFS_PARM1);
868 s = PRVM_G_FLOAT(OFS_PARM2);
870 for (e++ ; e < prog->num_edicts ; e++)
872 prog->xfunction->builtinsprofile++;
873 ed = PRVM_EDICT_NUM(e);
874 if (ed->priv.required->free)
876 if (PRVM_E_FLOAT(ed,f) == s)
883 VM_RETURN_EDICT(prog->edicts);
890 entity findchain(.string field, string match)
893 // chained search for strings in entity fields
894 // entity(.string field, string match) findchain = #402;
895 void VM_findchain (void)
900 prvm_edict_t *ent, *chain;
903 VM_SAFEPARMCOUNTRANGE(2,3,VM_findchain);
906 chainfield = PRVM_G_INT(OFS_PARM2);
908 chainfield = prog->fieldoffsets.chain;
910 PRVM_ERROR("VM_findchain: %s doesnt have the specified chain field !", PRVM_NAME);
912 chain = prog->edicts;
914 f = PRVM_G_INT(OFS_PARM0);
915 s = PRVM_G_STRING(OFS_PARM1);
917 // LordHavoc: apparently BloodMage does a find(world, weaponmodel, "") and
918 // expects it to find all the monsters, so we must be careful to support
921 ent = PRVM_NEXT_EDICT(prog->edicts);
922 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
924 prog->xfunction->builtinsprofile++;
925 if (ent->priv.required->free)
927 t = PRVM_E_STRING(ent,f);
933 PRVM_EDICTFIELDVALUE(ent,chainfield)->edict = PRVM_NUM_FOR_EDICT(chain);
937 VM_RETURN_EDICT(chain);
944 entity findchainfloat(.string field, float match)
945 entity findchainentity(.string field, entity match)
948 // LordHavoc: chained search for float, int, and entity reference fields
949 // entity(.string field, float match) findchainfloat = #403;
950 void VM_findchainfloat (void)
955 prvm_edict_t *ent, *chain;
958 VM_SAFEPARMCOUNTRANGE(2, 3, VM_findchainfloat);
961 chainfield = PRVM_G_INT(OFS_PARM2);
963 chainfield = prog->fieldoffsets.chain;
965 PRVM_ERROR("VM_findchain: %s doesnt have the specified chain field !", PRVM_NAME);
967 chain = (prvm_edict_t *)prog->edicts;
969 f = PRVM_G_INT(OFS_PARM0);
970 s = PRVM_G_FLOAT(OFS_PARM1);
972 ent = PRVM_NEXT_EDICT(prog->edicts);
973 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
975 prog->xfunction->builtinsprofile++;
976 if (ent->priv.required->free)
978 if (PRVM_E_FLOAT(ent,f) != s)
981 PRVM_EDICTFIELDVALUE(ent,chainfield)->edict = PRVM_EDICT_TO_PROG(chain);
985 VM_RETURN_EDICT(chain);
989 ========================
992 entity findflags(entity start, .float field, float match)
993 ========================
995 // LordHavoc: search for flags in float fields
996 void VM_findflags (void)
1003 VM_SAFEPARMCOUNT(3, VM_findflags);
1006 e = PRVM_G_EDICTNUM(OFS_PARM0);
1007 f = PRVM_G_INT(OFS_PARM1);
1008 s = (int)PRVM_G_FLOAT(OFS_PARM2);
1010 for (e++ ; e < prog->num_edicts ; e++)
1012 prog->xfunction->builtinsprofile++;
1013 ed = PRVM_EDICT_NUM(e);
1014 if (ed->priv.required->free)
1016 if (!PRVM_E_FLOAT(ed,f))
1018 if ((int)PRVM_E_FLOAT(ed,f) & s)
1020 VM_RETURN_EDICT(ed);
1025 VM_RETURN_EDICT(prog->edicts);
1029 ========================
1032 entity findchainflags(.float field, float match)
1033 ========================
1035 // LordHavoc: chained search for flags in float fields
1036 void VM_findchainflags (void)
1041 prvm_edict_t *ent, *chain;
1044 VM_SAFEPARMCOUNTRANGE(2, 3, VM_findchainflags);
1047 chainfield = PRVM_G_INT(OFS_PARM2);
1049 chainfield = prog->fieldoffsets.chain;
1051 PRVM_ERROR("VM_findchain: %s doesnt have the specified chain field !", PRVM_NAME);
1053 chain = (prvm_edict_t *)prog->edicts;
1055 f = PRVM_G_INT(OFS_PARM0);
1056 s = (int)PRVM_G_FLOAT(OFS_PARM1);
1058 ent = PRVM_NEXT_EDICT(prog->edicts);
1059 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
1061 prog->xfunction->builtinsprofile++;
1062 if (ent->priv.required->free)
1064 if (!PRVM_E_FLOAT(ent,f))
1066 if (!((int)PRVM_E_FLOAT(ent,f) & s))
1069 PRVM_EDICTFIELDVALUE(ent,chainfield)->edict = PRVM_EDICT_TO_PROG(chain);
1073 VM_RETURN_EDICT(chain);
1080 string precache_sound (string sample)
1083 void VM_precache_sound (void)
1087 VM_SAFEPARMCOUNT(1, VM_precache_sound);
1089 s = PRVM_G_STRING(OFS_PARM0);
1090 PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
1091 VM_CheckEmptyString(s);
1093 if(snd_initialized.integer && !S_PrecacheSound(s, true, true))
1095 VM_Warning("VM_precache_sound: Failed to load %s for %s\n", s, PRVM_NAME);
1104 returns the same string as output
1106 does nothing, only used by qcc to build .pak archives
1109 void VM_precache_file (void)
1111 VM_SAFEPARMCOUNT(1,VM_precache_file);
1112 // precache_file is only used to copy files with qcc, it does nothing
1113 PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
1123 void VM_coredump (void)
1125 VM_SAFEPARMCOUNT(0,VM_coredump);
1127 Cbuf_AddText("prvm_edicts ");
1128 Cbuf_AddText(PRVM_NAME);
1139 void PRVM_StackTrace(void);
1140 void VM_stackdump (void)
1142 VM_SAFEPARMCOUNT(0, VM_stackdump);
1157 VM_SAFEPARMCOUNT(0, VM_crash);
1159 PRVM_ERROR("Crash called by %s",PRVM_NAME);
1169 void VM_traceon (void)
1171 VM_SAFEPARMCOUNT(0,VM_traceon);
1183 void VM_traceoff (void)
1185 VM_SAFEPARMCOUNT(0,VM_traceoff);
1187 prog->trace = false;
1197 void VM_eprint (void)
1199 VM_SAFEPARMCOUNT(1,VM_eprint);
1201 PRVM_ED_PrintNum (PRVM_G_EDICTNUM(OFS_PARM0), NULL);
1214 VM_SAFEPARMCOUNT(1,VM_rint);
1216 f = PRVM_G_FLOAT(OFS_PARM0);
1218 PRVM_G_FLOAT(OFS_RETURN) = floor(f + 0.5);
1220 PRVM_G_FLOAT(OFS_RETURN) = ceil(f - 0.5);
1230 void VM_floor (void)
1232 VM_SAFEPARMCOUNT(1,VM_floor);
1234 PRVM_G_FLOAT(OFS_RETURN) = floor(PRVM_G_FLOAT(OFS_PARM0));
1246 VM_SAFEPARMCOUNT(1,VM_ceil);
1248 PRVM_G_FLOAT(OFS_RETURN) = ceil(PRVM_G_FLOAT(OFS_PARM0));
1256 entity nextent(entity)
1259 void VM_nextent (void)
1264 VM_SAFEPARMCOUNT(1, VM_nextent);
1266 i = PRVM_G_EDICTNUM(OFS_PARM0);
1269 prog->xfunction->builtinsprofile++;
1271 if (i == prog->num_edicts)
1273 VM_RETURN_EDICT(prog->edicts);
1276 ent = PRVM_EDICT_NUM(i);
1277 if (!ent->priv.required->free)
1279 VM_RETURN_EDICT(ent);
1285 //=============================================================================
1292 changelevel(string map)
1295 void VM_changelevel (void)
1297 VM_SAFEPARMCOUNT(1, VM_changelevel);
1301 VM_Warning("VM_changelevel: game is not server (%s)\n", PRVM_NAME);
1305 // make sure we don't issue two changelevels
1306 if (svs.changelevel_issued)
1308 svs.changelevel_issued = true;
1310 Cbuf_AddText (va("changelevel %s\n",PRVM_G_STRING(OFS_PARM0)));
1322 VM_SAFEPARMCOUNT(1,VM_sin);
1323 PRVM_G_FLOAT(OFS_RETURN) = sin(PRVM_G_FLOAT(OFS_PARM0));
1334 VM_SAFEPARMCOUNT(1,VM_cos);
1335 PRVM_G_FLOAT(OFS_RETURN) = cos(PRVM_G_FLOAT(OFS_PARM0));
1347 VM_SAFEPARMCOUNT(1,VM_sqrt);
1348 PRVM_G_FLOAT(OFS_RETURN) = sqrt(PRVM_G_FLOAT(OFS_PARM0));
1360 VM_SAFEPARMCOUNT(1,VM_asin);
1361 PRVM_G_FLOAT(OFS_RETURN) = asin(PRVM_G_FLOAT(OFS_PARM0));
1372 VM_SAFEPARMCOUNT(1,VM_acos);
1373 PRVM_G_FLOAT(OFS_RETURN) = acos(PRVM_G_FLOAT(OFS_PARM0));
1384 VM_SAFEPARMCOUNT(1,VM_atan);
1385 PRVM_G_FLOAT(OFS_RETURN) = atan(PRVM_G_FLOAT(OFS_PARM0));
1391 float atan2(float,float)
1394 void VM_atan2 (void)
1396 VM_SAFEPARMCOUNT(2,VM_atan2);
1397 PRVM_G_FLOAT(OFS_RETURN) = atan2(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1408 VM_SAFEPARMCOUNT(1,VM_tan);
1409 PRVM_G_FLOAT(OFS_RETURN) = tan(PRVM_G_FLOAT(OFS_PARM0));
1416 Returns a vector of length < 1 and > 0
1421 void VM_randomvec (void)
1426 VM_SAFEPARMCOUNT(0, VM_randomvec);
1431 temp[0] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1432 temp[1] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1433 temp[2] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1435 while (DotProduct(temp, temp) >= 1);
1436 VectorCopy (temp, PRVM_G_VECTOR(OFS_RETURN));
1439 temp[0] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1440 temp[1] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1441 temp[2] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1442 // length returned always > 0
1443 length = (rand()&32766 + 1) * (1.0 / 32767.0) / VectorLength(temp);
1444 VectorScale(temp,length, temp);*/
1445 //VectorCopy(temp, PRVM_G_VECTOR(OFS_RETURN));
1448 //=============================================================================
1454 float registercvar (string name, string value[, float flags])
1457 void VM_registercvar (void)
1459 const char *name, *value;
1462 VM_SAFEPARMCOUNTRANGE(2, 3, VM_registercvar);
1464 name = PRVM_G_STRING(OFS_PARM0);
1465 value = PRVM_G_STRING(OFS_PARM1);
1466 flags = prog->argc >= 3 ? (int)PRVM_G_FLOAT(OFS_PARM2) : 0;
1467 PRVM_G_FLOAT(OFS_RETURN) = 0;
1469 if(flags > CVAR_MAXFLAGSVAL)
1472 // first check to see if it has already been defined
1473 if (Cvar_FindVar (name))
1476 // check for overlap with a command
1477 if (Cmd_Exists (name))
1479 VM_Warning("VM_registercvar: %s is a command\n", name);
1483 Cvar_Get(name, value, flags, NULL);
1485 PRVM_G_FLOAT(OFS_RETURN) = 1; // success
1493 returns the minimum of two supplied floats
1495 float min(float a, float b, ...[float])
1500 VM_SAFEPARMCOUNTRANGE(2, 8, VM_min);
1501 // LordHavoc: 3+ argument enhancement suggested by FrikaC
1502 if (prog->argc >= 3)
1505 float f = PRVM_G_FLOAT(OFS_PARM0);
1506 for (i = 1;i < prog->argc;i++)
1507 if (f > PRVM_G_FLOAT((OFS_PARM0+i*3)))
1508 f = PRVM_G_FLOAT((OFS_PARM0+i*3));
1509 PRVM_G_FLOAT(OFS_RETURN) = f;
1512 PRVM_G_FLOAT(OFS_RETURN) = min(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1519 returns the maximum of two supplied floats
1521 float max(float a, float b, ...[float])
1526 VM_SAFEPARMCOUNTRANGE(2, 8, VM_max);
1527 // LordHavoc: 3+ argument enhancement suggested by FrikaC
1528 if (prog->argc >= 3)
1531 float f = PRVM_G_FLOAT(OFS_PARM0);
1532 for (i = 1;i < prog->argc;i++)
1533 if (f < PRVM_G_FLOAT((OFS_PARM0+i*3)))
1534 f = PRVM_G_FLOAT((OFS_PARM0+i*3));
1535 PRVM_G_FLOAT(OFS_RETURN) = f;
1538 PRVM_G_FLOAT(OFS_RETURN) = max(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1545 returns number bounded by supplied range
1547 float bound(float min, float value, float max)
1550 void VM_bound (void)
1552 VM_SAFEPARMCOUNT(3,VM_bound);
1553 PRVM_G_FLOAT(OFS_RETURN) = bound(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1), PRVM_G_FLOAT(OFS_PARM2));
1560 returns a raised to power b
1562 float pow(float a, float b)
1567 VM_SAFEPARMCOUNT(2,VM_pow);
1568 PRVM_G_FLOAT(OFS_RETURN) = pow(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1571 void VM_Files_Init(void)
1574 for (i = 0;i < PRVM_MAX_OPENFILES;i++)
1575 prog->openfiles[i] = NULL;
1578 void VM_Files_CloseAll(void)
1581 for (i = 0;i < PRVM_MAX_OPENFILES;i++)
1583 if (prog->openfiles[i])
1584 FS_Close(prog->openfiles[i]);
1585 prog->openfiles[i] = NULL;
1589 static qfile_t *VM_GetFileHandle( int index )
1591 if (index < 0 || index >= PRVM_MAX_OPENFILES)
1593 Con_Printf("VM_GetFileHandle: invalid file handle %i used in %s\n", index, PRVM_NAME);
1596 if (prog->openfiles[index] == NULL)
1598 Con_Printf("VM_GetFileHandle: no such file handle %i (or file has been closed) in %s\n", index, PRVM_NAME);
1601 return prog->openfiles[index];
1608 float fopen(string filename, float mode)
1611 // float(string filename, float mode) fopen = #110;
1612 // opens a file inside quake/gamedir/data/ (mode is FILE_READ, FILE_APPEND, or FILE_WRITE),
1613 // returns fhandle >= 0 if successful, or fhandle < 0 if unable to open file for any reason
1617 const char *modestring, *filename;
1619 VM_SAFEPARMCOUNT(2,VM_fopen);
1621 for (filenum = 0;filenum < PRVM_MAX_OPENFILES;filenum++)
1622 if (prog->openfiles[filenum] == NULL)
1624 if (filenum >= PRVM_MAX_OPENFILES)
1626 PRVM_G_FLOAT(OFS_RETURN) = -2;
1627 VM_Warning("VM_fopen: %s ran out of file handles (%i)\n", PRVM_NAME, PRVM_MAX_OPENFILES);
1630 filename = PRVM_G_STRING(OFS_PARM0);
1631 mode = (int)PRVM_G_FLOAT(OFS_PARM1);
1634 case 0: // FILE_READ
1636 prog->openfiles[filenum] = FS_OpenVirtualFile(va("data/%s", filename), false);
1637 if (prog->openfiles[filenum] == NULL)
1638 prog->openfiles[filenum] = FS_OpenVirtualFile(va("%s", filename), false);
1640 case 1: // FILE_APPEND
1642 prog->openfiles[filenum] = FS_OpenRealFile(va("data/%s", filename), modestring, false);
1644 case 2: // FILE_WRITE
1646 prog->openfiles[filenum] = FS_OpenRealFile(va("data/%s", filename), modestring, false);
1649 PRVM_G_FLOAT(OFS_RETURN) = -3;
1650 VM_Warning("VM_fopen: %s: no such mode %i (valid: 0 = read, 1 = append, 2 = write)\n", PRVM_NAME, mode);
1654 if (prog->openfiles[filenum] == NULL)
1656 PRVM_G_FLOAT(OFS_RETURN) = -1;
1657 if (developer.integer >= 100)
1658 VM_Warning("VM_fopen: %s: %s mode %s failed\n", PRVM_NAME, filename, modestring);
1662 PRVM_G_FLOAT(OFS_RETURN) = filenum;
1663 if (developer.integer >= 100)
1664 Con_Printf("VM_fopen: %s: %s mode %s opened as #%i\n", PRVM_NAME, filename, modestring, filenum);
1665 prog->openfiles_origin[filenum] = PRVM_AllocationOrigin();
1673 fclose(float fhandle)
1676 //void(float fhandle) fclose = #111; // closes a file
1677 void VM_fclose(void)
1681 VM_SAFEPARMCOUNT(1,VM_fclose);
1683 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1684 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
1686 VM_Warning("VM_fclose: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1689 if (prog->openfiles[filenum] == NULL)
1691 VM_Warning("VM_fclose: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1694 FS_Close(prog->openfiles[filenum]);
1695 prog->openfiles[filenum] = NULL;
1696 if(prog->openfiles_origin[filenum])
1697 PRVM_Free((char *)prog->openfiles_origin[filenum]);
1698 if (developer.integer >= 100)
1699 Con_Printf("VM_fclose: %s: #%i closed\n", PRVM_NAME, filenum);
1706 string fgets(float fhandle)
1709 //string(float fhandle) fgets = #112; // reads a line of text from the file and returns as a tempstring
1713 char string[VM_STRINGTEMP_LENGTH];
1716 VM_SAFEPARMCOUNT(1,VM_fgets);
1718 // set the return value regardless of any possible errors
1719 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
1721 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1722 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
1724 VM_Warning("VM_fgets: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1727 if (prog->openfiles[filenum] == NULL)
1729 VM_Warning("VM_fgets: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1735 c = FS_Getc(prog->openfiles[filenum]);
1736 if (c == '\r' || c == '\n' || c < 0)
1738 if (end < VM_STRINGTEMP_LENGTH - 1)
1742 // remove \n following \r
1745 c = FS_Getc(prog->openfiles[filenum]);
1747 FS_UnGetc(prog->openfiles[filenum], (unsigned char)c);
1749 if (developer.integer >= 100)
1750 Con_Printf("fgets: %s: %s\n", PRVM_NAME, string);
1752 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string);
1759 fputs(float fhandle, string s)
1762 //void(float fhandle, string s) fputs = #113; // writes a line of text to the end of the file
1766 char string[VM_STRINGTEMP_LENGTH];
1769 VM_SAFEPARMCOUNT(2,VM_fputs);
1771 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1772 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
1774 VM_Warning("VM_fputs: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1777 if (prog->openfiles[filenum] == NULL)
1779 VM_Warning("VM_fputs: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1782 VM_VarString(1, string, sizeof(string));
1783 if ((stringlength = (int)strlen(string)))
1784 FS_Write(prog->openfiles[filenum], string, stringlength);
1785 if (developer.integer >= 100)
1786 Con_Printf("fputs: %s: %s\n", PRVM_NAME, string);
1793 writetofile(float fhandle, entity ent)
1796 void VM_writetofile(void)
1801 VM_SAFEPARMCOUNT(2, VM_writetofile);
1803 file = VM_GetFileHandle( (int)PRVM_G_FLOAT(OFS_PARM0) );
1806 VM_Warning("VM_writetofile: invalid or closed file handle\n");
1810 ent = PRVM_G_EDICT(OFS_PARM1);
1811 if(ent->priv.required->free)
1813 VM_Warning("VM_writetofile: %s: entity %i is free !\n", PRVM_NAME, PRVM_NUM_FOR_EDICT(ent));
1817 PRVM_ED_Write (file, ent);
1820 // KrimZon - DP_QC_ENTITYDATA
1825 float() numentityfields
1826 Return the number of entity fields - NOT offsets
1829 void VM_numentityfields(void)
1831 PRVM_G_FLOAT(OFS_RETURN) = prog->progs->numfielddefs;
1834 // KrimZon - DP_QC_ENTITYDATA
1839 string(float fieldnum) entityfieldname
1840 Return name of the specified field as a string, or empty if the field is invalid (warning)
1843 void VM_entityfieldname(void)
1846 int i = (int)PRVM_G_FLOAT(OFS_PARM0);
1848 if (i < 0 || i >= prog->progs->numfielddefs)
1850 VM_Warning("VM_entityfieldname: %s: field index out of bounds\n", PRVM_NAME);
1851 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString("");
1855 d = &prog->fielddefs[i];
1856 PRVM_G_INT(OFS_RETURN) = d->s_name; // presuming that s_name points to a string already
1859 // KrimZon - DP_QC_ENTITYDATA
1864 float(float fieldnum) entityfieldtype
1867 void VM_entityfieldtype(void)
1870 int i = (int)PRVM_G_FLOAT(OFS_PARM0);
1872 if (i < 0 || i >= prog->progs->numfielddefs)
1874 VM_Warning("VM_entityfieldtype: %s: field index out of bounds\n", PRVM_NAME);
1875 PRVM_G_FLOAT(OFS_RETURN) = -1.0;
1879 d = &prog->fielddefs[i];
1880 PRVM_G_FLOAT(OFS_RETURN) = (float)d->type;
1883 // KrimZon - DP_QC_ENTITYDATA
1886 VM_getentityfieldstring
1888 string(float fieldnum, entity ent) getentityfieldstring
1891 void VM_getentityfieldstring(void)
1893 // put the data into a string
1898 int i = (int)PRVM_G_FLOAT(OFS_PARM0);
1900 if (i < 0 || i >= prog->progs->numfielddefs)
1902 VM_Warning("VM_entityfielddata: %s: field index out of bounds\n", PRVM_NAME);
1903 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString("");
1907 d = &prog->fielddefs[i];
1910 ent = PRVM_G_EDICT(OFS_PARM1);
1911 if(ent->priv.required->free)
1913 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString("");
1914 VM_Warning("VM_entityfielddata: %s: entity %i is free !\n", PRVM_NAME, PRVM_NUM_FOR_EDICT(ent));
1917 v = (int *)((char *)ent->fields.vp + d->ofs*4);
1919 // if it's 0 or blank, return an empty string
1920 type = d->type & ~DEF_SAVEGLOBAL;
1921 for (j=0 ; j<prvm_type_size[type] ; j++)
1924 if (j == prvm_type_size[type])
1926 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString("");
1930 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(PRVM_UglyValueString((etype_t)d->type, (prvm_eval_t *)v));
1933 // KrimZon - DP_QC_ENTITYDATA
1936 VM_putentityfieldstring
1938 float(float fieldnum, entity ent, string s) putentityfieldstring
1941 void VM_putentityfieldstring(void)
1945 int i = (int)PRVM_G_FLOAT(OFS_PARM0);
1947 if (i < 0 || i >= prog->progs->numfielddefs)
1949 VM_Warning("VM_entityfielddata: %s: field index out of bounds\n", PRVM_NAME);
1950 PRVM_G_FLOAT(OFS_RETURN) = 0.0f;
1954 d = &prog->fielddefs[i];
1957 ent = PRVM_G_EDICT(OFS_PARM1);
1958 if(ent->priv.required->free)
1960 VM_Warning("VM_entityfielddata: %s: entity %i is free !\n", PRVM_NAME, PRVM_NUM_FOR_EDICT(ent));
1961 PRVM_G_FLOAT(OFS_RETURN) = 0.0f;
1965 // parse the string into the value
1966 PRVM_G_FLOAT(OFS_RETURN) = ( PRVM_ED_ParseEpair(ent, d, PRVM_G_STRING(OFS_PARM2), false) ) ? 1.0f : 0.0f;
1973 float strlen(string s)
1976 //float(string s) strlen = #114; // returns how many characters are in a string
1977 void VM_strlen(void)
1979 VM_SAFEPARMCOUNT(1,VM_strlen);
1981 PRVM_G_FLOAT(OFS_RETURN) = strlen(PRVM_G_STRING(OFS_PARM0));
1984 // DRESK - Decolorized String
1989 string strdecolorize(string s)
1992 // string (string s) strdecolorize = #472; // returns the passed in string with color codes stripped
1993 void VM_strdecolorize(void)
1995 char szNewString[VM_STRINGTEMP_LENGTH];
1996 const char *szString;
1999 VM_SAFEPARMCOUNT(1,VM_strdecolorize);
2000 szString = PRVM_G_STRING(OFS_PARM0);
2001 COM_StringDecolorize(szString, 0, szNewString, sizeof(szNewString), TRUE);
2002 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(szNewString);
2005 // DRESK - String Length (not counting color codes)
2010 float strlennocol(string s)
2013 // float(string s) strlennocol = #471; // returns how many characters are in a string not including color codes
2014 // For example, ^2Dresk returns a length of 5
2015 void VM_strlennocol(void)
2017 const char *szString;
2020 VM_SAFEPARMCOUNT(1,VM_strlennocol);
2022 szString = PRVM_G_STRING(OFS_PARM0);
2024 nCnt = COM_StringLengthNoColors(szString, 0, NULL);
2026 PRVM_G_FLOAT(OFS_RETURN) = nCnt;
2029 // DRESK - String to Uppercase and Lowercase
2034 string strtolower(string s)
2037 // string (string s) strtolower = #480; // returns passed in string in lowercase form
2038 void VM_strtolower(void)
2040 char szNewString[VM_STRINGTEMP_LENGTH];
2041 const char *szString;
2044 VM_SAFEPARMCOUNT(1,VM_strtolower);
2045 szString = PRVM_G_STRING(OFS_PARM0);
2047 COM_ToLowerString(szString, szNewString, sizeof(szNewString) );
2049 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(szNewString);
2056 string strtoupper(string s)
2059 // string (string s) strtoupper = #481; // returns passed in string in uppercase form
2060 void VM_strtoupper(void)
2062 char szNewString[VM_STRINGTEMP_LENGTH];
2063 const char *szString;
2066 VM_SAFEPARMCOUNT(1,VM_strtoupper);
2067 szString = PRVM_G_STRING(OFS_PARM0);
2069 COM_ToUpperString(szString, szNewString, sizeof(szNewString) );
2071 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(szNewString);
2078 string strcat(string,string,...[string])
2081 //string(string s1, string s2) strcat = #115;
2082 // concatenates two strings (for example "abc", "def" would return "abcdef")
2083 // and returns as a tempstring
2084 void VM_strcat(void)
2086 char s[VM_STRINGTEMP_LENGTH];
2087 VM_SAFEPARMCOUNTRANGE(1, 8, VM_strcat);
2089 VM_VarString(0, s, sizeof(s));
2090 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(s);
2097 string substring(string s, float start, float length)
2100 // string(string s, float start, float length) substring = #116;
2101 // returns a section of a string as a tempstring
2102 void VM_substring(void)
2104 int start, length, slength, maxlen;
2106 char string[VM_STRINGTEMP_LENGTH];
2108 VM_SAFEPARMCOUNT(3,VM_substring);
2110 s = PRVM_G_STRING(OFS_PARM0);
2111 start = (int)PRVM_G_FLOAT(OFS_PARM1);
2112 length = (int)PRVM_G_FLOAT(OFS_PARM2);
2113 slength = strlen(s);
2115 if (start < 0) // FTE_STRINGS feature
2117 start = bound(0, start, slength);
2119 if (length < 0) // FTE_STRINGS feature
2120 length += slength - start + 1;
2121 maxlen = min((int)sizeof(string) - 1, slength - start);
2122 length = bound(0, length, maxlen);
2124 memcpy(string, s + start, length);
2126 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string);
2133 string(string search, string replace, string subject) strreplace = #484;
2136 // replaces all occurrences of search with replace in the string subject, and returns the result
2137 void VM_strreplace(void)
2140 const char *search, *replace, *subject;
2141 char string[VM_STRINGTEMP_LENGTH];
2142 int search_len, replace_len, subject_len;
2144 VM_SAFEPARMCOUNT(3,VM_strreplace);
2146 search = PRVM_G_STRING(OFS_PARM0);
2147 replace = PRVM_G_STRING(OFS_PARM1);
2148 subject = PRVM_G_STRING(OFS_PARM2);
2150 search_len = (int)strlen(search);
2151 replace_len = (int)strlen(replace);
2152 subject_len = (int)strlen(subject);
2155 for (i = 0; i < subject_len; i++)
2157 for (j = 0; j < search_len && i+j < subject_len; j++)
2158 if (subject[i+j] != search[j])
2160 if (j == search_len || i+j == subject_len)
2162 // found it at offset 'i'
2163 for (j = 0; j < replace_len && si < (int)sizeof(string) - 1; j++)
2164 string[si++] = replace[j];
2165 i += search_len - 1;
2170 if (si < (int)sizeof(string) - 1)
2171 string[si++] = subject[i];
2176 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string);
2183 string(string search, string replace, string subject) strireplace = #485;
2186 // case-insensitive version of strreplace
2187 void VM_strireplace(void)
2190 const char *search, *replace, *subject;
2191 char string[VM_STRINGTEMP_LENGTH];
2192 int search_len, replace_len, subject_len;
2194 VM_SAFEPARMCOUNT(3,VM_strreplace);
2196 search = PRVM_G_STRING(OFS_PARM0);
2197 replace = PRVM_G_STRING(OFS_PARM1);
2198 subject = PRVM_G_STRING(OFS_PARM2);
2200 search_len = (int)strlen(search);
2201 replace_len = (int)strlen(replace);
2202 subject_len = (int)strlen(subject);
2205 for (i = 0; i < subject_len; i++)
2207 for (j = 0; j < search_len && i+j < subject_len; j++)
2208 if (tolower(subject[i+j]) != tolower(search[j]))
2210 if (j == search_len || i+j == subject_len)
2212 // found it at offset 'i'
2213 for (j = 0; j < replace_len && si < (int)sizeof(string) - 1; j++)
2214 string[si++] = replace[j];
2215 i += search_len - 1;
2220 if (si < (int)sizeof(string) - 1)
2221 string[si++] = subject[i];
2226 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string);
2233 vector stov(string s)
2236 //vector(string s) stov = #117; // returns vector value from a string
2239 char string[VM_STRINGTEMP_LENGTH];
2241 VM_SAFEPARMCOUNT(1,VM_stov);
2243 VM_VarString(0, string, sizeof(string));
2244 Math_atov(string, PRVM_G_VECTOR(OFS_RETURN));
2251 string strzone(string s)
2254 //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)
2255 void VM_strzone(void)
2258 char string[VM_STRINGTEMP_LENGTH];
2261 VM_SAFEPARMCOUNT(1,VM_strzone);
2263 VM_VarString(0, string, sizeof(string));
2264 alloclen = strlen(string) + 1;
2265 PRVM_G_INT(OFS_RETURN) = PRVM_AllocString(alloclen, &out);
2266 memcpy(out, string, alloclen);
2276 //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!!!)
2277 void VM_strunzone(void)
2279 VM_SAFEPARMCOUNT(1,VM_strunzone);
2280 PRVM_FreeString(PRVM_G_INT(OFS_PARM0));
2285 VM_command (used by client and menu)
2287 clientcommand(float client, string s) (for client and menu)
2290 //void(entity e, string s) clientcommand = #440; // executes a command string as if it came from the specified client
2291 //this function originally written by KrimZon, made shorter by LordHavoc
2292 void VM_clcommand (void)
2294 client_t *temp_client;
2297 VM_SAFEPARMCOUNT(2,VM_clcommand);
2299 i = (int)PRVM_G_FLOAT(OFS_PARM0);
2300 if (!sv.active || i < 0 || i >= svs.maxclients || !svs.clients[i].active)
2302 VM_Warning("VM_clientcommand: %s: invalid client/server is not active !\n", PRVM_NAME);
2306 temp_client = host_client;
2307 host_client = svs.clients + i;
2308 Cmd_ExecuteString (PRVM_G_STRING(OFS_PARM1), src_client);
2309 host_client = temp_client;
2317 float tokenize(string s)
2320 //float(string s) tokenize = #441; // takes apart a string into individal words (access them with argv), returns how many
2321 //this function originally written by KrimZon, made shorter by LordHavoc
2322 //20040203: rewritten by LordHavoc (no longer uses allocations)
2323 static int num_tokens = 0;
2324 static int tokens[VM_STRINGTEMP_LENGTH / 2];
2325 static int tokens_startpos[VM_STRINGTEMP_LENGTH / 2];
2326 static int tokens_endpos[VM_STRINGTEMP_LENGTH / 2];
2327 static char tokenize_string[VM_STRINGTEMP_LENGTH];
2328 void VM_tokenize (void)
2332 VM_SAFEPARMCOUNT(1,VM_tokenize);
2334 strlcpy(tokenize_string, PRVM_G_STRING(OFS_PARM0), sizeof(tokenize_string));
2335 p = tokenize_string;
2340 if (num_tokens >= (int)(sizeof(tokens)/sizeof(tokens[0])))
2343 // skip whitespace here to find token start pos
2344 while(*p && ISWHITESPACE(*p))
2347 tokens_startpos[num_tokens] = p - tokenize_string;
2348 if(!COM_ParseToken_VM_Tokenize(&p, false))
2350 tokens_endpos[num_tokens] = p - tokenize_string;
2351 tokens[num_tokens] = PRVM_SetTempString(com_token);
2355 PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
2358 //float(string s) tokenize = #514; // takes apart a string into individal words (access them with argv), returns how many
2359 void VM_tokenize_console (void)
2363 VM_SAFEPARMCOUNT(1,VM_tokenize);
2365 strlcpy(tokenize_string, PRVM_G_STRING(OFS_PARM0), sizeof(tokenize_string));
2366 p = tokenize_string;
2371 if (num_tokens >= (int)(sizeof(tokens)/sizeof(tokens[0])))
2374 // skip whitespace here to find token start pos
2375 while(*p && ISWHITESPACE(*p))
2378 tokens_startpos[num_tokens] = p - tokenize_string;
2379 if(!COM_ParseToken_Console(&p))
2381 tokens_endpos[num_tokens] = p - tokenize_string;
2382 tokens[num_tokens] = PRVM_SetTempString(com_token);
2386 PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
2391 VM_tokenizebyseparator
2393 float tokenizebyseparator(string s, string separator1, ...)
2396 //float(string s, string separator1, ...) tokenizebyseparator = #479; // takes apart a string into individal words (access them with argv), returns how many
2397 //this function returns the token preceding each instance of a separator (of
2398 //which there can be multiple), and the text following the last separator
2399 //useful for parsing certain kinds of data like IP addresses
2401 //numnumbers = tokenizebyseparator("10.1.2.3", ".");
2402 //returns 4 and the tokens "10" "1" "2" "3".
2403 void VM_tokenizebyseparator (void)
2407 int separatorlen[7];
2408 const char *separators[7];
2411 char tokentext[MAX_INPUTLINE];
2413 VM_SAFEPARMCOUNTRANGE(2, 8,VM_tokenizebyseparator);
2415 strlcpy(tokenize_string, PRVM_G_STRING(OFS_PARM0), sizeof(tokenize_string));
2416 p = tokenize_string;
2419 for (j = 1;j < prog->argc;j++)
2421 // skip any blank separator strings
2422 const char *s = PRVM_G_STRING(OFS_PARM0+j*3);
2425 separators[numseparators] = s;
2426 separatorlen[numseparators] = strlen(s);
2433 while (num_tokens < (int)(sizeof(tokens)/sizeof(tokens[0])))
2435 token = tokentext + j;
2436 tokens_startpos[num_tokens] = p - tokenize_string;
2440 for (k = 0;k < numseparators;k++)
2442 if (!strncmp(p, separators[k], separatorlen[k]))
2444 p += separatorlen[k];
2448 if (k < numseparators)
2450 if (j < (int)sizeof(tokentext)-1)
2451 tokentext[j++] = *p;
2455 tokens_endpos[num_tokens] = p0 - tokenize_string;
2456 if (j >= (int)sizeof(tokentext))
2459 tokens[num_tokens++] = PRVM_SetTempString(token);
2464 PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
2467 //string(float n) argv = #442; // returns a word from the tokenized string (returns nothing for an invalid index)
2468 //this function originally written by KrimZon, made shorter by LordHavoc
2473 VM_SAFEPARMCOUNT(1,VM_argv);
2475 token_num = (int)PRVM_G_FLOAT(OFS_PARM0);
2478 token_num += num_tokens;
2480 if (token_num >= 0 && token_num < num_tokens)
2481 PRVM_G_INT(OFS_RETURN) = tokens[token_num];
2483 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
2486 //float(float n) argv_start_index = #515; // returns the start index of a token
2487 void VM_argv_start_index (void)
2491 VM_SAFEPARMCOUNT(1,VM_argv);
2493 token_num = (int)PRVM_G_FLOAT(OFS_PARM0);
2496 token_num += num_tokens;
2498 if (token_num >= 0 && token_num < num_tokens)
2499 PRVM_G_FLOAT(OFS_RETURN) = tokens_startpos[token_num];
2501 PRVM_G_FLOAT(OFS_RETURN) = -1;
2504 //float(float n) argv_end_index = #516; // returns the end index of a token
2505 void VM_argv_end_index (void)
2509 VM_SAFEPARMCOUNT(1,VM_argv);
2511 token_num = (int)PRVM_G_FLOAT(OFS_PARM0);
2514 token_num += num_tokens;
2516 if (token_num >= 0 && token_num < num_tokens)
2517 PRVM_G_FLOAT(OFS_RETURN) = tokens_endpos[token_num];
2519 PRVM_G_FLOAT(OFS_RETURN) = -1;
2529 void VM_isserver(void)
2531 VM_SAFEPARMCOUNT(0,VM_serverstate);
2533 PRVM_G_FLOAT(OFS_RETURN) = sv.active && (svs.maxclients > 1 || cls.state == ca_dedicated);
2543 void VM_clientcount(void)
2545 VM_SAFEPARMCOUNT(0,VM_clientcount);
2547 PRVM_G_FLOAT(OFS_RETURN) = svs.maxclients;
2557 void VM_clientstate(void)
2559 VM_SAFEPARMCOUNT(0,VM_clientstate);
2562 switch( cls.state ) {
2563 case ca_uninitialized:
2565 PRVM_G_FLOAT(OFS_RETURN) = 0;
2567 case ca_disconnected:
2568 PRVM_G_FLOAT(OFS_RETURN) = 1;
2571 PRVM_G_FLOAT(OFS_RETURN) = 2;
2574 // should never be reached!
2583 float getostype(void)
2585 */ // not used at the moment -> not included in the common list
2586 void VM_getostype(void)
2588 VM_SAFEPARMCOUNT(0,VM_getostype);
2593 OS_MAC - not supported
2597 PRVM_G_FLOAT(OFS_RETURN) = 0;
2598 #elif defined(MACOSX)
2599 PRVM_G_FLOAT(OFS_RETURN) = 2;
2601 PRVM_G_FLOAT(OFS_RETURN) = 1;
2612 extern double host_starttime;
2613 float CDAudio_GetPosition(void);
2614 void VM_gettime(void)
2618 VM_SAFEPARMCOUNTRANGE(0,1,VM_gettime);
2622 PRVM_G_FLOAT(OFS_RETURN) = (float) realtime;
2626 timer_index = (int) PRVM_G_FLOAT(OFS_PARM0);
2629 case 0: // GETTIME_FRAMESTART
2630 PRVM_G_FLOAT(OFS_RETURN) = (float) realtime;
2632 case 1: // GETTIME_REALTIME
2633 PRVM_G_FLOAT(OFS_RETURN) = (float) Sys_DoubleTime();
2635 case 2: // GETTIME_HIRES
2636 PRVM_G_FLOAT(OFS_RETURN) = (float) (Sys_DoubleTime() - realtime);
2638 case 3: // GETTIME_UPTIME
2639 PRVM_G_FLOAT(OFS_RETURN) = (float) (Sys_DoubleTime() - host_starttime);
2641 case 4: // GETTIME_CDTRACK
2642 PRVM_G_FLOAT(OFS_RETURN) = (float) CDAudio_GetPosition();
2645 VM_Warning("VM_gettime: %s: unsupported timer specified, returning realtime\n", PRVM_NAME);
2646 PRVM_G_FLOAT(OFS_RETURN) = (float) realtime;
2656 loadfromdata(string data)
2659 void VM_loadfromdata(void)
2661 VM_SAFEPARMCOUNT(1,VM_loadentsfromfile);
2663 PRVM_ED_LoadFromFile(PRVM_G_STRING(OFS_PARM0));
2667 ========================
2670 parseentitydata(entity ent, string data)
2671 ========================
2673 void VM_parseentitydata(void)
2678 VM_SAFEPARMCOUNT(2, VM_parseentitydata);
2680 // get edict and test it
2681 ent = PRVM_G_EDICT(OFS_PARM0);
2682 if (ent->priv.required->free)
2683 PRVM_ERROR ("VM_parseentitydata: %s: Can only set already spawned entities (entity %i is free)!", PRVM_NAME, PRVM_NUM_FOR_EDICT(ent));
2685 data = PRVM_G_STRING(OFS_PARM1);
2687 // parse the opening brace
2688 if (!COM_ParseToken_Simple(&data, false, false) || com_token[0] != '{' )
2689 PRVM_ERROR ("VM_parseentitydata: %s: Couldn't parse entity data:\n%s", PRVM_NAME, data );
2691 PRVM_ED_ParseEdict (data, ent);
2698 loadfromfile(string file)
2701 void VM_loadfromfile(void)
2703 const char *filename;
2706 VM_SAFEPARMCOUNT(1,VM_loadfromfile);
2708 filename = PRVM_G_STRING(OFS_PARM0);
2709 if (FS_CheckNastyPath(filename, false))
2711 PRVM_G_FLOAT(OFS_RETURN) = -4;
2712 VM_Warning("VM_loadfromfile: %s dangerous or non-portable filename \"%s\" not allowed. (contains : or \\ or begins with .. or /)\n", PRVM_NAME, filename);
2716 // not conform with VM_fopen
2717 data = (char *)FS_LoadFile(filename, tempmempool, false, NULL);
2719 PRVM_G_FLOAT(OFS_RETURN) = -1;
2721 PRVM_ED_LoadFromFile(data);
2732 float mod(float val, float m)
2735 void VM_modulo(void)
2738 VM_SAFEPARMCOUNT(2,VM_module);
2740 val = (int) PRVM_G_FLOAT(OFS_PARM0);
2741 m = (int) PRVM_G_FLOAT(OFS_PARM1);
2743 PRVM_G_FLOAT(OFS_RETURN) = (float) (val % m);
2746 void VM_Search_Init(void)
2749 for (i = 0;i < PRVM_MAX_OPENSEARCHES;i++)
2750 prog->opensearches[i] = NULL;
2753 void VM_Search_Reset(void)
2756 // reset the fssearch list
2757 for(i = 0; i < PRVM_MAX_OPENSEARCHES; i++)
2759 if(prog->opensearches[i])
2760 FS_FreeSearch(prog->opensearches[i]);
2761 prog->opensearches[i] = NULL;
2769 float search_begin(string pattern, float caseinsensitive, float quiet)
2772 void VM_search_begin(void)
2775 const char *pattern;
2776 int caseinsens, quiet;
2778 VM_SAFEPARMCOUNT(3, VM_search_begin);
2780 pattern = PRVM_G_STRING(OFS_PARM0);
2782 VM_CheckEmptyString(pattern);
2784 caseinsens = (int)PRVM_G_FLOAT(OFS_PARM1);
2785 quiet = (int)PRVM_G_FLOAT(OFS_PARM2);
2787 for(handle = 0; handle < PRVM_MAX_OPENSEARCHES; handle++)
2788 if(!prog->opensearches[handle])
2791 if(handle >= PRVM_MAX_OPENSEARCHES)
2793 PRVM_G_FLOAT(OFS_RETURN) = -2;
2794 VM_Warning("VM_search_begin: %s ran out of search handles (%i)\n", PRVM_NAME, PRVM_MAX_OPENSEARCHES);
2798 if(!(prog->opensearches[handle] = FS_Search(pattern,caseinsens, quiet)))
2799 PRVM_G_FLOAT(OFS_RETURN) = -1;
2802 prog->opensearches_origin[handle] = PRVM_AllocationOrigin();
2803 PRVM_G_FLOAT(OFS_RETURN) = handle;
2811 void search_end(float handle)
2814 void VM_search_end(void)
2817 VM_SAFEPARMCOUNT(1, VM_search_end);
2819 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
2821 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
2823 VM_Warning("VM_search_end: invalid handle %i used in %s\n", handle, PRVM_NAME);
2826 if(prog->opensearches[handle] == NULL)
2828 VM_Warning("VM_search_end: no such handle %i in %s\n", handle, PRVM_NAME);
2832 FS_FreeSearch(prog->opensearches[handle]);
2833 prog->opensearches[handle] = NULL;
2834 if(prog->opensearches_origin[handle])
2835 PRVM_Free((char *)prog->opensearches_origin[handle]);
2842 float search_getsize(float handle)
2845 void VM_search_getsize(void)
2848 VM_SAFEPARMCOUNT(1, VM_M_search_getsize);
2850 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
2852 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
2854 VM_Warning("VM_search_getsize: invalid handle %i used in %s\n", handle, PRVM_NAME);
2857 if(prog->opensearches[handle] == NULL)
2859 VM_Warning("VM_search_getsize: no such handle %i in %s\n", handle, PRVM_NAME);
2863 PRVM_G_FLOAT(OFS_RETURN) = prog->opensearches[handle]->numfilenames;
2868 VM_search_getfilename
2870 string search_getfilename(float handle, float num)
2873 void VM_search_getfilename(void)
2875 int handle, filenum;
2876 VM_SAFEPARMCOUNT(2, VM_search_getfilename);
2878 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
2879 filenum = (int)PRVM_G_FLOAT(OFS_PARM1);
2881 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
2883 VM_Warning("VM_search_getfilename: invalid handle %i used in %s\n", handle, PRVM_NAME);
2886 if(prog->opensearches[handle] == NULL)
2888 VM_Warning("VM_search_getfilename: no such handle %i in %s\n", handle, PRVM_NAME);
2891 if(filenum < 0 || filenum >= prog->opensearches[handle]->numfilenames)
2893 VM_Warning("VM_search_getfilename: invalid filenum %i in %s\n", filenum, PRVM_NAME);
2897 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog->opensearches[handle]->filenames[filenum]);
2904 string chr(float ascii)
2910 VM_SAFEPARMCOUNT(1, VM_chr);
2912 tmp[0] = (unsigned char) PRVM_G_FLOAT(OFS_PARM0);
2915 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(tmp);
2918 //=============================================================================
2919 // Draw builtins (client & menu)
2925 float iscachedpic(string pic)
2928 void VM_iscachedpic(void)
2930 VM_SAFEPARMCOUNT(1,VM_iscachedpic);
2932 // drawq hasnt such a function, thus always return true
2933 PRVM_G_FLOAT(OFS_RETURN) = false;
2940 string precache_pic(string pic)
2943 void VM_precache_pic(void)
2947 VM_SAFEPARMCOUNT(1, VM_precache_pic);
2949 s = PRVM_G_STRING(OFS_PARM0);
2950 PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
2951 VM_CheckEmptyString (s);
2953 // AK Draw_CachePic is supposed to always return a valid pointer
2954 if( Draw_CachePic_Flags(s, CACHEPICFLAG_NOTPERSISTENT)->tex == r_texture_notexture )
2955 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
2965 void VM_freepic(void)
2969 VM_SAFEPARMCOUNT(1,VM_freepic);
2971 s = PRVM_G_STRING(OFS_PARM0);
2972 VM_CheckEmptyString (s);
2977 dp_font_t *getdrawfont()
2979 if(prog->globaloffsets.drawfont >= 0)
2981 int f = (int) PRVM_G_FLOAT(prog->globaloffsets.drawfont);
2982 if(f < 0 || f >= MAX_FONTS)
2983 return FONT_DEFAULT;
2984 return &dp_fonts[f];
2987 return FONT_DEFAULT;
2994 float drawcharacter(vector position, float character, vector scale, vector rgb, float alpha, float flag)
2997 void VM_drawcharacter(void)
2999 float *pos,*scale,*rgb;
3002 VM_SAFEPARMCOUNT(6,VM_drawcharacter);
3004 character = (char) PRVM_G_FLOAT(OFS_PARM1);
3007 PRVM_G_FLOAT(OFS_RETURN) = -1;
3008 VM_Warning("VM_drawcharacter: %s passed null character !\n",PRVM_NAME);
3012 pos = PRVM_G_VECTOR(OFS_PARM0);
3013 scale = PRVM_G_VECTOR(OFS_PARM2);
3014 rgb = PRVM_G_VECTOR(OFS_PARM3);
3015 flag = (int)PRVM_G_FLOAT(OFS_PARM5);
3017 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3019 PRVM_G_FLOAT(OFS_RETURN) = -2;
3020 VM_Warning("VM_drawcharacter: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
3024 if(pos[2] || scale[2])
3025 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")));
3027 if(!scale[0] || !scale[1])
3029 PRVM_G_FLOAT(OFS_RETURN) = -3;
3030 VM_Warning("VM_drawcharacter: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
3034 DrawQ_String_Font(pos[0], pos[1], &character, 1, scale[0], scale[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM4), flag, NULL, true, getdrawfont());
3035 PRVM_G_FLOAT(OFS_RETURN) = 1;
3042 float drawstring(vector position, string text, vector scale, vector rgb, float alpha, float flag)
3045 void VM_drawstring(void)
3047 float *pos,*scale,*rgb;
3050 VM_SAFEPARMCOUNT(6,VM_drawstring);
3052 string = PRVM_G_STRING(OFS_PARM1);
3053 pos = PRVM_G_VECTOR(OFS_PARM0);
3054 scale = PRVM_G_VECTOR(OFS_PARM2);
3055 rgb = PRVM_G_VECTOR(OFS_PARM3);
3056 flag = (int)PRVM_G_FLOAT(OFS_PARM5);
3058 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3060 PRVM_G_FLOAT(OFS_RETURN) = -2;
3061 VM_Warning("VM_drawstring: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
3065 if(!scale[0] || !scale[1])
3067 PRVM_G_FLOAT(OFS_RETURN) = -3;
3068 VM_Warning("VM_drawstring: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
3072 if(pos[2] || scale[2])
3073 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")));
3075 DrawQ_String_Font(pos[0], pos[1], string, 0, scale[0], scale[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM4), flag, NULL, true, getdrawfont());
3076 PRVM_G_FLOAT(OFS_RETURN) = 1;
3081 VM_drawcolorcodedstring
3083 float drawcolorcodedstring(vector position, string text, vector scale, float alpha, float flag)
3086 void VM_drawcolorcodedstring(void)
3091 VM_SAFEPARMCOUNT(5,VM_drawstring);
3093 string = PRVM_G_STRING(OFS_PARM1);
3094 pos = PRVM_G_VECTOR(OFS_PARM0);
3095 scale = PRVM_G_VECTOR(OFS_PARM2);
3096 flag = (int)PRVM_G_FLOAT(OFS_PARM4);
3098 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3100 PRVM_G_FLOAT(OFS_RETURN) = -2;
3101 VM_Warning("VM_drawcolorcodedstring: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
3105 if(!scale[0] || !scale[1])
3107 PRVM_G_FLOAT(OFS_RETURN) = -3;
3108 VM_Warning("VM_drawcolorcodedstring: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
3112 if(pos[2] || scale[2])
3113 Con_Printf("VM_drawcolorcodedstring: z value%s from %s discarded\n",(pos[2] && scale[2]) ? "s" : " ",((pos[2] && scale[2]) ? "pos and scale" : (pos[2] ? "pos" : "scale")));
3116 DrawQ_String_Font(pos[0], pos[1], string, 0, scale[0], scale[1], 1, 1, 1, PRVM_G_FLOAT(OFS_PARM3), flag, NULL, false, getdrawfont());
3117 PRVM_G_FLOAT(OFS_RETURN) = 1;
3123 float stringwidth(string text, float allowColorCodes, float size)
3126 void VM_stringwidth(void)
3129 float sz, mult; // sz is intended font size so we can later add freetype support, mult is font size multiplier in pixels per character cell
3131 VM_SAFEPARMCOUNTRANGE(2,3,VM_drawstring);
3135 mult = sz = PRVM_G_FLOAT(OFS_PARM2);
3143 string = PRVM_G_STRING(OFS_PARM0);
3144 colors = (int)PRVM_G_FLOAT(OFS_PARM1);
3146 PRVM_G_FLOAT(OFS_RETURN) = DrawQ_TextWidth_Font(string, 0, !colors, getdrawfont()) * mult; // 1x1 characters, don't actually draw
3152 float drawpic(vector position, string pic, vector size, vector rgb, float alpha, float flag)
3155 void VM_drawpic(void)
3157 const char *picname;
3158 float *size, *pos, *rgb;
3161 VM_SAFEPARMCOUNT(6,VM_drawpic);
3163 picname = PRVM_G_STRING(OFS_PARM1);
3164 VM_CheckEmptyString (picname);
3166 // is pic cached ? no function yet for that
3169 PRVM_G_FLOAT(OFS_RETURN) = -4;
3170 VM_Warning("VM_drawpic: %s: %s not cached !\n", PRVM_NAME, picname);
3174 pos = PRVM_G_VECTOR(OFS_PARM0);
3175 size = PRVM_G_VECTOR(OFS_PARM2);
3176 rgb = PRVM_G_VECTOR(OFS_PARM3);
3177 flag = (int) PRVM_G_FLOAT(OFS_PARM5);
3179 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3181 PRVM_G_FLOAT(OFS_RETURN) = -2;
3182 VM_Warning("VM_drawpic: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
3186 if(pos[2] || size[2])
3187 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")));
3189 DrawQ_Pic(pos[0], pos[1], Draw_CachePic (picname), size[0], size[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM4), flag);
3190 PRVM_G_FLOAT(OFS_RETURN) = 1;
3196 float drawrotpic(vector position, string pic, vector size, vector org, float angle, vector rgb, float alpha, float flag)
3199 void VM_drawrotpic(void)
3201 const char *picname;
3202 float *size, *pos, *org, *rgb;
3205 VM_SAFEPARMCOUNT(8,VM_drawrotpic);
3207 picname = PRVM_G_STRING(OFS_PARM1);
3208 VM_CheckEmptyString (picname);
3210 // is pic cached ? no function yet for that
3213 PRVM_G_FLOAT(OFS_RETURN) = -4;
3214 VM_Warning("VM_drawrotpic: %s: %s not cached !\n", PRVM_NAME, picname);
3218 pos = PRVM_G_VECTOR(OFS_PARM0);
3219 size = PRVM_G_VECTOR(OFS_PARM2);
3220 org = PRVM_G_VECTOR(OFS_PARM3);
3221 rgb = PRVM_G_VECTOR(OFS_PARM5);
3222 flag = (int) PRVM_G_FLOAT(OFS_PARM7);
3224 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3226 PRVM_G_FLOAT(OFS_RETURN) = -2;
3227 VM_Warning("VM_drawrotpic: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
3231 if(pos[2] || size[2] || org[2])
3232 Con_Printf("VM_drawrotpic: z value from pos/size/org discarded\n");
3234 DrawQ_RotPic(pos[0], pos[1], Draw_CachePic(picname), size[0], size[1], org[0], org[1], PRVM_G_FLOAT(OFS_PARM4), rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM6), flag);
3235 PRVM_G_FLOAT(OFS_RETURN) = 1;
3241 float drawsubpic(vector position, vector size, string pic, vector srcPos, vector srcSize, vector rgb, float alpha, float flag)
3245 void VM_drawsubpic(void)
3247 const char *picname;
3248 float *size, *pos, *rgb, *srcPos, *srcSize, alpha;
3251 VM_SAFEPARMCOUNT(8,VM_drawsubpic);
3253 picname = PRVM_G_STRING(OFS_PARM2);
3254 VM_CheckEmptyString (picname);
3256 // is pic cached ? no function yet for that
3259 PRVM_G_FLOAT(OFS_RETURN) = -4;
3260 VM_Warning("VM_drawsubpic: %s: %s not cached !\n", PRVM_NAME, picname);
3264 pos = PRVM_G_VECTOR(OFS_PARM0);
3265 size = PRVM_G_VECTOR(OFS_PARM1);
3266 srcPos = PRVM_G_VECTOR(OFS_PARM3);
3267 srcSize = PRVM_G_VECTOR(OFS_PARM4);
3268 rgb = PRVM_G_VECTOR(OFS_PARM5);
3269 alpha = PRVM_G_FLOAT(OFS_PARM6);
3270 flag = (int) PRVM_G_FLOAT(OFS_PARM7);
3272 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3274 PRVM_G_FLOAT(OFS_RETURN) = -2;
3275 VM_Warning("VM_drawsubpic: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
3279 if(pos[2] || size[2])
3280 Con_Printf("VM_drawsubpic: z value%s from %s discarded\n",(pos[2] && size[2]) ? "s" : " ",((pos[2] && size[2]) ? "pos and size" : (pos[2] ? "pos" : "size")));
3282 DrawQ_SuperPic(pos[0], pos[1], Draw_CachePic (picname),
3284 srcPos[0], srcPos[1], rgb[0], rgb[1], rgb[2], alpha,
3285 srcPos[0] + srcSize[0], srcPos[1], rgb[0], rgb[1], rgb[2], alpha,
3286 srcPos[0], srcPos[1] + srcSize[1], rgb[0], rgb[1], rgb[2], alpha,
3287 srcPos[0] + srcSize[0], srcPos[1] + srcSize[1], rgb[0], rgb[1], rgb[2], alpha,
3289 PRVM_G_FLOAT(OFS_RETURN) = 1;
3296 float drawfill(vector position, vector size, vector rgb, float alpha, float flag)
3299 void VM_drawfill(void)
3301 float *size, *pos, *rgb;
3304 VM_SAFEPARMCOUNT(5,VM_drawfill);
3307 pos = PRVM_G_VECTOR(OFS_PARM0);
3308 size = PRVM_G_VECTOR(OFS_PARM1);
3309 rgb = PRVM_G_VECTOR(OFS_PARM2);
3310 flag = (int) PRVM_G_FLOAT(OFS_PARM4);
3312 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3314 PRVM_G_FLOAT(OFS_RETURN) = -2;
3315 VM_Warning("VM_drawfill: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
3319 if(pos[2] || size[2])
3320 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")));
3322 DrawQ_Fill(pos[0], pos[1], size[0], size[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM3), flag);
3323 PRVM_G_FLOAT(OFS_RETURN) = 1;
3330 drawsetcliparea(float x, float y, float width, float height)
3333 void VM_drawsetcliparea(void)
3336 VM_SAFEPARMCOUNT(4,VM_drawsetcliparea);
3338 x = bound(0, PRVM_G_FLOAT(OFS_PARM0), vid_conwidth.integer);
3339 y = bound(0, PRVM_G_FLOAT(OFS_PARM1), vid_conheight.integer);
3340 w = bound(0, PRVM_G_FLOAT(OFS_PARM2) + PRVM_G_FLOAT(OFS_PARM0) - x, (vid_conwidth.integer - x));
3341 h = bound(0, PRVM_G_FLOAT(OFS_PARM3) + PRVM_G_FLOAT(OFS_PARM1) - y, (vid_conheight.integer - y));
3343 DrawQ_SetClipArea(x, y, w, h);
3348 VM_drawresetcliparea
3353 void VM_drawresetcliparea(void)
3355 VM_SAFEPARMCOUNT(0,VM_drawresetcliparea);
3357 DrawQ_ResetClipArea();
3364 vector getimagesize(string pic)
3367 void VM_getimagesize(void)
3372 VM_SAFEPARMCOUNT(1,VM_getimagesize);
3374 p = PRVM_G_STRING(OFS_PARM0);
3375 VM_CheckEmptyString (p);
3377 pic = Draw_CachePic_Flags (p, CACHEPICFLAG_NOTPERSISTENT);
3379 PRVM_G_VECTOR(OFS_RETURN)[0] = pic->width;
3380 PRVM_G_VECTOR(OFS_RETURN)[1] = pic->height;
3381 PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
3388 string keynumtostring(float keynum)
3391 void VM_keynumtostring (void)
3393 VM_SAFEPARMCOUNT(1, VM_keynumtostring);
3395 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(Key_KeynumToString((int)PRVM_G_FLOAT(OFS_PARM0)));
3400 VM_findkeysforcommand
3402 string findkeysforcommand(string command)
3404 the returned string is an altstring
3407 #define NUMKEYS 5 // TODO: merge the constant in keys.c with this one somewhen
3409 void M_FindKeysForCommand(const char *command, int *keys);
3410 void VM_findkeysforcommand(void)
3413 char ret[VM_STRINGTEMP_LENGTH];
3417 VM_SAFEPARMCOUNT(1, VM_findkeysforcommand);
3419 cmd = PRVM_G_STRING(OFS_PARM0);
3421 VM_CheckEmptyString(cmd);
3423 M_FindKeysForCommand(cmd, keys);
3426 for(i = 0; i < NUMKEYS; i++)
3427 strlcat(ret, va(" \'%i\'", keys[i]), sizeof(ret));
3429 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(ret);
3436 float stringtokeynum(string key)
3439 void VM_stringtokeynum (void)
3441 VM_SAFEPARMCOUNT( 1, VM_keynumtostring );
3443 PRVM_G_INT(OFS_RETURN) = Key_StringToKeynum(PRVM_G_STRING(OFS_PARM0));
3446 // CL_Video interface functions
3449 ========================
3452 float cin_open(string file, string name)
3453 ========================
3455 void VM_cin_open( void )
3460 VM_SAFEPARMCOUNT( 2, VM_cin_open );
3462 file = PRVM_G_STRING( OFS_PARM0 );
3463 name = PRVM_G_STRING( OFS_PARM1 );
3465 VM_CheckEmptyString( file );
3466 VM_CheckEmptyString( name );
3468 if( CL_OpenVideo( file, name, MENUOWNER ) )
3469 PRVM_G_FLOAT( OFS_RETURN ) = 1;
3471 PRVM_G_FLOAT( OFS_RETURN ) = 0;
3475 ========================
3478 void cin_close(string name)
3479 ========================
3481 void VM_cin_close( void )
3485 VM_SAFEPARMCOUNT( 1, VM_cin_close );
3487 name = PRVM_G_STRING( OFS_PARM0 );
3488 VM_CheckEmptyString( name );
3490 CL_CloseVideo( CL_GetVideoByName( name ) );
3494 ========================
3496 void cin_setstate(string name, float type)
3497 ========================
3499 void VM_cin_setstate( void )
3502 clvideostate_t state;
3505 VM_SAFEPARMCOUNT( 2, VM_cin_netstate );
3507 name = PRVM_G_STRING( OFS_PARM0 );
3508 VM_CheckEmptyString( name );
3510 state = (clvideostate_t)((int)PRVM_G_FLOAT( OFS_PARM1 ));
3512 video = CL_GetVideoByName( name );
3513 if( video && state > CLVIDEO_UNUSED && state < CLVIDEO_STATECOUNT )
3514 CL_SetVideoState( video, state );
3518 ========================
3521 float cin_getstate(string name)
3522 ========================
3524 void VM_cin_getstate( void )
3529 VM_SAFEPARMCOUNT( 1, VM_cin_getstate );
3531 name = PRVM_G_STRING( OFS_PARM0 );
3532 VM_CheckEmptyString( name );
3534 video = CL_GetVideoByName( name );
3536 PRVM_G_FLOAT( OFS_RETURN ) = (int)video->state;
3538 PRVM_G_FLOAT( OFS_RETURN ) = 0;
3542 ========================
3545 void cin_restart(string name)
3546 ========================
3548 void VM_cin_restart( void )
3553 VM_SAFEPARMCOUNT( 1, VM_cin_restart );
3555 name = PRVM_G_STRING( OFS_PARM0 );
3556 VM_CheckEmptyString( name );
3558 video = CL_GetVideoByName( name );
3560 CL_RestartVideo( video );
3564 ========================
3566 ========================
3568 void VM_Gecko_Init( void ) {
3569 // the prog struct is memset to 0 by Initprog? [12/6/2007 Black]
3570 // FIXME: remove the other _Init functions then, too? [12/6/2007 Black]
3574 ========================
3576 ========================
3578 void VM_Gecko_Destroy( void ) {
3580 for( i = 0 ; i < PRVM_MAX_GECKOINSTANCES ; i++ ) {
3581 clgecko_t **instance = &prog->opengeckoinstances[ i ];
3583 CL_Gecko_DestroyBrowser( *instance );
3590 ========================
3593 float[bool] gecko_create( string name )
3594 ========================
3596 void VM_gecko_create( void ) {
3599 clgecko_t *instance;
3601 VM_SAFEPARMCOUNT( 1, VM_gecko_create );
3603 name = PRVM_G_STRING( OFS_PARM0 );
3604 VM_CheckEmptyString( name );
3606 // find an empty slot for this gecko browser..
3607 for( i = 0 ; i < PRVM_MAX_GECKOINSTANCES ; i++ ) {
3608 if( prog->opengeckoinstances[ i ] == NULL ) {
3612 if( i == PRVM_MAX_GECKOINSTANCES ) {
3613 VM_Warning("VM_gecko_create: %s ran out of gecko handles (%i)\n", PRVM_NAME, PRVM_MAX_GECKOINSTANCES);
3614 PRVM_G_FLOAT( OFS_RETURN ) = 0;
3618 instance = prog->opengeckoinstances[ i ] = CL_Gecko_CreateBrowser( name, PRVM_GetProgNr() );
3620 // TODO: error handling [12/3/2007 Black]
3621 PRVM_G_FLOAT( OFS_RETURN ) = 0;
3624 PRVM_G_FLOAT( OFS_RETURN ) = 1;
3628 ========================
3631 void gecko_destroy( string name )
3632 ========================
3634 void VM_gecko_destroy( void ) {
3636 clgecko_t *instance;
3638 VM_SAFEPARMCOUNT( 1, VM_gecko_destroy );
3640 name = PRVM_G_STRING( OFS_PARM0 );
3641 VM_CheckEmptyString( name );
3642 instance = CL_Gecko_FindBrowser( name );
3646 CL_Gecko_DestroyBrowser( instance );
3650 ========================
3653 void gecko_navigate( string name, string URI )
3654 ========================
3656 void VM_gecko_navigate( void ) {
3659 clgecko_t *instance;
3661 VM_SAFEPARMCOUNT( 2, VM_gecko_navigate );
3663 name = PRVM_G_STRING( OFS_PARM0 );
3664 URI = PRVM_G_STRING( OFS_PARM1 );
3665 VM_CheckEmptyString( name );
3666 VM_CheckEmptyString( URI );
3668 instance = CL_Gecko_FindBrowser( name );
3672 CL_Gecko_NavigateToURI( instance, URI );
3676 ========================
3679 float[bool] gecko_keyevent( string name, float key, float eventtype )
3680 ========================
3682 void VM_gecko_keyevent( void ) {
3685 clgecko_buttoneventtype_t eventtype;
3686 clgecko_t *instance;
3688 VM_SAFEPARMCOUNT( 3, VM_gecko_keyevent );
3690 name = PRVM_G_STRING( OFS_PARM0 );
3691 VM_CheckEmptyString( name );
3692 key = (unsigned int) PRVM_G_FLOAT( OFS_PARM1 );
3693 switch( (unsigned int) PRVM_G_FLOAT( OFS_PARM2 ) ) {
3695 eventtype = CLG_BET_DOWN;
3698 eventtype = CLG_BET_UP;
3701 eventtype = CLG_BET_PRESS;
3704 eventtype = CLG_BET_DOUBLECLICK;
3707 // TODO: console printf? [12/3/2007 Black]
3708 PRVM_G_FLOAT( OFS_RETURN ) = 0;
3712 instance = CL_Gecko_FindBrowser( name );
3714 PRVM_G_FLOAT( OFS_RETURN ) = 0;
3718 PRVM_G_FLOAT( OFS_RETURN ) = (CL_Gecko_Event_Key( instance, (keynum_t) key, eventtype ) == true);
3722 ========================
3725 void gecko_mousemove( string name, float x, float y )
3726 ========================
3728 void VM_gecko_movemouse( void ) {
3731 clgecko_t *instance;
3733 VM_SAFEPARMCOUNT( 3, VM_gecko_movemouse );
3735 name = PRVM_G_STRING( OFS_PARM0 );
3736 VM_CheckEmptyString( name );
3737 x = PRVM_G_FLOAT( OFS_PARM1 );
3738 y = PRVM_G_FLOAT( OFS_PARM2 );
3740 instance = CL_Gecko_FindBrowser( name );
3744 CL_Gecko_Event_CursorMove( instance, x, y );
3749 ========================
3752 void gecko_resize( string name, float w, float h )
3753 ========================
3755 void VM_gecko_resize( void ) {
3758 clgecko_t *instance;
3760 VM_SAFEPARMCOUNT( 3, VM_gecko_movemouse );
3762 name = PRVM_G_STRING( OFS_PARM0 );
3763 VM_CheckEmptyString( name );
3764 w = PRVM_G_FLOAT( OFS_PARM1 );
3765 h = PRVM_G_FLOAT( OFS_PARM2 );
3767 instance = CL_Gecko_FindBrowser( name );
3771 CL_Gecko_Resize( instance, (int) w, (int) h );
3776 ========================
3777 VM_gecko_get_texture_extent
3779 vector gecko_get_texture_extent( string name )
3780 ========================
3782 void VM_gecko_get_texture_extent( void ) {
3784 clgecko_t *instance;
3786 VM_SAFEPARMCOUNT( 1, VM_gecko_movemouse );
3788 name = PRVM_G_STRING( OFS_PARM0 );
3789 VM_CheckEmptyString( name );
3791 PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
3792 instance = CL_Gecko_FindBrowser( name );
3794 PRVM_G_VECTOR(OFS_RETURN)[0] = 0;
3795 PRVM_G_VECTOR(OFS_RETURN)[1] = 0;
3798 CL_Gecko_GetTextureExtent( instance,
3799 PRVM_G_VECTOR(OFS_RETURN), PRVM_G_VECTOR(OFS_RETURN)+1 );
3808 Writes new values for v_forward, v_up, and v_right based on angles
3809 void makevectors(vector angle)
3812 void VM_makevectors (void)
3814 prvm_eval_t *valforward, *valright, *valup;
3815 valforward = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_forward);
3816 valright = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_right);
3817 valup = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_up);
3818 if (!valforward || !valright || !valup)
3820 VM_Warning("makevectors: could not find v_forward, v_right, or v_up global variables\n");
3823 VM_SAFEPARMCOUNT(1, VM_makevectors);
3824 AngleVectors (PRVM_G_VECTOR(OFS_PARM0), valforward->vector, valright->vector, valup->vector);
3831 Writes new values for v_forward, v_up, and v_right based on the given forward vector
3832 vectorvectors(vector)
3835 void VM_vectorvectors (void)
3837 prvm_eval_t *valforward, *valright, *valup;
3838 valforward = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_forward);
3839 valright = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_right);
3840 valup = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_up);
3841 if (!valforward || !valright || !valup)
3843 VM_Warning("vectorvectors: could not find v_forward, v_right, or v_up global variables\n");
3846 VM_SAFEPARMCOUNT(1, VM_vectorvectors);
3847 VectorNormalize2(PRVM_G_VECTOR(OFS_PARM0), valforward->vector);
3848 VectorVectors(valforward->vector, valright->vector, valup->vector);
3852 ========================
3855 void drawline(float width, vector pos1, vector pos2, vector rgb, float alpha, float flags)
3856 ========================
3858 void VM_drawline (void)
3860 float *c1, *c2, *rgb;
3862 unsigned char flags;
3864 VM_SAFEPARMCOUNT(6, VM_drawline);
3865 width = PRVM_G_FLOAT(OFS_PARM0);
3866 c1 = PRVM_G_VECTOR(OFS_PARM1);
3867 c2 = PRVM_G_VECTOR(OFS_PARM2);
3868 rgb = PRVM_G_VECTOR(OFS_PARM3);
3869 alpha = PRVM_G_FLOAT(OFS_PARM4);
3870 flags = (int)PRVM_G_FLOAT(OFS_PARM5);
3871 DrawQ_Line(width, c1[0], c1[1], c2[0], c2[1], rgb[0], rgb[1], rgb[2], alpha, flags);
3874 // float(float number, float quantity) bitshift (EXT_BITSHIFT)
3875 void VM_bitshift (void)
3878 VM_SAFEPARMCOUNT(2, VM_bitshift);
3880 n1 = (int)fabs((int)PRVM_G_FLOAT(OFS_PARM0));
3881 n2 = (int)PRVM_G_FLOAT(OFS_PARM1);
3883 PRVM_G_FLOAT(OFS_RETURN) = n1;
3886 PRVM_G_FLOAT(OFS_RETURN) = (n1 >> -n2);
3888 PRVM_G_FLOAT(OFS_RETURN) = (n1 << n2);
3891 ////////////////////////////////////////
3892 // AltString functions
3893 ////////////////////////////////////////
3896 ========================
3899 float altstr_count(string)
3900 ========================
3902 void VM_altstr_count( void )
3904 const char *altstr, *pos;
3907 VM_SAFEPARMCOUNT( 1, VM_altstr_count );
3909 altstr = PRVM_G_STRING( OFS_PARM0 );
3910 //VM_CheckEmptyString( altstr );
3912 for( count = 0, pos = altstr ; *pos ; pos++ ) {
3913 if( *pos == '\\' ) {
3917 } else if( *pos == '\'' ) {
3922 PRVM_G_FLOAT( OFS_RETURN ) = (float) (count / 2);
3926 ========================
3929 string altstr_prepare(string)
3930 ========================
3932 void VM_altstr_prepare( void )
3935 const char *instr, *in;
3937 char outstr[VM_STRINGTEMP_LENGTH];
3939 VM_SAFEPARMCOUNT( 1, VM_altstr_prepare );
3941 instr = PRVM_G_STRING( OFS_PARM0 );
3943 for( out = outstr, in = instr, size = sizeof(outstr) - 1 ; size && *in ; size--, in++, out++ )
3952 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
3956 ========================
3959 string altstr_get(string, float)
3960 ========================
3962 void VM_altstr_get( void )
3964 const char *altstr, *pos;
3967 char outstr[VM_STRINGTEMP_LENGTH];
3969 VM_SAFEPARMCOUNT( 2, VM_altstr_get );
3971 altstr = PRVM_G_STRING( OFS_PARM0 );
3973 count = (int)PRVM_G_FLOAT( OFS_PARM1 );
3974 count = count * 2 + 1;
3976 for( pos = altstr ; *pos && count ; pos++ )
3977 if( *pos == '\\' ) {
3980 } else if( *pos == '\'' )
3984 PRVM_G_INT( OFS_RETURN ) = 0;
3988 for( out = outstr, size = sizeof(outstr) - 1 ; size && *pos ; size--, pos++, out++ )
3989 if( *pos == '\\' ) {
3994 } else if( *pos == '\'' )
4000 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
4004 ========================
4007 string altstr_set(string altstr, float num, string set)
4008 ========================
4010 void VM_altstr_set( void )
4013 const char *altstr, *str;
4016 char outstr[VM_STRINGTEMP_LENGTH];
4018 VM_SAFEPARMCOUNT( 3, VM_altstr_set );
4020 altstr = PRVM_G_STRING( OFS_PARM0 );
4022 num = (int)PRVM_G_FLOAT( OFS_PARM1 );
4024 str = PRVM_G_STRING( OFS_PARM2 );
4027 for( num = num * 2 + 1, in = altstr; *in && num; *out++ = *in++ )
4032 } else if( *in == '\'' ) {
4037 for( ; *str; *out++ = *str++ );
4038 // now jump over the old content
4040 if( *in == '\'' || (*in == '\\' && !*++in) )
4043 strlcpy(out, in, outstr + sizeof(outstr) - out);
4044 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
4048 ========================
4051 string altstr_ins(string altstr, float num, string set)
4052 ========================
4054 void VM_altstr_ins(void)
4062 char outstr[VM_STRINGTEMP_LENGTH];
4064 VM_SAFEPARMCOUNT(3, VM_altstr_ins);
4066 in = instr = PRVM_G_STRING( OFS_PARM0 );
4067 num = (int)PRVM_G_FLOAT( OFS_PARM1 );
4068 set = setstr = PRVM_G_STRING( OFS_PARM2 );
4071 for( num = num * 2 + 2 ; *in && num > 0 ; *out++ = *in++ )
4076 } else if( *in == '\'' ) {
4081 for( ; *set ; *out++ = *set++ );
4084 strlcpy(out, in, outstr + sizeof(outstr) - out);
4085 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
4089 ////////////////////////////////////////
4090 // BufString functions
4091 ////////////////////////////////////////
4092 //[515]: string buffers support
4094 static size_t stringbuffers_sortlength;
4096 static void BufStr_Expand(prvm_stringbuffer_t *stringbuffer, int strindex)
4098 if (stringbuffer->max_strings <= strindex)
4100 char **oldstrings = stringbuffer->strings;
4101 stringbuffer->max_strings = max(stringbuffer->max_strings * 2, 128);
4102 while (stringbuffer->max_strings <= strindex)
4103 stringbuffer->max_strings *= 2;
4104 stringbuffer->strings = (char **) Mem_Alloc(prog->progs_mempool, stringbuffer->max_strings * sizeof(stringbuffer->strings[0]));
4105 if (stringbuffer->num_strings > 0)
4106 memcpy(stringbuffer->strings, oldstrings, stringbuffer->num_strings * sizeof(stringbuffer->strings[0]));
4108 Mem_Free(oldstrings);
4112 static void BufStr_Shrink(prvm_stringbuffer_t *stringbuffer)
4114 // reduce num_strings if there are empty string slots at the end
4115 while (stringbuffer->num_strings > 0 && stringbuffer->strings[stringbuffer->num_strings - 1] == NULL)
4116 stringbuffer->num_strings--;
4118 // if empty, free the string pointer array
4119 if (stringbuffer->num_strings == 0)
4121 stringbuffer->max_strings = 0;
4122 if (stringbuffer->strings)
4123 Mem_Free(stringbuffer->strings);
4124 stringbuffer->strings = NULL;
4128 static int BufStr_SortStringsUP (const void *in1, const void *in2)
4131 a = *((const char **) in1);
4132 b = *((const char **) in2);
4134 if(!b[0]) return -1;
4135 return strncmp(a, b, stringbuffers_sortlength);
4138 static int BufStr_SortStringsDOWN (const void *in1, const void *in2)
4141 a = *((const char **) in1);
4142 b = *((const char **) in2);
4144 if(!b[0]) return -1;
4145 return strncmp(b, a, stringbuffers_sortlength);
4149 ========================
4151 creates new buffer, and returns it's index, returns -1 if failed
4152 float buf_create(void) = #460;
4153 ========================
4155 void VM_buf_create (void)
4157 prvm_stringbuffer_t *stringbuffer;
4159 VM_SAFEPARMCOUNT(0, VM_buf_create);
4160 stringbuffer = (prvm_stringbuffer_t *) Mem_ExpandableArray_AllocRecord(&prog->stringbuffersarray);
4161 for (i = 0;stringbuffer != Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, i);i++);
4162 stringbuffer->origin = PRVM_AllocationOrigin();
4163 PRVM_G_FLOAT(OFS_RETURN) = i;
4167 ========================
4169 deletes buffer and all strings in it
4170 void buf_del(float bufhandle) = #461;
4171 ========================
4173 void VM_buf_del (void)
4175 prvm_stringbuffer_t *stringbuffer;
4176 VM_SAFEPARMCOUNT(1, VM_buf_del);
4177 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4181 for (i = 0;i < stringbuffer->num_strings;i++)
4182 if (stringbuffer->strings[i])
4183 Mem_Free(stringbuffer->strings[i]);
4184 if (stringbuffer->strings)
4185 Mem_Free(stringbuffer->strings);
4186 if(stringbuffer->origin)
4187 PRVM_Free((char *)stringbuffer->origin);
4188 Mem_ExpandableArray_FreeRecord(&prog->stringbuffersarray, stringbuffer);
4192 VM_Warning("VM_buf_del: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4198 ========================
4200 how many strings are stored in buffer
4201 float buf_getsize(float bufhandle) = #462;
4202 ========================
4204 void VM_buf_getsize (void)
4206 prvm_stringbuffer_t *stringbuffer;
4207 VM_SAFEPARMCOUNT(1, VM_buf_getsize);
4209 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4212 PRVM_G_FLOAT(OFS_RETURN) = -1;
4213 VM_Warning("VM_buf_getsize: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4217 PRVM_G_FLOAT(OFS_RETURN) = stringbuffer->num_strings;
4221 ========================
4223 copy all content from one buffer to another, make sure it exists
4224 void buf_copy(float bufhandle_from, float bufhandle_to) = #463;
4225 ========================
4227 void VM_buf_copy (void)
4229 prvm_stringbuffer_t *srcstringbuffer, *dststringbuffer;
4231 VM_SAFEPARMCOUNT(2, VM_buf_copy);
4233 srcstringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4234 if(!srcstringbuffer)
4236 VM_Warning("VM_buf_copy: invalid source buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4239 i = (int)PRVM_G_FLOAT(OFS_PARM1);
4240 if(i == (int)PRVM_G_FLOAT(OFS_PARM0))
4242 VM_Warning("VM_buf_copy: source == destination (%i) in %s\n", i, PRVM_NAME);
4245 dststringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4246 if(!dststringbuffer)
4248 VM_Warning("VM_buf_copy: invalid destination buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM1), PRVM_NAME);
4252 for (i = 0;i < dststringbuffer->num_strings;i++)
4253 if (dststringbuffer->strings[i])
4254 Mem_Free(dststringbuffer->strings[i]);
4255 if (dststringbuffer->strings)
4256 Mem_Free(dststringbuffer->strings);
4257 *dststringbuffer = *srcstringbuffer;
4258 if (dststringbuffer->max_strings)
4259 dststringbuffer->strings = (char **)Mem_Alloc(prog->progs_mempool, sizeof(dststringbuffer->strings[0]) * dststringbuffer->max_strings);
4261 for (i = 0;i < dststringbuffer->num_strings;i++)
4263 if (srcstringbuffer->strings[i])
4266 stringlen = strlen(srcstringbuffer->strings[i]) + 1;
4267 dststringbuffer->strings[i] = (char *)Mem_Alloc(prog->progs_mempool, stringlen);
4268 memcpy(dststringbuffer->strings[i], srcstringbuffer->strings[i], stringlen);
4274 ========================
4276 sort buffer by beginnings of strings (cmplength defaults it's length)
4277 "backward == TRUE" means that sorting goes upside-down
4278 void buf_sort(float bufhandle, float cmplength, float backward) = #464;
4279 ========================
4281 void VM_buf_sort (void)
4283 prvm_stringbuffer_t *stringbuffer;
4284 VM_SAFEPARMCOUNT(3, VM_buf_sort);
4286 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4289 VM_Warning("VM_buf_sort: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4292 if(stringbuffer->num_strings <= 0)
4294 VM_Warning("VM_buf_sort: tried to sort empty buffer %i in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4297 stringbuffers_sortlength = (int)PRVM_G_FLOAT(OFS_PARM1);
4298 if(stringbuffers_sortlength <= 0)
4299 stringbuffers_sortlength = 0x7FFFFFFF;
4301 if(!PRVM_G_FLOAT(OFS_PARM2))
4302 qsort(stringbuffer->strings, stringbuffer->num_strings, sizeof(char*), BufStr_SortStringsUP);
4304 qsort(stringbuffer->strings, stringbuffer->num_strings, sizeof(char*), BufStr_SortStringsDOWN);
4306 BufStr_Shrink(stringbuffer);
4310 ========================
4312 concantenates all buffer string into one with "glue" separator and returns it as tempstring
4313 string buf_implode(float bufhandle, string glue) = #465;
4314 ========================
4316 void VM_buf_implode (void)
4318 prvm_stringbuffer_t *stringbuffer;
4319 char k[VM_STRINGTEMP_LENGTH];
4323 VM_SAFEPARMCOUNT(2, VM_buf_implode);
4325 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4326 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
4329 VM_Warning("VM_buf_implode: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4332 if(!stringbuffer->num_strings)
4334 sep = PRVM_G_STRING(OFS_PARM1);
4336 for(l = i = 0;i < stringbuffer->num_strings;i++)
4338 if(stringbuffer->strings[i])
4340 l += (i > 0 ? strlen(sep) : 0) + strlen(stringbuffer->strings[i]);
4341 if (l >= sizeof(k) - 1)
4343 strlcat(k, sep, sizeof(k));
4344 strlcat(k, stringbuffer->strings[i], sizeof(k));
4347 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(k);
4351 ========================
4353 get a string from buffer, returns tempstring, dont str_unzone it!
4354 string bufstr_get(float bufhandle, float string_index) = #465;
4355 ========================
4357 void VM_bufstr_get (void)
4359 prvm_stringbuffer_t *stringbuffer;
4361 VM_SAFEPARMCOUNT(2, VM_bufstr_get);
4363 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
4364 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4367 VM_Warning("VM_bufstr_get: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4370 strindex = (int)PRVM_G_FLOAT(OFS_PARM1);
4373 VM_Warning("VM_bufstr_get: invalid string index %i used in %s\n", strindex, PRVM_NAME);
4376 if (strindex < stringbuffer->num_strings && stringbuffer->strings[strindex])
4377 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(stringbuffer->strings[strindex]);
4381 ========================
4383 copies a string into selected slot of buffer
4384 void bufstr_set(float bufhandle, float string_index, string str) = #466;
4385 ========================
4387 void VM_bufstr_set (void)
4390 prvm_stringbuffer_t *stringbuffer;
4393 VM_SAFEPARMCOUNT(3, VM_bufstr_set);
4395 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4398 VM_Warning("VM_bufstr_set: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4401 strindex = (int)PRVM_G_FLOAT(OFS_PARM1);
4402 if(strindex < 0 || strindex >= 1000000) // huge number of strings
4404 VM_Warning("VM_bufstr_set: invalid string index %i used in %s\n", strindex, PRVM_NAME);
4408 BufStr_Expand(stringbuffer, strindex);
4409 stringbuffer->num_strings = max(stringbuffer->num_strings, strindex + 1);
4411 if(stringbuffer->strings[strindex])
4412 Mem_Free(stringbuffer->strings[strindex]);
4413 stringbuffer->strings[strindex] = NULL;
4415 news = PRVM_G_STRING(OFS_PARM2);
4416 if (news && news[0])
4418 size_t alloclen = strlen(news) + 1;
4419 stringbuffer->strings[strindex] = (char *)Mem_Alloc(prog->progs_mempool, alloclen);
4420 memcpy(stringbuffer->strings[strindex], news, alloclen);
4423 BufStr_Shrink(stringbuffer);
4427 ========================
4429 adds string to buffer in first free slot and returns its index
4430 "order == TRUE" means that string will be added after last "full" slot
4431 float bufstr_add(float bufhandle, string str, float order) = #467;
4432 ========================
4434 void VM_bufstr_add (void)
4436 int order, strindex;
4437 prvm_stringbuffer_t *stringbuffer;
4441 VM_SAFEPARMCOUNT(3, VM_bufstr_add);
4443 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4444 PRVM_G_FLOAT(OFS_RETURN) = -1;
4447 VM_Warning("VM_bufstr_add: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4450 string = PRVM_G_STRING(OFS_PARM1);
4451 if(!string || !string[0])
4453 VM_Warning("VM_bufstr_add: can not add an empty string to buffer %i in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4456 order = (int)PRVM_G_FLOAT(OFS_PARM2);
4458 strindex = stringbuffer->num_strings;
4460 for (strindex = 0;strindex < stringbuffer->num_strings;strindex++)
4461 if (stringbuffer->strings[strindex] == NULL)
4464 BufStr_Expand(stringbuffer, strindex);
4466 stringbuffer->num_strings = max(stringbuffer->num_strings, strindex + 1);
4467 alloclen = strlen(string) + 1;
4468 stringbuffer->strings[strindex] = (char *)Mem_Alloc(prog->progs_mempool, alloclen);
4469 memcpy(stringbuffer->strings[strindex], string, alloclen);
4471 PRVM_G_FLOAT(OFS_RETURN) = strindex;
4475 ========================
4477 delete string from buffer
4478 void bufstr_free(float bufhandle, float string_index) = #468;
4479 ========================
4481 void VM_bufstr_free (void)
4484 prvm_stringbuffer_t *stringbuffer;
4485 VM_SAFEPARMCOUNT(2, VM_bufstr_free);
4487 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4490 VM_Warning("VM_bufstr_free: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4493 i = (int)PRVM_G_FLOAT(OFS_PARM1);
4496 VM_Warning("VM_bufstr_free: invalid string index %i used in %s\n", i, PRVM_NAME);
4500 if (i < stringbuffer->num_strings)
4502 if(stringbuffer->strings[i])
4503 Mem_Free(stringbuffer->strings[i]);
4504 stringbuffer->strings[i] = NULL;
4507 BufStr_Shrink(stringbuffer);
4516 void VM_buf_cvarlist(void)
4519 const char *partial, *antipartial;
4520 size_t len, antilen;
4522 qboolean ispattern, antiispattern;
4524 prvm_stringbuffer_t *stringbuffer;
4525 VM_SAFEPARMCOUNTRANGE(2, 3, VM_buf_cvarlist);
4527 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4530 VM_Warning("VM_bufstr_free: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4534 partial = PRVM_G_STRING(OFS_PARM1);
4538 len = strlen(partial);
4541 antipartial = PRVM_G_STRING(OFS_PARM2);
4547 antilen = strlen(antipartial);
4549 for (n = 0;n < stringbuffer->num_strings;n++)
4550 if (stringbuffer->strings[n])
4551 Mem_Free(stringbuffer->strings[n]);
4552 if (stringbuffer->strings)
4553 Mem_Free(stringbuffer->strings);
4554 stringbuffer->strings = NULL;
4556 ispattern = partial && (strchr(partial, '*') || strchr(partial, '?'));
4557 antiispattern = antipartial && (strchr(antipartial, '*') || strchr(antipartial, '?'));
4560 for(cvar = cvar_vars; cvar; cvar = cvar->next)
4562 if(len && (ispattern ? !matchpattern_with_separator(cvar->name, partial, false, "", false) : strncmp(partial, cvar->name, len)))
4565 if(antilen && (antiispattern ? matchpattern_with_separator(cvar->name, antipartial, false, "", false) : !strncmp(antipartial, cvar->name, antilen)))
4571 stringbuffer->max_strings = stringbuffer->num_strings = n;
4572 if (stringbuffer->max_strings)
4573 stringbuffer->strings = (char **)Mem_Alloc(prog->progs_mempool, sizeof(stringbuffer->strings[0]) * stringbuffer->max_strings);
4576 for(cvar = cvar_vars; cvar; cvar = cvar->next)
4578 if(len && (ispattern ? !matchpattern_with_separator(cvar->name, partial, false, "", false) : strncmp(partial, cvar->name, len)))
4581 if(antilen && (antiispattern ? matchpattern_with_separator(cvar->name, antipartial, false, "", false) : !strncmp(antipartial, cvar->name, antilen)))
4584 alloclen = strlen(cvar->name) + 1;
4585 stringbuffer->strings[n] = (char *)Mem_Alloc(prog->progs_mempool, alloclen);
4586 memcpy(stringbuffer->strings[n], cvar->name, alloclen);
4601 This was a major timewaster in progs, so it was converted to C
4604 void VM_changeyaw (void)
4607 float ideal, current, move, speed;
4609 // this is called (VERY HACKISHLY) by SV_MoveToGoal, so it can not use any
4610 // parameters because they are the parameters to SV_MoveToGoal, not this
4611 //VM_SAFEPARMCOUNT(0, VM_changeyaw);
4613 ent = PRVM_PROG_TO_EDICT(PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict);
4614 if (ent == prog->edicts)
4616 VM_Warning("changeyaw: can not modify world entity\n");
4619 if (ent->priv.server->free)
4621 VM_Warning("changeyaw: can not modify free entity\n");
4624 if (prog->fieldoffsets.angles < 0 || prog->fieldoffsets.ideal_yaw < 0 || prog->fieldoffsets.yaw_speed < 0)
4626 VM_Warning("changeyaw: angles, ideal_yaw, or yaw_speed field(s) not found\n");
4629 current = ANGLEMOD(PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.angles)->vector[1]);
4630 ideal = PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.ideal_yaw)->_float;
4631 speed = PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.yaw_speed)->_float;
4633 if (current == ideal)
4635 move = ideal - current;
4636 if (ideal > current)
4657 PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.angles)->vector[1] = ANGLEMOD (current + move);
4665 void VM_changepitch (void)
4668 float ideal, current, move, speed;
4670 VM_SAFEPARMCOUNT(1, VM_changepitch);
4672 ent = PRVM_G_EDICT(OFS_PARM0);
4673 if (ent == prog->edicts)
4675 VM_Warning("changepitch: can not modify world entity\n");
4678 if (ent->priv.server->free)
4680 VM_Warning("changepitch: can not modify free entity\n");
4683 if (prog->fieldoffsets.angles < 0 || prog->fieldoffsets.idealpitch < 0 || prog->fieldoffsets.pitch_speed < 0)
4685 VM_Warning("changepitch: angles, idealpitch, or pitch_speed field(s) not found\n");
4688 current = ANGLEMOD(PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.angles)->vector[0]);
4689 ideal = PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.idealpitch)->_float;
4690 speed = PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.pitch_speed)->_float;
4692 if (current == ideal)
4694 move = ideal - current;
4695 if (ideal > current)
4716 PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.angles)->vector[0] = ANGLEMOD (current + move);
4720 void VM_uncolorstring (void)
4722 char szNewString[VM_STRINGTEMP_LENGTH];
4723 const char *szString;
4726 VM_SAFEPARMCOUNT(1, VM_uncolorstring);
4727 szString = PRVM_G_STRING(OFS_PARM0);
4728 COM_StringDecolorize(szString, 0, szNewString, sizeof(szNewString), TRUE);
4729 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(szNewString);
4733 // #221 float(string str, string sub[, float startpos]) strstrofs (FTE_STRINGS)
4734 //strstr, without generating a new string. Use in conjunction with FRIK_FILE's substring for more similar strstr.
4735 void VM_strstrofs (void)
4737 const char *instr, *match;
4739 VM_SAFEPARMCOUNTRANGE(2, 3, VM_strstrofs);
4740 instr = PRVM_G_STRING(OFS_PARM0);
4741 match = PRVM_G_STRING(OFS_PARM1);
4742 firstofs = (prog->argc > 2)?(int)PRVM_G_FLOAT(OFS_PARM2):0;
4744 if (firstofs && (firstofs < 0 || firstofs > (int)strlen(instr)))
4746 PRVM_G_FLOAT(OFS_RETURN) = -1;
4750 match = strstr(instr+firstofs, match);
4752 PRVM_G_FLOAT(OFS_RETURN) = -1;
4754 PRVM_G_FLOAT(OFS_RETURN) = match - instr;
4757 //#222 string(string s, float index) str2chr (FTE_STRINGS)
4758 void VM_str2chr (void)
4761 VM_SAFEPARMCOUNT(2, VM_str2chr);
4762 s = PRVM_G_STRING(OFS_PARM0);
4763 if((unsigned)PRVM_G_FLOAT(OFS_PARM1) < strlen(s))
4764 PRVM_G_FLOAT(OFS_RETURN) = (unsigned char)s[(unsigned)PRVM_G_FLOAT(OFS_PARM1)];
4766 PRVM_G_FLOAT(OFS_RETURN) = 0;
4769 //#223 string(float c, ...) chr2str (FTE_STRINGS)
4770 void VM_chr2str (void)
4774 VM_SAFEPARMCOUNTRANGE(0, 8, VM_chr2str);
4775 for(i = 0;i < prog->argc && i < (int)sizeof(t) - 1;i++)
4776 t[i] = (unsigned char)PRVM_G_FLOAT(OFS_PARM0+i*3);
4778 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(t);
4781 static int chrconv_number(int i, int base, int conv)
4806 static int chrconv_punct(int i, int base, int conv)
4824 static int chrchar_alpha(int i, int basec, int baset, int convc, int convt, int charnum)
4826 //convert case and colour seperatly...
4843 baset = 128*((charnum&1) == (convt-5));
4859 return i + basec + baset;
4861 // #224 string(float ccase, float calpha, float cnum, string s, ...) strconv (FTE_STRINGS)
4862 //bulk convert a string. change case or colouring.
4863 void VM_strconv (void)
4865 int ccase, redalpha, rednum, len, i;
4866 unsigned char resbuf[VM_STRINGTEMP_LENGTH];
4867 unsigned char *result = resbuf;
4869 VM_SAFEPARMCOUNTRANGE(3, 8, VM_strconv);
4871 ccase = (int) PRVM_G_FLOAT(OFS_PARM0); //0 same, 1 lower, 2 upper
4872 redalpha = (int) PRVM_G_FLOAT(OFS_PARM1); //0 same, 1 white, 2 red, 5 alternate, 6 alternate-alternate
4873 rednum = (int) PRVM_G_FLOAT(OFS_PARM2); //0 same, 1 white, 2 red, 3 redspecial, 4 whitespecial, 5 alternate, 6 alternate-alternate
4874 VM_VarString(3, (char *) resbuf, sizeof(resbuf));
4875 len = strlen((char *) resbuf);
4877 for (i = 0; i < len; i++, result++) //should this be done backwards?
4879 if (*result >= '0' && *result <= '9') //normal numbers...
4880 *result = chrconv_number(*result, '0', rednum);
4881 else if (*result >= '0'+128 && *result <= '9'+128)
4882 *result = chrconv_number(*result, '0'+128, rednum);
4883 else if (*result >= '0'+128-30 && *result <= '9'+128-30)
4884 *result = chrconv_number(*result, '0'+128-30, rednum);
4885 else if (*result >= '0'-30 && *result <= '9'-30)
4886 *result = chrconv_number(*result, '0'-30, rednum);
4888 else if (*result >= 'a' && *result <= 'z') //normal numbers...
4889 *result = chrchar_alpha(*result, 'a', 0, ccase, redalpha, i);
4890 else if (*result >= 'A' && *result <= 'Z') //normal numbers...
4891 *result = chrchar_alpha(*result, 'A', 0, ccase, redalpha, i);
4892 else if (*result >= 'a'+128 && *result <= 'z'+128) //normal numbers...
4893 *result = chrchar_alpha(*result, 'a', 128, ccase, redalpha, i);
4894 else if (*result >= 'A'+128 && *result <= 'Z'+128) //normal numbers...
4895 *result = chrchar_alpha(*result, 'A', 128, ccase, redalpha, i);
4897 else if ((*result & 127) < 16 || !redalpha) //special chars..
4899 else if (*result < 128)
4900 *result = chrconv_punct(*result, 0, redalpha);
4902 *result = chrconv_punct(*result, 128, redalpha);
4906 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString((char *) resbuf);
4909 // #225 string(float chars, string s, ...) strpad (FTE_STRINGS)
4910 void VM_strpad (void)
4912 char src[VM_STRINGTEMP_LENGTH];
4913 char destbuf[VM_STRINGTEMP_LENGTH];
4915 VM_SAFEPARMCOUNTRANGE(1, 8, VM_strpad);
4916 pad = (int) PRVM_G_FLOAT(OFS_PARM0);
4917 VM_VarString(1, src, sizeof(src));
4919 // note: < 0 = left padding, > 0 = right padding,
4920 // this is reverse logic of printf!
4921 dpsnprintf(destbuf, sizeof(destbuf), "%*s", -pad, src);
4923 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(destbuf);
4926 // #226 string(string info, string key, string value, ...) infoadd (FTE_STRINGS)
4927 //uses qw style \key\value strings
4928 void VM_infoadd (void)
4930 const char *info, *key;
4931 char value[VM_STRINGTEMP_LENGTH];
4932 char temp[VM_STRINGTEMP_LENGTH];
4934 VM_SAFEPARMCOUNTRANGE(2, 8, VM_infoadd);
4935 info = PRVM_G_STRING(OFS_PARM0);
4936 key = PRVM_G_STRING(OFS_PARM1);
4937 VM_VarString(2, value, sizeof(value));
4939 strlcpy(temp, info, VM_STRINGTEMP_LENGTH);
4941 InfoString_SetValue(temp, VM_STRINGTEMP_LENGTH, key, value);
4943 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(temp);
4946 // #227 string(string info, string key) infoget (FTE_STRINGS)
4947 //uses qw style \key\value strings
4948 void VM_infoget (void)
4952 char value[VM_STRINGTEMP_LENGTH];
4954 VM_SAFEPARMCOUNT(2, VM_infoget);
4955 info = PRVM_G_STRING(OFS_PARM0);
4956 key = PRVM_G_STRING(OFS_PARM1);
4958 InfoString_GetValue(info, key, value, VM_STRINGTEMP_LENGTH);
4960 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(value);
4963 //#228 float(string s1, string s2, float len) strncmp (FTE_STRINGS)
4964 // also float(string s1, string s2) strcmp (FRIK_FILE)
4965 void VM_strncmp (void)
4967 const char *s1, *s2;
4968 VM_SAFEPARMCOUNTRANGE(2, 3, VM_strncmp);
4969 s1 = PRVM_G_STRING(OFS_PARM0);
4970 s2 = PRVM_G_STRING(OFS_PARM1);
4973 PRVM_G_FLOAT(OFS_RETURN) = strncmp(s1, s2, (size_t)PRVM_G_FLOAT(OFS_PARM2));
4977 PRVM_G_FLOAT(OFS_RETURN) = strcmp(s1, s2);
4981 // #229 float(string s1, string s2) strcasecmp (FTE_STRINGS)
4982 // #230 float(string s1, string s2, float len) strncasecmp (FTE_STRINGS)
4983 void VM_strncasecmp (void)
4985 const char *s1, *s2;
4986 VM_SAFEPARMCOUNTRANGE(2, 3, VM_strncasecmp);
4987 s1 = PRVM_G_STRING(OFS_PARM0);
4988 s2 = PRVM_G_STRING(OFS_PARM1);
4991 PRVM_G_FLOAT(OFS_RETURN) = strncasecmp(s1, s2, (size_t)PRVM_G_FLOAT(OFS_PARM2));
4995 PRVM_G_FLOAT(OFS_RETURN) = strcasecmp(s1, s2);
4999 // #494 float(float caseinsensitive, string s, ...) crc16
5003 static char s[VM_STRINGTEMP_LENGTH];
5004 VM_SAFEPARMCOUNTRANGE(2, 8, VM_hash);
5005 insensitive = PRVM_G_FLOAT(OFS_PARM0);
5006 VM_VarString(1, s, sizeof(s));
5007 PRVM_G_FLOAT(OFS_RETURN) = (unsigned short) ((insensitive ? CRC_Block_CaseInsensitive : CRC_Block) ((unsigned char *) s, strlen(s)));
5010 void VM_wasfreed (void)
5012 VM_SAFEPARMCOUNT(1, VM_wasfreed);
5013 PRVM_G_FLOAT(OFS_RETURN) = PRVM_G_EDICT(OFS_PARM0)->priv.required->free;
5016 void VM_SetTraceGlobals(const trace_t *trace)
5019 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_allsolid)))
5020 val->_float = trace->allsolid;
5021 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_startsolid)))
5022 val->_float = trace->startsolid;
5023 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_fraction)))
5024 val->_float = trace->fraction;
5025 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_inwater)))
5026 val->_float = trace->inwater;
5027 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_inopen)))
5028 val->_float = trace->inopen;
5029 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_endpos)))
5030 VectorCopy(trace->endpos, val->vector);
5031 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_plane_normal)))
5032 VectorCopy(trace->plane.normal, val->vector);
5033 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_plane_dist)))
5034 val->_float = trace->plane.dist;
5035 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_ent)))
5036 val->edict = PRVM_EDICT_TO_PROG(trace->ent ? trace->ent : prog->edicts);
5037 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dpstartcontents)))
5038 val->_float = trace->startsupercontents;
5039 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dphitcontents)))
5040 val->_float = trace->hitsupercontents;
5041 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dphitq3surfaceflags)))
5042 val->_float = trace->hitq3surfaceflags;
5043 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dphittexturename)))
5044 val->string = trace->hittexture ? PRVM_SetTempString(trace->hittexture->name) : 0;
5049 void VM_Cmd_Init(void)
5051 // only init the stuff for the current prog
5055 // VM_BufStr_Init();
5058 void VM_Cmd_Reset(void)
5060 CL_PurgeOwner( MENUOWNER );
5062 VM_Files_CloseAll();
5064 // VM_BufStr_ShutDown();
5067 // #510 string(string input, ...) uri_escape (DP_QC_URI_ESCAPE)
5068 // does URI escaping on a string (replace evil stuff by %AB escapes)
5069 void VM_uri_escape (void)
5071 char src[VM_STRINGTEMP_LENGTH];
5072 char dest[VM_STRINGTEMP_LENGTH];
5074 static const char *hex = "0123456789ABCDEF";
5076 VM_SAFEPARMCOUNTRANGE(1, 8, VM_uri_escape);
5077 VM_VarString(0, src, sizeof(src));
5079 for(p = src, q = dest; *p && q < dest + sizeof(dest) - 3; ++p)
5081 if((*p >= 'A' && *p <= 'Z')
5082 || (*p >= 'a' && *p <= 'z')
5083 || (*p >= '0' && *p <= '9')
5084 || (*p == '-') || (*p == '_') || (*p == '.')
5085 || (*p == '!') || (*p == '~') || (*p == '*')
5086 || (*p == '\'') || (*p == '(') || (*p == ')'))
5091 *q++ = hex[(*(unsigned char *)p >> 4) & 0xF];
5092 *q++ = hex[ *(unsigned char *)p & 0xF];
5097 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(dest);
5100 // #510 string(string input, ...) uri_unescape (DP_QC_URI_ESCAPE)
5101 // does URI unescaping on a string (get back the evil stuff)
5102 void VM_uri_unescape (void)
5104 char src[VM_STRINGTEMP_LENGTH];
5105 char dest[VM_STRINGTEMP_LENGTH];
5109 VM_SAFEPARMCOUNTRANGE(1, 8, VM_uri_unescape);
5110 VM_VarString(0, src, sizeof(src));
5112 for(p = src, q = dest; *p; ) // no need to check size, because unescape can't expand
5116 if(p[1] >= '0' && p[1] <= '9')
5118 else if(p[1] >= 'a' && p[1] <= 'f')
5119 hi = p[1] - 'a' + 10;
5120 else if(p[1] >= 'A' && p[1] <= 'F')
5121 hi = p[1] - 'A' + 10;
5124 if(p[2] >= '0' && p[2] <= '9')
5126 else if(p[2] >= 'a' && p[2] <= 'f')
5127 lo = p[2] - 'a' + 10;
5128 else if(p[2] >= 'A' && p[2] <= 'F')
5129 lo = p[2] - 'A' + 10;
5132 if(hi != 0 || lo != 0) // don't unescape NUL bytes
5133 *q++ = (char) (hi * 0x10 + lo);
5144 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(dest);
5147 // #502 string(string filename) whichpack (DP_QC_WHICHPACK)
5148 // returns the name of the pack containing a file, or "" if it is not in any pack (but local or non-existant)
5149 void VM_whichpack (void)
5151 const char *fn, *pack;
5153 VM_SAFEPARMCOUNT(1, VM_whichpack);
5154 fn = PRVM_G_STRING(OFS_PARM0);
5155 pack = FS_WhichPack(fn);
5157 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(pack ? pack : "");
5165 char buffer[MAX_INPUTLINE];
5169 static void uri_to_string_callback(int status, size_t length_received, unsigned char *buffer, void *cbdata)
5171 uri_to_prog_t *handle = (uri_to_prog_t *) cbdata;
5173 if(!PRVM_ProgLoaded(handle->prognr))
5175 // curl reply came too late... so just drop it
5180 PRVM_SetProg(handle->prognr);
5182 if((prog->starttime == handle->starttime) && (prog->funcoffsets.URI_Get_Callback))
5184 if(length_received >= sizeof(handle->buffer))
5185 length_received = sizeof(handle->buffer) - 1;
5186 handle->buffer[length_received] = 0;
5188 PRVM_G_FLOAT(OFS_PARM0) = handle->id;
5189 PRVM_G_FLOAT(OFS_PARM1) = status;
5190 PRVM_G_INT(OFS_PARM2) = PRVM_SetTempString(handle->buffer);
5191 PRVM_ExecuteProgram(prog->funcoffsets.URI_Get_Callback, "QC function URI_Get_Callback is missing");
5198 // uri_get() gets content from an URL and calls a callback "uri_get_callback" with it set as string; an unique ID of the transfer is returned
5199 // returns 1 on success, and then calls the callback with the ID, 0 or the HTTP status code, and the received data in a string
5200 void VM_uri_get (void)
5205 uri_to_prog_t *handle;
5207 if(!prog->funcoffsets.URI_Get_Callback)
5208 PRVM_ERROR("uri_get called by %s without URI_Get_Callback defined", PRVM_NAME);
5210 VM_SAFEPARMCOUNT(2, VM_uri_get);
5212 url = PRVM_G_STRING(OFS_PARM0);
5213 id = PRVM_G_FLOAT(OFS_PARM1);
5214 handle = (uri_to_prog_t *) Z_Malloc(sizeof(*handle)); // this can't be the prog's mem pool, as curl may call the callback later!
5216 handle->prognr = PRVM_GetProgNr();
5217 handle->starttime = prog->starttime;
5219 ret = Curl_Begin_ToMemory(url, (unsigned char *) handle->buffer, sizeof(handle->buffer), uri_to_string_callback, handle);
5222 PRVM_G_INT(OFS_RETURN) = 1;
5227 PRVM_G_INT(OFS_RETURN) = 0;
5231 void VM_netaddress_resolve (void)
5234 char normalized[128];
5236 lhnetaddress_t addr;
5238 VM_SAFEPARMCOUNTRANGE(1, 2, VM_netaddress_resolve);
5240 ip = PRVM_G_STRING(OFS_PARM0);
5243 port = (int) PRVM_G_FLOAT(OFS_PARM1);
5245 if(LHNETADDRESS_FromString(&addr, ip, port) && LHNETADDRESS_ToString(&addr, normalized, sizeof(normalized), prog->argc > 1))
5246 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(normalized);
5248 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString("");
5251 //string(void) getextresponse = #624; // returns the next extResponse packet that was sent to this client
5252 void VM_getextresponse (void)
5254 VM_SAFEPARMCOUNT(0,VM_argv);
5256 if (net_extresponse_count <= 0)
5257 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
5261 --net_extresponse_count;
5262 first = (net_extresponse_last + NET_EXTRESPONSE_MAX - net_extresponse_count) % NET_EXTRESPONSE_MAX;
5263 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(net_extresponse[first]);