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
10 // LordHavoc: changed this to NOT use a return statement, so that it can be used in functions that must return a value
11 void VM_Warning(const char *fmt, ...)
14 char msg[MAX_INPUTLINE];
17 dpvsnprintf(msg,sizeof(msg),fmt,argptr);
21 // TODO: either add a cvar/cmd to control the state dumping or replace some of the calls with Con_Printf [9/13/2006 Black]
26 //============================================================================
29 // TODO DONE: move vm_files and vm_fssearchlist to prvm_prog_t struct
30 // TODO: move vm_files and vm_fssearchlist back [9/13/2006 Black]
31 // TODO: (move vm_files and vm_fssearchlist to prvm_prog_t struct again) [2007-01-23 LordHavoc]
32 // TODO: will this war ever end? [2007-01-23 LordHavoc]
34 void VM_CheckEmptyString (const char *s)
37 PRVM_ERROR ("%s: Bad string", PRVM_NAME);
40 //============================================================================
43 void VM_VarString(int first, char *out, int outlength)
49 outend = out + outlength - 1;
50 for (i = first;i < prog->argc && out < outend;i++)
52 s = PRVM_G_STRING((OFS_PARM0+i*3));
53 while (out < outend && *s)
63 returns true if the extension is supported by the server
65 checkextension(extensionname)
69 // kind of helper function
70 static qboolean checkextension(const char *name)
74 len = (int)strlen(name);
76 for (e = prog->extensionstring;*e;e++)
83 while (*e && *e != ' ')
85 if ((e - start) == len && !strncasecmp(start, name, len))
91 void VM_checkextension (void)
93 VM_SAFEPARMCOUNT(1,VM_checkextension);
95 PRVM_G_FLOAT(OFS_RETURN) = checkextension(PRVM_G_STRING(OFS_PARM0));
102 This is a TERMINAL error, which will kill off the entire prog.
111 char string[VM_STRINGTEMP_LENGTH];
113 VM_VarString(0, string, sizeof(string));
114 Con_Printf("======%s ERROR in %s:\n%s\n", PRVM_NAME, PRVM_GetString(prog->xfunction->s_name), string);
115 if (prog->globaloffsets.self >= 0)
117 ed = PRVM_PROG_TO_EDICT(PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict);
121 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);
128 Dumps out self, then an error message. The program is aborted and self is
129 removed, but the level can continue.
134 void VM_objerror (void)
137 char string[VM_STRINGTEMP_LENGTH];
139 VM_VarString(0, string, sizeof(string));
140 Con_Printf("======OBJECT ERROR======\n"); // , PRVM_NAME, PRVM_GetString(prog->xfunction->s_name), string); // or include them? FIXME
141 if (prog->globaloffsets.self >= 0)
143 ed = PRVM_PROG_TO_EDICT(PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict);
149 // objerror has to display the object fields -> else call
150 PRVM_ERROR ("VM_objecterror: self not defined !");
151 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);
165 char string[VM_STRINGTEMP_LENGTH];
167 VM_VarString(0, string, sizeof(string));
175 broadcast print to everyone on server
180 void VM_bprint (void)
182 char string[VM_STRINGTEMP_LENGTH];
186 VM_Warning("VM_bprint: game is not server(%s) !\n", PRVM_NAME);
190 VM_VarString(0, string, sizeof(string));
191 SV_BroadcastPrint(string);
196 VM_sprint (menu & client but only if server.active == true)
198 single print to a specific client
200 sprint(float clientnum,...[string])
203 void VM_sprint (void)
207 char string[VM_STRINGTEMP_LENGTH];
209 VM_SAFEPARMCOUNTRANGE(1, 8, VM_sprint);
211 //find client for this entity
212 clientnum = (int)PRVM_G_FLOAT(OFS_PARM0);
213 if (!sv.active || clientnum < 0 || clientnum >= svs.maxclients || !svs.clients[clientnum].active)
215 VM_Warning("VM_sprint: %s: invalid client or server is not active !\n", PRVM_NAME);
219 client = svs.clients + clientnum;
220 if (!client->netconnection)
223 VM_VarString(1, string, sizeof(string));
224 MSG_WriteChar(&client->netconnection->message,svc_print);
225 MSG_WriteString(&client->netconnection->message, string);
232 single print to the screen
237 void VM_centerprint (void)
239 char string[VM_STRINGTEMP_LENGTH];
241 VM_SAFEPARMCOUNTRANGE(1, 8, VM_centerprint);
242 VM_VarString(0, string, sizeof(string));
243 SCR_CenterPrint(string);
250 vector normalize(vector)
253 void VM_normalize (void)
259 VM_SAFEPARMCOUNT(1,VM_normalize);
261 value1 = PRVM_G_VECTOR(OFS_PARM0);
263 f = VectorLength2(value1);
267 VectorScale(value1, f, newvalue);
270 VectorClear(newvalue);
272 VectorCopy (newvalue, PRVM_G_VECTOR(OFS_RETURN));
284 VM_SAFEPARMCOUNT(1,VM_vlen);
285 PRVM_G_FLOAT(OFS_RETURN) = VectorLength(PRVM_G_VECTOR(OFS_PARM0));
292 float vectoyaw(vector)
295 void VM_vectoyaw (void)
300 VM_SAFEPARMCOUNT(1,VM_vectoyaw);
302 value1 = PRVM_G_VECTOR(OFS_PARM0);
304 if (value1[1] == 0 && value1[0] == 0)
308 yaw = (int) (atan2(value1[1], value1[0]) * 180 / M_PI);
313 PRVM_G_FLOAT(OFS_RETURN) = yaw;
321 vector vectoangles(vector)
324 void VM_vectoangles (void)
330 VM_SAFEPARMCOUNT(1,VM_vectoangles);
332 value1 = PRVM_G_VECTOR(OFS_PARM0);
334 if (value1[1] == 0 && value1[0] == 0)
344 // LordHavoc: optimized a bit
347 yaw = (atan2(value1[1], value1[0]) * 180 / M_PI);
351 else if (value1[1] > 0)
356 forward = sqrt(value1[0]*value1[0] + value1[1]*value1[1]);
357 pitch = (atan2(value1[2], forward) * 180 / M_PI);
362 PRVM_G_FLOAT(OFS_RETURN+0) = pitch;
363 PRVM_G_FLOAT(OFS_RETURN+1) = yaw;
364 PRVM_G_FLOAT(OFS_RETURN+2) = 0;
371 Returns a number from 0<= num < 1
376 void VM_random (void)
378 VM_SAFEPARMCOUNT(0,VM_random);
380 PRVM_G_FLOAT(OFS_RETURN) = lhrandom(0, 1);
387 localsound(string sample)
390 void VM_localsound(void)
394 VM_SAFEPARMCOUNT(1,VM_localsound);
396 s = PRVM_G_STRING(OFS_PARM0);
398 if(!S_LocalSound (s))
400 PRVM_G_FLOAT(OFS_RETURN) = -4;
401 VM_Warning("VM_localsound: Failed to play %s for %s !\n", s, PRVM_NAME);
405 PRVM_G_FLOAT(OFS_RETURN) = 1;
417 PRVM_ERROR ("%s: break statement", PRVM_NAME);
420 //============================================================================
426 Sends text over to the client's execution buffer
428 [localcmd (string, ...) or]
432 void VM_localcmd (void)
434 char string[VM_STRINGTEMP_LENGTH];
435 VM_SAFEPARMCOUNTRANGE(1, 8, VM_localcmd);
436 VM_VarString(0, string, sizeof(string));
437 Cbuf_AddText(string);
449 VM_SAFEPARMCOUNT(1,VM_cvar);
451 PRVM_G_FLOAT(OFS_RETURN) = Cvar_VariableValue(PRVM_G_STRING(OFS_PARM0));
458 const string VM_cvar_string (string)
461 void VM_cvar_string(void)
464 VM_SAFEPARMCOUNT(1,VM_cvar_string);
466 name = PRVM_G_STRING(OFS_PARM0);
468 VM_CheckEmptyString(name);
470 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(Cvar_VariableString(name));
475 ========================
478 const string VM_cvar_defstring (string)
479 ========================
481 void VM_cvar_defstring (void)
484 VM_SAFEPARMCOUNT(1,VM_cvar_string);
486 name = PRVM_G_STRING(OFS_PARM0);
488 VM_CheckEmptyString(name);
490 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(Cvar_VariableDefString(name));
496 void cvar_set (string,string)
499 void VM_cvar_set (void)
501 VM_SAFEPARMCOUNT(2,VM_cvar_set);
503 Cvar_Set(PRVM_G_STRING(OFS_PARM0), PRVM_G_STRING(OFS_PARM1));
513 void VM_dprint (void)
515 char string[VM_STRINGTEMP_LENGTH];
516 VM_SAFEPARMCOUNTRANGE(1, 8, VM_dprint);
517 if (developer.integer)
519 VM_VarString(0, string, sizeof(string));
521 Con_Printf("%s", string);
523 Con_Printf("%s: %s", PRVM_NAME, string);
541 VM_SAFEPARMCOUNT(1, VM_ftos);
543 v = PRVM_G_FLOAT(OFS_PARM0);
545 if ((float)((int)v) == v)
546 sprintf(s, "%i", (int)v);
549 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(s);
564 VM_SAFEPARMCOUNT(1,VM_fabs);
566 v = PRVM_G_FLOAT(OFS_PARM0);
567 PRVM_G_FLOAT(OFS_RETURN) = fabs(v);
582 VM_SAFEPARMCOUNT(1,VM_vtos);
584 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]);
585 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(s);
600 VM_SAFEPARMCOUNT(1, VM_etos);
602 sprintf (s, "entity %i", PRVM_G_EDICTNUM(OFS_PARM0));
603 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(s);
610 float stof(...[string])
615 char string[VM_STRINGTEMP_LENGTH];
616 VM_SAFEPARMCOUNTRANGE(1, 8, VM_stof);
617 VM_VarString(0, string, sizeof(string));
618 PRVM_G_FLOAT(OFS_RETURN) = atof(string);
622 ========================
626 ========================
630 VM_SAFEPARMCOUNT(1, VM_itof);
631 PRVM_G_FLOAT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
635 ========================
638 entity ftoe(float num)
639 ========================
644 VM_SAFEPARMCOUNT(1, VM_ftoe);
646 ent = (int)PRVM_G_FLOAT(OFS_PARM0);
647 if (ent < 0 || ent >= MAX_EDICTS || PRVM_PROG_TO_EDICT(ent)->priv.required->free)
648 ent = 0; // return world instead of a free or invalid entity
650 PRVM_G_INT(OFS_RETURN) = ent;
657 string strftime(float uselocaltime, string[, string ...])
660 void VM_strftime(void)
664 char fmt[VM_STRINGTEMP_LENGTH];
665 char result[VM_STRINGTEMP_LENGTH];
666 VM_SAFEPARMCOUNTRANGE(2, 8, VM_strftime);
667 VM_VarString(1, fmt, sizeof(fmt));
669 if (PRVM_G_FLOAT(OFS_PARM0))
675 PRVM_G_FLOAT(OFS_RETURN) = 0;
678 strftime(result, sizeof(result), fmt, tm);
679 PRVM_G_FLOAT(OFS_RETURN) = PRVM_SetTempString(result);
693 VM_SAFEPARMCOUNT(0, VM_spawn);
694 prog->xfunction->builtinsprofile += 20;
695 ed = PRVM_ED_Alloc();
707 void VM_remove (void)
710 prog->xfunction->builtinsprofile += 20;
712 VM_SAFEPARMCOUNT(1, VM_remove);
714 ed = PRVM_G_EDICT(OFS_PARM0);
715 if( PRVM_NUM_FOR_EDICT(ed) <= prog->reserved_edicts )
717 if (developer.integer >= 1)
718 VM_Warning( "VM_remove: tried to remove the null entity or a reserved entity!\n" );
720 else if( ed->priv.required->free )
722 if (developer.integer >= 1)
723 VM_Warning( "VM_remove: tried to remove an already freed entity!\n" );
727 // if (ed == prog->edicts)
728 // PRVM_ERROR ("remove: tried to remove world");
729 // if (PRVM_NUM_FOR_EDICT(ed) <= sv.maxclients)
730 // Host_Error("remove: tried to remove a client");
737 entity find(entity start, .string field, string match)
748 VM_SAFEPARMCOUNT(3,VM_find);
750 e = PRVM_G_EDICTNUM(OFS_PARM0);
751 f = PRVM_G_INT(OFS_PARM1);
752 s = PRVM_G_STRING(OFS_PARM2);
754 // LordHavoc: apparently BloodMage does a find(world, weaponmodel, "") and
755 // expects it to find all the monsters, so we must be careful to support
758 for (e++ ; e < prog->num_edicts ; e++)
760 prog->xfunction->builtinsprofile++;
761 ed = PRVM_EDICT_NUM(e);
762 if (ed->priv.required->free)
764 t = PRVM_E_STRING(ed,f);
774 VM_RETURN_EDICT(prog->edicts);
781 entity findfloat(entity start, .float field, float match)
782 entity findentity(entity start, .entity field, entity match)
785 // LordHavoc: added this for searching float, int, and entity reference fields
786 void VM_findfloat (void)
793 VM_SAFEPARMCOUNT(3,VM_findfloat);
795 e = PRVM_G_EDICTNUM(OFS_PARM0);
796 f = PRVM_G_INT(OFS_PARM1);
797 s = PRVM_G_FLOAT(OFS_PARM2);
799 for (e++ ; e < prog->num_edicts ; e++)
801 prog->xfunction->builtinsprofile++;
802 ed = PRVM_EDICT_NUM(e);
803 if (ed->priv.required->free)
805 if (PRVM_E_FLOAT(ed,f) == s)
812 VM_RETURN_EDICT(prog->edicts);
819 entity findchain(.string field, string match)
822 // chained search for strings in entity fields
823 // entity(.string field, string match) findchain = #402;
824 void VM_findchain (void)
829 prvm_edict_t *ent, *chain;
831 VM_SAFEPARMCOUNT(2,VM_findchain);
833 if (prog->fieldoffsets.chain < 0)
834 PRVM_ERROR("VM_findchain: %s doesnt have a chain field !", PRVM_NAME);
836 chain = prog->edicts;
838 f = PRVM_G_INT(OFS_PARM0);
839 s = PRVM_G_STRING(OFS_PARM1);
841 // LordHavoc: apparently BloodMage does a find(world, weaponmodel, "") and
842 // expects it to find all the monsters, so we must be careful to support
845 ent = PRVM_NEXT_EDICT(prog->edicts);
846 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
848 prog->xfunction->builtinsprofile++;
849 if (ent->priv.required->free)
851 t = PRVM_E_STRING(ent,f);
857 PRVM_EDICTFIELDVALUE(ent,prog->fieldoffsets.chain)->edict = PRVM_NUM_FOR_EDICT(chain);
861 VM_RETURN_EDICT(chain);
868 entity findchainfloat(.string field, float match)
869 entity findchainentity(.string field, entity match)
872 // LordHavoc: chained search for float, int, and entity reference fields
873 // entity(.string field, float match) findchainfloat = #403;
874 void VM_findchainfloat (void)
879 prvm_edict_t *ent, *chain;
881 VM_SAFEPARMCOUNT(2, VM_findchainfloat);
883 if (prog->fieldoffsets.chain < 0)
884 PRVM_ERROR("VM_findchainfloat: %s doesnt have a chain field !", PRVM_NAME);
886 chain = (prvm_edict_t *)prog->edicts;
888 f = PRVM_G_INT(OFS_PARM0);
889 s = PRVM_G_FLOAT(OFS_PARM1);
891 ent = PRVM_NEXT_EDICT(prog->edicts);
892 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
894 prog->xfunction->builtinsprofile++;
895 if (ent->priv.required->free)
897 if (PRVM_E_FLOAT(ent,f) != s)
900 PRVM_EDICTFIELDVALUE(ent,prog->fieldoffsets.chain)->edict = PRVM_EDICT_TO_PROG(chain);
904 VM_RETURN_EDICT(chain);
908 ========================
911 entity findflags(entity start, .float field, float match)
912 ========================
914 // LordHavoc: search for flags in float fields
915 void VM_findflags (void)
922 VM_SAFEPARMCOUNT(3, VM_findflags);
925 e = PRVM_G_EDICTNUM(OFS_PARM0);
926 f = PRVM_G_INT(OFS_PARM1);
927 s = (int)PRVM_G_FLOAT(OFS_PARM2);
929 for (e++ ; e < prog->num_edicts ; e++)
931 prog->xfunction->builtinsprofile++;
932 ed = PRVM_EDICT_NUM(e);
933 if (ed->priv.required->free)
935 if (!PRVM_E_FLOAT(ed,f))
937 if ((int)PRVM_E_FLOAT(ed,f) & s)
944 VM_RETURN_EDICT(prog->edicts);
948 ========================
951 entity findchainflags(.float field, float match)
952 ========================
954 // LordHavoc: chained search for flags in float fields
955 void VM_findchainflags (void)
960 prvm_edict_t *ent, *chain;
962 VM_SAFEPARMCOUNT(2, VM_findchainflags);
964 if (prog->fieldoffsets.chain < 0)
965 PRVM_ERROR("VM_findchainflags: %s doesnt have a chain field !", PRVM_NAME);
967 chain = (prvm_edict_t *)prog->edicts;
969 f = PRVM_G_INT(OFS_PARM0);
970 s = (int)PRVM_G_FLOAT(OFS_PARM1);
972 ent = PRVM_NEXT_EDICT(prog->edicts);
973 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
975 prog->xfunction->builtinsprofile++;
976 if (ent->priv.required->free)
978 if (!PRVM_E_FLOAT(ent,f))
980 if (!((int)PRVM_E_FLOAT(ent,f) & s))
983 PRVM_EDICTFIELDVALUE(ent,prog->fieldoffsets.chain)->edict = PRVM_EDICT_TO_PROG(chain);
987 VM_RETURN_EDICT(chain);
994 string precache_sound (string sample)
997 void VM_precache_sound (void)
1001 VM_SAFEPARMCOUNT(1, VM_precache_sound);
1003 s = PRVM_G_STRING(OFS_PARM0);
1004 PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
1005 VM_CheckEmptyString(s);
1007 if(snd_initialized.integer && !S_PrecacheSound(s, true, false))
1009 VM_Warning("VM_precache_sound: Failed to load %s for %s\n", s, PRVM_NAME);
1018 returns the same string as output
1020 does nothing, only used by qcc to build .pak archives
1023 void VM_precache_file (void)
1025 VM_SAFEPARMCOUNT(1,VM_precache_file);
1026 // precache_file is only used to copy files with qcc, it does nothing
1027 PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
1037 void VM_coredump (void)
1039 VM_SAFEPARMCOUNT(0,VM_coredump);
1041 Cbuf_AddText("prvm_edicts ");
1042 Cbuf_AddText(PRVM_NAME);
1053 void PRVM_StackTrace(void);
1054 void VM_stackdump (void)
1056 VM_SAFEPARMCOUNT(0, VM_stackdump);
1071 VM_SAFEPARMCOUNT(0, VM_crash);
1073 PRVM_ERROR("Crash called by %s",PRVM_NAME);
1083 void VM_traceon (void)
1085 VM_SAFEPARMCOUNT(0,VM_traceon);
1097 void VM_traceoff (void)
1099 VM_SAFEPARMCOUNT(0,VM_traceoff);
1101 prog->trace = false;
1111 void VM_eprint (void)
1113 VM_SAFEPARMCOUNT(1,VM_eprint);
1115 PRVM_ED_PrintNum (PRVM_G_EDICTNUM(OFS_PARM0));
1128 VM_SAFEPARMCOUNT(1,VM_rint);
1130 f = PRVM_G_FLOAT(OFS_PARM0);
1132 PRVM_G_FLOAT(OFS_RETURN) = floor(f + 0.5);
1134 PRVM_G_FLOAT(OFS_RETURN) = ceil(f - 0.5);
1144 void VM_floor (void)
1146 VM_SAFEPARMCOUNT(1,VM_floor);
1148 PRVM_G_FLOAT(OFS_RETURN) = floor(PRVM_G_FLOAT(OFS_PARM0));
1160 VM_SAFEPARMCOUNT(1,VM_ceil);
1162 PRVM_G_FLOAT(OFS_RETURN) = ceil(PRVM_G_FLOAT(OFS_PARM0));
1170 entity nextent(entity)
1173 void VM_nextent (void)
1178 VM_SAFEPARMCOUNT(1, VM_nextent);
1180 i = PRVM_G_EDICTNUM(OFS_PARM0);
1183 prog->xfunction->builtinsprofile++;
1185 if (i == prog->num_edicts)
1187 VM_RETURN_EDICT(prog->edicts);
1190 ent = PRVM_EDICT_NUM(i);
1191 if (!ent->priv.required->free)
1193 VM_RETURN_EDICT(ent);
1199 //=============================================================================
1206 changelevel(string map)
1209 void VM_changelevel (void)
1211 VM_SAFEPARMCOUNT(1, VM_changelevel);
1215 VM_Warning("VM_changelevel: game is not server (%s)\n", PRVM_NAME);
1219 // make sure we don't issue two changelevels
1220 if (svs.changelevel_issued)
1222 svs.changelevel_issued = true;
1224 Cbuf_AddText (va("changelevel %s\n",PRVM_G_STRING(OFS_PARM0)));
1236 VM_SAFEPARMCOUNT(1,VM_sin);
1237 PRVM_G_FLOAT(OFS_RETURN) = sin(PRVM_G_FLOAT(OFS_PARM0));
1248 VM_SAFEPARMCOUNT(1,VM_cos);
1249 PRVM_G_FLOAT(OFS_RETURN) = cos(PRVM_G_FLOAT(OFS_PARM0));
1261 VM_SAFEPARMCOUNT(1,VM_sqrt);
1262 PRVM_G_FLOAT(OFS_RETURN) = sqrt(PRVM_G_FLOAT(OFS_PARM0));
1274 VM_SAFEPARMCOUNT(1,VM_asin);
1275 PRVM_G_FLOAT(OFS_RETURN) = asin(PRVM_G_FLOAT(OFS_PARM0));
1286 VM_SAFEPARMCOUNT(1,VM_acos);
1287 PRVM_G_FLOAT(OFS_RETURN) = acos(PRVM_G_FLOAT(OFS_PARM0));
1298 VM_SAFEPARMCOUNT(1,VM_atan);
1299 PRVM_G_FLOAT(OFS_RETURN) = atan(PRVM_G_FLOAT(OFS_PARM0));
1305 float atan2(float,float)
1308 void VM_atan2 (void)
1310 VM_SAFEPARMCOUNT(2,VM_atan2);
1311 PRVM_G_FLOAT(OFS_RETURN) = atan2(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1322 VM_SAFEPARMCOUNT(1,VM_tan);
1323 PRVM_G_FLOAT(OFS_RETURN) = tan(PRVM_G_FLOAT(OFS_PARM0));
1330 Returns a vector of length < 1 and > 0
1335 void VM_randomvec (void)
1340 VM_SAFEPARMCOUNT(0, VM_randomvec);
1345 temp[0] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1346 temp[1] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1347 temp[2] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1349 while (DotProduct(temp, temp) >= 1);
1350 VectorCopy (temp, PRVM_G_VECTOR(OFS_RETURN));
1353 temp[0] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1354 temp[1] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1355 temp[2] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1356 // length returned always > 0
1357 length = (rand()&32766 + 1) * (1.0 / 32767.0) / VectorLength(temp);
1358 VectorScale(temp,length, temp);*/
1359 //VectorCopy(temp, PRVM_G_VECTOR(OFS_RETURN));
1362 //=============================================================================
1368 float registercvar (string name, string value[, float flags])
1371 void VM_registercvar (void)
1373 const char *name, *value;
1376 VM_SAFEPARMCOUNTRANGE(2, 3, VM_registercvar);
1378 name = PRVM_G_STRING(OFS_PARM0);
1379 value = PRVM_G_STRING(OFS_PARM1);
1380 flags = prog->argc >= 3 ? (int)PRVM_G_FLOAT(OFS_PARM2) : 0;
1381 PRVM_G_FLOAT(OFS_RETURN) = 0;
1383 if(flags > CVAR_MAXFLAGSVAL)
1386 // first check to see if it has already been defined
1387 if (Cvar_FindVar (name))
1390 // check for overlap with a command
1391 if (Cmd_Exists (name))
1393 VM_Warning("VM_registercvar: %s is a command\n", name);
1397 Cvar_Get(name, value, flags);
1399 PRVM_G_FLOAT(OFS_RETURN) = 1; // success
1407 returns the minimum of two supplied floats
1409 float min(float a, float b, ...[float])
1414 VM_SAFEPARMCOUNTRANGE(2, 8, VM_min);
1415 // LordHavoc: 3+ argument enhancement suggested by FrikaC
1416 if (prog->argc >= 3)
1419 float f = PRVM_G_FLOAT(OFS_PARM0);
1420 for (i = 1;i < prog->argc;i++)
1421 if (f > PRVM_G_FLOAT((OFS_PARM0+i*3)))
1422 f = PRVM_G_FLOAT((OFS_PARM0+i*3));
1423 PRVM_G_FLOAT(OFS_RETURN) = f;
1426 PRVM_G_FLOAT(OFS_RETURN) = min(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1433 returns the maximum of two supplied floats
1435 float max(float a, float b, ...[float])
1440 VM_SAFEPARMCOUNTRANGE(2, 8, VM_max);
1441 // LordHavoc: 3+ argument enhancement suggested by FrikaC
1442 if (prog->argc >= 3)
1445 float f = PRVM_G_FLOAT(OFS_PARM0);
1446 for (i = 1;i < prog->argc;i++)
1447 if (f < PRVM_G_FLOAT((OFS_PARM0+i*3)))
1448 f = PRVM_G_FLOAT((OFS_PARM0+i*3));
1449 PRVM_G_FLOAT(OFS_RETURN) = f;
1452 PRVM_G_FLOAT(OFS_RETURN) = max(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1459 returns number bounded by supplied range
1461 float bound(float min, float value, float max)
1464 void VM_bound (void)
1466 VM_SAFEPARMCOUNT(3,VM_bound);
1467 PRVM_G_FLOAT(OFS_RETURN) = bound(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1), PRVM_G_FLOAT(OFS_PARM2));
1474 returns a raised to power b
1476 float pow(float a, float b)
1481 VM_SAFEPARMCOUNT(2,VM_pow);
1482 PRVM_G_FLOAT(OFS_RETURN) = pow(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1485 void VM_Files_Init(void)
1488 for (i = 0;i < PRVM_MAX_OPENFILES;i++)
1489 prog->openfiles[i] = NULL;
1492 void VM_Files_CloseAll(void)
1495 for (i = 0;i < PRVM_MAX_OPENFILES;i++)
1497 if (prog->openfiles[i])
1498 FS_Close(prog->openfiles[i]);
1499 prog->openfiles[i] = NULL;
1503 static qfile_t *VM_GetFileHandle( int index )
1505 if (index < 0 || index >= PRVM_MAX_OPENFILES)
1507 Con_Printf("VM_GetFileHandle: invalid file handle %i used in %s\n", index, PRVM_NAME);
1510 if (prog->openfiles[index] == NULL)
1512 Con_Printf("VM_GetFileHandle: no such file handle %i (or file has been closed) in %s\n", index, PRVM_NAME);
1515 return prog->openfiles[index];
1522 float fopen(string filename, float mode)
1525 // float(string filename, float mode) fopen = #110;
1526 // opens a file inside quake/gamedir/data/ (mode is FILE_READ, FILE_APPEND, or FILE_WRITE),
1527 // returns fhandle >= 0 if successful, or fhandle < 0 if unable to open file for any reason
1531 const char *modestring, *filename;
1533 VM_SAFEPARMCOUNT(2,VM_fopen);
1535 for (filenum = 0;filenum < PRVM_MAX_OPENFILES;filenum++)
1536 if (prog->openfiles[filenum] == NULL)
1538 if (filenum >= PRVM_MAX_OPENFILES)
1540 PRVM_G_FLOAT(OFS_RETURN) = -2;
1541 VM_Warning("VM_fopen: %s ran out of file handles (%i)\n", PRVM_NAME, PRVM_MAX_OPENFILES);
1544 mode = (int)PRVM_G_FLOAT(OFS_PARM1);
1547 case 0: // FILE_READ
1550 case 1: // FILE_APPEND
1553 case 2: // FILE_WRITE
1557 PRVM_G_FLOAT(OFS_RETURN) = -3;
1558 VM_Warning("VM_fopen: %s: no such mode %i (valid: 0 = read, 1 = append, 2 = write)\n", PRVM_NAME, mode);
1561 filename = PRVM_G_STRING(OFS_PARM0);
1563 prog->openfiles[filenum] = FS_Open(va("data/%s", filename), modestring, false, false);
1564 if (prog->openfiles[filenum] == NULL && mode == 0)
1565 prog->openfiles[filenum] = FS_Open(va("%s", filename), modestring, false, false);
1567 if (prog->openfiles[filenum] == NULL)
1569 PRVM_G_FLOAT(OFS_RETURN) = -1;
1570 if (developer.integer >= 100)
1571 VM_Warning("VM_fopen: %s: %s mode %s failed\n", PRVM_NAME, filename, modestring);
1575 PRVM_G_FLOAT(OFS_RETURN) = filenum;
1576 if (developer.integer >= 100)
1577 Con_Printf("VM_fopen: %s: %s mode %s opened as #%i\n", PRVM_NAME, filename, modestring, filenum);
1585 fclose(float fhandle)
1588 //void(float fhandle) fclose = #111; // closes a file
1589 void VM_fclose(void)
1593 VM_SAFEPARMCOUNT(1,VM_fclose);
1595 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1596 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
1598 VM_Warning("VM_fclose: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1601 if (prog->openfiles[filenum] == NULL)
1603 VM_Warning("VM_fclose: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1606 FS_Close(prog->openfiles[filenum]);
1607 prog->openfiles[filenum] = NULL;
1608 if (developer.integer >= 100)
1609 Con_Printf("VM_fclose: %s: #%i closed\n", PRVM_NAME, filenum);
1616 string fgets(float fhandle)
1619 //string(float fhandle) fgets = #112; // reads a line of text from the file and returns as a tempstring
1623 char string[VM_STRINGTEMP_LENGTH];
1626 VM_SAFEPARMCOUNT(1,VM_fgets);
1628 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1629 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
1631 VM_Warning("VM_fgets: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1634 if (prog->openfiles[filenum] == NULL)
1636 VM_Warning("VM_fgets: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1642 c = FS_Getc(prog->openfiles[filenum]);
1643 if (c == '\r' || c == '\n' || c < 0)
1645 if (end < VM_STRINGTEMP_LENGTH - 1)
1649 // remove \n following \r
1652 c = FS_Getc(prog->openfiles[filenum]);
1654 FS_UnGetc(prog->openfiles[filenum], (unsigned char)c);
1656 if (developer.integer >= 100)
1657 Con_Printf("fgets: %s: %s\n", PRVM_NAME, string);
1659 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string);
1661 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
1668 fputs(float fhandle, string s)
1671 //void(float fhandle, string s) fputs = #113; // writes a line of text to the end of the file
1675 char string[VM_STRINGTEMP_LENGTH];
1678 VM_SAFEPARMCOUNT(2,VM_fputs);
1680 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1681 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
1683 VM_Warning("VM_fputs: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1686 if (prog->openfiles[filenum] == NULL)
1688 VM_Warning("VM_fputs: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1691 VM_VarString(1, string, sizeof(string));
1692 if ((stringlength = (int)strlen(string)))
1693 FS_Write(prog->openfiles[filenum], string, stringlength);
1694 if (developer.integer >= 100)
1695 Con_Printf("fputs: %s: %s\n", PRVM_NAME, string);
1702 writetofile(float fhandle, entity ent)
1705 void VM_writetofile(void)
1710 VM_SAFEPARMCOUNT(2, VM_writetofile);
1712 file = VM_GetFileHandle( (int)PRVM_G_FLOAT(OFS_PARM0) );
1715 VM_Warning("VM_writetofile: invalid or closed file handle\n");
1719 ent = PRVM_G_EDICT(OFS_PARM1);
1720 if(ent->priv.required->free)
1722 VM_Warning("VM_writetofile: %s: entity %i is free !\n", PRVM_NAME, PRVM_NUM_FOR_EDICT(ent));
1726 PRVM_ED_Write (file, ent);
1733 float strlen(string s)
1736 //float(string s) strlen = #114; // returns how many characters are in a string
1737 void VM_strlen(void)
1739 VM_SAFEPARMCOUNT(1,VM_strlen);
1741 PRVM_G_FLOAT(OFS_RETURN) = strlen(PRVM_G_STRING(OFS_PARM0));
1744 // DRESK - Decolorized String
1749 string strdecolorize(string s)
1752 // string (string s) strdecolorize = #472; // returns the passed in string with color codes stripped
1753 void VM_strdecolorize(void)
1755 char szNewString[VM_STRINGTEMP_LENGTH];
1756 const char *szString;
1759 VM_SAFEPARMCOUNT(1,VM_strdecolorize);
1760 szString = PRVM_G_STRING(OFS_PARM0);
1762 COM_StringDecolorize(szString, 0, szNewString, sizeof(szNewString), TRUE);
1764 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(szNewString);
1767 // DRESK - String Length (not counting color codes)
1772 float strlennocol(string s)
1775 // float(string s) strlennocol = #471; // returns how many characters are in a string not including color codes
1776 // For example, ^2Dresk returns a length of 5
1777 void VM_strlennocol(void)
1779 const char *szString;
1782 VM_SAFEPARMCOUNT(1,VM_strlennocol);
1784 szString = PRVM_G_STRING(OFS_PARM0);
1786 nCnt = COM_StringLengthNoColors(szString, 0, NULL);
1788 PRVM_G_FLOAT(OFS_RETURN) = nCnt;
1795 string strcat(string,string,...[string])
1798 //string(string s1, string s2) strcat = #115;
1799 // concatenates two strings (for example "abc", "def" would return "abcdef")
1800 // and returns as a tempstring
1801 void VM_strcat(void)
1803 char s[VM_STRINGTEMP_LENGTH];
1804 VM_SAFEPARMCOUNTRANGE(1, 8, VM_strcat);
1806 VM_VarString(0, s, sizeof(s));
1807 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(s);
1814 string substring(string s, float start, float length)
1817 // string(string s, float start, float length) substring = #116;
1818 // returns a section of a string as a tempstring
1819 void VM_substring(void)
1821 int i, start, length;
1823 char string[VM_STRINGTEMP_LENGTH];
1825 VM_SAFEPARMCOUNT(3,VM_substring);
1827 s = PRVM_G_STRING(OFS_PARM0);
1828 start = (int)PRVM_G_FLOAT(OFS_PARM1);
1829 length = (int)PRVM_G_FLOAT(OFS_PARM2);
1830 for (i = 0;i < start && *s;i++, s++);
1831 for (i = 0;i < (int)sizeof(string) - 1 && *s && i < length;i++, s++)
1834 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string);
1841 vector stov(string s)
1844 //vector(string s) stov = #117; // returns vector value from a string
1847 char string[VM_STRINGTEMP_LENGTH];
1849 VM_SAFEPARMCOUNT(1,VM_stov);
1851 VM_VarString(0, string, sizeof(string));
1852 Math_atov(string, PRVM_G_VECTOR(OFS_RETURN));
1859 string strzone(string s)
1862 //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)
1863 void VM_strzone(void)
1866 char string[VM_STRINGTEMP_LENGTH];
1869 VM_SAFEPARMCOUNT(1,VM_strzone);
1871 VM_VarString(0, string, sizeof(string));
1872 alloclen = strlen(string) + 1;
1873 PRVM_G_INT(OFS_RETURN) = PRVM_AllocString(alloclen, &out);
1874 memcpy(out, string, alloclen);
1884 //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!!!)
1885 void VM_strunzone(void)
1887 VM_SAFEPARMCOUNT(1,VM_strunzone);
1888 PRVM_FreeString(PRVM_G_INT(OFS_PARM0));
1893 VM_command (used by client and menu)
1895 clientcommand(float client, string s) (for client and menu)
1898 //void(entity e, string s) clientcommand = #440; // executes a command string as if it came from the specified client
1899 //this function originally written by KrimZon, made shorter by LordHavoc
1900 void VM_clcommand (void)
1902 client_t *temp_client;
1905 VM_SAFEPARMCOUNT(2,VM_clcommand);
1907 i = (int)PRVM_G_FLOAT(OFS_PARM0);
1908 if (!sv.active || i < 0 || i >= svs.maxclients || !svs.clients[i].active)
1910 VM_Warning("VM_clientcommand: %s: invalid client/server is not active !\n", PRVM_NAME);
1914 temp_client = host_client;
1915 host_client = svs.clients + i;
1916 Cmd_ExecuteString (PRVM_G_STRING(OFS_PARM1), src_client);
1917 host_client = temp_client;
1925 float tokenize(string s)
1928 //float(string s) tokenize = #441; // takes apart a string into individal words (access them with argv), returns how many
1929 //this function originally written by KrimZon, made shorter by LordHavoc
1930 //20040203: rewritten by LordHavoc (no longer uses allocations)
1933 void VM_tokenize (void)
1937 VM_SAFEPARMCOUNT(1,VM_tokenize);
1939 p = PRVM_G_STRING(OFS_PARM0);
1942 while(COM_ParseToken(&p, false))
1944 if (num_tokens >= (int)(sizeof(tokens)/sizeof(tokens[0])))
1946 tokens[num_tokens++] = PRVM_SetTempString(com_token);
1949 PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
1954 VM_tokenizebyseparator
1956 float tokenizebyseparator(string s, string separator1, ...)
1959 //float(string s, string separator1, ...) tokenizebyseparator = #479; // takes apart a string into individal words (access them with argv), returns how many
1960 //this function returns the token preceding each instance of a separator (of
1961 //which there can be multiple), and the text following the last separator
1962 //useful for parsing certain kinds of data like IP addresses
1964 //numnumbers = tokenizebyseparator("10.1.2.3", ".");
1965 //returns 4 and the tokens "10" "1" "2" "3".
1966 void VM_tokenizebyseparator (void)
1970 int separatorlen[7];
1971 const char *separators[7];
1973 char tokentext[MAX_INPUTLINE];
1975 VM_SAFEPARMCOUNTRANGE(2, 8,VM_tokenizebyseparator);
1977 p = PRVM_G_STRING(OFS_PARM0);
1980 for (j = 1;j < prog->argc;j++)
1982 // skip any blank separator strings
1983 if (!PRVM_G_STRING(OFS_PARM0 + j)[0])
1985 separators[numseparators] = PRVM_G_STRING(OFS_PARM0 + j);
1986 separatorlen[numseparators] = strlen(separators[numseparators]);
1991 for (num_tokens = 0;num_tokens < (int)(sizeof(tokens)/sizeof(tokens[0]));num_tokens++)
1995 for (k = 0;k < numseparators;k++)
1997 if (!strncmp(p, separators[k], separatorlen[k]))
1999 p += separatorlen[k];
2003 if (k < numseparators)
2005 if (j < (int)sizeof(tokentext[MAX_INPUTLINE]-1))
2006 tokentext[j++] = *p;
2010 tokens[num_tokens] = PRVM_SetTempString(tokentext);
2015 PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
2018 //string(float n) argv = #442; // returns a word from the tokenized string (returns nothing for an invalid index)
2019 //this function originally written by KrimZon, made shorter by LordHavoc
2024 VM_SAFEPARMCOUNT(1,VM_argv);
2026 token_num = (int)PRVM_G_FLOAT(OFS_PARM0);
2028 if (token_num >= 0 && token_num < num_tokens)
2029 PRVM_G_INT(OFS_RETURN) = tokens[token_num];
2031 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
2041 void VM_isserver(void)
2043 VM_SAFEPARMCOUNT(0,VM_serverstate);
2045 PRVM_G_FLOAT(OFS_RETURN) = sv.active && (svs.maxclients > 1 || cls.state == ca_dedicated);
2055 void VM_clientcount(void)
2057 VM_SAFEPARMCOUNT(0,VM_clientcount);
2059 PRVM_G_FLOAT(OFS_RETURN) = svs.maxclients;
2069 void VM_clientstate(void)
2071 VM_SAFEPARMCOUNT(0,VM_clientstate);
2073 PRVM_G_FLOAT(OFS_RETURN) = cls.state;
2080 float getostype(void)
2082 */ // not used at the moment -> not included in the common list
2083 void VM_getostype(void)
2085 VM_SAFEPARMCOUNT(0,VM_getostype);
2090 OS_MAC - not supported
2094 PRVM_G_FLOAT(OFS_RETURN) = 0;
2095 #elif defined(MACOSX)
2096 PRVM_G_FLOAT(OFS_RETURN) = 2;
2098 PRVM_G_FLOAT(OFS_RETURN) = 1;
2106 vector getmousepos()
2109 void VM_getmousepos(void)
2112 VM_SAFEPARMCOUNT(0,VM_getmousepos);
2114 PRVM_G_VECTOR(OFS_RETURN)[0] = in_mouse_x * vid_conwidth.integer / vid.width;
2115 PRVM_G_VECTOR(OFS_RETURN)[1] = in_mouse_y * vid_conheight.integer / vid.height;
2116 PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
2126 void VM_gettime(void)
2128 VM_SAFEPARMCOUNT(0,VM_gettime);
2130 PRVM_G_FLOAT(OFS_RETURN) = (float) realtime;
2137 loadfromdata(string data)
2140 void VM_loadfromdata(void)
2142 VM_SAFEPARMCOUNT(1,VM_loadentsfromfile);
2144 PRVM_ED_LoadFromFile(PRVM_G_STRING(OFS_PARM0));
2148 ========================
2151 parseentitydata(entity ent, string data)
2152 ========================
2154 void VM_parseentitydata(void)
2159 VM_SAFEPARMCOUNT(2, VM_parseentitydata);
2161 // get edict and test it
2162 ent = PRVM_G_EDICT(OFS_PARM0);
2163 if (ent->priv.required->free)
2164 PRVM_ERROR ("VM_parseentitydata: %s: Can only set already spawned entities (entity %i is free)!", PRVM_NAME, PRVM_NUM_FOR_EDICT(ent));
2166 data = PRVM_G_STRING(OFS_PARM1);
2168 // parse the opening brace
2169 if (!COM_ParseTokenConsole(&data) || com_token[0] != '{' )
2170 PRVM_ERROR ("VM_parseentitydata: %s: Couldn't parse entity data:\n%s", PRVM_NAME, data );
2172 PRVM_ED_ParseEdict (data, ent);
2179 loadfromfile(string file)
2182 void VM_loadfromfile(void)
2184 const char *filename;
2187 VM_SAFEPARMCOUNT(1,VM_loadfromfile);
2189 filename = PRVM_G_STRING(OFS_PARM0);
2190 if (FS_CheckNastyPath(filename, false))
2192 PRVM_G_FLOAT(OFS_RETURN) = -4;
2193 VM_Warning("VM_loadfromfile: %s dangerous or non-portable filename \"%s\" not allowed. (contains : or \\ or begins with .. or /)\n", PRVM_NAME, filename);
2197 // not conform with VM_fopen
2198 data = (char *)FS_LoadFile(filename, tempmempool, false, NULL);
2200 PRVM_G_FLOAT(OFS_RETURN) = -1;
2202 PRVM_ED_LoadFromFile(data);
2213 float mod(float val, float m)
2216 void VM_modulo(void)
2219 VM_SAFEPARMCOUNT(2,VM_module);
2221 val = (int) PRVM_G_FLOAT(OFS_PARM0);
2222 m = (int) PRVM_G_FLOAT(OFS_PARM1);
2224 PRVM_G_FLOAT(OFS_RETURN) = (float) (val % m);
2227 void VM_Search_Init(void)
2230 for (i = 0;i < PRVM_MAX_OPENSEARCHES;i++)
2231 prog->opensearches[i] = NULL;
2234 void VM_Search_Reset(void)
2237 // reset the fssearch list
2238 for(i = 0; i < PRVM_MAX_OPENSEARCHES; i++)
2240 if(prog->opensearches[i])
2241 FS_FreeSearch(prog->opensearches[i]);
2242 prog->opensearches[i] = NULL;
2250 float search_begin(string pattern, float caseinsensitive, float quiet)
2253 void VM_search_begin(void)
2256 const char *pattern;
2257 int caseinsens, quiet;
2259 VM_SAFEPARMCOUNT(3, VM_search_begin);
2261 pattern = PRVM_G_STRING(OFS_PARM0);
2263 VM_CheckEmptyString(pattern);
2265 caseinsens = (int)PRVM_G_FLOAT(OFS_PARM1);
2266 quiet = (int)PRVM_G_FLOAT(OFS_PARM2);
2268 for(handle = 0; handle < PRVM_MAX_OPENSEARCHES; handle++)
2269 if(!prog->opensearches[handle])
2272 if(handle >= PRVM_MAX_OPENSEARCHES)
2274 PRVM_G_FLOAT(OFS_RETURN) = -2;
2275 VM_Warning("VM_search_begin: %s ran out of search handles (%i)\n", PRVM_NAME, PRVM_MAX_OPENSEARCHES);
2279 if(!(prog->opensearches[handle] = FS_Search(pattern,caseinsens, quiet)))
2280 PRVM_G_FLOAT(OFS_RETURN) = -1;
2282 PRVM_G_FLOAT(OFS_RETURN) = handle;
2289 void search_end(float handle)
2292 void VM_search_end(void)
2295 VM_SAFEPARMCOUNT(1, VM_search_end);
2297 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
2299 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
2301 VM_Warning("VM_search_end: invalid handle %i used in %s\n", handle, PRVM_NAME);
2304 if(prog->opensearches[handle] == NULL)
2306 VM_Warning("VM_search_end: no such handle %i in %s\n", handle, PRVM_NAME);
2310 FS_FreeSearch(prog->opensearches[handle]);
2311 prog->opensearches[handle] = NULL;
2318 float search_getsize(float handle)
2321 void VM_search_getsize(void)
2324 VM_SAFEPARMCOUNT(1, VM_M_search_getsize);
2326 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
2328 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
2330 VM_Warning("VM_search_getsize: invalid handle %i used in %s\n", handle, PRVM_NAME);
2333 if(prog->opensearches[handle] == NULL)
2335 VM_Warning("VM_search_getsize: no such handle %i in %s\n", handle, PRVM_NAME);
2339 PRVM_G_FLOAT(OFS_RETURN) = prog->opensearches[handle]->numfilenames;
2344 VM_search_getfilename
2346 string search_getfilename(float handle, float num)
2349 void VM_search_getfilename(void)
2351 int handle, filenum;
2352 VM_SAFEPARMCOUNT(2, VM_search_getfilename);
2354 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
2355 filenum = (int)PRVM_G_FLOAT(OFS_PARM1);
2357 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
2359 VM_Warning("VM_search_getfilename: invalid handle %i used in %s\n", handle, PRVM_NAME);
2362 if(prog->opensearches[handle] == NULL)
2364 VM_Warning("VM_search_getfilename: no such handle %i in %s\n", handle, PRVM_NAME);
2367 if(filenum < 0 || filenum >= prog->opensearches[handle]->numfilenames)
2369 VM_Warning("VM_search_getfilename: invalid filenum %i in %s\n", filenum, PRVM_NAME);
2373 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog->opensearches[handle]->filenames[filenum]);
2380 string chr(float ascii)
2386 VM_SAFEPARMCOUNT(1, VM_chr);
2388 tmp[0] = (unsigned char) PRVM_G_FLOAT(OFS_PARM0);
2391 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(tmp);
2394 //=============================================================================
2395 // Draw builtins (client & menu)
2401 float iscachedpic(string pic)
2404 void VM_iscachedpic(void)
2406 VM_SAFEPARMCOUNT(1,VM_iscachedpic);
2408 // drawq hasnt such a function, thus always return true
2409 PRVM_G_FLOAT(OFS_RETURN) = false;
2416 string precache_pic(string pic)
2419 void VM_precache_pic(void)
2423 VM_SAFEPARMCOUNT(1, VM_precache_pic);
2425 s = PRVM_G_STRING(OFS_PARM0);
2426 PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
2427 VM_CheckEmptyString (s);
2429 // AK Draw_CachePic is supposed to always return a valid pointer
2430 if( Draw_CachePic(s, false)->tex == r_texture_notexture )
2431 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
2441 void VM_freepic(void)
2445 VM_SAFEPARMCOUNT(1,VM_freepic);
2447 s = PRVM_G_STRING(OFS_PARM0);
2448 VM_CheckEmptyString (s);
2457 float drawcharacter(vector position, float character, vector scale, vector rgb, float alpha, float flag)
2460 void VM_drawcharacter(void)
2462 float *pos,*scale,*rgb;
2465 VM_SAFEPARMCOUNT(6,VM_drawcharacter);
2467 character = (char) PRVM_G_FLOAT(OFS_PARM1);
2470 PRVM_G_FLOAT(OFS_RETURN) = -1;
2471 VM_Warning("VM_drawcharacter: %s passed null character !\n",PRVM_NAME);
2475 pos = PRVM_G_VECTOR(OFS_PARM0);
2476 scale = PRVM_G_VECTOR(OFS_PARM2);
2477 rgb = PRVM_G_VECTOR(OFS_PARM3);
2478 flag = (int)PRVM_G_FLOAT(OFS_PARM5);
2480 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2482 PRVM_G_FLOAT(OFS_RETURN) = -2;
2483 VM_Warning("VM_drawcharacter: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2487 if(pos[2] || scale[2])
2488 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")));
2490 if(!scale[0] || !scale[1])
2492 PRVM_G_FLOAT(OFS_RETURN) = -3;
2493 VM_Warning("VM_drawcharacter: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
2497 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);
2498 PRVM_G_FLOAT(OFS_RETURN) = 1;
2505 float drawstring(vector position, string text, vector scale, vector rgb, float alpha, float flag)
2508 void VM_drawstring(void)
2510 float *pos,*scale,*rgb;
2513 VM_SAFEPARMCOUNT(6,VM_drawstring);
2515 string = PRVM_G_STRING(OFS_PARM1);
2516 pos = PRVM_G_VECTOR(OFS_PARM0);
2517 scale = PRVM_G_VECTOR(OFS_PARM2);
2518 rgb = PRVM_G_VECTOR(OFS_PARM3);
2519 flag = (int)PRVM_G_FLOAT(OFS_PARM5);
2521 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2523 PRVM_G_FLOAT(OFS_RETURN) = -2;
2524 VM_Warning("VM_drawstring: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2528 if(!scale[0] || !scale[1])
2530 PRVM_G_FLOAT(OFS_RETURN) = -3;
2531 VM_Warning("VM_drawstring: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
2535 if(pos[2] || scale[2])
2536 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")));
2538 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);
2539 PRVM_G_FLOAT(OFS_RETURN) = 1;
2545 float drawpic(vector position, string pic, vector size, vector rgb, float alpha, float flag)
2548 void VM_drawpic(void)
2550 const char *picname;
2551 float *size, *pos, *rgb;
2554 VM_SAFEPARMCOUNT(6,VM_drawpic);
2556 picname = PRVM_G_STRING(OFS_PARM1);
2557 VM_CheckEmptyString (picname);
2559 // is pic cached ? no function yet for that
2562 PRVM_G_FLOAT(OFS_RETURN) = -4;
2563 VM_Warning("VM_drawpic: %s: %s not cached !\n", PRVM_NAME, picname);
2567 pos = PRVM_G_VECTOR(OFS_PARM0);
2568 size = PRVM_G_VECTOR(OFS_PARM2);
2569 rgb = PRVM_G_VECTOR(OFS_PARM3);
2570 flag = (int) PRVM_G_FLOAT(OFS_PARM5);
2572 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2574 PRVM_G_FLOAT(OFS_RETURN) = -2;
2575 VM_Warning("VM_drawpic: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2579 if(pos[2] || size[2])
2580 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")));
2582 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);
2583 PRVM_G_FLOAT(OFS_RETURN) = 1;
2590 float drawfill(vector position, vector size, vector rgb, float alpha, float flag)
2593 void VM_drawfill(void)
2595 float *size, *pos, *rgb;
2598 VM_SAFEPARMCOUNT(5,VM_drawfill);
2601 pos = PRVM_G_VECTOR(OFS_PARM0);
2602 size = PRVM_G_VECTOR(OFS_PARM1);
2603 rgb = PRVM_G_VECTOR(OFS_PARM2);
2604 flag = (int) PRVM_G_FLOAT(OFS_PARM4);
2606 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2608 PRVM_G_FLOAT(OFS_RETURN) = -2;
2609 VM_Warning("VM_drawfill: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2613 if(pos[2] || size[2])
2614 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")));
2616 DrawQ_Fill(pos[0], pos[1], size[0], size[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM3), flag);
2617 PRVM_G_FLOAT(OFS_RETURN) = 1;
2624 drawsetcliparea(float x, float y, float width, float height)
2627 void VM_drawsetcliparea(void)
2630 VM_SAFEPARMCOUNT(4,VM_drawsetcliparea);
2632 x = bound(0, PRVM_G_FLOAT(OFS_PARM0), vid_conwidth.integer);
2633 y = bound(0, PRVM_G_FLOAT(OFS_PARM1), vid_conheight.integer);
2634 w = bound(0, PRVM_G_FLOAT(OFS_PARM2) + PRVM_G_FLOAT(OFS_PARM0) - x, (vid_conwidth.integer - x));
2635 h = bound(0, PRVM_G_FLOAT(OFS_PARM3) + PRVM_G_FLOAT(OFS_PARM1) - y, (vid_conheight.integer - y));
2637 DrawQ_SetClipArea(x, y, w, h);
2642 VM_drawresetcliparea
2647 void VM_drawresetcliparea(void)
2649 VM_SAFEPARMCOUNT(0,VM_drawresetcliparea);
2651 DrawQ_ResetClipArea();
2658 vector getimagesize(string pic)
2661 void VM_getimagesize(void)
2666 VM_SAFEPARMCOUNT(1,VM_getimagesize);
2668 p = PRVM_G_STRING(OFS_PARM0);
2669 VM_CheckEmptyString (p);
2671 pic = Draw_CachePic (p, false);
2673 PRVM_G_VECTOR(OFS_RETURN)[0] = pic->width;
2674 PRVM_G_VECTOR(OFS_RETURN)[1] = pic->height;
2675 PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
2682 string keynumtostring(float keynum)
2685 void VM_keynumtostring (void)
2687 VM_SAFEPARMCOUNT(1, VM_keynumtostring);
2689 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(Key_KeynumToString((int)PRVM_G_FLOAT(OFS_PARM0)));
2696 float stringtokeynum(string key)
2699 void VM_stringtokeynum (void)
2701 VM_SAFEPARMCOUNT( 1, VM_keynumtostring );
2703 PRVM_G_INT(OFS_RETURN) = Key_StringToKeynum(PRVM_G_STRING(OFS_PARM0));
2706 // CL_Video interface functions
2709 ========================
2712 float cin_open(string file, string name)
2713 ========================
2715 void VM_cin_open( void )
2720 VM_SAFEPARMCOUNT( 2, VM_cin_open );
2722 file = PRVM_G_STRING( OFS_PARM0 );
2723 name = PRVM_G_STRING( OFS_PARM1 );
2725 VM_CheckEmptyString( file );
2726 VM_CheckEmptyString( name );
2728 if( CL_OpenVideo( file, name, MENUOWNER ) )
2729 PRVM_G_FLOAT( OFS_RETURN ) = 1;
2731 PRVM_G_FLOAT( OFS_RETURN ) = 0;
2735 ========================
2738 void cin_close(string name)
2739 ========================
2741 void VM_cin_close( void )
2745 VM_SAFEPARMCOUNT( 1, VM_cin_close );
2747 name = PRVM_G_STRING( OFS_PARM0 );
2748 VM_CheckEmptyString( name );
2750 CL_CloseVideo( CL_GetVideoByName( name ) );
2754 ========================
2756 void cin_setstate(string name, float type)
2757 ========================
2759 void VM_cin_setstate( void )
2762 clvideostate_t state;
2765 VM_SAFEPARMCOUNT( 2, VM_cin_netstate );
2767 name = PRVM_G_STRING( OFS_PARM0 );
2768 VM_CheckEmptyString( name );
2770 state = (clvideostate_t)((int)PRVM_G_FLOAT( OFS_PARM1 ));
2772 video = CL_GetVideoByName( name );
2773 if( video && state > CLVIDEO_UNUSED && state < CLVIDEO_STATECOUNT )
2774 CL_SetVideoState( video, state );
2778 ========================
2781 float cin_getstate(string name)
2782 ========================
2784 void VM_cin_getstate( void )
2789 VM_SAFEPARMCOUNT( 1, VM_cin_getstate );
2791 name = PRVM_G_STRING( OFS_PARM0 );
2792 VM_CheckEmptyString( name );
2794 video = CL_GetVideoByName( name );
2796 PRVM_G_FLOAT( OFS_RETURN ) = (int)video->state;
2798 PRVM_G_FLOAT( OFS_RETURN ) = 0;
2802 ========================
2805 void cin_restart(string name)
2806 ========================
2808 void VM_cin_restart( void )
2813 VM_SAFEPARMCOUNT( 1, VM_cin_restart );
2815 name = PRVM_G_STRING( OFS_PARM0 );
2816 VM_CheckEmptyString( name );
2818 video = CL_GetVideoByName( name );
2820 CL_RestartVideo( video );
2827 Writes new values for v_forward, v_up, and v_right based on angles
2828 void makevectors(vector angle)
2831 void VM_makevectors (void)
2833 prvm_eval_t *valforward, *valright, *valup;
2834 valforward = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_forward);
2835 valright = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_right);
2836 valup = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_up);
2837 if (!valforward || !valright || !valup)
2839 VM_Warning("makevectors: could not find v_forward, v_right, or v_up global variables\n");
2842 VM_SAFEPARMCOUNT(1, VM_makevectors);
2843 AngleVectors (PRVM_G_VECTOR(OFS_PARM0), valforward->vector, valright->vector, valup->vector);
2850 Writes new values for v_forward, v_up, and v_right based on the given forward vector
2851 vectorvectors(vector)
2854 void VM_vectorvectors (void)
2856 prvm_eval_t *valforward, *valright, *valup;
2857 valforward = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_forward);
2858 valright = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_right);
2859 valup = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_up);
2860 if (!valforward || !valright || !valup)
2862 VM_Warning("vectorvectors: could not find v_forward, v_right, or v_up global variables\n");
2865 VM_SAFEPARMCOUNT(1, VM_vectorvectors);
2866 VectorNormalize2(PRVM_G_VECTOR(OFS_PARM0), valforward->vector);
2867 VectorVectors(valforward->vector, valright->vector, valup->vector);
2871 ========================
2874 void drawline(float width, vector pos1, vector pos2, vector rgb, float alpha, float flags)
2875 ========================
2877 void VM_drawline (void)
2879 float *c1, *c2, *rgb;
2881 unsigned char flags;
2883 VM_SAFEPARMCOUNT(6, VM_drawline);
2884 width = PRVM_G_FLOAT(OFS_PARM0);
2885 c1 = PRVM_G_VECTOR(OFS_PARM1);
2886 c2 = PRVM_G_VECTOR(OFS_PARM2);
2887 rgb = PRVM_G_VECTOR(OFS_PARM3);
2888 alpha = PRVM_G_FLOAT(OFS_PARM4);
2889 flags = (int)PRVM_G_FLOAT(OFS_PARM5);
2890 DrawQ_Line(width, c1[0], c1[1], c2[0], c2[1], rgb[0], rgb[1], rgb[2], alpha, flags);
2897 // float(float number, float quantity) bitshift (EXT_BITSHIFT)
2898 void VM_bitshift (void)
2901 VM_SAFEPARMCOUNT(2, VM_bitshift);
2903 n1 = (int)fabs((int)PRVM_G_FLOAT(OFS_PARM0));
2904 n2 = (int)PRVM_G_FLOAT(OFS_PARM1);
2906 PRVM_G_FLOAT(OFS_RETURN) = n1;
2909 PRVM_G_FLOAT(OFS_RETURN) = (n1 >> -n2);
2911 PRVM_G_FLOAT(OFS_RETURN) = (n1 << n2);
2914 ////////////////////////////////////////
2915 // AltString functions
2916 ////////////////////////////////////////
2919 ========================
2922 float altstr_count(string)
2923 ========================
2925 void VM_altstr_count( void )
2927 const char *altstr, *pos;
2930 VM_SAFEPARMCOUNT( 1, VM_altstr_count );
2932 altstr = PRVM_G_STRING( OFS_PARM0 );
2933 //VM_CheckEmptyString( altstr );
2935 for( count = 0, pos = altstr ; *pos ; pos++ ) {
2936 if( *pos == '\\' ) {
2940 } else if( *pos == '\'' ) {
2945 PRVM_G_FLOAT( OFS_RETURN ) = (float) (count / 2);
2949 ========================
2952 string altstr_prepare(string)
2953 ========================
2955 void VM_altstr_prepare( void )
2958 const char *instr, *in;
2960 char outstr[VM_STRINGTEMP_LENGTH];
2962 VM_SAFEPARMCOUNT( 1, VM_altstr_prepare );
2964 instr = PRVM_G_STRING( OFS_PARM0 );
2966 for( out = outstr, in = instr, size = sizeof(outstr) - 1 ; size && *in ; size--, in++, out++ )
2975 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
2979 ========================
2982 string altstr_get(string, float)
2983 ========================
2985 void VM_altstr_get( void )
2987 const char *altstr, *pos;
2990 char outstr[VM_STRINGTEMP_LENGTH];
2992 VM_SAFEPARMCOUNT( 2, VM_altstr_get );
2994 altstr = PRVM_G_STRING( OFS_PARM0 );
2996 count = (int)PRVM_G_FLOAT( OFS_PARM1 );
2997 count = count * 2 + 1;
2999 for( pos = altstr ; *pos && count ; pos++ )
3000 if( *pos == '\\' ) {
3003 } else if( *pos == '\'' )
3007 PRVM_G_INT( OFS_RETURN ) = 0;
3011 for( out = outstr, size = sizeof(outstr) - 1 ; size && *pos ; size--, pos++, out++ )
3012 if( *pos == '\\' ) {
3017 } else if( *pos == '\'' )
3023 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
3027 ========================
3030 string altstr_set(string altstr, float num, string set)
3031 ========================
3033 void VM_altstr_set( void )
3036 const char *altstr, *str;
3039 char outstr[VM_STRINGTEMP_LENGTH];
3041 VM_SAFEPARMCOUNT( 3, VM_altstr_set );
3043 altstr = PRVM_G_STRING( OFS_PARM0 );
3045 num = (int)PRVM_G_FLOAT( OFS_PARM1 );
3047 str = PRVM_G_STRING( OFS_PARM2 );
3050 for( num = num * 2 + 1, in = altstr; *in && num; *out++ = *in++ )
3055 } else if( *in == '\'' ) {
3060 for( ; *str; *out++ = *str++ );
3061 // now jump over the old content
3063 if( *in == '\'' || (*in == '\\' && !*++in) )
3066 strlcpy(out, in, outstr + sizeof(outstr) - out);
3067 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
3071 ========================
3074 string altstr_ins(string altstr, float num, string set)
3075 ========================
3077 void VM_altstr_ins(void)
3085 char outstr[VM_STRINGTEMP_LENGTH];
3087 VM_SAFEPARMCOUNT(3, VM_altstr_ins);
3089 in = instr = PRVM_G_STRING( OFS_PARM0 );
3090 num = (int)PRVM_G_FLOAT( OFS_PARM1 );
3091 set = setstr = PRVM_G_STRING( OFS_PARM2 );
3094 for( num = num * 2 + 2 ; *in && num > 0 ; *out++ = *in++ )
3099 } else if( *in == '\'' ) {
3104 for( ; *set ; *out++ = *set++ );
3107 strlcpy(out, in, outstr + sizeof(outstr) - out);
3108 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
3112 ////////////////////////////////////////
3113 // BufString functions
3114 ////////////////////////////////////////
3115 //[515]: string buffers support
3116 #define MAX_QCSTR_BUFFERS 128
3117 #define MAX_QCSTR_STRINGS 1024
3122 char *strings[MAX_QCSTR_STRINGS];
3125 // FIXME: move stringbuffers to prog_t to allow multiple progs!
3126 static qcstrbuffer_t *qcstringbuffers[MAX_QCSTR_BUFFERS];
3127 static int num_qcstringbuffers;
3128 static int buf_sortpower;
3130 #define BUFSTR_BUFFER(a) (a>=MAX_QCSTR_BUFFERS) ? NULL : (qcstringbuffers[a])
3131 #define BUFSTR_ISFREE(a) (a<MAX_QCSTR_BUFFERS&&qcstringbuffers[a]&&qcstringbuffers[a]->num_strings<=0) ? 1 : 0
3133 static int BufStr_FindFreeBuffer (void)
3136 if(num_qcstringbuffers == MAX_QCSTR_BUFFERS)
3138 for(i=0;i<MAX_QCSTR_BUFFERS;i++)
3139 if(!qcstringbuffers[i])
3141 qcstringbuffers[i] = (qcstrbuffer_t *)Z_Malloc(sizeof(qcstrbuffer_t));
3142 memset(qcstringbuffers[i], 0, sizeof(qcstrbuffer_t));
3148 static void BufStr_ClearBuffer (int index)
3150 qcstrbuffer_t *b = qcstringbuffers[index];
3155 if(b->num_strings > 0)
3157 for(i=0;i<b->num_strings;i++)
3159 Z_Free(b->strings[i]);
3160 num_qcstringbuffers--;
3162 Z_Free(qcstringbuffers[index]);
3163 qcstringbuffers[index] = NULL;
3167 static int BufStr_FindFreeString (qcstrbuffer_t *b)
3170 for(i=0;i<b->num_strings;i++)
3171 if(!b->strings[i] || !b->strings[i][0])
3173 if(i == MAX_QCSTR_STRINGS) return -1;
3177 static int BufStr_SortStringsUP (const void *in1, const void *in2)
3180 a = *((const char **) in1);
3181 b = *((const char **) in2);
3183 if(!b[0]) return -1;
3184 return strncmp(a, b, buf_sortpower);
3187 static int BufStr_SortStringsDOWN (const void *in1, const void *in2)
3190 a = *((const char **) in1);
3191 b = *((const char **) in2);
3193 if(!b[0]) return -1;
3194 return strncmp(b, a, buf_sortpower);
3198 ========================
3200 creates new buffer, and returns it's index, returns -1 if failed
3201 float buf_create(void) = #460;
3202 ========================
3204 void VM_buf_create (void)
3207 VM_SAFEPARMCOUNT(0, VM_buf_create);
3208 i = BufStr_FindFreeBuffer();
3210 num_qcstringbuffers++;
3212 //Con_Printf("VM_buf_create: buffers overflow in %s\n", PRVM_NAME);
3213 PRVM_G_FLOAT(OFS_RETURN) = i;
3217 ========================
3219 deletes buffer and all strings in it
3220 void buf_del(float bufhandle) = #461;
3221 ========================
3223 void VM_buf_del (void)
3225 VM_SAFEPARMCOUNT(1, VM_buf_del);
3226 if(BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0)))
3227 BufStr_ClearBuffer((int)PRVM_G_FLOAT(OFS_PARM0));
3230 VM_Warning("VM_buf_del: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3236 ========================
3238 how many strings are stored in buffer
3239 float buf_getsize(float bufhandle) = #462;
3240 ========================
3242 void VM_buf_getsize (void)
3245 VM_SAFEPARMCOUNT(1, VM_buf_getsize);
3247 b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3250 PRVM_G_FLOAT(OFS_RETURN) = -1;
3251 VM_Warning("VM_buf_getsize: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3255 PRVM_G_FLOAT(OFS_RETURN) = b->num_strings;
3259 ========================
3261 copy all content from one buffer to another, make sure it exists
3262 void buf_copy(float bufhandle_from, float bufhandle_to) = #463;
3263 ========================
3265 void VM_buf_copy (void)
3267 qcstrbuffer_t *b1, *b2;
3269 VM_SAFEPARMCOUNT(2, VM_buf_copy);
3271 b1 = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3274 VM_Warning("VM_buf_copy: invalid source buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3277 i = (int)PRVM_G_FLOAT(OFS_PARM1);
3278 if(i == (int)PRVM_G_FLOAT(OFS_PARM0))
3280 VM_Warning("VM_buf_copy: source == destination (%i) in %s\n", i, PRVM_NAME);
3283 b2 = BUFSTR_BUFFER(i);
3286 VM_Warning("VM_buf_copy: invalid destination buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM1), PRVM_NAME);
3290 BufStr_ClearBuffer(i);
3291 qcstringbuffers[i] = (qcstrbuffer_t *)Z_Malloc(sizeof(qcstrbuffer_t));
3292 memset(qcstringbuffers[i], 0, sizeof(qcstrbuffer_t));
3293 b2->num_strings = b1->num_strings;
3295 for(i=0;i<b1->num_strings;i++)
3296 if(b1->strings[i] && b1->strings[i][0])
3299 stringlen = strlen(b1->strings[i]) + 1;
3300 b2->strings[i] = (char *)Z_Malloc(stringlen);
3303 VM_Warning("VM_buf_copy: not enough memory for buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM1), PRVM_NAME);
3306 memcpy(b2->strings[i], b1->strings[i], stringlen);
3311 ========================
3313 sort buffer by beginnings of strings (sortpower defaults it's lenght)
3314 "backward == TRUE" means that sorting goes upside-down
3315 void buf_sort(float bufhandle, float sortpower, float backward) = #464;
3316 ========================
3318 void VM_buf_sort (void)
3322 VM_SAFEPARMCOUNT(3, VM_buf_sort);
3324 b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3327 VM_Warning("VM_buf_sort: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3330 if(b->num_strings <= 0)
3332 VM_Warning("VM_buf_sort: tried to sort empty buffer %i in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3335 buf_sortpower = (int)PRVM_G_FLOAT(OFS_PARM1);
3336 if(buf_sortpower <= 0)
3337 buf_sortpower = 99999999;
3339 if(!PRVM_G_FLOAT(OFS_PARM2))
3340 qsort(b->strings, b->num_strings, sizeof(char*), BufStr_SortStringsUP);
3342 qsort(b->strings, b->num_strings, sizeof(char*), BufStr_SortStringsDOWN);
3344 for(i=b->num_strings-1;i>=0;i--) //[515]: delete empty lines
3347 if(b->strings[i][0])
3351 Z_Free(b->strings[i]);
3353 b->strings[i] = NULL;
3361 ========================
3363 concantenates all buffer string into one with "glue" separator and returns it as tempstring
3364 string buf_implode(float bufhandle, string glue) = #465;
3365 ========================
3367 void VM_buf_implode (void)
3370 char k[VM_STRINGTEMP_LENGTH];
3374 VM_SAFEPARMCOUNT(2, VM_buf_implode);
3376 b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3377 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
3380 VM_Warning("VM_buf_implode: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3385 sep = PRVM_G_STRING(OFS_PARM1);
3387 for(l=i=0;i<b->num_strings;i++)
3390 l += (i > 0 ? strlen(sep) : 0) + strlen(b->strings[i]);
3391 if (l >= sizeof(k) - 1)
3393 strlcat(k, sep, sizeof(k));
3394 strlcat(k, b->strings[i], sizeof(k));
3396 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(k);
3400 ========================
3402 get a string from buffer, returns tempstring, dont str_unzone it!
3403 string bufstr_get(float bufhandle, float string_index) = #465;
3404 ========================
3406 void VM_bufstr_get (void)
3410 VM_SAFEPARMCOUNT(2, VM_bufstr_get);
3412 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
3413 b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3416 VM_Warning("VM_bufstr_get: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3419 strindex = (int)PRVM_G_FLOAT(OFS_PARM1);
3420 if(strindex < 0 || strindex > MAX_QCSTR_STRINGS)
3422 VM_Warning("VM_bufstr_get: invalid string index %i used in %s\n", strindex, PRVM_NAME);
3425 if(b->num_strings <= strindex)
3427 if(b->strings[strindex])
3428 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(b->strings[strindex]);
3432 ========================
3434 copies a string into selected slot of buffer
3435 void bufstr_set(float bufhandle, float string_index, string str) = #466;
3436 ========================
3438 void VM_bufstr_set (void)
3440 int bufindex, strindex;
3445 VM_SAFEPARMCOUNT(3, VM_bufstr_set);
3447 bufindex = (int)PRVM_G_FLOAT(OFS_PARM0);
3448 b = BUFSTR_BUFFER(bufindex);
3451 VM_Warning("VM_bufstr_set: invalid buffer %i used in %s\n", bufindex, PRVM_NAME);
3454 strindex = (int)PRVM_G_FLOAT(OFS_PARM1);
3455 if(strindex < 0 || strindex > MAX_QCSTR_STRINGS)
3457 VM_Warning("VM_bufstr_set: invalid string index %i used in %s\n", strindex, PRVM_NAME);
3460 news = PRVM_G_STRING(OFS_PARM2);
3461 if(b->strings[strindex])
3462 Z_Free(b->strings[strindex]);
3463 alloclen = strlen(news) + 1;
3464 b->strings[strindex] = (char *)Z_Malloc(alloclen);
3465 memcpy(b->strings[strindex], news, alloclen);
3469 ========================
3471 adds string to buffer in nearest free slot and returns it
3472 "order == TRUE" means that string will be added after last "full" slot
3473 float bufstr_add(float bufhandle, string str, float order) = #467;
3474 ========================
3476 void VM_bufstr_add (void)
3478 int bufindex, order, strindex;
3483 VM_SAFEPARMCOUNT(3, VM_bufstr_add);
3485 bufindex = (int)PRVM_G_FLOAT(OFS_PARM0);
3486 b = BUFSTR_BUFFER(bufindex);
3487 PRVM_G_FLOAT(OFS_RETURN) = -1;
3490 VM_Warning("VM_bufstr_add: invalid buffer %i used in %s\n", bufindex, PRVM_NAME);
3493 string = PRVM_G_STRING(OFS_PARM1);
3494 order = (int)PRVM_G_FLOAT(OFS_PARM2);
3496 strindex = b->num_strings;
3499 strindex = BufStr_FindFreeString(b);
3502 VM_Warning("VM_bufstr_add: buffer %i has no free string slots in %s\n", bufindex, PRVM_NAME);
3507 while(b->num_strings <= strindex)
3509 if(b->num_strings == MAX_QCSTR_STRINGS)
3511 VM_Warning("VM_bufstr_add: buffer %i has no free string slots in %s\n", bufindex, PRVM_NAME);
3514 b->strings[b->num_strings] = NULL;
3517 if(b->strings[strindex])
3518 Z_Free(b->strings[strindex]);
3519 alloclen = strlen(string) + 1;
3520 b->strings[strindex] = (char *)Z_Malloc(alloclen);
3521 memcpy(b->strings[strindex], string, alloclen);
3522 PRVM_G_FLOAT(OFS_RETURN) = strindex;
3526 ========================
3528 delete string from buffer
3529 void bufstr_free(float bufhandle, float string_index) = #468;
3530 ========================
3532 void VM_bufstr_free (void)
3536 VM_SAFEPARMCOUNT(2, VM_bufstr_free);
3538 b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3541 VM_Warning("VM_bufstr_free: invalid 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 < 0 || i > MAX_QCSTR_STRINGS)
3547 VM_Warning("VM_bufstr_free: invalid string index %i used in %s\n", i, PRVM_NAME);
3551 Z_Free(b->strings[i]);
3552 b->strings[i] = NULL;
3553 if(i+1 == b->num_strings)
3563 This was a major timewaster in progs, so it was converted to C
3566 void VM_changeyaw (void)
3569 float ideal, current, move, speed;
3571 // this is called (VERY HACKISHLY) by SV_MoveToGoal, so it can not use any
3572 // parameters because they are the parameters to SV_MoveToGoal, not this
3573 //VM_SAFEPARMCOUNT(0, VM_changeyaw);
3575 ent = PRVM_PROG_TO_EDICT(PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict);
3576 if (ent == prog->edicts)
3578 VM_Warning("changeyaw: can not modify world entity\n");
3581 if (ent->priv.server->free)
3583 VM_Warning("changeyaw: can not modify free entity\n");
3586 if (prog->fieldoffsets.angles < 0 || prog->fieldoffsets.ideal_yaw < 0 || prog->fieldoffsets.yaw_speed < 0)
3588 VM_Warning("changeyaw: angles, ideal_yaw, or yaw_speed field(s) not found\n");
3591 current = ANGLEMOD(PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.angles)->vector[1]);
3592 ideal = PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.ideal_yaw)->_float;
3593 speed = PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.yaw_speed)->_float;
3595 if (current == ideal)
3597 move = ideal - current;
3598 if (ideal > current)
3619 PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.angles)->vector[1] = ANGLEMOD (current + move);
3627 void VM_changepitch (void)
3630 float ideal, current, move, speed;
3632 VM_SAFEPARMCOUNT(1, VM_changepitch);
3634 ent = PRVM_G_EDICT(OFS_PARM0);
3635 if (ent == prog->edicts)
3637 VM_Warning("changepitch: can not modify world entity\n");
3640 if (ent->priv.server->free)
3642 VM_Warning("changepitch: can not modify free entity\n");
3645 if (prog->fieldoffsets.angles < 0 || prog->fieldoffsets.idealpitch < 0 || prog->fieldoffsets.pitch_speed < 0)
3647 VM_Warning("changepitch: angles, idealpitch, or pitch_speed field(s) not found\n");
3650 current = ANGLEMOD(PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.angles)->vector[0]);
3651 ideal = PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.idealpitch)->_float;
3652 speed = PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.pitch_speed)->_float;
3654 if (current == ideal)
3656 move = ideal - current;
3657 if (ideal > current)
3678 PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.angles)->vector[0] = ANGLEMOD (current + move);
3682 static int Is_Text_Color (char c, char t)
3685 char c2 = c - (c & 128);
3686 char t2 = t - (t & 128);
3688 if(c != STRING_COLOR_TAG && c2 != STRING_COLOR_TAG) return 0;
3689 if(t >= '0' && t <= '9') a = 1;
3690 if(t2 >= '0' && t2 <= '9') a = 1;
3691 /* if(t >= 'A' && t <= 'Z') a = 2;
3692 if(t2 >= 'A' && t2 <= 'Z') a = 2;
3694 if(a == 1 && scr_colortext.integer > 0)
3696 if(a == 2 && scr_multifonts.integer > 0)
3702 void VM_uncolorstring (void)
3705 char out[VM_STRINGTEMP_LENGTH];
3708 VM_SAFEPARMCOUNT(1, VM_uncolorstring);
3709 in = PRVM_G_STRING(OFS_PARM0);
3710 VM_CheckEmptyString (in);
3715 if(Is_Text_Color(in[k], in[k+1]) == 1/* || (in[k] == '&' && in[k+1] == 'r')*/)
3724 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(out);
3727 //#222 string(string s, float index) str2chr (FTE_STRINGS)
3728 void VM_str2chr (void)
3731 VM_SAFEPARMCOUNT(2, VM_str2chr);
3732 s = PRVM_G_STRING(OFS_PARM0);
3733 if((unsigned)PRVM_G_FLOAT(OFS_PARM1) > strlen(s))
3735 PRVM_G_FLOAT(OFS_RETURN) = (unsigned char)s[(int)PRVM_G_FLOAT(OFS_PARM1)];
3738 //#223 string(float c, ...) chr2str (FTE_STRINGS)
3739 void VM_chr2str (void)
3743 VM_SAFEPARMCOUNTRANGE(0, 8, VM_chr2str);
3744 for(i = 0;i < prog->argc && i < (int)sizeof(t) - 1;i++)
3745 t[i] = (unsigned char)PRVM_G_FLOAT(OFS_PARM0+i*3);
3747 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(t);
3750 //#228 float(string s1, string s2, float len) strncmp (FTE_STRINGS)
3751 void VM_strncmp (void)
3753 const char *s1, *s2;
3754 VM_SAFEPARMCOUNT(1, VM_strncmp);
3755 s1 = PRVM_G_STRING(OFS_PARM0);
3756 s2 = PRVM_G_STRING(OFS_PARM1);
3757 PRVM_G_FLOAT(OFS_RETURN) = strncmp(s1, s2, (size_t)PRVM_G_FLOAT(OFS_PARM2));
3760 void VM_wasfreed (void)
3762 VM_SAFEPARMCOUNT(1, VM_wasfreed);
3763 PRVM_G_FLOAT(OFS_RETURN) = PRVM_G_EDICT(OFS_PARM0)->priv.required->free;
3766 void VM_SetTraceGlobals(const trace_t *trace)
3769 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_allsolid)))
3770 val->_float = trace->allsolid;
3771 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_startsolid)))
3772 val->_float = trace->startsolid;
3773 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_fraction)))
3774 val->_float = trace->fraction;
3775 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_inwater)))
3776 val->_float = trace->inwater;
3777 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_inopen)))
3778 val->_float = trace->inopen;
3779 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_endpos)))
3780 VectorCopy(trace->endpos, val->vector);
3781 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_plane_normal)))
3782 VectorCopy(trace->plane.normal, val->vector);
3783 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_plane_dist)))
3784 val->_float = trace->plane.dist;
3785 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_ent)))
3786 val->edict = PRVM_EDICT_TO_PROG(trace->ent ? trace->ent : prog->edicts);
3787 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dpstartcontents)))
3788 val->_float = trace->startsupercontents;
3789 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dphitcontents)))
3790 val->_float = trace->hitsupercontents;
3791 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dphitq3surfaceflags)))
3792 val->_float = trace->hitq3surfaceflags;
3793 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dphittexturename)))
3794 val->string = trace->hittexture ? PRVM_SetTempString(trace->hittexture->name) : 0;
3799 void VM_Cmd_Init(void)
3801 // only init the stuff for the current prog
3804 // VM_BufStr_Init();
3807 void VM_Cmd_Reset(void)
3809 CL_PurgeOwner( MENUOWNER );
3811 VM_Files_CloseAll();
3812 // VM_BufStr_ShutDown();