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;
1791 // DRESK - String to Uppercase and Lowercase
1796 string strtolower(string s)
1799 // string (string s) strtolower = #480; // returns passed in string in lowercase form
1800 void VM_strtolower(void)
1802 char szNewString[VM_STRINGTEMP_LENGTH];
1803 const char *szString;
1806 VM_SAFEPARMCOUNT(1,VM_strtolower);
1807 szString = PRVM_G_STRING(OFS_PARM0);
1809 COM_ToLowerString(szString, szNewString, sizeof(szNewString) );
1811 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(szNewString);
1818 string strtoupper(string s)
1821 // string (string s) strtoupper = #481; // returns passed in string in uppercase form
1822 void VM_strtoupper(void)
1824 char szNewString[VM_STRINGTEMP_LENGTH];
1825 const char *szString;
1828 VM_SAFEPARMCOUNT(1,VM_strtoupper);
1829 szString = PRVM_G_STRING(OFS_PARM0);
1831 COM_ToUpperString(szString, szNewString, sizeof(szNewString) );
1833 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(szNewString);
1840 string strcat(string,string,...[string])
1843 //string(string s1, string s2) strcat = #115;
1844 // concatenates two strings (for example "abc", "def" would return "abcdef")
1845 // and returns as a tempstring
1846 void VM_strcat(void)
1848 char s[VM_STRINGTEMP_LENGTH];
1849 VM_SAFEPARMCOUNTRANGE(1, 8, VM_strcat);
1851 VM_VarString(0, s, sizeof(s));
1852 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(s);
1859 string substring(string s, float start, float length)
1862 // string(string s, float start, float length) substring = #116;
1863 // returns a section of a string as a tempstring
1864 void VM_substring(void)
1866 int i, start, length;
1868 char string[VM_STRINGTEMP_LENGTH];
1870 VM_SAFEPARMCOUNT(3,VM_substring);
1872 s = PRVM_G_STRING(OFS_PARM0);
1873 start = (int)PRVM_G_FLOAT(OFS_PARM1);
1874 length = (int)PRVM_G_FLOAT(OFS_PARM2);
1875 for (i = 0;i < start && *s;i++, s++);
1876 for (i = 0;i < (int)sizeof(string) - 1 && *s && i < length;i++, s++)
1879 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string);
1886 vector stov(string s)
1889 //vector(string s) stov = #117; // returns vector value from a string
1892 char string[VM_STRINGTEMP_LENGTH];
1894 VM_SAFEPARMCOUNT(1,VM_stov);
1896 VM_VarString(0, string, sizeof(string));
1897 Math_atov(string, PRVM_G_VECTOR(OFS_RETURN));
1904 string strzone(string s)
1907 //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)
1908 void VM_strzone(void)
1911 char string[VM_STRINGTEMP_LENGTH];
1914 VM_SAFEPARMCOUNT(1,VM_strzone);
1916 VM_VarString(0, string, sizeof(string));
1917 alloclen = strlen(string) + 1;
1918 PRVM_G_INT(OFS_RETURN) = PRVM_AllocString(alloclen, &out);
1919 memcpy(out, string, alloclen);
1929 //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!!!)
1930 void VM_strunzone(void)
1932 VM_SAFEPARMCOUNT(1,VM_strunzone);
1933 PRVM_FreeString(PRVM_G_INT(OFS_PARM0));
1938 VM_command (used by client and menu)
1940 clientcommand(float client, string s) (for client and menu)
1943 //void(entity e, string s) clientcommand = #440; // executes a command string as if it came from the specified client
1944 //this function originally written by KrimZon, made shorter by LordHavoc
1945 void VM_clcommand (void)
1947 client_t *temp_client;
1950 VM_SAFEPARMCOUNT(2,VM_clcommand);
1952 i = (int)PRVM_G_FLOAT(OFS_PARM0);
1953 if (!sv.active || i < 0 || i >= svs.maxclients || !svs.clients[i].active)
1955 VM_Warning("VM_clientcommand: %s: invalid client/server is not active !\n", PRVM_NAME);
1959 temp_client = host_client;
1960 host_client = svs.clients + i;
1961 Cmd_ExecuteString (PRVM_G_STRING(OFS_PARM1), src_client);
1962 host_client = temp_client;
1970 float tokenize(string s)
1973 //float(string s) tokenize = #441; // takes apart a string into individal words (access them with argv), returns how many
1974 //this function originally written by KrimZon, made shorter by LordHavoc
1975 //20040203: rewritten by LordHavoc (no longer uses allocations)
1978 void VM_tokenize (void)
1982 VM_SAFEPARMCOUNT(1,VM_tokenize);
1984 p = PRVM_G_STRING(OFS_PARM0);
1987 while(COM_ParseToken(&p, false))
1989 if (num_tokens >= (int)(sizeof(tokens)/sizeof(tokens[0])))
1991 tokens[num_tokens++] = PRVM_SetTempString(com_token);
1994 PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
1999 VM_tokenizebyseparator
2001 float tokenizebyseparator(string s, string separator1, ...)
2004 //float(string s, string separator1, ...) tokenizebyseparator = #479; // takes apart a string into individal words (access them with argv), returns how many
2005 //this function returns the token preceding each instance of a separator (of
2006 //which there can be multiple), and the text following the last separator
2007 //useful for parsing certain kinds of data like IP addresses
2009 //numnumbers = tokenizebyseparator("10.1.2.3", ".");
2010 //returns 4 and the tokens "10" "1" "2" "3".
2011 void VM_tokenizebyseparator (void)
2015 int separatorlen[7];
2016 const char *separators[7];
2018 char tokentext[MAX_INPUTLINE];
2020 VM_SAFEPARMCOUNTRANGE(2, 8,VM_tokenizebyseparator);
2022 p = PRVM_G_STRING(OFS_PARM0);
2025 for (j = 1;j < prog->argc;j++)
2027 // skip any blank separator strings
2028 if (!PRVM_G_STRING(OFS_PARM0 + j)[0])
2030 separators[numseparators] = PRVM_G_STRING(OFS_PARM0 + j);
2031 separatorlen[numseparators] = strlen(separators[numseparators]);
2036 for (num_tokens = 0;num_tokens < (int)(sizeof(tokens)/sizeof(tokens[0]));num_tokens++)
2040 for (k = 0;k < numseparators;k++)
2042 if (!strncmp(p, separators[k], separatorlen[k]))
2044 p += separatorlen[k];
2048 if (k < numseparators)
2050 if (j < (int)sizeof(tokentext[MAX_INPUTLINE]-1))
2051 tokentext[j++] = *p;
2055 tokens[num_tokens] = PRVM_SetTempString(tokentext);
2060 PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
2063 //string(float n) argv = #442; // returns a word from the tokenized string (returns nothing for an invalid index)
2064 //this function originally written by KrimZon, made shorter by LordHavoc
2069 VM_SAFEPARMCOUNT(1,VM_argv);
2071 token_num = (int)PRVM_G_FLOAT(OFS_PARM0);
2073 if (token_num >= 0 && token_num < num_tokens)
2074 PRVM_G_INT(OFS_RETURN) = tokens[token_num];
2076 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
2086 void VM_isserver(void)
2088 VM_SAFEPARMCOUNT(0,VM_serverstate);
2090 PRVM_G_FLOAT(OFS_RETURN) = sv.active && (svs.maxclients > 1 || cls.state == ca_dedicated);
2100 void VM_clientcount(void)
2102 VM_SAFEPARMCOUNT(0,VM_clientcount);
2104 PRVM_G_FLOAT(OFS_RETURN) = svs.maxclients;
2114 void VM_clientstate(void)
2116 VM_SAFEPARMCOUNT(0,VM_clientstate);
2118 PRVM_G_FLOAT(OFS_RETURN) = cls.state;
2125 float getostype(void)
2127 */ // not used at the moment -> not included in the common list
2128 void VM_getostype(void)
2130 VM_SAFEPARMCOUNT(0,VM_getostype);
2135 OS_MAC - not supported
2139 PRVM_G_FLOAT(OFS_RETURN) = 0;
2140 #elif defined(MACOSX)
2141 PRVM_G_FLOAT(OFS_RETURN) = 2;
2143 PRVM_G_FLOAT(OFS_RETURN) = 1;
2151 vector getmousepos()
2154 void VM_getmousepos(void)
2157 VM_SAFEPARMCOUNT(0,VM_getmousepos);
2159 PRVM_G_VECTOR(OFS_RETURN)[0] = in_mouse_x * vid_conwidth.integer / vid.width;
2160 PRVM_G_VECTOR(OFS_RETURN)[1] = in_mouse_y * vid_conheight.integer / vid.height;
2161 PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
2171 void VM_gettime(void)
2173 VM_SAFEPARMCOUNT(0,VM_gettime);
2175 PRVM_G_FLOAT(OFS_RETURN) = (float) realtime;
2182 loadfromdata(string data)
2185 void VM_loadfromdata(void)
2187 VM_SAFEPARMCOUNT(1,VM_loadentsfromfile);
2189 PRVM_ED_LoadFromFile(PRVM_G_STRING(OFS_PARM0));
2193 ========================
2196 parseentitydata(entity ent, string data)
2197 ========================
2199 void VM_parseentitydata(void)
2204 VM_SAFEPARMCOUNT(2, VM_parseentitydata);
2206 // get edict and test it
2207 ent = PRVM_G_EDICT(OFS_PARM0);
2208 if (ent->priv.required->free)
2209 PRVM_ERROR ("VM_parseentitydata: %s: Can only set already spawned entities (entity %i is free)!", PRVM_NAME, PRVM_NUM_FOR_EDICT(ent));
2211 data = PRVM_G_STRING(OFS_PARM1);
2213 // parse the opening brace
2214 if (!COM_ParseTokenConsole(&data) || com_token[0] != '{' )
2215 PRVM_ERROR ("VM_parseentitydata: %s: Couldn't parse entity data:\n%s", PRVM_NAME, data );
2217 PRVM_ED_ParseEdict (data, ent);
2224 loadfromfile(string file)
2227 void VM_loadfromfile(void)
2229 const char *filename;
2232 VM_SAFEPARMCOUNT(1,VM_loadfromfile);
2234 filename = PRVM_G_STRING(OFS_PARM0);
2235 if (FS_CheckNastyPath(filename, false))
2237 PRVM_G_FLOAT(OFS_RETURN) = -4;
2238 VM_Warning("VM_loadfromfile: %s dangerous or non-portable filename \"%s\" not allowed. (contains : or \\ or begins with .. or /)\n", PRVM_NAME, filename);
2242 // not conform with VM_fopen
2243 data = (char *)FS_LoadFile(filename, tempmempool, false, NULL);
2245 PRVM_G_FLOAT(OFS_RETURN) = -1;
2247 PRVM_ED_LoadFromFile(data);
2258 float mod(float val, float m)
2261 void VM_modulo(void)
2264 VM_SAFEPARMCOUNT(2,VM_module);
2266 val = (int) PRVM_G_FLOAT(OFS_PARM0);
2267 m = (int) PRVM_G_FLOAT(OFS_PARM1);
2269 PRVM_G_FLOAT(OFS_RETURN) = (float) (val % m);
2272 void VM_Search_Init(void)
2275 for (i = 0;i < PRVM_MAX_OPENSEARCHES;i++)
2276 prog->opensearches[i] = NULL;
2279 void VM_Search_Reset(void)
2282 // reset the fssearch list
2283 for(i = 0; i < PRVM_MAX_OPENSEARCHES; i++)
2285 if(prog->opensearches[i])
2286 FS_FreeSearch(prog->opensearches[i]);
2287 prog->opensearches[i] = NULL;
2295 float search_begin(string pattern, float caseinsensitive, float quiet)
2298 void VM_search_begin(void)
2301 const char *pattern;
2302 int caseinsens, quiet;
2304 VM_SAFEPARMCOUNT(3, VM_search_begin);
2306 pattern = PRVM_G_STRING(OFS_PARM0);
2308 VM_CheckEmptyString(pattern);
2310 caseinsens = (int)PRVM_G_FLOAT(OFS_PARM1);
2311 quiet = (int)PRVM_G_FLOAT(OFS_PARM2);
2313 for(handle = 0; handle < PRVM_MAX_OPENSEARCHES; handle++)
2314 if(!prog->opensearches[handle])
2317 if(handle >= PRVM_MAX_OPENSEARCHES)
2319 PRVM_G_FLOAT(OFS_RETURN) = -2;
2320 VM_Warning("VM_search_begin: %s ran out of search handles (%i)\n", PRVM_NAME, PRVM_MAX_OPENSEARCHES);
2324 if(!(prog->opensearches[handle] = FS_Search(pattern,caseinsens, quiet)))
2325 PRVM_G_FLOAT(OFS_RETURN) = -1;
2327 PRVM_G_FLOAT(OFS_RETURN) = handle;
2334 void search_end(float handle)
2337 void VM_search_end(void)
2340 VM_SAFEPARMCOUNT(1, VM_search_end);
2342 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
2344 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
2346 VM_Warning("VM_search_end: invalid handle %i used in %s\n", handle, PRVM_NAME);
2349 if(prog->opensearches[handle] == NULL)
2351 VM_Warning("VM_search_end: no such handle %i in %s\n", handle, PRVM_NAME);
2355 FS_FreeSearch(prog->opensearches[handle]);
2356 prog->opensearches[handle] = NULL;
2363 float search_getsize(float handle)
2366 void VM_search_getsize(void)
2369 VM_SAFEPARMCOUNT(1, VM_M_search_getsize);
2371 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
2373 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
2375 VM_Warning("VM_search_getsize: invalid handle %i used in %s\n", handle, PRVM_NAME);
2378 if(prog->opensearches[handle] == NULL)
2380 VM_Warning("VM_search_getsize: no such handle %i in %s\n", handle, PRVM_NAME);
2384 PRVM_G_FLOAT(OFS_RETURN) = prog->opensearches[handle]->numfilenames;
2389 VM_search_getfilename
2391 string search_getfilename(float handle, float num)
2394 void VM_search_getfilename(void)
2396 int handle, filenum;
2397 VM_SAFEPARMCOUNT(2, VM_search_getfilename);
2399 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
2400 filenum = (int)PRVM_G_FLOAT(OFS_PARM1);
2402 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
2404 VM_Warning("VM_search_getfilename: invalid handle %i used in %s\n", handle, PRVM_NAME);
2407 if(prog->opensearches[handle] == NULL)
2409 VM_Warning("VM_search_getfilename: no such handle %i in %s\n", handle, PRVM_NAME);
2412 if(filenum < 0 || filenum >= prog->opensearches[handle]->numfilenames)
2414 VM_Warning("VM_search_getfilename: invalid filenum %i in %s\n", filenum, PRVM_NAME);
2418 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog->opensearches[handle]->filenames[filenum]);
2425 string chr(float ascii)
2431 VM_SAFEPARMCOUNT(1, VM_chr);
2433 tmp[0] = (unsigned char) PRVM_G_FLOAT(OFS_PARM0);
2436 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(tmp);
2439 //=============================================================================
2440 // Draw builtins (client & menu)
2446 float iscachedpic(string pic)
2449 void VM_iscachedpic(void)
2451 VM_SAFEPARMCOUNT(1,VM_iscachedpic);
2453 // drawq hasnt such a function, thus always return true
2454 PRVM_G_FLOAT(OFS_RETURN) = false;
2461 string precache_pic(string pic)
2464 void VM_precache_pic(void)
2468 VM_SAFEPARMCOUNT(1, VM_precache_pic);
2470 s = PRVM_G_STRING(OFS_PARM0);
2471 PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
2472 VM_CheckEmptyString (s);
2474 // AK Draw_CachePic is supposed to always return a valid pointer
2475 if( Draw_CachePic(s, false)->tex == r_texture_notexture )
2476 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
2486 void VM_freepic(void)
2490 VM_SAFEPARMCOUNT(1,VM_freepic);
2492 s = PRVM_G_STRING(OFS_PARM0);
2493 VM_CheckEmptyString (s);
2502 float drawcharacter(vector position, float character, vector scale, vector rgb, float alpha, float flag)
2505 void VM_drawcharacter(void)
2507 float *pos,*scale,*rgb;
2510 VM_SAFEPARMCOUNT(6,VM_drawcharacter);
2512 character = (char) PRVM_G_FLOAT(OFS_PARM1);
2515 PRVM_G_FLOAT(OFS_RETURN) = -1;
2516 VM_Warning("VM_drawcharacter: %s passed null character !\n",PRVM_NAME);
2520 pos = PRVM_G_VECTOR(OFS_PARM0);
2521 scale = PRVM_G_VECTOR(OFS_PARM2);
2522 rgb = PRVM_G_VECTOR(OFS_PARM3);
2523 flag = (int)PRVM_G_FLOAT(OFS_PARM5);
2525 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2527 PRVM_G_FLOAT(OFS_RETURN) = -2;
2528 VM_Warning("VM_drawcharacter: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2532 if(pos[2] || scale[2])
2533 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")));
2535 if(!scale[0] || !scale[1])
2537 PRVM_G_FLOAT(OFS_RETURN) = -3;
2538 VM_Warning("VM_drawcharacter: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
2542 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);
2543 PRVM_G_FLOAT(OFS_RETURN) = 1;
2550 float drawstring(vector position, string text, vector scale, vector rgb, float alpha, float flag)
2553 void VM_drawstring(void)
2555 float *pos,*scale,*rgb;
2558 VM_SAFEPARMCOUNT(6,VM_drawstring);
2560 string = PRVM_G_STRING(OFS_PARM1);
2561 pos = PRVM_G_VECTOR(OFS_PARM0);
2562 scale = PRVM_G_VECTOR(OFS_PARM2);
2563 rgb = PRVM_G_VECTOR(OFS_PARM3);
2564 flag = (int)PRVM_G_FLOAT(OFS_PARM5);
2566 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2568 PRVM_G_FLOAT(OFS_RETURN) = -2;
2569 VM_Warning("VM_drawstring: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2573 if(!scale[0] || !scale[1])
2575 PRVM_G_FLOAT(OFS_RETURN) = -3;
2576 VM_Warning("VM_drawstring: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
2580 if(pos[2] || scale[2])
2581 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")));
2583 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);
2584 PRVM_G_FLOAT(OFS_RETURN) = 1;
2590 float drawpic(vector position, string pic, vector size, vector rgb, float alpha, float flag)
2593 void VM_drawpic(void)
2595 const char *picname;
2596 float *size, *pos, *rgb;
2599 VM_SAFEPARMCOUNT(6,VM_drawpic);
2601 picname = PRVM_G_STRING(OFS_PARM1);
2602 VM_CheckEmptyString (picname);
2604 // is pic cached ? no function yet for that
2607 PRVM_G_FLOAT(OFS_RETURN) = -4;
2608 VM_Warning("VM_drawpic: %s: %s not cached !\n", PRVM_NAME, picname);
2612 pos = PRVM_G_VECTOR(OFS_PARM0);
2613 size = PRVM_G_VECTOR(OFS_PARM2);
2614 rgb = PRVM_G_VECTOR(OFS_PARM3);
2615 flag = (int) PRVM_G_FLOAT(OFS_PARM5);
2617 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2619 PRVM_G_FLOAT(OFS_RETURN) = -2;
2620 VM_Warning("VM_drawpic: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2624 if(pos[2] || size[2])
2625 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")));
2627 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);
2628 PRVM_G_FLOAT(OFS_RETURN) = 1;
2635 float drawfill(vector position, vector size, vector rgb, float alpha, float flag)
2638 void VM_drawfill(void)
2640 float *size, *pos, *rgb;
2643 VM_SAFEPARMCOUNT(5,VM_drawfill);
2646 pos = PRVM_G_VECTOR(OFS_PARM0);
2647 size = PRVM_G_VECTOR(OFS_PARM1);
2648 rgb = PRVM_G_VECTOR(OFS_PARM2);
2649 flag = (int) PRVM_G_FLOAT(OFS_PARM4);
2651 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2653 PRVM_G_FLOAT(OFS_RETURN) = -2;
2654 VM_Warning("VM_drawfill: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2658 if(pos[2] || size[2])
2659 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")));
2661 DrawQ_Fill(pos[0], pos[1], size[0], size[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM3), flag);
2662 PRVM_G_FLOAT(OFS_RETURN) = 1;
2669 drawsetcliparea(float x, float y, float width, float height)
2672 void VM_drawsetcliparea(void)
2675 VM_SAFEPARMCOUNT(4,VM_drawsetcliparea);
2677 x = bound(0, PRVM_G_FLOAT(OFS_PARM0), vid_conwidth.integer);
2678 y = bound(0, PRVM_G_FLOAT(OFS_PARM1), vid_conheight.integer);
2679 w = bound(0, PRVM_G_FLOAT(OFS_PARM2) + PRVM_G_FLOAT(OFS_PARM0) - x, (vid_conwidth.integer - x));
2680 h = bound(0, PRVM_G_FLOAT(OFS_PARM3) + PRVM_G_FLOAT(OFS_PARM1) - y, (vid_conheight.integer - y));
2682 DrawQ_SetClipArea(x, y, w, h);
2687 VM_drawresetcliparea
2692 void VM_drawresetcliparea(void)
2694 VM_SAFEPARMCOUNT(0,VM_drawresetcliparea);
2696 DrawQ_ResetClipArea();
2703 vector getimagesize(string pic)
2706 void VM_getimagesize(void)
2711 VM_SAFEPARMCOUNT(1,VM_getimagesize);
2713 p = PRVM_G_STRING(OFS_PARM0);
2714 VM_CheckEmptyString (p);
2716 pic = Draw_CachePic (p, false);
2718 PRVM_G_VECTOR(OFS_RETURN)[0] = pic->width;
2719 PRVM_G_VECTOR(OFS_RETURN)[1] = pic->height;
2720 PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
2727 string keynumtostring(float keynum)
2730 void VM_keynumtostring (void)
2732 VM_SAFEPARMCOUNT(1, VM_keynumtostring);
2734 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(Key_KeynumToString((int)PRVM_G_FLOAT(OFS_PARM0)));
2741 float stringtokeynum(string key)
2744 void VM_stringtokeynum (void)
2746 VM_SAFEPARMCOUNT( 1, VM_keynumtostring );
2748 PRVM_G_INT(OFS_RETURN) = Key_StringToKeynum(PRVM_G_STRING(OFS_PARM0));
2751 // CL_Video interface functions
2754 ========================
2757 float cin_open(string file, string name)
2758 ========================
2760 void VM_cin_open( void )
2765 VM_SAFEPARMCOUNT( 2, VM_cin_open );
2767 file = PRVM_G_STRING( OFS_PARM0 );
2768 name = PRVM_G_STRING( OFS_PARM1 );
2770 VM_CheckEmptyString( file );
2771 VM_CheckEmptyString( name );
2773 if( CL_OpenVideo( file, name, MENUOWNER ) )
2774 PRVM_G_FLOAT( OFS_RETURN ) = 1;
2776 PRVM_G_FLOAT( OFS_RETURN ) = 0;
2780 ========================
2783 void cin_close(string name)
2784 ========================
2786 void VM_cin_close( void )
2790 VM_SAFEPARMCOUNT( 1, VM_cin_close );
2792 name = PRVM_G_STRING( OFS_PARM0 );
2793 VM_CheckEmptyString( name );
2795 CL_CloseVideo( CL_GetVideoByName( name ) );
2799 ========================
2801 void cin_setstate(string name, float type)
2802 ========================
2804 void VM_cin_setstate( void )
2807 clvideostate_t state;
2810 VM_SAFEPARMCOUNT( 2, VM_cin_netstate );
2812 name = PRVM_G_STRING( OFS_PARM0 );
2813 VM_CheckEmptyString( name );
2815 state = (clvideostate_t)((int)PRVM_G_FLOAT( OFS_PARM1 ));
2817 video = CL_GetVideoByName( name );
2818 if( video && state > CLVIDEO_UNUSED && state < CLVIDEO_STATECOUNT )
2819 CL_SetVideoState( video, state );
2823 ========================
2826 float cin_getstate(string name)
2827 ========================
2829 void VM_cin_getstate( void )
2834 VM_SAFEPARMCOUNT( 1, VM_cin_getstate );
2836 name = PRVM_G_STRING( OFS_PARM0 );
2837 VM_CheckEmptyString( name );
2839 video = CL_GetVideoByName( name );
2841 PRVM_G_FLOAT( OFS_RETURN ) = (int)video->state;
2843 PRVM_G_FLOAT( OFS_RETURN ) = 0;
2847 ========================
2850 void cin_restart(string name)
2851 ========================
2853 void VM_cin_restart( void )
2858 VM_SAFEPARMCOUNT( 1, VM_cin_restart );
2860 name = PRVM_G_STRING( OFS_PARM0 );
2861 VM_CheckEmptyString( name );
2863 video = CL_GetVideoByName( name );
2865 CL_RestartVideo( video );
2872 Writes new values for v_forward, v_up, and v_right based on angles
2873 void makevectors(vector angle)
2876 void VM_makevectors (void)
2878 prvm_eval_t *valforward, *valright, *valup;
2879 valforward = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_forward);
2880 valright = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_right);
2881 valup = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_up);
2882 if (!valforward || !valright || !valup)
2884 VM_Warning("makevectors: could not find v_forward, v_right, or v_up global variables\n");
2887 VM_SAFEPARMCOUNT(1, VM_makevectors);
2888 AngleVectors (PRVM_G_VECTOR(OFS_PARM0), valforward->vector, valright->vector, valup->vector);
2895 Writes new values for v_forward, v_up, and v_right based on the given forward vector
2896 vectorvectors(vector)
2899 void VM_vectorvectors (void)
2901 prvm_eval_t *valforward, *valright, *valup;
2902 valforward = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_forward);
2903 valright = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_right);
2904 valup = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_up);
2905 if (!valforward || !valright || !valup)
2907 VM_Warning("vectorvectors: could not find v_forward, v_right, or v_up global variables\n");
2910 VM_SAFEPARMCOUNT(1, VM_vectorvectors);
2911 VectorNormalize2(PRVM_G_VECTOR(OFS_PARM0), valforward->vector);
2912 VectorVectors(valforward->vector, valright->vector, valup->vector);
2916 ========================
2919 void drawline(float width, vector pos1, vector pos2, vector rgb, float alpha, float flags)
2920 ========================
2922 void VM_drawline (void)
2924 float *c1, *c2, *rgb;
2926 unsigned char flags;
2928 VM_SAFEPARMCOUNT(6, VM_drawline);
2929 width = PRVM_G_FLOAT(OFS_PARM0);
2930 c1 = PRVM_G_VECTOR(OFS_PARM1);
2931 c2 = PRVM_G_VECTOR(OFS_PARM2);
2932 rgb = PRVM_G_VECTOR(OFS_PARM3);
2933 alpha = PRVM_G_FLOAT(OFS_PARM4);
2934 flags = (int)PRVM_G_FLOAT(OFS_PARM5);
2935 DrawQ_Line(width, c1[0], c1[1], c2[0], c2[1], rgb[0], rgb[1], rgb[2], alpha, flags);
2942 // float(float number, float quantity) bitshift (EXT_BITSHIFT)
2943 void VM_bitshift (void)
2946 VM_SAFEPARMCOUNT(2, VM_bitshift);
2948 n1 = (int)fabs((int)PRVM_G_FLOAT(OFS_PARM0));
2949 n2 = (int)PRVM_G_FLOAT(OFS_PARM1);
2951 PRVM_G_FLOAT(OFS_RETURN) = n1;
2954 PRVM_G_FLOAT(OFS_RETURN) = (n1 >> -n2);
2956 PRVM_G_FLOAT(OFS_RETURN) = (n1 << n2);
2959 ////////////////////////////////////////
2960 // AltString functions
2961 ////////////////////////////////////////
2964 ========================
2967 float altstr_count(string)
2968 ========================
2970 void VM_altstr_count( void )
2972 const char *altstr, *pos;
2975 VM_SAFEPARMCOUNT( 1, VM_altstr_count );
2977 altstr = PRVM_G_STRING( OFS_PARM0 );
2978 //VM_CheckEmptyString( altstr );
2980 for( count = 0, pos = altstr ; *pos ; pos++ ) {
2981 if( *pos == '\\' ) {
2985 } else if( *pos == '\'' ) {
2990 PRVM_G_FLOAT( OFS_RETURN ) = (float) (count / 2);
2994 ========================
2997 string altstr_prepare(string)
2998 ========================
3000 void VM_altstr_prepare( void )
3003 const char *instr, *in;
3005 char outstr[VM_STRINGTEMP_LENGTH];
3007 VM_SAFEPARMCOUNT( 1, VM_altstr_prepare );
3009 instr = PRVM_G_STRING( OFS_PARM0 );
3011 for( out = outstr, in = instr, size = sizeof(outstr) - 1 ; size && *in ; size--, in++, out++ )
3020 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
3024 ========================
3027 string altstr_get(string, float)
3028 ========================
3030 void VM_altstr_get( void )
3032 const char *altstr, *pos;
3035 char outstr[VM_STRINGTEMP_LENGTH];
3037 VM_SAFEPARMCOUNT( 2, VM_altstr_get );
3039 altstr = PRVM_G_STRING( OFS_PARM0 );
3041 count = (int)PRVM_G_FLOAT( OFS_PARM1 );
3042 count = count * 2 + 1;
3044 for( pos = altstr ; *pos && count ; pos++ )
3045 if( *pos == '\\' ) {
3048 } else if( *pos == '\'' )
3052 PRVM_G_INT( OFS_RETURN ) = 0;
3056 for( out = outstr, size = sizeof(outstr) - 1 ; size && *pos ; size--, pos++, out++ )
3057 if( *pos == '\\' ) {
3062 } else if( *pos == '\'' )
3068 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
3072 ========================
3075 string altstr_set(string altstr, float num, string set)
3076 ========================
3078 void VM_altstr_set( void )
3081 const char *altstr, *str;
3084 char outstr[VM_STRINGTEMP_LENGTH];
3086 VM_SAFEPARMCOUNT( 3, VM_altstr_set );
3088 altstr = PRVM_G_STRING( OFS_PARM0 );
3090 num = (int)PRVM_G_FLOAT( OFS_PARM1 );
3092 str = PRVM_G_STRING( OFS_PARM2 );
3095 for( num = num * 2 + 1, in = altstr; *in && num; *out++ = *in++ )
3100 } else if( *in == '\'' ) {
3105 for( ; *str; *out++ = *str++ );
3106 // now jump over the old content
3108 if( *in == '\'' || (*in == '\\' && !*++in) )
3111 strlcpy(out, in, outstr + sizeof(outstr) - out);
3112 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
3116 ========================
3119 string altstr_ins(string altstr, float num, string set)
3120 ========================
3122 void VM_altstr_ins(void)
3130 char outstr[VM_STRINGTEMP_LENGTH];
3132 VM_SAFEPARMCOUNT(3, VM_altstr_ins);
3134 in = instr = PRVM_G_STRING( OFS_PARM0 );
3135 num = (int)PRVM_G_FLOAT( OFS_PARM1 );
3136 set = setstr = PRVM_G_STRING( OFS_PARM2 );
3139 for( num = num * 2 + 2 ; *in && num > 0 ; *out++ = *in++ )
3144 } else if( *in == '\'' ) {
3149 for( ; *set ; *out++ = *set++ );
3152 strlcpy(out, in, outstr + sizeof(outstr) - out);
3153 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
3157 ////////////////////////////////////////
3158 // BufString functions
3159 ////////////////////////////////////////
3160 //[515]: string buffers support
3161 #define MAX_QCSTR_BUFFERS 128
3162 #define MAX_QCSTR_STRINGS 1024
3167 char *strings[MAX_QCSTR_STRINGS];
3170 // FIXME: move stringbuffers to prog_t to allow multiple progs!
3171 static qcstrbuffer_t *qcstringbuffers[MAX_QCSTR_BUFFERS];
3172 static int num_qcstringbuffers;
3173 static int buf_sortpower;
3175 #define BUFSTR_BUFFER(a) (a>=MAX_QCSTR_BUFFERS) ? NULL : (qcstringbuffers[a])
3176 #define BUFSTR_ISFREE(a) (a<MAX_QCSTR_BUFFERS&&qcstringbuffers[a]&&qcstringbuffers[a]->num_strings<=0) ? 1 : 0
3178 static int BufStr_FindFreeBuffer (void)
3181 if(num_qcstringbuffers == MAX_QCSTR_BUFFERS)
3183 for(i=0;i<MAX_QCSTR_BUFFERS;i++)
3184 if(!qcstringbuffers[i])
3186 qcstringbuffers[i] = (qcstrbuffer_t *)Z_Malloc(sizeof(qcstrbuffer_t));
3187 memset(qcstringbuffers[i], 0, sizeof(qcstrbuffer_t));
3193 static void BufStr_ClearBuffer (int index)
3195 qcstrbuffer_t *b = qcstringbuffers[index];
3200 if(b->num_strings > 0)
3202 for(i=0;i<b->num_strings;i++)
3204 Z_Free(b->strings[i]);
3205 num_qcstringbuffers--;
3207 Z_Free(qcstringbuffers[index]);
3208 qcstringbuffers[index] = NULL;
3212 static int BufStr_FindFreeString (qcstrbuffer_t *b)
3215 for(i=0;i<b->num_strings;i++)
3216 if(!b->strings[i] || !b->strings[i][0])
3218 if(i == MAX_QCSTR_STRINGS) return -1;
3222 static int BufStr_SortStringsUP (const void *in1, const void *in2)
3225 a = *((const char **) in1);
3226 b = *((const char **) in2);
3228 if(!b[0]) return -1;
3229 return strncmp(a, b, buf_sortpower);
3232 static int BufStr_SortStringsDOWN (const void *in1, const void *in2)
3235 a = *((const char **) in1);
3236 b = *((const char **) in2);
3238 if(!b[0]) return -1;
3239 return strncmp(b, a, buf_sortpower);
3243 ========================
3245 creates new buffer, and returns it's index, returns -1 if failed
3246 float buf_create(void) = #460;
3247 ========================
3249 void VM_buf_create (void)
3252 VM_SAFEPARMCOUNT(0, VM_buf_create);
3253 i = BufStr_FindFreeBuffer();
3255 num_qcstringbuffers++;
3257 //Con_Printf("VM_buf_create: buffers overflow in %s\n", PRVM_NAME);
3258 PRVM_G_FLOAT(OFS_RETURN) = i;
3262 ========================
3264 deletes buffer and all strings in it
3265 void buf_del(float bufhandle) = #461;
3266 ========================
3268 void VM_buf_del (void)
3270 VM_SAFEPARMCOUNT(1, VM_buf_del);
3271 if(BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0)))
3272 BufStr_ClearBuffer((int)PRVM_G_FLOAT(OFS_PARM0));
3275 VM_Warning("VM_buf_del: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3281 ========================
3283 how many strings are stored in buffer
3284 float buf_getsize(float bufhandle) = #462;
3285 ========================
3287 void VM_buf_getsize (void)
3290 VM_SAFEPARMCOUNT(1, VM_buf_getsize);
3292 b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3295 PRVM_G_FLOAT(OFS_RETURN) = -1;
3296 VM_Warning("VM_buf_getsize: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3300 PRVM_G_FLOAT(OFS_RETURN) = b->num_strings;
3304 ========================
3306 copy all content from one buffer to another, make sure it exists
3307 void buf_copy(float bufhandle_from, float bufhandle_to) = #463;
3308 ========================
3310 void VM_buf_copy (void)
3312 qcstrbuffer_t *b1, *b2;
3314 VM_SAFEPARMCOUNT(2, VM_buf_copy);
3316 b1 = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3319 VM_Warning("VM_buf_copy: invalid source buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3322 i = (int)PRVM_G_FLOAT(OFS_PARM1);
3323 if(i == (int)PRVM_G_FLOAT(OFS_PARM0))
3325 VM_Warning("VM_buf_copy: source == destination (%i) in %s\n", i, PRVM_NAME);
3328 b2 = BUFSTR_BUFFER(i);
3331 VM_Warning("VM_buf_copy: invalid destination buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM1), PRVM_NAME);
3335 BufStr_ClearBuffer(i);
3336 qcstringbuffers[i] = (qcstrbuffer_t *)Z_Malloc(sizeof(qcstrbuffer_t));
3337 memset(qcstringbuffers[i], 0, sizeof(qcstrbuffer_t));
3338 b2->num_strings = b1->num_strings;
3340 for(i=0;i<b1->num_strings;i++)
3341 if(b1->strings[i] && b1->strings[i][0])
3344 stringlen = strlen(b1->strings[i]) + 1;
3345 b2->strings[i] = (char *)Z_Malloc(stringlen);
3348 VM_Warning("VM_buf_copy: not enough memory for buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM1), PRVM_NAME);
3351 memcpy(b2->strings[i], b1->strings[i], stringlen);
3356 ========================
3358 sort buffer by beginnings of strings (sortpower defaults it's lenght)
3359 "backward == TRUE" means that sorting goes upside-down
3360 void buf_sort(float bufhandle, float sortpower, float backward) = #464;
3361 ========================
3363 void VM_buf_sort (void)
3367 VM_SAFEPARMCOUNT(3, VM_buf_sort);
3369 b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3372 VM_Warning("VM_buf_sort: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3375 if(b->num_strings <= 0)
3377 VM_Warning("VM_buf_sort: tried to sort empty buffer %i in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3380 buf_sortpower = (int)PRVM_G_FLOAT(OFS_PARM1);
3381 if(buf_sortpower <= 0)
3382 buf_sortpower = 99999999;
3384 if(!PRVM_G_FLOAT(OFS_PARM2))
3385 qsort(b->strings, b->num_strings, sizeof(char*), BufStr_SortStringsUP);
3387 qsort(b->strings, b->num_strings, sizeof(char*), BufStr_SortStringsDOWN);
3389 for(i=b->num_strings-1;i>=0;i--) //[515]: delete empty lines
3392 if(b->strings[i][0])
3396 Z_Free(b->strings[i]);
3398 b->strings[i] = NULL;
3406 ========================
3408 concantenates all buffer string into one with "glue" separator and returns it as tempstring
3409 string buf_implode(float bufhandle, string glue) = #465;
3410 ========================
3412 void VM_buf_implode (void)
3415 char k[VM_STRINGTEMP_LENGTH];
3419 VM_SAFEPARMCOUNT(2, VM_buf_implode);
3421 b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3422 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
3425 VM_Warning("VM_buf_implode: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3430 sep = PRVM_G_STRING(OFS_PARM1);
3432 for(l=i=0;i<b->num_strings;i++)
3435 l += (i > 0 ? strlen(sep) : 0) + strlen(b->strings[i]);
3436 if (l >= sizeof(k) - 1)
3438 strlcat(k, sep, sizeof(k));
3439 strlcat(k, b->strings[i], sizeof(k));
3441 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(k);
3445 ========================
3447 get a string from buffer, returns tempstring, dont str_unzone it!
3448 string bufstr_get(float bufhandle, float string_index) = #465;
3449 ========================
3451 void VM_bufstr_get (void)
3455 VM_SAFEPARMCOUNT(2, VM_bufstr_get);
3457 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
3458 b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3461 VM_Warning("VM_bufstr_get: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3464 strindex = (int)PRVM_G_FLOAT(OFS_PARM1);
3465 if(strindex < 0 || strindex > MAX_QCSTR_STRINGS)
3467 VM_Warning("VM_bufstr_get: invalid string index %i used in %s\n", strindex, PRVM_NAME);
3470 if(b->num_strings <= strindex)
3472 if(b->strings[strindex])
3473 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(b->strings[strindex]);
3477 ========================
3479 copies a string into selected slot of buffer
3480 void bufstr_set(float bufhandle, float string_index, string str) = #466;
3481 ========================
3483 void VM_bufstr_set (void)
3485 int bufindex, strindex;
3490 VM_SAFEPARMCOUNT(3, VM_bufstr_set);
3492 bufindex = (int)PRVM_G_FLOAT(OFS_PARM0);
3493 b = BUFSTR_BUFFER(bufindex);
3496 VM_Warning("VM_bufstr_set: invalid buffer %i used in %s\n", bufindex, PRVM_NAME);
3499 strindex = (int)PRVM_G_FLOAT(OFS_PARM1);
3500 if(strindex < 0 || strindex > MAX_QCSTR_STRINGS)
3502 VM_Warning("VM_bufstr_set: invalid string index %i used in %s\n", strindex, PRVM_NAME);
3505 news = PRVM_G_STRING(OFS_PARM2);
3506 if(b->strings[strindex])
3507 Z_Free(b->strings[strindex]);
3508 alloclen = strlen(news) + 1;
3509 b->strings[strindex] = (char *)Z_Malloc(alloclen);
3510 memcpy(b->strings[strindex], news, alloclen);
3514 ========================
3516 adds string to buffer in nearest free slot and returns it
3517 "order == TRUE" means that string will be added after last "full" slot
3518 float bufstr_add(float bufhandle, string str, float order) = #467;
3519 ========================
3521 void VM_bufstr_add (void)
3523 int bufindex, order, strindex;
3528 VM_SAFEPARMCOUNT(3, VM_bufstr_add);
3530 bufindex = (int)PRVM_G_FLOAT(OFS_PARM0);
3531 b = BUFSTR_BUFFER(bufindex);
3532 PRVM_G_FLOAT(OFS_RETURN) = -1;
3535 VM_Warning("VM_bufstr_add: invalid buffer %i used in %s\n", bufindex, PRVM_NAME);
3538 string = PRVM_G_STRING(OFS_PARM1);
3539 order = (int)PRVM_G_FLOAT(OFS_PARM2);
3541 strindex = b->num_strings;
3544 strindex = BufStr_FindFreeString(b);
3547 VM_Warning("VM_bufstr_add: buffer %i has no free string slots in %s\n", bufindex, PRVM_NAME);
3552 while(b->num_strings <= strindex)
3554 if(b->num_strings == MAX_QCSTR_STRINGS)
3556 VM_Warning("VM_bufstr_add: buffer %i has no free string slots in %s\n", bufindex, PRVM_NAME);
3559 b->strings[b->num_strings] = NULL;
3562 if(b->strings[strindex])
3563 Z_Free(b->strings[strindex]);
3564 alloclen = strlen(string) + 1;
3565 b->strings[strindex] = (char *)Z_Malloc(alloclen);
3566 memcpy(b->strings[strindex], string, alloclen);
3567 PRVM_G_FLOAT(OFS_RETURN) = strindex;
3571 ========================
3573 delete string from buffer
3574 void bufstr_free(float bufhandle, float string_index) = #468;
3575 ========================
3577 void VM_bufstr_free (void)
3581 VM_SAFEPARMCOUNT(2, VM_bufstr_free);
3583 b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3586 VM_Warning("VM_bufstr_free: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3589 i = (int)PRVM_G_FLOAT(OFS_PARM1);
3590 if(i < 0 || i > MAX_QCSTR_STRINGS)
3592 VM_Warning("VM_bufstr_free: invalid string index %i used in %s\n", i, PRVM_NAME);
3596 Z_Free(b->strings[i]);
3597 b->strings[i] = NULL;
3598 if(i+1 == b->num_strings)
3608 This was a major timewaster in progs, so it was converted to C
3611 void VM_changeyaw (void)
3614 float ideal, current, move, speed;
3616 // this is called (VERY HACKISHLY) by SV_MoveToGoal, so it can not use any
3617 // parameters because they are the parameters to SV_MoveToGoal, not this
3618 //VM_SAFEPARMCOUNT(0, VM_changeyaw);
3620 ent = PRVM_PROG_TO_EDICT(PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict);
3621 if (ent == prog->edicts)
3623 VM_Warning("changeyaw: can not modify world entity\n");
3626 if (ent->priv.server->free)
3628 VM_Warning("changeyaw: can not modify free entity\n");
3631 if (prog->fieldoffsets.angles < 0 || prog->fieldoffsets.ideal_yaw < 0 || prog->fieldoffsets.yaw_speed < 0)
3633 VM_Warning("changeyaw: angles, ideal_yaw, or yaw_speed field(s) not found\n");
3636 current = ANGLEMOD(PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.angles)->vector[1]);
3637 ideal = PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.ideal_yaw)->_float;
3638 speed = PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.yaw_speed)->_float;
3640 if (current == ideal)
3642 move = ideal - current;
3643 if (ideal > current)
3664 PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.angles)->vector[1] = ANGLEMOD (current + move);
3672 void VM_changepitch (void)
3675 float ideal, current, move, speed;
3677 VM_SAFEPARMCOUNT(1, VM_changepitch);
3679 ent = PRVM_G_EDICT(OFS_PARM0);
3680 if (ent == prog->edicts)
3682 VM_Warning("changepitch: can not modify world entity\n");
3685 if (ent->priv.server->free)
3687 VM_Warning("changepitch: can not modify free entity\n");
3690 if (prog->fieldoffsets.angles < 0 || prog->fieldoffsets.idealpitch < 0 || prog->fieldoffsets.pitch_speed < 0)
3692 VM_Warning("changepitch: angles, idealpitch, or pitch_speed field(s) not found\n");
3695 current = ANGLEMOD(PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.angles)->vector[0]);
3696 ideal = PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.idealpitch)->_float;
3697 speed = PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.pitch_speed)->_float;
3699 if (current == ideal)
3701 move = ideal - current;
3702 if (ideal > current)
3723 PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.angles)->vector[0] = ANGLEMOD (current + move);
3727 static int Is_Text_Color (char c, char t)
3730 char c2 = c - (c & 128);
3731 char t2 = t - (t & 128);
3733 if(c != STRING_COLOR_TAG && c2 != STRING_COLOR_TAG) return 0;
3734 if(t >= '0' && t <= '9') a = 1;
3735 if(t2 >= '0' && t2 <= '9') a = 1;
3736 /* if(t >= 'A' && t <= 'Z') a = 2;
3737 if(t2 >= 'A' && t2 <= 'Z') a = 2;
3739 if(a == 1 && scr_colortext.integer > 0)
3741 if(a == 2 && scr_multifonts.integer > 0)
3747 void VM_uncolorstring (void)
3750 char out[VM_STRINGTEMP_LENGTH];
3753 VM_SAFEPARMCOUNT(1, VM_uncolorstring);
3754 in = PRVM_G_STRING(OFS_PARM0);
3755 VM_CheckEmptyString (in);
3760 if(Is_Text_Color(in[k], in[k+1]) == 1/* || (in[k] == '&' && in[k+1] == 'r')*/)
3769 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(out);
3772 //#222 string(string s, float index) str2chr (FTE_STRINGS)
3773 void VM_str2chr (void)
3776 VM_SAFEPARMCOUNT(2, VM_str2chr);
3777 s = PRVM_G_STRING(OFS_PARM0);
3778 if((unsigned)PRVM_G_FLOAT(OFS_PARM1) > strlen(s))
3780 PRVM_G_FLOAT(OFS_RETURN) = (unsigned char)s[(int)PRVM_G_FLOAT(OFS_PARM1)];
3783 //#223 string(float c, ...) chr2str (FTE_STRINGS)
3784 void VM_chr2str (void)
3788 VM_SAFEPARMCOUNTRANGE(0, 8, VM_chr2str);
3789 for(i = 0;i < prog->argc && i < (int)sizeof(t) - 1;i++)
3790 t[i] = (unsigned char)PRVM_G_FLOAT(OFS_PARM0+i*3);
3792 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(t);
3795 //#228 float(string s1, string s2, float len) strncmp (FTE_STRINGS)
3796 void VM_strncmp (void)
3798 const char *s1, *s2;
3799 VM_SAFEPARMCOUNT(1, VM_strncmp);
3800 s1 = PRVM_G_STRING(OFS_PARM0);
3801 s2 = PRVM_G_STRING(OFS_PARM1);
3802 PRVM_G_FLOAT(OFS_RETURN) = strncmp(s1, s2, (size_t)PRVM_G_FLOAT(OFS_PARM2));
3805 void VM_wasfreed (void)
3807 VM_SAFEPARMCOUNT(1, VM_wasfreed);
3808 PRVM_G_FLOAT(OFS_RETURN) = PRVM_G_EDICT(OFS_PARM0)->priv.required->free;
3811 void VM_SetTraceGlobals(const trace_t *trace)
3814 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_allsolid)))
3815 val->_float = trace->allsolid;
3816 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_startsolid)))
3817 val->_float = trace->startsolid;
3818 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_fraction)))
3819 val->_float = trace->fraction;
3820 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_inwater)))
3821 val->_float = trace->inwater;
3822 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_inopen)))
3823 val->_float = trace->inopen;
3824 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_endpos)))
3825 VectorCopy(trace->endpos, val->vector);
3826 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_plane_normal)))
3827 VectorCopy(trace->plane.normal, val->vector);
3828 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_plane_dist)))
3829 val->_float = trace->plane.dist;
3830 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_ent)))
3831 val->edict = PRVM_EDICT_TO_PROG(trace->ent ? trace->ent : prog->edicts);
3832 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dpstartcontents)))
3833 val->_float = trace->startsupercontents;
3834 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dphitcontents)))
3835 val->_float = trace->hitsupercontents;
3836 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dphitq3surfaceflags)))
3837 val->_float = trace->hitq3surfaceflags;
3838 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dphittexturename)))
3839 val->string = trace->hittexture ? PRVM_SetTempString(trace->hittexture->name) : 0;
3844 void VM_Cmd_Init(void)
3846 // only init the stuff for the current prog
3849 // VM_BufStr_Init();
3852 void VM_Cmd_Reset(void)
3854 CL_PurgeOwner( MENUOWNER );
3856 VM_Files_CloseAll();
3857 // VM_BufStr_ShutDown();