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 static char vm_string_temp[VM_STRINGTEMP_BUFFERS][VM_STRINGTEMP_LENGTH];
15 static int vm_string_tempindex = 0;
18 #define MAX_VMFILES 256
19 #define MAX_PRVMFILES MAX_VMFILES * PRVM_MAXPROGS
20 #define VM_FILES ((qfile_t**)(vm_files + PRVM_GetProgNr() * MAX_VMFILES))
22 qfile_t *vm_files[MAX_PRVMFILES];
24 // qc fs search handling
25 #define MAX_VMSEARCHES 128
26 #define TOTAL_VMSEARCHES MAX_VMSEARCHES * PRVM_MAXPROGS
27 #define VM_SEARCHLIST ((fssearch_t**)(vm_fssearchlist + PRVM_GetProgNr() * MAX_VMSEARCHES))
29 fssearch_t *vm_fssearchlist[TOTAL_VMSEARCHES];
31 char *VM_GetTempString(void)
34 s = vm_string_temp[vm_string_tempindex];
35 vm_string_tempindex = (vm_string_tempindex + 1) % VM_STRINGTEMP_BUFFERS;
39 void VM_CheckEmptyString (const char *s)
42 PRVM_ERROR ("%s: Bad string", PRVM_NAME);
45 //============================================================================
48 void VM_VarString(int first, char *out, int outlength)
54 outend = out + outlength - 1;
55 for (i = first;i < prog->argc && out < outend;i++)
57 s = PRVM_G_STRING((OFS_PARM0+i*3));
58 while (out < outend && *s)
68 returns true if the extension is supported by the server
70 checkextension(extensionname)
74 // kind of helper function
75 static qboolean checkextension(const char *name)
79 len = (int)strlen(name);
81 for (e = prog->extensionstring;*e;e++)
88 while (*e && *e != ' ')
90 if ((e - start) == len && !strncasecmp(start, name, len))
96 void VM_checkextension (void)
98 VM_SAFEPARMCOUNT(1,VM_checkextension);
100 PRVM_G_FLOAT(OFS_RETURN) = checkextension(PRVM_G_STRING(OFS_PARM0));
107 This is a TERMINAL error, which will kill off the entire prog.
116 char string[VM_STRINGTEMP_LENGTH];
118 VM_VarString(0, string, sizeof(string));
119 Con_Printf("======%S ERROR in %s:\n%s\n", PRVM_NAME, PRVM_GetString(prog->xfunction->s_name), string);
122 ed = PRVM_G_EDICT(prog->self->ofs);
126 PRVM_ERROR ("%s: Program error", PRVM_NAME);
133 Dumps out self, then an error message. The program is aborted and self is
134 removed, but the level can continue.
139 void VM_objerror (void)
142 char string[VM_STRINGTEMP_LENGTH];
144 VM_VarString(0, string, sizeof(string));
145 Con_Printf("======%s OBJECT ERROR in %s:\n%s\n", PRVM_NAME, PRVM_GetString(prog->xfunction->s_name), string);
148 ed = PRVM_G_EDICT (prog->self->ofs);
154 // objerror has to display the object fields -> else call
155 PRVM_ERROR ("VM_objecterror: self not defined !\n");
160 VM_print (actually used only by client and menu)
169 char string[VM_STRINGTEMP_LENGTH];
171 VM_VarString(0, string, sizeof(string));
179 broadcast print to everyone on server
184 void VM_bprint (void)
186 char string[VM_STRINGTEMP_LENGTH];
190 Con_Printf("VM_bprint: game is not server(%s) !\n", PRVM_NAME);
194 VM_VarString(0, string, sizeof(string));
195 SV_BroadcastPrint(string);
200 VM_sprint (menu & client but only if server.active == true)
202 single print to a specific client
204 sprint(float clientnum,...[string])
207 void VM_sprint (void)
211 char string[VM_STRINGTEMP_LENGTH];
213 //find client for this entity
214 clientnum = PRVM_G_FLOAT(OFS_PARM0);
215 if (!sv.active || clientnum < 0 || clientnum >= svs.maxclients || !svs.clients[clientnum].active)
217 Con_Printf("VM_sprint: %s: invalid client or server is not active !\n", PRVM_NAME);
221 client = svs.clients + clientnum;
222 VM_VarString(1, string, sizeof(string));
223 MSG_WriteChar(&client->message,svc_print);
224 MSG_WriteString(&client->message, string);
231 single print to the screen
233 centerprint(clientent, value)
236 void VM_centerprint (void)
238 char string[VM_STRINGTEMP_LENGTH];
240 VM_VarString(0, string, sizeof(string));
241 SCR_CenterPrint(string);
248 vector normalize(vector)
251 void VM_normalize (void)
257 VM_SAFEPARMCOUNT(1,VM_normalize);
259 value1 = PRVM_G_VECTOR(OFS_PARM0);
261 new = value1[0] * value1[0] + value1[1] * value1[1] + value1[2]*value1[2];
265 newvalue[0] = newvalue[1] = newvalue[2] = 0;
269 newvalue[0] = value1[0] * new;
270 newvalue[1] = value1[1] * new;
271 newvalue[2] = value1[2] * new;
274 VectorCopy (newvalue, PRVM_G_VECTOR(OFS_RETURN));
289 VM_SAFEPARMCOUNT(1,VM_vlen);
291 value1 = PRVM_G_VECTOR(OFS_PARM0);
293 new = value1[0] * value1[0] + value1[1] * value1[1] + value1[2]*value1[2];
296 PRVM_G_FLOAT(OFS_RETURN) = new;
303 float vectoyaw(vector)
306 void VM_vectoyaw (void)
311 VM_SAFEPARMCOUNT(1,VM_vectoyaw);
313 value1 = PRVM_G_VECTOR(OFS_PARM0);
315 if (value1[1] == 0 && value1[0] == 0)
319 yaw = (int) (atan2(value1[1], value1[0]) * 180 / M_PI);
324 PRVM_G_FLOAT(OFS_RETURN) = yaw;
332 vector vectoangles(vector)
335 void VM_vectoangles (void)
341 VM_SAFEPARMCOUNT(1,VM_vectoangles);
343 value1 = PRVM_G_VECTOR(OFS_PARM0);
345 if (value1[1] == 0 && value1[0] == 0)
355 // LordHavoc: optimized a bit
358 yaw = (atan2(value1[1], value1[0]) * 180 / M_PI);
362 else if (value1[1] > 0)
367 forward = sqrt(value1[0]*value1[0] + value1[1]*value1[1]);
368 pitch = (atan2(value1[2], forward) * 180 / M_PI);
373 PRVM_G_FLOAT(OFS_RETURN+0) = pitch;
374 PRVM_G_FLOAT(OFS_RETURN+1) = yaw;
375 PRVM_G_FLOAT(OFS_RETURN+2) = 0;
382 Returns a number from 0<= num < 1
387 void VM_random (void)
389 VM_SAFEPARMCOUNT(0,VM_random);
391 PRVM_G_FLOAT(OFS_RETURN) = lhrandom(0, 1);
398 Each entity can have eight independant sound sources, like voice,
401 Channel 0 is an auto-allocate channel, the others override anything
402 already running on that entity/channel pair.
404 An attenuation of 0 will play full volume everywhere in the level.
405 Larger attenuations will drop off.
414 prvm_edict_t *entity;
418 entity = PRVM_G_EDICT(OFS_PARM0);
419 channel = PRVM_G_FLOAT(OFS_PARM1);
420 sample = PRVM_G_STRING(OFS_PARM2);
421 volume = PRVM_G_FLOAT(OFS_PARM3) * 255;
422 attenuation = PRVM_G_FLOAT(OFS_PARM4);
424 if (volume < 0 || volume > 255)
425 Host_Error ("SV_StartSound: volume = %i", volume);
427 if (attenuation < 0 || attenuation > 4)
428 Host_Error ("SV_StartSound: attenuation = %f", attenuation);
430 if (channel < 0 || channel > 7)
431 Host_Error ("SV_StartSound: channel = %i", channel);
433 SV_StartSound (entity, channel, sample, volume, attenuation);
441 localsound(string sample)
444 void VM_localsound(void)
448 VM_SAFEPARMCOUNT(1,VM_localsound);
450 s = PRVM_G_STRING(OFS_PARM0);
452 if(!S_LocalSound (s))
454 Con_Printf("VM_localsound: Failed to play %s for %s !\n", s, PRVM_NAME);
455 PRVM_G_FLOAT(OFS_RETURN) = -4;
459 PRVM_G_FLOAT(OFS_RETURN) = 1;
471 PRVM_ERROR ("%s: break statement", PRVM_NAME);
474 //============================================================================
480 Sends text over to the client's execution buffer
482 [localcmd (string) or]
486 void VM_localcmd (void)
488 VM_SAFEPARMCOUNT(1,VM_localcmd);
490 Cbuf_AddText(PRVM_G_STRING(OFS_PARM0));
502 VM_SAFEPARMCOUNT(1,VM_cvar);
504 PRVM_G_FLOAT(OFS_RETURN) = Cvar_VariableValue(PRVM_G_STRING(OFS_PARM0));
511 const string VM_cvar_string (string)
514 void VM_cvar_string(void)
518 const char *cvar_string;
519 VM_SAFEPARMCOUNT(1,VM_cvar_string);
521 name = PRVM_G_STRING(OFS_PARM0);
524 PRVM_ERROR("VM_cvar_string: %s: null string\n", PRVM_NAME);
526 VM_CheckEmptyString(name);
528 out = VM_GetTempString();
530 cvar_string = Cvar_VariableString(name);
532 strcpy(out, cvar_string);
534 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(out);
539 ========================
542 const string VM_cvar_defstring (string)
543 ========================
545 void VM_cvar_defstring (void)
549 const char *cvar_string;
550 VM_SAFEPARMCOUNT(1,VM_cvar_string);
552 name = PRVM_G_STRING(OFS_PARM0);
555 PRVM_ERROR("VM_cvar_defstring: %s: null string\n", PRVM_NAME);
557 VM_CheckEmptyString(name);
559 out = VM_GetTempString();
561 cvar_string = Cvar_VariableDefString(name);
563 strcpy(out, cvar_string);
565 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(out);
571 void cvar_set (string,string)
574 void VM_cvar_set (void)
576 VM_SAFEPARMCOUNT(2,VM_cvar_set);
578 Cvar_Set(PRVM_G_STRING(OFS_PARM0), PRVM_G_STRING(OFS_PARM1));
588 void VM_dprint (void)
590 char string[VM_STRINGTEMP_LENGTH];
591 if (developer.integer)
593 VM_VarString(0, string, sizeof(string));
595 Con_Printf("%s", string);
597 Con_Printf("%s: %s", PRVM_NAME, string);
615 VM_SAFEPARMCOUNT(1, VM_ftos);
617 v = PRVM_G_FLOAT(OFS_PARM0);
619 s = VM_GetTempString();
620 if ((float)((int)v) == v)
621 sprintf(s, "%i", (int)v);
624 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(s);
639 VM_SAFEPARMCOUNT(1,VM_fabs);
641 v = PRVM_G_FLOAT(OFS_PARM0);
642 PRVM_G_FLOAT(OFS_RETURN) = fabs(v);
657 VM_SAFEPARMCOUNT(1,VM_vtos);
659 s = VM_GetTempString();
660 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]);
661 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(s);
676 VM_SAFEPARMCOUNT(1, VM_etos);
678 s = VM_GetTempString();
679 sprintf (s, "entity %i", PRVM_G_EDICTNUM(OFS_PARM0));
680 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(s);
687 float stof(...[string])
692 char string[VM_STRINGTEMP_LENGTH];
693 VM_VarString(0, string, sizeof(string));
694 PRVM_G_FLOAT(OFS_RETURN) = atof(string);
698 ========================
702 ========================
706 VM_SAFEPARMCOUNT(1, VM_itof);
707 PRVM_G_FLOAT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
711 ========================
715 ========================
720 VM_SAFEPARMCOUNT(1, VM_ftoi);
722 ent = PRVM_G_FLOAT(OFS_PARM0);
723 if(PRVM_PROG_TO_EDICT(ent)->priv.required->free)
724 PRVM_ERROR ("VM_ftoe: %s tried to access a freed entity (entity %i)!\n", PRVM_NAME, ent);
726 PRVM_G_INT(OFS_RETURN) = ent;
740 prog->xfunction->builtinsprofile += 20;
741 ed = PRVM_ED_Alloc();
753 void VM_remove (void)
756 prog->xfunction->builtinsprofile += 20;
758 VM_SAFEPARMCOUNT(1, VM_remove);
760 ed = PRVM_G_EDICT(OFS_PARM0);
761 if( PRVM_NUM_FOR_EDICT(ed) <= prog->reserved_edicts ) {
762 Con_DPrint( "VM_remove: tried to remove the null entity or a reserved entity!\n" );
763 } else if( ed->priv.required->free ) {
764 Con_DPrint( "VM_remove: tried to remove an already freed entity!\n" );
768 // if (ed == prog->edicts)
769 // PRVM_ERROR ("remove: tried to remove world\n");
770 // if (PRVM_NUM_FOR_EDICT(ed) <= sv.maxclients)
771 // Host_Error("remove: tried to remove a client\n");
778 entity find(entity start, .string field, string match)
789 VM_SAFEPARMCOUNT(3,VM_find);
791 e = PRVM_G_EDICTNUM(OFS_PARM0);
792 f = PRVM_G_INT(OFS_PARM1);
793 s = PRVM_G_STRING(OFS_PARM2);
797 // return reserved edict 0 (could be used for whatever the prog wants)
798 VM_RETURN_EDICT(prog->edicts);
802 for (e++ ; e < prog->num_edicts ; e++)
804 prog->xfunction->builtinsprofile++;
805 ed = PRVM_EDICT_NUM(e);
806 if (ed->priv.required->free)
808 t = PRVM_E_STRING(ed,f);
818 VM_RETURN_EDICT(prog->edicts);
825 entity findfloat(entity start, .float field, float match)
826 entity findentity(entity start, .entity field, entity match)
829 // LordHavoc: added this for searching float, int, and entity reference fields
830 void VM_findfloat (void)
837 VM_SAFEPARMCOUNT(3,VM_findfloat);
839 e = PRVM_G_EDICTNUM(OFS_PARM0);
840 f = PRVM_G_INT(OFS_PARM1);
841 s = PRVM_G_FLOAT(OFS_PARM2);
843 for (e++ ; e < prog->num_edicts ; e++)
845 prog->xfunction->builtinsprofile++;
846 ed = PRVM_EDICT_NUM(e);
847 if (ed->priv.required->free)
849 if (PRVM_E_FLOAT(ed,f) == s)
856 VM_RETURN_EDICT(prog->edicts);
863 entity findchain(.string field, string match)
866 // chained search for strings in entity fields
867 // entity(.string field, string match) findchain = #402;
868 void VM_findchain (void)
874 prvm_edict_t *ent, *chain;
876 VM_SAFEPARMCOUNT(2,VM_findchain);
878 // is the same like !(prog->flag & PRVM_FE_CHAIN) - even if the operator precedence is another
879 if(!prog->flag & PRVM_FE_CHAIN)
880 PRVM_ERROR("VM_findchain: %s doesnt have a chain field !\n", PRVM_NAME);
882 chain_of = PRVM_ED_FindField("chain")->ofs;
884 chain = prog->edicts;
886 f = PRVM_G_INT(OFS_PARM0);
887 s = PRVM_G_STRING(OFS_PARM1);
890 VM_RETURN_EDICT(prog->edicts);
894 ent = PRVM_NEXT_EDICT(prog->edicts);
895 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
897 prog->xfunction->builtinsprofile++;
898 if (ent->priv.required->free)
900 t = PRVM_E_STRING(ent,f);
906 PRVM_E_INT(ent,chain_of) = PRVM_NUM_FOR_EDICT(chain);
910 VM_RETURN_EDICT(chain);
917 entity findchainfloat(.string field, float match)
918 entity findchainentity(.string field, entity match)
921 // LordHavoc: chained search for float, int, and entity reference fields
922 // entity(.string field, float match) findchainfloat = #403;
923 void VM_findchainfloat (void)
929 prvm_edict_t *ent, *chain;
931 VM_SAFEPARMCOUNT(2, VM_findchainfloat);
933 if(!prog->flag & PRVM_FE_CHAIN)
934 PRVM_ERROR("VM_findchainfloat: %s doesnt have a chain field !\n", PRVM_NAME);
936 chain_of = PRVM_ED_FindField("chain")->ofs;
938 chain = (prvm_edict_t *)prog->edicts;
940 f = PRVM_G_INT(OFS_PARM0);
941 s = PRVM_G_FLOAT(OFS_PARM1);
943 ent = PRVM_NEXT_EDICT(prog->edicts);
944 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
946 prog->xfunction->builtinsprofile++;
947 if (ent->priv.required->free)
949 if (PRVM_E_FLOAT(ent,f) != s)
952 PRVM_E_INT(ent,chain_of) = PRVM_EDICT_TO_PROG(chain);
956 VM_RETURN_EDICT(chain);
960 ========================
963 entity findflags(entity start, .float field, float match)
964 ========================
966 // LordHavoc: search for flags in float fields
967 void VM_findflags (void)
974 VM_SAFEPARMCOUNT(3, VM_findflags);
977 e = PRVM_G_EDICTNUM(OFS_PARM0);
978 f = PRVM_G_INT(OFS_PARM1);
979 s = (int)PRVM_G_FLOAT(OFS_PARM2);
981 for (e++ ; e < prog->num_edicts ; e++)
983 prog->xfunction->builtinsprofile++;
984 ed = PRVM_EDICT_NUM(e);
985 if (ed->priv.required->free)
987 if ((int)PRVM_E_FLOAT(ed,f) & s)
994 VM_RETURN_EDICT(prog->edicts);
998 ========================
1001 entity findchainflags(.float field, float match)
1002 ========================
1004 // LordHavoc: chained search for flags in float fields
1005 void VM_findchainflags (void)
1011 prvm_edict_t *ent, *chain;
1013 VM_SAFEPARMCOUNT(2, VM_findchainflags);
1015 if(!prog->flag & PRVM_FE_CHAIN)
1016 PRVM_ERROR("VM_findchainflags: %s doesnt have a chain field !\n", PRVM_NAME);
1018 chain_of = PRVM_ED_FindField("chain")->ofs;
1020 chain = (prvm_edict_t *)prog->edicts;
1022 f = PRVM_G_INT(OFS_PARM0);
1023 s = (int)PRVM_G_FLOAT(OFS_PARM1);
1025 ent = PRVM_NEXT_EDICT(prog->edicts);
1026 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
1028 prog->xfunction->builtinsprofile++;
1029 if (ent->priv.required->free)
1031 if (!((int)PRVM_E_FLOAT(ent,f) & s))
1034 PRVM_E_INT(ent,chain_of) = PRVM_EDICT_TO_PROG(chain);
1038 VM_RETURN_EDICT(chain);
1048 void VM_coredump (void)
1050 VM_SAFEPARMCOUNT(0,VM_coredump);
1052 Cbuf_AddText("prvm_edicts ");
1053 Cbuf_AddText(PRVM_NAME);
1064 void PRVM_StackTrace(void);
1065 void VM_stackdump (void)
1067 VM_SAFEPARMCOUNT(0, VM_stackdump);
1082 VM_SAFEPARMCOUNT(0, VM_crash);
1084 PRVM_ERROR("Crash called by %s\n",PRVM_NAME);
1094 void VM_traceon (void)
1096 VM_SAFEPARMCOUNT(0,VM_traceon);
1108 void VM_traceoff (void)
1110 VM_SAFEPARMCOUNT(0,VM_traceoff);
1112 prog->trace = false;
1122 void VM_eprint (void)
1124 VM_SAFEPARMCOUNT(1,VM_eprint);
1126 PRVM_ED_PrintNum (PRVM_G_EDICTNUM(OFS_PARM0));
1140 VM_SAFEPARMCOUNT(1,VM_rint);
1142 f = PRVM_G_FLOAT(OFS_PARM0);
1144 PRVM_G_FLOAT(OFS_RETURN) = (int)(f + 0.5);
1146 PRVM_G_FLOAT(OFS_RETURN) = (int)(f - 0.5);
1156 void VM_floor (void)
1158 VM_SAFEPARMCOUNT(1,VM_floor);
1160 PRVM_G_FLOAT(OFS_RETURN) = floor(PRVM_G_FLOAT(OFS_PARM0));
1172 VM_SAFEPARMCOUNT(1,VM_ceil);
1174 PRVM_G_FLOAT(OFS_RETURN) = ceil(PRVM_G_FLOAT(OFS_PARM0));
1182 entity nextent(entity)
1185 void VM_nextent (void)
1190 i = PRVM_G_EDICTNUM(OFS_PARM0);
1193 prog->xfunction->builtinsprofile++;
1195 if (i == prog->num_edicts)
1197 VM_RETURN_EDICT(prog->edicts);
1200 ent = PRVM_EDICT_NUM(i);
1201 if (!ent->priv.required->free)
1203 VM_RETURN_EDICT(ent);
1209 //=============================================================================
1216 changelevel(string map)
1219 void VM_changelevel (void)
1223 VM_SAFEPARMCOUNT(1, VM_changelevel);
1227 Con_Printf("VM_changelevel: game is not server (%s)\n", PRVM_NAME);
1231 // make sure we don't issue two changelevels
1232 if (svs.changelevel_issued)
1234 svs.changelevel_issued = true;
1236 s = PRVM_G_STRING(OFS_PARM0);
1237 Cbuf_AddText (va("changelevel %s\n",s));
1249 VM_SAFEPARMCOUNT(1,VM_sin);
1250 PRVM_G_FLOAT(OFS_RETURN) = sin(PRVM_G_FLOAT(OFS_PARM0));
1261 VM_SAFEPARMCOUNT(1,VM_cos);
1262 PRVM_G_FLOAT(OFS_RETURN) = cos(PRVM_G_FLOAT(OFS_PARM0));
1274 VM_SAFEPARMCOUNT(1,VM_sqrt);
1275 PRVM_G_FLOAT(OFS_RETURN) = sqrt(PRVM_G_FLOAT(OFS_PARM0));
1282 Returns a vector of length < 1 and > 0
1287 void VM_randomvec (void)
1292 VM_SAFEPARMCOUNT(0, VM_randomvec);
1297 temp[0] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1298 temp[1] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1299 temp[2] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1301 while (DotProduct(temp, temp) >= 1);
1302 VectorCopy (temp, PRVM_G_VECTOR(OFS_RETURN));
1305 temp[0] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1306 temp[1] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1307 temp[2] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1308 // length returned always > 0
1309 length = (rand()&32766 + 1) * (1.0 / 32767.0) / VectorLength(temp);
1310 VectorScale(temp,length, temp);*/
1311 //VectorCopy(temp, PRVM_G_VECTOR(OFS_RETURN));
1314 //=============================================================================
1320 float registercvar (string name, string value, float flags)
1323 void VM_registercvar (void)
1325 const char *name, *value;
1328 VM_SAFEPARMCOUNT(3,VM_registercvar);
1330 name = PRVM_G_STRING(OFS_PARM0);
1331 value = PRVM_G_STRING(OFS_PARM1);
1332 flags = PRVM_G_FLOAT(OFS_PARM2);
1333 PRVM_G_FLOAT(OFS_RETURN) = 0;
1335 if(flags > CVAR_MAXFLAGSVAL)
1338 // first check to see if it has already been defined
1339 if (Cvar_FindVar (name))
1342 // check for overlap with a command
1343 if (Cmd_Exists (name))
1345 Con_Printf("VM_registercvar: %s is a command\n", name);
1349 Cvar_Get(name, value, flags);
1351 PRVM_G_FLOAT(OFS_RETURN) = 1; // success
1358 returns the minimum of two supplied floats
1360 float min(float a, float b, ...[float])
1365 // LordHavoc: 3+ argument enhancement suggested by FrikaC
1366 if (prog->argc == 2)
1367 PRVM_G_FLOAT(OFS_RETURN) = min(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1368 else if (prog->argc >= 3)
1371 float f = PRVM_G_FLOAT(OFS_PARM0);
1372 for (i = 1;i < prog->argc;i++)
1373 if (PRVM_G_FLOAT((OFS_PARM0+i*3)) < f)
1374 f = PRVM_G_FLOAT((OFS_PARM0+i*3));
1375 PRVM_G_FLOAT(OFS_RETURN) = f;
1378 PRVM_ERROR("VM_min: %s must supply at least 2 floats\n", PRVM_NAME);
1385 returns the maximum of two supplied floats
1387 float max(float a, float b, ...[float])
1392 // LordHavoc: 3+ argument enhancement suggested by FrikaC
1393 if (prog->argc == 2)
1394 PRVM_G_FLOAT(OFS_RETURN) = max(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1395 else if (prog->argc >= 3)
1398 float f = PRVM_G_FLOAT(OFS_PARM0);
1399 for (i = 1;i < prog->argc;i++)
1400 if (PRVM_G_FLOAT((OFS_PARM0+i*3)) > f)
1401 f = PRVM_G_FLOAT((OFS_PARM0+i*3));
1402 PRVM_G_FLOAT(OFS_RETURN) = f;
1405 PRVM_ERROR("VM_max: %s must supply at least 2 floats\n", PRVM_NAME);
1412 returns number bounded by supplied range
1414 float bound(float min, float value, float max)
1417 void VM_bound (void)
1419 VM_SAFEPARMCOUNT(3,VM_bound);
1420 PRVM_G_FLOAT(OFS_RETURN) = bound(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1), PRVM_G_FLOAT(OFS_PARM2));
1427 returns a raised to power b
1429 float pow(float a, float b)
1434 VM_SAFEPARMCOUNT(2,VM_pow);
1435 PRVM_G_FLOAT(OFS_RETURN) = pow(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1442 copies data from one entity to another
1444 copyentity(entity src, entity dst)
1447 void VM_copyentity (void)
1449 prvm_edict_t *in, *out;
1450 VM_SAFEPARMCOUNT(2,VM_copyentity);
1451 in = PRVM_G_EDICT(OFS_PARM0);
1452 out = PRVM_G_EDICT(OFS_PARM1);
1453 memcpy(out->fields.vp, in->fields.vp, prog->progs->entityfields * 4);
1460 sets the color of a client and broadcasts the update to all connected clients
1462 setcolor(clientent, value)
1465 /*void PF_setcolor (void)
1471 entnum = PRVM_G_EDICTNUM(OFS_PARM0);
1472 i = PRVM_G_FLOAT(OFS_PARM1);
1474 if (entnum < 1 || entnum > svs.maxclients || !svs.clients[entnum-1].active)
1476 Con_Print("tried to setcolor a non-client\n");
1480 client = svs.clients + entnum-1;
1481 if ((val = PRVM_GETEDICTFIELDVALUE(client->edict, eval_clientcolors)))
1484 client->old_colors = i;
1485 client->edict->fields.server->team = (i & 15) + 1;
1487 MSG_WriteByte (&sv.reliable_datagram, svc_updatecolors);
1488 MSG_WriteByte (&sv.reliable_datagram, entnum - 1);
1489 MSG_WriteByte (&sv.reliable_datagram, i);
1492 void VM_Files_Init(void)
1494 memset(VM_FILES, 0, sizeof(qfile_t*[MAX_VMFILES]));
1497 void VM_Files_CloseAll(void)
1500 for (i = 0;i < MAX_VMFILES;i++)
1503 FS_Close(VM_FILES[i]);
1504 //VM_FILES[i] = NULL;
1506 memset(VM_FILES,0,sizeof(qfile_t*[MAX_VMFILES])); // this should be faster (is it ?)
1509 qfile_t *VM_GetFileHandle( int index )
1511 if (index < 0 || index >= MAX_VMFILES)
1513 Con_Printf("VM_GetFileHandle: invalid file handle %i used in %s\n", index, PRVM_NAME);
1516 if (VM_FILES[index] == NULL)
1518 Con_Printf("VM_GetFileHandle: no such file handle %i (or file has been closed) in %s\n", index, PRVM_NAME);
1521 return VM_FILES[index];
1528 float fopen(string filename, float mode)
1531 // float(string filename, float mode) fopen = #110;
1532 // opens a file inside quake/gamedir/data/ (mode is FILE_READ, FILE_APPEND, or FILE_WRITE),
1533 // returns fhandle >= 0 if successful, or fhandle < 0 if unable to open file for any reason
1537 const char *modestring, *filename;
1539 VM_SAFEPARMCOUNT(2,VM_fopen);
1541 for (filenum = 0;filenum < MAX_VMFILES;filenum++)
1542 if (VM_FILES[filenum] == NULL)
1544 if (filenum >= MAX_VMFILES)
1546 Con_Printf("VM_fopen: %s ran out of file handles (%i)\n", PRVM_NAME, MAX_VMFILES);
1547 PRVM_G_FLOAT(OFS_RETURN) = -2;
1550 mode = PRVM_G_FLOAT(OFS_PARM1);
1553 case 0: // FILE_READ
1556 case 1: // FILE_APPEND
1559 case 2: // FILE_WRITE
1563 Con_Printf("VM_fopen: %s: no such mode %i (valid: 0 = read, 1 = append, 2 = write)\n", PRVM_NAME, mode);
1564 PRVM_G_FLOAT(OFS_RETURN) = -3;
1567 filename = PRVM_G_STRING(OFS_PARM0);
1569 VM_FILES[filenum] = FS_Open(va("data/%s", filename), modestring, false, false);
1570 if (VM_FILES[filenum] == NULL && mode == 0)
1571 VM_FILES[filenum] = FS_Open(va("%s", filename), modestring, false, false);
1573 if (VM_FILES[filenum] == NULL)
1575 if (developer.integer)
1576 Con_Printf("VM_fopen: %s: %s mode %s failed\n", PRVM_NAME, filename, modestring);
1577 PRVM_G_FLOAT(OFS_RETURN) = -1;
1581 if (developer.integer)
1582 Con_Printf("VM_fopen: %s: %s mode %s opened as #%i\n", PRVM_NAME, filename, modestring, filenum);
1583 PRVM_G_FLOAT(OFS_RETURN) = filenum;
1591 fclose(float fhandle)
1594 //void(float fhandle) fclose = #111; // closes a file
1595 void VM_fclose(void)
1599 VM_SAFEPARMCOUNT(1,VM_fclose);
1601 filenum = PRVM_G_FLOAT(OFS_PARM0);
1602 if (filenum < 0 || filenum >= MAX_VMFILES)
1604 Con_Printf("VM_fclose: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1607 if (VM_FILES[filenum] == NULL)
1609 Con_Printf("VM_fclose: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1612 if (developer.integer)
1613 Con_Printf("VM_fclose: %s: #%i closed\n", PRVM_NAME, filenum);
1614 FS_Close(VM_FILES[filenum]);
1615 VM_FILES[filenum] = NULL;
1622 string fgets(float fhandle)
1625 //string(float fhandle) fgets = #112; // reads a line of text from the file and returns as a tempstring
1629 static char string[VM_STRINGTEMP_LENGTH];
1632 VM_SAFEPARMCOUNT(1,VM_fgets);
1634 filenum = PRVM_G_FLOAT(OFS_PARM0);
1635 if (filenum < 0 || filenum >= MAX_VMFILES)
1637 Con_Printf("VM_fgets: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1640 if (VM_FILES[filenum] == NULL)
1642 Con_Printf("VM_fgets: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1648 c = FS_Getc(VM_FILES[filenum]);
1649 if (c == '\r' || c == '\n' || c < 0)
1651 if (end < VM_STRINGTEMP_LENGTH - 1)
1655 // remove \n following \r
1658 c = FS_Getc(VM_FILES[filenum]);
1660 FS_UnGetc(VM_FILES[filenum], (unsigned char)c);
1662 if (developer.integer >= 3)
1663 Con_Printf("fgets: %s: %s\n", PRVM_NAME, string);
1665 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(string);
1667 PRVM_G_INT(OFS_RETURN) = 0;
1674 fputs(float fhandle, string s)
1677 //void(float fhandle, string s) fputs = #113; // writes a line of text to the end of the file
1681 char string[VM_STRINGTEMP_LENGTH];
1684 VM_SAFEPARMCOUNT(2,VM_fputs);
1686 filenum = PRVM_G_FLOAT(OFS_PARM0);
1687 if (filenum < 0 || filenum >= MAX_VMFILES)
1689 Con_Printf("VM_fputs: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1692 if (VM_FILES[filenum] == NULL)
1694 Con_Printf("VM_fputs: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1697 VM_VarString(1, string, sizeof(string));
1698 if ((stringlength = (int)strlen(string)))
1699 FS_Write(VM_FILES[filenum], string, stringlength);
1700 if (developer.integer)
1701 Con_Printf("fputs: %s: %s\n", PRVM_NAME, string);
1708 float strlen(string s)
1711 //float(string s) strlen = #114; // returns how many characters are in a string
1712 void VM_strlen(void)
1716 VM_SAFEPARMCOUNT(1,VM_strlen);
1718 s = PRVM_G_STRING(OFS_PARM0);
1720 PRVM_G_FLOAT(OFS_RETURN) = strlen(s);
1722 PRVM_G_FLOAT(OFS_RETURN) = 0;
1729 string strcat(string,string,...[string])
1732 //string(string s1, string s2) strcat = #115;
1733 // concatenates two strings (for example "abc", "def" would return "abcdef")
1734 // and returns as a tempstring
1735 void VM_strcat(void)
1740 PRVM_ERROR("VM_strcat wrong parameter count (min. 1 expected ) !\n");
1742 s = VM_GetTempString();
1743 VM_VarString(0, s, VM_STRINGTEMP_LENGTH);
1744 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(s);
1751 string substring(string s, float start, float length)
1754 // string(string s, float start, float length) substring = #116;
1755 // returns a section of a string as a tempstring
1756 void VM_substring(void)
1758 int i, start, length;
1762 VM_SAFEPARMCOUNT(3,VM_substring);
1764 string = VM_GetTempString();
1765 s = PRVM_G_STRING(OFS_PARM0);
1766 start = PRVM_G_FLOAT(OFS_PARM1);
1767 length = PRVM_G_FLOAT(OFS_PARM2);
1770 for (i = 0;i < start && *s;i++, s++);
1771 for (i = 0;i < VM_STRINGTEMP_LENGTH - 1 && *s && i < length;i++, s++)
1774 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(string);
1781 vector stov(string s)
1784 //vector(string s) stov = #117; // returns vector value from a string
1787 char string[VM_STRINGTEMP_LENGTH];
1789 VM_SAFEPARMCOUNT(1,VM_stov);
1791 VM_VarString(0, string, sizeof(string));
1792 Math_atov(string, PRVM_G_VECTOR(OFS_RETURN));
1799 string strzone(string s)
1802 //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)
1803 void VM_strzone(void)
1808 VM_SAFEPARMCOUNT(1,VM_strzone);
1810 in = PRVM_G_STRING(OFS_PARM0);
1811 PRVM_G_INT(OFS_RETURN) = PRVM_AllocString(strlen(in) + 1, &out);
1822 //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!!!)
1823 void VM_strunzone(void)
1825 VM_SAFEPARMCOUNT(1,VM_strunzone);
1826 PRVM_FreeString(PRVM_G_INT(OFS_PARM0));
1831 VM_command (used by client and menu)
1833 clientcommand(float client, string s) (for client and menu)
1836 //void(entity e, string s) clientcommand = #440; // executes a command string as if it came from the specified client
1837 //this function originally written by KrimZon, made shorter by LordHavoc
1838 void VM_clcommand (void)
1840 client_t *temp_client;
1843 VM_SAFEPARMCOUNT(2,VM_clcommand);
1845 i = PRVM_G_FLOAT(OFS_PARM0);
1846 if (!sv.active || i < 0 || i >= svs.maxclients || !svs.clients[i].active)
1848 Con_Printf("VM_clientcommand: %s: invalid client/server is not active !\n", PRVM_NAME);
1852 temp_client = host_client;
1853 host_client = svs.clients + i;
1854 Cmd_ExecuteString (PRVM_G_STRING(OFS_PARM1), src_client);
1855 host_client = temp_client;
1863 float tokenize(string s)
1866 //float(string s) tokenize = #441; // takes apart a string into individal words (access them with argv), returns how many
1867 //this function originally written by KrimZon, made shorter by LordHavoc
1868 //20040203: rewritten by LordHavoc (no longer uses allocations)
1870 char *tokens[256], tokenbuf[4096];
1871 void VM_tokenize (void)
1876 VM_SAFEPARMCOUNT(1,VM_tokenize);
1878 p = PRVM_G_STRING(OFS_PARM0);
1882 while(COM_ParseToken(&p, false))
1884 if (num_tokens >= (int)(sizeof(tokens)/sizeof(tokens[0])))
1886 if (pos + strlen(com_token) + 1 > sizeof(tokenbuf))
1888 tokens[num_tokens++] = tokenbuf + pos;
1889 strcpy(tokenbuf + pos, com_token);
1890 pos += strlen(com_token) + 1;
1893 PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
1896 //string(float n) argv = #442; // returns a word from the tokenized string (returns nothing for an invalid index)
1897 //this function originally written by KrimZon, made shorter by LordHavoc
1902 VM_SAFEPARMCOUNT(1,VM_argv);
1904 token_num = PRVM_G_FLOAT(OFS_PARM0);
1906 if (token_num >= 0 && token_num < num_tokens)
1907 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(tokens[token_num]);
1909 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(NULL);
1913 //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)
1914 void PF_setattachment (void)
1916 prvm_edict_t *e = PRVM_G_EDICT(OFS_PARM0);
1917 prvm_edict_t *tagentity = PRVM_G_EDICT(OFS_PARM1);
1918 char *tagname = PRVM_G_STRING(OFS_PARM2);
1923 if (tagentity == NULL)
1924 tagentity = prog->edicts;
1926 v = PRVM_GETEDICTFIELDVALUE(e, eval_tag_entity);
1928 fields.server->edict = PRVM_EDICT_TO_PROG(tagentity);
1930 v = PRVM_GETEDICTFIELDVALUE(e, eval_tag_index);
1932 fields.server->_float = 0;
1933 if (tagentity != NULL && tagentity != prog->edicts && tagname && tagname[0])
1935 modelindex = (int)tagentity->fields.server->modelindex;
1936 if (modelindex >= 0 && modelindex < MAX_MODELS)
1938 model = sv.models[modelindex];
1939 if (model->data_overridetagnamesforskin && (unsigned int)tagentity->fields.server->skin < (unsigned int)model->numskins && model->data_overridetagnamesforskin[(unsigned int)tagentity->fields.server->skin].num_overridetagnames)
1940 for (i = 0;i < model->data_overridetagnamesforskin[(unsigned int)tagentity->fields.server->skin].num_overridetagnames;i++)
1941 if (!strcmp(tagname, model->data_overridetagnamesforskin[(unsigned int)tagentity->fields.server->skin].data_overridetagnames[i].name))
1942 fields.server->_float = i + 1;
1943 // FIXME: use a model function to get tag info (need to handle skeletal)
1944 if (fields.server->_float == 0 && model->num_tags)
1945 for (i = 0;i < model->num_tags;i++)
1946 if (!strcmp(tagname, model->data_tags[i].name))
1947 fields.server->_float = i + 1;
1948 if (fields.server->_float == 0)
1949 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", PRVM_NUM_FOR_EDICT(e), PRVM_NUM_FOR_EDICT(tagentity), tagname, tagname, PRVM_NUM_FOR_EDICT(tagentity), model->name);
1952 Con_DPrintf("setattachment(edict %i, edict %i, string \"%s\"): tried to find tag named \"%s\" on entity %i but it has no model\n", PRVM_NUM_FOR_EDICT(e), PRVM_NUM_FOR_EDICT(tagentity), tagname, tagname, PRVM_NUM_FOR_EDICT(tagentity));
1963 void VM_isserver(void)
1965 VM_SAFEPARMCOUNT(0,VM_serverstate);
1967 PRVM_G_FLOAT(OFS_RETURN) = sv.active;
1977 void VM_clientcount(void)
1979 VM_SAFEPARMCOUNT(0,VM_clientcount);
1981 PRVM_G_FLOAT(OFS_RETURN) = svs.maxclients;
1991 void VM_clientstate(void)
1993 VM_SAFEPARMCOUNT(0,VM_clientstate);
1995 PRVM_G_FLOAT(OFS_RETURN) = cls.state;
2002 float getostype(void)
2004 */ // not used at the moment -> not included in the common list
2005 void VM_getostype(void)
2007 VM_SAFEPARMCOUNT(0,VM_getostype);
2012 OS_MAC - not supported
2016 PRVM_G_FLOAT(OFS_RETURN) = 0;
2018 PRVM_G_FLOAT(OFS_RETURN) = 2;
2020 PRVM_G_FLOAT(OFS_RETURN) = 1;
2028 vector getmousepos()
2031 void VM_getmousepos(void)
2034 VM_SAFEPARMCOUNT(0,VM_getmousepos);
2036 PRVM_G_VECTOR(OFS_RETURN)[0] = in_mouse_x * vid_conwidth.integer / vid.width;
2037 PRVM_G_VECTOR(OFS_RETURN)[1] = in_mouse_y * vid_conheight.integer / vid.height;
2038 PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
2048 void VM_gettime(void)
2050 VM_SAFEPARMCOUNT(0,VM_gettime);
2052 PRVM_G_FLOAT(OFS_RETURN) = (float) *prog->time;
2059 loadfromdata(string data)
2062 void VM_loadfromdata(void)
2064 VM_SAFEPARMCOUNT(1,VM_loadentsfromfile);
2066 PRVM_ED_LoadFromFile(PRVM_G_STRING(OFS_PARM0));
2070 ========================
2073 parseentitydata(entity ent, string data)
2074 ========================
2076 void VM_parseentitydata(void)
2081 VM_SAFEPARMCOUNT(2, VM_parseentitydata);
2083 // get edict and test it
2084 ent = PRVM_G_EDICT(OFS_PARM0);
2085 if (ent->priv.required->free)
2086 PRVM_ERROR ("VM_parseentitydata: %s: Can only set already spawned entities (entity %i is free)!\n", PRVM_NAME, PRVM_NUM_FOR_EDICT(ent));
2088 data = PRVM_G_STRING(OFS_PARM1);
2090 // parse the opening brace
2091 if (!COM_ParseToken(&data, false) || com_token[0] != '{' )
2092 PRVM_ERROR ("VM_parseentitydata: %s: Couldn't parse entity data:\n%s\n", PRVM_NAME, data );
2094 PRVM_ED_ParseEdict (data, ent);
2101 loadfromfile(string file)
2104 void VM_loadfromfile(void)
2106 const char *filename;
2109 VM_SAFEPARMCOUNT(1,VM_loadfromfile);
2111 filename = PRVM_G_STRING(OFS_PARM0);
2112 // .. is parent directory on many platforms
2113 // / is parent directory on Amiga
2114 // : is root of drive on Amiga (also used as a directory separator on Mac, but / works there too, so that's a bad idea)
2115 // \ is a windows-ism (so it's naughty to use it, / works on all platforms)
2116 if ((filename[0] == '.' && filename[1] == '.') || filename[0] == '/' || strrchr(filename, ':') || strrchr(filename, '\\'))
2118 Con_Printf("VM_loadfromfile: %s dangerous or non-portable filename \"%s\" not allowed. (contains : or \\ or begins with .. or /)\n", PRVM_NAME, filename);
2119 PRVM_G_FLOAT(OFS_RETURN) = -4;
2123 // not conform with VM_fopen
2124 data = FS_LoadFile(filename, tempmempool, false);
2126 PRVM_G_FLOAT(OFS_RETURN) = -1;
2128 PRVM_ED_LoadFromFile(data);
2139 float mod(float val, float m)
2142 void VM_modulo(void)
2145 VM_SAFEPARMCOUNT(2,VM_module);
2147 val = (int) PRVM_G_FLOAT(OFS_PARM0);
2148 m = (int) PRVM_G_FLOAT(OFS_PARM1);
2150 PRVM_G_FLOAT(OFS_RETURN) = (float) (val % m);
2153 void VM_Search_Init(void)
2155 memset(VM_SEARCHLIST,0,sizeof(fssearch_t*[MAX_VMSEARCHES]));
2158 void VM_Search_Reset(void)
2161 // reset the fssearch list
2162 for(i = 0; i < MAX_VMSEARCHES; i++)
2163 if(VM_SEARCHLIST[i])
2164 FS_FreeSearch(VM_SEARCHLIST[i]);
2165 memset(VM_SEARCHLIST,0,sizeof(fssearch_t*[MAX_VMSEARCHES]));
2172 float search_begin(string pattern, float caseinsensitive, float quiet)
2175 void VM_search_begin(void)
2178 const char *pattern;
2179 int caseinsens, quiet;
2181 VM_SAFEPARMCOUNT(3, VM_search_begin);
2183 pattern = PRVM_G_STRING(OFS_PARM0);
2185 VM_CheckEmptyString(pattern);
2187 caseinsens = PRVM_G_FLOAT(OFS_PARM1);
2188 quiet = PRVM_G_FLOAT(OFS_PARM2);
2190 for(handle = 0; handle < MAX_VMSEARCHES; handle++)
2191 if(!VM_SEARCHLIST[handle])
2194 if(handle >= MAX_VMSEARCHES)
2196 Con_Printf("VM_search_begin: %s ran out of search handles (%i)\n", PRVM_NAME, MAX_VMSEARCHES);
2197 PRVM_G_FLOAT(OFS_RETURN) = -2;
2201 if(!(VM_SEARCHLIST[handle] = FS_Search(pattern,caseinsens, quiet)))
2202 PRVM_G_FLOAT(OFS_RETURN) = -1;
2204 PRVM_G_FLOAT(OFS_RETURN) = handle;
2211 void search_end(float handle)
2214 void VM_search_end(void)
2217 VM_SAFEPARMCOUNT(1, VM_search_end);
2219 handle = PRVM_G_FLOAT(OFS_PARM0);
2221 if(handle < 0 || handle >= MAX_VMSEARCHES)
2223 Con_Printf("VM_search_end: invalid handle %i used in %s\n", handle, PRVM_NAME);
2226 if(VM_SEARCHLIST[handle] == NULL)
2228 Con_Printf("VM_search_end: no such handle %i in %s\n", handle, PRVM_NAME);
2232 FS_FreeSearch(VM_SEARCHLIST[handle]);
2233 VM_SEARCHLIST[handle] = NULL;
2240 float search_getsize(float handle)
2243 void VM_search_getsize(void)
2246 VM_SAFEPARMCOUNT(1, VM_M_search_getsize);
2248 handle = PRVM_G_FLOAT(OFS_PARM0);
2250 if(handle < 0 || handle >= MAX_VMSEARCHES)
2252 Con_Printf("VM_search_getsize: invalid handle %i used in %s\n", handle, PRVM_NAME);
2255 if(VM_SEARCHLIST[handle] == NULL)
2257 Con_Printf("VM_search_getsize: no such handle %i in %s\n", handle, PRVM_NAME);
2261 PRVM_G_FLOAT(OFS_RETURN) = VM_SEARCHLIST[handle]->numfilenames;
2266 VM_search_getfilename
2268 string search_getfilename(float handle, float num)
2271 void VM_search_getfilename(void)
2273 int handle, filenum;
2275 VM_SAFEPARMCOUNT(2, VM_search_getfilename);
2277 handle = PRVM_G_FLOAT(OFS_PARM0);
2278 filenum = PRVM_G_FLOAT(OFS_PARM1);
2280 if(handle < 0 || handle >= MAX_VMSEARCHES)
2282 Con_Printf("VM_search_getfilename: invalid handle %i used in %s\n", handle, PRVM_NAME);
2285 if(VM_SEARCHLIST[handle] == NULL)
2287 Con_Printf("VM_search_getfilename: no such handle %i in %s\n", handle, PRVM_NAME);
2290 if(filenum < 0 || filenum >= VM_SEARCHLIST[handle]->numfilenames)
2292 Con_Printf("VM_search_getfilename: invalid filenum %i in %s\n", filenum, PRVM_NAME);
2296 tmp = VM_GetTempString();
2297 strcpy(tmp, VM_SEARCHLIST[handle]->filenames[filenum]);
2299 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(tmp);
2306 string chr(float ascii)
2312 VM_SAFEPARMCOUNT(1, VM_chr);
2314 tmp = VM_GetTempString();
2315 tmp[0] = (unsigned char) PRVM_G_FLOAT(OFS_PARM0);
2318 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(tmp);
2321 //=============================================================================
2322 // Draw builtins (client & menu)
2328 float iscachedpic(string pic)
2331 void VM_iscachedpic(void)
2333 VM_SAFEPARMCOUNT(1,VM_iscachedpic);
2335 // drawq hasnt such a function, thus always return true
2336 PRVM_G_FLOAT(OFS_RETURN) = false;
2343 string precache_pic(string pic)
2346 void VM_precache_pic(void)
2350 VM_SAFEPARMCOUNT(1, VM_precache_pic);
2352 s = PRVM_G_STRING(OFS_PARM0);
2353 PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
2356 PRVM_ERROR ("VM_precache_pic: %s: NULL\n", PRVM_NAME);
2358 VM_CheckEmptyString (s);
2360 // AK Draw_CachePic is supposed to always return a valid pointer
2361 if( Draw_CachePic(s, false)->tex == r_texture_notexture )
2362 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(NULL);
2372 void VM_freepic(void)
2376 VM_SAFEPARMCOUNT(1,VM_freepic);
2378 s = PRVM_G_STRING(OFS_PARM0);
2381 PRVM_ERROR ("VM_freepic: %s: NULL\n");
2383 VM_CheckEmptyString (s);
2392 float drawcharacter(vector position, float character, vector scale, vector rgb, float alpha, float flag)
2395 void VM_drawcharacter(void)
2397 float *pos,*scale,*rgb;
2400 VM_SAFEPARMCOUNT(6,VM_drawcharacter);
2402 character = (char) PRVM_G_FLOAT(OFS_PARM1);
2405 Con_Printf("VM_drawcharacter: %s passed null character !\n",PRVM_NAME);
2406 PRVM_G_FLOAT(OFS_RETURN) = -1;
2410 pos = PRVM_G_VECTOR(OFS_PARM0);
2411 scale = PRVM_G_VECTOR(OFS_PARM2);
2412 rgb = PRVM_G_VECTOR(OFS_PARM3);
2413 flag = (int)PRVM_G_FLOAT(OFS_PARM5);
2415 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2417 Con_Printf("VM_drawcharacter: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2418 PRVM_G_FLOAT(OFS_RETURN) = -2;
2422 if(pos[2] || scale[2])
2423 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")));
2425 if(!scale[0] || !scale[1])
2427 Con_Printf("VM_drawcharacter: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
2428 PRVM_G_FLOAT(OFS_RETURN) = -3;
2432 DrawQ_String (pos[0], pos[1], &character, 1, scale[0], scale[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM4), flag);
2433 PRVM_G_FLOAT(OFS_RETURN) = 1;
2440 float drawstring(vector position, string text, vector scale, vector rgb, float alpha, float flag)
2443 void VM_drawstring(void)
2445 float *pos,*scale,*rgb;
2448 VM_SAFEPARMCOUNT(6,VM_drawstring);
2450 string = PRVM_G_STRING(OFS_PARM1);
2453 Con_Printf("VM_drawstring: %s passed null string !\n",PRVM_NAME);
2454 PRVM_G_FLOAT(OFS_RETURN) = -1;
2458 //VM_CheckEmptyString(string); Why should it be checked - perhaps the menu wants to support the precolored letters, too?
2460 pos = PRVM_G_VECTOR(OFS_PARM0);
2461 scale = PRVM_G_VECTOR(OFS_PARM2);
2462 rgb = PRVM_G_VECTOR(OFS_PARM3);
2463 flag = (int)PRVM_G_FLOAT(OFS_PARM5);
2465 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2467 Con_Printf("VM_drawstring: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2468 PRVM_G_FLOAT(OFS_RETURN) = -2;
2472 if(!scale[0] || !scale[1])
2474 Con_Printf("VM_drawstring: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
2475 PRVM_G_FLOAT(OFS_RETURN) = -3;
2479 if(pos[2] || scale[2])
2480 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")));
2482 DrawQ_String (pos[0], pos[1], string, 0, scale[0], scale[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM4), flag);
2483 PRVM_G_FLOAT(OFS_RETURN) = 1;
2489 float drawpic(vector position, string pic, vector size, vector rgb, float alpha, float flag)
2492 void VM_drawpic(void)
2495 float *size, *pos, *rgb;
2498 VM_SAFEPARMCOUNT(6,VM_drawpic);
2500 pic = PRVM_G_STRING(OFS_PARM1);
2504 Con_Printf("VM_drawpic: %s passed null picture name !\n", PRVM_NAME);
2505 PRVM_G_FLOAT(OFS_RETURN) = -1;
2509 VM_CheckEmptyString (pic);
2511 // is pic cached ? no function yet for that
2514 Con_Printf("VM_drawpic: %s: %s not cached !\n", PRVM_NAME, pic);
2515 PRVM_G_FLOAT(OFS_RETURN) = -4;
2519 pos = PRVM_G_VECTOR(OFS_PARM0);
2520 size = PRVM_G_VECTOR(OFS_PARM2);
2521 rgb = PRVM_G_VECTOR(OFS_PARM3);
2522 flag = (int) PRVM_G_FLOAT(OFS_PARM5);
2524 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2526 Con_Printf("VM_drawstring: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2527 PRVM_G_FLOAT(OFS_RETURN) = -2;
2531 if(pos[2] || size[2])
2532 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")));
2534 DrawQ_Pic(pos[0], pos[1], pic, size[0], size[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM4), flag);
2535 PRVM_G_FLOAT(OFS_RETURN) = 1;
2542 float drawfill(vector position, vector size, vector rgb, float alpha, float flag)
2545 void VM_drawfill(void)
2547 float *size, *pos, *rgb;
2550 VM_SAFEPARMCOUNT(5,VM_drawfill);
2553 pos = PRVM_G_VECTOR(OFS_PARM0);
2554 size = PRVM_G_VECTOR(OFS_PARM1);
2555 rgb = PRVM_G_VECTOR(OFS_PARM2);
2556 flag = (int) PRVM_G_FLOAT(OFS_PARM4);
2558 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2560 Con_Printf("VM_drawstring: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2561 PRVM_G_FLOAT(OFS_RETURN) = -2;
2565 if(pos[2] || size[2])
2566 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")));
2568 DrawQ_Pic(pos[0], pos[1], 0, size[0], size[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM3), flag);
2569 PRVM_G_FLOAT(OFS_RETURN) = 1;
2576 drawsetcliparea(float x, float y, float width, float height)
2579 void VM_drawsetcliparea(void)
2582 VM_SAFEPARMCOUNT(4,VM_drawsetcliparea);
2584 x = bound(0, PRVM_G_FLOAT(OFS_PARM0), vid_conwidth.integer);
2585 y = bound(0, PRVM_G_FLOAT(OFS_PARM1), vid_conheight.integer);
2586 w = bound(0, PRVM_G_FLOAT(OFS_PARM2) + PRVM_G_FLOAT(OFS_PARM0) - x, (vid_conwidth.integer - x));
2587 h = bound(0, PRVM_G_FLOAT(OFS_PARM3) + PRVM_G_FLOAT(OFS_PARM1) - y, (vid_conheight.integer - y));
2589 DrawQ_SetClipArea(x, y, w, h);
2594 VM_drawresetcliparea
2599 void VM_drawresetcliparea(void)
2601 VM_SAFEPARMCOUNT(0,VM_drawresetcliparea);
2603 DrawQ_ResetClipArea();
2610 vector getimagesize(string pic)
2613 void VM_getimagesize(void)
2618 VM_SAFEPARMCOUNT(1,VM_getimagesize);
2620 p = PRVM_G_STRING(OFS_PARM0);
2623 PRVM_ERROR("VM_getimagepos: %s passed null picture name !\n", PRVM_NAME);
2625 VM_CheckEmptyString (p);
2627 pic = Draw_CachePic (p, false);
2629 PRVM_G_VECTOR(OFS_RETURN)[0] = pic->width;
2630 PRVM_G_VECTOR(OFS_RETURN)[1] = pic->height;
2631 PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
2634 // CL_Video interface functions
2637 ========================
2640 float cin_open(string file, string name)
2641 ========================
2643 void VM_cin_open( void )
2648 VM_SAFEPARMCOUNT( 2, VM_cin_open );
2650 file = PRVM_G_STRING( OFS_PARM0 );
2651 name = PRVM_G_STRING( OFS_PARM1 );
2653 VM_CheckEmptyString( file );
2654 VM_CheckEmptyString( name );
2656 if( CL_OpenVideo( file, name, MENUOWNER ) )
2657 PRVM_G_FLOAT( OFS_RETURN ) = 1;
2659 PRVM_G_FLOAT( OFS_RETURN ) = 0;
2663 ========================
2666 void cin_close(string name)
2667 ========================
2669 void VM_cin_close( void )
2673 VM_SAFEPARMCOUNT( 1, VM_cin_close );
2675 name = PRVM_G_STRING( OFS_PARM0 );
2676 VM_CheckEmptyString( name );
2678 CL_CloseVideo( CL_GetVideo( name ) );
2682 ========================
2684 void cin_setstate(string name, float type)
2685 ========================
2687 void VM_cin_setstate( void )
2690 clvideostate_t state;
2693 VM_SAFEPARMCOUNT( 2, VM_cin_netstate );
2695 name = PRVM_G_STRING( OFS_PARM0 );
2696 VM_CheckEmptyString( name );
2698 state = PRVM_G_FLOAT( OFS_PARM1 );
2700 video = CL_GetVideo( name );
2701 if( video && state > CLVIDEO_UNUSED && state < CLVIDEO_STATECOUNT )
2702 CL_SetVideoState( video, state );
2706 ========================
2709 float cin_getstate(string name)
2710 ========================
2712 void VM_cin_getstate( void )
2717 VM_SAFEPARMCOUNT( 1, VM_cin_getstate );
2719 name = PRVM_G_STRING( OFS_PARM0 );
2720 VM_CheckEmptyString( name );
2722 video = CL_GetVideo( name );
2724 PRVM_G_FLOAT( OFS_RETURN ) = (int)video->state;
2726 PRVM_G_FLOAT( OFS_RETURN ) = 0;
2730 ========================
2733 void cin_restart(string name)
2734 ========================
2736 void VM_cin_restart( void )
2741 VM_SAFEPARMCOUNT( 1, VM_cin_restart );
2743 name = PRVM_G_STRING( OFS_PARM0 );
2744 VM_CheckEmptyString( name );
2746 video = CL_GetVideo( name );
2748 CL_RestartVideo( video );
2751 ////////////////////////////////////////
2752 // AltString functions
2753 ////////////////////////////////////////
2756 ========================
2759 float altstr_count(string)
2760 ========================
2762 void VM_altstr_count( void )
2764 const char *altstr, *pos;
2767 VM_SAFEPARMCOUNT( 1, VM_altstr_count );
2769 altstr = PRVM_G_STRING( OFS_PARM0 );
2770 //VM_CheckEmptyString( altstr );
2772 for( count = 0, pos = altstr ; *pos ; pos++ ) {
2773 if( *pos == '\\' ) {
2777 } else if( *pos == '\'' ) {
2782 PRVM_G_FLOAT( OFS_RETURN ) = (float) (count / 2);
2786 ========================
2789 string altstr_prepare(string)
2790 ========================
2792 void VM_altstr_prepare( void )
2795 const char *instr, *in;
2798 VM_SAFEPARMCOUNT( 1, VM_altstr_prepare );
2800 instr = PRVM_G_STRING( OFS_PARM0 );
2801 //VM_CheckEmptyString( instr );
2802 outstr = VM_GetTempString();
2804 for( out = outstr, in = instr, size = VM_STRINGTEMP_LENGTH - 1 ; size && *in ; size--, in++, out++ )
2813 PRVM_G_INT( OFS_RETURN ) = PRVM_SetEngineString( outstr );
2817 ========================
2820 string altstr_get(string, float)
2821 ========================
2823 void VM_altstr_get( void )
2825 const char *altstr, *pos;
2829 VM_SAFEPARMCOUNT( 2, VM_altstr_get );
2831 altstr = PRVM_G_STRING( OFS_PARM0 );
2832 //VM_CheckEmptyString( altstr );
2834 count = PRVM_G_FLOAT( OFS_PARM1 );
2835 count = count * 2 + 1;
2837 for( pos = altstr ; *pos && count ; pos++ )
2838 if( *pos == '\\' ) {
2841 } else if( *pos == '\'' )
2845 PRVM_G_INT( OFS_RETURN ) = PRVM_SetEngineString( NULL );
2849 outstr = VM_GetTempString();
2850 for( out = outstr, size = VM_STRINGTEMP_LENGTH - 1 ; size && *pos ; size--, pos++, out++ )
2851 if( *pos == '\\' ) {
2856 } else if( *pos == '\'' )
2862 PRVM_G_INT( OFS_RETURN ) = PRVM_SetEngineString( outstr );
2866 ========================
2869 string altstr_set(string altstr, float num, string set)
2870 ========================
2872 void VM_altstr_set( void )
2875 const char *altstr, *str;
2879 VM_SAFEPARMCOUNT( 3, VM_altstr_set );
2881 altstr = PRVM_G_STRING( OFS_PARM0 );
2882 //VM_CheckEmptyString( altstr );
2884 num = PRVM_G_FLOAT( OFS_PARM1 );
2886 str = PRVM_G_STRING( OFS_PARM2 );
2887 //VM_CheckEmptyString( str );
2889 outstr = out = VM_GetTempString();
2890 for( num = num * 2 + 1, in = altstr; *in && num; *out++ = *in++ )
2895 } else if( *in == '\'' ) {
2900 PRVM_G_INT( OFS_RETURN ) = PRVM_SetEngineString( altstr );
2904 for( ; *str; *out++ = *str++ );
2905 // now jump over the old content
2907 if( *in == '\'' || (*in == '\\' && !*++in) )
2911 PRVM_G_INT( OFS_RETURN ) = PRVM_SetEngineString( NULL );
2916 PRVM_G_INT( OFS_RETURN ) = PRVM_SetEngineString( outstr );
2920 ========================
2923 string altstr_ins(string altstr, float num, string set)
2924 ========================
2926 void VM_altstr_ins(void)
2936 in = instr = PRVM_G_STRING( OFS_PARM0 );
2937 num = PRVM_G_FLOAT( OFS_PARM1 );
2938 set = setstr = PRVM_G_STRING( OFS_PARM2 );
2940 out = outstr = VM_GetTempString();
2941 for( num = num * 2 + 2 ; *in && num > 0 ; *out++ = *in++ )
2946 } else if( *in == '\'' ) {
2951 for( ; *set ; *out++ = *set++ );
2955 PRVM_G_INT( OFS_RETURN ) = PRVM_SetEngineString( outstr );
2958 void VM_Cmd_Init(void)
2960 // only init the stuff for the current prog
2965 void VM_Cmd_Reset(void)
2967 CL_PurgeOwner( MENUOWNER );
2969 VM_Files_CloseAll();