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
12 // LordHavoc: changed this to NOT use a return statement, so that it can be used in functions that must return a value
13 void VM_Warning(const char *fmt, ...)
16 char msg[MAX_INPUTLINE];
19 dpvsnprintf(msg,sizeof(msg),fmt,argptr);
23 // TODO: either add a cvar/cmd to control the state dumping or replace some of the calls with Con_Printf [9/13/2006 Black]
28 //============================================================================
31 // TODO DONE: move vm_files and vm_fssearchlist to prvm_prog_t struct
32 // TODO: move vm_files and vm_fssearchlist back [9/13/2006 Black]
33 // TODO: (move vm_files and vm_fssearchlist to prvm_prog_t struct again) [2007-01-23 LordHavoc]
34 // TODO: will this war ever end? [2007-01-23 LordHavoc]
36 void VM_CheckEmptyString (const char *s)
39 PRVM_ERROR ("%s: Bad string", PRVM_NAME);
42 //============================================================================
45 void VM_VarString(int first, char *out, int outlength)
51 outend = out + outlength - 1;
52 for (i = first;i < prog->argc && out < outend;i++)
54 s = PRVM_G_STRING((OFS_PARM0+i*3));
55 while (out < outend && *s)
65 returns true if the extension is supported by the server
67 checkextension(extensionname)
71 // kind of helper function
72 static qboolean checkextension(const char *name)
76 len = (int)strlen(name);
78 for (e = prog->extensionstring;*e;e++)
85 while (*e && *e != ' ')
87 if ((e - start) == len && !strncasecmp(start, name, len))
93 void VM_checkextension (void)
95 VM_SAFEPARMCOUNT(1,VM_checkextension);
97 PRVM_G_FLOAT(OFS_RETURN) = checkextension(PRVM_G_STRING(OFS_PARM0));
104 This is a TERMINAL error, which will kill off the entire prog.
113 char string[VM_STRINGTEMP_LENGTH];
115 VM_VarString(0, string, sizeof(string));
116 Con_Printf("======%s ERROR in %s:\n%s\n", PRVM_NAME, PRVM_GetString(prog->xfunction->s_name), string);
117 if (prog->globaloffsets.self >= 0)
119 ed = PRVM_PROG_TO_EDICT(PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict);
120 PRVM_ED_Print(ed, NULL);
123 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);
130 Dumps out self, then an error message. The program is aborted and self is
131 removed, but the level can continue.
136 void VM_objerror (void)
139 char string[VM_STRINGTEMP_LENGTH];
141 VM_VarString(0, string, sizeof(string));
142 Con_Printf("======OBJECT ERROR======\n"); // , PRVM_NAME, PRVM_GetString(prog->xfunction->s_name), string); // or include them? FIXME
143 if (prog->globaloffsets.self >= 0)
145 ed = PRVM_PROG_TO_EDICT(PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict);
146 PRVM_ED_Print(ed, NULL);
151 // objerror has to display the object fields -> else call
152 PRVM_ERROR ("VM_objecterror: self not defined !");
153 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);
167 char string[VM_STRINGTEMP_LENGTH];
169 VM_VarString(0, string, sizeof(string));
177 broadcast print to everyone on server
182 void VM_bprint (void)
184 char string[VM_STRINGTEMP_LENGTH];
188 VM_Warning("VM_bprint: game is not server(%s) !\n", PRVM_NAME);
192 VM_VarString(0, string, sizeof(string));
193 SV_BroadcastPrint(string);
198 VM_sprint (menu & client but only if server.active == true)
200 single print to a specific client
202 sprint(float clientnum,...[string])
205 void VM_sprint (void)
209 char string[VM_STRINGTEMP_LENGTH];
211 VM_SAFEPARMCOUNTRANGE(1, 8, VM_sprint);
213 //find client for this entity
214 clientnum = (int)PRVM_G_FLOAT(OFS_PARM0);
215 if (!sv.active || clientnum < 0 || clientnum >= svs.maxclients || !svs.clients[clientnum].active)
217 VM_Warning("VM_sprint: %s: invalid client or server is not active !\n", PRVM_NAME);
221 client = svs.clients + clientnum;
222 if (!client->netconnection)
225 VM_VarString(1, string, sizeof(string));
226 MSG_WriteChar(&client->netconnection->message,svc_print);
227 MSG_WriteString(&client->netconnection->message, string);
234 single print to the screen
239 void VM_centerprint (void)
241 char string[VM_STRINGTEMP_LENGTH];
243 VM_SAFEPARMCOUNTRANGE(1, 8, VM_centerprint);
244 VM_VarString(0, string, sizeof(string));
245 SCR_CenterPrint(string);
252 vector normalize(vector)
255 void VM_normalize (void)
261 VM_SAFEPARMCOUNT(1,VM_normalize);
263 value1 = PRVM_G_VECTOR(OFS_PARM0);
265 f = VectorLength2(value1);
269 VectorScale(value1, f, newvalue);
272 VectorClear(newvalue);
274 VectorCopy (newvalue, PRVM_G_VECTOR(OFS_RETURN));
286 VM_SAFEPARMCOUNT(1,VM_vlen);
287 PRVM_G_FLOAT(OFS_RETURN) = VectorLength(PRVM_G_VECTOR(OFS_PARM0));
294 float vectoyaw(vector)
297 void VM_vectoyaw (void)
302 VM_SAFEPARMCOUNT(1,VM_vectoyaw);
304 value1 = PRVM_G_VECTOR(OFS_PARM0);
306 if (value1[1] == 0 && value1[0] == 0)
310 yaw = (int) (atan2(value1[1], value1[0]) * 180 / M_PI);
315 PRVM_G_FLOAT(OFS_RETURN) = yaw;
323 vector vectoangles(vector)
326 void VM_vectoangles (void)
332 VM_SAFEPARMCOUNT(1,VM_vectoangles);
334 value1 = PRVM_G_VECTOR(OFS_PARM0);
336 if (value1[1] == 0 && value1[0] == 0)
346 // LordHavoc: optimized a bit
349 yaw = (atan2(value1[1], value1[0]) * 180 / M_PI);
353 else if (value1[1] > 0)
358 forward = sqrt(value1[0]*value1[0] + value1[1]*value1[1]);
359 pitch = (atan2(value1[2], forward) * 180 / M_PI);
364 PRVM_G_FLOAT(OFS_RETURN+0) = pitch;
365 PRVM_G_FLOAT(OFS_RETURN+1) = yaw;
366 PRVM_G_FLOAT(OFS_RETURN+2) = 0;
373 Returns a number from 0<= num < 1
378 void VM_random (void)
380 VM_SAFEPARMCOUNT(0,VM_random);
382 PRVM_G_FLOAT(OFS_RETURN) = lhrandom(0, 1);
389 localsound(string sample)
392 void VM_localsound(void)
396 VM_SAFEPARMCOUNT(1,VM_localsound);
398 s = PRVM_G_STRING(OFS_PARM0);
400 if(!S_LocalSound (s))
402 PRVM_G_FLOAT(OFS_RETURN) = -4;
403 VM_Warning("VM_localsound: Failed to play %s for %s !\n", s, PRVM_NAME);
407 PRVM_G_FLOAT(OFS_RETURN) = 1;
419 PRVM_ERROR ("%s: break statement", PRVM_NAME);
422 //============================================================================
428 Sends text over to the client's execution buffer
430 [localcmd (string, ...) or]
434 void VM_localcmd (void)
436 char string[VM_STRINGTEMP_LENGTH];
437 VM_SAFEPARMCOUNTRANGE(1, 8, VM_localcmd);
438 VM_VarString(0, string, sizeof(string));
439 Cbuf_AddText(string);
451 char string[VM_STRINGTEMP_LENGTH];
452 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar);
453 VM_VarString(0, string, sizeof(string));
454 VM_CheckEmptyString(string);
455 PRVM_G_FLOAT(OFS_RETURN) = Cvar_VariableValue(string);
462 const string VM_cvar_string (string, ...)
465 void VM_cvar_string(void)
467 char string[VM_STRINGTEMP_LENGTH];
468 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar_string);
469 VM_VarString(0, string, sizeof(string));
470 VM_CheckEmptyString(string);
471 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(Cvar_VariableString(string));
476 ========================
479 const string VM_cvar_defstring (string, ...)
480 ========================
482 void VM_cvar_defstring (void)
484 char string[VM_STRINGTEMP_LENGTH];
485 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar_defstring);
486 VM_VarString(0, string, sizeof(string));
487 VM_CheckEmptyString(string);
488 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(Cvar_VariableDefString(string));
494 void cvar_set (string,string, ...)
497 void VM_cvar_set (void)
500 char string[VM_STRINGTEMP_LENGTH];
501 VM_SAFEPARMCOUNTRANGE(2,8,VM_cvar_set);
502 VM_VarString(1, string, sizeof(string));
503 name = PRVM_G_STRING(OFS_PARM0);
504 VM_CheckEmptyString(name);
505 Cvar_Set(name, string);
515 void VM_dprint (void)
517 char string[VM_STRINGTEMP_LENGTH];
518 VM_SAFEPARMCOUNTRANGE(1, 8, VM_dprint);
519 if (developer.integer)
521 VM_VarString(0, string, sizeof(string));
523 Con_Printf("%s", string);
525 Con_Printf("%s: %s", PRVM_NAME, string);
543 VM_SAFEPARMCOUNT(1, VM_ftos);
545 v = PRVM_G_FLOAT(OFS_PARM0);
547 if ((float)((int)v) == v)
548 sprintf(s, "%i", (int)v);
551 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(s);
566 VM_SAFEPARMCOUNT(1,VM_fabs);
568 v = PRVM_G_FLOAT(OFS_PARM0);
569 PRVM_G_FLOAT(OFS_RETURN) = fabs(v);
584 VM_SAFEPARMCOUNT(1,VM_vtos);
586 sprintf (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]);
587 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(s);
602 VM_SAFEPARMCOUNT(1, VM_etos);
604 sprintf (s, "entity %i", PRVM_G_EDICTNUM(OFS_PARM0));
605 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(s);
612 float stof(...[string])
617 char string[VM_STRINGTEMP_LENGTH];
618 VM_SAFEPARMCOUNTRANGE(1, 8, VM_stof);
619 VM_VarString(0, string, sizeof(string));
620 PRVM_G_FLOAT(OFS_RETURN) = atof(string);
624 ========================
628 ========================
632 VM_SAFEPARMCOUNT(1, VM_itof);
633 PRVM_G_FLOAT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
637 ========================
640 entity ftoe(float num)
641 ========================
646 VM_SAFEPARMCOUNT(1, VM_ftoe);
648 ent = (int)PRVM_G_FLOAT(OFS_PARM0);
649 if (ent < 0 || ent >= MAX_EDICTS || PRVM_PROG_TO_EDICT(ent)->priv.required->free)
650 ent = 0; // return world instead of a free or invalid entity
652 PRVM_G_INT(OFS_RETURN) = ent;
659 string strftime(float uselocaltime, string[, string ...])
662 void VM_strftime(void)
666 char fmt[VM_STRINGTEMP_LENGTH];
667 char result[VM_STRINGTEMP_LENGTH];
668 VM_SAFEPARMCOUNTRANGE(2, 8, VM_strftime);
669 VM_VarString(1, fmt, sizeof(fmt));
671 if (PRVM_G_FLOAT(OFS_PARM0))
677 PRVM_G_FLOAT(OFS_RETURN) = 0;
680 strftime(result, sizeof(result), fmt, tm);
681 PRVM_G_FLOAT(OFS_RETURN) = PRVM_SetTempString(result);
695 VM_SAFEPARMCOUNT(0, VM_spawn);
696 prog->xfunction->builtinsprofile += 20;
697 ed = PRVM_ED_Alloc();
709 void VM_remove (void)
712 prog->xfunction->builtinsprofile += 20;
714 VM_SAFEPARMCOUNT(1, VM_remove);
716 ed = PRVM_G_EDICT(OFS_PARM0);
717 if( PRVM_NUM_FOR_EDICT(ed) <= prog->reserved_edicts )
719 if (developer.integer >= 1)
720 VM_Warning( "VM_remove: tried to remove the null entity or a reserved entity!\n" );
722 else if( ed->priv.required->free )
724 if (developer.integer >= 1)
725 VM_Warning( "VM_remove: tried to remove an already freed entity!\n" );
735 entity find(entity start, .string field, string match)
746 VM_SAFEPARMCOUNT(3,VM_find);
748 e = PRVM_G_EDICTNUM(OFS_PARM0);
749 f = PRVM_G_INT(OFS_PARM1);
750 s = PRVM_G_STRING(OFS_PARM2);
752 // LordHavoc: apparently BloodMage does a find(world, weaponmodel, "") and
753 // expects it to find all the monsters, so we must be careful to support
756 for (e++ ; e < prog->num_edicts ; e++)
758 prog->xfunction->builtinsprofile++;
759 ed = PRVM_EDICT_NUM(e);
760 if (ed->priv.required->free)
762 t = PRVM_E_STRING(ed,f);
772 VM_RETURN_EDICT(prog->edicts);
779 entity findfloat(entity start, .float field, float match)
780 entity findentity(entity start, .entity field, entity match)
783 // LordHavoc: added this for searching float, int, and entity reference fields
784 void VM_findfloat (void)
791 VM_SAFEPARMCOUNT(3,VM_findfloat);
793 e = PRVM_G_EDICTNUM(OFS_PARM0);
794 f = PRVM_G_INT(OFS_PARM1);
795 s = PRVM_G_FLOAT(OFS_PARM2);
797 for (e++ ; e < prog->num_edicts ; e++)
799 prog->xfunction->builtinsprofile++;
800 ed = PRVM_EDICT_NUM(e);
801 if (ed->priv.required->free)
803 if (PRVM_E_FLOAT(ed,f) == s)
810 VM_RETURN_EDICT(prog->edicts);
817 entity findchain(.string field, string match)
820 // chained search for strings in entity fields
821 // entity(.string field, string match) findchain = #402;
822 void VM_findchain (void)
827 prvm_edict_t *ent, *chain;
829 VM_SAFEPARMCOUNT(2,VM_findchain);
831 if (prog->fieldoffsets.chain < 0)
832 PRVM_ERROR("VM_findchain: %s doesnt have a chain field !", PRVM_NAME);
834 chain = prog->edicts;
836 f = PRVM_G_INT(OFS_PARM0);
837 s = PRVM_G_STRING(OFS_PARM1);
839 // LordHavoc: apparently BloodMage does a find(world, weaponmodel, "") and
840 // expects it to find all the monsters, so we must be careful to support
843 ent = PRVM_NEXT_EDICT(prog->edicts);
844 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
846 prog->xfunction->builtinsprofile++;
847 if (ent->priv.required->free)
849 t = PRVM_E_STRING(ent,f);
855 PRVM_EDICTFIELDVALUE(ent,prog->fieldoffsets.chain)->edict = PRVM_NUM_FOR_EDICT(chain);
859 VM_RETURN_EDICT(chain);
866 entity findchainfloat(.string field, float match)
867 entity findchainentity(.string field, entity match)
870 // LordHavoc: chained search for float, int, and entity reference fields
871 // entity(.string field, float match) findchainfloat = #403;
872 void VM_findchainfloat (void)
877 prvm_edict_t *ent, *chain;
879 VM_SAFEPARMCOUNT(2, VM_findchainfloat);
881 if (prog->fieldoffsets.chain < 0)
882 PRVM_ERROR("VM_findchainfloat: %s doesnt have a chain field !", PRVM_NAME);
884 chain = (prvm_edict_t *)prog->edicts;
886 f = PRVM_G_INT(OFS_PARM0);
887 s = PRVM_G_FLOAT(OFS_PARM1);
889 ent = PRVM_NEXT_EDICT(prog->edicts);
890 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
892 prog->xfunction->builtinsprofile++;
893 if (ent->priv.required->free)
895 if (PRVM_E_FLOAT(ent,f) != s)
898 PRVM_EDICTFIELDVALUE(ent,prog->fieldoffsets.chain)->edict = PRVM_EDICT_TO_PROG(chain);
902 VM_RETURN_EDICT(chain);
906 ========================
909 entity findflags(entity start, .float field, float match)
910 ========================
912 // LordHavoc: search for flags in float fields
913 void VM_findflags (void)
920 VM_SAFEPARMCOUNT(3, VM_findflags);
923 e = PRVM_G_EDICTNUM(OFS_PARM0);
924 f = PRVM_G_INT(OFS_PARM1);
925 s = (int)PRVM_G_FLOAT(OFS_PARM2);
927 for (e++ ; e < prog->num_edicts ; e++)
929 prog->xfunction->builtinsprofile++;
930 ed = PRVM_EDICT_NUM(e);
931 if (ed->priv.required->free)
933 if (!PRVM_E_FLOAT(ed,f))
935 if ((int)PRVM_E_FLOAT(ed,f) & s)
942 VM_RETURN_EDICT(prog->edicts);
946 ========================
949 entity findchainflags(.float field, float match)
950 ========================
952 // LordHavoc: chained search for flags in float fields
953 void VM_findchainflags (void)
958 prvm_edict_t *ent, *chain;
960 VM_SAFEPARMCOUNT(2, VM_findchainflags);
962 if (prog->fieldoffsets.chain < 0)
963 PRVM_ERROR("VM_findchainflags: %s doesnt have a chain field !", PRVM_NAME);
965 chain = (prvm_edict_t *)prog->edicts;
967 f = PRVM_G_INT(OFS_PARM0);
968 s = (int)PRVM_G_FLOAT(OFS_PARM1);
970 ent = PRVM_NEXT_EDICT(prog->edicts);
971 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
973 prog->xfunction->builtinsprofile++;
974 if (ent->priv.required->free)
976 if (!PRVM_E_FLOAT(ent,f))
978 if (!((int)PRVM_E_FLOAT(ent,f) & s))
981 PRVM_EDICTFIELDVALUE(ent,prog->fieldoffsets.chain)->edict = PRVM_EDICT_TO_PROG(chain);
985 VM_RETURN_EDICT(chain);
992 string precache_sound (string sample)
995 void VM_precache_sound (void)
999 VM_SAFEPARMCOUNT(1, VM_precache_sound);
1001 s = PRVM_G_STRING(OFS_PARM0);
1002 PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
1003 VM_CheckEmptyString(s);
1005 if(snd_initialized.integer && !S_PrecacheSound(s, true, false))
1007 VM_Warning("VM_precache_sound: Failed to load %s for %s\n", s, PRVM_NAME);
1016 returns the same string as output
1018 does nothing, only used by qcc to build .pak archives
1021 void VM_precache_file (void)
1023 VM_SAFEPARMCOUNT(1,VM_precache_file);
1024 // precache_file is only used to copy files with qcc, it does nothing
1025 PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
1035 void VM_coredump (void)
1037 VM_SAFEPARMCOUNT(0,VM_coredump);
1039 Cbuf_AddText("prvm_edicts ");
1040 Cbuf_AddText(PRVM_NAME);
1051 void PRVM_StackTrace(void);
1052 void VM_stackdump (void)
1054 VM_SAFEPARMCOUNT(0, VM_stackdump);
1069 VM_SAFEPARMCOUNT(0, VM_crash);
1071 PRVM_ERROR("Crash called by %s",PRVM_NAME);
1081 void VM_traceon (void)
1083 VM_SAFEPARMCOUNT(0,VM_traceon);
1095 void VM_traceoff (void)
1097 VM_SAFEPARMCOUNT(0,VM_traceoff);
1099 prog->trace = false;
1109 void VM_eprint (void)
1111 VM_SAFEPARMCOUNT(1,VM_eprint);
1113 PRVM_ED_PrintNum (PRVM_G_EDICTNUM(OFS_PARM0), NULL);
1126 VM_SAFEPARMCOUNT(1,VM_rint);
1128 f = PRVM_G_FLOAT(OFS_PARM0);
1130 PRVM_G_FLOAT(OFS_RETURN) = floor(f + 0.5);
1132 PRVM_G_FLOAT(OFS_RETURN) = ceil(f - 0.5);
1142 void VM_floor (void)
1144 VM_SAFEPARMCOUNT(1,VM_floor);
1146 PRVM_G_FLOAT(OFS_RETURN) = floor(PRVM_G_FLOAT(OFS_PARM0));
1158 VM_SAFEPARMCOUNT(1,VM_ceil);
1160 PRVM_G_FLOAT(OFS_RETURN) = ceil(PRVM_G_FLOAT(OFS_PARM0));
1168 entity nextent(entity)
1171 void VM_nextent (void)
1176 VM_SAFEPARMCOUNT(1, VM_nextent);
1178 i = PRVM_G_EDICTNUM(OFS_PARM0);
1181 prog->xfunction->builtinsprofile++;
1183 if (i == prog->num_edicts)
1185 VM_RETURN_EDICT(prog->edicts);
1188 ent = PRVM_EDICT_NUM(i);
1189 if (!ent->priv.required->free)
1191 VM_RETURN_EDICT(ent);
1197 //=============================================================================
1204 changelevel(string map)
1207 void VM_changelevel (void)
1209 VM_SAFEPARMCOUNT(1, VM_changelevel);
1213 VM_Warning("VM_changelevel: game is not server (%s)\n", PRVM_NAME);
1217 // make sure we don't issue two changelevels
1218 if (svs.changelevel_issued)
1220 svs.changelevel_issued = true;
1222 Cbuf_AddText (va("changelevel %s\n",PRVM_G_STRING(OFS_PARM0)));
1234 VM_SAFEPARMCOUNT(1,VM_sin);
1235 PRVM_G_FLOAT(OFS_RETURN) = sin(PRVM_G_FLOAT(OFS_PARM0));
1246 VM_SAFEPARMCOUNT(1,VM_cos);
1247 PRVM_G_FLOAT(OFS_RETURN) = cos(PRVM_G_FLOAT(OFS_PARM0));
1259 VM_SAFEPARMCOUNT(1,VM_sqrt);
1260 PRVM_G_FLOAT(OFS_RETURN) = sqrt(PRVM_G_FLOAT(OFS_PARM0));
1272 VM_SAFEPARMCOUNT(1,VM_asin);
1273 PRVM_G_FLOAT(OFS_RETURN) = asin(PRVM_G_FLOAT(OFS_PARM0));
1284 VM_SAFEPARMCOUNT(1,VM_acos);
1285 PRVM_G_FLOAT(OFS_RETURN) = acos(PRVM_G_FLOAT(OFS_PARM0));
1296 VM_SAFEPARMCOUNT(1,VM_atan);
1297 PRVM_G_FLOAT(OFS_RETURN) = atan(PRVM_G_FLOAT(OFS_PARM0));
1303 float atan2(float,float)
1306 void VM_atan2 (void)
1308 VM_SAFEPARMCOUNT(2,VM_atan2);
1309 PRVM_G_FLOAT(OFS_RETURN) = atan2(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1320 VM_SAFEPARMCOUNT(1,VM_tan);
1321 PRVM_G_FLOAT(OFS_RETURN) = tan(PRVM_G_FLOAT(OFS_PARM0));
1328 Returns a vector of length < 1 and > 0
1333 void VM_randomvec (void)
1338 VM_SAFEPARMCOUNT(0, VM_randomvec);
1343 temp[0] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1344 temp[1] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1345 temp[2] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1347 while (DotProduct(temp, temp) >= 1);
1348 VectorCopy (temp, PRVM_G_VECTOR(OFS_RETURN));
1351 temp[0] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1352 temp[1] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1353 temp[2] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1354 // length returned always > 0
1355 length = (rand()&32766 + 1) * (1.0 / 32767.0) / VectorLength(temp);
1356 VectorScale(temp,length, temp);*/
1357 //VectorCopy(temp, PRVM_G_VECTOR(OFS_RETURN));
1360 //=============================================================================
1366 float registercvar (string name, string value[, float flags])
1369 void VM_registercvar (void)
1371 const char *name, *value;
1374 VM_SAFEPARMCOUNTRANGE(2, 3, VM_registercvar);
1376 name = PRVM_G_STRING(OFS_PARM0);
1377 value = PRVM_G_STRING(OFS_PARM1);
1378 flags = prog->argc >= 3 ? (int)PRVM_G_FLOAT(OFS_PARM2) : 0;
1379 PRVM_G_FLOAT(OFS_RETURN) = 0;
1381 if(flags > CVAR_MAXFLAGSVAL)
1384 // first check to see if it has already been defined
1385 if (Cvar_FindVar (name))
1388 // check for overlap with a command
1389 if (Cmd_Exists (name))
1391 VM_Warning("VM_registercvar: %s is a command\n", name);
1395 Cvar_Get(name, value, flags);
1397 PRVM_G_FLOAT(OFS_RETURN) = 1; // success
1405 returns the minimum of two supplied floats
1407 float min(float a, float b, ...[float])
1412 VM_SAFEPARMCOUNTRANGE(2, 8, VM_min);
1413 // LordHavoc: 3+ argument enhancement suggested by FrikaC
1414 if (prog->argc >= 3)
1417 float f = PRVM_G_FLOAT(OFS_PARM0);
1418 for (i = 1;i < prog->argc;i++)
1419 if (f > PRVM_G_FLOAT((OFS_PARM0+i*3)))
1420 f = PRVM_G_FLOAT((OFS_PARM0+i*3));
1421 PRVM_G_FLOAT(OFS_RETURN) = f;
1424 PRVM_G_FLOAT(OFS_RETURN) = min(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1431 returns the maximum of two supplied floats
1433 float max(float a, float b, ...[float])
1438 VM_SAFEPARMCOUNTRANGE(2, 8, VM_max);
1439 // LordHavoc: 3+ argument enhancement suggested by FrikaC
1440 if (prog->argc >= 3)
1443 float f = PRVM_G_FLOAT(OFS_PARM0);
1444 for (i = 1;i < prog->argc;i++)
1445 if (f < PRVM_G_FLOAT((OFS_PARM0+i*3)))
1446 f = PRVM_G_FLOAT((OFS_PARM0+i*3));
1447 PRVM_G_FLOAT(OFS_RETURN) = f;
1450 PRVM_G_FLOAT(OFS_RETURN) = max(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1457 returns number bounded by supplied range
1459 float bound(float min, float value, float max)
1462 void VM_bound (void)
1464 VM_SAFEPARMCOUNT(3,VM_bound);
1465 PRVM_G_FLOAT(OFS_RETURN) = bound(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1), PRVM_G_FLOAT(OFS_PARM2));
1472 returns a raised to power b
1474 float pow(float a, float b)
1479 VM_SAFEPARMCOUNT(2,VM_pow);
1480 PRVM_G_FLOAT(OFS_RETURN) = pow(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1483 void VM_Files_Init(void)
1486 for (i = 0;i < PRVM_MAX_OPENFILES;i++)
1487 prog->openfiles[i] = NULL;
1490 void VM_Files_CloseAll(void)
1493 for (i = 0;i < PRVM_MAX_OPENFILES;i++)
1495 if (prog->openfiles[i])
1496 FS_Close(prog->openfiles[i]);
1497 prog->openfiles[i] = NULL;
1501 static qfile_t *VM_GetFileHandle( int index )
1503 if (index < 0 || index >= PRVM_MAX_OPENFILES)
1505 Con_Printf("VM_GetFileHandle: invalid file handle %i used in %s\n", index, PRVM_NAME);
1508 if (prog->openfiles[index] == NULL)
1510 Con_Printf("VM_GetFileHandle: no such file handle %i (or file has been closed) in %s\n", index, PRVM_NAME);
1513 return prog->openfiles[index];
1520 float fopen(string filename, float mode)
1523 // float(string filename, float mode) fopen = #110;
1524 // opens a file inside quake/gamedir/data/ (mode is FILE_READ, FILE_APPEND, or FILE_WRITE),
1525 // returns fhandle >= 0 if successful, or fhandle < 0 if unable to open file for any reason
1529 const char *modestring, *filename;
1531 VM_SAFEPARMCOUNT(2,VM_fopen);
1533 for (filenum = 0;filenum < PRVM_MAX_OPENFILES;filenum++)
1534 if (prog->openfiles[filenum] == NULL)
1536 if (filenum >= PRVM_MAX_OPENFILES)
1538 PRVM_G_FLOAT(OFS_RETURN) = -2;
1539 VM_Warning("VM_fopen: %s ran out of file handles (%i)\n", PRVM_NAME, PRVM_MAX_OPENFILES);
1542 mode = (int)PRVM_G_FLOAT(OFS_PARM1);
1545 case 0: // FILE_READ
1548 case 1: // FILE_APPEND
1551 case 2: // FILE_WRITE
1555 PRVM_G_FLOAT(OFS_RETURN) = -3;
1556 VM_Warning("VM_fopen: %s: no such mode %i (valid: 0 = read, 1 = append, 2 = write)\n", PRVM_NAME, mode);
1559 filename = PRVM_G_STRING(OFS_PARM0);
1561 prog->openfiles[filenum] = FS_Open(va("data/%s", filename), modestring, false, false);
1562 if (prog->openfiles[filenum] == NULL && mode == 0)
1563 prog->openfiles[filenum] = FS_Open(va("%s", filename), modestring, false, false);
1565 if (prog->openfiles[filenum] == NULL)
1567 PRVM_G_FLOAT(OFS_RETURN) = -1;
1568 if (developer.integer >= 100)
1569 VM_Warning("VM_fopen: %s: %s mode %s failed\n", PRVM_NAME, filename, modestring);
1573 PRVM_G_FLOAT(OFS_RETURN) = filenum;
1574 if (developer.integer >= 100)
1575 Con_Printf("VM_fopen: %s: %s mode %s opened as #%i\n", PRVM_NAME, filename, modestring, filenum);
1583 fclose(float fhandle)
1586 //void(float fhandle) fclose = #111; // closes a file
1587 void VM_fclose(void)
1591 VM_SAFEPARMCOUNT(1,VM_fclose);
1593 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1594 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
1596 VM_Warning("VM_fclose: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1599 if (prog->openfiles[filenum] == NULL)
1601 VM_Warning("VM_fclose: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1604 FS_Close(prog->openfiles[filenum]);
1605 prog->openfiles[filenum] = NULL;
1606 if (developer.integer >= 100)
1607 Con_Printf("VM_fclose: %s: #%i closed\n", PRVM_NAME, filenum);
1614 string fgets(float fhandle)
1617 //string(float fhandle) fgets = #112; // reads a line of text from the file and returns as a tempstring
1621 char string[VM_STRINGTEMP_LENGTH];
1624 VM_SAFEPARMCOUNT(1,VM_fgets);
1626 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1627 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
1629 VM_Warning("VM_fgets: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1632 if (prog->openfiles[filenum] == NULL)
1634 VM_Warning("VM_fgets: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1640 c = FS_Getc(prog->openfiles[filenum]);
1641 if (c == '\r' || c == '\n' || c < 0)
1643 if (end < VM_STRINGTEMP_LENGTH - 1)
1647 // remove \n following \r
1650 c = FS_Getc(prog->openfiles[filenum]);
1652 FS_UnGetc(prog->openfiles[filenum], (unsigned char)c);
1654 if (developer.integer >= 100)
1655 Con_Printf("fgets: %s: %s\n", PRVM_NAME, string);
1657 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string);
1659 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
1666 fputs(float fhandle, string s)
1669 //void(float fhandle, string s) fputs = #113; // writes a line of text to the end of the file
1673 char string[VM_STRINGTEMP_LENGTH];
1676 VM_SAFEPARMCOUNT(2,VM_fputs);
1678 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1679 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
1681 VM_Warning("VM_fputs: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1684 if (prog->openfiles[filenum] == NULL)
1686 VM_Warning("VM_fputs: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1689 VM_VarString(1, string, sizeof(string));
1690 if ((stringlength = (int)strlen(string)))
1691 FS_Write(prog->openfiles[filenum], string, stringlength);
1692 if (developer.integer >= 100)
1693 Con_Printf("fputs: %s: %s\n", PRVM_NAME, string);
1700 writetofile(float fhandle, entity ent)
1703 void VM_writetofile(void)
1708 VM_SAFEPARMCOUNT(2, VM_writetofile);
1710 file = VM_GetFileHandle( (int)PRVM_G_FLOAT(OFS_PARM0) );
1713 VM_Warning("VM_writetofile: invalid or closed file handle\n");
1717 ent = PRVM_G_EDICT(OFS_PARM1);
1718 if(ent->priv.required->free)
1720 VM_Warning("VM_writetofile: %s: entity %i is free !\n", PRVM_NAME, PRVM_NUM_FOR_EDICT(ent));
1724 PRVM_ED_Write (file, ent);
1731 float strlen(string s)
1734 //float(string s) strlen = #114; // returns how many characters are in a string
1735 void VM_strlen(void)
1737 VM_SAFEPARMCOUNT(1,VM_strlen);
1739 PRVM_G_FLOAT(OFS_RETURN) = strlen(PRVM_G_STRING(OFS_PARM0));
1742 // DRESK - Decolorized String
1747 string strdecolorize(string s)
1750 // string (string s) strdecolorize = #472; // returns the passed in string with color codes stripped
1751 void VM_strdecolorize(void)
1753 char szNewString[VM_STRINGTEMP_LENGTH];
1754 const char *szString;
1757 VM_SAFEPARMCOUNT(1,VM_strdecolorize);
1758 szString = PRVM_G_STRING(OFS_PARM0);
1760 COM_StringDecolorize(szString, 0, szNewString, sizeof(szNewString), TRUE);
1762 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(szNewString);
1765 // DRESK - String Length (not counting color codes)
1770 float strlennocol(string s)
1773 // float(string s) strlennocol = #471; // returns how many characters are in a string not including color codes
1774 // For example, ^2Dresk returns a length of 5
1775 void VM_strlennocol(void)
1777 const char *szString;
1780 VM_SAFEPARMCOUNT(1,VM_strlennocol);
1782 szString = PRVM_G_STRING(OFS_PARM0);
1784 nCnt = COM_StringLengthNoColors(szString, 0, NULL);
1786 PRVM_G_FLOAT(OFS_RETURN) = nCnt;
1789 // DRESK - String to Uppercase and Lowercase
1794 string strtolower(string s)
1797 // string (string s) strtolower = #480; // returns passed in string in lowercase form
1798 void VM_strtolower(void)
1800 char szNewString[VM_STRINGTEMP_LENGTH];
1801 const char *szString;
1804 VM_SAFEPARMCOUNT(1,VM_strtolower);
1805 szString = PRVM_G_STRING(OFS_PARM0);
1807 COM_ToLowerString(szString, szNewString, sizeof(szNewString) );
1809 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(szNewString);
1816 string strtoupper(string s)
1819 // string (string s) strtoupper = #481; // returns passed in string in uppercase form
1820 void VM_strtoupper(void)
1822 char szNewString[VM_STRINGTEMP_LENGTH];
1823 const char *szString;
1826 VM_SAFEPARMCOUNT(1,VM_strtoupper);
1827 szString = PRVM_G_STRING(OFS_PARM0);
1829 COM_ToUpperString(szString, szNewString, sizeof(szNewString) );
1831 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(szNewString);
1838 string strcat(string,string,...[string])
1841 //string(string s1, string s2) strcat = #115;
1842 // concatenates two strings (for example "abc", "def" would return "abcdef")
1843 // and returns as a tempstring
1844 void VM_strcat(void)
1846 char s[VM_STRINGTEMP_LENGTH];
1847 VM_SAFEPARMCOUNTRANGE(1, 8, VM_strcat);
1849 VM_VarString(0, s, sizeof(s));
1850 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(s);
1857 string substring(string s, float start, float length)
1860 // string(string s, float start, float length) substring = #116;
1861 // returns a section of a string as a tempstring
1862 void VM_substring(void)
1864 int i, start, length;
1866 char string[VM_STRINGTEMP_LENGTH];
1868 VM_SAFEPARMCOUNT(3,VM_substring);
1870 s = PRVM_G_STRING(OFS_PARM0);
1871 start = (int)PRVM_G_FLOAT(OFS_PARM1);
1872 length = (int)PRVM_G_FLOAT(OFS_PARM2);
1873 for (i = 0;i < start && *s;i++, s++);
1874 for (i = 0;i < (int)sizeof(string) - 1 && *s && i < length;i++, s++)
1877 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string);
1884 string(string search, string replace, string subject) strreplace = #484;
1887 // replaces all occurrences of search with replace in the string subject, and returns the result
1888 void VM_strreplace(void)
1891 const char *search, *replace, *subject;
1892 char string[VM_STRINGTEMP_LENGTH];
1893 int search_len, replace_len, subject_len;
1895 VM_SAFEPARMCOUNT(3,VM_strreplace);
1897 search = PRVM_G_STRING(OFS_PARM0);
1898 replace = PRVM_G_STRING(OFS_PARM1);
1899 subject = PRVM_G_STRING(OFS_PARM2);
1901 search_len = (int)strlen(search);
1902 replace_len = (int)strlen(replace);
1903 subject_len = (int)strlen(subject);
1906 for (i = 0; i < subject_len; i++)
1908 for (j = 0; j < search_len && i+j < subject_len; j++)
1909 if (subject[i+j] != search[j])
1911 if (j == search_len || i+j == subject_len)
1913 // found it at offset 'i'
1914 for (j = 0; j < replace_len && si < (int)sizeof(string) - 1; j++)
1915 string[si++] = replace[j];
1916 i += search_len - 1;
1921 if (si < (int)sizeof(string) - 1)
1922 string[si++] = subject[i];
1927 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string);
1934 string(string search, string replace, string subject) strireplace = #485;
1937 // case-insensitive version of strreplace
1938 void VM_strireplace(void)
1941 const char *search, *replace, *subject;
1942 char string[VM_STRINGTEMP_LENGTH];
1943 int search_len, replace_len, subject_len;
1945 VM_SAFEPARMCOUNT(3,VM_strreplace);
1947 search = PRVM_G_STRING(OFS_PARM0);
1948 replace = PRVM_G_STRING(OFS_PARM1);
1949 subject = PRVM_G_STRING(OFS_PARM2);
1951 search_len = (int)strlen(search);
1952 replace_len = (int)strlen(replace);
1953 subject_len = (int)strlen(subject);
1956 for (i = 0; i < subject_len; i++)
1958 for (j = 0; j < search_len && i+j < subject_len; j++)
1959 if (tolower(subject[i+j]) != tolower(search[j]))
1961 if (j == search_len || i+j == subject_len)
1963 // found it at offset 'i'
1964 for (j = 0; j < replace_len && si < (int)sizeof(string) - 1; j++)
1965 string[si++] = replace[j];
1966 i += search_len - 1;
1971 if (si < (int)sizeof(string) - 1)
1972 string[si++] = subject[i];
1977 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string);
1984 vector stov(string s)
1987 //vector(string s) stov = #117; // returns vector value from a string
1990 char string[VM_STRINGTEMP_LENGTH];
1992 VM_SAFEPARMCOUNT(1,VM_stov);
1994 VM_VarString(0, string, sizeof(string));
1995 Math_atov(string, PRVM_G_VECTOR(OFS_RETURN));
2002 string strzone(string s)
2005 //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)
2006 void VM_strzone(void)
2009 char string[VM_STRINGTEMP_LENGTH];
2012 VM_SAFEPARMCOUNT(1,VM_strzone);
2014 VM_VarString(0, string, sizeof(string));
2015 alloclen = strlen(string) + 1;
2016 PRVM_G_INT(OFS_RETURN) = PRVM_AllocString(alloclen, &out);
2017 memcpy(out, string, alloclen);
2027 //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!!!)
2028 void VM_strunzone(void)
2030 VM_SAFEPARMCOUNT(1,VM_strunzone);
2031 PRVM_FreeString(PRVM_G_INT(OFS_PARM0));
2036 VM_command (used by client and menu)
2038 clientcommand(float client, string s) (for client and menu)
2041 //void(entity e, string s) clientcommand = #440; // executes a command string as if it came from the specified client
2042 //this function originally written by KrimZon, made shorter by LordHavoc
2043 void VM_clcommand (void)
2045 client_t *temp_client;
2048 VM_SAFEPARMCOUNT(2,VM_clcommand);
2050 i = (int)PRVM_G_FLOAT(OFS_PARM0);
2051 if (!sv.active || i < 0 || i >= svs.maxclients || !svs.clients[i].active)
2053 VM_Warning("VM_clientcommand: %s: invalid client/server is not active !\n", PRVM_NAME);
2057 temp_client = host_client;
2058 host_client = svs.clients + i;
2059 Cmd_ExecuteString (PRVM_G_STRING(OFS_PARM1), src_client);
2060 host_client = temp_client;
2068 float tokenize(string s)
2071 //float(string s) tokenize = #441; // takes apart a string into individal words (access them with argv), returns how many
2072 //this function originally written by KrimZon, made shorter by LordHavoc
2073 //20040203: rewritten by LordHavoc (no longer uses allocations)
2076 void VM_tokenize (void)
2079 static char string[VM_STRINGTEMP_LENGTH]; // static, because it's big
2081 VM_SAFEPARMCOUNT(1,VM_tokenize);
2083 strlcpy(string, PRVM_G_STRING(OFS_PARM0), sizeof(string));
2087 while(COM_ParseToken_VM_Tokenize(&p, false))
2089 if (num_tokens >= (int)(sizeof(tokens)/sizeof(tokens[0])))
2091 tokens[num_tokens++] = PRVM_SetTempString(com_token);
2094 PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
2099 VM_tokenizebyseparator
2101 float tokenizebyseparator(string s, string separator1, ...)
2104 //float(string s, string separator1, ...) tokenizebyseparator = #479; // takes apart a string into individal words (access them with argv), returns how many
2105 //this function returns the token preceding each instance of a separator (of
2106 //which there can be multiple), and the text following the last separator
2107 //useful for parsing certain kinds of data like IP addresses
2109 //numnumbers = tokenizebyseparator("10.1.2.3", ".");
2110 //returns 4 and the tokens "10" "1" "2" "3".
2111 void VM_tokenizebyseparator (void)
2115 int separatorlen[7];
2116 const char *separators[7];
2119 char tokentext[MAX_INPUTLINE];
2120 static char string[VM_STRINGTEMP_LENGTH]; // static, because it's big
2122 VM_SAFEPARMCOUNTRANGE(2, 8,VM_tokenizebyseparator);
2124 strlcpy(string, PRVM_G_STRING(OFS_PARM0), sizeof(string));
2128 for (j = 1;j < prog->argc;j++)
2130 // skip any blank separator strings
2131 const char *s = PRVM_G_STRING(OFS_PARM0+j*3);
2134 separators[numseparators] = s;
2135 separatorlen[numseparators] = strlen(s);
2142 while (num_tokens < (int)(sizeof(tokens)/sizeof(tokens[0])))
2144 token = tokentext + j;
2147 for (k = 0;k < numseparators;k++)
2149 if (!strncmp(p, separators[k], separatorlen[k]))
2151 p += separatorlen[k];
2155 if (k < numseparators)
2157 if (j < (int)sizeof(tokentext)-1)
2158 tokentext[j++] = *p;
2161 if (j >= (int)sizeof(tokentext))
2164 tokens[num_tokens++] = PRVM_SetTempString(token);
2169 PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
2172 //string(float n) argv = #442; // returns a word from the tokenized string (returns nothing for an invalid index)
2173 //this function originally written by KrimZon, made shorter by LordHavoc
2178 VM_SAFEPARMCOUNT(1,VM_argv);
2180 token_num = (int)PRVM_G_FLOAT(OFS_PARM0);
2182 if (token_num >= 0 && token_num < num_tokens)
2183 PRVM_G_INT(OFS_RETURN) = tokens[token_num];
2185 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
2195 void VM_isserver(void)
2197 VM_SAFEPARMCOUNT(0,VM_serverstate);
2199 PRVM_G_FLOAT(OFS_RETURN) = sv.active && (svs.maxclients > 1 || cls.state == ca_dedicated);
2209 void VM_clientcount(void)
2211 VM_SAFEPARMCOUNT(0,VM_clientcount);
2213 PRVM_G_FLOAT(OFS_RETURN) = svs.maxclients;
2223 void VM_clientstate(void)
2225 VM_SAFEPARMCOUNT(0,VM_clientstate);
2227 PRVM_G_FLOAT(OFS_RETURN) = cls.state;
2234 float getostype(void)
2236 */ // not used at the moment -> not included in the common list
2237 void VM_getostype(void)
2239 VM_SAFEPARMCOUNT(0,VM_getostype);
2244 OS_MAC - not supported
2248 PRVM_G_FLOAT(OFS_RETURN) = 0;
2249 #elif defined(MACOSX)
2250 PRVM_G_FLOAT(OFS_RETURN) = 2;
2252 PRVM_G_FLOAT(OFS_RETURN) = 1;
2260 vector getmousepos()
2263 void VM_getmousepos(void)
2266 VM_SAFEPARMCOUNT(0,VM_getmousepos);
2268 PRVM_G_VECTOR(OFS_RETURN)[0] = in_mouse_x * vid_conwidth.integer / vid.width;
2269 PRVM_G_VECTOR(OFS_RETURN)[1] = in_mouse_y * vid_conheight.integer / vid.height;
2270 PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
2280 void VM_gettime(void)
2282 VM_SAFEPARMCOUNT(0,VM_gettime);
2284 PRVM_G_FLOAT(OFS_RETURN) = (float) realtime;
2291 loadfromdata(string data)
2294 void VM_loadfromdata(void)
2296 VM_SAFEPARMCOUNT(1,VM_loadentsfromfile);
2298 PRVM_ED_LoadFromFile(PRVM_G_STRING(OFS_PARM0));
2302 ========================
2305 parseentitydata(entity ent, string data)
2306 ========================
2308 void VM_parseentitydata(void)
2313 VM_SAFEPARMCOUNT(2, VM_parseentitydata);
2315 // get edict and test it
2316 ent = PRVM_G_EDICT(OFS_PARM0);
2317 if (ent->priv.required->free)
2318 PRVM_ERROR ("VM_parseentitydata: %s: Can only set already spawned entities (entity %i is free)!", PRVM_NAME, PRVM_NUM_FOR_EDICT(ent));
2320 data = PRVM_G_STRING(OFS_PARM1);
2322 // parse the opening brace
2323 if (!COM_ParseToken_Simple(&data, false, false) || com_token[0] != '{' )
2324 PRVM_ERROR ("VM_parseentitydata: %s: Couldn't parse entity data:\n%s", PRVM_NAME, data );
2326 PRVM_ED_ParseEdict (data, ent);
2333 loadfromfile(string file)
2336 void VM_loadfromfile(void)
2338 const char *filename;
2341 VM_SAFEPARMCOUNT(1,VM_loadfromfile);
2343 filename = PRVM_G_STRING(OFS_PARM0);
2344 if (FS_CheckNastyPath(filename, false))
2346 PRVM_G_FLOAT(OFS_RETURN) = -4;
2347 VM_Warning("VM_loadfromfile: %s dangerous or non-portable filename \"%s\" not allowed. (contains : or \\ or begins with .. or /)\n", PRVM_NAME, filename);
2351 // not conform with VM_fopen
2352 data = (char *)FS_LoadFile(filename, tempmempool, false, NULL);
2354 PRVM_G_FLOAT(OFS_RETURN) = -1;
2356 PRVM_ED_LoadFromFile(data);
2367 float mod(float val, float m)
2370 void VM_modulo(void)
2373 VM_SAFEPARMCOUNT(2,VM_module);
2375 val = (int) PRVM_G_FLOAT(OFS_PARM0);
2376 m = (int) PRVM_G_FLOAT(OFS_PARM1);
2378 PRVM_G_FLOAT(OFS_RETURN) = (float) (val % m);
2381 void VM_Search_Init(void)
2384 for (i = 0;i < PRVM_MAX_OPENSEARCHES;i++)
2385 prog->opensearches[i] = NULL;
2388 void VM_Search_Reset(void)
2391 // reset the fssearch list
2392 for(i = 0; i < PRVM_MAX_OPENSEARCHES; i++)
2394 if(prog->opensearches[i])
2395 FS_FreeSearch(prog->opensearches[i]);
2396 prog->opensearches[i] = NULL;
2404 float search_begin(string pattern, float caseinsensitive, float quiet)
2407 void VM_search_begin(void)
2410 const char *pattern;
2411 int caseinsens, quiet;
2413 VM_SAFEPARMCOUNT(3, VM_search_begin);
2415 pattern = PRVM_G_STRING(OFS_PARM0);
2417 VM_CheckEmptyString(pattern);
2419 caseinsens = (int)PRVM_G_FLOAT(OFS_PARM1);
2420 quiet = (int)PRVM_G_FLOAT(OFS_PARM2);
2422 for(handle = 0; handle < PRVM_MAX_OPENSEARCHES; handle++)
2423 if(!prog->opensearches[handle])
2426 if(handle >= PRVM_MAX_OPENSEARCHES)
2428 PRVM_G_FLOAT(OFS_RETURN) = -2;
2429 VM_Warning("VM_search_begin: %s ran out of search handles (%i)\n", PRVM_NAME, PRVM_MAX_OPENSEARCHES);
2433 if(!(prog->opensearches[handle] = FS_Search(pattern,caseinsens, quiet)))
2434 PRVM_G_FLOAT(OFS_RETURN) = -1;
2436 PRVM_G_FLOAT(OFS_RETURN) = handle;
2443 void search_end(float handle)
2446 void VM_search_end(void)
2449 VM_SAFEPARMCOUNT(1, VM_search_end);
2451 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
2453 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
2455 VM_Warning("VM_search_end: invalid handle %i used in %s\n", handle, PRVM_NAME);
2458 if(prog->opensearches[handle] == NULL)
2460 VM_Warning("VM_search_end: no such handle %i in %s\n", handle, PRVM_NAME);
2464 FS_FreeSearch(prog->opensearches[handle]);
2465 prog->opensearches[handle] = NULL;
2472 float search_getsize(float handle)
2475 void VM_search_getsize(void)
2478 VM_SAFEPARMCOUNT(1, VM_M_search_getsize);
2480 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
2482 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
2484 VM_Warning("VM_search_getsize: invalid handle %i used in %s\n", handle, PRVM_NAME);
2487 if(prog->opensearches[handle] == NULL)
2489 VM_Warning("VM_search_getsize: no such handle %i in %s\n", handle, PRVM_NAME);
2493 PRVM_G_FLOAT(OFS_RETURN) = prog->opensearches[handle]->numfilenames;
2498 VM_search_getfilename
2500 string search_getfilename(float handle, float num)
2503 void VM_search_getfilename(void)
2505 int handle, filenum;
2506 VM_SAFEPARMCOUNT(2, VM_search_getfilename);
2508 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
2509 filenum = (int)PRVM_G_FLOAT(OFS_PARM1);
2511 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
2513 VM_Warning("VM_search_getfilename: invalid handle %i used in %s\n", handle, PRVM_NAME);
2516 if(prog->opensearches[handle] == NULL)
2518 VM_Warning("VM_search_getfilename: no such handle %i in %s\n", handle, PRVM_NAME);
2521 if(filenum < 0 || filenum >= prog->opensearches[handle]->numfilenames)
2523 VM_Warning("VM_search_getfilename: invalid filenum %i in %s\n", filenum, PRVM_NAME);
2527 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog->opensearches[handle]->filenames[filenum]);
2534 string chr(float ascii)
2540 VM_SAFEPARMCOUNT(1, VM_chr);
2542 tmp[0] = (unsigned char) PRVM_G_FLOAT(OFS_PARM0);
2545 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(tmp);
2548 //=============================================================================
2549 // Draw builtins (client & menu)
2555 float iscachedpic(string pic)
2558 void VM_iscachedpic(void)
2560 VM_SAFEPARMCOUNT(1,VM_iscachedpic);
2562 // drawq hasnt such a function, thus always return true
2563 PRVM_G_FLOAT(OFS_RETURN) = false;
2570 string precache_pic(string pic)
2573 void VM_precache_pic(void)
2577 VM_SAFEPARMCOUNT(1, VM_precache_pic);
2579 s = PRVM_G_STRING(OFS_PARM0);
2580 PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
2581 VM_CheckEmptyString (s);
2583 // AK Draw_CachePic is supposed to always return a valid pointer
2584 if( Draw_CachePic(s, false)->tex == r_texture_notexture )
2585 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
2595 void VM_freepic(void)
2599 VM_SAFEPARMCOUNT(1,VM_freepic);
2601 s = PRVM_G_STRING(OFS_PARM0);
2602 VM_CheckEmptyString (s);
2611 float drawcharacter(vector position, float character, vector scale, vector rgb, float alpha, float flag)
2614 void VM_drawcharacter(void)
2616 float *pos,*scale,*rgb;
2619 VM_SAFEPARMCOUNT(6,VM_drawcharacter);
2621 character = (char) PRVM_G_FLOAT(OFS_PARM1);
2624 PRVM_G_FLOAT(OFS_RETURN) = -1;
2625 VM_Warning("VM_drawcharacter: %s passed null character !\n",PRVM_NAME);
2629 pos = PRVM_G_VECTOR(OFS_PARM0);
2630 scale = PRVM_G_VECTOR(OFS_PARM2);
2631 rgb = PRVM_G_VECTOR(OFS_PARM3);
2632 flag = (int)PRVM_G_FLOAT(OFS_PARM5);
2634 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2636 PRVM_G_FLOAT(OFS_RETURN) = -2;
2637 VM_Warning("VM_drawcharacter: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2641 if(pos[2] || scale[2])
2642 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")));
2644 if(!scale[0] || !scale[1])
2646 PRVM_G_FLOAT(OFS_RETURN) = -3;
2647 VM_Warning("VM_drawcharacter: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
2651 DrawQ_String (pos[0], pos[1], &character, 1, scale[0], scale[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM4), flag, NULL, true);
2652 PRVM_G_FLOAT(OFS_RETURN) = 1;
2659 float drawstring(vector position, string text, vector scale, vector rgb, float alpha, float flag)
2662 void VM_drawstring(void)
2664 float *pos,*scale,*rgb;
2667 VM_SAFEPARMCOUNT(6,VM_drawstring);
2669 string = PRVM_G_STRING(OFS_PARM1);
2670 pos = PRVM_G_VECTOR(OFS_PARM0);
2671 scale = PRVM_G_VECTOR(OFS_PARM2);
2672 rgb = PRVM_G_VECTOR(OFS_PARM3);
2673 flag = (int)PRVM_G_FLOAT(OFS_PARM5);
2675 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2677 PRVM_G_FLOAT(OFS_RETURN) = -2;
2678 VM_Warning("VM_drawstring: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2682 if(!scale[0] || !scale[1])
2684 PRVM_G_FLOAT(OFS_RETURN) = -3;
2685 VM_Warning("VM_drawstring: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
2689 if(pos[2] || scale[2])
2690 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")));
2692 DrawQ_String (pos[0], pos[1], string, 0, scale[0], scale[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM4), flag, NULL, true);
2693 PRVM_G_FLOAT(OFS_RETURN) = 1;
2698 VM_drawcolorcodedstring
2700 float drawcolorcodedstring(vector position, string text, vector scale, float alpha, float flag)
2703 void VM_drawcolorcodedstring(void)
2708 VM_SAFEPARMCOUNT(5,VM_drawstring);
2710 string = PRVM_G_STRING(OFS_PARM1);
2711 pos = PRVM_G_VECTOR(OFS_PARM0);
2712 scale = PRVM_G_VECTOR(OFS_PARM2);
2713 flag = (int)PRVM_G_FLOAT(OFS_PARM5);
2715 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2717 PRVM_G_FLOAT(OFS_RETURN) = -2;
2718 VM_Warning("VM_drawcolorcodedstring: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2722 if(!scale[0] || !scale[1])
2724 PRVM_G_FLOAT(OFS_RETURN) = -3;
2725 VM_Warning("VM_drawcolorcodedstring: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
2729 if(pos[2] || scale[2])
2730 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")));
2733 DrawQ_String (pos[0], pos[1], string, 0, scale[0], scale[1], 1, 1, 1, PRVM_G_FLOAT(OFS_PARM3), flag, NULL, false);
2734 PRVM_G_FLOAT(OFS_RETURN) = 1;
2740 float stringwidth(string text, float allowColorCodes)
2743 void VM_stringwidth(void)
2747 VM_SAFEPARMCOUNT(2,VM_drawstring);
2749 string = PRVM_G_STRING(OFS_PARM0);
2750 colors = (int)PRVM_G_FLOAT(OFS_PARM1);
2752 PRVM_G_FLOAT(OFS_RETURN) = DrawQ_String(0, 0, string, 0, 1, 1, 0, 0, 0, 0, 0, NULL, !colors); // 1x1 characters, don't actually draw
2758 float drawpic(vector position, string pic, vector size, vector rgb, float alpha, float flag)
2761 void VM_drawpic(void)
2763 const char *picname;
2764 float *size, *pos, *rgb;
2767 VM_SAFEPARMCOUNT(6,VM_drawpic);
2769 picname = PRVM_G_STRING(OFS_PARM1);
2770 VM_CheckEmptyString (picname);
2772 // is pic cached ? no function yet for that
2775 PRVM_G_FLOAT(OFS_RETURN) = -4;
2776 VM_Warning("VM_drawpic: %s: %s not cached !\n", PRVM_NAME, picname);
2780 pos = PRVM_G_VECTOR(OFS_PARM0);
2781 size = PRVM_G_VECTOR(OFS_PARM2);
2782 rgb = PRVM_G_VECTOR(OFS_PARM3);
2783 flag = (int) PRVM_G_FLOAT(OFS_PARM5);
2785 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2787 PRVM_G_FLOAT(OFS_RETURN) = -2;
2788 VM_Warning("VM_drawpic: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2792 if(pos[2] || size[2])
2793 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")));
2795 DrawQ_Pic(pos[0], pos[1], Draw_CachePic(picname, true), size[0], size[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM4), flag);
2796 PRVM_G_FLOAT(OFS_RETURN) = 1;
2802 float drawsubpic(vector position, vector size, string pic, vector srcPos, vector srcSize, vector rgb, float alpha, float flag)
2806 void VM_drawsubpic(void)
2808 const char *picname;
2809 float *size, *pos, *rgb, *srcPos, *srcSize, alpha;
2812 VM_SAFEPARMCOUNT(8,VM_drawsubpic);
2814 picname = PRVM_G_STRING(OFS_PARM2);
2815 VM_CheckEmptyString (picname);
2817 // is pic cached ? no function yet for that
2820 PRVM_G_FLOAT(OFS_RETURN) = -4;
2821 VM_Warning("VM_drawsubpic: %s: %s not cached !\n", PRVM_NAME, picname);
2825 pos = PRVM_G_VECTOR(OFS_PARM0);
2826 size = PRVM_G_VECTOR(OFS_PARM1);
2827 srcPos = PRVM_G_VECTOR(OFS_PARM3);
2828 srcSize = PRVM_G_VECTOR(OFS_PARM4);
2829 rgb = PRVM_G_VECTOR(OFS_PARM5);
2830 alpha = PRVM_G_FLOAT(OFS_PARM6);
2831 flag = (int) PRVM_G_FLOAT(OFS_PARM7);
2833 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2835 PRVM_G_FLOAT(OFS_RETURN) = -2;
2836 VM_Warning("VM_drawsubpic: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2840 if(pos[2] || size[2])
2841 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")));
2843 DrawQ_SuperPic(pos[0], pos[1], Draw_CachePic(picname, true),
2845 srcPos[0], srcPos[1], rgb[0], rgb[1], rgb[2], alpha,
2846 srcPos[0] + srcSize[0], srcPos[1], rgb[0], rgb[1], rgb[2], alpha,
2847 srcPos[0], srcPos[1] + srcSize[1], rgb[0], rgb[1], rgb[2], alpha,
2848 srcPos[0] + srcSize[0], srcPos[1] + srcSize[1], rgb[0], rgb[1], rgb[2], alpha,
2850 PRVM_G_FLOAT(OFS_RETURN) = 1;
2857 float drawfill(vector position, vector size, vector rgb, float alpha, float flag)
2860 void VM_drawfill(void)
2862 float *size, *pos, *rgb;
2865 VM_SAFEPARMCOUNT(5,VM_drawfill);
2868 pos = PRVM_G_VECTOR(OFS_PARM0);
2869 size = PRVM_G_VECTOR(OFS_PARM1);
2870 rgb = PRVM_G_VECTOR(OFS_PARM2);
2871 flag = (int) PRVM_G_FLOAT(OFS_PARM4);
2873 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2875 PRVM_G_FLOAT(OFS_RETURN) = -2;
2876 VM_Warning("VM_drawfill: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2880 if(pos[2] || size[2])
2881 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")));
2883 DrawQ_Fill(pos[0], pos[1], size[0], size[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM3), flag);
2884 PRVM_G_FLOAT(OFS_RETURN) = 1;
2891 drawsetcliparea(float x, float y, float width, float height)
2894 void VM_drawsetcliparea(void)
2897 VM_SAFEPARMCOUNT(4,VM_drawsetcliparea);
2899 x = bound(0, PRVM_G_FLOAT(OFS_PARM0), vid_conwidth.integer);
2900 y = bound(0, PRVM_G_FLOAT(OFS_PARM1), vid_conheight.integer);
2901 w = bound(0, PRVM_G_FLOAT(OFS_PARM2) + PRVM_G_FLOAT(OFS_PARM0) - x, (vid_conwidth.integer - x));
2902 h = bound(0, PRVM_G_FLOAT(OFS_PARM3) + PRVM_G_FLOAT(OFS_PARM1) - y, (vid_conheight.integer - y));
2904 DrawQ_SetClipArea(x, y, w, h);
2909 VM_drawresetcliparea
2914 void VM_drawresetcliparea(void)
2916 VM_SAFEPARMCOUNT(0,VM_drawresetcliparea);
2918 DrawQ_ResetClipArea();
2925 vector getimagesize(string pic)
2928 void VM_getimagesize(void)
2933 VM_SAFEPARMCOUNT(1,VM_getimagesize);
2935 p = PRVM_G_STRING(OFS_PARM0);
2936 VM_CheckEmptyString (p);
2938 pic = Draw_CachePic (p, false);
2940 PRVM_G_VECTOR(OFS_RETURN)[0] = pic->width;
2941 PRVM_G_VECTOR(OFS_RETURN)[1] = pic->height;
2942 PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
2949 string keynumtostring(float keynum)
2952 void VM_keynumtostring (void)
2954 VM_SAFEPARMCOUNT(1, VM_keynumtostring);
2956 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(Key_KeynumToString((int)PRVM_G_FLOAT(OFS_PARM0)));
2963 float stringtokeynum(string key)
2966 void VM_stringtokeynum (void)
2968 VM_SAFEPARMCOUNT( 1, VM_keynumtostring );
2970 PRVM_G_INT(OFS_RETURN) = Key_StringToKeynum(PRVM_G_STRING(OFS_PARM0));
2973 // CL_Video interface functions
2976 ========================
2979 float cin_open(string file, string name)
2980 ========================
2982 void VM_cin_open( void )
2987 VM_SAFEPARMCOUNT( 2, VM_cin_open );
2989 file = PRVM_G_STRING( OFS_PARM0 );
2990 name = PRVM_G_STRING( OFS_PARM1 );
2992 VM_CheckEmptyString( file );
2993 VM_CheckEmptyString( name );
2995 if( CL_OpenVideo( file, name, MENUOWNER ) )
2996 PRVM_G_FLOAT( OFS_RETURN ) = 1;
2998 PRVM_G_FLOAT( OFS_RETURN ) = 0;
3002 ========================
3005 void cin_close(string name)
3006 ========================
3008 void VM_cin_close( void )
3012 VM_SAFEPARMCOUNT( 1, VM_cin_close );
3014 name = PRVM_G_STRING( OFS_PARM0 );
3015 VM_CheckEmptyString( name );
3017 CL_CloseVideo( CL_GetVideoByName( name ) );
3021 ========================
3023 void cin_setstate(string name, float type)
3024 ========================
3026 void VM_cin_setstate( void )
3029 clvideostate_t state;
3032 VM_SAFEPARMCOUNT( 2, VM_cin_netstate );
3034 name = PRVM_G_STRING( OFS_PARM0 );
3035 VM_CheckEmptyString( name );
3037 state = (clvideostate_t)((int)PRVM_G_FLOAT( OFS_PARM1 ));
3039 video = CL_GetVideoByName( name );
3040 if( video && state > CLVIDEO_UNUSED && state < CLVIDEO_STATECOUNT )
3041 CL_SetVideoState( video, state );
3045 ========================
3048 float cin_getstate(string name)
3049 ========================
3051 void VM_cin_getstate( void )
3056 VM_SAFEPARMCOUNT( 1, VM_cin_getstate );
3058 name = PRVM_G_STRING( OFS_PARM0 );
3059 VM_CheckEmptyString( name );
3061 video = CL_GetVideoByName( name );
3063 PRVM_G_FLOAT( OFS_RETURN ) = (int)video->state;
3065 PRVM_G_FLOAT( OFS_RETURN ) = 0;
3069 ========================
3072 void cin_restart(string name)
3073 ========================
3075 void VM_cin_restart( void )
3080 VM_SAFEPARMCOUNT( 1, VM_cin_restart );
3082 name = PRVM_G_STRING( OFS_PARM0 );
3083 VM_CheckEmptyString( name );
3085 video = CL_GetVideoByName( name );
3087 CL_RestartVideo( video );
3094 Writes new values for v_forward, v_up, and v_right based on angles
3095 void makevectors(vector angle)
3098 void VM_makevectors (void)
3100 prvm_eval_t *valforward, *valright, *valup;
3101 valforward = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_forward);
3102 valright = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_right);
3103 valup = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_up);
3104 if (!valforward || !valright || !valup)
3106 VM_Warning("makevectors: could not find v_forward, v_right, or v_up global variables\n");
3109 VM_SAFEPARMCOUNT(1, VM_makevectors);
3110 AngleVectors (PRVM_G_VECTOR(OFS_PARM0), valforward->vector, valright->vector, valup->vector);
3117 Writes new values for v_forward, v_up, and v_right based on the given forward vector
3118 vectorvectors(vector)
3121 void VM_vectorvectors (void)
3123 prvm_eval_t *valforward, *valright, *valup;
3124 valforward = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_forward);
3125 valright = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_right);
3126 valup = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_up);
3127 if (!valforward || !valright || !valup)
3129 VM_Warning("vectorvectors: could not find v_forward, v_right, or v_up global variables\n");
3132 VM_SAFEPARMCOUNT(1, VM_vectorvectors);
3133 VectorNormalize2(PRVM_G_VECTOR(OFS_PARM0), valforward->vector);
3134 VectorVectors(valforward->vector, valright->vector, valup->vector);
3138 ========================
3141 void drawline(float width, vector pos1, vector pos2, vector rgb, float alpha, float flags)
3142 ========================
3144 void VM_drawline (void)
3146 float *c1, *c2, *rgb;
3148 unsigned char flags;
3150 VM_SAFEPARMCOUNT(6, VM_drawline);
3151 width = PRVM_G_FLOAT(OFS_PARM0);
3152 c1 = PRVM_G_VECTOR(OFS_PARM1);
3153 c2 = PRVM_G_VECTOR(OFS_PARM2);
3154 rgb = PRVM_G_VECTOR(OFS_PARM3);
3155 alpha = PRVM_G_FLOAT(OFS_PARM4);
3156 flags = (int)PRVM_G_FLOAT(OFS_PARM5);
3157 DrawQ_Line(width, c1[0], c1[1], c2[0], c2[1], rgb[0], rgb[1], rgb[2], alpha, flags);
3164 // float(float number, float quantity) bitshift (EXT_BITSHIFT)
3165 void VM_bitshift (void)
3168 VM_SAFEPARMCOUNT(2, VM_bitshift);
3170 n1 = (int)fabs((int)PRVM_G_FLOAT(OFS_PARM0));
3171 n2 = (int)PRVM_G_FLOAT(OFS_PARM1);
3173 PRVM_G_FLOAT(OFS_RETURN) = n1;
3176 PRVM_G_FLOAT(OFS_RETURN) = (n1 >> -n2);
3178 PRVM_G_FLOAT(OFS_RETURN) = (n1 << n2);
3181 ////////////////////////////////////////
3182 // AltString functions
3183 ////////////////////////////////////////
3186 ========================
3189 float altstr_count(string)
3190 ========================
3192 void VM_altstr_count( void )
3194 const char *altstr, *pos;
3197 VM_SAFEPARMCOUNT( 1, VM_altstr_count );
3199 altstr = PRVM_G_STRING( OFS_PARM0 );
3200 //VM_CheckEmptyString( altstr );
3202 for( count = 0, pos = altstr ; *pos ; pos++ ) {
3203 if( *pos == '\\' ) {
3207 } else if( *pos == '\'' ) {
3212 PRVM_G_FLOAT( OFS_RETURN ) = (float) (count / 2);
3216 ========================
3219 string altstr_prepare(string)
3220 ========================
3222 void VM_altstr_prepare( void )
3225 const char *instr, *in;
3227 char outstr[VM_STRINGTEMP_LENGTH];
3229 VM_SAFEPARMCOUNT( 1, VM_altstr_prepare );
3231 instr = PRVM_G_STRING( OFS_PARM0 );
3233 for( out = outstr, in = instr, size = sizeof(outstr) - 1 ; size && *in ; size--, in++, out++ )
3242 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
3246 ========================
3249 string altstr_get(string, float)
3250 ========================
3252 void VM_altstr_get( void )
3254 const char *altstr, *pos;
3257 char outstr[VM_STRINGTEMP_LENGTH];
3259 VM_SAFEPARMCOUNT( 2, VM_altstr_get );
3261 altstr = PRVM_G_STRING( OFS_PARM0 );
3263 count = (int)PRVM_G_FLOAT( OFS_PARM1 );
3264 count = count * 2 + 1;
3266 for( pos = altstr ; *pos && count ; pos++ )
3267 if( *pos == '\\' ) {
3270 } else if( *pos == '\'' )
3274 PRVM_G_INT( OFS_RETURN ) = 0;
3278 for( out = outstr, size = sizeof(outstr) - 1 ; size && *pos ; size--, pos++, out++ )
3279 if( *pos == '\\' ) {
3284 } else if( *pos == '\'' )
3290 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
3294 ========================
3297 string altstr_set(string altstr, float num, string set)
3298 ========================
3300 void VM_altstr_set( void )
3303 const char *altstr, *str;
3306 char outstr[VM_STRINGTEMP_LENGTH];
3308 VM_SAFEPARMCOUNT( 3, VM_altstr_set );
3310 altstr = PRVM_G_STRING( OFS_PARM0 );
3312 num = (int)PRVM_G_FLOAT( OFS_PARM1 );
3314 str = PRVM_G_STRING( OFS_PARM2 );
3317 for( num = num * 2 + 1, in = altstr; *in && num; *out++ = *in++ )
3322 } else if( *in == '\'' ) {
3327 for( ; *str; *out++ = *str++ );
3328 // now jump over the old content
3330 if( *in == '\'' || (*in == '\\' && !*++in) )
3333 strlcpy(out, in, outstr + sizeof(outstr) - out);
3334 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
3338 ========================
3341 string altstr_ins(string altstr, float num, string set)
3342 ========================
3344 void VM_altstr_ins(void)
3352 char outstr[VM_STRINGTEMP_LENGTH];
3354 VM_SAFEPARMCOUNT(3, VM_altstr_ins);
3356 in = instr = PRVM_G_STRING( OFS_PARM0 );
3357 num = (int)PRVM_G_FLOAT( OFS_PARM1 );
3358 set = setstr = PRVM_G_STRING( OFS_PARM2 );
3361 for( num = num * 2 + 2 ; *in && num > 0 ; *out++ = *in++ )
3366 } else if( *in == '\'' ) {
3371 for( ; *set ; *out++ = *set++ );
3374 strlcpy(out, in, outstr + sizeof(outstr) - out);
3375 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
3379 ////////////////////////////////////////
3380 // BufString functions
3381 ////////////////////////////////////////
3382 //[515]: string buffers support
3383 #define MAX_QCSTR_BUFFERS 128
3384 #define MAX_QCSTR_STRINGS 1024
3389 char *strings[MAX_QCSTR_STRINGS];
3392 // FIXME: move stringbuffers to prog_t to allow multiple progs!
3393 static qcstrbuffer_t *qcstringbuffers[MAX_QCSTR_BUFFERS];
3394 static int num_qcstringbuffers;
3395 static int buf_sortpower;
3397 #define BUFSTR_BUFFER(a) (a>=MAX_QCSTR_BUFFERS) ? NULL : (qcstringbuffers[a])
3398 #define BUFSTR_ISFREE(a) (a<MAX_QCSTR_BUFFERS&&qcstringbuffers[a]&&qcstringbuffers[a]->num_strings<=0) ? 1 : 0
3400 static int BufStr_FindFreeBuffer (void)
3403 if(num_qcstringbuffers == MAX_QCSTR_BUFFERS)
3405 for(i=0;i<MAX_QCSTR_BUFFERS;i++)
3406 if(!qcstringbuffers[i])
3408 qcstringbuffers[i] = (qcstrbuffer_t *)Z_Malloc(sizeof(qcstrbuffer_t));
3409 memset(qcstringbuffers[i], 0, sizeof(qcstrbuffer_t));
3415 static void BufStr_ClearBuffer (int index)
3417 qcstrbuffer_t *b = qcstringbuffers[index];
3422 if(b->num_strings > 0)
3424 for(i=0;i<b->num_strings;i++)
3426 Z_Free(b->strings[i]);
3427 num_qcstringbuffers--;
3429 Z_Free(qcstringbuffers[index]);
3430 qcstringbuffers[index] = NULL;
3434 static int BufStr_FindFreeString (qcstrbuffer_t *b)
3437 for(i=0;i<b->num_strings;i++)
3438 if(!b->strings[i] || !b->strings[i][0])
3440 if(i == MAX_QCSTR_STRINGS) return -1;
3444 static int BufStr_SortStringsUP (const void *in1, const void *in2)
3447 a = *((const char **) in1);
3448 b = *((const char **) in2);
3450 if(!b[0]) return -1;
3451 return strncmp(a, b, buf_sortpower);
3454 static int BufStr_SortStringsDOWN (const void *in1, const void *in2)
3457 a = *((const char **) in1);
3458 b = *((const char **) in2);
3460 if(!b[0]) return -1;
3461 return strncmp(b, a, buf_sortpower);
3465 ========================
3467 creates new buffer, and returns it's index, returns -1 if failed
3468 float buf_create(void) = #460;
3469 ========================
3471 void VM_buf_create (void)
3474 VM_SAFEPARMCOUNT(0, VM_buf_create);
3475 i = BufStr_FindFreeBuffer();
3477 num_qcstringbuffers++;
3479 //Con_Printf("VM_buf_create: buffers overflow in %s\n", PRVM_NAME);
3480 PRVM_G_FLOAT(OFS_RETURN) = i;
3484 ========================
3486 deletes buffer and all strings in it
3487 void buf_del(float bufhandle) = #461;
3488 ========================
3490 void VM_buf_del (void)
3492 VM_SAFEPARMCOUNT(1, VM_buf_del);
3493 if(BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0)))
3494 BufStr_ClearBuffer((int)PRVM_G_FLOAT(OFS_PARM0));
3497 VM_Warning("VM_buf_del: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3503 ========================
3505 how many strings are stored in buffer
3506 float buf_getsize(float bufhandle) = #462;
3507 ========================
3509 void VM_buf_getsize (void)
3512 VM_SAFEPARMCOUNT(1, VM_buf_getsize);
3514 b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3517 PRVM_G_FLOAT(OFS_RETURN) = -1;
3518 VM_Warning("VM_buf_getsize: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3522 PRVM_G_FLOAT(OFS_RETURN) = b->num_strings;
3526 ========================
3528 copy all content from one buffer to another, make sure it exists
3529 void buf_copy(float bufhandle_from, float bufhandle_to) = #463;
3530 ========================
3532 void VM_buf_copy (void)
3534 qcstrbuffer_t *b1, *b2;
3536 VM_SAFEPARMCOUNT(2, VM_buf_copy);
3538 b1 = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3541 VM_Warning("VM_buf_copy: invalid source buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3544 i = (int)PRVM_G_FLOAT(OFS_PARM1);
3545 if(i == (int)PRVM_G_FLOAT(OFS_PARM0))
3547 VM_Warning("VM_buf_copy: source == destination (%i) in %s\n", i, PRVM_NAME);
3550 b2 = BUFSTR_BUFFER(i);
3553 VM_Warning("VM_buf_copy: invalid destination buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM1), PRVM_NAME);
3557 BufStr_ClearBuffer(i);
3558 qcstringbuffers[i] = (qcstrbuffer_t *)Z_Malloc(sizeof(qcstrbuffer_t));
3559 memset(qcstringbuffers[i], 0, sizeof(qcstrbuffer_t));
3560 b2->num_strings = b1->num_strings;
3562 for(i=0;i<b1->num_strings;i++)
3563 if(b1->strings[i] && b1->strings[i][0])
3566 stringlen = strlen(b1->strings[i]) + 1;
3567 b2->strings[i] = (char *)Z_Malloc(stringlen);
3570 VM_Warning("VM_buf_copy: not enough memory for buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM1), PRVM_NAME);
3573 memcpy(b2->strings[i], b1->strings[i], stringlen);
3578 ========================
3580 sort buffer by beginnings of strings (sortpower defaults it's lenght)
3581 "backward == TRUE" means that sorting goes upside-down
3582 void buf_sort(float bufhandle, float sortpower, float backward) = #464;
3583 ========================
3585 void VM_buf_sort (void)
3589 VM_SAFEPARMCOUNT(3, VM_buf_sort);
3591 b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3594 VM_Warning("VM_buf_sort: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3597 if(b->num_strings <= 0)
3599 VM_Warning("VM_buf_sort: tried to sort empty buffer %i in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3602 buf_sortpower = (int)PRVM_G_FLOAT(OFS_PARM1);
3603 if(buf_sortpower <= 0)
3604 buf_sortpower = 99999999;
3606 if(!PRVM_G_FLOAT(OFS_PARM2))
3607 qsort(b->strings, b->num_strings, sizeof(char*), BufStr_SortStringsUP);
3609 qsort(b->strings, b->num_strings, sizeof(char*), BufStr_SortStringsDOWN);
3611 for(i=b->num_strings-1;i>=0;i--) //[515]: delete empty lines
3614 if(b->strings[i][0])
3618 Z_Free(b->strings[i]);
3620 b->strings[i] = NULL;
3628 ========================
3630 concantenates all buffer string into one with "glue" separator and returns it as tempstring
3631 string buf_implode(float bufhandle, string glue) = #465;
3632 ========================
3634 void VM_buf_implode (void)
3637 char k[VM_STRINGTEMP_LENGTH];
3641 VM_SAFEPARMCOUNT(2, VM_buf_implode);
3643 b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3644 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
3647 VM_Warning("VM_buf_implode: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3652 sep = PRVM_G_STRING(OFS_PARM1);
3654 for(l=i=0;i<b->num_strings;i++)
3657 l += (i > 0 ? strlen(sep) : 0) + strlen(b->strings[i]);
3658 if (l >= sizeof(k) - 1)
3660 strlcat(k, sep, sizeof(k));
3661 strlcat(k, b->strings[i], sizeof(k));
3663 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(k);
3667 ========================
3669 get a string from buffer, returns tempstring, dont str_unzone it!
3670 string bufstr_get(float bufhandle, float string_index) = #465;
3671 ========================
3673 void VM_bufstr_get (void)
3677 VM_SAFEPARMCOUNT(2, VM_bufstr_get);
3679 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
3680 b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3683 VM_Warning("VM_bufstr_get: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3686 strindex = (int)PRVM_G_FLOAT(OFS_PARM1);
3687 if(strindex < 0 || strindex > MAX_QCSTR_STRINGS)
3689 VM_Warning("VM_bufstr_get: invalid string index %i used in %s\n", strindex, PRVM_NAME);
3692 if(b->num_strings <= strindex)
3694 if(b->strings[strindex])
3695 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(b->strings[strindex]);
3699 ========================
3701 copies a string into selected slot of buffer
3702 void bufstr_set(float bufhandle, float string_index, string str) = #466;
3703 ========================
3705 void VM_bufstr_set (void)
3707 int bufindex, strindex;
3712 VM_SAFEPARMCOUNT(3, VM_bufstr_set);
3714 bufindex = (int)PRVM_G_FLOAT(OFS_PARM0);
3715 b = BUFSTR_BUFFER(bufindex);
3718 VM_Warning("VM_bufstr_set: invalid buffer %i used in %s\n", bufindex, PRVM_NAME);
3721 strindex = (int)PRVM_G_FLOAT(OFS_PARM1);
3722 if(strindex < 0 || strindex > MAX_QCSTR_STRINGS)
3724 VM_Warning("VM_bufstr_set: invalid string index %i used in %s\n", strindex, PRVM_NAME);
3727 news = PRVM_G_STRING(OFS_PARM2);
3728 if(b->strings[strindex])
3729 Z_Free(b->strings[strindex]);
3730 alloclen = strlen(news) + 1;
3731 b->strings[strindex] = (char *)Z_Malloc(alloclen);
3732 memcpy(b->strings[strindex], news, alloclen);
3736 ========================
3738 adds string to buffer in nearest free slot and returns it
3739 "order == TRUE" means that string will be added after last "full" slot
3740 float bufstr_add(float bufhandle, string str, float order) = #467;
3741 ========================
3743 void VM_bufstr_add (void)
3745 int bufindex, order, strindex;
3750 VM_SAFEPARMCOUNT(3, VM_bufstr_add);
3752 bufindex = (int)PRVM_G_FLOAT(OFS_PARM0);
3753 b = BUFSTR_BUFFER(bufindex);
3754 PRVM_G_FLOAT(OFS_RETURN) = -1;
3757 VM_Warning("VM_bufstr_add: invalid buffer %i used in %s\n", bufindex, PRVM_NAME);
3760 string = PRVM_G_STRING(OFS_PARM1);
3761 order = (int)PRVM_G_FLOAT(OFS_PARM2);
3763 strindex = b->num_strings;
3766 strindex = BufStr_FindFreeString(b);
3769 VM_Warning("VM_bufstr_add: buffer %i has no free string slots in %s\n", bufindex, PRVM_NAME);
3774 while(b->num_strings <= strindex)
3776 if(b->num_strings == MAX_QCSTR_STRINGS)
3778 VM_Warning("VM_bufstr_add: buffer %i has no free string slots in %s\n", bufindex, PRVM_NAME);
3781 b->strings[b->num_strings] = NULL;
3784 if(b->strings[strindex])
3785 Z_Free(b->strings[strindex]);
3786 alloclen = strlen(string) + 1;
3787 b->strings[strindex] = (char *)Z_Malloc(alloclen);
3788 memcpy(b->strings[strindex], string, alloclen);
3789 PRVM_G_FLOAT(OFS_RETURN) = strindex;
3793 ========================
3795 delete string from buffer
3796 void bufstr_free(float bufhandle, float string_index) = #468;
3797 ========================
3799 void VM_bufstr_free (void)
3803 VM_SAFEPARMCOUNT(2, VM_bufstr_free);
3805 b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3808 VM_Warning("VM_bufstr_free: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3811 i = (int)PRVM_G_FLOAT(OFS_PARM1);
3812 if(i < 0 || i > MAX_QCSTR_STRINGS)
3814 VM_Warning("VM_bufstr_free: invalid string index %i used in %s\n", i, PRVM_NAME);
3818 Z_Free(b->strings[i]);
3819 b->strings[i] = NULL;
3820 if(i+1 == b->num_strings)
3830 This was a major timewaster in progs, so it was converted to C
3833 void VM_changeyaw (void)
3836 float ideal, current, move, speed;
3838 // this is called (VERY HACKISHLY) by SV_MoveToGoal, so it can not use any
3839 // parameters because they are the parameters to SV_MoveToGoal, not this
3840 //VM_SAFEPARMCOUNT(0, VM_changeyaw);
3842 ent = PRVM_PROG_TO_EDICT(PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict);
3843 if (ent == prog->edicts)
3845 VM_Warning("changeyaw: can not modify world entity\n");
3848 if (ent->priv.server->free)
3850 VM_Warning("changeyaw: can not modify free entity\n");
3853 if (prog->fieldoffsets.angles < 0 || prog->fieldoffsets.ideal_yaw < 0 || prog->fieldoffsets.yaw_speed < 0)
3855 VM_Warning("changeyaw: angles, ideal_yaw, or yaw_speed field(s) not found\n");
3858 current = ANGLEMOD(PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.angles)->vector[1]);
3859 ideal = PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.ideal_yaw)->_float;
3860 speed = PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.yaw_speed)->_float;
3862 if (current == ideal)
3864 move = ideal - current;
3865 if (ideal > current)
3886 PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.angles)->vector[1] = ANGLEMOD (current + move);
3894 void VM_changepitch (void)
3897 float ideal, current, move, speed;
3899 VM_SAFEPARMCOUNT(1, VM_changepitch);
3901 ent = PRVM_G_EDICT(OFS_PARM0);
3902 if (ent == prog->edicts)
3904 VM_Warning("changepitch: can not modify world entity\n");
3907 if (ent->priv.server->free)
3909 VM_Warning("changepitch: can not modify free entity\n");
3912 if (prog->fieldoffsets.angles < 0 || prog->fieldoffsets.idealpitch < 0 || prog->fieldoffsets.pitch_speed < 0)
3914 VM_Warning("changepitch: angles, idealpitch, or pitch_speed field(s) not found\n");
3917 current = ANGLEMOD(PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.angles)->vector[0]);
3918 ideal = PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.idealpitch)->_float;
3919 speed = PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.pitch_speed)->_float;
3921 if (current == ideal)
3923 move = ideal - current;
3924 if (ideal > current)
3945 PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.angles)->vector[0] = ANGLEMOD (current + move);
3949 static int Is_Text_Color (char c, char t)
3952 char c2 = c - (c & 128);
3953 char t2 = t - (t & 128);
3955 if(c != STRING_COLOR_TAG && c2 != STRING_COLOR_TAG) return 0;
3956 if(t >= '0' && t <= '9') a = 1;
3957 if(t2 >= '0' && t2 <= '9') a = 1;
3958 /* if(t >= 'A' && t <= 'Z') a = 2;
3959 if(t2 >= 'A' && t2 <= 'Z') a = 2;
3961 if(a == 1 && scr_colortext.integer > 0)
3963 if(a == 2 && scr_multifonts.integer > 0)
3969 void VM_uncolorstring (void)
3972 char out[VM_STRINGTEMP_LENGTH];
3975 VM_SAFEPARMCOUNT(1, VM_uncolorstring);
3976 in = PRVM_G_STRING(OFS_PARM0);
3977 VM_CheckEmptyString (in);
3982 if(Is_Text_Color(in[k], in[k+1]) == 1/* || (in[k] == '&' && in[k+1] == 'r')*/)
3991 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(out);
3994 // #221 float(string str, string sub[, float startpos]) strstrofs (FTE_STRINGS)
3995 //strstr, without generating a new string. Use in conjunction with FRIK_FILE's substring for more similar strstr.
3996 void VM_strstrofs (void)
3998 const char *instr, *match;
4000 VM_SAFEPARMCOUNTRANGE(2, 3, VM_strstrofs);
4001 instr = PRVM_G_STRING(OFS_PARM0);
4002 match = PRVM_G_STRING(OFS_PARM1);
4003 firstofs = (prog->argc > 2)?PRVM_G_FLOAT(OFS_PARM2):0;
4005 if (firstofs && (firstofs < 0 || firstofs > (int)strlen(instr)))
4007 PRVM_G_FLOAT(OFS_RETURN) = -1;
4011 match = strstr(instr+firstofs, match);
4013 PRVM_G_FLOAT(OFS_RETURN) = -1;
4015 PRVM_G_FLOAT(OFS_RETURN) = match - instr;
4018 //#222 string(string s, float index) str2chr (FTE_STRINGS)
4019 void VM_str2chr (void)
4022 VM_SAFEPARMCOUNT(2, VM_str2chr);
4023 s = PRVM_G_STRING(OFS_PARM0);
4024 if((unsigned)PRVM_G_FLOAT(OFS_PARM1) < strlen(s))
4025 PRVM_G_FLOAT(OFS_RETURN) = (unsigned char)s[(unsigned)PRVM_G_FLOAT(OFS_PARM1)];
4027 PRVM_G_FLOAT(OFS_RETURN) = 0;
4030 //#223 string(float c, ...) chr2str (FTE_STRINGS)
4031 void VM_chr2str (void)
4035 VM_SAFEPARMCOUNTRANGE(0, 8, VM_chr2str);
4036 for(i = 0;i < prog->argc && i < (int)sizeof(t) - 1;i++)
4037 t[i] = (unsigned char)PRVM_G_FLOAT(OFS_PARM0+i*3);
4039 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(t);
4042 static int chrconv_number(int i, int base, int conv)
4067 static int chrconv_punct(int i, int base, int conv)
4085 static int chrchar_alpha(int i, int basec, int baset, int convc, int convt, int charnum)
4087 //convert case and colour seperatly...
4104 baset = 128*((charnum&1) == (convt-5));
4120 return i + basec + baset;
4122 // #224 string(float ccase, float calpha, float cnum, string s, ...) strconv (FTE_STRINGS)
4123 //bulk convert a string. change case or colouring.
4124 void VM_strconv (void)
4126 int ccase, redalpha, rednum, len, i;
4127 unsigned char resbuf[VM_STRINGTEMP_LENGTH];
4128 unsigned char *result = resbuf;
4130 VM_SAFEPARMCOUNTRANGE(3, 8, VM_strconv);
4132 ccase = PRVM_G_FLOAT(OFS_PARM0); //0 same, 1 lower, 2 upper
4133 redalpha = PRVM_G_FLOAT(OFS_PARM1); //0 same, 1 white, 2 red, 5 alternate, 6 alternate-alternate
4134 rednum = PRVM_G_FLOAT(OFS_PARM2); //0 same, 1 white, 2 red, 3 redspecial, 4 whitespecial, 5 alternate, 6 alternate-alternate
4135 VM_VarString(3, (char *) resbuf, sizeof(resbuf));
4136 len = strlen((char *) resbuf);
4138 for (i = 0; i < len; i++, result++) //should this be done backwards?
4140 if (*result >= '0' && *result <= '9') //normal numbers...
4141 *result = chrconv_number(*result, '0', rednum);
4142 else if (*result >= '0'+128 && *result <= '9'+128)
4143 *result = chrconv_number(*result, '0'+128, rednum);
4144 else if (*result >= '0'+128-30 && *result <= '9'+128-30)
4145 *result = chrconv_number(*result, '0'+128-30, rednum);
4146 else if (*result >= '0'-30 && *result <= '9'-30)
4147 *result = chrconv_number(*result, '0'-30, rednum);
4149 else if (*result >= 'a' && *result <= 'z') //normal numbers...
4150 *result = chrchar_alpha(*result, 'a', 0, ccase, redalpha, i);
4151 else if (*result >= 'A' && *result <= 'Z') //normal numbers...
4152 *result = chrchar_alpha(*result, 'A', 0, ccase, redalpha, i);
4153 else if (*result >= 'a'+128 && *result <= 'z'+128) //normal numbers...
4154 *result = chrchar_alpha(*result, 'a', 128, ccase, redalpha, i);
4155 else if (*result >= 'A'+128 && *result <= 'Z'+128) //normal numbers...
4156 *result = chrchar_alpha(*result, 'A', 128, ccase, redalpha, i);
4158 else if ((*result & 127) < 16 || !redalpha) //special chars..
4160 else if (*result < 128)
4161 *result = chrconv_punct(*result, 0, redalpha);
4163 *result = chrconv_punct(*result, 128, redalpha);
4167 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString((char *) resbuf);
4170 // #225 string(float chars, string s, ...) strpad (FTE_STRINGS)
4171 void VM_strpad (void)
4173 char src[VM_STRINGTEMP_LENGTH];
4174 char destbuf[VM_STRINGTEMP_LENGTH];
4176 VM_SAFEPARMCOUNTRANGE(1, 8, VM_strpad);
4177 pad = PRVM_G_FLOAT(OFS_PARM0);
4178 VM_VarString(1, src, sizeof(src));
4180 // note: < 0 = left padding, > 0 = right padding,
4181 // this is reverse logic of printf!
4182 dpsnprintf(destbuf, sizeof(destbuf), "%*s", -pad, src);
4184 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(destbuf);
4187 // #226 string(string info, string key, string value, ...) infoadd (FTE_STRINGS)
4188 //uses qw style \key\value strings
4189 void VM_infoadd (void)
4191 const char *info, *key;
4192 char value[VM_STRINGTEMP_LENGTH];
4193 char temp[VM_STRINGTEMP_LENGTH];
4195 VM_SAFEPARMCOUNTRANGE(2, 8, VM_infoadd);
4196 info = PRVM_G_STRING(OFS_PARM0);
4197 key = PRVM_G_STRING(OFS_PARM1);
4198 VM_VarString(2, value, sizeof(value));
4200 strlcpy(temp, info, VM_STRINGTEMP_LENGTH);
4202 InfoString_SetValue(temp, VM_STRINGTEMP_LENGTH, key, value);
4204 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(temp);
4207 // #227 string(string info, string key) infoget (FTE_STRINGS)
4208 //uses qw style \key\value strings
4209 void VM_infoget (void)
4213 char value[VM_STRINGTEMP_LENGTH];
4215 VM_SAFEPARMCOUNT(2, VM_infoget);
4216 info = PRVM_G_STRING(OFS_PARM0);
4217 key = PRVM_G_STRING(OFS_PARM1);
4219 InfoString_GetValue(info, key, value, VM_STRINGTEMP_LENGTH);
4221 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(value);
4224 //#228 float(string s1, string s2, float len) strncmp (FTE_STRINGS)
4225 // also float(string s1, string s2) strcmp (FRIK_FILE)
4226 void VM_strncmp (void)
4228 const char *s1, *s2;
4229 VM_SAFEPARMCOUNTRANGE(2, 3, VM_strncmp);
4230 s1 = PRVM_G_STRING(OFS_PARM0);
4231 s2 = PRVM_G_STRING(OFS_PARM1);
4234 PRVM_G_FLOAT(OFS_RETURN) = strncmp(s1, s2, (size_t)PRVM_G_FLOAT(OFS_PARM2));
4238 PRVM_G_FLOAT(OFS_RETURN) = strcmp(s1, s2);
4242 // #229 float(string s1, string s2) strcasecmp (FTE_STRINGS)
4243 // #230 float(string s1, string s2, float len) strncasecmp (FTE_STRINGS)
4244 void VM_strncasecmp (void)
4246 const char *s1, *s2;
4247 VM_SAFEPARMCOUNTRANGE(2, 3, VM_strncasecmp);
4248 s1 = PRVM_G_STRING(OFS_PARM0);
4249 s2 = PRVM_G_STRING(OFS_PARM1);
4252 PRVM_G_FLOAT(OFS_RETURN) = strncasecmp(s1, s2, (size_t)PRVM_G_FLOAT(OFS_PARM2));
4256 PRVM_G_FLOAT(OFS_RETURN) = strcasecmp(s1, s2);
4260 void VM_wasfreed (void)
4262 VM_SAFEPARMCOUNT(1, VM_wasfreed);
4263 PRVM_G_FLOAT(OFS_RETURN) = PRVM_G_EDICT(OFS_PARM0)->priv.required->free;
4266 void VM_SetTraceGlobals(const trace_t *trace)
4269 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_allsolid)))
4270 val->_float = trace->allsolid;
4271 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_startsolid)))
4272 val->_float = trace->startsolid;
4273 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_fraction)))
4274 val->_float = trace->fraction;
4275 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_inwater)))
4276 val->_float = trace->inwater;
4277 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_inopen)))
4278 val->_float = trace->inopen;
4279 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_endpos)))
4280 VectorCopy(trace->endpos, val->vector);
4281 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_plane_normal)))
4282 VectorCopy(trace->plane.normal, val->vector);
4283 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_plane_dist)))
4284 val->_float = trace->plane.dist;
4285 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_ent)))
4286 val->edict = PRVM_EDICT_TO_PROG(trace->ent ? trace->ent : prog->edicts);
4287 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dpstartcontents)))
4288 val->_float = trace->startsupercontents;
4289 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dphitcontents)))
4290 val->_float = trace->hitsupercontents;
4291 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dphitq3surfaceflags)))
4292 val->_float = trace->hitq3surfaceflags;
4293 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dphittexturename)))
4294 val->string = trace->hittexture ? PRVM_SetTempString(trace->hittexture->name) : 0;
4299 void VM_Cmd_Init(void)
4301 // only init the stuff for the current prog
4304 // VM_BufStr_Init();
4307 void VM_Cmd_Reset(void)
4309 CL_PurgeOwner( MENUOWNER );
4311 VM_Files_CloseAll();
4312 // VM_BufStr_ShutDown();