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
12 extern cvar_t prvm_backtraceforwarnings;
14 // LordHavoc: changed this to NOT use a return statement, so that it can be used in functions that must return a value
15 void VM_Warning(const char *fmt, ...)
18 char msg[MAX_INPUTLINE];
19 static double recursive = -1;
22 dpvsnprintf(msg,sizeof(msg),fmt,argptr);
27 // TODO: either add a cvar/cmd to control the state dumping or replace some of the calls with Con_Printf [9/13/2006 Black]
28 if(prvm_backtraceforwarnings.integer && recursive != realtime) // NOTE: this compares to the time, just in case if PRVM_PrintState causes a Host_Error and keeps recursive set
37 //============================================================================
40 // TODO DONE: move vm_files and vm_fssearchlist to prvm_prog_t struct
41 // TODO: move vm_files and vm_fssearchlist back [9/13/2006 Black]
42 // TODO: (move vm_files and vm_fssearchlist to prvm_prog_t struct again) [2007-01-23 LordHavoc]
43 // TODO: will this war ever end? [2007-01-23 LordHavoc]
45 void VM_CheckEmptyString (const char *s)
48 PRVM_ERROR ("%s: Bad string", PRVM_NAME);
51 //============================================================================
54 void VM_VarString(int first, char *out, int outlength)
60 outend = out + outlength - 1;
61 for (i = first;i < prog->argc && out < outend;i++)
63 s = PRVM_G_STRING((OFS_PARM0+i*3));
64 while (out < outend && *s)
74 returns true if the extension is supported by the server
76 checkextension(extensionname)
80 // kind of helper function
81 static qboolean checkextension(const char *name)
85 len = (int)strlen(name);
87 for (e = prog->extensionstring;*e;e++)
94 while (*e && *e != ' ')
96 if ((e - start) == len && !strncasecmp(start, name, len))
102 void VM_checkextension (void)
104 VM_SAFEPARMCOUNT(1,VM_checkextension);
106 PRVM_G_FLOAT(OFS_RETURN) = checkextension(PRVM_G_STRING(OFS_PARM0));
113 This is a TERMINAL error, which will kill off the entire prog.
122 char string[VM_STRINGTEMP_LENGTH];
124 VM_VarString(0, string, sizeof(string));
125 Con_Printf("======%s ERROR in %s:\n%s\n", PRVM_NAME, PRVM_GetString(prog->xfunction->s_name), string);
126 if (prog->globaloffsets.self >= 0)
128 ed = PRVM_PROG_TO_EDICT(PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict);
129 PRVM_ED_Print(ed, NULL);
132 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);
139 Dumps out self, then an error message. The program is aborted and self is
140 removed, but the level can continue.
145 void VM_objerror (void)
148 char string[VM_STRINGTEMP_LENGTH];
150 VM_VarString(0, string, sizeof(string));
151 Con_Printf("======OBJECT ERROR======\n"); // , PRVM_NAME, PRVM_GetString(prog->xfunction->s_name), string); // or include them? FIXME
152 if (prog->globaloffsets.self >= 0)
154 ed = PRVM_PROG_TO_EDICT(PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict);
155 PRVM_ED_Print(ed, NULL);
160 // objerror has to display the object fields -> else call
161 PRVM_ERROR ("VM_objecterror: self not defined !");
162 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);
176 char string[VM_STRINGTEMP_LENGTH];
178 VM_VarString(0, string, sizeof(string));
186 broadcast print to everyone on server
191 void VM_bprint (void)
193 char string[VM_STRINGTEMP_LENGTH];
197 VM_Warning("VM_bprint: game is not server(%s) !\n", PRVM_NAME);
201 VM_VarString(0, string, sizeof(string));
202 SV_BroadcastPrint(string);
207 VM_sprint (menu & client but only if server.active == true)
209 single print to a specific client
211 sprint(float clientnum,...[string])
214 void VM_sprint (void)
218 char string[VM_STRINGTEMP_LENGTH];
220 VM_SAFEPARMCOUNTRANGE(1, 8, VM_sprint);
222 //find client for this entity
223 clientnum = (int)PRVM_G_FLOAT(OFS_PARM0);
224 if (!sv.active || clientnum < 0 || clientnum >= svs.maxclients || !svs.clients[clientnum].active)
226 VM_Warning("VM_sprint: %s: invalid client or server is not active !\n", PRVM_NAME);
230 client = svs.clients + clientnum;
231 if (!client->netconnection)
234 VM_VarString(1, string, sizeof(string));
235 MSG_WriteChar(&client->netconnection->message,svc_print);
236 MSG_WriteString(&client->netconnection->message, string);
243 single print to the screen
248 void VM_centerprint (void)
250 char string[VM_STRINGTEMP_LENGTH];
252 VM_SAFEPARMCOUNTRANGE(1, 8, VM_centerprint);
253 VM_VarString(0, string, sizeof(string));
254 SCR_CenterPrint(string);
261 vector normalize(vector)
264 void VM_normalize (void)
270 VM_SAFEPARMCOUNT(1,VM_normalize);
272 value1 = PRVM_G_VECTOR(OFS_PARM0);
274 f = VectorLength2(value1);
278 VectorScale(value1, f, newvalue);
281 VectorClear(newvalue);
283 VectorCopy (newvalue, PRVM_G_VECTOR(OFS_RETURN));
295 VM_SAFEPARMCOUNT(1,VM_vlen);
296 PRVM_G_FLOAT(OFS_RETURN) = VectorLength(PRVM_G_VECTOR(OFS_PARM0));
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[, vector])
335 void VM_vectoangles (void)
337 VM_SAFEPARMCOUNTRANGE(1, 2,VM_vectoangles);
339 AnglesFromVectors(PRVM_G_VECTOR(OFS_RETURN), PRVM_G_VECTOR(OFS_PARM0), prog->argc >= 2 ? PRVM_G_VECTOR(OFS_PARM1) : NULL, true);
346 Returns a number from 0<= num < 1
351 void VM_random (void)
353 VM_SAFEPARMCOUNT(0,VM_random);
355 PRVM_G_FLOAT(OFS_RETURN) = lhrandom(0, 1);
362 localsound(string sample)
365 void VM_localsound(void)
369 VM_SAFEPARMCOUNT(1,VM_localsound);
371 s = PRVM_G_STRING(OFS_PARM0);
373 if(!S_LocalSound (s))
375 PRVM_G_FLOAT(OFS_RETURN) = -4;
376 VM_Warning("VM_localsound: Failed to play %s for %s !\n", s, PRVM_NAME);
380 PRVM_G_FLOAT(OFS_RETURN) = 1;
392 PRVM_ERROR ("%s: break statement", PRVM_NAME);
395 //============================================================================
401 Sends text over to the client's execution buffer
403 [localcmd (string, ...) or]
407 void VM_localcmd (void)
409 char string[VM_STRINGTEMP_LENGTH];
410 VM_SAFEPARMCOUNTRANGE(1, 8, VM_localcmd);
411 VM_VarString(0, string, sizeof(string));
412 Cbuf_AddText(string);
424 char string[VM_STRINGTEMP_LENGTH];
425 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar);
426 VM_VarString(0, string, sizeof(string));
427 VM_CheckEmptyString(string);
428 PRVM_G_FLOAT(OFS_RETURN) = Cvar_VariableValue(string);
435 const string VM_cvar_string (string, ...)
438 void VM_cvar_string(void)
440 char string[VM_STRINGTEMP_LENGTH];
441 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar_string);
442 VM_VarString(0, string, sizeof(string));
443 VM_CheckEmptyString(string);
444 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(Cvar_VariableString(string));
449 ========================
452 const string VM_cvar_defstring (string, ...)
453 ========================
455 void VM_cvar_defstring (void)
457 char string[VM_STRINGTEMP_LENGTH];
458 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar_defstring);
459 VM_VarString(0, string, sizeof(string));
460 VM_CheckEmptyString(string);
461 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(Cvar_VariableDefString(string));
467 void cvar_set (string,string, ...)
470 void VM_cvar_set (void)
473 char string[VM_STRINGTEMP_LENGTH];
474 VM_SAFEPARMCOUNTRANGE(2,8,VM_cvar_set);
475 VM_VarString(1, string, sizeof(string));
476 name = PRVM_G_STRING(OFS_PARM0);
477 VM_CheckEmptyString(name);
478 Cvar_Set(name, string);
488 void VM_dprint (void)
490 char string[VM_STRINGTEMP_LENGTH];
491 VM_SAFEPARMCOUNTRANGE(1, 8, VM_dprint);
492 if (developer.integer)
494 VM_VarString(0, string, sizeof(string));
496 Con_Printf("%s", string);
498 Con_Printf("%s: %s", PRVM_NAME, string);
516 VM_SAFEPARMCOUNT(1, VM_ftos);
518 v = PRVM_G_FLOAT(OFS_PARM0);
520 if ((float)((int)v) == v)
521 sprintf(s, "%i", (int)v);
524 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(s);
539 VM_SAFEPARMCOUNT(1,VM_fabs);
541 v = PRVM_G_FLOAT(OFS_PARM0);
542 PRVM_G_FLOAT(OFS_RETURN) = fabs(v);
557 VM_SAFEPARMCOUNT(1,VM_vtos);
559 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]);
560 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(s);
575 VM_SAFEPARMCOUNT(1, VM_etos);
577 sprintf (s, "entity %i", PRVM_G_EDICTNUM(OFS_PARM0));
578 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(s);
585 float stof(...[string])
590 char string[VM_STRINGTEMP_LENGTH];
591 VM_SAFEPARMCOUNTRANGE(1, 8, VM_stof);
592 VM_VarString(0, string, sizeof(string));
593 PRVM_G_FLOAT(OFS_RETURN) = atof(string);
597 ========================
601 ========================
605 VM_SAFEPARMCOUNT(1, VM_itof);
606 PRVM_G_FLOAT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
610 ========================
613 entity ftoe(float num)
614 ========================
619 VM_SAFEPARMCOUNT(1, VM_ftoe);
621 ent = (int)PRVM_G_FLOAT(OFS_PARM0);
622 if (ent < 0 || ent >= MAX_EDICTS || PRVM_PROG_TO_EDICT(ent)->priv.required->free)
623 ent = 0; // return world instead of a free or invalid entity
625 PRVM_G_INT(OFS_RETURN) = ent;
632 string strftime(float uselocaltime, string[, string ...])
635 void VM_strftime(void)
639 char fmt[VM_STRINGTEMP_LENGTH];
640 char result[VM_STRINGTEMP_LENGTH];
641 VM_SAFEPARMCOUNTRANGE(2, 8, VM_strftime);
642 VM_VarString(1, fmt, sizeof(fmt));
644 if (PRVM_G_FLOAT(OFS_PARM0))
650 PRVM_G_FLOAT(OFS_RETURN) = 0;
653 strftime(result, sizeof(result), fmt, tm);
654 PRVM_G_FLOAT(OFS_RETURN) = PRVM_SetTempString(result);
668 VM_SAFEPARMCOUNT(0, VM_spawn);
669 prog->xfunction->builtinsprofile += 20;
670 ed = PRVM_ED_Alloc();
682 void VM_remove (void)
685 prog->xfunction->builtinsprofile += 20;
687 VM_SAFEPARMCOUNT(1, VM_remove);
689 ed = PRVM_G_EDICT(OFS_PARM0);
690 if( PRVM_NUM_FOR_EDICT(ed) <= prog->reserved_edicts )
692 if (developer.integer >= 1)
693 VM_Warning( "VM_remove: tried to remove the null entity or a reserved entity!\n" );
695 else if( ed->priv.required->free )
697 if (developer.integer >= 1)
698 VM_Warning( "VM_remove: tried to remove an already freed entity!\n" );
708 entity find(entity start, .string field, string match)
719 VM_SAFEPARMCOUNT(3,VM_find);
721 e = PRVM_G_EDICTNUM(OFS_PARM0);
722 f = PRVM_G_INT(OFS_PARM1);
723 s = PRVM_G_STRING(OFS_PARM2);
725 // LordHavoc: apparently BloodMage does a find(world, weaponmodel, "") and
726 // expects it to find all the monsters, so we must be careful to support
729 for (e++ ; e < prog->num_edicts ; e++)
731 prog->xfunction->builtinsprofile++;
732 ed = PRVM_EDICT_NUM(e);
733 if (ed->priv.required->free)
735 t = PRVM_E_STRING(ed,f);
745 VM_RETURN_EDICT(prog->edicts);
752 entity findfloat(entity start, .float field, float match)
753 entity findentity(entity start, .entity field, entity match)
756 // LordHavoc: added this for searching float, int, and entity reference fields
757 void VM_findfloat (void)
764 VM_SAFEPARMCOUNT(3,VM_findfloat);
766 e = PRVM_G_EDICTNUM(OFS_PARM0);
767 f = PRVM_G_INT(OFS_PARM1);
768 s = PRVM_G_FLOAT(OFS_PARM2);
770 for (e++ ; e < prog->num_edicts ; e++)
772 prog->xfunction->builtinsprofile++;
773 ed = PRVM_EDICT_NUM(e);
774 if (ed->priv.required->free)
776 if (PRVM_E_FLOAT(ed,f) == s)
783 VM_RETURN_EDICT(prog->edicts);
790 entity findchain(.string field, string match)
793 // chained search for strings in entity fields
794 // entity(.string field, string match) findchain = #402;
795 void VM_findchain (void)
800 prvm_edict_t *ent, *chain;
802 VM_SAFEPARMCOUNT(2,VM_findchain);
804 if (prog->fieldoffsets.chain < 0)
805 PRVM_ERROR("VM_findchain: %s doesnt have a chain field !", PRVM_NAME);
807 chain = prog->edicts;
809 f = PRVM_G_INT(OFS_PARM0);
810 s = PRVM_G_STRING(OFS_PARM1);
812 // LordHavoc: apparently BloodMage does a find(world, weaponmodel, "") and
813 // expects it to find all the monsters, so we must be careful to support
816 ent = PRVM_NEXT_EDICT(prog->edicts);
817 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
819 prog->xfunction->builtinsprofile++;
820 if (ent->priv.required->free)
822 t = PRVM_E_STRING(ent,f);
828 PRVM_EDICTFIELDVALUE(ent,prog->fieldoffsets.chain)->edict = PRVM_NUM_FOR_EDICT(chain);
832 VM_RETURN_EDICT(chain);
839 entity findchainfloat(.string field, float match)
840 entity findchainentity(.string field, entity match)
843 // LordHavoc: chained search for float, int, and entity reference fields
844 // entity(.string field, float match) findchainfloat = #403;
845 void VM_findchainfloat (void)
850 prvm_edict_t *ent, *chain;
852 VM_SAFEPARMCOUNT(2, VM_findchainfloat);
854 if (prog->fieldoffsets.chain < 0)
855 PRVM_ERROR("VM_findchainfloat: %s doesnt have a chain field !", PRVM_NAME);
857 chain = (prvm_edict_t *)prog->edicts;
859 f = PRVM_G_INT(OFS_PARM0);
860 s = PRVM_G_FLOAT(OFS_PARM1);
862 ent = PRVM_NEXT_EDICT(prog->edicts);
863 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
865 prog->xfunction->builtinsprofile++;
866 if (ent->priv.required->free)
868 if (PRVM_E_FLOAT(ent,f) != s)
871 PRVM_EDICTFIELDVALUE(ent,prog->fieldoffsets.chain)->edict = PRVM_EDICT_TO_PROG(chain);
875 VM_RETURN_EDICT(chain);
879 ========================
882 entity findflags(entity start, .float field, float match)
883 ========================
885 // LordHavoc: search for flags in float fields
886 void VM_findflags (void)
893 VM_SAFEPARMCOUNT(3, VM_findflags);
896 e = PRVM_G_EDICTNUM(OFS_PARM0);
897 f = PRVM_G_INT(OFS_PARM1);
898 s = (int)PRVM_G_FLOAT(OFS_PARM2);
900 for (e++ ; e < prog->num_edicts ; e++)
902 prog->xfunction->builtinsprofile++;
903 ed = PRVM_EDICT_NUM(e);
904 if (ed->priv.required->free)
906 if (!PRVM_E_FLOAT(ed,f))
908 if ((int)PRVM_E_FLOAT(ed,f) & s)
915 VM_RETURN_EDICT(prog->edicts);
919 ========================
922 entity findchainflags(.float field, float match)
923 ========================
925 // LordHavoc: chained search for flags in float fields
926 void VM_findchainflags (void)
931 prvm_edict_t *ent, *chain;
933 VM_SAFEPARMCOUNT(2, VM_findchainflags);
935 if (prog->fieldoffsets.chain < 0)
936 PRVM_ERROR("VM_findchainflags: %s doesnt have a chain field !", PRVM_NAME);
938 chain = (prvm_edict_t *)prog->edicts;
940 f = PRVM_G_INT(OFS_PARM0);
941 s = (int)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))
951 if (!((int)PRVM_E_FLOAT(ent,f) & s))
954 PRVM_EDICTFIELDVALUE(ent,prog->fieldoffsets.chain)->edict = PRVM_EDICT_TO_PROG(chain);
958 VM_RETURN_EDICT(chain);
965 string precache_sound (string sample)
968 void VM_precache_sound (void)
972 VM_SAFEPARMCOUNT(1, VM_precache_sound);
974 s = PRVM_G_STRING(OFS_PARM0);
975 PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
976 VM_CheckEmptyString(s);
978 if(snd_initialized.integer && !S_PrecacheSound(s, true, false))
980 VM_Warning("VM_precache_sound: Failed to load %s for %s\n", s, PRVM_NAME);
989 returns the same string as output
991 does nothing, only used by qcc to build .pak archives
994 void VM_precache_file (void)
996 VM_SAFEPARMCOUNT(1,VM_precache_file);
997 // precache_file is only used to copy files with qcc, it does nothing
998 PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
1008 void VM_coredump (void)
1010 VM_SAFEPARMCOUNT(0,VM_coredump);
1012 Cbuf_AddText("prvm_edicts ");
1013 Cbuf_AddText(PRVM_NAME);
1024 void PRVM_StackTrace(void);
1025 void VM_stackdump (void)
1027 VM_SAFEPARMCOUNT(0, VM_stackdump);
1042 VM_SAFEPARMCOUNT(0, VM_crash);
1044 PRVM_ERROR("Crash called by %s",PRVM_NAME);
1054 void VM_traceon (void)
1056 VM_SAFEPARMCOUNT(0,VM_traceon);
1068 void VM_traceoff (void)
1070 VM_SAFEPARMCOUNT(0,VM_traceoff);
1072 prog->trace = false;
1082 void VM_eprint (void)
1084 VM_SAFEPARMCOUNT(1,VM_eprint);
1086 PRVM_ED_PrintNum (PRVM_G_EDICTNUM(OFS_PARM0), NULL);
1099 VM_SAFEPARMCOUNT(1,VM_rint);
1101 f = PRVM_G_FLOAT(OFS_PARM0);
1103 PRVM_G_FLOAT(OFS_RETURN) = floor(f + 0.5);
1105 PRVM_G_FLOAT(OFS_RETURN) = ceil(f - 0.5);
1115 void VM_floor (void)
1117 VM_SAFEPARMCOUNT(1,VM_floor);
1119 PRVM_G_FLOAT(OFS_RETURN) = floor(PRVM_G_FLOAT(OFS_PARM0));
1131 VM_SAFEPARMCOUNT(1,VM_ceil);
1133 PRVM_G_FLOAT(OFS_RETURN) = ceil(PRVM_G_FLOAT(OFS_PARM0));
1141 entity nextent(entity)
1144 void VM_nextent (void)
1149 VM_SAFEPARMCOUNT(1, VM_nextent);
1151 i = PRVM_G_EDICTNUM(OFS_PARM0);
1154 prog->xfunction->builtinsprofile++;
1156 if (i == prog->num_edicts)
1158 VM_RETURN_EDICT(prog->edicts);
1161 ent = PRVM_EDICT_NUM(i);
1162 if (!ent->priv.required->free)
1164 VM_RETURN_EDICT(ent);
1170 //=============================================================================
1177 changelevel(string map)
1180 void VM_changelevel (void)
1182 VM_SAFEPARMCOUNT(1, VM_changelevel);
1186 VM_Warning("VM_changelevel: game is not server (%s)\n", PRVM_NAME);
1190 // make sure we don't issue two changelevels
1191 if (svs.changelevel_issued)
1193 svs.changelevel_issued = true;
1195 Cbuf_AddText (va("changelevel %s\n",PRVM_G_STRING(OFS_PARM0)));
1207 VM_SAFEPARMCOUNT(1,VM_sin);
1208 PRVM_G_FLOAT(OFS_RETURN) = sin(PRVM_G_FLOAT(OFS_PARM0));
1219 VM_SAFEPARMCOUNT(1,VM_cos);
1220 PRVM_G_FLOAT(OFS_RETURN) = cos(PRVM_G_FLOAT(OFS_PARM0));
1232 VM_SAFEPARMCOUNT(1,VM_sqrt);
1233 PRVM_G_FLOAT(OFS_RETURN) = sqrt(PRVM_G_FLOAT(OFS_PARM0));
1245 VM_SAFEPARMCOUNT(1,VM_asin);
1246 PRVM_G_FLOAT(OFS_RETURN) = asin(PRVM_G_FLOAT(OFS_PARM0));
1257 VM_SAFEPARMCOUNT(1,VM_acos);
1258 PRVM_G_FLOAT(OFS_RETURN) = acos(PRVM_G_FLOAT(OFS_PARM0));
1269 VM_SAFEPARMCOUNT(1,VM_atan);
1270 PRVM_G_FLOAT(OFS_RETURN) = atan(PRVM_G_FLOAT(OFS_PARM0));
1276 float atan2(float,float)
1279 void VM_atan2 (void)
1281 VM_SAFEPARMCOUNT(2,VM_atan2);
1282 PRVM_G_FLOAT(OFS_RETURN) = atan2(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1293 VM_SAFEPARMCOUNT(1,VM_tan);
1294 PRVM_G_FLOAT(OFS_RETURN) = tan(PRVM_G_FLOAT(OFS_PARM0));
1301 Returns a vector of length < 1 and > 0
1306 void VM_randomvec (void)
1311 VM_SAFEPARMCOUNT(0, VM_randomvec);
1316 temp[0] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1317 temp[1] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1318 temp[2] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1320 while (DotProduct(temp, temp) >= 1);
1321 VectorCopy (temp, PRVM_G_VECTOR(OFS_RETURN));
1324 temp[0] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1325 temp[1] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1326 temp[2] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1327 // length returned always > 0
1328 length = (rand()&32766 + 1) * (1.0 / 32767.0) / VectorLength(temp);
1329 VectorScale(temp,length, temp);*/
1330 //VectorCopy(temp, PRVM_G_VECTOR(OFS_RETURN));
1333 //=============================================================================
1339 float registercvar (string name, string value[, float flags])
1342 void VM_registercvar (void)
1344 const char *name, *value;
1347 VM_SAFEPARMCOUNTRANGE(2, 3, VM_registercvar);
1349 name = PRVM_G_STRING(OFS_PARM0);
1350 value = PRVM_G_STRING(OFS_PARM1);
1351 flags = prog->argc >= 3 ? (int)PRVM_G_FLOAT(OFS_PARM2) : 0;
1352 PRVM_G_FLOAT(OFS_RETURN) = 0;
1354 if(flags > CVAR_MAXFLAGSVAL)
1357 // first check to see if it has already been defined
1358 if (Cvar_FindVar (name))
1361 // check for overlap with a command
1362 if (Cmd_Exists (name))
1364 VM_Warning("VM_registercvar: %s is a command\n", name);
1368 Cvar_Get(name, value, flags);
1370 PRVM_G_FLOAT(OFS_RETURN) = 1; // success
1378 returns the minimum of two supplied floats
1380 float min(float a, float b, ...[float])
1385 VM_SAFEPARMCOUNTRANGE(2, 8, VM_min);
1386 // LordHavoc: 3+ argument enhancement suggested by FrikaC
1387 if (prog->argc >= 3)
1390 float f = PRVM_G_FLOAT(OFS_PARM0);
1391 for (i = 1;i < prog->argc;i++)
1392 if (f > PRVM_G_FLOAT((OFS_PARM0+i*3)))
1393 f = PRVM_G_FLOAT((OFS_PARM0+i*3));
1394 PRVM_G_FLOAT(OFS_RETURN) = f;
1397 PRVM_G_FLOAT(OFS_RETURN) = min(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1404 returns the maximum of two supplied floats
1406 float max(float a, float b, ...[float])
1411 VM_SAFEPARMCOUNTRANGE(2, 8, VM_max);
1412 // LordHavoc: 3+ argument enhancement suggested by FrikaC
1413 if (prog->argc >= 3)
1416 float f = PRVM_G_FLOAT(OFS_PARM0);
1417 for (i = 1;i < prog->argc;i++)
1418 if (f < PRVM_G_FLOAT((OFS_PARM0+i*3)))
1419 f = PRVM_G_FLOAT((OFS_PARM0+i*3));
1420 PRVM_G_FLOAT(OFS_RETURN) = f;
1423 PRVM_G_FLOAT(OFS_RETURN) = max(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1430 returns number bounded by supplied range
1432 float bound(float min, float value, float max)
1435 void VM_bound (void)
1437 VM_SAFEPARMCOUNT(3,VM_bound);
1438 PRVM_G_FLOAT(OFS_RETURN) = bound(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1), PRVM_G_FLOAT(OFS_PARM2));
1445 returns a raised to power b
1447 float pow(float a, float b)
1452 VM_SAFEPARMCOUNT(2,VM_pow);
1453 PRVM_G_FLOAT(OFS_RETURN) = pow(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1456 void VM_Files_Init(void)
1459 for (i = 0;i < PRVM_MAX_OPENFILES;i++)
1460 prog->openfiles[i] = NULL;
1463 void VM_Files_CloseAll(void)
1466 for (i = 0;i < PRVM_MAX_OPENFILES;i++)
1468 if (prog->openfiles[i])
1469 FS_Close(prog->openfiles[i]);
1470 prog->openfiles[i] = NULL;
1474 static qfile_t *VM_GetFileHandle( int index )
1476 if (index < 0 || index >= PRVM_MAX_OPENFILES)
1478 Con_Printf("VM_GetFileHandle: invalid file handle %i used in %s\n", index, PRVM_NAME);
1481 if (prog->openfiles[index] == NULL)
1483 Con_Printf("VM_GetFileHandle: no such file handle %i (or file has been closed) in %s\n", index, PRVM_NAME);
1486 return prog->openfiles[index];
1493 float fopen(string filename, float mode)
1496 // float(string filename, float mode) fopen = #110;
1497 // opens a file inside quake/gamedir/data/ (mode is FILE_READ, FILE_APPEND, or FILE_WRITE),
1498 // returns fhandle >= 0 if successful, or fhandle < 0 if unable to open file for any reason
1502 const char *modestring, *filename;
1504 VM_SAFEPARMCOUNT(2,VM_fopen);
1506 for (filenum = 0;filenum < PRVM_MAX_OPENFILES;filenum++)
1507 if (prog->openfiles[filenum] == NULL)
1509 if (filenum >= PRVM_MAX_OPENFILES)
1511 PRVM_G_FLOAT(OFS_RETURN) = -2;
1512 VM_Warning("VM_fopen: %s ran out of file handles (%i)\n", PRVM_NAME, PRVM_MAX_OPENFILES);
1515 mode = (int)PRVM_G_FLOAT(OFS_PARM1);
1518 case 0: // FILE_READ
1521 case 1: // FILE_APPEND
1524 case 2: // FILE_WRITE
1528 PRVM_G_FLOAT(OFS_RETURN) = -3;
1529 VM_Warning("VM_fopen: %s: no such mode %i (valid: 0 = read, 1 = append, 2 = write)\n", PRVM_NAME, mode);
1532 filename = PRVM_G_STRING(OFS_PARM0);
1534 prog->openfiles[filenum] = FS_Open(va("data/%s", filename), modestring, false, false);
1535 if (prog->openfiles[filenum] == NULL && mode == 0)
1536 prog->openfiles[filenum] = FS_Open(va("%s", filename), modestring, false, false);
1538 if (prog->openfiles[filenum] == NULL)
1540 PRVM_G_FLOAT(OFS_RETURN) = -1;
1541 if (developer.integer >= 100)
1542 VM_Warning("VM_fopen: %s: %s mode %s failed\n", PRVM_NAME, filename, modestring);
1546 PRVM_G_FLOAT(OFS_RETURN) = filenum;
1547 if (developer.integer >= 100)
1548 Con_Printf("VM_fopen: %s: %s mode %s opened as #%i\n", PRVM_NAME, filename, modestring, filenum);
1556 fclose(float fhandle)
1559 //void(float fhandle) fclose = #111; // closes a file
1560 void VM_fclose(void)
1564 VM_SAFEPARMCOUNT(1,VM_fclose);
1566 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1567 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
1569 VM_Warning("VM_fclose: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1572 if (prog->openfiles[filenum] == NULL)
1574 VM_Warning("VM_fclose: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1577 FS_Close(prog->openfiles[filenum]);
1578 prog->openfiles[filenum] = NULL;
1579 if (developer.integer >= 100)
1580 Con_Printf("VM_fclose: %s: #%i closed\n", PRVM_NAME, filenum);
1587 string fgets(float fhandle)
1590 //string(float fhandle) fgets = #112; // reads a line of text from the file and returns as a tempstring
1594 char string[VM_STRINGTEMP_LENGTH];
1597 VM_SAFEPARMCOUNT(1,VM_fgets);
1599 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1600 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
1602 VM_Warning("VM_fgets: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1605 if (prog->openfiles[filenum] == NULL)
1607 VM_Warning("VM_fgets: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1613 c = FS_Getc(prog->openfiles[filenum]);
1614 if (c == '\r' || c == '\n' || c < 0)
1616 if (end < VM_STRINGTEMP_LENGTH - 1)
1620 // remove \n following \r
1623 c = FS_Getc(prog->openfiles[filenum]);
1625 FS_UnGetc(prog->openfiles[filenum], (unsigned char)c);
1627 if (developer.integer >= 100)
1628 Con_Printf("fgets: %s: %s\n", PRVM_NAME, string);
1630 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string);
1632 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
1639 fputs(float fhandle, string s)
1642 //void(float fhandle, string s) fputs = #113; // writes a line of text to the end of the file
1646 char string[VM_STRINGTEMP_LENGTH];
1649 VM_SAFEPARMCOUNT(2,VM_fputs);
1651 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1652 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
1654 VM_Warning("VM_fputs: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1657 if (prog->openfiles[filenum] == NULL)
1659 VM_Warning("VM_fputs: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1662 VM_VarString(1, string, sizeof(string));
1663 if ((stringlength = (int)strlen(string)))
1664 FS_Write(prog->openfiles[filenum], string, stringlength);
1665 if (developer.integer >= 100)
1666 Con_Printf("fputs: %s: %s\n", PRVM_NAME, string);
1673 writetofile(float fhandle, entity ent)
1676 void VM_writetofile(void)
1681 VM_SAFEPARMCOUNT(2, VM_writetofile);
1683 file = VM_GetFileHandle( (int)PRVM_G_FLOAT(OFS_PARM0) );
1686 VM_Warning("VM_writetofile: invalid or closed file handle\n");
1690 ent = PRVM_G_EDICT(OFS_PARM1);
1691 if(ent->priv.required->free)
1693 VM_Warning("VM_writetofile: %s: entity %i is free !\n", PRVM_NAME, PRVM_NUM_FOR_EDICT(ent));
1697 PRVM_ED_Write (file, ent);
1704 float strlen(string s)
1707 //float(string s) strlen = #114; // returns how many characters are in a string
1708 void VM_strlen(void)
1710 VM_SAFEPARMCOUNT(1,VM_strlen);
1712 PRVM_G_FLOAT(OFS_RETURN) = strlen(PRVM_G_STRING(OFS_PARM0));
1715 // DRESK - Decolorized String
1720 string strdecolorize(string s)
1723 // string (string s) strdecolorize = #472; // returns the passed in string with color codes stripped
1724 void VM_strdecolorize(void)
1726 char szNewString[VM_STRINGTEMP_LENGTH];
1727 const char *szString;
1730 VM_SAFEPARMCOUNT(1,VM_strdecolorize);
1731 szString = PRVM_G_STRING(OFS_PARM0);
1733 COM_StringDecolorize(szString, 0, szNewString, sizeof(szNewString), TRUE);
1735 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(szNewString);
1738 // DRESK - String Length (not counting color codes)
1743 float strlennocol(string s)
1746 // float(string s) strlennocol = #471; // returns how many characters are in a string not including color codes
1747 // For example, ^2Dresk returns a length of 5
1748 void VM_strlennocol(void)
1750 const char *szString;
1753 VM_SAFEPARMCOUNT(1,VM_strlennocol);
1755 szString = PRVM_G_STRING(OFS_PARM0);
1757 nCnt = COM_StringLengthNoColors(szString, 0, NULL);
1759 PRVM_G_FLOAT(OFS_RETURN) = nCnt;
1762 // DRESK - String to Uppercase and Lowercase
1767 string strtolower(string s)
1770 // string (string s) strtolower = #480; // returns passed in string in lowercase form
1771 void VM_strtolower(void)
1773 char szNewString[VM_STRINGTEMP_LENGTH];
1774 const char *szString;
1777 VM_SAFEPARMCOUNT(1,VM_strtolower);
1778 szString = PRVM_G_STRING(OFS_PARM0);
1780 COM_ToLowerString(szString, szNewString, sizeof(szNewString) );
1782 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(szNewString);
1789 string strtoupper(string s)
1792 // string (string s) strtoupper = #481; // returns passed in string in uppercase form
1793 void VM_strtoupper(void)
1795 char szNewString[VM_STRINGTEMP_LENGTH];
1796 const char *szString;
1799 VM_SAFEPARMCOUNT(1,VM_strtoupper);
1800 szString = PRVM_G_STRING(OFS_PARM0);
1802 COM_ToUpperString(szString, szNewString, sizeof(szNewString) );
1804 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(szNewString);
1811 string strcat(string,string,...[string])
1814 //string(string s1, string s2) strcat = #115;
1815 // concatenates two strings (for example "abc", "def" would return "abcdef")
1816 // and returns as a tempstring
1817 void VM_strcat(void)
1819 char s[VM_STRINGTEMP_LENGTH];
1820 VM_SAFEPARMCOUNTRANGE(1, 8, VM_strcat);
1822 VM_VarString(0, s, sizeof(s));
1823 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(s);
1830 string substring(string s, float start, float length)
1833 // string(string s, float start, float length) substring = #116;
1834 // returns a section of a string as a tempstring
1835 void VM_substring(void)
1837 int i, start, length;
1839 char string[VM_STRINGTEMP_LENGTH];
1841 VM_SAFEPARMCOUNT(3,VM_substring);
1843 s = PRVM_G_STRING(OFS_PARM0);
1844 start = (int)PRVM_G_FLOAT(OFS_PARM1);
1845 length = (int)PRVM_G_FLOAT(OFS_PARM2);
1846 for (i = 0;i < start && *s;i++, s++);
1847 for (i = 0;i < (int)sizeof(string) - 1 && *s && i < length;i++, s++)
1850 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string);
1857 string(string search, string replace, string subject) strreplace = #484;
1860 // replaces all occurrences of search with replace in the string subject, and returns the result
1861 void VM_strreplace(void)
1864 const char *search, *replace, *subject;
1865 char string[VM_STRINGTEMP_LENGTH];
1866 int search_len, replace_len, subject_len;
1868 VM_SAFEPARMCOUNT(3,VM_strreplace);
1870 search = PRVM_G_STRING(OFS_PARM0);
1871 replace = PRVM_G_STRING(OFS_PARM1);
1872 subject = PRVM_G_STRING(OFS_PARM2);
1874 search_len = (int)strlen(search);
1875 replace_len = (int)strlen(replace);
1876 subject_len = (int)strlen(subject);
1879 for (i = 0; i < subject_len; i++)
1881 for (j = 0; j < search_len && i+j < subject_len; j++)
1882 if (subject[i+j] != search[j])
1884 if (j == search_len || i+j == subject_len)
1886 // found it at offset 'i'
1887 for (j = 0; j < replace_len && si < (int)sizeof(string) - 1; j++)
1888 string[si++] = replace[j];
1889 i += search_len - 1;
1894 if (si < (int)sizeof(string) - 1)
1895 string[si++] = subject[i];
1900 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string);
1907 string(string search, string replace, string subject) strireplace = #485;
1910 // case-insensitive version of strreplace
1911 void VM_strireplace(void)
1914 const char *search, *replace, *subject;
1915 char string[VM_STRINGTEMP_LENGTH];
1916 int search_len, replace_len, subject_len;
1918 VM_SAFEPARMCOUNT(3,VM_strreplace);
1920 search = PRVM_G_STRING(OFS_PARM0);
1921 replace = PRVM_G_STRING(OFS_PARM1);
1922 subject = PRVM_G_STRING(OFS_PARM2);
1924 search_len = (int)strlen(search);
1925 replace_len = (int)strlen(replace);
1926 subject_len = (int)strlen(subject);
1929 for (i = 0; i < subject_len; i++)
1931 for (j = 0; j < search_len && i+j < subject_len; j++)
1932 if (tolower(subject[i+j]) != tolower(search[j]))
1934 if (j == search_len || i+j == subject_len)
1936 // found it at offset 'i'
1937 for (j = 0; j < replace_len && si < (int)sizeof(string) - 1; j++)
1938 string[si++] = replace[j];
1939 i += search_len - 1;
1944 if (si < (int)sizeof(string) - 1)
1945 string[si++] = subject[i];
1950 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string);
1957 vector stov(string s)
1960 //vector(string s) stov = #117; // returns vector value from a string
1963 char string[VM_STRINGTEMP_LENGTH];
1965 VM_SAFEPARMCOUNT(1,VM_stov);
1967 VM_VarString(0, string, sizeof(string));
1968 Math_atov(string, PRVM_G_VECTOR(OFS_RETURN));
1975 string strzone(string s)
1978 //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)
1979 void VM_strzone(void)
1982 char string[VM_STRINGTEMP_LENGTH];
1985 VM_SAFEPARMCOUNT(1,VM_strzone);
1987 VM_VarString(0, string, sizeof(string));
1988 alloclen = strlen(string) + 1;
1989 PRVM_G_INT(OFS_RETURN) = PRVM_AllocString(alloclen, &out);
1990 memcpy(out, string, alloclen);
2000 //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!!!)
2001 void VM_strunzone(void)
2003 VM_SAFEPARMCOUNT(1,VM_strunzone);
2004 PRVM_FreeString(PRVM_G_INT(OFS_PARM0));
2009 VM_command (used by client and menu)
2011 clientcommand(float client, string s) (for client and menu)
2014 //void(entity e, string s) clientcommand = #440; // executes a command string as if it came from the specified client
2015 //this function originally written by KrimZon, made shorter by LordHavoc
2016 void VM_clcommand (void)
2018 client_t *temp_client;
2021 VM_SAFEPARMCOUNT(2,VM_clcommand);
2023 i = (int)PRVM_G_FLOAT(OFS_PARM0);
2024 if (!sv.active || i < 0 || i >= svs.maxclients || !svs.clients[i].active)
2026 VM_Warning("VM_clientcommand: %s: invalid client/server is not active !\n", PRVM_NAME);
2030 temp_client = host_client;
2031 host_client = svs.clients + i;
2032 Cmd_ExecuteString (PRVM_G_STRING(OFS_PARM1), src_client);
2033 host_client = temp_client;
2041 float tokenize(string s)
2044 //float(string s) tokenize = #441; // takes apart a string into individal words (access them with argv), returns how many
2045 //this function originally written by KrimZon, made shorter by LordHavoc
2046 //20040203: rewritten by LordHavoc (no longer uses allocations)
2049 void VM_tokenize (void)
2052 static char string[VM_STRINGTEMP_LENGTH]; // static, because it's big
2054 VM_SAFEPARMCOUNT(1,VM_tokenize);
2056 strlcpy(string, PRVM_G_STRING(OFS_PARM0), sizeof(string));
2060 while(COM_ParseToken_VM_Tokenize(&p, false))
2062 if (num_tokens >= (int)(sizeof(tokens)/sizeof(tokens[0])))
2064 tokens[num_tokens++] = PRVM_SetTempString(com_token);
2067 PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
2072 VM_tokenizebyseparator
2074 float tokenizebyseparator(string s, string separator1, ...)
2077 //float(string s, string separator1, ...) tokenizebyseparator = #479; // takes apart a string into individal words (access them with argv), returns how many
2078 //this function returns the token preceding each instance of a separator (of
2079 //which there can be multiple), and the text following the last separator
2080 //useful for parsing certain kinds of data like IP addresses
2082 //numnumbers = tokenizebyseparator("10.1.2.3", ".");
2083 //returns 4 and the tokens "10" "1" "2" "3".
2084 void VM_tokenizebyseparator (void)
2088 int separatorlen[7];
2089 const char *separators[7];
2092 char tokentext[MAX_INPUTLINE];
2093 static char string[VM_STRINGTEMP_LENGTH]; // static, because it's big
2095 VM_SAFEPARMCOUNTRANGE(2, 8,VM_tokenizebyseparator);
2097 strlcpy(string, PRVM_G_STRING(OFS_PARM0), sizeof(string));
2101 for (j = 1;j < prog->argc;j++)
2103 // skip any blank separator strings
2104 const char *s = PRVM_G_STRING(OFS_PARM0+j*3);
2107 separators[numseparators] = s;
2108 separatorlen[numseparators] = strlen(s);
2115 while (num_tokens < (int)(sizeof(tokens)/sizeof(tokens[0])))
2117 token = tokentext + j;
2120 for (k = 0;k < numseparators;k++)
2122 if (!strncmp(p, separators[k], separatorlen[k]))
2124 p += separatorlen[k];
2128 if (k < numseparators)
2130 if (j < (int)sizeof(tokentext)-1)
2131 tokentext[j++] = *p;
2134 if (j >= (int)sizeof(tokentext))
2137 tokens[num_tokens++] = PRVM_SetTempString(token);
2142 PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
2145 //string(float n) argv = #442; // returns a word from the tokenized string (returns nothing for an invalid index)
2146 //this function originally written by KrimZon, made shorter by LordHavoc
2151 VM_SAFEPARMCOUNT(1,VM_argv);
2153 token_num = (int)PRVM_G_FLOAT(OFS_PARM0);
2155 if (token_num >= 0 && token_num < num_tokens)
2156 PRVM_G_INT(OFS_RETURN) = tokens[token_num];
2158 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
2168 void VM_isserver(void)
2170 VM_SAFEPARMCOUNT(0,VM_serverstate);
2172 PRVM_G_FLOAT(OFS_RETURN) = sv.active && (svs.maxclients > 1 || cls.state == ca_dedicated);
2182 void VM_clientcount(void)
2184 VM_SAFEPARMCOUNT(0,VM_clientcount);
2186 PRVM_G_FLOAT(OFS_RETURN) = svs.maxclients;
2196 void VM_clientstate(void)
2198 VM_SAFEPARMCOUNT(0,VM_clientstate);
2200 PRVM_G_FLOAT(OFS_RETURN) = cls.state;
2207 float getostype(void)
2209 */ // not used at the moment -> not included in the common list
2210 void VM_getostype(void)
2212 VM_SAFEPARMCOUNT(0,VM_getostype);
2217 OS_MAC - not supported
2221 PRVM_G_FLOAT(OFS_RETURN) = 0;
2222 #elif defined(MACOSX)
2223 PRVM_G_FLOAT(OFS_RETURN) = 2;
2225 PRVM_G_FLOAT(OFS_RETURN) = 1;
2233 vector getmousepos()
2236 void VM_getmousepos(void)
2239 VM_SAFEPARMCOUNT(0,VM_getmousepos);
2241 PRVM_G_VECTOR(OFS_RETURN)[0] = in_mouse_x * vid_conwidth.integer / vid.width;
2242 PRVM_G_VECTOR(OFS_RETURN)[1] = in_mouse_y * vid_conheight.integer / vid.height;
2243 PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
2253 void VM_gettime(void)
2255 VM_SAFEPARMCOUNT(0,VM_gettime);
2257 PRVM_G_FLOAT(OFS_RETURN) = (float) realtime;
2264 loadfromdata(string data)
2267 void VM_loadfromdata(void)
2269 VM_SAFEPARMCOUNT(1,VM_loadentsfromfile);
2271 PRVM_ED_LoadFromFile(PRVM_G_STRING(OFS_PARM0));
2275 ========================
2278 parseentitydata(entity ent, string data)
2279 ========================
2281 void VM_parseentitydata(void)
2286 VM_SAFEPARMCOUNT(2, VM_parseentitydata);
2288 // get edict and test it
2289 ent = PRVM_G_EDICT(OFS_PARM0);
2290 if (ent->priv.required->free)
2291 PRVM_ERROR ("VM_parseentitydata: %s: Can only set already spawned entities (entity %i is free)!", PRVM_NAME, PRVM_NUM_FOR_EDICT(ent));
2293 data = PRVM_G_STRING(OFS_PARM1);
2295 // parse the opening brace
2296 if (!COM_ParseToken_Simple(&data, false, false) || com_token[0] != '{' )
2297 PRVM_ERROR ("VM_parseentitydata: %s: Couldn't parse entity data:\n%s", PRVM_NAME, data );
2299 PRVM_ED_ParseEdict (data, ent);
2306 loadfromfile(string file)
2309 void VM_loadfromfile(void)
2311 const char *filename;
2314 VM_SAFEPARMCOUNT(1,VM_loadfromfile);
2316 filename = PRVM_G_STRING(OFS_PARM0);
2317 if (FS_CheckNastyPath(filename, false))
2319 PRVM_G_FLOAT(OFS_RETURN) = -4;
2320 VM_Warning("VM_loadfromfile: %s dangerous or non-portable filename \"%s\" not allowed. (contains : or \\ or begins with .. or /)\n", PRVM_NAME, filename);
2324 // not conform with VM_fopen
2325 data = (char *)FS_LoadFile(filename, tempmempool, false, NULL);
2327 PRVM_G_FLOAT(OFS_RETURN) = -1;
2329 PRVM_ED_LoadFromFile(data);
2340 float mod(float val, float m)
2343 void VM_modulo(void)
2346 VM_SAFEPARMCOUNT(2,VM_module);
2348 val = (int) PRVM_G_FLOAT(OFS_PARM0);
2349 m = (int) PRVM_G_FLOAT(OFS_PARM1);
2351 PRVM_G_FLOAT(OFS_RETURN) = (float) (val % m);
2354 void VM_Search_Init(void)
2357 for (i = 0;i < PRVM_MAX_OPENSEARCHES;i++)
2358 prog->opensearches[i] = NULL;
2361 void VM_Search_Reset(void)
2364 // reset the fssearch list
2365 for(i = 0; i < PRVM_MAX_OPENSEARCHES; i++)
2367 if(prog->opensearches[i])
2368 FS_FreeSearch(prog->opensearches[i]);
2369 prog->opensearches[i] = NULL;
2377 float search_begin(string pattern, float caseinsensitive, float quiet)
2380 void VM_search_begin(void)
2383 const char *pattern;
2384 int caseinsens, quiet;
2386 VM_SAFEPARMCOUNT(3, VM_search_begin);
2388 pattern = PRVM_G_STRING(OFS_PARM0);
2390 VM_CheckEmptyString(pattern);
2392 caseinsens = (int)PRVM_G_FLOAT(OFS_PARM1);
2393 quiet = (int)PRVM_G_FLOAT(OFS_PARM2);
2395 for(handle = 0; handle < PRVM_MAX_OPENSEARCHES; handle++)
2396 if(!prog->opensearches[handle])
2399 if(handle >= PRVM_MAX_OPENSEARCHES)
2401 PRVM_G_FLOAT(OFS_RETURN) = -2;
2402 VM_Warning("VM_search_begin: %s ran out of search handles (%i)\n", PRVM_NAME, PRVM_MAX_OPENSEARCHES);
2406 if(!(prog->opensearches[handle] = FS_Search(pattern,caseinsens, quiet)))
2407 PRVM_G_FLOAT(OFS_RETURN) = -1;
2409 PRVM_G_FLOAT(OFS_RETURN) = handle;
2416 void search_end(float handle)
2419 void VM_search_end(void)
2422 VM_SAFEPARMCOUNT(1, VM_search_end);
2424 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
2426 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
2428 VM_Warning("VM_search_end: invalid handle %i used in %s\n", handle, PRVM_NAME);
2431 if(prog->opensearches[handle] == NULL)
2433 VM_Warning("VM_search_end: no such handle %i in %s\n", handle, PRVM_NAME);
2437 FS_FreeSearch(prog->opensearches[handle]);
2438 prog->opensearches[handle] = NULL;
2445 float search_getsize(float handle)
2448 void VM_search_getsize(void)
2451 VM_SAFEPARMCOUNT(1, VM_M_search_getsize);
2453 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
2455 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
2457 VM_Warning("VM_search_getsize: invalid handle %i used in %s\n", handle, PRVM_NAME);
2460 if(prog->opensearches[handle] == NULL)
2462 VM_Warning("VM_search_getsize: no such handle %i in %s\n", handle, PRVM_NAME);
2466 PRVM_G_FLOAT(OFS_RETURN) = prog->opensearches[handle]->numfilenames;
2471 VM_search_getfilename
2473 string search_getfilename(float handle, float num)
2476 void VM_search_getfilename(void)
2478 int handle, filenum;
2479 VM_SAFEPARMCOUNT(2, VM_search_getfilename);
2481 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
2482 filenum = (int)PRVM_G_FLOAT(OFS_PARM1);
2484 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
2486 VM_Warning("VM_search_getfilename: invalid handle %i used in %s\n", handle, PRVM_NAME);
2489 if(prog->opensearches[handle] == NULL)
2491 VM_Warning("VM_search_getfilename: no such handle %i in %s\n", handle, PRVM_NAME);
2494 if(filenum < 0 || filenum >= prog->opensearches[handle]->numfilenames)
2496 VM_Warning("VM_search_getfilename: invalid filenum %i in %s\n", filenum, PRVM_NAME);
2500 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog->opensearches[handle]->filenames[filenum]);
2507 string chr(float ascii)
2513 VM_SAFEPARMCOUNT(1, VM_chr);
2515 tmp[0] = (unsigned char) PRVM_G_FLOAT(OFS_PARM0);
2518 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(tmp);
2521 //=============================================================================
2522 // Draw builtins (client & menu)
2528 float iscachedpic(string pic)
2531 void VM_iscachedpic(void)
2533 VM_SAFEPARMCOUNT(1,VM_iscachedpic);
2535 // drawq hasnt such a function, thus always return true
2536 PRVM_G_FLOAT(OFS_RETURN) = false;
2543 string precache_pic(string pic)
2546 void VM_precache_pic(void)
2550 VM_SAFEPARMCOUNT(1, VM_precache_pic);
2552 s = PRVM_G_STRING(OFS_PARM0);
2553 PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
2554 VM_CheckEmptyString (s);
2556 // AK Draw_CachePic is supposed to always return a valid pointer
2557 if( Draw_CachePic(s, false)->tex == r_texture_notexture )
2558 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
2568 void VM_freepic(void)
2572 VM_SAFEPARMCOUNT(1,VM_freepic);
2574 s = PRVM_G_STRING(OFS_PARM0);
2575 VM_CheckEmptyString (s);
2580 dp_font_t *getdrawfont()
2582 if(prog->globaloffsets.drawfont >= 0)
2584 int f = PRVM_G_FLOAT(prog->globaloffsets.drawfont);
2585 if(f < 0 || f >= MAX_FONTS)
2586 return FONT_DEFAULT;
2587 return &dp_fonts[f];
2590 return FONT_DEFAULT;
2597 float drawcharacter(vector position, float character, vector scale, vector rgb, float alpha, float flag)
2600 void VM_drawcharacter(void)
2602 float *pos,*scale,*rgb;
2605 VM_SAFEPARMCOUNT(6,VM_drawcharacter);
2607 character = (char) PRVM_G_FLOAT(OFS_PARM1);
2610 PRVM_G_FLOAT(OFS_RETURN) = -1;
2611 VM_Warning("VM_drawcharacter: %s passed null character !\n",PRVM_NAME);
2615 pos = PRVM_G_VECTOR(OFS_PARM0);
2616 scale = 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_drawcharacter: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2627 if(pos[2] || scale[2])
2628 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")));
2630 if(!scale[0] || !scale[1])
2632 PRVM_G_FLOAT(OFS_RETURN) = -3;
2633 VM_Warning("VM_drawcharacter: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
2637 DrawQ_String_Font(pos[0], pos[1], &character, 1, scale[0], scale[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM4), flag, NULL, true, getdrawfont());
2638 PRVM_G_FLOAT(OFS_RETURN) = 1;
2645 float drawstring(vector position, string text, vector scale, vector rgb, float alpha, float flag)
2648 void VM_drawstring(void)
2650 float *pos,*scale,*rgb;
2653 VM_SAFEPARMCOUNT(6,VM_drawstring);
2655 string = PRVM_G_STRING(OFS_PARM1);
2656 pos = PRVM_G_VECTOR(OFS_PARM0);
2657 scale = PRVM_G_VECTOR(OFS_PARM2);
2658 rgb = PRVM_G_VECTOR(OFS_PARM3);
2659 flag = (int)PRVM_G_FLOAT(OFS_PARM5);
2661 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2663 PRVM_G_FLOAT(OFS_RETURN) = -2;
2664 VM_Warning("VM_drawstring: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2668 if(!scale[0] || !scale[1])
2670 PRVM_G_FLOAT(OFS_RETURN) = -3;
2671 VM_Warning("VM_drawstring: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
2675 if(pos[2] || scale[2])
2676 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")));
2678 DrawQ_String_Font(pos[0], pos[1], string, 0, scale[0], scale[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM4), flag, NULL, true, getdrawfont());
2679 PRVM_G_FLOAT(OFS_RETURN) = 1;
2684 VM_drawcolorcodedstring
2686 float drawcolorcodedstring(vector position, string text, vector scale, float alpha, float flag)
2689 void VM_drawcolorcodedstring(void)
2694 VM_SAFEPARMCOUNT(5,VM_drawstring);
2696 string = PRVM_G_STRING(OFS_PARM1);
2697 pos = PRVM_G_VECTOR(OFS_PARM0);
2698 scale = PRVM_G_VECTOR(OFS_PARM2);
2699 flag = (int)PRVM_G_FLOAT(OFS_PARM5);
2701 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2703 PRVM_G_FLOAT(OFS_RETURN) = -2;
2704 VM_Warning("VM_drawcolorcodedstring: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2708 if(!scale[0] || !scale[1])
2710 PRVM_G_FLOAT(OFS_RETURN) = -3;
2711 VM_Warning("VM_drawcolorcodedstring: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
2715 if(pos[2] || scale[2])
2716 Con_Printf("VM_drawcolorcodedstring: z value%s from %s discarded\n",(pos[2] && scale[2]) ? "s" : " ",((pos[2] && scale[2]) ? "pos and scale" : (pos[2] ? "pos" : "scale")));
2719 DrawQ_String_Font(pos[0], pos[1], string, 0, scale[0], scale[1], 1, 1, 1, PRVM_G_FLOAT(OFS_PARM3), flag, NULL, false, getdrawfont());
2720 PRVM_G_FLOAT(OFS_RETURN) = 1;
2726 float stringwidth(string text, float allowColorCodes)
2729 void VM_stringwidth(void)
2733 VM_SAFEPARMCOUNT(2,VM_drawstring);
2735 string = PRVM_G_STRING(OFS_PARM0);
2736 colors = (int)PRVM_G_FLOAT(OFS_PARM1);
2738 PRVM_G_FLOAT(OFS_RETURN) = DrawQ_TextWidth_Font(string, 0, !colors, getdrawfont()); // 1x1 characters, don't actually draw
2744 float drawpic(vector position, string pic, vector size, vector rgb, float alpha, float flag)
2747 void VM_drawpic(void)
2749 const char *picname;
2750 float *size, *pos, *rgb;
2753 VM_SAFEPARMCOUNT(6,VM_drawpic);
2755 picname = PRVM_G_STRING(OFS_PARM1);
2756 VM_CheckEmptyString (picname);
2758 // is pic cached ? no function yet for that
2761 PRVM_G_FLOAT(OFS_RETURN) = -4;
2762 VM_Warning("VM_drawpic: %s: %s not cached !\n", PRVM_NAME, picname);
2766 pos = PRVM_G_VECTOR(OFS_PARM0);
2767 size = PRVM_G_VECTOR(OFS_PARM2);
2768 rgb = PRVM_G_VECTOR(OFS_PARM3);
2769 flag = (int) PRVM_G_FLOAT(OFS_PARM5);
2771 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2773 PRVM_G_FLOAT(OFS_RETURN) = -2;
2774 VM_Warning("VM_drawpic: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2778 if(pos[2] || size[2])
2779 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")));
2781 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);
2782 PRVM_G_FLOAT(OFS_RETURN) = 1;
2788 float drawsubpic(vector position, vector size, string pic, vector srcPos, vector srcSize, vector rgb, float alpha, float flag)
2792 void VM_drawsubpic(void)
2794 const char *picname;
2795 float *size, *pos, *rgb, *srcPos, *srcSize, alpha;
2798 VM_SAFEPARMCOUNT(8,VM_drawsubpic);
2800 picname = PRVM_G_STRING(OFS_PARM2);
2801 VM_CheckEmptyString (picname);
2803 // is pic cached ? no function yet for that
2806 PRVM_G_FLOAT(OFS_RETURN) = -4;
2807 VM_Warning("VM_drawsubpic: %s: %s not cached !\n", PRVM_NAME, picname);
2811 pos = PRVM_G_VECTOR(OFS_PARM0);
2812 size = PRVM_G_VECTOR(OFS_PARM1);
2813 srcPos = PRVM_G_VECTOR(OFS_PARM3);
2814 srcSize = PRVM_G_VECTOR(OFS_PARM4);
2815 rgb = PRVM_G_VECTOR(OFS_PARM5);
2816 alpha = PRVM_G_FLOAT(OFS_PARM6);
2817 flag = (int) PRVM_G_FLOAT(OFS_PARM7);
2819 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2821 PRVM_G_FLOAT(OFS_RETURN) = -2;
2822 VM_Warning("VM_drawsubpic: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2826 if(pos[2] || size[2])
2827 Con_Printf("VM_drawsubpic: z value%s from %s discarded\n",(pos[2] && size[2]) ? "s" : " ",((pos[2] && size[2]) ? "pos and size" : (pos[2] ? "pos" : "size")));
2829 DrawQ_SuperPic(pos[0], pos[1], Draw_CachePic(picname, true),
2831 srcPos[0], srcPos[1], rgb[0], rgb[1], rgb[2], alpha,
2832 srcPos[0] + srcSize[0], srcPos[1], rgb[0], rgb[1], rgb[2], alpha,
2833 srcPos[0], srcPos[1] + srcSize[1], rgb[0], rgb[1], rgb[2], alpha,
2834 srcPos[0] + srcSize[0], srcPos[1] + srcSize[1], rgb[0], rgb[1], rgb[2], alpha,
2836 PRVM_G_FLOAT(OFS_RETURN) = 1;
2843 float drawfill(vector position, vector size, vector rgb, float alpha, float flag)
2846 void VM_drawfill(void)
2848 float *size, *pos, *rgb;
2851 VM_SAFEPARMCOUNT(5,VM_drawfill);
2854 pos = PRVM_G_VECTOR(OFS_PARM0);
2855 size = PRVM_G_VECTOR(OFS_PARM1);
2856 rgb = PRVM_G_VECTOR(OFS_PARM2);
2857 flag = (int) PRVM_G_FLOAT(OFS_PARM4);
2859 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2861 PRVM_G_FLOAT(OFS_RETURN) = -2;
2862 VM_Warning("VM_drawfill: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2866 if(pos[2] || size[2])
2867 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")));
2869 DrawQ_Fill(pos[0], pos[1], size[0], size[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM3), flag);
2870 PRVM_G_FLOAT(OFS_RETURN) = 1;
2877 drawsetcliparea(float x, float y, float width, float height)
2880 void VM_drawsetcliparea(void)
2883 VM_SAFEPARMCOUNT(4,VM_drawsetcliparea);
2885 x = bound(0, PRVM_G_FLOAT(OFS_PARM0), vid_conwidth.integer);
2886 y = bound(0, PRVM_G_FLOAT(OFS_PARM1), vid_conheight.integer);
2887 w = bound(0, PRVM_G_FLOAT(OFS_PARM2) + PRVM_G_FLOAT(OFS_PARM0) - x, (vid_conwidth.integer - x));
2888 h = bound(0, PRVM_G_FLOAT(OFS_PARM3) + PRVM_G_FLOAT(OFS_PARM1) - y, (vid_conheight.integer - y));
2890 DrawQ_SetClipArea(x, y, w, h);
2895 VM_drawresetcliparea
2900 void VM_drawresetcliparea(void)
2902 VM_SAFEPARMCOUNT(0,VM_drawresetcliparea);
2904 DrawQ_ResetClipArea();
2911 vector getimagesize(string pic)
2914 void VM_getimagesize(void)
2919 VM_SAFEPARMCOUNT(1,VM_getimagesize);
2921 p = PRVM_G_STRING(OFS_PARM0);
2922 VM_CheckEmptyString (p);
2924 pic = Draw_CachePic (p, false);
2926 PRVM_G_VECTOR(OFS_RETURN)[0] = pic->width;
2927 PRVM_G_VECTOR(OFS_RETURN)[1] = pic->height;
2928 PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
2935 string keynumtostring(float keynum)
2938 void VM_keynumtostring (void)
2940 VM_SAFEPARMCOUNT(1, VM_keynumtostring);
2942 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(Key_KeynumToString((int)PRVM_G_FLOAT(OFS_PARM0)));
2949 float stringtokeynum(string key)
2952 void VM_stringtokeynum (void)
2954 VM_SAFEPARMCOUNT( 1, VM_keynumtostring );
2956 PRVM_G_INT(OFS_RETURN) = Key_StringToKeynum(PRVM_G_STRING(OFS_PARM0));
2959 // CL_Video interface functions
2962 ========================
2965 float cin_open(string file, string name)
2966 ========================
2968 void VM_cin_open( void )
2973 VM_SAFEPARMCOUNT( 2, VM_cin_open );
2975 file = PRVM_G_STRING( OFS_PARM0 );
2976 name = PRVM_G_STRING( OFS_PARM1 );
2978 VM_CheckEmptyString( file );
2979 VM_CheckEmptyString( name );
2981 if( CL_OpenVideo( file, name, MENUOWNER ) )
2982 PRVM_G_FLOAT( OFS_RETURN ) = 1;
2984 PRVM_G_FLOAT( OFS_RETURN ) = 0;
2988 ========================
2991 void cin_close(string name)
2992 ========================
2994 void VM_cin_close( void )
2998 VM_SAFEPARMCOUNT( 1, VM_cin_close );
3000 name = PRVM_G_STRING( OFS_PARM0 );
3001 VM_CheckEmptyString( name );
3003 CL_CloseVideo( CL_GetVideoByName( name ) );
3007 ========================
3009 void cin_setstate(string name, float type)
3010 ========================
3012 void VM_cin_setstate( void )
3015 clvideostate_t state;
3018 VM_SAFEPARMCOUNT( 2, VM_cin_netstate );
3020 name = PRVM_G_STRING( OFS_PARM0 );
3021 VM_CheckEmptyString( name );
3023 state = (clvideostate_t)((int)PRVM_G_FLOAT( OFS_PARM1 ));
3025 video = CL_GetVideoByName( name );
3026 if( video && state > CLVIDEO_UNUSED && state < CLVIDEO_STATECOUNT )
3027 CL_SetVideoState( video, state );
3031 ========================
3034 float cin_getstate(string name)
3035 ========================
3037 void VM_cin_getstate( void )
3042 VM_SAFEPARMCOUNT( 1, VM_cin_getstate );
3044 name = PRVM_G_STRING( OFS_PARM0 );
3045 VM_CheckEmptyString( name );
3047 video = CL_GetVideoByName( name );
3049 PRVM_G_FLOAT( OFS_RETURN ) = (int)video->state;
3051 PRVM_G_FLOAT( OFS_RETURN ) = 0;
3055 ========================
3058 void cin_restart(string name)
3059 ========================
3061 void VM_cin_restart( void )
3066 VM_SAFEPARMCOUNT( 1, VM_cin_restart );
3068 name = PRVM_G_STRING( OFS_PARM0 );
3069 VM_CheckEmptyString( name );
3071 video = CL_GetVideoByName( name );
3073 CL_RestartVideo( video );
3076 #ifdef SUPPORT_GECKO
3078 ========================
3080 ========================
3082 void VM_Gecko_Init( void ) {
3083 // the prog struct is memset to 0 by Initprog? [12/6/2007 Black]
3084 // FIXME: remove the other _Init functions then, too? [12/6/2007 Black]
3088 ========================
3090 ========================
3092 void VM_Gecko_Destroy( void ) {
3094 for( i = 0 ; i < PRVM_MAX_GECKOINSTANCES ; i++ ) {
3095 clgecko_t **instance = &prog->opengeckoinstances[ i ];
3097 CL_Gecko_DestroyBrowser( *instance );
3104 ========================
3107 float[bool] gecko_create( string name )
3108 ========================
3110 void VM_gecko_create( void ) {
3113 clgecko_t *instance;
3115 VM_SAFEPARMCOUNT( 1, VM_gecko_create );
3117 name = PRVM_G_STRING( OFS_PARM0 );
3118 VM_CheckEmptyString( name );
3120 // find an empty slot for this gecko browser..
3121 for( i = 0 ; i < PRVM_MAX_GECKOINSTANCES ; i++ ) {
3122 if( prog->opengeckoinstances[ i ] == NULL ) {
3126 if( i == PRVM_MAX_GECKOINSTANCES ) {
3127 VM_Warning("VM_gecko_create: %s ran out of gecko handles (%i)\n", PRVM_NAME, PRVM_MAX_GECKOINSTANCES);
3128 PRVM_G_FLOAT( OFS_RETURN ) = 0;
3132 instance = prog->opengeckoinstances[ i ] = CL_Gecko_CreateBrowser( name );
3134 // TODO: error handling [12/3/2007 Black]
3135 PRVM_G_FLOAT( OFS_RETURN ) = 0;
3138 PRVM_G_FLOAT( OFS_RETURN ) = 1;
3142 ========================
3145 void gecko_destroy( string name )
3146 ========================
3148 void VM_gecko_destroy( void ) {
3150 clgecko_t *instance;
3152 VM_SAFEPARMCOUNT( 1, VM_gecko_destroy );
3154 name = PRVM_G_STRING( OFS_PARM0 );
3155 VM_CheckEmptyString( name );
3156 instance = CL_Gecko_FindBrowser( name );
3160 CL_Gecko_DestroyBrowser( instance );
3164 ========================
3167 void gecko_navigate( string name, string URI )
3168 ========================
3170 void VM_gecko_navigate( void ) {
3173 clgecko_t *instance;
3175 VM_SAFEPARMCOUNT( 2, VM_gecko_navigate );
3177 name = PRVM_G_STRING( OFS_PARM0 );
3178 URI = PRVM_G_STRING( OFS_PARM1 );
3179 VM_CheckEmptyString( name );
3180 VM_CheckEmptyString( URI );
3182 instance = CL_Gecko_FindBrowser( name );
3186 CL_Gecko_NavigateToURI( instance, URI );
3190 ========================
3193 float[bool] gecko_keyevent( string name, float key, float eventtype )
3194 ========================
3196 void VM_gecko_keyevent( void ) {
3199 clgecko_buttoneventtype_t eventtype;
3200 clgecko_t *instance;
3202 VM_SAFEPARMCOUNT( 3, VM_gecko_keyevent );
3204 name = PRVM_G_STRING( OFS_PARM0 );
3205 VM_CheckEmptyString( name );
3206 key = (unsigned int) PRVM_G_FLOAT( OFS_PARM1 );
3207 switch( (unsigned int) PRVM_G_FLOAT( OFS_PARM2 ) ) {
3209 eventtype = CLG_BET_DOWN;
3212 eventtype = CLG_BET_UP;
3215 eventtype = CLG_BET_PRESS;
3218 eventtype = CLG_BET_DOUBLECLICK;
3221 // TODO: console printf? [12/3/2007 Black]
3222 PRVM_G_FLOAT( OFS_RETURN ) = 0;
3226 instance = CL_Gecko_FindBrowser( name );
3228 PRVM_G_FLOAT( OFS_RETURN ) = 0;
3232 PRVM_G_FLOAT( OFS_RETURN ) = (CL_Gecko_Event_Key( instance, key, eventtype ) == true);
3236 ========================
3239 void gecko_mousemove( string name, float x, float y )
3240 ========================
3242 void VM_gecko_movemouse( void ) {
3245 clgecko_t *instance;
3247 VM_SAFEPARMCOUNT( 3, VM_gecko_movemouse );
3249 name = PRVM_G_STRING( OFS_PARM0 );
3250 VM_CheckEmptyString( name );
3251 x = PRVM_G_FLOAT( OFS_PARM1 );
3252 y = PRVM_G_FLOAT( OFS_PARM2 );
3254 instance = CL_Gecko_FindBrowser( name );
3258 CL_Gecko_Event_CursorMove( instance, x, y );
3263 ========================
3266 void gecko_resize( string name, float w, float h )
3267 ========================
3269 void VM_gecko_resize( void ) {
3272 clgecko_t *instance;
3274 VM_SAFEPARMCOUNT( 3, VM_gecko_movemouse );
3276 name = PRVM_G_STRING( OFS_PARM0 );
3277 VM_CheckEmptyString( name );
3278 w = PRVM_G_FLOAT( OFS_PARM1 );
3279 h = PRVM_G_FLOAT( OFS_PARM2 );
3281 instance = CL_Gecko_FindBrowser( name );
3285 CL_Gecko_Resize( instance, w, h );
3290 ========================
3291 VM_gecko_get_texture_extent
3293 vector gecko_get_texture_extent( string name )
3294 ========================
3296 void VM_gecko_get_texture_extent( void ) {
3298 clgecko_t *instance;
3300 VM_SAFEPARMCOUNT( 1, VM_gecko_movemouse );
3302 name = PRVM_G_STRING( OFS_PARM0 );
3303 VM_CheckEmptyString( name );
3305 PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
3306 instance = CL_Gecko_FindBrowser( name );
3308 PRVM_G_VECTOR(OFS_RETURN)[0] = 0;
3309 PRVM_G_VECTOR(OFS_RETURN)[1] = 0;
3312 CL_Gecko_GetTextureExtent( instance,
3313 PRVM_G_VECTOR(OFS_RETURN), PRVM_G_VECTOR(OFS_RETURN)+1 );
3323 Writes new values for v_forward, v_up, and v_right based on angles
3324 void makevectors(vector angle)
3327 void VM_makevectors (void)
3329 prvm_eval_t *valforward, *valright, *valup;
3330 valforward = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_forward);
3331 valright = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_right);
3332 valup = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_up);
3333 if (!valforward || !valright || !valup)
3335 VM_Warning("makevectors: could not find v_forward, v_right, or v_up global variables\n");
3338 VM_SAFEPARMCOUNT(1, VM_makevectors);
3339 AngleVectors (PRVM_G_VECTOR(OFS_PARM0), valforward->vector, valright->vector, valup->vector);
3346 Writes new values for v_forward, v_up, and v_right based on the given forward vector
3347 vectorvectors(vector)
3350 void VM_vectorvectors (void)
3352 prvm_eval_t *valforward, *valright, *valup;
3353 valforward = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_forward);
3354 valright = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_right);
3355 valup = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_up);
3356 if (!valforward || !valright || !valup)
3358 VM_Warning("vectorvectors: could not find v_forward, v_right, or v_up global variables\n");
3361 VM_SAFEPARMCOUNT(1, VM_vectorvectors);
3362 VectorNormalize2(PRVM_G_VECTOR(OFS_PARM0), valforward->vector);
3363 VectorVectors(valforward->vector, valright->vector, valup->vector);
3367 ========================
3370 void drawline(float width, vector pos1, vector pos2, vector rgb, float alpha, float flags)
3371 ========================
3373 void VM_drawline (void)
3375 float *c1, *c2, *rgb;
3377 unsigned char flags;
3379 VM_SAFEPARMCOUNT(6, VM_drawline);
3380 width = PRVM_G_FLOAT(OFS_PARM0);
3381 c1 = PRVM_G_VECTOR(OFS_PARM1);
3382 c2 = PRVM_G_VECTOR(OFS_PARM2);
3383 rgb = PRVM_G_VECTOR(OFS_PARM3);
3384 alpha = PRVM_G_FLOAT(OFS_PARM4);
3385 flags = (int)PRVM_G_FLOAT(OFS_PARM5);
3386 DrawQ_Line(width, c1[0], c1[1], c2[0], c2[1], rgb[0], rgb[1], rgb[2], alpha, flags);
3389 // float(float number, float quantity) bitshift (EXT_BITSHIFT)
3390 void VM_bitshift (void)
3393 VM_SAFEPARMCOUNT(2, VM_bitshift);
3395 n1 = (int)fabs((int)PRVM_G_FLOAT(OFS_PARM0));
3396 n2 = (int)PRVM_G_FLOAT(OFS_PARM1);
3398 PRVM_G_FLOAT(OFS_RETURN) = n1;
3401 PRVM_G_FLOAT(OFS_RETURN) = (n1 >> -n2);
3403 PRVM_G_FLOAT(OFS_RETURN) = (n1 << n2);
3406 ////////////////////////////////////////
3407 // AltString functions
3408 ////////////////////////////////////////
3411 ========================
3414 float altstr_count(string)
3415 ========================
3417 void VM_altstr_count( void )
3419 const char *altstr, *pos;
3422 VM_SAFEPARMCOUNT( 1, VM_altstr_count );
3424 altstr = PRVM_G_STRING( OFS_PARM0 );
3425 //VM_CheckEmptyString( altstr );
3427 for( count = 0, pos = altstr ; *pos ; pos++ ) {
3428 if( *pos == '\\' ) {
3432 } else if( *pos == '\'' ) {
3437 PRVM_G_FLOAT( OFS_RETURN ) = (float) (count / 2);
3441 ========================
3444 string altstr_prepare(string)
3445 ========================
3447 void VM_altstr_prepare( void )
3450 const char *instr, *in;
3452 char outstr[VM_STRINGTEMP_LENGTH];
3454 VM_SAFEPARMCOUNT( 1, VM_altstr_prepare );
3456 instr = PRVM_G_STRING( OFS_PARM0 );
3458 for( out = outstr, in = instr, size = sizeof(outstr) - 1 ; size && *in ; size--, in++, out++ )
3467 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
3471 ========================
3474 string altstr_get(string, float)
3475 ========================
3477 void VM_altstr_get( void )
3479 const char *altstr, *pos;
3482 char outstr[VM_STRINGTEMP_LENGTH];
3484 VM_SAFEPARMCOUNT( 2, VM_altstr_get );
3486 altstr = PRVM_G_STRING( OFS_PARM0 );
3488 count = (int)PRVM_G_FLOAT( OFS_PARM1 );
3489 count = count * 2 + 1;
3491 for( pos = altstr ; *pos && count ; pos++ )
3492 if( *pos == '\\' ) {
3495 } else if( *pos == '\'' )
3499 PRVM_G_INT( OFS_RETURN ) = 0;
3503 for( out = outstr, size = sizeof(outstr) - 1 ; size && *pos ; size--, pos++, out++ )
3504 if( *pos == '\\' ) {
3509 } else if( *pos == '\'' )
3515 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
3519 ========================
3522 string altstr_set(string altstr, float num, string set)
3523 ========================
3525 void VM_altstr_set( void )
3528 const char *altstr, *str;
3531 char outstr[VM_STRINGTEMP_LENGTH];
3533 VM_SAFEPARMCOUNT( 3, VM_altstr_set );
3535 altstr = PRVM_G_STRING( OFS_PARM0 );
3537 num = (int)PRVM_G_FLOAT( OFS_PARM1 );
3539 str = PRVM_G_STRING( OFS_PARM2 );
3542 for( num = num * 2 + 1, in = altstr; *in && num; *out++ = *in++ )
3547 } else if( *in == '\'' ) {
3552 for( ; *str; *out++ = *str++ );
3553 // now jump over the old content
3555 if( *in == '\'' || (*in == '\\' && !*++in) )
3558 strlcpy(out, in, outstr + sizeof(outstr) - out);
3559 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
3563 ========================
3566 string altstr_ins(string altstr, float num, string set)
3567 ========================
3569 void VM_altstr_ins(void)
3577 char outstr[VM_STRINGTEMP_LENGTH];
3579 VM_SAFEPARMCOUNT(3, VM_altstr_ins);
3581 in = instr = PRVM_G_STRING( OFS_PARM0 );
3582 num = (int)PRVM_G_FLOAT( OFS_PARM1 );
3583 set = setstr = PRVM_G_STRING( OFS_PARM2 );
3586 for( num = num * 2 + 2 ; *in && num > 0 ; *out++ = *in++ )
3591 } else if( *in == '\'' ) {
3596 for( ; *set ; *out++ = *set++ );
3599 strlcpy(out, in, outstr + sizeof(outstr) - out);
3600 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
3604 ////////////////////////////////////////
3605 // BufString functions
3606 ////////////////////////////////////////
3607 //[515]: string buffers support
3608 #define MAX_QCSTR_BUFFERS 128
3609 #define MAX_QCSTR_STRINGS 1024
3614 char *strings[MAX_QCSTR_STRINGS];
3617 // FIXME: move stringbuffers to prog_t to allow multiple progs!
3618 static qcstrbuffer_t *qcstringbuffers[MAX_QCSTR_BUFFERS];
3619 static int num_qcstringbuffers;
3620 static int buf_sortpower;
3622 #define BUFSTR_BUFFER(a) (a>=MAX_QCSTR_BUFFERS) ? NULL : (qcstringbuffers[a])
3623 #define BUFSTR_ISFREE(a) (a<MAX_QCSTR_BUFFERS&&qcstringbuffers[a]&&qcstringbuffers[a]->num_strings<=0) ? 1 : 0
3625 static int BufStr_FindFreeBuffer (void)
3628 if(num_qcstringbuffers == MAX_QCSTR_BUFFERS)
3630 for(i=0;i<MAX_QCSTR_BUFFERS;i++)
3631 if(!qcstringbuffers[i])
3633 qcstringbuffers[i] = (qcstrbuffer_t *)Z_Malloc(sizeof(qcstrbuffer_t));
3634 memset(qcstringbuffers[i], 0, sizeof(qcstrbuffer_t));
3640 static void BufStr_ClearBuffer (int index)
3642 qcstrbuffer_t *b = qcstringbuffers[index];
3647 if(b->num_strings > 0)
3649 for(i=0;i<b->num_strings;i++)
3651 Z_Free(b->strings[i]);
3652 num_qcstringbuffers--;
3654 Z_Free(qcstringbuffers[index]);
3655 qcstringbuffers[index] = NULL;
3659 static int BufStr_FindFreeString (qcstrbuffer_t *b)
3662 for(i=0;i<b->num_strings;i++)
3663 if(!b->strings[i] || !b->strings[i][0])
3665 if(i == MAX_QCSTR_STRINGS) return -1;
3669 static int BufStr_SortStringsUP (const void *in1, const void *in2)
3672 a = *((const char **) in1);
3673 b = *((const char **) in2);
3675 if(!b[0]) return -1;
3676 return strncmp(a, b, buf_sortpower);
3679 static int BufStr_SortStringsDOWN (const void *in1, const void *in2)
3682 a = *((const char **) in1);
3683 b = *((const char **) in2);
3685 if(!b[0]) return -1;
3686 return strncmp(b, a, buf_sortpower);
3690 ========================
3692 creates new buffer, and returns it's index, returns -1 if failed
3693 float buf_create(void) = #460;
3694 ========================
3696 void VM_buf_create (void)
3699 VM_SAFEPARMCOUNT(0, VM_buf_create);
3700 i = BufStr_FindFreeBuffer();
3702 num_qcstringbuffers++;
3704 //Con_Printf("VM_buf_create: buffers overflow in %s\n", PRVM_NAME);
3705 PRVM_G_FLOAT(OFS_RETURN) = i;
3709 ========================
3711 deletes buffer and all strings in it
3712 void buf_del(float bufhandle) = #461;
3713 ========================
3715 void VM_buf_del (void)
3717 VM_SAFEPARMCOUNT(1, VM_buf_del);
3718 if(BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0)))
3719 BufStr_ClearBuffer((int)PRVM_G_FLOAT(OFS_PARM0));
3722 VM_Warning("VM_buf_del: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3728 ========================
3730 how many strings are stored in buffer
3731 float buf_getsize(float bufhandle) = #462;
3732 ========================
3734 void VM_buf_getsize (void)
3737 VM_SAFEPARMCOUNT(1, VM_buf_getsize);
3739 b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3742 PRVM_G_FLOAT(OFS_RETURN) = -1;
3743 VM_Warning("VM_buf_getsize: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3747 PRVM_G_FLOAT(OFS_RETURN) = b->num_strings;
3751 ========================
3753 copy all content from one buffer to another, make sure it exists
3754 void buf_copy(float bufhandle_from, float bufhandle_to) = #463;
3755 ========================
3757 void VM_buf_copy (void)
3759 qcstrbuffer_t *b1, *b2;
3761 VM_SAFEPARMCOUNT(2, VM_buf_copy);
3763 b1 = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3766 VM_Warning("VM_buf_copy: invalid source buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3769 i = (int)PRVM_G_FLOAT(OFS_PARM1);
3770 if(i == (int)PRVM_G_FLOAT(OFS_PARM0))
3772 VM_Warning("VM_buf_copy: source == destination (%i) in %s\n", i, PRVM_NAME);
3775 b2 = BUFSTR_BUFFER(i);
3778 VM_Warning("VM_buf_copy: invalid destination buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM1), PRVM_NAME);
3782 BufStr_ClearBuffer(i);
3783 qcstringbuffers[i] = (qcstrbuffer_t *)Z_Malloc(sizeof(qcstrbuffer_t));
3784 memset(qcstringbuffers[i], 0, sizeof(qcstrbuffer_t));
3785 b2->num_strings = b1->num_strings;
3787 for(i=0;i<b1->num_strings;i++)
3788 if(b1->strings[i] && b1->strings[i][0])
3791 stringlen = strlen(b1->strings[i]) + 1;
3792 b2->strings[i] = (char *)Z_Malloc(stringlen);
3795 VM_Warning("VM_buf_copy: not enough memory for buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM1), PRVM_NAME);
3798 memcpy(b2->strings[i], b1->strings[i], stringlen);
3803 ========================
3805 sort buffer by beginnings of strings (cmplength defaults it's length)
3806 "backward == TRUE" means that sorting goes upside-down
3807 void buf_sort(float bufhandle, float cmplength, float backward) = #464;
3808 ========================
3810 void VM_buf_sort (void)
3814 VM_SAFEPARMCOUNT(3, VM_buf_sort);
3816 b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3819 VM_Warning("VM_buf_sort: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3822 if(b->num_strings <= 0)
3824 VM_Warning("VM_buf_sort: tried to sort empty buffer %i in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3827 // TODO: please someone rename this to buf_cmplength [12/3/2007 Black]
3828 buf_sortpower = (int)PRVM_G_FLOAT(OFS_PARM1);
3829 if(buf_sortpower <= 0)
3830 buf_sortpower = 99999999;
3832 if(!PRVM_G_FLOAT(OFS_PARM2))
3833 qsort(b->strings, b->num_strings, sizeof(char*), BufStr_SortStringsUP);
3835 qsort(b->strings, b->num_strings, sizeof(char*), BufStr_SortStringsDOWN);
3837 for(i=b->num_strings-1;i>=0;i--) //[515]: delete empty lines
3840 if(b->strings[i][0])
3844 Z_Free(b->strings[i]);
3846 b->strings[i] = NULL;
3854 ========================
3856 concantenates all buffer string into one with "glue" separator and returns it as tempstring
3857 string buf_implode(float bufhandle, string glue) = #465;
3858 ========================
3860 void VM_buf_implode (void)
3863 char k[VM_STRINGTEMP_LENGTH];
3867 VM_SAFEPARMCOUNT(2, VM_buf_implode);
3869 b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3870 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
3873 VM_Warning("VM_buf_implode: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3878 sep = PRVM_G_STRING(OFS_PARM1);
3880 for(l=i=0;i<b->num_strings;i++)
3883 l += (i > 0 ? strlen(sep) : 0) + strlen(b->strings[i]);
3884 if (l >= sizeof(k) - 1)
3886 strlcat(k, sep, sizeof(k));
3887 strlcat(k, b->strings[i], sizeof(k));
3889 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(k);
3893 ========================
3895 get a string from buffer, returns tempstring, dont str_unzone it!
3896 string bufstr_get(float bufhandle, float string_index) = #465;
3897 ========================
3899 void VM_bufstr_get (void)
3903 VM_SAFEPARMCOUNT(2, VM_bufstr_get);
3905 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
3906 b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3909 VM_Warning("VM_bufstr_get: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3912 strindex = (int)PRVM_G_FLOAT(OFS_PARM1);
3913 if(strindex < 0 || strindex > MAX_QCSTR_STRINGS)
3915 VM_Warning("VM_bufstr_get: invalid string index %i used in %s\n", strindex, PRVM_NAME);
3918 if(b->num_strings <= strindex)
3920 if(b->strings[strindex])
3921 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(b->strings[strindex]);
3925 ========================
3927 copies a string into selected slot of buffer
3928 void bufstr_set(float bufhandle, float string_index, string str) = #466;
3929 ========================
3931 void VM_bufstr_set (void)
3933 int bufindex, strindex;
3938 VM_SAFEPARMCOUNT(3, VM_bufstr_set);
3940 bufindex = (int)PRVM_G_FLOAT(OFS_PARM0);
3941 b = BUFSTR_BUFFER(bufindex);
3944 VM_Warning("VM_bufstr_set: invalid buffer %i used in %s\n", bufindex, PRVM_NAME);
3947 strindex = (int)PRVM_G_FLOAT(OFS_PARM1);
3948 if(strindex < 0 || strindex > MAX_QCSTR_STRINGS)
3950 VM_Warning("VM_bufstr_set: invalid string index %i used in %s\n", strindex, PRVM_NAME);
3953 news = PRVM_G_STRING(OFS_PARM2);
3954 if(b->strings[strindex])
3955 Z_Free(b->strings[strindex]);
3956 alloclen = strlen(news) + 1;
3957 b->strings[strindex] = (char *)Z_Malloc(alloclen);
3958 memcpy(b->strings[strindex], news, alloclen);
3962 ========================
3964 adds string to buffer in nearest free slot and returns it
3965 "order == TRUE" means that string will be added after last "full" slot
3966 float bufstr_add(float bufhandle, string str, float order) = #467;
3967 ========================
3969 void VM_bufstr_add (void)
3971 int bufindex, order, strindex;
3976 VM_SAFEPARMCOUNT(3, VM_bufstr_add);
3978 bufindex = (int)PRVM_G_FLOAT(OFS_PARM0);
3979 b = BUFSTR_BUFFER(bufindex);
3980 PRVM_G_FLOAT(OFS_RETURN) = -1;
3983 VM_Warning("VM_bufstr_add: invalid buffer %i used in %s\n", bufindex, PRVM_NAME);
3986 string = PRVM_G_STRING(OFS_PARM1);
3987 order = (int)PRVM_G_FLOAT(OFS_PARM2);
3989 strindex = b->num_strings;
3992 strindex = BufStr_FindFreeString(b);
3995 VM_Warning("VM_bufstr_add: buffer %i has no free string slots in %s\n", bufindex, PRVM_NAME);
4000 while(b->num_strings <= strindex)
4002 if(b->num_strings == MAX_QCSTR_STRINGS)
4004 VM_Warning("VM_bufstr_add: buffer %i has no free string slots in %s\n", bufindex, PRVM_NAME);
4007 b->strings[b->num_strings] = NULL;
4010 if(b->strings[strindex])
4011 Z_Free(b->strings[strindex]);
4012 alloclen = strlen(string) + 1;
4013 b->strings[strindex] = (char *)Z_Malloc(alloclen);
4014 memcpy(b->strings[strindex], string, alloclen);
4015 PRVM_G_FLOAT(OFS_RETURN) = strindex;
4019 ========================
4021 delete string from buffer
4022 void bufstr_free(float bufhandle, float string_index) = #468;
4023 ========================
4025 void VM_bufstr_free (void)
4029 VM_SAFEPARMCOUNT(2, VM_bufstr_free);
4031 b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
4034 VM_Warning("VM_bufstr_free: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4037 i = (int)PRVM_G_FLOAT(OFS_PARM1);
4038 if(i < 0 || i > MAX_QCSTR_STRINGS)
4040 VM_Warning("VM_bufstr_free: invalid string index %i used in %s\n", i, PRVM_NAME);
4044 Z_Free(b->strings[i]);
4045 b->strings[i] = NULL;
4046 if(i+1 == b->num_strings)
4056 This was a major timewaster in progs, so it was converted to C
4059 void VM_changeyaw (void)
4062 float ideal, current, move, speed;
4064 // this is called (VERY HACKISHLY) by SV_MoveToGoal, so it can not use any
4065 // parameters because they are the parameters to SV_MoveToGoal, not this
4066 //VM_SAFEPARMCOUNT(0, VM_changeyaw);
4068 ent = PRVM_PROG_TO_EDICT(PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict);
4069 if (ent == prog->edicts)
4071 VM_Warning("changeyaw: can not modify world entity\n");
4074 if (ent->priv.server->free)
4076 VM_Warning("changeyaw: can not modify free entity\n");
4079 if (prog->fieldoffsets.angles < 0 || prog->fieldoffsets.ideal_yaw < 0 || prog->fieldoffsets.yaw_speed < 0)
4081 VM_Warning("changeyaw: angles, ideal_yaw, or yaw_speed field(s) not found\n");
4084 current = ANGLEMOD(PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.angles)->vector[1]);
4085 ideal = PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.ideal_yaw)->_float;
4086 speed = PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.yaw_speed)->_float;
4088 if (current == ideal)
4090 move = ideal - current;
4091 if (ideal > current)
4112 PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.angles)->vector[1] = ANGLEMOD (current + move);
4120 void VM_changepitch (void)
4123 float ideal, current, move, speed;
4125 VM_SAFEPARMCOUNT(1, VM_changepitch);
4127 ent = PRVM_G_EDICT(OFS_PARM0);
4128 if (ent == prog->edicts)
4130 VM_Warning("changepitch: can not modify world entity\n");
4133 if (ent->priv.server->free)
4135 VM_Warning("changepitch: can not modify free entity\n");
4138 if (prog->fieldoffsets.angles < 0 || prog->fieldoffsets.idealpitch < 0 || prog->fieldoffsets.pitch_speed < 0)
4140 VM_Warning("changepitch: angles, idealpitch, or pitch_speed field(s) not found\n");
4143 current = ANGLEMOD(PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.angles)->vector[0]);
4144 ideal = PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.idealpitch)->_float;
4145 speed = PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.pitch_speed)->_float;
4147 if (current == ideal)
4149 move = ideal - current;
4150 if (ideal > current)
4171 PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.angles)->vector[0] = ANGLEMOD (current + move);
4174 // TODO: adapt all static function names to use a single naming convention... [12/3/2007 Black]
4175 static int Is_Text_Color (char c, char t)
4178 char c2 = c - (c & 128);
4179 char t2 = t - (t & 128);
4181 if(c != STRING_COLOR_TAG && c2 != STRING_COLOR_TAG) return 0;
4182 if(t >= '0' && t <= '9') a = 1;
4183 if(t2 >= '0' && t2 <= '9') a = 1;
4184 /* if(t >= 'A' && t <= 'Z') a = 2;
4185 if(t2 >= 'A' && t2 <= 'Z') a = 2;
4187 if(a == 1 && scr_colortext.integer > 0)
4189 if(a == 2 && scr_multifonts.integer > 0)
4195 void VM_uncolorstring (void)
4198 char out[VM_STRINGTEMP_LENGTH];
4201 VM_SAFEPARMCOUNT(1, VM_uncolorstring);
4202 in = PRVM_G_STRING(OFS_PARM0);
4203 VM_CheckEmptyString (in);
4208 if(Is_Text_Color(in[k], in[k+1]) == 1/* || (in[k] == '&' && in[k+1] == 'r')*/)
4217 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(out);
4220 // #221 float(string str, string sub[, float startpos]) strstrofs (FTE_STRINGS)
4221 //strstr, without generating a new string. Use in conjunction with FRIK_FILE's substring for more similar strstr.
4222 void VM_strstrofs (void)
4224 const char *instr, *match;
4226 VM_SAFEPARMCOUNTRANGE(2, 3, VM_strstrofs);
4227 instr = PRVM_G_STRING(OFS_PARM0);
4228 match = PRVM_G_STRING(OFS_PARM1);
4229 firstofs = (prog->argc > 2)?PRVM_G_FLOAT(OFS_PARM2):0;
4231 if (firstofs && (firstofs < 0 || firstofs > (int)strlen(instr)))
4233 PRVM_G_FLOAT(OFS_RETURN) = -1;
4237 match = strstr(instr+firstofs, match);
4239 PRVM_G_FLOAT(OFS_RETURN) = -1;
4241 PRVM_G_FLOAT(OFS_RETURN) = match - instr;
4244 //#222 string(string s, float index) str2chr (FTE_STRINGS)
4245 void VM_str2chr (void)
4248 VM_SAFEPARMCOUNT(2, VM_str2chr);
4249 s = PRVM_G_STRING(OFS_PARM0);
4250 if((unsigned)PRVM_G_FLOAT(OFS_PARM1) < strlen(s))
4251 PRVM_G_FLOAT(OFS_RETURN) = (unsigned char)s[(unsigned)PRVM_G_FLOAT(OFS_PARM1)];
4253 PRVM_G_FLOAT(OFS_RETURN) = 0;
4256 //#223 string(float c, ...) chr2str (FTE_STRINGS)
4257 void VM_chr2str (void)
4261 VM_SAFEPARMCOUNTRANGE(0, 8, VM_chr2str);
4262 for(i = 0;i < prog->argc && i < (int)sizeof(t) - 1;i++)
4263 t[i] = (unsigned char)PRVM_G_FLOAT(OFS_PARM0+i*3);
4265 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(t);
4268 static int chrconv_number(int i, int base, int conv)
4293 static int chrconv_punct(int i, int base, int conv)
4311 static int chrchar_alpha(int i, int basec, int baset, int convc, int convt, int charnum)
4313 //convert case and colour seperatly...
4330 baset = 128*((charnum&1) == (convt-5));
4346 return i + basec + baset;
4348 // #224 string(float ccase, float calpha, float cnum, string s, ...) strconv (FTE_STRINGS)
4349 //bulk convert a string. change case or colouring.
4350 void VM_strconv (void)
4352 int ccase, redalpha, rednum, len, i;
4353 unsigned char resbuf[VM_STRINGTEMP_LENGTH];
4354 unsigned char *result = resbuf;
4356 VM_SAFEPARMCOUNTRANGE(3, 8, VM_strconv);
4358 ccase = PRVM_G_FLOAT(OFS_PARM0); //0 same, 1 lower, 2 upper
4359 redalpha = PRVM_G_FLOAT(OFS_PARM1); //0 same, 1 white, 2 red, 5 alternate, 6 alternate-alternate
4360 rednum = PRVM_G_FLOAT(OFS_PARM2); //0 same, 1 white, 2 red, 3 redspecial, 4 whitespecial, 5 alternate, 6 alternate-alternate
4361 VM_VarString(3, (char *) resbuf, sizeof(resbuf));
4362 len = strlen((char *) resbuf);
4364 for (i = 0; i < len; i++, result++) //should this be done backwards?
4366 if (*result >= '0' && *result <= '9') //normal numbers...
4367 *result = chrconv_number(*result, '0', rednum);
4368 else if (*result >= '0'+128 && *result <= '9'+128)
4369 *result = chrconv_number(*result, '0'+128, rednum);
4370 else if (*result >= '0'+128-30 && *result <= '9'+128-30)
4371 *result = chrconv_number(*result, '0'+128-30, rednum);
4372 else if (*result >= '0'-30 && *result <= '9'-30)
4373 *result = chrconv_number(*result, '0'-30, rednum);
4375 else if (*result >= 'a' && *result <= 'z') //normal numbers...
4376 *result = chrchar_alpha(*result, 'a', 0, ccase, redalpha, i);
4377 else if (*result >= 'A' && *result <= 'Z') //normal numbers...
4378 *result = chrchar_alpha(*result, 'A', 0, ccase, redalpha, i);
4379 else if (*result >= 'a'+128 && *result <= 'z'+128) //normal numbers...
4380 *result = chrchar_alpha(*result, 'a', 128, ccase, redalpha, i);
4381 else if (*result >= 'A'+128 && *result <= 'Z'+128) //normal numbers...
4382 *result = chrchar_alpha(*result, 'A', 128, ccase, redalpha, i);
4384 else if ((*result & 127) < 16 || !redalpha) //special chars..
4386 else if (*result < 128)
4387 *result = chrconv_punct(*result, 0, redalpha);
4389 *result = chrconv_punct(*result, 128, redalpha);
4393 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString((char *) resbuf);
4396 // #225 string(float chars, string s, ...) strpad (FTE_STRINGS)
4397 void VM_strpad (void)
4399 char src[VM_STRINGTEMP_LENGTH];
4400 char destbuf[VM_STRINGTEMP_LENGTH];
4402 VM_SAFEPARMCOUNTRANGE(1, 8, VM_strpad);
4403 pad = PRVM_G_FLOAT(OFS_PARM0);
4404 VM_VarString(1, src, sizeof(src));
4406 // note: < 0 = left padding, > 0 = right padding,
4407 // this is reverse logic of printf!
4408 dpsnprintf(destbuf, sizeof(destbuf), "%*s", -pad, src);
4410 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(destbuf);
4413 // #226 string(string info, string key, string value, ...) infoadd (FTE_STRINGS)
4414 //uses qw style \key\value strings
4415 void VM_infoadd (void)
4417 const char *info, *key;
4418 char value[VM_STRINGTEMP_LENGTH];
4419 char temp[VM_STRINGTEMP_LENGTH];
4421 VM_SAFEPARMCOUNTRANGE(2, 8, VM_infoadd);
4422 info = PRVM_G_STRING(OFS_PARM0);
4423 key = PRVM_G_STRING(OFS_PARM1);
4424 VM_VarString(2, value, sizeof(value));
4426 strlcpy(temp, info, VM_STRINGTEMP_LENGTH);
4428 InfoString_SetValue(temp, VM_STRINGTEMP_LENGTH, key, value);
4430 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(temp);
4433 // #227 string(string info, string key) infoget (FTE_STRINGS)
4434 //uses qw style \key\value strings
4435 void VM_infoget (void)
4439 char value[VM_STRINGTEMP_LENGTH];
4441 VM_SAFEPARMCOUNT(2, VM_infoget);
4442 info = PRVM_G_STRING(OFS_PARM0);
4443 key = PRVM_G_STRING(OFS_PARM1);
4445 InfoString_GetValue(info, key, value, VM_STRINGTEMP_LENGTH);
4447 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(value);
4450 //#228 float(string s1, string s2, float len) strncmp (FTE_STRINGS)
4451 // also float(string s1, string s2) strcmp (FRIK_FILE)
4452 void VM_strncmp (void)
4454 const char *s1, *s2;
4455 VM_SAFEPARMCOUNTRANGE(2, 3, VM_strncmp);
4456 s1 = PRVM_G_STRING(OFS_PARM0);
4457 s2 = PRVM_G_STRING(OFS_PARM1);
4460 PRVM_G_FLOAT(OFS_RETURN) = strncmp(s1, s2, (size_t)PRVM_G_FLOAT(OFS_PARM2));
4464 PRVM_G_FLOAT(OFS_RETURN) = strcmp(s1, s2);
4468 // #229 float(string s1, string s2) strcasecmp (FTE_STRINGS)
4469 // #230 float(string s1, string s2, float len) strncasecmp (FTE_STRINGS)
4470 void VM_strncasecmp (void)
4472 const char *s1, *s2;
4473 VM_SAFEPARMCOUNTRANGE(2, 3, VM_strncasecmp);
4474 s1 = PRVM_G_STRING(OFS_PARM0);
4475 s2 = PRVM_G_STRING(OFS_PARM1);
4478 PRVM_G_FLOAT(OFS_RETURN) = strncasecmp(s1, s2, (size_t)PRVM_G_FLOAT(OFS_PARM2));
4482 PRVM_G_FLOAT(OFS_RETURN) = strcasecmp(s1, s2);
4486 void VM_wasfreed (void)
4488 VM_SAFEPARMCOUNT(1, VM_wasfreed);
4489 PRVM_G_FLOAT(OFS_RETURN) = PRVM_G_EDICT(OFS_PARM0)->priv.required->free;
4492 void VM_SetTraceGlobals(const trace_t *trace)
4495 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_allsolid)))
4496 val->_float = trace->allsolid;
4497 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_startsolid)))
4498 val->_float = trace->startsolid;
4499 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_fraction)))
4500 val->_float = trace->fraction;
4501 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_inwater)))
4502 val->_float = trace->inwater;
4503 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_inopen)))
4504 val->_float = trace->inopen;
4505 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_endpos)))
4506 VectorCopy(trace->endpos, val->vector);
4507 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_plane_normal)))
4508 VectorCopy(trace->plane.normal, val->vector);
4509 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_plane_dist)))
4510 val->_float = trace->plane.dist;
4511 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_ent)))
4512 val->edict = PRVM_EDICT_TO_PROG(trace->ent ? trace->ent : prog->edicts);
4513 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dpstartcontents)))
4514 val->_float = trace->startsupercontents;
4515 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dphitcontents)))
4516 val->_float = trace->hitsupercontents;
4517 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dphitq3surfaceflags)))
4518 val->_float = trace->hitq3surfaceflags;
4519 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dphittexturename)))
4520 val->string = trace->hittexture ? PRVM_SetTempString(trace->hittexture->name) : 0;
4525 void VM_Cmd_Init(void)
4527 // only init the stuff for the current prog
4530 #ifdef SUPPORT_GECKO
4533 // VM_BufStr_Init();
4536 void VM_Cmd_Reset(void)
4538 CL_PurgeOwner( MENUOWNER );
4540 VM_Files_CloseAll();
4541 #ifdef SUPPORT_GECKO
4544 // VM_BufStr_ShutDown();