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 mode = (int)PRVM_G_FLOAT(OFS_PARM1);
1591 case 0: // FILE_READ
1594 case 1: // FILE_APPEND
1597 case 2: // FILE_WRITE
1601 PRVM_G_FLOAT(OFS_RETURN) = -3;
1602 VM_Warning("VM_fopen: %s: no such mode %i (valid: 0 = read, 1 = append, 2 = write)\n", PRVM_NAME, mode);
1605 filename = PRVM_G_STRING(OFS_PARM0);
1607 prog->openfiles[filenum] = FS_Open(va("data/%s", filename), modestring, false, false);
1608 if (prog->openfiles[filenum] == NULL && mode == 0)
1609 prog->openfiles[filenum] = FS_Open(va("%s", filename), modestring, false, false);
1611 if (prog->openfiles[filenum] == NULL)
1613 PRVM_G_FLOAT(OFS_RETURN) = -1;
1614 if (developer.integer >= 100)
1615 VM_Warning("VM_fopen: %s: %s mode %s failed\n", PRVM_NAME, filename, modestring);
1619 PRVM_G_FLOAT(OFS_RETURN) = filenum;
1620 if (developer.integer >= 100)
1621 Con_Printf("VM_fopen: %s: %s mode %s opened as #%i\n", PRVM_NAME, filename, modestring, filenum);
1629 fclose(float fhandle)
1632 //void(float fhandle) fclose = #111; // closes a file
1633 void VM_fclose(void)
1637 VM_SAFEPARMCOUNT(1,VM_fclose);
1639 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1640 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
1642 VM_Warning("VM_fclose: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1645 if (prog->openfiles[filenum] == NULL)
1647 VM_Warning("VM_fclose: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1650 FS_Close(prog->openfiles[filenum]);
1651 prog->openfiles[filenum] = NULL;
1652 if (developer.integer >= 100)
1653 Con_Printf("VM_fclose: %s: #%i closed\n", PRVM_NAME, filenum);
1660 string fgets(float fhandle)
1663 //string(float fhandle) fgets = #112; // reads a line of text from the file and returns as a tempstring
1667 char string[VM_STRINGTEMP_LENGTH];
1670 VM_SAFEPARMCOUNT(1,VM_fgets);
1672 // set the return value regardless of any possible errors
1673 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
1675 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1676 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
1678 VM_Warning("VM_fgets: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1681 if (prog->openfiles[filenum] == NULL)
1683 VM_Warning("VM_fgets: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1689 c = FS_Getc(prog->openfiles[filenum]);
1690 if (c == '\r' || c == '\n' || c < 0)
1692 if (end < VM_STRINGTEMP_LENGTH - 1)
1696 // remove \n following \r
1699 c = FS_Getc(prog->openfiles[filenum]);
1701 FS_UnGetc(prog->openfiles[filenum], (unsigned char)c);
1703 if (developer.integer >= 100)
1704 Con_Printf("fgets: %s: %s\n", PRVM_NAME, string);
1706 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string);
1713 fputs(float fhandle, string s)
1716 //void(float fhandle, string s) fputs = #113; // writes a line of text to the end of the file
1720 char string[VM_STRINGTEMP_LENGTH];
1723 VM_SAFEPARMCOUNT(2,VM_fputs);
1725 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1726 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
1728 VM_Warning("VM_fputs: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1731 if (prog->openfiles[filenum] == NULL)
1733 VM_Warning("VM_fputs: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1736 VM_VarString(1, string, sizeof(string));
1737 if ((stringlength = (int)strlen(string)))
1738 FS_Write(prog->openfiles[filenum], string, stringlength);
1739 if (developer.integer >= 100)
1740 Con_Printf("fputs: %s: %s\n", PRVM_NAME, string);
1747 writetofile(float fhandle, entity ent)
1750 void VM_writetofile(void)
1755 VM_SAFEPARMCOUNT(2, VM_writetofile);
1757 file = VM_GetFileHandle( (int)PRVM_G_FLOAT(OFS_PARM0) );
1760 VM_Warning("VM_writetofile: invalid or closed file handle\n");
1764 ent = PRVM_G_EDICT(OFS_PARM1);
1765 if(ent->priv.required->free)
1767 VM_Warning("VM_writetofile: %s: entity %i is free !\n", PRVM_NAME, PRVM_NUM_FOR_EDICT(ent));
1771 PRVM_ED_Write (file, ent);
1774 // KrimZon - DP_QC_ENTITYDATA
1779 float() numentityfields
1780 Return the number of entity fields - NOT offsets
1783 void VM_numentityfields(void)
1785 PRVM_G_FLOAT(OFS_RETURN) = prog->progs->numfielddefs;
1788 // KrimZon - DP_QC_ENTITYDATA
1793 string(float fieldnum) entityfieldname
1794 Return name of the specified field as a string, or empty if the field is invalid (warning)
1797 void VM_entityfieldname(void)
1800 int i = (int)PRVM_G_FLOAT(OFS_PARM0);
1802 if (i < 0 || i >= prog->progs->numfielddefs)
1804 VM_Warning("VM_entityfieldname: %s: field index out of bounds\n", PRVM_NAME);
1805 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString("");
1809 d = &prog->fielddefs[i];
1810 PRVM_G_INT(OFS_RETURN) = d->s_name; // presuming that s_name points to a string already
1813 // KrimZon - DP_QC_ENTITYDATA
1818 float(float fieldnum) entityfieldtype
1821 void VM_entityfieldtype(void)
1824 int i = (int)PRVM_G_FLOAT(OFS_PARM0);
1826 if (i < 0 || i >= prog->progs->numfielddefs)
1828 VM_Warning("VM_entityfieldtype: %s: field index out of bounds\n", PRVM_NAME);
1829 PRVM_G_FLOAT(OFS_RETURN) = -1.0;
1833 d = &prog->fielddefs[i];
1834 PRVM_G_FLOAT(OFS_RETURN) = (float)d->type;
1837 // KrimZon - DP_QC_ENTITYDATA
1840 VM_getentityfieldstring
1842 string(float fieldnum, entity ent) getentityfieldstring
1845 void VM_getentityfieldstring(void)
1847 // put the data into a string
1852 int i = (int)PRVM_G_FLOAT(OFS_PARM0);
1854 if (i < 0 || i >= prog->progs->numfielddefs)
1856 VM_Warning("VM_entityfielddata: %s: field index out of bounds\n", PRVM_NAME);
1857 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString("");
1861 d = &prog->fielddefs[i];
1864 ent = PRVM_G_EDICT(OFS_PARM1);
1865 if(ent->priv.required->free)
1867 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString("");
1868 VM_Warning("VM_entityfielddata: %s: entity %i is free !\n", PRVM_NAME, PRVM_NUM_FOR_EDICT(ent));
1871 v = (int *)((char *)ent->fields.vp + d->ofs*4);
1873 // if it's 0 or blank, return an empty string
1874 type = d->type & ~DEF_SAVEGLOBAL;
1875 for (j=0 ; j<prvm_type_size[type] ; j++)
1878 if (j == prvm_type_size[type])
1880 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString("");
1884 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(PRVM_UglyValueString((etype_t)d->type, (prvm_eval_t *)v));
1887 // KrimZon - DP_QC_ENTITYDATA
1890 VM_putentityfieldstring
1892 float(float fieldnum, entity ent, string s) putentityfieldstring
1895 void VM_putentityfieldstring(void)
1899 int i = (int)PRVM_G_FLOAT(OFS_PARM0);
1901 if (i < 0 || i >= prog->progs->numfielddefs)
1903 VM_Warning("VM_entityfielddata: %s: field index out of bounds\n", PRVM_NAME);
1904 PRVM_G_FLOAT(OFS_RETURN) = 0.0f;
1908 d = &prog->fielddefs[i];
1911 ent = PRVM_G_EDICT(OFS_PARM1);
1912 if(ent->priv.required->free)
1914 VM_Warning("VM_entityfielddata: %s: entity %i is free !\n", PRVM_NAME, PRVM_NUM_FOR_EDICT(ent));
1915 PRVM_G_FLOAT(OFS_RETURN) = 0.0f;
1919 // parse the string into the value
1920 PRVM_G_FLOAT(OFS_RETURN) = ( PRVM_ED_ParseEpair(ent, d, PRVM_G_STRING(OFS_PARM2)) ) ? 1.0f : 0.0f;
1927 float strlen(string s)
1930 //float(string s) strlen = #114; // returns how many characters are in a string
1931 void VM_strlen(void)
1933 VM_SAFEPARMCOUNT(1,VM_strlen);
1935 PRVM_G_FLOAT(OFS_RETURN) = strlen(PRVM_G_STRING(OFS_PARM0));
1938 // DRESK - Decolorized String
1943 string strdecolorize(string s)
1946 // string (string s) strdecolorize = #472; // returns the passed in string with color codes stripped
1947 void VM_strdecolorize(void)
1949 char szNewString[VM_STRINGTEMP_LENGTH];
1950 const char *szString;
1953 VM_SAFEPARMCOUNT(1,VM_strdecolorize);
1954 szString = PRVM_G_STRING(OFS_PARM0);
1956 COM_StringDecolorize(szString, 0, szNewString, sizeof(szNewString), TRUE);
1958 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(szNewString);
1961 // DRESK - String Length (not counting color codes)
1966 float strlennocol(string s)
1969 // float(string s) strlennocol = #471; // returns how many characters are in a string not including color codes
1970 // For example, ^2Dresk returns a length of 5
1971 void VM_strlennocol(void)
1973 const char *szString;
1976 VM_SAFEPARMCOUNT(1,VM_strlennocol);
1978 szString = PRVM_G_STRING(OFS_PARM0);
1980 nCnt = COM_StringLengthNoColors(szString, 0, NULL);
1982 PRVM_G_FLOAT(OFS_RETURN) = nCnt;
1985 // DRESK - String to Uppercase and Lowercase
1990 string strtolower(string s)
1993 // string (string s) strtolower = #480; // returns passed in string in lowercase form
1994 void VM_strtolower(void)
1996 char szNewString[VM_STRINGTEMP_LENGTH];
1997 const char *szString;
2000 VM_SAFEPARMCOUNT(1,VM_strtolower);
2001 szString = PRVM_G_STRING(OFS_PARM0);
2003 COM_ToLowerString(szString, szNewString, sizeof(szNewString) );
2005 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(szNewString);
2012 string strtoupper(string s)
2015 // string (string s) strtoupper = #481; // returns passed in string in uppercase form
2016 void VM_strtoupper(void)
2018 char szNewString[VM_STRINGTEMP_LENGTH];
2019 const char *szString;
2022 VM_SAFEPARMCOUNT(1,VM_strtoupper);
2023 szString = PRVM_G_STRING(OFS_PARM0);
2025 COM_ToUpperString(szString, szNewString, sizeof(szNewString) );
2027 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(szNewString);
2034 string strcat(string,string,...[string])
2037 //string(string s1, string s2) strcat = #115;
2038 // concatenates two strings (for example "abc", "def" would return "abcdef")
2039 // and returns as a tempstring
2040 void VM_strcat(void)
2042 char s[VM_STRINGTEMP_LENGTH];
2043 VM_SAFEPARMCOUNTRANGE(1, 8, VM_strcat);
2045 VM_VarString(0, s, sizeof(s));
2046 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(s);
2053 string substring(string s, float start, float length)
2056 // string(string s, float start, float length) substring = #116;
2057 // returns a section of a string as a tempstring
2058 void VM_substring(void)
2060 int i, start, length;
2062 char string[VM_STRINGTEMP_LENGTH];
2064 VM_SAFEPARMCOUNT(3,VM_substring);
2066 s = PRVM_G_STRING(OFS_PARM0);
2067 start = (int)PRVM_G_FLOAT(OFS_PARM1);
2068 length = (int)PRVM_G_FLOAT(OFS_PARM2);
2069 for (i = 0;i < start && *s;i++, s++);
2070 for (i = 0;i < (int)sizeof(string) - 1 && *s && i < length;i++, s++)
2073 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string);
2080 string(string search, string replace, string subject) strreplace = #484;
2083 // replaces all occurrences of search with replace in the string subject, and returns the result
2084 void VM_strreplace(void)
2087 const char *search, *replace, *subject;
2088 char string[VM_STRINGTEMP_LENGTH];
2089 int search_len, replace_len, subject_len;
2091 VM_SAFEPARMCOUNT(3,VM_strreplace);
2093 search = PRVM_G_STRING(OFS_PARM0);
2094 replace = PRVM_G_STRING(OFS_PARM1);
2095 subject = PRVM_G_STRING(OFS_PARM2);
2097 search_len = (int)strlen(search);
2098 replace_len = (int)strlen(replace);
2099 subject_len = (int)strlen(subject);
2102 for (i = 0; i < subject_len; i++)
2104 for (j = 0; j < search_len && i+j < subject_len; j++)
2105 if (subject[i+j] != search[j])
2107 if (j == search_len || i+j == subject_len)
2109 // found it at offset 'i'
2110 for (j = 0; j < replace_len && si < (int)sizeof(string) - 1; j++)
2111 string[si++] = replace[j];
2112 i += search_len - 1;
2117 if (si < (int)sizeof(string) - 1)
2118 string[si++] = subject[i];
2123 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string);
2130 string(string search, string replace, string subject) strireplace = #485;
2133 // case-insensitive version of strreplace
2134 void VM_strireplace(void)
2137 const char *search, *replace, *subject;
2138 char string[VM_STRINGTEMP_LENGTH];
2139 int search_len, replace_len, subject_len;
2141 VM_SAFEPARMCOUNT(3,VM_strreplace);
2143 search = PRVM_G_STRING(OFS_PARM0);
2144 replace = PRVM_G_STRING(OFS_PARM1);
2145 subject = PRVM_G_STRING(OFS_PARM2);
2147 search_len = (int)strlen(search);
2148 replace_len = (int)strlen(replace);
2149 subject_len = (int)strlen(subject);
2152 for (i = 0; i < subject_len; i++)
2154 for (j = 0; j < search_len && i+j < subject_len; j++)
2155 if (tolower(subject[i+j]) != tolower(search[j]))
2157 if (j == search_len || i+j == subject_len)
2159 // found it at offset 'i'
2160 for (j = 0; j < replace_len && si < (int)sizeof(string) - 1; j++)
2161 string[si++] = replace[j];
2162 i += search_len - 1;
2167 if (si < (int)sizeof(string) - 1)
2168 string[si++] = subject[i];
2173 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string);
2180 vector stov(string s)
2183 //vector(string s) stov = #117; // returns vector value from a string
2186 char string[VM_STRINGTEMP_LENGTH];
2188 VM_SAFEPARMCOUNT(1,VM_stov);
2190 VM_VarString(0, string, sizeof(string));
2191 Math_atov(string, PRVM_G_VECTOR(OFS_RETURN));
2198 string strzone(string s)
2201 //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)
2202 void VM_strzone(void)
2205 char string[VM_STRINGTEMP_LENGTH];
2208 VM_SAFEPARMCOUNT(1,VM_strzone);
2210 VM_VarString(0, string, sizeof(string));
2211 alloclen = strlen(string) + 1;
2212 PRVM_G_INT(OFS_RETURN) = PRVM_AllocString(alloclen, &out);
2213 memcpy(out, string, alloclen);
2223 //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!!!)
2224 void VM_strunzone(void)
2226 VM_SAFEPARMCOUNT(1,VM_strunzone);
2227 PRVM_FreeString(PRVM_G_INT(OFS_PARM0));
2232 VM_command (used by client and menu)
2234 clientcommand(float client, string s) (for client and menu)
2237 //void(entity e, string s) clientcommand = #440; // executes a command string as if it came from the specified client
2238 //this function originally written by KrimZon, made shorter by LordHavoc
2239 void VM_clcommand (void)
2241 client_t *temp_client;
2244 VM_SAFEPARMCOUNT(2,VM_clcommand);
2246 i = (int)PRVM_G_FLOAT(OFS_PARM0);
2247 if (!sv.active || i < 0 || i >= svs.maxclients || !svs.clients[i].active)
2249 VM_Warning("VM_clientcommand: %s: invalid client/server is not active !\n", PRVM_NAME);
2253 temp_client = host_client;
2254 host_client = svs.clients + i;
2255 Cmd_ExecuteString (PRVM_G_STRING(OFS_PARM1), src_client);
2256 host_client = temp_client;
2264 float tokenize(string s)
2267 //float(string s) tokenize = #441; // takes apart a string into individal words (access them with argv), returns how many
2268 //this function originally written by KrimZon, made shorter by LordHavoc
2269 //20040203: rewritten by LordHavoc (no longer uses allocations)
2272 void VM_tokenize (void)
2275 static char string[VM_STRINGTEMP_LENGTH]; // static, because it's big
2277 VM_SAFEPARMCOUNT(1,VM_tokenize);
2279 strlcpy(string, PRVM_G_STRING(OFS_PARM0), sizeof(string));
2283 while(COM_ParseToken_VM_Tokenize(&p, false))
2285 if (num_tokens >= (int)(sizeof(tokens)/sizeof(tokens[0])))
2287 tokens[num_tokens++] = PRVM_SetTempString(com_token);
2290 PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
2295 VM_tokenizebyseparator
2297 float tokenizebyseparator(string s, string separator1, ...)
2300 //float(string s, string separator1, ...) tokenizebyseparator = #479; // takes apart a string into individal words (access them with argv), returns how many
2301 //this function returns the token preceding each instance of a separator (of
2302 //which there can be multiple), and the text following the last separator
2303 //useful for parsing certain kinds of data like IP addresses
2305 //numnumbers = tokenizebyseparator("10.1.2.3", ".");
2306 //returns 4 and the tokens "10" "1" "2" "3".
2307 void VM_tokenizebyseparator (void)
2311 int separatorlen[7];
2312 const char *separators[7];
2315 char tokentext[MAX_INPUTLINE];
2316 static char string[VM_STRINGTEMP_LENGTH]; // static, because it's big
2318 VM_SAFEPARMCOUNTRANGE(2, 8,VM_tokenizebyseparator);
2320 strlcpy(string, PRVM_G_STRING(OFS_PARM0), sizeof(string));
2324 for (j = 1;j < prog->argc;j++)
2326 // skip any blank separator strings
2327 const char *s = PRVM_G_STRING(OFS_PARM0+j*3);
2330 separators[numseparators] = s;
2331 separatorlen[numseparators] = strlen(s);
2338 while (num_tokens < (int)(sizeof(tokens)/sizeof(tokens[0])))
2340 token = tokentext + j;
2343 for (k = 0;k < numseparators;k++)
2345 if (!strncmp(p, separators[k], separatorlen[k]))
2347 p += separatorlen[k];
2351 if (k < numseparators)
2353 if (j < (int)sizeof(tokentext)-1)
2354 tokentext[j++] = *p;
2357 if (j >= (int)sizeof(tokentext))
2360 tokens[num_tokens++] = PRVM_SetTempString(token);
2365 PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
2368 //string(float n) argv = #442; // returns a word from the tokenized string (returns nothing for an invalid index)
2369 //this function originally written by KrimZon, made shorter by LordHavoc
2374 VM_SAFEPARMCOUNT(1,VM_argv);
2376 token_num = (int)PRVM_G_FLOAT(OFS_PARM0);
2378 if (token_num >= 0 && token_num < num_tokens)
2379 PRVM_G_INT(OFS_RETURN) = tokens[token_num];
2381 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
2391 void VM_isserver(void)
2393 VM_SAFEPARMCOUNT(0,VM_serverstate);
2395 PRVM_G_FLOAT(OFS_RETURN) = sv.active && (svs.maxclients > 1 || cls.state == ca_dedicated);
2405 void VM_clientcount(void)
2407 VM_SAFEPARMCOUNT(0,VM_clientcount);
2409 PRVM_G_FLOAT(OFS_RETURN) = svs.maxclients;
2419 void VM_clientstate(void)
2421 VM_SAFEPARMCOUNT(0,VM_clientstate);
2424 switch( cls.state ) {
2425 case ca_uninitialized:
2427 PRVM_G_FLOAT(OFS_RETURN) = 0;
2429 case ca_disconnected:
2430 PRVM_G_FLOAT(OFS_RETURN) = 1;
2433 PRVM_G_FLOAT(OFS_RETURN) = 2;
2436 // should never be reached!
2445 float getostype(void)
2447 */ // not used at the moment -> not included in the common list
2448 void VM_getostype(void)
2450 VM_SAFEPARMCOUNT(0,VM_getostype);
2455 OS_MAC - not supported
2459 PRVM_G_FLOAT(OFS_RETURN) = 0;
2460 #elif defined(MACOSX)
2461 PRVM_G_FLOAT(OFS_RETURN) = 2;
2463 PRVM_G_FLOAT(OFS_RETURN) = 1;
2474 void VM_gettime(void)
2476 VM_SAFEPARMCOUNT(0,VM_gettime);
2478 PRVM_G_FLOAT(OFS_RETURN) = (float) realtime;
2485 loadfromdata(string data)
2488 void VM_loadfromdata(void)
2490 VM_SAFEPARMCOUNT(1,VM_loadentsfromfile);
2492 PRVM_ED_LoadFromFile(PRVM_G_STRING(OFS_PARM0));
2496 ========================
2499 parseentitydata(entity ent, string data)
2500 ========================
2502 void VM_parseentitydata(void)
2507 VM_SAFEPARMCOUNT(2, VM_parseentitydata);
2509 // get edict and test it
2510 ent = PRVM_G_EDICT(OFS_PARM0);
2511 if (ent->priv.required->free)
2512 PRVM_ERROR ("VM_parseentitydata: %s: Can only set already spawned entities (entity %i is free)!", PRVM_NAME, PRVM_NUM_FOR_EDICT(ent));
2514 data = PRVM_G_STRING(OFS_PARM1);
2516 // parse the opening brace
2517 if (!COM_ParseToken_Simple(&data, false, false) || com_token[0] != '{' )
2518 PRVM_ERROR ("VM_parseentitydata: %s: Couldn't parse entity data:\n%s", PRVM_NAME, data );
2520 PRVM_ED_ParseEdict (data, ent);
2527 loadfromfile(string file)
2530 void VM_loadfromfile(void)
2532 const char *filename;
2535 VM_SAFEPARMCOUNT(1,VM_loadfromfile);
2537 filename = PRVM_G_STRING(OFS_PARM0);
2538 if (FS_CheckNastyPath(filename, false))
2540 PRVM_G_FLOAT(OFS_RETURN) = -4;
2541 VM_Warning("VM_loadfromfile: %s dangerous or non-portable filename \"%s\" not allowed. (contains : or \\ or begins with .. or /)\n", PRVM_NAME, filename);
2545 // not conform with VM_fopen
2546 data = (char *)FS_LoadFile(filename, tempmempool, false, NULL);
2548 PRVM_G_FLOAT(OFS_RETURN) = -1;
2550 PRVM_ED_LoadFromFile(data);
2561 float mod(float val, float m)
2564 void VM_modulo(void)
2567 VM_SAFEPARMCOUNT(2,VM_module);
2569 val = (int) PRVM_G_FLOAT(OFS_PARM0);
2570 m = (int) PRVM_G_FLOAT(OFS_PARM1);
2572 PRVM_G_FLOAT(OFS_RETURN) = (float) (val % m);
2575 void VM_Search_Init(void)
2578 for (i = 0;i < PRVM_MAX_OPENSEARCHES;i++)
2579 prog->opensearches[i] = NULL;
2582 void VM_Search_Reset(void)
2585 // reset the fssearch list
2586 for(i = 0; i < PRVM_MAX_OPENSEARCHES; i++)
2588 if(prog->opensearches[i])
2589 FS_FreeSearch(prog->opensearches[i]);
2590 prog->opensearches[i] = NULL;
2598 float search_begin(string pattern, float caseinsensitive, float quiet)
2601 void VM_search_begin(void)
2604 const char *pattern;
2605 int caseinsens, quiet;
2607 VM_SAFEPARMCOUNT(3, VM_search_begin);
2609 pattern = PRVM_G_STRING(OFS_PARM0);
2611 VM_CheckEmptyString(pattern);
2613 caseinsens = (int)PRVM_G_FLOAT(OFS_PARM1);
2614 quiet = (int)PRVM_G_FLOAT(OFS_PARM2);
2616 for(handle = 0; handle < PRVM_MAX_OPENSEARCHES; handle++)
2617 if(!prog->opensearches[handle])
2620 if(handle >= PRVM_MAX_OPENSEARCHES)
2622 PRVM_G_FLOAT(OFS_RETURN) = -2;
2623 VM_Warning("VM_search_begin: %s ran out of search handles (%i)\n", PRVM_NAME, PRVM_MAX_OPENSEARCHES);
2627 if(!(prog->opensearches[handle] = FS_Search(pattern,caseinsens, quiet)))
2628 PRVM_G_FLOAT(OFS_RETURN) = -1;
2630 PRVM_G_FLOAT(OFS_RETURN) = handle;
2637 void search_end(float handle)
2640 void VM_search_end(void)
2643 VM_SAFEPARMCOUNT(1, VM_search_end);
2645 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
2647 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
2649 VM_Warning("VM_search_end: invalid handle %i used in %s\n", handle, PRVM_NAME);
2652 if(prog->opensearches[handle] == NULL)
2654 VM_Warning("VM_search_end: no such handle %i in %s\n", handle, PRVM_NAME);
2658 FS_FreeSearch(prog->opensearches[handle]);
2659 prog->opensearches[handle] = NULL;
2666 float search_getsize(float handle)
2669 void VM_search_getsize(void)
2672 VM_SAFEPARMCOUNT(1, VM_M_search_getsize);
2674 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
2676 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
2678 VM_Warning("VM_search_getsize: invalid handle %i used in %s\n", handle, PRVM_NAME);
2681 if(prog->opensearches[handle] == NULL)
2683 VM_Warning("VM_search_getsize: no such handle %i in %s\n", handle, PRVM_NAME);
2687 PRVM_G_FLOAT(OFS_RETURN) = prog->opensearches[handle]->numfilenames;
2692 VM_search_getfilename
2694 string search_getfilename(float handle, float num)
2697 void VM_search_getfilename(void)
2699 int handle, filenum;
2700 VM_SAFEPARMCOUNT(2, VM_search_getfilename);
2702 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
2703 filenum = (int)PRVM_G_FLOAT(OFS_PARM1);
2705 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
2707 VM_Warning("VM_search_getfilename: invalid handle %i used in %s\n", handle, PRVM_NAME);
2710 if(prog->opensearches[handle] == NULL)
2712 VM_Warning("VM_search_getfilename: no such handle %i in %s\n", handle, PRVM_NAME);
2715 if(filenum < 0 || filenum >= prog->opensearches[handle]->numfilenames)
2717 VM_Warning("VM_search_getfilename: invalid filenum %i in %s\n", filenum, PRVM_NAME);
2721 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog->opensearches[handle]->filenames[filenum]);
2728 string chr(float ascii)
2734 VM_SAFEPARMCOUNT(1, VM_chr);
2736 tmp[0] = (unsigned char) PRVM_G_FLOAT(OFS_PARM0);
2739 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(tmp);
2742 //=============================================================================
2743 // Draw builtins (client & menu)
2749 float iscachedpic(string pic)
2752 void VM_iscachedpic(void)
2754 VM_SAFEPARMCOUNT(1,VM_iscachedpic);
2756 // drawq hasnt such a function, thus always return true
2757 PRVM_G_FLOAT(OFS_RETURN) = false;
2764 string precache_pic(string pic)
2767 void VM_precache_pic(void)
2771 VM_SAFEPARMCOUNT(1, VM_precache_pic);
2773 s = PRVM_G_STRING(OFS_PARM0);
2774 PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
2775 VM_CheckEmptyString (s);
2777 // AK Draw_CachePic is supposed to always return a valid pointer
2778 if( Draw_CachePic_Flags(s, CACHEPICFLAG_NOTPERSISTENT)->tex == r_texture_notexture )
2779 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
2789 void VM_freepic(void)
2793 VM_SAFEPARMCOUNT(1,VM_freepic);
2795 s = PRVM_G_STRING(OFS_PARM0);
2796 VM_CheckEmptyString (s);
2801 dp_font_t *getdrawfont()
2803 if(prog->globaloffsets.drawfont >= 0)
2805 int f = PRVM_G_FLOAT(prog->globaloffsets.drawfont);
2806 if(f < 0 || f >= MAX_FONTS)
2807 return FONT_DEFAULT;
2808 return &dp_fonts[f];
2811 return FONT_DEFAULT;
2818 float drawcharacter(vector position, float character, vector scale, vector rgb, float alpha, float flag)
2821 void VM_drawcharacter(void)
2823 float *pos,*scale,*rgb;
2826 VM_SAFEPARMCOUNT(6,VM_drawcharacter);
2828 character = (char) PRVM_G_FLOAT(OFS_PARM1);
2831 PRVM_G_FLOAT(OFS_RETURN) = -1;
2832 VM_Warning("VM_drawcharacter: %s passed null character !\n",PRVM_NAME);
2836 pos = PRVM_G_VECTOR(OFS_PARM0);
2837 scale = PRVM_G_VECTOR(OFS_PARM2);
2838 rgb = PRVM_G_VECTOR(OFS_PARM3);
2839 flag = (int)PRVM_G_FLOAT(OFS_PARM5);
2841 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2843 PRVM_G_FLOAT(OFS_RETURN) = -2;
2844 VM_Warning("VM_drawcharacter: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2848 if(pos[2] || scale[2])
2849 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")));
2851 if(!scale[0] || !scale[1])
2853 PRVM_G_FLOAT(OFS_RETURN) = -3;
2854 VM_Warning("VM_drawcharacter: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
2858 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());
2859 PRVM_G_FLOAT(OFS_RETURN) = 1;
2866 float drawstring(vector position, string text, vector scale, vector rgb, float alpha, float flag)
2869 void VM_drawstring(void)
2871 float *pos,*scale,*rgb;
2874 VM_SAFEPARMCOUNT(6,VM_drawstring);
2876 string = PRVM_G_STRING(OFS_PARM1);
2877 pos = PRVM_G_VECTOR(OFS_PARM0);
2878 scale = PRVM_G_VECTOR(OFS_PARM2);
2879 rgb = PRVM_G_VECTOR(OFS_PARM3);
2880 flag = (int)PRVM_G_FLOAT(OFS_PARM5);
2882 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2884 PRVM_G_FLOAT(OFS_RETURN) = -2;
2885 VM_Warning("VM_drawstring: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2889 if(!scale[0] || !scale[1])
2891 PRVM_G_FLOAT(OFS_RETURN) = -3;
2892 VM_Warning("VM_drawstring: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
2896 if(pos[2] || scale[2])
2897 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")));
2899 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());
2900 PRVM_G_FLOAT(OFS_RETURN) = 1;
2905 VM_drawcolorcodedstring
2907 float drawcolorcodedstring(vector position, string text, vector scale, float alpha, float flag)
2910 void VM_drawcolorcodedstring(void)
2915 VM_SAFEPARMCOUNT(5,VM_drawstring);
2917 string = PRVM_G_STRING(OFS_PARM1);
2918 pos = PRVM_G_VECTOR(OFS_PARM0);
2919 scale = PRVM_G_VECTOR(OFS_PARM2);
2920 flag = (int)PRVM_G_FLOAT(OFS_PARM5);
2922 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2924 PRVM_G_FLOAT(OFS_RETURN) = -2;
2925 VM_Warning("VM_drawcolorcodedstring: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2929 if(!scale[0] || !scale[1])
2931 PRVM_G_FLOAT(OFS_RETURN) = -3;
2932 VM_Warning("VM_drawcolorcodedstring: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
2936 if(pos[2] || scale[2])
2937 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")));
2940 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());
2941 PRVM_G_FLOAT(OFS_RETURN) = 1;
2947 float stringwidth(string text, float allowColorCodes)
2950 void VM_stringwidth(void)
2954 VM_SAFEPARMCOUNT(2,VM_drawstring);
2956 string = PRVM_G_STRING(OFS_PARM0);
2957 colors = (int)PRVM_G_FLOAT(OFS_PARM1);
2959 PRVM_G_FLOAT(OFS_RETURN) = DrawQ_TextWidth_Font(string, 0, !colors, getdrawfont()); // 1x1 characters, don't actually draw
2965 float drawpic(vector position, string pic, vector size, vector rgb, float alpha, float flag)
2968 void VM_drawpic(void)
2970 const char *picname;
2971 float *size, *pos, *rgb;
2974 VM_SAFEPARMCOUNT(6,VM_drawpic);
2976 picname = PRVM_G_STRING(OFS_PARM1);
2977 VM_CheckEmptyString (picname);
2979 // is pic cached ? no function yet for that
2982 PRVM_G_FLOAT(OFS_RETURN) = -4;
2983 VM_Warning("VM_drawpic: %s: %s not cached !\n", PRVM_NAME, picname);
2987 pos = PRVM_G_VECTOR(OFS_PARM0);
2988 size = PRVM_G_VECTOR(OFS_PARM2);
2989 rgb = PRVM_G_VECTOR(OFS_PARM3);
2990 flag = (int) PRVM_G_FLOAT(OFS_PARM5);
2992 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2994 PRVM_G_FLOAT(OFS_RETURN) = -2;
2995 VM_Warning("VM_drawpic: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2999 if(pos[2] || size[2])
3000 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")));
3002 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);
3003 PRVM_G_FLOAT(OFS_RETURN) = 1;
3009 float drawsubpic(vector position, vector size, string pic, vector srcPos, vector srcSize, vector rgb, float alpha, float flag)
3013 void VM_drawsubpic(void)
3015 const char *picname;
3016 float *size, *pos, *rgb, *srcPos, *srcSize, alpha;
3019 VM_SAFEPARMCOUNT(8,VM_drawsubpic);
3021 picname = PRVM_G_STRING(OFS_PARM2);
3022 VM_CheckEmptyString (picname);
3024 // is pic cached ? no function yet for that
3027 PRVM_G_FLOAT(OFS_RETURN) = -4;
3028 VM_Warning("VM_drawsubpic: %s: %s not cached !\n", PRVM_NAME, picname);
3032 pos = PRVM_G_VECTOR(OFS_PARM0);
3033 size = PRVM_G_VECTOR(OFS_PARM1);
3034 srcPos = PRVM_G_VECTOR(OFS_PARM3);
3035 srcSize = PRVM_G_VECTOR(OFS_PARM4);
3036 rgb = PRVM_G_VECTOR(OFS_PARM5);
3037 alpha = PRVM_G_FLOAT(OFS_PARM6);
3038 flag = (int) PRVM_G_FLOAT(OFS_PARM7);
3040 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3042 PRVM_G_FLOAT(OFS_RETURN) = -2;
3043 VM_Warning("VM_drawsubpic: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
3047 if(pos[2] || size[2])
3048 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")));
3050 DrawQ_SuperPic(pos[0], pos[1], Draw_CachePic (picname),
3052 srcPos[0], srcPos[1], rgb[0], rgb[1], rgb[2], alpha,
3053 srcPos[0] + srcSize[0], srcPos[1], rgb[0], rgb[1], rgb[2], alpha,
3054 srcPos[0], srcPos[1] + srcSize[1], rgb[0], rgb[1], rgb[2], alpha,
3055 srcPos[0] + srcSize[0], srcPos[1] + srcSize[1], rgb[0], rgb[1], rgb[2], alpha,
3057 PRVM_G_FLOAT(OFS_RETURN) = 1;
3064 float drawfill(vector position, vector size, vector rgb, float alpha, float flag)
3067 void VM_drawfill(void)
3069 float *size, *pos, *rgb;
3072 VM_SAFEPARMCOUNT(5,VM_drawfill);
3075 pos = PRVM_G_VECTOR(OFS_PARM0);
3076 size = PRVM_G_VECTOR(OFS_PARM1);
3077 rgb = PRVM_G_VECTOR(OFS_PARM2);
3078 flag = (int) PRVM_G_FLOAT(OFS_PARM4);
3080 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3082 PRVM_G_FLOAT(OFS_RETURN) = -2;
3083 VM_Warning("VM_drawfill: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
3087 if(pos[2] || size[2])
3088 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")));
3090 DrawQ_Fill(pos[0], pos[1], size[0], size[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM3), flag);
3091 PRVM_G_FLOAT(OFS_RETURN) = 1;
3098 drawsetcliparea(float x, float y, float width, float height)
3101 void VM_drawsetcliparea(void)
3104 VM_SAFEPARMCOUNT(4,VM_drawsetcliparea);
3106 x = bound(0, PRVM_G_FLOAT(OFS_PARM0), vid_conwidth.integer);
3107 y = bound(0, PRVM_G_FLOAT(OFS_PARM1), vid_conheight.integer);
3108 w = bound(0, PRVM_G_FLOAT(OFS_PARM2) + PRVM_G_FLOAT(OFS_PARM0) - x, (vid_conwidth.integer - x));
3109 h = bound(0, PRVM_G_FLOAT(OFS_PARM3) + PRVM_G_FLOAT(OFS_PARM1) - y, (vid_conheight.integer - y));
3111 DrawQ_SetClipArea(x, y, w, h);
3116 VM_drawresetcliparea
3121 void VM_drawresetcliparea(void)
3123 VM_SAFEPARMCOUNT(0,VM_drawresetcliparea);
3125 DrawQ_ResetClipArea();
3132 vector getimagesize(string pic)
3135 void VM_getimagesize(void)
3140 VM_SAFEPARMCOUNT(1,VM_getimagesize);
3142 p = PRVM_G_STRING(OFS_PARM0);
3143 VM_CheckEmptyString (p);
3145 pic = Draw_CachePic_Flags (p, CACHEPICFLAG_NOTPERSISTENT);
3147 PRVM_G_VECTOR(OFS_RETURN)[0] = pic->width;
3148 PRVM_G_VECTOR(OFS_RETURN)[1] = pic->height;
3149 PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
3156 string keynumtostring(float keynum)
3159 void VM_keynumtostring (void)
3161 VM_SAFEPARMCOUNT(1, VM_keynumtostring);
3163 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(Key_KeynumToString((int)PRVM_G_FLOAT(OFS_PARM0)));
3170 float stringtokeynum(string key)
3173 void VM_stringtokeynum (void)
3175 VM_SAFEPARMCOUNT( 1, VM_keynumtostring );
3177 PRVM_G_INT(OFS_RETURN) = Key_StringToKeynum(PRVM_G_STRING(OFS_PARM0));
3180 // CL_Video interface functions
3183 ========================
3186 float cin_open(string file, string name)
3187 ========================
3189 void VM_cin_open( void )
3194 VM_SAFEPARMCOUNT( 2, VM_cin_open );
3196 file = PRVM_G_STRING( OFS_PARM0 );
3197 name = PRVM_G_STRING( OFS_PARM1 );
3199 VM_CheckEmptyString( file );
3200 VM_CheckEmptyString( name );
3202 if( CL_OpenVideo( file, name, MENUOWNER ) )
3203 PRVM_G_FLOAT( OFS_RETURN ) = 1;
3205 PRVM_G_FLOAT( OFS_RETURN ) = 0;
3209 ========================
3212 void cin_close(string name)
3213 ========================
3215 void VM_cin_close( void )
3219 VM_SAFEPARMCOUNT( 1, VM_cin_close );
3221 name = PRVM_G_STRING( OFS_PARM0 );
3222 VM_CheckEmptyString( name );
3224 CL_CloseVideo( CL_GetVideoByName( name ) );
3228 ========================
3230 void cin_setstate(string name, float type)
3231 ========================
3233 void VM_cin_setstate( void )
3236 clvideostate_t state;
3239 VM_SAFEPARMCOUNT( 2, VM_cin_netstate );
3241 name = PRVM_G_STRING( OFS_PARM0 );
3242 VM_CheckEmptyString( name );
3244 state = (clvideostate_t)((int)PRVM_G_FLOAT( OFS_PARM1 ));
3246 video = CL_GetVideoByName( name );
3247 if( video && state > CLVIDEO_UNUSED && state < CLVIDEO_STATECOUNT )
3248 CL_SetVideoState( video, state );
3252 ========================
3255 float cin_getstate(string name)
3256 ========================
3258 void VM_cin_getstate( void )
3263 VM_SAFEPARMCOUNT( 1, VM_cin_getstate );
3265 name = PRVM_G_STRING( OFS_PARM0 );
3266 VM_CheckEmptyString( name );
3268 video = CL_GetVideoByName( name );
3270 PRVM_G_FLOAT( OFS_RETURN ) = (int)video->state;
3272 PRVM_G_FLOAT( OFS_RETURN ) = 0;
3276 ========================
3279 void cin_restart(string name)
3280 ========================
3282 void VM_cin_restart( void )
3287 VM_SAFEPARMCOUNT( 1, VM_cin_restart );
3289 name = PRVM_G_STRING( OFS_PARM0 );
3290 VM_CheckEmptyString( name );
3292 video = CL_GetVideoByName( name );
3294 CL_RestartVideo( video );
3298 ========================
3300 ========================
3302 void VM_Gecko_Init( void ) {
3303 // the prog struct is memset to 0 by Initprog? [12/6/2007 Black]
3304 // FIXME: remove the other _Init functions then, too? [12/6/2007 Black]
3308 ========================
3310 ========================
3312 void VM_Gecko_Destroy( void ) {
3314 for( i = 0 ; i < PRVM_MAX_GECKOINSTANCES ; i++ ) {
3315 clgecko_t **instance = &prog->opengeckoinstances[ i ];
3317 CL_Gecko_DestroyBrowser( *instance );
3324 ========================
3327 float[bool] gecko_create( string name )
3328 ========================
3330 void VM_gecko_create( void ) {
3333 clgecko_t *instance;
3335 VM_SAFEPARMCOUNT( 1, VM_gecko_create );
3337 name = PRVM_G_STRING( OFS_PARM0 );
3338 VM_CheckEmptyString( name );
3340 // find an empty slot for this gecko browser..
3341 for( i = 0 ; i < PRVM_MAX_GECKOINSTANCES ; i++ ) {
3342 if( prog->opengeckoinstances[ i ] == NULL ) {
3346 if( i == PRVM_MAX_GECKOINSTANCES ) {
3347 VM_Warning("VM_gecko_create: %s ran out of gecko handles (%i)\n", PRVM_NAME, PRVM_MAX_GECKOINSTANCES);
3348 PRVM_G_FLOAT( OFS_RETURN ) = 0;
3352 instance = prog->opengeckoinstances[ i ] = CL_Gecko_CreateBrowser( name, PRVM_GetProgNr() );
3354 // TODO: error handling [12/3/2007 Black]
3355 PRVM_G_FLOAT( OFS_RETURN ) = 0;
3358 PRVM_G_FLOAT( OFS_RETURN ) = 1;
3362 ========================
3365 void gecko_destroy( string name )
3366 ========================
3368 void VM_gecko_destroy( void ) {
3370 clgecko_t *instance;
3372 VM_SAFEPARMCOUNT( 1, VM_gecko_destroy );
3374 name = PRVM_G_STRING( OFS_PARM0 );
3375 VM_CheckEmptyString( name );
3376 instance = CL_Gecko_FindBrowser( name );
3380 CL_Gecko_DestroyBrowser( instance );
3384 ========================
3387 void gecko_navigate( string name, string URI )
3388 ========================
3390 void VM_gecko_navigate( void ) {
3393 clgecko_t *instance;
3395 VM_SAFEPARMCOUNT( 2, VM_gecko_navigate );
3397 name = PRVM_G_STRING( OFS_PARM0 );
3398 URI = PRVM_G_STRING( OFS_PARM1 );
3399 VM_CheckEmptyString( name );
3400 VM_CheckEmptyString( URI );
3402 instance = CL_Gecko_FindBrowser( name );
3406 CL_Gecko_NavigateToURI( instance, URI );
3410 ========================
3413 float[bool] gecko_keyevent( string name, float key, float eventtype )
3414 ========================
3416 void VM_gecko_keyevent( void ) {
3419 clgecko_buttoneventtype_t eventtype;
3420 clgecko_t *instance;
3422 VM_SAFEPARMCOUNT( 3, VM_gecko_keyevent );
3424 name = PRVM_G_STRING( OFS_PARM0 );
3425 VM_CheckEmptyString( name );
3426 key = (unsigned int) PRVM_G_FLOAT( OFS_PARM1 );
3427 switch( (unsigned int) PRVM_G_FLOAT( OFS_PARM2 ) ) {
3429 eventtype = CLG_BET_DOWN;
3432 eventtype = CLG_BET_UP;
3435 eventtype = CLG_BET_PRESS;
3438 eventtype = CLG_BET_DOUBLECLICK;
3441 // TODO: console printf? [12/3/2007 Black]
3442 PRVM_G_FLOAT( OFS_RETURN ) = 0;
3446 instance = CL_Gecko_FindBrowser( name );
3448 PRVM_G_FLOAT( OFS_RETURN ) = 0;
3452 PRVM_G_FLOAT( OFS_RETURN ) = (CL_Gecko_Event_Key( instance, key, eventtype ) == true);
3456 ========================
3459 void gecko_mousemove( string name, float x, float y )
3460 ========================
3462 void VM_gecko_movemouse( void ) {
3465 clgecko_t *instance;
3467 VM_SAFEPARMCOUNT( 3, VM_gecko_movemouse );
3469 name = PRVM_G_STRING( OFS_PARM0 );
3470 VM_CheckEmptyString( name );
3471 x = PRVM_G_FLOAT( OFS_PARM1 );
3472 y = PRVM_G_FLOAT( OFS_PARM2 );
3474 instance = CL_Gecko_FindBrowser( name );
3478 CL_Gecko_Event_CursorMove( instance, x, y );
3483 ========================
3486 void gecko_resize( string name, float w, float h )
3487 ========================
3489 void VM_gecko_resize( void ) {
3492 clgecko_t *instance;
3494 VM_SAFEPARMCOUNT( 3, VM_gecko_movemouse );
3496 name = PRVM_G_STRING( OFS_PARM0 );
3497 VM_CheckEmptyString( name );
3498 w = PRVM_G_FLOAT( OFS_PARM1 );
3499 h = PRVM_G_FLOAT( OFS_PARM2 );
3501 instance = CL_Gecko_FindBrowser( name );
3505 CL_Gecko_Resize( instance, w, h );
3510 ========================
3511 VM_gecko_get_texture_extent
3513 vector gecko_get_texture_extent( string name )
3514 ========================
3516 void VM_gecko_get_texture_extent( void ) {
3518 clgecko_t *instance;
3520 VM_SAFEPARMCOUNT( 1, VM_gecko_movemouse );
3522 name = PRVM_G_STRING( OFS_PARM0 );
3523 VM_CheckEmptyString( name );
3525 PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
3526 instance = CL_Gecko_FindBrowser( name );
3528 PRVM_G_VECTOR(OFS_RETURN)[0] = 0;
3529 PRVM_G_VECTOR(OFS_RETURN)[1] = 0;
3532 CL_Gecko_GetTextureExtent( instance,
3533 PRVM_G_VECTOR(OFS_RETURN), PRVM_G_VECTOR(OFS_RETURN)+1 );
3542 Writes new values for v_forward, v_up, and v_right based on angles
3543 void makevectors(vector angle)
3546 void VM_makevectors (void)
3548 prvm_eval_t *valforward, *valright, *valup;
3549 valforward = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_forward);
3550 valright = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_right);
3551 valup = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_up);
3552 if (!valforward || !valright || !valup)
3554 VM_Warning("makevectors: could not find v_forward, v_right, or v_up global variables\n");
3557 VM_SAFEPARMCOUNT(1, VM_makevectors);
3558 AngleVectors (PRVM_G_VECTOR(OFS_PARM0), valforward->vector, valright->vector, valup->vector);
3565 Writes new values for v_forward, v_up, and v_right based on the given forward vector
3566 vectorvectors(vector)
3569 void VM_vectorvectors (void)
3571 prvm_eval_t *valforward, *valright, *valup;
3572 valforward = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_forward);
3573 valright = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_right);
3574 valup = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_up);
3575 if (!valforward || !valright || !valup)
3577 VM_Warning("vectorvectors: could not find v_forward, v_right, or v_up global variables\n");
3580 VM_SAFEPARMCOUNT(1, VM_vectorvectors);
3581 VectorNormalize2(PRVM_G_VECTOR(OFS_PARM0), valforward->vector);
3582 VectorVectors(valforward->vector, valright->vector, valup->vector);
3586 ========================
3589 void drawline(float width, vector pos1, vector pos2, vector rgb, float alpha, float flags)
3590 ========================
3592 void VM_drawline (void)
3594 float *c1, *c2, *rgb;
3596 unsigned char flags;
3598 VM_SAFEPARMCOUNT(6, VM_drawline);
3599 width = PRVM_G_FLOAT(OFS_PARM0);
3600 c1 = PRVM_G_VECTOR(OFS_PARM1);
3601 c2 = PRVM_G_VECTOR(OFS_PARM2);
3602 rgb = PRVM_G_VECTOR(OFS_PARM3);
3603 alpha = PRVM_G_FLOAT(OFS_PARM4);
3604 flags = (int)PRVM_G_FLOAT(OFS_PARM5);
3605 DrawQ_Line(width, c1[0], c1[1], c2[0], c2[1], rgb[0], rgb[1], rgb[2], alpha, flags);
3608 // float(float number, float quantity) bitshift (EXT_BITSHIFT)
3609 void VM_bitshift (void)
3612 VM_SAFEPARMCOUNT(2, VM_bitshift);
3614 n1 = (int)fabs((int)PRVM_G_FLOAT(OFS_PARM0));
3615 n2 = (int)PRVM_G_FLOAT(OFS_PARM1);
3617 PRVM_G_FLOAT(OFS_RETURN) = n1;
3620 PRVM_G_FLOAT(OFS_RETURN) = (n1 >> -n2);
3622 PRVM_G_FLOAT(OFS_RETURN) = (n1 << n2);
3625 ////////////////////////////////////////
3626 // AltString functions
3627 ////////////////////////////////////////
3630 ========================
3633 float altstr_count(string)
3634 ========================
3636 void VM_altstr_count( void )
3638 const char *altstr, *pos;
3641 VM_SAFEPARMCOUNT( 1, VM_altstr_count );
3643 altstr = PRVM_G_STRING( OFS_PARM0 );
3644 //VM_CheckEmptyString( altstr );
3646 for( count = 0, pos = altstr ; *pos ; pos++ ) {
3647 if( *pos == '\\' ) {
3651 } else if( *pos == '\'' ) {
3656 PRVM_G_FLOAT( OFS_RETURN ) = (float) (count / 2);
3660 ========================
3663 string altstr_prepare(string)
3664 ========================
3666 void VM_altstr_prepare( void )
3669 const char *instr, *in;
3671 char outstr[VM_STRINGTEMP_LENGTH];
3673 VM_SAFEPARMCOUNT( 1, VM_altstr_prepare );
3675 instr = PRVM_G_STRING( OFS_PARM0 );
3677 for( out = outstr, in = instr, size = sizeof(outstr) - 1 ; size && *in ; size--, in++, out++ )
3686 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
3690 ========================
3693 string altstr_get(string, float)
3694 ========================
3696 void VM_altstr_get( void )
3698 const char *altstr, *pos;
3701 char outstr[VM_STRINGTEMP_LENGTH];
3703 VM_SAFEPARMCOUNT( 2, VM_altstr_get );
3705 altstr = PRVM_G_STRING( OFS_PARM0 );
3707 count = (int)PRVM_G_FLOAT( OFS_PARM1 );
3708 count = count * 2 + 1;
3710 for( pos = altstr ; *pos && count ; pos++ )
3711 if( *pos == '\\' ) {
3714 } else if( *pos == '\'' )
3718 PRVM_G_INT( OFS_RETURN ) = 0;
3722 for( out = outstr, size = sizeof(outstr) - 1 ; size && *pos ; size--, pos++, out++ )
3723 if( *pos == '\\' ) {
3728 } else if( *pos == '\'' )
3734 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
3738 ========================
3741 string altstr_set(string altstr, float num, string set)
3742 ========================
3744 void VM_altstr_set( void )
3747 const char *altstr, *str;
3750 char outstr[VM_STRINGTEMP_LENGTH];
3752 VM_SAFEPARMCOUNT( 3, VM_altstr_set );
3754 altstr = PRVM_G_STRING( OFS_PARM0 );
3756 num = (int)PRVM_G_FLOAT( OFS_PARM1 );
3758 str = PRVM_G_STRING( OFS_PARM2 );
3761 for( num = num * 2 + 1, in = altstr; *in && num; *out++ = *in++ )
3766 } else if( *in == '\'' ) {
3771 for( ; *str; *out++ = *str++ );
3772 // now jump over the old content
3774 if( *in == '\'' || (*in == '\\' && !*++in) )
3777 strlcpy(out, in, outstr + sizeof(outstr) - out);
3778 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
3782 ========================
3785 string altstr_ins(string altstr, float num, string set)
3786 ========================
3788 void VM_altstr_ins(void)
3796 char outstr[VM_STRINGTEMP_LENGTH];
3798 VM_SAFEPARMCOUNT(3, VM_altstr_ins);
3800 in = instr = PRVM_G_STRING( OFS_PARM0 );
3801 num = (int)PRVM_G_FLOAT( OFS_PARM1 );
3802 set = setstr = PRVM_G_STRING( OFS_PARM2 );
3805 for( num = num * 2 + 2 ; *in && num > 0 ; *out++ = *in++ )
3810 } else if( *in == '\'' ) {
3815 for( ; *set ; *out++ = *set++ );
3818 strlcpy(out, in, outstr + sizeof(outstr) - out);
3819 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
3823 ////////////////////////////////////////
3824 // BufString functions
3825 ////////////////////////////////////////
3826 //[515]: string buffers support
3828 static size_t stringbuffers_sortlength;
3830 static void BufStr_Expand(prvm_stringbuffer_t *stringbuffer, int strindex)
3832 if (stringbuffer->max_strings <= strindex)
3834 char **oldstrings = stringbuffer->strings;
3835 stringbuffer->max_strings = max(stringbuffer->max_strings * 2, 128);
3836 while (stringbuffer->max_strings <= strindex)
3837 stringbuffer->max_strings *= 2;
3838 stringbuffer->strings = Mem_Alloc(prog->progs_mempool, stringbuffer->max_strings * sizeof(stringbuffer->strings[0]));
3839 if (stringbuffer->num_strings > 0)
3840 memcpy(stringbuffer->strings, oldstrings, stringbuffer->num_strings * sizeof(stringbuffer->strings[0]));
3842 Mem_Free(oldstrings);
3846 static void BufStr_Shrink(prvm_stringbuffer_t *stringbuffer)
3848 // reduce num_strings if there are empty string slots at the end
3849 while (stringbuffer->num_strings > 0 && stringbuffer->strings[stringbuffer->num_strings - 1] == NULL)
3850 stringbuffer->num_strings--;
3852 // if empty, free the string pointer array
3853 if (stringbuffer->num_strings == 0)
3855 stringbuffer->max_strings = 0;
3856 if (stringbuffer->strings)
3857 Mem_Free(stringbuffer->strings);
3858 stringbuffer->strings = NULL;
3862 static int BufStr_SortStringsUP (const void *in1, const void *in2)
3865 a = *((const char **) in1);
3866 b = *((const char **) in2);
3868 if(!b[0]) return -1;
3869 return strncmp(a, b, stringbuffers_sortlength);
3872 static int BufStr_SortStringsDOWN (const void *in1, const void *in2)
3875 a = *((const char **) in1);
3876 b = *((const char **) in2);
3878 if(!b[0]) return -1;
3879 return strncmp(b, a, stringbuffers_sortlength);
3883 ========================
3885 creates new buffer, and returns it's index, returns -1 if failed
3886 float buf_create(void) = #460;
3887 ========================
3889 void VM_buf_create (void)
3891 prvm_stringbuffer_t *stringbuffer;
3893 VM_SAFEPARMCOUNT(0, VM_buf_create);
3894 stringbuffer = Mem_ExpandableArray_AllocRecord(&prog->stringbuffersarray);
3895 for (i = 0;stringbuffer != Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, i);i++);
3896 PRVM_G_FLOAT(OFS_RETURN) = i;
3900 ========================
3902 deletes buffer and all strings in it
3903 void buf_del(float bufhandle) = #461;
3904 ========================
3906 void VM_buf_del (void)
3908 prvm_stringbuffer_t *stringbuffer;
3909 VM_SAFEPARMCOUNT(1, VM_buf_del);
3910 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
3914 for (i = 0;i < stringbuffer->num_strings;i++)
3915 if (stringbuffer->strings[i])
3916 Mem_Free(stringbuffer->strings[i]);
3917 if (stringbuffer->strings)
3918 Mem_Free(stringbuffer->strings);
3919 Mem_ExpandableArray_FreeRecord(&prog->stringbuffersarray, stringbuffer);
3923 VM_Warning("VM_buf_del: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3929 ========================
3931 how many strings are stored in buffer
3932 float buf_getsize(float bufhandle) = #462;
3933 ========================
3935 void VM_buf_getsize (void)
3937 prvm_stringbuffer_t *stringbuffer;
3938 VM_SAFEPARMCOUNT(1, VM_buf_getsize);
3940 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
3943 PRVM_G_FLOAT(OFS_RETURN) = -1;
3944 VM_Warning("VM_buf_getsize: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3948 PRVM_G_FLOAT(OFS_RETURN) = stringbuffer->num_strings;
3952 ========================
3954 copy all content from one buffer to another, make sure it exists
3955 void buf_copy(float bufhandle_from, float bufhandle_to) = #463;
3956 ========================
3958 void VM_buf_copy (void)
3960 prvm_stringbuffer_t *srcstringbuffer, *dststringbuffer;
3962 VM_SAFEPARMCOUNT(2, VM_buf_copy);
3964 srcstringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
3965 if(!srcstringbuffer)
3967 VM_Warning("VM_buf_copy: invalid source buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3970 i = (int)PRVM_G_FLOAT(OFS_PARM1);
3971 if(i == (int)PRVM_G_FLOAT(OFS_PARM0))
3973 VM_Warning("VM_buf_copy: source == destination (%i) in %s\n", i, PRVM_NAME);
3976 dststringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
3977 if(!dststringbuffer)
3979 VM_Warning("VM_buf_copy: invalid destination buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM1), PRVM_NAME);
3983 for (i = 0;i < dststringbuffer->num_strings;i++)
3984 if (dststringbuffer->strings[i])
3985 Mem_Free(dststringbuffer->strings[i]);
3986 if (dststringbuffer->strings)
3987 Mem_Free(dststringbuffer->strings);
3988 *dststringbuffer = *srcstringbuffer;
3989 if (dststringbuffer->max_strings)
3990 dststringbuffer->strings = (char **)Mem_Alloc(prog->progs_mempool, sizeof(dststringbuffer->strings[0]) * dststringbuffer->max_strings);
3992 for (i = 0;i < dststringbuffer->num_strings;i++)
3994 if (srcstringbuffer->strings[i])
3997 stringlen = strlen(srcstringbuffer->strings[i]) + 1;
3998 dststringbuffer->strings[i] = (char *)Mem_Alloc(prog->progs_mempool, stringlen);
3999 memcpy(dststringbuffer->strings[i], srcstringbuffer->strings[i], stringlen);
4005 ========================
4007 sort buffer by beginnings of strings (cmplength defaults it's length)
4008 "backward == TRUE" means that sorting goes upside-down
4009 void buf_sort(float bufhandle, float cmplength, float backward) = #464;
4010 ========================
4012 void VM_buf_sort (void)
4014 prvm_stringbuffer_t *stringbuffer;
4015 VM_SAFEPARMCOUNT(3, VM_buf_sort);
4017 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4020 VM_Warning("VM_buf_sort: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4023 if(stringbuffer->num_strings <= 0)
4025 VM_Warning("VM_buf_sort: tried to sort empty buffer %i in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4028 stringbuffers_sortlength = (int)PRVM_G_FLOAT(OFS_PARM1);
4029 if(stringbuffers_sortlength <= 0)
4030 stringbuffers_sortlength = 0x7FFFFFFF;
4032 if(!PRVM_G_FLOAT(OFS_PARM2))
4033 qsort(stringbuffer->strings, stringbuffer->num_strings, sizeof(char*), BufStr_SortStringsUP);
4035 qsort(stringbuffer->strings, stringbuffer->num_strings, sizeof(char*), BufStr_SortStringsDOWN);
4037 BufStr_Shrink(stringbuffer);
4041 ========================
4043 concantenates all buffer string into one with "glue" separator and returns it as tempstring
4044 string buf_implode(float bufhandle, string glue) = #465;
4045 ========================
4047 void VM_buf_implode (void)
4049 prvm_stringbuffer_t *stringbuffer;
4050 char k[VM_STRINGTEMP_LENGTH];
4054 VM_SAFEPARMCOUNT(2, VM_buf_implode);
4056 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4057 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
4060 VM_Warning("VM_buf_implode: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4063 if(!stringbuffer->num_strings)
4065 sep = PRVM_G_STRING(OFS_PARM1);
4067 for(l = i = 0;i < stringbuffer->num_strings;i++)
4069 if(stringbuffer->strings[i])
4071 l += (i > 0 ? strlen(sep) : 0) + strlen(stringbuffer->strings[i]);
4072 if (l >= sizeof(k) - 1)
4074 strlcat(k, sep, sizeof(k));
4075 strlcat(k, stringbuffer->strings[i], sizeof(k));
4078 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(k);
4082 ========================
4084 get a string from buffer, returns tempstring, dont str_unzone it!
4085 string bufstr_get(float bufhandle, float string_index) = #465;
4086 ========================
4088 void VM_bufstr_get (void)
4090 prvm_stringbuffer_t *stringbuffer;
4092 VM_SAFEPARMCOUNT(2, VM_bufstr_get);
4094 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
4095 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4098 VM_Warning("VM_bufstr_get: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4101 strindex = (int)PRVM_G_FLOAT(OFS_PARM1);
4104 VM_Warning("VM_bufstr_get: invalid string index %i used in %s\n", strindex, PRVM_NAME);
4107 if (strindex < stringbuffer->num_strings && stringbuffer->strings[strindex])
4108 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(stringbuffer->strings[strindex]);
4112 ========================
4114 copies a string into selected slot of buffer
4115 void bufstr_set(float bufhandle, float string_index, string str) = #466;
4116 ========================
4118 void VM_bufstr_set (void)
4121 prvm_stringbuffer_t *stringbuffer;
4124 VM_SAFEPARMCOUNT(3, VM_bufstr_set);
4126 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4129 VM_Warning("VM_bufstr_set: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4132 strindex = (int)PRVM_G_FLOAT(OFS_PARM1);
4133 if(strindex < 0 || strindex >= 1000000) // huge number of strings
4135 VM_Warning("VM_bufstr_set: invalid string index %i used in %s\n", strindex, PRVM_NAME);
4139 BufStr_Expand(stringbuffer, strindex);
4140 stringbuffer->num_strings = max(stringbuffer->num_strings, strindex + 1);
4142 if(stringbuffer->strings[strindex])
4143 Mem_Free(stringbuffer->strings[strindex]);
4144 stringbuffer->strings[strindex] = NULL;
4146 news = PRVM_G_STRING(OFS_PARM2);
4147 if (news && news[0])
4149 size_t alloclen = strlen(news) + 1;
4150 stringbuffer->strings[strindex] = (char *)Mem_Alloc(prog->progs_mempool, alloclen);
4151 memcpy(stringbuffer->strings[strindex], news, alloclen);
4154 BufStr_Shrink(stringbuffer);
4158 ========================
4160 adds string to buffer in first free slot and returns its index
4161 "order == TRUE" means that string will be added after last "full" slot
4162 float bufstr_add(float bufhandle, string str, float order) = #467;
4163 ========================
4165 void VM_bufstr_add (void)
4167 int order, strindex;
4168 prvm_stringbuffer_t *stringbuffer;
4172 VM_SAFEPARMCOUNT(3, VM_bufstr_add);
4174 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4175 PRVM_G_FLOAT(OFS_RETURN) = -1;
4178 VM_Warning("VM_bufstr_add: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4181 string = PRVM_G_STRING(OFS_PARM1);
4182 if(!string || !string[0])
4184 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);
4187 order = (int)PRVM_G_FLOAT(OFS_PARM2);
4189 strindex = stringbuffer->num_strings;
4191 for (strindex = 0;strindex < stringbuffer->num_strings;strindex++)
4192 if (stringbuffer->strings[strindex] == NULL)
4195 BufStr_Expand(stringbuffer, strindex);
4197 stringbuffer->num_strings = max(stringbuffer->num_strings, strindex + 1);
4198 alloclen = strlen(string) + 1;
4199 stringbuffer->strings[strindex] = (char *)Mem_Alloc(prog->progs_mempool, alloclen);
4200 memcpy(stringbuffer->strings[strindex], string, alloclen);
4202 PRVM_G_FLOAT(OFS_RETURN) = strindex;
4206 ========================
4208 delete string from buffer
4209 void bufstr_free(float bufhandle, float string_index) = #468;
4210 ========================
4212 void VM_bufstr_free (void)
4215 prvm_stringbuffer_t *stringbuffer;
4216 VM_SAFEPARMCOUNT(2, VM_bufstr_free);
4218 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4221 VM_Warning("VM_bufstr_free: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4224 i = (int)PRVM_G_FLOAT(OFS_PARM1);
4227 VM_Warning("VM_bufstr_free: invalid string index %i used in %s\n", i, PRVM_NAME);
4231 if (i < stringbuffer->num_strings)
4233 if(stringbuffer->strings[i])
4234 Mem_Free(stringbuffer->strings[i]);
4235 stringbuffer->strings[i] = NULL;
4238 BufStr_Shrink(stringbuffer);
4247 This was a major timewaster in progs, so it was converted to C
4250 void VM_changeyaw (void)
4253 float ideal, current, move, speed;
4255 // this is called (VERY HACKISHLY) by SV_MoveToGoal, so it can not use any
4256 // parameters because they are the parameters to SV_MoveToGoal, not this
4257 //VM_SAFEPARMCOUNT(0, VM_changeyaw);
4259 ent = PRVM_PROG_TO_EDICT(PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict);
4260 if (ent == prog->edicts)
4262 VM_Warning("changeyaw: can not modify world entity\n");
4265 if (ent->priv.server->free)
4267 VM_Warning("changeyaw: can not modify free entity\n");
4270 if (prog->fieldoffsets.angles < 0 || prog->fieldoffsets.ideal_yaw < 0 || prog->fieldoffsets.yaw_speed < 0)
4272 VM_Warning("changeyaw: angles, ideal_yaw, or yaw_speed field(s) not found\n");
4275 current = ANGLEMOD(PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.angles)->vector[1]);
4276 ideal = PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.ideal_yaw)->_float;
4277 speed = PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.yaw_speed)->_float;
4279 if (current == ideal)
4281 move = ideal - current;
4282 if (ideal > current)
4303 PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.angles)->vector[1] = ANGLEMOD (current + move);
4311 void VM_changepitch (void)
4314 float ideal, current, move, speed;
4316 VM_SAFEPARMCOUNT(1, VM_changepitch);
4318 ent = PRVM_G_EDICT(OFS_PARM0);
4319 if (ent == prog->edicts)
4321 VM_Warning("changepitch: can not modify world entity\n");
4324 if (ent->priv.server->free)
4326 VM_Warning("changepitch: can not modify free entity\n");
4329 if (prog->fieldoffsets.angles < 0 || prog->fieldoffsets.idealpitch < 0 || prog->fieldoffsets.pitch_speed < 0)
4331 VM_Warning("changepitch: angles, idealpitch, or pitch_speed field(s) not found\n");
4334 current = ANGLEMOD(PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.angles)->vector[0]);
4335 ideal = PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.idealpitch)->_float;
4336 speed = PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.pitch_speed)->_float;
4338 if (current == ideal)
4340 move = ideal - current;
4341 if (ideal > current)
4362 PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.angles)->vector[0] = ANGLEMOD (current + move);
4365 // TODO: adapt all static function names to use a single naming convention... [12/3/2007 Black]
4366 static int Is_Text_Color (char c, char t)
4369 char c2 = c - (c & 128);
4370 char t2 = t - (t & 128);
4372 if(c != STRING_COLOR_TAG && c2 != STRING_COLOR_TAG) return 0;
4373 if(t >= '0' && t <= '9') a = 1;
4374 if(t2 >= '0' && t2 <= '9') a = 1;
4375 /* if(t >= 'A' && t <= 'Z') a = 2;
4376 if(t2 >= 'A' && t2 <= 'Z') a = 2;
4378 if(a == 1 && scr_colortext.integer > 0)
4380 if(a == 2 && scr_multifonts.integer > 0)
4386 void VM_uncolorstring (void)
4389 char out[VM_STRINGTEMP_LENGTH];
4392 VM_SAFEPARMCOUNT(1, VM_uncolorstring);
4393 in = PRVM_G_STRING(OFS_PARM0);
4394 VM_CheckEmptyString (in);
4399 if(Is_Text_Color(in[k], in[k+1]) == 1/* || (in[k] == '&' && in[k+1] == 'r')*/)
4408 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(out);
4411 // #221 float(string str, string sub[, float startpos]) strstrofs (FTE_STRINGS)
4412 //strstr, without generating a new string. Use in conjunction with FRIK_FILE's substring for more similar strstr.
4413 void VM_strstrofs (void)
4415 const char *instr, *match;
4417 VM_SAFEPARMCOUNTRANGE(2, 3, VM_strstrofs);
4418 instr = PRVM_G_STRING(OFS_PARM0);
4419 match = PRVM_G_STRING(OFS_PARM1);
4420 firstofs = (prog->argc > 2)?PRVM_G_FLOAT(OFS_PARM2):0;
4422 if (firstofs && (firstofs < 0 || firstofs > (int)strlen(instr)))
4424 PRVM_G_FLOAT(OFS_RETURN) = -1;
4428 match = strstr(instr+firstofs, match);
4430 PRVM_G_FLOAT(OFS_RETURN) = -1;
4432 PRVM_G_FLOAT(OFS_RETURN) = match - instr;
4435 //#222 string(string s, float index) str2chr (FTE_STRINGS)
4436 void VM_str2chr (void)
4439 VM_SAFEPARMCOUNT(2, VM_str2chr);
4440 s = PRVM_G_STRING(OFS_PARM0);
4441 if((unsigned)PRVM_G_FLOAT(OFS_PARM1) < strlen(s))
4442 PRVM_G_FLOAT(OFS_RETURN) = (unsigned char)s[(unsigned)PRVM_G_FLOAT(OFS_PARM1)];
4444 PRVM_G_FLOAT(OFS_RETURN) = 0;
4447 //#223 string(float c, ...) chr2str (FTE_STRINGS)
4448 void VM_chr2str (void)
4452 VM_SAFEPARMCOUNTRANGE(0, 8, VM_chr2str);
4453 for(i = 0;i < prog->argc && i < (int)sizeof(t) - 1;i++)
4454 t[i] = (unsigned char)PRVM_G_FLOAT(OFS_PARM0+i*3);
4456 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(t);
4459 static int chrconv_number(int i, int base, int conv)
4484 static int chrconv_punct(int i, int base, int conv)
4502 static int chrchar_alpha(int i, int basec, int baset, int convc, int convt, int charnum)
4504 //convert case and colour seperatly...
4521 baset = 128*((charnum&1) == (convt-5));
4537 return i + basec + baset;
4539 // #224 string(float ccase, float calpha, float cnum, string s, ...) strconv (FTE_STRINGS)
4540 //bulk convert a string. change case or colouring.
4541 void VM_strconv (void)
4543 int ccase, redalpha, rednum, len, i;
4544 unsigned char resbuf[VM_STRINGTEMP_LENGTH];
4545 unsigned char *result = resbuf;
4547 VM_SAFEPARMCOUNTRANGE(3, 8, VM_strconv);
4549 ccase = PRVM_G_FLOAT(OFS_PARM0); //0 same, 1 lower, 2 upper
4550 redalpha = PRVM_G_FLOAT(OFS_PARM1); //0 same, 1 white, 2 red, 5 alternate, 6 alternate-alternate
4551 rednum = PRVM_G_FLOAT(OFS_PARM2); //0 same, 1 white, 2 red, 3 redspecial, 4 whitespecial, 5 alternate, 6 alternate-alternate
4552 VM_VarString(3, (char *) resbuf, sizeof(resbuf));
4553 len = strlen((char *) resbuf);
4555 for (i = 0; i < len; i++, result++) //should this be done backwards?
4557 if (*result >= '0' && *result <= '9') //normal numbers...
4558 *result = chrconv_number(*result, '0', rednum);
4559 else if (*result >= '0'+128 && *result <= '9'+128)
4560 *result = chrconv_number(*result, '0'+128, rednum);
4561 else if (*result >= '0'+128-30 && *result <= '9'+128-30)
4562 *result = chrconv_number(*result, '0'+128-30, rednum);
4563 else if (*result >= '0'-30 && *result <= '9'-30)
4564 *result = chrconv_number(*result, '0'-30, rednum);
4566 else if (*result >= 'a' && *result <= 'z') //normal numbers...
4567 *result = chrchar_alpha(*result, 'a', 0, ccase, redalpha, i);
4568 else if (*result >= 'A' && *result <= 'Z') //normal numbers...
4569 *result = chrchar_alpha(*result, 'A', 0, ccase, redalpha, i);
4570 else if (*result >= 'a'+128 && *result <= 'z'+128) //normal numbers...
4571 *result = chrchar_alpha(*result, 'a', 128, ccase, redalpha, i);
4572 else if (*result >= 'A'+128 && *result <= 'Z'+128) //normal numbers...
4573 *result = chrchar_alpha(*result, 'A', 128, ccase, redalpha, i);
4575 else if ((*result & 127) < 16 || !redalpha) //special chars..
4577 else if (*result < 128)
4578 *result = chrconv_punct(*result, 0, redalpha);
4580 *result = chrconv_punct(*result, 128, redalpha);
4584 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString((char *) resbuf);
4587 // #225 string(float chars, string s, ...) strpad (FTE_STRINGS)
4588 void VM_strpad (void)
4590 char src[VM_STRINGTEMP_LENGTH];
4591 char destbuf[VM_STRINGTEMP_LENGTH];
4593 VM_SAFEPARMCOUNTRANGE(1, 8, VM_strpad);
4594 pad = PRVM_G_FLOAT(OFS_PARM0);
4595 VM_VarString(1, src, sizeof(src));
4597 // note: < 0 = left padding, > 0 = right padding,
4598 // this is reverse logic of printf!
4599 dpsnprintf(destbuf, sizeof(destbuf), "%*s", -pad, src);
4601 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(destbuf);
4604 // #226 string(string info, string key, string value, ...) infoadd (FTE_STRINGS)
4605 //uses qw style \key\value strings
4606 void VM_infoadd (void)
4608 const char *info, *key;
4609 char value[VM_STRINGTEMP_LENGTH];
4610 char temp[VM_STRINGTEMP_LENGTH];
4612 VM_SAFEPARMCOUNTRANGE(2, 8, VM_infoadd);
4613 info = PRVM_G_STRING(OFS_PARM0);
4614 key = PRVM_G_STRING(OFS_PARM1);
4615 VM_VarString(2, value, sizeof(value));
4617 strlcpy(temp, info, VM_STRINGTEMP_LENGTH);
4619 InfoString_SetValue(temp, VM_STRINGTEMP_LENGTH, key, value);
4621 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(temp);
4624 // #227 string(string info, string key) infoget (FTE_STRINGS)
4625 //uses qw style \key\value strings
4626 void VM_infoget (void)
4630 char value[VM_STRINGTEMP_LENGTH];
4632 VM_SAFEPARMCOUNT(2, VM_infoget);
4633 info = PRVM_G_STRING(OFS_PARM0);
4634 key = PRVM_G_STRING(OFS_PARM1);
4636 InfoString_GetValue(info, key, value, VM_STRINGTEMP_LENGTH);
4638 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(value);
4641 //#228 float(string s1, string s2, float len) strncmp (FTE_STRINGS)
4642 // also float(string s1, string s2) strcmp (FRIK_FILE)
4643 void VM_strncmp (void)
4645 const char *s1, *s2;
4646 VM_SAFEPARMCOUNTRANGE(2, 3, VM_strncmp);
4647 s1 = PRVM_G_STRING(OFS_PARM0);
4648 s2 = PRVM_G_STRING(OFS_PARM1);
4651 PRVM_G_FLOAT(OFS_RETURN) = strncmp(s1, s2, (size_t)PRVM_G_FLOAT(OFS_PARM2));
4655 PRVM_G_FLOAT(OFS_RETURN) = strcmp(s1, s2);
4659 // #229 float(string s1, string s2) strcasecmp (FTE_STRINGS)
4660 // #230 float(string s1, string s2, float len) strncasecmp (FTE_STRINGS)
4661 void VM_strncasecmp (void)
4663 const char *s1, *s2;
4664 VM_SAFEPARMCOUNTRANGE(2, 3, VM_strncasecmp);
4665 s1 = PRVM_G_STRING(OFS_PARM0);
4666 s2 = PRVM_G_STRING(OFS_PARM1);
4669 PRVM_G_FLOAT(OFS_RETURN) = strncasecmp(s1, s2, (size_t)PRVM_G_FLOAT(OFS_PARM2));
4673 PRVM_G_FLOAT(OFS_RETURN) = strcasecmp(s1, s2);
4677 // #494 float(float caseinsensitive, string s, ...) crc16
4681 static char s[VM_STRINGTEMP_LENGTH];
4682 VM_SAFEPARMCOUNTRANGE(2, 8, VM_hash);
4683 insensitive = PRVM_G_FLOAT(OFS_PARM0);
4684 VM_VarString(1, s, sizeof(s));
4685 PRVM_G_FLOAT(OFS_RETURN) = (unsigned short) ((insensitive ? CRC_Block_CaseInsensitive : CRC_Block) ((unsigned char *) s, strlen(s)));
4688 void VM_wasfreed (void)
4690 VM_SAFEPARMCOUNT(1, VM_wasfreed);
4691 PRVM_G_FLOAT(OFS_RETURN) = PRVM_G_EDICT(OFS_PARM0)->priv.required->free;
4694 void VM_SetTraceGlobals(const trace_t *trace)
4697 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_allsolid)))
4698 val->_float = trace->allsolid;
4699 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_startsolid)))
4700 val->_float = trace->startsolid;
4701 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_fraction)))
4702 val->_float = trace->fraction;
4703 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_inwater)))
4704 val->_float = trace->inwater;
4705 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_inopen)))
4706 val->_float = trace->inopen;
4707 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_endpos)))
4708 VectorCopy(trace->endpos, val->vector);
4709 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_plane_normal)))
4710 VectorCopy(trace->plane.normal, val->vector);
4711 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_plane_dist)))
4712 val->_float = trace->plane.dist;
4713 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_ent)))
4714 val->edict = PRVM_EDICT_TO_PROG(trace->ent ? trace->ent : prog->edicts);
4715 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dpstartcontents)))
4716 val->_float = trace->startsupercontents;
4717 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dphitcontents)))
4718 val->_float = trace->hitsupercontents;
4719 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dphitq3surfaceflags)))
4720 val->_float = trace->hitq3surfaceflags;
4721 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dphittexturename)))
4722 val->string = trace->hittexture ? PRVM_SetTempString(trace->hittexture->name) : 0;
4727 void VM_Cmd_Init(void)
4729 // only init the stuff for the current prog
4733 // VM_BufStr_Init();
4736 void VM_Cmd_Reset(void)
4738 CL_PurgeOwner( MENUOWNER );
4740 VM_Files_CloseAll();
4742 // VM_BufStr_ShutDown();
4745 // #510 string(string input, ...) uri_escape (DP_QC_URI_ESCAPE)
4746 // does URI escaping on a string (replace evil stuff by %AB escapes)
4747 void VM_uri_escape (void)
4749 char src[VM_STRINGTEMP_LENGTH];
4750 char dest[VM_STRINGTEMP_LENGTH];
4752 static const char *hex = "0123456789ABCDEF";
4754 VM_SAFEPARMCOUNTRANGE(1, 8, VM_uri_escape);
4755 VM_VarString(0, src, sizeof(src));
4757 for(p = src, q = dest; *p && q < dest + sizeof(dest) - 3; ++p)
4759 if((*p >= 'A' && *p <= 'Z')
4760 || (*p >= 'a' && *p <= 'z')
4761 || (*p >= '0' && *p <= '9')
4762 || (*p == '-') || (*p == '_') || (*p == '.')
4763 || (*p == '!') || (*p == '~') || (*p == '*')
4764 || (*p == '\'') || (*p == '(') || (*p == ')'))
4769 *q++ = hex[(*(unsigned char *)p >> 4) & 0xF];
4770 *q++ = hex[ *(unsigned char *)p & 0xF];
4775 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(dest);
4778 // #510 string(string input, ...) uri_unescape (DP_QC_URI_ESCAPE)
4779 // does URI unescaping on a string (get back the evil stuff)
4780 void VM_uri_unescape (void)
4782 char src[VM_STRINGTEMP_LENGTH];
4783 char dest[VM_STRINGTEMP_LENGTH];
4787 VM_SAFEPARMCOUNTRANGE(1, 8, VM_uri_unescape);
4788 VM_VarString(0, src, sizeof(src));
4790 for(p = src, q = dest; *p; ) // no need to check size, because unescape can't expand
4794 if(p[1] >= '0' && p[1] <= '9')
4796 else if(p[1] >= 'a' && p[1] <= 'f')
4797 hi = p[1] - 'a' + 10;
4798 else if(p[1] >= 'A' && p[1] <= 'F')
4799 hi = p[1] - 'A' + 10;
4802 if(p[2] >= '0' && p[2] <= '9')
4804 else if(p[2] >= 'a' && p[2] <= 'f')
4805 lo = p[2] - 'a' + 10;
4806 else if(p[2] >= 'A' && p[2] <= 'F')
4807 lo = p[2] - 'A' + 10;
4810 if(hi != 0 || lo != 0) // don't unescape NUL bytes
4811 *q++ = (char) (hi * 0x10 + lo);
4822 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(dest);
4825 // #502 string(string filename) whichpack (DP_QC_WHICHPACK)
4826 // returns the name of the pack containing a file, or "" if it is not in any pack (but local or non-existant)
4827 void VM_whichpack (void)
4829 const char *fn, *pack;
4831 fn = PRVM_G_STRING(OFS_PARM0);
4832 pack = FS_WhichPack(fn);
4834 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(pack ? pack : "");