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 sprintf(s, "%i", (int)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 sprintf (s, "'%5.1f %5.1f %5.1f'", PRVM_G_VECTOR(OFS_PARM0)[0], PRVM_G_VECTOR(OFS_PARM0)[1], PRVM_G_VECTOR(OFS_PARM0)[2]);
603 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(s);
618 VM_SAFEPARMCOUNT(1, VM_etos);
620 sprintf (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)
695 char fmt[VM_STRINGTEMP_LENGTH];
696 char result[VM_STRINGTEMP_LENGTH];
697 VM_SAFEPARMCOUNTRANGE(2, 8, VM_strftime);
698 VM_VarString(1, fmt, sizeof(fmt));
700 if (PRVM_G_FLOAT(OFS_PARM0))
706 PRVM_G_FLOAT(OFS_RETURN) = 0;
709 strftime(result, sizeof(result), fmt, tm);
710 PRVM_G_FLOAT(OFS_RETURN) = PRVM_SetTempString(result);
724 VM_SAFEPARMCOUNT(0, VM_spawn);
725 prog->xfunction->builtinsprofile += 20;
726 ed = PRVM_ED_Alloc();
738 void VM_remove (void)
741 prog->xfunction->builtinsprofile += 20;
743 VM_SAFEPARMCOUNT(1, VM_remove);
745 ed = PRVM_G_EDICT(OFS_PARM0);
746 if( PRVM_NUM_FOR_EDICT(ed) <= prog->reserved_edicts )
748 if (developer.integer >= 1)
749 VM_Warning( "VM_remove: tried to remove the null entity or a reserved entity!\n" );
751 else if( ed->priv.required->free )
753 if (developer.integer >= 1)
754 VM_Warning( "VM_remove: tried to remove an already freed entity!\n" );
764 entity find(entity start, .string field, string match)
775 VM_SAFEPARMCOUNT(3,VM_find);
777 e = PRVM_G_EDICTNUM(OFS_PARM0);
778 f = PRVM_G_INT(OFS_PARM1);
779 s = PRVM_G_STRING(OFS_PARM2);
781 // LordHavoc: apparently BloodMage does a find(world, weaponmodel, "") and
782 // expects it to find all the monsters, so we must be careful to support
785 for (e++ ; e < prog->num_edicts ; e++)
787 prog->xfunction->builtinsprofile++;
788 ed = PRVM_EDICT_NUM(e);
789 if (ed->priv.required->free)
791 t = PRVM_E_STRING(ed,f);
801 VM_RETURN_EDICT(prog->edicts);
808 entity findfloat(entity start, .float field, float match)
809 entity findentity(entity start, .entity field, entity match)
812 // LordHavoc: added this for searching float, int, and entity reference fields
813 void VM_findfloat (void)
820 VM_SAFEPARMCOUNT(3,VM_findfloat);
822 e = PRVM_G_EDICTNUM(OFS_PARM0);
823 f = PRVM_G_INT(OFS_PARM1);
824 s = PRVM_G_FLOAT(OFS_PARM2);
826 for (e++ ; e < prog->num_edicts ; e++)
828 prog->xfunction->builtinsprofile++;
829 ed = PRVM_EDICT_NUM(e);
830 if (ed->priv.required->free)
832 if (PRVM_E_FLOAT(ed,f) == s)
839 VM_RETURN_EDICT(prog->edicts);
846 entity findchain(.string field, string match)
849 // chained search for strings in entity fields
850 // entity(.string field, string match) findchain = #402;
851 void VM_findchain (void)
856 prvm_edict_t *ent, *chain;
858 VM_SAFEPARMCOUNT(2,VM_findchain);
860 if (prog->fieldoffsets.chain < 0)
861 PRVM_ERROR("VM_findchain: %s doesnt have a chain field !", PRVM_NAME);
863 chain = prog->edicts;
865 f = PRVM_G_INT(OFS_PARM0);
866 s = PRVM_G_STRING(OFS_PARM1);
868 // LordHavoc: apparently BloodMage does a find(world, weaponmodel, "") and
869 // expects it to find all the monsters, so we must be careful to support
872 ent = PRVM_NEXT_EDICT(prog->edicts);
873 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
875 prog->xfunction->builtinsprofile++;
876 if (ent->priv.required->free)
878 t = PRVM_E_STRING(ent,f);
884 PRVM_EDICTFIELDVALUE(ent,prog->fieldoffsets.chain)->edict = PRVM_NUM_FOR_EDICT(chain);
888 VM_RETURN_EDICT(chain);
895 entity findchainfloat(.string field, float match)
896 entity findchainentity(.string field, entity match)
899 // LordHavoc: chained search for float, int, and entity reference fields
900 // entity(.string field, float match) findchainfloat = #403;
901 void VM_findchainfloat (void)
906 prvm_edict_t *ent, *chain;
908 VM_SAFEPARMCOUNT(2, VM_findchainfloat);
910 if (prog->fieldoffsets.chain < 0)
911 PRVM_ERROR("VM_findchainfloat: %s doesnt have a chain field !", PRVM_NAME);
913 chain = (prvm_edict_t *)prog->edicts;
915 f = PRVM_G_INT(OFS_PARM0);
916 s = PRVM_G_FLOAT(OFS_PARM1);
918 ent = PRVM_NEXT_EDICT(prog->edicts);
919 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
921 prog->xfunction->builtinsprofile++;
922 if (ent->priv.required->free)
924 if (PRVM_E_FLOAT(ent,f) != s)
927 PRVM_EDICTFIELDVALUE(ent,prog->fieldoffsets.chain)->edict = PRVM_EDICT_TO_PROG(chain);
931 VM_RETURN_EDICT(chain);
935 ========================
938 entity findflags(entity start, .float field, float match)
939 ========================
941 // LordHavoc: search for flags in float fields
942 void VM_findflags (void)
949 VM_SAFEPARMCOUNT(3, VM_findflags);
952 e = PRVM_G_EDICTNUM(OFS_PARM0);
953 f = PRVM_G_INT(OFS_PARM1);
954 s = (int)PRVM_G_FLOAT(OFS_PARM2);
956 for (e++ ; e < prog->num_edicts ; e++)
958 prog->xfunction->builtinsprofile++;
959 ed = PRVM_EDICT_NUM(e);
960 if (ed->priv.required->free)
962 if (!PRVM_E_FLOAT(ed,f))
964 if ((int)PRVM_E_FLOAT(ed,f) & s)
971 VM_RETURN_EDICT(prog->edicts);
975 ========================
978 entity findchainflags(.float field, float match)
979 ========================
981 // LordHavoc: chained search for flags in float fields
982 void VM_findchainflags (void)
987 prvm_edict_t *ent, *chain;
989 VM_SAFEPARMCOUNT(2, VM_findchainflags);
991 if (prog->fieldoffsets.chain < 0)
992 PRVM_ERROR("VM_findchainflags: %s doesnt have a chain field !", PRVM_NAME);
994 chain = (prvm_edict_t *)prog->edicts;
996 f = PRVM_G_INT(OFS_PARM0);
997 s = (int)PRVM_G_FLOAT(OFS_PARM1);
999 ent = PRVM_NEXT_EDICT(prog->edicts);
1000 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
1002 prog->xfunction->builtinsprofile++;
1003 if (ent->priv.required->free)
1005 if (!PRVM_E_FLOAT(ent,f))
1007 if (!((int)PRVM_E_FLOAT(ent,f) & s))
1010 PRVM_EDICTFIELDVALUE(ent,prog->fieldoffsets.chain)->edict = PRVM_EDICT_TO_PROG(chain);
1014 VM_RETURN_EDICT(chain);
1021 string precache_sound (string sample)
1024 void VM_precache_sound (void)
1028 VM_SAFEPARMCOUNT(1, VM_precache_sound);
1030 s = PRVM_G_STRING(OFS_PARM0);
1031 PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
1032 VM_CheckEmptyString(s);
1034 if(snd_initialized.integer && !S_PrecacheSound(s, true, false))
1036 VM_Warning("VM_precache_sound: Failed to load %s for %s\n", s, PRVM_NAME);
1045 returns the same string as output
1047 does nothing, only used by qcc to build .pak archives
1050 void VM_precache_file (void)
1052 VM_SAFEPARMCOUNT(1,VM_precache_file);
1053 // precache_file is only used to copy files with qcc, it does nothing
1054 PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
1064 void VM_coredump (void)
1066 VM_SAFEPARMCOUNT(0,VM_coredump);
1068 Cbuf_AddText("prvm_edicts ");
1069 Cbuf_AddText(PRVM_NAME);
1080 void PRVM_StackTrace(void);
1081 void VM_stackdump (void)
1083 VM_SAFEPARMCOUNT(0, VM_stackdump);
1098 VM_SAFEPARMCOUNT(0, VM_crash);
1100 PRVM_ERROR("Crash called by %s",PRVM_NAME);
1110 void VM_traceon (void)
1112 VM_SAFEPARMCOUNT(0,VM_traceon);
1124 void VM_traceoff (void)
1126 VM_SAFEPARMCOUNT(0,VM_traceoff);
1128 prog->trace = false;
1138 void VM_eprint (void)
1140 VM_SAFEPARMCOUNT(1,VM_eprint);
1142 PRVM_ED_PrintNum (PRVM_G_EDICTNUM(OFS_PARM0), NULL);
1155 VM_SAFEPARMCOUNT(1,VM_rint);
1157 f = PRVM_G_FLOAT(OFS_PARM0);
1159 PRVM_G_FLOAT(OFS_RETURN) = floor(f + 0.5);
1161 PRVM_G_FLOAT(OFS_RETURN) = ceil(f - 0.5);
1171 void VM_floor (void)
1173 VM_SAFEPARMCOUNT(1,VM_floor);
1175 PRVM_G_FLOAT(OFS_RETURN) = floor(PRVM_G_FLOAT(OFS_PARM0));
1187 VM_SAFEPARMCOUNT(1,VM_ceil);
1189 PRVM_G_FLOAT(OFS_RETURN) = ceil(PRVM_G_FLOAT(OFS_PARM0));
1197 entity nextent(entity)
1200 void VM_nextent (void)
1205 VM_SAFEPARMCOUNT(1, VM_nextent);
1207 i = PRVM_G_EDICTNUM(OFS_PARM0);
1210 prog->xfunction->builtinsprofile++;
1212 if (i == prog->num_edicts)
1214 VM_RETURN_EDICT(prog->edicts);
1217 ent = PRVM_EDICT_NUM(i);
1218 if (!ent->priv.required->free)
1220 VM_RETURN_EDICT(ent);
1226 //=============================================================================
1233 changelevel(string map)
1236 void VM_changelevel (void)
1238 VM_SAFEPARMCOUNT(1, VM_changelevel);
1242 VM_Warning("VM_changelevel: game is not server (%s)\n", PRVM_NAME);
1246 // make sure we don't issue two changelevels
1247 if (svs.changelevel_issued)
1249 svs.changelevel_issued = true;
1251 Cbuf_AddText (va("changelevel %s\n",PRVM_G_STRING(OFS_PARM0)));
1263 VM_SAFEPARMCOUNT(1,VM_sin);
1264 PRVM_G_FLOAT(OFS_RETURN) = sin(PRVM_G_FLOAT(OFS_PARM0));
1275 VM_SAFEPARMCOUNT(1,VM_cos);
1276 PRVM_G_FLOAT(OFS_RETURN) = cos(PRVM_G_FLOAT(OFS_PARM0));
1288 VM_SAFEPARMCOUNT(1,VM_sqrt);
1289 PRVM_G_FLOAT(OFS_RETURN) = sqrt(PRVM_G_FLOAT(OFS_PARM0));
1301 VM_SAFEPARMCOUNT(1,VM_asin);
1302 PRVM_G_FLOAT(OFS_RETURN) = asin(PRVM_G_FLOAT(OFS_PARM0));
1313 VM_SAFEPARMCOUNT(1,VM_acos);
1314 PRVM_G_FLOAT(OFS_RETURN) = acos(PRVM_G_FLOAT(OFS_PARM0));
1325 VM_SAFEPARMCOUNT(1,VM_atan);
1326 PRVM_G_FLOAT(OFS_RETURN) = atan(PRVM_G_FLOAT(OFS_PARM0));
1332 float atan2(float,float)
1335 void VM_atan2 (void)
1337 VM_SAFEPARMCOUNT(2,VM_atan2);
1338 PRVM_G_FLOAT(OFS_RETURN) = atan2(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1349 VM_SAFEPARMCOUNT(1,VM_tan);
1350 PRVM_G_FLOAT(OFS_RETURN) = tan(PRVM_G_FLOAT(OFS_PARM0));
1357 Returns a vector of length < 1 and > 0
1362 void VM_randomvec (void)
1367 VM_SAFEPARMCOUNT(0, VM_randomvec);
1372 temp[0] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1373 temp[1] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1374 temp[2] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1376 while (DotProduct(temp, temp) >= 1);
1377 VectorCopy (temp, PRVM_G_VECTOR(OFS_RETURN));
1380 temp[0] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1381 temp[1] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1382 temp[2] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1383 // length returned always > 0
1384 length = (rand()&32766 + 1) * (1.0 / 32767.0) / VectorLength(temp);
1385 VectorScale(temp,length, temp);*/
1386 //VectorCopy(temp, PRVM_G_VECTOR(OFS_RETURN));
1389 //=============================================================================
1395 float registercvar (string name, string value[, float flags])
1398 void VM_registercvar (void)
1400 const char *name, *value;
1403 VM_SAFEPARMCOUNTRANGE(2, 3, VM_registercvar);
1405 name = PRVM_G_STRING(OFS_PARM0);
1406 value = PRVM_G_STRING(OFS_PARM1);
1407 flags = prog->argc >= 3 ? (int)PRVM_G_FLOAT(OFS_PARM2) : 0;
1408 PRVM_G_FLOAT(OFS_RETURN) = 0;
1410 if(flags > CVAR_MAXFLAGSVAL)
1413 // first check to see if it has already been defined
1414 if (Cvar_FindVar (name))
1417 // check for overlap with a command
1418 if (Cmd_Exists (name))
1420 VM_Warning("VM_registercvar: %s is a command\n", name);
1424 Cvar_Get(name, value, flags);
1426 PRVM_G_FLOAT(OFS_RETURN) = 1; // success
1434 returns the minimum of two supplied floats
1436 float min(float a, float b, ...[float])
1441 VM_SAFEPARMCOUNTRANGE(2, 8, VM_min);
1442 // LordHavoc: 3+ argument enhancement suggested by FrikaC
1443 if (prog->argc >= 3)
1446 float f = PRVM_G_FLOAT(OFS_PARM0);
1447 for (i = 1;i < prog->argc;i++)
1448 if (f > PRVM_G_FLOAT((OFS_PARM0+i*3)))
1449 f = PRVM_G_FLOAT((OFS_PARM0+i*3));
1450 PRVM_G_FLOAT(OFS_RETURN) = f;
1453 PRVM_G_FLOAT(OFS_RETURN) = min(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1460 returns the maximum of two supplied floats
1462 float max(float a, float b, ...[float])
1467 VM_SAFEPARMCOUNTRANGE(2, 8, VM_max);
1468 // LordHavoc: 3+ argument enhancement suggested by FrikaC
1469 if (prog->argc >= 3)
1472 float f = PRVM_G_FLOAT(OFS_PARM0);
1473 for (i = 1;i < prog->argc;i++)
1474 if (f < PRVM_G_FLOAT((OFS_PARM0+i*3)))
1475 f = PRVM_G_FLOAT((OFS_PARM0+i*3));
1476 PRVM_G_FLOAT(OFS_RETURN) = f;
1479 PRVM_G_FLOAT(OFS_RETURN) = max(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1486 returns number bounded by supplied range
1488 float bound(float min, float value, float max)
1491 void VM_bound (void)
1493 VM_SAFEPARMCOUNT(3,VM_bound);
1494 PRVM_G_FLOAT(OFS_RETURN) = bound(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1), PRVM_G_FLOAT(OFS_PARM2));
1501 returns a raised to power b
1503 float pow(float a, float b)
1508 VM_SAFEPARMCOUNT(2,VM_pow);
1509 PRVM_G_FLOAT(OFS_RETURN) = pow(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1512 void VM_Files_Init(void)
1515 for (i = 0;i < PRVM_MAX_OPENFILES;i++)
1516 prog->openfiles[i] = NULL;
1519 void VM_Files_CloseAll(void)
1522 for (i = 0;i < PRVM_MAX_OPENFILES;i++)
1524 if (prog->openfiles[i])
1525 FS_Close(prog->openfiles[i]);
1526 prog->openfiles[i] = NULL;
1530 static qfile_t *VM_GetFileHandle( int index )
1532 if (index < 0 || index >= PRVM_MAX_OPENFILES)
1534 Con_Printf("VM_GetFileHandle: invalid file handle %i used in %s\n", index, PRVM_NAME);
1537 if (prog->openfiles[index] == NULL)
1539 Con_Printf("VM_GetFileHandle: no such file handle %i (or file has been closed) in %s\n", index, PRVM_NAME);
1542 return prog->openfiles[index];
1549 float fopen(string filename, float mode)
1552 // float(string filename, float mode) fopen = #110;
1553 // opens a file inside quake/gamedir/data/ (mode is FILE_READ, FILE_APPEND, or FILE_WRITE),
1554 // returns fhandle >= 0 if successful, or fhandle < 0 if unable to open file for any reason
1558 const char *modestring, *filename;
1560 VM_SAFEPARMCOUNT(2,VM_fopen);
1562 for (filenum = 0;filenum < PRVM_MAX_OPENFILES;filenum++)
1563 if (prog->openfiles[filenum] == NULL)
1565 if (filenum >= PRVM_MAX_OPENFILES)
1567 PRVM_G_FLOAT(OFS_RETURN) = -2;
1568 VM_Warning("VM_fopen: %s ran out of file handles (%i)\n", PRVM_NAME, PRVM_MAX_OPENFILES);
1571 mode = (int)PRVM_G_FLOAT(OFS_PARM1);
1574 case 0: // FILE_READ
1577 case 1: // FILE_APPEND
1580 case 2: // FILE_WRITE
1584 PRVM_G_FLOAT(OFS_RETURN) = -3;
1585 VM_Warning("VM_fopen: %s: no such mode %i (valid: 0 = read, 1 = append, 2 = write)\n", PRVM_NAME, mode);
1588 filename = PRVM_G_STRING(OFS_PARM0);
1590 prog->openfiles[filenum] = FS_Open(va("data/%s", filename), modestring, false, false);
1591 if (prog->openfiles[filenum] == NULL && mode == 0)
1592 prog->openfiles[filenum] = FS_Open(va("%s", filename), modestring, false, false);
1594 if (prog->openfiles[filenum] == NULL)
1596 PRVM_G_FLOAT(OFS_RETURN) = -1;
1597 if (developer.integer >= 100)
1598 VM_Warning("VM_fopen: %s: %s mode %s failed\n", PRVM_NAME, filename, modestring);
1602 PRVM_G_FLOAT(OFS_RETURN) = filenum;
1603 if (developer.integer >= 100)
1604 Con_Printf("VM_fopen: %s: %s mode %s opened as #%i\n", PRVM_NAME, filename, modestring, filenum);
1612 fclose(float fhandle)
1615 //void(float fhandle) fclose = #111; // closes a file
1616 void VM_fclose(void)
1620 VM_SAFEPARMCOUNT(1,VM_fclose);
1622 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1623 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
1625 VM_Warning("VM_fclose: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1628 if (prog->openfiles[filenum] == NULL)
1630 VM_Warning("VM_fclose: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1633 FS_Close(prog->openfiles[filenum]);
1634 prog->openfiles[filenum] = NULL;
1635 if (developer.integer >= 100)
1636 Con_Printf("VM_fclose: %s: #%i closed\n", PRVM_NAME, filenum);
1643 string fgets(float fhandle)
1646 //string(float fhandle) fgets = #112; // reads a line of text from the file and returns as a tempstring
1650 char string[VM_STRINGTEMP_LENGTH];
1653 VM_SAFEPARMCOUNT(1,VM_fgets);
1655 // set the return value regardless of any possible errors
1656 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
1658 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1659 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
1661 VM_Warning("VM_fgets: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1664 if (prog->openfiles[filenum] == NULL)
1666 VM_Warning("VM_fgets: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1672 c = FS_Getc(prog->openfiles[filenum]);
1673 if (c == '\r' || c == '\n' || c < 0)
1675 if (end < VM_STRINGTEMP_LENGTH - 1)
1679 // remove \n following \r
1682 c = FS_Getc(prog->openfiles[filenum]);
1684 FS_UnGetc(prog->openfiles[filenum], (unsigned char)c);
1686 if (developer.integer >= 100)
1687 Con_Printf("fgets: %s: %s\n", PRVM_NAME, string);
1689 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string);
1696 fputs(float fhandle, string s)
1699 //void(float fhandle, string s) fputs = #113; // writes a line of text to the end of the file
1703 char string[VM_STRINGTEMP_LENGTH];
1706 VM_SAFEPARMCOUNT(2,VM_fputs);
1708 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1709 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
1711 VM_Warning("VM_fputs: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1714 if (prog->openfiles[filenum] == NULL)
1716 VM_Warning("VM_fputs: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1719 VM_VarString(1, string, sizeof(string));
1720 if ((stringlength = (int)strlen(string)))
1721 FS_Write(prog->openfiles[filenum], string, stringlength);
1722 if (developer.integer >= 100)
1723 Con_Printf("fputs: %s: %s\n", PRVM_NAME, string);
1730 writetofile(float fhandle, entity ent)
1733 void VM_writetofile(void)
1738 VM_SAFEPARMCOUNT(2, VM_writetofile);
1740 file = VM_GetFileHandle( (int)PRVM_G_FLOAT(OFS_PARM0) );
1743 VM_Warning("VM_writetofile: invalid or closed file handle\n");
1747 ent = PRVM_G_EDICT(OFS_PARM1);
1748 if(ent->priv.required->free)
1750 VM_Warning("VM_writetofile: %s: entity %i is free !\n", PRVM_NAME, PRVM_NUM_FOR_EDICT(ent));
1754 PRVM_ED_Write (file, ent);
1761 float strlen(string s)
1764 //float(string s) strlen = #114; // returns how many characters are in a string
1765 void VM_strlen(void)
1767 VM_SAFEPARMCOUNT(1,VM_strlen);
1769 PRVM_G_FLOAT(OFS_RETURN) = strlen(PRVM_G_STRING(OFS_PARM0));
1772 // DRESK - Decolorized String
1777 string strdecolorize(string s)
1780 // string (string s) strdecolorize = #472; // returns the passed in string with color codes stripped
1781 void VM_strdecolorize(void)
1783 char szNewString[VM_STRINGTEMP_LENGTH];
1784 const char *szString;
1787 VM_SAFEPARMCOUNT(1,VM_strdecolorize);
1788 szString = PRVM_G_STRING(OFS_PARM0);
1790 COM_StringDecolorize(szString, 0, szNewString, sizeof(szNewString), TRUE);
1792 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(szNewString);
1795 // DRESK - String Length (not counting color codes)
1800 float strlennocol(string s)
1803 // float(string s) strlennocol = #471; // returns how many characters are in a string not including color codes
1804 // For example, ^2Dresk returns a length of 5
1805 void VM_strlennocol(void)
1807 const char *szString;
1810 VM_SAFEPARMCOUNT(1,VM_strlennocol);
1812 szString = PRVM_G_STRING(OFS_PARM0);
1814 nCnt = COM_StringLengthNoColors(szString, 0, NULL);
1816 PRVM_G_FLOAT(OFS_RETURN) = nCnt;
1819 // DRESK - String to Uppercase and Lowercase
1824 string strtolower(string s)
1827 // string (string s) strtolower = #480; // returns passed in string in lowercase form
1828 void VM_strtolower(void)
1830 char szNewString[VM_STRINGTEMP_LENGTH];
1831 const char *szString;
1834 VM_SAFEPARMCOUNT(1,VM_strtolower);
1835 szString = PRVM_G_STRING(OFS_PARM0);
1837 COM_ToLowerString(szString, szNewString, sizeof(szNewString) );
1839 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(szNewString);
1846 string strtoupper(string s)
1849 // string (string s) strtoupper = #481; // returns passed in string in uppercase form
1850 void VM_strtoupper(void)
1852 char szNewString[VM_STRINGTEMP_LENGTH];
1853 const char *szString;
1856 VM_SAFEPARMCOUNT(1,VM_strtoupper);
1857 szString = PRVM_G_STRING(OFS_PARM0);
1859 COM_ToUpperString(szString, szNewString, sizeof(szNewString) );
1861 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(szNewString);
1868 string strcat(string,string,...[string])
1871 //string(string s1, string s2) strcat = #115;
1872 // concatenates two strings (for example "abc", "def" would return "abcdef")
1873 // and returns as a tempstring
1874 void VM_strcat(void)
1876 char s[VM_STRINGTEMP_LENGTH];
1877 VM_SAFEPARMCOUNTRANGE(1, 8, VM_strcat);
1879 VM_VarString(0, s, sizeof(s));
1880 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(s);
1887 string substring(string s, float start, float length)
1890 // string(string s, float start, float length) substring = #116;
1891 // returns a section of a string as a tempstring
1892 void VM_substring(void)
1894 int i, start, length;
1896 char string[VM_STRINGTEMP_LENGTH];
1898 VM_SAFEPARMCOUNT(3,VM_substring);
1900 s = PRVM_G_STRING(OFS_PARM0);
1901 start = (int)PRVM_G_FLOAT(OFS_PARM1);
1902 length = (int)PRVM_G_FLOAT(OFS_PARM2);
1903 for (i = 0;i < start && *s;i++, s++);
1904 for (i = 0;i < (int)sizeof(string) - 1 && *s && i < length;i++, s++)
1907 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string);
1914 string(string search, string replace, string subject) strreplace = #484;
1917 // replaces all occurrences of search with replace in the string subject, and returns the result
1918 void VM_strreplace(void)
1921 const char *search, *replace, *subject;
1922 char string[VM_STRINGTEMP_LENGTH];
1923 int search_len, replace_len, subject_len;
1925 VM_SAFEPARMCOUNT(3,VM_strreplace);
1927 search = PRVM_G_STRING(OFS_PARM0);
1928 replace = PRVM_G_STRING(OFS_PARM1);
1929 subject = PRVM_G_STRING(OFS_PARM2);
1931 search_len = (int)strlen(search);
1932 replace_len = (int)strlen(replace);
1933 subject_len = (int)strlen(subject);
1936 for (i = 0; i < subject_len; i++)
1938 for (j = 0; j < search_len && i+j < subject_len; j++)
1939 if (subject[i+j] != search[j])
1941 if (j == search_len || i+j == subject_len)
1943 // found it at offset 'i'
1944 for (j = 0; j < replace_len && si < (int)sizeof(string) - 1; j++)
1945 string[si++] = replace[j];
1946 i += search_len - 1;
1951 if (si < (int)sizeof(string) - 1)
1952 string[si++] = subject[i];
1957 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string);
1964 string(string search, string replace, string subject) strireplace = #485;
1967 // case-insensitive version of strreplace
1968 void VM_strireplace(void)
1971 const char *search, *replace, *subject;
1972 char string[VM_STRINGTEMP_LENGTH];
1973 int search_len, replace_len, subject_len;
1975 VM_SAFEPARMCOUNT(3,VM_strreplace);
1977 search = PRVM_G_STRING(OFS_PARM0);
1978 replace = PRVM_G_STRING(OFS_PARM1);
1979 subject = PRVM_G_STRING(OFS_PARM2);
1981 search_len = (int)strlen(search);
1982 replace_len = (int)strlen(replace);
1983 subject_len = (int)strlen(subject);
1986 for (i = 0; i < subject_len; i++)
1988 for (j = 0; j < search_len && i+j < subject_len; j++)
1989 if (tolower(subject[i+j]) != tolower(search[j]))
1991 if (j == search_len || i+j == subject_len)
1993 // found it at offset 'i'
1994 for (j = 0; j < replace_len && si < (int)sizeof(string) - 1; j++)
1995 string[si++] = replace[j];
1996 i += search_len - 1;
2001 if (si < (int)sizeof(string) - 1)
2002 string[si++] = subject[i];
2007 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string);
2014 vector stov(string s)
2017 //vector(string s) stov = #117; // returns vector value from a string
2020 char string[VM_STRINGTEMP_LENGTH];
2022 VM_SAFEPARMCOUNT(1,VM_stov);
2024 VM_VarString(0, string, sizeof(string));
2025 Math_atov(string, PRVM_G_VECTOR(OFS_RETURN));
2032 string strzone(string s)
2035 //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)
2036 void VM_strzone(void)
2039 char string[VM_STRINGTEMP_LENGTH];
2042 VM_SAFEPARMCOUNT(1,VM_strzone);
2044 VM_VarString(0, string, sizeof(string));
2045 alloclen = strlen(string) + 1;
2046 PRVM_G_INT(OFS_RETURN) = PRVM_AllocString(alloclen, &out);
2047 memcpy(out, string, alloclen);
2057 //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!!!)
2058 void VM_strunzone(void)
2060 VM_SAFEPARMCOUNT(1,VM_strunzone);
2061 PRVM_FreeString(PRVM_G_INT(OFS_PARM0));
2066 VM_command (used by client and menu)
2068 clientcommand(float client, string s) (for client and menu)
2071 //void(entity e, string s) clientcommand = #440; // executes a command string as if it came from the specified client
2072 //this function originally written by KrimZon, made shorter by LordHavoc
2073 void VM_clcommand (void)
2075 client_t *temp_client;
2078 VM_SAFEPARMCOUNT(2,VM_clcommand);
2080 i = (int)PRVM_G_FLOAT(OFS_PARM0);
2081 if (!sv.active || i < 0 || i >= svs.maxclients || !svs.clients[i].active)
2083 VM_Warning("VM_clientcommand: %s: invalid client/server is not active !\n", PRVM_NAME);
2087 temp_client = host_client;
2088 host_client = svs.clients + i;
2089 Cmd_ExecuteString (PRVM_G_STRING(OFS_PARM1), src_client);
2090 host_client = temp_client;
2098 float tokenize(string s)
2101 //float(string s) tokenize = #441; // takes apart a string into individal words (access them with argv), returns how many
2102 //this function originally written by KrimZon, made shorter by LordHavoc
2103 //20040203: rewritten by LordHavoc (no longer uses allocations)
2106 void VM_tokenize (void)
2109 static char string[VM_STRINGTEMP_LENGTH]; // static, because it's big
2111 VM_SAFEPARMCOUNT(1,VM_tokenize);
2113 strlcpy(string, PRVM_G_STRING(OFS_PARM0), sizeof(string));
2117 while(COM_ParseToken_VM_Tokenize(&p, false))
2119 if (num_tokens >= (int)(sizeof(tokens)/sizeof(tokens[0])))
2121 tokens[num_tokens++] = PRVM_SetTempString(com_token);
2124 PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
2129 VM_tokenizebyseparator
2131 float tokenizebyseparator(string s, string separator1, ...)
2134 //float(string s, string separator1, ...) tokenizebyseparator = #479; // takes apart a string into individal words (access them with argv), returns how many
2135 //this function returns the token preceding each instance of a separator (of
2136 //which there can be multiple), and the text following the last separator
2137 //useful for parsing certain kinds of data like IP addresses
2139 //numnumbers = tokenizebyseparator("10.1.2.3", ".");
2140 //returns 4 and the tokens "10" "1" "2" "3".
2141 void VM_tokenizebyseparator (void)
2145 int separatorlen[7];
2146 const char *separators[7];
2149 char tokentext[MAX_INPUTLINE];
2150 static char string[VM_STRINGTEMP_LENGTH]; // static, because it's big
2152 VM_SAFEPARMCOUNTRANGE(2, 8,VM_tokenizebyseparator);
2154 strlcpy(string, PRVM_G_STRING(OFS_PARM0), sizeof(string));
2158 for (j = 1;j < prog->argc;j++)
2160 // skip any blank separator strings
2161 const char *s = PRVM_G_STRING(OFS_PARM0+j*3);
2164 separators[numseparators] = s;
2165 separatorlen[numseparators] = strlen(s);
2172 while (num_tokens < (int)(sizeof(tokens)/sizeof(tokens[0])))
2174 token = tokentext + j;
2177 for (k = 0;k < numseparators;k++)
2179 if (!strncmp(p, separators[k], separatorlen[k]))
2181 p += separatorlen[k];
2185 if (k < numseparators)
2187 if (j < (int)sizeof(tokentext)-1)
2188 tokentext[j++] = *p;
2191 if (j >= (int)sizeof(tokentext))
2194 tokens[num_tokens++] = PRVM_SetTempString(token);
2199 PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
2202 //string(float n) argv = #442; // returns a word from the tokenized string (returns nothing for an invalid index)
2203 //this function originally written by KrimZon, made shorter by LordHavoc
2208 VM_SAFEPARMCOUNT(1,VM_argv);
2210 token_num = (int)PRVM_G_FLOAT(OFS_PARM0);
2212 if (token_num >= 0 && token_num < num_tokens)
2213 PRVM_G_INT(OFS_RETURN) = tokens[token_num];
2215 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
2225 void VM_isserver(void)
2227 VM_SAFEPARMCOUNT(0,VM_serverstate);
2229 PRVM_G_FLOAT(OFS_RETURN) = sv.active && (svs.maxclients > 1 || cls.state == ca_dedicated);
2239 void VM_clientcount(void)
2241 VM_SAFEPARMCOUNT(0,VM_clientcount);
2243 PRVM_G_FLOAT(OFS_RETURN) = svs.maxclients;
2253 void VM_clientstate(void)
2255 VM_SAFEPARMCOUNT(0,VM_clientstate);
2258 switch( cls.state ) {
2259 case ca_uninitialized:
2261 PRVM_G_FLOAT(OFS_RETURN) = 0;
2263 case ca_disconnected:
2264 PRVM_G_FLOAT(OFS_RETURN) = 1;
2267 PRVM_G_FLOAT(OFS_RETURN) = 2;
2270 // should never be reached!
2279 float getostype(void)
2281 */ // not used at the moment -> not included in the common list
2282 void VM_getostype(void)
2284 VM_SAFEPARMCOUNT(0,VM_getostype);
2289 OS_MAC - not supported
2293 PRVM_G_FLOAT(OFS_RETURN) = 0;
2294 #elif defined(MACOSX)
2295 PRVM_G_FLOAT(OFS_RETURN) = 2;
2297 PRVM_G_FLOAT(OFS_RETURN) = 1;
2305 vector getmousepos()
2308 void VM_getmousepos(void)
2310 VM_SAFEPARMCOUNT(0,VM_getmousepos);
2312 // FIXME: somehow this should involve in_client_mouse if this is menu progs
2313 if (cl.csqc_wantsmousemove)
2314 VectorSet(PRVM_G_VECTOR(OFS_RETURN), in_windowmouse_x * vid_conwidth.integer / vid.width, in_windowmouse_y * vid_conheight.integer / vid.height, 0);
2316 VectorSet(PRVM_G_VECTOR(OFS_RETURN), in_mouse_x * vid_conwidth.integer / vid.width, in_mouse_y * vid_conheight.integer / vid.height, 0);
2326 void VM_gettime(void)
2328 VM_SAFEPARMCOUNT(0,VM_gettime);
2330 PRVM_G_FLOAT(OFS_RETURN) = (float) realtime;
2337 loadfromdata(string data)
2340 void VM_loadfromdata(void)
2342 VM_SAFEPARMCOUNT(1,VM_loadentsfromfile);
2344 PRVM_ED_LoadFromFile(PRVM_G_STRING(OFS_PARM0));
2348 ========================
2351 parseentitydata(entity ent, string data)
2352 ========================
2354 void VM_parseentitydata(void)
2359 VM_SAFEPARMCOUNT(2, VM_parseentitydata);
2361 // get edict and test it
2362 ent = PRVM_G_EDICT(OFS_PARM0);
2363 if (ent->priv.required->free)
2364 PRVM_ERROR ("VM_parseentitydata: %s: Can only set already spawned entities (entity %i is free)!", PRVM_NAME, PRVM_NUM_FOR_EDICT(ent));
2366 data = PRVM_G_STRING(OFS_PARM1);
2368 // parse the opening brace
2369 if (!COM_ParseToken_Simple(&data, false, false) || com_token[0] != '{' )
2370 PRVM_ERROR ("VM_parseentitydata: %s: Couldn't parse entity data:\n%s", PRVM_NAME, data );
2372 PRVM_ED_ParseEdict (data, ent);
2379 loadfromfile(string file)
2382 void VM_loadfromfile(void)
2384 const char *filename;
2387 VM_SAFEPARMCOUNT(1,VM_loadfromfile);
2389 filename = PRVM_G_STRING(OFS_PARM0);
2390 if (FS_CheckNastyPath(filename, false))
2392 PRVM_G_FLOAT(OFS_RETURN) = -4;
2393 VM_Warning("VM_loadfromfile: %s dangerous or non-portable filename \"%s\" not allowed. (contains : or \\ or begins with .. or /)\n", PRVM_NAME, filename);
2397 // not conform with VM_fopen
2398 data = (char *)FS_LoadFile(filename, tempmempool, false, NULL);
2400 PRVM_G_FLOAT(OFS_RETURN) = -1;
2402 PRVM_ED_LoadFromFile(data);
2413 float mod(float val, float m)
2416 void VM_modulo(void)
2419 VM_SAFEPARMCOUNT(2,VM_module);
2421 val = (int) PRVM_G_FLOAT(OFS_PARM0);
2422 m = (int) PRVM_G_FLOAT(OFS_PARM1);
2424 PRVM_G_FLOAT(OFS_RETURN) = (float) (val % m);
2427 void VM_Search_Init(void)
2430 for (i = 0;i < PRVM_MAX_OPENSEARCHES;i++)
2431 prog->opensearches[i] = NULL;
2434 void VM_Search_Reset(void)
2437 // reset the fssearch list
2438 for(i = 0; i < PRVM_MAX_OPENSEARCHES; i++)
2440 if(prog->opensearches[i])
2441 FS_FreeSearch(prog->opensearches[i]);
2442 prog->opensearches[i] = NULL;
2450 float search_begin(string pattern, float caseinsensitive, float quiet)
2453 void VM_search_begin(void)
2456 const char *pattern;
2457 int caseinsens, quiet;
2459 VM_SAFEPARMCOUNT(3, VM_search_begin);
2461 pattern = PRVM_G_STRING(OFS_PARM0);
2463 VM_CheckEmptyString(pattern);
2465 caseinsens = (int)PRVM_G_FLOAT(OFS_PARM1);
2466 quiet = (int)PRVM_G_FLOAT(OFS_PARM2);
2468 for(handle = 0; handle < PRVM_MAX_OPENSEARCHES; handle++)
2469 if(!prog->opensearches[handle])
2472 if(handle >= PRVM_MAX_OPENSEARCHES)
2474 PRVM_G_FLOAT(OFS_RETURN) = -2;
2475 VM_Warning("VM_search_begin: %s ran out of search handles (%i)\n", PRVM_NAME, PRVM_MAX_OPENSEARCHES);
2479 if(!(prog->opensearches[handle] = FS_Search(pattern,caseinsens, quiet)))
2480 PRVM_G_FLOAT(OFS_RETURN) = -1;
2482 PRVM_G_FLOAT(OFS_RETURN) = handle;
2489 void search_end(float handle)
2492 void VM_search_end(void)
2495 VM_SAFEPARMCOUNT(1, VM_search_end);
2497 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
2499 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
2501 VM_Warning("VM_search_end: invalid handle %i used in %s\n", handle, PRVM_NAME);
2504 if(prog->opensearches[handle] == NULL)
2506 VM_Warning("VM_search_end: no such handle %i in %s\n", handle, PRVM_NAME);
2510 FS_FreeSearch(prog->opensearches[handle]);
2511 prog->opensearches[handle] = NULL;
2518 float search_getsize(float handle)
2521 void VM_search_getsize(void)
2524 VM_SAFEPARMCOUNT(1, VM_M_search_getsize);
2526 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
2528 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
2530 VM_Warning("VM_search_getsize: invalid handle %i used in %s\n", handle, PRVM_NAME);
2533 if(prog->opensearches[handle] == NULL)
2535 VM_Warning("VM_search_getsize: no such handle %i in %s\n", handle, PRVM_NAME);
2539 PRVM_G_FLOAT(OFS_RETURN) = prog->opensearches[handle]->numfilenames;
2544 VM_search_getfilename
2546 string search_getfilename(float handle, float num)
2549 void VM_search_getfilename(void)
2551 int handle, filenum;
2552 VM_SAFEPARMCOUNT(2, VM_search_getfilename);
2554 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
2555 filenum = (int)PRVM_G_FLOAT(OFS_PARM1);
2557 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
2559 VM_Warning("VM_search_getfilename: invalid handle %i used in %s\n", handle, PRVM_NAME);
2562 if(prog->opensearches[handle] == NULL)
2564 VM_Warning("VM_search_getfilename: no such handle %i in %s\n", handle, PRVM_NAME);
2567 if(filenum < 0 || filenum >= prog->opensearches[handle]->numfilenames)
2569 VM_Warning("VM_search_getfilename: invalid filenum %i in %s\n", filenum, PRVM_NAME);
2573 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog->opensearches[handle]->filenames[filenum]);
2580 string chr(float ascii)
2586 VM_SAFEPARMCOUNT(1, VM_chr);
2588 tmp[0] = (unsigned char) PRVM_G_FLOAT(OFS_PARM0);
2591 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(tmp);
2594 //=============================================================================
2595 // Draw builtins (client & menu)
2601 float iscachedpic(string pic)
2604 void VM_iscachedpic(void)
2606 VM_SAFEPARMCOUNT(1,VM_iscachedpic);
2608 // drawq hasnt such a function, thus always return true
2609 PRVM_G_FLOAT(OFS_RETURN) = false;
2616 string precache_pic(string pic)
2619 void VM_precache_pic(void)
2623 VM_SAFEPARMCOUNT(1, VM_precache_pic);
2625 s = PRVM_G_STRING(OFS_PARM0);
2626 PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
2627 VM_CheckEmptyString (s);
2629 // AK Draw_CachePic is supposed to always return a valid pointer
2630 if( Draw_CachePic_Flags(s, CACHEPICFLAG_NOTPERSISTENT)->tex == r_texture_notexture )
2631 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
2641 void VM_freepic(void)
2645 VM_SAFEPARMCOUNT(1,VM_freepic);
2647 s = PRVM_G_STRING(OFS_PARM0);
2648 VM_CheckEmptyString (s);
2653 dp_font_t *getdrawfont()
2655 if(prog->globaloffsets.drawfont >= 0)
2657 int f = PRVM_G_FLOAT(prog->globaloffsets.drawfont);
2658 if(f < 0 || f >= MAX_FONTS)
2659 return FONT_DEFAULT;
2660 return &dp_fonts[f];
2663 return FONT_DEFAULT;
2670 float drawcharacter(vector position, float character, vector scale, vector rgb, float alpha, float flag)
2673 void VM_drawcharacter(void)
2675 float *pos,*scale,*rgb;
2678 VM_SAFEPARMCOUNT(6,VM_drawcharacter);
2680 character = (char) PRVM_G_FLOAT(OFS_PARM1);
2683 PRVM_G_FLOAT(OFS_RETURN) = -1;
2684 VM_Warning("VM_drawcharacter: %s passed null character !\n",PRVM_NAME);
2688 pos = PRVM_G_VECTOR(OFS_PARM0);
2689 scale = PRVM_G_VECTOR(OFS_PARM2);
2690 rgb = PRVM_G_VECTOR(OFS_PARM3);
2691 flag = (int)PRVM_G_FLOAT(OFS_PARM5);
2693 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2695 PRVM_G_FLOAT(OFS_RETURN) = -2;
2696 VM_Warning("VM_drawcharacter: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2700 if(pos[2] || scale[2])
2701 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")));
2703 if(!scale[0] || !scale[1])
2705 PRVM_G_FLOAT(OFS_RETURN) = -3;
2706 VM_Warning("VM_drawcharacter: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
2710 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());
2711 PRVM_G_FLOAT(OFS_RETURN) = 1;
2718 float drawstring(vector position, string text, vector scale, vector rgb, float alpha, float flag)
2721 void VM_drawstring(void)
2723 float *pos,*scale,*rgb;
2726 VM_SAFEPARMCOUNT(6,VM_drawstring);
2728 string = PRVM_G_STRING(OFS_PARM1);
2729 pos = PRVM_G_VECTOR(OFS_PARM0);
2730 scale = PRVM_G_VECTOR(OFS_PARM2);
2731 rgb = PRVM_G_VECTOR(OFS_PARM3);
2732 flag = (int)PRVM_G_FLOAT(OFS_PARM5);
2734 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2736 PRVM_G_FLOAT(OFS_RETURN) = -2;
2737 VM_Warning("VM_drawstring: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2741 if(!scale[0] || !scale[1])
2743 PRVM_G_FLOAT(OFS_RETURN) = -3;
2744 VM_Warning("VM_drawstring: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
2748 if(pos[2] || scale[2])
2749 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")));
2751 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());
2752 PRVM_G_FLOAT(OFS_RETURN) = 1;
2757 VM_drawcolorcodedstring
2759 float drawcolorcodedstring(vector position, string text, vector scale, float alpha, float flag)
2762 void VM_drawcolorcodedstring(void)
2767 VM_SAFEPARMCOUNT(5,VM_drawstring);
2769 string = PRVM_G_STRING(OFS_PARM1);
2770 pos = PRVM_G_VECTOR(OFS_PARM0);
2771 scale = PRVM_G_VECTOR(OFS_PARM2);
2772 flag = (int)PRVM_G_FLOAT(OFS_PARM5);
2774 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2776 PRVM_G_FLOAT(OFS_RETURN) = -2;
2777 VM_Warning("VM_drawcolorcodedstring: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2781 if(!scale[0] || !scale[1])
2783 PRVM_G_FLOAT(OFS_RETURN) = -3;
2784 VM_Warning("VM_drawcolorcodedstring: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
2788 if(pos[2] || scale[2])
2789 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")));
2792 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());
2793 PRVM_G_FLOAT(OFS_RETURN) = 1;
2799 float stringwidth(string text, float allowColorCodes)
2802 void VM_stringwidth(void)
2806 VM_SAFEPARMCOUNT(2,VM_drawstring);
2808 string = PRVM_G_STRING(OFS_PARM0);
2809 colors = (int)PRVM_G_FLOAT(OFS_PARM1);
2811 PRVM_G_FLOAT(OFS_RETURN) = DrawQ_TextWidth_Font(string, 0, !colors, getdrawfont()); // 1x1 characters, don't actually draw
2817 float drawpic(vector position, string pic, vector size, vector rgb, float alpha, float flag)
2820 void VM_drawpic(void)
2822 const char *picname;
2823 float *size, *pos, *rgb;
2826 VM_SAFEPARMCOUNT(6,VM_drawpic);
2828 picname = PRVM_G_STRING(OFS_PARM1);
2829 VM_CheckEmptyString (picname);
2831 // is pic cached ? no function yet for that
2834 PRVM_G_FLOAT(OFS_RETURN) = -4;
2835 VM_Warning("VM_drawpic: %s: %s not cached !\n", PRVM_NAME, picname);
2839 pos = PRVM_G_VECTOR(OFS_PARM0);
2840 size = PRVM_G_VECTOR(OFS_PARM2);
2841 rgb = PRVM_G_VECTOR(OFS_PARM3);
2842 flag = (int) PRVM_G_FLOAT(OFS_PARM5);
2844 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2846 PRVM_G_FLOAT(OFS_RETURN) = -2;
2847 VM_Warning("VM_drawpic: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2851 if(pos[2] || size[2])
2852 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")));
2854 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);
2855 PRVM_G_FLOAT(OFS_RETURN) = 1;
2861 float drawsubpic(vector position, vector size, string pic, vector srcPos, vector srcSize, vector rgb, float alpha, float flag)
2865 void VM_drawsubpic(void)
2867 const char *picname;
2868 float *size, *pos, *rgb, *srcPos, *srcSize, alpha;
2871 VM_SAFEPARMCOUNT(8,VM_drawsubpic);
2873 picname = PRVM_G_STRING(OFS_PARM2);
2874 VM_CheckEmptyString (picname);
2876 // is pic cached ? no function yet for that
2879 PRVM_G_FLOAT(OFS_RETURN) = -4;
2880 VM_Warning("VM_drawsubpic: %s: %s not cached !\n", PRVM_NAME, picname);
2884 pos = PRVM_G_VECTOR(OFS_PARM0);
2885 size = PRVM_G_VECTOR(OFS_PARM1);
2886 srcPos = PRVM_G_VECTOR(OFS_PARM3);
2887 srcSize = PRVM_G_VECTOR(OFS_PARM4);
2888 rgb = PRVM_G_VECTOR(OFS_PARM5);
2889 alpha = PRVM_G_FLOAT(OFS_PARM6);
2890 flag = (int) PRVM_G_FLOAT(OFS_PARM7);
2892 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2894 PRVM_G_FLOAT(OFS_RETURN) = -2;
2895 VM_Warning("VM_drawsubpic: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2899 if(pos[2] || size[2])
2900 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")));
2902 DrawQ_SuperPic(pos[0], pos[1], Draw_CachePic (picname),
2904 srcPos[0], srcPos[1], rgb[0], rgb[1], rgb[2], alpha,
2905 srcPos[0] + srcSize[0], srcPos[1], rgb[0], rgb[1], rgb[2], alpha,
2906 srcPos[0], srcPos[1] + srcSize[1], rgb[0], rgb[1], rgb[2], alpha,
2907 srcPos[0] + srcSize[0], srcPos[1] + srcSize[1], rgb[0], rgb[1], rgb[2], alpha,
2909 PRVM_G_FLOAT(OFS_RETURN) = 1;
2916 float drawfill(vector position, vector size, vector rgb, float alpha, float flag)
2919 void VM_drawfill(void)
2921 float *size, *pos, *rgb;
2924 VM_SAFEPARMCOUNT(5,VM_drawfill);
2927 pos = PRVM_G_VECTOR(OFS_PARM0);
2928 size = PRVM_G_VECTOR(OFS_PARM1);
2929 rgb = PRVM_G_VECTOR(OFS_PARM2);
2930 flag = (int) PRVM_G_FLOAT(OFS_PARM4);
2932 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2934 PRVM_G_FLOAT(OFS_RETURN) = -2;
2935 VM_Warning("VM_drawfill: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2939 if(pos[2] || size[2])
2940 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")));
2942 DrawQ_Fill(pos[0], pos[1], size[0], size[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM3), flag);
2943 PRVM_G_FLOAT(OFS_RETURN) = 1;
2950 drawsetcliparea(float x, float y, float width, float height)
2953 void VM_drawsetcliparea(void)
2956 VM_SAFEPARMCOUNT(4,VM_drawsetcliparea);
2958 x = bound(0, PRVM_G_FLOAT(OFS_PARM0), vid_conwidth.integer);
2959 y = bound(0, PRVM_G_FLOAT(OFS_PARM1), vid_conheight.integer);
2960 w = bound(0, PRVM_G_FLOAT(OFS_PARM2) + PRVM_G_FLOAT(OFS_PARM0) - x, (vid_conwidth.integer - x));
2961 h = bound(0, PRVM_G_FLOAT(OFS_PARM3) + PRVM_G_FLOAT(OFS_PARM1) - y, (vid_conheight.integer - y));
2963 DrawQ_SetClipArea(x, y, w, h);
2968 VM_drawresetcliparea
2973 void VM_drawresetcliparea(void)
2975 VM_SAFEPARMCOUNT(0,VM_drawresetcliparea);
2977 DrawQ_ResetClipArea();
2984 vector getimagesize(string pic)
2987 void VM_getimagesize(void)
2992 VM_SAFEPARMCOUNT(1,VM_getimagesize);
2994 p = PRVM_G_STRING(OFS_PARM0);
2995 VM_CheckEmptyString (p);
2997 pic = Draw_CachePic_Flags (p, CACHEPICFLAG_NOTPERSISTENT);
2999 PRVM_G_VECTOR(OFS_RETURN)[0] = pic->width;
3000 PRVM_G_VECTOR(OFS_RETURN)[1] = pic->height;
3001 PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
3008 string keynumtostring(float keynum)
3011 void VM_keynumtostring (void)
3013 VM_SAFEPARMCOUNT(1, VM_keynumtostring);
3015 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(Key_KeynumToString((int)PRVM_G_FLOAT(OFS_PARM0)));
3022 float stringtokeynum(string key)
3025 void VM_stringtokeynum (void)
3027 VM_SAFEPARMCOUNT( 1, VM_keynumtostring );
3029 PRVM_G_INT(OFS_RETURN) = Key_StringToKeynum(PRVM_G_STRING(OFS_PARM0));
3032 // CL_Video interface functions
3035 ========================
3038 float cin_open(string file, string name)
3039 ========================
3041 void VM_cin_open( void )
3046 VM_SAFEPARMCOUNT( 2, VM_cin_open );
3048 file = PRVM_G_STRING( OFS_PARM0 );
3049 name = PRVM_G_STRING( OFS_PARM1 );
3051 VM_CheckEmptyString( file );
3052 VM_CheckEmptyString( name );
3054 if( CL_OpenVideo( file, name, MENUOWNER ) )
3055 PRVM_G_FLOAT( OFS_RETURN ) = 1;
3057 PRVM_G_FLOAT( OFS_RETURN ) = 0;
3061 ========================
3064 void cin_close(string name)
3065 ========================
3067 void VM_cin_close( void )
3071 VM_SAFEPARMCOUNT( 1, VM_cin_close );
3073 name = PRVM_G_STRING( OFS_PARM0 );
3074 VM_CheckEmptyString( name );
3076 CL_CloseVideo( CL_GetVideoByName( name ) );
3080 ========================
3082 void cin_setstate(string name, float type)
3083 ========================
3085 void VM_cin_setstate( void )
3088 clvideostate_t state;
3091 VM_SAFEPARMCOUNT( 2, VM_cin_netstate );
3093 name = PRVM_G_STRING( OFS_PARM0 );
3094 VM_CheckEmptyString( name );
3096 state = (clvideostate_t)((int)PRVM_G_FLOAT( OFS_PARM1 ));
3098 video = CL_GetVideoByName( name );
3099 if( video && state > CLVIDEO_UNUSED && state < CLVIDEO_STATECOUNT )
3100 CL_SetVideoState( video, state );
3104 ========================
3107 float cin_getstate(string name)
3108 ========================
3110 void VM_cin_getstate( void )
3115 VM_SAFEPARMCOUNT( 1, VM_cin_getstate );
3117 name = PRVM_G_STRING( OFS_PARM0 );
3118 VM_CheckEmptyString( name );
3120 video = CL_GetVideoByName( name );
3122 PRVM_G_FLOAT( OFS_RETURN ) = (int)video->state;
3124 PRVM_G_FLOAT( OFS_RETURN ) = 0;
3128 ========================
3131 void cin_restart(string name)
3132 ========================
3134 void VM_cin_restart( void )
3139 VM_SAFEPARMCOUNT( 1, VM_cin_restart );
3141 name = PRVM_G_STRING( OFS_PARM0 );
3142 VM_CheckEmptyString( name );
3144 video = CL_GetVideoByName( name );
3146 CL_RestartVideo( video );
3150 ========================
3152 ========================
3154 void VM_Gecko_Init( void ) {
3155 // the prog struct is memset to 0 by Initprog? [12/6/2007 Black]
3156 // FIXME: remove the other _Init functions then, too? [12/6/2007 Black]
3160 ========================
3162 ========================
3164 void VM_Gecko_Destroy( void ) {
3166 for( i = 0 ; i < PRVM_MAX_GECKOINSTANCES ; i++ ) {
3167 clgecko_t **instance = &prog->opengeckoinstances[ i ];
3169 CL_Gecko_DestroyBrowser( *instance );
3176 ========================
3179 float[bool] gecko_create( string name )
3180 ========================
3182 void VM_gecko_create( void ) {
3185 clgecko_t *instance;
3187 VM_SAFEPARMCOUNT( 1, VM_gecko_create );
3189 name = PRVM_G_STRING( OFS_PARM0 );
3190 VM_CheckEmptyString( name );
3192 // find an empty slot for this gecko browser..
3193 for( i = 0 ; i < PRVM_MAX_GECKOINSTANCES ; i++ ) {
3194 if( prog->opengeckoinstances[ i ] == NULL ) {
3198 if( i == PRVM_MAX_GECKOINSTANCES ) {
3199 VM_Warning("VM_gecko_create: %s ran out of gecko handles (%i)\n", PRVM_NAME, PRVM_MAX_GECKOINSTANCES);
3200 PRVM_G_FLOAT( OFS_RETURN ) = 0;
3204 instance = prog->opengeckoinstances[ i ] = CL_Gecko_CreateBrowser( name, PRVM_GetProgNr() );
3206 // TODO: error handling [12/3/2007 Black]
3207 PRVM_G_FLOAT( OFS_RETURN ) = 0;
3210 PRVM_G_FLOAT( OFS_RETURN ) = 1;
3214 ========================
3217 void gecko_destroy( string name )
3218 ========================
3220 void VM_gecko_destroy( void ) {
3222 clgecko_t *instance;
3224 VM_SAFEPARMCOUNT( 1, VM_gecko_destroy );
3226 name = PRVM_G_STRING( OFS_PARM0 );
3227 VM_CheckEmptyString( name );
3228 instance = CL_Gecko_FindBrowser( name );
3232 CL_Gecko_DestroyBrowser( instance );
3236 ========================
3239 void gecko_navigate( string name, string URI )
3240 ========================
3242 void VM_gecko_navigate( void ) {
3245 clgecko_t *instance;
3247 VM_SAFEPARMCOUNT( 2, VM_gecko_navigate );
3249 name = PRVM_G_STRING( OFS_PARM0 );
3250 URI = PRVM_G_STRING( OFS_PARM1 );
3251 VM_CheckEmptyString( name );
3252 VM_CheckEmptyString( URI );
3254 instance = CL_Gecko_FindBrowser( name );
3258 CL_Gecko_NavigateToURI( instance, URI );
3262 ========================
3265 float[bool] gecko_keyevent( string name, float key, float eventtype )
3266 ========================
3268 void VM_gecko_keyevent( void ) {
3271 clgecko_buttoneventtype_t eventtype;
3272 clgecko_t *instance;
3274 VM_SAFEPARMCOUNT( 3, VM_gecko_keyevent );
3276 name = PRVM_G_STRING( OFS_PARM0 );
3277 VM_CheckEmptyString( name );
3278 key = (unsigned int) PRVM_G_FLOAT( OFS_PARM1 );
3279 switch( (unsigned int) PRVM_G_FLOAT( OFS_PARM2 ) ) {
3281 eventtype = CLG_BET_DOWN;
3284 eventtype = CLG_BET_UP;
3287 eventtype = CLG_BET_PRESS;
3290 eventtype = CLG_BET_DOUBLECLICK;
3293 // TODO: console printf? [12/3/2007 Black]
3294 PRVM_G_FLOAT( OFS_RETURN ) = 0;
3298 instance = CL_Gecko_FindBrowser( name );
3300 PRVM_G_FLOAT( OFS_RETURN ) = 0;
3304 PRVM_G_FLOAT( OFS_RETURN ) = (CL_Gecko_Event_Key( instance, key, eventtype ) == true);
3308 ========================
3311 void gecko_mousemove( string name, float x, float y )
3312 ========================
3314 void VM_gecko_movemouse( void ) {
3317 clgecko_t *instance;
3319 VM_SAFEPARMCOUNT( 3, VM_gecko_movemouse );
3321 name = PRVM_G_STRING( OFS_PARM0 );
3322 VM_CheckEmptyString( name );
3323 x = PRVM_G_FLOAT( OFS_PARM1 );
3324 y = PRVM_G_FLOAT( OFS_PARM2 );
3326 instance = CL_Gecko_FindBrowser( name );
3330 CL_Gecko_Event_CursorMove( instance, x, y );
3335 ========================
3338 void gecko_resize( string name, float w, float h )
3339 ========================
3341 void VM_gecko_resize( void ) {
3344 clgecko_t *instance;
3346 VM_SAFEPARMCOUNT( 3, VM_gecko_movemouse );
3348 name = PRVM_G_STRING( OFS_PARM0 );
3349 VM_CheckEmptyString( name );
3350 w = PRVM_G_FLOAT( OFS_PARM1 );
3351 h = PRVM_G_FLOAT( OFS_PARM2 );
3353 instance = CL_Gecko_FindBrowser( name );
3357 CL_Gecko_Resize( instance, w, h );
3362 ========================
3363 VM_gecko_get_texture_extent
3365 vector gecko_get_texture_extent( string name )
3366 ========================
3368 void VM_gecko_get_texture_extent( void ) {
3370 clgecko_t *instance;
3372 VM_SAFEPARMCOUNT( 1, VM_gecko_movemouse );
3374 name = PRVM_G_STRING( OFS_PARM0 );
3375 VM_CheckEmptyString( name );
3377 PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
3378 instance = CL_Gecko_FindBrowser( name );
3380 PRVM_G_VECTOR(OFS_RETURN)[0] = 0;
3381 PRVM_G_VECTOR(OFS_RETURN)[1] = 0;
3384 CL_Gecko_GetTextureExtent( instance,
3385 PRVM_G_VECTOR(OFS_RETURN), PRVM_G_VECTOR(OFS_RETURN)+1 );
3394 Writes new values for v_forward, v_up, and v_right based on angles
3395 void makevectors(vector angle)
3398 void VM_makevectors (void)
3400 prvm_eval_t *valforward, *valright, *valup;
3401 valforward = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_forward);
3402 valright = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_right);
3403 valup = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_up);
3404 if (!valforward || !valright || !valup)
3406 VM_Warning("makevectors: could not find v_forward, v_right, or v_up global variables\n");
3409 VM_SAFEPARMCOUNT(1, VM_makevectors);
3410 AngleVectors (PRVM_G_VECTOR(OFS_PARM0), valforward->vector, valright->vector, valup->vector);
3417 Writes new values for v_forward, v_up, and v_right based on the given forward vector
3418 vectorvectors(vector)
3421 void VM_vectorvectors (void)
3423 prvm_eval_t *valforward, *valright, *valup;
3424 valforward = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_forward);
3425 valright = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_right);
3426 valup = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_up);
3427 if (!valforward || !valright || !valup)
3429 VM_Warning("vectorvectors: could not find v_forward, v_right, or v_up global variables\n");
3432 VM_SAFEPARMCOUNT(1, VM_vectorvectors);
3433 VectorNormalize2(PRVM_G_VECTOR(OFS_PARM0), valforward->vector);
3434 VectorVectors(valforward->vector, valright->vector, valup->vector);
3438 ========================
3441 void drawline(float width, vector pos1, vector pos2, vector rgb, float alpha, float flags)
3442 ========================
3444 void VM_drawline (void)
3446 float *c1, *c2, *rgb;
3448 unsigned char flags;
3450 VM_SAFEPARMCOUNT(6, VM_drawline);
3451 width = PRVM_G_FLOAT(OFS_PARM0);
3452 c1 = PRVM_G_VECTOR(OFS_PARM1);
3453 c2 = PRVM_G_VECTOR(OFS_PARM2);
3454 rgb = PRVM_G_VECTOR(OFS_PARM3);
3455 alpha = PRVM_G_FLOAT(OFS_PARM4);
3456 flags = (int)PRVM_G_FLOAT(OFS_PARM5);
3457 DrawQ_Line(width, c1[0], c1[1], c2[0], c2[1], rgb[0], rgb[1], rgb[2], alpha, flags);
3460 // float(float number, float quantity) bitshift (EXT_BITSHIFT)
3461 void VM_bitshift (void)
3464 VM_SAFEPARMCOUNT(2, VM_bitshift);
3466 n1 = (int)fabs((int)PRVM_G_FLOAT(OFS_PARM0));
3467 n2 = (int)PRVM_G_FLOAT(OFS_PARM1);
3469 PRVM_G_FLOAT(OFS_RETURN) = n1;
3472 PRVM_G_FLOAT(OFS_RETURN) = (n1 >> -n2);
3474 PRVM_G_FLOAT(OFS_RETURN) = (n1 << n2);
3477 ////////////////////////////////////////
3478 // AltString functions
3479 ////////////////////////////////////////
3482 ========================
3485 float altstr_count(string)
3486 ========================
3488 void VM_altstr_count( void )
3490 const char *altstr, *pos;
3493 VM_SAFEPARMCOUNT( 1, VM_altstr_count );
3495 altstr = PRVM_G_STRING( OFS_PARM0 );
3496 //VM_CheckEmptyString( altstr );
3498 for( count = 0, pos = altstr ; *pos ; pos++ ) {
3499 if( *pos == '\\' ) {
3503 } else if( *pos == '\'' ) {
3508 PRVM_G_FLOAT( OFS_RETURN ) = (float) (count / 2);
3512 ========================
3515 string altstr_prepare(string)
3516 ========================
3518 void VM_altstr_prepare( void )
3521 const char *instr, *in;
3523 char outstr[VM_STRINGTEMP_LENGTH];
3525 VM_SAFEPARMCOUNT( 1, VM_altstr_prepare );
3527 instr = PRVM_G_STRING( OFS_PARM0 );
3529 for( out = outstr, in = instr, size = sizeof(outstr) - 1 ; size && *in ; size--, in++, out++ )
3538 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
3542 ========================
3545 string altstr_get(string, float)
3546 ========================
3548 void VM_altstr_get( void )
3550 const char *altstr, *pos;
3553 char outstr[VM_STRINGTEMP_LENGTH];
3555 VM_SAFEPARMCOUNT( 2, VM_altstr_get );
3557 altstr = PRVM_G_STRING( OFS_PARM0 );
3559 count = (int)PRVM_G_FLOAT( OFS_PARM1 );
3560 count = count * 2 + 1;
3562 for( pos = altstr ; *pos && count ; pos++ )
3563 if( *pos == '\\' ) {
3566 } else if( *pos == '\'' )
3570 PRVM_G_INT( OFS_RETURN ) = 0;
3574 for( out = outstr, size = sizeof(outstr) - 1 ; size && *pos ; size--, pos++, out++ )
3575 if( *pos == '\\' ) {
3580 } else if( *pos == '\'' )
3586 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
3590 ========================
3593 string altstr_set(string altstr, float num, string set)
3594 ========================
3596 void VM_altstr_set( void )
3599 const char *altstr, *str;
3602 char outstr[VM_STRINGTEMP_LENGTH];
3604 VM_SAFEPARMCOUNT( 3, VM_altstr_set );
3606 altstr = PRVM_G_STRING( OFS_PARM0 );
3608 num = (int)PRVM_G_FLOAT( OFS_PARM1 );
3610 str = PRVM_G_STRING( OFS_PARM2 );
3613 for( num = num * 2 + 1, in = altstr; *in && num; *out++ = *in++ )
3618 } else if( *in == '\'' ) {
3623 for( ; *str; *out++ = *str++ );
3624 // now jump over the old content
3626 if( *in == '\'' || (*in == '\\' && !*++in) )
3629 strlcpy(out, in, outstr + sizeof(outstr) - out);
3630 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
3634 ========================
3637 string altstr_ins(string altstr, float num, string set)
3638 ========================
3640 void VM_altstr_ins(void)
3648 char outstr[VM_STRINGTEMP_LENGTH];
3650 VM_SAFEPARMCOUNT(3, VM_altstr_ins);
3652 in = instr = PRVM_G_STRING( OFS_PARM0 );
3653 num = (int)PRVM_G_FLOAT( OFS_PARM1 );
3654 set = setstr = PRVM_G_STRING( OFS_PARM2 );
3657 for( num = num * 2 + 2 ; *in && num > 0 ; *out++ = *in++ )
3662 } else if( *in == '\'' ) {
3667 for( ; *set ; *out++ = *set++ );
3670 strlcpy(out, in, outstr + sizeof(outstr) - out);
3671 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
3675 ////////////////////////////////////////
3676 // BufString functions
3677 ////////////////////////////////////////
3678 //[515]: string buffers support
3680 static size_t stringbuffers_sortlength;
3682 static void BufStr_Expand(prvm_stringbuffer_t *stringbuffer, int strindex)
3684 if (stringbuffer->max_strings <= strindex)
3686 char **oldstrings = stringbuffer->strings;
3687 stringbuffer->max_strings = max(stringbuffer->max_strings * 2, 128);
3688 while (stringbuffer->max_strings <= strindex)
3689 stringbuffer->max_strings *= 2;
3690 stringbuffer->strings = Mem_Alloc(prog->progs_mempool, stringbuffer->max_strings * sizeof(stringbuffer->strings[0]));
3691 if (stringbuffer->num_strings > 0)
3692 memcpy(stringbuffer->strings, oldstrings, stringbuffer->num_strings * sizeof(stringbuffer->strings[0]));
3694 Mem_Free(oldstrings);
3698 static void BufStr_Shrink(prvm_stringbuffer_t *stringbuffer)
3700 // reduce num_strings if there are empty string slots at the end
3701 while (stringbuffer->num_strings > 0 && stringbuffer->strings[stringbuffer->num_strings - 1] == NULL)
3702 stringbuffer->num_strings--;
3704 // if empty, free the string pointer array
3705 if (stringbuffer->num_strings == 0)
3707 stringbuffer->max_strings = 0;
3708 if (stringbuffer->strings)
3709 Mem_Free(stringbuffer->strings);
3710 stringbuffer->strings = NULL;
3714 static int BufStr_SortStringsUP (const void *in1, const void *in2)
3717 a = *((const char **) in1);
3718 b = *((const char **) in2);
3720 if(!b[0]) return -1;
3721 return strncmp(a, b, stringbuffers_sortlength);
3724 static int BufStr_SortStringsDOWN (const void *in1, const void *in2)
3727 a = *((const char **) in1);
3728 b = *((const char **) in2);
3730 if(!b[0]) return -1;
3731 return strncmp(b, a, stringbuffers_sortlength);
3735 ========================
3737 creates new buffer, and returns it's index, returns -1 if failed
3738 float buf_create(void) = #460;
3739 ========================
3741 void VM_buf_create (void)
3743 prvm_stringbuffer_t *stringbuffer;
3745 VM_SAFEPARMCOUNT(0, VM_buf_create);
3746 stringbuffer = Mem_ExpandableArray_AllocRecord(&prog->stringbuffersarray);
3747 for (i = 0;stringbuffer != Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, i);i++);
3748 PRVM_G_FLOAT(OFS_RETURN) = i;
3752 ========================
3754 deletes buffer and all strings in it
3755 void buf_del(float bufhandle) = #461;
3756 ========================
3758 void VM_buf_del (void)
3760 prvm_stringbuffer_t *stringbuffer;
3761 VM_SAFEPARMCOUNT(1, VM_buf_del);
3762 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
3766 for (i = 0;i < stringbuffer->num_strings;i++)
3767 if (stringbuffer->strings[i])
3768 Mem_Free(stringbuffer->strings[i]);
3769 if (stringbuffer->strings)
3770 Mem_Free(stringbuffer->strings);
3771 Mem_ExpandableArray_FreeRecord(&prog->stringbuffersarray, stringbuffer);
3775 VM_Warning("VM_buf_del: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3781 ========================
3783 how many strings are stored in buffer
3784 float buf_getsize(float bufhandle) = #462;
3785 ========================
3787 void VM_buf_getsize (void)
3789 prvm_stringbuffer_t *stringbuffer;
3790 VM_SAFEPARMCOUNT(1, VM_buf_getsize);
3792 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
3795 PRVM_G_FLOAT(OFS_RETURN) = -1;
3796 VM_Warning("VM_buf_getsize: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3800 PRVM_G_FLOAT(OFS_RETURN) = stringbuffer->num_strings;
3804 ========================
3806 copy all content from one buffer to another, make sure it exists
3807 void buf_copy(float bufhandle_from, float bufhandle_to) = #463;
3808 ========================
3810 void VM_buf_copy (void)
3812 prvm_stringbuffer_t *srcstringbuffer, *dststringbuffer;
3814 VM_SAFEPARMCOUNT(2, VM_buf_copy);
3816 srcstringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
3817 if(!srcstringbuffer)
3819 VM_Warning("VM_buf_copy: invalid source buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3822 i = (int)PRVM_G_FLOAT(OFS_PARM1);
3823 if(i == (int)PRVM_G_FLOAT(OFS_PARM0))
3825 VM_Warning("VM_buf_copy: source == destination (%i) in %s\n", i, PRVM_NAME);
3828 dststringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
3829 if(!dststringbuffer)
3831 VM_Warning("VM_buf_copy: invalid destination buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM1), PRVM_NAME);
3835 for (i = 0;i < dststringbuffer->num_strings;i++)
3836 if (dststringbuffer->strings[i])
3837 Mem_Free(dststringbuffer->strings[i]);
3838 if (dststringbuffer->strings)
3839 Mem_Free(dststringbuffer->strings);
3840 *dststringbuffer = *srcstringbuffer;
3841 if (dststringbuffer->max_strings)
3842 dststringbuffer->strings = (char **)Mem_Alloc(prog->progs_mempool, sizeof(dststringbuffer->strings[0]) * dststringbuffer->max_strings);
3844 for (i = 0;i < dststringbuffer->num_strings;i++)
3846 if (srcstringbuffer->strings[i])
3849 stringlen = strlen(srcstringbuffer->strings[i]) + 1;
3850 dststringbuffer->strings[i] = (char *)Mem_Alloc(prog->progs_mempool, stringlen);
3851 memcpy(dststringbuffer->strings[i], srcstringbuffer->strings[i], stringlen);
3857 ========================
3859 sort buffer by beginnings of strings (cmplength defaults it's length)
3860 "backward == TRUE" means that sorting goes upside-down
3861 void buf_sort(float bufhandle, float cmplength, float backward) = #464;
3862 ========================
3864 void VM_buf_sort (void)
3866 prvm_stringbuffer_t *stringbuffer;
3867 VM_SAFEPARMCOUNT(3, VM_buf_sort);
3869 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
3872 VM_Warning("VM_buf_sort: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3875 if(stringbuffer->num_strings <= 0)
3877 VM_Warning("VM_buf_sort: tried to sort empty buffer %i in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3880 stringbuffers_sortlength = (int)PRVM_G_FLOAT(OFS_PARM1);
3881 if(stringbuffers_sortlength <= 0)
3882 stringbuffers_sortlength = 0x7FFFFFFF;
3884 if(!PRVM_G_FLOAT(OFS_PARM2))
3885 qsort(stringbuffer->strings, stringbuffer->num_strings, sizeof(char*), BufStr_SortStringsUP);
3887 qsort(stringbuffer->strings, stringbuffer->num_strings, sizeof(char*), BufStr_SortStringsDOWN);
3889 BufStr_Shrink(stringbuffer);
3893 ========================
3895 concantenates all buffer string into one with "glue" separator and returns it as tempstring
3896 string buf_implode(float bufhandle, string glue) = #465;
3897 ========================
3899 void VM_buf_implode (void)
3901 prvm_stringbuffer_t *stringbuffer;
3902 char k[VM_STRINGTEMP_LENGTH];
3906 VM_SAFEPARMCOUNT(2, VM_buf_implode);
3908 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
3909 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
3912 VM_Warning("VM_buf_implode: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3915 if(!stringbuffer->num_strings)
3917 sep = PRVM_G_STRING(OFS_PARM1);
3919 for(l = i = 0;i < stringbuffer->num_strings;i++)
3921 if(stringbuffer->strings[i])
3923 l += (i > 0 ? strlen(sep) : 0) + strlen(stringbuffer->strings[i]);
3924 if (l >= sizeof(k) - 1)
3926 strlcat(k, sep, sizeof(k));
3927 strlcat(k, stringbuffer->strings[i], sizeof(k));
3930 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(k);
3934 ========================
3936 get a string from buffer, returns tempstring, dont str_unzone it!
3937 string bufstr_get(float bufhandle, float string_index) = #465;
3938 ========================
3940 void VM_bufstr_get (void)
3942 prvm_stringbuffer_t *stringbuffer;
3944 VM_SAFEPARMCOUNT(2, VM_bufstr_get);
3946 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
3947 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
3950 VM_Warning("VM_bufstr_get: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3953 strindex = (int)PRVM_G_FLOAT(OFS_PARM1);
3956 VM_Warning("VM_bufstr_get: invalid string index %i used in %s\n", strindex, PRVM_NAME);
3959 if (strindex < stringbuffer->num_strings && stringbuffer->strings[strindex])
3960 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(stringbuffer->strings[strindex]);
3964 ========================
3966 copies a string into selected slot of buffer
3967 void bufstr_set(float bufhandle, float string_index, string str) = #466;
3968 ========================
3970 void VM_bufstr_set (void)
3973 prvm_stringbuffer_t *stringbuffer;
3976 VM_SAFEPARMCOUNT(3, VM_bufstr_set);
3978 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
3981 VM_Warning("VM_bufstr_set: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3984 strindex = (int)PRVM_G_FLOAT(OFS_PARM1);
3985 if(strindex < 0 || strindex >= 1000000) // huge number of strings
3987 VM_Warning("VM_bufstr_set: invalid string index %i used in %s\n", strindex, PRVM_NAME);
3991 BufStr_Expand(stringbuffer, strindex);
3992 stringbuffer->num_strings = max(stringbuffer->num_strings, strindex + 1);
3994 if(stringbuffer->strings[strindex])
3995 Mem_Free(stringbuffer->strings[strindex]);
3996 stringbuffer->strings[strindex] = NULL;
3998 news = PRVM_G_STRING(OFS_PARM2);
3999 if (news && news[0])
4001 size_t alloclen = strlen(news) + 1;
4002 stringbuffer->strings[strindex] = (char *)Mem_Alloc(prog->progs_mempool, alloclen);
4003 memcpy(stringbuffer->strings[strindex], news, alloclen);
4006 BufStr_Shrink(stringbuffer);
4010 ========================
4012 adds string to buffer in first free slot and returns its index
4013 "order == TRUE" means that string will be added after last "full" slot
4014 float bufstr_add(float bufhandle, string str, float order) = #467;
4015 ========================
4017 void VM_bufstr_add (void)
4019 int order, strindex;
4020 prvm_stringbuffer_t *stringbuffer;
4024 VM_SAFEPARMCOUNT(3, VM_bufstr_add);
4026 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4027 PRVM_G_FLOAT(OFS_RETURN) = -1;
4030 VM_Warning("VM_bufstr_add: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4033 string = PRVM_G_STRING(OFS_PARM1);
4034 if(!string || !string[0])
4036 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);
4039 order = (int)PRVM_G_FLOAT(OFS_PARM2);
4041 strindex = stringbuffer->num_strings;
4043 for (strindex = 0;strindex < stringbuffer->num_strings;strindex++)
4044 if (stringbuffer->strings[strindex] == NULL)
4047 BufStr_Expand(stringbuffer, strindex);
4049 stringbuffer->num_strings = max(stringbuffer->num_strings, strindex + 1);
4050 alloclen = strlen(string) + 1;
4051 stringbuffer->strings[strindex] = (char *)Mem_Alloc(prog->progs_mempool, alloclen);
4052 memcpy(stringbuffer->strings[strindex], string, alloclen);
4054 PRVM_G_FLOAT(OFS_RETURN) = strindex;
4058 ========================
4060 delete string from buffer
4061 void bufstr_free(float bufhandle, float string_index) = #468;
4062 ========================
4064 void VM_bufstr_free (void)
4067 prvm_stringbuffer_t *stringbuffer;
4068 VM_SAFEPARMCOUNT(2, VM_bufstr_free);
4070 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4073 VM_Warning("VM_bufstr_free: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4076 i = (int)PRVM_G_FLOAT(OFS_PARM1);
4079 VM_Warning("VM_bufstr_free: invalid string index %i used in %s\n", i, PRVM_NAME);
4083 if (i < stringbuffer->num_strings)
4085 if(stringbuffer->strings[i])
4086 Mem_Free(stringbuffer->strings[i]);
4087 stringbuffer->strings[i] = NULL;
4090 BufStr_Shrink(stringbuffer);
4099 This was a major timewaster in progs, so it was converted to C
4102 void VM_changeyaw (void)
4105 float ideal, current, move, speed;
4107 // this is called (VERY HACKISHLY) by SV_MoveToGoal, so it can not use any
4108 // parameters because they are the parameters to SV_MoveToGoal, not this
4109 //VM_SAFEPARMCOUNT(0, VM_changeyaw);
4111 ent = PRVM_PROG_TO_EDICT(PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict);
4112 if (ent == prog->edicts)
4114 VM_Warning("changeyaw: can not modify world entity\n");
4117 if (ent->priv.server->free)
4119 VM_Warning("changeyaw: can not modify free entity\n");
4122 if (prog->fieldoffsets.angles < 0 || prog->fieldoffsets.ideal_yaw < 0 || prog->fieldoffsets.yaw_speed < 0)
4124 VM_Warning("changeyaw: angles, ideal_yaw, or yaw_speed field(s) not found\n");
4127 current = ANGLEMOD(PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.angles)->vector[1]);
4128 ideal = PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.ideal_yaw)->_float;
4129 speed = PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.yaw_speed)->_float;
4131 if (current == ideal)
4133 move = ideal - current;
4134 if (ideal > current)
4155 PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.angles)->vector[1] = ANGLEMOD (current + move);
4163 void VM_changepitch (void)
4166 float ideal, current, move, speed;
4168 VM_SAFEPARMCOUNT(1, VM_changepitch);
4170 ent = PRVM_G_EDICT(OFS_PARM0);
4171 if (ent == prog->edicts)
4173 VM_Warning("changepitch: can not modify world entity\n");
4176 if (ent->priv.server->free)
4178 VM_Warning("changepitch: can not modify free entity\n");
4181 if (prog->fieldoffsets.angles < 0 || prog->fieldoffsets.idealpitch < 0 || prog->fieldoffsets.pitch_speed < 0)
4183 VM_Warning("changepitch: angles, idealpitch, or pitch_speed field(s) not found\n");
4186 current = ANGLEMOD(PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.angles)->vector[0]);
4187 ideal = PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.idealpitch)->_float;
4188 speed = PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.pitch_speed)->_float;
4190 if (current == ideal)
4192 move = ideal - current;
4193 if (ideal > current)
4214 PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.angles)->vector[0] = ANGLEMOD (current + move);
4217 // TODO: adapt all static function names to use a single naming convention... [12/3/2007 Black]
4218 static int Is_Text_Color (char c, char t)
4221 char c2 = c - (c & 128);
4222 char t2 = t - (t & 128);
4224 if(c != STRING_COLOR_TAG && c2 != STRING_COLOR_TAG) return 0;
4225 if(t >= '0' && t <= '9') a = 1;
4226 if(t2 >= '0' && t2 <= '9') a = 1;
4227 /* if(t >= 'A' && t <= 'Z') a = 2;
4228 if(t2 >= 'A' && t2 <= 'Z') a = 2;
4230 if(a == 1 && scr_colortext.integer > 0)
4232 if(a == 2 && scr_multifonts.integer > 0)
4238 void VM_uncolorstring (void)
4241 char out[VM_STRINGTEMP_LENGTH];
4244 VM_SAFEPARMCOUNT(1, VM_uncolorstring);
4245 in = PRVM_G_STRING(OFS_PARM0);
4246 VM_CheckEmptyString (in);
4251 if(Is_Text_Color(in[k], in[k+1]) == 1/* || (in[k] == '&' && in[k+1] == 'r')*/)
4260 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(out);
4263 // #221 float(string str, string sub[, float startpos]) strstrofs (FTE_STRINGS)
4264 //strstr, without generating a new string. Use in conjunction with FRIK_FILE's substring for more similar strstr.
4265 void VM_strstrofs (void)
4267 const char *instr, *match;
4269 VM_SAFEPARMCOUNTRANGE(2, 3, VM_strstrofs);
4270 instr = PRVM_G_STRING(OFS_PARM0);
4271 match = PRVM_G_STRING(OFS_PARM1);
4272 firstofs = (prog->argc > 2)?PRVM_G_FLOAT(OFS_PARM2):0;
4274 if (firstofs && (firstofs < 0 || firstofs > (int)strlen(instr)))
4276 PRVM_G_FLOAT(OFS_RETURN) = -1;
4280 match = strstr(instr+firstofs, match);
4282 PRVM_G_FLOAT(OFS_RETURN) = -1;
4284 PRVM_G_FLOAT(OFS_RETURN) = match - instr;
4287 //#222 string(string s, float index) str2chr (FTE_STRINGS)
4288 void VM_str2chr (void)
4291 VM_SAFEPARMCOUNT(2, VM_str2chr);
4292 s = PRVM_G_STRING(OFS_PARM0);
4293 if((unsigned)PRVM_G_FLOAT(OFS_PARM1) < strlen(s))
4294 PRVM_G_FLOAT(OFS_RETURN) = (unsigned char)s[(unsigned)PRVM_G_FLOAT(OFS_PARM1)];
4296 PRVM_G_FLOAT(OFS_RETURN) = 0;
4299 //#223 string(float c, ...) chr2str (FTE_STRINGS)
4300 void VM_chr2str (void)
4304 VM_SAFEPARMCOUNTRANGE(0, 8, VM_chr2str);
4305 for(i = 0;i < prog->argc && i < (int)sizeof(t) - 1;i++)
4306 t[i] = (unsigned char)PRVM_G_FLOAT(OFS_PARM0+i*3);
4308 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(t);
4311 static int chrconv_number(int i, int base, int conv)
4336 static int chrconv_punct(int i, int base, int conv)
4354 static int chrchar_alpha(int i, int basec, int baset, int convc, int convt, int charnum)
4356 //convert case and colour seperatly...
4373 baset = 128*((charnum&1) == (convt-5));
4389 return i + basec + baset;
4391 // #224 string(float ccase, float calpha, float cnum, string s, ...) strconv (FTE_STRINGS)
4392 //bulk convert a string. change case or colouring.
4393 void VM_strconv (void)
4395 int ccase, redalpha, rednum, len, i;
4396 unsigned char resbuf[VM_STRINGTEMP_LENGTH];
4397 unsigned char *result = resbuf;
4399 VM_SAFEPARMCOUNTRANGE(3, 8, VM_strconv);
4401 ccase = PRVM_G_FLOAT(OFS_PARM0); //0 same, 1 lower, 2 upper
4402 redalpha = PRVM_G_FLOAT(OFS_PARM1); //0 same, 1 white, 2 red, 5 alternate, 6 alternate-alternate
4403 rednum = PRVM_G_FLOAT(OFS_PARM2); //0 same, 1 white, 2 red, 3 redspecial, 4 whitespecial, 5 alternate, 6 alternate-alternate
4404 VM_VarString(3, (char *) resbuf, sizeof(resbuf));
4405 len = strlen((char *) resbuf);
4407 for (i = 0; i < len; i++, result++) //should this be done backwards?
4409 if (*result >= '0' && *result <= '9') //normal numbers...
4410 *result = chrconv_number(*result, '0', rednum);
4411 else if (*result >= '0'+128 && *result <= '9'+128)
4412 *result = chrconv_number(*result, '0'+128, rednum);
4413 else if (*result >= '0'+128-30 && *result <= '9'+128-30)
4414 *result = chrconv_number(*result, '0'+128-30, rednum);
4415 else if (*result >= '0'-30 && *result <= '9'-30)
4416 *result = chrconv_number(*result, '0'-30, rednum);
4418 else if (*result >= 'a' && *result <= 'z') //normal numbers...
4419 *result = chrchar_alpha(*result, 'a', 0, ccase, redalpha, i);
4420 else if (*result >= 'A' && *result <= 'Z') //normal numbers...
4421 *result = chrchar_alpha(*result, 'A', 0, ccase, redalpha, i);
4422 else if (*result >= 'a'+128 && *result <= 'z'+128) //normal numbers...
4423 *result = chrchar_alpha(*result, 'a', 128, ccase, redalpha, i);
4424 else if (*result >= 'A'+128 && *result <= 'Z'+128) //normal numbers...
4425 *result = chrchar_alpha(*result, 'A', 128, ccase, redalpha, i);
4427 else if ((*result & 127) < 16 || !redalpha) //special chars..
4429 else if (*result < 128)
4430 *result = chrconv_punct(*result, 0, redalpha);
4432 *result = chrconv_punct(*result, 128, redalpha);
4436 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString((char *) resbuf);
4439 // #225 string(float chars, string s, ...) strpad (FTE_STRINGS)
4440 void VM_strpad (void)
4442 char src[VM_STRINGTEMP_LENGTH];
4443 char destbuf[VM_STRINGTEMP_LENGTH];
4445 VM_SAFEPARMCOUNTRANGE(1, 8, VM_strpad);
4446 pad = PRVM_G_FLOAT(OFS_PARM0);
4447 VM_VarString(1, src, sizeof(src));
4449 // note: < 0 = left padding, > 0 = right padding,
4450 // this is reverse logic of printf!
4451 dpsnprintf(destbuf, sizeof(destbuf), "%*s", -pad, src);
4453 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(destbuf);
4456 // #226 string(string info, string key, string value, ...) infoadd (FTE_STRINGS)
4457 //uses qw style \key\value strings
4458 void VM_infoadd (void)
4460 const char *info, *key;
4461 char value[VM_STRINGTEMP_LENGTH];
4462 char temp[VM_STRINGTEMP_LENGTH];
4464 VM_SAFEPARMCOUNTRANGE(2, 8, VM_infoadd);
4465 info = PRVM_G_STRING(OFS_PARM0);
4466 key = PRVM_G_STRING(OFS_PARM1);
4467 VM_VarString(2, value, sizeof(value));
4469 strlcpy(temp, info, VM_STRINGTEMP_LENGTH);
4471 InfoString_SetValue(temp, VM_STRINGTEMP_LENGTH, key, value);
4473 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(temp);
4476 // #227 string(string info, string key) infoget (FTE_STRINGS)
4477 //uses qw style \key\value strings
4478 void VM_infoget (void)
4482 char value[VM_STRINGTEMP_LENGTH];
4484 VM_SAFEPARMCOUNT(2, VM_infoget);
4485 info = PRVM_G_STRING(OFS_PARM0);
4486 key = PRVM_G_STRING(OFS_PARM1);
4488 InfoString_GetValue(info, key, value, VM_STRINGTEMP_LENGTH);
4490 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(value);
4493 //#228 float(string s1, string s2, float len) strncmp (FTE_STRINGS)
4494 // also float(string s1, string s2) strcmp (FRIK_FILE)
4495 void VM_strncmp (void)
4497 const char *s1, *s2;
4498 VM_SAFEPARMCOUNTRANGE(2, 3, VM_strncmp);
4499 s1 = PRVM_G_STRING(OFS_PARM0);
4500 s2 = PRVM_G_STRING(OFS_PARM1);
4503 PRVM_G_FLOAT(OFS_RETURN) = strncmp(s1, s2, (size_t)PRVM_G_FLOAT(OFS_PARM2));
4507 PRVM_G_FLOAT(OFS_RETURN) = strcmp(s1, s2);
4511 // #229 float(string s1, string s2) strcasecmp (FTE_STRINGS)
4512 // #230 float(string s1, string s2, float len) strncasecmp (FTE_STRINGS)
4513 void VM_strncasecmp (void)
4515 const char *s1, *s2;
4516 VM_SAFEPARMCOUNTRANGE(2, 3, VM_strncasecmp);
4517 s1 = PRVM_G_STRING(OFS_PARM0);
4518 s2 = PRVM_G_STRING(OFS_PARM1);
4521 PRVM_G_FLOAT(OFS_RETURN) = strncasecmp(s1, s2, (size_t)PRVM_G_FLOAT(OFS_PARM2));
4525 PRVM_G_FLOAT(OFS_RETURN) = strcasecmp(s1, s2);
4529 // #494 float(float caseinsensitive, string s, ...) crc16
4533 static char s[VM_STRINGTEMP_LENGTH];
4534 VM_SAFEPARMCOUNTRANGE(2, 8, VM_hash);
4535 insensitive = PRVM_G_FLOAT(OFS_PARM0);
4536 VM_VarString(1, s, sizeof(s));
4537 PRVM_G_FLOAT(OFS_RETURN) = (unsigned short) ((insensitive ? CRC_Block_CaseInsensitive : CRC_Block) ((unsigned char *) s, strlen(s)));
4540 void VM_wasfreed (void)
4542 VM_SAFEPARMCOUNT(1, VM_wasfreed);
4543 PRVM_G_FLOAT(OFS_RETURN) = PRVM_G_EDICT(OFS_PARM0)->priv.required->free;
4546 void VM_SetTraceGlobals(const trace_t *trace)
4549 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_allsolid)))
4550 val->_float = trace->allsolid;
4551 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_startsolid)))
4552 val->_float = trace->startsolid;
4553 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_fraction)))
4554 val->_float = trace->fraction;
4555 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_inwater)))
4556 val->_float = trace->inwater;
4557 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_inopen)))
4558 val->_float = trace->inopen;
4559 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_endpos)))
4560 VectorCopy(trace->endpos, val->vector);
4561 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_plane_normal)))
4562 VectorCopy(trace->plane.normal, val->vector);
4563 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_plane_dist)))
4564 val->_float = trace->plane.dist;
4565 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_ent)))
4566 val->edict = PRVM_EDICT_TO_PROG(trace->ent ? trace->ent : prog->edicts);
4567 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dpstartcontents)))
4568 val->_float = trace->startsupercontents;
4569 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dphitcontents)))
4570 val->_float = trace->hitsupercontents;
4571 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dphitq3surfaceflags)))
4572 val->_float = trace->hitq3surfaceflags;
4573 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dphittexturename)))
4574 val->string = trace->hittexture ? PRVM_SetTempString(trace->hittexture->name) : 0;
4579 void VM_Cmd_Init(void)
4581 // only init the stuff for the current prog
4585 // VM_BufStr_Init();
4588 void VM_Cmd_Reset(void)
4590 CL_PurgeOwner( MENUOWNER );
4592 VM_Files_CloseAll();
4594 // VM_BufStr_ShutDown();
4597 // #510 string(string input, ...) uri_escape (DP_QC_URI_ESCAPE)
4598 // does URI escaping on a string (replace evil stuff by %AB escapes)
4599 void VM_uri_escape (void)
4601 char src[VM_STRINGTEMP_LENGTH];
4602 char dest[VM_STRINGTEMP_LENGTH];
4604 static const char *hex = "0123456789ABCDEF";
4606 VM_SAFEPARMCOUNTRANGE(1, 8, VM_uri_escape);
4607 VM_VarString(0, src, sizeof(src));
4609 for(p = src, q = dest; *p && q < dest + sizeof(dest) - 3; ++p)
4611 if((*p >= 'A' && *p <= 'Z')
4612 || (*p >= 'a' && *p <= 'z')
4613 || (*p >= '0' && *p <= '9')
4614 || (*p == '-') || (*p == '_') || (*p == '.')
4615 || (*p == '!') || (*p == '~') || (*p == '*')
4616 || (*p == '\'') || (*p == '(') || (*p == ')'))
4621 *q++ = hex[(*(unsigned char *)p >> 4) & 0xF];
4622 *q++ = hex[ *(unsigned char *)p & 0xF];
4627 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(dest);
4630 // #510 string(string input, ...) uri_unescape (DP_QC_URI_ESCAPE)
4631 // does URI unescaping on a string (get back the evil stuff)
4632 void VM_uri_unescape (void)
4634 char src[VM_STRINGTEMP_LENGTH];
4635 char dest[VM_STRINGTEMP_LENGTH];
4639 VM_SAFEPARMCOUNTRANGE(1, 8, VM_uri_unescape);
4640 VM_VarString(0, src, sizeof(src));
4642 for(p = src, q = dest; *p; ) // no need to check size, because unescape can't expand
4646 if(p[1] >= '0' && p[1] <= '9')
4648 else if(p[1] >= 'a' && p[1] <= 'f')
4649 hi = p[1] - 'a' + 10;
4650 else if(p[1] >= 'A' && p[1] <= 'F')
4651 hi = p[1] - 'A' + 10;
4654 if(p[2] >= '0' && p[2] <= '9')
4656 else if(p[2] >= 'a' && p[2] <= 'f')
4657 lo = p[2] - 'a' + 10;
4658 else if(p[2] >= 'A' && p[2] <= 'F')
4659 lo = p[2] - 'A' + 10;
4662 if(hi != 0 || lo != 0) // don't unescape NUL bytes
4663 *q++ = (char) (hi * 0x10 + lo);
4674 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(dest);