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);
416 static qboolean PRVM_Cvar_ReadOk(const char *string)
419 cvar = Cvar_FindVar(string);
420 return ((cvar) && ((cvar->flags & CVAR_PRIVATE) == 0));
432 char string[VM_STRINGTEMP_LENGTH];
433 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar);
434 VM_VarString(0, string, sizeof(string));
435 VM_CheckEmptyString(string);
436 PRVM_G_FLOAT(OFS_RETURN) = PRVM_Cvar_ReadOk(string) ? Cvar_VariableValue(string) : 0;
443 float cvar_type (string)
444 float CVAR_TYPEFLAG_EXISTS = 1;
445 float CVAR_TYPEFLAG_SAVED = 2;
446 float CVAR_TYPEFLAG_PRIVATE = 4;
447 float CVAR_TYPEFLAG_ENGINE = 8;
448 float CVAR_TYPEFLAG_HASDESCRIPTION = 16;
449 float CVAR_TYPEFLAG_READONLY = 32;
452 void VM_cvar_type (void)
454 char string[VM_STRINGTEMP_LENGTH];
458 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar);
459 VM_VarString(0, string, sizeof(string));
460 VM_CheckEmptyString(string);
461 cvar = Cvar_FindVar(string);
466 PRVM_G_FLOAT(OFS_RETURN) = 0;
467 return; // CVAR_TYPE_NONE
470 ret = 1; // CVAR_EXISTS
471 if(cvar->flags & CVAR_SAVE)
472 ret |= 2; // CVAR_TYPE_SAVED
473 if(cvar->flags & CVAR_PRIVATE)
474 ret |= 4; // CVAR_TYPE_PRIVATE
475 if(!(cvar->flags & CVAR_ALLOCATED))
476 ret |= 8; // CVAR_TYPE_ENGINE
477 if(cvar->description != cvar_dummy_description)
478 ret |= 16; // CVAR_TYPE_HASDESCRIPTION
479 if(cvar->flags & CVAR_READONLY)
480 ret |= 32; // CVAR_TYPE_READONLY
482 PRVM_G_FLOAT(OFS_RETURN) = ret;
489 const string VM_cvar_string (string, ...)
492 void VM_cvar_string(void)
494 char string[VM_STRINGTEMP_LENGTH];
495 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar_string);
496 VM_VarString(0, string, sizeof(string));
497 VM_CheckEmptyString(string);
498 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(PRVM_Cvar_ReadOk(string) ? Cvar_VariableString(string) : "");
503 ========================
506 const string VM_cvar_defstring (string, ...)
507 ========================
509 void VM_cvar_defstring (void)
511 char string[VM_STRINGTEMP_LENGTH];
512 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar_defstring);
513 VM_VarString(0, string, sizeof(string));
514 VM_CheckEmptyString(string);
515 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(Cvar_VariableDefString(string));
519 ========================
522 const string VM_cvar_description (string, ...)
523 ========================
525 void VM_cvar_description (void)
527 char string[VM_STRINGTEMP_LENGTH];
528 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar_description);
529 VM_VarString(0, string, sizeof(string));
530 VM_CheckEmptyString(string);
531 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(Cvar_VariableDescription(string));
537 void cvar_set (string,string, ...)
540 void VM_cvar_set (void)
543 char string[VM_STRINGTEMP_LENGTH];
544 VM_SAFEPARMCOUNTRANGE(2,8,VM_cvar_set);
545 VM_VarString(1, string, sizeof(string));
546 name = PRVM_G_STRING(OFS_PARM0);
547 VM_CheckEmptyString(name);
548 Cvar_Set(name, string);
558 void VM_dprint (void)
560 char string[VM_STRINGTEMP_LENGTH];
561 VM_SAFEPARMCOUNTRANGE(1, 8, VM_dprint);
562 if (developer.integer)
564 VM_VarString(0, string, sizeof(string));
566 Con_Printf("%s", string);
568 Con_Printf("%s: %s", PRVM_NAME, string);
586 VM_SAFEPARMCOUNT(1, VM_ftos);
588 v = PRVM_G_FLOAT(OFS_PARM0);
590 if ((float)((int)v) == v)
591 dpsnprintf(s, sizeof(s), "%i", (int)v);
593 dpsnprintf(s, sizeof(s), "%f", v);
594 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(s);
609 VM_SAFEPARMCOUNT(1,VM_fabs);
611 v = PRVM_G_FLOAT(OFS_PARM0);
612 PRVM_G_FLOAT(OFS_RETURN) = fabs(v);
627 VM_SAFEPARMCOUNT(1,VM_vtos);
629 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]);
630 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(s);
645 VM_SAFEPARMCOUNT(1, VM_etos);
647 dpsnprintf (s, sizeof(s), "entity %i", PRVM_G_EDICTNUM(OFS_PARM0));
648 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(s);
655 float stof(...[string])
660 char string[VM_STRINGTEMP_LENGTH];
661 VM_SAFEPARMCOUNTRANGE(1, 8, VM_stof);
662 VM_VarString(0, string, sizeof(string));
663 PRVM_G_FLOAT(OFS_RETURN) = atof(string);
667 ========================
671 ========================
675 VM_SAFEPARMCOUNT(1, VM_itof);
676 PRVM_G_FLOAT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
680 ========================
683 entity ftoe(float num)
684 ========================
689 VM_SAFEPARMCOUNT(1, VM_ftoe);
691 ent = (int)PRVM_G_FLOAT(OFS_PARM0);
692 if (ent < 0 || ent >= MAX_EDICTS || PRVM_PROG_TO_EDICT(ent)->priv.required->free)
693 ent = 0; // return world instead of a free or invalid entity
695 PRVM_G_INT(OFS_RETURN) = ent;
699 ========================
702 float etof(entity ent)
703 ========================
707 VM_SAFEPARMCOUNT(1, VM_etof);
708 PRVM_G_FLOAT(OFS_RETURN) = PRVM_G_EDICTNUM(OFS_PARM0);
715 string strftime(float uselocaltime, string[, string ...])
718 void VM_strftime(void)
727 char fmt[VM_STRINGTEMP_LENGTH];
728 char result[VM_STRINGTEMP_LENGTH];
729 VM_SAFEPARMCOUNTRANGE(2, 8, VM_strftime);
730 VM_VarString(1, fmt, sizeof(fmt));
733 if (PRVM_G_FLOAT(OFS_PARM0))
734 tmresult = localtime_s(&tm, &t);
736 tmresult = gmtime_s(&tm, &t);
739 if (PRVM_G_FLOAT(OFS_PARM0))
746 PRVM_G_INT(OFS_RETURN) = 0;
750 strftime(result, sizeof(result), fmt, &tm);
752 strftime(result, sizeof(result), fmt, tm);
754 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(result);
768 VM_SAFEPARMCOUNT(0, VM_spawn);
769 prog->xfunction->builtinsprofile += 20;
770 ed = PRVM_ED_Alloc();
782 void VM_remove (void)
785 prog->xfunction->builtinsprofile += 20;
787 VM_SAFEPARMCOUNT(1, VM_remove);
789 ed = PRVM_G_EDICT(OFS_PARM0);
790 if( PRVM_NUM_FOR_EDICT(ed) <= prog->reserved_edicts )
792 if (developer.integer >= 1)
793 VM_Warning( "VM_remove: tried to remove the null entity or a reserved entity!\n" );
795 else if( ed->priv.required->free )
797 if (developer.integer >= 1)
798 VM_Warning( "VM_remove: tried to remove an already freed entity!\n" );
808 entity find(entity start, .string field, string match)
819 VM_SAFEPARMCOUNT(3,VM_find);
821 e = PRVM_G_EDICTNUM(OFS_PARM0);
822 f = PRVM_G_INT(OFS_PARM1);
823 s = PRVM_G_STRING(OFS_PARM2);
825 // LordHavoc: apparently BloodMage does a find(world, weaponmodel, "") and
826 // expects it to find all the monsters, so we must be careful to support
829 for (e++ ; e < prog->num_edicts ; e++)
831 prog->xfunction->builtinsprofile++;
832 ed = PRVM_EDICT_NUM(e);
833 if (ed->priv.required->free)
835 t = PRVM_E_STRING(ed,f);
845 VM_RETURN_EDICT(prog->edicts);
852 entity findfloat(entity start, .float field, float match)
853 entity findentity(entity start, .entity field, entity match)
856 // LordHavoc: added this for searching float, int, and entity reference fields
857 void VM_findfloat (void)
864 VM_SAFEPARMCOUNT(3,VM_findfloat);
866 e = PRVM_G_EDICTNUM(OFS_PARM0);
867 f = PRVM_G_INT(OFS_PARM1);
868 s = PRVM_G_FLOAT(OFS_PARM2);
870 for (e++ ; e < prog->num_edicts ; e++)
872 prog->xfunction->builtinsprofile++;
873 ed = PRVM_EDICT_NUM(e);
874 if (ed->priv.required->free)
876 if (PRVM_E_FLOAT(ed,f) == s)
883 VM_RETURN_EDICT(prog->edicts);
890 entity findchain(.string field, string match)
893 // chained search for strings in entity fields
894 // entity(.string field, string match) findchain = #402;
895 void VM_findchain (void)
900 prvm_edict_t *ent, *chain;
903 VM_SAFEPARMCOUNTRANGE(2,3,VM_findchain);
906 chainfield = PRVM_G_INT(OFS_PARM2);
908 chainfield = prog->fieldoffsets.chain;
910 PRVM_ERROR("VM_findchain: %s doesnt have the specified chain field !", PRVM_NAME);
912 chain = prog->edicts;
914 f = PRVM_G_INT(OFS_PARM0);
915 s = PRVM_G_STRING(OFS_PARM1);
917 // LordHavoc: apparently BloodMage does a find(world, weaponmodel, "") and
918 // expects it to find all the monsters, so we must be careful to support
921 ent = PRVM_NEXT_EDICT(prog->edicts);
922 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
924 prog->xfunction->builtinsprofile++;
925 if (ent->priv.required->free)
927 t = PRVM_E_STRING(ent,f);
933 PRVM_EDICTFIELDVALUE(ent,chainfield)->edict = PRVM_NUM_FOR_EDICT(chain);
937 VM_RETURN_EDICT(chain);
944 entity findchainfloat(.string field, float match)
945 entity findchainentity(.string field, entity match)
948 // LordHavoc: chained search for float, int, and entity reference fields
949 // entity(.string field, float match) findchainfloat = #403;
950 void VM_findchainfloat (void)
955 prvm_edict_t *ent, *chain;
958 VM_SAFEPARMCOUNTRANGE(2, 3, VM_findchainfloat);
961 chainfield = PRVM_G_INT(OFS_PARM2);
963 chainfield = prog->fieldoffsets.chain;
965 PRVM_ERROR("VM_findchain: %s doesnt have the specified chain field !", PRVM_NAME);
967 chain = (prvm_edict_t *)prog->edicts;
969 f = PRVM_G_INT(OFS_PARM0);
970 s = PRVM_G_FLOAT(OFS_PARM1);
972 ent = PRVM_NEXT_EDICT(prog->edicts);
973 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
975 prog->xfunction->builtinsprofile++;
976 if (ent->priv.required->free)
978 if (PRVM_E_FLOAT(ent,f) != s)
981 PRVM_EDICTFIELDVALUE(ent,chainfield)->edict = PRVM_EDICT_TO_PROG(chain);
985 VM_RETURN_EDICT(chain);
989 ========================
992 entity findflags(entity start, .float field, float match)
993 ========================
995 // LordHavoc: search for flags in float fields
996 void VM_findflags (void)
1003 VM_SAFEPARMCOUNT(3, VM_findflags);
1006 e = PRVM_G_EDICTNUM(OFS_PARM0);
1007 f = PRVM_G_INT(OFS_PARM1);
1008 s = (int)PRVM_G_FLOAT(OFS_PARM2);
1010 for (e++ ; e < prog->num_edicts ; e++)
1012 prog->xfunction->builtinsprofile++;
1013 ed = PRVM_EDICT_NUM(e);
1014 if (ed->priv.required->free)
1016 if (!PRVM_E_FLOAT(ed,f))
1018 if ((int)PRVM_E_FLOAT(ed,f) & s)
1020 VM_RETURN_EDICT(ed);
1025 VM_RETURN_EDICT(prog->edicts);
1029 ========================
1032 entity findchainflags(.float field, float match)
1033 ========================
1035 // LordHavoc: chained search for flags in float fields
1036 void VM_findchainflags (void)
1041 prvm_edict_t *ent, *chain;
1044 VM_SAFEPARMCOUNTRANGE(2, 3, VM_findchainflags);
1047 chainfield = PRVM_G_INT(OFS_PARM2);
1049 chainfield = prog->fieldoffsets.chain;
1051 PRVM_ERROR("VM_findchain: %s doesnt have the specified chain field !", PRVM_NAME);
1053 chain = (prvm_edict_t *)prog->edicts;
1055 f = PRVM_G_INT(OFS_PARM0);
1056 s = (int)PRVM_G_FLOAT(OFS_PARM1);
1058 ent = PRVM_NEXT_EDICT(prog->edicts);
1059 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
1061 prog->xfunction->builtinsprofile++;
1062 if (ent->priv.required->free)
1064 if (!PRVM_E_FLOAT(ent,f))
1066 if (!((int)PRVM_E_FLOAT(ent,f) & s))
1069 PRVM_EDICTFIELDVALUE(ent,chainfield)->edict = PRVM_EDICT_TO_PROG(chain);
1073 VM_RETURN_EDICT(chain);
1080 string precache_sound (string sample)
1083 void VM_precache_sound (void)
1087 VM_SAFEPARMCOUNT(1, VM_precache_sound);
1089 s = PRVM_G_STRING(OFS_PARM0);
1090 PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
1091 VM_CheckEmptyString(s);
1093 if(snd_initialized.integer && !S_PrecacheSound(s, true, true))
1095 VM_Warning("VM_precache_sound: Failed to load %s for %s\n", s, PRVM_NAME);
1104 returns the same string as output
1106 does nothing, only used by qcc to build .pak archives
1109 void VM_precache_file (void)
1111 VM_SAFEPARMCOUNT(1,VM_precache_file);
1112 // precache_file is only used to copy files with qcc, it does nothing
1113 PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
1123 void VM_coredump (void)
1125 VM_SAFEPARMCOUNT(0,VM_coredump);
1127 Cbuf_AddText("prvm_edicts ");
1128 Cbuf_AddText(PRVM_NAME);
1139 void PRVM_StackTrace(void);
1140 void VM_stackdump (void)
1142 VM_SAFEPARMCOUNT(0, VM_stackdump);
1157 VM_SAFEPARMCOUNT(0, VM_crash);
1159 PRVM_ERROR("Crash called by %s",PRVM_NAME);
1169 void VM_traceon (void)
1171 VM_SAFEPARMCOUNT(0,VM_traceon);
1183 void VM_traceoff (void)
1185 VM_SAFEPARMCOUNT(0,VM_traceoff);
1187 prog->trace = false;
1197 void VM_eprint (void)
1199 VM_SAFEPARMCOUNT(1,VM_eprint);
1201 PRVM_ED_PrintNum (PRVM_G_EDICTNUM(OFS_PARM0), NULL);
1214 VM_SAFEPARMCOUNT(1,VM_rint);
1216 f = PRVM_G_FLOAT(OFS_PARM0);
1218 PRVM_G_FLOAT(OFS_RETURN) = floor(f + 0.5);
1220 PRVM_G_FLOAT(OFS_RETURN) = ceil(f - 0.5);
1230 void VM_floor (void)
1232 VM_SAFEPARMCOUNT(1,VM_floor);
1234 PRVM_G_FLOAT(OFS_RETURN) = floor(PRVM_G_FLOAT(OFS_PARM0));
1246 VM_SAFEPARMCOUNT(1,VM_ceil);
1248 PRVM_G_FLOAT(OFS_RETURN) = ceil(PRVM_G_FLOAT(OFS_PARM0));
1256 entity nextent(entity)
1259 void VM_nextent (void)
1264 VM_SAFEPARMCOUNT(1, VM_nextent);
1266 i = PRVM_G_EDICTNUM(OFS_PARM0);
1269 prog->xfunction->builtinsprofile++;
1271 if (i == prog->num_edicts)
1273 VM_RETURN_EDICT(prog->edicts);
1276 ent = PRVM_EDICT_NUM(i);
1277 if (!ent->priv.required->free)
1279 VM_RETURN_EDICT(ent);
1285 //=============================================================================
1292 changelevel(string map)
1295 void VM_changelevel (void)
1297 VM_SAFEPARMCOUNT(1, VM_changelevel);
1301 VM_Warning("VM_changelevel: game is not server (%s)\n", PRVM_NAME);
1305 // make sure we don't issue two changelevels
1306 if (svs.changelevel_issued)
1308 svs.changelevel_issued = true;
1310 Cbuf_AddText (va("changelevel %s\n",PRVM_G_STRING(OFS_PARM0)));
1322 VM_SAFEPARMCOUNT(1,VM_sin);
1323 PRVM_G_FLOAT(OFS_RETURN) = sin(PRVM_G_FLOAT(OFS_PARM0));
1334 VM_SAFEPARMCOUNT(1,VM_cos);
1335 PRVM_G_FLOAT(OFS_RETURN) = cos(PRVM_G_FLOAT(OFS_PARM0));
1347 VM_SAFEPARMCOUNT(1,VM_sqrt);
1348 PRVM_G_FLOAT(OFS_RETURN) = sqrt(PRVM_G_FLOAT(OFS_PARM0));
1360 VM_SAFEPARMCOUNT(1,VM_asin);
1361 PRVM_G_FLOAT(OFS_RETURN) = asin(PRVM_G_FLOAT(OFS_PARM0));
1372 VM_SAFEPARMCOUNT(1,VM_acos);
1373 PRVM_G_FLOAT(OFS_RETURN) = acos(PRVM_G_FLOAT(OFS_PARM0));
1384 VM_SAFEPARMCOUNT(1,VM_atan);
1385 PRVM_G_FLOAT(OFS_RETURN) = atan(PRVM_G_FLOAT(OFS_PARM0));
1391 float atan2(float,float)
1394 void VM_atan2 (void)
1396 VM_SAFEPARMCOUNT(2,VM_atan2);
1397 PRVM_G_FLOAT(OFS_RETURN) = atan2(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1408 VM_SAFEPARMCOUNT(1,VM_tan);
1409 PRVM_G_FLOAT(OFS_RETURN) = tan(PRVM_G_FLOAT(OFS_PARM0));
1416 Returns a vector of length < 1 and > 0
1421 void VM_randomvec (void)
1426 VM_SAFEPARMCOUNT(0, VM_randomvec);
1431 temp[0] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1432 temp[1] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1433 temp[2] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1435 while (DotProduct(temp, temp) >= 1);
1436 VectorCopy (temp, PRVM_G_VECTOR(OFS_RETURN));
1439 temp[0] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1440 temp[1] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1441 temp[2] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1442 // length returned always > 0
1443 length = (rand()&32766 + 1) * (1.0 / 32767.0) / VectorLength(temp);
1444 VectorScale(temp,length, temp);*/
1445 //VectorCopy(temp, PRVM_G_VECTOR(OFS_RETURN));
1448 //=============================================================================
1454 float registercvar (string name, string value[, float flags])
1457 void VM_registercvar (void)
1459 const char *name, *value;
1462 VM_SAFEPARMCOUNTRANGE(2, 3, VM_registercvar);
1464 name = PRVM_G_STRING(OFS_PARM0);
1465 value = PRVM_G_STRING(OFS_PARM1);
1466 flags = prog->argc >= 3 ? (int)PRVM_G_FLOAT(OFS_PARM2) : 0;
1467 PRVM_G_FLOAT(OFS_RETURN) = 0;
1469 if(flags > CVAR_MAXFLAGSVAL)
1472 // first check to see if it has already been defined
1473 if (Cvar_FindVar (name))
1476 // check for overlap with a command
1477 if (Cmd_Exists (name))
1479 VM_Warning("VM_registercvar: %s is a command\n", name);
1483 Cvar_Get(name, value, flags, NULL);
1485 PRVM_G_FLOAT(OFS_RETURN) = 1; // success
1493 returns the minimum of two supplied floats
1495 float min(float a, float b, ...[float])
1500 VM_SAFEPARMCOUNTRANGE(2, 8, VM_min);
1501 // LordHavoc: 3+ argument enhancement suggested by FrikaC
1502 if (prog->argc >= 3)
1505 float f = PRVM_G_FLOAT(OFS_PARM0);
1506 for (i = 1;i < prog->argc;i++)
1507 if (f > PRVM_G_FLOAT((OFS_PARM0+i*3)))
1508 f = PRVM_G_FLOAT((OFS_PARM0+i*3));
1509 PRVM_G_FLOAT(OFS_RETURN) = f;
1512 PRVM_G_FLOAT(OFS_RETURN) = min(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1519 returns the maximum of two supplied floats
1521 float max(float a, float b, ...[float])
1526 VM_SAFEPARMCOUNTRANGE(2, 8, VM_max);
1527 // LordHavoc: 3+ argument enhancement suggested by FrikaC
1528 if (prog->argc >= 3)
1531 float f = PRVM_G_FLOAT(OFS_PARM0);
1532 for (i = 1;i < prog->argc;i++)
1533 if (f < PRVM_G_FLOAT((OFS_PARM0+i*3)))
1534 f = PRVM_G_FLOAT((OFS_PARM0+i*3));
1535 PRVM_G_FLOAT(OFS_RETURN) = f;
1538 PRVM_G_FLOAT(OFS_RETURN) = max(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1545 returns number bounded by supplied range
1547 float bound(float min, float value, float max)
1550 void VM_bound (void)
1552 VM_SAFEPARMCOUNT(3,VM_bound);
1553 PRVM_G_FLOAT(OFS_RETURN) = bound(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1), PRVM_G_FLOAT(OFS_PARM2));
1560 returns a raised to power b
1562 float pow(float a, float b)
1567 VM_SAFEPARMCOUNT(2,VM_pow);
1568 PRVM_G_FLOAT(OFS_RETURN) = pow(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1573 VM_SAFEPARMCOUNT(1,VM_log);
1574 PRVM_G_FLOAT(OFS_RETURN) = log(PRVM_G_FLOAT(OFS_PARM0));
1577 void VM_Files_Init(void)
1580 for (i = 0;i < PRVM_MAX_OPENFILES;i++)
1581 prog->openfiles[i] = NULL;
1584 void VM_Files_CloseAll(void)
1587 for (i = 0;i < PRVM_MAX_OPENFILES;i++)
1589 if (prog->openfiles[i])
1590 FS_Close(prog->openfiles[i]);
1591 prog->openfiles[i] = NULL;
1595 static qfile_t *VM_GetFileHandle( int index )
1597 if (index < 0 || index >= PRVM_MAX_OPENFILES)
1599 Con_Printf("VM_GetFileHandle: invalid file handle %i used in %s\n", index, PRVM_NAME);
1602 if (prog->openfiles[index] == NULL)
1604 Con_Printf("VM_GetFileHandle: no such file handle %i (or file has been closed) in %s\n", index, PRVM_NAME);
1607 return prog->openfiles[index];
1614 float fopen(string filename, float mode)
1617 // float(string filename, float mode) fopen = #110;
1618 // opens a file inside quake/gamedir/data/ (mode is FILE_READ, FILE_APPEND, or FILE_WRITE),
1619 // returns fhandle >= 0 if successful, or fhandle < 0 if unable to open file for any reason
1623 const char *modestring, *filename;
1625 VM_SAFEPARMCOUNT(2,VM_fopen);
1627 for (filenum = 0;filenum < PRVM_MAX_OPENFILES;filenum++)
1628 if (prog->openfiles[filenum] == NULL)
1630 if (filenum >= PRVM_MAX_OPENFILES)
1632 PRVM_G_FLOAT(OFS_RETURN) = -2;
1633 VM_Warning("VM_fopen: %s ran out of file handles (%i)\n", PRVM_NAME, PRVM_MAX_OPENFILES);
1636 filename = PRVM_G_STRING(OFS_PARM0);
1637 mode = (int)PRVM_G_FLOAT(OFS_PARM1);
1640 case 0: // FILE_READ
1642 prog->openfiles[filenum] = FS_OpenVirtualFile(va("data/%s", filename), false);
1643 if (prog->openfiles[filenum] == NULL)
1644 prog->openfiles[filenum] = FS_OpenVirtualFile(va("%s", filename), false);
1646 case 1: // FILE_APPEND
1648 prog->openfiles[filenum] = FS_OpenRealFile(va("data/%s", filename), modestring, false);
1650 case 2: // FILE_WRITE
1652 prog->openfiles[filenum] = FS_OpenRealFile(va("data/%s", filename), modestring, false);
1655 PRVM_G_FLOAT(OFS_RETURN) = -3;
1656 VM_Warning("VM_fopen: %s: no such mode %i (valid: 0 = read, 1 = append, 2 = write)\n", PRVM_NAME, mode);
1660 if (prog->openfiles[filenum] == NULL)
1662 PRVM_G_FLOAT(OFS_RETURN) = -1;
1663 if (developer.integer >= 100)
1664 VM_Warning("VM_fopen: %s: %s mode %s failed\n", PRVM_NAME, filename, modestring);
1668 PRVM_G_FLOAT(OFS_RETURN) = filenum;
1669 if (developer.integer >= 100)
1670 Con_Printf("VM_fopen: %s: %s mode %s opened as #%i\n", PRVM_NAME, filename, modestring, filenum);
1671 prog->openfiles_origin[filenum] = PRVM_AllocationOrigin();
1679 fclose(float fhandle)
1682 //void(float fhandle) fclose = #111; // closes a file
1683 void VM_fclose(void)
1687 VM_SAFEPARMCOUNT(1,VM_fclose);
1689 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1690 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
1692 VM_Warning("VM_fclose: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1695 if (prog->openfiles[filenum] == NULL)
1697 VM_Warning("VM_fclose: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1700 FS_Close(prog->openfiles[filenum]);
1701 prog->openfiles[filenum] = NULL;
1702 if(prog->openfiles_origin[filenum])
1703 PRVM_Free((char *)prog->openfiles_origin[filenum]);
1704 if (developer.integer >= 100)
1705 Con_Printf("VM_fclose: %s: #%i closed\n", PRVM_NAME, filenum);
1712 string fgets(float fhandle)
1715 //string(float fhandle) fgets = #112; // reads a line of text from the file and returns as a tempstring
1719 char string[VM_STRINGTEMP_LENGTH];
1722 VM_SAFEPARMCOUNT(1,VM_fgets);
1724 // set the return value regardless of any possible errors
1725 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
1727 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1728 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
1730 VM_Warning("VM_fgets: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1733 if (prog->openfiles[filenum] == NULL)
1735 VM_Warning("VM_fgets: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1741 c = FS_Getc(prog->openfiles[filenum]);
1742 if (c == '\r' || c == '\n' || c < 0)
1744 if (end < VM_STRINGTEMP_LENGTH - 1)
1748 // remove \n following \r
1751 c = FS_Getc(prog->openfiles[filenum]);
1753 FS_UnGetc(prog->openfiles[filenum], (unsigned char)c);
1755 if (developer.integer >= 100)
1756 Con_Printf("fgets: %s: %s\n", PRVM_NAME, string);
1758 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string);
1765 fputs(float fhandle, string s)
1768 //void(float fhandle, string s) fputs = #113; // writes a line of text to the end of the file
1772 char string[VM_STRINGTEMP_LENGTH];
1775 VM_SAFEPARMCOUNT(2,VM_fputs);
1777 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1778 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
1780 VM_Warning("VM_fputs: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1783 if (prog->openfiles[filenum] == NULL)
1785 VM_Warning("VM_fputs: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1788 VM_VarString(1, string, sizeof(string));
1789 if ((stringlength = (int)strlen(string)))
1790 FS_Write(prog->openfiles[filenum], string, stringlength);
1791 if (developer.integer >= 100)
1792 Con_Printf("fputs: %s: %s\n", PRVM_NAME, string);
1799 writetofile(float fhandle, entity ent)
1802 void VM_writetofile(void)
1807 VM_SAFEPARMCOUNT(2, VM_writetofile);
1809 file = VM_GetFileHandle( (int)PRVM_G_FLOAT(OFS_PARM0) );
1812 VM_Warning("VM_writetofile: invalid or closed file handle\n");
1816 ent = PRVM_G_EDICT(OFS_PARM1);
1817 if(ent->priv.required->free)
1819 VM_Warning("VM_writetofile: %s: entity %i is free !\n", PRVM_NAME, PRVM_NUM_FOR_EDICT(ent));
1823 PRVM_ED_Write (file, ent);
1826 // KrimZon - DP_QC_ENTITYDATA
1831 float() numentityfields
1832 Return the number of entity fields - NOT offsets
1835 void VM_numentityfields(void)
1837 PRVM_G_FLOAT(OFS_RETURN) = prog->progs->numfielddefs;
1840 // KrimZon - DP_QC_ENTITYDATA
1845 string(float fieldnum) entityfieldname
1846 Return name of the specified field as a string, or empty if the field is invalid (warning)
1849 void VM_entityfieldname(void)
1852 int i = (int)PRVM_G_FLOAT(OFS_PARM0);
1854 if (i < 0 || i >= prog->progs->numfielddefs)
1856 VM_Warning("VM_entityfieldname: %s: field index out of bounds\n", PRVM_NAME);
1857 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString("");
1861 d = &prog->fielddefs[i];
1862 PRVM_G_INT(OFS_RETURN) = d->s_name; // presuming that s_name points to a string already
1865 // KrimZon - DP_QC_ENTITYDATA
1870 float(float fieldnum) entityfieldtype
1873 void VM_entityfieldtype(void)
1876 int i = (int)PRVM_G_FLOAT(OFS_PARM0);
1878 if (i < 0 || i >= prog->progs->numfielddefs)
1880 VM_Warning("VM_entityfieldtype: %s: field index out of bounds\n", PRVM_NAME);
1881 PRVM_G_FLOAT(OFS_RETURN) = -1.0;
1885 d = &prog->fielddefs[i];
1886 PRVM_G_FLOAT(OFS_RETURN) = (float)d->type;
1889 // KrimZon - DP_QC_ENTITYDATA
1892 VM_getentityfieldstring
1894 string(float fieldnum, entity ent) getentityfieldstring
1897 void VM_getentityfieldstring(void)
1899 // put the data into a string
1904 int i = (int)PRVM_G_FLOAT(OFS_PARM0);
1906 if (i < 0 || i >= prog->progs->numfielddefs)
1908 VM_Warning("VM_entityfielddata: %s: field index out of bounds\n", PRVM_NAME);
1909 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString("");
1913 d = &prog->fielddefs[i];
1916 ent = PRVM_G_EDICT(OFS_PARM1);
1917 if(ent->priv.required->free)
1919 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString("");
1920 VM_Warning("VM_entityfielddata: %s: entity %i is free !\n", PRVM_NAME, PRVM_NUM_FOR_EDICT(ent));
1923 v = (int *)((char *)ent->fields.vp + d->ofs*4);
1925 // if it's 0 or blank, return an empty string
1926 type = d->type & ~DEF_SAVEGLOBAL;
1927 for (j=0 ; j<prvm_type_size[type] ; j++)
1930 if (j == prvm_type_size[type])
1932 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString("");
1936 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(PRVM_UglyValueString((etype_t)d->type, (prvm_eval_t *)v));
1939 // KrimZon - DP_QC_ENTITYDATA
1942 VM_putentityfieldstring
1944 float(float fieldnum, entity ent, string s) putentityfieldstring
1947 void VM_putentityfieldstring(void)
1951 int i = (int)PRVM_G_FLOAT(OFS_PARM0);
1953 if (i < 0 || i >= prog->progs->numfielddefs)
1955 VM_Warning("VM_entityfielddata: %s: field index out of bounds\n", PRVM_NAME);
1956 PRVM_G_FLOAT(OFS_RETURN) = 0.0f;
1960 d = &prog->fielddefs[i];
1963 ent = PRVM_G_EDICT(OFS_PARM1);
1964 if(ent->priv.required->free)
1966 VM_Warning("VM_entityfielddata: %s: entity %i is free !\n", PRVM_NAME, PRVM_NUM_FOR_EDICT(ent));
1967 PRVM_G_FLOAT(OFS_RETURN) = 0.0f;
1971 // parse the string into the value
1972 PRVM_G_FLOAT(OFS_RETURN) = ( PRVM_ED_ParseEpair(ent, d, PRVM_G_STRING(OFS_PARM2), false) ) ? 1.0f : 0.0f;
1979 float strlen(string s)
1982 //float(string s) strlen = #114; // returns how many characters are in a string
1983 void VM_strlen(void)
1985 VM_SAFEPARMCOUNT(1,VM_strlen);
1987 PRVM_G_FLOAT(OFS_RETURN) = strlen(PRVM_G_STRING(OFS_PARM0));
1990 // DRESK - Decolorized String
1995 string strdecolorize(string s)
1998 // string (string s) strdecolorize = #472; // returns the passed in string with color codes stripped
1999 void VM_strdecolorize(void)
2001 char szNewString[VM_STRINGTEMP_LENGTH];
2002 const char *szString;
2005 VM_SAFEPARMCOUNT(1,VM_strdecolorize);
2006 szString = PRVM_G_STRING(OFS_PARM0);
2007 COM_StringDecolorize(szString, 0, szNewString, sizeof(szNewString), TRUE);
2008 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(szNewString);
2011 // DRESK - String Length (not counting color codes)
2016 float strlennocol(string s)
2019 // float(string s) strlennocol = #471; // returns how many characters are in a string not including color codes
2020 // For example, ^2Dresk returns a length of 5
2021 void VM_strlennocol(void)
2023 const char *szString;
2026 VM_SAFEPARMCOUNT(1,VM_strlennocol);
2028 szString = PRVM_G_STRING(OFS_PARM0);
2030 nCnt = COM_StringLengthNoColors(szString, 0, NULL);
2032 PRVM_G_FLOAT(OFS_RETURN) = nCnt;
2035 // DRESK - String to Uppercase and Lowercase
2040 string strtolower(string s)
2043 // string (string s) strtolower = #480; // returns passed in string in lowercase form
2044 void VM_strtolower(void)
2046 char szNewString[VM_STRINGTEMP_LENGTH];
2047 const char *szString;
2050 VM_SAFEPARMCOUNT(1,VM_strtolower);
2051 szString = PRVM_G_STRING(OFS_PARM0);
2053 COM_ToLowerString(szString, szNewString, sizeof(szNewString) );
2055 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(szNewString);
2062 string strtoupper(string s)
2065 // string (string s) strtoupper = #481; // returns passed in string in uppercase form
2066 void VM_strtoupper(void)
2068 char szNewString[VM_STRINGTEMP_LENGTH];
2069 const char *szString;
2072 VM_SAFEPARMCOUNT(1,VM_strtoupper);
2073 szString = PRVM_G_STRING(OFS_PARM0);
2075 COM_ToUpperString(szString, szNewString, sizeof(szNewString) );
2077 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(szNewString);
2084 string strcat(string,string,...[string])
2087 //string(string s1, string s2) strcat = #115;
2088 // concatenates two strings (for example "abc", "def" would return "abcdef")
2089 // and returns as a tempstring
2090 void VM_strcat(void)
2092 char s[VM_STRINGTEMP_LENGTH];
2093 VM_SAFEPARMCOUNTRANGE(1, 8, VM_strcat);
2095 VM_VarString(0, s, sizeof(s));
2096 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(s);
2103 string substring(string s, float start, float length)
2106 // string(string s, float start, float length) substring = #116;
2107 // returns a section of a string as a tempstring
2108 void VM_substring(void)
2110 int start, length, slength, maxlen;
2112 char string[VM_STRINGTEMP_LENGTH];
2114 VM_SAFEPARMCOUNT(3,VM_substring);
2116 s = PRVM_G_STRING(OFS_PARM0);
2117 start = (int)PRVM_G_FLOAT(OFS_PARM1);
2118 length = (int)PRVM_G_FLOAT(OFS_PARM2);
2119 slength = strlen(s);
2121 if (start < 0) // FTE_STRINGS feature
2123 start = bound(0, start, slength);
2125 if (length < 0) // FTE_STRINGS feature
2126 length += slength - start + 1;
2127 maxlen = min((int)sizeof(string) - 1, slength - start);
2128 length = bound(0, length, maxlen);
2130 memcpy(string, s + start, length);
2132 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string);
2139 string(string search, string replace, string subject) strreplace = #484;
2142 // replaces all occurrences of search with replace in the string subject, and returns the result
2143 void VM_strreplace(void)
2146 const char *search, *replace, *subject;
2147 char string[VM_STRINGTEMP_LENGTH];
2148 int search_len, replace_len, subject_len;
2150 VM_SAFEPARMCOUNT(3,VM_strreplace);
2152 search = PRVM_G_STRING(OFS_PARM0);
2153 replace = PRVM_G_STRING(OFS_PARM1);
2154 subject = PRVM_G_STRING(OFS_PARM2);
2156 search_len = (int)strlen(search);
2157 replace_len = (int)strlen(replace);
2158 subject_len = (int)strlen(subject);
2161 for (i = 0; i < subject_len; i++)
2163 for (j = 0; j < search_len && i+j < subject_len; j++)
2164 if (subject[i+j] != search[j])
2166 if (j == search_len || i+j == subject_len)
2168 // found it at offset 'i'
2169 for (j = 0; j < replace_len && si < (int)sizeof(string) - 1; j++)
2170 string[si++] = replace[j];
2171 i += search_len - 1;
2176 if (si < (int)sizeof(string) - 1)
2177 string[si++] = subject[i];
2182 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string);
2189 string(string search, string replace, string subject) strireplace = #485;
2192 // case-insensitive version of strreplace
2193 void VM_strireplace(void)
2196 const char *search, *replace, *subject;
2197 char string[VM_STRINGTEMP_LENGTH];
2198 int search_len, replace_len, subject_len;
2200 VM_SAFEPARMCOUNT(3,VM_strreplace);
2202 search = PRVM_G_STRING(OFS_PARM0);
2203 replace = PRVM_G_STRING(OFS_PARM1);
2204 subject = PRVM_G_STRING(OFS_PARM2);
2206 search_len = (int)strlen(search);
2207 replace_len = (int)strlen(replace);
2208 subject_len = (int)strlen(subject);
2211 for (i = 0; i < subject_len; i++)
2213 for (j = 0; j < search_len && i+j < subject_len; j++)
2214 if (tolower(subject[i+j]) != tolower(search[j]))
2216 if (j == search_len || i+j == subject_len)
2218 // found it at offset 'i'
2219 for (j = 0; j < replace_len && si < (int)sizeof(string) - 1; j++)
2220 string[si++] = replace[j];
2221 i += search_len - 1;
2226 if (si < (int)sizeof(string) - 1)
2227 string[si++] = subject[i];
2232 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string);
2239 vector stov(string s)
2242 //vector(string s) stov = #117; // returns vector value from a string
2245 char string[VM_STRINGTEMP_LENGTH];
2247 VM_SAFEPARMCOUNT(1,VM_stov);
2249 VM_VarString(0, string, sizeof(string));
2250 Math_atov(string, PRVM_G_VECTOR(OFS_RETURN));
2257 string strzone(string s)
2260 //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)
2261 void VM_strzone(void)
2264 char string[VM_STRINGTEMP_LENGTH];
2267 VM_SAFEPARMCOUNT(1,VM_strzone);
2269 VM_VarString(0, string, sizeof(string));
2270 alloclen = strlen(string) + 1;
2271 PRVM_G_INT(OFS_RETURN) = PRVM_AllocString(alloclen, &out);
2272 memcpy(out, string, alloclen);
2282 //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!!!)
2283 void VM_strunzone(void)
2285 VM_SAFEPARMCOUNT(1,VM_strunzone);
2286 PRVM_FreeString(PRVM_G_INT(OFS_PARM0));
2291 VM_command (used by client and menu)
2293 clientcommand(float client, string s) (for client and menu)
2296 //void(entity e, string s) clientcommand = #440; // executes a command string as if it came from the specified client
2297 //this function originally written by KrimZon, made shorter by LordHavoc
2298 void VM_clcommand (void)
2300 client_t *temp_client;
2303 VM_SAFEPARMCOUNT(2,VM_clcommand);
2305 i = (int)PRVM_G_FLOAT(OFS_PARM0);
2306 if (!sv.active || i < 0 || i >= svs.maxclients || !svs.clients[i].active)
2308 VM_Warning("VM_clientcommand: %s: invalid client/server is not active !\n", PRVM_NAME);
2312 temp_client = host_client;
2313 host_client = svs.clients + i;
2314 Cmd_ExecuteString (PRVM_G_STRING(OFS_PARM1), src_client);
2315 host_client = temp_client;
2323 float tokenize(string s)
2326 //float(string s) tokenize = #441; // takes apart a string into individal words (access them with argv), returns how many
2327 //this function originally written by KrimZon, made shorter by LordHavoc
2328 //20040203: rewritten by LordHavoc (no longer uses allocations)
2329 static int num_tokens = 0;
2330 static int tokens[VM_STRINGTEMP_LENGTH / 2];
2331 static int tokens_startpos[VM_STRINGTEMP_LENGTH / 2];
2332 static int tokens_endpos[VM_STRINGTEMP_LENGTH / 2];
2333 static char tokenize_string[VM_STRINGTEMP_LENGTH];
2334 void VM_tokenize (void)
2338 VM_SAFEPARMCOUNT(1,VM_tokenize);
2340 strlcpy(tokenize_string, PRVM_G_STRING(OFS_PARM0), sizeof(tokenize_string));
2341 p = tokenize_string;
2346 if (num_tokens >= (int)(sizeof(tokens)/sizeof(tokens[0])))
2349 // skip whitespace here to find token start pos
2350 while(*p && ISWHITESPACE(*p))
2353 tokens_startpos[num_tokens] = p - tokenize_string;
2354 if(!COM_ParseToken_VM_Tokenize(&p, false))
2356 tokens_endpos[num_tokens] = p - tokenize_string;
2357 tokens[num_tokens] = PRVM_SetTempString(com_token);
2361 PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
2364 //float(string s) tokenize = #514; // takes apart a string into individal words (access them with argv), returns how many
2365 void VM_tokenize_console (void)
2369 VM_SAFEPARMCOUNT(1,VM_tokenize);
2371 strlcpy(tokenize_string, PRVM_G_STRING(OFS_PARM0), sizeof(tokenize_string));
2372 p = tokenize_string;
2377 if (num_tokens >= (int)(sizeof(tokens)/sizeof(tokens[0])))
2380 // skip whitespace here to find token start pos
2381 while(*p && ISWHITESPACE(*p))
2384 tokens_startpos[num_tokens] = p - tokenize_string;
2385 if(!COM_ParseToken_Console(&p))
2387 tokens_endpos[num_tokens] = p - tokenize_string;
2388 tokens[num_tokens] = PRVM_SetTempString(com_token);
2392 PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
2397 VM_tokenizebyseparator
2399 float tokenizebyseparator(string s, string separator1, ...)
2402 //float(string s, string separator1, ...) tokenizebyseparator = #479; // takes apart a string into individal words (access them with argv), returns how many
2403 //this function returns the token preceding each instance of a separator (of
2404 //which there can be multiple), and the text following the last separator
2405 //useful for parsing certain kinds of data like IP addresses
2407 //numnumbers = tokenizebyseparator("10.1.2.3", ".");
2408 //returns 4 and the tokens "10" "1" "2" "3".
2409 void VM_tokenizebyseparator (void)
2413 int separatorlen[7];
2414 const char *separators[7];
2417 char tokentext[MAX_INPUTLINE];
2419 VM_SAFEPARMCOUNTRANGE(2, 8,VM_tokenizebyseparator);
2421 strlcpy(tokenize_string, PRVM_G_STRING(OFS_PARM0), sizeof(tokenize_string));
2422 p = tokenize_string;
2425 for (j = 1;j < prog->argc;j++)
2427 // skip any blank separator strings
2428 const char *s = PRVM_G_STRING(OFS_PARM0+j*3);
2431 separators[numseparators] = s;
2432 separatorlen[numseparators] = strlen(s);
2439 while (num_tokens < (int)(sizeof(tokens)/sizeof(tokens[0])))
2441 token = tokentext + j;
2442 tokens_startpos[num_tokens] = p - tokenize_string;
2446 for (k = 0;k < numseparators;k++)
2448 if (!strncmp(p, separators[k], separatorlen[k]))
2450 p += separatorlen[k];
2454 if (k < numseparators)
2456 if (j < (int)sizeof(tokentext)-1)
2457 tokentext[j++] = *p;
2461 tokens_endpos[num_tokens] = p0 - tokenize_string;
2462 if (j >= (int)sizeof(tokentext))
2465 tokens[num_tokens++] = PRVM_SetTempString(token);
2470 PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
2473 //string(float n) argv = #442; // returns a word from the tokenized string (returns nothing for an invalid index)
2474 //this function originally written by KrimZon, made shorter by LordHavoc
2479 VM_SAFEPARMCOUNT(1,VM_argv);
2481 token_num = (int)PRVM_G_FLOAT(OFS_PARM0);
2484 token_num += num_tokens;
2486 if (token_num >= 0 && token_num < num_tokens)
2487 PRVM_G_INT(OFS_RETURN) = tokens[token_num];
2489 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
2492 //float(float n) argv_start_index = #515; // returns the start index of a token
2493 void VM_argv_start_index (void)
2497 VM_SAFEPARMCOUNT(1,VM_argv);
2499 token_num = (int)PRVM_G_FLOAT(OFS_PARM0);
2502 token_num += num_tokens;
2504 if (token_num >= 0 && token_num < num_tokens)
2505 PRVM_G_FLOAT(OFS_RETURN) = tokens_startpos[token_num];
2507 PRVM_G_FLOAT(OFS_RETURN) = -1;
2510 //float(float n) argv_end_index = #516; // returns the end index of a token
2511 void VM_argv_end_index (void)
2515 VM_SAFEPARMCOUNT(1,VM_argv);
2517 token_num = (int)PRVM_G_FLOAT(OFS_PARM0);
2520 token_num += num_tokens;
2522 if (token_num >= 0 && token_num < num_tokens)
2523 PRVM_G_FLOAT(OFS_RETURN) = tokens_endpos[token_num];
2525 PRVM_G_FLOAT(OFS_RETURN) = -1;
2535 void VM_isserver(void)
2537 VM_SAFEPARMCOUNT(0,VM_serverstate);
2539 PRVM_G_FLOAT(OFS_RETURN) = sv.active && (svs.maxclients > 1 || cls.state == ca_dedicated);
2549 void VM_clientcount(void)
2551 VM_SAFEPARMCOUNT(0,VM_clientcount);
2553 PRVM_G_FLOAT(OFS_RETURN) = svs.maxclients;
2563 void VM_clientstate(void)
2565 VM_SAFEPARMCOUNT(0,VM_clientstate);
2568 switch( cls.state ) {
2569 case ca_uninitialized:
2571 PRVM_G_FLOAT(OFS_RETURN) = 0;
2573 case ca_disconnected:
2574 PRVM_G_FLOAT(OFS_RETURN) = 1;
2577 PRVM_G_FLOAT(OFS_RETURN) = 2;
2580 // should never be reached!
2589 float getostype(void)
2591 */ // not used at the moment -> not included in the common list
2592 void VM_getostype(void)
2594 VM_SAFEPARMCOUNT(0,VM_getostype);
2599 OS_MAC - not supported
2603 PRVM_G_FLOAT(OFS_RETURN) = 0;
2604 #elif defined(MACOSX)
2605 PRVM_G_FLOAT(OFS_RETURN) = 2;
2607 PRVM_G_FLOAT(OFS_RETURN) = 1;
2618 extern double host_starttime;
2619 float CDAudio_GetPosition(void);
2620 void VM_gettime(void)
2624 VM_SAFEPARMCOUNTRANGE(0,1,VM_gettime);
2628 PRVM_G_FLOAT(OFS_RETURN) = (float) realtime;
2632 timer_index = (int) PRVM_G_FLOAT(OFS_PARM0);
2635 case 0: // GETTIME_FRAMESTART
2636 PRVM_G_FLOAT(OFS_RETURN) = (float) realtime;
2638 case 1: // GETTIME_REALTIME
2639 PRVM_G_FLOAT(OFS_RETURN) = (float) Sys_DoubleTime();
2641 case 2: // GETTIME_HIRES
2642 PRVM_G_FLOAT(OFS_RETURN) = (float) (Sys_DoubleTime() - realtime);
2644 case 3: // GETTIME_UPTIME
2645 PRVM_G_FLOAT(OFS_RETURN) = (float) (Sys_DoubleTime() - host_starttime);
2647 case 4: // GETTIME_CDTRACK
2648 PRVM_G_FLOAT(OFS_RETURN) = (float) CDAudio_GetPosition();
2651 VM_Warning("VM_gettime: %s: unsupported timer specified, returning realtime\n", PRVM_NAME);
2652 PRVM_G_FLOAT(OFS_RETURN) = (float) realtime;
2662 loadfromdata(string data)
2665 void VM_loadfromdata(void)
2667 VM_SAFEPARMCOUNT(1,VM_loadentsfromfile);
2669 PRVM_ED_LoadFromFile(PRVM_G_STRING(OFS_PARM0));
2673 ========================
2676 parseentitydata(entity ent, string data)
2677 ========================
2679 void VM_parseentitydata(void)
2684 VM_SAFEPARMCOUNT(2, VM_parseentitydata);
2686 // get edict and test it
2687 ent = PRVM_G_EDICT(OFS_PARM0);
2688 if (ent->priv.required->free)
2689 PRVM_ERROR ("VM_parseentitydata: %s: Can only set already spawned entities (entity %i is free)!", PRVM_NAME, PRVM_NUM_FOR_EDICT(ent));
2691 data = PRVM_G_STRING(OFS_PARM1);
2693 // parse the opening brace
2694 if (!COM_ParseToken_Simple(&data, false, false) || com_token[0] != '{' )
2695 PRVM_ERROR ("VM_parseentitydata: %s: Couldn't parse entity data:\n%s", PRVM_NAME, data );
2697 PRVM_ED_ParseEdict (data, ent);
2704 loadfromfile(string file)
2707 void VM_loadfromfile(void)
2709 const char *filename;
2712 VM_SAFEPARMCOUNT(1,VM_loadfromfile);
2714 filename = PRVM_G_STRING(OFS_PARM0);
2715 if (FS_CheckNastyPath(filename, false))
2717 PRVM_G_FLOAT(OFS_RETURN) = -4;
2718 VM_Warning("VM_loadfromfile: %s dangerous or non-portable filename \"%s\" not allowed. (contains : or \\ or begins with .. or /)\n", PRVM_NAME, filename);
2722 // not conform with VM_fopen
2723 data = (char *)FS_LoadFile(filename, tempmempool, false, NULL);
2725 PRVM_G_FLOAT(OFS_RETURN) = -1;
2727 PRVM_ED_LoadFromFile(data);
2738 float mod(float val, float m)
2741 void VM_modulo(void)
2744 VM_SAFEPARMCOUNT(2,VM_module);
2746 val = (int) PRVM_G_FLOAT(OFS_PARM0);
2747 m = (int) PRVM_G_FLOAT(OFS_PARM1);
2749 PRVM_G_FLOAT(OFS_RETURN) = (float) (val % m);
2752 void VM_Search_Init(void)
2755 for (i = 0;i < PRVM_MAX_OPENSEARCHES;i++)
2756 prog->opensearches[i] = NULL;
2759 void VM_Search_Reset(void)
2762 // reset the fssearch list
2763 for(i = 0; i < PRVM_MAX_OPENSEARCHES; i++)
2765 if(prog->opensearches[i])
2766 FS_FreeSearch(prog->opensearches[i]);
2767 prog->opensearches[i] = NULL;
2775 float search_begin(string pattern, float caseinsensitive, float quiet)
2778 void VM_search_begin(void)
2781 const char *pattern;
2782 int caseinsens, quiet;
2784 VM_SAFEPARMCOUNT(3, VM_search_begin);
2786 pattern = PRVM_G_STRING(OFS_PARM0);
2788 VM_CheckEmptyString(pattern);
2790 caseinsens = (int)PRVM_G_FLOAT(OFS_PARM1);
2791 quiet = (int)PRVM_G_FLOAT(OFS_PARM2);
2793 for(handle = 0; handle < PRVM_MAX_OPENSEARCHES; handle++)
2794 if(!prog->opensearches[handle])
2797 if(handle >= PRVM_MAX_OPENSEARCHES)
2799 PRVM_G_FLOAT(OFS_RETURN) = -2;
2800 VM_Warning("VM_search_begin: %s ran out of search handles (%i)\n", PRVM_NAME, PRVM_MAX_OPENSEARCHES);
2804 if(!(prog->opensearches[handle] = FS_Search(pattern,caseinsens, quiet)))
2805 PRVM_G_FLOAT(OFS_RETURN) = -1;
2808 prog->opensearches_origin[handle] = PRVM_AllocationOrigin();
2809 PRVM_G_FLOAT(OFS_RETURN) = handle;
2817 void search_end(float handle)
2820 void VM_search_end(void)
2823 VM_SAFEPARMCOUNT(1, VM_search_end);
2825 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
2827 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
2829 VM_Warning("VM_search_end: invalid handle %i used in %s\n", handle, PRVM_NAME);
2832 if(prog->opensearches[handle] == NULL)
2834 VM_Warning("VM_search_end: no such handle %i in %s\n", handle, PRVM_NAME);
2838 FS_FreeSearch(prog->opensearches[handle]);
2839 prog->opensearches[handle] = NULL;
2840 if(prog->opensearches_origin[handle])
2841 PRVM_Free((char *)prog->opensearches_origin[handle]);
2848 float search_getsize(float handle)
2851 void VM_search_getsize(void)
2854 VM_SAFEPARMCOUNT(1, VM_M_search_getsize);
2856 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
2858 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
2860 VM_Warning("VM_search_getsize: invalid handle %i used in %s\n", handle, PRVM_NAME);
2863 if(prog->opensearches[handle] == NULL)
2865 VM_Warning("VM_search_getsize: no such handle %i in %s\n", handle, PRVM_NAME);
2869 PRVM_G_FLOAT(OFS_RETURN) = prog->opensearches[handle]->numfilenames;
2874 VM_search_getfilename
2876 string search_getfilename(float handle, float num)
2879 void VM_search_getfilename(void)
2881 int handle, filenum;
2882 VM_SAFEPARMCOUNT(2, VM_search_getfilename);
2884 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
2885 filenum = (int)PRVM_G_FLOAT(OFS_PARM1);
2887 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
2889 VM_Warning("VM_search_getfilename: invalid handle %i used in %s\n", handle, PRVM_NAME);
2892 if(prog->opensearches[handle] == NULL)
2894 VM_Warning("VM_search_getfilename: no such handle %i in %s\n", handle, PRVM_NAME);
2897 if(filenum < 0 || filenum >= prog->opensearches[handle]->numfilenames)
2899 VM_Warning("VM_search_getfilename: invalid filenum %i in %s\n", filenum, PRVM_NAME);
2903 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog->opensearches[handle]->filenames[filenum]);
2910 string chr(float ascii)
2916 VM_SAFEPARMCOUNT(1, VM_chr);
2918 tmp[0] = (unsigned char) PRVM_G_FLOAT(OFS_PARM0);
2921 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(tmp);
2924 //=============================================================================
2925 // Draw builtins (client & menu)
2931 float iscachedpic(string pic)
2934 void VM_iscachedpic(void)
2936 VM_SAFEPARMCOUNT(1,VM_iscachedpic);
2938 // drawq hasnt such a function, thus always return true
2939 PRVM_G_FLOAT(OFS_RETURN) = false;
2946 string precache_pic(string pic)
2949 void VM_precache_pic(void)
2953 VM_SAFEPARMCOUNT(1, VM_precache_pic);
2955 s = PRVM_G_STRING(OFS_PARM0);
2956 PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
2957 VM_CheckEmptyString (s);
2959 // AK Draw_CachePic is supposed to always return a valid pointer
2960 if( Draw_CachePic_Flags(s, CACHEPICFLAG_NOTPERSISTENT)->tex == r_texture_notexture )
2961 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
2971 void VM_freepic(void)
2975 VM_SAFEPARMCOUNT(1,VM_freepic);
2977 s = PRVM_G_STRING(OFS_PARM0);
2978 VM_CheckEmptyString (s);
2983 dp_font_t *getdrawfont(void)
2985 if(prog->globaloffsets.drawfont >= 0)
2987 int f = (int) PRVM_G_FLOAT(prog->globaloffsets.drawfont);
2988 if(f < 0 || f >= MAX_FONTS)
2989 return FONT_DEFAULT;
2990 return &dp_fonts[f];
2993 return FONT_DEFAULT;
3000 float drawcharacter(vector position, float character, vector scale, vector rgb, float alpha, float flag)
3003 void VM_drawcharacter(void)
3005 float *pos,*scale,*rgb;
3008 VM_SAFEPARMCOUNT(6,VM_drawcharacter);
3010 character = (char) PRVM_G_FLOAT(OFS_PARM1);
3013 PRVM_G_FLOAT(OFS_RETURN) = -1;
3014 VM_Warning("VM_drawcharacter: %s passed null character !\n",PRVM_NAME);
3018 pos = PRVM_G_VECTOR(OFS_PARM0);
3019 scale = PRVM_G_VECTOR(OFS_PARM2);
3020 rgb = PRVM_G_VECTOR(OFS_PARM3);
3021 flag = (int)PRVM_G_FLOAT(OFS_PARM5);
3023 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3025 PRVM_G_FLOAT(OFS_RETURN) = -2;
3026 VM_Warning("VM_drawcharacter: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
3030 if(pos[2] || scale[2])
3031 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")));
3033 if(!scale[0] || !scale[1])
3035 PRVM_G_FLOAT(OFS_RETURN) = -3;
3036 VM_Warning("VM_drawcharacter: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
3040 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());
3041 PRVM_G_FLOAT(OFS_RETURN) = 1;
3048 float drawstring(vector position, string text, vector scale, vector rgb, float alpha, float flag)
3051 void VM_drawstring(void)
3053 float *pos,*scale,*rgb;
3056 VM_SAFEPARMCOUNT(6,VM_drawstring);
3058 string = PRVM_G_STRING(OFS_PARM1);
3059 pos = PRVM_G_VECTOR(OFS_PARM0);
3060 scale = PRVM_G_VECTOR(OFS_PARM2);
3061 rgb = PRVM_G_VECTOR(OFS_PARM3);
3062 flag = (int)PRVM_G_FLOAT(OFS_PARM5);
3064 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3066 PRVM_G_FLOAT(OFS_RETURN) = -2;
3067 VM_Warning("VM_drawstring: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
3071 if(!scale[0] || !scale[1])
3073 PRVM_G_FLOAT(OFS_RETURN) = -3;
3074 VM_Warning("VM_drawstring: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
3078 if(pos[2] || scale[2])
3079 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")));
3081 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());
3082 PRVM_G_FLOAT(OFS_RETURN) = 1;
3087 VM_drawcolorcodedstring
3089 float drawcolorcodedstring(vector position, string text, vector scale, float alpha, float flag)
3092 void VM_drawcolorcodedstring(void)
3097 VM_SAFEPARMCOUNT(5,VM_drawstring);
3099 string = PRVM_G_STRING(OFS_PARM1);
3100 pos = PRVM_G_VECTOR(OFS_PARM0);
3101 scale = PRVM_G_VECTOR(OFS_PARM2);
3102 flag = (int)PRVM_G_FLOAT(OFS_PARM4);
3104 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3106 PRVM_G_FLOAT(OFS_RETURN) = -2;
3107 VM_Warning("VM_drawcolorcodedstring: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
3111 if(!scale[0] || !scale[1])
3113 PRVM_G_FLOAT(OFS_RETURN) = -3;
3114 VM_Warning("VM_drawcolorcodedstring: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
3118 if(pos[2] || scale[2])
3119 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")));
3122 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());
3123 PRVM_G_FLOAT(OFS_RETURN) = 1;
3129 float stringwidth(string text, float allowColorCodes, float size)
3132 void VM_stringwidth(void)
3135 float sz, mult; // sz is intended font size so we can later add freetype support, mult is font size multiplier in pixels per character cell
3137 VM_SAFEPARMCOUNTRANGE(2,3,VM_drawstring);
3141 mult = sz = PRVM_G_FLOAT(OFS_PARM2);
3149 string = PRVM_G_STRING(OFS_PARM0);
3150 colors = (int)PRVM_G_FLOAT(OFS_PARM1);
3152 PRVM_G_FLOAT(OFS_RETURN) = DrawQ_TextWidth_Font(string, 0, !colors, getdrawfont()) * mult; // 1x1 characters, don't actually draw
3158 float drawpic(vector position, string pic, vector size, vector rgb, float alpha, float flag)
3161 void VM_drawpic(void)
3163 const char *picname;
3164 float *size, *pos, *rgb;
3167 VM_SAFEPARMCOUNT(6,VM_drawpic);
3169 picname = PRVM_G_STRING(OFS_PARM1);
3170 VM_CheckEmptyString (picname);
3172 // is pic cached ? no function yet for that
3175 PRVM_G_FLOAT(OFS_RETURN) = -4;
3176 VM_Warning("VM_drawpic: %s: %s not cached !\n", PRVM_NAME, picname);
3180 pos = PRVM_G_VECTOR(OFS_PARM0);
3181 size = PRVM_G_VECTOR(OFS_PARM2);
3182 rgb = PRVM_G_VECTOR(OFS_PARM3);
3183 flag = (int) PRVM_G_FLOAT(OFS_PARM5);
3185 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3187 PRVM_G_FLOAT(OFS_RETURN) = -2;
3188 VM_Warning("VM_drawpic: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
3192 if(pos[2] || size[2])
3193 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")));
3195 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);
3196 PRVM_G_FLOAT(OFS_RETURN) = 1;
3202 float drawrotpic(vector position, string pic, vector size, vector org, float angle, vector rgb, float alpha, float flag)
3205 void VM_drawrotpic(void)
3207 const char *picname;
3208 float *size, *pos, *org, *rgb;
3211 VM_SAFEPARMCOUNT(8,VM_drawrotpic);
3213 picname = PRVM_G_STRING(OFS_PARM1);
3214 VM_CheckEmptyString (picname);
3216 // is pic cached ? no function yet for that
3219 PRVM_G_FLOAT(OFS_RETURN) = -4;
3220 VM_Warning("VM_drawrotpic: %s: %s not cached !\n", PRVM_NAME, picname);
3224 pos = PRVM_G_VECTOR(OFS_PARM0);
3225 size = PRVM_G_VECTOR(OFS_PARM2);
3226 org = PRVM_G_VECTOR(OFS_PARM3);
3227 rgb = PRVM_G_VECTOR(OFS_PARM5);
3228 flag = (int) PRVM_G_FLOAT(OFS_PARM7);
3230 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3232 PRVM_G_FLOAT(OFS_RETURN) = -2;
3233 VM_Warning("VM_drawrotpic: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
3237 if(pos[2] || size[2] || org[2])
3238 Con_Printf("VM_drawrotpic: z value from pos/size/org discarded\n");
3240 DrawQ_RotPic(pos[0], pos[1], Draw_CachePic(picname), size[0], size[1], org[0], org[1], PRVM_G_FLOAT(OFS_PARM4), rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM6), flag);
3241 PRVM_G_FLOAT(OFS_RETURN) = 1;
3247 float drawsubpic(vector position, vector size, string pic, vector srcPos, vector srcSize, vector rgb, float alpha, float flag)
3251 void VM_drawsubpic(void)
3253 const char *picname;
3254 float *size, *pos, *rgb, *srcPos, *srcSize, alpha;
3257 VM_SAFEPARMCOUNT(8,VM_drawsubpic);
3259 picname = PRVM_G_STRING(OFS_PARM2);
3260 VM_CheckEmptyString (picname);
3262 // is pic cached ? no function yet for that
3265 PRVM_G_FLOAT(OFS_RETURN) = -4;
3266 VM_Warning("VM_drawsubpic: %s: %s not cached !\n", PRVM_NAME, picname);
3270 pos = PRVM_G_VECTOR(OFS_PARM0);
3271 size = PRVM_G_VECTOR(OFS_PARM1);
3272 srcPos = PRVM_G_VECTOR(OFS_PARM3);
3273 srcSize = PRVM_G_VECTOR(OFS_PARM4);
3274 rgb = PRVM_G_VECTOR(OFS_PARM5);
3275 alpha = PRVM_G_FLOAT(OFS_PARM6);
3276 flag = (int) PRVM_G_FLOAT(OFS_PARM7);
3278 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3280 PRVM_G_FLOAT(OFS_RETURN) = -2;
3281 VM_Warning("VM_drawsubpic: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
3285 if(pos[2] || size[2])
3286 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")));
3288 DrawQ_SuperPic(pos[0], pos[1], Draw_CachePic (picname),
3290 srcPos[0], srcPos[1], rgb[0], rgb[1], rgb[2], alpha,
3291 srcPos[0] + srcSize[0], srcPos[1], rgb[0], rgb[1], rgb[2], alpha,
3292 srcPos[0], srcPos[1] + srcSize[1], rgb[0], rgb[1], rgb[2], alpha,
3293 srcPos[0] + srcSize[0], srcPos[1] + srcSize[1], rgb[0], rgb[1], rgb[2], alpha,
3295 PRVM_G_FLOAT(OFS_RETURN) = 1;
3302 float drawfill(vector position, vector size, vector rgb, float alpha, float flag)
3305 void VM_drawfill(void)
3307 float *size, *pos, *rgb;
3310 VM_SAFEPARMCOUNT(5,VM_drawfill);
3313 pos = PRVM_G_VECTOR(OFS_PARM0);
3314 size = PRVM_G_VECTOR(OFS_PARM1);
3315 rgb = PRVM_G_VECTOR(OFS_PARM2);
3316 flag = (int) PRVM_G_FLOAT(OFS_PARM4);
3318 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3320 PRVM_G_FLOAT(OFS_RETURN) = -2;
3321 VM_Warning("VM_drawfill: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
3325 if(pos[2] || size[2])
3326 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")));
3328 DrawQ_Fill(pos[0], pos[1], size[0], size[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM3), flag);
3329 PRVM_G_FLOAT(OFS_RETURN) = 1;
3336 drawsetcliparea(float x, float y, float width, float height)
3339 void VM_drawsetcliparea(void)
3342 VM_SAFEPARMCOUNT(4,VM_drawsetcliparea);
3344 x = bound(0, PRVM_G_FLOAT(OFS_PARM0), vid_conwidth.integer);
3345 y = bound(0, PRVM_G_FLOAT(OFS_PARM1), vid_conheight.integer);
3346 w = bound(0, PRVM_G_FLOAT(OFS_PARM2) + PRVM_G_FLOAT(OFS_PARM0) - x, (vid_conwidth.integer - x));
3347 h = bound(0, PRVM_G_FLOAT(OFS_PARM3) + PRVM_G_FLOAT(OFS_PARM1) - y, (vid_conheight.integer - y));
3349 DrawQ_SetClipArea(x, y, w, h);
3354 VM_drawresetcliparea
3359 void VM_drawresetcliparea(void)
3361 VM_SAFEPARMCOUNT(0,VM_drawresetcliparea);
3363 DrawQ_ResetClipArea();
3370 vector getimagesize(string pic)
3373 void VM_getimagesize(void)
3378 VM_SAFEPARMCOUNT(1,VM_getimagesize);
3380 p = PRVM_G_STRING(OFS_PARM0);
3381 VM_CheckEmptyString (p);
3383 pic = Draw_CachePic_Flags (p, CACHEPICFLAG_NOTPERSISTENT);
3385 PRVM_G_VECTOR(OFS_RETURN)[0] = pic->width;
3386 PRVM_G_VECTOR(OFS_RETURN)[1] = pic->height;
3387 PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
3394 string keynumtostring(float keynum)
3397 void VM_keynumtostring (void)
3399 VM_SAFEPARMCOUNT(1, VM_keynumtostring);
3401 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(Key_KeynumToString((int)PRVM_G_FLOAT(OFS_PARM0)));
3406 VM_findkeysforcommand
3408 string findkeysforcommand(string command)
3410 the returned string is an altstring
3413 #define NUMKEYS 5 // TODO: merge the constant in keys.c with this one somewhen
3415 void M_FindKeysForCommand(const char *command, int *keys);
3416 void VM_findkeysforcommand(void)
3419 char ret[VM_STRINGTEMP_LENGTH];
3423 VM_SAFEPARMCOUNT(1, VM_findkeysforcommand);
3425 cmd = PRVM_G_STRING(OFS_PARM0);
3427 VM_CheckEmptyString(cmd);
3429 M_FindKeysForCommand(cmd, keys);
3432 for(i = 0; i < NUMKEYS; i++)
3433 strlcat(ret, va(" \'%i\'", keys[i]), sizeof(ret));
3435 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(ret);
3442 float stringtokeynum(string key)
3445 void VM_stringtokeynum (void)
3447 VM_SAFEPARMCOUNT( 1, VM_keynumtostring );
3449 PRVM_G_INT(OFS_RETURN) = Key_StringToKeynum(PRVM_G_STRING(OFS_PARM0));
3452 // CL_Video interface functions
3455 ========================
3458 float cin_open(string file, string name)
3459 ========================
3461 void VM_cin_open( void )
3466 VM_SAFEPARMCOUNT( 2, VM_cin_open );
3468 file = PRVM_G_STRING( OFS_PARM0 );
3469 name = PRVM_G_STRING( OFS_PARM1 );
3471 VM_CheckEmptyString( file );
3472 VM_CheckEmptyString( name );
3474 if( CL_OpenVideo( file, name, MENUOWNER ) )
3475 PRVM_G_FLOAT( OFS_RETURN ) = 1;
3477 PRVM_G_FLOAT( OFS_RETURN ) = 0;
3481 ========================
3484 void cin_close(string name)
3485 ========================
3487 void VM_cin_close( void )
3491 VM_SAFEPARMCOUNT( 1, VM_cin_close );
3493 name = PRVM_G_STRING( OFS_PARM0 );
3494 VM_CheckEmptyString( name );
3496 CL_CloseVideo( CL_GetVideoByName( name ) );
3500 ========================
3502 void cin_setstate(string name, float type)
3503 ========================
3505 void VM_cin_setstate( void )
3508 clvideostate_t state;
3511 VM_SAFEPARMCOUNT( 2, VM_cin_netstate );
3513 name = PRVM_G_STRING( OFS_PARM0 );
3514 VM_CheckEmptyString( name );
3516 state = (clvideostate_t)((int)PRVM_G_FLOAT( OFS_PARM1 ));
3518 video = CL_GetVideoByName( name );
3519 if( video && state > CLVIDEO_UNUSED && state < CLVIDEO_STATECOUNT )
3520 CL_SetVideoState( video, state );
3524 ========================
3527 float cin_getstate(string name)
3528 ========================
3530 void VM_cin_getstate( void )
3535 VM_SAFEPARMCOUNT( 1, VM_cin_getstate );
3537 name = PRVM_G_STRING( OFS_PARM0 );
3538 VM_CheckEmptyString( name );
3540 video = CL_GetVideoByName( name );
3542 PRVM_G_FLOAT( OFS_RETURN ) = (int)video->state;
3544 PRVM_G_FLOAT( OFS_RETURN ) = 0;
3548 ========================
3551 void cin_restart(string name)
3552 ========================
3554 void VM_cin_restart( void )
3559 VM_SAFEPARMCOUNT( 1, VM_cin_restart );
3561 name = PRVM_G_STRING( OFS_PARM0 );
3562 VM_CheckEmptyString( name );
3564 video = CL_GetVideoByName( name );
3566 CL_RestartVideo( video );
3570 ========================
3572 ========================
3574 void VM_Gecko_Init( void ) {
3575 // the prog struct is memset to 0 by Initprog? [12/6/2007 Black]
3576 // FIXME: remove the other _Init functions then, too? [12/6/2007 Black]
3580 ========================
3582 ========================
3584 void VM_Gecko_Destroy( void ) {
3586 for( i = 0 ; i < PRVM_MAX_GECKOINSTANCES ; i++ ) {
3587 clgecko_t **instance = &prog->opengeckoinstances[ i ];
3589 CL_Gecko_DestroyBrowser( *instance );
3596 ========================
3599 float[bool] gecko_create( string name )
3600 ========================
3602 void VM_gecko_create( void ) {
3605 clgecko_t *instance;
3607 VM_SAFEPARMCOUNT( 1, VM_gecko_create );
3609 name = PRVM_G_STRING( OFS_PARM0 );
3610 VM_CheckEmptyString( name );
3612 // find an empty slot for this gecko browser..
3613 for( i = 0 ; i < PRVM_MAX_GECKOINSTANCES ; i++ ) {
3614 if( prog->opengeckoinstances[ i ] == NULL ) {
3618 if( i == PRVM_MAX_GECKOINSTANCES ) {
3619 VM_Warning("VM_gecko_create: %s ran out of gecko handles (%i)\n", PRVM_NAME, PRVM_MAX_GECKOINSTANCES);
3620 PRVM_G_FLOAT( OFS_RETURN ) = 0;
3624 instance = prog->opengeckoinstances[ i ] = CL_Gecko_CreateBrowser( name, PRVM_GetProgNr() );
3626 // TODO: error handling [12/3/2007 Black]
3627 PRVM_G_FLOAT( OFS_RETURN ) = 0;
3630 PRVM_G_FLOAT( OFS_RETURN ) = 1;
3634 ========================
3637 void gecko_destroy( string name )
3638 ========================
3640 void VM_gecko_destroy( void ) {
3642 clgecko_t *instance;
3644 VM_SAFEPARMCOUNT( 1, VM_gecko_destroy );
3646 name = PRVM_G_STRING( OFS_PARM0 );
3647 VM_CheckEmptyString( name );
3648 instance = CL_Gecko_FindBrowser( name );
3652 CL_Gecko_DestroyBrowser( instance );
3656 ========================
3659 void gecko_navigate( string name, string URI )
3660 ========================
3662 void VM_gecko_navigate( void ) {
3665 clgecko_t *instance;
3667 VM_SAFEPARMCOUNT( 2, VM_gecko_navigate );
3669 name = PRVM_G_STRING( OFS_PARM0 );
3670 URI = PRVM_G_STRING( OFS_PARM1 );
3671 VM_CheckEmptyString( name );
3672 VM_CheckEmptyString( URI );
3674 instance = CL_Gecko_FindBrowser( name );
3678 CL_Gecko_NavigateToURI( instance, URI );
3682 ========================
3685 float[bool] gecko_keyevent( string name, float key, float eventtype )
3686 ========================
3688 void VM_gecko_keyevent( void ) {
3691 clgecko_buttoneventtype_t eventtype;
3692 clgecko_t *instance;
3694 VM_SAFEPARMCOUNT( 3, VM_gecko_keyevent );
3696 name = PRVM_G_STRING( OFS_PARM0 );
3697 VM_CheckEmptyString( name );
3698 key = (unsigned int) PRVM_G_FLOAT( OFS_PARM1 );
3699 switch( (unsigned int) PRVM_G_FLOAT( OFS_PARM2 ) ) {
3701 eventtype = CLG_BET_DOWN;
3704 eventtype = CLG_BET_UP;
3707 eventtype = CLG_BET_PRESS;
3710 eventtype = CLG_BET_DOUBLECLICK;
3713 // TODO: console printf? [12/3/2007 Black]
3714 PRVM_G_FLOAT( OFS_RETURN ) = 0;
3718 instance = CL_Gecko_FindBrowser( name );
3720 PRVM_G_FLOAT( OFS_RETURN ) = 0;
3724 PRVM_G_FLOAT( OFS_RETURN ) = (CL_Gecko_Event_Key( instance, (keynum_t) key, eventtype ) == true);
3728 ========================
3731 void gecko_mousemove( string name, float x, float y )
3732 ========================
3734 void VM_gecko_movemouse( void ) {
3737 clgecko_t *instance;
3739 VM_SAFEPARMCOUNT( 3, VM_gecko_movemouse );
3741 name = PRVM_G_STRING( OFS_PARM0 );
3742 VM_CheckEmptyString( name );
3743 x = PRVM_G_FLOAT( OFS_PARM1 );
3744 y = PRVM_G_FLOAT( OFS_PARM2 );
3746 instance = CL_Gecko_FindBrowser( name );
3750 CL_Gecko_Event_CursorMove( instance, x, y );
3755 ========================
3758 void gecko_resize( string name, float w, float h )
3759 ========================
3761 void VM_gecko_resize( void ) {
3764 clgecko_t *instance;
3766 VM_SAFEPARMCOUNT( 3, VM_gecko_movemouse );
3768 name = PRVM_G_STRING( OFS_PARM0 );
3769 VM_CheckEmptyString( name );
3770 w = PRVM_G_FLOAT( OFS_PARM1 );
3771 h = PRVM_G_FLOAT( OFS_PARM2 );
3773 instance = CL_Gecko_FindBrowser( name );
3777 CL_Gecko_Resize( instance, (int) w, (int) h );
3782 ========================
3783 VM_gecko_get_texture_extent
3785 vector gecko_get_texture_extent( string name )
3786 ========================
3788 void VM_gecko_get_texture_extent( void ) {
3790 clgecko_t *instance;
3792 VM_SAFEPARMCOUNT( 1, VM_gecko_movemouse );
3794 name = PRVM_G_STRING( OFS_PARM0 );
3795 VM_CheckEmptyString( name );
3797 PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
3798 instance = CL_Gecko_FindBrowser( name );
3800 PRVM_G_VECTOR(OFS_RETURN)[0] = 0;
3801 PRVM_G_VECTOR(OFS_RETURN)[1] = 0;
3804 CL_Gecko_GetTextureExtent( instance,
3805 PRVM_G_VECTOR(OFS_RETURN), PRVM_G_VECTOR(OFS_RETURN)+1 );
3814 Writes new values for v_forward, v_up, and v_right based on angles
3815 void makevectors(vector angle)
3818 void VM_makevectors (void)
3820 prvm_eval_t *valforward, *valright, *valup;
3821 valforward = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_forward);
3822 valright = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_right);
3823 valup = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_up);
3824 if (!valforward || !valright || !valup)
3826 VM_Warning("makevectors: could not find v_forward, v_right, or v_up global variables\n");
3829 VM_SAFEPARMCOUNT(1, VM_makevectors);
3830 AngleVectors (PRVM_G_VECTOR(OFS_PARM0), valforward->vector, valright->vector, valup->vector);
3837 Writes new values for v_forward, v_up, and v_right based on the given forward vector
3838 vectorvectors(vector)
3841 void VM_vectorvectors (void)
3843 prvm_eval_t *valforward, *valright, *valup;
3844 valforward = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_forward);
3845 valright = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_right);
3846 valup = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_up);
3847 if (!valforward || !valright || !valup)
3849 VM_Warning("vectorvectors: could not find v_forward, v_right, or v_up global variables\n");
3852 VM_SAFEPARMCOUNT(1, VM_vectorvectors);
3853 VectorNormalize2(PRVM_G_VECTOR(OFS_PARM0), valforward->vector);
3854 VectorVectors(valforward->vector, valright->vector, valup->vector);
3858 ========================
3861 void drawline(float width, vector pos1, vector pos2, vector rgb, float alpha, float flags)
3862 ========================
3864 void VM_drawline (void)
3866 float *c1, *c2, *rgb;
3868 unsigned char flags;
3870 VM_SAFEPARMCOUNT(6, VM_drawline);
3871 width = PRVM_G_FLOAT(OFS_PARM0);
3872 c1 = PRVM_G_VECTOR(OFS_PARM1);
3873 c2 = PRVM_G_VECTOR(OFS_PARM2);
3874 rgb = PRVM_G_VECTOR(OFS_PARM3);
3875 alpha = PRVM_G_FLOAT(OFS_PARM4);
3876 flags = (int)PRVM_G_FLOAT(OFS_PARM5);
3877 DrawQ_Line(width, c1[0], c1[1], c2[0], c2[1], rgb[0], rgb[1], rgb[2], alpha, flags);
3880 // float(float number, float quantity) bitshift (EXT_BITSHIFT)
3881 void VM_bitshift (void)
3884 VM_SAFEPARMCOUNT(2, VM_bitshift);
3886 n1 = (int)fabs((float)((int)PRVM_G_FLOAT(OFS_PARM0)));
3887 n2 = (int)PRVM_G_FLOAT(OFS_PARM1);
3889 PRVM_G_FLOAT(OFS_RETURN) = n1;
3892 PRVM_G_FLOAT(OFS_RETURN) = (n1 >> -n2);
3894 PRVM_G_FLOAT(OFS_RETURN) = (n1 << n2);
3897 ////////////////////////////////////////
3898 // AltString functions
3899 ////////////////////////////////////////
3902 ========================
3905 float altstr_count(string)
3906 ========================
3908 void VM_altstr_count( void )
3910 const char *altstr, *pos;
3913 VM_SAFEPARMCOUNT( 1, VM_altstr_count );
3915 altstr = PRVM_G_STRING( OFS_PARM0 );
3916 //VM_CheckEmptyString( altstr );
3918 for( count = 0, pos = altstr ; *pos ; pos++ ) {
3919 if( *pos == '\\' ) {
3923 } else if( *pos == '\'' ) {
3928 PRVM_G_FLOAT( OFS_RETURN ) = (float) (count / 2);
3932 ========================
3935 string altstr_prepare(string)
3936 ========================
3938 void VM_altstr_prepare( void )
3941 const char *instr, *in;
3943 char outstr[VM_STRINGTEMP_LENGTH];
3945 VM_SAFEPARMCOUNT( 1, VM_altstr_prepare );
3947 instr = PRVM_G_STRING( OFS_PARM0 );
3949 for( out = outstr, in = instr, size = sizeof(outstr) - 1 ; size && *in ; size--, in++, out++ )
3958 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
3962 ========================
3965 string altstr_get(string, float)
3966 ========================
3968 void VM_altstr_get( void )
3970 const char *altstr, *pos;
3973 char outstr[VM_STRINGTEMP_LENGTH];
3975 VM_SAFEPARMCOUNT( 2, VM_altstr_get );
3977 altstr = PRVM_G_STRING( OFS_PARM0 );
3979 count = (int)PRVM_G_FLOAT( OFS_PARM1 );
3980 count = count * 2 + 1;
3982 for( pos = altstr ; *pos && count ; pos++ )
3983 if( *pos == '\\' ) {
3986 } else if( *pos == '\'' )
3990 PRVM_G_INT( OFS_RETURN ) = 0;
3994 for( out = outstr, size = sizeof(outstr) - 1 ; size && *pos ; size--, pos++, out++ )
3995 if( *pos == '\\' ) {
4000 } else if( *pos == '\'' )
4006 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
4010 ========================
4013 string altstr_set(string altstr, float num, string set)
4014 ========================
4016 void VM_altstr_set( void )
4019 const char *altstr, *str;
4022 char outstr[VM_STRINGTEMP_LENGTH];
4024 VM_SAFEPARMCOUNT( 3, VM_altstr_set );
4026 altstr = PRVM_G_STRING( OFS_PARM0 );
4028 num = (int)PRVM_G_FLOAT( OFS_PARM1 );
4030 str = PRVM_G_STRING( OFS_PARM2 );
4033 for( num = num * 2 + 1, in = altstr; *in && num; *out++ = *in++ )
4038 } else if( *in == '\'' ) {
4043 for( ; *str; *out++ = *str++ );
4044 // now jump over the old content
4046 if( *in == '\'' || (*in == '\\' && !*++in) )
4049 strlcpy(out, in, outstr + sizeof(outstr) - out);
4050 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
4054 ========================
4057 string altstr_ins(string altstr, float num, string set)
4058 ========================
4060 void VM_altstr_ins(void)
4068 char outstr[VM_STRINGTEMP_LENGTH];
4070 VM_SAFEPARMCOUNT(3, VM_altstr_ins);
4072 in = instr = PRVM_G_STRING( OFS_PARM0 );
4073 num = (int)PRVM_G_FLOAT( OFS_PARM1 );
4074 set = setstr = PRVM_G_STRING( OFS_PARM2 );
4077 for( num = num * 2 + 2 ; *in && num > 0 ; *out++ = *in++ )
4082 } else if( *in == '\'' ) {
4087 for( ; *set ; *out++ = *set++ );
4090 strlcpy(out, in, outstr + sizeof(outstr) - out);
4091 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
4095 ////////////////////////////////////////
4096 // BufString functions
4097 ////////////////////////////////////////
4098 //[515]: string buffers support
4100 static size_t stringbuffers_sortlength;
4102 static void BufStr_Expand(prvm_stringbuffer_t *stringbuffer, int strindex)
4104 if (stringbuffer->max_strings <= strindex)
4106 char **oldstrings = stringbuffer->strings;
4107 stringbuffer->max_strings = max(stringbuffer->max_strings * 2, 128);
4108 while (stringbuffer->max_strings <= strindex)
4109 stringbuffer->max_strings *= 2;
4110 stringbuffer->strings = (char **) Mem_Alloc(prog->progs_mempool, stringbuffer->max_strings * sizeof(stringbuffer->strings[0]));
4111 if (stringbuffer->num_strings > 0)
4112 memcpy(stringbuffer->strings, oldstrings, stringbuffer->num_strings * sizeof(stringbuffer->strings[0]));
4114 Mem_Free(oldstrings);
4118 static void BufStr_Shrink(prvm_stringbuffer_t *stringbuffer)
4120 // reduce num_strings if there are empty string slots at the end
4121 while (stringbuffer->num_strings > 0 && stringbuffer->strings[stringbuffer->num_strings - 1] == NULL)
4122 stringbuffer->num_strings--;
4124 // if empty, free the string pointer array
4125 if (stringbuffer->num_strings == 0)
4127 stringbuffer->max_strings = 0;
4128 if (stringbuffer->strings)
4129 Mem_Free(stringbuffer->strings);
4130 stringbuffer->strings = NULL;
4134 static int BufStr_SortStringsUP (const void *in1, const void *in2)
4137 a = *((const char **) in1);
4138 b = *((const char **) in2);
4140 if(!b[0]) return -1;
4141 return strncmp(a, b, stringbuffers_sortlength);
4144 static int BufStr_SortStringsDOWN (const void *in1, const void *in2)
4147 a = *((const char **) in1);
4148 b = *((const char **) in2);
4150 if(!b[0]) return -1;
4151 return strncmp(b, a, stringbuffers_sortlength);
4155 ========================
4157 creates new buffer, and returns it's index, returns -1 if failed
4158 float buf_create(void) = #460;
4159 ========================
4161 void VM_buf_create (void)
4163 prvm_stringbuffer_t *stringbuffer;
4165 VM_SAFEPARMCOUNT(0, VM_buf_create);
4166 stringbuffer = (prvm_stringbuffer_t *) Mem_ExpandableArray_AllocRecord(&prog->stringbuffersarray);
4167 for (i = 0;stringbuffer != Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, i);i++);
4168 stringbuffer->origin = PRVM_AllocationOrigin();
4169 PRVM_G_FLOAT(OFS_RETURN) = i;
4173 ========================
4175 deletes buffer and all strings in it
4176 void buf_del(float bufhandle) = #461;
4177 ========================
4179 void VM_buf_del (void)
4181 prvm_stringbuffer_t *stringbuffer;
4182 VM_SAFEPARMCOUNT(1, VM_buf_del);
4183 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4187 for (i = 0;i < stringbuffer->num_strings;i++)
4188 if (stringbuffer->strings[i])
4189 Mem_Free(stringbuffer->strings[i]);
4190 if (stringbuffer->strings)
4191 Mem_Free(stringbuffer->strings);
4192 if(stringbuffer->origin)
4193 PRVM_Free((char *)stringbuffer->origin);
4194 Mem_ExpandableArray_FreeRecord(&prog->stringbuffersarray, stringbuffer);
4198 VM_Warning("VM_buf_del: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4204 ========================
4206 how many strings are stored in buffer
4207 float buf_getsize(float bufhandle) = #462;
4208 ========================
4210 void VM_buf_getsize (void)
4212 prvm_stringbuffer_t *stringbuffer;
4213 VM_SAFEPARMCOUNT(1, VM_buf_getsize);
4215 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4218 PRVM_G_FLOAT(OFS_RETURN) = -1;
4219 VM_Warning("VM_buf_getsize: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4223 PRVM_G_FLOAT(OFS_RETURN) = stringbuffer->num_strings;
4227 ========================
4229 copy all content from one buffer to another, make sure it exists
4230 void buf_copy(float bufhandle_from, float bufhandle_to) = #463;
4231 ========================
4233 void VM_buf_copy (void)
4235 prvm_stringbuffer_t *srcstringbuffer, *dststringbuffer;
4237 VM_SAFEPARMCOUNT(2, VM_buf_copy);
4239 srcstringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4240 if(!srcstringbuffer)
4242 VM_Warning("VM_buf_copy: invalid source buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4245 i = (int)PRVM_G_FLOAT(OFS_PARM1);
4246 if(i == (int)PRVM_G_FLOAT(OFS_PARM0))
4248 VM_Warning("VM_buf_copy: source == destination (%i) in %s\n", i, PRVM_NAME);
4251 dststringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4252 if(!dststringbuffer)
4254 VM_Warning("VM_buf_copy: invalid destination buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM1), PRVM_NAME);
4258 for (i = 0;i < dststringbuffer->num_strings;i++)
4259 if (dststringbuffer->strings[i])
4260 Mem_Free(dststringbuffer->strings[i]);
4261 if (dststringbuffer->strings)
4262 Mem_Free(dststringbuffer->strings);
4263 *dststringbuffer = *srcstringbuffer;
4264 if (dststringbuffer->max_strings)
4265 dststringbuffer->strings = (char **)Mem_Alloc(prog->progs_mempool, sizeof(dststringbuffer->strings[0]) * dststringbuffer->max_strings);
4267 for (i = 0;i < dststringbuffer->num_strings;i++)
4269 if (srcstringbuffer->strings[i])
4272 stringlen = strlen(srcstringbuffer->strings[i]) + 1;
4273 dststringbuffer->strings[i] = (char *)Mem_Alloc(prog->progs_mempool, stringlen);
4274 memcpy(dststringbuffer->strings[i], srcstringbuffer->strings[i], stringlen);
4280 ========================
4282 sort buffer by beginnings of strings (cmplength defaults it's length)
4283 "backward == TRUE" means that sorting goes upside-down
4284 void buf_sort(float bufhandle, float cmplength, float backward) = #464;
4285 ========================
4287 void VM_buf_sort (void)
4289 prvm_stringbuffer_t *stringbuffer;
4290 VM_SAFEPARMCOUNT(3, VM_buf_sort);
4292 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4295 VM_Warning("VM_buf_sort: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4298 if(stringbuffer->num_strings <= 0)
4300 VM_Warning("VM_buf_sort: tried to sort empty buffer %i in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4303 stringbuffers_sortlength = (int)PRVM_G_FLOAT(OFS_PARM1);
4304 if(stringbuffers_sortlength <= 0)
4305 stringbuffers_sortlength = 0x7FFFFFFF;
4307 if(!PRVM_G_FLOAT(OFS_PARM2))
4308 qsort(stringbuffer->strings, stringbuffer->num_strings, sizeof(char*), BufStr_SortStringsUP);
4310 qsort(stringbuffer->strings, stringbuffer->num_strings, sizeof(char*), BufStr_SortStringsDOWN);
4312 BufStr_Shrink(stringbuffer);
4316 ========================
4318 concantenates all buffer string into one with "glue" separator and returns it as tempstring
4319 string buf_implode(float bufhandle, string glue) = #465;
4320 ========================
4322 void VM_buf_implode (void)
4324 prvm_stringbuffer_t *stringbuffer;
4325 char k[VM_STRINGTEMP_LENGTH];
4329 VM_SAFEPARMCOUNT(2, VM_buf_implode);
4331 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4332 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
4335 VM_Warning("VM_buf_implode: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4338 if(!stringbuffer->num_strings)
4340 sep = PRVM_G_STRING(OFS_PARM1);
4342 for(l = i = 0;i < stringbuffer->num_strings;i++)
4344 if(stringbuffer->strings[i])
4346 l += (i > 0 ? strlen(sep) : 0) + strlen(stringbuffer->strings[i]);
4347 if (l >= sizeof(k) - 1)
4349 strlcat(k, sep, sizeof(k));
4350 strlcat(k, stringbuffer->strings[i], sizeof(k));
4353 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(k);
4357 ========================
4359 get a string from buffer, returns tempstring, dont str_unzone it!
4360 string bufstr_get(float bufhandle, float string_index) = #465;
4361 ========================
4363 void VM_bufstr_get (void)
4365 prvm_stringbuffer_t *stringbuffer;
4367 VM_SAFEPARMCOUNT(2, VM_bufstr_get);
4369 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
4370 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4373 VM_Warning("VM_bufstr_get: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4376 strindex = (int)PRVM_G_FLOAT(OFS_PARM1);
4379 VM_Warning("VM_bufstr_get: invalid string index %i used in %s\n", strindex, PRVM_NAME);
4382 if (strindex < stringbuffer->num_strings && stringbuffer->strings[strindex])
4383 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(stringbuffer->strings[strindex]);
4387 ========================
4389 copies a string into selected slot of buffer
4390 void bufstr_set(float bufhandle, float string_index, string str) = #466;
4391 ========================
4393 void VM_bufstr_set (void)
4396 prvm_stringbuffer_t *stringbuffer;
4399 VM_SAFEPARMCOUNT(3, VM_bufstr_set);
4401 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4404 VM_Warning("VM_bufstr_set: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4407 strindex = (int)PRVM_G_FLOAT(OFS_PARM1);
4408 if(strindex < 0 || strindex >= 1000000) // huge number of strings
4410 VM_Warning("VM_bufstr_set: invalid string index %i used in %s\n", strindex, PRVM_NAME);
4414 BufStr_Expand(stringbuffer, strindex);
4415 stringbuffer->num_strings = max(stringbuffer->num_strings, strindex + 1);
4417 if(stringbuffer->strings[strindex])
4418 Mem_Free(stringbuffer->strings[strindex]);
4419 stringbuffer->strings[strindex] = NULL;
4421 news = PRVM_G_STRING(OFS_PARM2);
4422 if (news && news[0])
4424 size_t alloclen = strlen(news) + 1;
4425 stringbuffer->strings[strindex] = (char *)Mem_Alloc(prog->progs_mempool, alloclen);
4426 memcpy(stringbuffer->strings[strindex], news, alloclen);
4429 BufStr_Shrink(stringbuffer);
4433 ========================
4435 adds string to buffer in first free slot and returns its index
4436 "order == TRUE" means that string will be added after last "full" slot
4437 float bufstr_add(float bufhandle, string str, float order) = #467;
4438 ========================
4440 void VM_bufstr_add (void)
4442 int order, strindex;
4443 prvm_stringbuffer_t *stringbuffer;
4447 VM_SAFEPARMCOUNT(3, VM_bufstr_add);
4449 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4450 PRVM_G_FLOAT(OFS_RETURN) = -1;
4453 VM_Warning("VM_bufstr_add: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4456 string = PRVM_G_STRING(OFS_PARM1);
4457 if(!string || !string[0])
4459 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);
4462 order = (int)PRVM_G_FLOAT(OFS_PARM2);
4464 strindex = stringbuffer->num_strings;
4466 for (strindex = 0;strindex < stringbuffer->num_strings;strindex++)
4467 if (stringbuffer->strings[strindex] == NULL)
4470 BufStr_Expand(stringbuffer, strindex);
4472 stringbuffer->num_strings = max(stringbuffer->num_strings, strindex + 1);
4473 alloclen = strlen(string) + 1;
4474 stringbuffer->strings[strindex] = (char *)Mem_Alloc(prog->progs_mempool, alloclen);
4475 memcpy(stringbuffer->strings[strindex], string, alloclen);
4477 PRVM_G_FLOAT(OFS_RETURN) = strindex;
4481 ========================
4483 delete string from buffer
4484 void bufstr_free(float bufhandle, float string_index) = #468;
4485 ========================
4487 void VM_bufstr_free (void)
4490 prvm_stringbuffer_t *stringbuffer;
4491 VM_SAFEPARMCOUNT(2, VM_bufstr_free);
4493 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4496 VM_Warning("VM_bufstr_free: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4499 i = (int)PRVM_G_FLOAT(OFS_PARM1);
4502 VM_Warning("VM_bufstr_free: invalid string index %i used in %s\n", i, PRVM_NAME);
4506 if (i < stringbuffer->num_strings)
4508 if(stringbuffer->strings[i])
4509 Mem_Free(stringbuffer->strings[i]);
4510 stringbuffer->strings[i] = NULL;
4513 BufStr_Shrink(stringbuffer);
4522 void VM_buf_cvarlist(void)
4525 const char *partial, *antipartial;
4526 size_t len, antilen;
4528 qboolean ispattern, antiispattern;
4530 prvm_stringbuffer_t *stringbuffer;
4531 VM_SAFEPARMCOUNTRANGE(2, 3, VM_buf_cvarlist);
4533 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4536 VM_Warning("VM_bufstr_free: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4540 partial = PRVM_G_STRING(OFS_PARM1);
4544 len = strlen(partial);
4547 antipartial = PRVM_G_STRING(OFS_PARM2);
4553 antilen = strlen(antipartial);
4555 for (n = 0;n < stringbuffer->num_strings;n++)
4556 if (stringbuffer->strings[n])
4557 Mem_Free(stringbuffer->strings[n]);
4558 if (stringbuffer->strings)
4559 Mem_Free(stringbuffer->strings);
4560 stringbuffer->strings = NULL;
4562 ispattern = partial && (strchr(partial, '*') || strchr(partial, '?'));
4563 antiispattern = antipartial && (strchr(antipartial, '*') || strchr(antipartial, '?'));
4566 for(cvar = cvar_vars; cvar; cvar = cvar->next)
4568 if(len && (ispattern ? !matchpattern_with_separator(cvar->name, partial, false, "", false) : strncmp(partial, cvar->name, len)))
4571 if(antilen && (antiispattern ? matchpattern_with_separator(cvar->name, antipartial, false, "", false) : !strncmp(antipartial, cvar->name, antilen)))
4577 stringbuffer->max_strings = stringbuffer->num_strings = n;
4578 if (stringbuffer->max_strings)
4579 stringbuffer->strings = (char **)Mem_Alloc(prog->progs_mempool, sizeof(stringbuffer->strings[0]) * stringbuffer->max_strings);
4582 for(cvar = cvar_vars; cvar; cvar = cvar->next)
4584 if(len && (ispattern ? !matchpattern_with_separator(cvar->name, partial, false, "", false) : strncmp(partial, cvar->name, len)))
4587 if(antilen && (antiispattern ? matchpattern_with_separator(cvar->name, antipartial, false, "", false) : !strncmp(antipartial, cvar->name, antilen)))
4590 alloclen = strlen(cvar->name) + 1;
4591 stringbuffer->strings[n] = (char *)Mem_Alloc(prog->progs_mempool, alloclen);
4592 memcpy(stringbuffer->strings[n], cvar->name, alloclen);
4607 This was a major timewaster in progs, so it was converted to C
4610 void VM_changeyaw (void)
4613 float ideal, current, move, speed;
4615 // this is called (VERY HACKISHLY) by SV_MoveToGoal, so it can not use any
4616 // parameters because they are the parameters to SV_MoveToGoal, not this
4617 //VM_SAFEPARMCOUNT(0, VM_changeyaw);
4619 ent = PRVM_PROG_TO_EDICT(PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict);
4620 if (ent == prog->edicts)
4622 VM_Warning("changeyaw: can not modify world entity\n");
4625 if (ent->priv.server->free)
4627 VM_Warning("changeyaw: can not modify free entity\n");
4630 if (prog->fieldoffsets.angles < 0 || prog->fieldoffsets.ideal_yaw < 0 || prog->fieldoffsets.yaw_speed < 0)
4632 VM_Warning("changeyaw: angles, ideal_yaw, or yaw_speed field(s) not found\n");
4635 current = ANGLEMOD(PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.angles)->vector[1]);
4636 ideal = PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.ideal_yaw)->_float;
4637 speed = PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.yaw_speed)->_float;
4639 if (current == ideal)
4641 move = ideal - current;
4642 if (ideal > current)
4663 PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.angles)->vector[1] = ANGLEMOD (current + move);
4671 void VM_changepitch (void)
4674 float ideal, current, move, speed;
4676 VM_SAFEPARMCOUNT(1, VM_changepitch);
4678 ent = PRVM_G_EDICT(OFS_PARM0);
4679 if (ent == prog->edicts)
4681 VM_Warning("changepitch: can not modify world entity\n");
4684 if (ent->priv.server->free)
4686 VM_Warning("changepitch: can not modify free entity\n");
4689 if (prog->fieldoffsets.angles < 0 || prog->fieldoffsets.idealpitch < 0 || prog->fieldoffsets.pitch_speed < 0)
4691 VM_Warning("changepitch: angles, idealpitch, or pitch_speed field(s) not found\n");
4694 current = ANGLEMOD(PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.angles)->vector[0]);
4695 ideal = PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.idealpitch)->_float;
4696 speed = PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.pitch_speed)->_float;
4698 if (current == ideal)
4700 move = ideal - current;
4701 if (ideal > current)
4722 PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.angles)->vector[0] = ANGLEMOD (current + move);
4726 void VM_uncolorstring (void)
4728 char szNewString[VM_STRINGTEMP_LENGTH];
4729 const char *szString;
4732 VM_SAFEPARMCOUNT(1, VM_uncolorstring);
4733 szString = PRVM_G_STRING(OFS_PARM0);
4734 COM_StringDecolorize(szString, 0, szNewString, sizeof(szNewString), TRUE);
4735 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(szNewString);
4739 // #221 float(string str, string sub[, float startpos]) strstrofs (FTE_STRINGS)
4740 //strstr, without generating a new string. Use in conjunction with FRIK_FILE's substring for more similar strstr.
4741 void VM_strstrofs (void)
4743 const char *instr, *match;
4745 VM_SAFEPARMCOUNTRANGE(2, 3, VM_strstrofs);
4746 instr = PRVM_G_STRING(OFS_PARM0);
4747 match = PRVM_G_STRING(OFS_PARM1);
4748 firstofs = (prog->argc > 2)?(int)PRVM_G_FLOAT(OFS_PARM2):0;
4750 if (firstofs && (firstofs < 0 || firstofs > (int)strlen(instr)))
4752 PRVM_G_FLOAT(OFS_RETURN) = -1;
4756 match = strstr(instr+firstofs, match);
4758 PRVM_G_FLOAT(OFS_RETURN) = -1;
4760 PRVM_G_FLOAT(OFS_RETURN) = match - instr;
4763 //#222 string(string s, float index) str2chr (FTE_STRINGS)
4764 void VM_str2chr (void)
4767 VM_SAFEPARMCOUNT(2, VM_str2chr);
4768 s = PRVM_G_STRING(OFS_PARM0);
4769 if((unsigned)PRVM_G_FLOAT(OFS_PARM1) < strlen(s))
4770 PRVM_G_FLOAT(OFS_RETURN) = (unsigned char)s[(unsigned)PRVM_G_FLOAT(OFS_PARM1)];
4772 PRVM_G_FLOAT(OFS_RETURN) = 0;
4775 //#223 string(float c, ...) chr2str (FTE_STRINGS)
4776 void VM_chr2str (void)
4780 VM_SAFEPARMCOUNTRANGE(0, 8, VM_chr2str);
4781 for(i = 0;i < prog->argc && i < (int)sizeof(t) - 1;i++)
4782 t[i] = (unsigned char)PRVM_G_FLOAT(OFS_PARM0+i*3);
4784 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(t);
4787 static int chrconv_number(int i, int base, int conv)
4812 static int chrconv_punct(int i, int base, int conv)
4830 static int chrchar_alpha(int i, int basec, int baset, int convc, int convt, int charnum)
4832 //convert case and colour seperatly...
4849 baset = 128*((charnum&1) == (convt-5));
4865 return i + basec + baset;
4867 // #224 string(float ccase, float calpha, float cnum, string s, ...) strconv (FTE_STRINGS)
4868 //bulk convert a string. change case or colouring.
4869 void VM_strconv (void)
4871 int ccase, redalpha, rednum, len, i;
4872 unsigned char resbuf[VM_STRINGTEMP_LENGTH];
4873 unsigned char *result = resbuf;
4875 VM_SAFEPARMCOUNTRANGE(3, 8, VM_strconv);
4877 ccase = (int) PRVM_G_FLOAT(OFS_PARM0); //0 same, 1 lower, 2 upper
4878 redalpha = (int) PRVM_G_FLOAT(OFS_PARM1); //0 same, 1 white, 2 red, 5 alternate, 6 alternate-alternate
4879 rednum = (int) PRVM_G_FLOAT(OFS_PARM2); //0 same, 1 white, 2 red, 3 redspecial, 4 whitespecial, 5 alternate, 6 alternate-alternate
4880 VM_VarString(3, (char *) resbuf, sizeof(resbuf));
4881 len = strlen((char *) resbuf);
4883 for (i = 0; i < len; i++, result++) //should this be done backwards?
4885 if (*result >= '0' && *result <= '9') //normal numbers...
4886 *result = chrconv_number(*result, '0', rednum);
4887 else if (*result >= '0'+128 && *result <= '9'+128)
4888 *result = chrconv_number(*result, '0'+128, rednum);
4889 else if (*result >= '0'+128-30 && *result <= '9'+128-30)
4890 *result = chrconv_number(*result, '0'+128-30, rednum);
4891 else if (*result >= '0'-30 && *result <= '9'-30)
4892 *result = chrconv_number(*result, '0'-30, rednum);
4894 else if (*result >= 'a' && *result <= 'z') //normal numbers...
4895 *result = chrchar_alpha(*result, 'a', 0, ccase, redalpha, i);
4896 else if (*result >= 'A' && *result <= 'Z') //normal numbers...
4897 *result = chrchar_alpha(*result, 'A', 0, ccase, redalpha, i);
4898 else if (*result >= 'a'+128 && *result <= 'z'+128) //normal numbers...
4899 *result = chrchar_alpha(*result, 'a', 128, ccase, redalpha, i);
4900 else if (*result >= 'A'+128 && *result <= 'Z'+128) //normal numbers...
4901 *result = chrchar_alpha(*result, 'A', 128, ccase, redalpha, i);
4903 else if ((*result & 127) < 16 || !redalpha) //special chars..
4905 else if (*result < 128)
4906 *result = chrconv_punct(*result, 0, redalpha);
4908 *result = chrconv_punct(*result, 128, redalpha);
4912 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString((char *) resbuf);
4915 // #225 string(float chars, string s, ...) strpad (FTE_STRINGS)
4916 void VM_strpad (void)
4918 char src[VM_STRINGTEMP_LENGTH];
4919 char destbuf[VM_STRINGTEMP_LENGTH];
4921 VM_SAFEPARMCOUNTRANGE(1, 8, VM_strpad);
4922 pad = (int) PRVM_G_FLOAT(OFS_PARM0);
4923 VM_VarString(1, src, sizeof(src));
4925 // note: < 0 = left padding, > 0 = right padding,
4926 // this is reverse logic of printf!
4927 dpsnprintf(destbuf, sizeof(destbuf), "%*s", -pad, src);
4929 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(destbuf);
4932 // #226 string(string info, string key, string value, ...) infoadd (FTE_STRINGS)
4933 //uses qw style \key\value strings
4934 void VM_infoadd (void)
4936 const char *info, *key;
4937 char value[VM_STRINGTEMP_LENGTH];
4938 char temp[VM_STRINGTEMP_LENGTH];
4940 VM_SAFEPARMCOUNTRANGE(2, 8, VM_infoadd);
4941 info = PRVM_G_STRING(OFS_PARM0);
4942 key = PRVM_G_STRING(OFS_PARM1);
4943 VM_VarString(2, value, sizeof(value));
4945 strlcpy(temp, info, VM_STRINGTEMP_LENGTH);
4947 InfoString_SetValue(temp, VM_STRINGTEMP_LENGTH, key, value);
4949 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(temp);
4952 // #227 string(string info, string key) infoget (FTE_STRINGS)
4953 //uses qw style \key\value strings
4954 void VM_infoget (void)
4958 char value[VM_STRINGTEMP_LENGTH];
4960 VM_SAFEPARMCOUNT(2, VM_infoget);
4961 info = PRVM_G_STRING(OFS_PARM0);
4962 key = PRVM_G_STRING(OFS_PARM1);
4964 InfoString_GetValue(info, key, value, VM_STRINGTEMP_LENGTH);
4966 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(value);
4969 //#228 float(string s1, string s2, float len) strncmp (FTE_STRINGS)
4970 // also float(string s1, string s2) strcmp (FRIK_FILE)
4971 void VM_strncmp (void)
4973 const char *s1, *s2;
4974 VM_SAFEPARMCOUNTRANGE(2, 3, VM_strncmp);
4975 s1 = PRVM_G_STRING(OFS_PARM0);
4976 s2 = PRVM_G_STRING(OFS_PARM1);
4979 PRVM_G_FLOAT(OFS_RETURN) = strncmp(s1, s2, (size_t)PRVM_G_FLOAT(OFS_PARM2));
4983 PRVM_G_FLOAT(OFS_RETURN) = strcmp(s1, s2);
4987 // #229 float(string s1, string s2) strcasecmp (FTE_STRINGS)
4988 // #230 float(string s1, string s2, float len) strncasecmp (FTE_STRINGS)
4989 void VM_strncasecmp (void)
4991 const char *s1, *s2;
4992 VM_SAFEPARMCOUNTRANGE(2, 3, VM_strncasecmp);
4993 s1 = PRVM_G_STRING(OFS_PARM0);
4994 s2 = PRVM_G_STRING(OFS_PARM1);
4997 PRVM_G_FLOAT(OFS_RETURN) = strncasecmp(s1, s2, (size_t)PRVM_G_FLOAT(OFS_PARM2));
5001 PRVM_G_FLOAT(OFS_RETURN) = strcasecmp(s1, s2);
5005 // #494 float(float caseinsensitive, string s, ...) crc16
5009 static char s[VM_STRINGTEMP_LENGTH];
5010 VM_SAFEPARMCOUNTRANGE(2, 8, VM_hash);
5011 insensitive = PRVM_G_FLOAT(OFS_PARM0);
5012 VM_VarString(1, s, sizeof(s));
5013 PRVM_G_FLOAT(OFS_RETURN) = (unsigned short) ((insensitive ? CRC_Block_CaseInsensitive : CRC_Block) ((unsigned char *) s, strlen(s)));
5016 void VM_wasfreed (void)
5018 VM_SAFEPARMCOUNT(1, VM_wasfreed);
5019 PRVM_G_FLOAT(OFS_RETURN) = PRVM_G_EDICT(OFS_PARM0)->priv.required->free;
5022 void VM_SetTraceGlobals(const trace_t *trace)
5025 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_allsolid)))
5026 val->_float = trace->allsolid;
5027 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_startsolid)))
5028 val->_float = trace->startsolid;
5029 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_fraction)))
5030 val->_float = trace->fraction;
5031 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_inwater)))
5032 val->_float = trace->inwater;
5033 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_inopen)))
5034 val->_float = trace->inopen;
5035 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_endpos)))
5036 VectorCopy(trace->endpos, val->vector);
5037 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_plane_normal)))
5038 VectorCopy(trace->plane.normal, val->vector);
5039 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_plane_dist)))
5040 val->_float = trace->plane.dist;
5041 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_ent)))
5042 val->edict = PRVM_EDICT_TO_PROG(trace->ent ? trace->ent : prog->edicts);
5043 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dpstartcontents)))
5044 val->_float = trace->startsupercontents;
5045 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dphitcontents)))
5046 val->_float = trace->hitsupercontents;
5047 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dphitq3surfaceflags)))
5048 val->_float = trace->hitq3surfaceflags;
5049 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dphittexturename)))
5050 val->string = trace->hittexture ? PRVM_SetTempString(trace->hittexture->name) : 0;
5053 void VM_ClearTraceGlobals(void)
5055 // clean up all trace globals when leaving the VM (anti-triggerbot safeguard)
5057 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_allsolid)))
5059 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_startsolid)))
5061 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_fraction)))
5063 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_inwater)))
5065 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_inopen)))
5067 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_endpos)))
5068 VectorClear(val->vector);
5069 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_plane_normal)))
5070 VectorClear(val->vector);
5071 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_plane_dist)))
5073 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_ent)))
5074 val->edict = PRVM_EDICT_TO_PROG(prog->edicts);
5075 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dpstartcontents)))
5077 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dphitcontents)))
5079 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dphitq3surfaceflags)))
5081 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dphittexturename)))
5087 void VM_Cmd_Init(void)
5089 // only init the stuff for the current prog
5093 // VM_BufStr_Init();
5096 void VM_Cmd_Reset(void)
5098 CL_PurgeOwner( MENUOWNER );
5100 VM_Files_CloseAll();
5102 // VM_BufStr_ShutDown();
5105 // #510 string(string input, ...) uri_escape (DP_QC_URI_ESCAPE)
5106 // does URI escaping on a string (replace evil stuff by %AB escapes)
5107 void VM_uri_escape (void)
5109 char src[VM_STRINGTEMP_LENGTH];
5110 char dest[VM_STRINGTEMP_LENGTH];
5112 static const char *hex = "0123456789ABCDEF";
5114 VM_SAFEPARMCOUNTRANGE(1, 8, VM_uri_escape);
5115 VM_VarString(0, src, sizeof(src));
5117 for(p = src, q = dest; *p && q < dest + sizeof(dest) - 3; ++p)
5119 if((*p >= 'A' && *p <= 'Z')
5120 || (*p >= 'a' && *p <= 'z')
5121 || (*p >= '0' && *p <= '9')
5122 || (*p == '-') || (*p == '_') || (*p == '.')
5123 || (*p == '!') || (*p == '~') || (*p == '*')
5124 || (*p == '\'') || (*p == '(') || (*p == ')'))
5129 *q++ = hex[(*(unsigned char *)p >> 4) & 0xF];
5130 *q++ = hex[ *(unsigned char *)p & 0xF];
5135 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(dest);
5138 // #510 string(string input, ...) uri_unescape (DP_QC_URI_ESCAPE)
5139 // does URI unescaping on a string (get back the evil stuff)
5140 void VM_uri_unescape (void)
5142 char src[VM_STRINGTEMP_LENGTH];
5143 char dest[VM_STRINGTEMP_LENGTH];
5147 VM_SAFEPARMCOUNTRANGE(1, 8, VM_uri_unescape);
5148 VM_VarString(0, src, sizeof(src));
5150 for(p = src, q = dest; *p; ) // no need to check size, because unescape can't expand
5154 if(p[1] >= '0' && p[1] <= '9')
5156 else if(p[1] >= 'a' && p[1] <= 'f')
5157 hi = p[1] - 'a' + 10;
5158 else if(p[1] >= 'A' && p[1] <= 'F')
5159 hi = p[1] - 'A' + 10;
5162 if(p[2] >= '0' && p[2] <= '9')
5164 else if(p[2] >= 'a' && p[2] <= 'f')
5165 lo = p[2] - 'a' + 10;
5166 else if(p[2] >= 'A' && p[2] <= 'F')
5167 lo = p[2] - 'A' + 10;
5170 if(hi != 0 || lo != 0) // don't unescape NUL bytes
5171 *q++ = (char) (hi * 0x10 + lo);
5182 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(dest);
5185 // #502 string(string filename) whichpack (DP_QC_WHICHPACK)
5186 // returns the name of the pack containing a file, or "" if it is not in any pack (but local or non-existant)
5187 void VM_whichpack (void)
5189 const char *fn, *pack;
5191 VM_SAFEPARMCOUNT(1, VM_whichpack);
5192 fn = PRVM_G_STRING(OFS_PARM0);
5193 pack = FS_WhichPack(fn);
5195 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(pack ? pack : "");
5203 char buffer[MAX_INPUTLINE];
5207 static void uri_to_string_callback(int status, size_t length_received, unsigned char *buffer, void *cbdata)
5209 uri_to_prog_t *handle = (uri_to_prog_t *) cbdata;
5211 if(!PRVM_ProgLoaded(handle->prognr))
5213 // curl reply came too late... so just drop it
5218 PRVM_SetProg(handle->prognr);
5220 if((prog->starttime == handle->starttime) && (prog->funcoffsets.URI_Get_Callback))
5222 if(length_received >= sizeof(handle->buffer))
5223 length_received = sizeof(handle->buffer) - 1;
5224 handle->buffer[length_received] = 0;
5226 PRVM_G_FLOAT(OFS_PARM0) = handle->id;
5227 PRVM_G_FLOAT(OFS_PARM1) = status;
5228 PRVM_G_INT(OFS_PARM2) = PRVM_SetTempString(handle->buffer);
5229 PRVM_ExecuteProgram(prog->funcoffsets.URI_Get_Callback, "QC function URI_Get_Callback is missing");
5236 // 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
5237 // 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
5238 void VM_uri_get (void)
5243 uri_to_prog_t *handle;
5245 if(!prog->funcoffsets.URI_Get_Callback)
5246 PRVM_ERROR("uri_get called by %s without URI_Get_Callback defined", PRVM_NAME);
5248 VM_SAFEPARMCOUNT(2, VM_uri_get);
5250 url = PRVM_G_STRING(OFS_PARM0);
5251 id = PRVM_G_FLOAT(OFS_PARM1);
5252 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!
5254 handle->prognr = PRVM_GetProgNr();
5255 handle->starttime = prog->starttime;
5257 ret = Curl_Begin_ToMemory(url, (unsigned char *) handle->buffer, sizeof(handle->buffer), uri_to_string_callback, handle);
5260 PRVM_G_INT(OFS_RETURN) = 1;
5265 PRVM_G_INT(OFS_RETURN) = 0;
5269 void VM_netaddress_resolve (void)
5272 char normalized[128];
5274 lhnetaddress_t addr;
5276 VM_SAFEPARMCOUNTRANGE(1, 2, VM_netaddress_resolve);
5278 ip = PRVM_G_STRING(OFS_PARM0);
5281 port = (int) PRVM_G_FLOAT(OFS_PARM1);
5283 if(LHNETADDRESS_FromString(&addr, ip, port) && LHNETADDRESS_ToString(&addr, normalized, sizeof(normalized), prog->argc > 1))
5284 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(normalized);
5286 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString("");
5289 //string(void) getextresponse = #624; // returns the next extResponse packet that was sent to this client
5290 void VM_getextresponse (void)
5292 VM_SAFEPARMCOUNT(0,VM_argv);
5294 if (net_extresponse_count <= 0)
5295 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
5299 --net_extresponse_count;
5300 first = (net_extresponse_last + NET_EXTRESPONSE_MAX - net_extresponse_count) % NET_EXTRESPONSE_MAX;
5301 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(net_extresponse[first]);
5309 callfunction(...,string function_name)
5313 mfunction_t *PRVM_ED_FindFunction (const char *name);
5314 void VM_callfunction(void)
5319 VM_SAFEPARMCOUNTRANGE(1, 8, VM_callfunction);
5321 s = PRVM_G_STRING(OFS_PARM0+(prog->argc - 1)*3);
5323 VM_CheckEmptyString(s);
5325 func = PRVM_ED_FindFunction(s);
5328 PRVM_ERROR("VM_callfunciton: function %s not found !", s);
5329 else if (func->first_statement < 0)
5331 // negative statements are built in functions
5332 int builtinnumber = -func->first_statement;
5333 prog->xfunction->builtinsprofile++;
5334 if (builtinnumber < prog->numbuiltins && prog->builtins[builtinnumber])
5335 prog->builtins[builtinnumber]();
5337 PRVM_ERROR("No such builtin #%i in %s; most likely cause: outdated engine build. Try updating!", builtinnumber, PRVM_NAME);
5339 else if(func - prog->functions > 0)
5342 PRVM_ExecuteProgram(func - prog->functions,"");
5351 float isfunction(string function_name)
5354 mfunction_t *PRVM_ED_FindFunction (const char *name);
5355 void VM_isfunction(void)
5360 VM_SAFEPARMCOUNT(1, VM_isfunction);
5362 s = PRVM_G_STRING(OFS_PARM0);
5364 VM_CheckEmptyString(s);
5366 func = PRVM_ED_FindFunction(s);
5369 PRVM_G_FLOAT(OFS_RETURN) = false;
5371 PRVM_G_FLOAT(OFS_RETURN) = true;