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
15 extern cvar_t prvm_backtraceforwarnings;
17 // LordHavoc: changed this to NOT use a return statement, so that it can be used in functions that must return a value
18 void VM_Warning(const char *fmt, ...)
21 char msg[MAX_INPUTLINE];
22 static double recursive = -1;
25 dpvsnprintf(msg,sizeof(msg),fmt,argptr);
30 // TODO: either add a cvar/cmd to control the state dumping or replace some of the calls with Con_Printf [9/13/2006 Black]
31 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
40 //============================================================================
43 // TODO DONE: move vm_files and vm_fssearchlist to prvm_prog_t struct
44 // TODO: move vm_files and vm_fssearchlist back [9/13/2006 Black]
45 // TODO: (move vm_files and vm_fssearchlist to prvm_prog_t struct again) [2007-01-23 LordHavoc]
46 // TODO: will this war ever end? [2007-01-23 LordHavoc]
48 void VM_CheckEmptyString (const char *s)
50 if (ISWHITESPACE(s[0]))
51 PRVM_ERROR ("%s: Bad string", PRVM_NAME);
54 //============================================================================
57 void VM_VarString(int first, char *out, int outlength)
63 outend = out + outlength - 1;
64 for (i = first;i < prog->argc && out < outend;i++)
66 s = PRVM_G_STRING((OFS_PARM0+i*3));
67 while (out < outend && *s)
77 returns true if the extension is supported by the server
79 checkextension(extensionname)
83 // kind of helper function
84 static qboolean checkextension(const char *name)
88 len = (int)strlen(name);
90 for (e = prog->extensionstring;*e;e++)
97 while (*e && *e != ' ')
99 if ((e - start) == len && !strncasecmp(start, name, len))
105 void VM_checkextension (void)
107 VM_SAFEPARMCOUNT(1,VM_checkextension);
109 PRVM_G_FLOAT(OFS_RETURN) = checkextension(PRVM_G_STRING(OFS_PARM0));
116 This is a TERMINAL error, which will kill off the entire prog.
125 char string[VM_STRINGTEMP_LENGTH];
127 VM_VarString(0, string, sizeof(string));
128 Con_Printf("======%s ERROR in %s:\n%s\n", PRVM_NAME, PRVM_GetString(prog->xfunction->s_name), string);
129 if (prog->globaloffsets.self >= 0)
131 ed = PRVM_PROG_TO_EDICT(PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict);
132 PRVM_ED_Print(ed, NULL);
135 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);
142 Dumps out self, then an error message. The program is aborted and self is
143 removed, but the level can continue.
148 void VM_objerror (void)
151 char string[VM_STRINGTEMP_LENGTH];
153 VM_VarString(0, string, sizeof(string));
154 Con_Printf("======OBJECT ERROR======\n"); // , PRVM_NAME, PRVM_GetString(prog->xfunction->s_name), string); // or include them? FIXME
155 if (prog->globaloffsets.self >= 0)
157 ed = PRVM_PROG_TO_EDICT(PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict);
158 PRVM_ED_Print(ed, NULL);
163 // objerror has to display the object fields -> else call
164 PRVM_ERROR ("VM_objecterror: self not defined !");
165 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);
179 char string[VM_STRINGTEMP_LENGTH];
181 VM_VarString(0, string, sizeof(string));
189 broadcast print to everyone on server
194 void VM_bprint (void)
196 char string[VM_STRINGTEMP_LENGTH];
200 VM_Warning("VM_bprint: game is not server(%s) !\n", PRVM_NAME);
204 VM_VarString(0, string, sizeof(string));
205 SV_BroadcastPrint(string);
210 VM_sprint (menu & client but only if server.active == true)
212 single print to a specific client
214 sprint(float clientnum,...[string])
217 void VM_sprint (void)
221 char string[VM_STRINGTEMP_LENGTH];
223 VM_SAFEPARMCOUNTRANGE(1, 8, VM_sprint);
225 //find client for this entity
226 clientnum = (int)PRVM_G_FLOAT(OFS_PARM0);
227 if (!sv.active || clientnum < 0 || clientnum >= svs.maxclients || !svs.clients[clientnum].active)
229 VM_Warning("VM_sprint: %s: invalid client or server is not active !\n", PRVM_NAME);
233 client = svs.clients + clientnum;
234 if (!client->netconnection)
237 VM_VarString(1, string, sizeof(string));
238 MSG_WriteChar(&client->netconnection->message,svc_print);
239 MSG_WriteString(&client->netconnection->message, string);
246 single print to the screen
251 void VM_centerprint (void)
253 char string[VM_STRINGTEMP_LENGTH];
255 VM_SAFEPARMCOUNTRANGE(1, 8, VM_centerprint);
256 VM_VarString(0, string, sizeof(string));
257 SCR_CenterPrint(string);
264 vector normalize(vector)
267 void VM_normalize (void)
273 VM_SAFEPARMCOUNT(1,VM_normalize);
275 value1 = PRVM_G_VECTOR(OFS_PARM0);
277 f = VectorLength2(value1);
281 VectorScale(value1, f, newvalue);
284 VectorClear(newvalue);
286 VectorCopy (newvalue, PRVM_G_VECTOR(OFS_RETURN));
298 VM_SAFEPARMCOUNT(1,VM_vlen);
299 PRVM_G_FLOAT(OFS_RETURN) = VectorLength(PRVM_G_VECTOR(OFS_PARM0));
306 float vectoyaw(vector)
309 void VM_vectoyaw (void)
314 VM_SAFEPARMCOUNT(1,VM_vectoyaw);
316 value1 = PRVM_G_VECTOR(OFS_PARM0);
318 if (value1[1] == 0 && value1[0] == 0)
322 yaw = (int) (atan2(value1[1], value1[0]) * 180 / M_PI);
327 PRVM_G_FLOAT(OFS_RETURN) = yaw;
335 vector vectoangles(vector[, vector])
338 void VM_vectoangles (void)
340 VM_SAFEPARMCOUNTRANGE(1, 2,VM_vectoangles);
342 AnglesFromVectors(PRVM_G_VECTOR(OFS_RETURN), PRVM_G_VECTOR(OFS_PARM0), prog->argc >= 2 ? PRVM_G_VECTOR(OFS_PARM1) : NULL, true);
349 Returns a number from 0<= num < 1
354 void VM_random (void)
356 VM_SAFEPARMCOUNT(0,VM_random);
358 PRVM_G_FLOAT(OFS_RETURN) = lhrandom(0, 1);
365 localsound(string sample)
368 void VM_localsound(void)
372 VM_SAFEPARMCOUNT(1,VM_localsound);
374 s = PRVM_G_STRING(OFS_PARM0);
376 if(!S_LocalSound (s))
378 PRVM_G_FLOAT(OFS_RETURN) = -4;
379 VM_Warning("VM_localsound: Failed to play %s for %s !\n", s, PRVM_NAME);
383 PRVM_G_FLOAT(OFS_RETURN) = 1;
395 PRVM_ERROR ("%s: break statement", PRVM_NAME);
398 //============================================================================
404 Sends text over to the client's execution buffer
406 [localcmd (string, ...) or]
410 void VM_localcmd (void)
412 char string[VM_STRINGTEMP_LENGTH];
413 VM_SAFEPARMCOUNTRANGE(1, 8, VM_localcmd);
414 VM_VarString(0, string, sizeof(string));
415 Cbuf_AddText(string);
418 static qboolean PRVM_Cvar_ReadOk(const char *string)
421 cvar = Cvar_FindVar(string);
422 return ((cvar) && ((cvar->flags & CVAR_PRIVATE) == 0));
434 char string[VM_STRINGTEMP_LENGTH];
435 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar);
436 VM_VarString(0, string, sizeof(string));
437 VM_CheckEmptyString(string);
438 PRVM_G_FLOAT(OFS_RETURN) = PRVM_Cvar_ReadOk(string) ? Cvar_VariableValue(string) : 0;
445 float cvar_type (string)
446 float CVAR_TYPEFLAG_EXISTS = 1;
447 float CVAR_TYPEFLAG_SAVED = 2;
448 float CVAR_TYPEFLAG_PRIVATE = 4;
449 float CVAR_TYPEFLAG_ENGINE = 8;
450 float CVAR_TYPEFLAG_HASDESCRIPTION = 16;
451 float CVAR_TYPEFLAG_READONLY = 32;
454 void VM_cvar_type (void)
456 char string[VM_STRINGTEMP_LENGTH];
460 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar);
461 VM_VarString(0, string, sizeof(string));
462 VM_CheckEmptyString(string);
463 cvar = Cvar_FindVar(string);
468 PRVM_G_FLOAT(OFS_RETURN) = 0;
469 return; // CVAR_TYPE_NONE
472 ret = 1; // CVAR_EXISTS
473 if(cvar->flags & CVAR_SAVE)
474 ret |= 2; // CVAR_TYPE_SAVED
475 if(cvar->flags & CVAR_PRIVATE)
476 ret |= 4; // CVAR_TYPE_PRIVATE
477 if(!(cvar->flags & CVAR_ALLOCATED))
478 ret |= 8; // CVAR_TYPE_ENGINE
479 if(cvar->description != cvar_dummy_description)
480 ret |= 16; // CVAR_TYPE_HASDESCRIPTION
481 if(cvar->flags & CVAR_READONLY)
482 ret |= 32; // CVAR_TYPE_READONLY
484 PRVM_G_FLOAT(OFS_RETURN) = ret;
491 const string VM_cvar_string (string, ...)
494 void VM_cvar_string(void)
496 char string[VM_STRINGTEMP_LENGTH];
497 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar_string);
498 VM_VarString(0, string, sizeof(string));
499 VM_CheckEmptyString(string);
500 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(PRVM_Cvar_ReadOk(string) ? Cvar_VariableString(string) : "");
505 ========================
508 const string VM_cvar_defstring (string, ...)
509 ========================
511 void VM_cvar_defstring (void)
513 char string[VM_STRINGTEMP_LENGTH];
514 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar_defstring);
515 VM_VarString(0, string, sizeof(string));
516 VM_CheckEmptyString(string);
517 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(Cvar_VariableDefString(string));
521 ========================
524 const string VM_cvar_description (string, ...)
525 ========================
527 void VM_cvar_description (void)
529 char string[VM_STRINGTEMP_LENGTH];
530 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar_description);
531 VM_VarString(0, string, sizeof(string));
532 VM_CheckEmptyString(string);
533 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(Cvar_VariableDescription(string));
539 void cvar_set (string,string, ...)
542 void VM_cvar_set (void)
545 char string[VM_STRINGTEMP_LENGTH];
546 VM_SAFEPARMCOUNTRANGE(2,8,VM_cvar_set);
547 VM_VarString(1, string, sizeof(string));
548 name = PRVM_G_STRING(OFS_PARM0);
549 VM_CheckEmptyString(name);
550 Cvar_Set(name, string);
560 void VM_dprint (void)
562 char string[VM_STRINGTEMP_LENGTH];
563 VM_SAFEPARMCOUNTRANGE(1, 8, VM_dprint);
564 if (developer.integer)
566 VM_VarString(0, string, sizeof(string));
568 Con_Printf("%s", string);
570 Con_Printf("%s: %s", PRVM_NAME, string);
588 VM_SAFEPARMCOUNT(1, VM_ftos);
590 v = PRVM_G_FLOAT(OFS_PARM0);
592 if ((float)((int)v) == v)
593 dpsnprintf(s, sizeof(s), "%i", (int)v);
595 dpsnprintf(s, sizeof(s), "%f", v);
596 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(s);
611 VM_SAFEPARMCOUNT(1,VM_fabs);
613 v = PRVM_G_FLOAT(OFS_PARM0);
614 PRVM_G_FLOAT(OFS_RETURN) = fabs(v);
629 VM_SAFEPARMCOUNT(1,VM_vtos);
631 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]);
632 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(s);
647 VM_SAFEPARMCOUNT(1, VM_etos);
649 dpsnprintf (s, sizeof(s), "entity %i", PRVM_G_EDICTNUM(OFS_PARM0));
650 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(s);
657 float stof(...[string])
662 char string[VM_STRINGTEMP_LENGTH];
663 VM_SAFEPARMCOUNTRANGE(1, 8, VM_stof);
664 VM_VarString(0, string, sizeof(string));
665 PRVM_G_FLOAT(OFS_RETURN) = atof(string);
669 ========================
673 ========================
677 VM_SAFEPARMCOUNT(1, VM_itof);
678 PRVM_G_FLOAT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
682 ========================
685 entity ftoe(float num)
686 ========================
691 VM_SAFEPARMCOUNT(1, VM_ftoe);
693 ent = (int)PRVM_G_FLOAT(OFS_PARM0);
694 if (ent < 0 || ent >= MAX_EDICTS || PRVM_PROG_TO_EDICT(ent)->priv.required->free)
695 ent = 0; // return world instead of a free or invalid entity
697 PRVM_G_INT(OFS_RETURN) = ent;
701 ========================
704 float etof(entity ent)
705 ========================
709 VM_SAFEPARMCOUNT(1, VM_etof);
710 PRVM_G_FLOAT(OFS_RETURN) = PRVM_G_EDICTNUM(OFS_PARM0);
717 string strftime(float uselocaltime, string[, string ...])
720 void VM_strftime(void)
729 char fmt[VM_STRINGTEMP_LENGTH];
730 char result[VM_STRINGTEMP_LENGTH];
731 VM_SAFEPARMCOUNTRANGE(2, 8, VM_strftime);
732 VM_VarString(1, fmt, sizeof(fmt));
735 if (PRVM_G_FLOAT(OFS_PARM0))
736 tmresult = localtime_s(&tm, &t);
738 tmresult = gmtime_s(&tm, &t);
741 if (PRVM_G_FLOAT(OFS_PARM0))
748 PRVM_G_INT(OFS_RETURN) = 0;
752 strftime(result, sizeof(result), fmt, &tm);
754 strftime(result, sizeof(result), fmt, tm);
756 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(result);
770 VM_SAFEPARMCOUNT(0, VM_spawn);
771 prog->xfunction->builtinsprofile += 20;
772 ed = PRVM_ED_Alloc();
784 void VM_remove (void)
787 prog->xfunction->builtinsprofile += 20;
789 VM_SAFEPARMCOUNT(1, VM_remove);
791 ed = PRVM_G_EDICT(OFS_PARM0);
792 if( PRVM_NUM_FOR_EDICT(ed) <= prog->reserved_edicts )
794 if (developer.integer >= 1)
795 VM_Warning( "VM_remove: tried to remove the null entity or a reserved entity!\n" );
797 else if( ed->priv.required->free )
799 if (developer.integer >= 1)
800 VM_Warning( "VM_remove: tried to remove an already freed entity!\n" );
810 entity find(entity start, .string field, string match)
821 VM_SAFEPARMCOUNT(3,VM_find);
823 e = PRVM_G_EDICTNUM(OFS_PARM0);
824 f = PRVM_G_INT(OFS_PARM1);
825 s = PRVM_G_STRING(OFS_PARM2);
827 // LordHavoc: apparently BloodMage does a find(world, weaponmodel, "") and
828 // expects it to find all the monsters, so we must be careful to support
831 for (e++ ; e < prog->num_edicts ; e++)
833 prog->xfunction->builtinsprofile++;
834 ed = PRVM_EDICT_NUM(e);
835 if (ed->priv.required->free)
837 t = PRVM_E_STRING(ed,f);
847 VM_RETURN_EDICT(prog->edicts);
854 entity findfloat(entity start, .float field, float match)
855 entity findentity(entity start, .entity field, entity match)
858 // LordHavoc: added this for searching float, int, and entity reference fields
859 void VM_findfloat (void)
866 VM_SAFEPARMCOUNT(3,VM_findfloat);
868 e = PRVM_G_EDICTNUM(OFS_PARM0);
869 f = PRVM_G_INT(OFS_PARM1);
870 s = PRVM_G_FLOAT(OFS_PARM2);
872 for (e++ ; e < prog->num_edicts ; e++)
874 prog->xfunction->builtinsprofile++;
875 ed = PRVM_EDICT_NUM(e);
876 if (ed->priv.required->free)
878 if (PRVM_E_FLOAT(ed,f) == s)
885 VM_RETURN_EDICT(prog->edicts);
892 entity findchain(.string field, string match)
895 // chained search for strings in entity fields
896 // entity(.string field, string match) findchain = #402;
897 void VM_findchain (void)
902 prvm_edict_t *ent, *chain;
905 VM_SAFEPARMCOUNTRANGE(2,3,VM_findchain);
908 chainfield = PRVM_G_INT(OFS_PARM2);
910 chainfield = prog->fieldoffsets.chain;
912 PRVM_ERROR("VM_findchain: %s doesnt have the specified chain field !", PRVM_NAME);
914 chain = prog->edicts;
916 f = PRVM_G_INT(OFS_PARM0);
917 s = PRVM_G_STRING(OFS_PARM1);
919 // LordHavoc: apparently BloodMage does a find(world, weaponmodel, "") and
920 // expects it to find all the monsters, so we must be careful to support
923 ent = PRVM_NEXT_EDICT(prog->edicts);
924 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
926 prog->xfunction->builtinsprofile++;
927 if (ent->priv.required->free)
929 t = PRVM_E_STRING(ent,f);
935 PRVM_EDICTFIELDVALUE(ent,chainfield)->edict = PRVM_NUM_FOR_EDICT(chain);
939 VM_RETURN_EDICT(chain);
946 entity findchainfloat(.string field, float match)
947 entity findchainentity(.string field, entity match)
950 // LordHavoc: chained search for float, int, and entity reference fields
951 // entity(.string field, float match) findchainfloat = #403;
952 void VM_findchainfloat (void)
957 prvm_edict_t *ent, *chain;
960 VM_SAFEPARMCOUNTRANGE(2, 3, VM_findchainfloat);
963 chainfield = PRVM_G_INT(OFS_PARM2);
965 chainfield = prog->fieldoffsets.chain;
967 PRVM_ERROR("VM_findchain: %s doesnt have the specified chain field !", PRVM_NAME);
969 chain = (prvm_edict_t *)prog->edicts;
971 f = PRVM_G_INT(OFS_PARM0);
972 s = PRVM_G_FLOAT(OFS_PARM1);
974 ent = PRVM_NEXT_EDICT(prog->edicts);
975 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
977 prog->xfunction->builtinsprofile++;
978 if (ent->priv.required->free)
980 if (PRVM_E_FLOAT(ent,f) != s)
983 PRVM_EDICTFIELDVALUE(ent,chainfield)->edict = PRVM_EDICT_TO_PROG(chain);
987 VM_RETURN_EDICT(chain);
991 ========================
994 entity findflags(entity start, .float field, float match)
995 ========================
997 // LordHavoc: search for flags in float fields
998 void VM_findflags (void)
1005 VM_SAFEPARMCOUNT(3, VM_findflags);
1008 e = PRVM_G_EDICTNUM(OFS_PARM0);
1009 f = PRVM_G_INT(OFS_PARM1);
1010 s = (int)PRVM_G_FLOAT(OFS_PARM2);
1012 for (e++ ; e < prog->num_edicts ; e++)
1014 prog->xfunction->builtinsprofile++;
1015 ed = PRVM_EDICT_NUM(e);
1016 if (ed->priv.required->free)
1018 if (!PRVM_E_FLOAT(ed,f))
1020 if ((int)PRVM_E_FLOAT(ed,f) & s)
1022 VM_RETURN_EDICT(ed);
1027 VM_RETURN_EDICT(prog->edicts);
1031 ========================
1034 entity findchainflags(.float field, float match)
1035 ========================
1037 // LordHavoc: chained search for flags in float fields
1038 void VM_findchainflags (void)
1043 prvm_edict_t *ent, *chain;
1046 VM_SAFEPARMCOUNTRANGE(2, 3, VM_findchainflags);
1049 chainfield = PRVM_G_INT(OFS_PARM2);
1051 chainfield = prog->fieldoffsets.chain;
1053 PRVM_ERROR("VM_findchain: %s doesnt have the specified chain field !", PRVM_NAME);
1055 chain = (prvm_edict_t *)prog->edicts;
1057 f = PRVM_G_INT(OFS_PARM0);
1058 s = (int)PRVM_G_FLOAT(OFS_PARM1);
1060 ent = PRVM_NEXT_EDICT(prog->edicts);
1061 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
1063 prog->xfunction->builtinsprofile++;
1064 if (ent->priv.required->free)
1066 if (!PRVM_E_FLOAT(ent,f))
1068 if (!((int)PRVM_E_FLOAT(ent,f) & s))
1071 PRVM_EDICTFIELDVALUE(ent,chainfield)->edict = PRVM_EDICT_TO_PROG(chain);
1075 VM_RETURN_EDICT(chain);
1082 string precache_sound (string sample)
1085 void VM_precache_sound (void)
1089 VM_SAFEPARMCOUNT(1, VM_precache_sound);
1091 s = PRVM_G_STRING(OFS_PARM0);
1092 PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
1093 VM_CheckEmptyString(s);
1095 if(snd_initialized.integer && !S_PrecacheSound(s, true, true))
1097 VM_Warning("VM_precache_sound: Failed to load %s for %s\n", s, PRVM_NAME);
1106 returns the same string as output
1108 does nothing, only used by qcc to build .pak archives
1111 void VM_precache_file (void)
1113 VM_SAFEPARMCOUNT(1,VM_precache_file);
1114 // precache_file is only used to copy files with qcc, it does nothing
1115 PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
1125 void VM_coredump (void)
1127 VM_SAFEPARMCOUNT(0,VM_coredump);
1129 Cbuf_AddText("prvm_edicts ");
1130 Cbuf_AddText(PRVM_NAME);
1141 void PRVM_StackTrace(void);
1142 void VM_stackdump (void)
1144 VM_SAFEPARMCOUNT(0, VM_stackdump);
1159 VM_SAFEPARMCOUNT(0, VM_crash);
1161 PRVM_ERROR("Crash called by %s",PRVM_NAME);
1171 void VM_traceon (void)
1173 VM_SAFEPARMCOUNT(0,VM_traceon);
1185 void VM_traceoff (void)
1187 VM_SAFEPARMCOUNT(0,VM_traceoff);
1189 prog->trace = false;
1199 void VM_eprint (void)
1201 VM_SAFEPARMCOUNT(1,VM_eprint);
1203 PRVM_ED_PrintNum (PRVM_G_EDICTNUM(OFS_PARM0), NULL);
1216 VM_SAFEPARMCOUNT(1,VM_rint);
1218 f = PRVM_G_FLOAT(OFS_PARM0);
1220 PRVM_G_FLOAT(OFS_RETURN) = floor(f + 0.5);
1222 PRVM_G_FLOAT(OFS_RETURN) = ceil(f - 0.5);
1232 void VM_floor (void)
1234 VM_SAFEPARMCOUNT(1,VM_floor);
1236 PRVM_G_FLOAT(OFS_RETURN) = floor(PRVM_G_FLOAT(OFS_PARM0));
1248 VM_SAFEPARMCOUNT(1,VM_ceil);
1250 PRVM_G_FLOAT(OFS_RETURN) = ceil(PRVM_G_FLOAT(OFS_PARM0));
1258 entity nextent(entity)
1261 void VM_nextent (void)
1266 VM_SAFEPARMCOUNT(1, VM_nextent);
1268 i = PRVM_G_EDICTNUM(OFS_PARM0);
1271 prog->xfunction->builtinsprofile++;
1273 if (i == prog->num_edicts)
1275 VM_RETURN_EDICT(prog->edicts);
1278 ent = PRVM_EDICT_NUM(i);
1279 if (!ent->priv.required->free)
1281 VM_RETURN_EDICT(ent);
1287 //=============================================================================
1294 changelevel(string map)
1297 void VM_changelevel (void)
1299 VM_SAFEPARMCOUNT(1, VM_changelevel);
1303 VM_Warning("VM_changelevel: game is not server (%s)\n", PRVM_NAME);
1307 // make sure we don't issue two changelevels
1308 if (svs.changelevel_issued)
1310 svs.changelevel_issued = true;
1312 Cbuf_AddText (va("changelevel %s\n",PRVM_G_STRING(OFS_PARM0)));
1324 VM_SAFEPARMCOUNT(1,VM_sin);
1325 PRVM_G_FLOAT(OFS_RETURN) = sin(PRVM_G_FLOAT(OFS_PARM0));
1336 VM_SAFEPARMCOUNT(1,VM_cos);
1337 PRVM_G_FLOAT(OFS_RETURN) = cos(PRVM_G_FLOAT(OFS_PARM0));
1349 VM_SAFEPARMCOUNT(1,VM_sqrt);
1350 PRVM_G_FLOAT(OFS_RETURN) = sqrt(PRVM_G_FLOAT(OFS_PARM0));
1362 VM_SAFEPARMCOUNT(1,VM_asin);
1363 PRVM_G_FLOAT(OFS_RETURN) = asin(PRVM_G_FLOAT(OFS_PARM0));
1374 VM_SAFEPARMCOUNT(1,VM_acos);
1375 PRVM_G_FLOAT(OFS_RETURN) = acos(PRVM_G_FLOAT(OFS_PARM0));
1386 VM_SAFEPARMCOUNT(1,VM_atan);
1387 PRVM_G_FLOAT(OFS_RETURN) = atan(PRVM_G_FLOAT(OFS_PARM0));
1393 float atan2(float,float)
1396 void VM_atan2 (void)
1398 VM_SAFEPARMCOUNT(2,VM_atan2);
1399 PRVM_G_FLOAT(OFS_RETURN) = atan2(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1410 VM_SAFEPARMCOUNT(1,VM_tan);
1411 PRVM_G_FLOAT(OFS_RETURN) = tan(PRVM_G_FLOAT(OFS_PARM0));
1418 Returns a vector of length < 1 and > 0
1423 void VM_randomvec (void)
1428 VM_SAFEPARMCOUNT(0, VM_randomvec);
1433 temp[0] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1434 temp[1] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1435 temp[2] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1437 while (DotProduct(temp, temp) >= 1);
1438 VectorCopy (temp, PRVM_G_VECTOR(OFS_RETURN));
1441 temp[0] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1442 temp[1] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1443 temp[2] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1444 // length returned always > 0
1445 length = (rand()&32766 + 1) * (1.0 / 32767.0) / VectorLength(temp);
1446 VectorScale(temp,length, temp);*/
1447 //VectorCopy(temp, PRVM_G_VECTOR(OFS_RETURN));
1450 //=============================================================================
1456 float registercvar (string name, string value[, float flags])
1459 void VM_registercvar (void)
1461 const char *name, *value;
1464 VM_SAFEPARMCOUNTRANGE(2, 3, VM_registercvar);
1466 name = PRVM_G_STRING(OFS_PARM0);
1467 value = PRVM_G_STRING(OFS_PARM1);
1468 flags = prog->argc >= 3 ? (int)PRVM_G_FLOAT(OFS_PARM2) : 0;
1469 PRVM_G_FLOAT(OFS_RETURN) = 0;
1471 if(flags > CVAR_MAXFLAGSVAL)
1474 // first check to see if it has already been defined
1475 if (Cvar_FindVar (name))
1478 // check for overlap with a command
1479 if (Cmd_Exists (name))
1481 VM_Warning("VM_registercvar: %s is a command\n", name);
1485 Cvar_Get(name, value, flags, NULL);
1487 PRVM_G_FLOAT(OFS_RETURN) = 1; // success
1495 returns the minimum of two supplied floats
1497 float min(float a, float b, ...[float])
1502 VM_SAFEPARMCOUNTRANGE(2, 8, VM_min);
1503 // LordHavoc: 3+ argument enhancement suggested by FrikaC
1504 if (prog->argc >= 3)
1507 float f = PRVM_G_FLOAT(OFS_PARM0);
1508 for (i = 1;i < prog->argc;i++)
1509 if (f > PRVM_G_FLOAT((OFS_PARM0+i*3)))
1510 f = PRVM_G_FLOAT((OFS_PARM0+i*3));
1511 PRVM_G_FLOAT(OFS_RETURN) = f;
1514 PRVM_G_FLOAT(OFS_RETURN) = min(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1521 returns the maximum of two supplied floats
1523 float max(float a, float b, ...[float])
1528 VM_SAFEPARMCOUNTRANGE(2, 8, VM_max);
1529 // LordHavoc: 3+ argument enhancement suggested by FrikaC
1530 if (prog->argc >= 3)
1533 float f = PRVM_G_FLOAT(OFS_PARM0);
1534 for (i = 1;i < prog->argc;i++)
1535 if (f < PRVM_G_FLOAT((OFS_PARM0+i*3)))
1536 f = PRVM_G_FLOAT((OFS_PARM0+i*3));
1537 PRVM_G_FLOAT(OFS_RETURN) = f;
1540 PRVM_G_FLOAT(OFS_RETURN) = max(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1547 returns number bounded by supplied range
1549 float bound(float min, float value, float max)
1552 void VM_bound (void)
1554 VM_SAFEPARMCOUNT(3,VM_bound);
1555 PRVM_G_FLOAT(OFS_RETURN) = bound(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1), PRVM_G_FLOAT(OFS_PARM2));
1562 returns a raised to power b
1564 float pow(float a, float b)
1569 VM_SAFEPARMCOUNT(2,VM_pow);
1570 PRVM_G_FLOAT(OFS_RETURN) = pow(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1573 void VM_Files_Init(void)
1576 for (i = 0;i < PRVM_MAX_OPENFILES;i++)
1577 prog->openfiles[i] = NULL;
1580 void VM_Files_CloseAll(void)
1583 for (i = 0;i < PRVM_MAX_OPENFILES;i++)
1585 if (prog->openfiles[i])
1586 FS_Close(prog->openfiles[i]);
1587 prog->openfiles[i] = NULL;
1591 static qfile_t *VM_GetFileHandle( int index )
1593 if (index < 0 || index >= PRVM_MAX_OPENFILES)
1595 Con_Printf("VM_GetFileHandle: invalid file handle %i used in %s\n", index, PRVM_NAME);
1598 if (prog->openfiles[index] == NULL)
1600 Con_Printf("VM_GetFileHandle: no such file handle %i (or file has been closed) in %s\n", index, PRVM_NAME);
1603 return prog->openfiles[index];
1610 float fopen(string filename, float mode)
1613 // float(string filename, float mode) fopen = #110;
1614 // opens a file inside quake/gamedir/data/ (mode is FILE_READ, FILE_APPEND, or FILE_WRITE),
1615 // returns fhandle >= 0 if successful, or fhandle < 0 if unable to open file for any reason
1619 const char *modestring, *filename;
1621 VM_SAFEPARMCOUNT(2,VM_fopen);
1623 for (filenum = 0;filenum < PRVM_MAX_OPENFILES;filenum++)
1624 if (prog->openfiles[filenum] == NULL)
1626 if (filenum >= PRVM_MAX_OPENFILES)
1628 PRVM_G_FLOAT(OFS_RETURN) = -2;
1629 VM_Warning("VM_fopen: %s ran out of file handles (%i)\n", PRVM_NAME, PRVM_MAX_OPENFILES);
1632 filename = PRVM_G_STRING(OFS_PARM0);
1633 mode = (int)PRVM_G_FLOAT(OFS_PARM1);
1636 case 0: // FILE_READ
1638 prog->openfiles[filenum] = FS_OpenVirtualFile(va("data/%s", filename), false);
1639 if (prog->openfiles[filenum] == NULL)
1640 prog->openfiles[filenum] = FS_OpenVirtualFile(va("%s", filename), false);
1642 case 1: // FILE_APPEND
1644 prog->openfiles[filenum] = FS_OpenRealFile(va("data/%s", filename), modestring, false);
1646 case 2: // FILE_WRITE
1648 prog->openfiles[filenum] = FS_OpenRealFile(va("data/%s", filename), modestring, false);
1651 PRVM_G_FLOAT(OFS_RETURN) = -3;
1652 VM_Warning("VM_fopen: %s: no such mode %i (valid: 0 = read, 1 = append, 2 = write)\n", PRVM_NAME, mode);
1656 if (prog->openfiles[filenum] == NULL)
1658 PRVM_G_FLOAT(OFS_RETURN) = -1;
1659 if (developer.integer >= 100)
1660 VM_Warning("VM_fopen: %s: %s mode %s failed\n", PRVM_NAME, filename, modestring);
1664 PRVM_G_FLOAT(OFS_RETURN) = filenum;
1665 if (developer.integer >= 100)
1666 Con_Printf("VM_fopen: %s: %s mode %s opened as #%i\n", PRVM_NAME, filename, modestring, filenum);
1667 prog->openfiles_origin[filenum] = PRVM_AllocationOrigin();
1675 fclose(float fhandle)
1678 //void(float fhandle) fclose = #111; // closes a file
1679 void VM_fclose(void)
1683 VM_SAFEPARMCOUNT(1,VM_fclose);
1685 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1686 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
1688 VM_Warning("VM_fclose: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1691 if (prog->openfiles[filenum] == NULL)
1693 VM_Warning("VM_fclose: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1696 FS_Close(prog->openfiles[filenum]);
1697 prog->openfiles[filenum] = NULL;
1698 if(prog->openfiles_origin[filenum])
1699 PRVM_Free((char *)prog->openfiles_origin[filenum]);
1700 if (developer.integer >= 100)
1701 Con_Printf("VM_fclose: %s: #%i closed\n", PRVM_NAME, filenum);
1708 string fgets(float fhandle)
1711 //string(float fhandle) fgets = #112; // reads a line of text from the file and returns as a tempstring
1715 char string[VM_STRINGTEMP_LENGTH];
1718 VM_SAFEPARMCOUNT(1,VM_fgets);
1720 // set the return value regardless of any possible errors
1721 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
1723 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1724 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
1726 VM_Warning("VM_fgets: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1729 if (prog->openfiles[filenum] == NULL)
1731 VM_Warning("VM_fgets: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1737 c = FS_Getc(prog->openfiles[filenum]);
1738 if (c == '\r' || c == '\n' || c < 0)
1740 if (end < VM_STRINGTEMP_LENGTH - 1)
1744 // remove \n following \r
1747 c = FS_Getc(prog->openfiles[filenum]);
1749 FS_UnGetc(prog->openfiles[filenum], (unsigned char)c);
1751 if (developer.integer >= 100)
1752 Con_Printf("fgets: %s: %s\n", PRVM_NAME, string);
1754 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string);
1761 fputs(float fhandle, string s)
1764 //void(float fhandle, string s) fputs = #113; // writes a line of text to the end of the file
1768 char string[VM_STRINGTEMP_LENGTH];
1771 VM_SAFEPARMCOUNT(2,VM_fputs);
1773 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1774 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
1776 VM_Warning("VM_fputs: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1779 if (prog->openfiles[filenum] == NULL)
1781 VM_Warning("VM_fputs: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1784 VM_VarString(1, string, sizeof(string));
1785 if ((stringlength = (int)strlen(string)))
1786 FS_Write(prog->openfiles[filenum], string, stringlength);
1787 if (developer.integer >= 100)
1788 Con_Printf("fputs: %s: %s\n", PRVM_NAME, string);
1795 writetofile(float fhandle, entity ent)
1798 void VM_writetofile(void)
1803 VM_SAFEPARMCOUNT(2, VM_writetofile);
1805 file = VM_GetFileHandle( (int)PRVM_G_FLOAT(OFS_PARM0) );
1808 VM_Warning("VM_writetofile: invalid or closed file handle\n");
1812 ent = PRVM_G_EDICT(OFS_PARM1);
1813 if(ent->priv.required->free)
1815 VM_Warning("VM_writetofile: %s: entity %i is free !\n", PRVM_NAME, PRVM_NUM_FOR_EDICT(ent));
1819 PRVM_ED_Write (file, ent);
1822 // KrimZon - DP_QC_ENTITYDATA
1827 float() numentityfields
1828 Return the number of entity fields - NOT offsets
1831 void VM_numentityfields(void)
1833 PRVM_G_FLOAT(OFS_RETURN) = prog->progs->numfielddefs;
1836 // KrimZon - DP_QC_ENTITYDATA
1841 string(float fieldnum) entityfieldname
1842 Return name of the specified field as a string, or empty if the field is invalid (warning)
1845 void VM_entityfieldname(void)
1848 int i = (int)PRVM_G_FLOAT(OFS_PARM0);
1850 if (i < 0 || i >= prog->progs->numfielddefs)
1852 VM_Warning("VM_entityfieldname: %s: field index out of bounds\n", PRVM_NAME);
1853 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString("");
1857 d = &prog->fielddefs[i];
1858 PRVM_G_INT(OFS_RETURN) = d->s_name; // presuming that s_name points to a string already
1861 // KrimZon - DP_QC_ENTITYDATA
1866 float(float fieldnum) entityfieldtype
1869 void VM_entityfieldtype(void)
1872 int i = (int)PRVM_G_FLOAT(OFS_PARM0);
1874 if (i < 0 || i >= prog->progs->numfielddefs)
1876 VM_Warning("VM_entityfieldtype: %s: field index out of bounds\n", PRVM_NAME);
1877 PRVM_G_FLOAT(OFS_RETURN) = -1.0;
1881 d = &prog->fielddefs[i];
1882 PRVM_G_FLOAT(OFS_RETURN) = (float)d->type;
1885 // KrimZon - DP_QC_ENTITYDATA
1888 VM_getentityfieldstring
1890 string(float fieldnum, entity ent) getentityfieldstring
1893 void VM_getentityfieldstring(void)
1895 // put the data into a string
1900 int i = (int)PRVM_G_FLOAT(OFS_PARM0);
1902 if (i < 0 || i >= prog->progs->numfielddefs)
1904 VM_Warning("VM_entityfielddata: %s: field index out of bounds\n", PRVM_NAME);
1905 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString("");
1909 d = &prog->fielddefs[i];
1912 ent = PRVM_G_EDICT(OFS_PARM1);
1913 if(ent->priv.required->free)
1915 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString("");
1916 VM_Warning("VM_entityfielddata: %s: entity %i is free !\n", PRVM_NAME, PRVM_NUM_FOR_EDICT(ent));
1919 v = (int *)((char *)ent->fields.vp + d->ofs*4);
1921 // if it's 0 or blank, return an empty string
1922 type = d->type & ~DEF_SAVEGLOBAL;
1923 for (j=0 ; j<prvm_type_size[type] ; j++)
1926 if (j == prvm_type_size[type])
1928 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString("");
1932 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(PRVM_UglyValueString((etype_t)d->type, (prvm_eval_t *)v));
1935 // KrimZon - DP_QC_ENTITYDATA
1938 VM_putentityfieldstring
1940 float(float fieldnum, entity ent, string s) putentityfieldstring
1943 void VM_putentityfieldstring(void)
1947 int i = (int)PRVM_G_FLOAT(OFS_PARM0);
1949 if (i < 0 || i >= prog->progs->numfielddefs)
1951 VM_Warning("VM_entityfielddata: %s: field index out of bounds\n", PRVM_NAME);
1952 PRVM_G_FLOAT(OFS_RETURN) = 0.0f;
1956 d = &prog->fielddefs[i];
1959 ent = PRVM_G_EDICT(OFS_PARM1);
1960 if(ent->priv.required->free)
1962 VM_Warning("VM_entityfielddata: %s: entity %i is free !\n", PRVM_NAME, PRVM_NUM_FOR_EDICT(ent));
1963 PRVM_G_FLOAT(OFS_RETURN) = 0.0f;
1967 // parse the string into the value
1968 PRVM_G_FLOAT(OFS_RETURN) = ( PRVM_ED_ParseEpair(ent, d, PRVM_G_STRING(OFS_PARM2), false) ) ? 1.0f : 0.0f;
1975 float strlen(string s)
1978 //float(string s) strlen = #114; // returns how many characters are in a string
1979 void VM_strlen(void)
1981 VM_SAFEPARMCOUNT(1,VM_strlen);
1983 //PRVM_G_FLOAT(OFS_RETURN) = strlen(PRVM_G_STRING(OFS_PARM0));
1984 PRVM_G_FLOAT(OFS_RETURN) = u8_strlen(PRVM_G_STRING(OFS_PARM0));
1987 // DRESK - Decolorized String
1992 string strdecolorize(string s)
1995 // string (string s) strdecolorize = #472; // returns the passed in string with color codes stripped
1996 void VM_strdecolorize(void)
1998 char szNewString[VM_STRINGTEMP_LENGTH];
1999 const char *szString;
2002 VM_SAFEPARMCOUNT(1,VM_strdecolorize);
2003 szString = PRVM_G_STRING(OFS_PARM0);
2004 COM_StringDecolorize(szString, 0, szNewString, sizeof(szNewString), TRUE);
2005 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(szNewString);
2008 // DRESK - String Length (not counting color codes)
2013 float strlennocol(string s)
2016 // float(string s) strlennocol = #471; // returns how many characters are in a string not including color codes
2017 // For example, ^2Dresk returns a length of 5
2018 void VM_strlennocol(void)
2020 const char *szString;
2023 VM_SAFEPARMCOUNT(1,VM_strlennocol);
2025 szString = PRVM_G_STRING(OFS_PARM0);
2027 //nCnt = COM_StringLengthNoColors(szString, 0, NULL);
2028 nCnt = u8_COM_StringLengthNoColors(szString, 0, NULL);
2030 PRVM_G_FLOAT(OFS_RETURN) = nCnt;
2033 // DRESK - String to Uppercase and Lowercase
2038 string strtolower(string s)
2041 // string (string s) strtolower = #480; // returns passed in string in lowercase form
2042 void VM_strtolower(void)
2044 char szNewString[VM_STRINGTEMP_LENGTH];
2045 const char *szString;
2048 VM_SAFEPARMCOUNT(1,VM_strtolower);
2049 szString = PRVM_G_STRING(OFS_PARM0);
2051 COM_ToLowerString(szString, szNewString, sizeof(szNewString) );
2053 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(szNewString);
2060 string strtoupper(string s)
2063 // string (string s) strtoupper = #481; // returns passed in string in uppercase form
2064 void VM_strtoupper(void)
2066 char szNewString[VM_STRINGTEMP_LENGTH];
2067 const char *szString;
2070 VM_SAFEPARMCOUNT(1,VM_strtoupper);
2071 szString = PRVM_G_STRING(OFS_PARM0);
2073 COM_ToUpperString(szString, szNewString, sizeof(szNewString) );
2075 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(szNewString);
2082 string strcat(string,string,...[string])
2085 //string(string s1, string s2) strcat = #115;
2086 // concatenates two strings (for example "abc", "def" would return "abcdef")
2087 // and returns as a tempstring
2088 void VM_strcat(void)
2090 char s[VM_STRINGTEMP_LENGTH];
2091 VM_SAFEPARMCOUNTRANGE(1, 8, VM_strcat);
2093 VM_VarString(0, s, sizeof(s));
2094 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(s);
2101 string substring(string s, float start, float length)
2104 // string(string s, float start, float length) substring = #116;
2105 // returns a section of a string as a tempstring
2106 void VM_substring(void)
2109 int u_slength = 0, u_start;
2112 char string[VM_STRINGTEMP_LENGTH];
2114 VM_SAFEPARMCOUNT(3,VM_substring);
2117 s = PRVM_G_STRING(OFS_PARM0);
2118 start = (int)PRVM_G_FLOAT(OFS_PARM1);
2119 length = (int)PRVM_G_FLOAT(OFS_PARM2);
2120 slength = strlen(s);
2122 if (start < 0) // FTE_STRINGS feature
2124 start = bound(0, start, slength);
2126 if (length < 0) // FTE_STRINGS feature
2127 length += slength - start + 1;
2128 maxlen = min((int)sizeof(string) - 1, slength - start);
2129 length = bound(0, length, maxlen);
2131 memcpy(string, s + start, length);
2133 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string);
2136 s = PRVM_G_STRING(OFS_PARM0);
2137 start = (int)PRVM_G_FLOAT(OFS_PARM1);
2138 length = (int)PRVM_G_FLOAT(OFS_PARM2);
2140 if (start < 0) // FTE_STRINGS feature
2142 u_slength = u8_strlen(s);
2144 start = bound(0, start, u_slength);
2147 if (length < 0) // FTE_STRINGS feature
2149 if (!u_slength) // it's not calculated when it's not needed above
2150 u_slength = u8_strlen(s);
2151 length += u_slength - start + 1;
2154 // positive start, positive length
2155 u_start = u8_byteofs(s, start, NULL);
2158 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString("");
2161 u_length = u8_bytelen(s + u_start, length);
2162 if (u_length >= sizeof(string)-1)
2163 u_length = sizeof(string)-1;
2165 memcpy(string, s + u_start, u_length);
2166 string[u_length] = 0;
2167 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string);
2174 string(string search, string replace, string subject) strreplace = #484;
2177 // replaces all occurrences of search with replace in the string subject, and returns the result
2178 void VM_strreplace(void)
2181 const char *search, *replace, *subject;
2182 char string[VM_STRINGTEMP_LENGTH];
2183 int search_len, replace_len, subject_len;
2185 VM_SAFEPARMCOUNT(3,VM_strreplace);
2187 search = PRVM_G_STRING(OFS_PARM0);
2188 replace = PRVM_G_STRING(OFS_PARM1);
2189 subject = PRVM_G_STRING(OFS_PARM2);
2191 search_len = (int)strlen(search);
2192 replace_len = (int)strlen(replace);
2193 subject_len = (int)strlen(subject);
2196 for (i = 0; i < subject_len; i++)
2198 for (j = 0; j < search_len && i+j < subject_len; j++)
2199 if (subject[i+j] != search[j])
2201 if (j == search_len || i+j == subject_len)
2203 // found it at offset 'i'
2204 for (j = 0; j < replace_len && si < (int)sizeof(string) - 1; j++)
2205 string[si++] = replace[j];
2206 i += search_len - 1;
2211 if (si < (int)sizeof(string) - 1)
2212 string[si++] = subject[i];
2217 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string);
2224 string(string search, string replace, string subject) strireplace = #485;
2227 // case-insensitive version of strreplace
2228 void VM_strireplace(void)
2231 const char *search, *replace, *subject;
2232 char string[VM_STRINGTEMP_LENGTH];
2233 int search_len, replace_len, subject_len;
2235 VM_SAFEPARMCOUNT(3,VM_strreplace);
2237 search = PRVM_G_STRING(OFS_PARM0);
2238 replace = PRVM_G_STRING(OFS_PARM1);
2239 subject = PRVM_G_STRING(OFS_PARM2);
2241 search_len = (int)strlen(search);
2242 replace_len = (int)strlen(replace);
2243 subject_len = (int)strlen(subject);
2246 for (i = 0; i < subject_len; i++)
2248 for (j = 0; j < search_len && i+j < subject_len; j++)
2249 if (tolower(subject[i+j]) != tolower(search[j]))
2251 if (j == search_len || i+j == subject_len)
2253 // found it at offset 'i'
2254 for (j = 0; j < replace_len && si < (int)sizeof(string) - 1; j++)
2255 string[si++] = replace[j];
2256 i += search_len - 1;
2261 if (si < (int)sizeof(string) - 1)
2262 string[si++] = subject[i];
2267 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string);
2274 vector stov(string s)
2277 //vector(string s) stov = #117; // returns vector value from a string
2280 char string[VM_STRINGTEMP_LENGTH];
2282 VM_SAFEPARMCOUNT(1,VM_stov);
2284 VM_VarString(0, string, sizeof(string));
2285 Math_atov(string, PRVM_G_VECTOR(OFS_RETURN));
2292 string strzone(string s)
2295 //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)
2296 void VM_strzone(void)
2299 char string[VM_STRINGTEMP_LENGTH];
2302 VM_SAFEPARMCOUNT(1,VM_strzone);
2304 VM_VarString(0, string, sizeof(string));
2305 alloclen = strlen(string) + 1;
2306 PRVM_G_INT(OFS_RETURN) = PRVM_AllocString(alloclen, &out);
2307 memcpy(out, string, alloclen);
2317 //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!!!)
2318 void VM_strunzone(void)
2320 VM_SAFEPARMCOUNT(1,VM_strunzone);
2321 PRVM_FreeString(PRVM_G_INT(OFS_PARM0));
2326 VM_command (used by client and menu)
2328 clientcommand(float client, string s) (for client and menu)
2331 //void(entity e, string s) clientcommand = #440; // executes a command string as if it came from the specified client
2332 //this function originally written by KrimZon, made shorter by LordHavoc
2333 void VM_clcommand (void)
2335 client_t *temp_client;
2338 VM_SAFEPARMCOUNT(2,VM_clcommand);
2340 i = (int)PRVM_G_FLOAT(OFS_PARM0);
2341 if (!sv.active || i < 0 || i >= svs.maxclients || !svs.clients[i].active)
2343 VM_Warning("VM_clientcommand: %s: invalid client/server is not active !\n", PRVM_NAME);
2347 temp_client = host_client;
2348 host_client = svs.clients + i;
2349 Cmd_ExecuteString (PRVM_G_STRING(OFS_PARM1), src_client);
2350 host_client = temp_client;
2358 float tokenize(string s)
2361 //float(string s) tokenize = #441; // takes apart a string into individal words (access them with argv), returns how many
2362 //this function originally written by KrimZon, made shorter by LordHavoc
2363 //20040203: rewritten by LordHavoc (no longer uses allocations)
2364 static int num_tokens = 0;
2365 static int tokens[VM_STRINGTEMP_LENGTH / 2];
2366 static int tokens_startpos[VM_STRINGTEMP_LENGTH / 2];
2367 static int tokens_endpos[VM_STRINGTEMP_LENGTH / 2];
2368 static char tokenize_string[VM_STRINGTEMP_LENGTH];
2369 void VM_tokenize (void)
2373 VM_SAFEPARMCOUNT(1,VM_tokenize);
2375 strlcpy(tokenize_string, PRVM_G_STRING(OFS_PARM0), sizeof(tokenize_string));
2376 p = tokenize_string;
2381 if (num_tokens >= (int)(sizeof(tokens)/sizeof(tokens[0])))
2384 // skip whitespace here to find token start pos
2385 while(*p && ISWHITESPACE(*p))
2388 tokens_startpos[num_tokens] = p - tokenize_string;
2389 if(!COM_ParseToken_VM_Tokenize(&p, false))
2391 tokens_endpos[num_tokens] = p - tokenize_string;
2392 tokens[num_tokens] = PRVM_SetTempString(com_token);
2396 PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
2399 //float(string s) tokenize = #514; // takes apart a string into individal words (access them with argv), returns how many
2400 void VM_tokenize_console (void)
2404 VM_SAFEPARMCOUNT(1,VM_tokenize);
2406 strlcpy(tokenize_string, PRVM_G_STRING(OFS_PARM0), sizeof(tokenize_string));
2407 p = tokenize_string;
2412 if (num_tokens >= (int)(sizeof(tokens)/sizeof(tokens[0])))
2415 // skip whitespace here to find token start pos
2416 while(*p && ISWHITESPACE(*p))
2419 tokens_startpos[num_tokens] = p - tokenize_string;
2420 if(!COM_ParseToken_Console(&p))
2422 tokens_endpos[num_tokens] = p - tokenize_string;
2423 tokens[num_tokens] = PRVM_SetTempString(com_token);
2427 PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
2432 VM_tokenizebyseparator
2434 float tokenizebyseparator(string s, string separator1, ...)
2437 //float(string s, string separator1, ...) tokenizebyseparator = #479; // takes apart a string into individal words (access them with argv), returns how many
2438 //this function returns the token preceding each instance of a separator (of
2439 //which there can be multiple), and the text following the last separator
2440 //useful for parsing certain kinds of data like IP addresses
2442 //numnumbers = tokenizebyseparator("10.1.2.3", ".");
2443 //returns 4 and the tokens "10" "1" "2" "3".
2444 void VM_tokenizebyseparator (void)
2448 int separatorlen[7];
2449 const char *separators[7];
2452 char tokentext[MAX_INPUTLINE];
2454 VM_SAFEPARMCOUNTRANGE(2, 8,VM_tokenizebyseparator);
2456 strlcpy(tokenize_string, PRVM_G_STRING(OFS_PARM0), sizeof(tokenize_string));
2457 p = tokenize_string;
2460 for (j = 1;j < prog->argc;j++)
2462 // skip any blank separator strings
2463 const char *s = PRVM_G_STRING(OFS_PARM0+j*3);
2466 separators[numseparators] = s;
2467 separatorlen[numseparators] = strlen(s);
2474 while (num_tokens < (int)(sizeof(tokens)/sizeof(tokens[0])))
2476 token = tokentext + j;
2477 tokens_startpos[num_tokens] = p - tokenize_string;
2481 for (k = 0;k < numseparators;k++)
2483 if (!strncmp(p, separators[k], separatorlen[k]))
2485 p += separatorlen[k];
2489 if (k < numseparators)
2491 if (j < (int)sizeof(tokentext)-1)
2492 tokentext[j++] = *p;
2496 tokens_endpos[num_tokens] = p0 - tokenize_string;
2497 if (j >= (int)sizeof(tokentext))
2500 tokens[num_tokens++] = PRVM_SetTempString(token);
2505 PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
2508 //string(float n) argv = #442; // returns a word from the tokenized string (returns nothing for an invalid index)
2509 //this function originally written by KrimZon, made shorter by LordHavoc
2514 VM_SAFEPARMCOUNT(1,VM_argv);
2516 token_num = (int)PRVM_G_FLOAT(OFS_PARM0);
2519 token_num += num_tokens;
2521 if (token_num >= 0 && token_num < num_tokens)
2522 PRVM_G_INT(OFS_RETURN) = tokens[token_num];
2524 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
2527 //float(float n) argv_start_index = #515; // returns the start index of a token
2528 void VM_argv_start_index (void)
2532 VM_SAFEPARMCOUNT(1,VM_argv);
2534 token_num = (int)PRVM_G_FLOAT(OFS_PARM0);
2537 token_num += num_tokens;
2539 if (token_num >= 0 && token_num < num_tokens)
2540 PRVM_G_FLOAT(OFS_RETURN) = tokens_startpos[token_num];
2542 PRVM_G_FLOAT(OFS_RETURN) = -1;
2545 //float(float n) argv_end_index = #516; // returns the end index of a token
2546 void VM_argv_end_index (void)
2550 VM_SAFEPARMCOUNT(1,VM_argv);
2552 token_num = (int)PRVM_G_FLOAT(OFS_PARM0);
2555 token_num += num_tokens;
2557 if (token_num >= 0 && token_num < num_tokens)
2558 PRVM_G_FLOAT(OFS_RETURN) = tokens_endpos[token_num];
2560 PRVM_G_FLOAT(OFS_RETURN) = -1;
2570 void VM_isserver(void)
2572 VM_SAFEPARMCOUNT(0,VM_serverstate);
2574 PRVM_G_FLOAT(OFS_RETURN) = sv.active && (svs.maxclients > 1 || cls.state == ca_dedicated);
2584 void VM_clientcount(void)
2586 VM_SAFEPARMCOUNT(0,VM_clientcount);
2588 PRVM_G_FLOAT(OFS_RETURN) = svs.maxclients;
2598 void VM_clientstate(void)
2600 VM_SAFEPARMCOUNT(0,VM_clientstate);
2603 switch( cls.state ) {
2604 case ca_uninitialized:
2606 PRVM_G_FLOAT(OFS_RETURN) = 0;
2608 case ca_disconnected:
2609 PRVM_G_FLOAT(OFS_RETURN) = 1;
2612 PRVM_G_FLOAT(OFS_RETURN) = 2;
2615 // should never be reached!
2624 float getostype(void)
2626 */ // not used at the moment -> not included in the common list
2627 void VM_getostype(void)
2629 VM_SAFEPARMCOUNT(0,VM_getostype);
2634 OS_MAC - not supported
2638 PRVM_G_FLOAT(OFS_RETURN) = 0;
2639 #elif defined(MACOSX)
2640 PRVM_G_FLOAT(OFS_RETURN) = 2;
2642 PRVM_G_FLOAT(OFS_RETURN) = 1;
2653 extern double host_starttime;
2654 float CDAudio_GetPosition(void);
2655 void VM_gettime(void)
2659 VM_SAFEPARMCOUNTRANGE(0,1,VM_gettime);
2663 PRVM_G_FLOAT(OFS_RETURN) = (float) realtime;
2667 timer_index = (int) PRVM_G_FLOAT(OFS_PARM0);
2670 case 0: // GETTIME_FRAMESTART
2671 PRVM_G_FLOAT(OFS_RETURN) = (float) realtime;
2673 case 1: // GETTIME_REALTIME
2674 PRVM_G_FLOAT(OFS_RETURN) = (float) Sys_DoubleTime();
2676 case 2: // GETTIME_HIRES
2677 PRVM_G_FLOAT(OFS_RETURN) = (float) (Sys_DoubleTime() - realtime);
2679 case 3: // GETTIME_UPTIME
2680 PRVM_G_FLOAT(OFS_RETURN) = (float) (Sys_DoubleTime() - host_starttime);
2682 case 4: // GETTIME_CDTRACK
2683 PRVM_G_FLOAT(OFS_RETURN) = (float) CDAudio_GetPosition();
2686 VM_Warning("VM_gettime: %s: unsupported timer specified, returning realtime\n", PRVM_NAME);
2687 PRVM_G_FLOAT(OFS_RETURN) = (float) realtime;
2697 loadfromdata(string data)
2700 void VM_loadfromdata(void)
2702 VM_SAFEPARMCOUNT(1,VM_loadentsfromfile);
2704 PRVM_ED_LoadFromFile(PRVM_G_STRING(OFS_PARM0));
2708 ========================
2711 parseentitydata(entity ent, string data)
2712 ========================
2714 void VM_parseentitydata(void)
2719 VM_SAFEPARMCOUNT(2, VM_parseentitydata);
2721 // get edict and test it
2722 ent = PRVM_G_EDICT(OFS_PARM0);
2723 if (ent->priv.required->free)
2724 PRVM_ERROR ("VM_parseentitydata: %s: Can only set already spawned entities (entity %i is free)!", PRVM_NAME, PRVM_NUM_FOR_EDICT(ent));
2726 data = PRVM_G_STRING(OFS_PARM1);
2728 // parse the opening brace
2729 if (!COM_ParseToken_Simple(&data, false, false) || com_token[0] != '{' )
2730 PRVM_ERROR ("VM_parseentitydata: %s: Couldn't parse entity data:\n%s", PRVM_NAME, data );
2732 PRVM_ED_ParseEdict (data, ent);
2739 loadfromfile(string file)
2742 void VM_loadfromfile(void)
2744 const char *filename;
2747 VM_SAFEPARMCOUNT(1,VM_loadfromfile);
2749 filename = PRVM_G_STRING(OFS_PARM0);
2750 if (FS_CheckNastyPath(filename, false))
2752 PRVM_G_FLOAT(OFS_RETURN) = -4;
2753 VM_Warning("VM_loadfromfile: %s dangerous or non-portable filename \"%s\" not allowed. (contains : or \\ or begins with .. or /)\n", PRVM_NAME, filename);
2757 // not conform with VM_fopen
2758 data = (char *)FS_LoadFile(filename, tempmempool, false, NULL);
2760 PRVM_G_FLOAT(OFS_RETURN) = -1;
2762 PRVM_ED_LoadFromFile(data);
2773 float mod(float val, float m)
2776 void VM_modulo(void)
2779 VM_SAFEPARMCOUNT(2,VM_module);
2781 val = (int) PRVM_G_FLOAT(OFS_PARM0);
2782 m = (int) PRVM_G_FLOAT(OFS_PARM1);
2784 PRVM_G_FLOAT(OFS_RETURN) = (float) (val % m);
2787 void VM_Search_Init(void)
2790 for (i = 0;i < PRVM_MAX_OPENSEARCHES;i++)
2791 prog->opensearches[i] = NULL;
2794 void VM_Search_Reset(void)
2797 // reset the fssearch list
2798 for(i = 0; i < PRVM_MAX_OPENSEARCHES; i++)
2800 if(prog->opensearches[i])
2801 FS_FreeSearch(prog->opensearches[i]);
2802 prog->opensearches[i] = NULL;
2810 float search_begin(string pattern, float caseinsensitive, float quiet)
2813 void VM_search_begin(void)
2816 const char *pattern;
2817 int caseinsens, quiet;
2819 VM_SAFEPARMCOUNT(3, VM_search_begin);
2821 pattern = PRVM_G_STRING(OFS_PARM0);
2823 VM_CheckEmptyString(pattern);
2825 caseinsens = (int)PRVM_G_FLOAT(OFS_PARM1);
2826 quiet = (int)PRVM_G_FLOAT(OFS_PARM2);
2828 for(handle = 0; handle < PRVM_MAX_OPENSEARCHES; handle++)
2829 if(!prog->opensearches[handle])
2832 if(handle >= PRVM_MAX_OPENSEARCHES)
2834 PRVM_G_FLOAT(OFS_RETURN) = -2;
2835 VM_Warning("VM_search_begin: %s ran out of search handles (%i)\n", PRVM_NAME, PRVM_MAX_OPENSEARCHES);
2839 if(!(prog->opensearches[handle] = FS_Search(pattern,caseinsens, quiet)))
2840 PRVM_G_FLOAT(OFS_RETURN) = -1;
2843 prog->opensearches_origin[handle] = PRVM_AllocationOrigin();
2844 PRVM_G_FLOAT(OFS_RETURN) = handle;
2852 void search_end(float handle)
2855 void VM_search_end(void)
2858 VM_SAFEPARMCOUNT(1, VM_search_end);
2860 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
2862 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
2864 VM_Warning("VM_search_end: invalid handle %i used in %s\n", handle, PRVM_NAME);
2867 if(prog->opensearches[handle] == NULL)
2869 VM_Warning("VM_search_end: no such handle %i in %s\n", handle, PRVM_NAME);
2873 FS_FreeSearch(prog->opensearches[handle]);
2874 prog->opensearches[handle] = NULL;
2875 if(prog->opensearches_origin[handle])
2876 PRVM_Free((char *)prog->opensearches_origin[handle]);
2883 float search_getsize(float handle)
2886 void VM_search_getsize(void)
2889 VM_SAFEPARMCOUNT(1, VM_M_search_getsize);
2891 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
2893 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
2895 VM_Warning("VM_search_getsize: invalid handle %i used in %s\n", handle, PRVM_NAME);
2898 if(prog->opensearches[handle] == NULL)
2900 VM_Warning("VM_search_getsize: no such handle %i in %s\n", handle, PRVM_NAME);
2904 PRVM_G_FLOAT(OFS_RETURN) = prog->opensearches[handle]->numfilenames;
2909 VM_search_getfilename
2911 string search_getfilename(float handle, float num)
2914 void VM_search_getfilename(void)
2916 int handle, filenum;
2917 VM_SAFEPARMCOUNT(2, VM_search_getfilename);
2919 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
2920 filenum = (int)PRVM_G_FLOAT(OFS_PARM1);
2922 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
2924 VM_Warning("VM_search_getfilename: invalid handle %i used in %s\n", handle, PRVM_NAME);
2927 if(prog->opensearches[handle] == NULL)
2929 VM_Warning("VM_search_getfilename: no such handle %i in %s\n", handle, PRVM_NAME);
2932 if(filenum < 0 || filenum >= prog->opensearches[handle]->numfilenames)
2934 VM_Warning("VM_search_getfilename: invalid filenum %i in %s\n", filenum, PRVM_NAME);
2938 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog->opensearches[handle]->filenames[filenum]);
2945 string chr(float ascii)
2951 VM_SAFEPARMCOUNT(1, VM_chr);
2953 tmp[0] = (unsigned char) PRVM_G_FLOAT(OFS_PARM0);
2956 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(tmp);
2959 //=============================================================================
2960 // Draw builtins (client & menu)
2966 float iscachedpic(string pic)
2969 void VM_iscachedpic(void)
2971 VM_SAFEPARMCOUNT(1,VM_iscachedpic);
2973 // drawq hasnt such a function, thus always return true
2974 PRVM_G_FLOAT(OFS_RETURN) = false;
2981 string precache_pic(string pic)
2984 void VM_precache_pic(void)
2988 VM_SAFEPARMCOUNT(1, VM_precache_pic);
2990 s = PRVM_G_STRING(OFS_PARM0);
2991 PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
2992 VM_CheckEmptyString (s);
2994 // AK Draw_CachePic is supposed to always return a valid pointer
2995 if( Draw_CachePic_Flags(s, CACHEPICFLAG_NOTPERSISTENT)->tex == r_texture_notexture )
2996 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
3006 void VM_freepic(void)
3010 VM_SAFEPARMCOUNT(1,VM_freepic);
3012 s = PRVM_G_STRING(OFS_PARM0);
3013 VM_CheckEmptyString (s);
3018 dp_font_t *getdrawfont(void)
3020 if(prog->globaloffsets.drawfont >= 0)
3022 int f = (int) PRVM_G_FLOAT(prog->globaloffsets.drawfont);
3023 if(f < 0 || f >= MAX_FONTS)
3024 return FONT_DEFAULT;
3025 return &dp_fonts[f];
3028 return FONT_DEFAULT;
3035 float drawcharacter(vector position, float character, vector scale, vector rgb, float alpha, float flag)
3038 void VM_drawcharacter(void)
3040 float *pos,*scale,*rgb;
3043 VM_SAFEPARMCOUNT(6,VM_drawcharacter);
3045 character = (char) PRVM_G_FLOAT(OFS_PARM1);
3048 PRVM_G_FLOAT(OFS_RETURN) = -1;
3049 VM_Warning("VM_drawcharacter: %s passed null character !\n",PRVM_NAME);
3053 pos = PRVM_G_VECTOR(OFS_PARM0);
3054 scale = PRVM_G_VECTOR(OFS_PARM2);
3055 rgb = PRVM_G_VECTOR(OFS_PARM3);
3056 flag = (int)PRVM_G_FLOAT(OFS_PARM5);
3058 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3060 PRVM_G_FLOAT(OFS_RETURN) = -2;
3061 VM_Warning("VM_drawcharacter: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
3065 if(pos[2] || scale[2])
3066 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")));
3068 if(!scale[0] || !scale[1])
3070 PRVM_G_FLOAT(OFS_RETURN) = -3;
3071 VM_Warning("VM_drawcharacter: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
3075 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());
3076 PRVM_G_FLOAT(OFS_RETURN) = 1;
3083 float drawstring(vector position, string text, vector scale, vector rgb, float alpha, float flag)
3086 void VM_drawstring(void)
3088 float *pos,*scale,*rgb;
3091 VM_SAFEPARMCOUNT(6,VM_drawstring);
3093 string = PRVM_G_STRING(OFS_PARM1);
3094 pos = PRVM_G_VECTOR(OFS_PARM0);
3095 scale = PRVM_G_VECTOR(OFS_PARM2);
3096 rgb = PRVM_G_VECTOR(OFS_PARM3);
3097 flag = (int)PRVM_G_FLOAT(OFS_PARM5);
3099 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3101 PRVM_G_FLOAT(OFS_RETURN) = -2;
3102 VM_Warning("VM_drawstring: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
3106 if(!scale[0] || !scale[1])
3108 PRVM_G_FLOAT(OFS_RETURN) = -3;
3109 VM_Warning("VM_drawstring: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
3113 if(pos[2] || scale[2])
3114 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")));
3116 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());
3117 //Font_DrawString(pos[0], pos[1], string, 0, scale[0], scale[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM4), flag, NULL, true);
3118 PRVM_G_FLOAT(OFS_RETURN) = 1;
3123 VM_drawcolorcodedstring
3125 float drawcolorcodedstring(vector position, string text, vector scale, float alpha, float flag)
3128 void VM_drawcolorcodedstring(void)
3133 VM_SAFEPARMCOUNT(5,VM_drawstring);
3135 string = PRVM_G_STRING(OFS_PARM1);
3136 pos = PRVM_G_VECTOR(OFS_PARM0);
3137 scale = PRVM_G_VECTOR(OFS_PARM2);
3138 flag = (int)PRVM_G_FLOAT(OFS_PARM4);
3140 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3142 PRVM_G_FLOAT(OFS_RETURN) = -2;
3143 VM_Warning("VM_drawcolorcodedstring: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
3147 if(!scale[0] || !scale[1])
3149 PRVM_G_FLOAT(OFS_RETURN) = -3;
3150 VM_Warning("VM_drawcolorcodedstring: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
3154 if(pos[2] || scale[2])
3155 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")));
3158 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());
3159 PRVM_G_FLOAT(OFS_RETURN) = 1;
3165 float stringwidth(string text, float allowColorCodes, float size)
3168 void VM_stringwidth(void)
3172 float mult; // sz is intended font size so we can later add freetype support, mult is font size multiplier in pixels per character cell
3175 VM_SAFEPARMCOUNTRANGE(2,3,VM_drawstring);
3178 string = PRVM_G_STRING(OFS_PARM0);
3179 colors = (int)PRVM_G_FLOAT(OFS_PARM1);
3181 if (prog->argc == 3)
3183 sizevec = PRVM_G_VECTOR(OFS_PARM2);
3184 // support 0 parameter for either w or h
3185 if (sizevec[0] > 5 || sizevec[1] > 5)
3186 Con_Printf(": %f %f\n", sizevec[0], sizevec[1]);
3187 if (!sizevec[0]) sizevec[0] = sizevec[1];
3188 if (!sizevec[1]) sizevec[1] = sizevec[0];
3189 //PRVM_G_FLOAT(OFS_RETURN) = DrawQ_TextWidth_Font_Size(string, sizevec[0], sizevec[1], 0, !colors, getdrawfont());
3190 PRVM_G_FLOAT(OFS_RETURN) = DrawQ_TextWidth_Font(string, 0, !colors, getdrawfont()) * sizevec[0];
3194 static qboolean dbg_info = false;
3198 Con_Print("VM_stringwidth(): Your code uses a deprecated version of stringwidth()\n");
3200 PRVM_G_FLOAT(OFS_RETURN) = DrawQ_TextWidth_Font(string, 0, !colors, getdrawfont());
3206 static float defsize[] = {0, 0};
3208 szv = PRVM_G_VECTOR(OFS_PARM2);
3213 static float defsize[] = {0, 0};
3219 string = PRVM_G_STRING(OFS_PARM0);
3220 colors = (int)PRVM_G_FLOAT(OFS_PARM1);
3222 PRVM_G_FLOAT(OFS_RETURN) = DrawQ_TextWidth_Font(string, 0, !colors, getdrawfont()) * mult; // 1x1 characters, don't actually draw
3223 Con_Printf(": %f\n", PRVM_G_FLOAT(OFS_RETURN));
3228 mult = sz = PRVM_G_FLOAT(OFS_PARM2);
3236 string = PRVM_G_STRING(OFS_PARM0);
3237 colors = (int)PRVM_G_FLOAT(OFS_PARM1);
3239 PRVM_G_FLOAT(OFS_RETURN) = DrawQ_TextWidth_Font(string, 0, !colors, getdrawfont()) * mult; // 1x1 characters, don't actually draw
3247 float drawpic(vector position, string pic, vector size, vector rgb, float alpha, float flag)
3250 void VM_drawpic(void)
3252 const char *picname;
3253 float *size, *pos, *rgb;
3256 VM_SAFEPARMCOUNT(6,VM_drawpic);
3258 picname = PRVM_G_STRING(OFS_PARM1);
3259 VM_CheckEmptyString (picname);
3261 // is pic cached ? no function yet for that
3264 PRVM_G_FLOAT(OFS_RETURN) = -4;
3265 VM_Warning("VM_drawpic: %s: %s not cached !\n", PRVM_NAME, picname);
3269 pos = PRVM_G_VECTOR(OFS_PARM0);
3270 size = PRVM_G_VECTOR(OFS_PARM2);
3271 rgb = PRVM_G_VECTOR(OFS_PARM3);
3272 flag = (int) PRVM_G_FLOAT(OFS_PARM5);
3274 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3276 PRVM_G_FLOAT(OFS_RETURN) = -2;
3277 VM_Warning("VM_drawpic: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
3281 if(pos[2] || size[2])
3282 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")));
3284 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);
3285 PRVM_G_FLOAT(OFS_RETURN) = 1;
3291 float drawrotpic(vector position, string pic, vector size, vector org, float angle, vector rgb, float alpha, float flag)
3294 void VM_drawrotpic(void)
3296 const char *picname;
3297 float *size, *pos, *org, *rgb;
3300 VM_SAFEPARMCOUNT(8,VM_drawrotpic);
3302 picname = PRVM_G_STRING(OFS_PARM1);
3303 VM_CheckEmptyString (picname);
3305 // is pic cached ? no function yet for that
3308 PRVM_G_FLOAT(OFS_RETURN) = -4;
3309 VM_Warning("VM_drawrotpic: %s: %s not cached !\n", PRVM_NAME, picname);
3313 pos = PRVM_G_VECTOR(OFS_PARM0);
3314 size = PRVM_G_VECTOR(OFS_PARM2);
3315 org = PRVM_G_VECTOR(OFS_PARM3);
3316 rgb = PRVM_G_VECTOR(OFS_PARM5);
3317 flag = (int) PRVM_G_FLOAT(OFS_PARM7);
3319 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3321 PRVM_G_FLOAT(OFS_RETURN) = -2;
3322 VM_Warning("VM_drawrotpic: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
3326 if(pos[2] || size[2] || org[2])
3327 Con_Printf("VM_drawrotpic: z value from pos/size/org discarded\n");
3329 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);
3330 PRVM_G_FLOAT(OFS_RETURN) = 1;
3336 float drawsubpic(vector position, vector size, string pic, vector srcPos, vector srcSize, vector rgb, float alpha, float flag)
3340 void VM_drawsubpic(void)
3342 const char *picname;
3343 float *size, *pos, *rgb, *srcPos, *srcSize, alpha;
3346 VM_SAFEPARMCOUNT(8,VM_drawsubpic);
3348 picname = PRVM_G_STRING(OFS_PARM2);
3349 VM_CheckEmptyString (picname);
3351 // is pic cached ? no function yet for that
3354 PRVM_G_FLOAT(OFS_RETURN) = -4;
3355 VM_Warning("VM_drawsubpic: %s: %s not cached !\n", PRVM_NAME, picname);
3359 pos = PRVM_G_VECTOR(OFS_PARM0);
3360 size = PRVM_G_VECTOR(OFS_PARM1);
3361 srcPos = PRVM_G_VECTOR(OFS_PARM3);
3362 srcSize = PRVM_G_VECTOR(OFS_PARM4);
3363 rgb = PRVM_G_VECTOR(OFS_PARM5);
3364 alpha = PRVM_G_FLOAT(OFS_PARM6);
3365 flag = (int) PRVM_G_FLOAT(OFS_PARM7);
3367 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3369 PRVM_G_FLOAT(OFS_RETURN) = -2;
3370 VM_Warning("VM_drawsubpic: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
3374 if(pos[2] || size[2])
3375 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")));
3377 DrawQ_SuperPic(pos[0], pos[1], Draw_CachePic (picname),
3379 srcPos[0], srcPos[1], rgb[0], rgb[1], rgb[2], alpha,
3380 srcPos[0] + srcSize[0], srcPos[1], rgb[0], rgb[1], rgb[2], alpha,
3381 srcPos[0], srcPos[1] + srcSize[1], rgb[0], rgb[1], rgb[2], alpha,
3382 srcPos[0] + srcSize[0], srcPos[1] + srcSize[1], rgb[0], rgb[1], rgb[2], alpha,
3384 PRVM_G_FLOAT(OFS_RETURN) = 1;
3391 float drawfill(vector position, vector size, vector rgb, float alpha, float flag)
3394 void VM_drawfill(void)
3396 float *size, *pos, *rgb;
3399 VM_SAFEPARMCOUNT(5,VM_drawfill);
3402 pos = PRVM_G_VECTOR(OFS_PARM0);
3403 size = PRVM_G_VECTOR(OFS_PARM1);
3404 rgb = PRVM_G_VECTOR(OFS_PARM2);
3405 flag = (int) PRVM_G_FLOAT(OFS_PARM4);
3407 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3409 PRVM_G_FLOAT(OFS_RETURN) = -2;
3410 VM_Warning("VM_drawfill: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
3414 if(pos[2] || size[2])
3415 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")));
3417 DrawQ_Fill(pos[0], pos[1], size[0], size[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM3), flag);
3418 PRVM_G_FLOAT(OFS_RETURN) = 1;
3425 drawsetcliparea(float x, float y, float width, float height)
3428 void VM_drawsetcliparea(void)
3431 VM_SAFEPARMCOUNT(4,VM_drawsetcliparea);
3433 x = bound(0, PRVM_G_FLOAT(OFS_PARM0), vid_conwidth.integer);
3434 y = bound(0, PRVM_G_FLOAT(OFS_PARM1), vid_conheight.integer);
3435 w = bound(0, PRVM_G_FLOAT(OFS_PARM2) + PRVM_G_FLOAT(OFS_PARM0) - x, (vid_conwidth.integer - x));
3436 h = bound(0, PRVM_G_FLOAT(OFS_PARM3) + PRVM_G_FLOAT(OFS_PARM1) - y, (vid_conheight.integer - y));
3438 DrawQ_SetClipArea(x, y, w, h);
3443 VM_drawresetcliparea
3448 void VM_drawresetcliparea(void)
3450 VM_SAFEPARMCOUNT(0,VM_drawresetcliparea);
3452 DrawQ_ResetClipArea();
3459 vector getimagesize(string pic)
3462 void VM_getimagesize(void)
3467 VM_SAFEPARMCOUNT(1,VM_getimagesize);
3469 p = PRVM_G_STRING(OFS_PARM0);
3470 VM_CheckEmptyString (p);
3472 pic = Draw_CachePic_Flags (p, CACHEPICFLAG_NOTPERSISTENT);
3474 PRVM_G_VECTOR(OFS_RETURN)[0] = pic->width;
3475 PRVM_G_VECTOR(OFS_RETURN)[1] = pic->height;
3476 PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
3483 string keynumtostring(float keynum)
3486 void VM_keynumtostring (void)
3488 VM_SAFEPARMCOUNT(1, VM_keynumtostring);
3490 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(Key_KeynumToString((int)PRVM_G_FLOAT(OFS_PARM0)));
3495 VM_findkeysforcommand
3497 string findkeysforcommand(string command)
3499 the returned string is an altstring
3502 #define NUMKEYS 5 // TODO: merge the constant in keys.c with this one somewhen
3504 void M_FindKeysForCommand(const char *command, int *keys);
3505 void VM_findkeysforcommand(void)
3508 char ret[VM_STRINGTEMP_LENGTH];
3512 VM_SAFEPARMCOUNT(1, VM_findkeysforcommand);
3514 cmd = PRVM_G_STRING(OFS_PARM0);
3516 VM_CheckEmptyString(cmd);
3518 M_FindKeysForCommand(cmd, keys);
3521 for(i = 0; i < NUMKEYS; i++)
3522 strlcat(ret, va(" \'%i\'", keys[i]), sizeof(ret));
3524 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(ret);
3531 float stringtokeynum(string key)
3534 void VM_stringtokeynum (void)
3536 VM_SAFEPARMCOUNT( 1, VM_keynumtostring );
3538 PRVM_G_INT(OFS_RETURN) = Key_StringToKeynum(PRVM_G_STRING(OFS_PARM0));
3541 // CL_Video interface functions
3544 ========================
3547 float cin_open(string file, string name)
3548 ========================
3550 void VM_cin_open( void )
3555 VM_SAFEPARMCOUNT( 2, VM_cin_open );
3557 file = PRVM_G_STRING( OFS_PARM0 );
3558 name = PRVM_G_STRING( OFS_PARM1 );
3560 VM_CheckEmptyString( file );
3561 VM_CheckEmptyString( name );
3563 if( CL_OpenVideo( file, name, MENUOWNER ) )
3564 PRVM_G_FLOAT( OFS_RETURN ) = 1;
3566 PRVM_G_FLOAT( OFS_RETURN ) = 0;
3570 ========================
3573 void cin_close(string name)
3574 ========================
3576 void VM_cin_close( void )
3580 VM_SAFEPARMCOUNT( 1, VM_cin_close );
3582 name = PRVM_G_STRING( OFS_PARM0 );
3583 VM_CheckEmptyString( name );
3585 CL_CloseVideo( CL_GetVideoByName( name ) );
3589 ========================
3591 void cin_setstate(string name, float type)
3592 ========================
3594 void VM_cin_setstate( void )
3597 clvideostate_t state;
3600 VM_SAFEPARMCOUNT( 2, VM_cin_netstate );
3602 name = PRVM_G_STRING( OFS_PARM0 );
3603 VM_CheckEmptyString( name );
3605 state = (clvideostate_t)((int)PRVM_G_FLOAT( OFS_PARM1 ));
3607 video = CL_GetVideoByName( name );
3608 if( video && state > CLVIDEO_UNUSED && state < CLVIDEO_STATECOUNT )
3609 CL_SetVideoState( video, state );
3613 ========================
3616 float cin_getstate(string name)
3617 ========================
3619 void VM_cin_getstate( void )
3624 VM_SAFEPARMCOUNT( 1, VM_cin_getstate );
3626 name = PRVM_G_STRING( OFS_PARM0 );
3627 VM_CheckEmptyString( name );
3629 video = CL_GetVideoByName( name );
3631 PRVM_G_FLOAT( OFS_RETURN ) = (int)video->state;
3633 PRVM_G_FLOAT( OFS_RETURN ) = 0;
3637 ========================
3640 void cin_restart(string name)
3641 ========================
3643 void VM_cin_restart( void )
3648 VM_SAFEPARMCOUNT( 1, VM_cin_restart );
3650 name = PRVM_G_STRING( OFS_PARM0 );
3651 VM_CheckEmptyString( name );
3653 video = CL_GetVideoByName( name );
3655 CL_RestartVideo( video );
3659 ========================
3661 ========================
3663 void VM_Gecko_Init( void ) {
3664 // the prog struct is memset to 0 by Initprog? [12/6/2007 Black]
3665 // FIXME: remove the other _Init functions then, too? [12/6/2007 Black]
3669 ========================
3671 ========================
3673 void VM_Gecko_Destroy( void ) {
3675 for( i = 0 ; i < PRVM_MAX_GECKOINSTANCES ; i++ ) {
3676 clgecko_t **instance = &prog->opengeckoinstances[ i ];
3678 CL_Gecko_DestroyBrowser( *instance );
3685 ========================
3688 float[bool] gecko_create( string name )
3689 ========================
3691 void VM_gecko_create( void ) {
3694 clgecko_t *instance;
3696 VM_SAFEPARMCOUNT( 1, VM_gecko_create );
3698 name = PRVM_G_STRING( OFS_PARM0 );
3699 VM_CheckEmptyString( name );
3701 // find an empty slot for this gecko browser..
3702 for( i = 0 ; i < PRVM_MAX_GECKOINSTANCES ; i++ ) {
3703 if( prog->opengeckoinstances[ i ] == NULL ) {
3707 if( i == PRVM_MAX_GECKOINSTANCES ) {
3708 VM_Warning("VM_gecko_create: %s ran out of gecko handles (%i)\n", PRVM_NAME, PRVM_MAX_GECKOINSTANCES);
3709 PRVM_G_FLOAT( OFS_RETURN ) = 0;
3713 instance = prog->opengeckoinstances[ i ] = CL_Gecko_CreateBrowser( name, PRVM_GetProgNr() );
3715 // TODO: error handling [12/3/2007 Black]
3716 PRVM_G_FLOAT( OFS_RETURN ) = 0;
3719 PRVM_G_FLOAT( OFS_RETURN ) = 1;
3723 ========================
3726 void gecko_destroy( string name )
3727 ========================
3729 void VM_gecko_destroy( void ) {
3731 clgecko_t *instance;
3733 VM_SAFEPARMCOUNT( 1, VM_gecko_destroy );
3735 name = PRVM_G_STRING( OFS_PARM0 );
3736 VM_CheckEmptyString( name );
3737 instance = CL_Gecko_FindBrowser( name );
3741 CL_Gecko_DestroyBrowser( instance );
3745 ========================
3748 void gecko_navigate( string name, string URI )
3749 ========================
3751 void VM_gecko_navigate( void ) {
3754 clgecko_t *instance;
3756 VM_SAFEPARMCOUNT( 2, VM_gecko_navigate );
3758 name = PRVM_G_STRING( OFS_PARM0 );
3759 URI = PRVM_G_STRING( OFS_PARM1 );
3760 VM_CheckEmptyString( name );
3761 VM_CheckEmptyString( URI );
3763 instance = CL_Gecko_FindBrowser( name );
3767 CL_Gecko_NavigateToURI( instance, URI );
3771 ========================
3774 float[bool] gecko_keyevent( string name, float key, float eventtype )
3775 ========================
3777 void VM_gecko_keyevent( void ) {
3780 clgecko_buttoneventtype_t eventtype;
3781 clgecko_t *instance;
3783 VM_SAFEPARMCOUNT( 3, VM_gecko_keyevent );
3785 name = PRVM_G_STRING( OFS_PARM0 );
3786 VM_CheckEmptyString( name );
3787 key = (unsigned int) PRVM_G_FLOAT( OFS_PARM1 );
3788 switch( (unsigned int) PRVM_G_FLOAT( OFS_PARM2 ) ) {
3790 eventtype = CLG_BET_DOWN;
3793 eventtype = CLG_BET_UP;
3796 eventtype = CLG_BET_PRESS;
3799 eventtype = CLG_BET_DOUBLECLICK;
3802 // TODO: console printf? [12/3/2007 Black]
3803 PRVM_G_FLOAT( OFS_RETURN ) = 0;
3807 instance = CL_Gecko_FindBrowser( name );
3809 PRVM_G_FLOAT( OFS_RETURN ) = 0;
3813 PRVM_G_FLOAT( OFS_RETURN ) = (CL_Gecko_Event_Key( instance, (keynum_t) key, eventtype ) == true);
3817 ========================
3820 void gecko_mousemove( string name, float x, float y )
3821 ========================
3823 void VM_gecko_movemouse( void ) {
3826 clgecko_t *instance;
3828 VM_SAFEPARMCOUNT( 3, VM_gecko_movemouse );
3830 name = PRVM_G_STRING( OFS_PARM0 );
3831 VM_CheckEmptyString( name );
3832 x = PRVM_G_FLOAT( OFS_PARM1 );
3833 y = PRVM_G_FLOAT( OFS_PARM2 );
3835 instance = CL_Gecko_FindBrowser( name );
3839 CL_Gecko_Event_CursorMove( instance, x, y );
3844 ========================
3847 void gecko_resize( string name, float w, float h )
3848 ========================
3850 void VM_gecko_resize( void ) {
3853 clgecko_t *instance;
3855 VM_SAFEPARMCOUNT( 3, VM_gecko_movemouse );
3857 name = PRVM_G_STRING( OFS_PARM0 );
3858 VM_CheckEmptyString( name );
3859 w = PRVM_G_FLOAT( OFS_PARM1 );
3860 h = PRVM_G_FLOAT( OFS_PARM2 );
3862 instance = CL_Gecko_FindBrowser( name );
3866 CL_Gecko_Resize( instance, (int) w, (int) h );
3871 ========================
3872 VM_gecko_get_texture_extent
3874 vector gecko_get_texture_extent( string name )
3875 ========================
3877 void VM_gecko_get_texture_extent( void ) {
3879 clgecko_t *instance;
3881 VM_SAFEPARMCOUNT( 1, VM_gecko_movemouse );
3883 name = PRVM_G_STRING( OFS_PARM0 );
3884 VM_CheckEmptyString( name );
3886 PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
3887 instance = CL_Gecko_FindBrowser( name );
3889 PRVM_G_VECTOR(OFS_RETURN)[0] = 0;
3890 PRVM_G_VECTOR(OFS_RETURN)[1] = 0;
3893 CL_Gecko_GetTextureExtent( instance,
3894 PRVM_G_VECTOR(OFS_RETURN), PRVM_G_VECTOR(OFS_RETURN)+1 );
3903 Writes new values for v_forward, v_up, and v_right based on angles
3904 void makevectors(vector angle)
3907 void VM_makevectors (void)
3909 prvm_eval_t *valforward, *valright, *valup;
3910 valforward = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_forward);
3911 valright = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_right);
3912 valup = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_up);
3913 if (!valforward || !valright || !valup)
3915 VM_Warning("makevectors: could not find v_forward, v_right, or v_up global variables\n");
3918 VM_SAFEPARMCOUNT(1, VM_makevectors);
3919 AngleVectors (PRVM_G_VECTOR(OFS_PARM0), valforward->vector, valright->vector, valup->vector);
3926 Writes new values for v_forward, v_up, and v_right based on the given forward vector
3927 vectorvectors(vector)
3930 void VM_vectorvectors (void)
3932 prvm_eval_t *valforward, *valright, *valup;
3933 valforward = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_forward);
3934 valright = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_right);
3935 valup = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_up);
3936 if (!valforward || !valright || !valup)
3938 VM_Warning("vectorvectors: could not find v_forward, v_right, or v_up global variables\n");
3941 VM_SAFEPARMCOUNT(1, VM_vectorvectors);
3942 VectorNormalize2(PRVM_G_VECTOR(OFS_PARM0), valforward->vector);
3943 VectorVectors(valforward->vector, valright->vector, valup->vector);
3947 ========================
3950 void drawline(float width, vector pos1, vector pos2, vector rgb, float alpha, float flags)
3951 ========================
3953 void VM_drawline (void)
3955 float *c1, *c2, *rgb;
3957 unsigned char flags;
3959 VM_SAFEPARMCOUNT(6, VM_drawline);
3960 width = PRVM_G_FLOAT(OFS_PARM0);
3961 c1 = PRVM_G_VECTOR(OFS_PARM1);
3962 c2 = PRVM_G_VECTOR(OFS_PARM2);
3963 rgb = PRVM_G_VECTOR(OFS_PARM3);
3964 alpha = PRVM_G_FLOAT(OFS_PARM4);
3965 flags = (int)PRVM_G_FLOAT(OFS_PARM5);
3966 DrawQ_Line(width, c1[0], c1[1], c2[0], c2[1], rgb[0], rgb[1], rgb[2], alpha, flags);
3969 // float(float number, float quantity) bitshift (EXT_BITSHIFT)
3970 void VM_bitshift (void)
3973 VM_SAFEPARMCOUNT(2, VM_bitshift);
3975 n1 = (int)fabs((int)PRVM_G_FLOAT(OFS_PARM0));
3976 n2 = (int)PRVM_G_FLOAT(OFS_PARM1);
3978 PRVM_G_FLOAT(OFS_RETURN) = n1;
3981 PRVM_G_FLOAT(OFS_RETURN) = (n1 >> -n2);
3983 PRVM_G_FLOAT(OFS_RETURN) = (n1 << n2);
3986 ////////////////////////////////////////
3987 // AltString functions
3988 ////////////////////////////////////////
3991 ========================
3994 float altstr_count(string)
3995 ========================
3997 void VM_altstr_count( void )
3999 const char *altstr, *pos;
4002 VM_SAFEPARMCOUNT( 1, VM_altstr_count );
4004 altstr = PRVM_G_STRING( OFS_PARM0 );
4005 //VM_CheckEmptyString( altstr );
4007 for( count = 0, pos = altstr ; *pos ; pos++ ) {
4008 if( *pos == '\\' ) {
4012 } else if( *pos == '\'' ) {
4017 PRVM_G_FLOAT( OFS_RETURN ) = (float) (count / 2);
4021 ========================
4024 string altstr_prepare(string)
4025 ========================
4027 void VM_altstr_prepare( void )
4030 const char *instr, *in;
4032 char outstr[VM_STRINGTEMP_LENGTH];
4034 VM_SAFEPARMCOUNT( 1, VM_altstr_prepare );
4036 instr = PRVM_G_STRING( OFS_PARM0 );
4038 for( out = outstr, in = instr, size = sizeof(outstr) - 1 ; size && *in ; size--, in++, out++ )
4047 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
4051 ========================
4054 string altstr_get(string, float)
4055 ========================
4057 void VM_altstr_get( void )
4059 const char *altstr, *pos;
4062 char outstr[VM_STRINGTEMP_LENGTH];
4064 VM_SAFEPARMCOUNT( 2, VM_altstr_get );
4066 altstr = PRVM_G_STRING( OFS_PARM0 );
4068 count = (int)PRVM_G_FLOAT( OFS_PARM1 );
4069 count = count * 2 + 1;
4071 for( pos = altstr ; *pos && count ; pos++ )
4072 if( *pos == '\\' ) {
4075 } else if( *pos == '\'' )
4079 PRVM_G_INT( OFS_RETURN ) = 0;
4083 for( out = outstr, size = sizeof(outstr) - 1 ; size && *pos ; size--, pos++, out++ )
4084 if( *pos == '\\' ) {
4089 } else if( *pos == '\'' )
4095 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
4099 ========================
4102 string altstr_set(string altstr, float num, string set)
4103 ========================
4105 void VM_altstr_set( void )
4108 const char *altstr, *str;
4111 char outstr[VM_STRINGTEMP_LENGTH];
4113 VM_SAFEPARMCOUNT( 3, VM_altstr_set );
4115 altstr = PRVM_G_STRING( OFS_PARM0 );
4117 num = (int)PRVM_G_FLOAT( OFS_PARM1 );
4119 str = PRVM_G_STRING( OFS_PARM2 );
4122 for( num = num * 2 + 1, in = altstr; *in && num; *out++ = *in++ )
4127 } else if( *in == '\'' ) {
4132 for( ; *str; *out++ = *str++ );
4133 // now jump over the old content
4135 if( *in == '\'' || (*in == '\\' && !*++in) )
4138 strlcpy(out, in, outstr + sizeof(outstr) - out);
4139 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
4143 ========================
4146 string altstr_ins(string altstr, float num, string set)
4147 ========================
4149 void VM_altstr_ins(void)
4157 char outstr[VM_STRINGTEMP_LENGTH];
4159 VM_SAFEPARMCOUNT(3, VM_altstr_ins);
4161 in = instr = PRVM_G_STRING( OFS_PARM0 );
4162 num = (int)PRVM_G_FLOAT( OFS_PARM1 );
4163 set = setstr = PRVM_G_STRING( OFS_PARM2 );
4166 for( num = num * 2 + 2 ; *in && num > 0 ; *out++ = *in++ )
4171 } else if( *in == '\'' ) {
4176 for( ; *set ; *out++ = *set++ );
4179 strlcpy(out, in, outstr + sizeof(outstr) - out);
4180 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
4184 ////////////////////////////////////////
4185 // BufString functions
4186 ////////////////////////////////////////
4187 //[515]: string buffers support
4189 static size_t stringbuffers_sortlength;
4191 static void BufStr_Expand(prvm_stringbuffer_t *stringbuffer, int strindex)
4193 if (stringbuffer->max_strings <= strindex)
4195 char **oldstrings = stringbuffer->strings;
4196 stringbuffer->max_strings = max(stringbuffer->max_strings * 2, 128);
4197 while (stringbuffer->max_strings <= strindex)
4198 stringbuffer->max_strings *= 2;
4199 stringbuffer->strings = (char **) Mem_Alloc(prog->progs_mempool, stringbuffer->max_strings * sizeof(stringbuffer->strings[0]));
4200 if (stringbuffer->num_strings > 0)
4201 memcpy(stringbuffer->strings, oldstrings, stringbuffer->num_strings * sizeof(stringbuffer->strings[0]));
4203 Mem_Free(oldstrings);
4207 static void BufStr_Shrink(prvm_stringbuffer_t *stringbuffer)
4209 // reduce num_strings if there are empty string slots at the end
4210 while (stringbuffer->num_strings > 0 && stringbuffer->strings[stringbuffer->num_strings - 1] == NULL)
4211 stringbuffer->num_strings--;
4213 // if empty, free the string pointer array
4214 if (stringbuffer->num_strings == 0)
4216 stringbuffer->max_strings = 0;
4217 if (stringbuffer->strings)
4218 Mem_Free(stringbuffer->strings);
4219 stringbuffer->strings = NULL;
4223 static int BufStr_SortStringsUP (const void *in1, const void *in2)
4226 a = *((const char **) in1);
4227 b = *((const char **) in2);
4229 if(!b[0]) return -1;
4230 return strncmp(a, b, stringbuffers_sortlength);
4233 static int BufStr_SortStringsDOWN (const void *in1, const void *in2)
4236 a = *((const char **) in1);
4237 b = *((const char **) in2);
4239 if(!b[0]) return -1;
4240 return strncmp(b, a, stringbuffers_sortlength);
4244 ========================
4246 creates new buffer, and returns it's index, returns -1 if failed
4247 float buf_create(void) = #460;
4248 ========================
4250 void VM_buf_create (void)
4252 prvm_stringbuffer_t *stringbuffer;
4254 VM_SAFEPARMCOUNT(0, VM_buf_create);
4255 stringbuffer = (prvm_stringbuffer_t *) Mem_ExpandableArray_AllocRecord(&prog->stringbuffersarray);
4256 for (i = 0;stringbuffer != Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, i);i++);
4257 stringbuffer->origin = PRVM_AllocationOrigin();
4258 PRVM_G_FLOAT(OFS_RETURN) = i;
4262 ========================
4264 deletes buffer and all strings in it
4265 void buf_del(float bufhandle) = #461;
4266 ========================
4268 void VM_buf_del (void)
4270 prvm_stringbuffer_t *stringbuffer;
4271 VM_SAFEPARMCOUNT(1, VM_buf_del);
4272 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4276 for (i = 0;i < stringbuffer->num_strings;i++)
4277 if (stringbuffer->strings[i])
4278 Mem_Free(stringbuffer->strings[i]);
4279 if (stringbuffer->strings)
4280 Mem_Free(stringbuffer->strings);
4281 if(stringbuffer->origin)
4282 PRVM_Free((char *)stringbuffer->origin);
4283 Mem_ExpandableArray_FreeRecord(&prog->stringbuffersarray, stringbuffer);
4287 VM_Warning("VM_buf_del: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4293 ========================
4295 how many strings are stored in buffer
4296 float buf_getsize(float bufhandle) = #462;
4297 ========================
4299 void VM_buf_getsize (void)
4301 prvm_stringbuffer_t *stringbuffer;
4302 VM_SAFEPARMCOUNT(1, VM_buf_getsize);
4304 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4307 PRVM_G_FLOAT(OFS_RETURN) = -1;
4308 VM_Warning("VM_buf_getsize: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4312 PRVM_G_FLOAT(OFS_RETURN) = stringbuffer->num_strings;
4316 ========================
4318 copy all content from one buffer to another, make sure it exists
4319 void buf_copy(float bufhandle_from, float bufhandle_to) = #463;
4320 ========================
4322 void VM_buf_copy (void)
4324 prvm_stringbuffer_t *srcstringbuffer, *dststringbuffer;
4326 VM_SAFEPARMCOUNT(2, VM_buf_copy);
4328 srcstringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4329 if(!srcstringbuffer)
4331 VM_Warning("VM_buf_copy: invalid source buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4334 i = (int)PRVM_G_FLOAT(OFS_PARM1);
4335 if(i == (int)PRVM_G_FLOAT(OFS_PARM0))
4337 VM_Warning("VM_buf_copy: source == destination (%i) in %s\n", i, PRVM_NAME);
4340 dststringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4341 if(!dststringbuffer)
4343 VM_Warning("VM_buf_copy: invalid destination buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM1), PRVM_NAME);
4347 for (i = 0;i < dststringbuffer->num_strings;i++)
4348 if (dststringbuffer->strings[i])
4349 Mem_Free(dststringbuffer->strings[i]);
4350 if (dststringbuffer->strings)
4351 Mem_Free(dststringbuffer->strings);
4352 *dststringbuffer = *srcstringbuffer;
4353 if (dststringbuffer->max_strings)
4354 dststringbuffer->strings = (char **)Mem_Alloc(prog->progs_mempool, sizeof(dststringbuffer->strings[0]) * dststringbuffer->max_strings);
4356 for (i = 0;i < dststringbuffer->num_strings;i++)
4358 if (srcstringbuffer->strings[i])
4361 stringlen = strlen(srcstringbuffer->strings[i]) + 1;
4362 dststringbuffer->strings[i] = (char *)Mem_Alloc(prog->progs_mempool, stringlen);
4363 memcpy(dststringbuffer->strings[i], srcstringbuffer->strings[i], stringlen);
4369 ========================
4371 sort buffer by beginnings of strings (cmplength defaults it's length)
4372 "backward == TRUE" means that sorting goes upside-down
4373 void buf_sort(float bufhandle, float cmplength, float backward) = #464;
4374 ========================
4376 void VM_buf_sort (void)
4378 prvm_stringbuffer_t *stringbuffer;
4379 VM_SAFEPARMCOUNT(3, VM_buf_sort);
4381 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4384 VM_Warning("VM_buf_sort: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4387 if(stringbuffer->num_strings <= 0)
4389 VM_Warning("VM_buf_sort: tried to sort empty buffer %i in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4392 stringbuffers_sortlength = (int)PRVM_G_FLOAT(OFS_PARM1);
4393 if(stringbuffers_sortlength <= 0)
4394 stringbuffers_sortlength = 0x7FFFFFFF;
4396 if(!PRVM_G_FLOAT(OFS_PARM2))
4397 qsort(stringbuffer->strings, stringbuffer->num_strings, sizeof(char*), BufStr_SortStringsUP);
4399 qsort(stringbuffer->strings, stringbuffer->num_strings, sizeof(char*), BufStr_SortStringsDOWN);
4401 BufStr_Shrink(stringbuffer);
4405 ========================
4407 concantenates all buffer string into one with "glue" separator and returns it as tempstring
4408 string buf_implode(float bufhandle, string glue) = #465;
4409 ========================
4411 void VM_buf_implode (void)
4413 prvm_stringbuffer_t *stringbuffer;
4414 char k[VM_STRINGTEMP_LENGTH];
4418 VM_SAFEPARMCOUNT(2, VM_buf_implode);
4420 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4421 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
4424 VM_Warning("VM_buf_implode: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4427 if(!stringbuffer->num_strings)
4429 sep = PRVM_G_STRING(OFS_PARM1);
4431 for(l = i = 0;i < stringbuffer->num_strings;i++)
4433 if(stringbuffer->strings[i])
4435 l += (i > 0 ? strlen(sep) : 0) + strlen(stringbuffer->strings[i]);
4436 if (l >= sizeof(k) - 1)
4438 strlcat(k, sep, sizeof(k));
4439 strlcat(k, stringbuffer->strings[i], sizeof(k));
4442 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(k);
4446 ========================
4448 get a string from buffer, returns tempstring, dont str_unzone it!
4449 string bufstr_get(float bufhandle, float string_index) = #465;
4450 ========================
4452 void VM_bufstr_get (void)
4454 prvm_stringbuffer_t *stringbuffer;
4456 VM_SAFEPARMCOUNT(2, VM_bufstr_get);
4458 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
4459 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4462 VM_Warning("VM_bufstr_get: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4465 strindex = (int)PRVM_G_FLOAT(OFS_PARM1);
4468 VM_Warning("VM_bufstr_get: invalid string index %i used in %s\n", strindex, PRVM_NAME);
4471 if (strindex < stringbuffer->num_strings && stringbuffer->strings[strindex])
4472 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(stringbuffer->strings[strindex]);
4476 ========================
4478 copies a string into selected slot of buffer
4479 void bufstr_set(float bufhandle, float string_index, string str) = #466;
4480 ========================
4482 void VM_bufstr_set (void)
4485 prvm_stringbuffer_t *stringbuffer;
4488 VM_SAFEPARMCOUNT(3, VM_bufstr_set);
4490 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4493 VM_Warning("VM_bufstr_set: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4496 strindex = (int)PRVM_G_FLOAT(OFS_PARM1);
4497 if(strindex < 0 || strindex >= 1000000) // huge number of strings
4499 VM_Warning("VM_bufstr_set: invalid string index %i used in %s\n", strindex, PRVM_NAME);
4503 BufStr_Expand(stringbuffer, strindex);
4504 stringbuffer->num_strings = max(stringbuffer->num_strings, strindex + 1);
4506 if(stringbuffer->strings[strindex])
4507 Mem_Free(stringbuffer->strings[strindex]);
4508 stringbuffer->strings[strindex] = NULL;
4510 news = PRVM_G_STRING(OFS_PARM2);
4511 if (news && news[0])
4513 size_t alloclen = strlen(news) + 1;
4514 stringbuffer->strings[strindex] = (char *)Mem_Alloc(prog->progs_mempool, alloclen);
4515 memcpy(stringbuffer->strings[strindex], news, alloclen);
4518 BufStr_Shrink(stringbuffer);
4522 ========================
4524 adds string to buffer in first free slot and returns its index
4525 "order == TRUE" means that string will be added after last "full" slot
4526 float bufstr_add(float bufhandle, string str, float order) = #467;
4527 ========================
4529 void VM_bufstr_add (void)
4531 int order, strindex;
4532 prvm_stringbuffer_t *stringbuffer;
4536 VM_SAFEPARMCOUNT(3, VM_bufstr_add);
4538 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4539 PRVM_G_FLOAT(OFS_RETURN) = -1;
4542 VM_Warning("VM_bufstr_add: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4545 string = PRVM_G_STRING(OFS_PARM1);
4546 if(!string || !string[0])
4548 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);
4551 order = (int)PRVM_G_FLOAT(OFS_PARM2);
4553 strindex = stringbuffer->num_strings;
4555 for (strindex = 0;strindex < stringbuffer->num_strings;strindex++)
4556 if (stringbuffer->strings[strindex] == NULL)
4559 BufStr_Expand(stringbuffer, strindex);
4561 stringbuffer->num_strings = max(stringbuffer->num_strings, strindex + 1);
4562 alloclen = strlen(string) + 1;
4563 stringbuffer->strings[strindex] = (char *)Mem_Alloc(prog->progs_mempool, alloclen);
4564 memcpy(stringbuffer->strings[strindex], string, alloclen);
4566 PRVM_G_FLOAT(OFS_RETURN) = strindex;
4570 ========================
4572 delete string from buffer
4573 void bufstr_free(float bufhandle, float string_index) = #468;
4574 ========================
4576 void VM_bufstr_free (void)
4579 prvm_stringbuffer_t *stringbuffer;
4580 VM_SAFEPARMCOUNT(2, VM_bufstr_free);
4582 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4585 VM_Warning("VM_bufstr_free: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4588 i = (int)PRVM_G_FLOAT(OFS_PARM1);
4591 VM_Warning("VM_bufstr_free: invalid string index %i used in %s\n", i, PRVM_NAME);
4595 if (i < stringbuffer->num_strings)
4597 if(stringbuffer->strings[i])
4598 Mem_Free(stringbuffer->strings[i]);
4599 stringbuffer->strings[i] = NULL;
4602 BufStr_Shrink(stringbuffer);
4611 void VM_buf_cvarlist(void)
4614 const char *partial, *antipartial;
4615 size_t len, antilen;
4617 qboolean ispattern, antiispattern;
4619 prvm_stringbuffer_t *stringbuffer;
4620 VM_SAFEPARMCOUNTRANGE(2, 3, VM_buf_cvarlist);
4622 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4625 VM_Warning("VM_bufstr_free: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4629 partial = PRVM_G_STRING(OFS_PARM1);
4633 len = strlen(partial);
4636 antipartial = PRVM_G_STRING(OFS_PARM2);
4642 antilen = strlen(antipartial);
4644 for (n = 0;n < stringbuffer->num_strings;n++)
4645 if (stringbuffer->strings[n])
4646 Mem_Free(stringbuffer->strings[n]);
4647 if (stringbuffer->strings)
4648 Mem_Free(stringbuffer->strings);
4649 stringbuffer->strings = NULL;
4651 ispattern = partial && (strchr(partial, '*') || strchr(partial, '?'));
4652 antiispattern = antipartial && (strchr(antipartial, '*') || strchr(antipartial, '?'));
4655 for(cvar = cvar_vars; cvar; cvar = cvar->next)
4657 if(len && (ispattern ? !matchpattern_with_separator(cvar->name, partial, false, "", false) : strncmp(partial, cvar->name, len)))
4660 if(antilen && (antiispattern ? matchpattern_with_separator(cvar->name, antipartial, false, "", false) : !strncmp(antipartial, cvar->name, antilen)))
4666 stringbuffer->max_strings = stringbuffer->num_strings = n;
4667 if (stringbuffer->max_strings)
4668 stringbuffer->strings = (char **)Mem_Alloc(prog->progs_mempool, sizeof(stringbuffer->strings[0]) * stringbuffer->max_strings);
4671 for(cvar = cvar_vars; cvar; cvar = cvar->next)
4673 if(len && (ispattern ? !matchpattern_with_separator(cvar->name, partial, false, "", false) : strncmp(partial, cvar->name, len)))
4676 if(antilen && (antiispattern ? matchpattern_with_separator(cvar->name, antipartial, false, "", false) : !strncmp(antipartial, cvar->name, antilen)))
4679 alloclen = strlen(cvar->name) + 1;
4680 stringbuffer->strings[n] = (char *)Mem_Alloc(prog->progs_mempool, alloclen);
4681 memcpy(stringbuffer->strings[n], cvar->name, alloclen);
4696 This was a major timewaster in progs, so it was converted to C
4699 void VM_changeyaw (void)
4702 float ideal, current, move, speed;
4704 // this is called (VERY HACKISHLY) by SV_MoveToGoal, so it can not use any
4705 // parameters because they are the parameters to SV_MoveToGoal, not this
4706 //VM_SAFEPARMCOUNT(0, VM_changeyaw);
4708 ent = PRVM_PROG_TO_EDICT(PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict);
4709 if (ent == prog->edicts)
4711 VM_Warning("changeyaw: can not modify world entity\n");
4714 if (ent->priv.server->free)
4716 VM_Warning("changeyaw: can not modify free entity\n");
4719 if (prog->fieldoffsets.angles < 0 || prog->fieldoffsets.ideal_yaw < 0 || prog->fieldoffsets.yaw_speed < 0)
4721 VM_Warning("changeyaw: angles, ideal_yaw, or yaw_speed field(s) not found\n");
4724 current = ANGLEMOD(PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.angles)->vector[1]);
4725 ideal = PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.ideal_yaw)->_float;
4726 speed = PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.yaw_speed)->_float;
4728 if (current == ideal)
4730 move = ideal - current;
4731 if (ideal > current)
4752 PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.angles)->vector[1] = ANGLEMOD (current + move);
4760 void VM_changepitch (void)
4763 float ideal, current, move, speed;
4765 VM_SAFEPARMCOUNT(1, VM_changepitch);
4767 ent = PRVM_G_EDICT(OFS_PARM0);
4768 if (ent == prog->edicts)
4770 VM_Warning("changepitch: can not modify world entity\n");
4773 if (ent->priv.server->free)
4775 VM_Warning("changepitch: can not modify free entity\n");
4778 if (prog->fieldoffsets.angles < 0 || prog->fieldoffsets.idealpitch < 0 || prog->fieldoffsets.pitch_speed < 0)
4780 VM_Warning("changepitch: angles, idealpitch, or pitch_speed field(s) not found\n");
4783 current = ANGLEMOD(PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.angles)->vector[0]);
4784 ideal = PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.idealpitch)->_float;
4785 speed = PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.pitch_speed)->_float;
4787 if (current == ideal)
4789 move = ideal - current;
4790 if (ideal > current)
4811 PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.angles)->vector[0] = ANGLEMOD (current + move);
4815 void VM_uncolorstring (void)
4817 char szNewString[VM_STRINGTEMP_LENGTH];
4818 const char *szString;
4821 VM_SAFEPARMCOUNT(1, VM_uncolorstring);
4822 szString = PRVM_G_STRING(OFS_PARM0);
4823 COM_StringDecolorize(szString, 0, szNewString, sizeof(szNewString), TRUE);
4824 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(szNewString);
4828 // #221 float(string str, string sub[, float startpos]) strstrofs (FTE_STRINGS)
4829 //strstr, without generating a new string. Use in conjunction with FRIK_FILE's substring for more similar strstr.
4830 void VM_strstrofs (void)
4832 const char *instr, *match;
4834 VM_SAFEPARMCOUNTRANGE(2, 3, VM_strstrofs);
4835 instr = PRVM_G_STRING(OFS_PARM0);
4836 match = PRVM_G_STRING(OFS_PARM1);
4837 firstofs = (prog->argc > 2)?(int)PRVM_G_FLOAT(OFS_PARM2):0;
4838 firstofs = u8_bytelen(instr, firstofs);
4840 if (firstofs && (firstofs < 0 || firstofs > (int)strlen(instr)))
4842 PRVM_G_FLOAT(OFS_RETURN) = -1;
4846 match = strstr(instr+firstofs, match);
4848 PRVM_G_FLOAT(OFS_RETURN) = -1;
4850 PRVM_G_FLOAT(OFS_RETURN) = match - instr;
4853 //#222 string(string s, float index) str2chr (FTE_STRINGS)
4854 void VM_str2chr (void)
4859 VM_SAFEPARMCOUNT(2, VM_str2chr);
4860 s = PRVM_G_STRING(OFS_PARM0);
4861 index = u8_bytelen(s, (int)PRVM_G_FLOAT(OFS_PARM1));
4863 if((unsigned)index < strlen(s))
4865 ch = u8_getchar(s + index, NULL);
4866 PRVM_G_FLOAT(OFS_RETURN) = ch;
4869 PRVM_G_FLOAT(OFS_RETURN) = 0;
4872 //#223 string(float c, ...) chr2str (FTE_STRINGS)
4873 void VM_chr2str (void)
4878 VM_SAFEPARMCOUNTRANGE(0, 8, VM_chr2str);
4879 for(i = 0;i < prog->argc && i < (int)sizeof(t) - 1;i++)
4880 t[i] = (unsigned char)PRVM_G_FLOAT(OFS_PARM0+i*3);
4882 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(t);
4887 VM_SAFEPARMCOUNTRANGE(0, 8, VM_chr2str);
4888 for(i = 0; i < prog->argc && len < sizeof(t)-1; ++i)
4890 int add = u8_fromchar((Uchar)PRVM_G_FLOAT(OFS_PARM0+i*3), t + len, sizeof(t)-1);
4895 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(t);
4898 static int chrconv_number(int i, int base, int conv)
4923 static int chrconv_punct(int i, int base, int conv)
4941 static int chrchar_alpha(int i, int basec, int baset, int convc, int convt, int charnum)
4943 //convert case and colour seperatly...
4960 baset = 128*((charnum&1) == (convt-5));
4976 return i + basec + baset;
4978 // #224 string(float ccase, float calpha, float cnum, string s, ...) strconv (FTE_STRINGS)
4979 //bulk convert a string. change case or colouring.
4980 void VM_strconv (void)
4982 int ccase, redalpha, rednum, len, i;
4983 unsigned char resbuf[VM_STRINGTEMP_LENGTH];
4984 unsigned char *result = resbuf;
4986 VM_SAFEPARMCOUNTRANGE(3, 8, VM_strconv);
4988 ccase = (int) PRVM_G_FLOAT(OFS_PARM0); //0 same, 1 lower, 2 upper
4989 redalpha = (int) PRVM_G_FLOAT(OFS_PARM1); //0 same, 1 white, 2 red, 5 alternate, 6 alternate-alternate
4990 rednum = (int) PRVM_G_FLOAT(OFS_PARM2); //0 same, 1 white, 2 red, 3 redspecial, 4 whitespecial, 5 alternate, 6 alternate-alternate
4991 VM_VarString(3, (char *) resbuf, sizeof(resbuf));
4992 len = strlen((char *) resbuf);
4994 for (i = 0; i < len; i++, result++) //should this be done backwards?
4996 if (*result >= '0' && *result <= '9') //normal numbers...
4997 *result = chrconv_number(*result, '0', rednum);
4998 else if (*result >= '0'+128 && *result <= '9'+128)
4999 *result = chrconv_number(*result, '0'+128, rednum);
5000 else if (*result >= '0'+128-30 && *result <= '9'+128-30)
5001 *result = chrconv_number(*result, '0'+128-30, rednum);
5002 else if (*result >= '0'-30 && *result <= '9'-30)
5003 *result = chrconv_number(*result, '0'-30, rednum);
5005 else if (*result >= 'a' && *result <= 'z') //normal numbers...
5006 *result = chrchar_alpha(*result, 'a', 0, ccase, redalpha, i);
5007 else if (*result >= 'A' && *result <= 'Z') //normal numbers...
5008 *result = chrchar_alpha(*result, 'A', 0, ccase, redalpha, i);
5009 else if (*result >= 'a'+128 && *result <= 'z'+128) //normal numbers...
5010 *result = chrchar_alpha(*result, 'a', 128, ccase, redalpha, i);
5011 else if (*result >= 'A'+128 && *result <= 'Z'+128) //normal numbers...
5012 *result = chrchar_alpha(*result, 'A', 128, ccase, redalpha, i);
5014 else if ((*result & 127) < 16 || !redalpha) //special chars..
5016 else if (*result < 128)
5017 *result = chrconv_punct(*result, 0, redalpha);
5019 *result = chrconv_punct(*result, 128, redalpha);
5023 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString((char *) resbuf);
5026 // #225 string(float chars, string s, ...) strpad (FTE_STRINGS)
5027 void VM_strpad (void)
5029 char src[VM_STRINGTEMP_LENGTH];
5030 char destbuf[VM_STRINGTEMP_LENGTH];
5032 VM_SAFEPARMCOUNTRANGE(1, 8, VM_strpad);
5033 pad = (int) PRVM_G_FLOAT(OFS_PARM0);
5034 VM_VarString(1, src, sizeof(src));
5036 // note: < 0 = left padding, > 0 = right padding,
5037 // this is reverse logic of printf!
5038 dpsnprintf(destbuf, sizeof(destbuf), "%*s", -pad, src);
5040 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(destbuf);
5043 // #226 string(string info, string key, string value, ...) infoadd (FTE_STRINGS)
5044 //uses qw style \key\value strings
5045 void VM_infoadd (void)
5047 const char *info, *key;
5048 char value[VM_STRINGTEMP_LENGTH];
5049 char temp[VM_STRINGTEMP_LENGTH];
5051 VM_SAFEPARMCOUNTRANGE(2, 8, VM_infoadd);
5052 info = PRVM_G_STRING(OFS_PARM0);
5053 key = PRVM_G_STRING(OFS_PARM1);
5054 VM_VarString(2, value, sizeof(value));
5056 strlcpy(temp, info, VM_STRINGTEMP_LENGTH);
5058 InfoString_SetValue(temp, VM_STRINGTEMP_LENGTH, key, value);
5060 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(temp);
5063 // #227 string(string info, string key) infoget (FTE_STRINGS)
5064 //uses qw style \key\value strings
5065 void VM_infoget (void)
5069 char value[VM_STRINGTEMP_LENGTH];
5071 VM_SAFEPARMCOUNT(2, VM_infoget);
5072 info = PRVM_G_STRING(OFS_PARM0);
5073 key = PRVM_G_STRING(OFS_PARM1);
5075 InfoString_GetValue(info, key, value, VM_STRINGTEMP_LENGTH);
5077 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(value);
5080 //#228 float(string s1, string s2, float len) strncmp (FTE_STRINGS)
5081 // also float(string s1, string s2) strcmp (FRIK_FILE)
5082 void VM_strncmp (void)
5084 const char *s1, *s2;
5085 VM_SAFEPARMCOUNTRANGE(2, 3, VM_strncmp);
5086 s1 = PRVM_G_STRING(OFS_PARM0);
5087 s2 = PRVM_G_STRING(OFS_PARM1);
5090 PRVM_G_FLOAT(OFS_RETURN) = strncmp(s1, s2, (size_t)PRVM_G_FLOAT(OFS_PARM2));
5094 PRVM_G_FLOAT(OFS_RETURN) = strcmp(s1, s2);
5098 // #229 float(string s1, string s2) strcasecmp (FTE_STRINGS)
5099 // #230 float(string s1, string s2, float len) strncasecmp (FTE_STRINGS)
5100 void VM_strncasecmp (void)
5102 const char *s1, *s2;
5103 VM_SAFEPARMCOUNTRANGE(2, 3, VM_strncasecmp);
5104 s1 = PRVM_G_STRING(OFS_PARM0);
5105 s2 = PRVM_G_STRING(OFS_PARM1);
5108 PRVM_G_FLOAT(OFS_RETURN) = strncasecmp(s1, s2, (size_t)PRVM_G_FLOAT(OFS_PARM2));
5112 PRVM_G_FLOAT(OFS_RETURN) = strcasecmp(s1, s2);
5116 // #494 float(float caseinsensitive, string s, ...) crc16
5120 static char s[VM_STRINGTEMP_LENGTH];
5121 VM_SAFEPARMCOUNTRANGE(2, 8, VM_hash);
5122 insensitive = PRVM_G_FLOAT(OFS_PARM0);
5123 VM_VarString(1, s, sizeof(s));
5124 PRVM_G_FLOAT(OFS_RETURN) = (unsigned short) ((insensitive ? CRC_Block_CaseInsensitive : CRC_Block) ((unsigned char *) s, strlen(s)));
5127 void VM_wasfreed (void)
5129 VM_SAFEPARMCOUNT(1, VM_wasfreed);
5130 PRVM_G_FLOAT(OFS_RETURN) = PRVM_G_EDICT(OFS_PARM0)->priv.required->free;
5133 void VM_SetTraceGlobals(const trace_t *trace)
5136 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_allsolid)))
5137 val->_float = trace->allsolid;
5138 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_startsolid)))
5139 val->_float = trace->startsolid;
5140 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_fraction)))
5141 val->_float = trace->fraction;
5142 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_inwater)))
5143 val->_float = trace->inwater;
5144 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_inopen)))
5145 val->_float = trace->inopen;
5146 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_endpos)))
5147 VectorCopy(trace->endpos, val->vector);
5148 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_plane_normal)))
5149 VectorCopy(trace->plane.normal, val->vector);
5150 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_plane_dist)))
5151 val->_float = trace->plane.dist;
5152 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_ent)))
5153 val->edict = PRVM_EDICT_TO_PROG(trace->ent ? trace->ent : prog->edicts);
5154 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dpstartcontents)))
5155 val->_float = trace->startsupercontents;
5156 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dphitcontents)))
5157 val->_float = trace->hitsupercontents;
5158 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dphitq3surfaceflags)))
5159 val->_float = trace->hitq3surfaceflags;
5160 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dphittexturename)))
5161 val->string = trace->hittexture ? PRVM_SetTempString(trace->hittexture->name) : 0;
5166 void VM_Cmd_Init(void)
5168 // only init the stuff for the current prog
5172 // VM_BufStr_Init();
5175 void VM_Cmd_Reset(void)
5177 CL_PurgeOwner( MENUOWNER );
5179 VM_Files_CloseAll();
5181 // VM_BufStr_ShutDown();
5184 // #510 string(string input, ...) uri_escape (DP_QC_URI_ESCAPE)
5185 // does URI escaping on a string (replace evil stuff by %AB escapes)
5186 void VM_uri_escape (void)
5188 char src[VM_STRINGTEMP_LENGTH];
5189 char dest[VM_STRINGTEMP_LENGTH];
5191 static const char *hex = "0123456789ABCDEF";
5193 VM_SAFEPARMCOUNTRANGE(1, 8, VM_uri_escape);
5194 VM_VarString(0, src, sizeof(src));
5196 for(p = src, q = dest; *p && q < dest + sizeof(dest) - 3; ++p)
5198 if((*p >= 'A' && *p <= 'Z')
5199 || (*p >= 'a' && *p <= 'z')
5200 || (*p >= '0' && *p <= '9')
5201 || (*p == '-') || (*p == '_') || (*p == '.')
5202 || (*p == '!') || (*p == '~') || (*p == '*')
5203 || (*p == '\'') || (*p == '(') || (*p == ')'))
5208 *q++ = hex[(*(unsigned char *)p >> 4) & 0xF];
5209 *q++ = hex[ *(unsigned char *)p & 0xF];
5214 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(dest);
5217 // #510 string(string input, ...) uri_unescape (DP_QC_URI_ESCAPE)
5218 // does URI unescaping on a string (get back the evil stuff)
5219 void VM_uri_unescape (void)
5221 char src[VM_STRINGTEMP_LENGTH];
5222 char dest[VM_STRINGTEMP_LENGTH];
5226 VM_SAFEPARMCOUNTRANGE(1, 8, VM_uri_unescape);
5227 VM_VarString(0, src, sizeof(src));
5229 for(p = src, q = dest; *p; ) // no need to check size, because unescape can't expand
5233 if(p[1] >= '0' && p[1] <= '9')
5235 else if(p[1] >= 'a' && p[1] <= 'f')
5236 hi = p[1] - 'a' + 10;
5237 else if(p[1] >= 'A' && p[1] <= 'F')
5238 hi = p[1] - 'A' + 10;
5241 if(p[2] >= '0' && p[2] <= '9')
5243 else if(p[2] >= 'a' && p[2] <= 'f')
5244 lo = p[2] - 'a' + 10;
5245 else if(p[2] >= 'A' && p[2] <= 'F')
5246 lo = p[2] - 'A' + 10;
5249 if(hi != 0 || lo != 0) // don't unescape NUL bytes
5250 *q++ = (char) (hi * 0x10 + lo);
5261 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(dest);
5264 // #502 string(string filename) whichpack (DP_QC_WHICHPACK)
5265 // returns the name of the pack containing a file, or "" if it is not in any pack (but local or non-existant)
5266 void VM_whichpack (void)
5268 const char *fn, *pack;
5270 VM_SAFEPARMCOUNT(1, VM_whichpack);
5271 fn = PRVM_G_STRING(OFS_PARM0);
5272 pack = FS_WhichPack(fn);
5274 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(pack ? pack : "");
5282 char buffer[MAX_INPUTLINE];
5286 static void uri_to_string_callback(int status, size_t length_received, unsigned char *buffer, void *cbdata)
5288 uri_to_prog_t *handle = (uri_to_prog_t *) cbdata;
5290 if(!PRVM_ProgLoaded(handle->prognr))
5292 // curl reply came too late... so just drop it
5297 PRVM_SetProg(handle->prognr);
5299 if((prog->starttime == handle->starttime) && (prog->funcoffsets.URI_Get_Callback))
5301 if(length_received >= sizeof(handle->buffer))
5302 length_received = sizeof(handle->buffer) - 1;
5303 handle->buffer[length_received] = 0;
5305 PRVM_G_FLOAT(OFS_PARM0) = handle->id;
5306 PRVM_G_FLOAT(OFS_PARM1) = status;
5307 PRVM_G_INT(OFS_PARM2) = PRVM_SetTempString(handle->buffer);
5308 PRVM_ExecuteProgram(prog->funcoffsets.URI_Get_Callback, "QC function URI_Get_Callback is missing");
5315 // 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
5316 // 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
5317 void VM_uri_get (void)
5322 uri_to_prog_t *handle;
5324 if(!prog->funcoffsets.URI_Get_Callback)
5325 PRVM_ERROR("uri_get called by %s without URI_Get_Callback defined", PRVM_NAME);
5327 VM_SAFEPARMCOUNT(2, VM_uri_get);
5329 url = PRVM_G_STRING(OFS_PARM0);
5330 id = PRVM_G_FLOAT(OFS_PARM1);
5331 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!
5333 handle->prognr = PRVM_GetProgNr();
5334 handle->starttime = prog->starttime;
5336 ret = Curl_Begin_ToMemory(url, (unsigned char *) handle->buffer, sizeof(handle->buffer), uri_to_string_callback, handle);
5339 PRVM_G_INT(OFS_RETURN) = 1;
5344 PRVM_G_INT(OFS_RETURN) = 0;
5348 void VM_netaddress_resolve (void)
5351 char normalized[128];
5353 lhnetaddress_t addr;
5355 VM_SAFEPARMCOUNTRANGE(1, 2, VM_netaddress_resolve);
5357 ip = PRVM_G_STRING(OFS_PARM0);
5360 port = (int) PRVM_G_FLOAT(OFS_PARM1);
5362 if(LHNETADDRESS_FromString(&addr, ip, port) && LHNETADDRESS_ToString(&addr, normalized, sizeof(normalized), prog->argc > 1))
5363 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(normalized);
5365 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString("");
5368 //string(void) getextresponse = #624; // returns the next extResponse packet that was sent to this client
5369 void VM_getextresponse (void)
5371 VM_SAFEPARMCOUNT(0,VM_argv);
5373 if (net_extresponse_count <= 0)
5374 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
5378 --net_extresponse_count;
5379 first = (net_extresponse_last + NET_EXTRESPONSE_MAX - net_extresponse_count) % NET_EXTRESPONSE_MAX;
5380 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(net_extresponse[first]);