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;
442 float CVAR_TYPEFLAG_READONLY = 32;
445 void VM_cvar_type (void)
447 char string[VM_STRINGTEMP_LENGTH];
451 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar);
452 VM_VarString(0, string, sizeof(string));
453 VM_CheckEmptyString(string);
454 cvar = Cvar_FindVar(string);
459 PRVM_G_FLOAT(OFS_RETURN) = 0;
460 return; // CVAR_TYPE_NONE
463 ret = 1; // CVAR_EXISTS
464 if(cvar->flags & CVAR_SAVE)
465 ret |= 2; // CVAR_TYPE_SAVED
466 if(cvar->flags & CVAR_PRIVATE)
467 ret |= 4; // CVAR_TYPE_PRIVATE
468 if(!(cvar->flags & CVAR_ALLOCATED))
469 ret |= 8; // CVAR_TYPE_ENGINE
470 if(cvar->description != cvar_dummy_description)
471 ret |= 16; // CVAR_TYPE_HASDESCRIPTION
472 if(cvar->flags & CVAR_READONLY)
473 ret |= 32; // CVAR_TYPE_READONLY
475 PRVM_G_FLOAT(OFS_RETURN) = ret;
482 const string VM_cvar_string (string, ...)
485 void VM_cvar_string(void)
487 char string[VM_STRINGTEMP_LENGTH];
488 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar_string);
489 VM_VarString(0, string, sizeof(string));
490 VM_CheckEmptyString(string);
491 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(Cvar_VariableString(string));
496 ========================
499 const string VM_cvar_defstring (string, ...)
500 ========================
502 void VM_cvar_defstring (void)
504 char string[VM_STRINGTEMP_LENGTH];
505 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar_defstring);
506 VM_VarString(0, string, sizeof(string));
507 VM_CheckEmptyString(string);
508 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(Cvar_VariableDefString(string));
512 ========================
515 const string VM_cvar_description (string, ...)
516 ========================
518 void VM_cvar_description (void)
520 char string[VM_STRINGTEMP_LENGTH];
521 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar_description);
522 VM_VarString(0, string, sizeof(string));
523 VM_CheckEmptyString(string);
524 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(Cvar_VariableDescription(string));
530 void cvar_set (string,string, ...)
533 void VM_cvar_set (void)
536 char string[VM_STRINGTEMP_LENGTH];
537 VM_SAFEPARMCOUNTRANGE(2,8,VM_cvar_set);
538 VM_VarString(1, string, sizeof(string));
539 name = PRVM_G_STRING(OFS_PARM0);
540 VM_CheckEmptyString(name);
541 Cvar_Set(name, string);
551 void VM_dprint (void)
553 char string[VM_STRINGTEMP_LENGTH];
554 VM_SAFEPARMCOUNTRANGE(1, 8, VM_dprint);
555 if (developer.integer)
557 VM_VarString(0, string, sizeof(string));
559 Con_Printf("%s", string);
561 Con_Printf("%s: %s", PRVM_NAME, string);
579 VM_SAFEPARMCOUNT(1, VM_ftos);
581 v = PRVM_G_FLOAT(OFS_PARM0);
583 if ((float)((int)v) == v)
584 dpsnprintf(s, sizeof(s), "%i", (int)v);
586 dpsnprintf(s, sizeof(s), "%f", v);
587 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(s);
602 VM_SAFEPARMCOUNT(1,VM_fabs);
604 v = PRVM_G_FLOAT(OFS_PARM0);
605 PRVM_G_FLOAT(OFS_RETURN) = fabs(v);
620 VM_SAFEPARMCOUNT(1,VM_vtos);
622 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]);
623 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(s);
638 VM_SAFEPARMCOUNT(1, VM_etos);
640 dpsnprintf (s, sizeof(s), "entity %i", PRVM_G_EDICTNUM(OFS_PARM0));
641 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(s);
648 float stof(...[string])
653 char string[VM_STRINGTEMP_LENGTH];
654 VM_SAFEPARMCOUNTRANGE(1, 8, VM_stof);
655 VM_VarString(0, string, sizeof(string));
656 PRVM_G_FLOAT(OFS_RETURN) = atof(string);
660 ========================
664 ========================
668 VM_SAFEPARMCOUNT(1, VM_itof);
669 PRVM_G_FLOAT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
673 ========================
676 entity ftoe(float num)
677 ========================
682 VM_SAFEPARMCOUNT(1, VM_ftoe);
684 ent = (int)PRVM_G_FLOAT(OFS_PARM0);
685 if (ent < 0 || ent >= MAX_EDICTS || PRVM_PROG_TO_EDICT(ent)->priv.required->free)
686 ent = 0; // return world instead of a free or invalid entity
688 PRVM_G_INT(OFS_RETURN) = ent;
692 ========================
695 float etof(entity ent)
696 ========================
700 VM_SAFEPARMCOUNT(1, VM_etof);
701 PRVM_G_FLOAT(OFS_RETURN) = PRVM_G_EDICTNUM(OFS_PARM0);
708 string strftime(float uselocaltime, string[, string ...])
711 void VM_strftime(void)
720 char fmt[VM_STRINGTEMP_LENGTH];
721 char result[VM_STRINGTEMP_LENGTH];
722 VM_SAFEPARMCOUNTRANGE(2, 8, VM_strftime);
723 VM_VarString(1, fmt, sizeof(fmt));
726 if (PRVM_G_FLOAT(OFS_PARM0))
727 tmresult = localtime_s(&tm, &t);
729 tmresult = gmtime_s(&tm, &t);
732 if (PRVM_G_FLOAT(OFS_PARM0))
739 PRVM_G_INT(OFS_RETURN) = 0;
743 strftime(result, sizeof(result), fmt, &tm);
745 strftime(result, sizeof(result), fmt, tm);
747 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(result);
761 VM_SAFEPARMCOUNT(0, VM_spawn);
762 prog->xfunction->builtinsprofile += 20;
763 ed = PRVM_ED_Alloc();
775 void VM_remove (void)
778 prog->xfunction->builtinsprofile += 20;
780 VM_SAFEPARMCOUNT(1, VM_remove);
782 ed = PRVM_G_EDICT(OFS_PARM0);
783 if( PRVM_NUM_FOR_EDICT(ed) <= prog->reserved_edicts )
785 if (developer.integer >= 1)
786 VM_Warning( "VM_remove: tried to remove the null entity or a reserved entity!\n" );
788 else if( ed->priv.required->free )
790 if (developer.integer >= 1)
791 VM_Warning( "VM_remove: tried to remove an already freed entity!\n" );
801 entity find(entity start, .string field, string match)
812 VM_SAFEPARMCOUNT(3,VM_find);
814 e = PRVM_G_EDICTNUM(OFS_PARM0);
815 f = PRVM_G_INT(OFS_PARM1);
816 s = PRVM_G_STRING(OFS_PARM2);
818 // LordHavoc: apparently BloodMage does a find(world, weaponmodel, "") and
819 // expects it to find all the monsters, so we must be careful to support
822 for (e++ ; e < prog->num_edicts ; e++)
824 prog->xfunction->builtinsprofile++;
825 ed = PRVM_EDICT_NUM(e);
826 if (ed->priv.required->free)
828 t = PRVM_E_STRING(ed,f);
838 VM_RETURN_EDICT(prog->edicts);
845 entity findfloat(entity start, .float field, float match)
846 entity findentity(entity start, .entity field, entity match)
849 // LordHavoc: added this for searching float, int, and entity reference fields
850 void VM_findfloat (void)
857 VM_SAFEPARMCOUNT(3,VM_findfloat);
859 e = PRVM_G_EDICTNUM(OFS_PARM0);
860 f = PRVM_G_INT(OFS_PARM1);
861 s = PRVM_G_FLOAT(OFS_PARM2);
863 for (e++ ; e < prog->num_edicts ; e++)
865 prog->xfunction->builtinsprofile++;
866 ed = PRVM_EDICT_NUM(e);
867 if (ed->priv.required->free)
869 if (PRVM_E_FLOAT(ed,f) == s)
876 VM_RETURN_EDICT(prog->edicts);
883 entity findchain(.string field, string match)
886 // chained search for strings in entity fields
887 // entity(.string field, string match) findchain = #402;
888 void VM_findchain (void)
893 prvm_edict_t *ent, *chain;
895 VM_SAFEPARMCOUNT(2,VM_findchain);
897 if (prog->fieldoffsets.chain < 0)
898 PRVM_ERROR("VM_findchain: %s doesnt have a chain field !", PRVM_NAME);
900 chain = prog->edicts;
902 f = PRVM_G_INT(OFS_PARM0);
903 s = PRVM_G_STRING(OFS_PARM1);
905 // LordHavoc: apparently BloodMage does a find(world, weaponmodel, "") and
906 // expects it to find all the monsters, so we must be careful to support
909 ent = PRVM_NEXT_EDICT(prog->edicts);
910 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
912 prog->xfunction->builtinsprofile++;
913 if (ent->priv.required->free)
915 t = PRVM_E_STRING(ent,f);
921 PRVM_EDICTFIELDVALUE(ent,prog->fieldoffsets.chain)->edict = PRVM_NUM_FOR_EDICT(chain);
925 VM_RETURN_EDICT(chain);
932 entity findchainfloat(.string field, float match)
933 entity findchainentity(.string field, entity match)
936 // LordHavoc: chained search for float, int, and entity reference fields
937 // entity(.string field, float match) findchainfloat = #403;
938 void VM_findchainfloat (void)
943 prvm_edict_t *ent, *chain;
945 VM_SAFEPARMCOUNT(2, VM_findchainfloat);
947 if (prog->fieldoffsets.chain < 0)
948 PRVM_ERROR("VM_findchainfloat: %s doesnt have a chain field !", PRVM_NAME);
950 chain = (prvm_edict_t *)prog->edicts;
952 f = PRVM_G_INT(OFS_PARM0);
953 s = PRVM_G_FLOAT(OFS_PARM1);
955 ent = PRVM_NEXT_EDICT(prog->edicts);
956 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
958 prog->xfunction->builtinsprofile++;
959 if (ent->priv.required->free)
961 if (PRVM_E_FLOAT(ent,f) != s)
964 PRVM_EDICTFIELDVALUE(ent,prog->fieldoffsets.chain)->edict = PRVM_EDICT_TO_PROG(chain);
968 VM_RETURN_EDICT(chain);
972 ========================
975 entity findflags(entity start, .float field, float match)
976 ========================
978 // LordHavoc: search for flags in float fields
979 void VM_findflags (void)
986 VM_SAFEPARMCOUNT(3, VM_findflags);
989 e = PRVM_G_EDICTNUM(OFS_PARM0);
990 f = PRVM_G_INT(OFS_PARM1);
991 s = (int)PRVM_G_FLOAT(OFS_PARM2);
993 for (e++ ; e < prog->num_edicts ; e++)
995 prog->xfunction->builtinsprofile++;
996 ed = PRVM_EDICT_NUM(e);
997 if (ed->priv.required->free)
999 if (!PRVM_E_FLOAT(ed,f))
1001 if ((int)PRVM_E_FLOAT(ed,f) & s)
1003 VM_RETURN_EDICT(ed);
1008 VM_RETURN_EDICT(prog->edicts);
1012 ========================
1015 entity findchainflags(.float field, float match)
1016 ========================
1018 // LordHavoc: chained search for flags in float fields
1019 void VM_findchainflags (void)
1024 prvm_edict_t *ent, *chain;
1026 VM_SAFEPARMCOUNT(2, VM_findchainflags);
1028 if (prog->fieldoffsets.chain < 0)
1029 PRVM_ERROR("VM_findchainflags: %s doesnt have a chain field !", PRVM_NAME);
1031 chain = (prvm_edict_t *)prog->edicts;
1033 f = PRVM_G_INT(OFS_PARM0);
1034 s = (int)PRVM_G_FLOAT(OFS_PARM1);
1036 ent = PRVM_NEXT_EDICT(prog->edicts);
1037 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
1039 prog->xfunction->builtinsprofile++;
1040 if (ent->priv.required->free)
1042 if (!PRVM_E_FLOAT(ent,f))
1044 if (!((int)PRVM_E_FLOAT(ent,f) & s))
1047 PRVM_EDICTFIELDVALUE(ent,prog->fieldoffsets.chain)->edict = PRVM_EDICT_TO_PROG(chain);
1051 VM_RETURN_EDICT(chain);
1058 string precache_sound (string sample)
1061 void VM_precache_sound (void)
1065 VM_SAFEPARMCOUNT(1, VM_precache_sound);
1067 s = PRVM_G_STRING(OFS_PARM0);
1068 PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
1069 VM_CheckEmptyString(s);
1071 if(snd_initialized.integer && !S_PrecacheSound(s, true, false))
1073 VM_Warning("VM_precache_sound: Failed to load %s for %s\n", s, PRVM_NAME);
1082 returns the same string as output
1084 does nothing, only used by qcc to build .pak archives
1087 void VM_precache_file (void)
1089 VM_SAFEPARMCOUNT(1,VM_precache_file);
1090 // precache_file is only used to copy files with qcc, it does nothing
1091 PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
1101 void VM_coredump (void)
1103 VM_SAFEPARMCOUNT(0,VM_coredump);
1105 Cbuf_AddText("prvm_edicts ");
1106 Cbuf_AddText(PRVM_NAME);
1117 void PRVM_StackTrace(void);
1118 void VM_stackdump (void)
1120 VM_SAFEPARMCOUNT(0, VM_stackdump);
1135 VM_SAFEPARMCOUNT(0, VM_crash);
1137 PRVM_ERROR("Crash called by %s",PRVM_NAME);
1147 void VM_traceon (void)
1149 VM_SAFEPARMCOUNT(0,VM_traceon);
1161 void VM_traceoff (void)
1163 VM_SAFEPARMCOUNT(0,VM_traceoff);
1165 prog->trace = false;
1175 void VM_eprint (void)
1177 VM_SAFEPARMCOUNT(1,VM_eprint);
1179 PRVM_ED_PrintNum (PRVM_G_EDICTNUM(OFS_PARM0), NULL);
1192 VM_SAFEPARMCOUNT(1,VM_rint);
1194 f = PRVM_G_FLOAT(OFS_PARM0);
1196 PRVM_G_FLOAT(OFS_RETURN) = floor(f + 0.5);
1198 PRVM_G_FLOAT(OFS_RETURN) = ceil(f - 0.5);
1208 void VM_floor (void)
1210 VM_SAFEPARMCOUNT(1,VM_floor);
1212 PRVM_G_FLOAT(OFS_RETURN) = floor(PRVM_G_FLOAT(OFS_PARM0));
1224 VM_SAFEPARMCOUNT(1,VM_ceil);
1226 PRVM_G_FLOAT(OFS_RETURN) = ceil(PRVM_G_FLOAT(OFS_PARM0));
1234 entity nextent(entity)
1237 void VM_nextent (void)
1242 VM_SAFEPARMCOUNT(1, VM_nextent);
1244 i = PRVM_G_EDICTNUM(OFS_PARM0);
1247 prog->xfunction->builtinsprofile++;
1249 if (i == prog->num_edicts)
1251 VM_RETURN_EDICT(prog->edicts);
1254 ent = PRVM_EDICT_NUM(i);
1255 if (!ent->priv.required->free)
1257 VM_RETURN_EDICT(ent);
1263 //=============================================================================
1270 changelevel(string map)
1273 void VM_changelevel (void)
1275 VM_SAFEPARMCOUNT(1, VM_changelevel);
1279 VM_Warning("VM_changelevel: game is not server (%s)\n", PRVM_NAME);
1283 // make sure we don't issue two changelevels
1284 if (svs.changelevel_issued)
1286 svs.changelevel_issued = true;
1288 Cbuf_AddText (va("changelevel %s\n",PRVM_G_STRING(OFS_PARM0)));
1300 VM_SAFEPARMCOUNT(1,VM_sin);
1301 PRVM_G_FLOAT(OFS_RETURN) = sin(PRVM_G_FLOAT(OFS_PARM0));
1312 VM_SAFEPARMCOUNT(1,VM_cos);
1313 PRVM_G_FLOAT(OFS_RETURN) = cos(PRVM_G_FLOAT(OFS_PARM0));
1325 VM_SAFEPARMCOUNT(1,VM_sqrt);
1326 PRVM_G_FLOAT(OFS_RETURN) = sqrt(PRVM_G_FLOAT(OFS_PARM0));
1338 VM_SAFEPARMCOUNT(1,VM_asin);
1339 PRVM_G_FLOAT(OFS_RETURN) = asin(PRVM_G_FLOAT(OFS_PARM0));
1350 VM_SAFEPARMCOUNT(1,VM_acos);
1351 PRVM_G_FLOAT(OFS_RETURN) = acos(PRVM_G_FLOAT(OFS_PARM0));
1362 VM_SAFEPARMCOUNT(1,VM_atan);
1363 PRVM_G_FLOAT(OFS_RETURN) = atan(PRVM_G_FLOAT(OFS_PARM0));
1369 float atan2(float,float)
1372 void VM_atan2 (void)
1374 VM_SAFEPARMCOUNT(2,VM_atan2);
1375 PRVM_G_FLOAT(OFS_RETURN) = atan2(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1386 VM_SAFEPARMCOUNT(1,VM_tan);
1387 PRVM_G_FLOAT(OFS_RETURN) = tan(PRVM_G_FLOAT(OFS_PARM0));
1394 Returns a vector of length < 1 and > 0
1399 void VM_randomvec (void)
1404 VM_SAFEPARMCOUNT(0, VM_randomvec);
1409 temp[0] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1410 temp[1] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1411 temp[2] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1413 while (DotProduct(temp, temp) >= 1);
1414 VectorCopy (temp, PRVM_G_VECTOR(OFS_RETURN));
1417 temp[0] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1418 temp[1] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1419 temp[2] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1420 // length returned always > 0
1421 length = (rand()&32766 + 1) * (1.0 / 32767.0) / VectorLength(temp);
1422 VectorScale(temp,length, temp);*/
1423 //VectorCopy(temp, PRVM_G_VECTOR(OFS_RETURN));
1426 //=============================================================================
1432 float registercvar (string name, string value[, float flags])
1435 void VM_registercvar (void)
1437 const char *name, *value;
1440 VM_SAFEPARMCOUNTRANGE(2, 3, VM_registercvar);
1442 name = PRVM_G_STRING(OFS_PARM0);
1443 value = PRVM_G_STRING(OFS_PARM1);
1444 flags = prog->argc >= 3 ? (int)PRVM_G_FLOAT(OFS_PARM2) : 0;
1445 PRVM_G_FLOAT(OFS_RETURN) = 0;
1447 if(flags > CVAR_MAXFLAGSVAL)
1450 // first check to see if it has already been defined
1451 if (Cvar_FindVar (name))
1454 // check for overlap with a command
1455 if (Cmd_Exists (name))
1457 VM_Warning("VM_registercvar: %s is a command\n", name);
1461 Cvar_Get(name, value, flags, NULL);
1463 PRVM_G_FLOAT(OFS_RETURN) = 1; // success
1471 returns the minimum of two supplied floats
1473 float min(float a, float b, ...[float])
1478 VM_SAFEPARMCOUNTRANGE(2, 8, VM_min);
1479 // LordHavoc: 3+ argument enhancement suggested by FrikaC
1480 if (prog->argc >= 3)
1483 float f = PRVM_G_FLOAT(OFS_PARM0);
1484 for (i = 1;i < prog->argc;i++)
1485 if (f > PRVM_G_FLOAT((OFS_PARM0+i*3)))
1486 f = PRVM_G_FLOAT((OFS_PARM0+i*3));
1487 PRVM_G_FLOAT(OFS_RETURN) = f;
1490 PRVM_G_FLOAT(OFS_RETURN) = min(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1497 returns the maximum of two supplied floats
1499 float max(float a, float b, ...[float])
1504 VM_SAFEPARMCOUNTRANGE(2, 8, VM_max);
1505 // LordHavoc: 3+ argument enhancement suggested by FrikaC
1506 if (prog->argc >= 3)
1509 float f = PRVM_G_FLOAT(OFS_PARM0);
1510 for (i = 1;i < prog->argc;i++)
1511 if (f < PRVM_G_FLOAT((OFS_PARM0+i*3)))
1512 f = PRVM_G_FLOAT((OFS_PARM0+i*3));
1513 PRVM_G_FLOAT(OFS_RETURN) = f;
1516 PRVM_G_FLOAT(OFS_RETURN) = max(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1523 returns number bounded by supplied range
1525 float bound(float min, float value, float max)
1528 void VM_bound (void)
1530 VM_SAFEPARMCOUNT(3,VM_bound);
1531 PRVM_G_FLOAT(OFS_RETURN) = bound(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1), PRVM_G_FLOAT(OFS_PARM2));
1538 returns a raised to power b
1540 float pow(float a, float b)
1545 VM_SAFEPARMCOUNT(2,VM_pow);
1546 PRVM_G_FLOAT(OFS_RETURN) = pow(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1549 void VM_Files_Init(void)
1552 for (i = 0;i < PRVM_MAX_OPENFILES;i++)
1553 prog->openfiles[i] = NULL;
1556 void VM_Files_CloseAll(void)
1559 for (i = 0;i < PRVM_MAX_OPENFILES;i++)
1561 if (prog->openfiles[i])
1562 FS_Close(prog->openfiles[i]);
1563 prog->openfiles[i] = NULL;
1567 static qfile_t *VM_GetFileHandle( int index )
1569 if (index < 0 || index >= PRVM_MAX_OPENFILES)
1571 Con_Printf("VM_GetFileHandle: invalid file handle %i used in %s\n", index, PRVM_NAME);
1574 if (prog->openfiles[index] == NULL)
1576 Con_Printf("VM_GetFileHandle: no such file handle %i (or file has been closed) in %s\n", index, PRVM_NAME);
1579 return prog->openfiles[index];
1586 float fopen(string filename, float mode)
1589 // float(string filename, float mode) fopen = #110;
1590 // opens a file inside quake/gamedir/data/ (mode is FILE_READ, FILE_APPEND, or FILE_WRITE),
1591 // returns fhandle >= 0 if successful, or fhandle < 0 if unable to open file for any reason
1595 const char *modestring, *filename;
1597 VM_SAFEPARMCOUNT(2,VM_fopen);
1599 for (filenum = 0;filenum < PRVM_MAX_OPENFILES;filenum++)
1600 if (prog->openfiles[filenum] == NULL)
1602 if (filenum >= PRVM_MAX_OPENFILES)
1604 PRVM_G_FLOAT(OFS_RETURN) = -2;
1605 VM_Warning("VM_fopen: %s ran out of file handles (%i)\n", PRVM_NAME, PRVM_MAX_OPENFILES);
1608 filename = PRVM_G_STRING(OFS_PARM0);
1609 mode = (int)PRVM_G_FLOAT(OFS_PARM1);
1612 case 0: // FILE_READ
1614 prog->openfiles[filenum] = FS_OpenVirtualFile(va("data/%s", filename), false);
1615 if (prog->openfiles[filenum] == NULL)
1616 prog->openfiles[filenum] = FS_OpenVirtualFile(va("%s", filename), false);
1618 case 1: // FILE_APPEND
1620 prog->openfiles[filenum] = FS_OpenRealFile(va("data/%s", filename), modestring, false);
1622 case 2: // FILE_WRITE
1624 prog->openfiles[filenum] = FS_OpenRealFile(va("data/%s", filename), modestring, false);
1627 PRVM_G_FLOAT(OFS_RETURN) = -3;
1628 VM_Warning("VM_fopen: %s: no such mode %i (valid: 0 = read, 1 = append, 2 = write)\n", PRVM_NAME, mode);
1632 if (prog->openfiles[filenum] == NULL)
1634 PRVM_G_FLOAT(OFS_RETURN) = -1;
1635 if (developer.integer >= 100)
1636 VM_Warning("VM_fopen: %s: %s mode %s failed\n", PRVM_NAME, filename, modestring);
1640 PRVM_G_FLOAT(OFS_RETURN) = filenum;
1641 if (developer.integer >= 100)
1642 Con_Printf("VM_fopen: %s: %s mode %s opened as #%i\n", PRVM_NAME, filename, modestring, filenum);
1643 prog->openfiles_origin[filenum] = PRVM_AllocationOrigin();
1651 fclose(float fhandle)
1654 //void(float fhandle) fclose = #111; // closes a file
1655 void VM_fclose(void)
1659 VM_SAFEPARMCOUNT(1,VM_fclose);
1661 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1662 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
1664 VM_Warning("VM_fclose: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1667 if (prog->openfiles[filenum] == NULL)
1669 VM_Warning("VM_fclose: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1672 FS_Close(prog->openfiles[filenum]);
1673 prog->openfiles[filenum] = NULL;
1674 if(prog->openfiles_origin[filenum])
1675 PRVM_Free((char *)prog->openfiles_origin[filenum]);
1676 if (developer.integer >= 100)
1677 Con_Printf("VM_fclose: %s: #%i closed\n", PRVM_NAME, filenum);
1684 string fgets(float fhandle)
1687 //string(float fhandle) fgets = #112; // reads a line of text from the file and returns as a tempstring
1691 char string[VM_STRINGTEMP_LENGTH];
1694 VM_SAFEPARMCOUNT(1,VM_fgets);
1696 // set the return value regardless of any possible errors
1697 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
1699 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1700 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
1702 VM_Warning("VM_fgets: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1705 if (prog->openfiles[filenum] == NULL)
1707 VM_Warning("VM_fgets: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1713 c = FS_Getc(prog->openfiles[filenum]);
1714 if (c == '\r' || c == '\n' || c < 0)
1716 if (end < VM_STRINGTEMP_LENGTH - 1)
1720 // remove \n following \r
1723 c = FS_Getc(prog->openfiles[filenum]);
1725 FS_UnGetc(prog->openfiles[filenum], (unsigned char)c);
1727 if (developer.integer >= 100)
1728 Con_Printf("fgets: %s: %s\n", PRVM_NAME, string);
1730 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string);
1737 fputs(float fhandle, string s)
1740 //void(float fhandle, string s) fputs = #113; // writes a line of text to the end of the file
1744 char string[VM_STRINGTEMP_LENGTH];
1747 VM_SAFEPARMCOUNT(2,VM_fputs);
1749 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1750 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
1752 VM_Warning("VM_fputs: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1755 if (prog->openfiles[filenum] == NULL)
1757 VM_Warning("VM_fputs: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1760 VM_VarString(1, string, sizeof(string));
1761 if ((stringlength = (int)strlen(string)))
1762 FS_Write(prog->openfiles[filenum], string, stringlength);
1763 if (developer.integer >= 100)
1764 Con_Printf("fputs: %s: %s\n", PRVM_NAME, string);
1771 writetofile(float fhandle, entity ent)
1774 void VM_writetofile(void)
1779 VM_SAFEPARMCOUNT(2, VM_writetofile);
1781 file = VM_GetFileHandle( (int)PRVM_G_FLOAT(OFS_PARM0) );
1784 VM_Warning("VM_writetofile: invalid or closed file handle\n");
1788 ent = PRVM_G_EDICT(OFS_PARM1);
1789 if(ent->priv.required->free)
1791 VM_Warning("VM_writetofile: %s: entity %i is free !\n", PRVM_NAME, PRVM_NUM_FOR_EDICT(ent));
1795 PRVM_ED_Write (file, ent);
1798 // KrimZon - DP_QC_ENTITYDATA
1803 float() numentityfields
1804 Return the number of entity fields - NOT offsets
1807 void VM_numentityfields(void)
1809 PRVM_G_FLOAT(OFS_RETURN) = prog->progs->numfielddefs;
1812 // KrimZon - DP_QC_ENTITYDATA
1817 string(float fieldnum) entityfieldname
1818 Return name of the specified field as a string, or empty if the field is invalid (warning)
1821 void VM_entityfieldname(void)
1824 int i = (int)PRVM_G_FLOAT(OFS_PARM0);
1826 if (i < 0 || i >= prog->progs->numfielddefs)
1828 VM_Warning("VM_entityfieldname: %s: field index out of bounds\n", PRVM_NAME);
1829 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString("");
1833 d = &prog->fielddefs[i];
1834 PRVM_G_INT(OFS_RETURN) = d->s_name; // presuming that s_name points to a string already
1837 // KrimZon - DP_QC_ENTITYDATA
1842 float(float fieldnum) entityfieldtype
1845 void VM_entityfieldtype(void)
1848 int i = (int)PRVM_G_FLOAT(OFS_PARM0);
1850 if (i < 0 || i >= prog->progs->numfielddefs)
1852 VM_Warning("VM_entityfieldtype: %s: field index out of bounds\n", PRVM_NAME);
1853 PRVM_G_FLOAT(OFS_RETURN) = -1.0;
1857 d = &prog->fielddefs[i];
1858 PRVM_G_FLOAT(OFS_RETURN) = (float)d->type;
1861 // KrimZon - DP_QC_ENTITYDATA
1864 VM_getentityfieldstring
1866 string(float fieldnum, entity ent) getentityfieldstring
1869 void VM_getentityfieldstring(void)
1871 // put the data into a string
1876 int i = (int)PRVM_G_FLOAT(OFS_PARM0);
1878 if (i < 0 || i >= prog->progs->numfielddefs)
1880 VM_Warning("VM_entityfielddata: %s: field index out of bounds\n", PRVM_NAME);
1881 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString("");
1885 d = &prog->fielddefs[i];
1888 ent = PRVM_G_EDICT(OFS_PARM1);
1889 if(ent->priv.required->free)
1891 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString("");
1892 VM_Warning("VM_entityfielddata: %s: entity %i is free !\n", PRVM_NAME, PRVM_NUM_FOR_EDICT(ent));
1895 v = (int *)((char *)ent->fields.vp + d->ofs*4);
1897 // if it's 0 or blank, return an empty string
1898 type = d->type & ~DEF_SAVEGLOBAL;
1899 for (j=0 ; j<prvm_type_size[type] ; j++)
1902 if (j == prvm_type_size[type])
1904 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString("");
1908 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(PRVM_UglyValueString((etype_t)d->type, (prvm_eval_t *)v));
1911 // KrimZon - DP_QC_ENTITYDATA
1914 VM_putentityfieldstring
1916 float(float fieldnum, entity ent, string s) putentityfieldstring
1919 void VM_putentityfieldstring(void)
1923 int i = (int)PRVM_G_FLOAT(OFS_PARM0);
1925 if (i < 0 || i >= prog->progs->numfielddefs)
1927 VM_Warning("VM_entityfielddata: %s: field index out of bounds\n", PRVM_NAME);
1928 PRVM_G_FLOAT(OFS_RETURN) = 0.0f;
1932 d = &prog->fielddefs[i];
1935 ent = PRVM_G_EDICT(OFS_PARM1);
1936 if(ent->priv.required->free)
1938 VM_Warning("VM_entityfielddata: %s: entity %i is free !\n", PRVM_NAME, PRVM_NUM_FOR_EDICT(ent));
1939 PRVM_G_FLOAT(OFS_RETURN) = 0.0f;
1943 // parse the string into the value
1944 PRVM_G_FLOAT(OFS_RETURN) = ( PRVM_ED_ParseEpair(ent, d, PRVM_G_STRING(OFS_PARM2), false) ) ? 1.0f : 0.0f;
1951 float strlen(string s)
1954 //float(string s) strlen = #114; // returns how many characters are in a string
1955 void VM_strlen(void)
1957 VM_SAFEPARMCOUNT(1,VM_strlen);
1959 PRVM_G_FLOAT(OFS_RETURN) = strlen(PRVM_G_STRING(OFS_PARM0));
1962 // DRESK - Decolorized String
1967 string strdecolorize(string s)
1970 // string (string s) strdecolorize = #472; // returns the passed in string with color codes stripped
1971 void VM_strdecolorize(void)
1973 char szNewString[VM_STRINGTEMP_LENGTH];
1974 const char *szString;
1977 VM_SAFEPARMCOUNT(1,VM_strdecolorize);
1978 szString = PRVM_G_STRING(OFS_PARM0);
1979 COM_StringDecolorize(szString, 0, szNewString, sizeof(szNewString), TRUE);
1980 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(szNewString);
1983 // DRESK - String Length (not counting color codes)
1988 float strlennocol(string s)
1991 // float(string s) strlennocol = #471; // returns how many characters are in a string not including color codes
1992 // For example, ^2Dresk returns a length of 5
1993 void VM_strlennocol(void)
1995 const char *szString;
1998 VM_SAFEPARMCOUNT(1,VM_strlennocol);
2000 szString = PRVM_G_STRING(OFS_PARM0);
2002 nCnt = COM_StringLengthNoColors(szString, 0, NULL);
2004 PRVM_G_FLOAT(OFS_RETURN) = nCnt;
2007 // DRESK - String to Uppercase and Lowercase
2012 string strtolower(string s)
2015 // string (string s) strtolower = #480; // returns passed in string in lowercase form
2016 void VM_strtolower(void)
2018 char szNewString[VM_STRINGTEMP_LENGTH];
2019 const char *szString;
2022 VM_SAFEPARMCOUNT(1,VM_strtolower);
2023 szString = PRVM_G_STRING(OFS_PARM0);
2025 COM_ToLowerString(szString, szNewString, sizeof(szNewString) );
2027 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(szNewString);
2034 string strtoupper(string s)
2037 // string (string s) strtoupper = #481; // returns passed in string in uppercase form
2038 void VM_strtoupper(void)
2040 char szNewString[VM_STRINGTEMP_LENGTH];
2041 const char *szString;
2044 VM_SAFEPARMCOUNT(1,VM_strtoupper);
2045 szString = PRVM_G_STRING(OFS_PARM0);
2047 COM_ToUpperString(szString, szNewString, sizeof(szNewString) );
2049 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(szNewString);
2056 string strcat(string,string,...[string])
2059 //string(string s1, string s2) strcat = #115;
2060 // concatenates two strings (for example "abc", "def" would return "abcdef")
2061 // and returns as a tempstring
2062 void VM_strcat(void)
2064 char s[VM_STRINGTEMP_LENGTH];
2065 VM_SAFEPARMCOUNTRANGE(1, 8, VM_strcat);
2067 VM_VarString(0, s, sizeof(s));
2068 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(s);
2075 string substring(string s, float start, float length)
2078 // string(string s, float start, float length) substring = #116;
2079 // returns a section of a string as a tempstring
2080 void VM_substring(void)
2082 int i, start, length;
2084 char string[VM_STRINGTEMP_LENGTH];
2086 VM_SAFEPARMCOUNT(3,VM_substring);
2088 s = PRVM_G_STRING(OFS_PARM0);
2089 start = (int)PRVM_G_FLOAT(OFS_PARM1);
2090 length = (int)PRVM_G_FLOAT(OFS_PARM2);
2091 for (i = 0;i < start && *s;i++, s++);
2092 for (i = 0;i < (int)sizeof(string) - 1 && *s && i < length;i++, s++)
2095 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string);
2102 string(string search, string replace, string subject) strreplace = #484;
2105 // replaces all occurrences of search with replace in the string subject, and returns the result
2106 void VM_strreplace(void)
2109 const char *search, *replace, *subject;
2110 char string[VM_STRINGTEMP_LENGTH];
2111 int search_len, replace_len, subject_len;
2113 VM_SAFEPARMCOUNT(3,VM_strreplace);
2115 search = PRVM_G_STRING(OFS_PARM0);
2116 replace = PRVM_G_STRING(OFS_PARM1);
2117 subject = PRVM_G_STRING(OFS_PARM2);
2119 search_len = (int)strlen(search);
2120 replace_len = (int)strlen(replace);
2121 subject_len = (int)strlen(subject);
2124 for (i = 0; i < subject_len; i++)
2126 for (j = 0; j < search_len && i+j < subject_len; j++)
2127 if (subject[i+j] != search[j])
2129 if (j == search_len || i+j == subject_len)
2131 // found it at offset 'i'
2132 for (j = 0; j < replace_len && si < (int)sizeof(string) - 1; j++)
2133 string[si++] = replace[j];
2134 i += search_len - 1;
2139 if (si < (int)sizeof(string) - 1)
2140 string[si++] = subject[i];
2145 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string);
2152 string(string search, string replace, string subject) strireplace = #485;
2155 // case-insensitive version of strreplace
2156 void VM_strireplace(void)
2159 const char *search, *replace, *subject;
2160 char string[VM_STRINGTEMP_LENGTH];
2161 int search_len, replace_len, subject_len;
2163 VM_SAFEPARMCOUNT(3,VM_strreplace);
2165 search = PRVM_G_STRING(OFS_PARM0);
2166 replace = PRVM_G_STRING(OFS_PARM1);
2167 subject = PRVM_G_STRING(OFS_PARM2);
2169 search_len = (int)strlen(search);
2170 replace_len = (int)strlen(replace);
2171 subject_len = (int)strlen(subject);
2174 for (i = 0; i < subject_len; i++)
2176 for (j = 0; j < search_len && i+j < subject_len; j++)
2177 if (tolower(subject[i+j]) != tolower(search[j]))
2179 if (j == search_len || i+j == subject_len)
2181 // found it at offset 'i'
2182 for (j = 0; j < replace_len && si < (int)sizeof(string) - 1; j++)
2183 string[si++] = replace[j];
2184 i += search_len - 1;
2189 if (si < (int)sizeof(string) - 1)
2190 string[si++] = subject[i];
2195 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string);
2202 vector stov(string s)
2205 //vector(string s) stov = #117; // returns vector value from a string
2208 char string[VM_STRINGTEMP_LENGTH];
2210 VM_SAFEPARMCOUNT(1,VM_stov);
2212 VM_VarString(0, string, sizeof(string));
2213 Math_atov(string, PRVM_G_VECTOR(OFS_RETURN));
2220 string strzone(string s)
2223 //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)
2224 void VM_strzone(void)
2227 char string[VM_STRINGTEMP_LENGTH];
2230 VM_SAFEPARMCOUNT(1,VM_strzone);
2232 VM_VarString(0, string, sizeof(string));
2233 alloclen = strlen(string) + 1;
2234 PRVM_G_INT(OFS_RETURN) = PRVM_AllocString(alloclen, &out);
2235 memcpy(out, string, alloclen);
2245 //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!!!)
2246 void VM_strunzone(void)
2248 VM_SAFEPARMCOUNT(1,VM_strunzone);
2249 PRVM_FreeString(PRVM_G_INT(OFS_PARM0));
2254 VM_command (used by client and menu)
2256 clientcommand(float client, string s) (for client and menu)
2259 //void(entity e, string s) clientcommand = #440; // executes a command string as if it came from the specified client
2260 //this function originally written by KrimZon, made shorter by LordHavoc
2261 void VM_clcommand (void)
2263 client_t *temp_client;
2266 VM_SAFEPARMCOUNT(2,VM_clcommand);
2268 i = (int)PRVM_G_FLOAT(OFS_PARM0);
2269 if (!sv.active || i < 0 || i >= svs.maxclients || !svs.clients[i].active)
2271 VM_Warning("VM_clientcommand: %s: invalid client/server is not active !\n", PRVM_NAME);
2275 temp_client = host_client;
2276 host_client = svs.clients + i;
2277 Cmd_ExecuteString (PRVM_G_STRING(OFS_PARM1), src_client);
2278 host_client = temp_client;
2286 float tokenize(string s)
2289 //float(string s) tokenize = #441; // takes apart a string into individal words (access them with argv), returns how many
2290 //this function originally written by KrimZon, made shorter by LordHavoc
2291 //20040203: rewritten by LordHavoc (no longer uses allocations)
2292 static int num_tokens = 0;
2293 static int tokens[VM_STRINGTEMP_LENGTH / 2];
2294 static int tokens_startpos[VM_STRINGTEMP_LENGTH / 2];
2295 static int tokens_endpos[VM_STRINGTEMP_LENGTH / 2];
2296 static char tokenize_string[VM_STRINGTEMP_LENGTH];
2297 void VM_tokenize (void)
2301 VM_SAFEPARMCOUNT(1,VM_tokenize);
2303 strlcpy(tokenize_string, PRVM_G_STRING(OFS_PARM0), sizeof(tokenize_string));
2304 p = tokenize_string;
2309 if (num_tokens >= (int)(sizeof(tokens)/sizeof(tokens[0])))
2312 // skip whitespace here to find token start pos
2313 while(*p && ISWHITESPACE(*p))
2316 tokens_startpos[num_tokens] = p - tokenize_string;
2317 if(!COM_ParseToken_VM_Tokenize(&p, false))
2319 tokens_endpos[num_tokens] = p - tokenize_string;
2320 tokens[num_tokens] = PRVM_SetTempString(com_token);
2324 PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
2327 //float(string s) tokenize = #514; // takes apart a string into individal words (access them with argv), returns how many
2328 void VM_tokenize_console (void)
2332 VM_SAFEPARMCOUNT(1,VM_tokenize);
2334 strlcpy(tokenize_string, PRVM_G_STRING(OFS_PARM0), sizeof(tokenize_string));
2335 p = tokenize_string;
2340 if (num_tokens >= (int)(sizeof(tokens)/sizeof(tokens[0])))
2343 // skip whitespace here to find token start pos
2344 while(*p && ISWHITESPACE(*p))
2347 tokens_startpos[num_tokens] = p - tokenize_string;
2348 if(!COM_ParseToken_Console(&p))
2350 tokens_endpos[num_tokens] = p - tokenize_string;
2351 tokens[num_tokens] = PRVM_SetTempString(com_token);
2355 PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
2360 VM_tokenizebyseparator
2362 float tokenizebyseparator(string s, string separator1, ...)
2365 //float(string s, string separator1, ...) tokenizebyseparator = #479; // takes apart a string into individal words (access them with argv), returns how many
2366 //this function returns the token preceding each instance of a separator (of
2367 //which there can be multiple), and the text following the last separator
2368 //useful for parsing certain kinds of data like IP addresses
2370 //numnumbers = tokenizebyseparator("10.1.2.3", ".");
2371 //returns 4 and the tokens "10" "1" "2" "3".
2372 void VM_tokenizebyseparator (void)
2376 int separatorlen[7];
2377 const char *separators[7];
2380 char tokentext[MAX_INPUTLINE];
2382 VM_SAFEPARMCOUNTRANGE(2, 8,VM_tokenizebyseparator);
2384 strlcpy(tokenize_string, PRVM_G_STRING(OFS_PARM0), sizeof(tokenize_string));
2385 p = tokenize_string;
2388 for (j = 1;j < prog->argc;j++)
2390 // skip any blank separator strings
2391 const char *s = PRVM_G_STRING(OFS_PARM0+j*3);
2394 separators[numseparators] = s;
2395 separatorlen[numseparators] = strlen(s);
2402 while (num_tokens < (int)(sizeof(tokens)/sizeof(tokens[0])))
2404 token = tokentext + j;
2405 tokens_startpos[num_tokens] = p - tokenize_string;
2409 for (k = 0;k < numseparators;k++)
2411 if (!strncmp(p, separators[k], separatorlen[k]))
2413 p += separatorlen[k];
2417 if (k < numseparators)
2419 if (j < (int)sizeof(tokentext)-1)
2420 tokentext[j++] = *p;
2424 tokens_endpos[num_tokens] = p0 - tokenize_string;
2425 if (j >= (int)sizeof(tokentext))
2428 tokens[num_tokens++] = PRVM_SetTempString(token);
2433 PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
2436 //string(float n) argv = #442; // returns a word from the tokenized string (returns nothing for an invalid index)
2437 //this function originally written by KrimZon, made shorter by LordHavoc
2442 VM_SAFEPARMCOUNT(1,VM_argv);
2444 token_num = (int)PRVM_G_FLOAT(OFS_PARM0);
2447 token_num += num_tokens;
2449 if (token_num >= 0 && token_num < num_tokens)
2450 PRVM_G_INT(OFS_RETURN) = tokens[token_num];
2452 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
2455 //float(float n) argv_start_index = #515; // returns the start index of a token
2456 void VM_argv_start_index (void)
2460 VM_SAFEPARMCOUNT(1,VM_argv);
2462 token_num = (int)PRVM_G_FLOAT(OFS_PARM0);
2465 token_num += num_tokens;
2467 if (token_num >= 0 && token_num < num_tokens)
2468 PRVM_G_FLOAT(OFS_RETURN) = tokens_startpos[token_num];
2470 PRVM_G_FLOAT(OFS_RETURN) = -1;
2473 //float(float n) argv_end_index = #516; // returns the end index of a token
2474 void VM_argv_end_index (void)
2478 VM_SAFEPARMCOUNT(1,VM_argv);
2480 token_num = (int)PRVM_G_FLOAT(OFS_PARM0);
2483 token_num += num_tokens;
2485 if (token_num >= 0 && token_num < num_tokens)
2486 PRVM_G_FLOAT(OFS_RETURN) = tokens_endpos[token_num];
2488 PRVM_G_FLOAT(OFS_RETURN) = -1;
2498 void VM_isserver(void)
2500 VM_SAFEPARMCOUNT(0,VM_serverstate);
2502 PRVM_G_FLOAT(OFS_RETURN) = sv.active && (svs.maxclients > 1 || cls.state == ca_dedicated);
2512 void VM_clientcount(void)
2514 VM_SAFEPARMCOUNT(0,VM_clientcount);
2516 PRVM_G_FLOAT(OFS_RETURN) = svs.maxclients;
2526 void VM_clientstate(void)
2528 VM_SAFEPARMCOUNT(0,VM_clientstate);
2531 switch( cls.state ) {
2532 case ca_uninitialized:
2534 PRVM_G_FLOAT(OFS_RETURN) = 0;
2536 case ca_disconnected:
2537 PRVM_G_FLOAT(OFS_RETURN) = 1;
2540 PRVM_G_FLOAT(OFS_RETURN) = 2;
2543 // should never be reached!
2552 float getostype(void)
2554 */ // not used at the moment -> not included in the common list
2555 void VM_getostype(void)
2557 VM_SAFEPARMCOUNT(0,VM_getostype);
2562 OS_MAC - not supported
2566 PRVM_G_FLOAT(OFS_RETURN) = 0;
2567 #elif defined(MACOSX)
2568 PRVM_G_FLOAT(OFS_RETURN) = 2;
2570 PRVM_G_FLOAT(OFS_RETURN) = 1;
2581 void VM_gettime(void)
2583 VM_SAFEPARMCOUNT(0,VM_gettime);
2585 PRVM_G_FLOAT(OFS_RETURN) = (float) realtime;
2592 loadfromdata(string data)
2595 void VM_loadfromdata(void)
2597 VM_SAFEPARMCOUNT(1,VM_loadentsfromfile);
2599 PRVM_ED_LoadFromFile(PRVM_G_STRING(OFS_PARM0));
2603 ========================
2606 parseentitydata(entity ent, string data)
2607 ========================
2609 void VM_parseentitydata(void)
2614 VM_SAFEPARMCOUNT(2, VM_parseentitydata);
2616 // get edict and test it
2617 ent = PRVM_G_EDICT(OFS_PARM0);
2618 if (ent->priv.required->free)
2619 PRVM_ERROR ("VM_parseentitydata: %s: Can only set already spawned entities (entity %i is free)!", PRVM_NAME, PRVM_NUM_FOR_EDICT(ent));
2621 data = PRVM_G_STRING(OFS_PARM1);
2623 // parse the opening brace
2624 if (!COM_ParseToken_Simple(&data, false, false) || com_token[0] != '{' )
2625 PRVM_ERROR ("VM_parseentitydata: %s: Couldn't parse entity data:\n%s", PRVM_NAME, data );
2627 PRVM_ED_ParseEdict (data, ent);
2634 loadfromfile(string file)
2637 void VM_loadfromfile(void)
2639 const char *filename;
2642 VM_SAFEPARMCOUNT(1,VM_loadfromfile);
2644 filename = PRVM_G_STRING(OFS_PARM0);
2645 if (FS_CheckNastyPath(filename, false))
2647 PRVM_G_FLOAT(OFS_RETURN) = -4;
2648 VM_Warning("VM_loadfromfile: %s dangerous or non-portable filename \"%s\" not allowed. (contains : or \\ or begins with .. or /)\n", PRVM_NAME, filename);
2652 // not conform with VM_fopen
2653 data = (char *)FS_LoadFile(filename, tempmempool, false, NULL);
2655 PRVM_G_FLOAT(OFS_RETURN) = -1;
2657 PRVM_ED_LoadFromFile(data);
2668 float mod(float val, float m)
2671 void VM_modulo(void)
2674 VM_SAFEPARMCOUNT(2,VM_module);
2676 val = (int) PRVM_G_FLOAT(OFS_PARM0);
2677 m = (int) PRVM_G_FLOAT(OFS_PARM1);
2679 PRVM_G_FLOAT(OFS_RETURN) = (float) (val % m);
2682 void VM_Search_Init(void)
2685 for (i = 0;i < PRVM_MAX_OPENSEARCHES;i++)
2686 prog->opensearches[i] = NULL;
2689 void VM_Search_Reset(void)
2692 // reset the fssearch list
2693 for(i = 0; i < PRVM_MAX_OPENSEARCHES; i++)
2695 if(prog->opensearches[i])
2696 FS_FreeSearch(prog->opensearches[i]);
2697 prog->opensearches[i] = NULL;
2705 float search_begin(string pattern, float caseinsensitive, float quiet)
2708 void VM_search_begin(void)
2711 const char *pattern;
2712 int caseinsens, quiet;
2714 VM_SAFEPARMCOUNT(3, VM_search_begin);
2716 pattern = PRVM_G_STRING(OFS_PARM0);
2718 VM_CheckEmptyString(pattern);
2720 caseinsens = (int)PRVM_G_FLOAT(OFS_PARM1);
2721 quiet = (int)PRVM_G_FLOAT(OFS_PARM2);
2723 for(handle = 0; handle < PRVM_MAX_OPENSEARCHES; handle++)
2724 if(!prog->opensearches[handle])
2727 if(handle >= PRVM_MAX_OPENSEARCHES)
2729 PRVM_G_FLOAT(OFS_RETURN) = -2;
2730 VM_Warning("VM_search_begin: %s ran out of search handles (%i)\n", PRVM_NAME, PRVM_MAX_OPENSEARCHES);
2734 if(!(prog->opensearches[handle] = FS_Search(pattern,caseinsens, quiet)))
2735 PRVM_G_FLOAT(OFS_RETURN) = -1;
2738 prog->opensearches_origin[handle] = PRVM_AllocationOrigin();
2739 PRVM_G_FLOAT(OFS_RETURN) = handle;
2747 void search_end(float handle)
2750 void VM_search_end(void)
2753 VM_SAFEPARMCOUNT(1, VM_search_end);
2755 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
2757 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
2759 VM_Warning("VM_search_end: invalid handle %i used in %s\n", handle, PRVM_NAME);
2762 if(prog->opensearches[handle] == NULL)
2764 VM_Warning("VM_search_end: no such handle %i in %s\n", handle, PRVM_NAME);
2768 FS_FreeSearch(prog->opensearches[handle]);
2769 prog->opensearches[handle] = NULL;
2770 if(prog->opensearches_origin[handle])
2771 PRVM_Free((char *)prog->opensearches_origin[handle]);
2778 float search_getsize(float handle)
2781 void VM_search_getsize(void)
2784 VM_SAFEPARMCOUNT(1, VM_M_search_getsize);
2786 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
2788 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
2790 VM_Warning("VM_search_getsize: invalid handle %i used in %s\n", handle, PRVM_NAME);
2793 if(prog->opensearches[handle] == NULL)
2795 VM_Warning("VM_search_getsize: no such handle %i in %s\n", handle, PRVM_NAME);
2799 PRVM_G_FLOAT(OFS_RETURN) = prog->opensearches[handle]->numfilenames;
2804 VM_search_getfilename
2806 string search_getfilename(float handle, float num)
2809 void VM_search_getfilename(void)
2811 int handle, filenum;
2812 VM_SAFEPARMCOUNT(2, VM_search_getfilename);
2814 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
2815 filenum = (int)PRVM_G_FLOAT(OFS_PARM1);
2817 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
2819 VM_Warning("VM_search_getfilename: invalid handle %i used in %s\n", handle, PRVM_NAME);
2822 if(prog->opensearches[handle] == NULL)
2824 VM_Warning("VM_search_getfilename: no such handle %i in %s\n", handle, PRVM_NAME);
2827 if(filenum < 0 || filenum >= prog->opensearches[handle]->numfilenames)
2829 VM_Warning("VM_search_getfilename: invalid filenum %i in %s\n", filenum, PRVM_NAME);
2833 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog->opensearches[handle]->filenames[filenum]);
2840 string chr(float ascii)
2846 VM_SAFEPARMCOUNT(1, VM_chr);
2848 tmp[0] = (unsigned char) PRVM_G_FLOAT(OFS_PARM0);
2851 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(tmp);
2854 //=============================================================================
2855 // Draw builtins (client & menu)
2861 float iscachedpic(string pic)
2864 void VM_iscachedpic(void)
2866 VM_SAFEPARMCOUNT(1,VM_iscachedpic);
2868 // drawq hasnt such a function, thus always return true
2869 PRVM_G_FLOAT(OFS_RETURN) = false;
2876 string precache_pic(string pic)
2879 void VM_precache_pic(void)
2883 VM_SAFEPARMCOUNT(1, VM_precache_pic);
2885 s = PRVM_G_STRING(OFS_PARM0);
2886 PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
2887 VM_CheckEmptyString (s);
2889 // AK Draw_CachePic is supposed to always return a valid pointer
2890 if( Draw_CachePic_Flags(s, CACHEPICFLAG_NOTPERSISTENT)->tex == r_texture_notexture )
2891 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
2901 void VM_freepic(void)
2905 VM_SAFEPARMCOUNT(1,VM_freepic);
2907 s = PRVM_G_STRING(OFS_PARM0);
2908 VM_CheckEmptyString (s);
2913 dp_font_t *getdrawfont()
2915 if(prog->globaloffsets.drawfont >= 0)
2917 int f = (int) PRVM_G_FLOAT(prog->globaloffsets.drawfont);
2918 if(f < 0 || f >= MAX_FONTS)
2919 return FONT_DEFAULT;
2920 return &dp_fonts[f];
2923 return FONT_DEFAULT;
2930 float drawcharacter(vector position, float character, vector scale, vector rgb, float alpha, float flag)
2933 void VM_drawcharacter(void)
2935 float *pos,*scale,*rgb;
2938 VM_SAFEPARMCOUNT(6,VM_drawcharacter);
2940 character = (char) PRVM_G_FLOAT(OFS_PARM1);
2943 PRVM_G_FLOAT(OFS_RETURN) = -1;
2944 VM_Warning("VM_drawcharacter: %s passed null character !\n",PRVM_NAME);
2948 pos = PRVM_G_VECTOR(OFS_PARM0);
2949 scale = PRVM_G_VECTOR(OFS_PARM2);
2950 rgb = PRVM_G_VECTOR(OFS_PARM3);
2951 flag = (int)PRVM_G_FLOAT(OFS_PARM5);
2953 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2955 PRVM_G_FLOAT(OFS_RETURN) = -2;
2956 VM_Warning("VM_drawcharacter: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2960 if(pos[2] || scale[2])
2961 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")));
2963 if(!scale[0] || !scale[1])
2965 PRVM_G_FLOAT(OFS_RETURN) = -3;
2966 VM_Warning("VM_drawcharacter: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
2970 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());
2971 PRVM_G_FLOAT(OFS_RETURN) = 1;
2978 float drawstring(vector position, string text, vector scale, vector rgb, float alpha, float flag)
2981 void VM_drawstring(void)
2983 float *pos,*scale,*rgb;
2986 VM_SAFEPARMCOUNT(6,VM_drawstring);
2988 string = PRVM_G_STRING(OFS_PARM1);
2989 pos = PRVM_G_VECTOR(OFS_PARM0);
2990 scale = PRVM_G_VECTOR(OFS_PARM2);
2991 rgb = PRVM_G_VECTOR(OFS_PARM3);
2992 flag = (int)PRVM_G_FLOAT(OFS_PARM5);
2994 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2996 PRVM_G_FLOAT(OFS_RETURN) = -2;
2997 VM_Warning("VM_drawstring: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
3001 if(!scale[0] || !scale[1])
3003 PRVM_G_FLOAT(OFS_RETURN) = -3;
3004 VM_Warning("VM_drawstring: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
3008 if(pos[2] || scale[2])
3009 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")));
3011 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());
3012 PRVM_G_FLOAT(OFS_RETURN) = 1;
3017 VM_drawcolorcodedstring
3019 float drawcolorcodedstring(vector position, string text, vector scale, float alpha, float flag)
3022 void VM_drawcolorcodedstring(void)
3027 VM_SAFEPARMCOUNT(5,VM_drawstring);
3029 string = PRVM_G_STRING(OFS_PARM1);
3030 pos = PRVM_G_VECTOR(OFS_PARM0);
3031 scale = PRVM_G_VECTOR(OFS_PARM2);
3032 flag = (int)PRVM_G_FLOAT(OFS_PARM4);
3034 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3036 PRVM_G_FLOAT(OFS_RETURN) = -2;
3037 VM_Warning("VM_drawcolorcodedstring: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
3041 if(!scale[0] || !scale[1])
3043 PRVM_G_FLOAT(OFS_RETURN) = -3;
3044 VM_Warning("VM_drawcolorcodedstring: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
3048 if(pos[2] || scale[2])
3049 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")));
3052 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());
3053 PRVM_G_FLOAT(OFS_RETURN) = 1;
3059 float stringwidth(string text, float allowColorCodes)
3062 void VM_stringwidth(void)
3066 VM_SAFEPARMCOUNT(2,VM_drawstring);
3068 string = PRVM_G_STRING(OFS_PARM0);
3069 colors = (int)PRVM_G_FLOAT(OFS_PARM1);
3071 PRVM_G_FLOAT(OFS_RETURN) = DrawQ_TextWidth_Font(string, 0, !colors, getdrawfont()); // 1x1 characters, don't actually draw
3077 float drawpic(vector position, string pic, vector size, vector rgb, float alpha, float flag)
3080 void VM_drawpic(void)
3082 const char *picname;
3083 float *size, *pos, *rgb;
3086 VM_SAFEPARMCOUNT(6,VM_drawpic);
3088 picname = PRVM_G_STRING(OFS_PARM1);
3089 VM_CheckEmptyString (picname);
3091 // is pic cached ? no function yet for that
3094 PRVM_G_FLOAT(OFS_RETURN) = -4;
3095 VM_Warning("VM_drawpic: %s: %s not cached !\n", PRVM_NAME, picname);
3099 pos = PRVM_G_VECTOR(OFS_PARM0);
3100 size = PRVM_G_VECTOR(OFS_PARM2);
3101 rgb = PRVM_G_VECTOR(OFS_PARM3);
3102 flag = (int) PRVM_G_FLOAT(OFS_PARM5);
3104 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3106 PRVM_G_FLOAT(OFS_RETURN) = -2;
3107 VM_Warning("VM_drawpic: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
3111 if(pos[2] || size[2])
3112 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")));
3114 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);
3115 PRVM_G_FLOAT(OFS_RETURN) = 1;
3121 float drawsubpic(vector position, vector size, string pic, vector srcPos, vector srcSize, vector rgb, float alpha, float flag)
3125 void VM_drawsubpic(void)
3127 const char *picname;
3128 float *size, *pos, *rgb, *srcPos, *srcSize, alpha;
3131 VM_SAFEPARMCOUNT(8,VM_drawsubpic);
3133 picname = PRVM_G_STRING(OFS_PARM2);
3134 VM_CheckEmptyString (picname);
3136 // is pic cached ? no function yet for that
3139 PRVM_G_FLOAT(OFS_RETURN) = -4;
3140 VM_Warning("VM_drawsubpic: %s: %s not cached !\n", PRVM_NAME, picname);
3144 pos = PRVM_G_VECTOR(OFS_PARM0);
3145 size = PRVM_G_VECTOR(OFS_PARM1);
3146 srcPos = PRVM_G_VECTOR(OFS_PARM3);
3147 srcSize = PRVM_G_VECTOR(OFS_PARM4);
3148 rgb = PRVM_G_VECTOR(OFS_PARM5);
3149 alpha = PRVM_G_FLOAT(OFS_PARM6);
3150 flag = (int) PRVM_G_FLOAT(OFS_PARM7);
3152 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3154 PRVM_G_FLOAT(OFS_RETURN) = -2;
3155 VM_Warning("VM_drawsubpic: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
3159 if(pos[2] || size[2])
3160 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")));
3162 DrawQ_SuperPic(pos[0], pos[1], Draw_CachePic (picname),
3164 srcPos[0], srcPos[1], rgb[0], rgb[1], rgb[2], alpha,
3165 srcPos[0] + srcSize[0], srcPos[1], rgb[0], rgb[1], rgb[2], alpha,
3166 srcPos[0], srcPos[1] + srcSize[1], rgb[0], rgb[1], rgb[2], alpha,
3167 srcPos[0] + srcSize[0], srcPos[1] + srcSize[1], rgb[0], rgb[1], rgb[2], alpha,
3169 PRVM_G_FLOAT(OFS_RETURN) = 1;
3176 float drawfill(vector position, vector size, vector rgb, float alpha, float flag)
3179 void VM_drawfill(void)
3181 float *size, *pos, *rgb;
3184 VM_SAFEPARMCOUNT(5,VM_drawfill);
3187 pos = PRVM_G_VECTOR(OFS_PARM0);
3188 size = PRVM_G_VECTOR(OFS_PARM1);
3189 rgb = PRVM_G_VECTOR(OFS_PARM2);
3190 flag = (int) PRVM_G_FLOAT(OFS_PARM4);
3192 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3194 PRVM_G_FLOAT(OFS_RETURN) = -2;
3195 VM_Warning("VM_drawfill: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
3199 if(pos[2] || size[2])
3200 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")));
3202 DrawQ_Fill(pos[0], pos[1], size[0], size[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM3), flag);
3203 PRVM_G_FLOAT(OFS_RETURN) = 1;
3210 drawsetcliparea(float x, float y, float width, float height)
3213 void VM_drawsetcliparea(void)
3216 VM_SAFEPARMCOUNT(4,VM_drawsetcliparea);
3218 x = bound(0, PRVM_G_FLOAT(OFS_PARM0), vid_conwidth.integer);
3219 y = bound(0, PRVM_G_FLOAT(OFS_PARM1), vid_conheight.integer);
3220 w = bound(0, PRVM_G_FLOAT(OFS_PARM2) + PRVM_G_FLOAT(OFS_PARM0) - x, (vid_conwidth.integer - x));
3221 h = bound(0, PRVM_G_FLOAT(OFS_PARM3) + PRVM_G_FLOAT(OFS_PARM1) - y, (vid_conheight.integer - y));
3223 DrawQ_SetClipArea(x, y, w, h);
3228 VM_drawresetcliparea
3233 void VM_drawresetcliparea(void)
3235 VM_SAFEPARMCOUNT(0,VM_drawresetcliparea);
3237 DrawQ_ResetClipArea();
3244 vector getimagesize(string pic)
3247 void VM_getimagesize(void)
3252 VM_SAFEPARMCOUNT(1,VM_getimagesize);
3254 p = PRVM_G_STRING(OFS_PARM0);
3255 VM_CheckEmptyString (p);
3257 pic = Draw_CachePic_Flags (p, CACHEPICFLAG_NOTPERSISTENT);
3259 PRVM_G_VECTOR(OFS_RETURN)[0] = pic->width;
3260 PRVM_G_VECTOR(OFS_RETURN)[1] = pic->height;
3261 PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
3268 string keynumtostring(float keynum)
3271 void VM_keynumtostring (void)
3273 VM_SAFEPARMCOUNT(1, VM_keynumtostring);
3275 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(Key_KeynumToString((int)PRVM_G_FLOAT(OFS_PARM0)));
3280 VM_findkeysforcommand
3282 string findkeysforcommand(string command)
3284 the returned string is an altstring
3287 #define NUMKEYS 5 // TODO: merge the constant in keys.c with this one somewhen
3289 void M_FindKeysForCommand(const char *command, int *keys);
3290 void VM_findkeysforcommand(void)
3293 char ret[VM_STRINGTEMP_LENGTH];
3297 VM_SAFEPARMCOUNT(1, VM_findkeysforcommand);
3299 cmd = PRVM_G_STRING(OFS_PARM0);
3301 VM_CheckEmptyString(cmd);
3303 M_FindKeysForCommand(cmd, keys);
3306 for(i = 0; i < NUMKEYS; i++)
3307 strlcat(ret, va(" \'%i\'", keys[i]), sizeof(ret));
3309 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(ret);
3316 float stringtokeynum(string key)
3319 void VM_stringtokeynum (void)
3321 VM_SAFEPARMCOUNT( 1, VM_keynumtostring );
3323 PRVM_G_INT(OFS_RETURN) = Key_StringToKeynum(PRVM_G_STRING(OFS_PARM0));
3326 // CL_Video interface functions
3329 ========================
3332 float cin_open(string file, string name)
3333 ========================
3335 void VM_cin_open( void )
3340 VM_SAFEPARMCOUNT( 2, VM_cin_open );
3342 file = PRVM_G_STRING( OFS_PARM0 );
3343 name = PRVM_G_STRING( OFS_PARM1 );
3345 VM_CheckEmptyString( file );
3346 VM_CheckEmptyString( name );
3348 if( CL_OpenVideo( file, name, MENUOWNER ) )
3349 PRVM_G_FLOAT( OFS_RETURN ) = 1;
3351 PRVM_G_FLOAT( OFS_RETURN ) = 0;
3355 ========================
3358 void cin_close(string name)
3359 ========================
3361 void VM_cin_close( void )
3365 VM_SAFEPARMCOUNT( 1, VM_cin_close );
3367 name = PRVM_G_STRING( OFS_PARM0 );
3368 VM_CheckEmptyString( name );
3370 CL_CloseVideo( CL_GetVideoByName( name ) );
3374 ========================
3376 void cin_setstate(string name, float type)
3377 ========================
3379 void VM_cin_setstate( void )
3382 clvideostate_t state;
3385 VM_SAFEPARMCOUNT( 2, VM_cin_netstate );
3387 name = PRVM_G_STRING( OFS_PARM0 );
3388 VM_CheckEmptyString( name );
3390 state = (clvideostate_t)((int)PRVM_G_FLOAT( OFS_PARM1 ));
3392 video = CL_GetVideoByName( name );
3393 if( video && state > CLVIDEO_UNUSED && state < CLVIDEO_STATECOUNT )
3394 CL_SetVideoState( video, state );
3398 ========================
3401 float cin_getstate(string name)
3402 ========================
3404 void VM_cin_getstate( void )
3409 VM_SAFEPARMCOUNT( 1, VM_cin_getstate );
3411 name = PRVM_G_STRING( OFS_PARM0 );
3412 VM_CheckEmptyString( name );
3414 video = CL_GetVideoByName( name );
3416 PRVM_G_FLOAT( OFS_RETURN ) = (int)video->state;
3418 PRVM_G_FLOAT( OFS_RETURN ) = 0;
3422 ========================
3425 void cin_restart(string name)
3426 ========================
3428 void VM_cin_restart( void )
3433 VM_SAFEPARMCOUNT( 1, VM_cin_restart );
3435 name = PRVM_G_STRING( OFS_PARM0 );
3436 VM_CheckEmptyString( name );
3438 video = CL_GetVideoByName( name );
3440 CL_RestartVideo( video );
3444 ========================
3446 ========================
3448 void VM_Gecko_Init( void ) {
3449 // the prog struct is memset to 0 by Initprog? [12/6/2007 Black]
3450 // FIXME: remove the other _Init functions then, too? [12/6/2007 Black]
3454 ========================
3456 ========================
3458 void VM_Gecko_Destroy( void ) {
3460 for( i = 0 ; i < PRVM_MAX_GECKOINSTANCES ; i++ ) {
3461 clgecko_t **instance = &prog->opengeckoinstances[ i ];
3463 CL_Gecko_DestroyBrowser( *instance );
3470 ========================
3473 float[bool] gecko_create( string name )
3474 ========================
3476 void VM_gecko_create( void ) {
3479 clgecko_t *instance;
3481 VM_SAFEPARMCOUNT( 1, VM_gecko_create );
3483 name = PRVM_G_STRING( OFS_PARM0 );
3484 VM_CheckEmptyString( name );
3486 // find an empty slot for this gecko browser..
3487 for( i = 0 ; i < PRVM_MAX_GECKOINSTANCES ; i++ ) {
3488 if( prog->opengeckoinstances[ i ] == NULL ) {
3492 if( i == PRVM_MAX_GECKOINSTANCES ) {
3493 VM_Warning("VM_gecko_create: %s ran out of gecko handles (%i)\n", PRVM_NAME, PRVM_MAX_GECKOINSTANCES);
3494 PRVM_G_FLOAT( OFS_RETURN ) = 0;
3498 instance = prog->opengeckoinstances[ i ] = CL_Gecko_CreateBrowser( name, PRVM_GetProgNr() );
3500 // TODO: error handling [12/3/2007 Black]
3501 PRVM_G_FLOAT( OFS_RETURN ) = 0;
3504 PRVM_G_FLOAT( OFS_RETURN ) = 1;
3508 ========================
3511 void gecko_destroy( string name )
3512 ========================
3514 void VM_gecko_destroy( void ) {
3516 clgecko_t *instance;
3518 VM_SAFEPARMCOUNT( 1, VM_gecko_destroy );
3520 name = PRVM_G_STRING( OFS_PARM0 );
3521 VM_CheckEmptyString( name );
3522 instance = CL_Gecko_FindBrowser( name );
3526 CL_Gecko_DestroyBrowser( instance );
3530 ========================
3533 void gecko_navigate( string name, string URI )
3534 ========================
3536 void VM_gecko_navigate( void ) {
3539 clgecko_t *instance;
3541 VM_SAFEPARMCOUNT( 2, VM_gecko_navigate );
3543 name = PRVM_G_STRING( OFS_PARM0 );
3544 URI = PRVM_G_STRING( OFS_PARM1 );
3545 VM_CheckEmptyString( name );
3546 VM_CheckEmptyString( URI );
3548 instance = CL_Gecko_FindBrowser( name );
3552 CL_Gecko_NavigateToURI( instance, URI );
3556 ========================
3559 float[bool] gecko_keyevent( string name, float key, float eventtype )
3560 ========================
3562 void VM_gecko_keyevent( void ) {
3565 clgecko_buttoneventtype_t eventtype;
3566 clgecko_t *instance;
3568 VM_SAFEPARMCOUNT( 3, VM_gecko_keyevent );
3570 name = PRVM_G_STRING( OFS_PARM0 );
3571 VM_CheckEmptyString( name );
3572 key = (unsigned int) PRVM_G_FLOAT( OFS_PARM1 );
3573 switch( (unsigned int) PRVM_G_FLOAT( OFS_PARM2 ) ) {
3575 eventtype = CLG_BET_DOWN;
3578 eventtype = CLG_BET_UP;
3581 eventtype = CLG_BET_PRESS;
3584 eventtype = CLG_BET_DOUBLECLICK;
3587 // TODO: console printf? [12/3/2007 Black]
3588 PRVM_G_FLOAT( OFS_RETURN ) = 0;
3592 instance = CL_Gecko_FindBrowser( name );
3594 PRVM_G_FLOAT( OFS_RETURN ) = 0;
3598 PRVM_G_FLOAT( OFS_RETURN ) = (CL_Gecko_Event_Key( instance, (keynum_t) key, eventtype ) == true);
3602 ========================
3605 void gecko_mousemove( string name, float x, float y )
3606 ========================
3608 void VM_gecko_movemouse( void ) {
3611 clgecko_t *instance;
3613 VM_SAFEPARMCOUNT( 3, VM_gecko_movemouse );
3615 name = PRVM_G_STRING( OFS_PARM0 );
3616 VM_CheckEmptyString( name );
3617 x = PRVM_G_FLOAT( OFS_PARM1 );
3618 y = PRVM_G_FLOAT( OFS_PARM2 );
3620 instance = CL_Gecko_FindBrowser( name );
3624 CL_Gecko_Event_CursorMove( instance, x, y );
3629 ========================
3632 void gecko_resize( string name, float w, float h )
3633 ========================
3635 void VM_gecko_resize( void ) {
3638 clgecko_t *instance;
3640 VM_SAFEPARMCOUNT( 3, VM_gecko_movemouse );
3642 name = PRVM_G_STRING( OFS_PARM0 );
3643 VM_CheckEmptyString( name );
3644 w = PRVM_G_FLOAT( OFS_PARM1 );
3645 h = PRVM_G_FLOAT( OFS_PARM2 );
3647 instance = CL_Gecko_FindBrowser( name );
3651 CL_Gecko_Resize( instance, (int) w, (int) h );
3656 ========================
3657 VM_gecko_get_texture_extent
3659 vector gecko_get_texture_extent( string name )
3660 ========================
3662 void VM_gecko_get_texture_extent( void ) {
3664 clgecko_t *instance;
3666 VM_SAFEPARMCOUNT( 1, VM_gecko_movemouse );
3668 name = PRVM_G_STRING( OFS_PARM0 );
3669 VM_CheckEmptyString( name );
3671 PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
3672 instance = CL_Gecko_FindBrowser( name );
3674 PRVM_G_VECTOR(OFS_RETURN)[0] = 0;
3675 PRVM_G_VECTOR(OFS_RETURN)[1] = 0;
3678 CL_Gecko_GetTextureExtent( instance,
3679 PRVM_G_VECTOR(OFS_RETURN), PRVM_G_VECTOR(OFS_RETURN)+1 );
3688 Writes new values for v_forward, v_up, and v_right based on angles
3689 void makevectors(vector angle)
3692 void VM_makevectors (void)
3694 prvm_eval_t *valforward, *valright, *valup;
3695 valforward = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_forward);
3696 valright = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_right);
3697 valup = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_up);
3698 if (!valforward || !valright || !valup)
3700 VM_Warning("makevectors: could not find v_forward, v_right, or v_up global variables\n");
3703 VM_SAFEPARMCOUNT(1, VM_makevectors);
3704 AngleVectors (PRVM_G_VECTOR(OFS_PARM0), valforward->vector, valright->vector, valup->vector);
3711 Writes new values for v_forward, v_up, and v_right based on the given forward vector
3712 vectorvectors(vector)
3715 void VM_vectorvectors (void)
3717 prvm_eval_t *valforward, *valright, *valup;
3718 valforward = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_forward);
3719 valright = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_right);
3720 valup = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_up);
3721 if (!valforward || !valright || !valup)
3723 VM_Warning("vectorvectors: could not find v_forward, v_right, or v_up global variables\n");
3726 VM_SAFEPARMCOUNT(1, VM_vectorvectors);
3727 VectorNormalize2(PRVM_G_VECTOR(OFS_PARM0), valforward->vector);
3728 VectorVectors(valforward->vector, valright->vector, valup->vector);
3732 ========================
3735 void drawline(float width, vector pos1, vector pos2, vector rgb, float alpha, float flags)
3736 ========================
3738 void VM_drawline (void)
3740 float *c1, *c2, *rgb;
3742 unsigned char flags;
3744 VM_SAFEPARMCOUNT(6, VM_drawline);
3745 width = PRVM_G_FLOAT(OFS_PARM0);
3746 c1 = PRVM_G_VECTOR(OFS_PARM1);
3747 c2 = PRVM_G_VECTOR(OFS_PARM2);
3748 rgb = PRVM_G_VECTOR(OFS_PARM3);
3749 alpha = PRVM_G_FLOAT(OFS_PARM4);
3750 flags = (int)PRVM_G_FLOAT(OFS_PARM5);
3751 DrawQ_Line(width, c1[0], c1[1], c2[0], c2[1], rgb[0], rgb[1], rgb[2], alpha, flags);
3754 // float(float number, float quantity) bitshift (EXT_BITSHIFT)
3755 void VM_bitshift (void)
3758 VM_SAFEPARMCOUNT(2, VM_bitshift);
3760 n1 = (int)fabs((int)PRVM_G_FLOAT(OFS_PARM0));
3761 n2 = (int)PRVM_G_FLOAT(OFS_PARM1);
3763 PRVM_G_FLOAT(OFS_RETURN) = n1;
3766 PRVM_G_FLOAT(OFS_RETURN) = (n1 >> -n2);
3768 PRVM_G_FLOAT(OFS_RETURN) = (n1 << n2);
3771 ////////////////////////////////////////
3772 // AltString functions
3773 ////////////////////////////////////////
3776 ========================
3779 float altstr_count(string)
3780 ========================
3782 void VM_altstr_count( void )
3784 const char *altstr, *pos;
3787 VM_SAFEPARMCOUNT( 1, VM_altstr_count );
3789 altstr = PRVM_G_STRING( OFS_PARM0 );
3790 //VM_CheckEmptyString( altstr );
3792 for( count = 0, pos = altstr ; *pos ; pos++ ) {
3793 if( *pos == '\\' ) {
3797 } else if( *pos == '\'' ) {
3802 PRVM_G_FLOAT( OFS_RETURN ) = (float) (count / 2);
3806 ========================
3809 string altstr_prepare(string)
3810 ========================
3812 void VM_altstr_prepare( void )
3815 const char *instr, *in;
3817 char outstr[VM_STRINGTEMP_LENGTH];
3819 VM_SAFEPARMCOUNT( 1, VM_altstr_prepare );
3821 instr = PRVM_G_STRING( OFS_PARM0 );
3823 for( out = outstr, in = instr, size = sizeof(outstr) - 1 ; size && *in ; size--, in++, out++ )
3832 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
3836 ========================
3839 string altstr_get(string, float)
3840 ========================
3842 void VM_altstr_get( void )
3844 const char *altstr, *pos;
3847 char outstr[VM_STRINGTEMP_LENGTH];
3849 VM_SAFEPARMCOUNT( 2, VM_altstr_get );
3851 altstr = PRVM_G_STRING( OFS_PARM0 );
3853 count = (int)PRVM_G_FLOAT( OFS_PARM1 );
3854 count = count * 2 + 1;
3856 for( pos = altstr ; *pos && count ; pos++ )
3857 if( *pos == '\\' ) {
3860 } else if( *pos == '\'' )
3864 PRVM_G_INT( OFS_RETURN ) = 0;
3868 for( out = outstr, size = sizeof(outstr) - 1 ; size && *pos ; size--, pos++, out++ )
3869 if( *pos == '\\' ) {
3874 } else if( *pos == '\'' )
3880 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
3884 ========================
3887 string altstr_set(string altstr, float num, string set)
3888 ========================
3890 void VM_altstr_set( void )
3893 const char *altstr, *str;
3896 char outstr[VM_STRINGTEMP_LENGTH];
3898 VM_SAFEPARMCOUNT( 3, VM_altstr_set );
3900 altstr = PRVM_G_STRING( OFS_PARM0 );
3902 num = (int)PRVM_G_FLOAT( OFS_PARM1 );
3904 str = PRVM_G_STRING( OFS_PARM2 );
3907 for( num = num * 2 + 1, in = altstr; *in && num; *out++ = *in++ )
3912 } else if( *in == '\'' ) {
3917 for( ; *str; *out++ = *str++ );
3918 // now jump over the old content
3920 if( *in == '\'' || (*in == '\\' && !*++in) )
3923 strlcpy(out, in, outstr + sizeof(outstr) - out);
3924 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
3928 ========================
3931 string altstr_ins(string altstr, float num, string set)
3932 ========================
3934 void VM_altstr_ins(void)
3942 char outstr[VM_STRINGTEMP_LENGTH];
3944 VM_SAFEPARMCOUNT(3, VM_altstr_ins);
3946 in = instr = PRVM_G_STRING( OFS_PARM0 );
3947 num = (int)PRVM_G_FLOAT( OFS_PARM1 );
3948 set = setstr = PRVM_G_STRING( OFS_PARM2 );
3951 for( num = num * 2 + 2 ; *in && num > 0 ; *out++ = *in++ )
3956 } else if( *in == '\'' ) {
3961 for( ; *set ; *out++ = *set++ );
3964 strlcpy(out, in, outstr + sizeof(outstr) - out);
3965 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
3969 ////////////////////////////////////////
3970 // BufString functions
3971 ////////////////////////////////////////
3972 //[515]: string buffers support
3974 static size_t stringbuffers_sortlength;
3976 static void BufStr_Expand(prvm_stringbuffer_t *stringbuffer, int strindex)
3978 if (stringbuffer->max_strings <= strindex)
3980 char **oldstrings = stringbuffer->strings;
3981 stringbuffer->max_strings = max(stringbuffer->max_strings * 2, 128);
3982 while (stringbuffer->max_strings <= strindex)
3983 stringbuffer->max_strings *= 2;
3984 stringbuffer->strings = (char **) Mem_Alloc(prog->progs_mempool, stringbuffer->max_strings * sizeof(stringbuffer->strings[0]));
3985 if (stringbuffer->num_strings > 0)
3986 memcpy(stringbuffer->strings, oldstrings, stringbuffer->num_strings * sizeof(stringbuffer->strings[0]));
3988 Mem_Free(oldstrings);
3992 static void BufStr_Shrink(prvm_stringbuffer_t *stringbuffer)
3994 // reduce num_strings if there are empty string slots at the end
3995 while (stringbuffer->num_strings > 0 && stringbuffer->strings[stringbuffer->num_strings - 1] == NULL)
3996 stringbuffer->num_strings--;
3998 // if empty, free the string pointer array
3999 if (stringbuffer->num_strings == 0)
4001 stringbuffer->max_strings = 0;
4002 if (stringbuffer->strings)
4003 Mem_Free(stringbuffer->strings);
4004 stringbuffer->strings = NULL;
4008 static int BufStr_SortStringsUP (const void *in1, const void *in2)
4011 a = *((const char **) in1);
4012 b = *((const char **) in2);
4014 if(!b[0]) return -1;
4015 return strncmp(a, b, stringbuffers_sortlength);
4018 static int BufStr_SortStringsDOWN (const void *in1, const void *in2)
4021 a = *((const char **) in1);
4022 b = *((const char **) in2);
4024 if(!b[0]) return -1;
4025 return strncmp(b, a, stringbuffers_sortlength);
4029 ========================
4031 creates new buffer, and returns it's index, returns -1 if failed
4032 float buf_create(void) = #460;
4033 ========================
4035 void VM_buf_create (void)
4037 prvm_stringbuffer_t *stringbuffer;
4039 VM_SAFEPARMCOUNT(0, VM_buf_create);
4040 stringbuffer = (prvm_stringbuffer_t *) Mem_ExpandableArray_AllocRecord(&prog->stringbuffersarray);
4041 for (i = 0;stringbuffer != Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, i);i++);
4042 stringbuffer->origin = PRVM_AllocationOrigin();
4043 PRVM_G_FLOAT(OFS_RETURN) = i;
4047 ========================
4049 deletes buffer and all strings in it
4050 void buf_del(float bufhandle) = #461;
4051 ========================
4053 void VM_buf_del (void)
4055 prvm_stringbuffer_t *stringbuffer;
4056 VM_SAFEPARMCOUNT(1, VM_buf_del);
4057 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4061 for (i = 0;i < stringbuffer->num_strings;i++)
4062 if (stringbuffer->strings[i])
4063 Mem_Free(stringbuffer->strings[i]);
4064 if (stringbuffer->strings)
4065 Mem_Free(stringbuffer->strings);
4066 if(stringbuffer->origin)
4067 PRVM_Free((char *)stringbuffer->origin);
4068 Mem_ExpandableArray_FreeRecord(&prog->stringbuffersarray, stringbuffer);
4072 VM_Warning("VM_buf_del: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4078 ========================
4080 how many strings are stored in buffer
4081 float buf_getsize(float bufhandle) = #462;
4082 ========================
4084 void VM_buf_getsize (void)
4086 prvm_stringbuffer_t *stringbuffer;
4087 VM_SAFEPARMCOUNT(1, VM_buf_getsize);
4089 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4092 PRVM_G_FLOAT(OFS_RETURN) = -1;
4093 VM_Warning("VM_buf_getsize: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4097 PRVM_G_FLOAT(OFS_RETURN) = stringbuffer->num_strings;
4101 ========================
4103 copy all content from one buffer to another, make sure it exists
4104 void buf_copy(float bufhandle_from, float bufhandle_to) = #463;
4105 ========================
4107 void VM_buf_copy (void)
4109 prvm_stringbuffer_t *srcstringbuffer, *dststringbuffer;
4111 VM_SAFEPARMCOUNT(2, VM_buf_copy);
4113 srcstringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4114 if(!srcstringbuffer)
4116 VM_Warning("VM_buf_copy: invalid source buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4119 i = (int)PRVM_G_FLOAT(OFS_PARM1);
4120 if(i == (int)PRVM_G_FLOAT(OFS_PARM0))
4122 VM_Warning("VM_buf_copy: source == destination (%i) in %s\n", i, PRVM_NAME);
4125 dststringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4126 if(!dststringbuffer)
4128 VM_Warning("VM_buf_copy: invalid destination buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM1), PRVM_NAME);
4132 for (i = 0;i < dststringbuffer->num_strings;i++)
4133 if (dststringbuffer->strings[i])
4134 Mem_Free(dststringbuffer->strings[i]);
4135 if (dststringbuffer->strings)
4136 Mem_Free(dststringbuffer->strings);
4137 *dststringbuffer = *srcstringbuffer;
4138 if (dststringbuffer->max_strings)
4139 dststringbuffer->strings = (char **)Mem_Alloc(prog->progs_mempool, sizeof(dststringbuffer->strings[0]) * dststringbuffer->max_strings);
4141 for (i = 0;i < dststringbuffer->num_strings;i++)
4143 if (srcstringbuffer->strings[i])
4146 stringlen = strlen(srcstringbuffer->strings[i]) + 1;
4147 dststringbuffer->strings[i] = (char *)Mem_Alloc(prog->progs_mempool, stringlen);
4148 memcpy(dststringbuffer->strings[i], srcstringbuffer->strings[i], stringlen);
4154 ========================
4156 sort buffer by beginnings of strings (cmplength defaults it's length)
4157 "backward == TRUE" means that sorting goes upside-down
4158 void buf_sort(float bufhandle, float cmplength, float backward) = #464;
4159 ========================
4161 void VM_buf_sort (void)
4163 prvm_stringbuffer_t *stringbuffer;
4164 VM_SAFEPARMCOUNT(3, VM_buf_sort);
4166 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4169 VM_Warning("VM_buf_sort: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4172 if(stringbuffer->num_strings <= 0)
4174 VM_Warning("VM_buf_sort: tried to sort empty buffer %i in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4177 stringbuffers_sortlength = (int)PRVM_G_FLOAT(OFS_PARM1);
4178 if(stringbuffers_sortlength <= 0)
4179 stringbuffers_sortlength = 0x7FFFFFFF;
4181 if(!PRVM_G_FLOAT(OFS_PARM2))
4182 qsort(stringbuffer->strings, stringbuffer->num_strings, sizeof(char*), BufStr_SortStringsUP);
4184 qsort(stringbuffer->strings, stringbuffer->num_strings, sizeof(char*), BufStr_SortStringsDOWN);
4186 BufStr_Shrink(stringbuffer);
4190 ========================
4192 concantenates all buffer string into one with "glue" separator and returns it as tempstring
4193 string buf_implode(float bufhandle, string glue) = #465;
4194 ========================
4196 void VM_buf_implode (void)
4198 prvm_stringbuffer_t *stringbuffer;
4199 char k[VM_STRINGTEMP_LENGTH];
4203 VM_SAFEPARMCOUNT(2, VM_buf_implode);
4205 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4206 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
4209 VM_Warning("VM_buf_implode: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4212 if(!stringbuffer->num_strings)
4214 sep = PRVM_G_STRING(OFS_PARM1);
4216 for(l = i = 0;i < stringbuffer->num_strings;i++)
4218 if(stringbuffer->strings[i])
4220 l += (i > 0 ? strlen(sep) : 0) + strlen(stringbuffer->strings[i]);
4221 if (l >= sizeof(k) - 1)
4223 strlcat(k, sep, sizeof(k));
4224 strlcat(k, stringbuffer->strings[i], sizeof(k));
4227 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(k);
4231 ========================
4233 get a string from buffer, returns tempstring, dont str_unzone it!
4234 string bufstr_get(float bufhandle, float string_index) = #465;
4235 ========================
4237 void VM_bufstr_get (void)
4239 prvm_stringbuffer_t *stringbuffer;
4241 VM_SAFEPARMCOUNT(2, VM_bufstr_get);
4243 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
4244 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4247 VM_Warning("VM_bufstr_get: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4250 strindex = (int)PRVM_G_FLOAT(OFS_PARM1);
4253 VM_Warning("VM_bufstr_get: invalid string index %i used in %s\n", strindex, PRVM_NAME);
4256 if (strindex < stringbuffer->num_strings && stringbuffer->strings[strindex])
4257 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(stringbuffer->strings[strindex]);
4261 ========================
4263 copies a string into selected slot of buffer
4264 void bufstr_set(float bufhandle, float string_index, string str) = #466;
4265 ========================
4267 void VM_bufstr_set (void)
4270 prvm_stringbuffer_t *stringbuffer;
4273 VM_SAFEPARMCOUNT(3, VM_bufstr_set);
4275 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4278 VM_Warning("VM_bufstr_set: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4281 strindex = (int)PRVM_G_FLOAT(OFS_PARM1);
4282 if(strindex < 0 || strindex >= 1000000) // huge number of strings
4284 VM_Warning("VM_bufstr_set: invalid string index %i used in %s\n", strindex, PRVM_NAME);
4288 BufStr_Expand(stringbuffer, strindex);
4289 stringbuffer->num_strings = max(stringbuffer->num_strings, strindex + 1);
4291 if(stringbuffer->strings[strindex])
4292 Mem_Free(stringbuffer->strings[strindex]);
4293 stringbuffer->strings[strindex] = NULL;
4295 news = PRVM_G_STRING(OFS_PARM2);
4296 if (news && news[0])
4298 size_t alloclen = strlen(news) + 1;
4299 stringbuffer->strings[strindex] = (char *)Mem_Alloc(prog->progs_mempool, alloclen);
4300 memcpy(stringbuffer->strings[strindex], news, alloclen);
4303 BufStr_Shrink(stringbuffer);
4307 ========================
4309 adds string to buffer in first free slot and returns its index
4310 "order == TRUE" means that string will be added after last "full" slot
4311 float bufstr_add(float bufhandle, string str, float order) = #467;
4312 ========================
4314 void VM_bufstr_add (void)
4316 int order, strindex;
4317 prvm_stringbuffer_t *stringbuffer;
4321 VM_SAFEPARMCOUNT(3, VM_bufstr_add);
4323 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4324 PRVM_G_FLOAT(OFS_RETURN) = -1;
4327 VM_Warning("VM_bufstr_add: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4330 string = PRVM_G_STRING(OFS_PARM1);
4331 if(!string || !string[0])
4333 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);
4336 order = (int)PRVM_G_FLOAT(OFS_PARM2);
4338 strindex = stringbuffer->num_strings;
4340 for (strindex = 0;strindex < stringbuffer->num_strings;strindex++)
4341 if (stringbuffer->strings[strindex] == NULL)
4344 BufStr_Expand(stringbuffer, strindex);
4346 stringbuffer->num_strings = max(stringbuffer->num_strings, strindex + 1);
4347 alloclen = strlen(string) + 1;
4348 stringbuffer->strings[strindex] = (char *)Mem_Alloc(prog->progs_mempool, alloclen);
4349 memcpy(stringbuffer->strings[strindex], string, alloclen);
4351 PRVM_G_FLOAT(OFS_RETURN) = strindex;
4355 ========================
4357 delete string from buffer
4358 void bufstr_free(float bufhandle, float string_index) = #468;
4359 ========================
4361 void VM_bufstr_free (void)
4364 prvm_stringbuffer_t *stringbuffer;
4365 VM_SAFEPARMCOUNT(2, VM_bufstr_free);
4367 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4370 VM_Warning("VM_bufstr_free: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4373 i = (int)PRVM_G_FLOAT(OFS_PARM1);
4376 VM_Warning("VM_bufstr_free: invalid string index %i used in %s\n", i, PRVM_NAME);
4380 if (i < stringbuffer->num_strings)
4382 if(stringbuffer->strings[i])
4383 Mem_Free(stringbuffer->strings[i]);
4384 stringbuffer->strings[i] = NULL;
4387 BufStr_Shrink(stringbuffer);
4396 void VM_buf_cvarlist(void)
4399 const char *partial, *antipartial;
4400 size_t len, antilen;
4402 qboolean ispattern, antiispattern;
4404 prvm_stringbuffer_t *stringbuffer;
4405 VM_SAFEPARMCOUNTRANGE(2, 3, VM_buf_cvarlist);
4407 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4410 VM_Warning("VM_bufstr_free: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4414 partial = PRVM_G_STRING(OFS_PARM1);
4418 len = strlen(partial);
4421 antipartial = PRVM_G_STRING(OFS_PARM2);
4427 antilen = strlen(antipartial);
4429 for (n = 0;n < stringbuffer->num_strings;n++)
4430 if (stringbuffer->strings[n])
4431 Mem_Free(stringbuffer->strings[n]);
4432 if (stringbuffer->strings)
4433 Mem_Free(stringbuffer->strings);
4434 stringbuffer->strings = NULL;
4436 ispattern = partial && (strchr(partial, '*') || strchr(partial, '?'));
4437 antiispattern = antipartial && (strchr(antipartial, '*') || strchr(antipartial, '?'));
4440 for(cvar = cvar_vars; cvar; cvar = cvar->next)
4442 if(len && (ispattern ? !matchpattern_with_separator(cvar->name, partial, false, "", false) : strncmp(partial, cvar->name, len)))
4445 if(antilen && (antiispattern ? matchpattern_with_separator(cvar->name, antipartial, false, "", false) : !strncmp(antipartial, cvar->name, antilen)))
4451 stringbuffer->max_strings = stringbuffer->num_strings = n;
4452 if (stringbuffer->max_strings)
4453 stringbuffer->strings = (char **)Mem_Alloc(prog->progs_mempool, sizeof(stringbuffer->strings[0]) * stringbuffer->max_strings);
4456 for(cvar = cvar_vars; cvar; cvar = cvar->next)
4458 if(len && (ispattern ? !matchpattern_with_separator(cvar->name, partial, false, "", false) : strncmp(partial, cvar->name, len)))
4461 if(antilen && (antiispattern ? matchpattern_with_separator(cvar->name, antipartial, false, "", false) : !strncmp(antipartial, cvar->name, antilen)))
4464 alloclen = strlen(cvar->name) + 1;
4465 stringbuffer->strings[n] = (char *)Mem_Alloc(prog->progs_mempool, alloclen);
4466 memcpy(stringbuffer->strings[n], cvar->name, alloclen);
4481 This was a major timewaster in progs, so it was converted to C
4484 void VM_changeyaw (void)
4487 float ideal, current, move, speed;
4489 // this is called (VERY HACKISHLY) by SV_MoveToGoal, so it can not use any
4490 // parameters because they are the parameters to SV_MoveToGoal, not this
4491 //VM_SAFEPARMCOUNT(0, VM_changeyaw);
4493 ent = PRVM_PROG_TO_EDICT(PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict);
4494 if (ent == prog->edicts)
4496 VM_Warning("changeyaw: can not modify world entity\n");
4499 if (ent->priv.server->free)
4501 VM_Warning("changeyaw: can not modify free entity\n");
4504 if (prog->fieldoffsets.angles < 0 || prog->fieldoffsets.ideal_yaw < 0 || prog->fieldoffsets.yaw_speed < 0)
4506 VM_Warning("changeyaw: angles, ideal_yaw, or yaw_speed field(s) not found\n");
4509 current = ANGLEMOD(PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.angles)->vector[1]);
4510 ideal = PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.ideal_yaw)->_float;
4511 speed = PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.yaw_speed)->_float;
4513 if (current == ideal)
4515 move = ideal - current;
4516 if (ideal > current)
4537 PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.angles)->vector[1] = ANGLEMOD (current + move);
4545 void VM_changepitch (void)
4548 float ideal, current, move, speed;
4550 VM_SAFEPARMCOUNT(1, VM_changepitch);
4552 ent = PRVM_G_EDICT(OFS_PARM0);
4553 if (ent == prog->edicts)
4555 VM_Warning("changepitch: can not modify world entity\n");
4558 if (ent->priv.server->free)
4560 VM_Warning("changepitch: can not modify free entity\n");
4563 if (prog->fieldoffsets.angles < 0 || prog->fieldoffsets.idealpitch < 0 || prog->fieldoffsets.pitch_speed < 0)
4565 VM_Warning("changepitch: angles, idealpitch, or pitch_speed field(s) not found\n");
4568 current = ANGLEMOD(PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.angles)->vector[0]);
4569 ideal = PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.idealpitch)->_float;
4570 speed = PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.pitch_speed)->_float;
4572 if (current == ideal)
4574 move = ideal - current;
4575 if (ideal > current)
4596 PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.angles)->vector[0] = ANGLEMOD (current + move);
4600 void VM_uncolorstring (void)
4602 char szNewString[VM_STRINGTEMP_LENGTH];
4603 const char *szString;
4606 VM_SAFEPARMCOUNT(1, VM_uncolorstring);
4607 szString = PRVM_G_STRING(OFS_PARM0);
4608 COM_StringDecolorize(szString, 0, szNewString, sizeof(szNewString), TRUE);
4609 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(szNewString);
4613 // #221 float(string str, string sub[, float startpos]) strstrofs (FTE_STRINGS)
4614 //strstr, without generating a new string. Use in conjunction with FRIK_FILE's substring for more similar strstr.
4615 void VM_strstrofs (void)
4617 const char *instr, *match;
4619 VM_SAFEPARMCOUNTRANGE(2, 3, VM_strstrofs);
4620 instr = PRVM_G_STRING(OFS_PARM0);
4621 match = PRVM_G_STRING(OFS_PARM1);
4622 firstofs = (prog->argc > 2)?(int)PRVM_G_FLOAT(OFS_PARM2):0;
4624 if (firstofs && (firstofs < 0 || firstofs > (int)strlen(instr)))
4626 PRVM_G_FLOAT(OFS_RETURN) = -1;
4630 match = strstr(instr+firstofs, match);
4632 PRVM_G_FLOAT(OFS_RETURN) = -1;
4634 PRVM_G_FLOAT(OFS_RETURN) = match - instr;
4637 //#222 string(string s, float index) str2chr (FTE_STRINGS)
4638 void VM_str2chr (void)
4641 VM_SAFEPARMCOUNT(2, VM_str2chr);
4642 s = PRVM_G_STRING(OFS_PARM0);
4643 if((unsigned)PRVM_G_FLOAT(OFS_PARM1) < strlen(s))
4644 PRVM_G_FLOAT(OFS_RETURN) = (unsigned char)s[(unsigned)PRVM_G_FLOAT(OFS_PARM1)];
4646 PRVM_G_FLOAT(OFS_RETURN) = 0;
4649 //#223 string(float c, ...) chr2str (FTE_STRINGS)
4650 void VM_chr2str (void)
4654 VM_SAFEPARMCOUNTRANGE(0, 8, VM_chr2str);
4655 for(i = 0;i < prog->argc && i < (int)sizeof(t) - 1;i++)
4656 t[i] = (unsigned char)PRVM_G_FLOAT(OFS_PARM0+i*3);
4658 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(t);
4661 static int chrconv_number(int i, int base, int conv)
4686 static int chrconv_punct(int i, int base, int conv)
4704 static int chrchar_alpha(int i, int basec, int baset, int convc, int convt, int charnum)
4706 //convert case and colour seperatly...
4723 baset = 128*((charnum&1) == (convt-5));
4739 return i + basec + baset;
4741 // #224 string(float ccase, float calpha, float cnum, string s, ...) strconv (FTE_STRINGS)
4742 //bulk convert a string. change case or colouring.
4743 void VM_strconv (void)
4745 int ccase, redalpha, rednum, len, i;
4746 unsigned char resbuf[VM_STRINGTEMP_LENGTH];
4747 unsigned char *result = resbuf;
4749 VM_SAFEPARMCOUNTRANGE(3, 8, VM_strconv);
4751 ccase = (int) PRVM_G_FLOAT(OFS_PARM0); //0 same, 1 lower, 2 upper
4752 redalpha = (int) PRVM_G_FLOAT(OFS_PARM1); //0 same, 1 white, 2 red, 5 alternate, 6 alternate-alternate
4753 rednum = (int) PRVM_G_FLOAT(OFS_PARM2); //0 same, 1 white, 2 red, 3 redspecial, 4 whitespecial, 5 alternate, 6 alternate-alternate
4754 VM_VarString(3, (char *) resbuf, sizeof(resbuf));
4755 len = strlen((char *) resbuf);
4757 for (i = 0; i < len; i++, result++) //should this be done backwards?
4759 if (*result >= '0' && *result <= '9') //normal numbers...
4760 *result = chrconv_number(*result, '0', rednum);
4761 else if (*result >= '0'+128 && *result <= '9'+128)
4762 *result = chrconv_number(*result, '0'+128, rednum);
4763 else if (*result >= '0'+128-30 && *result <= '9'+128-30)
4764 *result = chrconv_number(*result, '0'+128-30, rednum);
4765 else if (*result >= '0'-30 && *result <= '9'-30)
4766 *result = chrconv_number(*result, '0'-30, rednum);
4768 else if (*result >= 'a' && *result <= 'z') //normal numbers...
4769 *result = chrchar_alpha(*result, 'a', 0, ccase, redalpha, i);
4770 else if (*result >= 'A' && *result <= 'Z') //normal numbers...
4771 *result = chrchar_alpha(*result, 'A', 0, ccase, redalpha, i);
4772 else if (*result >= 'a'+128 && *result <= 'z'+128) //normal numbers...
4773 *result = chrchar_alpha(*result, 'a', 128, ccase, redalpha, i);
4774 else if (*result >= 'A'+128 && *result <= 'Z'+128) //normal numbers...
4775 *result = chrchar_alpha(*result, 'A', 128, ccase, redalpha, i);
4777 else if ((*result & 127) < 16 || !redalpha) //special chars..
4779 else if (*result < 128)
4780 *result = chrconv_punct(*result, 0, redalpha);
4782 *result = chrconv_punct(*result, 128, redalpha);
4786 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString((char *) resbuf);
4789 // #225 string(float chars, string s, ...) strpad (FTE_STRINGS)
4790 void VM_strpad (void)
4792 char src[VM_STRINGTEMP_LENGTH];
4793 char destbuf[VM_STRINGTEMP_LENGTH];
4795 VM_SAFEPARMCOUNTRANGE(1, 8, VM_strpad);
4796 pad = (int) PRVM_G_FLOAT(OFS_PARM0);
4797 VM_VarString(1, src, sizeof(src));
4799 // note: < 0 = left padding, > 0 = right padding,
4800 // this is reverse logic of printf!
4801 dpsnprintf(destbuf, sizeof(destbuf), "%*s", -pad, src);
4803 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(destbuf);
4806 // #226 string(string info, string key, string value, ...) infoadd (FTE_STRINGS)
4807 //uses qw style \key\value strings
4808 void VM_infoadd (void)
4810 const char *info, *key;
4811 char value[VM_STRINGTEMP_LENGTH];
4812 char temp[VM_STRINGTEMP_LENGTH];
4814 VM_SAFEPARMCOUNTRANGE(2, 8, VM_infoadd);
4815 info = PRVM_G_STRING(OFS_PARM0);
4816 key = PRVM_G_STRING(OFS_PARM1);
4817 VM_VarString(2, value, sizeof(value));
4819 strlcpy(temp, info, VM_STRINGTEMP_LENGTH);
4821 InfoString_SetValue(temp, VM_STRINGTEMP_LENGTH, key, value);
4823 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(temp);
4826 // #227 string(string info, string key) infoget (FTE_STRINGS)
4827 //uses qw style \key\value strings
4828 void VM_infoget (void)
4832 char value[VM_STRINGTEMP_LENGTH];
4834 VM_SAFEPARMCOUNT(2, VM_infoget);
4835 info = PRVM_G_STRING(OFS_PARM0);
4836 key = PRVM_G_STRING(OFS_PARM1);
4838 InfoString_GetValue(info, key, value, VM_STRINGTEMP_LENGTH);
4840 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(value);
4843 //#228 float(string s1, string s2, float len) strncmp (FTE_STRINGS)
4844 // also float(string s1, string s2) strcmp (FRIK_FILE)
4845 void VM_strncmp (void)
4847 const char *s1, *s2;
4848 VM_SAFEPARMCOUNTRANGE(2, 3, VM_strncmp);
4849 s1 = PRVM_G_STRING(OFS_PARM0);
4850 s2 = PRVM_G_STRING(OFS_PARM1);
4853 PRVM_G_FLOAT(OFS_RETURN) = strncmp(s1, s2, (size_t)PRVM_G_FLOAT(OFS_PARM2));
4857 PRVM_G_FLOAT(OFS_RETURN) = strcmp(s1, s2);
4861 // #229 float(string s1, string s2) strcasecmp (FTE_STRINGS)
4862 // #230 float(string s1, string s2, float len) strncasecmp (FTE_STRINGS)
4863 void VM_strncasecmp (void)
4865 const char *s1, *s2;
4866 VM_SAFEPARMCOUNTRANGE(2, 3, VM_strncasecmp);
4867 s1 = PRVM_G_STRING(OFS_PARM0);
4868 s2 = PRVM_G_STRING(OFS_PARM1);
4871 PRVM_G_FLOAT(OFS_RETURN) = strncasecmp(s1, s2, (size_t)PRVM_G_FLOAT(OFS_PARM2));
4875 PRVM_G_FLOAT(OFS_RETURN) = strcasecmp(s1, s2);
4879 // #494 float(float caseinsensitive, string s, ...) crc16
4883 static char s[VM_STRINGTEMP_LENGTH];
4884 VM_SAFEPARMCOUNTRANGE(2, 8, VM_hash);
4885 insensitive = PRVM_G_FLOAT(OFS_PARM0);
4886 VM_VarString(1, s, sizeof(s));
4887 PRVM_G_FLOAT(OFS_RETURN) = (unsigned short) ((insensitive ? CRC_Block_CaseInsensitive : CRC_Block) ((unsigned char *) s, strlen(s)));
4890 void VM_wasfreed (void)
4892 VM_SAFEPARMCOUNT(1, VM_wasfreed);
4893 PRVM_G_FLOAT(OFS_RETURN) = PRVM_G_EDICT(OFS_PARM0)->priv.required->free;
4896 void VM_SetTraceGlobals(const trace_t *trace)
4899 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_allsolid)))
4900 val->_float = trace->allsolid;
4901 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_startsolid)))
4902 val->_float = trace->startsolid;
4903 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_fraction)))
4904 val->_float = trace->fraction;
4905 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_inwater)))
4906 val->_float = trace->inwater;
4907 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_inopen)))
4908 val->_float = trace->inopen;
4909 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_endpos)))
4910 VectorCopy(trace->endpos, val->vector);
4911 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_plane_normal)))
4912 VectorCopy(trace->plane.normal, val->vector);
4913 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_plane_dist)))
4914 val->_float = trace->plane.dist;
4915 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_ent)))
4916 val->edict = PRVM_EDICT_TO_PROG(trace->ent ? trace->ent : prog->edicts);
4917 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dpstartcontents)))
4918 val->_float = trace->startsupercontents;
4919 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dphitcontents)))
4920 val->_float = trace->hitsupercontents;
4921 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dphitq3surfaceflags)))
4922 val->_float = trace->hitq3surfaceflags;
4923 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dphittexturename)))
4924 val->string = trace->hittexture ? PRVM_SetTempString(trace->hittexture->name) : 0;
4929 void VM_Cmd_Init(void)
4931 // only init the stuff for the current prog
4935 // VM_BufStr_Init();
4938 void VM_Cmd_Reset(void)
4940 CL_PurgeOwner( MENUOWNER );
4942 VM_Files_CloseAll();
4944 // VM_BufStr_ShutDown();
4947 // #510 string(string input, ...) uri_escape (DP_QC_URI_ESCAPE)
4948 // does URI escaping on a string (replace evil stuff by %AB escapes)
4949 void VM_uri_escape (void)
4951 char src[VM_STRINGTEMP_LENGTH];
4952 char dest[VM_STRINGTEMP_LENGTH];
4954 static const char *hex = "0123456789ABCDEF";
4956 VM_SAFEPARMCOUNTRANGE(1, 8, VM_uri_escape);
4957 VM_VarString(0, src, sizeof(src));
4959 for(p = src, q = dest; *p && q < dest + sizeof(dest) - 3; ++p)
4961 if((*p >= 'A' && *p <= 'Z')
4962 || (*p >= 'a' && *p <= 'z')
4963 || (*p >= '0' && *p <= '9')
4964 || (*p == '-') || (*p == '_') || (*p == '.')
4965 || (*p == '!') || (*p == '~') || (*p == '*')
4966 || (*p == '\'') || (*p == '(') || (*p == ')'))
4971 *q++ = hex[(*(unsigned char *)p >> 4) & 0xF];
4972 *q++ = hex[ *(unsigned char *)p & 0xF];
4977 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(dest);
4980 // #510 string(string input, ...) uri_unescape (DP_QC_URI_ESCAPE)
4981 // does URI unescaping on a string (get back the evil stuff)
4982 void VM_uri_unescape (void)
4984 char src[VM_STRINGTEMP_LENGTH];
4985 char dest[VM_STRINGTEMP_LENGTH];
4989 VM_SAFEPARMCOUNTRANGE(1, 8, VM_uri_unescape);
4990 VM_VarString(0, src, sizeof(src));
4992 for(p = src, q = dest; *p; ) // no need to check size, because unescape can't expand
4996 if(p[1] >= '0' && p[1] <= '9')
4998 else if(p[1] >= 'a' && p[1] <= 'f')
4999 hi = p[1] - 'a' + 10;
5000 else if(p[1] >= 'A' && p[1] <= 'F')
5001 hi = p[1] - 'A' + 10;
5004 if(p[2] >= '0' && p[2] <= '9')
5006 else if(p[2] >= 'a' && p[2] <= 'f')
5007 lo = p[2] - 'a' + 10;
5008 else if(p[2] >= 'A' && p[2] <= 'F')
5009 lo = p[2] - 'A' + 10;
5012 if(hi != 0 || lo != 0) // don't unescape NUL bytes
5013 *q++ = (char) (hi * 0x10 + lo);
5024 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(dest);
5027 // #502 string(string filename) whichpack (DP_QC_WHICHPACK)
5028 // returns the name of the pack containing a file, or "" if it is not in any pack (but local or non-existant)
5029 void VM_whichpack (void)
5031 const char *fn, *pack;
5033 VM_SAFEPARMCOUNT(1, VM_whichpack);
5034 fn = PRVM_G_STRING(OFS_PARM0);
5035 pack = FS_WhichPack(fn);
5037 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(pack ? pack : "");
5045 char buffer[MAX_INPUTLINE];
5049 static void uri_to_string_callback(int status, size_t length_received, unsigned char *buffer, void *cbdata)
5051 uri_to_prog_t *handle = (uri_to_prog_t *) cbdata;
5053 if(!PRVM_ProgLoaded(handle->prognr))
5055 // curl reply came too late... so just drop it
5060 PRVM_SetProg(handle->prognr);
5062 if((prog->starttime == handle->starttime) && (prog->funcoffsets.URI_Get_Callback))
5064 if(length_received >= sizeof(handle->buffer))
5065 length_received = sizeof(handle->buffer) - 1;
5066 handle->buffer[length_received] = 0;
5068 PRVM_G_FLOAT(OFS_PARM0) = handle->id;
5069 PRVM_G_FLOAT(OFS_PARM1) = status;
5070 PRVM_G_INT(OFS_PARM2) = PRVM_SetTempString(handle->buffer);
5071 PRVM_ExecuteProgram(prog->funcoffsets.URI_Get_Callback, "QC function URI_Get_Callback is missing");
5078 // 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
5079 // 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
5080 void VM_uri_get (void)
5085 uri_to_prog_t *handle;
5087 if(!prog->funcoffsets.URI_Get_Callback)
5088 PRVM_ERROR("uri_get called by %s without URI_Get_Callback defined", PRVM_NAME);
5090 VM_SAFEPARMCOUNT(2, VM_uri_get);
5092 url = PRVM_G_STRING(OFS_PARM0);
5093 id = PRVM_G_FLOAT(OFS_PARM1);
5094 handle = (uri_to_prog_t *) Z_Malloc(sizeof(*handle)); // this can't be the prog's mem pool, as curl may call the callback later!
5096 handle->prognr = PRVM_GetProgNr();
5097 handle->starttime = prog->starttime;
5099 ret = Curl_Begin_ToMemory(url, (unsigned char *) handle->buffer, sizeof(handle->buffer), uri_to_string_callback, handle);
5102 PRVM_G_INT(OFS_RETURN) = 1;
5107 PRVM_G_INT(OFS_RETURN) = 0;
5111 void VM_netaddress_resolve (void)
5114 char normalized[128];
5116 lhnetaddress_t addr;
5118 VM_SAFEPARMCOUNTRANGE(1, 2, VM_netaddress_resolve);
5120 ip = PRVM_G_STRING(OFS_PARM0);
5123 port = (int) PRVM_G_FLOAT(OFS_PARM1);
5125 if(LHNETADDRESS_FromString(&addr, ip, port) && LHNETADDRESS_ToString(&addr, normalized, sizeof(normalized), prog->argc > 1))
5126 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(normalized);
5128 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString("");