2 // Basically every vm builtin cmd should be in here.
3 // All 3 builtin and extension lists can be found here
4 // cause large (I think they will) parts are from pr_cmds the same copyright like in pr_cmds
10 // LordHavoc: changed this to NOT use a return statement, so that it can be used in functions that must return a value
11 void VM_Warning(const char *fmt, ...)
14 char msg[MAX_INPUTLINE];
17 dpvsnprintf(msg,sizeof(msg),fmt,argptr);
21 // TODO: either add a cvar/cmd to control the state dumping or replace some of the calls with Con_Printf [9/13/2006 Black]
26 //============================================================================
29 // TODO DONE: move vm_files and vm_fssearchlist to prvm_prog_t struct
30 // TODO: move vm_files and vm_fssearchlist back [9/13/2006 Black]
31 // TODO: (move vm_files and vm_fssearchlist to prvm_prog_t struct again) [2007-01-23 LordHavoc]
32 // TODO: will this war ever end? [2007-01-23 LordHavoc]
34 void VM_CheckEmptyString (const char *s)
37 PRVM_ERROR ("%s: Bad string", PRVM_NAME);
40 //============================================================================
43 void VM_VarString(int first, char *out, int outlength)
49 outend = out + outlength - 1;
50 for (i = first;i < prog->argc && out < outend;i++)
52 s = PRVM_G_STRING((OFS_PARM0+i*3));
53 while (out < outend && *s)
63 returns true if the extension is supported by the server
65 checkextension(extensionname)
69 // kind of helper function
70 static qboolean checkextension(const char *name)
74 len = (int)strlen(name);
76 for (e = prog->extensionstring;*e;e++)
83 while (*e && *e != ' ')
85 if ((e - start) == len && !strncasecmp(start, name, len))
91 void VM_checkextension (void)
93 VM_SAFEPARMCOUNT(1,VM_checkextension);
95 PRVM_G_FLOAT(OFS_RETURN) = checkextension(PRVM_G_STRING(OFS_PARM0));
102 This is a TERMINAL error, which will kill off the entire prog.
111 char string[VM_STRINGTEMP_LENGTH];
113 VM_VarString(0, string, sizeof(string));
114 Con_Printf("======%s ERROR in %s:\n%s\n", PRVM_NAME, PRVM_GetString(prog->xfunction->s_name), string);
115 if (prog->globaloffsets.self >= 0)
117 ed = PRVM_PROG_TO_EDICT(PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict);
118 PRVM_ED_Print(ed, NULL);
121 PRVM_ERROR ("%s: Program error in function %s:\n%s\nTip: read above for entity information\n", PRVM_NAME, PRVM_GetString(prog->xfunction->s_name), string);
128 Dumps out self, then an error message. The program is aborted and self is
129 removed, but the level can continue.
134 void VM_objerror (void)
137 char string[VM_STRINGTEMP_LENGTH];
139 VM_VarString(0, string, sizeof(string));
140 Con_Printf("======OBJECT ERROR======\n"); // , PRVM_NAME, PRVM_GetString(prog->xfunction->s_name), string); // or include them? FIXME
141 if (prog->globaloffsets.self >= 0)
143 ed = PRVM_PROG_TO_EDICT(PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict);
144 PRVM_ED_Print(ed, NULL);
149 // objerror has to display the object fields -> else call
150 PRVM_ERROR ("VM_objecterror: self not defined !");
151 Con_Printf("%s OBJECT ERROR in %s:\n%s\nTip: read above for entity information\n", PRVM_NAME, PRVM_GetString(prog->xfunction->s_name), string);
165 char string[VM_STRINGTEMP_LENGTH];
167 VM_VarString(0, string, sizeof(string));
175 broadcast print to everyone on server
180 void VM_bprint (void)
182 char string[VM_STRINGTEMP_LENGTH];
186 VM_Warning("VM_bprint: game is not server(%s) !\n", PRVM_NAME);
190 VM_VarString(0, string, sizeof(string));
191 SV_BroadcastPrint(string);
196 VM_sprint (menu & client but only if server.active == true)
198 single print to a specific client
200 sprint(float clientnum,...[string])
203 void VM_sprint (void)
207 char string[VM_STRINGTEMP_LENGTH];
209 VM_SAFEPARMCOUNTRANGE(1, 8, VM_sprint);
211 //find client for this entity
212 clientnum = (int)PRVM_G_FLOAT(OFS_PARM0);
213 if (!sv.active || clientnum < 0 || clientnum >= svs.maxclients || !svs.clients[clientnum].active)
215 VM_Warning("VM_sprint: %s: invalid client or server is not active !\n", PRVM_NAME);
219 client = svs.clients + clientnum;
220 if (!client->netconnection)
223 VM_VarString(1, string, sizeof(string));
224 MSG_WriteChar(&client->netconnection->message,svc_print);
225 MSG_WriteString(&client->netconnection->message, string);
232 single print to the screen
237 void VM_centerprint (void)
239 char string[VM_STRINGTEMP_LENGTH];
241 VM_SAFEPARMCOUNTRANGE(1, 8, VM_centerprint);
242 VM_VarString(0, string, sizeof(string));
243 SCR_CenterPrint(string);
250 vector normalize(vector)
253 void VM_normalize (void)
259 VM_SAFEPARMCOUNT(1,VM_normalize);
261 value1 = PRVM_G_VECTOR(OFS_PARM0);
263 f = VectorLength2(value1);
267 VectorScale(value1, f, newvalue);
270 VectorClear(newvalue);
272 VectorCopy (newvalue, PRVM_G_VECTOR(OFS_RETURN));
284 VM_SAFEPARMCOUNT(1,VM_vlen);
285 PRVM_G_FLOAT(OFS_RETURN) = VectorLength(PRVM_G_VECTOR(OFS_PARM0));
292 float vectoyaw(vector)
295 void VM_vectoyaw (void)
300 VM_SAFEPARMCOUNT(1,VM_vectoyaw);
302 value1 = PRVM_G_VECTOR(OFS_PARM0);
304 if (value1[1] == 0 && value1[0] == 0)
308 yaw = (int) (atan2(value1[1], value1[0]) * 180 / M_PI);
313 PRVM_G_FLOAT(OFS_RETURN) = yaw;
321 vector vectoangles(vector)
324 void VM_vectoangles (void)
330 VM_SAFEPARMCOUNT(1,VM_vectoangles);
332 value1 = PRVM_G_VECTOR(OFS_PARM0);
334 if (value1[1] == 0 && value1[0] == 0)
344 // LordHavoc: optimized a bit
347 yaw = (atan2(value1[1], value1[0]) * 180 / M_PI);
351 else if (value1[1] > 0)
356 forward = sqrt(value1[0]*value1[0] + value1[1]*value1[1]);
357 pitch = (atan2(value1[2], forward) * 180 / M_PI);
362 PRVM_G_FLOAT(OFS_RETURN+0) = pitch;
363 PRVM_G_FLOAT(OFS_RETURN+1) = yaw;
364 PRVM_G_FLOAT(OFS_RETURN+2) = 0;
371 Returns a number from 0<= num < 1
376 void VM_random (void)
378 VM_SAFEPARMCOUNT(0,VM_random);
380 PRVM_G_FLOAT(OFS_RETURN) = lhrandom(0, 1);
387 localsound(string sample)
390 void VM_localsound(void)
394 VM_SAFEPARMCOUNT(1,VM_localsound);
396 s = PRVM_G_STRING(OFS_PARM0);
398 if(!S_LocalSound (s))
400 PRVM_G_FLOAT(OFS_RETURN) = -4;
401 VM_Warning("VM_localsound: Failed to play %s for %s !\n", s, PRVM_NAME);
405 PRVM_G_FLOAT(OFS_RETURN) = 1;
417 PRVM_ERROR ("%s: break statement", PRVM_NAME);
420 //============================================================================
426 Sends text over to the client's execution buffer
428 [localcmd (string, ...) or]
432 void VM_localcmd (void)
434 char string[VM_STRINGTEMP_LENGTH];
435 VM_SAFEPARMCOUNTRANGE(1, 8, VM_localcmd);
436 VM_VarString(0, string, sizeof(string));
437 Cbuf_AddText(string);
449 char string[VM_STRINGTEMP_LENGTH];
450 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar);
451 VM_VarString(0, string, sizeof(string));
452 VM_CheckEmptyString(string);
453 PRVM_G_FLOAT(OFS_RETURN) = Cvar_VariableValue(string);
460 const string VM_cvar_string (string, ...)
463 void VM_cvar_string(void)
465 char string[VM_STRINGTEMP_LENGTH];
466 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar_string);
467 VM_VarString(0, string, sizeof(string));
468 VM_CheckEmptyString(string);
469 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(Cvar_VariableString(string));
474 ========================
477 const string VM_cvar_defstring (string, ...)
478 ========================
480 void VM_cvar_defstring (void)
482 char string[VM_STRINGTEMP_LENGTH];
483 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar_defstring);
484 VM_VarString(0, string, sizeof(string));
485 VM_CheckEmptyString(string);
486 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(Cvar_VariableDefString(string));
492 void cvar_set (string,string, ...)
495 void VM_cvar_set (void)
498 char string[VM_STRINGTEMP_LENGTH];
499 VM_SAFEPARMCOUNTRANGE(2,8,VM_cvar_set);
500 VM_VarString(1, string, sizeof(string));
501 name = PRVM_G_STRING(OFS_PARM0);
502 VM_CheckEmptyString(name);
503 Cvar_Set(name, string);
513 void VM_dprint (void)
515 char string[VM_STRINGTEMP_LENGTH];
516 VM_SAFEPARMCOUNTRANGE(1, 8, VM_dprint);
517 if (developer.integer)
519 VM_VarString(0, string, sizeof(string));
521 Con_Printf("%s", string);
523 Con_Printf("%s: %s", PRVM_NAME, string);
541 VM_SAFEPARMCOUNT(1, VM_ftos);
543 v = PRVM_G_FLOAT(OFS_PARM0);
545 if ((float)((int)v) == v)
546 sprintf(s, "%i", (int)v);
549 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(s);
564 VM_SAFEPARMCOUNT(1,VM_fabs);
566 v = PRVM_G_FLOAT(OFS_PARM0);
567 PRVM_G_FLOAT(OFS_RETURN) = fabs(v);
582 VM_SAFEPARMCOUNT(1,VM_vtos);
584 sprintf (s, "'%5.1f %5.1f %5.1f'", PRVM_G_VECTOR(OFS_PARM0)[0], PRVM_G_VECTOR(OFS_PARM0)[1], PRVM_G_VECTOR(OFS_PARM0)[2]);
585 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(s);
600 VM_SAFEPARMCOUNT(1, VM_etos);
602 sprintf (s, "entity %i", PRVM_G_EDICTNUM(OFS_PARM0));
603 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(s);
610 float stof(...[string])
615 char string[VM_STRINGTEMP_LENGTH];
616 VM_SAFEPARMCOUNTRANGE(1, 8, VM_stof);
617 VM_VarString(0, string, sizeof(string));
618 PRVM_G_FLOAT(OFS_RETURN) = atof(string);
622 ========================
626 ========================
630 VM_SAFEPARMCOUNT(1, VM_itof);
631 PRVM_G_FLOAT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
635 ========================
638 entity ftoe(float num)
639 ========================
644 VM_SAFEPARMCOUNT(1, VM_ftoe);
646 ent = (int)PRVM_G_FLOAT(OFS_PARM0);
647 if (ent < 0 || ent >= MAX_EDICTS || PRVM_PROG_TO_EDICT(ent)->priv.required->free)
648 ent = 0; // return world instead of a free or invalid entity
650 PRVM_G_INT(OFS_RETURN) = ent;
657 string strftime(float uselocaltime, string[, string ...])
660 void VM_strftime(void)
664 char fmt[VM_STRINGTEMP_LENGTH];
665 char result[VM_STRINGTEMP_LENGTH];
666 VM_SAFEPARMCOUNTRANGE(2, 8, VM_strftime);
667 VM_VarString(1, fmt, sizeof(fmt));
669 if (PRVM_G_FLOAT(OFS_PARM0))
675 PRVM_G_FLOAT(OFS_RETURN) = 0;
678 strftime(result, sizeof(result), fmt, tm);
679 PRVM_G_FLOAT(OFS_RETURN) = PRVM_SetTempString(result);
693 VM_SAFEPARMCOUNT(0, VM_spawn);
694 prog->xfunction->builtinsprofile += 20;
695 ed = PRVM_ED_Alloc();
707 void VM_remove (void)
710 prog->xfunction->builtinsprofile += 20;
712 VM_SAFEPARMCOUNT(1, VM_remove);
714 ed = PRVM_G_EDICT(OFS_PARM0);
715 if( PRVM_NUM_FOR_EDICT(ed) <= prog->reserved_edicts )
717 if (developer.integer >= 1)
718 VM_Warning( "VM_remove: tried to remove the null entity or a reserved entity!\n" );
720 else if( ed->priv.required->free )
722 if (developer.integer >= 1)
723 VM_Warning( "VM_remove: tried to remove an already freed entity!\n" );
733 entity find(entity start, .string field, string match)
744 VM_SAFEPARMCOUNT(3,VM_find);
746 e = PRVM_G_EDICTNUM(OFS_PARM0);
747 f = PRVM_G_INT(OFS_PARM1);
748 s = PRVM_G_STRING(OFS_PARM2);
750 // LordHavoc: apparently BloodMage does a find(world, weaponmodel, "") and
751 // expects it to find all the monsters, so we must be careful to support
754 for (e++ ; e < prog->num_edicts ; e++)
756 prog->xfunction->builtinsprofile++;
757 ed = PRVM_EDICT_NUM(e);
758 if (ed->priv.required->free)
760 t = PRVM_E_STRING(ed,f);
770 VM_RETURN_EDICT(prog->edicts);
777 entity findfloat(entity start, .float field, float match)
778 entity findentity(entity start, .entity field, entity match)
781 // LordHavoc: added this for searching float, int, and entity reference fields
782 void VM_findfloat (void)
789 VM_SAFEPARMCOUNT(3,VM_findfloat);
791 e = PRVM_G_EDICTNUM(OFS_PARM0);
792 f = PRVM_G_INT(OFS_PARM1);
793 s = PRVM_G_FLOAT(OFS_PARM2);
795 for (e++ ; e < prog->num_edicts ; e++)
797 prog->xfunction->builtinsprofile++;
798 ed = PRVM_EDICT_NUM(e);
799 if (ed->priv.required->free)
801 if (PRVM_E_FLOAT(ed,f) == s)
808 VM_RETURN_EDICT(prog->edicts);
815 entity findchain(.string field, string match)
818 // chained search for strings in entity fields
819 // entity(.string field, string match) findchain = #402;
820 void VM_findchain (void)
825 prvm_edict_t *ent, *chain;
827 VM_SAFEPARMCOUNT(2,VM_findchain);
829 if (prog->fieldoffsets.chain < 0)
830 PRVM_ERROR("VM_findchain: %s doesnt have a chain field !", PRVM_NAME);
832 chain = prog->edicts;
834 f = PRVM_G_INT(OFS_PARM0);
835 s = PRVM_G_STRING(OFS_PARM1);
837 // LordHavoc: apparently BloodMage does a find(world, weaponmodel, "") and
838 // expects it to find all the monsters, so we must be careful to support
841 ent = PRVM_NEXT_EDICT(prog->edicts);
842 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
844 prog->xfunction->builtinsprofile++;
845 if (ent->priv.required->free)
847 t = PRVM_E_STRING(ent,f);
853 PRVM_EDICTFIELDVALUE(ent,prog->fieldoffsets.chain)->edict = PRVM_NUM_FOR_EDICT(chain);
857 VM_RETURN_EDICT(chain);
864 entity findchainfloat(.string field, float match)
865 entity findchainentity(.string field, entity match)
868 // LordHavoc: chained search for float, int, and entity reference fields
869 // entity(.string field, float match) findchainfloat = #403;
870 void VM_findchainfloat (void)
875 prvm_edict_t *ent, *chain;
877 VM_SAFEPARMCOUNT(2, VM_findchainfloat);
879 if (prog->fieldoffsets.chain < 0)
880 PRVM_ERROR("VM_findchainfloat: %s doesnt have a chain field !", PRVM_NAME);
882 chain = (prvm_edict_t *)prog->edicts;
884 f = PRVM_G_INT(OFS_PARM0);
885 s = PRVM_G_FLOAT(OFS_PARM1);
887 ent = PRVM_NEXT_EDICT(prog->edicts);
888 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
890 prog->xfunction->builtinsprofile++;
891 if (ent->priv.required->free)
893 if (PRVM_E_FLOAT(ent,f) != s)
896 PRVM_EDICTFIELDVALUE(ent,prog->fieldoffsets.chain)->edict = PRVM_EDICT_TO_PROG(chain);
900 VM_RETURN_EDICT(chain);
904 ========================
907 entity findflags(entity start, .float field, float match)
908 ========================
910 // LordHavoc: search for flags in float fields
911 void VM_findflags (void)
918 VM_SAFEPARMCOUNT(3, VM_findflags);
921 e = PRVM_G_EDICTNUM(OFS_PARM0);
922 f = PRVM_G_INT(OFS_PARM1);
923 s = (int)PRVM_G_FLOAT(OFS_PARM2);
925 for (e++ ; e < prog->num_edicts ; e++)
927 prog->xfunction->builtinsprofile++;
928 ed = PRVM_EDICT_NUM(e);
929 if (ed->priv.required->free)
931 if (!PRVM_E_FLOAT(ed,f))
933 if ((int)PRVM_E_FLOAT(ed,f) & s)
940 VM_RETURN_EDICT(prog->edicts);
944 ========================
947 entity findchainflags(.float field, float match)
948 ========================
950 // LordHavoc: chained search for flags in float fields
951 void VM_findchainflags (void)
956 prvm_edict_t *ent, *chain;
958 VM_SAFEPARMCOUNT(2, VM_findchainflags);
960 if (prog->fieldoffsets.chain < 0)
961 PRVM_ERROR("VM_findchainflags: %s doesnt have a chain field !", PRVM_NAME);
963 chain = (prvm_edict_t *)prog->edicts;
965 f = PRVM_G_INT(OFS_PARM0);
966 s = (int)PRVM_G_FLOAT(OFS_PARM1);
968 ent = PRVM_NEXT_EDICT(prog->edicts);
969 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
971 prog->xfunction->builtinsprofile++;
972 if (ent->priv.required->free)
974 if (!PRVM_E_FLOAT(ent,f))
976 if (!((int)PRVM_E_FLOAT(ent,f) & s))
979 PRVM_EDICTFIELDVALUE(ent,prog->fieldoffsets.chain)->edict = PRVM_EDICT_TO_PROG(chain);
983 VM_RETURN_EDICT(chain);
990 string precache_sound (string sample)
993 void VM_precache_sound (void)
997 VM_SAFEPARMCOUNT(1, VM_precache_sound);
999 s = PRVM_G_STRING(OFS_PARM0);
1000 PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
1001 VM_CheckEmptyString(s);
1003 if(snd_initialized.integer && !S_PrecacheSound(s, true, false))
1005 VM_Warning("VM_precache_sound: Failed to load %s for %s\n", s, PRVM_NAME);
1014 returns the same string as output
1016 does nothing, only used by qcc to build .pak archives
1019 void VM_precache_file (void)
1021 VM_SAFEPARMCOUNT(1,VM_precache_file);
1022 // precache_file is only used to copy files with qcc, it does nothing
1023 PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
1033 void VM_coredump (void)
1035 VM_SAFEPARMCOUNT(0,VM_coredump);
1037 Cbuf_AddText("prvm_edicts ");
1038 Cbuf_AddText(PRVM_NAME);
1049 void PRVM_StackTrace(void);
1050 void VM_stackdump (void)
1052 VM_SAFEPARMCOUNT(0, VM_stackdump);
1067 VM_SAFEPARMCOUNT(0, VM_crash);
1069 PRVM_ERROR("Crash called by %s",PRVM_NAME);
1079 void VM_traceon (void)
1081 VM_SAFEPARMCOUNT(0,VM_traceon);
1093 void VM_traceoff (void)
1095 VM_SAFEPARMCOUNT(0,VM_traceoff);
1097 prog->trace = false;
1107 void VM_eprint (void)
1109 VM_SAFEPARMCOUNT(1,VM_eprint);
1111 PRVM_ED_PrintNum (PRVM_G_EDICTNUM(OFS_PARM0), NULL);
1124 VM_SAFEPARMCOUNT(1,VM_rint);
1126 f = PRVM_G_FLOAT(OFS_PARM0);
1128 PRVM_G_FLOAT(OFS_RETURN) = floor(f + 0.5);
1130 PRVM_G_FLOAT(OFS_RETURN) = ceil(f - 0.5);
1140 void VM_floor (void)
1142 VM_SAFEPARMCOUNT(1,VM_floor);
1144 PRVM_G_FLOAT(OFS_RETURN) = floor(PRVM_G_FLOAT(OFS_PARM0));
1156 VM_SAFEPARMCOUNT(1,VM_ceil);
1158 PRVM_G_FLOAT(OFS_RETURN) = ceil(PRVM_G_FLOAT(OFS_PARM0));
1166 entity nextent(entity)
1169 void VM_nextent (void)
1174 VM_SAFEPARMCOUNT(1, VM_nextent);
1176 i = PRVM_G_EDICTNUM(OFS_PARM0);
1179 prog->xfunction->builtinsprofile++;
1181 if (i == prog->num_edicts)
1183 VM_RETURN_EDICT(prog->edicts);
1186 ent = PRVM_EDICT_NUM(i);
1187 if (!ent->priv.required->free)
1189 VM_RETURN_EDICT(ent);
1195 //=============================================================================
1202 changelevel(string map)
1205 void VM_changelevel (void)
1207 VM_SAFEPARMCOUNT(1, VM_changelevel);
1211 VM_Warning("VM_changelevel: game is not server (%s)\n", PRVM_NAME);
1215 // make sure we don't issue two changelevels
1216 if (svs.changelevel_issued)
1218 svs.changelevel_issued = true;
1220 Cbuf_AddText (va("changelevel %s\n",PRVM_G_STRING(OFS_PARM0)));
1232 VM_SAFEPARMCOUNT(1,VM_sin);
1233 PRVM_G_FLOAT(OFS_RETURN) = sin(PRVM_G_FLOAT(OFS_PARM0));
1244 VM_SAFEPARMCOUNT(1,VM_cos);
1245 PRVM_G_FLOAT(OFS_RETURN) = cos(PRVM_G_FLOAT(OFS_PARM0));
1257 VM_SAFEPARMCOUNT(1,VM_sqrt);
1258 PRVM_G_FLOAT(OFS_RETURN) = sqrt(PRVM_G_FLOAT(OFS_PARM0));
1270 VM_SAFEPARMCOUNT(1,VM_asin);
1271 PRVM_G_FLOAT(OFS_RETURN) = asin(PRVM_G_FLOAT(OFS_PARM0));
1282 VM_SAFEPARMCOUNT(1,VM_acos);
1283 PRVM_G_FLOAT(OFS_RETURN) = acos(PRVM_G_FLOAT(OFS_PARM0));
1294 VM_SAFEPARMCOUNT(1,VM_atan);
1295 PRVM_G_FLOAT(OFS_RETURN) = atan(PRVM_G_FLOAT(OFS_PARM0));
1301 float atan2(float,float)
1304 void VM_atan2 (void)
1306 VM_SAFEPARMCOUNT(2,VM_atan2);
1307 PRVM_G_FLOAT(OFS_RETURN) = atan2(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1318 VM_SAFEPARMCOUNT(1,VM_tan);
1319 PRVM_G_FLOAT(OFS_RETURN) = tan(PRVM_G_FLOAT(OFS_PARM0));
1326 Returns a vector of length < 1 and > 0
1331 void VM_randomvec (void)
1336 VM_SAFEPARMCOUNT(0, VM_randomvec);
1341 temp[0] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1342 temp[1] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1343 temp[2] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1345 while (DotProduct(temp, temp) >= 1);
1346 VectorCopy (temp, PRVM_G_VECTOR(OFS_RETURN));
1349 temp[0] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1350 temp[1] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1351 temp[2] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1352 // length returned always > 0
1353 length = (rand()&32766 + 1) * (1.0 / 32767.0) / VectorLength(temp);
1354 VectorScale(temp,length, temp);*/
1355 //VectorCopy(temp, PRVM_G_VECTOR(OFS_RETURN));
1358 //=============================================================================
1364 float registercvar (string name, string value[, float flags])
1367 void VM_registercvar (void)
1369 const char *name, *value;
1372 VM_SAFEPARMCOUNTRANGE(2, 3, VM_registercvar);
1374 name = PRVM_G_STRING(OFS_PARM0);
1375 value = PRVM_G_STRING(OFS_PARM1);
1376 flags = prog->argc >= 3 ? (int)PRVM_G_FLOAT(OFS_PARM2) : 0;
1377 PRVM_G_FLOAT(OFS_RETURN) = 0;
1379 if(flags > CVAR_MAXFLAGSVAL)
1382 // first check to see if it has already been defined
1383 if (Cvar_FindVar (name))
1386 // check for overlap with a command
1387 if (Cmd_Exists (name))
1389 VM_Warning("VM_registercvar: %s is a command\n", name);
1393 Cvar_Get(name, value, flags);
1395 PRVM_G_FLOAT(OFS_RETURN) = 1; // success
1403 returns the minimum of two supplied floats
1405 float min(float a, float b, ...[float])
1410 VM_SAFEPARMCOUNTRANGE(2, 8, VM_min);
1411 // LordHavoc: 3+ argument enhancement suggested by FrikaC
1412 if (prog->argc >= 3)
1415 float f = PRVM_G_FLOAT(OFS_PARM0);
1416 for (i = 1;i < prog->argc;i++)
1417 if (f > PRVM_G_FLOAT((OFS_PARM0+i*3)))
1418 f = PRVM_G_FLOAT((OFS_PARM0+i*3));
1419 PRVM_G_FLOAT(OFS_RETURN) = f;
1422 PRVM_G_FLOAT(OFS_RETURN) = min(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1429 returns the maximum of two supplied floats
1431 float max(float a, float b, ...[float])
1436 VM_SAFEPARMCOUNTRANGE(2, 8, VM_max);
1437 // LordHavoc: 3+ argument enhancement suggested by FrikaC
1438 if (prog->argc >= 3)
1441 float f = PRVM_G_FLOAT(OFS_PARM0);
1442 for (i = 1;i < prog->argc;i++)
1443 if (f < PRVM_G_FLOAT((OFS_PARM0+i*3)))
1444 f = PRVM_G_FLOAT((OFS_PARM0+i*3));
1445 PRVM_G_FLOAT(OFS_RETURN) = f;
1448 PRVM_G_FLOAT(OFS_RETURN) = max(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1455 returns number bounded by supplied range
1457 float bound(float min, float value, float max)
1460 void VM_bound (void)
1462 VM_SAFEPARMCOUNT(3,VM_bound);
1463 PRVM_G_FLOAT(OFS_RETURN) = bound(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1), PRVM_G_FLOAT(OFS_PARM2));
1470 returns a raised to power b
1472 float pow(float a, float b)
1477 VM_SAFEPARMCOUNT(2,VM_pow);
1478 PRVM_G_FLOAT(OFS_RETURN) = pow(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1481 void VM_Files_Init(void)
1484 for (i = 0;i < PRVM_MAX_OPENFILES;i++)
1485 prog->openfiles[i] = NULL;
1488 void VM_Files_CloseAll(void)
1491 for (i = 0;i < PRVM_MAX_OPENFILES;i++)
1493 if (prog->openfiles[i])
1494 FS_Close(prog->openfiles[i]);
1495 prog->openfiles[i] = NULL;
1499 static qfile_t *VM_GetFileHandle( int index )
1501 if (index < 0 || index >= PRVM_MAX_OPENFILES)
1503 Con_Printf("VM_GetFileHandle: invalid file handle %i used in %s\n", index, PRVM_NAME);
1506 if (prog->openfiles[index] == NULL)
1508 Con_Printf("VM_GetFileHandle: no such file handle %i (or file has been closed) in %s\n", index, PRVM_NAME);
1511 return prog->openfiles[index];
1518 float fopen(string filename, float mode)
1521 // float(string filename, float mode) fopen = #110;
1522 // opens a file inside quake/gamedir/data/ (mode is FILE_READ, FILE_APPEND, or FILE_WRITE),
1523 // returns fhandle >= 0 if successful, or fhandle < 0 if unable to open file for any reason
1527 const char *modestring, *filename;
1529 VM_SAFEPARMCOUNT(2,VM_fopen);
1531 for (filenum = 0;filenum < PRVM_MAX_OPENFILES;filenum++)
1532 if (prog->openfiles[filenum] == NULL)
1534 if (filenum >= PRVM_MAX_OPENFILES)
1536 PRVM_G_FLOAT(OFS_RETURN) = -2;
1537 VM_Warning("VM_fopen: %s ran out of file handles (%i)\n", PRVM_NAME, PRVM_MAX_OPENFILES);
1540 mode = (int)PRVM_G_FLOAT(OFS_PARM1);
1543 case 0: // FILE_READ
1546 case 1: // FILE_APPEND
1549 case 2: // FILE_WRITE
1553 PRVM_G_FLOAT(OFS_RETURN) = -3;
1554 VM_Warning("VM_fopen: %s: no such mode %i (valid: 0 = read, 1 = append, 2 = write)\n", PRVM_NAME, mode);
1557 filename = PRVM_G_STRING(OFS_PARM0);
1559 prog->openfiles[filenum] = FS_Open(va("data/%s", filename), modestring, false, false);
1560 if (prog->openfiles[filenum] == NULL && mode == 0)
1561 prog->openfiles[filenum] = FS_Open(va("%s", filename), modestring, false, false);
1563 if (prog->openfiles[filenum] == NULL)
1565 PRVM_G_FLOAT(OFS_RETURN) = -1;
1566 if (developer.integer >= 100)
1567 VM_Warning("VM_fopen: %s: %s mode %s failed\n", PRVM_NAME, filename, modestring);
1571 PRVM_G_FLOAT(OFS_RETURN) = filenum;
1572 if (developer.integer >= 100)
1573 Con_Printf("VM_fopen: %s: %s mode %s opened as #%i\n", PRVM_NAME, filename, modestring, filenum);
1581 fclose(float fhandle)
1584 //void(float fhandle) fclose = #111; // closes a file
1585 void VM_fclose(void)
1589 VM_SAFEPARMCOUNT(1,VM_fclose);
1591 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1592 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
1594 VM_Warning("VM_fclose: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1597 if (prog->openfiles[filenum] == NULL)
1599 VM_Warning("VM_fclose: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1602 FS_Close(prog->openfiles[filenum]);
1603 prog->openfiles[filenum] = NULL;
1604 if (developer.integer >= 100)
1605 Con_Printf("VM_fclose: %s: #%i closed\n", PRVM_NAME, filenum);
1612 string fgets(float fhandle)
1615 //string(float fhandle) fgets = #112; // reads a line of text from the file and returns as a tempstring
1619 char string[VM_STRINGTEMP_LENGTH];
1622 VM_SAFEPARMCOUNT(1,VM_fgets);
1624 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1625 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
1627 VM_Warning("VM_fgets: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1630 if (prog->openfiles[filenum] == NULL)
1632 VM_Warning("VM_fgets: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1638 c = FS_Getc(prog->openfiles[filenum]);
1639 if (c == '\r' || c == '\n' || c < 0)
1641 if (end < VM_STRINGTEMP_LENGTH - 1)
1645 // remove \n following \r
1648 c = FS_Getc(prog->openfiles[filenum]);
1650 FS_UnGetc(prog->openfiles[filenum], (unsigned char)c);
1652 if (developer.integer >= 100)
1653 Con_Printf("fgets: %s: %s\n", PRVM_NAME, string);
1655 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string);
1657 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
1664 fputs(float fhandle, string s)
1667 //void(float fhandle, string s) fputs = #113; // writes a line of text to the end of the file
1671 char string[VM_STRINGTEMP_LENGTH];
1674 VM_SAFEPARMCOUNT(2,VM_fputs);
1676 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1677 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
1679 VM_Warning("VM_fputs: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1682 if (prog->openfiles[filenum] == NULL)
1684 VM_Warning("VM_fputs: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1687 VM_VarString(1, string, sizeof(string));
1688 if ((stringlength = (int)strlen(string)))
1689 FS_Write(prog->openfiles[filenum], string, stringlength);
1690 if (developer.integer >= 100)
1691 Con_Printf("fputs: %s: %s\n", PRVM_NAME, string);
1698 writetofile(float fhandle, entity ent)
1701 void VM_writetofile(void)
1706 VM_SAFEPARMCOUNT(2, VM_writetofile);
1708 file = VM_GetFileHandle( (int)PRVM_G_FLOAT(OFS_PARM0) );
1711 VM_Warning("VM_writetofile: invalid or closed file handle\n");
1715 ent = PRVM_G_EDICT(OFS_PARM1);
1716 if(ent->priv.required->free)
1718 VM_Warning("VM_writetofile: %s: entity %i is free !\n", PRVM_NAME, PRVM_NUM_FOR_EDICT(ent));
1722 PRVM_ED_Write (file, ent);
1729 float strlen(string s)
1732 //float(string s) strlen = #114; // returns how many characters are in a string
1733 void VM_strlen(void)
1735 VM_SAFEPARMCOUNT(1,VM_strlen);
1737 PRVM_G_FLOAT(OFS_RETURN) = strlen(PRVM_G_STRING(OFS_PARM0));
1740 // DRESK - Decolorized String
1745 string strdecolorize(string s)
1748 // string (string s) strdecolorize = #472; // returns the passed in string with color codes stripped
1749 void VM_strdecolorize(void)
1751 char szNewString[VM_STRINGTEMP_LENGTH];
1752 const char *szString;
1755 VM_SAFEPARMCOUNT(1,VM_strdecolorize);
1756 szString = PRVM_G_STRING(OFS_PARM0);
1758 COM_StringDecolorize(szString, 0, szNewString, sizeof(szNewString), TRUE);
1760 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(szNewString);
1763 // DRESK - String Length (not counting color codes)
1768 float strlennocol(string s)
1771 // float(string s) strlennocol = #471; // returns how many characters are in a string not including color codes
1772 // For example, ^2Dresk returns a length of 5
1773 void VM_strlennocol(void)
1775 const char *szString;
1778 VM_SAFEPARMCOUNT(1,VM_strlennocol);
1780 szString = PRVM_G_STRING(OFS_PARM0);
1782 nCnt = COM_StringLengthNoColors(szString, 0, NULL);
1784 PRVM_G_FLOAT(OFS_RETURN) = nCnt;
1787 // DRESK - String to Uppercase and Lowercase
1792 string strtolower(string s)
1795 // string (string s) strtolower = #480; // returns passed in string in lowercase form
1796 void VM_strtolower(void)
1798 char szNewString[VM_STRINGTEMP_LENGTH];
1799 const char *szString;
1802 VM_SAFEPARMCOUNT(1,VM_strtolower);
1803 szString = PRVM_G_STRING(OFS_PARM0);
1805 COM_ToLowerString(szString, szNewString, sizeof(szNewString) );
1807 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(szNewString);
1814 string strtoupper(string s)
1817 // string (string s) strtoupper = #481; // returns passed in string in uppercase form
1818 void VM_strtoupper(void)
1820 char szNewString[VM_STRINGTEMP_LENGTH];
1821 const char *szString;
1824 VM_SAFEPARMCOUNT(1,VM_strtoupper);
1825 szString = PRVM_G_STRING(OFS_PARM0);
1827 COM_ToUpperString(szString, szNewString, sizeof(szNewString) );
1829 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(szNewString);
1836 string strcat(string,string,...[string])
1839 //string(string s1, string s2) strcat = #115;
1840 // concatenates two strings (for example "abc", "def" would return "abcdef")
1841 // and returns as a tempstring
1842 void VM_strcat(void)
1844 char s[VM_STRINGTEMP_LENGTH];
1845 VM_SAFEPARMCOUNTRANGE(1, 8, VM_strcat);
1847 VM_VarString(0, s, sizeof(s));
1848 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(s);
1855 string substring(string s, float start, float length)
1858 // string(string s, float start, float length) substring = #116;
1859 // returns a section of a string as a tempstring
1860 void VM_substring(void)
1862 int i, start, length;
1864 char string[VM_STRINGTEMP_LENGTH];
1866 VM_SAFEPARMCOUNT(3,VM_substring);
1868 s = PRVM_G_STRING(OFS_PARM0);
1869 start = (int)PRVM_G_FLOAT(OFS_PARM1);
1870 length = (int)PRVM_G_FLOAT(OFS_PARM2);
1871 for (i = 0;i < start && *s;i++, s++);
1872 for (i = 0;i < (int)sizeof(string) - 1 && *s && i < length;i++, s++)
1875 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string);
1882 vector stov(string s)
1885 //vector(string s) stov = #117; // returns vector value from a string
1888 char string[VM_STRINGTEMP_LENGTH];
1890 VM_SAFEPARMCOUNT(1,VM_stov);
1892 VM_VarString(0, string, sizeof(string));
1893 Math_atov(string, PRVM_G_VECTOR(OFS_RETURN));
1900 string strzone(string s)
1903 //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)
1904 void VM_strzone(void)
1907 char string[VM_STRINGTEMP_LENGTH];
1910 VM_SAFEPARMCOUNT(1,VM_strzone);
1912 VM_VarString(0, string, sizeof(string));
1913 alloclen = strlen(string) + 1;
1914 PRVM_G_INT(OFS_RETURN) = PRVM_AllocString(alloclen, &out);
1915 memcpy(out, string, alloclen);
1925 //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!!!)
1926 void VM_strunzone(void)
1928 VM_SAFEPARMCOUNT(1,VM_strunzone);
1929 PRVM_FreeString(PRVM_G_INT(OFS_PARM0));
1934 VM_command (used by client and menu)
1936 clientcommand(float client, string s) (for client and menu)
1939 //void(entity e, string s) clientcommand = #440; // executes a command string as if it came from the specified client
1940 //this function originally written by KrimZon, made shorter by LordHavoc
1941 void VM_clcommand (void)
1943 client_t *temp_client;
1946 VM_SAFEPARMCOUNT(2,VM_clcommand);
1948 i = (int)PRVM_G_FLOAT(OFS_PARM0);
1949 if (!sv.active || i < 0 || i >= svs.maxclients || !svs.clients[i].active)
1951 VM_Warning("VM_clientcommand: %s: invalid client/server is not active !\n", PRVM_NAME);
1955 temp_client = host_client;
1956 host_client = svs.clients + i;
1957 Cmd_ExecuteString (PRVM_G_STRING(OFS_PARM1), src_client);
1958 host_client = temp_client;
1966 float tokenize(string s)
1969 //float(string s) tokenize = #441; // takes apart a string into individal words (access them with argv), returns how many
1970 //this function originally written by KrimZon, made shorter by LordHavoc
1971 //20040203: rewritten by LordHavoc (no longer uses allocations)
1974 void VM_tokenize (void)
1978 VM_SAFEPARMCOUNT(1,VM_tokenize);
1980 p = PRVM_G_STRING(OFS_PARM0);
1983 while(COM_ParseToken_VM_Tokenize(&p, false))
1985 if (num_tokens >= (int)(sizeof(tokens)/sizeof(tokens[0])))
1987 tokens[num_tokens++] = PRVM_SetTempString(com_token);
1990 PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
1995 VM_tokenizebyseparator
1997 float tokenizebyseparator(string s, string separator1, ...)
2000 //float(string s, string separator1, ...) tokenizebyseparator = #479; // takes apart a string into individal words (access them with argv), returns how many
2001 //this function returns the token preceding each instance of a separator (of
2002 //which there can be multiple), and the text following the last separator
2003 //useful for parsing certain kinds of data like IP addresses
2005 //numnumbers = tokenizebyseparator("10.1.2.3", ".");
2006 //returns 4 and the tokens "10" "1" "2" "3".
2007 void VM_tokenizebyseparator (void)
2011 int separatorlen[7];
2012 const char *separators[7];
2015 char tokentext[MAX_INPUTLINE];
2017 VM_SAFEPARMCOUNTRANGE(2, 8,VM_tokenizebyseparator);
2019 p = PRVM_G_STRING(OFS_PARM0);
2022 for (j = 1;j < prog->argc;j++)
2024 // skip any blank separator strings
2025 const char *s = PRVM_G_STRING(OFS_PARM0+j*3);
2028 separators[numseparators] = s;
2029 separatorlen[numseparators] = strlen(s);
2036 while (num_tokens < (int)(sizeof(tokens)/sizeof(tokens[0])))
2038 token = tokentext + j;
2041 for (k = 0;k < numseparators;k++)
2043 if (!strncmp(p, separators[k], separatorlen[k]))
2045 p += separatorlen[k];
2049 if (k < numseparators)
2051 if (j < (int)sizeof(tokentext)-1)
2052 tokentext[j++] = *p;
2055 if (j >= (int)sizeof(tokentext))
2058 tokens[num_tokens++] = PRVM_SetTempString(token);
2063 PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
2066 //string(float n) argv = #442; // returns a word from the tokenized string (returns nothing for an invalid index)
2067 //this function originally written by KrimZon, made shorter by LordHavoc
2072 VM_SAFEPARMCOUNT(1,VM_argv);
2074 token_num = (int)PRVM_G_FLOAT(OFS_PARM0);
2076 if (token_num >= 0 && token_num < num_tokens)
2077 PRVM_G_INT(OFS_RETURN) = tokens[token_num];
2079 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
2089 void VM_isserver(void)
2091 VM_SAFEPARMCOUNT(0,VM_serverstate);
2093 PRVM_G_FLOAT(OFS_RETURN) = sv.active && (svs.maxclients > 1 || cls.state == ca_dedicated);
2103 void VM_clientcount(void)
2105 VM_SAFEPARMCOUNT(0,VM_clientcount);
2107 PRVM_G_FLOAT(OFS_RETURN) = svs.maxclients;
2117 void VM_clientstate(void)
2119 VM_SAFEPARMCOUNT(0,VM_clientstate);
2121 PRVM_G_FLOAT(OFS_RETURN) = cls.state;
2128 float getostype(void)
2130 */ // not used at the moment -> not included in the common list
2131 void VM_getostype(void)
2133 VM_SAFEPARMCOUNT(0,VM_getostype);
2138 OS_MAC - not supported
2142 PRVM_G_FLOAT(OFS_RETURN) = 0;
2143 #elif defined(MACOSX)
2144 PRVM_G_FLOAT(OFS_RETURN) = 2;
2146 PRVM_G_FLOAT(OFS_RETURN) = 1;
2154 vector getmousepos()
2157 void VM_getmousepos(void)
2160 VM_SAFEPARMCOUNT(0,VM_getmousepos);
2162 PRVM_G_VECTOR(OFS_RETURN)[0] = in_mouse_x * vid_conwidth.integer / vid.width;
2163 PRVM_G_VECTOR(OFS_RETURN)[1] = in_mouse_y * vid_conheight.integer / vid.height;
2164 PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
2174 void VM_gettime(void)
2176 VM_SAFEPARMCOUNT(0,VM_gettime);
2178 PRVM_G_FLOAT(OFS_RETURN) = (float) realtime;
2185 loadfromdata(string data)
2188 void VM_loadfromdata(void)
2190 VM_SAFEPARMCOUNT(1,VM_loadentsfromfile);
2192 PRVM_ED_LoadFromFile(PRVM_G_STRING(OFS_PARM0));
2196 ========================
2199 parseentitydata(entity ent, string data)
2200 ========================
2202 void VM_parseentitydata(void)
2207 VM_SAFEPARMCOUNT(2, VM_parseentitydata);
2209 // get edict and test it
2210 ent = PRVM_G_EDICT(OFS_PARM0);
2211 if (ent->priv.required->free)
2212 PRVM_ERROR ("VM_parseentitydata: %s: Can only set already spawned entities (entity %i is free)!", PRVM_NAME, PRVM_NUM_FOR_EDICT(ent));
2214 data = PRVM_G_STRING(OFS_PARM1);
2216 // parse the opening brace
2217 if (!COM_ParseToken_Simple(&data, false) || com_token[0] != '{' )
2218 PRVM_ERROR ("VM_parseentitydata: %s: Couldn't parse entity data:\n%s", PRVM_NAME, data );
2220 PRVM_ED_ParseEdict (data, ent);
2227 loadfromfile(string file)
2230 void VM_loadfromfile(void)
2232 const char *filename;
2235 VM_SAFEPARMCOUNT(1,VM_loadfromfile);
2237 filename = PRVM_G_STRING(OFS_PARM0);
2238 if (FS_CheckNastyPath(filename, false))
2240 PRVM_G_FLOAT(OFS_RETURN) = -4;
2241 VM_Warning("VM_loadfromfile: %s dangerous or non-portable filename \"%s\" not allowed. (contains : or \\ or begins with .. or /)\n", PRVM_NAME, filename);
2245 // not conform with VM_fopen
2246 data = (char *)FS_LoadFile(filename, tempmempool, false, NULL);
2248 PRVM_G_FLOAT(OFS_RETURN) = -1;
2250 PRVM_ED_LoadFromFile(data);
2261 float mod(float val, float m)
2264 void VM_modulo(void)
2267 VM_SAFEPARMCOUNT(2,VM_module);
2269 val = (int) PRVM_G_FLOAT(OFS_PARM0);
2270 m = (int) PRVM_G_FLOAT(OFS_PARM1);
2272 PRVM_G_FLOAT(OFS_RETURN) = (float) (val % m);
2275 void VM_Search_Init(void)
2278 for (i = 0;i < PRVM_MAX_OPENSEARCHES;i++)
2279 prog->opensearches[i] = NULL;
2282 void VM_Search_Reset(void)
2285 // reset the fssearch list
2286 for(i = 0; i < PRVM_MAX_OPENSEARCHES; i++)
2288 if(prog->opensearches[i])
2289 FS_FreeSearch(prog->opensearches[i]);
2290 prog->opensearches[i] = NULL;
2298 float search_begin(string pattern, float caseinsensitive, float quiet)
2301 void VM_search_begin(void)
2304 const char *pattern;
2305 int caseinsens, quiet;
2307 VM_SAFEPARMCOUNT(3, VM_search_begin);
2309 pattern = PRVM_G_STRING(OFS_PARM0);
2311 VM_CheckEmptyString(pattern);
2313 caseinsens = (int)PRVM_G_FLOAT(OFS_PARM1);
2314 quiet = (int)PRVM_G_FLOAT(OFS_PARM2);
2316 for(handle = 0; handle < PRVM_MAX_OPENSEARCHES; handle++)
2317 if(!prog->opensearches[handle])
2320 if(handle >= PRVM_MAX_OPENSEARCHES)
2322 PRVM_G_FLOAT(OFS_RETURN) = -2;
2323 VM_Warning("VM_search_begin: %s ran out of search handles (%i)\n", PRVM_NAME, PRVM_MAX_OPENSEARCHES);
2327 if(!(prog->opensearches[handle] = FS_Search(pattern,caseinsens, quiet)))
2328 PRVM_G_FLOAT(OFS_RETURN) = -1;
2330 PRVM_G_FLOAT(OFS_RETURN) = handle;
2337 void search_end(float handle)
2340 void VM_search_end(void)
2343 VM_SAFEPARMCOUNT(1, VM_search_end);
2345 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
2347 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
2349 VM_Warning("VM_search_end: invalid handle %i used in %s\n", handle, PRVM_NAME);
2352 if(prog->opensearches[handle] == NULL)
2354 VM_Warning("VM_search_end: no such handle %i in %s\n", handle, PRVM_NAME);
2358 FS_FreeSearch(prog->opensearches[handle]);
2359 prog->opensearches[handle] = NULL;
2366 float search_getsize(float handle)
2369 void VM_search_getsize(void)
2372 VM_SAFEPARMCOUNT(1, VM_M_search_getsize);
2374 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
2376 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
2378 VM_Warning("VM_search_getsize: invalid handle %i used in %s\n", handle, PRVM_NAME);
2381 if(prog->opensearches[handle] == NULL)
2383 VM_Warning("VM_search_getsize: no such handle %i in %s\n", handle, PRVM_NAME);
2387 PRVM_G_FLOAT(OFS_RETURN) = prog->opensearches[handle]->numfilenames;
2392 VM_search_getfilename
2394 string search_getfilename(float handle, float num)
2397 void VM_search_getfilename(void)
2399 int handle, filenum;
2400 VM_SAFEPARMCOUNT(2, VM_search_getfilename);
2402 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
2403 filenum = (int)PRVM_G_FLOAT(OFS_PARM1);
2405 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
2407 VM_Warning("VM_search_getfilename: invalid handle %i used in %s\n", handle, PRVM_NAME);
2410 if(prog->opensearches[handle] == NULL)
2412 VM_Warning("VM_search_getfilename: no such handle %i in %s\n", handle, PRVM_NAME);
2415 if(filenum < 0 || filenum >= prog->opensearches[handle]->numfilenames)
2417 VM_Warning("VM_search_getfilename: invalid filenum %i in %s\n", filenum, PRVM_NAME);
2421 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog->opensearches[handle]->filenames[filenum]);
2428 string chr(float ascii)
2434 VM_SAFEPARMCOUNT(1, VM_chr);
2436 tmp[0] = (unsigned char) PRVM_G_FLOAT(OFS_PARM0);
2439 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(tmp);
2442 //=============================================================================
2443 // Draw builtins (client & menu)
2449 float iscachedpic(string pic)
2452 void VM_iscachedpic(void)
2454 VM_SAFEPARMCOUNT(1,VM_iscachedpic);
2456 // drawq hasnt such a function, thus always return true
2457 PRVM_G_FLOAT(OFS_RETURN) = false;
2464 string precache_pic(string pic)
2467 void VM_precache_pic(void)
2471 VM_SAFEPARMCOUNT(1, VM_precache_pic);
2473 s = PRVM_G_STRING(OFS_PARM0);
2474 PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
2475 VM_CheckEmptyString (s);
2477 // AK Draw_CachePic is supposed to always return a valid pointer
2478 if( Draw_CachePic(s, false)->tex == r_texture_notexture )
2479 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
2489 void VM_freepic(void)
2493 VM_SAFEPARMCOUNT(1,VM_freepic);
2495 s = PRVM_G_STRING(OFS_PARM0);
2496 VM_CheckEmptyString (s);
2505 float drawcharacter(vector position, float character, vector scale, vector rgb, float alpha, float flag)
2508 void VM_drawcharacter(void)
2510 float *pos,*scale,*rgb;
2513 VM_SAFEPARMCOUNT(6,VM_drawcharacter);
2515 character = (char) PRVM_G_FLOAT(OFS_PARM1);
2518 PRVM_G_FLOAT(OFS_RETURN) = -1;
2519 VM_Warning("VM_drawcharacter: %s passed null character !\n",PRVM_NAME);
2523 pos = PRVM_G_VECTOR(OFS_PARM0);
2524 scale = PRVM_G_VECTOR(OFS_PARM2);
2525 rgb = PRVM_G_VECTOR(OFS_PARM3);
2526 flag = (int)PRVM_G_FLOAT(OFS_PARM5);
2528 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2530 PRVM_G_FLOAT(OFS_RETURN) = -2;
2531 VM_Warning("VM_drawcharacter: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2535 if(pos[2] || scale[2])
2536 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")));
2538 if(!scale[0] || !scale[1])
2540 PRVM_G_FLOAT(OFS_RETURN) = -3;
2541 VM_Warning("VM_drawcharacter: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
2545 DrawQ_String (pos[0], pos[1], &character, 1, scale[0], scale[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM4), flag, NULL, true);
2546 PRVM_G_FLOAT(OFS_RETURN) = 1;
2553 float drawstring(vector position, string text, vector scale, vector rgb, float alpha, float flag)
2556 void VM_drawstring(void)
2558 float *pos,*scale,*rgb;
2561 VM_SAFEPARMCOUNT(6,VM_drawstring);
2563 string = PRVM_G_STRING(OFS_PARM1);
2564 pos = PRVM_G_VECTOR(OFS_PARM0);
2565 scale = PRVM_G_VECTOR(OFS_PARM2);
2566 rgb = PRVM_G_VECTOR(OFS_PARM3);
2567 flag = (int)PRVM_G_FLOAT(OFS_PARM5);
2569 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2571 PRVM_G_FLOAT(OFS_RETURN) = -2;
2572 VM_Warning("VM_drawstring: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2576 if(!scale[0] || !scale[1])
2578 PRVM_G_FLOAT(OFS_RETURN) = -3;
2579 VM_Warning("VM_drawstring: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
2583 if(pos[2] || scale[2])
2584 Con_Printf("VM_drawstring: z value%s from %s discarded\n",(pos[2] && scale[2]) ? "s" : " ",((pos[2] && scale[2]) ? "pos and scale" : (pos[2] ? "pos" : "scale")));
2586 DrawQ_String (pos[0], pos[1], string, 0, scale[0], scale[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM4), flag, NULL, true);
2587 PRVM_G_FLOAT(OFS_RETURN) = 1;
2593 float drawpic(vector position, string pic, vector size, vector rgb, float alpha, float flag)
2596 void VM_drawpic(void)
2598 const char *picname;
2599 float *size, *pos, *rgb;
2602 VM_SAFEPARMCOUNT(6,VM_drawpic);
2604 picname = PRVM_G_STRING(OFS_PARM1);
2605 VM_CheckEmptyString (picname);
2607 // is pic cached ? no function yet for that
2610 PRVM_G_FLOAT(OFS_RETURN) = -4;
2611 VM_Warning("VM_drawpic: %s: %s not cached !\n", PRVM_NAME, picname);
2615 pos = PRVM_G_VECTOR(OFS_PARM0);
2616 size = PRVM_G_VECTOR(OFS_PARM2);
2617 rgb = PRVM_G_VECTOR(OFS_PARM3);
2618 flag = (int) PRVM_G_FLOAT(OFS_PARM5);
2620 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2622 PRVM_G_FLOAT(OFS_RETURN) = -2;
2623 VM_Warning("VM_drawpic: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2627 if(pos[2] || size[2])
2628 Con_Printf("VM_drawpic: z value%s from %s discarded\n",(pos[2] && size[2]) ? "s" : " ",((pos[2] && size[2]) ? "pos and size" : (pos[2] ? "pos" : "size")));
2630 DrawQ_Pic(pos[0], pos[1], Draw_CachePic(picname, true), size[0], size[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM4), flag);
2631 PRVM_G_FLOAT(OFS_RETURN) = 1;
2638 float drawfill(vector position, vector size, vector rgb, float alpha, float flag)
2641 void VM_drawfill(void)
2643 float *size, *pos, *rgb;
2646 VM_SAFEPARMCOUNT(5,VM_drawfill);
2649 pos = PRVM_G_VECTOR(OFS_PARM0);
2650 size = PRVM_G_VECTOR(OFS_PARM1);
2651 rgb = PRVM_G_VECTOR(OFS_PARM2);
2652 flag = (int) PRVM_G_FLOAT(OFS_PARM4);
2654 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2656 PRVM_G_FLOAT(OFS_RETURN) = -2;
2657 VM_Warning("VM_drawfill: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2661 if(pos[2] || size[2])
2662 Con_Printf("VM_drawfill: z value%s from %s discarded\n",(pos[2] && size[2]) ? "s" : " ",((pos[2] && size[2]) ? "pos and size" : (pos[2] ? "pos" : "size")));
2664 DrawQ_Fill(pos[0], pos[1], size[0], size[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM3), flag);
2665 PRVM_G_FLOAT(OFS_RETURN) = 1;
2672 drawsetcliparea(float x, float y, float width, float height)
2675 void VM_drawsetcliparea(void)
2678 VM_SAFEPARMCOUNT(4,VM_drawsetcliparea);
2680 x = bound(0, PRVM_G_FLOAT(OFS_PARM0), vid_conwidth.integer);
2681 y = bound(0, PRVM_G_FLOAT(OFS_PARM1), vid_conheight.integer);
2682 w = bound(0, PRVM_G_FLOAT(OFS_PARM2) + PRVM_G_FLOAT(OFS_PARM0) - x, (vid_conwidth.integer - x));
2683 h = bound(0, PRVM_G_FLOAT(OFS_PARM3) + PRVM_G_FLOAT(OFS_PARM1) - y, (vid_conheight.integer - y));
2685 DrawQ_SetClipArea(x, y, w, h);
2690 VM_drawresetcliparea
2695 void VM_drawresetcliparea(void)
2697 VM_SAFEPARMCOUNT(0,VM_drawresetcliparea);
2699 DrawQ_ResetClipArea();
2706 vector getimagesize(string pic)
2709 void VM_getimagesize(void)
2714 VM_SAFEPARMCOUNT(1,VM_getimagesize);
2716 p = PRVM_G_STRING(OFS_PARM0);
2717 VM_CheckEmptyString (p);
2719 pic = Draw_CachePic (p, false);
2721 PRVM_G_VECTOR(OFS_RETURN)[0] = pic->width;
2722 PRVM_G_VECTOR(OFS_RETURN)[1] = pic->height;
2723 PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
2730 string keynumtostring(float keynum)
2733 void VM_keynumtostring (void)
2735 VM_SAFEPARMCOUNT(1, VM_keynumtostring);
2737 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(Key_KeynumToString((int)PRVM_G_FLOAT(OFS_PARM0)));
2744 float stringtokeynum(string key)
2747 void VM_stringtokeynum (void)
2749 VM_SAFEPARMCOUNT( 1, VM_keynumtostring );
2751 PRVM_G_INT(OFS_RETURN) = Key_StringToKeynum(PRVM_G_STRING(OFS_PARM0));
2754 // CL_Video interface functions
2757 ========================
2760 float cin_open(string file, string name)
2761 ========================
2763 void VM_cin_open( void )
2768 VM_SAFEPARMCOUNT( 2, VM_cin_open );
2770 file = PRVM_G_STRING( OFS_PARM0 );
2771 name = PRVM_G_STRING( OFS_PARM1 );
2773 VM_CheckEmptyString( file );
2774 VM_CheckEmptyString( name );
2776 if( CL_OpenVideo( file, name, MENUOWNER ) )
2777 PRVM_G_FLOAT( OFS_RETURN ) = 1;
2779 PRVM_G_FLOAT( OFS_RETURN ) = 0;
2783 ========================
2786 void cin_close(string name)
2787 ========================
2789 void VM_cin_close( void )
2793 VM_SAFEPARMCOUNT( 1, VM_cin_close );
2795 name = PRVM_G_STRING( OFS_PARM0 );
2796 VM_CheckEmptyString( name );
2798 CL_CloseVideo( CL_GetVideoByName( name ) );
2802 ========================
2804 void cin_setstate(string name, float type)
2805 ========================
2807 void VM_cin_setstate( void )
2810 clvideostate_t state;
2813 VM_SAFEPARMCOUNT( 2, VM_cin_netstate );
2815 name = PRVM_G_STRING( OFS_PARM0 );
2816 VM_CheckEmptyString( name );
2818 state = (clvideostate_t)((int)PRVM_G_FLOAT( OFS_PARM1 ));
2820 video = CL_GetVideoByName( name );
2821 if( video && state > CLVIDEO_UNUSED && state < CLVIDEO_STATECOUNT )
2822 CL_SetVideoState( video, state );
2826 ========================
2829 float cin_getstate(string name)
2830 ========================
2832 void VM_cin_getstate( void )
2837 VM_SAFEPARMCOUNT( 1, VM_cin_getstate );
2839 name = PRVM_G_STRING( OFS_PARM0 );
2840 VM_CheckEmptyString( name );
2842 video = CL_GetVideoByName( name );
2844 PRVM_G_FLOAT( OFS_RETURN ) = (int)video->state;
2846 PRVM_G_FLOAT( OFS_RETURN ) = 0;
2850 ========================
2853 void cin_restart(string name)
2854 ========================
2856 void VM_cin_restart( void )
2861 VM_SAFEPARMCOUNT( 1, VM_cin_restart );
2863 name = PRVM_G_STRING( OFS_PARM0 );
2864 VM_CheckEmptyString( name );
2866 video = CL_GetVideoByName( name );
2868 CL_RestartVideo( video );
2875 Writes new values for v_forward, v_up, and v_right based on angles
2876 void makevectors(vector angle)
2879 void VM_makevectors (void)
2881 prvm_eval_t *valforward, *valright, *valup;
2882 valforward = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_forward);
2883 valright = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_right);
2884 valup = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_up);
2885 if (!valforward || !valright || !valup)
2887 VM_Warning("makevectors: could not find v_forward, v_right, or v_up global variables\n");
2890 VM_SAFEPARMCOUNT(1, VM_makevectors);
2891 AngleVectors (PRVM_G_VECTOR(OFS_PARM0), valforward->vector, valright->vector, valup->vector);
2898 Writes new values for v_forward, v_up, and v_right based on the given forward vector
2899 vectorvectors(vector)
2902 void VM_vectorvectors (void)
2904 prvm_eval_t *valforward, *valright, *valup;
2905 valforward = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_forward);
2906 valright = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_right);
2907 valup = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_up);
2908 if (!valforward || !valright || !valup)
2910 VM_Warning("vectorvectors: could not find v_forward, v_right, or v_up global variables\n");
2913 VM_SAFEPARMCOUNT(1, VM_vectorvectors);
2914 VectorNormalize2(PRVM_G_VECTOR(OFS_PARM0), valforward->vector);
2915 VectorVectors(valforward->vector, valright->vector, valup->vector);
2919 ========================
2922 void drawline(float width, vector pos1, vector pos2, vector rgb, float alpha, float flags)
2923 ========================
2925 void VM_drawline (void)
2927 float *c1, *c2, *rgb;
2929 unsigned char flags;
2931 VM_SAFEPARMCOUNT(6, VM_drawline);
2932 width = PRVM_G_FLOAT(OFS_PARM0);
2933 c1 = PRVM_G_VECTOR(OFS_PARM1);
2934 c2 = PRVM_G_VECTOR(OFS_PARM2);
2935 rgb = PRVM_G_VECTOR(OFS_PARM3);
2936 alpha = PRVM_G_FLOAT(OFS_PARM4);
2937 flags = (int)PRVM_G_FLOAT(OFS_PARM5);
2938 DrawQ_Line(width, c1[0], c1[1], c2[0], c2[1], rgb[0], rgb[1], rgb[2], alpha, flags);
2945 // float(float number, float quantity) bitshift (EXT_BITSHIFT)
2946 void VM_bitshift (void)
2949 VM_SAFEPARMCOUNT(2, VM_bitshift);
2951 n1 = (int)fabs((int)PRVM_G_FLOAT(OFS_PARM0));
2952 n2 = (int)PRVM_G_FLOAT(OFS_PARM1);
2954 PRVM_G_FLOAT(OFS_RETURN) = n1;
2957 PRVM_G_FLOAT(OFS_RETURN) = (n1 >> -n2);
2959 PRVM_G_FLOAT(OFS_RETURN) = (n1 << n2);
2962 ////////////////////////////////////////
2963 // AltString functions
2964 ////////////////////////////////////////
2967 ========================
2970 float altstr_count(string)
2971 ========================
2973 void VM_altstr_count( void )
2975 const char *altstr, *pos;
2978 VM_SAFEPARMCOUNT( 1, VM_altstr_count );
2980 altstr = PRVM_G_STRING( OFS_PARM0 );
2981 //VM_CheckEmptyString( altstr );
2983 for( count = 0, pos = altstr ; *pos ; pos++ ) {
2984 if( *pos == '\\' ) {
2988 } else if( *pos == '\'' ) {
2993 PRVM_G_FLOAT( OFS_RETURN ) = (float) (count / 2);
2997 ========================
3000 string altstr_prepare(string)
3001 ========================
3003 void VM_altstr_prepare( void )
3006 const char *instr, *in;
3008 char outstr[VM_STRINGTEMP_LENGTH];
3010 VM_SAFEPARMCOUNT( 1, VM_altstr_prepare );
3012 instr = PRVM_G_STRING( OFS_PARM0 );
3014 for( out = outstr, in = instr, size = sizeof(outstr) - 1 ; size && *in ; size--, in++, out++ )
3023 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
3027 ========================
3030 string altstr_get(string, float)
3031 ========================
3033 void VM_altstr_get( void )
3035 const char *altstr, *pos;
3038 char outstr[VM_STRINGTEMP_LENGTH];
3040 VM_SAFEPARMCOUNT( 2, VM_altstr_get );
3042 altstr = PRVM_G_STRING( OFS_PARM0 );
3044 count = (int)PRVM_G_FLOAT( OFS_PARM1 );
3045 count = count * 2 + 1;
3047 for( pos = altstr ; *pos && count ; pos++ )
3048 if( *pos == '\\' ) {
3051 } else if( *pos == '\'' )
3055 PRVM_G_INT( OFS_RETURN ) = 0;
3059 for( out = outstr, size = sizeof(outstr) - 1 ; size && *pos ; size--, pos++, out++ )
3060 if( *pos == '\\' ) {
3065 } else if( *pos == '\'' )
3071 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
3075 ========================
3078 string altstr_set(string altstr, float num, string set)
3079 ========================
3081 void VM_altstr_set( void )
3084 const char *altstr, *str;
3087 char outstr[VM_STRINGTEMP_LENGTH];
3089 VM_SAFEPARMCOUNT( 3, VM_altstr_set );
3091 altstr = PRVM_G_STRING( OFS_PARM0 );
3093 num = (int)PRVM_G_FLOAT( OFS_PARM1 );
3095 str = PRVM_G_STRING( OFS_PARM2 );
3098 for( num = num * 2 + 1, in = altstr; *in && num; *out++ = *in++ )
3103 } else if( *in == '\'' ) {
3108 for( ; *str; *out++ = *str++ );
3109 // now jump over the old content
3111 if( *in == '\'' || (*in == '\\' && !*++in) )
3114 strlcpy(out, in, outstr + sizeof(outstr) - out);
3115 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
3119 ========================
3122 string altstr_ins(string altstr, float num, string set)
3123 ========================
3125 void VM_altstr_ins(void)
3133 char outstr[VM_STRINGTEMP_LENGTH];
3135 VM_SAFEPARMCOUNT(3, VM_altstr_ins);
3137 in = instr = PRVM_G_STRING( OFS_PARM0 );
3138 num = (int)PRVM_G_FLOAT( OFS_PARM1 );
3139 set = setstr = PRVM_G_STRING( OFS_PARM2 );
3142 for( num = num * 2 + 2 ; *in && num > 0 ; *out++ = *in++ )
3147 } else if( *in == '\'' ) {
3152 for( ; *set ; *out++ = *set++ );
3155 strlcpy(out, in, outstr + sizeof(outstr) - out);
3156 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
3160 ////////////////////////////////////////
3161 // BufString functions
3162 ////////////////////////////////////////
3163 //[515]: string buffers support
3164 #define MAX_QCSTR_BUFFERS 128
3165 #define MAX_QCSTR_STRINGS 1024
3170 char *strings[MAX_QCSTR_STRINGS];
3173 // FIXME: move stringbuffers to prog_t to allow multiple progs!
3174 static qcstrbuffer_t *qcstringbuffers[MAX_QCSTR_BUFFERS];
3175 static int num_qcstringbuffers;
3176 static int buf_sortpower;
3178 #define BUFSTR_BUFFER(a) (a>=MAX_QCSTR_BUFFERS) ? NULL : (qcstringbuffers[a])
3179 #define BUFSTR_ISFREE(a) (a<MAX_QCSTR_BUFFERS&&qcstringbuffers[a]&&qcstringbuffers[a]->num_strings<=0) ? 1 : 0
3181 static int BufStr_FindFreeBuffer (void)
3184 if(num_qcstringbuffers == MAX_QCSTR_BUFFERS)
3186 for(i=0;i<MAX_QCSTR_BUFFERS;i++)
3187 if(!qcstringbuffers[i])
3189 qcstringbuffers[i] = (qcstrbuffer_t *)Z_Malloc(sizeof(qcstrbuffer_t));
3190 memset(qcstringbuffers[i], 0, sizeof(qcstrbuffer_t));
3196 static void BufStr_ClearBuffer (int index)
3198 qcstrbuffer_t *b = qcstringbuffers[index];
3203 if(b->num_strings > 0)
3205 for(i=0;i<b->num_strings;i++)
3207 Z_Free(b->strings[i]);
3208 num_qcstringbuffers--;
3210 Z_Free(qcstringbuffers[index]);
3211 qcstringbuffers[index] = NULL;
3215 static int BufStr_FindFreeString (qcstrbuffer_t *b)
3218 for(i=0;i<b->num_strings;i++)
3219 if(!b->strings[i] || !b->strings[i][0])
3221 if(i == MAX_QCSTR_STRINGS) return -1;
3225 static int BufStr_SortStringsUP (const void *in1, const void *in2)
3228 a = *((const char **) in1);
3229 b = *((const char **) in2);
3231 if(!b[0]) return -1;
3232 return strncmp(a, b, buf_sortpower);
3235 static int BufStr_SortStringsDOWN (const void *in1, const void *in2)
3238 a = *((const char **) in1);
3239 b = *((const char **) in2);
3241 if(!b[0]) return -1;
3242 return strncmp(b, a, buf_sortpower);
3246 ========================
3248 creates new buffer, and returns it's index, returns -1 if failed
3249 float buf_create(void) = #460;
3250 ========================
3252 void VM_buf_create (void)
3255 VM_SAFEPARMCOUNT(0, VM_buf_create);
3256 i = BufStr_FindFreeBuffer();
3258 num_qcstringbuffers++;
3260 //Con_Printf("VM_buf_create: buffers overflow in %s\n", PRVM_NAME);
3261 PRVM_G_FLOAT(OFS_RETURN) = i;
3265 ========================
3267 deletes buffer and all strings in it
3268 void buf_del(float bufhandle) = #461;
3269 ========================
3271 void VM_buf_del (void)
3273 VM_SAFEPARMCOUNT(1, VM_buf_del);
3274 if(BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0)))
3275 BufStr_ClearBuffer((int)PRVM_G_FLOAT(OFS_PARM0));
3278 VM_Warning("VM_buf_del: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3284 ========================
3286 how many strings are stored in buffer
3287 float buf_getsize(float bufhandle) = #462;
3288 ========================
3290 void VM_buf_getsize (void)
3293 VM_SAFEPARMCOUNT(1, VM_buf_getsize);
3295 b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3298 PRVM_G_FLOAT(OFS_RETURN) = -1;
3299 VM_Warning("VM_buf_getsize: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3303 PRVM_G_FLOAT(OFS_RETURN) = b->num_strings;
3307 ========================
3309 copy all content from one buffer to another, make sure it exists
3310 void buf_copy(float bufhandle_from, float bufhandle_to) = #463;
3311 ========================
3313 void VM_buf_copy (void)
3315 qcstrbuffer_t *b1, *b2;
3317 VM_SAFEPARMCOUNT(2, VM_buf_copy);
3319 b1 = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3322 VM_Warning("VM_buf_copy: invalid source buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3325 i = (int)PRVM_G_FLOAT(OFS_PARM1);
3326 if(i == (int)PRVM_G_FLOAT(OFS_PARM0))
3328 VM_Warning("VM_buf_copy: source == destination (%i) in %s\n", i, PRVM_NAME);
3331 b2 = BUFSTR_BUFFER(i);
3334 VM_Warning("VM_buf_copy: invalid destination buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM1), PRVM_NAME);
3338 BufStr_ClearBuffer(i);
3339 qcstringbuffers[i] = (qcstrbuffer_t *)Z_Malloc(sizeof(qcstrbuffer_t));
3340 memset(qcstringbuffers[i], 0, sizeof(qcstrbuffer_t));
3341 b2->num_strings = b1->num_strings;
3343 for(i=0;i<b1->num_strings;i++)
3344 if(b1->strings[i] && b1->strings[i][0])
3347 stringlen = strlen(b1->strings[i]) + 1;
3348 b2->strings[i] = (char *)Z_Malloc(stringlen);
3351 VM_Warning("VM_buf_copy: not enough memory for buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM1), PRVM_NAME);
3354 memcpy(b2->strings[i], b1->strings[i], stringlen);
3359 ========================
3361 sort buffer by beginnings of strings (sortpower defaults it's lenght)
3362 "backward == TRUE" means that sorting goes upside-down
3363 void buf_sort(float bufhandle, float sortpower, float backward) = #464;
3364 ========================
3366 void VM_buf_sort (void)
3370 VM_SAFEPARMCOUNT(3, VM_buf_sort);
3372 b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3375 VM_Warning("VM_buf_sort: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3378 if(b->num_strings <= 0)
3380 VM_Warning("VM_buf_sort: tried to sort empty buffer %i in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3383 buf_sortpower = (int)PRVM_G_FLOAT(OFS_PARM1);
3384 if(buf_sortpower <= 0)
3385 buf_sortpower = 99999999;
3387 if(!PRVM_G_FLOAT(OFS_PARM2))
3388 qsort(b->strings, b->num_strings, sizeof(char*), BufStr_SortStringsUP);
3390 qsort(b->strings, b->num_strings, sizeof(char*), BufStr_SortStringsDOWN);
3392 for(i=b->num_strings-1;i>=0;i--) //[515]: delete empty lines
3395 if(b->strings[i][0])
3399 Z_Free(b->strings[i]);
3401 b->strings[i] = NULL;
3409 ========================
3411 concantenates all buffer string into one with "glue" separator and returns it as tempstring
3412 string buf_implode(float bufhandle, string glue) = #465;
3413 ========================
3415 void VM_buf_implode (void)
3418 char k[VM_STRINGTEMP_LENGTH];
3422 VM_SAFEPARMCOUNT(2, VM_buf_implode);
3424 b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3425 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
3428 VM_Warning("VM_buf_implode: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3433 sep = PRVM_G_STRING(OFS_PARM1);
3435 for(l=i=0;i<b->num_strings;i++)
3438 l += (i > 0 ? strlen(sep) : 0) + strlen(b->strings[i]);
3439 if (l >= sizeof(k) - 1)
3441 strlcat(k, sep, sizeof(k));
3442 strlcat(k, b->strings[i], sizeof(k));
3444 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(k);
3448 ========================
3450 get a string from buffer, returns tempstring, dont str_unzone it!
3451 string bufstr_get(float bufhandle, float string_index) = #465;
3452 ========================
3454 void VM_bufstr_get (void)
3458 VM_SAFEPARMCOUNT(2, VM_bufstr_get);
3460 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
3461 b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3464 VM_Warning("VM_bufstr_get: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3467 strindex = (int)PRVM_G_FLOAT(OFS_PARM1);
3468 if(strindex < 0 || strindex > MAX_QCSTR_STRINGS)
3470 VM_Warning("VM_bufstr_get: invalid string index %i used in %s\n", strindex, PRVM_NAME);
3473 if(b->num_strings <= strindex)
3475 if(b->strings[strindex])
3476 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(b->strings[strindex]);
3480 ========================
3482 copies a string into selected slot of buffer
3483 void bufstr_set(float bufhandle, float string_index, string str) = #466;
3484 ========================
3486 void VM_bufstr_set (void)
3488 int bufindex, strindex;
3493 VM_SAFEPARMCOUNT(3, VM_bufstr_set);
3495 bufindex = (int)PRVM_G_FLOAT(OFS_PARM0);
3496 b = BUFSTR_BUFFER(bufindex);
3499 VM_Warning("VM_bufstr_set: invalid buffer %i used in %s\n", bufindex, PRVM_NAME);
3502 strindex = (int)PRVM_G_FLOAT(OFS_PARM1);
3503 if(strindex < 0 || strindex > MAX_QCSTR_STRINGS)
3505 VM_Warning("VM_bufstr_set: invalid string index %i used in %s\n", strindex, PRVM_NAME);
3508 news = PRVM_G_STRING(OFS_PARM2);
3509 if(b->strings[strindex])
3510 Z_Free(b->strings[strindex]);
3511 alloclen = strlen(news) + 1;
3512 b->strings[strindex] = (char *)Z_Malloc(alloclen);
3513 memcpy(b->strings[strindex], news, alloclen);
3517 ========================
3519 adds string to buffer in nearest free slot and returns it
3520 "order == TRUE" means that string will be added after last "full" slot
3521 float bufstr_add(float bufhandle, string str, float order) = #467;
3522 ========================
3524 void VM_bufstr_add (void)
3526 int bufindex, order, strindex;
3531 VM_SAFEPARMCOUNT(3, VM_bufstr_add);
3533 bufindex = (int)PRVM_G_FLOAT(OFS_PARM0);
3534 b = BUFSTR_BUFFER(bufindex);
3535 PRVM_G_FLOAT(OFS_RETURN) = -1;
3538 VM_Warning("VM_bufstr_add: invalid buffer %i used in %s\n", bufindex, PRVM_NAME);
3541 string = PRVM_G_STRING(OFS_PARM1);
3542 order = (int)PRVM_G_FLOAT(OFS_PARM2);
3544 strindex = b->num_strings;
3547 strindex = BufStr_FindFreeString(b);
3550 VM_Warning("VM_bufstr_add: buffer %i has no free string slots in %s\n", bufindex, PRVM_NAME);
3555 while(b->num_strings <= strindex)
3557 if(b->num_strings == MAX_QCSTR_STRINGS)
3559 VM_Warning("VM_bufstr_add: buffer %i has no free string slots in %s\n", bufindex, PRVM_NAME);
3562 b->strings[b->num_strings] = NULL;
3565 if(b->strings[strindex])
3566 Z_Free(b->strings[strindex]);
3567 alloclen = strlen(string) + 1;
3568 b->strings[strindex] = (char *)Z_Malloc(alloclen);
3569 memcpy(b->strings[strindex], string, alloclen);
3570 PRVM_G_FLOAT(OFS_RETURN) = strindex;
3574 ========================
3576 delete string from buffer
3577 void bufstr_free(float bufhandle, float string_index) = #468;
3578 ========================
3580 void VM_bufstr_free (void)
3584 VM_SAFEPARMCOUNT(2, VM_bufstr_free);
3586 b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3589 VM_Warning("VM_bufstr_free: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3592 i = (int)PRVM_G_FLOAT(OFS_PARM1);
3593 if(i < 0 || i > MAX_QCSTR_STRINGS)
3595 VM_Warning("VM_bufstr_free: invalid string index %i used in %s\n", i, PRVM_NAME);
3599 Z_Free(b->strings[i]);
3600 b->strings[i] = NULL;
3601 if(i+1 == b->num_strings)
3611 This was a major timewaster in progs, so it was converted to C
3614 void VM_changeyaw (void)
3617 float ideal, current, move, speed;
3619 // this is called (VERY HACKISHLY) by SV_MoveToGoal, so it can not use any
3620 // parameters because they are the parameters to SV_MoveToGoal, not this
3621 //VM_SAFEPARMCOUNT(0, VM_changeyaw);
3623 ent = PRVM_PROG_TO_EDICT(PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict);
3624 if (ent == prog->edicts)
3626 VM_Warning("changeyaw: can not modify world entity\n");
3629 if (ent->priv.server->free)
3631 VM_Warning("changeyaw: can not modify free entity\n");
3634 if (prog->fieldoffsets.angles < 0 || prog->fieldoffsets.ideal_yaw < 0 || prog->fieldoffsets.yaw_speed < 0)
3636 VM_Warning("changeyaw: angles, ideal_yaw, or yaw_speed field(s) not found\n");
3639 current = ANGLEMOD(PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.angles)->vector[1]);
3640 ideal = PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.ideal_yaw)->_float;
3641 speed = PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.yaw_speed)->_float;
3643 if (current == ideal)
3645 move = ideal - current;
3646 if (ideal > current)
3667 PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.angles)->vector[1] = ANGLEMOD (current + move);
3675 void VM_changepitch (void)
3678 float ideal, current, move, speed;
3680 VM_SAFEPARMCOUNT(1, VM_changepitch);
3682 ent = PRVM_G_EDICT(OFS_PARM0);
3683 if (ent == prog->edicts)
3685 VM_Warning("changepitch: can not modify world entity\n");
3688 if (ent->priv.server->free)
3690 VM_Warning("changepitch: can not modify free entity\n");
3693 if (prog->fieldoffsets.angles < 0 || prog->fieldoffsets.idealpitch < 0 || prog->fieldoffsets.pitch_speed < 0)
3695 VM_Warning("changepitch: angles, idealpitch, or pitch_speed field(s) not found\n");
3698 current = ANGLEMOD(PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.angles)->vector[0]);
3699 ideal = PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.idealpitch)->_float;
3700 speed = PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.pitch_speed)->_float;
3702 if (current == ideal)
3704 move = ideal - current;
3705 if (ideal > current)
3726 PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.angles)->vector[0] = ANGLEMOD (current + move);
3730 static int Is_Text_Color (char c, char t)
3733 char c2 = c - (c & 128);
3734 char t2 = t - (t & 128);
3736 if(c != STRING_COLOR_TAG && c2 != STRING_COLOR_TAG) return 0;
3737 if(t >= '0' && t <= '9') a = 1;
3738 if(t2 >= '0' && t2 <= '9') a = 1;
3739 /* if(t >= 'A' && t <= 'Z') a = 2;
3740 if(t2 >= 'A' && t2 <= 'Z') a = 2;
3742 if(a == 1 && scr_colortext.integer > 0)
3744 if(a == 2 && scr_multifonts.integer > 0)
3750 void VM_uncolorstring (void)
3753 char out[VM_STRINGTEMP_LENGTH];
3756 VM_SAFEPARMCOUNT(1, VM_uncolorstring);
3757 in = PRVM_G_STRING(OFS_PARM0);
3758 VM_CheckEmptyString (in);
3763 if(Is_Text_Color(in[k], in[k+1]) == 1/* || (in[k] == '&' && in[k+1] == 'r')*/)
3772 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(out);
3775 //#222 string(string s, float index) str2chr (FTE_STRINGS)
3776 void VM_str2chr (void)
3779 VM_SAFEPARMCOUNT(2, VM_str2chr);
3780 s = PRVM_G_STRING(OFS_PARM0);
3781 if((unsigned)PRVM_G_FLOAT(OFS_PARM1) > strlen(s))
3783 PRVM_G_FLOAT(OFS_RETURN) = (unsigned char)s[(int)PRVM_G_FLOAT(OFS_PARM1)];
3786 //#223 string(float c, ...) chr2str (FTE_STRINGS)
3787 void VM_chr2str (void)
3791 VM_SAFEPARMCOUNTRANGE(0, 8, VM_chr2str);
3792 for(i = 0;i < prog->argc && i < (int)sizeof(t) - 1;i++)
3793 t[i] = (unsigned char)PRVM_G_FLOAT(OFS_PARM0+i*3);
3795 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(t);
3798 //#228 float(string s1, string s2, float len) strncmp (FTE_STRINGS)
3799 void VM_strncmp (void)
3801 const char *s1, *s2;
3802 VM_SAFEPARMCOUNT(3, VM_strncmp);
3803 s1 = PRVM_G_STRING(OFS_PARM0);
3804 s2 = PRVM_G_STRING(OFS_PARM1);
3805 PRVM_G_FLOAT(OFS_RETURN) = strncmp(s1, s2, (size_t)PRVM_G_FLOAT(OFS_PARM2));
3808 void VM_wasfreed (void)
3810 VM_SAFEPARMCOUNT(1, VM_wasfreed);
3811 PRVM_G_FLOAT(OFS_RETURN) = PRVM_G_EDICT(OFS_PARM0)->priv.required->free;
3814 void VM_SetTraceGlobals(const trace_t *trace)
3817 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_allsolid)))
3818 val->_float = trace->allsolid;
3819 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_startsolid)))
3820 val->_float = trace->startsolid;
3821 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_fraction)))
3822 val->_float = trace->fraction;
3823 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_inwater)))
3824 val->_float = trace->inwater;
3825 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_inopen)))
3826 val->_float = trace->inopen;
3827 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_endpos)))
3828 VectorCopy(trace->endpos, val->vector);
3829 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_plane_normal)))
3830 VectorCopy(trace->plane.normal, val->vector);
3831 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_plane_dist)))
3832 val->_float = trace->plane.dist;
3833 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_ent)))
3834 val->edict = PRVM_EDICT_TO_PROG(trace->ent ? trace->ent : prog->edicts);
3835 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dpstartcontents)))
3836 val->_float = trace->startsupercontents;
3837 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dphitcontents)))
3838 val->_float = trace->hitsupercontents;
3839 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dphitq3surfaceflags)))
3840 val->_float = trace->hitq3surfaceflags;
3841 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dphittexturename)))
3842 val->string = trace->hittexture ? PRVM_SetTempString(trace->hittexture->name) : 0;
3847 void VM_Cmd_Init(void)
3849 // only init the stuff for the current prog
3852 // VM_BufStr_Init();
3855 void VM_Cmd_Reset(void)
3857 CL_PurgeOwner( MENUOWNER );
3859 VM_Files_CloseAll();
3860 // VM_BufStr_ShutDown();