2 // Basically every vm builtin cmd should be in here.
3 // All 3 builtin and extension lists can be found here
4 // cause large (I think they will) parts are from pr_cmds the same copyright like in pr_cmds
13 extern cvar_t prvm_backtraceforwarnings;
15 // LordHavoc: changed this to NOT use a return statement, so that it can be used in functions that must return a value
16 void VM_Warning(const char *fmt, ...)
19 char msg[MAX_INPUTLINE];
20 static double recursive = -1;
23 dpvsnprintf(msg,sizeof(msg),fmt,argptr);
28 // TODO: either add a cvar/cmd to control the state dumping or replace some of the calls with Con_Printf [9/13/2006 Black]
29 if(prvm_backtraceforwarnings.integer && recursive != realtime) // NOTE: this compares to the time, just in case if PRVM_PrintState causes a Host_Error and keeps recursive set
38 //============================================================================
41 // TODO DONE: move vm_files and vm_fssearchlist to prvm_prog_t struct
42 // TODO: move vm_files and vm_fssearchlist back [9/13/2006 Black]
43 // TODO: (move vm_files and vm_fssearchlist to prvm_prog_t struct again) [2007-01-23 LordHavoc]
44 // TODO: will this war ever end? [2007-01-23 LordHavoc]
46 void VM_CheckEmptyString (const char *s)
48 if (ISWHITESPACE(s[0]))
49 PRVM_ERROR ("%s: Bad string", PRVM_NAME);
52 //============================================================================
55 void VM_VarString(int first, char *out, int outlength)
61 outend = out + outlength - 1;
62 for (i = first;i < prog->argc && out < outend;i++)
64 s = PRVM_G_STRING((OFS_PARM0+i*3));
65 while (out < outend && *s)
75 returns true if the extension is supported by the server
77 checkextension(extensionname)
81 // kind of helper function
82 static qboolean checkextension(const char *name)
86 len = (int)strlen(name);
88 for (e = prog->extensionstring;*e;e++)
95 while (*e && *e != ' ')
97 if ((e - start) == len && !strncasecmp(start, name, len))
103 void VM_checkextension (void)
105 VM_SAFEPARMCOUNT(1,VM_checkextension);
107 PRVM_G_FLOAT(OFS_RETURN) = checkextension(PRVM_G_STRING(OFS_PARM0));
114 This is a TERMINAL error, which will kill off the entire prog.
123 char string[VM_STRINGTEMP_LENGTH];
125 VM_VarString(0, string, sizeof(string));
126 Con_Printf("======%s ERROR in %s:\n%s\n", PRVM_NAME, PRVM_GetString(prog->xfunction->s_name), string);
127 if (prog->globaloffsets.self >= 0)
129 ed = PRVM_PROG_TO_EDICT(PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict);
130 PRVM_ED_Print(ed, NULL);
133 PRVM_ERROR ("%s: Program error in function %s:\n%s\nTip: read above for entity information\n", PRVM_NAME, PRVM_GetString(prog->xfunction->s_name), string);
140 Dumps out self, then an error message. The program is aborted and self is
141 removed, but the level can continue.
146 void VM_objerror (void)
149 char string[VM_STRINGTEMP_LENGTH];
151 VM_VarString(0, string, sizeof(string));
152 Con_Printf("======OBJECT ERROR======\n"); // , PRVM_NAME, PRVM_GetString(prog->xfunction->s_name), string); // or include them? FIXME
153 if (prog->globaloffsets.self >= 0)
155 ed = PRVM_PROG_TO_EDICT(PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict);
156 PRVM_ED_Print(ed, NULL);
161 // objerror has to display the object fields -> else call
162 PRVM_ERROR ("VM_objecterror: self not defined !");
163 Con_Printf("%s OBJECT ERROR in %s:\n%s\nTip: read above for entity information\n", PRVM_NAME, PRVM_GetString(prog->xfunction->s_name), string);
177 char string[VM_STRINGTEMP_LENGTH];
179 VM_VarString(0, string, sizeof(string));
187 broadcast print to everyone on server
192 void VM_bprint (void)
194 char string[VM_STRINGTEMP_LENGTH];
198 VM_Warning("VM_bprint: game is not server(%s) !\n", PRVM_NAME);
202 VM_VarString(0, string, sizeof(string));
203 SV_BroadcastPrint(string);
208 VM_sprint (menu & client but only if server.active == true)
210 single print to a specific client
212 sprint(float clientnum,...[string])
215 void VM_sprint (void)
219 char string[VM_STRINGTEMP_LENGTH];
221 VM_SAFEPARMCOUNTRANGE(1, 8, VM_sprint);
223 //find client for this entity
224 clientnum = (int)PRVM_G_FLOAT(OFS_PARM0);
225 if (!sv.active || clientnum < 0 || clientnum >= svs.maxclients || !svs.clients[clientnum].active)
227 VM_Warning("VM_sprint: %s: invalid client or server is not active !\n", PRVM_NAME);
231 client = svs.clients + clientnum;
232 if (!client->netconnection)
235 VM_VarString(1, string, sizeof(string));
236 MSG_WriteChar(&client->netconnection->message,svc_print);
237 MSG_WriteString(&client->netconnection->message, string);
244 single print to the screen
249 void VM_centerprint (void)
251 char string[VM_STRINGTEMP_LENGTH];
253 VM_SAFEPARMCOUNTRANGE(1, 8, VM_centerprint);
254 VM_VarString(0, string, sizeof(string));
255 SCR_CenterPrint(string);
262 vector normalize(vector)
265 void VM_normalize (void)
271 VM_SAFEPARMCOUNT(1,VM_normalize);
273 value1 = PRVM_G_VECTOR(OFS_PARM0);
275 f = VectorLength2(value1);
279 VectorScale(value1, f, newvalue);
282 VectorClear(newvalue);
284 VectorCopy (newvalue, PRVM_G_VECTOR(OFS_RETURN));
296 VM_SAFEPARMCOUNT(1,VM_vlen);
297 PRVM_G_FLOAT(OFS_RETURN) = VectorLength(PRVM_G_VECTOR(OFS_PARM0));
304 float vectoyaw(vector)
307 void VM_vectoyaw (void)
312 VM_SAFEPARMCOUNT(1,VM_vectoyaw);
314 value1 = PRVM_G_VECTOR(OFS_PARM0);
316 if (value1[1] == 0 && value1[0] == 0)
320 yaw = (int) (atan2(value1[1], value1[0]) * 180 / M_PI);
325 PRVM_G_FLOAT(OFS_RETURN) = yaw;
333 vector vectoangles(vector[, vector])
336 void VM_vectoangles (void)
338 VM_SAFEPARMCOUNTRANGE(1, 2,VM_vectoangles);
340 AnglesFromVectors(PRVM_G_VECTOR(OFS_RETURN), PRVM_G_VECTOR(OFS_PARM0), prog->argc >= 2 ? PRVM_G_VECTOR(OFS_PARM1) : NULL, true);
347 Returns a number from 0<= num < 1
352 void VM_random (void)
354 VM_SAFEPARMCOUNT(0,VM_random);
356 PRVM_G_FLOAT(OFS_RETURN) = lhrandom(0, 1);
363 localsound(string sample)
366 void VM_localsound(void)
370 VM_SAFEPARMCOUNT(1,VM_localsound);
372 s = PRVM_G_STRING(OFS_PARM0);
374 if(!S_LocalSound (s))
376 PRVM_G_FLOAT(OFS_RETURN) = -4;
377 VM_Warning("VM_localsound: Failed to play %s for %s !\n", s, PRVM_NAME);
381 PRVM_G_FLOAT(OFS_RETURN) = 1;
393 PRVM_ERROR ("%s: break statement", PRVM_NAME);
396 //============================================================================
402 Sends text over to the client's execution buffer
404 [localcmd (string, ...) or]
408 void VM_localcmd (void)
410 char string[VM_STRINGTEMP_LENGTH];
411 VM_SAFEPARMCOUNTRANGE(1, 8, VM_localcmd);
412 VM_VarString(0, string, sizeof(string));
413 Cbuf_AddText(string);
425 char string[VM_STRINGTEMP_LENGTH];
426 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar);
427 VM_VarString(0, string, sizeof(string));
428 VM_CheckEmptyString(string);
429 PRVM_G_FLOAT(OFS_RETURN) = Cvar_VariableValue(string);
436 float cvar_type (string)
437 float CVAR_TYPEFLAG_EXISTS = 1;
438 float CVAR_TYPEFLAG_SAVED = 2;
439 float CVAR_TYPEFLAG_PRIVATE = 4;
440 float CVAR_TYPEFLAG_ENGINE = 8;
441 float CVAR_TYPEFLAG_HASDESCRIPTION = 16;
444 void VM_cvar_type (void)
446 char string[VM_STRINGTEMP_LENGTH];
450 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar);
451 VM_VarString(0, string, sizeof(string));
452 VM_CheckEmptyString(string);
453 cvar = Cvar_FindVar(string);
458 PRVM_G_FLOAT(OFS_RETURN) = 0;
459 return; // CVAR_TYPE_NONE
462 ret = 1; // CVAR_EXISTS
463 if(cvar->flags & CVAR_SAVE)
464 ret |= 2; // CVAR_TYPE_SAVED
465 if(cvar->flags & CVAR_PRIVATE)
466 ret |= 4; // CVAR_TYPE_PRIVATE
467 if(!(cvar->flags & CVAR_ALLOCATED))
468 ret |= 8; // CVAR_TYPE_ENGINE
469 if(strcmp(cvar->description, "custom cvar")) // has to match Cvar_Get's placeholder string
470 ret |= 16; // CVAR_TYPE_HASDESCRIPTION
472 PRVM_G_FLOAT(OFS_RETURN) = ret;
479 const string VM_cvar_string (string, ...)
482 void VM_cvar_string(void)
484 char string[VM_STRINGTEMP_LENGTH];
485 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar_string);
486 VM_VarString(0, string, sizeof(string));
487 VM_CheckEmptyString(string);
488 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(Cvar_VariableString(string));
493 ========================
496 const string VM_cvar_defstring (string, ...)
497 ========================
499 void VM_cvar_defstring (void)
501 char string[VM_STRINGTEMP_LENGTH];
502 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar_defstring);
503 VM_VarString(0, string, sizeof(string));
504 VM_CheckEmptyString(string);
505 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(Cvar_VariableDefString(string));
511 void cvar_set (string,string, ...)
514 void VM_cvar_set (void)
517 char string[VM_STRINGTEMP_LENGTH];
518 VM_SAFEPARMCOUNTRANGE(2,8,VM_cvar_set);
519 VM_VarString(1, string, sizeof(string));
520 name = PRVM_G_STRING(OFS_PARM0);
521 VM_CheckEmptyString(name);
522 Cvar_Set(name, string);
532 void VM_dprint (void)
534 char string[VM_STRINGTEMP_LENGTH];
535 VM_SAFEPARMCOUNTRANGE(1, 8, VM_dprint);
536 if (developer.integer)
538 VM_VarString(0, string, sizeof(string));
540 Con_Printf("%s", string);
542 Con_Printf("%s: %s", PRVM_NAME, string);
560 VM_SAFEPARMCOUNT(1, VM_ftos);
562 v = PRVM_G_FLOAT(OFS_PARM0);
564 if ((float)((int)v) == v)
565 dpsnprintf(s, sizeof(s), "%i", (int)v);
567 dpsnprintf(s, sizeof(s), "%f", v);
568 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(s);
583 VM_SAFEPARMCOUNT(1,VM_fabs);
585 v = PRVM_G_FLOAT(OFS_PARM0);
586 PRVM_G_FLOAT(OFS_RETURN) = fabs(v);
601 VM_SAFEPARMCOUNT(1,VM_vtos);
603 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]);
604 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(s);
619 VM_SAFEPARMCOUNT(1, VM_etos);
621 dpsnprintf (s, sizeof(s), "entity %i", PRVM_G_EDICTNUM(OFS_PARM0));
622 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(s);
629 float stof(...[string])
634 char string[VM_STRINGTEMP_LENGTH];
635 VM_SAFEPARMCOUNTRANGE(1, 8, VM_stof);
636 VM_VarString(0, string, sizeof(string));
637 PRVM_G_FLOAT(OFS_RETURN) = atof(string);
641 ========================
645 ========================
649 VM_SAFEPARMCOUNT(1, VM_itof);
650 PRVM_G_FLOAT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
654 ========================
657 entity ftoe(float num)
658 ========================
663 VM_SAFEPARMCOUNT(1, VM_ftoe);
665 ent = (int)PRVM_G_FLOAT(OFS_PARM0);
666 if (ent < 0 || ent >= MAX_EDICTS || PRVM_PROG_TO_EDICT(ent)->priv.required->free)
667 ent = 0; // return world instead of a free or invalid entity
669 PRVM_G_INT(OFS_RETURN) = ent;
673 ========================
676 float etof(entity ent)
677 ========================
681 VM_SAFEPARMCOUNT(1, VM_etof);
682 PRVM_G_FLOAT(OFS_RETURN) = PRVM_G_EDICTNUM(OFS_PARM0);
689 string strftime(float uselocaltime, string[, string ...])
692 void VM_strftime(void)
701 char fmt[VM_STRINGTEMP_LENGTH];
702 char result[VM_STRINGTEMP_LENGTH];
703 VM_SAFEPARMCOUNTRANGE(2, 8, VM_strftime);
704 VM_VarString(1, fmt, sizeof(fmt));
707 if (PRVM_G_FLOAT(OFS_PARM0))
708 tmresult = localtime_s(&tm, &t);
710 tmresult = gmtime_s(&tm, &t);
713 if (PRVM_G_FLOAT(OFS_PARM0))
720 PRVM_G_INT(OFS_RETURN) = 0;
724 strftime(result, sizeof(result), fmt, &tm);
726 strftime(result, sizeof(result), fmt, tm);
728 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(result);
742 VM_SAFEPARMCOUNT(0, VM_spawn);
743 prog->xfunction->builtinsprofile += 20;
744 ed = PRVM_ED_Alloc();
756 void VM_remove (void)
759 prog->xfunction->builtinsprofile += 20;
761 VM_SAFEPARMCOUNT(1, VM_remove);
763 ed = PRVM_G_EDICT(OFS_PARM0);
764 if( PRVM_NUM_FOR_EDICT(ed) <= prog->reserved_edicts )
766 if (developer.integer >= 1)
767 VM_Warning( "VM_remove: tried to remove the null entity or a reserved entity!\n" );
769 else if( ed->priv.required->free )
771 if (developer.integer >= 1)
772 VM_Warning( "VM_remove: tried to remove an already freed entity!\n" );
782 entity find(entity start, .string field, string match)
793 VM_SAFEPARMCOUNT(3,VM_find);
795 e = PRVM_G_EDICTNUM(OFS_PARM0);
796 f = PRVM_G_INT(OFS_PARM1);
797 s = PRVM_G_STRING(OFS_PARM2);
799 // LordHavoc: apparently BloodMage does a find(world, weaponmodel, "") and
800 // expects it to find all the monsters, so we must be careful to support
803 for (e++ ; e < prog->num_edicts ; e++)
805 prog->xfunction->builtinsprofile++;
806 ed = PRVM_EDICT_NUM(e);
807 if (ed->priv.required->free)
809 t = PRVM_E_STRING(ed,f);
819 VM_RETURN_EDICT(prog->edicts);
826 entity findfloat(entity start, .float field, float match)
827 entity findentity(entity start, .entity field, entity match)
830 // LordHavoc: added this for searching float, int, and entity reference fields
831 void VM_findfloat (void)
838 VM_SAFEPARMCOUNT(3,VM_findfloat);
840 e = PRVM_G_EDICTNUM(OFS_PARM0);
841 f = PRVM_G_INT(OFS_PARM1);
842 s = PRVM_G_FLOAT(OFS_PARM2);
844 for (e++ ; e < prog->num_edicts ; e++)
846 prog->xfunction->builtinsprofile++;
847 ed = PRVM_EDICT_NUM(e);
848 if (ed->priv.required->free)
850 if (PRVM_E_FLOAT(ed,f) == s)
857 VM_RETURN_EDICT(prog->edicts);
864 entity findchain(.string field, string match)
867 // chained search for strings in entity fields
868 // entity(.string field, string match) findchain = #402;
869 void VM_findchain (void)
874 prvm_edict_t *ent, *chain;
876 VM_SAFEPARMCOUNT(2,VM_findchain);
878 if (prog->fieldoffsets.chain < 0)
879 PRVM_ERROR("VM_findchain: %s doesnt have a chain field !", PRVM_NAME);
881 chain = prog->edicts;
883 f = PRVM_G_INT(OFS_PARM0);
884 s = PRVM_G_STRING(OFS_PARM1);
886 // LordHavoc: apparently BloodMage does a find(world, weaponmodel, "") and
887 // expects it to find all the monsters, so we must be careful to support
890 ent = PRVM_NEXT_EDICT(prog->edicts);
891 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
893 prog->xfunction->builtinsprofile++;
894 if (ent->priv.required->free)
896 t = PRVM_E_STRING(ent,f);
902 PRVM_EDICTFIELDVALUE(ent,prog->fieldoffsets.chain)->edict = PRVM_NUM_FOR_EDICT(chain);
906 VM_RETURN_EDICT(chain);
913 entity findchainfloat(.string field, float match)
914 entity findchainentity(.string field, entity match)
917 // LordHavoc: chained search for float, int, and entity reference fields
918 // entity(.string field, float match) findchainfloat = #403;
919 void VM_findchainfloat (void)
924 prvm_edict_t *ent, *chain;
926 VM_SAFEPARMCOUNT(2, VM_findchainfloat);
928 if (prog->fieldoffsets.chain < 0)
929 PRVM_ERROR("VM_findchainfloat: %s doesnt have a chain field !", PRVM_NAME);
931 chain = (prvm_edict_t *)prog->edicts;
933 f = PRVM_G_INT(OFS_PARM0);
934 s = PRVM_G_FLOAT(OFS_PARM1);
936 ent = PRVM_NEXT_EDICT(prog->edicts);
937 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
939 prog->xfunction->builtinsprofile++;
940 if (ent->priv.required->free)
942 if (PRVM_E_FLOAT(ent,f) != s)
945 PRVM_EDICTFIELDVALUE(ent,prog->fieldoffsets.chain)->edict = PRVM_EDICT_TO_PROG(chain);
949 VM_RETURN_EDICT(chain);
953 ========================
956 entity findflags(entity start, .float field, float match)
957 ========================
959 // LordHavoc: search for flags in float fields
960 void VM_findflags (void)
967 VM_SAFEPARMCOUNT(3, VM_findflags);
970 e = PRVM_G_EDICTNUM(OFS_PARM0);
971 f = PRVM_G_INT(OFS_PARM1);
972 s = (int)PRVM_G_FLOAT(OFS_PARM2);
974 for (e++ ; e < prog->num_edicts ; e++)
976 prog->xfunction->builtinsprofile++;
977 ed = PRVM_EDICT_NUM(e);
978 if (ed->priv.required->free)
980 if (!PRVM_E_FLOAT(ed,f))
982 if ((int)PRVM_E_FLOAT(ed,f) & s)
989 VM_RETURN_EDICT(prog->edicts);
993 ========================
996 entity findchainflags(.float field, float match)
997 ========================
999 // LordHavoc: chained search for flags in float fields
1000 void VM_findchainflags (void)
1005 prvm_edict_t *ent, *chain;
1007 VM_SAFEPARMCOUNT(2, VM_findchainflags);
1009 if (prog->fieldoffsets.chain < 0)
1010 PRVM_ERROR("VM_findchainflags: %s doesnt have a chain field !", PRVM_NAME);
1012 chain = (prvm_edict_t *)prog->edicts;
1014 f = PRVM_G_INT(OFS_PARM0);
1015 s = (int)PRVM_G_FLOAT(OFS_PARM1);
1017 ent = PRVM_NEXT_EDICT(prog->edicts);
1018 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
1020 prog->xfunction->builtinsprofile++;
1021 if (ent->priv.required->free)
1023 if (!PRVM_E_FLOAT(ent,f))
1025 if (!((int)PRVM_E_FLOAT(ent,f) & s))
1028 PRVM_EDICTFIELDVALUE(ent,prog->fieldoffsets.chain)->edict = PRVM_EDICT_TO_PROG(chain);
1032 VM_RETURN_EDICT(chain);
1039 string precache_sound (string sample)
1042 void VM_precache_sound (void)
1046 VM_SAFEPARMCOUNT(1, VM_precache_sound);
1048 s = PRVM_G_STRING(OFS_PARM0);
1049 PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
1050 VM_CheckEmptyString(s);
1052 if(snd_initialized.integer && !S_PrecacheSound(s, true, false))
1054 VM_Warning("VM_precache_sound: Failed to load %s for %s\n", s, PRVM_NAME);
1063 returns the same string as output
1065 does nothing, only used by qcc to build .pak archives
1068 void VM_precache_file (void)
1070 VM_SAFEPARMCOUNT(1,VM_precache_file);
1071 // precache_file is only used to copy files with qcc, it does nothing
1072 PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
1082 void VM_coredump (void)
1084 VM_SAFEPARMCOUNT(0,VM_coredump);
1086 Cbuf_AddText("prvm_edicts ");
1087 Cbuf_AddText(PRVM_NAME);
1098 void PRVM_StackTrace(void);
1099 void VM_stackdump (void)
1101 VM_SAFEPARMCOUNT(0, VM_stackdump);
1116 VM_SAFEPARMCOUNT(0, VM_crash);
1118 PRVM_ERROR("Crash called by %s",PRVM_NAME);
1128 void VM_traceon (void)
1130 VM_SAFEPARMCOUNT(0,VM_traceon);
1142 void VM_traceoff (void)
1144 VM_SAFEPARMCOUNT(0,VM_traceoff);
1146 prog->trace = false;
1156 void VM_eprint (void)
1158 VM_SAFEPARMCOUNT(1,VM_eprint);
1160 PRVM_ED_PrintNum (PRVM_G_EDICTNUM(OFS_PARM0), NULL);
1173 VM_SAFEPARMCOUNT(1,VM_rint);
1175 f = PRVM_G_FLOAT(OFS_PARM0);
1177 PRVM_G_FLOAT(OFS_RETURN) = floor(f + 0.5);
1179 PRVM_G_FLOAT(OFS_RETURN) = ceil(f - 0.5);
1189 void VM_floor (void)
1191 VM_SAFEPARMCOUNT(1,VM_floor);
1193 PRVM_G_FLOAT(OFS_RETURN) = floor(PRVM_G_FLOAT(OFS_PARM0));
1205 VM_SAFEPARMCOUNT(1,VM_ceil);
1207 PRVM_G_FLOAT(OFS_RETURN) = ceil(PRVM_G_FLOAT(OFS_PARM0));
1215 entity nextent(entity)
1218 void VM_nextent (void)
1223 VM_SAFEPARMCOUNT(1, VM_nextent);
1225 i = PRVM_G_EDICTNUM(OFS_PARM0);
1228 prog->xfunction->builtinsprofile++;
1230 if (i == prog->num_edicts)
1232 VM_RETURN_EDICT(prog->edicts);
1235 ent = PRVM_EDICT_NUM(i);
1236 if (!ent->priv.required->free)
1238 VM_RETURN_EDICT(ent);
1244 //=============================================================================
1251 changelevel(string map)
1254 void VM_changelevel (void)
1256 VM_SAFEPARMCOUNT(1, VM_changelevel);
1260 VM_Warning("VM_changelevel: game is not server (%s)\n", PRVM_NAME);
1264 // make sure we don't issue two changelevels
1265 if (svs.changelevel_issued)
1267 svs.changelevel_issued = true;
1269 Cbuf_AddText (va("changelevel %s\n",PRVM_G_STRING(OFS_PARM0)));
1281 VM_SAFEPARMCOUNT(1,VM_sin);
1282 PRVM_G_FLOAT(OFS_RETURN) = sin(PRVM_G_FLOAT(OFS_PARM0));
1293 VM_SAFEPARMCOUNT(1,VM_cos);
1294 PRVM_G_FLOAT(OFS_RETURN) = cos(PRVM_G_FLOAT(OFS_PARM0));
1306 VM_SAFEPARMCOUNT(1,VM_sqrt);
1307 PRVM_G_FLOAT(OFS_RETURN) = sqrt(PRVM_G_FLOAT(OFS_PARM0));
1319 VM_SAFEPARMCOUNT(1,VM_asin);
1320 PRVM_G_FLOAT(OFS_RETURN) = asin(PRVM_G_FLOAT(OFS_PARM0));
1331 VM_SAFEPARMCOUNT(1,VM_acos);
1332 PRVM_G_FLOAT(OFS_RETURN) = acos(PRVM_G_FLOAT(OFS_PARM0));
1343 VM_SAFEPARMCOUNT(1,VM_atan);
1344 PRVM_G_FLOAT(OFS_RETURN) = atan(PRVM_G_FLOAT(OFS_PARM0));
1350 float atan2(float,float)
1353 void VM_atan2 (void)
1355 VM_SAFEPARMCOUNT(2,VM_atan2);
1356 PRVM_G_FLOAT(OFS_RETURN) = atan2(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1367 VM_SAFEPARMCOUNT(1,VM_tan);
1368 PRVM_G_FLOAT(OFS_RETURN) = tan(PRVM_G_FLOAT(OFS_PARM0));
1375 Returns a vector of length < 1 and > 0
1380 void VM_randomvec (void)
1385 VM_SAFEPARMCOUNT(0, VM_randomvec);
1390 temp[0] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1391 temp[1] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1392 temp[2] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1394 while (DotProduct(temp, temp) >= 1);
1395 VectorCopy (temp, PRVM_G_VECTOR(OFS_RETURN));
1398 temp[0] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1399 temp[1] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1400 temp[2] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1401 // length returned always > 0
1402 length = (rand()&32766 + 1) * (1.0 / 32767.0) / VectorLength(temp);
1403 VectorScale(temp,length, temp);*/
1404 //VectorCopy(temp, PRVM_G_VECTOR(OFS_RETURN));
1407 //=============================================================================
1413 float registercvar (string name, string value[, float flags])
1416 void VM_registercvar (void)
1418 const char *name, *value;
1421 VM_SAFEPARMCOUNTRANGE(2, 3, VM_registercvar);
1423 name = PRVM_G_STRING(OFS_PARM0);
1424 value = PRVM_G_STRING(OFS_PARM1);
1425 flags = prog->argc >= 3 ? (int)PRVM_G_FLOAT(OFS_PARM2) : 0;
1426 PRVM_G_FLOAT(OFS_RETURN) = 0;
1428 if(flags > CVAR_MAXFLAGSVAL)
1431 // first check to see if it has already been defined
1432 if (Cvar_FindVar (name))
1435 // check for overlap with a command
1436 if (Cmd_Exists (name))
1438 VM_Warning("VM_registercvar: %s is a command\n", name);
1442 Cvar_Get(name, value, flags);
1444 PRVM_G_FLOAT(OFS_RETURN) = 1; // success
1452 returns the minimum of two supplied floats
1454 float min(float a, float b, ...[float])
1459 VM_SAFEPARMCOUNTRANGE(2, 8, VM_min);
1460 // LordHavoc: 3+ argument enhancement suggested by FrikaC
1461 if (prog->argc >= 3)
1464 float f = PRVM_G_FLOAT(OFS_PARM0);
1465 for (i = 1;i < prog->argc;i++)
1466 if (f > PRVM_G_FLOAT((OFS_PARM0+i*3)))
1467 f = PRVM_G_FLOAT((OFS_PARM0+i*3));
1468 PRVM_G_FLOAT(OFS_RETURN) = f;
1471 PRVM_G_FLOAT(OFS_RETURN) = min(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1478 returns the maximum of two supplied floats
1480 float max(float a, float b, ...[float])
1485 VM_SAFEPARMCOUNTRANGE(2, 8, VM_max);
1486 // LordHavoc: 3+ argument enhancement suggested by FrikaC
1487 if (prog->argc >= 3)
1490 float f = PRVM_G_FLOAT(OFS_PARM0);
1491 for (i = 1;i < prog->argc;i++)
1492 if (f < PRVM_G_FLOAT((OFS_PARM0+i*3)))
1493 f = PRVM_G_FLOAT((OFS_PARM0+i*3));
1494 PRVM_G_FLOAT(OFS_RETURN) = f;
1497 PRVM_G_FLOAT(OFS_RETURN) = max(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1504 returns number bounded by supplied range
1506 float bound(float min, float value, float max)
1509 void VM_bound (void)
1511 VM_SAFEPARMCOUNT(3,VM_bound);
1512 PRVM_G_FLOAT(OFS_RETURN) = bound(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1), PRVM_G_FLOAT(OFS_PARM2));
1519 returns a raised to power b
1521 float pow(float a, float b)
1526 VM_SAFEPARMCOUNT(2,VM_pow);
1527 PRVM_G_FLOAT(OFS_RETURN) = pow(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1530 void VM_Files_Init(void)
1533 for (i = 0;i < PRVM_MAX_OPENFILES;i++)
1534 prog->openfiles[i] = NULL;
1537 void VM_Files_CloseAll(void)
1540 for (i = 0;i < PRVM_MAX_OPENFILES;i++)
1542 if (prog->openfiles[i])
1543 FS_Close(prog->openfiles[i]);
1544 prog->openfiles[i] = NULL;
1548 static qfile_t *VM_GetFileHandle( int index )
1550 if (index < 0 || index >= PRVM_MAX_OPENFILES)
1552 Con_Printf("VM_GetFileHandle: invalid file handle %i used in %s\n", index, PRVM_NAME);
1555 if (prog->openfiles[index] == NULL)
1557 Con_Printf("VM_GetFileHandle: no such file handle %i (or file has been closed) in %s\n", index, PRVM_NAME);
1560 return prog->openfiles[index];
1567 float fopen(string filename, float mode)
1570 // float(string filename, float mode) fopen = #110;
1571 // opens a file inside quake/gamedir/data/ (mode is FILE_READ, FILE_APPEND, or FILE_WRITE),
1572 // returns fhandle >= 0 if successful, or fhandle < 0 if unable to open file for any reason
1576 const char *modestring, *filename;
1578 VM_SAFEPARMCOUNT(2,VM_fopen);
1580 for (filenum = 0;filenum < PRVM_MAX_OPENFILES;filenum++)
1581 if (prog->openfiles[filenum] == NULL)
1583 if (filenum >= PRVM_MAX_OPENFILES)
1585 PRVM_G_FLOAT(OFS_RETURN) = -2;
1586 VM_Warning("VM_fopen: %s ran out of file handles (%i)\n", PRVM_NAME, PRVM_MAX_OPENFILES);
1589 filename = PRVM_G_STRING(OFS_PARM0);
1590 mode = (int)PRVM_G_FLOAT(OFS_PARM1);
1593 case 0: // FILE_READ
1595 prog->openfiles[filenum] = FS_OpenVirtualFile(va("data/%s", filename), false);
1596 if (prog->openfiles[filenum] == NULL)
1597 prog->openfiles[filenum] = FS_OpenVirtualFile(va("%s", filename), false);
1599 case 1: // FILE_APPEND
1601 prog->openfiles[filenum] = FS_OpenRealFile(va("data/%s", filename), modestring, false);
1603 case 2: // FILE_WRITE
1605 prog->openfiles[filenum] = FS_OpenRealFile(va("data/%s", filename), modestring, false);
1608 PRVM_G_FLOAT(OFS_RETURN) = -3;
1609 VM_Warning("VM_fopen: %s: no such mode %i (valid: 0 = read, 1 = append, 2 = write)\n", PRVM_NAME, mode);
1613 if (prog->openfiles[filenum] == NULL)
1615 PRVM_G_FLOAT(OFS_RETURN) = -1;
1616 if (developer.integer >= 100)
1617 VM_Warning("VM_fopen: %s: %s mode %s failed\n", PRVM_NAME, filename, modestring);
1621 PRVM_G_FLOAT(OFS_RETURN) = filenum;
1622 if (developer.integer >= 100)
1623 Con_Printf("VM_fopen: %s: %s mode %s opened as #%i\n", PRVM_NAME, filename, modestring, filenum);
1624 prog->openfiles_origin[filenum] = PRVM_AllocationOrigin();
1632 fclose(float fhandle)
1635 //void(float fhandle) fclose = #111; // closes a file
1636 void VM_fclose(void)
1640 VM_SAFEPARMCOUNT(1,VM_fclose);
1642 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1643 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
1645 VM_Warning("VM_fclose: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1648 if (prog->openfiles[filenum] == NULL)
1650 VM_Warning("VM_fclose: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1653 FS_Close(prog->openfiles[filenum]);
1654 prog->openfiles[filenum] = NULL;
1655 if(prog->openfiles_origin[filenum])
1656 PRVM_Free((char *)prog->openfiles_origin[filenum]);
1657 if (developer.integer >= 100)
1658 Con_Printf("VM_fclose: %s: #%i closed\n", PRVM_NAME, filenum);
1665 string fgets(float fhandle)
1668 //string(float fhandle) fgets = #112; // reads a line of text from the file and returns as a tempstring
1672 char string[VM_STRINGTEMP_LENGTH];
1675 VM_SAFEPARMCOUNT(1,VM_fgets);
1677 // set the return value regardless of any possible errors
1678 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
1680 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1681 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
1683 VM_Warning("VM_fgets: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1686 if (prog->openfiles[filenum] == NULL)
1688 VM_Warning("VM_fgets: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1694 c = FS_Getc(prog->openfiles[filenum]);
1695 if (c == '\r' || c == '\n' || c < 0)
1697 if (end < VM_STRINGTEMP_LENGTH - 1)
1701 // remove \n following \r
1704 c = FS_Getc(prog->openfiles[filenum]);
1706 FS_UnGetc(prog->openfiles[filenum], (unsigned char)c);
1708 if (developer.integer >= 100)
1709 Con_Printf("fgets: %s: %s\n", PRVM_NAME, string);
1711 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string);
1718 fputs(float fhandle, string s)
1721 //void(float fhandle, string s) fputs = #113; // writes a line of text to the end of the file
1725 char string[VM_STRINGTEMP_LENGTH];
1728 VM_SAFEPARMCOUNT(2,VM_fputs);
1730 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1731 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
1733 VM_Warning("VM_fputs: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1736 if (prog->openfiles[filenum] == NULL)
1738 VM_Warning("VM_fputs: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1741 VM_VarString(1, string, sizeof(string));
1742 if ((stringlength = (int)strlen(string)))
1743 FS_Write(prog->openfiles[filenum], string, stringlength);
1744 if (developer.integer >= 100)
1745 Con_Printf("fputs: %s: %s\n", PRVM_NAME, string);
1752 writetofile(float fhandle, entity ent)
1755 void VM_writetofile(void)
1760 VM_SAFEPARMCOUNT(2, VM_writetofile);
1762 file = VM_GetFileHandle( (int)PRVM_G_FLOAT(OFS_PARM0) );
1765 VM_Warning("VM_writetofile: invalid or closed file handle\n");
1769 ent = PRVM_G_EDICT(OFS_PARM1);
1770 if(ent->priv.required->free)
1772 VM_Warning("VM_writetofile: %s: entity %i is free !\n", PRVM_NAME, PRVM_NUM_FOR_EDICT(ent));
1776 PRVM_ED_Write (file, ent);
1779 // KrimZon - DP_QC_ENTITYDATA
1784 float() numentityfields
1785 Return the number of entity fields - NOT offsets
1788 void VM_numentityfields(void)
1790 PRVM_G_FLOAT(OFS_RETURN) = prog->progs->numfielddefs;
1793 // KrimZon - DP_QC_ENTITYDATA
1798 string(float fieldnum) entityfieldname
1799 Return name of the specified field as a string, or empty if the field is invalid (warning)
1802 void VM_entityfieldname(void)
1805 int i = (int)PRVM_G_FLOAT(OFS_PARM0);
1807 if (i < 0 || i >= prog->progs->numfielddefs)
1809 VM_Warning("VM_entityfieldname: %s: field index out of bounds\n", PRVM_NAME);
1810 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString("");
1814 d = &prog->fielddefs[i];
1815 PRVM_G_INT(OFS_RETURN) = d->s_name; // presuming that s_name points to a string already
1818 // KrimZon - DP_QC_ENTITYDATA
1823 float(float fieldnum) entityfieldtype
1826 void VM_entityfieldtype(void)
1829 int i = (int)PRVM_G_FLOAT(OFS_PARM0);
1831 if (i < 0 || i >= prog->progs->numfielddefs)
1833 VM_Warning("VM_entityfieldtype: %s: field index out of bounds\n", PRVM_NAME);
1834 PRVM_G_FLOAT(OFS_RETURN) = -1.0;
1838 d = &prog->fielddefs[i];
1839 PRVM_G_FLOAT(OFS_RETURN) = (float)d->type;
1842 // KrimZon - DP_QC_ENTITYDATA
1845 VM_getentityfieldstring
1847 string(float fieldnum, entity ent) getentityfieldstring
1850 void VM_getentityfieldstring(void)
1852 // put the data into a string
1857 int i = (int)PRVM_G_FLOAT(OFS_PARM0);
1859 if (i < 0 || i >= prog->progs->numfielddefs)
1861 VM_Warning("VM_entityfielddata: %s: field index out of bounds\n", PRVM_NAME);
1862 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString("");
1866 d = &prog->fielddefs[i];
1869 ent = PRVM_G_EDICT(OFS_PARM1);
1870 if(ent->priv.required->free)
1872 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString("");
1873 VM_Warning("VM_entityfielddata: %s: entity %i is free !\n", PRVM_NAME, PRVM_NUM_FOR_EDICT(ent));
1876 v = (int *)((char *)ent->fields.vp + d->ofs*4);
1878 // if it's 0 or blank, return an empty string
1879 type = d->type & ~DEF_SAVEGLOBAL;
1880 for (j=0 ; j<prvm_type_size[type] ; j++)
1883 if (j == prvm_type_size[type])
1885 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString("");
1889 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(PRVM_UglyValueString((etype_t)d->type, (prvm_eval_t *)v));
1892 // KrimZon - DP_QC_ENTITYDATA
1895 VM_putentityfieldstring
1897 float(float fieldnum, entity ent, string s) putentityfieldstring
1900 void VM_putentityfieldstring(void)
1904 int i = (int)PRVM_G_FLOAT(OFS_PARM0);
1906 if (i < 0 || i >= prog->progs->numfielddefs)
1908 VM_Warning("VM_entityfielddata: %s: field index out of bounds\n", PRVM_NAME);
1909 PRVM_G_FLOAT(OFS_RETURN) = 0.0f;
1913 d = &prog->fielddefs[i];
1916 ent = PRVM_G_EDICT(OFS_PARM1);
1917 if(ent->priv.required->free)
1919 VM_Warning("VM_entityfielddata: %s: entity %i is free !\n", PRVM_NAME, PRVM_NUM_FOR_EDICT(ent));
1920 PRVM_G_FLOAT(OFS_RETURN) = 0.0f;
1924 // parse the string into the value
1925 PRVM_G_FLOAT(OFS_RETURN) = ( PRVM_ED_ParseEpair(ent, d, PRVM_G_STRING(OFS_PARM2)) ) ? 1.0f : 0.0f;
1932 float strlen(string s)
1935 //float(string s) strlen = #114; // returns how many characters are in a string
1936 void VM_strlen(void)
1938 VM_SAFEPARMCOUNT(1,VM_strlen);
1940 PRVM_G_FLOAT(OFS_RETURN) = strlen(PRVM_G_STRING(OFS_PARM0));
1943 // DRESK - Decolorized String
1948 string strdecolorize(string s)
1951 // string (string s) strdecolorize = #472; // returns the passed in string with color codes stripped
1952 void VM_strdecolorize(void)
1954 char szNewString[VM_STRINGTEMP_LENGTH];
1955 const char *szString;
1958 VM_SAFEPARMCOUNT(1,VM_strdecolorize);
1959 szString = PRVM_G_STRING(OFS_PARM0);
1960 COM_StringDecolorize(szString, 0, szNewString, sizeof(szNewString), TRUE);
1961 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(szNewString);
1964 // DRESK - String Length (not counting color codes)
1969 float strlennocol(string s)
1972 // float(string s) strlennocol = #471; // returns how many characters are in a string not including color codes
1973 // For example, ^2Dresk returns a length of 5
1974 void VM_strlennocol(void)
1976 const char *szString;
1979 VM_SAFEPARMCOUNT(1,VM_strlennocol);
1981 szString = PRVM_G_STRING(OFS_PARM0);
1983 nCnt = COM_StringLengthNoColors(szString, 0, NULL);
1985 PRVM_G_FLOAT(OFS_RETURN) = nCnt;
1988 // DRESK - String to Uppercase and Lowercase
1993 string strtolower(string s)
1996 // string (string s) strtolower = #480; // returns passed in string in lowercase form
1997 void VM_strtolower(void)
1999 char szNewString[VM_STRINGTEMP_LENGTH];
2000 const char *szString;
2003 VM_SAFEPARMCOUNT(1,VM_strtolower);
2004 szString = PRVM_G_STRING(OFS_PARM0);
2006 COM_ToLowerString(szString, szNewString, sizeof(szNewString) );
2008 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(szNewString);
2015 string strtoupper(string s)
2018 // string (string s) strtoupper = #481; // returns passed in string in uppercase form
2019 void VM_strtoupper(void)
2021 char szNewString[VM_STRINGTEMP_LENGTH];
2022 const char *szString;
2025 VM_SAFEPARMCOUNT(1,VM_strtoupper);
2026 szString = PRVM_G_STRING(OFS_PARM0);
2028 COM_ToUpperString(szString, szNewString, sizeof(szNewString) );
2030 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(szNewString);
2037 string strcat(string,string,...[string])
2040 //string(string s1, string s2) strcat = #115;
2041 // concatenates two strings (for example "abc", "def" would return "abcdef")
2042 // and returns as a tempstring
2043 void VM_strcat(void)
2045 char s[VM_STRINGTEMP_LENGTH];
2046 VM_SAFEPARMCOUNTRANGE(1, 8, VM_strcat);
2048 VM_VarString(0, s, sizeof(s));
2049 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(s);
2056 string substring(string s, float start, float length)
2059 // string(string s, float start, float length) substring = #116;
2060 // returns a section of a string as a tempstring
2061 void VM_substring(void)
2063 int i, start, length;
2065 char string[VM_STRINGTEMP_LENGTH];
2067 VM_SAFEPARMCOUNT(3,VM_substring);
2069 s = PRVM_G_STRING(OFS_PARM0);
2070 start = (int)PRVM_G_FLOAT(OFS_PARM1);
2071 length = (int)PRVM_G_FLOAT(OFS_PARM2);
2072 for (i = 0;i < start && *s;i++, s++);
2073 for (i = 0;i < (int)sizeof(string) - 1 && *s && i < length;i++, s++)
2076 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string);
2083 string(string search, string replace, string subject) strreplace = #484;
2086 // replaces all occurrences of search with replace in the string subject, and returns the result
2087 void VM_strreplace(void)
2090 const char *search, *replace, *subject;
2091 char string[VM_STRINGTEMP_LENGTH];
2092 int search_len, replace_len, subject_len;
2094 VM_SAFEPARMCOUNT(3,VM_strreplace);
2096 search = PRVM_G_STRING(OFS_PARM0);
2097 replace = PRVM_G_STRING(OFS_PARM1);
2098 subject = PRVM_G_STRING(OFS_PARM2);
2100 search_len = (int)strlen(search);
2101 replace_len = (int)strlen(replace);
2102 subject_len = (int)strlen(subject);
2105 for (i = 0; i < subject_len; i++)
2107 for (j = 0; j < search_len && i+j < subject_len; j++)
2108 if (subject[i+j] != search[j])
2110 if (j == search_len || i+j == subject_len)
2112 // found it at offset 'i'
2113 for (j = 0; j < replace_len && si < (int)sizeof(string) - 1; j++)
2114 string[si++] = replace[j];
2115 i += search_len - 1;
2120 if (si < (int)sizeof(string) - 1)
2121 string[si++] = subject[i];
2126 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string);
2133 string(string search, string replace, string subject) strireplace = #485;
2136 // case-insensitive version of strreplace
2137 void VM_strireplace(void)
2140 const char *search, *replace, *subject;
2141 char string[VM_STRINGTEMP_LENGTH];
2142 int search_len, replace_len, subject_len;
2144 VM_SAFEPARMCOUNT(3,VM_strreplace);
2146 search = PRVM_G_STRING(OFS_PARM0);
2147 replace = PRVM_G_STRING(OFS_PARM1);
2148 subject = PRVM_G_STRING(OFS_PARM2);
2150 search_len = (int)strlen(search);
2151 replace_len = (int)strlen(replace);
2152 subject_len = (int)strlen(subject);
2155 for (i = 0; i < subject_len; i++)
2157 for (j = 0; j < search_len && i+j < subject_len; j++)
2158 if (tolower(subject[i+j]) != tolower(search[j]))
2160 if (j == search_len || i+j == subject_len)
2162 // found it at offset 'i'
2163 for (j = 0; j < replace_len && si < (int)sizeof(string) - 1; j++)
2164 string[si++] = replace[j];
2165 i += search_len - 1;
2170 if (si < (int)sizeof(string) - 1)
2171 string[si++] = subject[i];
2176 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string);
2183 vector stov(string s)
2186 //vector(string s) stov = #117; // returns vector value from a string
2189 char string[VM_STRINGTEMP_LENGTH];
2191 VM_SAFEPARMCOUNT(1,VM_stov);
2193 VM_VarString(0, string, sizeof(string));
2194 Math_atov(string, PRVM_G_VECTOR(OFS_RETURN));
2201 string strzone(string s)
2204 //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)
2205 void VM_strzone(void)
2208 char string[VM_STRINGTEMP_LENGTH];
2211 VM_SAFEPARMCOUNT(1,VM_strzone);
2213 VM_VarString(0, string, sizeof(string));
2214 alloclen = strlen(string) + 1;
2215 PRVM_G_INT(OFS_RETURN) = PRVM_AllocString(alloclen, &out);
2216 memcpy(out, string, alloclen);
2226 //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!!!)
2227 void VM_strunzone(void)
2229 VM_SAFEPARMCOUNT(1,VM_strunzone);
2230 PRVM_FreeString(PRVM_G_INT(OFS_PARM0));
2235 VM_command (used by client and menu)
2237 clientcommand(float client, string s) (for client and menu)
2240 //void(entity e, string s) clientcommand = #440; // executes a command string as if it came from the specified client
2241 //this function originally written by KrimZon, made shorter by LordHavoc
2242 void VM_clcommand (void)
2244 client_t *temp_client;
2247 VM_SAFEPARMCOUNT(2,VM_clcommand);
2249 i = (int)PRVM_G_FLOAT(OFS_PARM0);
2250 if (!sv.active || i < 0 || i >= svs.maxclients || !svs.clients[i].active)
2252 VM_Warning("VM_clientcommand: %s: invalid client/server is not active !\n", PRVM_NAME);
2256 temp_client = host_client;
2257 host_client = svs.clients + i;
2258 Cmd_ExecuteString (PRVM_G_STRING(OFS_PARM1), src_client);
2259 host_client = temp_client;
2267 float tokenize(string s)
2270 //float(string s) tokenize = #441; // takes apart a string into individal words (access them with argv), returns how many
2271 //this function originally written by KrimZon, made shorter by LordHavoc
2272 //20040203: rewritten by LordHavoc (no longer uses allocations)
2273 static int num_tokens = 0;
2274 static int tokens[256];
2275 static int tokens_startpos[256];
2276 static int tokens_endpos[256];
2277 static char tokenize_string[VM_STRINGTEMP_LENGTH];
2278 void VM_tokenize (void)
2282 VM_SAFEPARMCOUNT(1,VM_tokenize);
2284 strlcpy(tokenize_string, PRVM_G_STRING(OFS_PARM0), sizeof(tokenize_string));
2285 p = tokenize_string;
2290 if (num_tokens >= (int)(sizeof(tokens)/sizeof(tokens[0])))
2293 // skip whitespace here to find token start pos
2294 while(*p && ISWHITESPACE(*p))
2297 tokens_startpos[num_tokens] = p - tokenize_string;
2298 if(!COM_ParseToken_VM_Tokenize(&p, false))
2300 tokens_endpos[num_tokens] = p - tokenize_string;
2301 tokens[num_tokens] = PRVM_SetTempString(com_token);
2305 PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
2308 //float(string s) tokenize = #514; // takes apart a string into individal words (access them with argv), returns how many
2309 void VM_tokenize_console (void)
2313 VM_SAFEPARMCOUNT(1,VM_tokenize);
2315 strlcpy(tokenize_string, PRVM_G_STRING(OFS_PARM0), sizeof(tokenize_string));
2316 p = tokenize_string;
2321 if (num_tokens >= (int)(sizeof(tokens)/sizeof(tokens[0])))
2324 // skip whitespace here to find token start pos
2325 while(*p && ISWHITESPACE(*p))
2328 tokens_startpos[num_tokens] = p - tokenize_string;
2329 if(!COM_ParseToken_Console(&p))
2331 tokens_endpos[num_tokens] = p - tokenize_string;
2332 tokens[num_tokens] = PRVM_SetTempString(com_token);
2336 PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
2341 VM_tokenizebyseparator
2343 float tokenizebyseparator(string s, string separator1, ...)
2346 //float(string s, string separator1, ...) tokenizebyseparator = #479; // takes apart a string into individal words (access them with argv), returns how many
2347 //this function returns the token preceding each instance of a separator (of
2348 //which there can be multiple), and the text following the last separator
2349 //useful for parsing certain kinds of data like IP addresses
2351 //numnumbers = tokenizebyseparator("10.1.2.3", ".");
2352 //returns 4 and the tokens "10" "1" "2" "3".
2353 void VM_tokenizebyseparator (void)
2357 int separatorlen[7];
2358 const char *separators[7];
2361 char tokentext[MAX_INPUTLINE];
2363 VM_SAFEPARMCOUNTRANGE(2, 8,VM_tokenizebyseparator);
2365 strlcpy(tokenize_string, PRVM_G_STRING(OFS_PARM0), sizeof(tokenize_string));
2366 p = tokenize_string;
2369 for (j = 1;j < prog->argc;j++)
2371 // skip any blank separator strings
2372 const char *s = PRVM_G_STRING(OFS_PARM0+j*3);
2375 separators[numseparators] = s;
2376 separatorlen[numseparators] = strlen(s);
2383 while (num_tokens < (int)(sizeof(tokens)/sizeof(tokens[0])))
2385 token = tokentext + j;
2386 tokens_startpos[num_tokens] = p - tokenize_string;
2389 for (k = 0;k < numseparators;k++)
2391 if (!strncmp(p, separators[k], separatorlen[k]))
2393 p += separatorlen[k];
2397 if (k < numseparators)
2399 if (j < (int)sizeof(tokentext)-1)
2400 tokentext[j++] = *p;
2403 tokens_endpos[num_tokens] = p - tokenize_string;
2404 if (j >= (int)sizeof(tokentext))
2407 tokens[num_tokens++] = PRVM_SetTempString(token);
2412 PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
2415 //string(float n) argv = #442; // returns a word from the tokenized string (returns nothing for an invalid index)
2416 //this function originally written by KrimZon, made shorter by LordHavoc
2421 VM_SAFEPARMCOUNT(1,VM_argv);
2423 token_num = (int)PRVM_G_FLOAT(OFS_PARM0);
2426 token_num += num_tokens;
2428 if (token_num >= 0 && token_num < num_tokens)
2429 PRVM_G_INT(OFS_RETURN) = tokens[token_num];
2431 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
2434 //float(float n) argv_start_index = #515; // returns the start index of a token
2435 void VM_argv_start_index (void)
2439 VM_SAFEPARMCOUNT(1,VM_argv);
2441 token_num = (int)PRVM_G_FLOAT(OFS_PARM0);
2444 token_num += num_tokens;
2446 if (token_num >= 0 && token_num < num_tokens)
2447 PRVM_G_FLOAT(OFS_RETURN) = tokens_startpos[token_num];
2449 PRVM_G_FLOAT(OFS_RETURN) = -1;
2452 //float(float n) argv_end_index = #516; // returns the end index of a token
2453 void VM_argv_end_index (void)
2457 VM_SAFEPARMCOUNT(1,VM_argv);
2459 token_num = (int)PRVM_G_FLOAT(OFS_PARM0);
2462 token_num += num_tokens;
2464 if (token_num >= 0 && token_num < num_tokens)
2465 PRVM_G_FLOAT(OFS_RETURN) = tokens_endpos[token_num];
2467 PRVM_G_FLOAT(OFS_RETURN) = -1;
2477 void VM_isserver(void)
2479 VM_SAFEPARMCOUNT(0,VM_serverstate);
2481 PRVM_G_FLOAT(OFS_RETURN) = sv.active && (svs.maxclients > 1 || cls.state == ca_dedicated);
2491 void VM_clientcount(void)
2493 VM_SAFEPARMCOUNT(0,VM_clientcount);
2495 PRVM_G_FLOAT(OFS_RETURN) = svs.maxclients;
2505 void VM_clientstate(void)
2507 VM_SAFEPARMCOUNT(0,VM_clientstate);
2510 switch( cls.state ) {
2511 case ca_uninitialized:
2513 PRVM_G_FLOAT(OFS_RETURN) = 0;
2515 case ca_disconnected:
2516 PRVM_G_FLOAT(OFS_RETURN) = 1;
2519 PRVM_G_FLOAT(OFS_RETURN) = 2;
2522 // should never be reached!
2531 float getostype(void)
2533 */ // not used at the moment -> not included in the common list
2534 void VM_getostype(void)
2536 VM_SAFEPARMCOUNT(0,VM_getostype);
2541 OS_MAC - not supported
2545 PRVM_G_FLOAT(OFS_RETURN) = 0;
2546 #elif defined(MACOSX)
2547 PRVM_G_FLOAT(OFS_RETURN) = 2;
2549 PRVM_G_FLOAT(OFS_RETURN) = 1;
2560 void VM_gettime(void)
2562 VM_SAFEPARMCOUNT(0,VM_gettime);
2564 PRVM_G_FLOAT(OFS_RETURN) = (float) realtime;
2571 loadfromdata(string data)
2574 void VM_loadfromdata(void)
2576 VM_SAFEPARMCOUNT(1,VM_loadentsfromfile);
2578 PRVM_ED_LoadFromFile(PRVM_G_STRING(OFS_PARM0));
2582 ========================
2585 parseentitydata(entity ent, string data)
2586 ========================
2588 void VM_parseentitydata(void)
2593 VM_SAFEPARMCOUNT(2, VM_parseentitydata);
2595 // get edict and test it
2596 ent = PRVM_G_EDICT(OFS_PARM0);
2597 if (ent->priv.required->free)
2598 PRVM_ERROR ("VM_parseentitydata: %s: Can only set already spawned entities (entity %i is free)!", PRVM_NAME, PRVM_NUM_FOR_EDICT(ent));
2600 data = PRVM_G_STRING(OFS_PARM1);
2602 // parse the opening brace
2603 if (!COM_ParseToken_Simple(&data, false, false) || com_token[0] != '{' )
2604 PRVM_ERROR ("VM_parseentitydata: %s: Couldn't parse entity data:\n%s", PRVM_NAME, data );
2606 PRVM_ED_ParseEdict (data, ent);
2613 loadfromfile(string file)
2616 void VM_loadfromfile(void)
2618 const char *filename;
2621 VM_SAFEPARMCOUNT(1,VM_loadfromfile);
2623 filename = PRVM_G_STRING(OFS_PARM0);
2624 if (FS_CheckNastyPath(filename, false))
2626 PRVM_G_FLOAT(OFS_RETURN) = -4;
2627 VM_Warning("VM_loadfromfile: %s dangerous or non-portable filename \"%s\" not allowed. (contains : or \\ or begins with .. or /)\n", PRVM_NAME, filename);
2631 // not conform with VM_fopen
2632 data = (char *)FS_LoadFile(filename, tempmempool, false, NULL);
2634 PRVM_G_FLOAT(OFS_RETURN) = -1;
2636 PRVM_ED_LoadFromFile(data);
2647 float mod(float val, float m)
2650 void VM_modulo(void)
2653 VM_SAFEPARMCOUNT(2,VM_module);
2655 val = (int) PRVM_G_FLOAT(OFS_PARM0);
2656 m = (int) PRVM_G_FLOAT(OFS_PARM1);
2658 PRVM_G_FLOAT(OFS_RETURN) = (float) (val % m);
2661 void VM_Search_Init(void)
2664 for (i = 0;i < PRVM_MAX_OPENSEARCHES;i++)
2665 prog->opensearches[i] = NULL;
2668 void VM_Search_Reset(void)
2671 // reset the fssearch list
2672 for(i = 0; i < PRVM_MAX_OPENSEARCHES; i++)
2674 if(prog->opensearches[i])
2675 FS_FreeSearch(prog->opensearches[i]);
2676 prog->opensearches[i] = NULL;
2684 float search_begin(string pattern, float caseinsensitive, float quiet)
2687 void VM_search_begin(void)
2690 const char *pattern;
2691 int caseinsens, quiet;
2693 VM_SAFEPARMCOUNT(3, VM_search_begin);
2695 pattern = PRVM_G_STRING(OFS_PARM0);
2697 VM_CheckEmptyString(pattern);
2699 caseinsens = (int)PRVM_G_FLOAT(OFS_PARM1);
2700 quiet = (int)PRVM_G_FLOAT(OFS_PARM2);
2702 for(handle = 0; handle < PRVM_MAX_OPENSEARCHES; handle++)
2703 if(!prog->opensearches[handle])
2706 if(handle >= PRVM_MAX_OPENSEARCHES)
2708 PRVM_G_FLOAT(OFS_RETURN) = -2;
2709 VM_Warning("VM_search_begin: %s ran out of search handles (%i)\n", PRVM_NAME, PRVM_MAX_OPENSEARCHES);
2713 if(!(prog->opensearches[handle] = FS_Search(pattern,caseinsens, quiet)))
2714 PRVM_G_FLOAT(OFS_RETURN) = -1;
2717 prog->opensearches_origin[handle] = PRVM_AllocationOrigin();
2718 PRVM_G_FLOAT(OFS_RETURN) = handle;
2726 void search_end(float handle)
2729 void VM_search_end(void)
2732 VM_SAFEPARMCOUNT(1, VM_search_end);
2734 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
2736 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
2738 VM_Warning("VM_search_end: invalid handle %i used in %s\n", handle, PRVM_NAME);
2741 if(prog->opensearches[handle] == NULL)
2743 VM_Warning("VM_search_end: no such handle %i in %s\n", handle, PRVM_NAME);
2747 FS_FreeSearch(prog->opensearches[handle]);
2748 prog->opensearches[handle] = NULL;
2749 if(prog->opensearches_origin[handle])
2750 PRVM_Free((char *)prog->opensearches_origin[handle]);
2757 float search_getsize(float handle)
2760 void VM_search_getsize(void)
2763 VM_SAFEPARMCOUNT(1, VM_M_search_getsize);
2765 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
2767 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
2769 VM_Warning("VM_search_getsize: invalid handle %i used in %s\n", handle, PRVM_NAME);
2772 if(prog->opensearches[handle] == NULL)
2774 VM_Warning("VM_search_getsize: no such handle %i in %s\n", handle, PRVM_NAME);
2778 PRVM_G_FLOAT(OFS_RETURN) = prog->opensearches[handle]->numfilenames;
2783 VM_search_getfilename
2785 string search_getfilename(float handle, float num)
2788 void VM_search_getfilename(void)
2790 int handle, filenum;
2791 VM_SAFEPARMCOUNT(2, VM_search_getfilename);
2793 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
2794 filenum = (int)PRVM_G_FLOAT(OFS_PARM1);
2796 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
2798 VM_Warning("VM_search_getfilename: invalid handle %i used in %s\n", handle, PRVM_NAME);
2801 if(prog->opensearches[handle] == NULL)
2803 VM_Warning("VM_search_getfilename: no such handle %i in %s\n", handle, PRVM_NAME);
2806 if(filenum < 0 || filenum >= prog->opensearches[handle]->numfilenames)
2808 VM_Warning("VM_search_getfilename: invalid filenum %i in %s\n", filenum, PRVM_NAME);
2812 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog->opensearches[handle]->filenames[filenum]);
2819 string chr(float ascii)
2825 VM_SAFEPARMCOUNT(1, VM_chr);
2827 tmp[0] = (unsigned char) PRVM_G_FLOAT(OFS_PARM0);
2830 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(tmp);
2833 //=============================================================================
2834 // Draw builtins (client & menu)
2840 float iscachedpic(string pic)
2843 void VM_iscachedpic(void)
2845 VM_SAFEPARMCOUNT(1,VM_iscachedpic);
2847 // drawq hasnt such a function, thus always return true
2848 PRVM_G_FLOAT(OFS_RETURN) = false;
2855 string precache_pic(string pic)
2858 void VM_precache_pic(void)
2862 VM_SAFEPARMCOUNT(1, VM_precache_pic);
2864 s = PRVM_G_STRING(OFS_PARM0);
2865 PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
2866 VM_CheckEmptyString (s);
2868 // AK Draw_CachePic is supposed to always return a valid pointer
2869 if( Draw_CachePic_Flags(s, CACHEPICFLAG_NOTPERSISTENT)->tex == r_texture_notexture )
2870 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
2880 void VM_freepic(void)
2884 VM_SAFEPARMCOUNT(1,VM_freepic);
2886 s = PRVM_G_STRING(OFS_PARM0);
2887 VM_CheckEmptyString (s);
2892 dp_font_t *getdrawfont()
2894 if(prog->globaloffsets.drawfont >= 0)
2896 int f = PRVM_G_FLOAT(prog->globaloffsets.drawfont);
2897 if(f < 0 || f >= MAX_FONTS)
2898 return FONT_DEFAULT;
2899 return &dp_fonts[f];
2902 return FONT_DEFAULT;
2909 float drawcharacter(vector position, float character, vector scale, vector rgb, float alpha, float flag)
2912 void VM_drawcharacter(void)
2914 float *pos,*scale,*rgb;
2917 VM_SAFEPARMCOUNT(6,VM_drawcharacter);
2919 character = (char) PRVM_G_FLOAT(OFS_PARM1);
2922 PRVM_G_FLOAT(OFS_RETURN) = -1;
2923 VM_Warning("VM_drawcharacter: %s passed null character !\n",PRVM_NAME);
2927 pos = PRVM_G_VECTOR(OFS_PARM0);
2928 scale = PRVM_G_VECTOR(OFS_PARM2);
2929 rgb = PRVM_G_VECTOR(OFS_PARM3);
2930 flag = (int)PRVM_G_FLOAT(OFS_PARM5);
2932 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2934 PRVM_G_FLOAT(OFS_RETURN) = -2;
2935 VM_Warning("VM_drawcharacter: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2939 if(pos[2] || scale[2])
2940 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")));
2942 if(!scale[0] || !scale[1])
2944 PRVM_G_FLOAT(OFS_RETURN) = -3;
2945 VM_Warning("VM_drawcharacter: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
2949 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());
2950 PRVM_G_FLOAT(OFS_RETURN) = 1;
2957 float drawstring(vector position, string text, vector scale, vector rgb, float alpha, float flag)
2960 void VM_drawstring(void)
2962 float *pos,*scale,*rgb;
2965 VM_SAFEPARMCOUNT(6,VM_drawstring);
2967 string = PRVM_G_STRING(OFS_PARM1);
2968 pos = PRVM_G_VECTOR(OFS_PARM0);
2969 scale = PRVM_G_VECTOR(OFS_PARM2);
2970 rgb = PRVM_G_VECTOR(OFS_PARM3);
2971 flag = (int)PRVM_G_FLOAT(OFS_PARM5);
2973 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2975 PRVM_G_FLOAT(OFS_RETURN) = -2;
2976 VM_Warning("VM_drawstring: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2980 if(!scale[0] || !scale[1])
2982 PRVM_G_FLOAT(OFS_RETURN) = -3;
2983 VM_Warning("VM_drawstring: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
2987 if(pos[2] || scale[2])
2988 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")));
2990 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());
2991 PRVM_G_FLOAT(OFS_RETURN) = 1;
2996 VM_drawcolorcodedstring
2998 float drawcolorcodedstring(vector position, string text, vector scale, float alpha, float flag)
3001 void VM_drawcolorcodedstring(void)
3006 VM_SAFEPARMCOUNT(5,VM_drawstring);
3008 string = PRVM_G_STRING(OFS_PARM1);
3009 pos = PRVM_G_VECTOR(OFS_PARM0);
3010 scale = PRVM_G_VECTOR(OFS_PARM2);
3011 flag = (int)PRVM_G_FLOAT(OFS_PARM4);
3013 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3015 PRVM_G_FLOAT(OFS_RETURN) = -2;
3016 VM_Warning("VM_drawcolorcodedstring: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
3020 if(!scale[0] || !scale[1])
3022 PRVM_G_FLOAT(OFS_RETURN) = -3;
3023 VM_Warning("VM_drawcolorcodedstring: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
3027 if(pos[2] || scale[2])
3028 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")));
3031 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());
3032 PRVM_G_FLOAT(OFS_RETURN) = 1;
3038 float stringwidth(string text, float allowColorCodes)
3041 void VM_stringwidth(void)
3045 VM_SAFEPARMCOUNT(2,VM_drawstring);
3047 string = PRVM_G_STRING(OFS_PARM0);
3048 colors = (int)PRVM_G_FLOAT(OFS_PARM1);
3050 PRVM_G_FLOAT(OFS_RETURN) = DrawQ_TextWidth_Font(string, 0, !colors, getdrawfont()); // 1x1 characters, don't actually draw
3056 float drawpic(vector position, string pic, vector size, vector rgb, float alpha, float flag)
3059 void VM_drawpic(void)
3061 const char *picname;
3062 float *size, *pos, *rgb;
3065 VM_SAFEPARMCOUNT(6,VM_drawpic);
3067 picname = PRVM_G_STRING(OFS_PARM1);
3068 VM_CheckEmptyString (picname);
3070 // is pic cached ? no function yet for that
3073 PRVM_G_FLOAT(OFS_RETURN) = -4;
3074 VM_Warning("VM_drawpic: %s: %s not cached !\n", PRVM_NAME, picname);
3078 pos = PRVM_G_VECTOR(OFS_PARM0);
3079 size = PRVM_G_VECTOR(OFS_PARM2);
3080 rgb = PRVM_G_VECTOR(OFS_PARM3);
3081 flag = (int) PRVM_G_FLOAT(OFS_PARM5);
3083 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3085 PRVM_G_FLOAT(OFS_RETURN) = -2;
3086 VM_Warning("VM_drawpic: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
3090 if(pos[2] || size[2])
3091 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")));
3093 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);
3094 PRVM_G_FLOAT(OFS_RETURN) = 1;
3100 float drawsubpic(vector position, vector size, string pic, vector srcPos, vector srcSize, vector rgb, float alpha, float flag)
3104 void VM_drawsubpic(void)
3106 const char *picname;
3107 float *size, *pos, *rgb, *srcPos, *srcSize, alpha;
3110 VM_SAFEPARMCOUNT(8,VM_drawsubpic);
3112 picname = PRVM_G_STRING(OFS_PARM2);
3113 VM_CheckEmptyString (picname);
3115 // is pic cached ? no function yet for that
3118 PRVM_G_FLOAT(OFS_RETURN) = -4;
3119 VM_Warning("VM_drawsubpic: %s: %s not cached !\n", PRVM_NAME, picname);
3123 pos = PRVM_G_VECTOR(OFS_PARM0);
3124 size = PRVM_G_VECTOR(OFS_PARM1);
3125 srcPos = PRVM_G_VECTOR(OFS_PARM3);
3126 srcSize = PRVM_G_VECTOR(OFS_PARM4);
3127 rgb = PRVM_G_VECTOR(OFS_PARM5);
3128 alpha = PRVM_G_FLOAT(OFS_PARM6);
3129 flag = (int) PRVM_G_FLOAT(OFS_PARM7);
3131 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3133 PRVM_G_FLOAT(OFS_RETURN) = -2;
3134 VM_Warning("VM_drawsubpic: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
3138 if(pos[2] || size[2])
3139 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")));
3141 DrawQ_SuperPic(pos[0], pos[1], Draw_CachePic (picname),
3143 srcPos[0], srcPos[1], rgb[0], rgb[1], rgb[2], alpha,
3144 srcPos[0] + srcSize[0], srcPos[1], rgb[0], rgb[1], rgb[2], alpha,
3145 srcPos[0], srcPos[1] + srcSize[1], rgb[0], rgb[1], rgb[2], alpha,
3146 srcPos[0] + srcSize[0], srcPos[1] + srcSize[1], rgb[0], rgb[1], rgb[2], alpha,
3148 PRVM_G_FLOAT(OFS_RETURN) = 1;
3155 float drawfill(vector position, vector size, vector rgb, float alpha, float flag)
3158 void VM_drawfill(void)
3160 float *size, *pos, *rgb;
3163 VM_SAFEPARMCOUNT(5,VM_drawfill);
3166 pos = PRVM_G_VECTOR(OFS_PARM0);
3167 size = PRVM_G_VECTOR(OFS_PARM1);
3168 rgb = PRVM_G_VECTOR(OFS_PARM2);
3169 flag = (int) PRVM_G_FLOAT(OFS_PARM4);
3171 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3173 PRVM_G_FLOAT(OFS_RETURN) = -2;
3174 VM_Warning("VM_drawfill: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
3178 if(pos[2] || size[2])
3179 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")));
3181 DrawQ_Fill(pos[0], pos[1], size[0], size[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM3), flag);
3182 PRVM_G_FLOAT(OFS_RETURN) = 1;
3189 drawsetcliparea(float x, float y, float width, float height)
3192 void VM_drawsetcliparea(void)
3195 VM_SAFEPARMCOUNT(4,VM_drawsetcliparea);
3197 x = bound(0, PRVM_G_FLOAT(OFS_PARM0), vid_conwidth.integer);
3198 y = bound(0, PRVM_G_FLOAT(OFS_PARM1), vid_conheight.integer);
3199 w = bound(0, PRVM_G_FLOAT(OFS_PARM2) + PRVM_G_FLOAT(OFS_PARM0) - x, (vid_conwidth.integer - x));
3200 h = bound(0, PRVM_G_FLOAT(OFS_PARM3) + PRVM_G_FLOAT(OFS_PARM1) - y, (vid_conheight.integer - y));
3202 DrawQ_SetClipArea(x, y, w, h);
3207 VM_drawresetcliparea
3212 void VM_drawresetcliparea(void)
3214 VM_SAFEPARMCOUNT(0,VM_drawresetcliparea);
3216 DrawQ_ResetClipArea();
3223 vector getimagesize(string pic)
3226 void VM_getimagesize(void)
3231 VM_SAFEPARMCOUNT(1,VM_getimagesize);
3233 p = PRVM_G_STRING(OFS_PARM0);
3234 VM_CheckEmptyString (p);
3236 pic = Draw_CachePic_Flags (p, CACHEPICFLAG_NOTPERSISTENT);
3238 PRVM_G_VECTOR(OFS_RETURN)[0] = pic->width;
3239 PRVM_G_VECTOR(OFS_RETURN)[1] = pic->height;
3240 PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
3247 string keynumtostring(float keynum)
3250 void VM_keynumtostring (void)
3252 VM_SAFEPARMCOUNT(1, VM_keynumtostring);
3254 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(Key_KeynumToString((int)PRVM_G_FLOAT(OFS_PARM0)));
3259 VM_findkeysforcommand
3261 string findkeysforcommand(string command)
3263 the returned string is an altstring
3266 #define NUMKEYS 5 // TODO: merge the constant in keys.c with this one somewhen
3268 void M_FindKeysForCommand(const char *command, int *keys);
3269 void VM_findkeysforcommand(void)
3272 char ret[VM_STRINGTEMP_LENGTH];
3276 VM_SAFEPARMCOUNT(1, VM_findkeysforcommand);
3278 cmd = PRVM_G_STRING(OFS_PARM0);
3280 VM_CheckEmptyString(cmd);
3282 M_FindKeysForCommand(cmd, keys);
3285 for(i = 0; i < NUMKEYS; i++)
3286 strlcat(ret, va(" \'%i\'", keys[i]), sizeof(ret));
3288 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(ret);
3295 float stringtokeynum(string key)
3298 void VM_stringtokeynum (void)
3300 VM_SAFEPARMCOUNT( 1, VM_keynumtostring );
3302 PRVM_G_INT(OFS_RETURN) = Key_StringToKeynum(PRVM_G_STRING(OFS_PARM0));
3305 // CL_Video interface functions
3308 ========================
3311 float cin_open(string file, string name)
3312 ========================
3314 void VM_cin_open( void )
3319 VM_SAFEPARMCOUNT( 2, VM_cin_open );
3321 file = PRVM_G_STRING( OFS_PARM0 );
3322 name = PRVM_G_STRING( OFS_PARM1 );
3324 VM_CheckEmptyString( file );
3325 VM_CheckEmptyString( name );
3327 if( CL_OpenVideo( file, name, MENUOWNER ) )
3328 PRVM_G_FLOAT( OFS_RETURN ) = 1;
3330 PRVM_G_FLOAT( OFS_RETURN ) = 0;
3334 ========================
3337 void cin_close(string name)
3338 ========================
3340 void VM_cin_close( void )
3344 VM_SAFEPARMCOUNT( 1, VM_cin_close );
3346 name = PRVM_G_STRING( OFS_PARM0 );
3347 VM_CheckEmptyString( name );
3349 CL_CloseVideo( CL_GetVideoByName( name ) );
3353 ========================
3355 void cin_setstate(string name, float type)
3356 ========================
3358 void VM_cin_setstate( void )
3361 clvideostate_t state;
3364 VM_SAFEPARMCOUNT( 2, VM_cin_netstate );
3366 name = PRVM_G_STRING( OFS_PARM0 );
3367 VM_CheckEmptyString( name );
3369 state = (clvideostate_t)((int)PRVM_G_FLOAT( OFS_PARM1 ));
3371 video = CL_GetVideoByName( name );
3372 if( video && state > CLVIDEO_UNUSED && state < CLVIDEO_STATECOUNT )
3373 CL_SetVideoState( video, state );
3377 ========================
3380 float cin_getstate(string name)
3381 ========================
3383 void VM_cin_getstate( void )
3388 VM_SAFEPARMCOUNT( 1, VM_cin_getstate );
3390 name = PRVM_G_STRING( OFS_PARM0 );
3391 VM_CheckEmptyString( name );
3393 video = CL_GetVideoByName( name );
3395 PRVM_G_FLOAT( OFS_RETURN ) = (int)video->state;
3397 PRVM_G_FLOAT( OFS_RETURN ) = 0;
3401 ========================
3404 void cin_restart(string name)
3405 ========================
3407 void VM_cin_restart( void )
3412 VM_SAFEPARMCOUNT( 1, VM_cin_restart );
3414 name = PRVM_G_STRING( OFS_PARM0 );
3415 VM_CheckEmptyString( name );
3417 video = CL_GetVideoByName( name );
3419 CL_RestartVideo( video );
3423 ========================
3425 ========================
3427 void VM_Gecko_Init( void ) {
3428 // the prog struct is memset to 0 by Initprog? [12/6/2007 Black]
3429 // FIXME: remove the other _Init functions then, too? [12/6/2007 Black]
3433 ========================
3435 ========================
3437 void VM_Gecko_Destroy( void ) {
3439 for( i = 0 ; i < PRVM_MAX_GECKOINSTANCES ; i++ ) {
3440 clgecko_t **instance = &prog->opengeckoinstances[ i ];
3442 CL_Gecko_DestroyBrowser( *instance );
3449 ========================
3452 float[bool] gecko_create( string name )
3453 ========================
3455 void VM_gecko_create( void ) {
3458 clgecko_t *instance;
3460 VM_SAFEPARMCOUNT( 1, VM_gecko_create );
3462 name = PRVM_G_STRING( OFS_PARM0 );
3463 VM_CheckEmptyString( name );
3465 // find an empty slot for this gecko browser..
3466 for( i = 0 ; i < PRVM_MAX_GECKOINSTANCES ; i++ ) {
3467 if( prog->opengeckoinstances[ i ] == NULL ) {
3471 if( i == PRVM_MAX_GECKOINSTANCES ) {
3472 VM_Warning("VM_gecko_create: %s ran out of gecko handles (%i)\n", PRVM_NAME, PRVM_MAX_GECKOINSTANCES);
3473 PRVM_G_FLOAT( OFS_RETURN ) = 0;
3477 instance = prog->opengeckoinstances[ i ] = CL_Gecko_CreateBrowser( name, PRVM_GetProgNr() );
3479 // TODO: error handling [12/3/2007 Black]
3480 PRVM_G_FLOAT( OFS_RETURN ) = 0;
3483 PRVM_G_FLOAT( OFS_RETURN ) = 1;
3487 ========================
3490 void gecko_destroy( string name )
3491 ========================
3493 void VM_gecko_destroy( void ) {
3495 clgecko_t *instance;
3497 VM_SAFEPARMCOUNT( 1, VM_gecko_destroy );
3499 name = PRVM_G_STRING( OFS_PARM0 );
3500 VM_CheckEmptyString( name );
3501 instance = CL_Gecko_FindBrowser( name );
3505 CL_Gecko_DestroyBrowser( instance );
3509 ========================
3512 void gecko_navigate( string name, string URI )
3513 ========================
3515 void VM_gecko_navigate( void ) {
3518 clgecko_t *instance;
3520 VM_SAFEPARMCOUNT( 2, VM_gecko_navigate );
3522 name = PRVM_G_STRING( OFS_PARM0 );
3523 URI = PRVM_G_STRING( OFS_PARM1 );
3524 VM_CheckEmptyString( name );
3525 VM_CheckEmptyString( URI );
3527 instance = CL_Gecko_FindBrowser( name );
3531 CL_Gecko_NavigateToURI( instance, URI );
3535 ========================
3538 float[bool] gecko_keyevent( string name, float key, float eventtype )
3539 ========================
3541 void VM_gecko_keyevent( void ) {
3544 clgecko_buttoneventtype_t eventtype;
3545 clgecko_t *instance;
3547 VM_SAFEPARMCOUNT( 3, VM_gecko_keyevent );
3549 name = PRVM_G_STRING( OFS_PARM0 );
3550 VM_CheckEmptyString( name );
3551 key = (unsigned int) PRVM_G_FLOAT( OFS_PARM1 );
3552 switch( (unsigned int) PRVM_G_FLOAT( OFS_PARM2 ) ) {
3554 eventtype = CLG_BET_DOWN;
3557 eventtype = CLG_BET_UP;
3560 eventtype = CLG_BET_PRESS;
3563 eventtype = CLG_BET_DOUBLECLICK;
3566 // TODO: console printf? [12/3/2007 Black]
3567 PRVM_G_FLOAT( OFS_RETURN ) = 0;
3571 instance = CL_Gecko_FindBrowser( name );
3573 PRVM_G_FLOAT( OFS_RETURN ) = 0;
3577 PRVM_G_FLOAT( OFS_RETURN ) = (CL_Gecko_Event_Key( instance, key, eventtype ) == true);
3581 ========================
3584 void gecko_mousemove( string name, float x, float y )
3585 ========================
3587 void VM_gecko_movemouse( void ) {
3590 clgecko_t *instance;
3592 VM_SAFEPARMCOUNT( 3, VM_gecko_movemouse );
3594 name = PRVM_G_STRING( OFS_PARM0 );
3595 VM_CheckEmptyString( name );
3596 x = PRVM_G_FLOAT( OFS_PARM1 );
3597 y = PRVM_G_FLOAT( OFS_PARM2 );
3599 instance = CL_Gecko_FindBrowser( name );
3603 CL_Gecko_Event_CursorMove( instance, x, y );
3608 ========================
3611 void gecko_resize( string name, float w, float h )
3612 ========================
3614 void VM_gecko_resize( void ) {
3617 clgecko_t *instance;
3619 VM_SAFEPARMCOUNT( 3, VM_gecko_movemouse );
3621 name = PRVM_G_STRING( OFS_PARM0 );
3622 VM_CheckEmptyString( name );
3623 w = PRVM_G_FLOAT( OFS_PARM1 );
3624 h = PRVM_G_FLOAT( OFS_PARM2 );
3626 instance = CL_Gecko_FindBrowser( name );
3630 CL_Gecko_Resize( instance, w, h );
3635 ========================
3636 VM_gecko_get_texture_extent
3638 vector gecko_get_texture_extent( string name )
3639 ========================
3641 void VM_gecko_get_texture_extent( void ) {
3643 clgecko_t *instance;
3645 VM_SAFEPARMCOUNT( 1, VM_gecko_movemouse );
3647 name = PRVM_G_STRING( OFS_PARM0 );
3648 VM_CheckEmptyString( name );
3650 PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
3651 instance = CL_Gecko_FindBrowser( name );
3653 PRVM_G_VECTOR(OFS_RETURN)[0] = 0;
3654 PRVM_G_VECTOR(OFS_RETURN)[1] = 0;
3657 CL_Gecko_GetTextureExtent( instance,
3658 PRVM_G_VECTOR(OFS_RETURN), PRVM_G_VECTOR(OFS_RETURN)+1 );
3667 Writes new values for v_forward, v_up, and v_right based on angles
3668 void makevectors(vector angle)
3671 void VM_makevectors (void)
3673 prvm_eval_t *valforward, *valright, *valup;
3674 valforward = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_forward);
3675 valright = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_right);
3676 valup = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_up);
3677 if (!valforward || !valright || !valup)
3679 VM_Warning("makevectors: could not find v_forward, v_right, or v_up global variables\n");
3682 VM_SAFEPARMCOUNT(1, VM_makevectors);
3683 AngleVectors (PRVM_G_VECTOR(OFS_PARM0), valforward->vector, valright->vector, valup->vector);
3690 Writes new values for v_forward, v_up, and v_right based on the given forward vector
3691 vectorvectors(vector)
3694 void VM_vectorvectors (void)
3696 prvm_eval_t *valforward, *valright, *valup;
3697 valforward = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_forward);
3698 valright = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_right);
3699 valup = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_up);
3700 if (!valforward || !valright || !valup)
3702 VM_Warning("vectorvectors: could not find v_forward, v_right, or v_up global variables\n");
3705 VM_SAFEPARMCOUNT(1, VM_vectorvectors);
3706 VectorNormalize2(PRVM_G_VECTOR(OFS_PARM0), valforward->vector);
3707 VectorVectors(valforward->vector, valright->vector, valup->vector);
3711 ========================
3714 void drawline(float width, vector pos1, vector pos2, vector rgb, float alpha, float flags)
3715 ========================
3717 void VM_drawline (void)
3719 float *c1, *c2, *rgb;
3721 unsigned char flags;
3723 VM_SAFEPARMCOUNT(6, VM_drawline);
3724 width = PRVM_G_FLOAT(OFS_PARM0);
3725 c1 = PRVM_G_VECTOR(OFS_PARM1);
3726 c2 = PRVM_G_VECTOR(OFS_PARM2);
3727 rgb = PRVM_G_VECTOR(OFS_PARM3);
3728 alpha = PRVM_G_FLOAT(OFS_PARM4);
3729 flags = (int)PRVM_G_FLOAT(OFS_PARM5);
3730 DrawQ_Line(width, c1[0], c1[1], c2[0], c2[1], rgb[0], rgb[1], rgb[2], alpha, flags);
3733 // float(float number, float quantity) bitshift (EXT_BITSHIFT)
3734 void VM_bitshift (void)
3737 VM_SAFEPARMCOUNT(2, VM_bitshift);
3739 n1 = (int)fabs((int)PRVM_G_FLOAT(OFS_PARM0));
3740 n2 = (int)PRVM_G_FLOAT(OFS_PARM1);
3742 PRVM_G_FLOAT(OFS_RETURN) = n1;
3745 PRVM_G_FLOAT(OFS_RETURN) = (n1 >> -n2);
3747 PRVM_G_FLOAT(OFS_RETURN) = (n1 << n2);
3750 ////////////////////////////////////////
3751 // AltString functions
3752 ////////////////////////////////////////
3755 ========================
3758 float altstr_count(string)
3759 ========================
3761 void VM_altstr_count( void )
3763 const char *altstr, *pos;
3766 VM_SAFEPARMCOUNT( 1, VM_altstr_count );
3768 altstr = PRVM_G_STRING( OFS_PARM0 );
3769 //VM_CheckEmptyString( altstr );
3771 for( count = 0, pos = altstr ; *pos ; pos++ ) {
3772 if( *pos == '\\' ) {
3776 } else if( *pos == '\'' ) {
3781 PRVM_G_FLOAT( OFS_RETURN ) = (float) (count / 2);
3785 ========================
3788 string altstr_prepare(string)
3789 ========================
3791 void VM_altstr_prepare( void )
3794 const char *instr, *in;
3796 char outstr[VM_STRINGTEMP_LENGTH];
3798 VM_SAFEPARMCOUNT( 1, VM_altstr_prepare );
3800 instr = PRVM_G_STRING( OFS_PARM0 );
3802 for( out = outstr, in = instr, size = sizeof(outstr) - 1 ; size && *in ; size--, in++, out++ )
3811 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
3815 ========================
3818 string altstr_get(string, float)
3819 ========================
3821 void VM_altstr_get( void )
3823 const char *altstr, *pos;
3826 char outstr[VM_STRINGTEMP_LENGTH];
3828 VM_SAFEPARMCOUNT( 2, VM_altstr_get );
3830 altstr = PRVM_G_STRING( OFS_PARM0 );
3832 count = (int)PRVM_G_FLOAT( OFS_PARM1 );
3833 count = count * 2 + 1;
3835 for( pos = altstr ; *pos && count ; pos++ )
3836 if( *pos == '\\' ) {
3839 } else if( *pos == '\'' )
3843 PRVM_G_INT( OFS_RETURN ) = 0;
3847 for( out = outstr, size = sizeof(outstr) - 1 ; size && *pos ; size--, pos++, out++ )
3848 if( *pos == '\\' ) {
3853 } else if( *pos == '\'' )
3859 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
3863 ========================
3866 string altstr_set(string altstr, float num, string set)
3867 ========================
3869 void VM_altstr_set( void )
3872 const char *altstr, *str;
3875 char outstr[VM_STRINGTEMP_LENGTH];
3877 VM_SAFEPARMCOUNT( 3, VM_altstr_set );
3879 altstr = PRVM_G_STRING( OFS_PARM0 );
3881 num = (int)PRVM_G_FLOAT( OFS_PARM1 );
3883 str = PRVM_G_STRING( OFS_PARM2 );
3886 for( num = num * 2 + 1, in = altstr; *in && num; *out++ = *in++ )
3891 } else if( *in == '\'' ) {
3896 for( ; *str; *out++ = *str++ );
3897 // now jump over the old content
3899 if( *in == '\'' || (*in == '\\' && !*++in) )
3902 strlcpy(out, in, outstr + sizeof(outstr) - out);
3903 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
3907 ========================
3910 string altstr_ins(string altstr, float num, string set)
3911 ========================
3913 void VM_altstr_ins(void)
3921 char outstr[VM_STRINGTEMP_LENGTH];
3923 VM_SAFEPARMCOUNT(3, VM_altstr_ins);
3925 in = instr = PRVM_G_STRING( OFS_PARM0 );
3926 num = (int)PRVM_G_FLOAT( OFS_PARM1 );
3927 set = setstr = PRVM_G_STRING( OFS_PARM2 );
3930 for( num = num * 2 + 2 ; *in && num > 0 ; *out++ = *in++ )
3935 } else if( *in == '\'' ) {
3940 for( ; *set ; *out++ = *set++ );
3943 strlcpy(out, in, outstr + sizeof(outstr) - out);
3944 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
3948 ////////////////////////////////////////
3949 // BufString functions
3950 ////////////////////////////////////////
3951 //[515]: string buffers support
3953 static size_t stringbuffers_sortlength;
3955 static void BufStr_Expand(prvm_stringbuffer_t *stringbuffer, int strindex)
3957 if (stringbuffer->max_strings <= strindex)
3959 char **oldstrings = stringbuffer->strings;
3960 stringbuffer->max_strings = max(stringbuffer->max_strings * 2, 128);
3961 while (stringbuffer->max_strings <= strindex)
3962 stringbuffer->max_strings *= 2;
3963 stringbuffer->strings = Mem_Alloc(prog->progs_mempool, stringbuffer->max_strings * sizeof(stringbuffer->strings[0]));
3964 if (stringbuffer->num_strings > 0)
3965 memcpy(stringbuffer->strings, oldstrings, stringbuffer->num_strings * sizeof(stringbuffer->strings[0]));
3967 Mem_Free(oldstrings);
3971 static void BufStr_Shrink(prvm_stringbuffer_t *stringbuffer)
3973 // reduce num_strings if there are empty string slots at the end
3974 while (stringbuffer->num_strings > 0 && stringbuffer->strings[stringbuffer->num_strings - 1] == NULL)
3975 stringbuffer->num_strings--;
3977 // if empty, free the string pointer array
3978 if (stringbuffer->num_strings == 0)
3980 stringbuffer->max_strings = 0;
3981 if (stringbuffer->strings)
3982 Mem_Free(stringbuffer->strings);
3983 stringbuffer->strings = NULL;
3987 static int BufStr_SortStringsUP (const void *in1, const void *in2)
3990 a = *((const char **) in1);
3991 b = *((const char **) in2);
3993 if(!b[0]) return -1;
3994 return strncmp(a, b, stringbuffers_sortlength);
3997 static int BufStr_SortStringsDOWN (const void *in1, const void *in2)
4000 a = *((const char **) in1);
4001 b = *((const char **) in2);
4003 if(!b[0]) return -1;
4004 return strncmp(b, a, stringbuffers_sortlength);
4008 ========================
4010 creates new buffer, and returns it's index, returns -1 if failed
4011 float buf_create(void) = #460;
4012 ========================
4014 void VM_buf_create (void)
4016 prvm_stringbuffer_t *stringbuffer;
4018 VM_SAFEPARMCOUNT(0, VM_buf_create);
4019 stringbuffer = Mem_ExpandableArray_AllocRecord(&prog->stringbuffersarray);
4020 for (i = 0;stringbuffer != Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, i);i++);
4021 stringbuffer->origin = PRVM_AllocationOrigin();
4022 PRVM_G_FLOAT(OFS_RETURN) = i;
4026 ========================
4028 deletes buffer and all strings in it
4029 void buf_del(float bufhandle) = #461;
4030 ========================
4032 void VM_buf_del (void)
4034 prvm_stringbuffer_t *stringbuffer;
4035 VM_SAFEPARMCOUNT(1, VM_buf_del);
4036 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4040 for (i = 0;i < stringbuffer->num_strings;i++)
4041 if (stringbuffer->strings[i])
4042 Mem_Free(stringbuffer->strings[i]);
4043 if (stringbuffer->strings)
4044 Mem_Free(stringbuffer->strings);
4045 if(stringbuffer->origin)
4046 PRVM_Free((char *)stringbuffer->origin);
4047 Mem_ExpandableArray_FreeRecord(&prog->stringbuffersarray, stringbuffer);
4051 VM_Warning("VM_buf_del: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4057 ========================
4059 how many strings are stored in buffer
4060 float buf_getsize(float bufhandle) = #462;
4061 ========================
4063 void VM_buf_getsize (void)
4065 prvm_stringbuffer_t *stringbuffer;
4066 VM_SAFEPARMCOUNT(1, VM_buf_getsize);
4068 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4071 PRVM_G_FLOAT(OFS_RETURN) = -1;
4072 VM_Warning("VM_buf_getsize: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4076 PRVM_G_FLOAT(OFS_RETURN) = stringbuffer->num_strings;
4080 ========================
4082 copy all content from one buffer to another, make sure it exists
4083 void buf_copy(float bufhandle_from, float bufhandle_to) = #463;
4084 ========================
4086 void VM_buf_copy (void)
4088 prvm_stringbuffer_t *srcstringbuffer, *dststringbuffer;
4090 VM_SAFEPARMCOUNT(2, VM_buf_copy);
4092 srcstringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4093 if(!srcstringbuffer)
4095 VM_Warning("VM_buf_copy: invalid source buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4098 i = (int)PRVM_G_FLOAT(OFS_PARM1);
4099 if(i == (int)PRVM_G_FLOAT(OFS_PARM0))
4101 VM_Warning("VM_buf_copy: source == destination (%i) in %s\n", i, PRVM_NAME);
4104 dststringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4105 if(!dststringbuffer)
4107 VM_Warning("VM_buf_copy: invalid destination buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM1), PRVM_NAME);
4111 for (i = 0;i < dststringbuffer->num_strings;i++)
4112 if (dststringbuffer->strings[i])
4113 Mem_Free(dststringbuffer->strings[i]);
4114 if (dststringbuffer->strings)
4115 Mem_Free(dststringbuffer->strings);
4116 *dststringbuffer = *srcstringbuffer;
4117 if (dststringbuffer->max_strings)
4118 dststringbuffer->strings = (char **)Mem_Alloc(prog->progs_mempool, sizeof(dststringbuffer->strings[0]) * dststringbuffer->max_strings);
4120 for (i = 0;i < dststringbuffer->num_strings;i++)
4122 if (srcstringbuffer->strings[i])
4125 stringlen = strlen(srcstringbuffer->strings[i]) + 1;
4126 dststringbuffer->strings[i] = (char *)Mem_Alloc(prog->progs_mempool, stringlen);
4127 memcpy(dststringbuffer->strings[i], srcstringbuffer->strings[i], stringlen);
4133 ========================
4135 sort buffer by beginnings of strings (cmplength defaults it's length)
4136 "backward == TRUE" means that sorting goes upside-down
4137 void buf_sort(float bufhandle, float cmplength, float backward) = #464;
4138 ========================
4140 void VM_buf_sort (void)
4142 prvm_stringbuffer_t *stringbuffer;
4143 VM_SAFEPARMCOUNT(3, VM_buf_sort);
4145 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4148 VM_Warning("VM_buf_sort: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4151 if(stringbuffer->num_strings <= 0)
4153 VM_Warning("VM_buf_sort: tried to sort empty buffer %i in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4156 stringbuffers_sortlength = (int)PRVM_G_FLOAT(OFS_PARM1);
4157 if(stringbuffers_sortlength <= 0)
4158 stringbuffers_sortlength = 0x7FFFFFFF;
4160 if(!PRVM_G_FLOAT(OFS_PARM2))
4161 qsort(stringbuffer->strings, stringbuffer->num_strings, sizeof(char*), BufStr_SortStringsUP);
4163 qsort(stringbuffer->strings, stringbuffer->num_strings, sizeof(char*), BufStr_SortStringsDOWN);
4165 BufStr_Shrink(stringbuffer);
4169 ========================
4171 concantenates all buffer string into one with "glue" separator and returns it as tempstring
4172 string buf_implode(float bufhandle, string glue) = #465;
4173 ========================
4175 void VM_buf_implode (void)
4177 prvm_stringbuffer_t *stringbuffer;
4178 char k[VM_STRINGTEMP_LENGTH];
4182 VM_SAFEPARMCOUNT(2, VM_buf_implode);
4184 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4185 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
4188 VM_Warning("VM_buf_implode: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4191 if(!stringbuffer->num_strings)
4193 sep = PRVM_G_STRING(OFS_PARM1);
4195 for(l = i = 0;i < stringbuffer->num_strings;i++)
4197 if(stringbuffer->strings[i])
4199 l += (i > 0 ? strlen(sep) : 0) + strlen(stringbuffer->strings[i]);
4200 if (l >= sizeof(k) - 1)
4202 strlcat(k, sep, sizeof(k));
4203 strlcat(k, stringbuffer->strings[i], sizeof(k));
4206 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(k);
4210 ========================
4212 get a string from buffer, returns tempstring, dont str_unzone it!
4213 string bufstr_get(float bufhandle, float string_index) = #465;
4214 ========================
4216 void VM_bufstr_get (void)
4218 prvm_stringbuffer_t *stringbuffer;
4220 VM_SAFEPARMCOUNT(2, VM_bufstr_get);
4222 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
4223 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4226 VM_Warning("VM_bufstr_get: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4229 strindex = (int)PRVM_G_FLOAT(OFS_PARM1);
4232 VM_Warning("VM_bufstr_get: invalid string index %i used in %s\n", strindex, PRVM_NAME);
4235 if (strindex < stringbuffer->num_strings && stringbuffer->strings[strindex])
4236 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(stringbuffer->strings[strindex]);
4240 ========================
4242 copies a string into selected slot of buffer
4243 void bufstr_set(float bufhandle, float string_index, string str) = #466;
4244 ========================
4246 void VM_bufstr_set (void)
4249 prvm_stringbuffer_t *stringbuffer;
4252 VM_SAFEPARMCOUNT(3, VM_bufstr_set);
4254 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4257 VM_Warning("VM_bufstr_set: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4260 strindex = (int)PRVM_G_FLOAT(OFS_PARM1);
4261 if(strindex < 0 || strindex >= 1000000) // huge number of strings
4263 VM_Warning("VM_bufstr_set: invalid string index %i used in %s\n", strindex, PRVM_NAME);
4267 BufStr_Expand(stringbuffer, strindex);
4268 stringbuffer->num_strings = max(stringbuffer->num_strings, strindex + 1);
4270 if(stringbuffer->strings[strindex])
4271 Mem_Free(stringbuffer->strings[strindex]);
4272 stringbuffer->strings[strindex] = NULL;
4274 news = PRVM_G_STRING(OFS_PARM2);
4275 if (news && news[0])
4277 size_t alloclen = strlen(news) + 1;
4278 stringbuffer->strings[strindex] = (char *)Mem_Alloc(prog->progs_mempool, alloclen);
4279 memcpy(stringbuffer->strings[strindex], news, alloclen);
4282 BufStr_Shrink(stringbuffer);
4286 ========================
4288 adds string to buffer in first free slot and returns its index
4289 "order == TRUE" means that string will be added after last "full" slot
4290 float bufstr_add(float bufhandle, string str, float order) = #467;
4291 ========================
4293 void VM_bufstr_add (void)
4295 int order, strindex;
4296 prvm_stringbuffer_t *stringbuffer;
4300 VM_SAFEPARMCOUNT(3, VM_bufstr_add);
4302 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4303 PRVM_G_FLOAT(OFS_RETURN) = -1;
4306 VM_Warning("VM_bufstr_add: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4309 string = PRVM_G_STRING(OFS_PARM1);
4310 if(!string || !string[0])
4312 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);
4315 order = (int)PRVM_G_FLOAT(OFS_PARM2);
4317 strindex = stringbuffer->num_strings;
4319 for (strindex = 0;strindex < stringbuffer->num_strings;strindex++)
4320 if (stringbuffer->strings[strindex] == NULL)
4323 BufStr_Expand(stringbuffer, strindex);
4325 stringbuffer->num_strings = max(stringbuffer->num_strings, strindex + 1);
4326 alloclen = strlen(string) + 1;
4327 stringbuffer->strings[strindex] = (char *)Mem_Alloc(prog->progs_mempool, alloclen);
4328 memcpy(stringbuffer->strings[strindex], string, alloclen);
4330 PRVM_G_FLOAT(OFS_RETURN) = strindex;
4334 ========================
4336 delete string from buffer
4337 void bufstr_free(float bufhandle, float string_index) = #468;
4338 ========================
4340 void VM_bufstr_free (void)
4343 prvm_stringbuffer_t *stringbuffer;
4344 VM_SAFEPARMCOUNT(2, VM_bufstr_free);
4346 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4349 VM_Warning("VM_bufstr_free: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4352 i = (int)PRVM_G_FLOAT(OFS_PARM1);
4355 VM_Warning("VM_bufstr_free: invalid string index %i used in %s\n", i, PRVM_NAME);
4359 if (i < stringbuffer->num_strings)
4361 if(stringbuffer->strings[i])
4362 Mem_Free(stringbuffer->strings[i]);
4363 stringbuffer->strings[i] = NULL;
4366 BufStr_Shrink(stringbuffer);
4375 This was a major timewaster in progs, so it was converted to C
4378 void VM_changeyaw (void)
4381 float ideal, current, move, speed;
4383 // this is called (VERY HACKISHLY) by SV_MoveToGoal, so it can not use any
4384 // parameters because they are the parameters to SV_MoveToGoal, not this
4385 //VM_SAFEPARMCOUNT(0, VM_changeyaw);
4387 ent = PRVM_PROG_TO_EDICT(PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict);
4388 if (ent == prog->edicts)
4390 VM_Warning("changeyaw: can not modify world entity\n");
4393 if (ent->priv.server->free)
4395 VM_Warning("changeyaw: can not modify free entity\n");
4398 if (prog->fieldoffsets.angles < 0 || prog->fieldoffsets.ideal_yaw < 0 || prog->fieldoffsets.yaw_speed < 0)
4400 VM_Warning("changeyaw: angles, ideal_yaw, or yaw_speed field(s) not found\n");
4403 current = ANGLEMOD(PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.angles)->vector[1]);
4404 ideal = PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.ideal_yaw)->_float;
4405 speed = PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.yaw_speed)->_float;
4407 if (current == ideal)
4409 move = ideal - current;
4410 if (ideal > current)
4431 PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.angles)->vector[1] = ANGLEMOD (current + move);
4439 void VM_changepitch (void)
4442 float ideal, current, move, speed;
4444 VM_SAFEPARMCOUNT(1, VM_changepitch);
4446 ent = PRVM_G_EDICT(OFS_PARM0);
4447 if (ent == prog->edicts)
4449 VM_Warning("changepitch: can not modify world entity\n");
4452 if (ent->priv.server->free)
4454 VM_Warning("changepitch: can not modify free entity\n");
4457 if (prog->fieldoffsets.angles < 0 || prog->fieldoffsets.idealpitch < 0 || prog->fieldoffsets.pitch_speed < 0)
4459 VM_Warning("changepitch: angles, idealpitch, or pitch_speed field(s) not found\n");
4462 current = ANGLEMOD(PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.angles)->vector[0]);
4463 ideal = PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.idealpitch)->_float;
4464 speed = PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.pitch_speed)->_float;
4466 if (current == ideal)
4468 move = ideal - current;
4469 if (ideal > current)
4490 PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.angles)->vector[0] = ANGLEMOD (current + move);
4494 void VM_uncolorstring (void)
4496 char szNewString[VM_STRINGTEMP_LENGTH];
4497 const char *szString;
4500 VM_SAFEPARMCOUNT(1, VM_uncolorstring);
4501 szString = PRVM_G_STRING(OFS_PARM0);
4502 COM_StringDecolorize(szString, 0, szNewString, sizeof(szNewString), TRUE);
4503 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(szNewString);
4507 // #221 float(string str, string sub[, float startpos]) strstrofs (FTE_STRINGS)
4508 //strstr, without generating a new string. Use in conjunction with FRIK_FILE's substring for more similar strstr.
4509 void VM_strstrofs (void)
4511 const char *instr, *match;
4513 VM_SAFEPARMCOUNTRANGE(2, 3, VM_strstrofs);
4514 instr = PRVM_G_STRING(OFS_PARM0);
4515 match = PRVM_G_STRING(OFS_PARM1);
4516 firstofs = (prog->argc > 2)?PRVM_G_FLOAT(OFS_PARM2):0;
4518 if (firstofs && (firstofs < 0 || firstofs > (int)strlen(instr)))
4520 PRVM_G_FLOAT(OFS_RETURN) = -1;
4524 match = strstr(instr+firstofs, match);
4526 PRVM_G_FLOAT(OFS_RETURN) = -1;
4528 PRVM_G_FLOAT(OFS_RETURN) = match - instr;
4531 //#222 string(string s, float index) str2chr (FTE_STRINGS)
4532 void VM_str2chr (void)
4535 VM_SAFEPARMCOUNT(2, VM_str2chr);
4536 s = PRVM_G_STRING(OFS_PARM0);
4537 if((unsigned)PRVM_G_FLOAT(OFS_PARM1) < strlen(s))
4538 PRVM_G_FLOAT(OFS_RETURN) = (unsigned char)s[(unsigned)PRVM_G_FLOAT(OFS_PARM1)];
4540 PRVM_G_FLOAT(OFS_RETURN) = 0;
4543 //#223 string(float c, ...) chr2str (FTE_STRINGS)
4544 void VM_chr2str (void)
4548 VM_SAFEPARMCOUNTRANGE(0, 8, VM_chr2str);
4549 for(i = 0;i < prog->argc && i < (int)sizeof(t) - 1;i++)
4550 t[i] = (unsigned char)PRVM_G_FLOAT(OFS_PARM0+i*3);
4552 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(t);
4555 static int chrconv_number(int i, int base, int conv)
4580 static int chrconv_punct(int i, int base, int conv)
4598 static int chrchar_alpha(int i, int basec, int baset, int convc, int convt, int charnum)
4600 //convert case and colour seperatly...
4617 baset = 128*((charnum&1) == (convt-5));
4633 return i + basec + baset;
4635 // #224 string(float ccase, float calpha, float cnum, string s, ...) strconv (FTE_STRINGS)
4636 //bulk convert a string. change case or colouring.
4637 void VM_strconv (void)
4639 int ccase, redalpha, rednum, len, i;
4640 unsigned char resbuf[VM_STRINGTEMP_LENGTH];
4641 unsigned char *result = resbuf;
4643 VM_SAFEPARMCOUNTRANGE(3, 8, VM_strconv);
4645 ccase = PRVM_G_FLOAT(OFS_PARM0); //0 same, 1 lower, 2 upper
4646 redalpha = PRVM_G_FLOAT(OFS_PARM1); //0 same, 1 white, 2 red, 5 alternate, 6 alternate-alternate
4647 rednum = PRVM_G_FLOAT(OFS_PARM2); //0 same, 1 white, 2 red, 3 redspecial, 4 whitespecial, 5 alternate, 6 alternate-alternate
4648 VM_VarString(3, (char *) resbuf, sizeof(resbuf));
4649 len = strlen((char *) resbuf);
4651 for (i = 0; i < len; i++, result++) //should this be done backwards?
4653 if (*result >= '0' && *result <= '9') //normal numbers...
4654 *result = chrconv_number(*result, '0', rednum);
4655 else if (*result >= '0'+128 && *result <= '9'+128)
4656 *result = chrconv_number(*result, '0'+128, rednum);
4657 else if (*result >= '0'+128-30 && *result <= '9'+128-30)
4658 *result = chrconv_number(*result, '0'+128-30, rednum);
4659 else if (*result >= '0'-30 && *result <= '9'-30)
4660 *result = chrconv_number(*result, '0'-30, rednum);
4662 else if (*result >= 'a' && *result <= 'z') //normal numbers...
4663 *result = chrchar_alpha(*result, 'a', 0, ccase, redalpha, i);
4664 else if (*result >= 'A' && *result <= 'Z') //normal numbers...
4665 *result = chrchar_alpha(*result, 'A', 0, ccase, redalpha, i);
4666 else if (*result >= 'a'+128 && *result <= 'z'+128) //normal numbers...
4667 *result = chrchar_alpha(*result, 'a', 128, ccase, redalpha, i);
4668 else if (*result >= 'A'+128 && *result <= 'Z'+128) //normal numbers...
4669 *result = chrchar_alpha(*result, 'A', 128, ccase, redalpha, i);
4671 else if ((*result & 127) < 16 || !redalpha) //special chars..
4673 else if (*result < 128)
4674 *result = chrconv_punct(*result, 0, redalpha);
4676 *result = chrconv_punct(*result, 128, redalpha);
4680 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString((char *) resbuf);
4683 // #225 string(float chars, string s, ...) strpad (FTE_STRINGS)
4684 void VM_strpad (void)
4686 char src[VM_STRINGTEMP_LENGTH];
4687 char destbuf[VM_STRINGTEMP_LENGTH];
4689 VM_SAFEPARMCOUNTRANGE(1, 8, VM_strpad);
4690 pad = PRVM_G_FLOAT(OFS_PARM0);
4691 VM_VarString(1, src, sizeof(src));
4693 // note: < 0 = left padding, > 0 = right padding,
4694 // this is reverse logic of printf!
4695 dpsnprintf(destbuf, sizeof(destbuf), "%*s", -pad, src);
4697 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(destbuf);
4700 // #226 string(string info, string key, string value, ...) infoadd (FTE_STRINGS)
4701 //uses qw style \key\value strings
4702 void VM_infoadd (void)
4704 const char *info, *key;
4705 char value[VM_STRINGTEMP_LENGTH];
4706 char temp[VM_STRINGTEMP_LENGTH];
4708 VM_SAFEPARMCOUNTRANGE(2, 8, VM_infoadd);
4709 info = PRVM_G_STRING(OFS_PARM0);
4710 key = PRVM_G_STRING(OFS_PARM1);
4711 VM_VarString(2, value, sizeof(value));
4713 strlcpy(temp, info, VM_STRINGTEMP_LENGTH);
4715 InfoString_SetValue(temp, VM_STRINGTEMP_LENGTH, key, value);
4717 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(temp);
4720 // #227 string(string info, string key) infoget (FTE_STRINGS)
4721 //uses qw style \key\value strings
4722 void VM_infoget (void)
4726 char value[VM_STRINGTEMP_LENGTH];
4728 VM_SAFEPARMCOUNT(2, VM_infoget);
4729 info = PRVM_G_STRING(OFS_PARM0);
4730 key = PRVM_G_STRING(OFS_PARM1);
4732 InfoString_GetValue(info, key, value, VM_STRINGTEMP_LENGTH);
4734 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(value);
4737 //#228 float(string s1, string s2, float len) strncmp (FTE_STRINGS)
4738 // also float(string s1, string s2) strcmp (FRIK_FILE)
4739 void VM_strncmp (void)
4741 const char *s1, *s2;
4742 VM_SAFEPARMCOUNTRANGE(2, 3, VM_strncmp);
4743 s1 = PRVM_G_STRING(OFS_PARM0);
4744 s2 = PRVM_G_STRING(OFS_PARM1);
4747 PRVM_G_FLOAT(OFS_RETURN) = strncmp(s1, s2, (size_t)PRVM_G_FLOAT(OFS_PARM2));
4751 PRVM_G_FLOAT(OFS_RETURN) = strcmp(s1, s2);
4755 // #229 float(string s1, string s2) strcasecmp (FTE_STRINGS)
4756 // #230 float(string s1, string s2, float len) strncasecmp (FTE_STRINGS)
4757 void VM_strncasecmp (void)
4759 const char *s1, *s2;
4760 VM_SAFEPARMCOUNTRANGE(2, 3, VM_strncasecmp);
4761 s1 = PRVM_G_STRING(OFS_PARM0);
4762 s2 = PRVM_G_STRING(OFS_PARM1);
4765 PRVM_G_FLOAT(OFS_RETURN) = strncasecmp(s1, s2, (size_t)PRVM_G_FLOAT(OFS_PARM2));
4769 PRVM_G_FLOAT(OFS_RETURN) = strcasecmp(s1, s2);
4773 // #494 float(float caseinsensitive, string s, ...) crc16
4777 static char s[VM_STRINGTEMP_LENGTH];
4778 VM_SAFEPARMCOUNTRANGE(2, 8, VM_hash);
4779 insensitive = PRVM_G_FLOAT(OFS_PARM0);
4780 VM_VarString(1, s, sizeof(s));
4781 PRVM_G_FLOAT(OFS_RETURN) = (unsigned short) ((insensitive ? CRC_Block_CaseInsensitive : CRC_Block) ((unsigned char *) s, strlen(s)));
4784 void VM_wasfreed (void)
4786 VM_SAFEPARMCOUNT(1, VM_wasfreed);
4787 PRVM_G_FLOAT(OFS_RETURN) = PRVM_G_EDICT(OFS_PARM0)->priv.required->free;
4790 void VM_SetTraceGlobals(const trace_t *trace)
4793 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_allsolid)))
4794 val->_float = trace->allsolid;
4795 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_startsolid)))
4796 val->_float = trace->startsolid;
4797 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_fraction)))
4798 val->_float = trace->fraction;
4799 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_inwater)))
4800 val->_float = trace->inwater;
4801 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_inopen)))
4802 val->_float = trace->inopen;
4803 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_endpos)))
4804 VectorCopy(trace->endpos, val->vector);
4805 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_plane_normal)))
4806 VectorCopy(trace->plane.normal, val->vector);
4807 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_plane_dist)))
4808 val->_float = trace->plane.dist;
4809 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_ent)))
4810 val->edict = PRVM_EDICT_TO_PROG(trace->ent ? trace->ent : prog->edicts);
4811 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dpstartcontents)))
4812 val->_float = trace->startsupercontents;
4813 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dphitcontents)))
4814 val->_float = trace->hitsupercontents;
4815 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dphitq3surfaceflags)))
4816 val->_float = trace->hitq3surfaceflags;
4817 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dphittexturename)))
4818 val->string = trace->hittexture ? PRVM_SetTempString(trace->hittexture->name) : 0;
4823 void VM_Cmd_Init(void)
4825 // only init the stuff for the current prog
4829 // VM_BufStr_Init();
4832 void VM_Cmd_Reset(void)
4834 CL_PurgeOwner( MENUOWNER );
4836 VM_Files_CloseAll();
4838 // VM_BufStr_ShutDown();
4841 // #510 string(string input, ...) uri_escape (DP_QC_URI_ESCAPE)
4842 // does URI escaping on a string (replace evil stuff by %AB escapes)
4843 void VM_uri_escape (void)
4845 char src[VM_STRINGTEMP_LENGTH];
4846 char dest[VM_STRINGTEMP_LENGTH];
4848 static const char *hex = "0123456789ABCDEF";
4850 VM_SAFEPARMCOUNTRANGE(1, 8, VM_uri_escape);
4851 VM_VarString(0, src, sizeof(src));
4853 for(p = src, q = dest; *p && q < dest + sizeof(dest) - 3; ++p)
4855 if((*p >= 'A' && *p <= 'Z')
4856 || (*p >= 'a' && *p <= 'z')
4857 || (*p >= '0' && *p <= '9')
4858 || (*p == '-') || (*p == '_') || (*p == '.')
4859 || (*p == '!') || (*p == '~') || (*p == '*')
4860 || (*p == '\'') || (*p == '(') || (*p == ')'))
4865 *q++ = hex[(*(unsigned char *)p >> 4) & 0xF];
4866 *q++ = hex[ *(unsigned char *)p & 0xF];
4871 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(dest);
4874 // #510 string(string input, ...) uri_unescape (DP_QC_URI_ESCAPE)
4875 // does URI unescaping on a string (get back the evil stuff)
4876 void VM_uri_unescape (void)
4878 char src[VM_STRINGTEMP_LENGTH];
4879 char dest[VM_STRINGTEMP_LENGTH];
4883 VM_SAFEPARMCOUNTRANGE(1, 8, VM_uri_unescape);
4884 VM_VarString(0, src, sizeof(src));
4886 for(p = src, q = dest; *p; ) // no need to check size, because unescape can't expand
4890 if(p[1] >= '0' && p[1] <= '9')
4892 else if(p[1] >= 'a' && p[1] <= 'f')
4893 hi = p[1] - 'a' + 10;
4894 else if(p[1] >= 'A' && p[1] <= 'F')
4895 hi = p[1] - 'A' + 10;
4898 if(p[2] >= '0' && p[2] <= '9')
4900 else if(p[2] >= 'a' && p[2] <= 'f')
4901 lo = p[2] - 'a' + 10;
4902 else if(p[2] >= 'A' && p[2] <= 'F')
4903 lo = p[2] - 'A' + 10;
4906 if(hi != 0 || lo != 0) // don't unescape NUL bytes
4907 *q++ = (char) (hi * 0x10 + lo);
4918 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(dest);
4921 // #502 string(string filename) whichpack (DP_QC_WHICHPACK)
4922 // returns the name of the pack containing a file, or "" if it is not in any pack (but local or non-existant)
4923 void VM_whichpack (void)
4925 const char *fn, *pack;
4927 VM_SAFEPARMCOUNT(1, VM_whichpack);
4928 fn = PRVM_G_STRING(OFS_PARM0);
4929 pack = FS_WhichPack(fn);
4931 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(pack ? pack : "");
4939 char buffer[MAX_INPUTLINE];
4943 static void uri_to_string_callback(int status, size_t length_received, unsigned char *buffer, void *cbdata)
4945 uri_to_prog_t *handle = cbdata;
4947 if(!PRVM_ProgLoaded(handle->prognr))
4949 // curl reply came too late... so just drop it
4954 PRVM_SetProg(handle->prognr);
4956 if((prog->starttime == handle->starttime) && (prog->funcoffsets.URI_Get_Callback))
4958 if(length_received >= sizeof(handle->buffer))
4959 length_received = sizeof(handle->buffer) - 1;
4960 handle->buffer[length_received] = 0;
4962 PRVM_G_FLOAT(OFS_PARM0) = handle->id;
4963 PRVM_G_FLOAT(OFS_PARM1) = status;
4964 PRVM_G_INT(OFS_PARM2) = PRVM_SetTempString(handle->buffer);
4965 PRVM_ExecuteProgram(prog->funcoffsets.URI_Get_Callback, "QC function URI_Get_Callback is missing");
4972 // uri_get() gets content from an URL and calls a callback "uri_get_callback" with it set as string; an unique ID of the transfer is returned
4973 // returns 1 on success, and then calls the callback with the ID, 0 or the HTTP status code, and the received data in a string
4974 void VM_uri_get (void)
4979 uri_to_prog_t *handle;
4981 if(!prog->funcoffsets.URI_Get_Callback)
4982 PRVM_ERROR("uri_get called by %s without URI_Get_Callback defined", PRVM_NAME);
4984 VM_SAFEPARMCOUNT(2, VM_uri_get);
4986 url = PRVM_G_STRING(OFS_PARM0);
4987 id = PRVM_G_FLOAT(OFS_PARM1);
4988 handle = Z_Malloc(sizeof(*handle)); // this can't be the prog's mem pool, as curl may call the callback later!
4990 handle->prognr = PRVM_GetProgNr();
4991 handle->starttime = prog->starttime;
4993 ret = Curl_Begin_ToMemory(url, (unsigned char *) handle->buffer, sizeof(handle->buffer), uri_to_string_callback, handle);
4996 PRVM_G_INT(OFS_RETURN) = 1;
5001 PRVM_G_INT(OFS_RETURN) = 0;
5005 void VM_netaddress_resolve (void)
5008 char normalized[128];
5010 lhnetaddress_t addr;
5012 VM_SAFEPARMCOUNTRANGE(1, 2, VM_netaddress_resolve);
5014 ip = PRVM_G_STRING(OFS_PARM0);
5017 port = PRVM_G_FLOAT(OFS_PARM1);
5019 if(LHNETADDRESS_FromString(&addr, ip, port) && LHNETADDRESS_ToString(&addr, normalized, sizeof(normalized), prog->argc > 1))
5020 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(normalized);
5022 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString("");