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;
1767 VM_SAFEPARMCOUNT(1,VM_strdecolorize);
1768 szString = PRVM_G_STRING(OFS_PARM0);
1771 { // Traverse through String
1772 if( szString[nPos] == '\n' || szString[nPos] == '\r' || szString[nPos] <= 0)
1773 { // String End Found
1774 szNewString[nFillPos++] = szString[nPos];
1778 if( szString[nPos] == STRING_COLOR_TAG)
1779 { // Color Code Located
1780 if( szString[nPos + 1] == STRING_COLOR_TAG)
1781 { // Valid Characters to Include
1782 szNewString[nFillPos++] = szString[nPos];
1784 szNewString[nFillPos++] = szString[nPos];
1787 if( szString[nPos + 1] >= '0' && szString[nPos + 1] <= '9' )
1788 { // Color Code Found; Increment Position
1792 { // Unknown Color Code; Include
1793 szNewString[nFillPos++] = szString[nPos];
1798 // Include Character
1799 szNewString[nFillPos++] = szString[nPos];
1801 // Increment Position
1805 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(szNewString);
1808 // DRESK - String Length (not counting color codes)
1813 float strlennocol(string s)
1816 // float(string s) strlennocol = #471; // returns how many characters are in a string not including color codes
1817 // For example, ^2Dresk returns a length of 5
1818 void VM_strlennocol(void)
1820 const char *szString;
1828 VM_SAFEPARMCOUNT(1,VM_strlennocol);
1830 szString = PRVM_G_STRING(OFS_PARM0);
1833 { // Count Characters
1834 // SV_BroadcastPrintf("Position '%d'; Character '%c'; Length '%d'\n", nPos, szString[nPos], nCnt);
1836 if( szString[nPos] == '\n' || szString[nPos] == '\r' || szString[nPos] <= 0)
1837 { // String End Found
1838 // SV_BroadcastPrintf("Found End of String at '%d'\n", nPos);
1842 if( szString[nPos] == STRING_COLOR_TAG)
1843 { // Color Code Located
1844 if( szString[nPos + 1] == STRING_COLOR_TAG)
1845 { // Increment Length; Skip Color Code
1850 if( szString[nPos + 1] >= '0' && szString[nPos + 1] <= '9' )
1851 { // Color Code Found; Increment Position
1852 // SV_BroadcastPrintf("Found Color Codes at '%d'\n", nPos);
1856 { // Unknown Color Code; Increment Length!
1862 // Increment String Length
1865 // Increment Position
1868 PRVM_G_FLOAT(OFS_RETURN) = nCnt;
1875 string strcat(string,string,...[string])
1878 //string(string s1, string s2) strcat = #115;
1879 // concatenates two strings (for example "abc", "def" would return "abcdef")
1880 // and returns as a tempstring
1881 void VM_strcat(void)
1883 char s[VM_STRINGTEMP_LENGTH];
1884 VM_SAFEPARMCOUNTRANGE(1, 8, VM_strcat);
1886 VM_VarString(0, s, sizeof(s));
1887 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(s);
1894 string substring(string s, float start, float length)
1897 // string(string s, float start, float length) substring = #116;
1898 // returns a section of a string as a tempstring
1899 void VM_substring(void)
1901 int i, start, length;
1903 char string[VM_STRINGTEMP_LENGTH];
1905 VM_SAFEPARMCOUNT(3,VM_substring);
1907 s = PRVM_G_STRING(OFS_PARM0);
1908 start = (int)PRVM_G_FLOAT(OFS_PARM1);
1909 length = (int)PRVM_G_FLOAT(OFS_PARM2);
1910 for (i = 0;i < start && *s;i++, s++);
1911 for (i = 0;i < (int)sizeof(string) - 1 && *s && i < length;i++, s++)
1914 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string);
1921 vector stov(string s)
1924 //vector(string s) stov = #117; // returns vector value from a string
1927 char string[VM_STRINGTEMP_LENGTH];
1929 VM_SAFEPARMCOUNT(1,VM_stov);
1931 VM_VarString(0, string, sizeof(string));
1932 Math_atov(string, PRVM_G_VECTOR(OFS_RETURN));
1939 string strzone(string s)
1942 //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)
1943 void VM_strzone(void)
1946 char string[VM_STRINGTEMP_LENGTH];
1949 VM_SAFEPARMCOUNT(1,VM_strzone);
1951 VM_VarString(0, string, sizeof(string));
1952 alloclen = strlen(string) + 1;
1953 PRVM_G_INT(OFS_RETURN) = PRVM_AllocString(alloclen, &out);
1954 memcpy(out, string, alloclen);
1964 //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!!!)
1965 void VM_strunzone(void)
1967 VM_SAFEPARMCOUNT(1,VM_strunzone);
1968 PRVM_FreeString(PRVM_G_INT(OFS_PARM0));
1973 VM_command (used by client and menu)
1975 clientcommand(float client, string s) (for client and menu)
1978 //void(entity e, string s) clientcommand = #440; // executes a command string as if it came from the specified client
1979 //this function originally written by KrimZon, made shorter by LordHavoc
1980 void VM_clcommand (void)
1982 client_t *temp_client;
1985 VM_SAFEPARMCOUNT(2,VM_clcommand);
1987 i = (int)PRVM_G_FLOAT(OFS_PARM0);
1988 if (!sv.active || i < 0 || i >= svs.maxclients || !svs.clients[i].active)
1990 VM_Warning("VM_clientcommand: %s: invalid client/server is not active !\n", PRVM_NAME);
1994 temp_client = host_client;
1995 host_client = svs.clients + i;
1996 Cmd_ExecuteString (PRVM_G_STRING(OFS_PARM1), src_client);
1997 host_client = temp_client;
2005 float tokenize(string s)
2008 //float(string s) tokenize = #441; // takes apart a string into individal words (access them with argv), returns how many
2009 //this function originally written by KrimZon, made shorter by LordHavoc
2010 //20040203: rewritten by LordHavoc (no longer uses allocations)
2013 void VM_tokenize (void)
2018 char tokenbuf[MAX_INPUTLINE];
2022 VM_SAFEPARMCOUNT(1,VM_tokenize);
2024 p = PRVM_G_STRING(OFS_PARM0);
2027 while(COM_ParseToken(&p, false))
2029 if (num_tokens >= (int)(sizeof(tokens)/sizeof(tokens[0])))
2032 tokenlen = strlen(com_token) + 1;
2033 if (pos + tokenlen > sizeof(tokenbuf))
2035 tokens[num_tokens++] = PRVM_SetEngineString(tokenbuf + pos);
2036 memcpy(tokenbuf + pos, com_token, tokenlen);
2039 tokens[num_tokens++] = PRVM_SetTempString(com_token);
2043 PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
2046 //string(float n) argv = #442; // returns a word from the tokenized string (returns nothing for an invalid index)
2047 //this function originally written by KrimZon, made shorter by LordHavoc
2052 VM_SAFEPARMCOUNT(1,VM_argv);
2054 token_num = (int)PRVM_G_FLOAT(OFS_PARM0);
2056 if (token_num >= 0 && token_num < num_tokens)
2057 PRVM_G_INT(OFS_RETURN) = tokens[token_num];
2059 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
2069 void VM_isserver(void)
2071 VM_SAFEPARMCOUNT(0,VM_serverstate);
2073 PRVM_G_FLOAT(OFS_RETURN) = sv.active && (svs.maxclients > 1 || cls.state == ca_dedicated);
2083 void VM_clientcount(void)
2085 VM_SAFEPARMCOUNT(0,VM_clientcount);
2087 PRVM_G_FLOAT(OFS_RETURN) = svs.maxclients;
2097 void VM_clientstate(void)
2099 VM_SAFEPARMCOUNT(0,VM_clientstate);
2101 PRVM_G_FLOAT(OFS_RETURN) = cls.state;
2108 float getostype(void)
2110 */ // not used at the moment -> not included in the common list
2111 void VM_getostype(void)
2113 VM_SAFEPARMCOUNT(0,VM_getostype);
2118 OS_MAC - not supported
2122 PRVM_G_FLOAT(OFS_RETURN) = 0;
2123 #elif defined(MACOSX)
2124 PRVM_G_FLOAT(OFS_RETURN) = 2;
2126 PRVM_G_FLOAT(OFS_RETURN) = 1;
2134 vector getmousepos()
2137 void VM_getmousepos(void)
2140 VM_SAFEPARMCOUNT(0,VM_getmousepos);
2142 PRVM_G_VECTOR(OFS_RETURN)[0] = in_mouse_x * vid_conwidth.integer / vid.width;
2143 PRVM_G_VECTOR(OFS_RETURN)[1] = in_mouse_y * vid_conheight.integer / vid.height;
2144 PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
2154 void VM_gettime(void)
2156 VM_SAFEPARMCOUNT(0,VM_gettime);
2158 PRVM_G_FLOAT(OFS_RETURN) = (float) realtime;
2165 loadfromdata(string data)
2168 void VM_loadfromdata(void)
2170 VM_SAFEPARMCOUNT(1,VM_loadentsfromfile);
2172 PRVM_ED_LoadFromFile(PRVM_G_STRING(OFS_PARM0));
2176 ========================
2179 parseentitydata(entity ent, string data)
2180 ========================
2182 void VM_parseentitydata(void)
2187 VM_SAFEPARMCOUNT(2, VM_parseentitydata);
2189 // get edict and test it
2190 ent = PRVM_G_EDICT(OFS_PARM0);
2191 if (ent->priv.required->free)
2192 PRVM_ERROR ("VM_parseentitydata: %s: Can only set already spawned entities (entity %i is free)!", PRVM_NAME, PRVM_NUM_FOR_EDICT(ent));
2194 data = PRVM_G_STRING(OFS_PARM1);
2196 // parse the opening brace
2197 if (!COM_ParseTokenConsole(&data) || com_token[0] != '{' )
2198 PRVM_ERROR ("VM_parseentitydata: %s: Couldn't parse entity data:\n%s", PRVM_NAME, data );
2200 PRVM_ED_ParseEdict (data, ent);
2207 loadfromfile(string file)
2210 void VM_loadfromfile(void)
2212 const char *filename;
2215 VM_SAFEPARMCOUNT(1,VM_loadfromfile);
2217 filename = PRVM_G_STRING(OFS_PARM0);
2218 if (FS_CheckNastyPath(filename, false))
2220 PRVM_G_FLOAT(OFS_RETURN) = -4;
2221 VM_Warning("VM_loadfromfile: %s dangerous or non-portable filename \"%s\" not allowed. (contains : or \\ or begins with .. or /)\n", PRVM_NAME, filename);
2225 // not conform with VM_fopen
2226 data = (char *)FS_LoadFile(filename, tempmempool, false, NULL);
2228 PRVM_G_FLOAT(OFS_RETURN) = -1;
2230 PRVM_ED_LoadFromFile(data);
2241 float mod(float val, float m)
2244 void VM_modulo(void)
2247 VM_SAFEPARMCOUNT(2,VM_module);
2249 val = (int) PRVM_G_FLOAT(OFS_PARM0);
2250 m = (int) PRVM_G_FLOAT(OFS_PARM1);
2252 PRVM_G_FLOAT(OFS_RETURN) = (float) (val % m);
2255 void VM_Search_Init(void)
2258 for (i = 0;i < PRVM_MAX_OPENSEARCHES;i++)
2259 prog->opensearches[i] = NULL;
2262 void VM_Search_Reset(void)
2265 // reset the fssearch list
2266 for(i = 0; i < PRVM_MAX_OPENSEARCHES; i++)
2268 if(prog->opensearches[i])
2269 FS_FreeSearch(prog->opensearches[i]);
2270 prog->opensearches[i] = NULL;
2278 float search_begin(string pattern, float caseinsensitive, float quiet)
2281 void VM_search_begin(void)
2284 const char *pattern;
2285 int caseinsens, quiet;
2287 VM_SAFEPARMCOUNT(3, VM_search_begin);
2289 pattern = PRVM_G_STRING(OFS_PARM0);
2291 VM_CheckEmptyString(pattern);
2293 caseinsens = (int)PRVM_G_FLOAT(OFS_PARM1);
2294 quiet = (int)PRVM_G_FLOAT(OFS_PARM2);
2296 for(handle = 0; handle < PRVM_MAX_OPENSEARCHES; handle++)
2297 if(!prog->opensearches[handle])
2300 if(handle >= PRVM_MAX_OPENSEARCHES)
2302 PRVM_G_FLOAT(OFS_RETURN) = -2;
2303 VM_Warning("VM_search_begin: %s ran out of search handles (%i)\n", PRVM_NAME, PRVM_MAX_OPENSEARCHES);
2307 if(!(prog->opensearches[handle] = FS_Search(pattern,caseinsens, quiet)))
2308 PRVM_G_FLOAT(OFS_RETURN) = -1;
2310 PRVM_G_FLOAT(OFS_RETURN) = handle;
2317 void search_end(float handle)
2320 void VM_search_end(void)
2323 VM_SAFEPARMCOUNT(1, VM_search_end);
2325 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
2327 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
2329 VM_Warning("VM_search_end: invalid handle %i used in %s\n", handle, PRVM_NAME);
2332 if(prog->opensearches[handle] == NULL)
2334 VM_Warning("VM_search_end: no such handle %i in %s\n", handle, PRVM_NAME);
2338 FS_FreeSearch(prog->opensearches[handle]);
2339 prog->opensearches[handle] = NULL;
2346 float search_getsize(float handle)
2349 void VM_search_getsize(void)
2352 VM_SAFEPARMCOUNT(1, VM_M_search_getsize);
2354 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
2356 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
2358 VM_Warning("VM_search_getsize: invalid handle %i used in %s\n", handle, PRVM_NAME);
2361 if(prog->opensearches[handle] == NULL)
2363 VM_Warning("VM_search_getsize: no such handle %i in %s\n", handle, PRVM_NAME);
2367 PRVM_G_FLOAT(OFS_RETURN) = prog->opensearches[handle]->numfilenames;
2372 VM_search_getfilename
2374 string search_getfilename(float handle, float num)
2377 void VM_search_getfilename(void)
2379 int handle, filenum;
2380 VM_SAFEPARMCOUNT(2, VM_search_getfilename);
2382 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
2383 filenum = (int)PRVM_G_FLOAT(OFS_PARM1);
2385 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
2387 VM_Warning("VM_search_getfilename: invalid handle %i used in %s\n", handle, PRVM_NAME);
2390 if(prog->opensearches[handle] == NULL)
2392 VM_Warning("VM_search_getfilename: no such handle %i in %s\n", handle, PRVM_NAME);
2395 if(filenum < 0 || filenum >= prog->opensearches[handle]->numfilenames)
2397 VM_Warning("VM_search_getfilename: invalid filenum %i in %s\n", filenum, PRVM_NAME);
2401 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog->opensearches[handle]->filenames[filenum]);
2408 string chr(float ascii)
2414 VM_SAFEPARMCOUNT(1, VM_chr);
2416 tmp[0] = (unsigned char) PRVM_G_FLOAT(OFS_PARM0);
2419 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(tmp);
2422 //=============================================================================
2423 // Draw builtins (client & menu)
2429 float iscachedpic(string pic)
2432 void VM_iscachedpic(void)
2434 VM_SAFEPARMCOUNT(1,VM_iscachedpic);
2436 // drawq hasnt such a function, thus always return true
2437 PRVM_G_FLOAT(OFS_RETURN) = false;
2444 string precache_pic(string pic)
2447 void VM_precache_pic(void)
2451 VM_SAFEPARMCOUNT(1, VM_precache_pic);
2453 s = PRVM_G_STRING(OFS_PARM0);
2454 PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
2455 VM_CheckEmptyString (s);
2457 // AK Draw_CachePic is supposed to always return a valid pointer
2458 if( Draw_CachePic(s, false)->tex == r_texture_notexture )
2459 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
2469 void VM_freepic(void)
2473 VM_SAFEPARMCOUNT(1,VM_freepic);
2475 s = PRVM_G_STRING(OFS_PARM0);
2476 VM_CheckEmptyString (s);
2485 float drawcharacter(vector position, float character, vector scale, vector rgb, float alpha, float flag)
2488 void VM_drawcharacter(void)
2490 float *pos,*scale,*rgb;
2493 VM_SAFEPARMCOUNT(6,VM_drawcharacter);
2495 character = (char) PRVM_G_FLOAT(OFS_PARM1);
2498 PRVM_G_FLOAT(OFS_RETURN) = -1;
2499 VM_Warning("VM_drawcharacter: %s passed null character !\n",PRVM_NAME);
2503 pos = PRVM_G_VECTOR(OFS_PARM0);
2504 scale = PRVM_G_VECTOR(OFS_PARM2);
2505 rgb = PRVM_G_VECTOR(OFS_PARM3);
2506 flag = (int)PRVM_G_FLOAT(OFS_PARM5);
2508 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2510 PRVM_G_FLOAT(OFS_RETURN) = -2;
2511 VM_Warning("VM_drawcharacter: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2515 if(pos[2] || scale[2])
2516 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")));
2518 if(!scale[0] || !scale[1])
2520 PRVM_G_FLOAT(OFS_RETURN) = -3;
2521 VM_Warning("VM_drawcharacter: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
2525 DrawQ_String (pos[0], pos[1], &character, 1, scale[0], scale[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM4), flag);
2526 PRVM_G_FLOAT(OFS_RETURN) = 1;
2533 float drawstring(vector position, string text, vector scale, vector rgb, float alpha, float flag)
2536 void VM_drawstring(void)
2538 float *pos,*scale,*rgb;
2541 VM_SAFEPARMCOUNT(6,VM_drawstring);
2543 string = PRVM_G_STRING(OFS_PARM1);
2544 pos = PRVM_G_VECTOR(OFS_PARM0);
2545 scale = PRVM_G_VECTOR(OFS_PARM2);
2546 rgb = PRVM_G_VECTOR(OFS_PARM3);
2547 flag = (int)PRVM_G_FLOAT(OFS_PARM5);
2549 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2551 PRVM_G_FLOAT(OFS_RETURN) = -2;
2552 VM_Warning("VM_drawstring: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2556 if(!scale[0] || !scale[1])
2558 PRVM_G_FLOAT(OFS_RETURN) = -3;
2559 VM_Warning("VM_drawstring: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
2563 if(pos[2] || scale[2])
2564 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")));
2566 DrawQ_String (pos[0], pos[1], string, 0, scale[0], scale[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM4), flag);
2567 PRVM_G_FLOAT(OFS_RETURN) = 1;
2573 float drawpic(vector position, string pic, vector size, vector rgb, float alpha, float flag)
2576 void VM_drawpic(void)
2578 const char *picname;
2579 float *size, *pos, *rgb;
2582 VM_SAFEPARMCOUNT(6,VM_drawpic);
2584 picname = PRVM_G_STRING(OFS_PARM1);
2585 VM_CheckEmptyString (picname);
2587 // is pic cached ? no function yet for that
2590 PRVM_G_FLOAT(OFS_RETURN) = -4;
2591 VM_Warning("VM_drawpic: %s: %s not cached !\n", PRVM_NAME, picname);
2595 pos = PRVM_G_VECTOR(OFS_PARM0);
2596 size = PRVM_G_VECTOR(OFS_PARM2);
2597 rgb = PRVM_G_VECTOR(OFS_PARM3);
2598 flag = (int) PRVM_G_FLOAT(OFS_PARM5);
2600 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2602 PRVM_G_FLOAT(OFS_RETURN) = -2;
2603 VM_Warning("VM_drawpic: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2607 if(pos[2] || size[2])
2608 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")));
2610 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);
2611 PRVM_G_FLOAT(OFS_RETURN) = 1;
2618 float drawfill(vector position, vector size, vector rgb, float alpha, float flag)
2621 void VM_drawfill(void)
2623 float *size, *pos, *rgb;
2626 VM_SAFEPARMCOUNT(5,VM_drawfill);
2629 pos = PRVM_G_VECTOR(OFS_PARM0);
2630 size = PRVM_G_VECTOR(OFS_PARM1);
2631 rgb = PRVM_G_VECTOR(OFS_PARM2);
2632 flag = (int) PRVM_G_FLOAT(OFS_PARM4);
2634 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2636 PRVM_G_FLOAT(OFS_RETURN) = -2;
2637 VM_Warning("VM_drawfill: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2641 if(pos[2] || size[2])
2642 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")));
2644 DrawQ_Pic(pos[0], pos[1], NULL, size[0], size[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM3), flag);
2645 PRVM_G_FLOAT(OFS_RETURN) = 1;
2652 drawsetcliparea(float x, float y, float width, float height)
2655 void VM_drawsetcliparea(void)
2658 VM_SAFEPARMCOUNT(4,VM_drawsetcliparea);
2660 x = bound(0, PRVM_G_FLOAT(OFS_PARM0), vid_conwidth.integer);
2661 y = bound(0, PRVM_G_FLOAT(OFS_PARM1), vid_conheight.integer);
2662 w = bound(0, PRVM_G_FLOAT(OFS_PARM2) + PRVM_G_FLOAT(OFS_PARM0) - x, (vid_conwidth.integer - x));
2663 h = bound(0, PRVM_G_FLOAT(OFS_PARM3) + PRVM_G_FLOAT(OFS_PARM1) - y, (vid_conheight.integer - y));
2665 DrawQ_SetClipArea(x, y, w, h);
2670 VM_drawresetcliparea
2675 void VM_drawresetcliparea(void)
2677 VM_SAFEPARMCOUNT(0,VM_drawresetcliparea);
2679 DrawQ_ResetClipArea();
2686 vector getimagesize(string pic)
2689 void VM_getimagesize(void)
2694 VM_SAFEPARMCOUNT(1,VM_getimagesize);
2696 p = PRVM_G_STRING(OFS_PARM0);
2697 VM_CheckEmptyString (p);
2699 pic = Draw_CachePic (p, false);
2701 PRVM_G_VECTOR(OFS_RETURN)[0] = pic->width;
2702 PRVM_G_VECTOR(OFS_RETURN)[1] = pic->height;
2703 PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
2710 string keynumtostring(float keynum)
2713 void VM_keynumtostring (void)
2715 VM_SAFEPARMCOUNT(1, VM_keynumtostring);
2717 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(Key_KeynumToString((int)PRVM_G_FLOAT(OFS_PARM0)));
2724 float stringtokeynum(string key)
2727 void VM_stringtokeynum (void)
2729 VM_SAFEPARMCOUNT( 1, VM_keynumtostring );
2731 PRVM_G_INT(OFS_RETURN) = Key_StringToKeynum(PRVM_G_STRING(OFS_PARM0));
2734 // CL_Video interface functions
2737 ========================
2740 float cin_open(string file, string name)
2741 ========================
2743 void VM_cin_open( void )
2748 VM_SAFEPARMCOUNT( 2, VM_cin_open );
2750 file = PRVM_G_STRING( OFS_PARM0 );
2751 name = PRVM_G_STRING( OFS_PARM1 );
2753 VM_CheckEmptyString( file );
2754 VM_CheckEmptyString( name );
2756 if( CL_OpenVideo( file, name, MENUOWNER ) )
2757 PRVM_G_FLOAT( OFS_RETURN ) = 1;
2759 PRVM_G_FLOAT( OFS_RETURN ) = 0;
2763 ========================
2766 void cin_close(string name)
2767 ========================
2769 void VM_cin_close( void )
2773 VM_SAFEPARMCOUNT( 1, VM_cin_close );
2775 name = PRVM_G_STRING( OFS_PARM0 );
2776 VM_CheckEmptyString( name );
2778 CL_CloseVideo( CL_GetVideoByName( name ) );
2782 ========================
2784 void cin_setstate(string name, float type)
2785 ========================
2787 void VM_cin_setstate( void )
2790 clvideostate_t state;
2793 VM_SAFEPARMCOUNT( 2, VM_cin_netstate );
2795 name = PRVM_G_STRING( OFS_PARM0 );
2796 VM_CheckEmptyString( name );
2798 state = (clvideostate_t)((int)PRVM_G_FLOAT( OFS_PARM1 ));
2800 video = CL_GetVideoByName( name );
2801 if( video && state > CLVIDEO_UNUSED && state < CLVIDEO_STATECOUNT )
2802 CL_SetVideoState( video, state );
2806 ========================
2809 float cin_getstate(string name)
2810 ========================
2812 void VM_cin_getstate( void )
2817 VM_SAFEPARMCOUNT( 1, VM_cin_getstate );
2819 name = PRVM_G_STRING( OFS_PARM0 );
2820 VM_CheckEmptyString( name );
2822 video = CL_GetVideoByName( name );
2824 PRVM_G_FLOAT( OFS_RETURN ) = (int)video->state;
2826 PRVM_G_FLOAT( OFS_RETURN ) = 0;
2830 ========================
2833 void cin_restart(string name)
2834 ========================
2836 void VM_cin_restart( void )
2841 VM_SAFEPARMCOUNT( 1, VM_cin_restart );
2843 name = PRVM_G_STRING( OFS_PARM0 );
2844 VM_CheckEmptyString( name );
2846 video = CL_GetVideoByName( name );
2848 CL_RestartVideo( video );
2855 Writes new values for v_forward, v_up, and v_right based on angles
2856 void makevectors(vector angle)
2859 void VM_makevectors (void)
2861 prvm_eval_t *valforward, *valright, *valup;
2862 valforward = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_forward);
2863 valright = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_right);
2864 valup = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_up);
2865 if (!valforward || !valright || !valup)
2867 VM_Warning("makevectors: could not find v_forward, v_right, or v_up global variables\n");
2870 VM_SAFEPARMCOUNT(1, VM_makevectors);
2871 AngleVectors (PRVM_G_VECTOR(OFS_PARM0), valforward->vector, valright->vector, valup->vector);
2878 Writes new values for v_forward, v_up, and v_right based on the given forward vector
2879 vectorvectors(vector)
2882 void VM_vectorvectors (void)
2884 prvm_eval_t *valforward, *valright, *valup;
2885 valforward = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_forward);
2886 valright = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_right);
2887 valup = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_up);
2888 if (!valforward || !valright || !valup)
2890 VM_Warning("vectorvectors: could not find v_forward, v_right, or v_up global variables\n");
2893 VM_SAFEPARMCOUNT(1, VM_vectorvectors);
2894 VectorNormalize2(PRVM_G_VECTOR(OFS_PARM0), valforward->vector);
2895 VectorVectors(valforward->vector, valright->vector, valup->vector);
2899 ========================
2902 void drawline(float width, vector pos1, vector pos2, vector rgb, float alpha, float flags)
2903 ========================
2905 void VM_drawline (void)
2907 float *c1, *c2, *rgb;
2909 unsigned char flags;
2911 VM_SAFEPARMCOUNT(6, VM_drawline);
2912 width = PRVM_G_FLOAT(OFS_PARM0);
2913 c1 = PRVM_G_VECTOR(OFS_PARM1);
2914 c2 = PRVM_G_VECTOR(OFS_PARM2);
2915 rgb = PRVM_G_VECTOR(OFS_PARM3);
2916 alpha = PRVM_G_FLOAT(OFS_PARM4);
2917 flags = (int)PRVM_G_FLOAT(OFS_PARM5);
2918 DrawQ_Line(width, c1[0], c1[1], c2[0], c2[1], rgb[0], rgb[1], rgb[2], alpha, flags);
2925 // float(float number, float quantity) bitshift (EXT_BITSHIFT)
2926 void VM_bitshift (void)
2929 VM_SAFEPARMCOUNT(2, VM_bitshift);
2931 n1 = (int)fabs((int)PRVM_G_FLOAT(OFS_PARM0));
2932 n2 = (int)PRVM_G_FLOAT(OFS_PARM1);
2934 PRVM_G_FLOAT(OFS_RETURN) = n1;
2937 PRVM_G_FLOAT(OFS_RETURN) = (n1 >> -n2);
2939 PRVM_G_FLOAT(OFS_RETURN) = (n1 << n2);
2942 ////////////////////////////////////////
2943 // AltString functions
2944 ////////////////////////////////////////
2947 ========================
2950 float altstr_count(string)
2951 ========================
2953 void VM_altstr_count( void )
2955 const char *altstr, *pos;
2958 VM_SAFEPARMCOUNT( 1, VM_altstr_count );
2960 altstr = PRVM_G_STRING( OFS_PARM0 );
2961 //VM_CheckEmptyString( altstr );
2963 for( count = 0, pos = altstr ; *pos ; pos++ ) {
2964 if( *pos == '\\' ) {
2968 } else if( *pos == '\'' ) {
2973 PRVM_G_FLOAT( OFS_RETURN ) = (float) (count / 2);
2977 ========================
2980 string altstr_prepare(string)
2981 ========================
2983 void VM_altstr_prepare( void )
2986 const char *instr, *in;
2988 char outstr[VM_STRINGTEMP_LENGTH];
2990 VM_SAFEPARMCOUNT( 1, VM_altstr_prepare );
2992 instr = PRVM_G_STRING( OFS_PARM0 );
2994 for( out = outstr, in = instr, size = sizeof(outstr) - 1 ; size && *in ; size--, in++, out++ )
3003 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
3007 ========================
3010 string altstr_get(string, float)
3011 ========================
3013 void VM_altstr_get( void )
3015 const char *altstr, *pos;
3018 char outstr[VM_STRINGTEMP_LENGTH];
3020 VM_SAFEPARMCOUNT( 2, VM_altstr_get );
3022 altstr = PRVM_G_STRING( OFS_PARM0 );
3024 count = (int)PRVM_G_FLOAT( OFS_PARM1 );
3025 count = count * 2 + 1;
3027 for( pos = altstr ; *pos && count ; pos++ )
3028 if( *pos == '\\' ) {
3031 } else if( *pos == '\'' )
3035 PRVM_G_INT( OFS_RETURN ) = 0;
3039 for( out = outstr, size = sizeof(outstr) - 1 ; size && *pos ; size--, pos++, out++ )
3040 if( *pos == '\\' ) {
3045 } else if( *pos == '\'' )
3051 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
3055 ========================
3058 string altstr_set(string altstr, float num, string set)
3059 ========================
3061 void VM_altstr_set( void )
3064 const char *altstr, *str;
3067 char outstr[VM_STRINGTEMP_LENGTH];
3069 VM_SAFEPARMCOUNT( 3, VM_altstr_set );
3071 altstr = PRVM_G_STRING( OFS_PARM0 );
3073 num = (int)PRVM_G_FLOAT( OFS_PARM1 );
3075 str = PRVM_G_STRING( OFS_PARM2 );
3078 for( num = num * 2 + 1, in = altstr; *in && num; *out++ = *in++ )
3083 } else if( *in == '\'' ) {
3088 for( ; *str; *out++ = *str++ );
3089 // now jump over the old content
3091 if( *in == '\'' || (*in == '\\' && !*++in) )
3094 strlcpy(out, in, outstr + sizeof(outstr) - out);
3095 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
3099 ========================
3102 string altstr_ins(string altstr, float num, string set)
3103 ========================
3105 void VM_altstr_ins(void)
3113 char outstr[VM_STRINGTEMP_LENGTH];
3115 VM_SAFEPARMCOUNT(3, VM_altstr_ins);
3117 in = instr = PRVM_G_STRING( OFS_PARM0 );
3118 num = (int)PRVM_G_FLOAT( OFS_PARM1 );
3119 set = setstr = PRVM_G_STRING( OFS_PARM2 );
3122 for( num = num * 2 + 2 ; *in && num > 0 ; *out++ = *in++ )
3127 } else if( *in == '\'' ) {
3132 for( ; *set ; *out++ = *set++ );
3135 strlcpy(out, in, outstr + sizeof(outstr) - out);
3136 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
3140 ////////////////////////////////////////
3141 // BufString functions
3142 ////////////////////////////////////////
3143 //[515]: string buffers support
3144 #define MAX_QCSTR_BUFFERS 128
3145 #define MAX_QCSTR_STRINGS 1024
3150 char *strings[MAX_QCSTR_STRINGS];
3153 // FIXME: move stringbuffers to prog_t to allow multiple progs!
3154 static qcstrbuffer_t *qcstringbuffers[MAX_QCSTR_BUFFERS];
3155 static int num_qcstringbuffers;
3156 static int buf_sortpower;
3158 #define BUFSTR_BUFFER(a) (a>=MAX_QCSTR_BUFFERS) ? NULL : (qcstringbuffers[a])
3159 #define BUFSTR_ISFREE(a) (a<MAX_QCSTR_BUFFERS&&qcstringbuffers[a]&&qcstringbuffers[a]->num_strings<=0) ? 1 : 0
3161 static int BufStr_FindFreeBuffer (void)
3164 if(num_qcstringbuffers == MAX_QCSTR_BUFFERS)
3166 for(i=0;i<MAX_QCSTR_BUFFERS;i++)
3167 if(!qcstringbuffers[i])
3169 qcstringbuffers[i] = (qcstrbuffer_t *)Z_Malloc(sizeof(qcstrbuffer_t));
3170 memset(qcstringbuffers[i], 0, sizeof(qcstrbuffer_t));
3176 static void BufStr_ClearBuffer (int index)
3178 qcstrbuffer_t *b = qcstringbuffers[index];
3183 if(b->num_strings > 0)
3185 for(i=0;i<b->num_strings;i++)
3187 Z_Free(b->strings[i]);
3188 num_qcstringbuffers--;
3190 Z_Free(qcstringbuffers[index]);
3191 qcstringbuffers[index] = NULL;
3195 static int BufStr_FindFreeString (qcstrbuffer_t *b)
3198 for(i=0;i<b->num_strings;i++)
3199 if(!b->strings[i] || !b->strings[i][0])
3201 if(i == MAX_QCSTR_STRINGS) return -1;
3205 static int BufStr_SortStringsUP (const void *in1, const void *in2)
3208 a = *((const char **) in1);
3209 b = *((const char **) in2);
3211 if(!b[0]) return -1;
3212 return strncmp(a, b, buf_sortpower);
3215 static int BufStr_SortStringsDOWN (const void *in1, const void *in2)
3218 a = *((const char **) in1);
3219 b = *((const char **) in2);
3221 if(!b[0]) return -1;
3222 return strncmp(b, a, buf_sortpower);
3226 ========================
3228 creates new buffer, and returns it's index, returns -1 if failed
3229 float buf_create(void) = #460;
3230 ========================
3232 void VM_buf_create (void)
3235 VM_SAFEPARMCOUNT(0, VM_buf_create);
3236 i = BufStr_FindFreeBuffer();
3238 num_qcstringbuffers++;
3240 //Con_Printf("VM_buf_create: buffers overflow in %s\n", PRVM_NAME);
3241 PRVM_G_FLOAT(OFS_RETURN) = i;
3245 ========================
3247 deletes buffer and all strings in it
3248 void buf_del(float bufhandle) = #461;
3249 ========================
3251 void VM_buf_del (void)
3253 VM_SAFEPARMCOUNT(1, VM_buf_del);
3254 if(BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0)))
3255 BufStr_ClearBuffer((int)PRVM_G_FLOAT(OFS_PARM0));
3258 VM_Warning("VM_buf_del: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3264 ========================
3266 how many strings are stored in buffer
3267 float buf_getsize(float bufhandle) = #462;
3268 ========================
3270 void VM_buf_getsize (void)
3273 VM_SAFEPARMCOUNT(1, VM_buf_getsize);
3275 b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3278 PRVM_G_FLOAT(OFS_RETURN) = -1;
3279 VM_Warning("VM_buf_getsize: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3283 PRVM_G_FLOAT(OFS_RETURN) = b->num_strings;
3287 ========================
3289 copy all content from one buffer to another, make sure it exists
3290 void buf_copy(float bufhandle_from, float bufhandle_to) = #463;
3291 ========================
3293 void VM_buf_copy (void)
3295 qcstrbuffer_t *b1, *b2;
3297 VM_SAFEPARMCOUNT(2, VM_buf_copy);
3299 b1 = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3302 VM_Warning("VM_buf_copy: invalid source buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3305 i = (int)PRVM_G_FLOAT(OFS_PARM1);
3306 if(i == (int)PRVM_G_FLOAT(OFS_PARM0))
3308 VM_Warning("VM_buf_copy: source == destination (%i) in %s\n", i, PRVM_NAME);
3311 b2 = BUFSTR_BUFFER(i);
3314 VM_Warning("VM_buf_copy: invalid destination buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM1), PRVM_NAME);
3318 BufStr_ClearBuffer(i);
3319 qcstringbuffers[i] = (qcstrbuffer_t *)Z_Malloc(sizeof(qcstrbuffer_t));
3320 memset(qcstringbuffers[i], 0, sizeof(qcstrbuffer_t));
3321 b2->num_strings = b1->num_strings;
3323 for(i=0;i<b1->num_strings;i++)
3324 if(b1->strings[i] && b1->strings[i][0])
3327 stringlen = strlen(b1->strings[i]) + 1;
3328 b2->strings[i] = (char *)Z_Malloc(stringlen);
3331 VM_Warning("VM_buf_copy: not enough memory for buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM1), PRVM_NAME);
3334 memcpy(b2->strings[i], b1->strings[i], stringlen);
3339 ========================
3341 sort buffer by beginnings of strings (sortpower defaults it's lenght)
3342 "backward == TRUE" means that sorting goes upside-down
3343 void buf_sort(float bufhandle, float sortpower, float backward) = #464;
3344 ========================
3346 void VM_buf_sort (void)
3350 VM_SAFEPARMCOUNT(3, VM_buf_sort);
3352 b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3355 VM_Warning("VM_buf_sort: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3358 if(b->num_strings <= 0)
3360 VM_Warning("VM_buf_sort: tried to sort empty buffer %i in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3363 buf_sortpower = (int)PRVM_G_FLOAT(OFS_PARM1);
3364 if(buf_sortpower <= 0)
3365 buf_sortpower = 99999999;
3367 if(!PRVM_G_FLOAT(OFS_PARM2))
3368 qsort(b->strings, b->num_strings, sizeof(char*), BufStr_SortStringsUP);
3370 qsort(b->strings, b->num_strings, sizeof(char*), BufStr_SortStringsDOWN);
3372 for(i=b->num_strings-1;i>=0;i--) //[515]: delete empty lines
3375 if(b->strings[i][0])
3379 Z_Free(b->strings[i]);
3381 b->strings[i] = NULL;
3389 ========================
3391 concantenates all buffer string into one with "glue" separator and returns it as tempstring
3392 string buf_implode(float bufhandle, string glue) = #465;
3393 ========================
3395 void VM_buf_implode (void)
3398 char k[VM_STRINGTEMP_LENGTH];
3402 VM_SAFEPARMCOUNT(2, VM_buf_implode);
3404 b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3405 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
3408 VM_Warning("VM_buf_implode: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3413 sep = PRVM_G_STRING(OFS_PARM1);
3415 for(l=i=0;i<b->num_strings;i++)
3418 l += (i > 0 ? strlen(sep) : 0) + strlen(b->strings[i]);
3419 if (l >= sizeof(k) - 1)
3421 strlcat(k, sep, sizeof(k));
3422 strlcat(k, b->strings[i], sizeof(k));
3424 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(k);
3428 ========================
3430 get a string from buffer, returns tempstring, dont str_unzone it!
3431 string bufstr_get(float bufhandle, float string_index) = #465;
3432 ========================
3434 void VM_bufstr_get (void)
3438 VM_SAFEPARMCOUNT(2, VM_bufstr_get);
3440 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
3441 b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3444 VM_Warning("VM_bufstr_get: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3447 strindex = (int)PRVM_G_FLOAT(OFS_PARM1);
3448 if(strindex < 0 || strindex > MAX_QCSTR_STRINGS)
3450 VM_Warning("VM_bufstr_get: invalid string index %i used in %s\n", strindex, PRVM_NAME);
3453 if(b->num_strings <= strindex)
3455 if(b->strings[strindex])
3456 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(b->strings[strindex]);
3460 ========================
3462 copies a string into selected slot of buffer
3463 void bufstr_set(float bufhandle, float string_index, string str) = #466;
3464 ========================
3466 void VM_bufstr_set (void)
3468 int bufindex, strindex;
3473 VM_SAFEPARMCOUNT(3, VM_bufstr_set);
3475 bufindex = (int)PRVM_G_FLOAT(OFS_PARM0);
3476 b = BUFSTR_BUFFER(bufindex);
3479 VM_Warning("VM_bufstr_set: invalid buffer %i used in %s\n", bufindex, PRVM_NAME);
3482 strindex = (int)PRVM_G_FLOAT(OFS_PARM1);
3483 if(strindex < 0 || strindex > MAX_QCSTR_STRINGS)
3485 VM_Warning("VM_bufstr_set: invalid string index %i used in %s\n", strindex, PRVM_NAME);
3488 news = PRVM_G_STRING(OFS_PARM2);
3489 if(b->strings[strindex])
3490 Z_Free(b->strings[strindex]);
3491 alloclen = strlen(news) + 1;
3492 b->strings[strindex] = (char *)Z_Malloc(alloclen);
3493 memcpy(b->strings[strindex], news, alloclen);
3497 ========================
3499 adds string to buffer in nearest free slot and returns it
3500 "order == TRUE" means that string will be added after last "full" slot
3501 float bufstr_add(float bufhandle, string str, float order) = #467;
3502 ========================
3504 void VM_bufstr_add (void)
3506 int bufindex, order, strindex;
3511 VM_SAFEPARMCOUNT(3, VM_bufstr_add);
3513 bufindex = (int)PRVM_G_FLOAT(OFS_PARM0);
3514 b = BUFSTR_BUFFER(bufindex);
3515 PRVM_G_FLOAT(OFS_RETURN) = -1;
3518 VM_Warning("VM_bufstr_add: invalid buffer %i used in %s\n", bufindex, PRVM_NAME);
3521 string = PRVM_G_STRING(OFS_PARM1);
3522 order = (int)PRVM_G_FLOAT(OFS_PARM2);
3524 strindex = b->num_strings;
3527 strindex = BufStr_FindFreeString(b);
3530 VM_Warning("VM_bufstr_add: buffer %i has no free string slots in %s\n", bufindex, PRVM_NAME);
3535 while(b->num_strings <= strindex)
3537 if(b->num_strings == MAX_QCSTR_STRINGS)
3539 VM_Warning("VM_bufstr_add: buffer %i has no free string slots in %s\n", bufindex, PRVM_NAME);
3542 b->strings[b->num_strings] = NULL;
3545 if(b->strings[strindex])
3546 Z_Free(b->strings[strindex]);
3547 alloclen = strlen(string) + 1;
3548 b->strings[strindex] = (char *)Z_Malloc(alloclen);
3549 memcpy(b->strings[strindex], string, alloclen);
3550 PRVM_G_FLOAT(OFS_RETURN) = strindex;
3554 ========================
3556 delete string from buffer
3557 void bufstr_free(float bufhandle, float string_index) = #468;
3558 ========================
3560 void VM_bufstr_free (void)
3564 VM_SAFEPARMCOUNT(2, VM_bufstr_free);
3566 b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3569 VM_Warning("VM_bufstr_free: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3572 i = (int)PRVM_G_FLOAT(OFS_PARM1);
3573 if(i < 0 || i > MAX_QCSTR_STRINGS)
3575 VM_Warning("VM_bufstr_free: invalid string index %i used in %s\n", i, PRVM_NAME);
3579 Z_Free(b->strings[i]);
3580 b->strings[i] = NULL;
3581 if(i+1 == b->num_strings)
3591 This was a major timewaster in progs, so it was converted to C
3594 void VM_changeyaw (void)
3597 float ideal, current, move, speed;
3599 // this is called (VERY HACKISHLY) by SV_MoveToGoal, so it can not use any
3600 // parameters because they are the parameters to SV_MoveToGoal, not this
3601 //VM_SAFEPARMCOUNT(0, VM_changeyaw);
3603 ent = PRVM_PROG_TO_EDICT(PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict);
3604 if (ent == prog->edicts)
3606 VM_Warning("changeyaw: can not modify world entity\n");
3609 if (ent->priv.server->free)
3611 VM_Warning("changeyaw: can not modify free entity\n");
3614 if (prog->fieldoffsets.angles < 0 || prog->fieldoffsets.ideal_yaw < 0 || prog->fieldoffsets.yaw_speed < 0)
3616 VM_Warning("changeyaw: angles, ideal_yaw, or yaw_speed field(s) not found\n");
3619 current = ANGLEMOD(PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.angles)->vector[1]);
3620 ideal = PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.ideal_yaw)->_float;
3621 speed = PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.yaw_speed)->_float;
3623 if (current == ideal)
3625 move = ideal - current;
3626 if (ideal > current)
3647 PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.angles)->vector[1] = ANGLEMOD (current + move);
3655 void VM_changepitch (void)
3658 float ideal, current, move, speed;
3660 VM_SAFEPARMCOUNT(1, VM_changepitch);
3662 ent = PRVM_G_EDICT(OFS_PARM0);
3663 if (ent == prog->edicts)
3665 VM_Warning("changepitch: can not modify world entity\n");
3668 if (ent->priv.server->free)
3670 VM_Warning("changepitch: can not modify free entity\n");
3673 if (prog->fieldoffsets.angles < 0 || prog->fieldoffsets.idealpitch < 0 || prog->fieldoffsets.pitch_speed < 0)
3675 VM_Warning("changepitch: angles, idealpitch, or pitch_speed field(s) not found\n");
3678 current = ANGLEMOD(PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.angles)->vector[0]);
3679 ideal = PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.idealpitch)->_float;
3680 speed = PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.pitch_speed)->_float;
3682 if (current == ideal)
3684 move = ideal - current;
3685 if (ideal > current)
3706 PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.angles)->vector[0] = ANGLEMOD (current + move);
3710 static int Is_Text_Color (char c, char t)
3713 char c2 = c - (c & 128);
3714 char t2 = t - (t & 128);
3716 if(c != STRING_COLOR_TAG && c2 != STRING_COLOR_TAG) return 0;
3717 if(t >= '0' && t <= '9') a = 1;
3718 if(t2 >= '0' && t2 <= '9') a = 1;
3719 /* if(t >= 'A' && t <= 'Z') a = 2;
3720 if(t2 >= 'A' && t2 <= 'Z') a = 2;
3722 if(a == 1 && scr_colortext.integer > 0)
3724 if(a == 2 && scr_multifonts.integer > 0)
3730 void VM_uncolorstring (void)
3733 char out[VM_STRINGTEMP_LENGTH];
3736 VM_SAFEPARMCOUNT(1, VM_uncolorstring);
3737 in = PRVM_G_STRING(OFS_PARM0);
3738 VM_CheckEmptyString (in);
3743 if(Is_Text_Color(in[k], in[k+1]) == 1/* || (in[k] == '&' && in[k+1] == 'r')*/)
3752 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(out);
3755 //#222 string(string s, float index) str2chr (FTE_STRINGS)
3756 void VM_str2chr (void)
3759 VM_SAFEPARMCOUNT(2, VM_str2chr);
3760 s = PRVM_G_STRING(OFS_PARM0);
3761 if((unsigned)PRVM_G_FLOAT(OFS_PARM1) > strlen(s))
3763 PRVM_G_FLOAT(OFS_RETURN) = (unsigned char)s[(int)PRVM_G_FLOAT(OFS_PARM1)];
3766 //#223 string(float c, ...) chr2str (FTE_STRINGS)
3767 void VM_chr2str (void)
3771 VM_SAFEPARMCOUNTRANGE(0, 8, VM_chr2str);
3772 for(i = 0;i < prog->argc && i < (int)sizeof(t) - 1;i++)
3773 t[i] = (unsigned char)PRVM_G_FLOAT(OFS_PARM0+i*3);
3775 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(t);
3778 //#228 float(string s1, string s2, float len) strncmp (FTE_STRINGS)
3779 void VM_strncmp (void)
3781 const char *s1, *s2;
3782 VM_SAFEPARMCOUNT(1, VM_strncmp);
3783 s1 = PRVM_G_STRING(OFS_PARM0);
3784 s2 = PRVM_G_STRING(OFS_PARM1);
3785 PRVM_G_FLOAT(OFS_RETURN) = strncmp(s1, s2, (size_t)PRVM_G_FLOAT(OFS_PARM2));
3788 void VM_wasfreed (void)
3790 VM_SAFEPARMCOUNT(1, VM_wasfreed);
3791 PRVM_G_FLOAT(OFS_RETURN) = PRVM_G_EDICT(OFS_PARM0)->priv.required->free;
3794 void VM_SetTraceGlobals(const trace_t *trace)
3797 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_allsolid)))
3798 val->_float = trace->allsolid;
3799 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_startsolid)))
3800 val->_float = trace->startsolid;
3801 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_fraction)))
3802 val->_float = trace->fraction;
3803 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_inwater)))
3804 val->_float = trace->inwater;
3805 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_inopen)))
3806 val->_float = trace->inopen;
3807 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_endpos)))
3808 VectorCopy(trace->endpos, val->vector);
3809 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_plane_normal)))
3810 VectorCopy(trace->plane.normal, val->vector);
3811 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_plane_dist)))
3812 val->_float = trace->plane.dist;
3813 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_ent)))
3814 val->edict = PRVM_EDICT_TO_PROG(trace->ent ? trace->ent : prog->edicts);
3815 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dpstartcontents)))
3816 val->_float = trace->startsupercontents;
3817 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dphitcontents)))
3818 val->_float = trace->hitsupercontents;
3819 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dphitq3surfaceflags)))
3820 val->_float = trace->hitq3surfaceflags;
3821 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dphittexturename)))
3822 val->string = trace->hittexture ? PRVM_SetTempString(trace->hittexture->name) : 0;
3827 void VM_Cmd_Init(void)
3829 // only init the stuff for the current prog
3832 // VM_BufStr_Init();
3835 void VM_Cmd_Reset(void)
3837 CL_PurgeOwner( MENUOWNER );
3839 VM_Files_CloseAll();
3840 // VM_BufStr_ShutDown();