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[256];
2294 static int tokens_startpos[256];
2295 static int tokens_endpos[256];
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;
2408 for (k = 0;k < numseparators;k++)
2410 if (!strncmp(p, separators[k], separatorlen[k]))
2412 p += separatorlen[k];
2416 if (k < numseparators)
2418 if (j < (int)sizeof(tokentext)-1)
2419 tokentext[j++] = *p;
2422 tokens_endpos[num_tokens] = p - tokenize_string;
2423 if (j >= (int)sizeof(tokentext))
2426 tokens[num_tokens++] = PRVM_SetTempString(token);
2431 PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
2434 //string(float n) argv = #442; // returns a word from the tokenized string (returns nothing for an invalid index)
2435 //this function originally written by KrimZon, made shorter by LordHavoc
2440 VM_SAFEPARMCOUNT(1,VM_argv);
2442 token_num = (int)PRVM_G_FLOAT(OFS_PARM0);
2445 token_num += num_tokens;
2447 if (token_num >= 0 && token_num < num_tokens)
2448 PRVM_G_INT(OFS_RETURN) = tokens[token_num];
2450 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
2453 //float(float n) argv_start_index = #515; // returns the start index of a token
2454 void VM_argv_start_index (void)
2458 VM_SAFEPARMCOUNT(1,VM_argv);
2460 token_num = (int)PRVM_G_FLOAT(OFS_PARM0);
2463 token_num += num_tokens;
2465 if (token_num >= 0 && token_num < num_tokens)
2466 PRVM_G_FLOAT(OFS_RETURN) = tokens_startpos[token_num];
2468 PRVM_G_FLOAT(OFS_RETURN) = -1;
2471 //float(float n) argv_end_index = #516; // returns the end index of a token
2472 void VM_argv_end_index (void)
2476 VM_SAFEPARMCOUNT(1,VM_argv);
2478 token_num = (int)PRVM_G_FLOAT(OFS_PARM0);
2481 token_num += num_tokens;
2483 if (token_num >= 0 && token_num < num_tokens)
2484 PRVM_G_FLOAT(OFS_RETURN) = tokens_endpos[token_num];
2486 PRVM_G_FLOAT(OFS_RETURN) = -1;
2496 void VM_isserver(void)
2498 VM_SAFEPARMCOUNT(0,VM_serverstate);
2500 PRVM_G_FLOAT(OFS_RETURN) = sv.active && (svs.maxclients > 1 || cls.state == ca_dedicated);
2510 void VM_clientcount(void)
2512 VM_SAFEPARMCOUNT(0,VM_clientcount);
2514 PRVM_G_FLOAT(OFS_RETURN) = svs.maxclients;
2524 void VM_clientstate(void)
2526 VM_SAFEPARMCOUNT(0,VM_clientstate);
2529 switch( cls.state ) {
2530 case ca_uninitialized:
2532 PRVM_G_FLOAT(OFS_RETURN) = 0;
2534 case ca_disconnected:
2535 PRVM_G_FLOAT(OFS_RETURN) = 1;
2538 PRVM_G_FLOAT(OFS_RETURN) = 2;
2541 // should never be reached!
2550 float getostype(void)
2552 */ // not used at the moment -> not included in the common list
2553 void VM_getostype(void)
2555 VM_SAFEPARMCOUNT(0,VM_getostype);
2560 OS_MAC - not supported
2564 PRVM_G_FLOAT(OFS_RETURN) = 0;
2565 #elif defined(MACOSX)
2566 PRVM_G_FLOAT(OFS_RETURN) = 2;
2568 PRVM_G_FLOAT(OFS_RETURN) = 1;
2579 void VM_gettime(void)
2581 VM_SAFEPARMCOUNT(0,VM_gettime);
2583 PRVM_G_FLOAT(OFS_RETURN) = (float) realtime;
2590 loadfromdata(string data)
2593 void VM_loadfromdata(void)
2595 VM_SAFEPARMCOUNT(1,VM_loadentsfromfile);
2597 PRVM_ED_LoadFromFile(PRVM_G_STRING(OFS_PARM0));
2601 ========================
2604 parseentitydata(entity ent, string data)
2605 ========================
2607 void VM_parseentitydata(void)
2612 VM_SAFEPARMCOUNT(2, VM_parseentitydata);
2614 // get edict and test it
2615 ent = PRVM_G_EDICT(OFS_PARM0);
2616 if (ent->priv.required->free)
2617 PRVM_ERROR ("VM_parseentitydata: %s: Can only set already spawned entities (entity %i is free)!", PRVM_NAME, PRVM_NUM_FOR_EDICT(ent));
2619 data = PRVM_G_STRING(OFS_PARM1);
2621 // parse the opening brace
2622 if (!COM_ParseToken_Simple(&data, false, false) || com_token[0] != '{' )
2623 PRVM_ERROR ("VM_parseentitydata: %s: Couldn't parse entity data:\n%s", PRVM_NAME, data );
2625 PRVM_ED_ParseEdict (data, ent);
2632 loadfromfile(string file)
2635 void VM_loadfromfile(void)
2637 const char *filename;
2640 VM_SAFEPARMCOUNT(1,VM_loadfromfile);
2642 filename = PRVM_G_STRING(OFS_PARM0);
2643 if (FS_CheckNastyPath(filename, false))
2645 PRVM_G_FLOAT(OFS_RETURN) = -4;
2646 VM_Warning("VM_loadfromfile: %s dangerous or non-portable filename \"%s\" not allowed. (contains : or \\ or begins with .. or /)\n", PRVM_NAME, filename);
2650 // not conform with VM_fopen
2651 data = (char *)FS_LoadFile(filename, tempmempool, false, NULL);
2653 PRVM_G_FLOAT(OFS_RETURN) = -1;
2655 PRVM_ED_LoadFromFile(data);
2666 float mod(float val, float m)
2669 void VM_modulo(void)
2672 VM_SAFEPARMCOUNT(2,VM_module);
2674 val = (int) PRVM_G_FLOAT(OFS_PARM0);
2675 m = (int) PRVM_G_FLOAT(OFS_PARM1);
2677 PRVM_G_FLOAT(OFS_RETURN) = (float) (val % m);
2680 void VM_Search_Init(void)
2683 for (i = 0;i < PRVM_MAX_OPENSEARCHES;i++)
2684 prog->opensearches[i] = NULL;
2687 void VM_Search_Reset(void)
2690 // reset the fssearch list
2691 for(i = 0; i < PRVM_MAX_OPENSEARCHES; i++)
2693 if(prog->opensearches[i])
2694 FS_FreeSearch(prog->opensearches[i]);
2695 prog->opensearches[i] = NULL;
2703 float search_begin(string pattern, float caseinsensitive, float quiet)
2706 void VM_search_begin(void)
2709 const char *pattern;
2710 int caseinsens, quiet;
2712 VM_SAFEPARMCOUNT(3, VM_search_begin);
2714 pattern = PRVM_G_STRING(OFS_PARM0);
2716 VM_CheckEmptyString(pattern);
2718 caseinsens = (int)PRVM_G_FLOAT(OFS_PARM1);
2719 quiet = (int)PRVM_G_FLOAT(OFS_PARM2);
2721 for(handle = 0; handle < PRVM_MAX_OPENSEARCHES; handle++)
2722 if(!prog->opensearches[handle])
2725 if(handle >= PRVM_MAX_OPENSEARCHES)
2727 PRVM_G_FLOAT(OFS_RETURN) = -2;
2728 VM_Warning("VM_search_begin: %s ran out of search handles (%i)\n", PRVM_NAME, PRVM_MAX_OPENSEARCHES);
2732 if(!(prog->opensearches[handle] = FS_Search(pattern,caseinsens, quiet)))
2733 PRVM_G_FLOAT(OFS_RETURN) = -1;
2736 prog->opensearches_origin[handle] = PRVM_AllocationOrigin();
2737 PRVM_G_FLOAT(OFS_RETURN) = handle;
2745 void search_end(float handle)
2748 void VM_search_end(void)
2751 VM_SAFEPARMCOUNT(1, VM_search_end);
2753 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
2755 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
2757 VM_Warning("VM_search_end: invalid handle %i used in %s\n", handle, PRVM_NAME);
2760 if(prog->opensearches[handle] == NULL)
2762 VM_Warning("VM_search_end: no such handle %i in %s\n", handle, PRVM_NAME);
2766 FS_FreeSearch(prog->opensearches[handle]);
2767 prog->opensearches[handle] = NULL;
2768 if(prog->opensearches_origin[handle])
2769 PRVM_Free((char *)prog->opensearches_origin[handle]);
2776 float search_getsize(float handle)
2779 void VM_search_getsize(void)
2782 VM_SAFEPARMCOUNT(1, VM_M_search_getsize);
2784 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
2786 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
2788 VM_Warning("VM_search_getsize: invalid handle %i used in %s\n", handle, PRVM_NAME);
2791 if(prog->opensearches[handle] == NULL)
2793 VM_Warning("VM_search_getsize: no such handle %i in %s\n", handle, PRVM_NAME);
2797 PRVM_G_FLOAT(OFS_RETURN) = prog->opensearches[handle]->numfilenames;
2802 VM_search_getfilename
2804 string search_getfilename(float handle, float num)
2807 void VM_search_getfilename(void)
2809 int handle, filenum;
2810 VM_SAFEPARMCOUNT(2, VM_search_getfilename);
2812 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
2813 filenum = (int)PRVM_G_FLOAT(OFS_PARM1);
2815 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
2817 VM_Warning("VM_search_getfilename: invalid handle %i used in %s\n", handle, PRVM_NAME);
2820 if(prog->opensearches[handle] == NULL)
2822 VM_Warning("VM_search_getfilename: no such handle %i in %s\n", handle, PRVM_NAME);
2825 if(filenum < 0 || filenum >= prog->opensearches[handle]->numfilenames)
2827 VM_Warning("VM_search_getfilename: invalid filenum %i in %s\n", filenum, PRVM_NAME);
2831 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog->opensearches[handle]->filenames[filenum]);
2838 string chr(float ascii)
2844 VM_SAFEPARMCOUNT(1, VM_chr);
2846 tmp[0] = (unsigned char) PRVM_G_FLOAT(OFS_PARM0);
2849 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(tmp);
2852 //=============================================================================
2853 // Draw builtins (client & menu)
2859 float iscachedpic(string pic)
2862 void VM_iscachedpic(void)
2864 VM_SAFEPARMCOUNT(1,VM_iscachedpic);
2866 // drawq hasnt such a function, thus always return true
2867 PRVM_G_FLOAT(OFS_RETURN) = false;
2874 string precache_pic(string pic)
2877 void VM_precache_pic(void)
2881 VM_SAFEPARMCOUNT(1, VM_precache_pic);
2883 s = PRVM_G_STRING(OFS_PARM0);
2884 PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
2885 VM_CheckEmptyString (s);
2887 // AK Draw_CachePic is supposed to always return a valid pointer
2888 if( Draw_CachePic_Flags(s, CACHEPICFLAG_NOTPERSISTENT)->tex == r_texture_notexture )
2889 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
2899 void VM_freepic(void)
2903 VM_SAFEPARMCOUNT(1,VM_freepic);
2905 s = PRVM_G_STRING(OFS_PARM0);
2906 VM_CheckEmptyString (s);
2911 dp_font_t *getdrawfont()
2913 if(prog->globaloffsets.drawfont >= 0)
2915 int f = (int) PRVM_G_FLOAT(prog->globaloffsets.drawfont);
2916 if(f < 0 || f >= MAX_FONTS)
2917 return FONT_DEFAULT;
2918 return &dp_fonts[f];
2921 return FONT_DEFAULT;
2928 float drawcharacter(vector position, float character, vector scale, vector rgb, float alpha, float flag)
2931 void VM_drawcharacter(void)
2933 float *pos,*scale,*rgb;
2936 VM_SAFEPARMCOUNT(6,VM_drawcharacter);
2938 character = (char) PRVM_G_FLOAT(OFS_PARM1);
2941 PRVM_G_FLOAT(OFS_RETURN) = -1;
2942 VM_Warning("VM_drawcharacter: %s passed null character !\n",PRVM_NAME);
2946 pos = PRVM_G_VECTOR(OFS_PARM0);
2947 scale = PRVM_G_VECTOR(OFS_PARM2);
2948 rgb = PRVM_G_VECTOR(OFS_PARM3);
2949 flag = (int)PRVM_G_FLOAT(OFS_PARM5);
2951 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2953 PRVM_G_FLOAT(OFS_RETURN) = -2;
2954 VM_Warning("VM_drawcharacter: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2958 if(pos[2] || scale[2])
2959 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")));
2961 if(!scale[0] || !scale[1])
2963 PRVM_G_FLOAT(OFS_RETURN) = -3;
2964 VM_Warning("VM_drawcharacter: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
2968 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());
2969 PRVM_G_FLOAT(OFS_RETURN) = 1;
2976 float drawstring(vector position, string text, vector scale, vector rgb, float alpha, float flag)
2979 void VM_drawstring(void)
2981 float *pos,*scale,*rgb;
2984 VM_SAFEPARMCOUNT(6,VM_drawstring);
2986 string = PRVM_G_STRING(OFS_PARM1);
2987 pos = PRVM_G_VECTOR(OFS_PARM0);
2988 scale = PRVM_G_VECTOR(OFS_PARM2);
2989 rgb = PRVM_G_VECTOR(OFS_PARM3);
2990 flag = (int)PRVM_G_FLOAT(OFS_PARM5);
2992 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2994 PRVM_G_FLOAT(OFS_RETURN) = -2;
2995 VM_Warning("VM_drawstring: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2999 if(!scale[0] || !scale[1])
3001 PRVM_G_FLOAT(OFS_RETURN) = -3;
3002 VM_Warning("VM_drawstring: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
3006 if(pos[2] || scale[2])
3007 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")));
3009 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());
3010 PRVM_G_FLOAT(OFS_RETURN) = 1;
3015 VM_drawcolorcodedstring
3017 float drawcolorcodedstring(vector position, string text, vector scale, float alpha, float flag)
3020 void VM_drawcolorcodedstring(void)
3025 VM_SAFEPARMCOUNT(5,VM_drawstring);
3027 string = PRVM_G_STRING(OFS_PARM1);
3028 pos = PRVM_G_VECTOR(OFS_PARM0);
3029 scale = PRVM_G_VECTOR(OFS_PARM2);
3030 flag = (int)PRVM_G_FLOAT(OFS_PARM4);
3032 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3034 PRVM_G_FLOAT(OFS_RETURN) = -2;
3035 VM_Warning("VM_drawcolorcodedstring: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
3039 if(!scale[0] || !scale[1])
3041 PRVM_G_FLOAT(OFS_RETURN) = -3;
3042 VM_Warning("VM_drawcolorcodedstring: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
3046 if(pos[2] || scale[2])
3047 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")));
3050 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());
3051 PRVM_G_FLOAT(OFS_RETURN) = 1;
3057 float stringwidth(string text, float allowColorCodes)
3060 void VM_stringwidth(void)
3064 VM_SAFEPARMCOUNT(2,VM_drawstring);
3066 string = PRVM_G_STRING(OFS_PARM0);
3067 colors = (int)PRVM_G_FLOAT(OFS_PARM1);
3069 PRVM_G_FLOAT(OFS_RETURN) = DrawQ_TextWidth_Font(string, 0, !colors, getdrawfont()); // 1x1 characters, don't actually draw
3075 float drawpic(vector position, string pic, vector size, vector rgb, float alpha, float flag)
3078 void VM_drawpic(void)
3080 const char *picname;
3081 float *size, *pos, *rgb;
3084 VM_SAFEPARMCOUNT(6,VM_drawpic);
3086 picname = PRVM_G_STRING(OFS_PARM1);
3087 VM_CheckEmptyString (picname);
3089 // is pic cached ? no function yet for that
3092 PRVM_G_FLOAT(OFS_RETURN) = -4;
3093 VM_Warning("VM_drawpic: %s: %s not cached !\n", PRVM_NAME, picname);
3097 pos = PRVM_G_VECTOR(OFS_PARM0);
3098 size = PRVM_G_VECTOR(OFS_PARM2);
3099 rgb = PRVM_G_VECTOR(OFS_PARM3);
3100 flag = (int) PRVM_G_FLOAT(OFS_PARM5);
3102 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3104 PRVM_G_FLOAT(OFS_RETURN) = -2;
3105 VM_Warning("VM_drawpic: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
3109 if(pos[2] || size[2])
3110 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")));
3112 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);
3113 PRVM_G_FLOAT(OFS_RETURN) = 1;
3119 float drawsubpic(vector position, vector size, string pic, vector srcPos, vector srcSize, vector rgb, float alpha, float flag)
3123 void VM_drawsubpic(void)
3125 const char *picname;
3126 float *size, *pos, *rgb, *srcPos, *srcSize, alpha;
3129 VM_SAFEPARMCOUNT(8,VM_drawsubpic);
3131 picname = PRVM_G_STRING(OFS_PARM2);
3132 VM_CheckEmptyString (picname);
3134 // is pic cached ? no function yet for that
3137 PRVM_G_FLOAT(OFS_RETURN) = -4;
3138 VM_Warning("VM_drawsubpic: %s: %s not cached !\n", PRVM_NAME, picname);
3142 pos = PRVM_G_VECTOR(OFS_PARM0);
3143 size = PRVM_G_VECTOR(OFS_PARM1);
3144 srcPos = PRVM_G_VECTOR(OFS_PARM3);
3145 srcSize = PRVM_G_VECTOR(OFS_PARM4);
3146 rgb = PRVM_G_VECTOR(OFS_PARM5);
3147 alpha = PRVM_G_FLOAT(OFS_PARM6);
3148 flag = (int) PRVM_G_FLOAT(OFS_PARM7);
3150 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3152 PRVM_G_FLOAT(OFS_RETURN) = -2;
3153 VM_Warning("VM_drawsubpic: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
3157 if(pos[2] || size[2])
3158 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")));
3160 DrawQ_SuperPic(pos[0], pos[1], Draw_CachePic (picname),
3162 srcPos[0], srcPos[1], rgb[0], rgb[1], rgb[2], alpha,
3163 srcPos[0] + srcSize[0], srcPos[1], rgb[0], rgb[1], rgb[2], alpha,
3164 srcPos[0], srcPos[1] + srcSize[1], rgb[0], rgb[1], rgb[2], alpha,
3165 srcPos[0] + srcSize[0], srcPos[1] + srcSize[1], rgb[0], rgb[1], rgb[2], alpha,
3167 PRVM_G_FLOAT(OFS_RETURN) = 1;
3174 float drawfill(vector position, vector size, vector rgb, float alpha, float flag)
3177 void VM_drawfill(void)
3179 float *size, *pos, *rgb;
3182 VM_SAFEPARMCOUNT(5,VM_drawfill);
3185 pos = PRVM_G_VECTOR(OFS_PARM0);
3186 size = PRVM_G_VECTOR(OFS_PARM1);
3187 rgb = PRVM_G_VECTOR(OFS_PARM2);
3188 flag = (int) PRVM_G_FLOAT(OFS_PARM4);
3190 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3192 PRVM_G_FLOAT(OFS_RETURN) = -2;
3193 VM_Warning("VM_drawfill: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
3197 if(pos[2] || size[2])
3198 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")));
3200 DrawQ_Fill(pos[0], pos[1], size[0], size[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM3), flag);
3201 PRVM_G_FLOAT(OFS_RETURN) = 1;
3208 drawsetcliparea(float x, float y, float width, float height)
3211 void VM_drawsetcliparea(void)
3214 VM_SAFEPARMCOUNT(4,VM_drawsetcliparea);
3216 x = bound(0, PRVM_G_FLOAT(OFS_PARM0), vid_conwidth.integer);
3217 y = bound(0, PRVM_G_FLOAT(OFS_PARM1), vid_conheight.integer);
3218 w = bound(0, PRVM_G_FLOAT(OFS_PARM2) + PRVM_G_FLOAT(OFS_PARM0) - x, (vid_conwidth.integer - x));
3219 h = bound(0, PRVM_G_FLOAT(OFS_PARM3) + PRVM_G_FLOAT(OFS_PARM1) - y, (vid_conheight.integer - y));
3221 DrawQ_SetClipArea(x, y, w, h);
3226 VM_drawresetcliparea
3231 void VM_drawresetcliparea(void)
3233 VM_SAFEPARMCOUNT(0,VM_drawresetcliparea);
3235 DrawQ_ResetClipArea();
3242 vector getimagesize(string pic)
3245 void VM_getimagesize(void)
3250 VM_SAFEPARMCOUNT(1,VM_getimagesize);
3252 p = PRVM_G_STRING(OFS_PARM0);
3253 VM_CheckEmptyString (p);
3255 pic = Draw_CachePic_Flags (p, CACHEPICFLAG_NOTPERSISTENT);
3257 PRVM_G_VECTOR(OFS_RETURN)[0] = pic->width;
3258 PRVM_G_VECTOR(OFS_RETURN)[1] = pic->height;
3259 PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
3266 string keynumtostring(float keynum)
3269 void VM_keynumtostring (void)
3271 VM_SAFEPARMCOUNT(1, VM_keynumtostring);
3273 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(Key_KeynumToString((int)PRVM_G_FLOAT(OFS_PARM0)));
3278 VM_findkeysforcommand
3280 string findkeysforcommand(string command)
3282 the returned string is an altstring
3285 #define NUMKEYS 5 // TODO: merge the constant in keys.c with this one somewhen
3287 void M_FindKeysForCommand(const char *command, int *keys);
3288 void VM_findkeysforcommand(void)
3291 char ret[VM_STRINGTEMP_LENGTH];
3295 VM_SAFEPARMCOUNT(1, VM_findkeysforcommand);
3297 cmd = PRVM_G_STRING(OFS_PARM0);
3299 VM_CheckEmptyString(cmd);
3301 M_FindKeysForCommand(cmd, keys);
3304 for(i = 0; i < NUMKEYS; i++)
3305 strlcat(ret, va(" \'%i\'", keys[i]), sizeof(ret));
3307 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(ret);
3314 float stringtokeynum(string key)
3317 void VM_stringtokeynum (void)
3319 VM_SAFEPARMCOUNT( 1, VM_keynumtostring );
3321 PRVM_G_INT(OFS_RETURN) = Key_StringToKeynum(PRVM_G_STRING(OFS_PARM0));
3324 // CL_Video interface functions
3327 ========================
3330 float cin_open(string file, string name)
3331 ========================
3333 void VM_cin_open( void )
3338 VM_SAFEPARMCOUNT( 2, VM_cin_open );
3340 file = PRVM_G_STRING( OFS_PARM0 );
3341 name = PRVM_G_STRING( OFS_PARM1 );
3343 VM_CheckEmptyString( file );
3344 VM_CheckEmptyString( name );
3346 if( CL_OpenVideo( file, name, MENUOWNER ) )
3347 PRVM_G_FLOAT( OFS_RETURN ) = 1;
3349 PRVM_G_FLOAT( OFS_RETURN ) = 0;
3353 ========================
3356 void cin_close(string name)
3357 ========================
3359 void VM_cin_close( void )
3363 VM_SAFEPARMCOUNT( 1, VM_cin_close );
3365 name = PRVM_G_STRING( OFS_PARM0 );
3366 VM_CheckEmptyString( name );
3368 CL_CloseVideo( CL_GetVideoByName( name ) );
3372 ========================
3374 void cin_setstate(string name, float type)
3375 ========================
3377 void VM_cin_setstate( void )
3380 clvideostate_t state;
3383 VM_SAFEPARMCOUNT( 2, VM_cin_netstate );
3385 name = PRVM_G_STRING( OFS_PARM0 );
3386 VM_CheckEmptyString( name );
3388 state = (clvideostate_t)((int)PRVM_G_FLOAT( OFS_PARM1 ));
3390 video = CL_GetVideoByName( name );
3391 if( video && state > CLVIDEO_UNUSED && state < CLVIDEO_STATECOUNT )
3392 CL_SetVideoState( video, state );
3396 ========================
3399 float cin_getstate(string name)
3400 ========================
3402 void VM_cin_getstate( void )
3407 VM_SAFEPARMCOUNT( 1, VM_cin_getstate );
3409 name = PRVM_G_STRING( OFS_PARM0 );
3410 VM_CheckEmptyString( name );
3412 video = CL_GetVideoByName( name );
3414 PRVM_G_FLOAT( OFS_RETURN ) = (int)video->state;
3416 PRVM_G_FLOAT( OFS_RETURN ) = 0;
3420 ========================
3423 void cin_restart(string name)
3424 ========================
3426 void VM_cin_restart( void )
3431 VM_SAFEPARMCOUNT( 1, VM_cin_restart );
3433 name = PRVM_G_STRING( OFS_PARM0 );
3434 VM_CheckEmptyString( name );
3436 video = CL_GetVideoByName( name );
3438 CL_RestartVideo( video );
3442 ========================
3444 ========================
3446 void VM_Gecko_Init( void ) {
3447 // the prog struct is memset to 0 by Initprog? [12/6/2007 Black]
3448 // FIXME: remove the other _Init functions then, too? [12/6/2007 Black]
3452 ========================
3454 ========================
3456 void VM_Gecko_Destroy( void ) {
3458 for( i = 0 ; i < PRVM_MAX_GECKOINSTANCES ; i++ ) {
3459 clgecko_t **instance = &prog->opengeckoinstances[ i ];
3461 CL_Gecko_DestroyBrowser( *instance );
3468 ========================
3471 float[bool] gecko_create( string name )
3472 ========================
3474 void VM_gecko_create( void ) {
3477 clgecko_t *instance;
3479 VM_SAFEPARMCOUNT( 1, VM_gecko_create );
3481 name = PRVM_G_STRING( OFS_PARM0 );
3482 VM_CheckEmptyString( name );
3484 // find an empty slot for this gecko browser..
3485 for( i = 0 ; i < PRVM_MAX_GECKOINSTANCES ; i++ ) {
3486 if( prog->opengeckoinstances[ i ] == NULL ) {
3490 if( i == PRVM_MAX_GECKOINSTANCES ) {
3491 VM_Warning("VM_gecko_create: %s ran out of gecko handles (%i)\n", PRVM_NAME, PRVM_MAX_GECKOINSTANCES);
3492 PRVM_G_FLOAT( OFS_RETURN ) = 0;
3496 instance = prog->opengeckoinstances[ i ] = CL_Gecko_CreateBrowser( name, PRVM_GetProgNr() );
3498 // TODO: error handling [12/3/2007 Black]
3499 PRVM_G_FLOAT( OFS_RETURN ) = 0;
3502 PRVM_G_FLOAT( OFS_RETURN ) = 1;
3506 ========================
3509 void gecko_destroy( string name )
3510 ========================
3512 void VM_gecko_destroy( void ) {
3514 clgecko_t *instance;
3516 VM_SAFEPARMCOUNT( 1, VM_gecko_destroy );
3518 name = PRVM_G_STRING( OFS_PARM0 );
3519 VM_CheckEmptyString( name );
3520 instance = CL_Gecko_FindBrowser( name );
3524 CL_Gecko_DestroyBrowser( instance );
3528 ========================
3531 void gecko_navigate( string name, string URI )
3532 ========================
3534 void VM_gecko_navigate( void ) {
3537 clgecko_t *instance;
3539 VM_SAFEPARMCOUNT( 2, VM_gecko_navigate );
3541 name = PRVM_G_STRING( OFS_PARM0 );
3542 URI = PRVM_G_STRING( OFS_PARM1 );
3543 VM_CheckEmptyString( name );
3544 VM_CheckEmptyString( URI );
3546 instance = CL_Gecko_FindBrowser( name );
3550 CL_Gecko_NavigateToURI( instance, URI );
3554 ========================
3557 float[bool] gecko_keyevent( string name, float key, float eventtype )
3558 ========================
3560 void VM_gecko_keyevent( void ) {
3563 clgecko_buttoneventtype_t eventtype;
3564 clgecko_t *instance;
3566 VM_SAFEPARMCOUNT( 3, VM_gecko_keyevent );
3568 name = PRVM_G_STRING( OFS_PARM0 );
3569 VM_CheckEmptyString( name );
3570 key = (unsigned int) PRVM_G_FLOAT( OFS_PARM1 );
3571 switch( (unsigned int) PRVM_G_FLOAT( OFS_PARM2 ) ) {
3573 eventtype = CLG_BET_DOWN;
3576 eventtype = CLG_BET_UP;
3579 eventtype = CLG_BET_PRESS;
3582 eventtype = CLG_BET_DOUBLECLICK;
3585 // TODO: console printf? [12/3/2007 Black]
3586 PRVM_G_FLOAT( OFS_RETURN ) = 0;
3590 instance = CL_Gecko_FindBrowser( name );
3592 PRVM_G_FLOAT( OFS_RETURN ) = 0;
3596 PRVM_G_FLOAT( OFS_RETURN ) = (CL_Gecko_Event_Key( instance, (keynum_t) key, eventtype ) == true);
3600 ========================
3603 void gecko_mousemove( string name, float x, float y )
3604 ========================
3606 void VM_gecko_movemouse( void ) {
3609 clgecko_t *instance;
3611 VM_SAFEPARMCOUNT( 3, VM_gecko_movemouse );
3613 name = PRVM_G_STRING( OFS_PARM0 );
3614 VM_CheckEmptyString( name );
3615 x = PRVM_G_FLOAT( OFS_PARM1 );
3616 y = PRVM_G_FLOAT( OFS_PARM2 );
3618 instance = CL_Gecko_FindBrowser( name );
3622 CL_Gecko_Event_CursorMove( instance, x, y );
3627 ========================
3630 void gecko_resize( string name, float w, float h )
3631 ========================
3633 void VM_gecko_resize( void ) {
3636 clgecko_t *instance;
3638 VM_SAFEPARMCOUNT( 3, VM_gecko_movemouse );
3640 name = PRVM_G_STRING( OFS_PARM0 );
3641 VM_CheckEmptyString( name );
3642 w = PRVM_G_FLOAT( OFS_PARM1 );
3643 h = PRVM_G_FLOAT( OFS_PARM2 );
3645 instance = CL_Gecko_FindBrowser( name );
3649 CL_Gecko_Resize( instance, (int) w, (int) h );
3654 ========================
3655 VM_gecko_get_texture_extent
3657 vector gecko_get_texture_extent( string name )
3658 ========================
3660 void VM_gecko_get_texture_extent( void ) {
3662 clgecko_t *instance;
3664 VM_SAFEPARMCOUNT( 1, VM_gecko_movemouse );
3666 name = PRVM_G_STRING( OFS_PARM0 );
3667 VM_CheckEmptyString( name );
3669 PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
3670 instance = CL_Gecko_FindBrowser( name );
3672 PRVM_G_VECTOR(OFS_RETURN)[0] = 0;
3673 PRVM_G_VECTOR(OFS_RETURN)[1] = 0;
3676 CL_Gecko_GetTextureExtent( instance,
3677 PRVM_G_VECTOR(OFS_RETURN), PRVM_G_VECTOR(OFS_RETURN)+1 );
3686 Writes new values for v_forward, v_up, and v_right based on angles
3687 void makevectors(vector angle)
3690 void VM_makevectors (void)
3692 prvm_eval_t *valforward, *valright, *valup;
3693 valforward = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_forward);
3694 valright = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_right);
3695 valup = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_up);
3696 if (!valforward || !valright || !valup)
3698 VM_Warning("makevectors: could not find v_forward, v_right, or v_up global variables\n");
3701 VM_SAFEPARMCOUNT(1, VM_makevectors);
3702 AngleVectors (PRVM_G_VECTOR(OFS_PARM0), valforward->vector, valright->vector, valup->vector);
3709 Writes new values for v_forward, v_up, and v_right based on the given forward vector
3710 vectorvectors(vector)
3713 void VM_vectorvectors (void)
3715 prvm_eval_t *valforward, *valright, *valup;
3716 valforward = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_forward);
3717 valright = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_right);
3718 valup = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_up);
3719 if (!valforward || !valright || !valup)
3721 VM_Warning("vectorvectors: could not find v_forward, v_right, or v_up global variables\n");
3724 VM_SAFEPARMCOUNT(1, VM_vectorvectors);
3725 VectorNormalize2(PRVM_G_VECTOR(OFS_PARM0), valforward->vector);
3726 VectorVectors(valforward->vector, valright->vector, valup->vector);
3730 ========================
3733 void drawline(float width, vector pos1, vector pos2, vector rgb, float alpha, float flags)
3734 ========================
3736 void VM_drawline (void)
3738 float *c1, *c2, *rgb;
3740 unsigned char flags;
3742 VM_SAFEPARMCOUNT(6, VM_drawline);
3743 width = PRVM_G_FLOAT(OFS_PARM0);
3744 c1 = PRVM_G_VECTOR(OFS_PARM1);
3745 c2 = PRVM_G_VECTOR(OFS_PARM2);
3746 rgb = PRVM_G_VECTOR(OFS_PARM3);
3747 alpha = PRVM_G_FLOAT(OFS_PARM4);
3748 flags = (int)PRVM_G_FLOAT(OFS_PARM5);
3749 DrawQ_Line(width, c1[0], c1[1], c2[0], c2[1], rgb[0], rgb[1], rgb[2], alpha, flags);
3752 // float(float number, float quantity) bitshift (EXT_BITSHIFT)
3753 void VM_bitshift (void)
3756 VM_SAFEPARMCOUNT(2, VM_bitshift);
3758 n1 = (int)fabs((int)PRVM_G_FLOAT(OFS_PARM0));
3759 n2 = (int)PRVM_G_FLOAT(OFS_PARM1);
3761 PRVM_G_FLOAT(OFS_RETURN) = n1;
3764 PRVM_G_FLOAT(OFS_RETURN) = (n1 >> -n2);
3766 PRVM_G_FLOAT(OFS_RETURN) = (n1 << n2);
3769 ////////////////////////////////////////
3770 // AltString functions
3771 ////////////////////////////////////////
3774 ========================
3777 float altstr_count(string)
3778 ========================
3780 void VM_altstr_count( void )
3782 const char *altstr, *pos;
3785 VM_SAFEPARMCOUNT( 1, VM_altstr_count );
3787 altstr = PRVM_G_STRING( OFS_PARM0 );
3788 //VM_CheckEmptyString( altstr );
3790 for( count = 0, pos = altstr ; *pos ; pos++ ) {
3791 if( *pos == '\\' ) {
3795 } else if( *pos == '\'' ) {
3800 PRVM_G_FLOAT( OFS_RETURN ) = (float) (count / 2);
3804 ========================
3807 string altstr_prepare(string)
3808 ========================
3810 void VM_altstr_prepare( void )
3813 const char *instr, *in;
3815 char outstr[VM_STRINGTEMP_LENGTH];
3817 VM_SAFEPARMCOUNT( 1, VM_altstr_prepare );
3819 instr = PRVM_G_STRING( OFS_PARM0 );
3821 for( out = outstr, in = instr, size = sizeof(outstr) - 1 ; size && *in ; size--, in++, out++ )
3830 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
3834 ========================
3837 string altstr_get(string, float)
3838 ========================
3840 void VM_altstr_get( void )
3842 const char *altstr, *pos;
3845 char outstr[VM_STRINGTEMP_LENGTH];
3847 VM_SAFEPARMCOUNT( 2, VM_altstr_get );
3849 altstr = PRVM_G_STRING( OFS_PARM0 );
3851 count = (int)PRVM_G_FLOAT( OFS_PARM1 );
3852 count = count * 2 + 1;
3854 for( pos = altstr ; *pos && count ; pos++ )
3855 if( *pos == '\\' ) {
3858 } else if( *pos == '\'' )
3862 PRVM_G_INT( OFS_RETURN ) = 0;
3866 for( out = outstr, size = sizeof(outstr) - 1 ; size && *pos ; size--, pos++, out++ )
3867 if( *pos == '\\' ) {
3872 } else if( *pos == '\'' )
3878 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
3882 ========================
3885 string altstr_set(string altstr, float num, string set)
3886 ========================
3888 void VM_altstr_set( void )
3891 const char *altstr, *str;
3894 char outstr[VM_STRINGTEMP_LENGTH];
3896 VM_SAFEPARMCOUNT( 3, VM_altstr_set );
3898 altstr = PRVM_G_STRING( OFS_PARM0 );
3900 num = (int)PRVM_G_FLOAT( OFS_PARM1 );
3902 str = PRVM_G_STRING( OFS_PARM2 );
3905 for( num = num * 2 + 1, in = altstr; *in && num; *out++ = *in++ )
3910 } else if( *in == '\'' ) {
3915 for( ; *str; *out++ = *str++ );
3916 // now jump over the old content
3918 if( *in == '\'' || (*in == '\\' && !*++in) )
3921 strlcpy(out, in, outstr + sizeof(outstr) - out);
3922 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
3926 ========================
3929 string altstr_ins(string altstr, float num, string set)
3930 ========================
3932 void VM_altstr_ins(void)
3940 char outstr[VM_STRINGTEMP_LENGTH];
3942 VM_SAFEPARMCOUNT(3, VM_altstr_ins);
3944 in = instr = PRVM_G_STRING( OFS_PARM0 );
3945 num = (int)PRVM_G_FLOAT( OFS_PARM1 );
3946 set = setstr = PRVM_G_STRING( OFS_PARM2 );
3949 for( num = num * 2 + 2 ; *in && num > 0 ; *out++ = *in++ )
3954 } else if( *in == '\'' ) {
3959 for( ; *set ; *out++ = *set++ );
3962 strlcpy(out, in, outstr + sizeof(outstr) - out);
3963 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
3967 ////////////////////////////////////////
3968 // BufString functions
3969 ////////////////////////////////////////
3970 //[515]: string buffers support
3972 static size_t stringbuffers_sortlength;
3974 static void BufStr_Expand(prvm_stringbuffer_t *stringbuffer, int strindex)
3976 if (stringbuffer->max_strings <= strindex)
3978 char **oldstrings = stringbuffer->strings;
3979 stringbuffer->max_strings = max(stringbuffer->max_strings * 2, 128);
3980 while (stringbuffer->max_strings <= strindex)
3981 stringbuffer->max_strings *= 2;
3982 stringbuffer->strings = (char **) Mem_Alloc(prog->progs_mempool, stringbuffer->max_strings * sizeof(stringbuffer->strings[0]));
3983 if (stringbuffer->num_strings > 0)
3984 memcpy(stringbuffer->strings, oldstrings, stringbuffer->num_strings * sizeof(stringbuffer->strings[0]));
3986 Mem_Free(oldstrings);
3990 static void BufStr_Shrink(prvm_stringbuffer_t *stringbuffer)
3992 // reduce num_strings if there are empty string slots at the end
3993 while (stringbuffer->num_strings > 0 && stringbuffer->strings[stringbuffer->num_strings - 1] == NULL)
3994 stringbuffer->num_strings--;
3996 // if empty, free the string pointer array
3997 if (stringbuffer->num_strings == 0)
3999 stringbuffer->max_strings = 0;
4000 if (stringbuffer->strings)
4001 Mem_Free(stringbuffer->strings);
4002 stringbuffer->strings = NULL;
4006 static int BufStr_SortStringsUP (const void *in1, const void *in2)
4009 a = *((const char **) in1);
4010 b = *((const char **) in2);
4012 if(!b[0]) return -1;
4013 return strncmp(a, b, stringbuffers_sortlength);
4016 static int BufStr_SortStringsDOWN (const void *in1, const void *in2)
4019 a = *((const char **) in1);
4020 b = *((const char **) in2);
4022 if(!b[0]) return -1;
4023 return strncmp(b, a, stringbuffers_sortlength);
4027 ========================
4029 creates new buffer, and returns it's index, returns -1 if failed
4030 float buf_create(void) = #460;
4031 ========================
4033 void VM_buf_create (void)
4035 prvm_stringbuffer_t *stringbuffer;
4037 VM_SAFEPARMCOUNT(0, VM_buf_create);
4038 stringbuffer = (prvm_stringbuffer_t *) Mem_ExpandableArray_AllocRecord(&prog->stringbuffersarray);
4039 for (i = 0;stringbuffer != Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, i);i++);
4040 stringbuffer->origin = PRVM_AllocationOrigin();
4041 PRVM_G_FLOAT(OFS_RETURN) = i;
4045 ========================
4047 deletes buffer and all strings in it
4048 void buf_del(float bufhandle) = #461;
4049 ========================
4051 void VM_buf_del (void)
4053 prvm_stringbuffer_t *stringbuffer;
4054 VM_SAFEPARMCOUNT(1, VM_buf_del);
4055 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4059 for (i = 0;i < stringbuffer->num_strings;i++)
4060 if (stringbuffer->strings[i])
4061 Mem_Free(stringbuffer->strings[i]);
4062 if (stringbuffer->strings)
4063 Mem_Free(stringbuffer->strings);
4064 if(stringbuffer->origin)
4065 PRVM_Free((char *)stringbuffer->origin);
4066 Mem_ExpandableArray_FreeRecord(&prog->stringbuffersarray, stringbuffer);
4070 VM_Warning("VM_buf_del: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4076 ========================
4078 how many strings are stored in buffer
4079 float buf_getsize(float bufhandle) = #462;
4080 ========================
4082 void VM_buf_getsize (void)
4084 prvm_stringbuffer_t *stringbuffer;
4085 VM_SAFEPARMCOUNT(1, VM_buf_getsize);
4087 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4090 PRVM_G_FLOAT(OFS_RETURN) = -1;
4091 VM_Warning("VM_buf_getsize: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4095 PRVM_G_FLOAT(OFS_RETURN) = stringbuffer->num_strings;
4099 ========================
4101 copy all content from one buffer to another, make sure it exists
4102 void buf_copy(float bufhandle_from, float bufhandle_to) = #463;
4103 ========================
4105 void VM_buf_copy (void)
4107 prvm_stringbuffer_t *srcstringbuffer, *dststringbuffer;
4109 VM_SAFEPARMCOUNT(2, VM_buf_copy);
4111 srcstringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4112 if(!srcstringbuffer)
4114 VM_Warning("VM_buf_copy: invalid source buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4117 i = (int)PRVM_G_FLOAT(OFS_PARM1);
4118 if(i == (int)PRVM_G_FLOAT(OFS_PARM0))
4120 VM_Warning("VM_buf_copy: source == destination (%i) in %s\n", i, PRVM_NAME);
4123 dststringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4124 if(!dststringbuffer)
4126 VM_Warning("VM_buf_copy: invalid destination buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM1), PRVM_NAME);
4130 for (i = 0;i < dststringbuffer->num_strings;i++)
4131 if (dststringbuffer->strings[i])
4132 Mem_Free(dststringbuffer->strings[i]);
4133 if (dststringbuffer->strings)
4134 Mem_Free(dststringbuffer->strings);
4135 *dststringbuffer = *srcstringbuffer;
4136 if (dststringbuffer->max_strings)
4137 dststringbuffer->strings = (char **)Mem_Alloc(prog->progs_mempool, sizeof(dststringbuffer->strings[0]) * dststringbuffer->max_strings);
4139 for (i = 0;i < dststringbuffer->num_strings;i++)
4141 if (srcstringbuffer->strings[i])
4144 stringlen = strlen(srcstringbuffer->strings[i]) + 1;
4145 dststringbuffer->strings[i] = (char *)Mem_Alloc(prog->progs_mempool, stringlen);
4146 memcpy(dststringbuffer->strings[i], srcstringbuffer->strings[i], stringlen);
4152 ========================
4154 sort buffer by beginnings of strings (cmplength defaults it's length)
4155 "backward == TRUE" means that sorting goes upside-down
4156 void buf_sort(float bufhandle, float cmplength, float backward) = #464;
4157 ========================
4159 void VM_buf_sort (void)
4161 prvm_stringbuffer_t *stringbuffer;
4162 VM_SAFEPARMCOUNT(3, VM_buf_sort);
4164 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4167 VM_Warning("VM_buf_sort: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4170 if(stringbuffer->num_strings <= 0)
4172 VM_Warning("VM_buf_sort: tried to sort empty buffer %i in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4175 stringbuffers_sortlength = (int)PRVM_G_FLOAT(OFS_PARM1);
4176 if(stringbuffers_sortlength <= 0)
4177 stringbuffers_sortlength = 0x7FFFFFFF;
4179 if(!PRVM_G_FLOAT(OFS_PARM2))
4180 qsort(stringbuffer->strings, stringbuffer->num_strings, sizeof(char*), BufStr_SortStringsUP);
4182 qsort(stringbuffer->strings, stringbuffer->num_strings, sizeof(char*), BufStr_SortStringsDOWN);
4184 BufStr_Shrink(stringbuffer);
4188 ========================
4190 concantenates all buffer string into one with "glue" separator and returns it as tempstring
4191 string buf_implode(float bufhandle, string glue) = #465;
4192 ========================
4194 void VM_buf_implode (void)
4196 prvm_stringbuffer_t *stringbuffer;
4197 char k[VM_STRINGTEMP_LENGTH];
4201 VM_SAFEPARMCOUNT(2, VM_buf_implode);
4203 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4204 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
4207 VM_Warning("VM_buf_implode: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4210 if(!stringbuffer->num_strings)
4212 sep = PRVM_G_STRING(OFS_PARM1);
4214 for(l = i = 0;i < stringbuffer->num_strings;i++)
4216 if(stringbuffer->strings[i])
4218 l += (i > 0 ? strlen(sep) : 0) + strlen(stringbuffer->strings[i]);
4219 if (l >= sizeof(k) - 1)
4221 strlcat(k, sep, sizeof(k));
4222 strlcat(k, stringbuffer->strings[i], sizeof(k));
4225 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(k);
4229 ========================
4231 get a string from buffer, returns tempstring, dont str_unzone it!
4232 string bufstr_get(float bufhandle, float string_index) = #465;
4233 ========================
4235 void VM_bufstr_get (void)
4237 prvm_stringbuffer_t *stringbuffer;
4239 VM_SAFEPARMCOUNT(2, VM_bufstr_get);
4241 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
4242 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4245 VM_Warning("VM_bufstr_get: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4248 strindex = (int)PRVM_G_FLOAT(OFS_PARM1);
4251 VM_Warning("VM_bufstr_get: invalid string index %i used in %s\n", strindex, PRVM_NAME);
4254 if (strindex < stringbuffer->num_strings && stringbuffer->strings[strindex])
4255 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(stringbuffer->strings[strindex]);
4259 ========================
4261 copies a string into selected slot of buffer
4262 void bufstr_set(float bufhandle, float string_index, string str) = #466;
4263 ========================
4265 void VM_bufstr_set (void)
4268 prvm_stringbuffer_t *stringbuffer;
4271 VM_SAFEPARMCOUNT(3, VM_bufstr_set);
4273 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4276 VM_Warning("VM_bufstr_set: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4279 strindex = (int)PRVM_G_FLOAT(OFS_PARM1);
4280 if(strindex < 0 || strindex >= 1000000) // huge number of strings
4282 VM_Warning("VM_bufstr_set: invalid string index %i used in %s\n", strindex, PRVM_NAME);
4286 BufStr_Expand(stringbuffer, strindex);
4287 stringbuffer->num_strings = max(stringbuffer->num_strings, strindex + 1);
4289 if(stringbuffer->strings[strindex])
4290 Mem_Free(stringbuffer->strings[strindex]);
4291 stringbuffer->strings[strindex] = NULL;
4293 news = PRVM_G_STRING(OFS_PARM2);
4294 if (news && news[0])
4296 size_t alloclen = strlen(news) + 1;
4297 stringbuffer->strings[strindex] = (char *)Mem_Alloc(prog->progs_mempool, alloclen);
4298 memcpy(stringbuffer->strings[strindex], news, alloclen);
4301 BufStr_Shrink(stringbuffer);
4305 ========================
4307 adds string to buffer in first free slot and returns its index
4308 "order == TRUE" means that string will be added after last "full" slot
4309 float bufstr_add(float bufhandle, string str, float order) = #467;
4310 ========================
4312 void VM_bufstr_add (void)
4314 int order, strindex;
4315 prvm_stringbuffer_t *stringbuffer;
4319 VM_SAFEPARMCOUNT(3, VM_bufstr_add);
4321 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4322 PRVM_G_FLOAT(OFS_RETURN) = -1;
4325 VM_Warning("VM_bufstr_add: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4328 string = PRVM_G_STRING(OFS_PARM1);
4329 if(!string || !string[0])
4331 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);
4334 order = (int)PRVM_G_FLOAT(OFS_PARM2);
4336 strindex = stringbuffer->num_strings;
4338 for (strindex = 0;strindex < stringbuffer->num_strings;strindex++)
4339 if (stringbuffer->strings[strindex] == NULL)
4342 BufStr_Expand(stringbuffer, strindex);
4344 stringbuffer->num_strings = max(stringbuffer->num_strings, strindex + 1);
4345 alloclen = strlen(string) + 1;
4346 stringbuffer->strings[strindex] = (char *)Mem_Alloc(prog->progs_mempool, alloclen);
4347 memcpy(stringbuffer->strings[strindex], string, alloclen);
4349 PRVM_G_FLOAT(OFS_RETURN) = strindex;
4353 ========================
4355 delete string from buffer
4356 void bufstr_free(float bufhandle, float string_index) = #468;
4357 ========================
4359 void VM_bufstr_free (void)
4362 prvm_stringbuffer_t *stringbuffer;
4363 VM_SAFEPARMCOUNT(2, VM_bufstr_free);
4365 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4368 VM_Warning("VM_bufstr_free: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4371 i = (int)PRVM_G_FLOAT(OFS_PARM1);
4374 VM_Warning("VM_bufstr_free: invalid string index %i used in %s\n", i, PRVM_NAME);
4378 if (i < stringbuffer->num_strings)
4380 if(stringbuffer->strings[i])
4381 Mem_Free(stringbuffer->strings[i]);
4382 stringbuffer->strings[i] = NULL;
4385 BufStr_Shrink(stringbuffer);
4394 void VM_buf_cvarlist(void)
4397 const char *partial, *antipartial;
4398 size_t len, antilen;
4400 qboolean ispattern, antiispattern;
4402 prvm_stringbuffer_t *stringbuffer;
4403 VM_SAFEPARMCOUNTRANGE(2, 3, VM_buf_cvarlist);
4405 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4408 VM_Warning("VM_bufstr_free: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4412 partial = PRVM_G_STRING(OFS_PARM1);
4416 len = strlen(partial);
4419 antipartial = PRVM_G_STRING(OFS_PARM2);
4425 antilen = strlen(antipartial);
4427 for (n = 0;n < stringbuffer->num_strings;n++)
4428 if (stringbuffer->strings[n])
4429 Mem_Free(stringbuffer->strings[n]);
4430 if (stringbuffer->strings)
4431 Mem_Free(stringbuffer->strings);
4432 stringbuffer->strings = NULL;
4434 ispattern = partial && (strchr(partial, '*') || strchr(partial, '?'));
4435 antiispattern = antipartial && (strchr(antipartial, '*') || strchr(antipartial, '?'));
4438 for(cvar = cvar_vars; cvar; cvar = cvar->next)
4440 if(len && (ispattern ? !matchpattern_with_separator(cvar->name, partial, false, "", false) : strncmp(partial, cvar->name, len)))
4443 if(antilen && (antiispattern ? matchpattern_with_separator(cvar->name, antipartial, false, "", false) : !strncmp(antipartial, cvar->name, antilen)))
4449 stringbuffer->max_strings = stringbuffer->num_strings = n;
4450 if (stringbuffer->max_strings)
4451 stringbuffer->strings = (char **)Mem_Alloc(prog->progs_mempool, sizeof(stringbuffer->strings[0]) * stringbuffer->max_strings);
4454 for(cvar = cvar_vars; cvar; cvar = cvar->next)
4456 if(len && (ispattern ? !matchpattern_with_separator(cvar->name, partial, false, "", false) : strncmp(partial, cvar->name, len)))
4459 if(antilen && (antiispattern ? matchpattern_with_separator(cvar->name, antipartial, false, "", false) : !strncmp(antipartial, cvar->name, antilen)))
4462 alloclen = strlen(cvar->name) + 1;
4463 stringbuffer->strings[n] = (char *)Mem_Alloc(prog->progs_mempool, alloclen);
4464 memcpy(stringbuffer->strings[n], cvar->name, alloclen);
4479 This was a major timewaster in progs, so it was converted to C
4482 void VM_changeyaw (void)
4485 float ideal, current, move, speed;
4487 // this is called (VERY HACKISHLY) by SV_MoveToGoal, so it can not use any
4488 // parameters because they are the parameters to SV_MoveToGoal, not this
4489 //VM_SAFEPARMCOUNT(0, VM_changeyaw);
4491 ent = PRVM_PROG_TO_EDICT(PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict);
4492 if (ent == prog->edicts)
4494 VM_Warning("changeyaw: can not modify world entity\n");
4497 if (ent->priv.server->free)
4499 VM_Warning("changeyaw: can not modify free entity\n");
4502 if (prog->fieldoffsets.angles < 0 || prog->fieldoffsets.ideal_yaw < 0 || prog->fieldoffsets.yaw_speed < 0)
4504 VM_Warning("changeyaw: angles, ideal_yaw, or yaw_speed field(s) not found\n");
4507 current = ANGLEMOD(PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.angles)->vector[1]);
4508 ideal = PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.ideal_yaw)->_float;
4509 speed = PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.yaw_speed)->_float;
4511 if (current == ideal)
4513 move = ideal - current;
4514 if (ideal > current)
4535 PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.angles)->vector[1] = ANGLEMOD (current + move);
4543 void VM_changepitch (void)
4546 float ideal, current, move, speed;
4548 VM_SAFEPARMCOUNT(1, VM_changepitch);
4550 ent = PRVM_G_EDICT(OFS_PARM0);
4551 if (ent == prog->edicts)
4553 VM_Warning("changepitch: can not modify world entity\n");
4556 if (ent->priv.server->free)
4558 VM_Warning("changepitch: can not modify free entity\n");
4561 if (prog->fieldoffsets.angles < 0 || prog->fieldoffsets.idealpitch < 0 || prog->fieldoffsets.pitch_speed < 0)
4563 VM_Warning("changepitch: angles, idealpitch, or pitch_speed field(s) not found\n");
4566 current = ANGLEMOD(PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.angles)->vector[0]);
4567 ideal = PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.idealpitch)->_float;
4568 speed = PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.pitch_speed)->_float;
4570 if (current == ideal)
4572 move = ideal - current;
4573 if (ideal > current)
4594 PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.angles)->vector[0] = ANGLEMOD (current + move);
4598 void VM_uncolorstring (void)
4600 char szNewString[VM_STRINGTEMP_LENGTH];
4601 const char *szString;
4604 VM_SAFEPARMCOUNT(1, VM_uncolorstring);
4605 szString = PRVM_G_STRING(OFS_PARM0);
4606 COM_StringDecolorize(szString, 0, szNewString, sizeof(szNewString), TRUE);
4607 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(szNewString);
4611 // #221 float(string str, string sub[, float startpos]) strstrofs (FTE_STRINGS)
4612 //strstr, without generating a new string. Use in conjunction with FRIK_FILE's substring for more similar strstr.
4613 void VM_strstrofs (void)
4615 const char *instr, *match;
4617 VM_SAFEPARMCOUNTRANGE(2, 3, VM_strstrofs);
4618 instr = PRVM_G_STRING(OFS_PARM0);
4619 match = PRVM_G_STRING(OFS_PARM1);
4620 firstofs = (prog->argc > 2)?(int)PRVM_G_FLOAT(OFS_PARM2):0;
4622 if (firstofs && (firstofs < 0 || firstofs > (int)strlen(instr)))
4624 PRVM_G_FLOAT(OFS_RETURN) = -1;
4628 match = strstr(instr+firstofs, match);
4630 PRVM_G_FLOAT(OFS_RETURN) = -1;
4632 PRVM_G_FLOAT(OFS_RETURN) = match - instr;
4635 //#222 string(string s, float index) str2chr (FTE_STRINGS)
4636 void VM_str2chr (void)
4639 VM_SAFEPARMCOUNT(2, VM_str2chr);
4640 s = PRVM_G_STRING(OFS_PARM0);
4641 if((unsigned)PRVM_G_FLOAT(OFS_PARM1) < strlen(s))
4642 PRVM_G_FLOAT(OFS_RETURN) = (unsigned char)s[(unsigned)PRVM_G_FLOAT(OFS_PARM1)];
4644 PRVM_G_FLOAT(OFS_RETURN) = 0;
4647 //#223 string(float c, ...) chr2str (FTE_STRINGS)
4648 void VM_chr2str (void)
4652 VM_SAFEPARMCOUNTRANGE(0, 8, VM_chr2str);
4653 for(i = 0;i < prog->argc && i < (int)sizeof(t) - 1;i++)
4654 t[i] = (unsigned char)PRVM_G_FLOAT(OFS_PARM0+i*3);
4656 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(t);
4659 static int chrconv_number(int i, int base, int conv)
4684 static int chrconv_punct(int i, int base, int conv)
4702 static int chrchar_alpha(int i, int basec, int baset, int convc, int convt, int charnum)
4704 //convert case and colour seperatly...
4721 baset = 128*((charnum&1) == (convt-5));
4737 return i + basec + baset;
4739 // #224 string(float ccase, float calpha, float cnum, string s, ...) strconv (FTE_STRINGS)
4740 //bulk convert a string. change case or colouring.
4741 void VM_strconv (void)
4743 int ccase, redalpha, rednum, len, i;
4744 unsigned char resbuf[VM_STRINGTEMP_LENGTH];
4745 unsigned char *result = resbuf;
4747 VM_SAFEPARMCOUNTRANGE(3, 8, VM_strconv);
4749 ccase = (int) PRVM_G_FLOAT(OFS_PARM0); //0 same, 1 lower, 2 upper
4750 redalpha = (int) PRVM_G_FLOAT(OFS_PARM1); //0 same, 1 white, 2 red, 5 alternate, 6 alternate-alternate
4751 rednum = (int) PRVM_G_FLOAT(OFS_PARM2); //0 same, 1 white, 2 red, 3 redspecial, 4 whitespecial, 5 alternate, 6 alternate-alternate
4752 VM_VarString(3, (char *) resbuf, sizeof(resbuf));
4753 len = strlen((char *) resbuf);
4755 for (i = 0; i < len; i++, result++) //should this be done backwards?
4757 if (*result >= '0' && *result <= '9') //normal numbers...
4758 *result = chrconv_number(*result, '0', rednum);
4759 else if (*result >= '0'+128 && *result <= '9'+128)
4760 *result = chrconv_number(*result, '0'+128, rednum);
4761 else if (*result >= '0'+128-30 && *result <= '9'+128-30)
4762 *result = chrconv_number(*result, '0'+128-30, rednum);
4763 else if (*result >= '0'-30 && *result <= '9'-30)
4764 *result = chrconv_number(*result, '0'-30, rednum);
4766 else if (*result >= 'a' && *result <= 'z') //normal numbers...
4767 *result = chrchar_alpha(*result, 'a', 0, ccase, redalpha, i);
4768 else if (*result >= 'A' && *result <= 'Z') //normal numbers...
4769 *result = chrchar_alpha(*result, 'A', 0, ccase, redalpha, i);
4770 else if (*result >= 'a'+128 && *result <= 'z'+128) //normal numbers...
4771 *result = chrchar_alpha(*result, 'a', 128, ccase, redalpha, i);
4772 else if (*result >= 'A'+128 && *result <= 'Z'+128) //normal numbers...
4773 *result = chrchar_alpha(*result, 'A', 128, ccase, redalpha, i);
4775 else if ((*result & 127) < 16 || !redalpha) //special chars..
4777 else if (*result < 128)
4778 *result = chrconv_punct(*result, 0, redalpha);
4780 *result = chrconv_punct(*result, 128, redalpha);
4784 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString((char *) resbuf);
4787 // #225 string(float chars, string s, ...) strpad (FTE_STRINGS)
4788 void VM_strpad (void)
4790 char src[VM_STRINGTEMP_LENGTH];
4791 char destbuf[VM_STRINGTEMP_LENGTH];
4793 VM_SAFEPARMCOUNTRANGE(1, 8, VM_strpad);
4794 pad = (int) PRVM_G_FLOAT(OFS_PARM0);
4795 VM_VarString(1, src, sizeof(src));
4797 // note: < 0 = left padding, > 0 = right padding,
4798 // this is reverse logic of printf!
4799 dpsnprintf(destbuf, sizeof(destbuf), "%*s", -pad, src);
4801 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(destbuf);
4804 // #226 string(string info, string key, string value, ...) infoadd (FTE_STRINGS)
4805 //uses qw style \key\value strings
4806 void VM_infoadd (void)
4808 const char *info, *key;
4809 char value[VM_STRINGTEMP_LENGTH];
4810 char temp[VM_STRINGTEMP_LENGTH];
4812 VM_SAFEPARMCOUNTRANGE(2, 8, VM_infoadd);
4813 info = PRVM_G_STRING(OFS_PARM0);
4814 key = PRVM_G_STRING(OFS_PARM1);
4815 VM_VarString(2, value, sizeof(value));
4817 strlcpy(temp, info, VM_STRINGTEMP_LENGTH);
4819 InfoString_SetValue(temp, VM_STRINGTEMP_LENGTH, key, value);
4821 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(temp);
4824 // #227 string(string info, string key) infoget (FTE_STRINGS)
4825 //uses qw style \key\value strings
4826 void VM_infoget (void)
4830 char value[VM_STRINGTEMP_LENGTH];
4832 VM_SAFEPARMCOUNT(2, VM_infoget);
4833 info = PRVM_G_STRING(OFS_PARM0);
4834 key = PRVM_G_STRING(OFS_PARM1);
4836 InfoString_GetValue(info, key, value, VM_STRINGTEMP_LENGTH);
4838 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(value);
4841 //#228 float(string s1, string s2, float len) strncmp (FTE_STRINGS)
4842 // also float(string s1, string s2) strcmp (FRIK_FILE)
4843 void VM_strncmp (void)
4845 const char *s1, *s2;
4846 VM_SAFEPARMCOUNTRANGE(2, 3, VM_strncmp);
4847 s1 = PRVM_G_STRING(OFS_PARM0);
4848 s2 = PRVM_G_STRING(OFS_PARM1);
4851 PRVM_G_FLOAT(OFS_RETURN) = strncmp(s1, s2, (size_t)PRVM_G_FLOAT(OFS_PARM2));
4855 PRVM_G_FLOAT(OFS_RETURN) = strcmp(s1, s2);
4859 // #229 float(string s1, string s2) strcasecmp (FTE_STRINGS)
4860 // #230 float(string s1, string s2, float len) strncasecmp (FTE_STRINGS)
4861 void VM_strncasecmp (void)
4863 const char *s1, *s2;
4864 VM_SAFEPARMCOUNTRANGE(2, 3, VM_strncasecmp);
4865 s1 = PRVM_G_STRING(OFS_PARM0);
4866 s2 = PRVM_G_STRING(OFS_PARM1);
4869 PRVM_G_FLOAT(OFS_RETURN) = strncasecmp(s1, s2, (size_t)PRVM_G_FLOAT(OFS_PARM2));
4873 PRVM_G_FLOAT(OFS_RETURN) = strcasecmp(s1, s2);
4877 // #494 float(float caseinsensitive, string s, ...) crc16
4881 static char s[VM_STRINGTEMP_LENGTH];
4882 VM_SAFEPARMCOUNTRANGE(2, 8, VM_hash);
4883 insensitive = PRVM_G_FLOAT(OFS_PARM0);
4884 VM_VarString(1, s, sizeof(s));
4885 PRVM_G_FLOAT(OFS_RETURN) = (unsigned short) ((insensitive ? CRC_Block_CaseInsensitive : CRC_Block) ((unsigned char *) s, strlen(s)));
4888 void VM_wasfreed (void)
4890 VM_SAFEPARMCOUNT(1, VM_wasfreed);
4891 PRVM_G_FLOAT(OFS_RETURN) = PRVM_G_EDICT(OFS_PARM0)->priv.required->free;
4894 void VM_SetTraceGlobals(const trace_t *trace)
4897 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_allsolid)))
4898 val->_float = trace->allsolid;
4899 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_startsolid)))
4900 val->_float = trace->startsolid;
4901 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_fraction)))
4902 val->_float = trace->fraction;
4903 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_inwater)))
4904 val->_float = trace->inwater;
4905 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_inopen)))
4906 val->_float = trace->inopen;
4907 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_endpos)))
4908 VectorCopy(trace->endpos, val->vector);
4909 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_plane_normal)))
4910 VectorCopy(trace->plane.normal, val->vector);
4911 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_plane_dist)))
4912 val->_float = trace->plane.dist;
4913 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_ent)))
4914 val->edict = PRVM_EDICT_TO_PROG(trace->ent ? trace->ent : prog->edicts);
4915 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dpstartcontents)))
4916 val->_float = trace->startsupercontents;
4917 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dphitcontents)))
4918 val->_float = trace->hitsupercontents;
4919 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dphitq3surfaceflags)))
4920 val->_float = trace->hitq3surfaceflags;
4921 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dphittexturename)))
4922 val->string = trace->hittexture ? PRVM_SetTempString(trace->hittexture->name) : 0;
4927 void VM_Cmd_Init(void)
4929 // only init the stuff for the current prog
4933 // VM_BufStr_Init();
4936 void VM_Cmd_Reset(void)
4938 CL_PurgeOwner( MENUOWNER );
4940 VM_Files_CloseAll();
4942 // VM_BufStr_ShutDown();
4945 // #510 string(string input, ...) uri_escape (DP_QC_URI_ESCAPE)
4946 // does URI escaping on a string (replace evil stuff by %AB escapes)
4947 void VM_uri_escape (void)
4949 char src[VM_STRINGTEMP_LENGTH];
4950 char dest[VM_STRINGTEMP_LENGTH];
4952 static const char *hex = "0123456789ABCDEF";
4954 VM_SAFEPARMCOUNTRANGE(1, 8, VM_uri_escape);
4955 VM_VarString(0, src, sizeof(src));
4957 for(p = src, q = dest; *p && q < dest + sizeof(dest) - 3; ++p)
4959 if((*p >= 'A' && *p <= 'Z')
4960 || (*p >= 'a' && *p <= 'z')
4961 || (*p >= '0' && *p <= '9')
4962 || (*p == '-') || (*p == '_') || (*p == '.')
4963 || (*p == '!') || (*p == '~') || (*p == '*')
4964 || (*p == '\'') || (*p == '(') || (*p == ')'))
4969 *q++ = hex[(*(unsigned char *)p >> 4) & 0xF];
4970 *q++ = hex[ *(unsigned char *)p & 0xF];
4975 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(dest);
4978 // #510 string(string input, ...) uri_unescape (DP_QC_URI_ESCAPE)
4979 // does URI unescaping on a string (get back the evil stuff)
4980 void VM_uri_unescape (void)
4982 char src[VM_STRINGTEMP_LENGTH];
4983 char dest[VM_STRINGTEMP_LENGTH];
4987 VM_SAFEPARMCOUNTRANGE(1, 8, VM_uri_unescape);
4988 VM_VarString(0, src, sizeof(src));
4990 for(p = src, q = dest; *p; ) // no need to check size, because unescape can't expand
4994 if(p[1] >= '0' && p[1] <= '9')
4996 else if(p[1] >= 'a' && p[1] <= 'f')
4997 hi = p[1] - 'a' + 10;
4998 else if(p[1] >= 'A' && p[1] <= 'F')
4999 hi = p[1] - 'A' + 10;
5002 if(p[2] >= '0' && p[2] <= '9')
5004 else if(p[2] >= 'a' && p[2] <= 'f')
5005 lo = p[2] - 'a' + 10;
5006 else if(p[2] >= 'A' && p[2] <= 'F')
5007 lo = p[2] - 'A' + 10;
5010 if(hi != 0 || lo != 0) // don't unescape NUL bytes
5011 *q++ = (char) (hi * 0x10 + lo);
5022 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(dest);
5025 // #502 string(string filename) whichpack (DP_QC_WHICHPACK)
5026 // returns the name of the pack containing a file, or "" if it is not in any pack (but local or non-existant)
5027 void VM_whichpack (void)
5029 const char *fn, *pack;
5031 VM_SAFEPARMCOUNT(1, VM_whichpack);
5032 fn = PRVM_G_STRING(OFS_PARM0);
5033 pack = FS_WhichPack(fn);
5035 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(pack ? pack : "");
5043 char buffer[MAX_INPUTLINE];
5047 static void uri_to_string_callback(int status, size_t length_received, unsigned char *buffer, void *cbdata)
5049 uri_to_prog_t *handle = (uri_to_prog_t *) cbdata;
5051 if(!PRVM_ProgLoaded(handle->prognr))
5053 // curl reply came too late... so just drop it
5058 PRVM_SetProg(handle->prognr);
5060 if((prog->starttime == handle->starttime) && (prog->funcoffsets.URI_Get_Callback))
5062 if(length_received >= sizeof(handle->buffer))
5063 length_received = sizeof(handle->buffer) - 1;
5064 handle->buffer[length_received] = 0;
5066 PRVM_G_FLOAT(OFS_PARM0) = handle->id;
5067 PRVM_G_FLOAT(OFS_PARM1) = status;
5068 PRVM_G_INT(OFS_PARM2) = PRVM_SetTempString(handle->buffer);
5069 PRVM_ExecuteProgram(prog->funcoffsets.URI_Get_Callback, "QC function URI_Get_Callback is missing");
5076 // 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
5077 // 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
5078 void VM_uri_get (void)
5083 uri_to_prog_t *handle;
5085 if(!prog->funcoffsets.URI_Get_Callback)
5086 PRVM_ERROR("uri_get called by %s without URI_Get_Callback defined", PRVM_NAME);
5088 VM_SAFEPARMCOUNT(2, VM_uri_get);
5090 url = PRVM_G_STRING(OFS_PARM0);
5091 id = PRVM_G_FLOAT(OFS_PARM1);
5092 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!
5094 handle->prognr = PRVM_GetProgNr();
5095 handle->starttime = prog->starttime;
5097 ret = Curl_Begin_ToMemory(url, (unsigned char *) handle->buffer, sizeof(handle->buffer), uri_to_string_callback, handle);
5100 PRVM_G_INT(OFS_RETURN) = 1;
5105 PRVM_G_INT(OFS_RETURN) = 0;
5109 void VM_netaddress_resolve (void)
5112 char normalized[128];
5114 lhnetaddress_t addr;
5116 VM_SAFEPARMCOUNTRANGE(1, 2, VM_netaddress_resolve);
5118 ip = PRVM_G_STRING(OFS_PARM0);
5121 port = (int) PRVM_G_FLOAT(OFS_PARM1);
5123 if(LHNETADDRESS_FromString(&addr, ip, port) && LHNETADDRESS_ToString(&addr, normalized, sizeof(normalized), prog->argc > 1))
5124 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(normalized);
5126 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString("");