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
9 //============================================================================
12 // temp string handling
13 // LordHavoc: added this to semi-fix the problem of using many ftos calls in a print
14 #define VM_STRINGTEMP_BUFFERS 16
15 #define VM_STRINGTEMP_LENGTH 4096
16 static char vm_string_temp[VM_STRINGTEMP_BUFFERS][VM_STRINGTEMP_LENGTH];
17 static int vm_string_tempindex = 0;
20 #define MAX_VMFILES 256
21 #define MAX_PRVMFILES MAX_VMFILES * PRVM_MAXPROGS
22 #define VM_FILES ((qfile_t**)(vm_files + PRVM_GetProgNr() * MAX_VMFILES))
24 qfile_t *vm_files[MAX_PRVMFILES];
26 // qc fs search handling
27 #define MAX_VMSEARCHES 128
28 #define TOTAL_VMSEARCHES MAX_VMSEARCHES * PRVM_MAXPROGS
29 #define VM_SEARCHLIST ((fssearch_t**)(vm_fssearchlist + PRVM_GetProgNr() * MAX_VMSEARCHES))
31 fssearch_t *vm_fssearchlist[TOTAL_VMSEARCHES];
33 char *VM_GetTempString(void)
36 s = vm_string_temp[vm_string_tempindex];
37 vm_string_tempindex = (vm_string_tempindex + 1) % VM_STRINGTEMP_BUFFERS;
41 void VM_CheckEmptyString (const char *s)
44 PRVM_ERROR ("%s: Bad string", PRVM_NAME);
47 //============================================================================
50 void VM_VarString(int first, char *out, int outlength)
56 outend = out + outlength - 1;
57 for (i = first;i < prog->argc && out < outend;i++)
59 s = PRVM_G_STRING((OFS_PARM0+i*3));
60 while (out < outend && *s)
70 returns true if the extension is supported by the server
72 checkextension(extensionname)
76 // kind of helper function
77 static qboolean checkextension(const char *name)
83 for (e = prog->extensionstring;*e;e++)
90 while (*e && *e != ' ')
93 if (!strncasecmp(start, name, len))
101 void VM_checkextension (void)
103 VM_SAFEPARMCOUNT(1,VM_checkextension);
105 PRVM_G_FLOAT(OFS_RETURN) = checkextension(PRVM_G_STRING(OFS_PARM0));
112 This is a TERMINAL error, which will kill off the entire prog.
121 char string[VM_STRINGTEMP_LENGTH];
123 VM_VarString(0, string, sizeof(string));
124 Con_Printf("======%S ERROR in %s:\n%s\n", PRVM_NAME, PRVM_GetString(prog->xfunction->s_name), string);
127 ed = PRVM_G_EDICT(prog->self->ofs);
131 PRVM_ERROR ("%s: Program error", PRVM_NAME);
138 Dumps out self, then an error message. The program is aborted and self is
139 removed, but the level can continue.
144 void VM_objerror (void)
147 char string[VM_STRINGTEMP_LENGTH];
149 VM_VarString(0, string, sizeof(string));
150 Con_Printf("======%s OBJECT ERROR in %s:\n%s\n", PRVM_NAME, PRVM_GetString(prog->xfunction->s_name), string);
153 ed = PRVM_G_EDICT (prog->self->ofs);
159 // objerror has to display the object fields -> else call
160 PRVM_ERROR ("VM_objecterror: self not defined !\n");
165 VM_print (actually used only by client and menu)
174 char string[VM_STRINGTEMP_LENGTH];
176 VM_VarString(0, string, sizeof(string));
184 broadcast print to everyone on server
189 void VM_bprint (void)
191 char string[VM_STRINGTEMP_LENGTH];
195 Con_Printf("VM_bprint: game is not server(%s) !\n", PRVM_NAME);
199 VM_VarString(0, string, sizeof(string));
200 SV_BroadcastPrint(string);
205 VM_sprint (menu & client but only if server.active == true)
207 single print to a specific client
209 sprint(float clientnum,...[string])
212 void VM_sprint (void)
216 char string[VM_STRINGTEMP_LENGTH];
218 //find client for this entity
219 clientnum = PRVM_G_FLOAT(OFS_PARM0);
220 if (!sv.active || clientnum < 0 || clientnum >= svs.maxclients || !svs.clients[clientnum].active)
222 Con_Printf("VM_sprint: %s: invalid client or server is not active !\n", PRVM_NAME);
226 client = svs.clients + clientnum;
227 VM_VarString(1, string, sizeof(string));
228 MSG_WriteChar(&client->message,svc_print);
229 MSG_WriteString(&client->message, string);
236 single print to the screen
238 centerprint(clientent, value)
241 void VM_centerprint (void)
243 char string[VM_STRINGTEMP_LENGTH];
245 VM_VarString(0, string, sizeof(string));
246 SCR_CenterPrint(string);
253 vector normalize(vector)
256 void VM_normalize (void)
262 VM_SAFEPARMCOUNT(1,VM_normalize);
264 value1 = PRVM_G_VECTOR(OFS_PARM0);
266 new = value1[0] * value1[0] + value1[1] * value1[1] + value1[2]*value1[2];
270 newvalue[0] = newvalue[1] = newvalue[2] = 0;
274 newvalue[0] = value1[0] * new;
275 newvalue[1] = value1[1] * new;
276 newvalue[2] = value1[2] * new;
279 VectorCopy (newvalue, PRVM_G_VECTOR(OFS_RETURN));
294 VM_SAFEPARMCOUNT(1,VM_vlen);
296 value1 = PRVM_G_VECTOR(OFS_PARM0);
298 new = value1[0] * value1[0] + value1[1] * value1[1] + value1[2]*value1[2];
301 PRVM_G_FLOAT(OFS_RETURN) = new;
308 float vectoyaw(vector)
311 void VM_vectoyaw (void)
316 VM_SAFEPARMCOUNT(1,VM_vectoyaw);
318 value1 = PRVM_G_VECTOR(OFS_PARM0);
320 if (value1[1] == 0 && value1[0] == 0)
324 yaw = (int) (atan2(value1[1], value1[0]) * 180 / M_PI);
329 PRVM_G_FLOAT(OFS_RETURN) = yaw;
337 vector vectoangles(vector)
340 void VM_vectoangles (void)
346 VM_SAFEPARMCOUNT(1,VM_vectoangles);
348 value1 = PRVM_G_VECTOR(OFS_PARM0);
350 if (value1[1] == 0 && value1[0] == 0)
360 // LordHavoc: optimized a bit
363 yaw = (atan2(value1[1], value1[0]) * 180 / M_PI);
367 else if (value1[1] > 0)
372 forward = sqrt(value1[0]*value1[0] + value1[1]*value1[1]);
373 pitch = (int) (atan2(value1[2], forward) * 180 / M_PI);
378 PRVM_G_FLOAT(OFS_RETURN+0) = pitch;
379 PRVM_G_FLOAT(OFS_RETURN+1) = yaw;
380 PRVM_G_FLOAT(OFS_RETURN+2) = 0;
387 Returns a number from 0<= num < 1
392 void VM_random (void)
394 VM_SAFEPARMCOUNT(0,VM_random);
396 PRVM_G_FLOAT(OFS_RETURN) = lhrandom(0, 1);
403 Each entity can have eight independant sound sources, like voice,
406 Channel 0 is an auto-allocate channel, the others override anything
407 already running on that entity/channel pair.
409 An attenuation of 0 will play full volume everywhere in the level.
410 Larger attenuations will drop off.
423 entity = G_EDICT(OFS_PARM0);
424 channel = G_FLOAT(OFS_PARM1);
425 sample = G_STRING(OFS_PARM2);
426 volume = G_FLOAT(OFS_PARM3) * 255;
427 attenuation = G_FLOAT(OFS_PARM4);
429 if (volume < 0 || volume > 255)
430 Host_Error ("SV_StartSound: volume = %i", volume);
432 if (attenuation < 0 || attenuation > 4)
433 Host_Error ("SV_StartSound: attenuation = %f", attenuation);
435 if (channel < 0 || channel > 7)
436 Host_Error ("SV_StartSound: channel = %i", channel);
438 SV_StartSound (entity, channel, sample, volume, attenuation);
446 localsound(string sample)
449 void VM_localsound(void)
453 VM_SAFEPARMCOUNT(1,VM_localsound);
455 s = PRVM_G_STRING(OFS_PARM0);
457 if(!S_LocalSound (s))
459 Con_Printf("VM_localsound: Failed to play %s for %s !\n", s, PRVM_NAME);
460 PRVM_G_FLOAT(OFS_RETURN) = -4;
464 PRVM_G_FLOAT(OFS_RETURN) = 1;
476 PRVM_ERROR ("%s: break statement", PRVM_NAME);
479 //============================================================================
485 Sends text over to the client's execution buffer
487 [localcmd (string) or]
491 void VM_localcmd (void)
493 VM_SAFEPARMCOUNT(1,VM_localcmd);
495 Cbuf_AddText(PRVM_G_STRING(OFS_PARM0));
507 VM_SAFEPARMCOUNT(1,VM_cvar);
509 PRVM_G_FLOAT(OFS_RETURN) = Cvar_VariableValue(PRVM_G_STRING(OFS_PARM0));
516 const string VM_cvar_string (string)
519 void VM_cvar_string(void)
523 const char *cvar_string;
524 VM_SAFEPARMCOUNT(1,VM_cvar_string);
526 name = PRVM_G_STRING(OFS_PARM0);
529 PRVM_ERROR("VM_str_cvar: %s: null string\n", PRVM_NAME);
531 VM_CheckEmptyString(name);
533 out = VM_GetTempString();
535 cvar_string = Cvar_VariableString(name);
537 strcpy(out, cvar_string);
539 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(out);
546 void cvar_set (string,string)
549 void VM_cvar_set (void)
551 VM_SAFEPARMCOUNT(2,VM_cvar_set);
553 Cvar_Set(PRVM_G_STRING(OFS_PARM0), PRVM_G_STRING(OFS_PARM1));
563 void VM_dprint (void)
565 char string[VM_STRINGTEMP_LENGTH];
566 if (developer.integer)
568 VM_VarString(0, string, sizeof(string));
569 Con_Printf("%s: %s", PRVM_NAME, string);
586 VM_SAFEPARMCOUNT(1, VM_ftos);
588 v = PRVM_G_FLOAT(OFS_PARM0);
590 s = VM_GetTempString();
591 if ((float)((int)v) == v)
592 sprintf(s, "%i", (int)v);
595 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(s);
610 VM_SAFEPARMCOUNT(1,VM_fabs);
612 v = PRVM_G_FLOAT(OFS_PARM0);
613 PRVM_G_FLOAT(OFS_RETURN) = fabs(v);
628 VM_SAFEPARMCOUNT(1,VM_vtos);
630 s = VM_GetTempString();
631 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]);
632 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(s);
647 VM_SAFEPARMCOUNT(1, VM_etos);
649 s = VM_GetTempString();
650 sprintf (s, "entity %i", PRVM_G_EDICTNUM(OFS_PARM0));
651 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(s);
658 float stof(...[string])
663 char string[VM_STRINGTEMP_LENGTH];
664 VM_VarString(0, string, sizeof(string));
665 PRVM_G_FLOAT(OFS_RETURN) = atof(string);
669 ========================
673 ========================
677 VM_SAFEPARMCOUNT(1, VM_itof);
678 PRVM_G_FLOAT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
682 ========================
686 ========================
691 VM_SAFEPARMCOUNT(1, VM_ftoi);
693 ent = PRVM_G_FLOAT(OFS_PARM0);
694 if(PRVM_PROG_TO_EDICT(ent)->priv.required->free)
695 PRVM_ERROR ("VM_ftoe: %s tried to access a freed entity (entity %i)!\n", PRVM_NAME, ent);
697 PRVM_G_INT(OFS_RETURN) = ent;
711 prog->xfunction->builtinsprofile += 20;
712 ed = PRVM_ED_Alloc();
724 void VM_remove (void)
727 prog->xfunction->builtinsprofile += 20;
729 VM_SAFEPARMCOUNT(1, VM_remove);
731 ed = PRVM_G_EDICT(OFS_PARM0);
732 // if (ed == prog->edicts)
733 // PRVM_ERROR ("remove: tried to remove world\n");
734 // if (PRVM_NUM_FOR_EDICT(ed) <= sv.maxclients)
735 // Host_Error("remove: tried to remove a client\n");
743 entity find(entity start, .string field, string match)
754 VM_SAFEPARMCOUNT(3,VM_find);
756 e = PRVM_G_EDICTNUM(OFS_PARM0);
757 f = PRVM_G_INT(OFS_PARM1);
758 s = PRVM_G_STRING(OFS_PARM2);
762 // return reserved edict 0 (could be used for whatever the prog wants)
763 VM_RETURN_EDICT(prog->edicts);
767 for (e++ ; e < prog->num_edicts ; e++)
769 prog->xfunction->builtinsprofile++;
770 ed = PRVM_EDICT_NUM(e);
771 if (ed->priv.required->free)
773 t = PRVM_E_STRING(ed,f);
783 VM_RETURN_EDICT(prog->edicts);
790 entity findfloat(entity start, .float field, float match)
791 entity findentity(entity start, .entity field, entity match)
794 // LordHavoc: added this for searching float, int, and entity reference fields
795 void VM_findfloat (void)
802 VM_SAFEPARMCOUNT(3,VM_findfloat);
804 e = PRVM_G_EDICTNUM(OFS_PARM0);
805 f = PRVM_G_INT(OFS_PARM1);
806 s = PRVM_G_FLOAT(OFS_PARM2);
808 for (e++ ; e < prog->num_edicts ; e++)
810 prog->xfunction->builtinsprofile++;
811 ed = PRVM_EDICT_NUM(e);
812 if (ed->priv.required->free)
814 if (PRVM_E_FLOAT(ed,f) == s)
821 VM_RETURN_EDICT(prog->edicts);
828 entity findchain(.string field, string match)
831 int PRVM_ED_FindFieldOffset(const char *field);
832 // chained search for strings in entity fields
833 // entity(.string field, string match) findchain = #402;
834 void VM_findchain (void)
840 prvm_edict_t *ent, *chain;
842 VM_SAFEPARMCOUNT(2,VM_findchain);
844 // is the same like !(prog->flag & PRVM_FE_CHAIN) - even if the operator precedence is another
845 if(!prog->flag & PRVM_FE_CHAIN)
846 PRVM_ERROR("VM_findchain: %s doesnt have a chain field !\n", PRVM_NAME);
848 chain_of = PRVM_ED_FindFieldOffset ("chain");
850 chain = prog->edicts;
852 f = PRVM_G_INT(OFS_PARM0);
853 s = PRVM_G_STRING(OFS_PARM1);
856 VM_RETURN_EDICT(prog->edicts);
860 ent = PRVM_NEXT_EDICT(prog->edicts);
861 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
863 prog->xfunction->builtinsprofile++;
864 if (ent->priv.required->free)
866 t = PRVM_E_STRING(ent,f);
872 PRVM_E_INT(ent,chain_of) = PRVM_NUM_FOR_EDICT(chain);
876 VM_RETURN_EDICT(chain);
883 entity findchainfloat(.string field, float match)
884 entity findchainentity(.string field, entity match)
887 // LordHavoc: chained search for float, int, and entity reference fields
888 // entity(.string field, float match) findchainfloat = #403;
889 void VM_findchainfloat (void)
895 prvm_edict_t *ent, *chain;
897 VM_SAFEPARMCOUNT(2, VM_findchainfloat);
899 if(!prog->flag & PRVM_FE_CHAIN)
900 PRVM_ERROR("VM_findchainfloat: %s doesnt have a chain field !\n", PRVM_NAME);
902 chain_of = PRVM_ED_FindFieldOffset ("chain");
904 chain = (prvm_edict_t *)prog->edicts;
906 f = PRVM_G_INT(OFS_PARM0);
907 s = PRVM_G_FLOAT(OFS_PARM1);
909 ent = PRVM_NEXT_EDICT(prog->edicts);
910 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
912 prog->xfunction->builtinsprofile++;
913 if (ent->priv.required->free)
915 if (PRVM_E_FLOAT(ent,f) != s)
918 PRVM_E_INT(ent,chain_of) = PRVM_EDICT_TO_PROG(chain);
922 VM_RETURN_EDICT(chain);
929 string precache_file(string)
932 void VM_precache_file (void)
933 { // precache_file is only used to copy files with qcc, it does nothing
934 VM_SAFEPARMCOUNT(1,VM_precache_file);
936 PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
943 used instead of the other VM_precache_* functions in the builtin list
947 void VM_precache_error (void)
949 PRVM_ERROR ("PF_Precache_*: Precache can only be done in spawn functions");
956 string precache_sound (string sample)
959 void VM_precache_sound (void)
963 VM_SAFEPARMCOUNT(1, VM_precache_sound);
965 s = PRVM_G_STRING(OFS_PARM0);
966 PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
967 VM_CheckEmptyString (s);
969 if(snd_initialized.integer && !S_PrecacheSound (s,true, true))
970 Con_Printf("VM_precache_sound: Failed to load %s for %s\n", s, PRVM_NAME);
980 void VM_coredump (void)
982 VM_SAFEPARMCOUNT(0,VM_coredump);
984 Cbuf_AddText("prvm_edicts ");
985 Cbuf_AddText(PRVM_NAME);
996 void PRVM_StackTrace(void);
997 void VM_stackdump (void)
999 VM_SAFEPARMCOUNT(0, VM_stackdump);
1014 VM_SAFEPARMCOUNT(0, VM_crash);
1016 PRVM_ERROR("Crash called by %s\n",PRVM_NAME);
1026 void VM_traceon (void)
1028 VM_SAFEPARMCOUNT(0,VM_traceon);
1040 void VM_traceoff (void)
1042 VM_SAFEPARMCOUNT(0,VM_traceoff);
1044 prog->trace = false;
1054 void VM_eprint (void)
1056 VM_SAFEPARMCOUNT(1,VM_eprint);
1058 PRVM_ED_PrintNum (PRVM_G_EDICTNUM(OFS_PARM0));
1072 VM_SAFEPARMCOUNT(1,VM_rint);
1074 f = PRVM_G_FLOAT(OFS_PARM0);
1076 PRVM_G_FLOAT(OFS_RETURN) = (int)(f + 0.5);
1078 PRVM_G_FLOAT(OFS_RETURN) = (int)(f - 0.5);
1088 void VM_floor (void)
1090 VM_SAFEPARMCOUNT(1,VM_floor);
1092 PRVM_G_FLOAT(OFS_RETURN) = floor(PRVM_G_FLOAT(OFS_PARM0));
1104 VM_SAFEPARMCOUNT(1,VM_ceil);
1106 PRVM_G_FLOAT(OFS_RETURN) = ceil(PRVM_G_FLOAT(OFS_PARM0));
1114 entity nextent(entity)
1117 void VM_nextent (void)
1122 i = PRVM_G_EDICTNUM(OFS_PARM0);
1125 prog->xfunction->builtinsprofile++;
1127 if (i == prog->num_edicts)
1129 VM_RETURN_EDICT(prog->edicts);
1132 ent = PRVM_EDICT_NUM(i);
1133 if (!ent->priv.required->free)
1135 VM_RETURN_EDICT(ent);
1142 ===============================================================================
1145 used only for client and menu
1146 severs uses VM_SV_...
1148 Write*(* data, float type, float to)
1150 ===============================================================================
1153 #define MSG_BROADCAST 0 // unreliable to all
1154 #define MSG_ONE 1 // reliable to one (msg_entity)
1155 #define MSG_ALL 2 // reliable to all
1156 #define MSG_INIT 3 // write to the init string
1158 sizebuf_t *VM_WriteDest (void)
1164 PRVM_ERROR("VM_WriteDest: game is not server (%s)\n", PRVM_NAME);
1166 dest = G_FLOAT(OFS_PARM1);
1170 return &sv.datagram;
1173 destclient = (int) PRVM_G_FLOAT(OFS_PARM2);
1174 if (destclient < 0 || destclient >= svs.maxclients || !svs.clients[destclient].active)
1175 PRVM_ERROR("VM_clientcommand: %s: invalid client !\n", PRVM_NAME);
1177 return &svs.clients[destclient].message;
1180 return &sv.reliable_datagram;
1186 PRVM_ERROR ("WriteDest: bad destination");
1193 void VM_WriteByte (void)
1195 MSG_WriteByte (VM_WriteDest(), PRVM_G_FLOAT(OFS_PARM0));
1198 void VM_WriteChar (void)
1200 MSG_WriteChar (VM_WriteDest(), PRVM_G_FLOAT(OFS_PARM0));
1203 void VM_WriteShort (void)
1205 MSG_WriteShort (VM_WriteDest(), PRVM_G_FLOAT(OFS_PARM0));
1208 void VM_WriteLong (void)
1210 MSG_WriteLong (VM_WriteDest(), PRVM_G_FLOAT(OFS_PARM0));
1213 void VM_WriteAngle (void)
1215 MSG_WriteAngle (VM_WriteDest(), PRVM_G_FLOAT(OFS_PARM0), sv.protocol);
1218 void VM_WriteCoord (void)
1220 MSG_WriteCoord (VM_WriteDest(), PRVM_G_FLOAT(OFS_PARM0), sv.protocol);
1223 void VM_WriteString (void)
1225 MSG_WriteString (VM_WriteDest(), PRVM_G_STRING(OFS_PARM0));
1228 void VM_WriteEntity (void)
1230 MSG_WriteShort (VM_WriteDest(), PRVM_G_EDICTNUM(OFS_PARM0));
1233 //=============================================================================
1240 changelevel(string map)
1243 void VM_changelevel (void)
1247 VM_SAFEPARMCOUNT(1, VM_changelevel);
1251 Con_Printf("VM_changelevel: game is not server (%s)\n", PRVM_NAME);
1255 // make sure we don't issue two changelevels
1256 if (svs.changelevel_issued)
1258 svs.changelevel_issued = true;
1260 s = PRVM_G_STRING(OFS_PARM0);
1261 Cbuf_AddText (va("changelevel %s\n",s));
1273 VM_SAFEPARMCOUNT(1,VM_sin);
1274 PRVM_G_FLOAT(OFS_RETURN) = sin(PRVM_G_FLOAT(OFS_PARM0));
1285 VM_SAFEPARMCOUNT(1,VM_cos);
1286 PRVM_G_FLOAT(OFS_RETURN) = cos(PRVM_G_FLOAT(OFS_PARM0));
1298 VM_SAFEPARMCOUNT(1,VM_sqrt);
1299 PRVM_G_FLOAT(OFS_RETURN) = sqrt(PRVM_G_FLOAT(OFS_PARM0));
1306 Returns a vector of length < 1 and > 0
1311 void VM_randomvec (void)
1316 VM_SAFEPARMCOUNT(0, VM_randomvec);
1321 temp[0] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1322 temp[1] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1323 temp[2] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1325 while (DotProduct(temp, temp) >= 1);
1326 VectorCopy (temp, PRVM_G_VECTOR(OFS_RETURN));
1329 temp[0] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1330 temp[1] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1331 temp[2] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1332 // length returned always > 0
1333 length = (rand()&32766 + 1) * (1.0 / 32767.0) / VectorLength(temp);
1334 VectorScale(temp,length, temp);*/
1335 //VectorCopy(temp, PRVM_G_VECTOR(OFS_RETURN));
1338 //=============================================================================
1344 float registercvar (string name, string value, float flags)
1347 void VM_registercvar (void)
1349 const char *name, *value;
1352 VM_SAFEPARMCOUNT(3,VM_registercvar);
1354 name = PRVM_G_STRING(OFS_PARM0);
1355 value = PRVM_G_STRING(OFS_PARM1);
1356 flags = PRVM_G_FLOAT(OFS_PARM2);
1357 PRVM_G_FLOAT(OFS_RETURN) = 0;
1359 if(flags > CVAR_MAXFLAGSVAL)
1362 // first check to see if it has already been defined
1363 if (Cvar_FindVar (name))
1366 // check for overlap with a command
1367 if (Cmd_Exists (name))
1369 Con_Printf("VM_registercvar: %s is a command\n", name);
1373 Cvar_Get(name, value, flags);
1375 PRVM_G_FLOAT(OFS_RETURN) = 1; // success
1382 returns the minimum of two supplied floats
1384 float min(float a, float b, ...[float])
1389 // LordHavoc: 3+ argument enhancement suggested by FrikaC
1390 if (prog->argc == 2)
1391 PRVM_G_FLOAT(OFS_RETURN) = min(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1392 else if (prog->argc >= 3)
1395 float f = PRVM_G_FLOAT(OFS_PARM0);
1396 for (i = 1;i < prog->argc;i++)
1397 if (PRVM_G_FLOAT((OFS_PARM0+i*3)) < f)
1398 f = PRVM_G_FLOAT((OFS_PARM0+i*3));
1399 PRVM_G_FLOAT(OFS_RETURN) = f;
1402 PRVM_ERROR("VM_min: %s must supply at least 2 floats\n", PRVM_NAME);
1409 returns the maximum of two supplied floats
1411 float max(float a, float b, ...[float])
1416 // LordHavoc: 3+ argument enhancement suggested by FrikaC
1417 if (prog->argc == 2)
1418 PRVM_G_FLOAT(OFS_RETURN) = max(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1419 else if (prog->argc >= 3)
1422 float f = PRVM_G_FLOAT(OFS_PARM0);
1423 for (i = 1;i < prog->argc;i++)
1424 if (PRVM_G_FLOAT((OFS_PARM0+i*3)) > f)
1425 f = PRVM_G_FLOAT((OFS_PARM0+i*3));
1426 PRVM_G_FLOAT(OFS_RETURN) = f;
1429 PRVM_ERROR("VM_max: %s must supply at least 2 floats\n", PRVM_NAME);
1436 returns number bounded by supplied range
1438 float bound(float min, float value, float max)
1441 void VM_bound (void)
1443 VM_SAFEPARMCOUNT(3,VM_bound);
1444 PRVM_G_FLOAT(OFS_RETURN) = bound(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1), PRVM_G_FLOAT(OFS_PARM2));
1451 returns a raised to power b
1453 float pow(float a, float b)
1458 VM_SAFEPARMCOUNT(2,VM_pow);
1459 PRVM_G_FLOAT(OFS_RETURN) = pow(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1466 copies data from one entity to another
1468 copyentity(entity src, entity dst)
1471 void VM_copyentity (void)
1473 prvm_edict_t *in, *out;
1474 VM_SAFEPARMCOUNT(2,VM_copyentity);
1475 in = PRVM_G_EDICT(OFS_PARM0);
1476 out = PRVM_G_EDICT(OFS_PARM1);
1477 memcpy(out->fields.vp, in->fields.vp, prog->progs->entityfields * 4);
1484 sets the color of a client and broadcasts the update to all connected clients
1486 setcolor(clientent, value)
1489 /*void PF_setcolor (void)
1495 entnum = G_EDICTNUM(OFS_PARM0);
1496 i = G_FLOAT(OFS_PARM1);
1498 if (entnum < 1 || entnum > svs.maxclients || !svs.clients[entnum-1].active)
1500 Con_Print("tried to setcolor a non-client\n");
1504 client = svs.clients + entnum-1;
1505 if ((val = GETEDICTFIELDVALUE(client->edict, eval_clientcolors)))
1508 client->old_colors = i;
1509 client->edict->v->team = (i & 15) + 1;
1511 MSG_WriteByte (&sv.reliable_datagram, svc_updatecolors);
1512 MSG_WriteByte (&sv.reliable_datagram, entnum - 1);
1513 MSG_WriteByte (&sv.reliable_datagram, i);
1516 void VM_Files_Init(void)
1518 memset(VM_FILES, 0, sizeof(qfile_t*[MAX_VMFILES]));
1521 void VM_Files_CloseAll(void)
1524 for (i = 0;i < MAX_VMFILES;i++)
1527 FS_Close(VM_FILES[i]);
1528 //VM_FILES[i] = NULL;
1530 memset(VM_FILES,0,sizeof(qfile_t*[MAX_VMFILES])); // this should be faster (is it ?)
1533 qfile_t *VM_GetFileHandle( int index )
1535 if (index < 0 || index >= MAX_VMFILES)
1537 Con_Printf("VM_GetFileHandle: invalid file handle %i used in %s\n", index, PRVM_NAME);
1540 if (VM_FILES[index] == NULL)
1542 Con_Printf("VM_GetFileHandle: no such file handle %i (or file has been closed) in %s\n", index, PRVM_NAME);
1545 return VM_FILES[index];
1552 float fopen(string filename, float mode)
1555 // float(string filename, float mode) fopen = #110;
1556 // opens a file inside quake/gamedir/data/ (mode is FILE_READ, FILE_APPEND, or FILE_WRITE),
1557 // returns fhandle >= 0 if successful, or fhandle < 0 if unable to open file for any reason
1561 const char *modestring, *filename;
1563 VM_SAFEPARMCOUNT(2,VM_fopen);
1565 for (filenum = 0;filenum < MAX_VMFILES;filenum++)
1566 if (VM_FILES[filenum] == NULL)
1568 if (filenum >= MAX_VMFILES)
1570 Con_Printf("VM_fopen: %s ran out of file handles (%i)\n", PRVM_NAME, MAX_VMFILES);
1571 PRVM_G_FLOAT(OFS_RETURN) = -2;
1574 mode = PRVM_G_FLOAT(OFS_PARM1);
1577 case 0: // FILE_READ
1580 case 1: // FILE_APPEND
1583 case 2: // FILE_WRITE
1587 Con_Printf("VM_fopen: %s no such mode %i (valid: 0 = read, 1 = append, 2 = write)\n", PRVM_NAME, mode);
1588 PRVM_G_FLOAT(OFS_RETURN) = -3;
1591 filename = PRVM_G_STRING(OFS_PARM0);
1592 // .. is parent directory on many platforms
1593 // / is parent directory on Amiga
1594 // : is root of drive on Amiga (also used as a directory separator on Mac, but / works there too, so that's a bad idea)
1595 // \ is a windows-ism (so it's naughty to use it, / works on all platforms)
1596 if ((filename[0] == '.' && filename[1] == '.') || filename[0] == '/' || strrchr(filename, ':') || strrchr(filename, '\\'))
1598 Con_Printf("VM_fopen: %s dangerous or non-portable filename \"%s\" not allowed. (contains : or \\ or begins with .. or /)\n", PRVM_NAME, filename);
1599 PRVM_G_FLOAT(OFS_RETURN) = -4;
1602 VM_FILES[filenum] = FS_Open(va("data/%s", filename), modestring, false, false);
1603 if (VM_FILES[filenum] == NULL && mode == 0)
1604 VM_FILES[filenum] = FS_Open(va("%s", filename), modestring, false, false);
1606 if (VM_FILES[filenum] == NULL)
1607 PRVM_G_FLOAT(OFS_RETURN) = -1;
1609 PRVM_G_FLOAT(OFS_RETURN) = filenum;
1616 fclose(float fhandle)
1619 //void(float fhandle) fclose = #111; // closes a file
1620 void VM_fclose(void)
1624 VM_SAFEPARMCOUNT(1,VM_fclose);
1626 filenum = PRVM_G_FLOAT(OFS_PARM0);
1627 if (filenum < 0 || filenum >= MAX_VMFILES)
1629 Con_Printf("VM_fclose: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1632 if (VM_FILES[filenum] == NULL)
1634 Con_Printf("VM_fclose: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1637 FS_Close(VM_FILES[filenum]);
1638 VM_FILES[filenum] = NULL;
1645 string fgets(float fhandle)
1648 //string(float fhandle) fgets = #112; // reads a line of text from the file and returns as a tempstring
1652 static char string[VM_STRINGTEMP_LENGTH];
1655 VM_SAFEPARMCOUNT(1,VM_fgets);
1657 filenum = PRVM_G_FLOAT(OFS_PARM0);
1658 if (filenum < 0 || filenum >= MAX_VMFILES)
1660 Con_Printf("VM_fgets: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1663 if (VM_FILES[filenum] == NULL)
1665 Con_Printf("VM_fgets: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1671 c = FS_Getc(VM_FILES[filenum]);
1672 if (c == '\r' || c == '\n' || c < 0)
1674 if (end < VM_STRINGTEMP_LENGTH - 1)
1678 // remove \n following \r
1681 c = FS_Getc(VM_FILES[filenum]);
1683 FS_UnGetc(VM_FILES[filenum], (unsigned char)c);
1685 if (developer.integer >= 3)
1686 Con_Printf("fgets: %s: %s\n", PRVM_NAME, string);
1688 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(string);
1690 PRVM_G_INT(OFS_RETURN) = 0;
1697 fputs(float fhandle, string s)
1700 //void(float fhandle, string s) fputs = #113; // writes a line of text to the end of the file
1704 char string[VM_STRINGTEMP_LENGTH];
1707 VM_SAFEPARMCOUNT(2,VM_fputs);
1709 filenum = PRVM_G_FLOAT(OFS_PARM0);
1710 if (filenum < 0 || filenum >= MAX_VMFILES)
1712 Con_Printf("VM_fputs: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1715 if (VM_FILES[filenum] == NULL)
1717 Con_Printf("VM_fputs: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1720 VM_VarString(1, string, sizeof(string));
1721 if ((stringlength = strlen(string)))
1722 FS_Write(VM_FILES[filenum], string, stringlength);
1723 if (developer.integer)
1724 Con_Printf("fputs: %s: %s\n", PRVM_NAME, string);
1731 float strlen(string s)
1734 //float(string s) strlen = #114; // returns how many characters are in a string
1735 void VM_strlen(void)
1739 VM_SAFEPARMCOUNT(1,VM_strlen);
1741 s = PRVM_G_STRING(OFS_PARM0);
1743 PRVM_G_FLOAT(OFS_RETURN) = strlen(s);
1745 PRVM_G_FLOAT(OFS_RETURN) = 0;
1752 string strcat(string,string,...[string])
1755 //string(string s1, string s2) strcat = #115;
1756 // concatenates two strings (for example "abc", "def" would return "abcdef")
1757 // and returns as a tempstring
1758 void VM_strcat(void)
1763 PRVM_ERROR("VM_strcat wrong parameter count (min. 1 expected ) !\n");
1765 s = VM_GetTempString();
1766 VM_VarString(0, s, VM_STRINGTEMP_LENGTH);
1767 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(s);
1774 string substring(string s, float start, float length)
1777 // string(string s, float start, float length) substring = #116;
1778 // returns a section of a string as a tempstring
1779 void VM_substring(void)
1781 int i, start, length;
1785 VM_SAFEPARMCOUNT(3,VM_substring);
1787 string = VM_GetTempString();
1788 s = PRVM_G_STRING(OFS_PARM0);
1789 start = PRVM_G_FLOAT(OFS_PARM1);
1790 length = PRVM_G_FLOAT(OFS_PARM2);
1793 for (i = 0;i < start && *s;i++, s++);
1794 for (i = 0;i < VM_STRINGTEMP_LENGTH - 1 && *s && i < length;i++, s++)
1797 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(string);
1804 vector stov(string s)
1807 //vector(string s) stov = #117; // returns vector value from a string
1810 char string[VM_STRINGTEMP_LENGTH];
1812 VM_SAFEPARMCOUNT(1,VM_stov);
1814 VM_VarString(0, string, sizeof(string));
1815 Math_atov(string, PRVM_G_VECTOR(OFS_RETURN));
1822 string strzone(string s)
1825 //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)
1826 void VM_strzone(void)
1831 VM_SAFEPARMCOUNT(1,VM_strzone);
1833 in = PRVM_G_STRING(OFS_PARM0);
1834 out = PRVM_AllocString(strlen(in) + 1);
1836 PRVM_G_INT(OFS_RETURN) = PRVM_SetQCString(out);
1846 //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!!!)
1847 void VM_strunzone(void)
1849 VM_SAFEPARMCOUNT(1,VM_strunzone);
1850 PRVM_FreeString((char *)PRVM_G_STRING(OFS_PARM0));
1855 VM_command (used by client and menu)
1857 clientcommand(float client, string s) (for client and menu)
1860 //void(entity e, string s) clientcommand = #440; // executes a command string as if it came from the specified client
1861 //this function originally written by KrimZon, made shorter by LordHavoc
1862 void VM_clcommand (void)
1864 client_t *temp_client;
1867 VM_SAFEPARMCOUNT(2,VM_clcommand);
1869 i = PRVM_G_FLOAT(OFS_PARM0);
1870 if (!sv.active || i < 0 || i >= svs.maxclients || !svs.clients[i].active)
1872 Con_Printf("VM_clientcommand: %s: invalid client/server is not active !\n", PRVM_NAME);
1876 temp_client = host_client;
1877 host_client = svs.clients + i;
1878 Cmd_ExecuteString (PRVM_G_STRING(OFS_PARM1), src_client);
1879 host_client = temp_client;
1887 float tokenize(string s)
1890 //float(string s) tokenize = #441;
1891 // takes apart a string into individal words (access them with argv), returns how many
1892 // this function originally written by KrimZon, made shorter by LordHavoc
1893 static char **tokens = NULL;
1894 static int max_tokens, num_tokens = 0;
1895 void VM_tokenize (void)
1897 const char *p, *str;
1899 VM_SAFEPARMCOUNT(1,VM_tokenize);
1901 str = PRVM_G_STRING(OFS_PARM0);
1906 for (i=0;i<num_tokens;i++)
1912 tokens = Z_Malloc(strlen(str) * sizeof(char *));
1913 max_tokens = strlen(str);
1915 for (p = str;COM_ParseToken(&p, false) && num_tokens < max_tokens;num_tokens++)
1917 tokens[num_tokens] = Z_Malloc(strlen(com_token) + 1);
1918 strcpy(tokens[num_tokens], com_token);
1921 PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
1928 string argv(float n)
1931 //string(float n) argv = #442;
1932 // returns a word from the tokenized string (returns nothing for an invalid index)
1933 // this function originally written by KrimZon, made shorter by LordHavoc
1938 VM_SAFEPARMCOUNT(1,VM_argv);
1940 token_num = PRVM_G_FLOAT(OFS_PARM0);
1941 if (token_num >= 0 && token_num < num_tokens)
1942 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(tokens[token_num]);
1944 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(NULL);
1948 //void(entity e, entity tagentity, string tagname) setattachment = #443; // attachs e to a tag on tagentity (note: use "" to attach to entity origin/angles instead of a tag)
1949 void PF_setattachment (void)
1951 edict_t *e = G_EDICT(OFS_PARM0);
1952 edict_t *tagentity = G_EDICT(OFS_PARM1);
1953 char *tagname = G_STRING(OFS_PARM2);
1958 if (tagentity == NULL)
1959 tagentity = sv.edicts;
1961 v = GETEDICTFIELDVALUE(e, eval_tag_entity);
1963 v->edict = EDICT_TO_PROG(tagentity);
1965 v = GETEDICTFIELDVALUE(e, eval_tag_index);
1968 if (tagentity != NULL && tagentity != sv.edicts && tagname && tagname[0])
1970 modelindex = (int)tagentity->v->modelindex;
1971 if (modelindex >= 0 && modelindex < MAX_MODELS)
1973 model = sv.models[modelindex];
1974 if (model->data_overridetagnamesforskin && (unsigned int)tagentity->v->skin < (unsigned int)model->numskins && model->data_overridetagnamesforskin[(unsigned int)tagentity->v->skin].num_overridetagnames)
1975 for (i = 0;i < model->data_overridetagnamesforskin[(unsigned int)tagentity->v->skin].num_overridetagnames;i++)
1976 if (!strcmp(tagname, model->data_overridetagnamesforskin[(unsigned int)tagentity->v->skin].data_overridetagnames[i].name))
1978 // FIXME: use a model function to get tag info (need to handle skeletal)
1979 if (v->_float == 0 && model->num_tags)
1980 for (i = 0;i < model->num_tags;i++)
1981 if (!strcmp(tagname, model->data_tags[i].name))
1984 Con_DPrintf("setattachment(edict %i, edict %i, string \"%s\"): tried to find tag named \"%s\" on entity %i (model \"%s\") but could not find it\n", NUM_FOR_EDICT(e), NUM_FOR_EDICT(tagentity), tagname, tagname, NUM_FOR_EDICT(tagentity), model->name);
1987 Con_DPrintf("setattachment(edict %i, edict %i, string \"%s\"): tried to find tag named \"%s\" on entity %i but it has no model\n", NUM_FOR_EDICT(e), NUM_FOR_EDICT(tagentity), tagname, tagname, NUM_FOR_EDICT(tagentity));
1998 void VM_isserver(void)
2000 VM_SAFEPARMCOUNT(0,VM_serverstate);
2002 PRVM_G_FLOAT(OFS_RETURN) = sv.active;
2012 void VM_clientcount(void)
2014 VM_SAFEPARMCOUNT(0,VM_clientcount);
2016 PRVM_G_FLOAT(OFS_RETURN) = svs.maxclients;
2026 void VM_clientstate(void)
2028 VM_SAFEPARMCOUNT(0,VM_clientstate);
2030 PRVM_G_FLOAT(OFS_RETURN) = cls.state;
2037 float getostype(void)
2039 */ // not used at the moment -> not included in the common list
2040 void VM_getostype(void)
2042 VM_SAFEPARMCOUNT(0,VM_getostype);
2047 OS_MAC - not supported
2051 PRVM_G_FLOAT(OFS_RETURN) = 0;
2053 PRVM_G_FLOAT(OFS_RETURN) = 2;
2055 PRVM_G_FLOAT(OFS_RETURN) = 1;
2063 vector getmousepos()
2066 void VM_getmousepos(void)
2069 VM_SAFEPARMCOUNT(0,VM_getmousepos);
2071 PRVM_G_VECTOR(OFS_RETURN)[0] = in_mouse_x * vid_conwidth.integer / vid.width;
2072 PRVM_G_VECTOR(OFS_RETURN)[1] = in_mouse_y * vid_conheight.integer / vid.height;
2073 PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
2083 void VM_gettime(void)
2085 VM_SAFEPARMCOUNT(0,VM_gettime);
2087 PRVM_G_FLOAT(OFS_RETURN) = (float) *prog->time;
2094 loadfromdata(string data)
2097 void VM_loadfromdata(void)
2099 VM_SAFEPARMCOUNT(1,VM_loadentsfromfile);
2101 PRVM_ED_LoadFromFile(PRVM_G_STRING(OFS_PARM0));
2105 ========================
2108 parseentitydata(entity ent, string data)
2109 ========================
2111 void VM_parseentitydata(void)
2116 VM_SAFEPARMCOUNT(2, VM_parseentitydata);
2118 // get edict and test it
2119 ent = PRVM_G_EDICT(OFS_PARM0);
2120 if (ent->priv.required->free)
2121 PRVM_ERROR ("VM_parseentitydata: %s: Can only set already spawned entities (entity %i is free)!\n", PRVM_NAME, PRVM_NUM_FOR_EDICT(ent));
2123 data = PRVM_G_STRING(OFS_PARM1);
2125 // parse the opening brace
2126 if (!COM_ParseToken(&data, false) || com_token[0] != '{' )
2127 PRVM_ERROR ("VM_parseentitydata: %s: Couldn't parse entity data:\n%s\n", PRVM_NAME, data );
2129 PRVM_ED_ParseEdict (data, ent);
2136 loadfromfile(string file)
2139 void VM_loadfromfile(void)
2141 const char *filename;
2144 VM_SAFEPARMCOUNT(1,VM_loadfromfile);
2146 filename = PRVM_G_STRING(OFS_PARM0);
2147 // .. is parent directory on many platforms
2148 // / is parent directory on Amiga
2149 // : is root of drive on Amiga (also used as a directory separator on Mac, but / works there too, so that's a bad idea)
2150 // \ is a windows-ism (so it's naughty to use it, / works on all platforms)
2151 if ((filename[0] == '.' && filename[1] == '.') || filename[0] == '/' || strrchr(filename, ':') || strrchr(filename, '\\'))
2153 Con_Printf("VM_loadfromfile: %s dangerous or non-portable filename \"%s\" not allowed. (contains : or \\ or begins with .. or /)\n", PRVM_NAME, filename);
2154 PRVM_G_FLOAT(OFS_RETURN) = -4;
2158 // not conform with VM_fopen
2159 data = FS_LoadFile(filename, tempmempool, false);
2161 PRVM_G_FLOAT(OFS_RETURN) = -1;
2163 PRVM_ED_LoadFromFile(data);
2174 float mod(float val, float m)
2177 void VM_modulo(void)
2180 VM_SAFEPARMCOUNT(2,VM_module);
2182 val = (int) PRVM_G_FLOAT(OFS_PARM0);
2183 m = (int) PRVM_G_FLOAT(OFS_PARM1);
2185 PRVM_G_FLOAT(OFS_RETURN) = (float) (val % m);
2188 void VM_Search_Init(void)
2190 memset(VM_SEARCHLIST,0,sizeof(fssearch_t*[MAX_VMSEARCHES]));
2193 void VM_Search_Reset(void)
2196 // reset the fssearch list
2197 for(i = 0; i < MAX_VMSEARCHES; i++)
2198 if(VM_SEARCHLIST[i])
2199 FS_FreeSearch(VM_SEARCHLIST[i]);
2200 memset(VM_SEARCHLIST,0,sizeof(fssearch_t*[MAX_VMSEARCHES]));
2207 float search_begin(string pattern, float caseinsensitive, float quiet)
2210 void VM_search_begin(void)
2213 const char *pattern;
2214 int caseinsens, quiet;
2216 VM_SAFEPARMCOUNT(3, VM_search_begin);
2218 pattern = PRVM_G_STRING(OFS_PARM0);
2220 VM_CheckEmptyString(pattern);
2222 caseinsens = PRVM_G_FLOAT(OFS_PARM1);
2223 quiet = PRVM_G_FLOAT(OFS_PARM2);
2225 for(handle = 0; handle < MAX_VMSEARCHES; handle++)
2226 if(!VM_SEARCHLIST[handle])
2229 if(handle >= MAX_VMSEARCHES)
2231 Con_Printf("VM_search_begin: %s ran out of search handles (%i)\n", PRVM_NAME, MAX_VMSEARCHES);
2232 PRVM_G_FLOAT(OFS_RETURN) = -2;
2236 if(!(VM_SEARCHLIST[handle] = FS_Search(pattern,caseinsens, quiet)))
2237 PRVM_G_FLOAT(OFS_RETURN) = -1;
2239 PRVM_G_FLOAT(OFS_RETURN) = handle;
2246 void search_end(float handle)
2249 void VM_search_end(void)
2252 VM_SAFEPARMCOUNT(1, VM_search_end);
2254 handle = PRVM_G_FLOAT(OFS_PARM0);
2256 if(handle < 0 || handle >= MAX_VMSEARCHES)
2258 Con_Printf("VM_search_end: invalid handle %i used in %s\n", handle, PRVM_NAME);
2261 if(VM_SEARCHLIST[handle] == NULL)
2263 Con_Printf("VM_search_end: no such handle %i in %s\n", handle, PRVM_NAME);
2267 FS_FreeSearch(VM_SEARCHLIST[handle]);
2268 VM_SEARCHLIST[handle] = NULL;
2275 float search_getsize(float handle)
2278 void VM_search_getsize(void)
2281 VM_SAFEPARMCOUNT(1, VM_M_search_getsize);
2283 handle = PRVM_G_FLOAT(OFS_PARM0);
2285 if(handle < 0 || handle >= MAX_VMSEARCHES)
2287 Con_Printf("VM_search_getsize: invalid handle %i used in %s\n", handle, PRVM_NAME);
2290 if(VM_SEARCHLIST[handle] == NULL)
2292 Con_Printf("VM_search_getsize: no such handle %i in %s\n", handle, PRVM_NAME);
2296 PRVM_G_FLOAT(OFS_RETURN) = VM_SEARCHLIST[handle]->numfilenames;
2301 VM_search_getfilename
2303 string search_getfilename(float handle, float num)
2306 void VM_search_getfilename(void)
2308 int handle, filenum;
2310 VM_SAFEPARMCOUNT(2, VM_search_getfilename);
2312 handle = PRVM_G_FLOAT(OFS_PARM0);
2313 filenum = PRVM_G_FLOAT(OFS_PARM1);
2315 if(handle < 0 || handle >= MAX_VMSEARCHES)
2317 Con_Printf("VM_search_getfilename: invalid handle %i used in %s\n", handle, PRVM_NAME);
2320 if(VM_SEARCHLIST[handle] == NULL)
2322 Con_Printf("VM_search_getfilename: no such handle %i in %s\n", handle, PRVM_NAME);
2325 if(filenum < 0 || filenum >= VM_SEARCHLIST[handle]->numfilenames)
2327 Con_Printf("VM_search_getfilename: invalid filenum %i in %s\n", filenum, PRVM_NAME);
2331 tmp = VM_GetTempString();
2332 strcpy(tmp, VM_SEARCHLIST[handle]->filenames[filenum]);
2334 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(tmp);
2341 string chr(float ascii)
2347 VM_SAFEPARMCOUNT(1, VM_chr);
2349 tmp = VM_GetTempString();
2350 tmp[0] = (unsigned char) PRVM_G_FLOAT(OFS_PARM0);
2353 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(tmp);
2356 //=============================================================================
2357 // Draw builtins (client & menu)
2363 float iscachedpic(string pic)
2366 void VM_iscachedpic(void)
2368 VM_SAFEPARMCOUNT(1,VM_iscachedpic);
2370 // drawq hasnt such a function, thus always return true
2371 PRVM_G_FLOAT(OFS_RETURN) = false;
2378 string precache_pic(string pic)
2381 void VM_precache_pic(void)
2385 VM_SAFEPARMCOUNT(1, VM_precache_pic);
2387 s = PRVM_G_STRING(OFS_PARM0);
2388 PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
2391 PRVM_ERROR ("VM_precache_pic: %s: NULL\n", PRVM_NAME);
2393 VM_CheckEmptyString (s);
2395 // AK Draw_CachePic is supposed to always return a valid pointer
2396 if( Draw_CachePic(s, false)->tex == r_texture_notexture )
2397 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(NULL);
2407 void VM_freepic(void)
2411 VM_SAFEPARMCOUNT(1,VM_freepic);
2413 s = PRVM_G_STRING(OFS_PARM0);
2416 PRVM_ERROR ("VM_freepic: %s: NULL\n");
2418 VM_CheckEmptyString (s);
2427 float drawcharacter(vector position, float character, vector scale, vector rgb, float alpha, float flag)
2430 void VM_drawcharacter(void)
2432 float *pos,*scale,*rgb;
2435 VM_SAFEPARMCOUNT(6,VM_drawcharacter);
2437 character = (char) PRVM_G_FLOAT(OFS_PARM1);
2440 Con_Printf("VM_drawcharacter: %s passed null character !\n",PRVM_NAME);
2441 PRVM_G_FLOAT(OFS_RETURN) = -1;
2445 pos = PRVM_G_VECTOR(OFS_PARM0);
2446 scale = PRVM_G_VECTOR(OFS_PARM2);
2447 rgb = PRVM_G_VECTOR(OFS_PARM3);
2448 flag = (int)PRVM_G_FLOAT(OFS_PARM5);
2450 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2452 Con_Printf("VM_drawcharacter: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2453 PRVM_G_FLOAT(OFS_RETURN) = -2;
2457 if(pos[2] || scale[2])
2458 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")));
2460 if(!scale[0] || !scale[1])
2462 Con_Printf("VM_drawcharacter: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
2463 PRVM_G_FLOAT(OFS_RETURN) = -3;
2467 DrawQ_String (pos[0], pos[1], &character, 1, scale[0], scale[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM4), flag);
2468 PRVM_G_FLOAT(OFS_RETURN) = 1;
2475 float drawstring(vector position, string text, vector scale, vector rgb, float alpha, float flag)
2478 void VM_drawstring(void)
2480 float *pos,*scale,*rgb;
2483 VM_SAFEPARMCOUNT(6,VM_drawstring);
2485 string = PRVM_G_STRING(OFS_PARM1);
2488 Con_Printf("VM_drawstring: %s passed null string !\n",PRVM_NAME);
2489 PRVM_G_FLOAT(OFS_RETURN) = -1;
2493 //VM_CheckEmptyString(string); Why should it be checked - perhaps the menu wants to support the precolored letters, too?
2495 pos = PRVM_G_VECTOR(OFS_PARM0);
2496 scale = PRVM_G_VECTOR(OFS_PARM2);
2497 rgb = PRVM_G_VECTOR(OFS_PARM3);
2498 flag = (int)PRVM_G_FLOAT(OFS_PARM5);
2500 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2502 Con_Printf("VM_drawstring: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2503 PRVM_G_FLOAT(OFS_RETURN) = -2;
2507 if(!scale[0] || !scale[1])
2509 Con_Printf("VM_drawstring: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
2510 PRVM_G_FLOAT(OFS_RETURN) = -3;
2514 if(pos[2] || scale[2])
2515 Con_Printf("VM_drawstring: z value%c from %s discarded\n",(pos[2] && scale[2]) ? 's' : 0,((pos[2] && scale[2]) ? "pos and scale" : (pos[2] ? "pos" : "scale")));
2517 DrawQ_String (pos[0], pos[1], string, 0, scale[0], scale[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM4), flag);
2518 PRVM_G_FLOAT(OFS_RETURN) = 1;
2524 float drawpic(vector position, string pic, vector size, vector rgb, float alpha, float flag)
2527 void VM_drawpic(void)
2530 float *size, *pos, *rgb;
2533 VM_SAFEPARMCOUNT(6,VM_drawpic);
2535 pic = PRVM_G_STRING(OFS_PARM1);
2539 Con_Printf("VM_drawpic: %s passed null picture name !\n", PRVM_NAME);
2540 PRVM_G_FLOAT(OFS_RETURN) = -1;
2544 VM_CheckEmptyString (pic);
2546 // is pic cached ? no function yet for that
2549 Con_Printf("VM_drawpic: %s: %s not cached !\n", PRVM_NAME, pic);
2550 PRVM_G_FLOAT(OFS_RETURN) = -4;
2554 pos = PRVM_G_VECTOR(OFS_PARM0);
2555 size = PRVM_G_VECTOR(OFS_PARM2);
2556 rgb = PRVM_G_VECTOR(OFS_PARM3);
2557 flag = (int) PRVM_G_FLOAT(OFS_PARM5);
2559 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2561 Con_Printf("VM_drawstring: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2562 PRVM_G_FLOAT(OFS_RETURN) = -2;
2566 if(pos[2] || size[2])
2567 Con_Printf("VM_drawstring: z value%c from %s discarded\n",(pos[2] && size[2]) ? 's' : 0,((pos[2] && size[2]) ? "pos and size" : (pos[2] ? "pos" : "size")));
2569 DrawQ_Pic(pos[0], pos[1], pic, size[0], size[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM4), flag);
2570 PRVM_G_FLOAT(OFS_RETURN) = 1;
2577 float drawfill(vector position, vector size, vector rgb, float alpha, float flag)
2580 void VM_drawfill(void)
2582 float *size, *pos, *rgb;
2585 VM_SAFEPARMCOUNT(5,VM_drawfill);
2588 pos = PRVM_G_VECTOR(OFS_PARM0);
2589 size = PRVM_G_VECTOR(OFS_PARM1);
2590 rgb = PRVM_G_VECTOR(OFS_PARM2);
2591 flag = (int) PRVM_G_FLOAT(OFS_PARM4);
2593 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2595 Con_Printf("VM_drawstring: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2596 PRVM_G_FLOAT(OFS_RETURN) = -2;
2600 if(pos[2] || size[2])
2601 Con_Printf("VM_drawstring: z value%c from %s discarded\n",(pos[2] && size[2]) ? 's' : 0,((pos[2] && size[2]) ? "pos and size" : (pos[2] ? "pos" : "size")));
2603 DrawQ_Pic(pos[0], pos[1], 0, size[0], size[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM3), flag);
2604 PRVM_G_FLOAT(OFS_RETURN) = 1;
2611 drawsetcliparea(float x, float y, float width, float height)
2614 void VM_drawsetcliparea(void)
2617 VM_SAFEPARMCOUNT(4,VM_drawsetcliparea);
2619 x = bound(0, PRVM_G_FLOAT(OFS_PARM0), vid_conwidth.integer);
2620 y = bound(0, PRVM_G_FLOAT(OFS_PARM1), vid_conheight.integer);
2621 w = bound(0, PRVM_G_FLOAT(OFS_PARM2) + PRVM_G_FLOAT(OFS_PARM0) - x, (vid_conwidth.integer - x));
2622 h = bound(0, PRVM_G_FLOAT(OFS_PARM3) + PRVM_G_FLOAT(OFS_PARM1) - y, (vid_conheight.integer - y));
2624 DrawQ_SetClipArea(x, y, w, h);
2629 VM_drawresetcliparea
2634 void VM_drawresetcliparea(void)
2636 VM_SAFEPARMCOUNT(0,VM_drawresetcliparea);
2638 DrawQ_ResetClipArea();
2645 vector getimagesize(string pic)
2648 void VM_getimagesize(void)
2653 VM_SAFEPARMCOUNT(1,VM_getimagesize);
2655 p = PRVM_G_STRING(OFS_PARM0);
2658 PRVM_ERROR("VM_getimagepos: %s passed null picture name !\n", PRVM_NAME);
2660 VM_CheckEmptyString (p);
2662 pic = Draw_CachePic (p, false);
2664 PRVM_G_VECTOR(OFS_RETURN)[0] = pic->width;
2665 PRVM_G_VECTOR(OFS_RETURN)[1] = pic->height;
2666 PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
2669 // CL_Video interface functions
2672 ========================
2675 float cin_open(string file, string name)
2676 ========================
2678 void VM_cin_open( void )
2683 VM_SAFEPARMCOUNT( 2, VM_cin_open );
2685 file = PRVM_G_STRING( OFS_PARM0 );
2686 name = PRVM_G_STRING( OFS_PARM1 );
2688 VM_CheckEmptyString( file );
2689 VM_CheckEmptyString( name );
2691 if( CL_OpenVideo( file, name, MENUOWNER ) )
2692 PRVM_G_FLOAT( OFS_RETURN ) = 1;
2694 PRVM_G_FLOAT( OFS_RETURN ) = 0;
2698 ========================
2701 void cin_close(string name)
2702 ========================
2704 void VM_cin_close( void )
2708 VM_SAFEPARMCOUNT( 1, VM_cin_close );
2710 name = PRVM_G_STRING( OFS_PARM0 );
2711 VM_CheckEmptyString( name );
2713 CL_CloseVideo( CL_GetVideo( name ) );
2717 ========================
2719 void cin_setstate(string name, float type)
2720 ========================
2722 void VM_cin_setstate( void )
2725 clvideostate_t state;
2728 VM_SAFEPARMCOUNT( 2, VM_cin_netstate );
2730 name = PRVM_G_STRING( OFS_PARM0 );
2731 VM_CheckEmptyString( name );
2733 state = PRVM_G_FLOAT( OFS_PARM1 );
2735 video = CL_GetVideo( name );
2736 if( video && state > CLVIDEO_UNUSED && state < CLVIDEO_STATECOUNT )
2737 CL_SetVideoState( video, state );
2741 ========================
2744 float cin_getstate(string name)
2745 ========================
2747 void VM_cin_getstate( void )
2752 VM_SAFEPARMCOUNT( 1, VM_cin_getstate );
2754 name = PRVM_G_STRING( OFS_PARM0 );
2755 VM_CheckEmptyString( name );
2757 video = CL_GetVideo( name );
2759 PRVM_G_FLOAT( OFS_RETURN ) = (int)video->state;
2761 PRVM_G_FLOAT( OFS_RETURN ) = 0;
2765 ========================
2768 void cin_restart(string name)
2769 ========================
2771 void VM_cin_restart( void )
2776 VM_SAFEPARMCOUNT( 1, VM_cin_restart );
2778 name = PRVM_G_STRING( OFS_PARM0 );
2779 VM_CheckEmptyString( name );
2781 video = CL_GetVideo( name );
2783 CL_RestartVideo( video );
2786 ////////////////////////////////////////
2787 // AltString functions
2788 ////////////////////////////////////////
2791 ========================
2794 float altstr_count(string)
2795 ========================
2797 void VM_altstr_count( void )
2799 const char *altstr, *pos;
2802 VM_SAFEPARMCOUNT( 1, VM_altstr_count );
2804 altstr = PRVM_G_STRING( OFS_PARM0 );
2805 //VM_CheckEmptyString( altstr );
2807 for( count = 0, pos = altstr ; *pos ; pos++ ) {
2808 if( *pos == '\\' ) {
2812 } else if( *pos == '\'' ) {
2817 PRVM_G_FLOAT( OFS_RETURN ) = (float) (count / 2);
2821 ========================
2824 string altstr_prepare(string)
2825 ========================
2827 void VM_altstr_prepare( void )
2830 const char *instr, *in;
2833 VM_SAFEPARMCOUNT( 1, VM_altstr_prepare );
2835 instr = PRVM_G_STRING( OFS_PARM0 );
2836 //VM_CheckEmptyString( instr );
2837 outstr = VM_GetTempString();
2839 for( out = outstr, in = instr, size = VM_STRINGTEMP_LENGTH - 1 ; size && *in ; size--, in++, out++ )
2848 PRVM_G_INT( OFS_RETURN ) = PRVM_SetEngineString( outstr );
2852 ========================
2855 string altstr_get(string, float)
2856 ========================
2858 void VM_altstr_get( void )
2860 const char *altstr, *pos;
2864 VM_SAFEPARMCOUNT( 2, VM_altstr_get );
2866 altstr = PRVM_G_STRING( OFS_PARM0 );
2867 //VM_CheckEmptyString( altstr );
2869 count = PRVM_G_FLOAT( OFS_PARM1 );
2870 count = count * 2 + 1;
2872 for( pos = altstr ; *pos && count ; pos++ )
2873 if( *pos == '\\' ) {
2876 } else if( *pos == '\'' )
2880 PRVM_G_INT( OFS_RETURN ) = PRVM_SetEngineString( NULL );
2884 outstr = VM_GetTempString();
2885 for( out = outstr, size = VM_STRINGTEMP_LENGTH - 1 ; size && *pos ; size--, pos++, out++ )
2886 if( *pos == '\\' ) {
2891 } else if( *pos == '\'' )
2897 PRVM_G_INT( OFS_RETURN ) = PRVM_SetEngineString( outstr );
2901 ========================
2904 string altstr_set(string altstr, float num, string set)
2905 ========================
2907 void VM_altstr_set( void )
2910 const char *altstr, *str;
2914 VM_SAFEPARMCOUNT( 3, VM_altstr_set );
2916 altstr = PRVM_G_STRING( OFS_PARM0 );
2917 //VM_CheckEmptyString( altstr );
2919 num = PRVM_G_FLOAT( OFS_PARM1 );
2921 str = PRVM_G_STRING( OFS_PARM2 );
2922 //VM_CheckEmptyString( str );
2924 outstr = out = VM_GetTempString();
2925 for( num = num * 2 + 1, in = altstr; *in && num; *out++ = *in++ )
2930 } else if( *in == '\'' ) {
2935 PRVM_G_INT( OFS_RETURN ) = PRVM_SetEngineString( altstr );
2939 for( ; *str; *out++ = *str++ );
2940 // now jump over the old content
2942 if( *in == '\'' || (*in == '\\' && !*++in) )
2946 PRVM_G_INT( OFS_RETURN ) = PRVM_SetEngineString( NULL );
2951 PRVM_G_INT( OFS_RETURN ) = PRVM_SetEngineString( outstr );
2955 ========================
2958 string altstr_ins(string altstr, float num, string set)
2959 ========================
2961 void VM_altstr_ins(void)
2971 in = instr = PRVM_G_STRING( OFS_PARM0 );
2972 num = PRVM_G_FLOAT( OFS_PARM1 );
2973 set = setstr = PRVM_G_STRING( OFS_PARM2 );
2975 out = outstr = VM_GetTempString();
2976 for( num = num * 2 + 2 ; *in && num > 0 ; *out++ = *in++ )
2981 } else if( *in == '\'' ) {
2986 for( ; *set ; *out++ = *set++ );
2990 PRVM_G_INT( OFS_RETURN ) = PRVM_SetEngineString( outstr );
2993 void VM_Cmd_Init(void)
2995 // only init the stuff for the current prog
3000 void VM_Cmd_Reset(void)
3002 CL_PurgeOwner( MENUOWNER );
3004 VM_Files_CloseAll();