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
12 extern cvar_t prvm_backtraceforwarnings;
14 // LordHavoc: changed this to NOT use a return statement, so that it can be used in functions that must return a value
15 void VM_Warning(const char *fmt, ...)
18 char msg[MAX_INPUTLINE];
19 static double recursive = -1;
22 dpvsnprintf(msg,sizeof(msg),fmt,argptr);
27 // TODO: either add a cvar/cmd to control the state dumping or replace some of the calls with Con_Printf [9/13/2006 Black]
28 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
37 //============================================================================
40 // TODO DONE: move vm_files and vm_fssearchlist to prvm_prog_t struct
41 // TODO: move vm_files and vm_fssearchlist back [9/13/2006 Black]
42 // TODO: (move vm_files and vm_fssearchlist to prvm_prog_t struct again) [2007-01-23 LordHavoc]
43 // TODO: will this war ever end? [2007-01-23 LordHavoc]
45 void VM_CheckEmptyString (const char *s)
48 PRVM_ERROR ("%s: Bad string", PRVM_NAME);
51 //============================================================================
54 void VM_VarString(int first, char *out, int outlength)
60 outend = out + outlength - 1;
61 for (i = first;i < prog->argc && out < outend;i++)
63 s = PRVM_G_STRING((OFS_PARM0+i*3));
64 while (out < outend && *s)
74 returns true if the extension is supported by the server
76 checkextension(extensionname)
80 // kind of helper function
81 static qboolean checkextension(const char *name)
85 len = (int)strlen(name);
87 for (e = prog->extensionstring;*e;e++)
94 while (*e && *e != ' ')
96 if ((e - start) == len && !strncasecmp(start, name, len))
102 void VM_checkextension (void)
104 VM_SAFEPARMCOUNT(1,VM_checkextension);
106 PRVM_G_FLOAT(OFS_RETURN) = checkextension(PRVM_G_STRING(OFS_PARM0));
113 This is a TERMINAL error, which will kill off the entire prog.
122 char string[VM_STRINGTEMP_LENGTH];
124 VM_VarString(0, string, sizeof(string));
125 Con_Printf("======%s ERROR in %s:\n%s\n", PRVM_NAME, PRVM_GetString(prog->xfunction->s_name), string);
126 if (prog->globaloffsets.self >= 0)
128 ed = PRVM_PROG_TO_EDICT(PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict);
129 PRVM_ED_Print(ed, NULL);
132 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);
139 Dumps out self, then an error message. The program is aborted and self is
140 removed, but the level can continue.
145 void VM_objerror (void)
148 char string[VM_STRINGTEMP_LENGTH];
150 VM_VarString(0, string, sizeof(string));
151 Con_Printf("======OBJECT ERROR======\n"); // , PRVM_NAME, PRVM_GetString(prog->xfunction->s_name), string); // or include them? FIXME
152 if (prog->globaloffsets.self >= 0)
154 ed = PRVM_PROG_TO_EDICT(PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict);
155 PRVM_ED_Print(ed, NULL);
160 // objerror has to display the object fields -> else call
161 PRVM_ERROR ("VM_objecterror: self not defined !");
162 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);
176 char string[VM_STRINGTEMP_LENGTH];
178 VM_VarString(0, string, sizeof(string));
186 broadcast print to everyone on server
191 void VM_bprint (void)
193 char string[VM_STRINGTEMP_LENGTH];
197 VM_Warning("VM_bprint: game is not server(%s) !\n", PRVM_NAME);
201 VM_VarString(0, string, sizeof(string));
202 SV_BroadcastPrint(string);
207 VM_sprint (menu & client but only if server.active == true)
209 single print to a specific client
211 sprint(float clientnum,...[string])
214 void VM_sprint (void)
218 char string[VM_STRINGTEMP_LENGTH];
220 VM_SAFEPARMCOUNTRANGE(1, 8, VM_sprint);
222 //find client for this entity
223 clientnum = (int)PRVM_G_FLOAT(OFS_PARM0);
224 if (!sv.active || clientnum < 0 || clientnum >= svs.maxclients || !svs.clients[clientnum].active)
226 VM_Warning("VM_sprint: %s: invalid client or server is not active !\n", PRVM_NAME);
230 client = svs.clients + clientnum;
231 if (!client->netconnection)
234 VM_VarString(1, string, sizeof(string));
235 MSG_WriteChar(&client->netconnection->message,svc_print);
236 MSG_WriteString(&client->netconnection->message, string);
243 single print to the screen
248 void VM_centerprint (void)
250 char string[VM_STRINGTEMP_LENGTH];
252 VM_SAFEPARMCOUNTRANGE(1, 8, VM_centerprint);
253 VM_VarString(0, string, sizeof(string));
254 SCR_CenterPrint(string);
261 vector normalize(vector)
264 void VM_normalize (void)
270 VM_SAFEPARMCOUNT(1,VM_normalize);
272 value1 = PRVM_G_VECTOR(OFS_PARM0);
274 f = VectorLength2(value1);
278 VectorScale(value1, f, newvalue);
281 VectorClear(newvalue);
283 VectorCopy (newvalue, PRVM_G_VECTOR(OFS_RETURN));
295 VM_SAFEPARMCOUNT(1,VM_vlen);
296 PRVM_G_FLOAT(OFS_RETURN) = VectorLength(PRVM_G_VECTOR(OFS_PARM0));
303 float vectoyaw(vector)
306 void VM_vectoyaw (void)
311 VM_SAFEPARMCOUNT(1,VM_vectoyaw);
313 value1 = PRVM_G_VECTOR(OFS_PARM0);
315 if (value1[1] == 0 && value1[0] == 0)
319 yaw = (int) (atan2(value1[1], value1[0]) * 180 / M_PI);
324 PRVM_G_FLOAT(OFS_RETURN) = yaw;
332 vector vectoangles(vector[, vector])
335 void VM_vectoangles (void)
337 VM_SAFEPARMCOUNTRANGE(1, 2,VM_vectoangles);
339 AnglesFromVectors(PRVM_G_VECTOR(OFS_RETURN), PRVM_G_VECTOR(OFS_PARM0), prog->argc >= 2 ? PRVM_G_VECTOR(OFS_PARM1) : NULL, true);
346 Returns a number from 0<= num < 1
351 void VM_random (void)
353 VM_SAFEPARMCOUNT(0,VM_random);
355 PRVM_G_FLOAT(OFS_RETURN) = lhrandom(0, 1);
362 localsound(string sample)
365 void VM_localsound(void)
369 VM_SAFEPARMCOUNT(1,VM_localsound);
371 s = PRVM_G_STRING(OFS_PARM0);
373 if(!S_LocalSound (s))
375 PRVM_G_FLOAT(OFS_RETURN) = -4;
376 VM_Warning("VM_localsound: Failed to play %s for %s !\n", s, PRVM_NAME);
380 PRVM_G_FLOAT(OFS_RETURN) = 1;
392 PRVM_ERROR ("%s: break statement", PRVM_NAME);
395 //============================================================================
401 Sends text over to the client's execution buffer
403 [localcmd (string, ...) or]
407 void VM_localcmd (void)
409 char string[VM_STRINGTEMP_LENGTH];
410 VM_SAFEPARMCOUNTRANGE(1, 8, VM_localcmd);
411 VM_VarString(0, string, sizeof(string));
412 Cbuf_AddText(string);
424 char string[VM_STRINGTEMP_LENGTH];
425 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar);
426 VM_VarString(0, string, sizeof(string));
427 VM_CheckEmptyString(string);
428 PRVM_G_FLOAT(OFS_RETURN) = Cvar_VariableValue(string);
435 float cvar_type (string)
436 float CVAR_TYPEFLAG_EXISTS = 1;
437 float CVAR_TYPEFLAG_SAVED = 2;
438 float CVAR_TYPEFLAG_PRIVATE = 4;
439 float CVAR_TYPEFLAG_ENGINE = 8;
440 float CVAR_TYPEFLAG_HASDESCRIPTION = 16;
443 void VM_cvar_type (void)
445 char string[VM_STRINGTEMP_LENGTH];
449 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar);
450 VM_VarString(0, string, sizeof(string));
451 VM_CheckEmptyString(string);
452 cvar = Cvar_FindVar(string);
457 PRVM_G_FLOAT(OFS_RETURN) = 0;
458 return; // CVAR_TYPE_NONE
461 ret = 1; // CVAR_EXISTS
462 if(cvar->flags & CVAR_SAVE)
463 ret |= 2; // CVAR_TYPE_SAVED
464 if(cvar->flags & CVAR_PRIVATE)
465 ret |= 4; // CVAR_TYPE_PRIVATE
466 if(!(cvar->flags & CVAR_ALLOCATED))
467 ret |= 8; // CVAR_TYPE_ENGINE
468 if(strcmp(cvar->description, "custom cvar")) // has to match Cvar_Get's placeholder string
469 ret |= 16; // CVAR_TYPE_HASDESCRIPTION
471 PRVM_G_FLOAT(OFS_RETURN) = ret;
478 const string VM_cvar_string (string, ...)
481 void VM_cvar_string(void)
483 char string[VM_STRINGTEMP_LENGTH];
484 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar_string);
485 VM_VarString(0, string, sizeof(string));
486 VM_CheckEmptyString(string);
487 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(Cvar_VariableString(string));
492 ========================
495 const string VM_cvar_defstring (string, ...)
496 ========================
498 void VM_cvar_defstring (void)
500 char string[VM_STRINGTEMP_LENGTH];
501 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar_defstring);
502 VM_VarString(0, string, sizeof(string));
503 VM_CheckEmptyString(string);
504 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(Cvar_VariableDefString(string));
510 void cvar_set (string,string, ...)
513 void VM_cvar_set (void)
516 char string[VM_STRINGTEMP_LENGTH];
517 VM_SAFEPARMCOUNTRANGE(2,8,VM_cvar_set);
518 VM_VarString(1, string, sizeof(string));
519 name = PRVM_G_STRING(OFS_PARM0);
520 VM_CheckEmptyString(name);
521 Cvar_Set(name, string);
531 void VM_dprint (void)
533 char string[VM_STRINGTEMP_LENGTH];
534 VM_SAFEPARMCOUNTRANGE(1, 8, VM_dprint);
535 if (developer.integer)
537 VM_VarString(0, string, sizeof(string));
539 Con_Printf("%s", string);
541 Con_Printf("%s: %s", PRVM_NAME, string);
559 VM_SAFEPARMCOUNT(1, VM_ftos);
561 v = PRVM_G_FLOAT(OFS_PARM0);
563 if ((float)((int)v) == v)
564 dpsnprintf(s, sizeof(s), "%i", (int)v);
566 dpsnprintf(s, sizeof(s), "%f", v);
567 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(s);
582 VM_SAFEPARMCOUNT(1,VM_fabs);
584 v = PRVM_G_FLOAT(OFS_PARM0);
585 PRVM_G_FLOAT(OFS_RETURN) = fabs(v);
600 VM_SAFEPARMCOUNT(1,VM_vtos);
602 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]);
603 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(s);
618 VM_SAFEPARMCOUNT(1, VM_etos);
620 dpsnprintf (s, sizeof(s), "entity %i", PRVM_G_EDICTNUM(OFS_PARM0));
621 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(s);
628 float stof(...[string])
633 char string[VM_STRINGTEMP_LENGTH];
634 VM_SAFEPARMCOUNTRANGE(1, 8, VM_stof);
635 VM_VarString(0, string, sizeof(string));
636 PRVM_G_FLOAT(OFS_RETURN) = atof(string);
640 ========================
644 ========================
648 VM_SAFEPARMCOUNT(1, VM_itof);
649 PRVM_G_FLOAT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
653 ========================
656 entity ftoe(float num)
657 ========================
662 VM_SAFEPARMCOUNT(1, VM_ftoe);
664 ent = (int)PRVM_G_FLOAT(OFS_PARM0);
665 if (ent < 0 || ent >= MAX_EDICTS || PRVM_PROG_TO_EDICT(ent)->priv.required->free)
666 ent = 0; // return world instead of a free or invalid entity
668 PRVM_G_INT(OFS_RETURN) = ent;
672 ========================
675 float etof(entity ent)
676 ========================
680 VM_SAFEPARMCOUNT(1, VM_etof);
681 PRVM_G_FLOAT(OFS_RETURN) = PRVM_G_EDICTNUM(OFS_PARM0);
688 string strftime(float uselocaltime, string[, string ...])
691 void VM_strftime(void)
700 char fmt[VM_STRINGTEMP_LENGTH];
701 char result[VM_STRINGTEMP_LENGTH];
702 VM_SAFEPARMCOUNTRANGE(2, 8, VM_strftime);
703 VM_VarString(1, fmt, sizeof(fmt));
706 if (PRVM_G_FLOAT(OFS_PARM0))
707 tmresult = localtime_s(&tm, &t);
709 tmresult = gmtime_s(&tm, &t);
712 if (PRVM_G_FLOAT(OFS_PARM0))
719 PRVM_G_INT(OFS_RETURN) = 0;
723 strftime(result, sizeof(result), fmt, &tm);
725 strftime(result, sizeof(result), fmt, tm);
727 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(result);
741 VM_SAFEPARMCOUNT(0, VM_spawn);
742 prog->xfunction->builtinsprofile += 20;
743 ed = PRVM_ED_Alloc();
755 void VM_remove (void)
758 prog->xfunction->builtinsprofile += 20;
760 VM_SAFEPARMCOUNT(1, VM_remove);
762 ed = PRVM_G_EDICT(OFS_PARM0);
763 if( PRVM_NUM_FOR_EDICT(ed) <= prog->reserved_edicts )
765 if (developer.integer >= 1)
766 VM_Warning( "VM_remove: tried to remove the null entity or a reserved entity!\n" );
768 else if( ed->priv.required->free )
770 if (developer.integer >= 1)
771 VM_Warning( "VM_remove: tried to remove an already freed entity!\n" );
781 entity find(entity start, .string field, string match)
792 VM_SAFEPARMCOUNT(3,VM_find);
794 e = PRVM_G_EDICTNUM(OFS_PARM0);
795 f = PRVM_G_INT(OFS_PARM1);
796 s = PRVM_G_STRING(OFS_PARM2);
798 // LordHavoc: apparently BloodMage does a find(world, weaponmodel, "") and
799 // expects it to find all the monsters, so we must be careful to support
802 for (e++ ; e < prog->num_edicts ; e++)
804 prog->xfunction->builtinsprofile++;
805 ed = PRVM_EDICT_NUM(e);
806 if (ed->priv.required->free)
808 t = PRVM_E_STRING(ed,f);
818 VM_RETURN_EDICT(prog->edicts);
825 entity findfloat(entity start, .float field, float match)
826 entity findentity(entity start, .entity field, entity match)
829 // LordHavoc: added this for searching float, int, and entity reference fields
830 void VM_findfloat (void)
837 VM_SAFEPARMCOUNT(3,VM_findfloat);
839 e = PRVM_G_EDICTNUM(OFS_PARM0);
840 f = PRVM_G_INT(OFS_PARM1);
841 s = PRVM_G_FLOAT(OFS_PARM2);
843 for (e++ ; e < prog->num_edicts ; e++)
845 prog->xfunction->builtinsprofile++;
846 ed = PRVM_EDICT_NUM(e);
847 if (ed->priv.required->free)
849 if (PRVM_E_FLOAT(ed,f) == s)
856 VM_RETURN_EDICT(prog->edicts);
863 entity findchain(.string field, string match)
866 // chained search for strings in entity fields
867 // entity(.string field, string match) findchain = #402;
868 void VM_findchain (void)
873 prvm_edict_t *ent, *chain;
875 VM_SAFEPARMCOUNT(2,VM_findchain);
877 if (prog->fieldoffsets.chain < 0)
878 PRVM_ERROR("VM_findchain: %s doesnt have a chain field !", PRVM_NAME);
880 chain = prog->edicts;
882 f = PRVM_G_INT(OFS_PARM0);
883 s = PRVM_G_STRING(OFS_PARM1);
885 // LordHavoc: apparently BloodMage does a find(world, weaponmodel, "") and
886 // expects it to find all the monsters, so we must be careful to support
889 ent = PRVM_NEXT_EDICT(prog->edicts);
890 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
892 prog->xfunction->builtinsprofile++;
893 if (ent->priv.required->free)
895 t = PRVM_E_STRING(ent,f);
901 PRVM_EDICTFIELDVALUE(ent,prog->fieldoffsets.chain)->edict = PRVM_NUM_FOR_EDICT(chain);
905 VM_RETURN_EDICT(chain);
912 entity findchainfloat(.string field, float match)
913 entity findchainentity(.string field, entity match)
916 // LordHavoc: chained search for float, int, and entity reference fields
917 // entity(.string field, float match) findchainfloat = #403;
918 void VM_findchainfloat (void)
923 prvm_edict_t *ent, *chain;
925 VM_SAFEPARMCOUNT(2, VM_findchainfloat);
927 if (prog->fieldoffsets.chain < 0)
928 PRVM_ERROR("VM_findchainfloat: %s doesnt have a chain field !", PRVM_NAME);
930 chain = (prvm_edict_t *)prog->edicts;
932 f = PRVM_G_INT(OFS_PARM0);
933 s = PRVM_G_FLOAT(OFS_PARM1);
935 ent = PRVM_NEXT_EDICT(prog->edicts);
936 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
938 prog->xfunction->builtinsprofile++;
939 if (ent->priv.required->free)
941 if (PRVM_E_FLOAT(ent,f) != s)
944 PRVM_EDICTFIELDVALUE(ent,prog->fieldoffsets.chain)->edict = PRVM_EDICT_TO_PROG(chain);
948 VM_RETURN_EDICT(chain);
952 ========================
955 entity findflags(entity start, .float field, float match)
956 ========================
958 // LordHavoc: search for flags in float fields
959 void VM_findflags (void)
966 VM_SAFEPARMCOUNT(3, VM_findflags);
969 e = PRVM_G_EDICTNUM(OFS_PARM0);
970 f = PRVM_G_INT(OFS_PARM1);
971 s = (int)PRVM_G_FLOAT(OFS_PARM2);
973 for (e++ ; e < prog->num_edicts ; e++)
975 prog->xfunction->builtinsprofile++;
976 ed = PRVM_EDICT_NUM(e);
977 if (ed->priv.required->free)
979 if (!PRVM_E_FLOAT(ed,f))
981 if ((int)PRVM_E_FLOAT(ed,f) & s)
988 VM_RETURN_EDICT(prog->edicts);
992 ========================
995 entity findchainflags(.float field, float match)
996 ========================
998 // LordHavoc: chained search for flags in float fields
999 void VM_findchainflags (void)
1004 prvm_edict_t *ent, *chain;
1006 VM_SAFEPARMCOUNT(2, VM_findchainflags);
1008 if (prog->fieldoffsets.chain < 0)
1009 PRVM_ERROR("VM_findchainflags: %s doesnt have a chain field !", PRVM_NAME);
1011 chain = (prvm_edict_t *)prog->edicts;
1013 f = PRVM_G_INT(OFS_PARM0);
1014 s = (int)PRVM_G_FLOAT(OFS_PARM1);
1016 ent = PRVM_NEXT_EDICT(prog->edicts);
1017 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
1019 prog->xfunction->builtinsprofile++;
1020 if (ent->priv.required->free)
1022 if (!PRVM_E_FLOAT(ent,f))
1024 if (!((int)PRVM_E_FLOAT(ent,f) & s))
1027 PRVM_EDICTFIELDVALUE(ent,prog->fieldoffsets.chain)->edict = PRVM_EDICT_TO_PROG(chain);
1031 VM_RETURN_EDICT(chain);
1038 string precache_sound (string sample)
1041 void VM_precache_sound (void)
1045 VM_SAFEPARMCOUNT(1, VM_precache_sound);
1047 s = PRVM_G_STRING(OFS_PARM0);
1048 PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
1049 VM_CheckEmptyString(s);
1051 if(snd_initialized.integer && !S_PrecacheSound(s, true, false))
1053 VM_Warning("VM_precache_sound: Failed to load %s for %s\n", s, PRVM_NAME);
1062 returns the same string as output
1064 does nothing, only used by qcc to build .pak archives
1067 void VM_precache_file (void)
1069 VM_SAFEPARMCOUNT(1,VM_precache_file);
1070 // precache_file is only used to copy files with qcc, it does nothing
1071 PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
1081 void VM_coredump (void)
1083 VM_SAFEPARMCOUNT(0,VM_coredump);
1085 Cbuf_AddText("prvm_edicts ");
1086 Cbuf_AddText(PRVM_NAME);
1097 void PRVM_StackTrace(void);
1098 void VM_stackdump (void)
1100 VM_SAFEPARMCOUNT(0, VM_stackdump);
1115 VM_SAFEPARMCOUNT(0, VM_crash);
1117 PRVM_ERROR("Crash called by %s",PRVM_NAME);
1127 void VM_traceon (void)
1129 VM_SAFEPARMCOUNT(0,VM_traceon);
1141 void VM_traceoff (void)
1143 VM_SAFEPARMCOUNT(0,VM_traceoff);
1145 prog->trace = false;
1155 void VM_eprint (void)
1157 VM_SAFEPARMCOUNT(1,VM_eprint);
1159 PRVM_ED_PrintNum (PRVM_G_EDICTNUM(OFS_PARM0), NULL);
1172 VM_SAFEPARMCOUNT(1,VM_rint);
1174 f = PRVM_G_FLOAT(OFS_PARM0);
1176 PRVM_G_FLOAT(OFS_RETURN) = floor(f + 0.5);
1178 PRVM_G_FLOAT(OFS_RETURN) = ceil(f - 0.5);
1188 void VM_floor (void)
1190 VM_SAFEPARMCOUNT(1,VM_floor);
1192 PRVM_G_FLOAT(OFS_RETURN) = floor(PRVM_G_FLOAT(OFS_PARM0));
1204 VM_SAFEPARMCOUNT(1,VM_ceil);
1206 PRVM_G_FLOAT(OFS_RETURN) = ceil(PRVM_G_FLOAT(OFS_PARM0));
1214 entity nextent(entity)
1217 void VM_nextent (void)
1222 VM_SAFEPARMCOUNT(1, VM_nextent);
1224 i = PRVM_G_EDICTNUM(OFS_PARM0);
1227 prog->xfunction->builtinsprofile++;
1229 if (i == prog->num_edicts)
1231 VM_RETURN_EDICT(prog->edicts);
1234 ent = PRVM_EDICT_NUM(i);
1235 if (!ent->priv.required->free)
1237 VM_RETURN_EDICT(ent);
1243 //=============================================================================
1250 changelevel(string map)
1253 void VM_changelevel (void)
1255 VM_SAFEPARMCOUNT(1, VM_changelevel);
1259 VM_Warning("VM_changelevel: game is not server (%s)\n", PRVM_NAME);
1263 // make sure we don't issue two changelevels
1264 if (svs.changelevel_issued)
1266 svs.changelevel_issued = true;
1268 Cbuf_AddText (va("changelevel %s\n",PRVM_G_STRING(OFS_PARM0)));
1280 VM_SAFEPARMCOUNT(1,VM_sin);
1281 PRVM_G_FLOAT(OFS_RETURN) = sin(PRVM_G_FLOAT(OFS_PARM0));
1292 VM_SAFEPARMCOUNT(1,VM_cos);
1293 PRVM_G_FLOAT(OFS_RETURN) = cos(PRVM_G_FLOAT(OFS_PARM0));
1305 VM_SAFEPARMCOUNT(1,VM_sqrt);
1306 PRVM_G_FLOAT(OFS_RETURN) = sqrt(PRVM_G_FLOAT(OFS_PARM0));
1318 VM_SAFEPARMCOUNT(1,VM_asin);
1319 PRVM_G_FLOAT(OFS_RETURN) = asin(PRVM_G_FLOAT(OFS_PARM0));
1330 VM_SAFEPARMCOUNT(1,VM_acos);
1331 PRVM_G_FLOAT(OFS_RETURN) = acos(PRVM_G_FLOAT(OFS_PARM0));
1342 VM_SAFEPARMCOUNT(1,VM_atan);
1343 PRVM_G_FLOAT(OFS_RETURN) = atan(PRVM_G_FLOAT(OFS_PARM0));
1349 float atan2(float,float)
1352 void VM_atan2 (void)
1354 VM_SAFEPARMCOUNT(2,VM_atan2);
1355 PRVM_G_FLOAT(OFS_RETURN) = atan2(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1366 VM_SAFEPARMCOUNT(1,VM_tan);
1367 PRVM_G_FLOAT(OFS_RETURN) = tan(PRVM_G_FLOAT(OFS_PARM0));
1374 Returns a vector of length < 1 and > 0
1379 void VM_randomvec (void)
1384 VM_SAFEPARMCOUNT(0, VM_randomvec);
1389 temp[0] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1390 temp[1] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1391 temp[2] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1393 while (DotProduct(temp, temp) >= 1);
1394 VectorCopy (temp, PRVM_G_VECTOR(OFS_RETURN));
1397 temp[0] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1398 temp[1] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1399 temp[2] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1400 // length returned always > 0
1401 length = (rand()&32766 + 1) * (1.0 / 32767.0) / VectorLength(temp);
1402 VectorScale(temp,length, temp);*/
1403 //VectorCopy(temp, PRVM_G_VECTOR(OFS_RETURN));
1406 //=============================================================================
1412 float registercvar (string name, string value[, float flags])
1415 void VM_registercvar (void)
1417 const char *name, *value;
1420 VM_SAFEPARMCOUNTRANGE(2, 3, VM_registercvar);
1422 name = PRVM_G_STRING(OFS_PARM0);
1423 value = PRVM_G_STRING(OFS_PARM1);
1424 flags = prog->argc >= 3 ? (int)PRVM_G_FLOAT(OFS_PARM2) : 0;
1425 PRVM_G_FLOAT(OFS_RETURN) = 0;
1427 if(flags > CVAR_MAXFLAGSVAL)
1430 // first check to see if it has already been defined
1431 if (Cvar_FindVar (name))
1434 // check for overlap with a command
1435 if (Cmd_Exists (name))
1437 VM_Warning("VM_registercvar: %s is a command\n", name);
1441 Cvar_Get(name, value, flags);
1443 PRVM_G_FLOAT(OFS_RETURN) = 1; // success
1451 returns the minimum of two supplied floats
1453 float min(float a, float b, ...[float])
1458 VM_SAFEPARMCOUNTRANGE(2, 8, VM_min);
1459 // LordHavoc: 3+ argument enhancement suggested by FrikaC
1460 if (prog->argc >= 3)
1463 float f = PRVM_G_FLOAT(OFS_PARM0);
1464 for (i = 1;i < prog->argc;i++)
1465 if (f > PRVM_G_FLOAT((OFS_PARM0+i*3)))
1466 f = PRVM_G_FLOAT((OFS_PARM0+i*3));
1467 PRVM_G_FLOAT(OFS_RETURN) = f;
1470 PRVM_G_FLOAT(OFS_RETURN) = min(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1477 returns the maximum of two supplied floats
1479 float max(float a, float b, ...[float])
1484 VM_SAFEPARMCOUNTRANGE(2, 8, VM_max);
1485 // LordHavoc: 3+ argument enhancement suggested by FrikaC
1486 if (prog->argc >= 3)
1489 float f = PRVM_G_FLOAT(OFS_PARM0);
1490 for (i = 1;i < prog->argc;i++)
1491 if (f < PRVM_G_FLOAT((OFS_PARM0+i*3)))
1492 f = PRVM_G_FLOAT((OFS_PARM0+i*3));
1493 PRVM_G_FLOAT(OFS_RETURN) = f;
1496 PRVM_G_FLOAT(OFS_RETURN) = max(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1503 returns number bounded by supplied range
1505 float bound(float min, float value, float max)
1508 void VM_bound (void)
1510 VM_SAFEPARMCOUNT(3,VM_bound);
1511 PRVM_G_FLOAT(OFS_RETURN) = bound(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1), PRVM_G_FLOAT(OFS_PARM2));
1518 returns a raised to power b
1520 float pow(float a, float b)
1525 VM_SAFEPARMCOUNT(2,VM_pow);
1526 PRVM_G_FLOAT(OFS_RETURN) = pow(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1529 void VM_Files_Init(void)
1532 for (i = 0;i < PRVM_MAX_OPENFILES;i++)
1533 prog->openfiles[i] = NULL;
1536 void VM_Files_CloseAll(void)
1539 for (i = 0;i < PRVM_MAX_OPENFILES;i++)
1541 if (prog->openfiles[i])
1542 FS_Close(prog->openfiles[i]);
1543 prog->openfiles[i] = NULL;
1547 static qfile_t *VM_GetFileHandle( int index )
1549 if (index < 0 || index >= PRVM_MAX_OPENFILES)
1551 Con_Printf("VM_GetFileHandle: invalid file handle %i used in %s\n", index, PRVM_NAME);
1554 if (prog->openfiles[index] == NULL)
1556 Con_Printf("VM_GetFileHandle: no such file handle %i (or file has been closed) in %s\n", index, PRVM_NAME);
1559 return prog->openfiles[index];
1566 float fopen(string filename, float mode)
1569 // float(string filename, float mode) fopen = #110;
1570 // opens a file inside quake/gamedir/data/ (mode is FILE_READ, FILE_APPEND, or FILE_WRITE),
1571 // returns fhandle >= 0 if successful, or fhandle < 0 if unable to open file for any reason
1575 const char *modestring, *filename;
1577 VM_SAFEPARMCOUNT(2,VM_fopen);
1579 for (filenum = 0;filenum < PRVM_MAX_OPENFILES;filenum++)
1580 if (prog->openfiles[filenum] == NULL)
1582 if (filenum >= PRVM_MAX_OPENFILES)
1584 PRVM_G_FLOAT(OFS_RETURN) = -2;
1585 VM_Warning("VM_fopen: %s ran out of file handles (%i)\n", PRVM_NAME, PRVM_MAX_OPENFILES);
1588 filename = PRVM_G_STRING(OFS_PARM0);
1589 mode = (int)PRVM_G_FLOAT(OFS_PARM1);
1592 case 0: // FILE_READ
1594 prog->openfiles[filenum] = FS_OpenVirtualFile(va("data/%s", filename), false);
1595 if (prog->openfiles[filenum] == NULL)
1596 prog->openfiles[filenum] = FS_OpenVirtualFile(va("%s", filename), false);
1598 case 1: // FILE_APPEND
1600 prog->openfiles[filenum] = FS_OpenRealFile(va("data/%s", filename), modestring, false);
1602 case 2: // FILE_WRITE
1604 prog->openfiles[filenum] = FS_OpenRealFile(va("data/%s", filename), modestring, false);
1607 PRVM_G_FLOAT(OFS_RETURN) = -3;
1608 VM_Warning("VM_fopen: %s: no such mode %i (valid: 0 = read, 1 = append, 2 = write)\n", PRVM_NAME, mode);
1612 if (prog->openfiles[filenum] == NULL)
1614 PRVM_G_FLOAT(OFS_RETURN) = -1;
1615 if (developer.integer >= 100)
1616 VM_Warning("VM_fopen: %s: %s mode %s failed\n", PRVM_NAME, filename, modestring);
1620 PRVM_G_FLOAT(OFS_RETURN) = filenum;
1621 if (developer.integer >= 100)
1622 Con_Printf("VM_fopen: %s: %s mode %s opened as #%i\n", PRVM_NAME, filename, modestring, filenum);
1623 prog->openfiles_origin[filenum] = PRVM_AllocationOrigin();
1631 fclose(float fhandle)
1634 //void(float fhandle) fclose = #111; // closes a file
1635 void VM_fclose(void)
1639 VM_SAFEPARMCOUNT(1,VM_fclose);
1641 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1642 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
1644 VM_Warning("VM_fclose: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1647 if (prog->openfiles[filenum] == NULL)
1649 VM_Warning("VM_fclose: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1652 FS_Close(prog->openfiles[filenum]);
1653 prog->openfiles[filenum] = NULL;
1654 if(prog->openfiles_origin[filenum])
1655 PRVM_Free((char *)prog->openfiles_origin[filenum]);
1656 if (developer.integer >= 100)
1657 Con_Printf("VM_fclose: %s: #%i closed\n", PRVM_NAME, filenum);
1664 string fgets(float fhandle)
1667 //string(float fhandle) fgets = #112; // reads a line of text from the file and returns as a tempstring
1671 char string[VM_STRINGTEMP_LENGTH];
1674 VM_SAFEPARMCOUNT(1,VM_fgets);
1676 // set the return value regardless of any possible errors
1677 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
1679 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1680 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
1682 VM_Warning("VM_fgets: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1685 if (prog->openfiles[filenum] == NULL)
1687 VM_Warning("VM_fgets: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1693 c = FS_Getc(prog->openfiles[filenum]);
1694 if (c == '\r' || c == '\n' || c < 0)
1696 if (end < VM_STRINGTEMP_LENGTH - 1)
1700 // remove \n following \r
1703 c = FS_Getc(prog->openfiles[filenum]);
1705 FS_UnGetc(prog->openfiles[filenum], (unsigned char)c);
1707 if (developer.integer >= 100)
1708 Con_Printf("fgets: %s: %s\n", PRVM_NAME, string);
1710 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string);
1717 fputs(float fhandle, string s)
1720 //void(float fhandle, string s) fputs = #113; // writes a line of text to the end of the file
1724 char string[VM_STRINGTEMP_LENGTH];
1727 VM_SAFEPARMCOUNT(2,VM_fputs);
1729 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1730 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
1732 VM_Warning("VM_fputs: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1735 if (prog->openfiles[filenum] == NULL)
1737 VM_Warning("VM_fputs: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1740 VM_VarString(1, string, sizeof(string));
1741 if ((stringlength = (int)strlen(string)))
1742 FS_Write(prog->openfiles[filenum], string, stringlength);
1743 if (developer.integer >= 100)
1744 Con_Printf("fputs: %s: %s\n", PRVM_NAME, string);
1751 writetofile(float fhandle, entity ent)
1754 void VM_writetofile(void)
1759 VM_SAFEPARMCOUNT(2, VM_writetofile);
1761 file = VM_GetFileHandle( (int)PRVM_G_FLOAT(OFS_PARM0) );
1764 VM_Warning("VM_writetofile: invalid or closed file handle\n");
1768 ent = PRVM_G_EDICT(OFS_PARM1);
1769 if(ent->priv.required->free)
1771 VM_Warning("VM_writetofile: %s: entity %i is free !\n", PRVM_NAME, PRVM_NUM_FOR_EDICT(ent));
1775 PRVM_ED_Write (file, ent);
1778 // KrimZon - DP_QC_ENTITYDATA
1783 float() numentityfields
1784 Return the number of entity fields - NOT offsets
1787 void VM_numentityfields(void)
1789 PRVM_G_FLOAT(OFS_RETURN) = prog->progs->numfielddefs;
1792 // KrimZon - DP_QC_ENTITYDATA
1797 string(float fieldnum) entityfieldname
1798 Return name of the specified field as a string, or empty if the field is invalid (warning)
1801 void VM_entityfieldname(void)
1804 int i = (int)PRVM_G_FLOAT(OFS_PARM0);
1806 if (i < 0 || i >= prog->progs->numfielddefs)
1808 VM_Warning("VM_entityfieldname: %s: field index out of bounds\n", PRVM_NAME);
1809 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString("");
1813 d = &prog->fielddefs[i];
1814 PRVM_G_INT(OFS_RETURN) = d->s_name; // presuming that s_name points to a string already
1817 // KrimZon - DP_QC_ENTITYDATA
1822 float(float fieldnum) entityfieldtype
1825 void VM_entityfieldtype(void)
1828 int i = (int)PRVM_G_FLOAT(OFS_PARM0);
1830 if (i < 0 || i >= prog->progs->numfielddefs)
1832 VM_Warning("VM_entityfieldtype: %s: field index out of bounds\n", PRVM_NAME);
1833 PRVM_G_FLOAT(OFS_RETURN) = -1.0;
1837 d = &prog->fielddefs[i];
1838 PRVM_G_FLOAT(OFS_RETURN) = (float)d->type;
1841 // KrimZon - DP_QC_ENTITYDATA
1844 VM_getentityfieldstring
1846 string(float fieldnum, entity ent) getentityfieldstring
1849 void VM_getentityfieldstring(void)
1851 // put the data into a string
1856 int i = (int)PRVM_G_FLOAT(OFS_PARM0);
1858 if (i < 0 || i >= prog->progs->numfielddefs)
1860 VM_Warning("VM_entityfielddata: %s: field index out of bounds\n", PRVM_NAME);
1861 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString("");
1865 d = &prog->fielddefs[i];
1868 ent = PRVM_G_EDICT(OFS_PARM1);
1869 if(ent->priv.required->free)
1871 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString("");
1872 VM_Warning("VM_entityfielddata: %s: entity %i is free !\n", PRVM_NAME, PRVM_NUM_FOR_EDICT(ent));
1875 v = (int *)((char *)ent->fields.vp + d->ofs*4);
1877 // if it's 0 or blank, return an empty string
1878 type = d->type & ~DEF_SAVEGLOBAL;
1879 for (j=0 ; j<prvm_type_size[type] ; j++)
1882 if (j == prvm_type_size[type])
1884 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString("");
1888 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(PRVM_UglyValueString((etype_t)d->type, (prvm_eval_t *)v));
1891 // KrimZon - DP_QC_ENTITYDATA
1894 VM_putentityfieldstring
1896 float(float fieldnum, entity ent, string s) putentityfieldstring
1899 void VM_putentityfieldstring(void)
1903 int i = (int)PRVM_G_FLOAT(OFS_PARM0);
1905 if (i < 0 || i >= prog->progs->numfielddefs)
1907 VM_Warning("VM_entityfielddata: %s: field index out of bounds\n", PRVM_NAME);
1908 PRVM_G_FLOAT(OFS_RETURN) = 0.0f;
1912 d = &prog->fielddefs[i];
1915 ent = PRVM_G_EDICT(OFS_PARM1);
1916 if(ent->priv.required->free)
1918 VM_Warning("VM_entityfielddata: %s: entity %i is free !\n", PRVM_NAME, PRVM_NUM_FOR_EDICT(ent));
1919 PRVM_G_FLOAT(OFS_RETURN) = 0.0f;
1923 // parse the string into the value
1924 PRVM_G_FLOAT(OFS_RETURN) = ( PRVM_ED_ParseEpair(ent, d, PRVM_G_STRING(OFS_PARM2)) ) ? 1.0f : 0.0f;
1931 float strlen(string s)
1934 //float(string s) strlen = #114; // returns how many characters are in a string
1935 void VM_strlen(void)
1937 VM_SAFEPARMCOUNT(1,VM_strlen);
1939 PRVM_G_FLOAT(OFS_RETURN) = strlen(PRVM_G_STRING(OFS_PARM0));
1942 // DRESK - Decolorized String
1947 string strdecolorize(string s)
1950 // string (string s) strdecolorize = #472; // returns the passed in string with color codes stripped
1951 void VM_strdecolorize(void)
1953 char szNewString[VM_STRINGTEMP_LENGTH];
1954 const char *szString;
1957 VM_SAFEPARMCOUNT(1,VM_strdecolorize);
1958 szString = PRVM_G_STRING(OFS_PARM0);
1960 COM_StringDecolorize(szString, 0, szNewString, sizeof(szNewString), TRUE);
1962 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(szNewString);
1965 // DRESK - String Length (not counting color codes)
1970 float strlennocol(string s)
1973 // float(string s) strlennocol = #471; // returns how many characters are in a string not including color codes
1974 // For example, ^2Dresk returns a length of 5
1975 void VM_strlennocol(void)
1977 const char *szString;
1980 VM_SAFEPARMCOUNT(1,VM_strlennocol);
1982 szString = PRVM_G_STRING(OFS_PARM0);
1984 nCnt = COM_StringLengthNoColors(szString, 0, NULL);
1986 PRVM_G_FLOAT(OFS_RETURN) = nCnt;
1989 // DRESK - String to Uppercase and Lowercase
1994 string strtolower(string s)
1997 // string (string s) strtolower = #480; // returns passed in string in lowercase form
1998 void VM_strtolower(void)
2000 char szNewString[VM_STRINGTEMP_LENGTH];
2001 const char *szString;
2004 VM_SAFEPARMCOUNT(1,VM_strtolower);
2005 szString = PRVM_G_STRING(OFS_PARM0);
2007 COM_ToLowerString(szString, szNewString, sizeof(szNewString) );
2009 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(szNewString);
2016 string strtoupper(string s)
2019 // string (string s) strtoupper = #481; // returns passed in string in uppercase form
2020 void VM_strtoupper(void)
2022 char szNewString[VM_STRINGTEMP_LENGTH];
2023 const char *szString;
2026 VM_SAFEPARMCOUNT(1,VM_strtoupper);
2027 szString = PRVM_G_STRING(OFS_PARM0);
2029 COM_ToUpperString(szString, szNewString, sizeof(szNewString) );
2031 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(szNewString);
2038 string strcat(string,string,...[string])
2041 //string(string s1, string s2) strcat = #115;
2042 // concatenates two strings (for example "abc", "def" would return "abcdef")
2043 // and returns as a tempstring
2044 void VM_strcat(void)
2046 char s[VM_STRINGTEMP_LENGTH];
2047 VM_SAFEPARMCOUNTRANGE(1, 8, VM_strcat);
2049 VM_VarString(0, s, sizeof(s));
2050 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(s);
2057 string substring(string s, float start, float length)
2060 // string(string s, float start, float length) substring = #116;
2061 // returns a section of a string as a tempstring
2062 void VM_substring(void)
2064 int i, start, length;
2066 char string[VM_STRINGTEMP_LENGTH];
2068 VM_SAFEPARMCOUNT(3,VM_substring);
2070 s = PRVM_G_STRING(OFS_PARM0);
2071 start = (int)PRVM_G_FLOAT(OFS_PARM1);
2072 length = (int)PRVM_G_FLOAT(OFS_PARM2);
2073 for (i = 0;i < start && *s;i++, s++);
2074 for (i = 0;i < (int)sizeof(string) - 1 && *s && i < length;i++, s++)
2077 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string);
2084 string(string search, string replace, string subject) strreplace = #484;
2087 // replaces all occurrences of search with replace in the string subject, and returns the result
2088 void VM_strreplace(void)
2091 const char *search, *replace, *subject;
2092 char string[VM_STRINGTEMP_LENGTH];
2093 int search_len, replace_len, subject_len;
2095 VM_SAFEPARMCOUNT(3,VM_strreplace);
2097 search = PRVM_G_STRING(OFS_PARM0);
2098 replace = PRVM_G_STRING(OFS_PARM1);
2099 subject = PRVM_G_STRING(OFS_PARM2);
2101 search_len = (int)strlen(search);
2102 replace_len = (int)strlen(replace);
2103 subject_len = (int)strlen(subject);
2106 for (i = 0; i < subject_len; i++)
2108 for (j = 0; j < search_len && i+j < subject_len; j++)
2109 if (subject[i+j] != search[j])
2111 if (j == search_len || i+j == subject_len)
2113 // found it at offset 'i'
2114 for (j = 0; j < replace_len && si < (int)sizeof(string) - 1; j++)
2115 string[si++] = replace[j];
2116 i += search_len - 1;
2121 if (si < (int)sizeof(string) - 1)
2122 string[si++] = subject[i];
2127 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string);
2134 string(string search, string replace, string subject) strireplace = #485;
2137 // case-insensitive version of strreplace
2138 void VM_strireplace(void)
2141 const char *search, *replace, *subject;
2142 char string[VM_STRINGTEMP_LENGTH];
2143 int search_len, replace_len, subject_len;
2145 VM_SAFEPARMCOUNT(3,VM_strreplace);
2147 search = PRVM_G_STRING(OFS_PARM0);
2148 replace = PRVM_G_STRING(OFS_PARM1);
2149 subject = PRVM_G_STRING(OFS_PARM2);
2151 search_len = (int)strlen(search);
2152 replace_len = (int)strlen(replace);
2153 subject_len = (int)strlen(subject);
2156 for (i = 0; i < subject_len; i++)
2158 for (j = 0; j < search_len && i+j < subject_len; j++)
2159 if (tolower(subject[i+j]) != tolower(search[j]))
2161 if (j == search_len || i+j == subject_len)
2163 // found it at offset 'i'
2164 for (j = 0; j < replace_len && si < (int)sizeof(string) - 1; j++)
2165 string[si++] = replace[j];
2166 i += search_len - 1;
2171 if (si < (int)sizeof(string) - 1)
2172 string[si++] = subject[i];
2177 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string);
2184 vector stov(string s)
2187 //vector(string s) stov = #117; // returns vector value from a string
2190 char string[VM_STRINGTEMP_LENGTH];
2192 VM_SAFEPARMCOUNT(1,VM_stov);
2194 VM_VarString(0, string, sizeof(string));
2195 Math_atov(string, PRVM_G_VECTOR(OFS_RETURN));
2202 string strzone(string s)
2205 //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)
2206 void VM_strzone(void)
2209 char string[VM_STRINGTEMP_LENGTH];
2212 VM_SAFEPARMCOUNT(1,VM_strzone);
2214 VM_VarString(0, string, sizeof(string));
2215 alloclen = strlen(string) + 1;
2216 PRVM_G_INT(OFS_RETURN) = PRVM_AllocString(alloclen, &out);
2217 memcpy(out, string, alloclen);
2227 //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!!!)
2228 void VM_strunzone(void)
2230 VM_SAFEPARMCOUNT(1,VM_strunzone);
2231 PRVM_FreeString(PRVM_G_INT(OFS_PARM0));
2236 VM_command (used by client and menu)
2238 clientcommand(float client, string s) (for client and menu)
2241 //void(entity e, string s) clientcommand = #440; // executes a command string as if it came from the specified client
2242 //this function originally written by KrimZon, made shorter by LordHavoc
2243 void VM_clcommand (void)
2245 client_t *temp_client;
2248 VM_SAFEPARMCOUNT(2,VM_clcommand);
2250 i = (int)PRVM_G_FLOAT(OFS_PARM0);
2251 if (!sv.active || i < 0 || i >= svs.maxclients || !svs.clients[i].active)
2253 VM_Warning("VM_clientcommand: %s: invalid client/server is not active !\n", PRVM_NAME);
2257 temp_client = host_client;
2258 host_client = svs.clients + i;
2259 Cmd_ExecuteString (PRVM_G_STRING(OFS_PARM1), src_client);
2260 host_client = temp_client;
2268 float tokenize(string s)
2271 //float(string s) tokenize = #441; // takes apart a string into individal words (access them with argv), returns how many
2272 //this function originally written by KrimZon, made shorter by LordHavoc
2273 //20040203: rewritten by LordHavoc (no longer uses allocations)
2276 void VM_tokenize (void)
2279 static char string[VM_STRINGTEMP_LENGTH]; // static, because it's big
2281 VM_SAFEPARMCOUNT(1,VM_tokenize);
2283 strlcpy(string, PRVM_G_STRING(OFS_PARM0), sizeof(string));
2287 while(COM_ParseToken_VM_Tokenize(&p, false))
2289 if (num_tokens >= (int)(sizeof(tokens)/sizeof(tokens[0])))
2291 tokens[num_tokens++] = PRVM_SetTempString(com_token);
2294 PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
2299 VM_tokenizebyseparator
2301 float tokenizebyseparator(string s, string separator1, ...)
2304 //float(string s, string separator1, ...) tokenizebyseparator = #479; // takes apart a string into individal words (access them with argv), returns how many
2305 //this function returns the token preceding each instance of a separator (of
2306 //which there can be multiple), and the text following the last separator
2307 //useful for parsing certain kinds of data like IP addresses
2309 //numnumbers = tokenizebyseparator("10.1.2.3", ".");
2310 //returns 4 and the tokens "10" "1" "2" "3".
2311 void VM_tokenizebyseparator (void)
2315 int separatorlen[7];
2316 const char *separators[7];
2319 char tokentext[MAX_INPUTLINE];
2320 static char string[VM_STRINGTEMP_LENGTH]; // static, because it's big
2322 VM_SAFEPARMCOUNTRANGE(2, 8,VM_tokenizebyseparator);
2324 strlcpy(string, PRVM_G_STRING(OFS_PARM0), sizeof(string));
2328 for (j = 1;j < prog->argc;j++)
2330 // skip any blank separator strings
2331 const char *s = PRVM_G_STRING(OFS_PARM0+j*3);
2334 separators[numseparators] = s;
2335 separatorlen[numseparators] = strlen(s);
2342 while (num_tokens < (int)(sizeof(tokens)/sizeof(tokens[0])))
2344 token = tokentext + j;
2347 for (k = 0;k < numseparators;k++)
2349 if (!strncmp(p, separators[k], separatorlen[k]))
2351 p += separatorlen[k];
2355 if (k < numseparators)
2357 if (j < (int)sizeof(tokentext)-1)
2358 tokentext[j++] = *p;
2361 if (j >= (int)sizeof(tokentext))
2364 tokens[num_tokens++] = PRVM_SetTempString(token);
2369 PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
2372 //string(float n) argv = #442; // returns a word from the tokenized string (returns nothing for an invalid index)
2373 //this function originally written by KrimZon, made shorter by LordHavoc
2378 VM_SAFEPARMCOUNT(1,VM_argv);
2380 token_num = (int)PRVM_G_FLOAT(OFS_PARM0);
2382 if (token_num >= 0 && token_num < num_tokens)
2383 PRVM_G_INT(OFS_RETURN) = tokens[token_num];
2385 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
2395 void VM_isserver(void)
2397 VM_SAFEPARMCOUNT(0,VM_serverstate);
2399 PRVM_G_FLOAT(OFS_RETURN) = sv.active && (svs.maxclients > 1 || cls.state == ca_dedicated);
2409 void VM_clientcount(void)
2411 VM_SAFEPARMCOUNT(0,VM_clientcount);
2413 PRVM_G_FLOAT(OFS_RETURN) = svs.maxclients;
2423 void VM_clientstate(void)
2425 VM_SAFEPARMCOUNT(0,VM_clientstate);
2428 switch( cls.state ) {
2429 case ca_uninitialized:
2431 PRVM_G_FLOAT(OFS_RETURN) = 0;
2433 case ca_disconnected:
2434 PRVM_G_FLOAT(OFS_RETURN) = 1;
2437 PRVM_G_FLOAT(OFS_RETURN) = 2;
2440 // should never be reached!
2449 float getostype(void)
2451 */ // not used at the moment -> not included in the common list
2452 void VM_getostype(void)
2454 VM_SAFEPARMCOUNT(0,VM_getostype);
2459 OS_MAC - not supported
2463 PRVM_G_FLOAT(OFS_RETURN) = 0;
2464 #elif defined(MACOSX)
2465 PRVM_G_FLOAT(OFS_RETURN) = 2;
2467 PRVM_G_FLOAT(OFS_RETURN) = 1;
2478 void VM_gettime(void)
2480 VM_SAFEPARMCOUNT(0,VM_gettime);
2482 PRVM_G_FLOAT(OFS_RETURN) = (float) realtime;
2489 loadfromdata(string data)
2492 void VM_loadfromdata(void)
2494 VM_SAFEPARMCOUNT(1,VM_loadentsfromfile);
2496 PRVM_ED_LoadFromFile(PRVM_G_STRING(OFS_PARM0));
2500 ========================
2503 parseentitydata(entity ent, string data)
2504 ========================
2506 void VM_parseentitydata(void)
2511 VM_SAFEPARMCOUNT(2, VM_parseentitydata);
2513 // get edict and test it
2514 ent = PRVM_G_EDICT(OFS_PARM0);
2515 if (ent->priv.required->free)
2516 PRVM_ERROR ("VM_parseentitydata: %s: Can only set already spawned entities (entity %i is free)!", PRVM_NAME, PRVM_NUM_FOR_EDICT(ent));
2518 data = PRVM_G_STRING(OFS_PARM1);
2520 // parse the opening brace
2521 if (!COM_ParseToken_Simple(&data, false, false) || com_token[0] != '{' )
2522 PRVM_ERROR ("VM_parseentitydata: %s: Couldn't parse entity data:\n%s", PRVM_NAME, data );
2524 PRVM_ED_ParseEdict (data, ent);
2531 loadfromfile(string file)
2534 void VM_loadfromfile(void)
2536 const char *filename;
2539 VM_SAFEPARMCOUNT(1,VM_loadfromfile);
2541 filename = PRVM_G_STRING(OFS_PARM0);
2542 if (FS_CheckNastyPath(filename, false))
2544 PRVM_G_FLOAT(OFS_RETURN) = -4;
2545 VM_Warning("VM_loadfromfile: %s dangerous or non-portable filename \"%s\" not allowed. (contains : or \\ or begins with .. or /)\n", PRVM_NAME, filename);
2549 // not conform with VM_fopen
2550 data = (char *)FS_LoadFile(filename, tempmempool, false, NULL);
2552 PRVM_G_FLOAT(OFS_RETURN) = -1;
2554 PRVM_ED_LoadFromFile(data);
2565 float mod(float val, float m)
2568 void VM_modulo(void)
2571 VM_SAFEPARMCOUNT(2,VM_module);
2573 val = (int) PRVM_G_FLOAT(OFS_PARM0);
2574 m = (int) PRVM_G_FLOAT(OFS_PARM1);
2576 PRVM_G_FLOAT(OFS_RETURN) = (float) (val % m);
2579 void VM_Search_Init(void)
2582 for (i = 0;i < PRVM_MAX_OPENSEARCHES;i++)
2583 prog->opensearches[i] = NULL;
2586 void VM_Search_Reset(void)
2589 // reset the fssearch list
2590 for(i = 0; i < PRVM_MAX_OPENSEARCHES; i++)
2592 if(prog->opensearches[i])
2593 FS_FreeSearch(prog->opensearches[i]);
2594 prog->opensearches[i] = NULL;
2602 float search_begin(string pattern, float caseinsensitive, float quiet)
2605 void VM_search_begin(void)
2608 const char *pattern;
2609 int caseinsens, quiet;
2611 VM_SAFEPARMCOUNT(3, VM_search_begin);
2613 pattern = PRVM_G_STRING(OFS_PARM0);
2615 VM_CheckEmptyString(pattern);
2617 caseinsens = (int)PRVM_G_FLOAT(OFS_PARM1);
2618 quiet = (int)PRVM_G_FLOAT(OFS_PARM2);
2620 for(handle = 0; handle < PRVM_MAX_OPENSEARCHES; handle++)
2621 if(!prog->opensearches[handle])
2624 if(handle >= PRVM_MAX_OPENSEARCHES)
2626 PRVM_G_FLOAT(OFS_RETURN) = -2;
2627 VM_Warning("VM_search_begin: %s ran out of search handles (%i)\n", PRVM_NAME, PRVM_MAX_OPENSEARCHES);
2631 if(!(prog->opensearches[handle] = FS_Search(pattern,caseinsens, quiet)))
2632 PRVM_G_FLOAT(OFS_RETURN) = -1;
2635 prog->opensearches_origin[handle] = PRVM_AllocationOrigin();
2636 PRVM_G_FLOAT(OFS_RETURN) = handle;
2644 void search_end(float handle)
2647 void VM_search_end(void)
2650 VM_SAFEPARMCOUNT(1, VM_search_end);
2652 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
2654 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
2656 VM_Warning("VM_search_end: invalid handle %i used in %s\n", handle, PRVM_NAME);
2659 if(prog->opensearches[handle] == NULL)
2661 VM_Warning("VM_search_end: no such handle %i in %s\n", handle, PRVM_NAME);
2665 FS_FreeSearch(prog->opensearches[handle]);
2666 prog->opensearches[handle] = NULL;
2667 if(prog->opensearches_origin[handle])
2668 PRVM_Free((char *)prog->opensearches_origin[handle]);
2675 float search_getsize(float handle)
2678 void VM_search_getsize(void)
2681 VM_SAFEPARMCOUNT(1, VM_M_search_getsize);
2683 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
2685 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
2687 VM_Warning("VM_search_getsize: invalid handle %i used in %s\n", handle, PRVM_NAME);
2690 if(prog->opensearches[handle] == NULL)
2692 VM_Warning("VM_search_getsize: no such handle %i in %s\n", handle, PRVM_NAME);
2696 PRVM_G_FLOAT(OFS_RETURN) = prog->opensearches[handle]->numfilenames;
2701 VM_search_getfilename
2703 string search_getfilename(float handle, float num)
2706 void VM_search_getfilename(void)
2708 int handle, filenum;
2709 VM_SAFEPARMCOUNT(2, VM_search_getfilename);
2711 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
2712 filenum = (int)PRVM_G_FLOAT(OFS_PARM1);
2714 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
2716 VM_Warning("VM_search_getfilename: invalid handle %i used in %s\n", handle, PRVM_NAME);
2719 if(prog->opensearches[handle] == NULL)
2721 VM_Warning("VM_search_getfilename: no such handle %i in %s\n", handle, PRVM_NAME);
2724 if(filenum < 0 || filenum >= prog->opensearches[handle]->numfilenames)
2726 VM_Warning("VM_search_getfilename: invalid filenum %i in %s\n", filenum, PRVM_NAME);
2730 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog->opensearches[handle]->filenames[filenum]);
2737 string chr(float ascii)
2743 VM_SAFEPARMCOUNT(1, VM_chr);
2745 tmp[0] = (unsigned char) PRVM_G_FLOAT(OFS_PARM0);
2748 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(tmp);
2751 //=============================================================================
2752 // Draw builtins (client & menu)
2758 float iscachedpic(string pic)
2761 void VM_iscachedpic(void)
2763 VM_SAFEPARMCOUNT(1,VM_iscachedpic);
2765 // drawq hasnt such a function, thus always return true
2766 PRVM_G_FLOAT(OFS_RETURN) = false;
2773 string precache_pic(string pic)
2776 void VM_precache_pic(void)
2780 VM_SAFEPARMCOUNT(1, VM_precache_pic);
2782 s = PRVM_G_STRING(OFS_PARM0);
2783 PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
2784 VM_CheckEmptyString (s);
2786 // AK Draw_CachePic is supposed to always return a valid pointer
2787 if( Draw_CachePic_Flags(s, CACHEPICFLAG_NOTPERSISTENT)->tex == r_texture_notexture )
2788 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
2798 void VM_freepic(void)
2802 VM_SAFEPARMCOUNT(1,VM_freepic);
2804 s = PRVM_G_STRING(OFS_PARM0);
2805 VM_CheckEmptyString (s);
2810 dp_font_t *getdrawfont()
2812 if(prog->globaloffsets.drawfont >= 0)
2814 int f = PRVM_G_FLOAT(prog->globaloffsets.drawfont);
2815 if(f < 0 || f >= MAX_FONTS)
2816 return FONT_DEFAULT;
2817 return &dp_fonts[f];
2820 return FONT_DEFAULT;
2827 float drawcharacter(vector position, float character, vector scale, vector rgb, float alpha, float flag)
2830 void VM_drawcharacter(void)
2832 float *pos,*scale,*rgb;
2835 VM_SAFEPARMCOUNT(6,VM_drawcharacter);
2837 character = (char) PRVM_G_FLOAT(OFS_PARM1);
2840 PRVM_G_FLOAT(OFS_RETURN) = -1;
2841 VM_Warning("VM_drawcharacter: %s passed null character !\n",PRVM_NAME);
2845 pos = PRVM_G_VECTOR(OFS_PARM0);
2846 scale = PRVM_G_VECTOR(OFS_PARM2);
2847 rgb = PRVM_G_VECTOR(OFS_PARM3);
2848 flag = (int)PRVM_G_FLOAT(OFS_PARM5);
2850 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2852 PRVM_G_FLOAT(OFS_RETURN) = -2;
2853 VM_Warning("VM_drawcharacter: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2857 if(pos[2] || scale[2])
2858 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")));
2860 if(!scale[0] || !scale[1])
2862 PRVM_G_FLOAT(OFS_RETURN) = -3;
2863 VM_Warning("VM_drawcharacter: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
2867 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());
2868 PRVM_G_FLOAT(OFS_RETURN) = 1;
2875 float drawstring(vector position, string text, vector scale, vector rgb, float alpha, float flag)
2878 void VM_drawstring(void)
2880 float *pos,*scale,*rgb;
2883 VM_SAFEPARMCOUNT(6,VM_drawstring);
2885 string = PRVM_G_STRING(OFS_PARM1);
2886 pos = PRVM_G_VECTOR(OFS_PARM0);
2887 scale = PRVM_G_VECTOR(OFS_PARM2);
2888 rgb = PRVM_G_VECTOR(OFS_PARM3);
2889 flag = (int)PRVM_G_FLOAT(OFS_PARM5);
2891 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2893 PRVM_G_FLOAT(OFS_RETURN) = -2;
2894 VM_Warning("VM_drawstring: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2898 if(!scale[0] || !scale[1])
2900 PRVM_G_FLOAT(OFS_RETURN) = -3;
2901 VM_Warning("VM_drawstring: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
2905 if(pos[2] || scale[2])
2906 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")));
2908 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());
2909 PRVM_G_FLOAT(OFS_RETURN) = 1;
2914 VM_drawcolorcodedstring
2916 float drawcolorcodedstring(vector position, string text, vector scale, float alpha, float flag)
2919 void VM_drawcolorcodedstring(void)
2924 VM_SAFEPARMCOUNT(5,VM_drawstring);
2926 string = PRVM_G_STRING(OFS_PARM1);
2927 pos = PRVM_G_VECTOR(OFS_PARM0);
2928 scale = PRVM_G_VECTOR(OFS_PARM2);
2929 flag = (int)PRVM_G_FLOAT(OFS_PARM4);
2931 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2933 PRVM_G_FLOAT(OFS_RETURN) = -2;
2934 VM_Warning("VM_drawcolorcodedstring: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2938 if(!scale[0] || !scale[1])
2940 PRVM_G_FLOAT(OFS_RETURN) = -3;
2941 VM_Warning("VM_drawcolorcodedstring: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
2945 if(pos[2] || scale[2])
2946 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")));
2949 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());
2950 PRVM_G_FLOAT(OFS_RETURN) = 1;
2956 float stringwidth(string text, float allowColorCodes)
2959 void VM_stringwidth(void)
2963 VM_SAFEPARMCOUNT(2,VM_drawstring);
2965 string = PRVM_G_STRING(OFS_PARM0);
2966 colors = (int)PRVM_G_FLOAT(OFS_PARM1);
2968 PRVM_G_FLOAT(OFS_RETURN) = DrawQ_TextWidth_Font(string, 0, !colors, getdrawfont()); // 1x1 characters, don't actually draw
2974 float drawpic(vector position, string pic, vector size, vector rgb, float alpha, float flag)
2977 void VM_drawpic(void)
2979 const char *picname;
2980 float *size, *pos, *rgb;
2983 VM_SAFEPARMCOUNT(6,VM_drawpic);
2985 picname = PRVM_G_STRING(OFS_PARM1);
2986 VM_CheckEmptyString (picname);
2988 // is pic cached ? no function yet for that
2991 PRVM_G_FLOAT(OFS_RETURN) = -4;
2992 VM_Warning("VM_drawpic: %s: %s not cached !\n", PRVM_NAME, picname);
2996 pos = PRVM_G_VECTOR(OFS_PARM0);
2997 size = PRVM_G_VECTOR(OFS_PARM2);
2998 rgb = PRVM_G_VECTOR(OFS_PARM3);
2999 flag = (int) PRVM_G_FLOAT(OFS_PARM5);
3001 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3003 PRVM_G_FLOAT(OFS_RETURN) = -2;
3004 VM_Warning("VM_drawpic: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
3008 if(pos[2] || size[2])
3009 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")));
3011 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);
3012 PRVM_G_FLOAT(OFS_RETURN) = 1;
3018 float drawsubpic(vector position, vector size, string pic, vector srcPos, vector srcSize, vector rgb, float alpha, float flag)
3022 void VM_drawsubpic(void)
3024 const char *picname;
3025 float *size, *pos, *rgb, *srcPos, *srcSize, alpha;
3028 VM_SAFEPARMCOUNT(8,VM_drawsubpic);
3030 picname = PRVM_G_STRING(OFS_PARM2);
3031 VM_CheckEmptyString (picname);
3033 // is pic cached ? no function yet for that
3036 PRVM_G_FLOAT(OFS_RETURN) = -4;
3037 VM_Warning("VM_drawsubpic: %s: %s not cached !\n", PRVM_NAME, picname);
3041 pos = PRVM_G_VECTOR(OFS_PARM0);
3042 size = PRVM_G_VECTOR(OFS_PARM1);
3043 srcPos = PRVM_G_VECTOR(OFS_PARM3);
3044 srcSize = PRVM_G_VECTOR(OFS_PARM4);
3045 rgb = PRVM_G_VECTOR(OFS_PARM5);
3046 alpha = PRVM_G_FLOAT(OFS_PARM6);
3047 flag = (int) PRVM_G_FLOAT(OFS_PARM7);
3049 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3051 PRVM_G_FLOAT(OFS_RETURN) = -2;
3052 VM_Warning("VM_drawsubpic: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
3056 if(pos[2] || size[2])
3057 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")));
3059 DrawQ_SuperPic(pos[0], pos[1], Draw_CachePic (picname),
3061 srcPos[0], srcPos[1], rgb[0], rgb[1], rgb[2], alpha,
3062 srcPos[0] + srcSize[0], srcPos[1], rgb[0], rgb[1], rgb[2], alpha,
3063 srcPos[0], srcPos[1] + srcSize[1], rgb[0], rgb[1], rgb[2], alpha,
3064 srcPos[0] + srcSize[0], srcPos[1] + srcSize[1], rgb[0], rgb[1], rgb[2], alpha,
3066 PRVM_G_FLOAT(OFS_RETURN) = 1;
3073 float drawfill(vector position, vector size, vector rgb, float alpha, float flag)
3076 void VM_drawfill(void)
3078 float *size, *pos, *rgb;
3081 VM_SAFEPARMCOUNT(5,VM_drawfill);
3084 pos = PRVM_G_VECTOR(OFS_PARM0);
3085 size = PRVM_G_VECTOR(OFS_PARM1);
3086 rgb = PRVM_G_VECTOR(OFS_PARM2);
3087 flag = (int) PRVM_G_FLOAT(OFS_PARM4);
3089 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3091 PRVM_G_FLOAT(OFS_RETURN) = -2;
3092 VM_Warning("VM_drawfill: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
3096 if(pos[2] || size[2])
3097 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")));
3099 DrawQ_Fill(pos[0], pos[1], size[0], size[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM3), flag);
3100 PRVM_G_FLOAT(OFS_RETURN) = 1;
3107 drawsetcliparea(float x, float y, float width, float height)
3110 void VM_drawsetcliparea(void)
3113 VM_SAFEPARMCOUNT(4,VM_drawsetcliparea);
3115 x = bound(0, PRVM_G_FLOAT(OFS_PARM0), vid_conwidth.integer);
3116 y = bound(0, PRVM_G_FLOAT(OFS_PARM1), vid_conheight.integer);
3117 w = bound(0, PRVM_G_FLOAT(OFS_PARM2) + PRVM_G_FLOAT(OFS_PARM0) - x, (vid_conwidth.integer - x));
3118 h = bound(0, PRVM_G_FLOAT(OFS_PARM3) + PRVM_G_FLOAT(OFS_PARM1) - y, (vid_conheight.integer - y));
3120 DrawQ_SetClipArea(x, y, w, h);
3125 VM_drawresetcliparea
3130 void VM_drawresetcliparea(void)
3132 VM_SAFEPARMCOUNT(0,VM_drawresetcliparea);
3134 DrawQ_ResetClipArea();
3141 vector getimagesize(string pic)
3144 void VM_getimagesize(void)
3149 VM_SAFEPARMCOUNT(1,VM_getimagesize);
3151 p = PRVM_G_STRING(OFS_PARM0);
3152 VM_CheckEmptyString (p);
3154 pic = Draw_CachePic_Flags (p, CACHEPICFLAG_NOTPERSISTENT);
3156 PRVM_G_VECTOR(OFS_RETURN)[0] = pic->width;
3157 PRVM_G_VECTOR(OFS_RETURN)[1] = pic->height;
3158 PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
3165 string keynumtostring(float keynum)
3168 void VM_keynumtostring (void)
3170 VM_SAFEPARMCOUNT(1, VM_keynumtostring);
3172 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(Key_KeynumToString((int)PRVM_G_FLOAT(OFS_PARM0)));
3179 float stringtokeynum(string key)
3182 void VM_stringtokeynum (void)
3184 VM_SAFEPARMCOUNT( 1, VM_keynumtostring );
3186 PRVM_G_INT(OFS_RETURN) = Key_StringToKeynum(PRVM_G_STRING(OFS_PARM0));
3189 // CL_Video interface functions
3192 ========================
3195 float cin_open(string file, string name)
3196 ========================
3198 void VM_cin_open( void )
3203 VM_SAFEPARMCOUNT( 2, VM_cin_open );
3205 file = PRVM_G_STRING( OFS_PARM0 );
3206 name = PRVM_G_STRING( OFS_PARM1 );
3208 VM_CheckEmptyString( file );
3209 VM_CheckEmptyString( name );
3211 if( CL_OpenVideo( file, name, MENUOWNER ) )
3212 PRVM_G_FLOAT( OFS_RETURN ) = 1;
3214 PRVM_G_FLOAT( OFS_RETURN ) = 0;
3218 ========================
3221 void cin_close(string name)
3222 ========================
3224 void VM_cin_close( void )
3228 VM_SAFEPARMCOUNT( 1, VM_cin_close );
3230 name = PRVM_G_STRING( OFS_PARM0 );
3231 VM_CheckEmptyString( name );
3233 CL_CloseVideo( CL_GetVideoByName( name ) );
3237 ========================
3239 void cin_setstate(string name, float type)
3240 ========================
3242 void VM_cin_setstate( void )
3245 clvideostate_t state;
3248 VM_SAFEPARMCOUNT( 2, VM_cin_netstate );
3250 name = PRVM_G_STRING( OFS_PARM0 );
3251 VM_CheckEmptyString( name );
3253 state = (clvideostate_t)((int)PRVM_G_FLOAT( OFS_PARM1 ));
3255 video = CL_GetVideoByName( name );
3256 if( video && state > CLVIDEO_UNUSED && state < CLVIDEO_STATECOUNT )
3257 CL_SetVideoState( video, state );
3261 ========================
3264 float cin_getstate(string name)
3265 ========================
3267 void VM_cin_getstate( void )
3272 VM_SAFEPARMCOUNT( 1, VM_cin_getstate );
3274 name = PRVM_G_STRING( OFS_PARM0 );
3275 VM_CheckEmptyString( name );
3277 video = CL_GetVideoByName( name );
3279 PRVM_G_FLOAT( OFS_RETURN ) = (int)video->state;
3281 PRVM_G_FLOAT( OFS_RETURN ) = 0;
3285 ========================
3288 void cin_restart(string name)
3289 ========================
3291 void VM_cin_restart( void )
3296 VM_SAFEPARMCOUNT( 1, VM_cin_restart );
3298 name = PRVM_G_STRING( OFS_PARM0 );
3299 VM_CheckEmptyString( name );
3301 video = CL_GetVideoByName( name );
3303 CL_RestartVideo( video );
3307 ========================
3309 ========================
3311 void VM_Gecko_Init( void ) {
3312 // the prog struct is memset to 0 by Initprog? [12/6/2007 Black]
3313 // FIXME: remove the other _Init functions then, too? [12/6/2007 Black]
3317 ========================
3319 ========================
3321 void VM_Gecko_Destroy( void ) {
3323 for( i = 0 ; i < PRVM_MAX_GECKOINSTANCES ; i++ ) {
3324 clgecko_t **instance = &prog->opengeckoinstances[ i ];
3326 CL_Gecko_DestroyBrowser( *instance );
3333 ========================
3336 float[bool] gecko_create( string name )
3337 ========================
3339 void VM_gecko_create( void ) {
3342 clgecko_t *instance;
3344 VM_SAFEPARMCOUNT( 1, VM_gecko_create );
3346 name = PRVM_G_STRING( OFS_PARM0 );
3347 VM_CheckEmptyString( name );
3349 // find an empty slot for this gecko browser..
3350 for( i = 0 ; i < PRVM_MAX_GECKOINSTANCES ; i++ ) {
3351 if( prog->opengeckoinstances[ i ] == NULL ) {
3355 if( i == PRVM_MAX_GECKOINSTANCES ) {
3356 VM_Warning("VM_gecko_create: %s ran out of gecko handles (%i)\n", PRVM_NAME, PRVM_MAX_GECKOINSTANCES);
3357 PRVM_G_FLOAT( OFS_RETURN ) = 0;
3361 instance = prog->opengeckoinstances[ i ] = CL_Gecko_CreateBrowser( name, PRVM_GetProgNr() );
3363 // TODO: error handling [12/3/2007 Black]
3364 PRVM_G_FLOAT( OFS_RETURN ) = 0;
3367 PRVM_G_FLOAT( OFS_RETURN ) = 1;
3371 ========================
3374 void gecko_destroy( string name )
3375 ========================
3377 void VM_gecko_destroy( void ) {
3379 clgecko_t *instance;
3381 VM_SAFEPARMCOUNT( 1, VM_gecko_destroy );
3383 name = PRVM_G_STRING( OFS_PARM0 );
3384 VM_CheckEmptyString( name );
3385 instance = CL_Gecko_FindBrowser( name );
3389 CL_Gecko_DestroyBrowser( instance );
3393 ========================
3396 void gecko_navigate( string name, string URI )
3397 ========================
3399 void VM_gecko_navigate( void ) {
3402 clgecko_t *instance;
3404 VM_SAFEPARMCOUNT( 2, VM_gecko_navigate );
3406 name = PRVM_G_STRING( OFS_PARM0 );
3407 URI = PRVM_G_STRING( OFS_PARM1 );
3408 VM_CheckEmptyString( name );
3409 VM_CheckEmptyString( URI );
3411 instance = CL_Gecko_FindBrowser( name );
3415 CL_Gecko_NavigateToURI( instance, URI );
3419 ========================
3422 float[bool] gecko_keyevent( string name, float key, float eventtype )
3423 ========================
3425 void VM_gecko_keyevent( void ) {
3428 clgecko_buttoneventtype_t eventtype;
3429 clgecko_t *instance;
3431 VM_SAFEPARMCOUNT( 3, VM_gecko_keyevent );
3433 name = PRVM_G_STRING( OFS_PARM0 );
3434 VM_CheckEmptyString( name );
3435 key = (unsigned int) PRVM_G_FLOAT( OFS_PARM1 );
3436 switch( (unsigned int) PRVM_G_FLOAT( OFS_PARM2 ) ) {
3438 eventtype = CLG_BET_DOWN;
3441 eventtype = CLG_BET_UP;
3444 eventtype = CLG_BET_PRESS;
3447 eventtype = CLG_BET_DOUBLECLICK;
3450 // TODO: console printf? [12/3/2007 Black]
3451 PRVM_G_FLOAT( OFS_RETURN ) = 0;
3455 instance = CL_Gecko_FindBrowser( name );
3457 PRVM_G_FLOAT( OFS_RETURN ) = 0;
3461 PRVM_G_FLOAT( OFS_RETURN ) = (CL_Gecko_Event_Key( instance, key, eventtype ) == true);
3465 ========================
3468 void gecko_mousemove( string name, float x, float y )
3469 ========================
3471 void VM_gecko_movemouse( void ) {
3474 clgecko_t *instance;
3476 VM_SAFEPARMCOUNT( 3, VM_gecko_movemouse );
3478 name = PRVM_G_STRING( OFS_PARM0 );
3479 VM_CheckEmptyString( name );
3480 x = PRVM_G_FLOAT( OFS_PARM1 );
3481 y = PRVM_G_FLOAT( OFS_PARM2 );
3483 instance = CL_Gecko_FindBrowser( name );
3487 CL_Gecko_Event_CursorMove( instance, x, y );
3492 ========================
3495 void gecko_resize( string name, float w, float h )
3496 ========================
3498 void VM_gecko_resize( void ) {
3501 clgecko_t *instance;
3503 VM_SAFEPARMCOUNT( 3, VM_gecko_movemouse );
3505 name = PRVM_G_STRING( OFS_PARM0 );
3506 VM_CheckEmptyString( name );
3507 w = PRVM_G_FLOAT( OFS_PARM1 );
3508 h = PRVM_G_FLOAT( OFS_PARM2 );
3510 instance = CL_Gecko_FindBrowser( name );
3514 CL_Gecko_Resize( instance, w, h );
3519 ========================
3520 VM_gecko_get_texture_extent
3522 vector gecko_get_texture_extent( string name )
3523 ========================
3525 void VM_gecko_get_texture_extent( void ) {
3527 clgecko_t *instance;
3529 VM_SAFEPARMCOUNT( 1, VM_gecko_movemouse );
3531 name = PRVM_G_STRING( OFS_PARM0 );
3532 VM_CheckEmptyString( name );
3534 PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
3535 instance = CL_Gecko_FindBrowser( name );
3537 PRVM_G_VECTOR(OFS_RETURN)[0] = 0;
3538 PRVM_G_VECTOR(OFS_RETURN)[1] = 0;
3541 CL_Gecko_GetTextureExtent( instance,
3542 PRVM_G_VECTOR(OFS_RETURN), PRVM_G_VECTOR(OFS_RETURN)+1 );
3551 Writes new values for v_forward, v_up, and v_right based on angles
3552 void makevectors(vector angle)
3555 void VM_makevectors (void)
3557 prvm_eval_t *valforward, *valright, *valup;
3558 valforward = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_forward);
3559 valright = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_right);
3560 valup = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_up);
3561 if (!valforward || !valright || !valup)
3563 VM_Warning("makevectors: could not find v_forward, v_right, or v_up global variables\n");
3566 VM_SAFEPARMCOUNT(1, VM_makevectors);
3567 AngleVectors (PRVM_G_VECTOR(OFS_PARM0), valforward->vector, valright->vector, valup->vector);
3574 Writes new values for v_forward, v_up, and v_right based on the given forward vector
3575 vectorvectors(vector)
3578 void VM_vectorvectors (void)
3580 prvm_eval_t *valforward, *valright, *valup;
3581 valforward = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_forward);
3582 valright = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_right);
3583 valup = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_up);
3584 if (!valforward || !valright || !valup)
3586 VM_Warning("vectorvectors: could not find v_forward, v_right, or v_up global variables\n");
3589 VM_SAFEPARMCOUNT(1, VM_vectorvectors);
3590 VectorNormalize2(PRVM_G_VECTOR(OFS_PARM0), valforward->vector);
3591 VectorVectors(valforward->vector, valright->vector, valup->vector);
3595 ========================
3598 void drawline(float width, vector pos1, vector pos2, vector rgb, float alpha, float flags)
3599 ========================
3601 void VM_drawline (void)
3603 float *c1, *c2, *rgb;
3605 unsigned char flags;
3607 VM_SAFEPARMCOUNT(6, VM_drawline);
3608 width = PRVM_G_FLOAT(OFS_PARM0);
3609 c1 = PRVM_G_VECTOR(OFS_PARM1);
3610 c2 = PRVM_G_VECTOR(OFS_PARM2);
3611 rgb = PRVM_G_VECTOR(OFS_PARM3);
3612 alpha = PRVM_G_FLOAT(OFS_PARM4);
3613 flags = (int)PRVM_G_FLOAT(OFS_PARM5);
3614 DrawQ_Line(width, c1[0], c1[1], c2[0], c2[1], rgb[0], rgb[1], rgb[2], alpha, flags);
3617 // float(float number, float quantity) bitshift (EXT_BITSHIFT)
3618 void VM_bitshift (void)
3621 VM_SAFEPARMCOUNT(2, VM_bitshift);
3623 n1 = (int)fabs((int)PRVM_G_FLOAT(OFS_PARM0));
3624 n2 = (int)PRVM_G_FLOAT(OFS_PARM1);
3626 PRVM_G_FLOAT(OFS_RETURN) = n1;
3629 PRVM_G_FLOAT(OFS_RETURN) = (n1 >> -n2);
3631 PRVM_G_FLOAT(OFS_RETURN) = (n1 << n2);
3634 ////////////////////////////////////////
3635 // AltString functions
3636 ////////////////////////////////////////
3639 ========================
3642 float altstr_count(string)
3643 ========================
3645 void VM_altstr_count( void )
3647 const char *altstr, *pos;
3650 VM_SAFEPARMCOUNT( 1, VM_altstr_count );
3652 altstr = PRVM_G_STRING( OFS_PARM0 );
3653 //VM_CheckEmptyString( altstr );
3655 for( count = 0, pos = altstr ; *pos ; pos++ ) {
3656 if( *pos == '\\' ) {
3660 } else if( *pos == '\'' ) {
3665 PRVM_G_FLOAT( OFS_RETURN ) = (float) (count / 2);
3669 ========================
3672 string altstr_prepare(string)
3673 ========================
3675 void VM_altstr_prepare( void )
3678 const char *instr, *in;
3680 char outstr[VM_STRINGTEMP_LENGTH];
3682 VM_SAFEPARMCOUNT( 1, VM_altstr_prepare );
3684 instr = PRVM_G_STRING( OFS_PARM0 );
3686 for( out = outstr, in = instr, size = sizeof(outstr) - 1 ; size && *in ; size--, in++, out++ )
3695 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
3699 ========================
3702 string altstr_get(string, float)
3703 ========================
3705 void VM_altstr_get( void )
3707 const char *altstr, *pos;
3710 char outstr[VM_STRINGTEMP_LENGTH];
3712 VM_SAFEPARMCOUNT( 2, VM_altstr_get );
3714 altstr = PRVM_G_STRING( OFS_PARM0 );
3716 count = (int)PRVM_G_FLOAT( OFS_PARM1 );
3717 count = count * 2 + 1;
3719 for( pos = altstr ; *pos && count ; pos++ )
3720 if( *pos == '\\' ) {
3723 } else if( *pos == '\'' )
3727 PRVM_G_INT( OFS_RETURN ) = 0;
3731 for( out = outstr, size = sizeof(outstr) - 1 ; size && *pos ; size--, pos++, out++ )
3732 if( *pos == '\\' ) {
3737 } else if( *pos == '\'' )
3743 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
3747 ========================
3750 string altstr_set(string altstr, float num, string set)
3751 ========================
3753 void VM_altstr_set( void )
3756 const char *altstr, *str;
3759 char outstr[VM_STRINGTEMP_LENGTH];
3761 VM_SAFEPARMCOUNT( 3, VM_altstr_set );
3763 altstr = PRVM_G_STRING( OFS_PARM0 );
3765 num = (int)PRVM_G_FLOAT( OFS_PARM1 );
3767 str = PRVM_G_STRING( OFS_PARM2 );
3770 for( num = num * 2 + 1, in = altstr; *in && num; *out++ = *in++ )
3775 } else if( *in == '\'' ) {
3780 for( ; *str; *out++ = *str++ );
3781 // now jump over the old content
3783 if( *in == '\'' || (*in == '\\' && !*++in) )
3786 strlcpy(out, in, outstr + sizeof(outstr) - out);
3787 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
3791 ========================
3794 string altstr_ins(string altstr, float num, string set)
3795 ========================
3797 void VM_altstr_ins(void)
3805 char outstr[VM_STRINGTEMP_LENGTH];
3807 VM_SAFEPARMCOUNT(3, VM_altstr_ins);
3809 in = instr = PRVM_G_STRING( OFS_PARM0 );
3810 num = (int)PRVM_G_FLOAT( OFS_PARM1 );
3811 set = setstr = PRVM_G_STRING( OFS_PARM2 );
3814 for( num = num * 2 + 2 ; *in && num > 0 ; *out++ = *in++ )
3819 } else if( *in == '\'' ) {
3824 for( ; *set ; *out++ = *set++ );
3827 strlcpy(out, in, outstr + sizeof(outstr) - out);
3828 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
3832 ////////////////////////////////////////
3833 // BufString functions
3834 ////////////////////////////////////////
3835 //[515]: string buffers support
3837 static size_t stringbuffers_sortlength;
3839 static void BufStr_Expand(prvm_stringbuffer_t *stringbuffer, int strindex)
3841 if (stringbuffer->max_strings <= strindex)
3843 char **oldstrings = stringbuffer->strings;
3844 stringbuffer->max_strings = max(stringbuffer->max_strings * 2, 128);
3845 while (stringbuffer->max_strings <= strindex)
3846 stringbuffer->max_strings *= 2;
3847 stringbuffer->strings = Mem_Alloc(prog->progs_mempool, stringbuffer->max_strings * sizeof(stringbuffer->strings[0]));
3848 if (stringbuffer->num_strings > 0)
3849 memcpy(stringbuffer->strings, oldstrings, stringbuffer->num_strings * sizeof(stringbuffer->strings[0]));
3851 Mem_Free(oldstrings);
3855 static void BufStr_Shrink(prvm_stringbuffer_t *stringbuffer)
3857 // reduce num_strings if there are empty string slots at the end
3858 while (stringbuffer->num_strings > 0 && stringbuffer->strings[stringbuffer->num_strings - 1] == NULL)
3859 stringbuffer->num_strings--;
3861 // if empty, free the string pointer array
3862 if (stringbuffer->num_strings == 0)
3864 stringbuffer->max_strings = 0;
3865 if (stringbuffer->strings)
3866 Mem_Free(stringbuffer->strings);
3867 stringbuffer->strings = NULL;
3871 static int BufStr_SortStringsUP (const void *in1, const void *in2)
3874 a = *((const char **) in1);
3875 b = *((const char **) in2);
3877 if(!b[0]) return -1;
3878 return strncmp(a, b, stringbuffers_sortlength);
3881 static int BufStr_SortStringsDOWN (const void *in1, const void *in2)
3884 a = *((const char **) in1);
3885 b = *((const char **) in2);
3887 if(!b[0]) return -1;
3888 return strncmp(b, a, stringbuffers_sortlength);
3892 ========================
3894 creates new buffer, and returns it's index, returns -1 if failed
3895 float buf_create(void) = #460;
3896 ========================
3898 void VM_buf_create (void)
3900 prvm_stringbuffer_t *stringbuffer;
3902 VM_SAFEPARMCOUNT(0, VM_buf_create);
3903 stringbuffer = Mem_ExpandableArray_AllocRecord(&prog->stringbuffersarray);
3904 for (i = 0;stringbuffer != Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, i);i++);
3905 stringbuffer->origin = PRVM_AllocationOrigin();
3906 PRVM_G_FLOAT(OFS_RETURN) = i;
3910 ========================
3912 deletes buffer and all strings in it
3913 void buf_del(float bufhandle) = #461;
3914 ========================
3916 void VM_buf_del (void)
3918 prvm_stringbuffer_t *stringbuffer;
3919 VM_SAFEPARMCOUNT(1, VM_buf_del);
3920 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
3924 for (i = 0;i < stringbuffer->num_strings;i++)
3925 if (stringbuffer->strings[i])
3926 Mem_Free(stringbuffer->strings[i]);
3927 if (stringbuffer->strings)
3928 Mem_Free(stringbuffer->strings);
3929 if(stringbuffer->origin)
3930 PRVM_Free((char *)stringbuffer->origin);
3931 Mem_ExpandableArray_FreeRecord(&prog->stringbuffersarray, stringbuffer);
3935 VM_Warning("VM_buf_del: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3941 ========================
3943 how many strings are stored in buffer
3944 float buf_getsize(float bufhandle) = #462;
3945 ========================
3947 void VM_buf_getsize (void)
3949 prvm_stringbuffer_t *stringbuffer;
3950 VM_SAFEPARMCOUNT(1, VM_buf_getsize);
3952 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
3955 PRVM_G_FLOAT(OFS_RETURN) = -1;
3956 VM_Warning("VM_buf_getsize: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3960 PRVM_G_FLOAT(OFS_RETURN) = stringbuffer->num_strings;
3964 ========================
3966 copy all content from one buffer to another, make sure it exists
3967 void buf_copy(float bufhandle_from, float bufhandle_to) = #463;
3968 ========================
3970 void VM_buf_copy (void)
3972 prvm_stringbuffer_t *srcstringbuffer, *dststringbuffer;
3974 VM_SAFEPARMCOUNT(2, VM_buf_copy);
3976 srcstringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
3977 if(!srcstringbuffer)
3979 VM_Warning("VM_buf_copy: invalid source buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3982 i = (int)PRVM_G_FLOAT(OFS_PARM1);
3983 if(i == (int)PRVM_G_FLOAT(OFS_PARM0))
3985 VM_Warning("VM_buf_copy: source == destination (%i) in %s\n", i, PRVM_NAME);
3988 dststringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
3989 if(!dststringbuffer)
3991 VM_Warning("VM_buf_copy: invalid destination buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM1), PRVM_NAME);
3995 for (i = 0;i < dststringbuffer->num_strings;i++)
3996 if (dststringbuffer->strings[i])
3997 Mem_Free(dststringbuffer->strings[i]);
3998 if (dststringbuffer->strings)
3999 Mem_Free(dststringbuffer->strings);
4000 *dststringbuffer = *srcstringbuffer;
4001 if (dststringbuffer->max_strings)
4002 dststringbuffer->strings = (char **)Mem_Alloc(prog->progs_mempool, sizeof(dststringbuffer->strings[0]) * dststringbuffer->max_strings);
4004 for (i = 0;i < dststringbuffer->num_strings;i++)
4006 if (srcstringbuffer->strings[i])
4009 stringlen = strlen(srcstringbuffer->strings[i]) + 1;
4010 dststringbuffer->strings[i] = (char *)Mem_Alloc(prog->progs_mempool, stringlen);
4011 memcpy(dststringbuffer->strings[i], srcstringbuffer->strings[i], stringlen);
4017 ========================
4019 sort buffer by beginnings of strings (cmplength defaults it's length)
4020 "backward == TRUE" means that sorting goes upside-down
4021 void buf_sort(float bufhandle, float cmplength, float backward) = #464;
4022 ========================
4024 void VM_buf_sort (void)
4026 prvm_stringbuffer_t *stringbuffer;
4027 VM_SAFEPARMCOUNT(3, VM_buf_sort);
4029 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4032 VM_Warning("VM_buf_sort: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4035 if(stringbuffer->num_strings <= 0)
4037 VM_Warning("VM_buf_sort: tried to sort empty buffer %i in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4040 stringbuffers_sortlength = (int)PRVM_G_FLOAT(OFS_PARM1);
4041 if(stringbuffers_sortlength <= 0)
4042 stringbuffers_sortlength = 0x7FFFFFFF;
4044 if(!PRVM_G_FLOAT(OFS_PARM2))
4045 qsort(stringbuffer->strings, stringbuffer->num_strings, sizeof(char*), BufStr_SortStringsUP);
4047 qsort(stringbuffer->strings, stringbuffer->num_strings, sizeof(char*), BufStr_SortStringsDOWN);
4049 BufStr_Shrink(stringbuffer);
4053 ========================
4055 concantenates all buffer string into one with "glue" separator and returns it as tempstring
4056 string buf_implode(float bufhandle, string glue) = #465;
4057 ========================
4059 void VM_buf_implode (void)
4061 prvm_stringbuffer_t *stringbuffer;
4062 char k[VM_STRINGTEMP_LENGTH];
4066 VM_SAFEPARMCOUNT(2, VM_buf_implode);
4068 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4069 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
4072 VM_Warning("VM_buf_implode: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4075 if(!stringbuffer->num_strings)
4077 sep = PRVM_G_STRING(OFS_PARM1);
4079 for(l = i = 0;i < stringbuffer->num_strings;i++)
4081 if(stringbuffer->strings[i])
4083 l += (i > 0 ? strlen(sep) : 0) + strlen(stringbuffer->strings[i]);
4084 if (l >= sizeof(k) - 1)
4086 strlcat(k, sep, sizeof(k));
4087 strlcat(k, stringbuffer->strings[i], sizeof(k));
4090 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(k);
4094 ========================
4096 get a string from buffer, returns tempstring, dont str_unzone it!
4097 string bufstr_get(float bufhandle, float string_index) = #465;
4098 ========================
4100 void VM_bufstr_get (void)
4102 prvm_stringbuffer_t *stringbuffer;
4104 VM_SAFEPARMCOUNT(2, VM_bufstr_get);
4106 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
4107 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4110 VM_Warning("VM_bufstr_get: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4113 strindex = (int)PRVM_G_FLOAT(OFS_PARM1);
4116 VM_Warning("VM_bufstr_get: invalid string index %i used in %s\n", strindex, PRVM_NAME);
4119 if (strindex < stringbuffer->num_strings && stringbuffer->strings[strindex])
4120 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(stringbuffer->strings[strindex]);
4124 ========================
4126 copies a string into selected slot of buffer
4127 void bufstr_set(float bufhandle, float string_index, string str) = #466;
4128 ========================
4130 void VM_bufstr_set (void)
4133 prvm_stringbuffer_t *stringbuffer;
4136 VM_SAFEPARMCOUNT(3, VM_bufstr_set);
4138 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4141 VM_Warning("VM_bufstr_set: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4144 strindex = (int)PRVM_G_FLOAT(OFS_PARM1);
4145 if(strindex < 0 || strindex >= 1000000) // huge number of strings
4147 VM_Warning("VM_bufstr_set: invalid string index %i used in %s\n", strindex, PRVM_NAME);
4151 BufStr_Expand(stringbuffer, strindex);
4152 stringbuffer->num_strings = max(stringbuffer->num_strings, strindex + 1);
4154 if(stringbuffer->strings[strindex])
4155 Mem_Free(stringbuffer->strings[strindex]);
4156 stringbuffer->strings[strindex] = NULL;
4158 news = PRVM_G_STRING(OFS_PARM2);
4159 if (news && news[0])
4161 size_t alloclen = strlen(news) + 1;
4162 stringbuffer->strings[strindex] = (char *)Mem_Alloc(prog->progs_mempool, alloclen);
4163 memcpy(stringbuffer->strings[strindex], news, alloclen);
4166 BufStr_Shrink(stringbuffer);
4170 ========================
4172 adds string to buffer in first free slot and returns its index
4173 "order == TRUE" means that string will be added after last "full" slot
4174 float bufstr_add(float bufhandle, string str, float order) = #467;
4175 ========================
4177 void VM_bufstr_add (void)
4179 int order, strindex;
4180 prvm_stringbuffer_t *stringbuffer;
4184 VM_SAFEPARMCOUNT(3, VM_bufstr_add);
4186 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4187 PRVM_G_FLOAT(OFS_RETURN) = -1;
4190 VM_Warning("VM_bufstr_add: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4193 string = PRVM_G_STRING(OFS_PARM1);
4194 if(!string || !string[0])
4196 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);
4199 order = (int)PRVM_G_FLOAT(OFS_PARM2);
4201 strindex = stringbuffer->num_strings;
4203 for (strindex = 0;strindex < stringbuffer->num_strings;strindex++)
4204 if (stringbuffer->strings[strindex] == NULL)
4207 BufStr_Expand(stringbuffer, strindex);
4209 stringbuffer->num_strings = max(stringbuffer->num_strings, strindex + 1);
4210 alloclen = strlen(string) + 1;
4211 stringbuffer->strings[strindex] = (char *)Mem_Alloc(prog->progs_mempool, alloclen);
4212 memcpy(stringbuffer->strings[strindex], string, alloclen);
4214 PRVM_G_FLOAT(OFS_RETURN) = strindex;
4218 ========================
4220 delete string from buffer
4221 void bufstr_free(float bufhandle, float string_index) = #468;
4222 ========================
4224 void VM_bufstr_free (void)
4227 prvm_stringbuffer_t *stringbuffer;
4228 VM_SAFEPARMCOUNT(2, VM_bufstr_free);
4230 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4233 VM_Warning("VM_bufstr_free: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4236 i = (int)PRVM_G_FLOAT(OFS_PARM1);
4239 VM_Warning("VM_bufstr_free: invalid string index %i used in %s\n", i, PRVM_NAME);
4243 if (i < stringbuffer->num_strings)
4245 if(stringbuffer->strings[i])
4246 Mem_Free(stringbuffer->strings[i]);
4247 stringbuffer->strings[i] = NULL;
4250 BufStr_Shrink(stringbuffer);
4259 This was a major timewaster in progs, so it was converted to C
4262 void VM_changeyaw (void)
4265 float ideal, current, move, speed;
4267 // this is called (VERY HACKISHLY) by SV_MoveToGoal, so it can not use any
4268 // parameters because they are the parameters to SV_MoveToGoal, not this
4269 //VM_SAFEPARMCOUNT(0, VM_changeyaw);
4271 ent = PRVM_PROG_TO_EDICT(PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict);
4272 if (ent == prog->edicts)
4274 VM_Warning("changeyaw: can not modify world entity\n");
4277 if (ent->priv.server->free)
4279 VM_Warning("changeyaw: can not modify free entity\n");
4282 if (prog->fieldoffsets.angles < 0 || prog->fieldoffsets.ideal_yaw < 0 || prog->fieldoffsets.yaw_speed < 0)
4284 VM_Warning("changeyaw: angles, ideal_yaw, or yaw_speed field(s) not found\n");
4287 current = ANGLEMOD(PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.angles)->vector[1]);
4288 ideal = PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.ideal_yaw)->_float;
4289 speed = PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.yaw_speed)->_float;
4291 if (current == ideal)
4293 move = ideal - current;
4294 if (ideal > current)
4315 PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.angles)->vector[1] = ANGLEMOD (current + move);
4323 void VM_changepitch (void)
4326 float ideal, current, move, speed;
4328 VM_SAFEPARMCOUNT(1, VM_changepitch);
4330 ent = PRVM_G_EDICT(OFS_PARM0);
4331 if (ent == prog->edicts)
4333 VM_Warning("changepitch: can not modify world entity\n");
4336 if (ent->priv.server->free)
4338 VM_Warning("changepitch: can not modify free entity\n");
4341 if (prog->fieldoffsets.angles < 0 || prog->fieldoffsets.idealpitch < 0 || prog->fieldoffsets.pitch_speed < 0)
4343 VM_Warning("changepitch: angles, idealpitch, or pitch_speed field(s) not found\n");
4346 current = ANGLEMOD(PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.angles)->vector[0]);
4347 ideal = PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.idealpitch)->_float;
4348 speed = PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.pitch_speed)->_float;
4350 if (current == ideal)
4352 move = ideal - current;
4353 if (ideal > current)
4374 PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.angles)->vector[0] = ANGLEMOD (current + move);
4377 // TODO: adapt all static function names to use a single naming convention... [12/3/2007 Black]
4378 static int Is_Text_Color (char c, char t)
4381 char c2 = c - (c & 128);
4382 char t2 = t - (t & 128);
4384 if(c != STRING_COLOR_TAG && c2 != STRING_COLOR_TAG) return 0;
4385 if(t >= '0' && t <= '9') a = 1;
4386 if(t2 >= '0' && t2 <= '9') a = 1;
4387 /* if(t >= 'A' && t <= 'Z') a = 2;
4388 if(t2 >= 'A' && t2 <= 'Z') a = 2;
4390 if(a == 1 && scr_colortext.integer > 0)
4392 if(a == 2 && scr_multifonts.integer > 0)
4398 void VM_uncolorstring (void)
4401 char out[VM_STRINGTEMP_LENGTH];
4404 VM_SAFEPARMCOUNT(1, VM_uncolorstring);
4405 in = PRVM_G_STRING(OFS_PARM0);
4406 VM_CheckEmptyString (in);
4411 if(Is_Text_Color(in[k], in[k+1]) == 1/* || (in[k] == '&' && in[k+1] == 'r')*/)
4420 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(out);
4423 // #221 float(string str, string sub[, float startpos]) strstrofs (FTE_STRINGS)
4424 //strstr, without generating a new string. Use in conjunction with FRIK_FILE's substring for more similar strstr.
4425 void VM_strstrofs (void)
4427 const char *instr, *match;
4429 VM_SAFEPARMCOUNTRANGE(2, 3, VM_strstrofs);
4430 instr = PRVM_G_STRING(OFS_PARM0);
4431 match = PRVM_G_STRING(OFS_PARM1);
4432 firstofs = (prog->argc > 2)?PRVM_G_FLOAT(OFS_PARM2):0;
4434 if (firstofs && (firstofs < 0 || firstofs > (int)strlen(instr)))
4436 PRVM_G_FLOAT(OFS_RETURN) = -1;
4440 match = strstr(instr+firstofs, match);
4442 PRVM_G_FLOAT(OFS_RETURN) = -1;
4444 PRVM_G_FLOAT(OFS_RETURN) = match - instr;
4447 //#222 string(string s, float index) str2chr (FTE_STRINGS)
4448 void VM_str2chr (void)
4451 VM_SAFEPARMCOUNT(2, VM_str2chr);
4452 s = PRVM_G_STRING(OFS_PARM0);
4453 if((unsigned)PRVM_G_FLOAT(OFS_PARM1) < strlen(s))
4454 PRVM_G_FLOAT(OFS_RETURN) = (unsigned char)s[(unsigned)PRVM_G_FLOAT(OFS_PARM1)];
4456 PRVM_G_FLOAT(OFS_RETURN) = 0;
4459 //#223 string(float c, ...) chr2str (FTE_STRINGS)
4460 void VM_chr2str (void)
4464 VM_SAFEPARMCOUNTRANGE(0, 8, VM_chr2str);
4465 for(i = 0;i < prog->argc && i < (int)sizeof(t) - 1;i++)
4466 t[i] = (unsigned char)PRVM_G_FLOAT(OFS_PARM0+i*3);
4468 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(t);
4471 static int chrconv_number(int i, int base, int conv)
4496 static int chrconv_punct(int i, int base, int conv)
4514 static int chrchar_alpha(int i, int basec, int baset, int convc, int convt, int charnum)
4516 //convert case and colour seperatly...
4533 baset = 128*((charnum&1) == (convt-5));
4549 return i + basec + baset;
4551 // #224 string(float ccase, float calpha, float cnum, string s, ...) strconv (FTE_STRINGS)
4552 //bulk convert a string. change case or colouring.
4553 void VM_strconv (void)
4555 int ccase, redalpha, rednum, len, i;
4556 unsigned char resbuf[VM_STRINGTEMP_LENGTH];
4557 unsigned char *result = resbuf;
4559 VM_SAFEPARMCOUNTRANGE(3, 8, VM_strconv);
4561 ccase = PRVM_G_FLOAT(OFS_PARM0); //0 same, 1 lower, 2 upper
4562 redalpha = PRVM_G_FLOAT(OFS_PARM1); //0 same, 1 white, 2 red, 5 alternate, 6 alternate-alternate
4563 rednum = PRVM_G_FLOAT(OFS_PARM2); //0 same, 1 white, 2 red, 3 redspecial, 4 whitespecial, 5 alternate, 6 alternate-alternate
4564 VM_VarString(3, (char *) resbuf, sizeof(resbuf));
4565 len = strlen((char *) resbuf);
4567 for (i = 0; i < len; i++, result++) //should this be done backwards?
4569 if (*result >= '0' && *result <= '9') //normal numbers...
4570 *result = chrconv_number(*result, '0', rednum);
4571 else if (*result >= '0'+128 && *result <= '9'+128)
4572 *result = chrconv_number(*result, '0'+128, rednum);
4573 else if (*result >= '0'+128-30 && *result <= '9'+128-30)
4574 *result = chrconv_number(*result, '0'+128-30, rednum);
4575 else if (*result >= '0'-30 && *result <= '9'-30)
4576 *result = chrconv_number(*result, '0'-30, rednum);
4578 else if (*result >= 'a' && *result <= 'z') //normal numbers...
4579 *result = chrchar_alpha(*result, 'a', 0, ccase, redalpha, i);
4580 else if (*result >= 'A' && *result <= 'Z') //normal numbers...
4581 *result = chrchar_alpha(*result, 'A', 0, ccase, redalpha, i);
4582 else if (*result >= 'a'+128 && *result <= 'z'+128) //normal numbers...
4583 *result = chrchar_alpha(*result, 'a', 128, ccase, redalpha, i);
4584 else if (*result >= 'A'+128 && *result <= 'Z'+128) //normal numbers...
4585 *result = chrchar_alpha(*result, 'A', 128, ccase, redalpha, i);
4587 else if ((*result & 127) < 16 || !redalpha) //special chars..
4589 else if (*result < 128)
4590 *result = chrconv_punct(*result, 0, redalpha);
4592 *result = chrconv_punct(*result, 128, redalpha);
4596 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString((char *) resbuf);
4599 // #225 string(float chars, string s, ...) strpad (FTE_STRINGS)
4600 void VM_strpad (void)
4602 char src[VM_STRINGTEMP_LENGTH];
4603 char destbuf[VM_STRINGTEMP_LENGTH];
4605 VM_SAFEPARMCOUNTRANGE(1, 8, VM_strpad);
4606 pad = PRVM_G_FLOAT(OFS_PARM0);
4607 VM_VarString(1, src, sizeof(src));
4609 // note: < 0 = left padding, > 0 = right padding,
4610 // this is reverse logic of printf!
4611 dpsnprintf(destbuf, sizeof(destbuf), "%*s", -pad, src);
4613 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(destbuf);
4616 // #226 string(string info, string key, string value, ...) infoadd (FTE_STRINGS)
4617 //uses qw style \key\value strings
4618 void VM_infoadd (void)
4620 const char *info, *key;
4621 char value[VM_STRINGTEMP_LENGTH];
4622 char temp[VM_STRINGTEMP_LENGTH];
4624 VM_SAFEPARMCOUNTRANGE(2, 8, VM_infoadd);
4625 info = PRVM_G_STRING(OFS_PARM0);
4626 key = PRVM_G_STRING(OFS_PARM1);
4627 VM_VarString(2, value, sizeof(value));
4629 strlcpy(temp, info, VM_STRINGTEMP_LENGTH);
4631 InfoString_SetValue(temp, VM_STRINGTEMP_LENGTH, key, value);
4633 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(temp);
4636 // #227 string(string info, string key) infoget (FTE_STRINGS)
4637 //uses qw style \key\value strings
4638 void VM_infoget (void)
4642 char value[VM_STRINGTEMP_LENGTH];
4644 VM_SAFEPARMCOUNT(2, VM_infoget);
4645 info = PRVM_G_STRING(OFS_PARM0);
4646 key = PRVM_G_STRING(OFS_PARM1);
4648 InfoString_GetValue(info, key, value, VM_STRINGTEMP_LENGTH);
4650 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(value);
4653 //#228 float(string s1, string s2, float len) strncmp (FTE_STRINGS)
4654 // also float(string s1, string s2) strcmp (FRIK_FILE)
4655 void VM_strncmp (void)
4657 const char *s1, *s2;
4658 VM_SAFEPARMCOUNTRANGE(2, 3, VM_strncmp);
4659 s1 = PRVM_G_STRING(OFS_PARM0);
4660 s2 = PRVM_G_STRING(OFS_PARM1);
4663 PRVM_G_FLOAT(OFS_RETURN) = strncmp(s1, s2, (size_t)PRVM_G_FLOAT(OFS_PARM2));
4667 PRVM_G_FLOAT(OFS_RETURN) = strcmp(s1, s2);
4671 // #229 float(string s1, string s2) strcasecmp (FTE_STRINGS)
4672 // #230 float(string s1, string s2, float len) strncasecmp (FTE_STRINGS)
4673 void VM_strncasecmp (void)
4675 const char *s1, *s2;
4676 VM_SAFEPARMCOUNTRANGE(2, 3, VM_strncasecmp);
4677 s1 = PRVM_G_STRING(OFS_PARM0);
4678 s2 = PRVM_G_STRING(OFS_PARM1);
4681 PRVM_G_FLOAT(OFS_RETURN) = strncasecmp(s1, s2, (size_t)PRVM_G_FLOAT(OFS_PARM2));
4685 PRVM_G_FLOAT(OFS_RETURN) = strcasecmp(s1, s2);
4689 // #494 float(float caseinsensitive, string s, ...) crc16
4693 static char s[VM_STRINGTEMP_LENGTH];
4694 VM_SAFEPARMCOUNTRANGE(2, 8, VM_hash);
4695 insensitive = PRVM_G_FLOAT(OFS_PARM0);
4696 VM_VarString(1, s, sizeof(s));
4697 PRVM_G_FLOAT(OFS_RETURN) = (unsigned short) ((insensitive ? CRC_Block_CaseInsensitive : CRC_Block) ((unsigned char *) s, strlen(s)));
4700 void VM_wasfreed (void)
4702 VM_SAFEPARMCOUNT(1, VM_wasfreed);
4703 PRVM_G_FLOAT(OFS_RETURN) = PRVM_G_EDICT(OFS_PARM0)->priv.required->free;
4706 void VM_SetTraceGlobals(const trace_t *trace)
4709 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_allsolid)))
4710 val->_float = trace->allsolid;
4711 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_startsolid)))
4712 val->_float = trace->startsolid;
4713 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_fraction)))
4714 val->_float = trace->fraction;
4715 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_inwater)))
4716 val->_float = trace->inwater;
4717 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_inopen)))
4718 val->_float = trace->inopen;
4719 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_endpos)))
4720 VectorCopy(trace->endpos, val->vector);
4721 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_plane_normal)))
4722 VectorCopy(trace->plane.normal, val->vector);
4723 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_plane_dist)))
4724 val->_float = trace->plane.dist;
4725 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_ent)))
4726 val->edict = PRVM_EDICT_TO_PROG(trace->ent ? trace->ent : prog->edicts);
4727 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dpstartcontents)))
4728 val->_float = trace->startsupercontents;
4729 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dphitcontents)))
4730 val->_float = trace->hitsupercontents;
4731 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dphitq3surfaceflags)))
4732 val->_float = trace->hitq3surfaceflags;
4733 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dphittexturename)))
4734 val->string = trace->hittexture ? PRVM_SetTempString(trace->hittexture->name) : 0;
4739 void VM_Cmd_Init(void)
4741 // only init the stuff for the current prog
4745 // VM_BufStr_Init();
4748 void VM_Cmd_Reset(void)
4750 CL_PurgeOwner( MENUOWNER );
4752 VM_Files_CloseAll();
4754 // VM_BufStr_ShutDown();
4757 // #510 string(string input, ...) uri_escape (DP_QC_URI_ESCAPE)
4758 // does URI escaping on a string (replace evil stuff by %AB escapes)
4759 void VM_uri_escape (void)
4761 char src[VM_STRINGTEMP_LENGTH];
4762 char dest[VM_STRINGTEMP_LENGTH];
4764 static const char *hex = "0123456789ABCDEF";
4766 VM_SAFEPARMCOUNTRANGE(1, 8, VM_uri_escape);
4767 VM_VarString(0, src, sizeof(src));
4769 for(p = src, q = dest; *p && q < dest + sizeof(dest) - 3; ++p)
4771 if((*p >= 'A' && *p <= 'Z')
4772 || (*p >= 'a' && *p <= 'z')
4773 || (*p >= '0' && *p <= '9')
4774 || (*p == '-') || (*p == '_') || (*p == '.')
4775 || (*p == '!') || (*p == '~') || (*p == '*')
4776 || (*p == '\'') || (*p == '(') || (*p == ')'))
4781 *q++ = hex[(*(unsigned char *)p >> 4) & 0xF];
4782 *q++ = hex[ *(unsigned char *)p & 0xF];
4787 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(dest);
4790 // #510 string(string input, ...) uri_unescape (DP_QC_URI_ESCAPE)
4791 // does URI unescaping on a string (get back the evil stuff)
4792 void VM_uri_unescape (void)
4794 char src[VM_STRINGTEMP_LENGTH];
4795 char dest[VM_STRINGTEMP_LENGTH];
4799 VM_SAFEPARMCOUNTRANGE(1, 8, VM_uri_unescape);
4800 VM_VarString(0, src, sizeof(src));
4802 for(p = src, q = dest; *p; ) // no need to check size, because unescape can't expand
4806 if(p[1] >= '0' && p[1] <= '9')
4808 else if(p[1] >= 'a' && p[1] <= 'f')
4809 hi = p[1] - 'a' + 10;
4810 else if(p[1] >= 'A' && p[1] <= 'F')
4811 hi = p[1] - 'A' + 10;
4814 if(p[2] >= '0' && p[2] <= '9')
4816 else if(p[2] >= 'a' && p[2] <= 'f')
4817 lo = p[2] - 'a' + 10;
4818 else if(p[2] >= 'A' && p[2] <= 'F')
4819 lo = p[2] - 'A' + 10;
4822 if(hi != 0 || lo != 0) // don't unescape NUL bytes
4823 *q++ = (char) (hi * 0x10 + lo);
4834 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(dest);
4837 // #502 string(string filename) whichpack (DP_QC_WHICHPACK)
4838 // returns the name of the pack containing a file, or "" if it is not in any pack (but local or non-existant)
4839 void VM_whichpack (void)
4841 const char *fn, *pack;
4843 fn = PRVM_G_STRING(OFS_PARM0);
4844 pack = FS_WhichPack(fn);
4846 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(pack ? pack : "");