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 // LordHavoc: changed this to NOT use a return statement, so that it can be used in functions that must return a value
13 void VM_Warning(const char *fmt, ...)
16 char msg[MAX_INPUTLINE];
19 dpvsnprintf(msg,sizeof(msg),fmt,argptr);
23 // 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 //============================================================================
31 // TODO DONE: move vm_files and vm_fssearchlist to prvm_prog_t struct
32 // TODO: move vm_files and vm_fssearchlist back [9/13/2006 Black]
33 // TODO: (move vm_files and vm_fssearchlist to prvm_prog_t struct again) [2007-01-23 LordHavoc]
34 // TODO: will this war ever end? [2007-01-23 LordHavoc]
36 void VM_CheckEmptyString (const char *s)
39 PRVM_ERROR ("%s: Bad string", PRVM_NAME);
42 //============================================================================
45 void VM_VarString(int first, char *out, int outlength)
51 outend = out + outlength - 1;
52 for (i = first;i < prog->argc && out < outend;i++)
54 s = PRVM_G_STRING((OFS_PARM0+i*3));
55 while (out < outend && *s)
65 returns true if the extension is supported by the server
67 checkextension(extensionname)
71 // kind of helper function
72 static qboolean checkextension(const char *name)
76 len = (int)strlen(name);
78 for (e = prog->extensionstring;*e;e++)
85 while (*e && *e != ' ')
87 if ((e - start) == len && !strncasecmp(start, name, len))
93 void VM_checkextension (void)
95 VM_SAFEPARMCOUNT(1,VM_checkextension);
97 PRVM_G_FLOAT(OFS_RETURN) = checkextension(PRVM_G_STRING(OFS_PARM0));
104 This is a TERMINAL error, which will kill off the entire prog.
113 char string[VM_STRINGTEMP_LENGTH];
115 VM_VarString(0, string, sizeof(string));
116 Con_Printf("======%s ERROR in %s:\n%s\n", PRVM_NAME, PRVM_GetString(prog->xfunction->s_name), string);
117 if (prog->globaloffsets.self >= 0)
119 ed = PRVM_PROG_TO_EDICT(PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict);
120 PRVM_ED_Print(ed, NULL);
123 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);
130 Dumps out self, then an error message. The program is aborted and self is
131 removed, but the level can continue.
136 void VM_objerror (void)
139 char string[VM_STRINGTEMP_LENGTH];
141 VM_VarString(0, string, sizeof(string));
142 Con_Printf("======OBJECT ERROR======\n"); // , PRVM_NAME, PRVM_GetString(prog->xfunction->s_name), string); // or include them? FIXME
143 if (prog->globaloffsets.self >= 0)
145 ed = PRVM_PROG_TO_EDICT(PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict);
146 PRVM_ED_Print(ed, NULL);
151 // objerror has to display the object fields -> else call
152 PRVM_ERROR ("VM_objecterror: self not defined !");
153 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);
167 char string[VM_STRINGTEMP_LENGTH];
169 VM_VarString(0, string, sizeof(string));
177 broadcast print to everyone on server
182 void VM_bprint (void)
184 char string[VM_STRINGTEMP_LENGTH];
188 VM_Warning("VM_bprint: game is not server(%s) !\n", PRVM_NAME);
192 VM_VarString(0, string, sizeof(string));
193 SV_BroadcastPrint(string);
198 VM_sprint (menu & client but only if server.active == true)
200 single print to a specific client
202 sprint(float clientnum,...[string])
205 void VM_sprint (void)
209 char string[VM_STRINGTEMP_LENGTH];
211 VM_SAFEPARMCOUNTRANGE(1, 8, VM_sprint);
213 //find client for this entity
214 clientnum = (int)PRVM_G_FLOAT(OFS_PARM0);
215 if (!sv.active || clientnum < 0 || clientnum >= svs.maxclients || !svs.clients[clientnum].active)
217 VM_Warning("VM_sprint: %s: invalid client or server is not active !\n", PRVM_NAME);
221 client = svs.clients + clientnum;
222 if (!client->netconnection)
225 VM_VarString(1, string, sizeof(string));
226 MSG_WriteChar(&client->netconnection->message,svc_print);
227 MSG_WriteString(&client->netconnection->message, string);
234 single print to the screen
239 void VM_centerprint (void)
241 char string[VM_STRINGTEMP_LENGTH];
243 VM_SAFEPARMCOUNTRANGE(1, 8, VM_centerprint);
244 VM_VarString(0, string, sizeof(string));
245 SCR_CenterPrint(string);
252 vector normalize(vector)
255 void VM_normalize (void)
261 VM_SAFEPARMCOUNT(1,VM_normalize);
263 value1 = PRVM_G_VECTOR(OFS_PARM0);
265 f = VectorLength2(value1);
269 VectorScale(value1, f, newvalue);
272 VectorClear(newvalue);
274 VectorCopy (newvalue, PRVM_G_VECTOR(OFS_RETURN));
286 VM_SAFEPARMCOUNT(1,VM_vlen);
287 PRVM_G_FLOAT(OFS_RETURN) = VectorLength(PRVM_G_VECTOR(OFS_PARM0));
294 float vectoyaw(vector)
297 void VM_vectoyaw (void)
302 VM_SAFEPARMCOUNT(1,VM_vectoyaw);
304 value1 = PRVM_G_VECTOR(OFS_PARM0);
306 if (value1[1] == 0 && value1[0] == 0)
310 yaw = (int) (atan2(value1[1], value1[0]) * 180 / M_PI);
315 PRVM_G_FLOAT(OFS_RETURN) = yaw;
323 vector vectoangles(vector[, vector])
326 void VM_vectoangles (void)
328 VM_SAFEPARMCOUNTRANGE(1, 2,VM_vectoangles);
330 AnglesFromVectors(PRVM_G_VECTOR(OFS_RETURN), PRVM_G_VECTOR(OFS_PARM0), prog->argc >= 2 ? PRVM_G_VECTOR(OFS_PARM1) : NULL, true);
337 Returns a number from 0<= num < 1
342 void VM_random (void)
344 VM_SAFEPARMCOUNT(0,VM_random);
346 PRVM_G_FLOAT(OFS_RETURN) = lhrandom(0, 1);
353 localsound(string sample)
356 void VM_localsound(void)
360 VM_SAFEPARMCOUNT(1,VM_localsound);
362 s = PRVM_G_STRING(OFS_PARM0);
364 if(!S_LocalSound (s))
366 PRVM_G_FLOAT(OFS_RETURN) = -4;
367 VM_Warning("VM_localsound: Failed to play %s for %s !\n", s, PRVM_NAME);
371 PRVM_G_FLOAT(OFS_RETURN) = 1;
383 PRVM_ERROR ("%s: break statement", PRVM_NAME);
386 //============================================================================
392 Sends text over to the client's execution buffer
394 [localcmd (string, ...) or]
398 void VM_localcmd (void)
400 char string[VM_STRINGTEMP_LENGTH];
401 VM_SAFEPARMCOUNTRANGE(1, 8, VM_localcmd);
402 VM_VarString(0, string, sizeof(string));
403 Cbuf_AddText(string);
415 char string[VM_STRINGTEMP_LENGTH];
416 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar);
417 VM_VarString(0, string, sizeof(string));
418 VM_CheckEmptyString(string);
419 PRVM_G_FLOAT(OFS_RETURN) = Cvar_VariableValue(string);
426 const string VM_cvar_string (string, ...)
429 void VM_cvar_string(void)
431 char string[VM_STRINGTEMP_LENGTH];
432 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar_string);
433 VM_VarString(0, string, sizeof(string));
434 VM_CheckEmptyString(string);
435 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(Cvar_VariableString(string));
440 ========================
443 const string VM_cvar_defstring (string, ...)
444 ========================
446 void VM_cvar_defstring (void)
448 char string[VM_STRINGTEMP_LENGTH];
449 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar_defstring);
450 VM_VarString(0, string, sizeof(string));
451 VM_CheckEmptyString(string);
452 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(Cvar_VariableDefString(string));
458 void cvar_set (string,string, ...)
461 void VM_cvar_set (void)
464 char string[VM_STRINGTEMP_LENGTH];
465 VM_SAFEPARMCOUNTRANGE(2,8,VM_cvar_set);
466 VM_VarString(1, string, sizeof(string));
467 name = PRVM_G_STRING(OFS_PARM0);
468 VM_CheckEmptyString(name);
469 Cvar_Set(name, string);
479 void VM_dprint (void)
481 char string[VM_STRINGTEMP_LENGTH];
482 VM_SAFEPARMCOUNTRANGE(1, 8, VM_dprint);
483 if (developer.integer)
485 VM_VarString(0, string, sizeof(string));
487 Con_Printf("%s", string);
489 Con_Printf("%s: %s", PRVM_NAME, string);
507 VM_SAFEPARMCOUNT(1, VM_ftos);
509 v = PRVM_G_FLOAT(OFS_PARM0);
511 if ((float)((int)v) == v)
512 sprintf(s, "%i", (int)v);
515 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(s);
530 VM_SAFEPARMCOUNT(1,VM_fabs);
532 v = PRVM_G_FLOAT(OFS_PARM0);
533 PRVM_G_FLOAT(OFS_RETURN) = fabs(v);
548 VM_SAFEPARMCOUNT(1,VM_vtos);
550 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]);
551 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(s);
566 VM_SAFEPARMCOUNT(1, VM_etos);
568 sprintf (s, "entity %i", PRVM_G_EDICTNUM(OFS_PARM0));
569 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(s);
576 float stof(...[string])
581 char string[VM_STRINGTEMP_LENGTH];
582 VM_SAFEPARMCOUNTRANGE(1, 8, VM_stof);
583 VM_VarString(0, string, sizeof(string));
584 PRVM_G_FLOAT(OFS_RETURN) = atof(string);
588 ========================
592 ========================
596 VM_SAFEPARMCOUNT(1, VM_itof);
597 PRVM_G_FLOAT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
601 ========================
604 entity ftoe(float num)
605 ========================
610 VM_SAFEPARMCOUNT(1, VM_ftoe);
612 ent = (int)PRVM_G_FLOAT(OFS_PARM0);
613 if (ent < 0 || ent >= MAX_EDICTS || PRVM_PROG_TO_EDICT(ent)->priv.required->free)
614 ent = 0; // return world instead of a free or invalid entity
616 PRVM_G_INT(OFS_RETURN) = ent;
623 string strftime(float uselocaltime, string[, string ...])
626 void VM_strftime(void)
630 char fmt[VM_STRINGTEMP_LENGTH];
631 char result[VM_STRINGTEMP_LENGTH];
632 VM_SAFEPARMCOUNTRANGE(2, 8, VM_strftime);
633 VM_VarString(1, fmt, sizeof(fmt));
635 if (PRVM_G_FLOAT(OFS_PARM0))
641 PRVM_G_FLOAT(OFS_RETURN) = 0;
644 strftime(result, sizeof(result), fmt, tm);
645 PRVM_G_FLOAT(OFS_RETURN) = PRVM_SetTempString(result);
659 VM_SAFEPARMCOUNT(0, VM_spawn);
660 prog->xfunction->builtinsprofile += 20;
661 ed = PRVM_ED_Alloc();
673 void VM_remove (void)
676 prog->xfunction->builtinsprofile += 20;
678 VM_SAFEPARMCOUNT(1, VM_remove);
680 ed = PRVM_G_EDICT(OFS_PARM0);
681 if( PRVM_NUM_FOR_EDICT(ed) <= prog->reserved_edicts )
683 if (developer.integer >= 1)
684 VM_Warning( "VM_remove: tried to remove the null entity or a reserved entity!\n" );
686 else if( ed->priv.required->free )
688 if (developer.integer >= 1)
689 VM_Warning( "VM_remove: tried to remove an already freed entity!\n" );
699 entity find(entity start, .string field, string match)
710 VM_SAFEPARMCOUNT(3,VM_find);
712 e = PRVM_G_EDICTNUM(OFS_PARM0);
713 f = PRVM_G_INT(OFS_PARM1);
714 s = PRVM_G_STRING(OFS_PARM2);
716 // LordHavoc: apparently BloodMage does a find(world, weaponmodel, "") and
717 // expects it to find all the monsters, so we must be careful to support
720 for (e++ ; e < prog->num_edicts ; e++)
722 prog->xfunction->builtinsprofile++;
723 ed = PRVM_EDICT_NUM(e);
724 if (ed->priv.required->free)
726 t = PRVM_E_STRING(ed,f);
736 VM_RETURN_EDICT(prog->edicts);
743 entity findfloat(entity start, .float field, float match)
744 entity findentity(entity start, .entity field, entity match)
747 // LordHavoc: added this for searching float, int, and entity reference fields
748 void VM_findfloat (void)
755 VM_SAFEPARMCOUNT(3,VM_findfloat);
757 e = PRVM_G_EDICTNUM(OFS_PARM0);
758 f = PRVM_G_INT(OFS_PARM1);
759 s = PRVM_G_FLOAT(OFS_PARM2);
761 for (e++ ; e < prog->num_edicts ; e++)
763 prog->xfunction->builtinsprofile++;
764 ed = PRVM_EDICT_NUM(e);
765 if (ed->priv.required->free)
767 if (PRVM_E_FLOAT(ed,f) == s)
774 VM_RETURN_EDICT(prog->edicts);
781 entity findchain(.string field, string match)
784 // chained search for strings in entity fields
785 // entity(.string field, string match) findchain = #402;
786 void VM_findchain (void)
791 prvm_edict_t *ent, *chain;
793 VM_SAFEPARMCOUNT(2,VM_findchain);
795 if (prog->fieldoffsets.chain < 0)
796 PRVM_ERROR("VM_findchain: %s doesnt have a chain field !", PRVM_NAME);
798 chain = prog->edicts;
800 f = PRVM_G_INT(OFS_PARM0);
801 s = PRVM_G_STRING(OFS_PARM1);
803 // LordHavoc: apparently BloodMage does a find(world, weaponmodel, "") and
804 // expects it to find all the monsters, so we must be careful to support
807 ent = PRVM_NEXT_EDICT(prog->edicts);
808 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
810 prog->xfunction->builtinsprofile++;
811 if (ent->priv.required->free)
813 t = PRVM_E_STRING(ent,f);
819 PRVM_EDICTFIELDVALUE(ent,prog->fieldoffsets.chain)->edict = PRVM_NUM_FOR_EDICT(chain);
823 VM_RETURN_EDICT(chain);
830 entity findchainfloat(.string field, float match)
831 entity findchainentity(.string field, entity match)
834 // LordHavoc: chained search for float, int, and entity reference fields
835 // entity(.string field, float match) findchainfloat = #403;
836 void VM_findchainfloat (void)
841 prvm_edict_t *ent, *chain;
843 VM_SAFEPARMCOUNT(2, VM_findchainfloat);
845 if (prog->fieldoffsets.chain < 0)
846 PRVM_ERROR("VM_findchainfloat: %s doesnt have a chain field !", PRVM_NAME);
848 chain = (prvm_edict_t *)prog->edicts;
850 f = PRVM_G_INT(OFS_PARM0);
851 s = PRVM_G_FLOAT(OFS_PARM1);
853 ent = PRVM_NEXT_EDICT(prog->edicts);
854 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
856 prog->xfunction->builtinsprofile++;
857 if (ent->priv.required->free)
859 if (PRVM_E_FLOAT(ent,f) != s)
862 PRVM_EDICTFIELDVALUE(ent,prog->fieldoffsets.chain)->edict = PRVM_EDICT_TO_PROG(chain);
866 VM_RETURN_EDICT(chain);
870 ========================
873 entity findflags(entity start, .float field, float match)
874 ========================
876 // LordHavoc: search for flags in float fields
877 void VM_findflags (void)
884 VM_SAFEPARMCOUNT(3, VM_findflags);
887 e = PRVM_G_EDICTNUM(OFS_PARM0);
888 f = PRVM_G_INT(OFS_PARM1);
889 s = (int)PRVM_G_FLOAT(OFS_PARM2);
891 for (e++ ; e < prog->num_edicts ; e++)
893 prog->xfunction->builtinsprofile++;
894 ed = PRVM_EDICT_NUM(e);
895 if (ed->priv.required->free)
897 if (!PRVM_E_FLOAT(ed,f))
899 if ((int)PRVM_E_FLOAT(ed,f) & s)
906 VM_RETURN_EDICT(prog->edicts);
910 ========================
913 entity findchainflags(.float field, float match)
914 ========================
916 // LordHavoc: chained search for flags in float fields
917 void VM_findchainflags (void)
922 prvm_edict_t *ent, *chain;
924 VM_SAFEPARMCOUNT(2, VM_findchainflags);
926 if (prog->fieldoffsets.chain < 0)
927 PRVM_ERROR("VM_findchainflags: %s doesnt have a chain field !", PRVM_NAME);
929 chain = (prvm_edict_t *)prog->edicts;
931 f = PRVM_G_INT(OFS_PARM0);
932 s = (int)PRVM_G_FLOAT(OFS_PARM1);
934 ent = PRVM_NEXT_EDICT(prog->edicts);
935 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
937 prog->xfunction->builtinsprofile++;
938 if (ent->priv.required->free)
940 if (!PRVM_E_FLOAT(ent,f))
942 if (!((int)PRVM_E_FLOAT(ent,f) & s))
945 PRVM_EDICTFIELDVALUE(ent,prog->fieldoffsets.chain)->edict = PRVM_EDICT_TO_PROG(chain);
949 VM_RETURN_EDICT(chain);
956 string precache_sound (string sample)
959 void VM_precache_sound (void)
963 VM_SAFEPARMCOUNT(1, VM_precache_sound);
965 s = PRVM_G_STRING(OFS_PARM0);
966 PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
967 VM_CheckEmptyString(s);
969 if(snd_initialized.integer && !S_PrecacheSound(s, true, false))
971 VM_Warning("VM_precache_sound: Failed to load %s for %s\n", s, PRVM_NAME);
980 returns the same string as output
982 does nothing, only used by qcc to build .pak archives
985 void VM_precache_file (void)
987 VM_SAFEPARMCOUNT(1,VM_precache_file);
988 // precache_file is only used to copy files with qcc, it does nothing
989 PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
999 void VM_coredump (void)
1001 VM_SAFEPARMCOUNT(0,VM_coredump);
1003 Cbuf_AddText("prvm_edicts ");
1004 Cbuf_AddText(PRVM_NAME);
1015 void PRVM_StackTrace(void);
1016 void VM_stackdump (void)
1018 VM_SAFEPARMCOUNT(0, VM_stackdump);
1033 VM_SAFEPARMCOUNT(0, VM_crash);
1035 PRVM_ERROR("Crash called by %s",PRVM_NAME);
1045 void VM_traceon (void)
1047 VM_SAFEPARMCOUNT(0,VM_traceon);
1059 void VM_traceoff (void)
1061 VM_SAFEPARMCOUNT(0,VM_traceoff);
1063 prog->trace = false;
1073 void VM_eprint (void)
1075 VM_SAFEPARMCOUNT(1,VM_eprint);
1077 PRVM_ED_PrintNum (PRVM_G_EDICTNUM(OFS_PARM0), NULL);
1090 VM_SAFEPARMCOUNT(1,VM_rint);
1092 f = PRVM_G_FLOAT(OFS_PARM0);
1094 PRVM_G_FLOAT(OFS_RETURN) = floor(f + 0.5);
1096 PRVM_G_FLOAT(OFS_RETURN) = ceil(f - 0.5);
1106 void VM_floor (void)
1108 VM_SAFEPARMCOUNT(1,VM_floor);
1110 PRVM_G_FLOAT(OFS_RETURN) = floor(PRVM_G_FLOAT(OFS_PARM0));
1122 VM_SAFEPARMCOUNT(1,VM_ceil);
1124 PRVM_G_FLOAT(OFS_RETURN) = ceil(PRVM_G_FLOAT(OFS_PARM0));
1132 entity nextent(entity)
1135 void VM_nextent (void)
1140 VM_SAFEPARMCOUNT(1, VM_nextent);
1142 i = PRVM_G_EDICTNUM(OFS_PARM0);
1145 prog->xfunction->builtinsprofile++;
1147 if (i == prog->num_edicts)
1149 VM_RETURN_EDICT(prog->edicts);
1152 ent = PRVM_EDICT_NUM(i);
1153 if (!ent->priv.required->free)
1155 VM_RETURN_EDICT(ent);
1161 //=============================================================================
1168 changelevel(string map)
1171 void VM_changelevel (void)
1173 VM_SAFEPARMCOUNT(1, VM_changelevel);
1177 VM_Warning("VM_changelevel: game is not server (%s)\n", PRVM_NAME);
1181 // make sure we don't issue two changelevels
1182 if (svs.changelevel_issued)
1184 svs.changelevel_issued = true;
1186 Cbuf_AddText (va("changelevel %s\n",PRVM_G_STRING(OFS_PARM0)));
1198 VM_SAFEPARMCOUNT(1,VM_sin);
1199 PRVM_G_FLOAT(OFS_RETURN) = sin(PRVM_G_FLOAT(OFS_PARM0));
1210 VM_SAFEPARMCOUNT(1,VM_cos);
1211 PRVM_G_FLOAT(OFS_RETURN) = cos(PRVM_G_FLOAT(OFS_PARM0));
1223 VM_SAFEPARMCOUNT(1,VM_sqrt);
1224 PRVM_G_FLOAT(OFS_RETURN) = sqrt(PRVM_G_FLOAT(OFS_PARM0));
1236 VM_SAFEPARMCOUNT(1,VM_asin);
1237 PRVM_G_FLOAT(OFS_RETURN) = asin(PRVM_G_FLOAT(OFS_PARM0));
1248 VM_SAFEPARMCOUNT(1,VM_acos);
1249 PRVM_G_FLOAT(OFS_RETURN) = acos(PRVM_G_FLOAT(OFS_PARM0));
1260 VM_SAFEPARMCOUNT(1,VM_atan);
1261 PRVM_G_FLOAT(OFS_RETURN) = atan(PRVM_G_FLOAT(OFS_PARM0));
1267 float atan2(float,float)
1270 void VM_atan2 (void)
1272 VM_SAFEPARMCOUNT(2,VM_atan2);
1273 PRVM_G_FLOAT(OFS_RETURN) = atan2(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1284 VM_SAFEPARMCOUNT(1,VM_tan);
1285 PRVM_G_FLOAT(OFS_RETURN) = tan(PRVM_G_FLOAT(OFS_PARM0));
1292 Returns a vector of length < 1 and > 0
1297 void VM_randomvec (void)
1302 VM_SAFEPARMCOUNT(0, VM_randomvec);
1307 temp[0] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1308 temp[1] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1309 temp[2] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1311 while (DotProduct(temp, temp) >= 1);
1312 VectorCopy (temp, PRVM_G_VECTOR(OFS_RETURN));
1315 temp[0] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1316 temp[1] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1317 temp[2] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1318 // length returned always > 0
1319 length = (rand()&32766 + 1) * (1.0 / 32767.0) / VectorLength(temp);
1320 VectorScale(temp,length, temp);*/
1321 //VectorCopy(temp, PRVM_G_VECTOR(OFS_RETURN));
1324 //=============================================================================
1330 float registercvar (string name, string value[, float flags])
1333 void VM_registercvar (void)
1335 const char *name, *value;
1338 VM_SAFEPARMCOUNTRANGE(2, 3, VM_registercvar);
1340 name = PRVM_G_STRING(OFS_PARM0);
1341 value = PRVM_G_STRING(OFS_PARM1);
1342 flags = prog->argc >= 3 ? (int)PRVM_G_FLOAT(OFS_PARM2) : 0;
1343 PRVM_G_FLOAT(OFS_RETURN) = 0;
1345 if(flags > CVAR_MAXFLAGSVAL)
1348 // first check to see if it has already been defined
1349 if (Cvar_FindVar (name))
1352 // check for overlap with a command
1353 if (Cmd_Exists (name))
1355 VM_Warning("VM_registercvar: %s is a command\n", name);
1359 Cvar_Get(name, value, flags);
1361 PRVM_G_FLOAT(OFS_RETURN) = 1; // success
1369 returns the minimum of two supplied floats
1371 float min(float a, float b, ...[float])
1376 VM_SAFEPARMCOUNTRANGE(2, 8, VM_min);
1377 // LordHavoc: 3+ argument enhancement suggested by FrikaC
1378 if (prog->argc >= 3)
1381 float f = PRVM_G_FLOAT(OFS_PARM0);
1382 for (i = 1;i < prog->argc;i++)
1383 if (f > PRVM_G_FLOAT((OFS_PARM0+i*3)))
1384 f = PRVM_G_FLOAT((OFS_PARM0+i*3));
1385 PRVM_G_FLOAT(OFS_RETURN) = f;
1388 PRVM_G_FLOAT(OFS_RETURN) = min(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1395 returns the maximum of two supplied floats
1397 float max(float a, float b, ...[float])
1402 VM_SAFEPARMCOUNTRANGE(2, 8, VM_max);
1403 // LordHavoc: 3+ argument enhancement suggested by FrikaC
1404 if (prog->argc >= 3)
1407 float f = PRVM_G_FLOAT(OFS_PARM0);
1408 for (i = 1;i < prog->argc;i++)
1409 if (f < PRVM_G_FLOAT((OFS_PARM0+i*3)))
1410 f = PRVM_G_FLOAT((OFS_PARM0+i*3));
1411 PRVM_G_FLOAT(OFS_RETURN) = f;
1414 PRVM_G_FLOAT(OFS_RETURN) = max(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1421 returns number bounded by supplied range
1423 float bound(float min, float value, float max)
1426 void VM_bound (void)
1428 VM_SAFEPARMCOUNT(3,VM_bound);
1429 PRVM_G_FLOAT(OFS_RETURN) = bound(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1), PRVM_G_FLOAT(OFS_PARM2));
1436 returns a raised to power b
1438 float pow(float a, float b)
1443 VM_SAFEPARMCOUNT(2,VM_pow);
1444 PRVM_G_FLOAT(OFS_RETURN) = pow(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1447 void VM_Files_Init(void)
1450 for (i = 0;i < PRVM_MAX_OPENFILES;i++)
1451 prog->openfiles[i] = NULL;
1454 void VM_Files_CloseAll(void)
1457 for (i = 0;i < PRVM_MAX_OPENFILES;i++)
1459 if (prog->openfiles[i])
1460 FS_Close(prog->openfiles[i]);
1461 prog->openfiles[i] = NULL;
1465 static qfile_t *VM_GetFileHandle( int index )
1467 if (index < 0 || index >= PRVM_MAX_OPENFILES)
1469 Con_Printf("VM_GetFileHandle: invalid file handle %i used in %s\n", index, PRVM_NAME);
1472 if (prog->openfiles[index] == NULL)
1474 Con_Printf("VM_GetFileHandle: no such file handle %i (or file has been closed) in %s\n", index, PRVM_NAME);
1477 return prog->openfiles[index];
1484 float fopen(string filename, float mode)
1487 // float(string filename, float mode) fopen = #110;
1488 // opens a file inside quake/gamedir/data/ (mode is FILE_READ, FILE_APPEND, or FILE_WRITE),
1489 // returns fhandle >= 0 if successful, or fhandle < 0 if unable to open file for any reason
1493 const char *modestring, *filename;
1495 VM_SAFEPARMCOUNT(2,VM_fopen);
1497 for (filenum = 0;filenum < PRVM_MAX_OPENFILES;filenum++)
1498 if (prog->openfiles[filenum] == NULL)
1500 if (filenum >= PRVM_MAX_OPENFILES)
1502 PRVM_G_FLOAT(OFS_RETURN) = -2;
1503 VM_Warning("VM_fopen: %s ran out of file handles (%i)\n", PRVM_NAME, PRVM_MAX_OPENFILES);
1506 mode = (int)PRVM_G_FLOAT(OFS_PARM1);
1509 case 0: // FILE_READ
1512 case 1: // FILE_APPEND
1515 case 2: // FILE_WRITE
1519 PRVM_G_FLOAT(OFS_RETURN) = -3;
1520 VM_Warning("VM_fopen: %s: no such mode %i (valid: 0 = read, 1 = append, 2 = write)\n", PRVM_NAME, mode);
1523 filename = PRVM_G_STRING(OFS_PARM0);
1525 prog->openfiles[filenum] = FS_Open(va("data/%s", filename), modestring, false, false);
1526 if (prog->openfiles[filenum] == NULL && mode == 0)
1527 prog->openfiles[filenum] = FS_Open(va("%s", filename), modestring, false, false);
1529 if (prog->openfiles[filenum] == NULL)
1531 PRVM_G_FLOAT(OFS_RETURN) = -1;
1532 if (developer.integer >= 100)
1533 VM_Warning("VM_fopen: %s: %s mode %s failed\n", PRVM_NAME, filename, modestring);
1537 PRVM_G_FLOAT(OFS_RETURN) = filenum;
1538 if (developer.integer >= 100)
1539 Con_Printf("VM_fopen: %s: %s mode %s opened as #%i\n", PRVM_NAME, filename, modestring, filenum);
1547 fclose(float fhandle)
1550 //void(float fhandle) fclose = #111; // closes a file
1551 void VM_fclose(void)
1555 VM_SAFEPARMCOUNT(1,VM_fclose);
1557 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1558 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
1560 VM_Warning("VM_fclose: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1563 if (prog->openfiles[filenum] == NULL)
1565 VM_Warning("VM_fclose: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1568 FS_Close(prog->openfiles[filenum]);
1569 prog->openfiles[filenum] = NULL;
1570 if (developer.integer >= 100)
1571 Con_Printf("VM_fclose: %s: #%i closed\n", PRVM_NAME, filenum);
1578 string fgets(float fhandle)
1581 //string(float fhandle) fgets = #112; // reads a line of text from the file and returns as a tempstring
1585 char string[VM_STRINGTEMP_LENGTH];
1588 VM_SAFEPARMCOUNT(1,VM_fgets);
1590 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1591 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
1593 VM_Warning("VM_fgets: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1596 if (prog->openfiles[filenum] == NULL)
1598 VM_Warning("VM_fgets: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1604 c = FS_Getc(prog->openfiles[filenum]);
1605 if (c == '\r' || c == '\n' || c < 0)
1607 if (end < VM_STRINGTEMP_LENGTH - 1)
1611 // remove \n following \r
1614 c = FS_Getc(prog->openfiles[filenum]);
1616 FS_UnGetc(prog->openfiles[filenum], (unsigned char)c);
1618 if (developer.integer >= 100)
1619 Con_Printf("fgets: %s: %s\n", PRVM_NAME, string);
1621 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string);
1623 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
1630 fputs(float fhandle, string s)
1633 //void(float fhandle, string s) fputs = #113; // writes a line of text to the end of the file
1637 char string[VM_STRINGTEMP_LENGTH];
1640 VM_SAFEPARMCOUNT(2,VM_fputs);
1642 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1643 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
1645 VM_Warning("VM_fputs: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1648 if (prog->openfiles[filenum] == NULL)
1650 VM_Warning("VM_fputs: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1653 VM_VarString(1, string, sizeof(string));
1654 if ((stringlength = (int)strlen(string)))
1655 FS_Write(prog->openfiles[filenum], string, stringlength);
1656 if (developer.integer >= 100)
1657 Con_Printf("fputs: %s: %s\n", PRVM_NAME, string);
1664 writetofile(float fhandle, entity ent)
1667 void VM_writetofile(void)
1672 VM_SAFEPARMCOUNT(2, VM_writetofile);
1674 file = VM_GetFileHandle( (int)PRVM_G_FLOAT(OFS_PARM0) );
1677 VM_Warning("VM_writetofile: invalid or closed file handle\n");
1681 ent = PRVM_G_EDICT(OFS_PARM1);
1682 if(ent->priv.required->free)
1684 VM_Warning("VM_writetofile: %s: entity %i is free !\n", PRVM_NAME, PRVM_NUM_FOR_EDICT(ent));
1688 PRVM_ED_Write (file, ent);
1695 float strlen(string s)
1698 //float(string s) strlen = #114; // returns how many characters are in a string
1699 void VM_strlen(void)
1701 VM_SAFEPARMCOUNT(1,VM_strlen);
1703 PRVM_G_FLOAT(OFS_RETURN) = strlen(PRVM_G_STRING(OFS_PARM0));
1706 // DRESK - Decolorized String
1711 string strdecolorize(string s)
1714 // string (string s) strdecolorize = #472; // returns the passed in string with color codes stripped
1715 void VM_strdecolorize(void)
1717 char szNewString[VM_STRINGTEMP_LENGTH];
1718 const char *szString;
1721 VM_SAFEPARMCOUNT(1,VM_strdecolorize);
1722 szString = PRVM_G_STRING(OFS_PARM0);
1724 COM_StringDecolorize(szString, 0, szNewString, sizeof(szNewString), TRUE);
1726 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(szNewString);
1729 // DRESK - String Length (not counting color codes)
1734 float strlennocol(string s)
1737 // float(string s) strlennocol = #471; // returns how many characters are in a string not including color codes
1738 // For example, ^2Dresk returns a length of 5
1739 void VM_strlennocol(void)
1741 const char *szString;
1744 VM_SAFEPARMCOUNT(1,VM_strlennocol);
1746 szString = PRVM_G_STRING(OFS_PARM0);
1748 nCnt = COM_StringLengthNoColors(szString, 0, NULL);
1750 PRVM_G_FLOAT(OFS_RETURN) = nCnt;
1753 // DRESK - String to Uppercase and Lowercase
1758 string strtolower(string s)
1761 // string (string s) strtolower = #480; // returns passed in string in lowercase form
1762 void VM_strtolower(void)
1764 char szNewString[VM_STRINGTEMP_LENGTH];
1765 const char *szString;
1768 VM_SAFEPARMCOUNT(1,VM_strtolower);
1769 szString = PRVM_G_STRING(OFS_PARM0);
1771 COM_ToLowerString(szString, szNewString, sizeof(szNewString) );
1773 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(szNewString);
1780 string strtoupper(string s)
1783 // string (string s) strtoupper = #481; // returns passed in string in uppercase form
1784 void VM_strtoupper(void)
1786 char szNewString[VM_STRINGTEMP_LENGTH];
1787 const char *szString;
1790 VM_SAFEPARMCOUNT(1,VM_strtoupper);
1791 szString = PRVM_G_STRING(OFS_PARM0);
1793 COM_ToUpperString(szString, szNewString, sizeof(szNewString) );
1795 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(szNewString);
1802 string strcat(string,string,...[string])
1805 //string(string s1, string s2) strcat = #115;
1806 // concatenates two strings (for example "abc", "def" would return "abcdef")
1807 // and returns as a tempstring
1808 void VM_strcat(void)
1810 char s[VM_STRINGTEMP_LENGTH];
1811 VM_SAFEPARMCOUNTRANGE(1, 8, VM_strcat);
1813 VM_VarString(0, s, sizeof(s));
1814 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(s);
1821 string substring(string s, float start, float length)
1824 // string(string s, float start, float length) substring = #116;
1825 // returns a section of a string as a tempstring
1826 void VM_substring(void)
1828 int i, start, length;
1830 char string[VM_STRINGTEMP_LENGTH];
1832 VM_SAFEPARMCOUNT(3,VM_substring);
1834 s = PRVM_G_STRING(OFS_PARM0);
1835 start = (int)PRVM_G_FLOAT(OFS_PARM1);
1836 length = (int)PRVM_G_FLOAT(OFS_PARM2);
1837 for (i = 0;i < start && *s;i++, s++);
1838 for (i = 0;i < (int)sizeof(string) - 1 && *s && i < length;i++, s++)
1841 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string);
1848 string(string search, string replace, string subject) strreplace = #484;
1851 // replaces all occurrences of search with replace in the string subject, and returns the result
1852 void VM_strreplace(void)
1855 const char *search, *replace, *subject;
1856 char string[VM_STRINGTEMP_LENGTH];
1857 int search_len, replace_len, subject_len;
1859 VM_SAFEPARMCOUNT(3,VM_strreplace);
1861 search = PRVM_G_STRING(OFS_PARM0);
1862 replace = PRVM_G_STRING(OFS_PARM1);
1863 subject = PRVM_G_STRING(OFS_PARM2);
1865 search_len = (int)strlen(search);
1866 replace_len = (int)strlen(replace);
1867 subject_len = (int)strlen(subject);
1870 for (i = 0; i < subject_len; i++)
1872 for (j = 0; j < search_len && i+j < subject_len; j++)
1873 if (subject[i+j] != search[j])
1875 if (j == search_len || i+j == subject_len)
1877 // found it at offset 'i'
1878 for (j = 0; j < replace_len && si < (int)sizeof(string) - 1; j++)
1879 string[si++] = replace[j];
1880 i += search_len - 1;
1885 if (si < (int)sizeof(string) - 1)
1886 string[si++] = subject[i];
1891 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string);
1898 string(string search, string replace, string subject) strireplace = #485;
1901 // case-insensitive version of strreplace
1902 void VM_strireplace(void)
1905 const char *search, *replace, *subject;
1906 char string[VM_STRINGTEMP_LENGTH];
1907 int search_len, replace_len, subject_len;
1909 VM_SAFEPARMCOUNT(3,VM_strreplace);
1911 search = PRVM_G_STRING(OFS_PARM0);
1912 replace = PRVM_G_STRING(OFS_PARM1);
1913 subject = PRVM_G_STRING(OFS_PARM2);
1915 search_len = (int)strlen(search);
1916 replace_len = (int)strlen(replace);
1917 subject_len = (int)strlen(subject);
1920 for (i = 0; i < subject_len; i++)
1922 for (j = 0; j < search_len && i+j < subject_len; j++)
1923 if (tolower(subject[i+j]) != tolower(search[j]))
1925 if (j == search_len || i+j == subject_len)
1927 // found it at offset 'i'
1928 for (j = 0; j < replace_len && si < (int)sizeof(string) - 1; j++)
1929 string[si++] = replace[j];
1930 i += search_len - 1;
1935 if (si < (int)sizeof(string) - 1)
1936 string[si++] = subject[i];
1941 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string);
1948 vector stov(string s)
1951 //vector(string s) stov = #117; // returns vector value from a string
1954 char string[VM_STRINGTEMP_LENGTH];
1956 VM_SAFEPARMCOUNT(1,VM_stov);
1958 VM_VarString(0, string, sizeof(string));
1959 Math_atov(string, PRVM_G_VECTOR(OFS_RETURN));
1966 string strzone(string s)
1969 //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)
1970 void VM_strzone(void)
1973 char string[VM_STRINGTEMP_LENGTH];
1976 VM_SAFEPARMCOUNT(1,VM_strzone);
1978 VM_VarString(0, string, sizeof(string));
1979 alloclen = strlen(string) + 1;
1980 PRVM_G_INT(OFS_RETURN) = PRVM_AllocString(alloclen, &out);
1981 memcpy(out, string, alloclen);
1991 //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!!!)
1992 void VM_strunzone(void)
1994 VM_SAFEPARMCOUNT(1,VM_strunzone);
1995 PRVM_FreeString(PRVM_G_INT(OFS_PARM0));
2000 VM_command (used by client and menu)
2002 clientcommand(float client, string s) (for client and menu)
2005 //void(entity e, string s) clientcommand = #440; // executes a command string as if it came from the specified client
2006 //this function originally written by KrimZon, made shorter by LordHavoc
2007 void VM_clcommand (void)
2009 client_t *temp_client;
2012 VM_SAFEPARMCOUNT(2,VM_clcommand);
2014 i = (int)PRVM_G_FLOAT(OFS_PARM0);
2015 if (!sv.active || i < 0 || i >= svs.maxclients || !svs.clients[i].active)
2017 VM_Warning("VM_clientcommand: %s: invalid client/server is not active !\n", PRVM_NAME);
2021 temp_client = host_client;
2022 host_client = svs.clients + i;
2023 Cmd_ExecuteString (PRVM_G_STRING(OFS_PARM1), src_client);
2024 host_client = temp_client;
2032 float tokenize(string s)
2035 //float(string s) tokenize = #441; // takes apart a string into individal words (access them with argv), returns how many
2036 //this function originally written by KrimZon, made shorter by LordHavoc
2037 //20040203: rewritten by LordHavoc (no longer uses allocations)
2040 void VM_tokenize (void)
2043 static char string[VM_STRINGTEMP_LENGTH]; // static, because it's big
2045 VM_SAFEPARMCOUNT(1,VM_tokenize);
2047 strlcpy(string, PRVM_G_STRING(OFS_PARM0), sizeof(string));
2051 while(COM_ParseToken_VM_Tokenize(&p, false))
2053 if (num_tokens >= (int)(sizeof(tokens)/sizeof(tokens[0])))
2055 tokens[num_tokens++] = PRVM_SetTempString(com_token);
2058 PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
2063 VM_tokenizebyseparator
2065 float tokenizebyseparator(string s, string separator1, ...)
2068 //float(string s, string separator1, ...) tokenizebyseparator = #479; // takes apart a string into individal words (access them with argv), returns how many
2069 //this function returns the token preceding each instance of a separator (of
2070 //which there can be multiple), and the text following the last separator
2071 //useful for parsing certain kinds of data like IP addresses
2073 //numnumbers = tokenizebyseparator("10.1.2.3", ".");
2074 //returns 4 and the tokens "10" "1" "2" "3".
2075 void VM_tokenizebyseparator (void)
2079 int separatorlen[7];
2080 const char *separators[7];
2083 char tokentext[MAX_INPUTLINE];
2084 static char string[VM_STRINGTEMP_LENGTH]; // static, because it's big
2086 VM_SAFEPARMCOUNTRANGE(2, 8,VM_tokenizebyseparator);
2088 strlcpy(string, PRVM_G_STRING(OFS_PARM0), sizeof(string));
2092 for (j = 1;j < prog->argc;j++)
2094 // skip any blank separator strings
2095 const char *s = PRVM_G_STRING(OFS_PARM0+j*3);
2098 separators[numseparators] = s;
2099 separatorlen[numseparators] = strlen(s);
2106 while (num_tokens < (int)(sizeof(tokens)/sizeof(tokens[0])))
2108 token = tokentext + j;
2111 for (k = 0;k < numseparators;k++)
2113 if (!strncmp(p, separators[k], separatorlen[k]))
2115 p += separatorlen[k];
2119 if (k < numseparators)
2121 if (j < (int)sizeof(tokentext)-1)
2122 tokentext[j++] = *p;
2125 if (j >= (int)sizeof(tokentext))
2128 tokens[num_tokens++] = PRVM_SetTempString(token);
2133 PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
2136 //string(float n) argv = #442; // returns a word from the tokenized string (returns nothing for an invalid index)
2137 //this function originally written by KrimZon, made shorter by LordHavoc
2142 VM_SAFEPARMCOUNT(1,VM_argv);
2144 token_num = (int)PRVM_G_FLOAT(OFS_PARM0);
2146 if (token_num >= 0 && token_num < num_tokens)
2147 PRVM_G_INT(OFS_RETURN) = tokens[token_num];
2149 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
2159 void VM_isserver(void)
2161 VM_SAFEPARMCOUNT(0,VM_serverstate);
2163 PRVM_G_FLOAT(OFS_RETURN) = sv.active && (svs.maxclients > 1 || cls.state == ca_dedicated);
2173 void VM_clientcount(void)
2175 VM_SAFEPARMCOUNT(0,VM_clientcount);
2177 PRVM_G_FLOAT(OFS_RETURN) = svs.maxclients;
2187 void VM_clientstate(void)
2189 VM_SAFEPARMCOUNT(0,VM_clientstate);
2191 PRVM_G_FLOAT(OFS_RETURN) = cls.state;
2198 float getostype(void)
2200 */ // not used at the moment -> not included in the common list
2201 void VM_getostype(void)
2203 VM_SAFEPARMCOUNT(0,VM_getostype);
2208 OS_MAC - not supported
2212 PRVM_G_FLOAT(OFS_RETURN) = 0;
2213 #elif defined(MACOSX)
2214 PRVM_G_FLOAT(OFS_RETURN) = 2;
2216 PRVM_G_FLOAT(OFS_RETURN) = 1;
2224 vector getmousepos()
2227 void VM_getmousepos(void)
2230 VM_SAFEPARMCOUNT(0,VM_getmousepos);
2232 PRVM_G_VECTOR(OFS_RETURN)[0] = in_mouse_x * vid_conwidth.integer / vid.width;
2233 PRVM_G_VECTOR(OFS_RETURN)[1] = in_mouse_y * vid_conheight.integer / vid.height;
2234 PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
2244 void VM_gettime(void)
2246 VM_SAFEPARMCOUNT(0,VM_gettime);
2248 PRVM_G_FLOAT(OFS_RETURN) = (float) realtime;
2255 loadfromdata(string data)
2258 void VM_loadfromdata(void)
2260 VM_SAFEPARMCOUNT(1,VM_loadentsfromfile);
2262 PRVM_ED_LoadFromFile(PRVM_G_STRING(OFS_PARM0));
2266 ========================
2269 parseentitydata(entity ent, string data)
2270 ========================
2272 void VM_parseentitydata(void)
2277 VM_SAFEPARMCOUNT(2, VM_parseentitydata);
2279 // get edict and test it
2280 ent = PRVM_G_EDICT(OFS_PARM0);
2281 if (ent->priv.required->free)
2282 PRVM_ERROR ("VM_parseentitydata: %s: Can only set already spawned entities (entity %i is free)!", PRVM_NAME, PRVM_NUM_FOR_EDICT(ent));
2284 data = PRVM_G_STRING(OFS_PARM1);
2286 // parse the opening brace
2287 if (!COM_ParseToken_Simple(&data, false, false) || com_token[0] != '{' )
2288 PRVM_ERROR ("VM_parseentitydata: %s: Couldn't parse entity data:\n%s", PRVM_NAME, data );
2290 PRVM_ED_ParseEdict (data, ent);
2297 loadfromfile(string file)
2300 void VM_loadfromfile(void)
2302 const char *filename;
2305 VM_SAFEPARMCOUNT(1,VM_loadfromfile);
2307 filename = PRVM_G_STRING(OFS_PARM0);
2308 if (FS_CheckNastyPath(filename, false))
2310 PRVM_G_FLOAT(OFS_RETURN) = -4;
2311 VM_Warning("VM_loadfromfile: %s dangerous or non-portable filename \"%s\" not allowed. (contains : or \\ or begins with .. or /)\n", PRVM_NAME, filename);
2315 // not conform with VM_fopen
2316 data = (char *)FS_LoadFile(filename, tempmempool, false, NULL);
2318 PRVM_G_FLOAT(OFS_RETURN) = -1;
2320 PRVM_ED_LoadFromFile(data);
2331 float mod(float val, float m)
2334 void VM_modulo(void)
2337 VM_SAFEPARMCOUNT(2,VM_module);
2339 val = (int) PRVM_G_FLOAT(OFS_PARM0);
2340 m = (int) PRVM_G_FLOAT(OFS_PARM1);
2342 PRVM_G_FLOAT(OFS_RETURN) = (float) (val % m);
2345 void VM_Search_Init(void)
2348 for (i = 0;i < PRVM_MAX_OPENSEARCHES;i++)
2349 prog->opensearches[i] = NULL;
2352 void VM_Search_Reset(void)
2355 // reset the fssearch list
2356 for(i = 0; i < PRVM_MAX_OPENSEARCHES; i++)
2358 if(prog->opensearches[i])
2359 FS_FreeSearch(prog->opensearches[i]);
2360 prog->opensearches[i] = NULL;
2368 float search_begin(string pattern, float caseinsensitive, float quiet)
2371 void VM_search_begin(void)
2374 const char *pattern;
2375 int caseinsens, quiet;
2377 VM_SAFEPARMCOUNT(3, VM_search_begin);
2379 pattern = PRVM_G_STRING(OFS_PARM0);
2381 VM_CheckEmptyString(pattern);
2383 caseinsens = (int)PRVM_G_FLOAT(OFS_PARM1);
2384 quiet = (int)PRVM_G_FLOAT(OFS_PARM2);
2386 for(handle = 0; handle < PRVM_MAX_OPENSEARCHES; handle++)
2387 if(!prog->opensearches[handle])
2390 if(handle >= PRVM_MAX_OPENSEARCHES)
2392 PRVM_G_FLOAT(OFS_RETURN) = -2;
2393 VM_Warning("VM_search_begin: %s ran out of search handles (%i)\n", PRVM_NAME, PRVM_MAX_OPENSEARCHES);
2397 if(!(prog->opensearches[handle] = FS_Search(pattern,caseinsens, quiet)))
2398 PRVM_G_FLOAT(OFS_RETURN) = -1;
2400 PRVM_G_FLOAT(OFS_RETURN) = handle;
2407 void search_end(float handle)
2410 void VM_search_end(void)
2413 VM_SAFEPARMCOUNT(1, VM_search_end);
2415 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
2417 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
2419 VM_Warning("VM_search_end: invalid handle %i used in %s\n", handle, PRVM_NAME);
2422 if(prog->opensearches[handle] == NULL)
2424 VM_Warning("VM_search_end: no such handle %i in %s\n", handle, PRVM_NAME);
2428 FS_FreeSearch(prog->opensearches[handle]);
2429 prog->opensearches[handle] = NULL;
2436 float search_getsize(float handle)
2439 void VM_search_getsize(void)
2442 VM_SAFEPARMCOUNT(1, VM_M_search_getsize);
2444 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
2446 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
2448 VM_Warning("VM_search_getsize: invalid handle %i used in %s\n", handle, PRVM_NAME);
2451 if(prog->opensearches[handle] == NULL)
2453 VM_Warning("VM_search_getsize: no such handle %i in %s\n", handle, PRVM_NAME);
2457 PRVM_G_FLOAT(OFS_RETURN) = prog->opensearches[handle]->numfilenames;
2462 VM_search_getfilename
2464 string search_getfilename(float handle, float num)
2467 void VM_search_getfilename(void)
2469 int handle, filenum;
2470 VM_SAFEPARMCOUNT(2, VM_search_getfilename);
2472 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
2473 filenum = (int)PRVM_G_FLOAT(OFS_PARM1);
2475 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
2477 VM_Warning("VM_search_getfilename: invalid handle %i used in %s\n", handle, PRVM_NAME);
2480 if(prog->opensearches[handle] == NULL)
2482 VM_Warning("VM_search_getfilename: no such handle %i in %s\n", handle, PRVM_NAME);
2485 if(filenum < 0 || filenum >= prog->opensearches[handle]->numfilenames)
2487 VM_Warning("VM_search_getfilename: invalid filenum %i in %s\n", filenum, PRVM_NAME);
2491 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog->opensearches[handle]->filenames[filenum]);
2498 string chr(float ascii)
2504 VM_SAFEPARMCOUNT(1, VM_chr);
2506 tmp[0] = (unsigned char) PRVM_G_FLOAT(OFS_PARM0);
2509 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(tmp);
2512 //=============================================================================
2513 // Draw builtins (client & menu)
2519 float iscachedpic(string pic)
2522 void VM_iscachedpic(void)
2524 VM_SAFEPARMCOUNT(1,VM_iscachedpic);
2526 // drawq hasnt such a function, thus always return true
2527 PRVM_G_FLOAT(OFS_RETURN) = false;
2534 string precache_pic(string pic)
2537 void VM_precache_pic(void)
2541 VM_SAFEPARMCOUNT(1, VM_precache_pic);
2543 s = PRVM_G_STRING(OFS_PARM0);
2544 PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
2545 VM_CheckEmptyString (s);
2547 // AK Draw_CachePic is supposed to always return a valid pointer
2548 if( Draw_CachePic(s, false)->tex == r_texture_notexture )
2549 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
2559 void VM_freepic(void)
2563 VM_SAFEPARMCOUNT(1,VM_freepic);
2565 s = PRVM_G_STRING(OFS_PARM0);
2566 VM_CheckEmptyString (s);
2571 dp_font_t *getdrawfont()
2573 if(prog->globaloffsets.drawfont >= 0)
2575 int f = PRVM_G_FLOAT(prog->globaloffsets.drawfont);
2576 if(f < 0 || f >= MAX_FONTS)
2577 return FONT_DEFAULT;
2578 return &dp_fonts[f];
2581 return FONT_DEFAULT;
2588 float drawcharacter(vector position, float character, vector scale, vector rgb, float alpha, float flag)
2591 void VM_drawcharacter(void)
2593 float *pos,*scale,*rgb;
2596 VM_SAFEPARMCOUNT(6,VM_drawcharacter);
2598 character = (char) PRVM_G_FLOAT(OFS_PARM1);
2601 PRVM_G_FLOAT(OFS_RETURN) = -1;
2602 VM_Warning("VM_drawcharacter: %s passed null character !\n",PRVM_NAME);
2606 pos = PRVM_G_VECTOR(OFS_PARM0);
2607 scale = PRVM_G_VECTOR(OFS_PARM2);
2608 rgb = PRVM_G_VECTOR(OFS_PARM3);
2609 flag = (int)PRVM_G_FLOAT(OFS_PARM5);
2611 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2613 PRVM_G_FLOAT(OFS_RETURN) = -2;
2614 VM_Warning("VM_drawcharacter: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2618 if(pos[2] || scale[2])
2619 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")));
2621 if(!scale[0] || !scale[1])
2623 PRVM_G_FLOAT(OFS_RETURN) = -3;
2624 VM_Warning("VM_drawcharacter: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
2628 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());
2629 PRVM_G_FLOAT(OFS_RETURN) = 1;
2636 float drawstring(vector position, string text, vector scale, vector rgb, float alpha, float flag)
2639 void VM_drawstring(void)
2641 float *pos,*scale,*rgb;
2644 VM_SAFEPARMCOUNT(6,VM_drawstring);
2646 string = PRVM_G_STRING(OFS_PARM1);
2647 pos = PRVM_G_VECTOR(OFS_PARM0);
2648 scale = PRVM_G_VECTOR(OFS_PARM2);
2649 rgb = PRVM_G_VECTOR(OFS_PARM3);
2650 flag = (int)PRVM_G_FLOAT(OFS_PARM5);
2652 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2654 PRVM_G_FLOAT(OFS_RETURN) = -2;
2655 VM_Warning("VM_drawstring: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2659 if(!scale[0] || !scale[1])
2661 PRVM_G_FLOAT(OFS_RETURN) = -3;
2662 VM_Warning("VM_drawstring: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
2666 if(pos[2] || scale[2])
2667 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")));
2669 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());
2670 PRVM_G_FLOAT(OFS_RETURN) = 1;
2675 VM_drawcolorcodedstring
2677 float drawcolorcodedstring(vector position, string text, vector scale, float alpha, float flag)
2680 void VM_drawcolorcodedstring(void)
2685 VM_SAFEPARMCOUNT(5,VM_drawstring);
2687 string = PRVM_G_STRING(OFS_PARM1);
2688 pos = PRVM_G_VECTOR(OFS_PARM0);
2689 scale = PRVM_G_VECTOR(OFS_PARM2);
2690 flag = (int)PRVM_G_FLOAT(OFS_PARM5);
2692 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2694 PRVM_G_FLOAT(OFS_RETURN) = -2;
2695 VM_Warning("VM_drawcolorcodedstring: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2699 if(!scale[0] || !scale[1])
2701 PRVM_G_FLOAT(OFS_RETURN) = -3;
2702 VM_Warning("VM_drawcolorcodedstring: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
2706 if(pos[2] || scale[2])
2707 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")));
2710 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());
2711 PRVM_G_FLOAT(OFS_RETURN) = 1;
2717 float stringwidth(string text, float allowColorCodes)
2720 void VM_stringwidth(void)
2724 VM_SAFEPARMCOUNT(2,VM_drawstring);
2726 string = PRVM_G_STRING(OFS_PARM0);
2727 colors = (int)PRVM_G_FLOAT(OFS_PARM1);
2729 PRVM_G_FLOAT(OFS_RETURN) = DrawQ_TextWidth_Font(string, 0, !colors, getdrawfont()); // 1x1 characters, don't actually draw
2735 float drawpic(vector position, string pic, vector size, vector rgb, float alpha, float flag)
2738 void VM_drawpic(void)
2740 const char *picname;
2741 float *size, *pos, *rgb;
2744 VM_SAFEPARMCOUNT(6,VM_drawpic);
2746 picname = PRVM_G_STRING(OFS_PARM1);
2747 VM_CheckEmptyString (picname);
2749 // is pic cached ? no function yet for that
2752 PRVM_G_FLOAT(OFS_RETURN) = -4;
2753 VM_Warning("VM_drawpic: %s: %s not cached !\n", PRVM_NAME, picname);
2757 pos = PRVM_G_VECTOR(OFS_PARM0);
2758 size = PRVM_G_VECTOR(OFS_PARM2);
2759 rgb = PRVM_G_VECTOR(OFS_PARM3);
2760 flag = (int) PRVM_G_FLOAT(OFS_PARM5);
2762 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2764 PRVM_G_FLOAT(OFS_RETURN) = -2;
2765 VM_Warning("VM_drawpic: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2769 if(pos[2] || size[2])
2770 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")));
2772 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);
2773 PRVM_G_FLOAT(OFS_RETURN) = 1;
2779 float drawsubpic(vector position, vector size, string pic, vector srcPos, vector srcSize, vector rgb, float alpha, float flag)
2783 void VM_drawsubpic(void)
2785 const char *picname;
2786 float *size, *pos, *rgb, *srcPos, *srcSize, alpha;
2789 VM_SAFEPARMCOUNT(8,VM_drawsubpic);
2791 picname = PRVM_G_STRING(OFS_PARM2);
2792 VM_CheckEmptyString (picname);
2794 // is pic cached ? no function yet for that
2797 PRVM_G_FLOAT(OFS_RETURN) = -4;
2798 VM_Warning("VM_drawsubpic: %s: %s not cached !\n", PRVM_NAME, picname);
2802 pos = PRVM_G_VECTOR(OFS_PARM0);
2803 size = PRVM_G_VECTOR(OFS_PARM1);
2804 srcPos = PRVM_G_VECTOR(OFS_PARM3);
2805 srcSize = PRVM_G_VECTOR(OFS_PARM4);
2806 rgb = PRVM_G_VECTOR(OFS_PARM5);
2807 alpha = PRVM_G_FLOAT(OFS_PARM6);
2808 flag = (int) PRVM_G_FLOAT(OFS_PARM7);
2810 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2812 PRVM_G_FLOAT(OFS_RETURN) = -2;
2813 VM_Warning("VM_drawsubpic: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2817 if(pos[2] || size[2])
2818 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")));
2820 DrawQ_SuperPic(pos[0], pos[1], Draw_CachePic(picname, true),
2822 srcPos[0], srcPos[1], rgb[0], rgb[1], rgb[2], alpha,
2823 srcPos[0] + srcSize[0], srcPos[1], rgb[0], rgb[1], rgb[2], alpha,
2824 srcPos[0], srcPos[1] + srcSize[1], rgb[0], rgb[1], rgb[2], alpha,
2825 srcPos[0] + srcSize[0], srcPos[1] + srcSize[1], rgb[0], rgb[1], rgb[2], alpha,
2827 PRVM_G_FLOAT(OFS_RETURN) = 1;
2834 float drawfill(vector position, vector size, vector rgb, float alpha, float flag)
2837 void VM_drawfill(void)
2839 float *size, *pos, *rgb;
2842 VM_SAFEPARMCOUNT(5,VM_drawfill);
2845 pos = PRVM_G_VECTOR(OFS_PARM0);
2846 size = PRVM_G_VECTOR(OFS_PARM1);
2847 rgb = PRVM_G_VECTOR(OFS_PARM2);
2848 flag = (int) PRVM_G_FLOAT(OFS_PARM4);
2850 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2852 PRVM_G_FLOAT(OFS_RETURN) = -2;
2853 VM_Warning("VM_drawfill: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2857 if(pos[2] || size[2])
2858 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")));
2860 DrawQ_Fill(pos[0], pos[1], size[0], size[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM3), flag);
2861 PRVM_G_FLOAT(OFS_RETURN) = 1;
2868 drawsetcliparea(float x, float y, float width, float height)
2871 void VM_drawsetcliparea(void)
2874 VM_SAFEPARMCOUNT(4,VM_drawsetcliparea);
2876 x = bound(0, PRVM_G_FLOAT(OFS_PARM0), vid_conwidth.integer);
2877 y = bound(0, PRVM_G_FLOAT(OFS_PARM1), vid_conheight.integer);
2878 w = bound(0, PRVM_G_FLOAT(OFS_PARM2) + PRVM_G_FLOAT(OFS_PARM0) - x, (vid_conwidth.integer - x));
2879 h = bound(0, PRVM_G_FLOAT(OFS_PARM3) + PRVM_G_FLOAT(OFS_PARM1) - y, (vid_conheight.integer - y));
2881 DrawQ_SetClipArea(x, y, w, h);
2886 VM_drawresetcliparea
2891 void VM_drawresetcliparea(void)
2893 VM_SAFEPARMCOUNT(0,VM_drawresetcliparea);
2895 DrawQ_ResetClipArea();
2902 vector getimagesize(string pic)
2905 void VM_getimagesize(void)
2910 VM_SAFEPARMCOUNT(1,VM_getimagesize);
2912 p = PRVM_G_STRING(OFS_PARM0);
2913 VM_CheckEmptyString (p);
2915 pic = Draw_CachePic (p, false);
2917 PRVM_G_VECTOR(OFS_RETURN)[0] = pic->width;
2918 PRVM_G_VECTOR(OFS_RETURN)[1] = pic->height;
2919 PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
2926 string keynumtostring(float keynum)
2929 void VM_keynumtostring (void)
2931 VM_SAFEPARMCOUNT(1, VM_keynumtostring);
2933 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(Key_KeynumToString((int)PRVM_G_FLOAT(OFS_PARM0)));
2940 float stringtokeynum(string key)
2943 void VM_stringtokeynum (void)
2945 VM_SAFEPARMCOUNT( 1, VM_keynumtostring );
2947 PRVM_G_INT(OFS_RETURN) = Key_StringToKeynum(PRVM_G_STRING(OFS_PARM0));
2950 // CL_Video interface functions
2953 ========================
2956 float cin_open(string file, string name)
2957 ========================
2959 void VM_cin_open( void )
2964 VM_SAFEPARMCOUNT( 2, VM_cin_open );
2966 file = PRVM_G_STRING( OFS_PARM0 );
2967 name = PRVM_G_STRING( OFS_PARM1 );
2969 VM_CheckEmptyString( file );
2970 VM_CheckEmptyString( name );
2972 if( CL_OpenVideo( file, name, MENUOWNER ) )
2973 PRVM_G_FLOAT( OFS_RETURN ) = 1;
2975 PRVM_G_FLOAT( OFS_RETURN ) = 0;
2979 ========================
2982 void cin_close(string name)
2983 ========================
2985 void VM_cin_close( void )
2989 VM_SAFEPARMCOUNT( 1, VM_cin_close );
2991 name = PRVM_G_STRING( OFS_PARM0 );
2992 VM_CheckEmptyString( name );
2994 CL_CloseVideo( CL_GetVideoByName( name ) );
2998 ========================
3000 void cin_setstate(string name, float type)
3001 ========================
3003 void VM_cin_setstate( void )
3006 clvideostate_t state;
3009 VM_SAFEPARMCOUNT( 2, VM_cin_netstate );
3011 name = PRVM_G_STRING( OFS_PARM0 );
3012 VM_CheckEmptyString( name );
3014 state = (clvideostate_t)((int)PRVM_G_FLOAT( OFS_PARM1 ));
3016 video = CL_GetVideoByName( name );
3017 if( video && state > CLVIDEO_UNUSED && state < CLVIDEO_STATECOUNT )
3018 CL_SetVideoState( video, state );
3022 ========================
3025 float cin_getstate(string name)
3026 ========================
3028 void VM_cin_getstate( void )
3033 VM_SAFEPARMCOUNT( 1, VM_cin_getstate );
3035 name = PRVM_G_STRING( OFS_PARM0 );
3036 VM_CheckEmptyString( name );
3038 video = CL_GetVideoByName( name );
3040 PRVM_G_FLOAT( OFS_RETURN ) = (int)video->state;
3042 PRVM_G_FLOAT( OFS_RETURN ) = 0;
3046 ========================
3049 void cin_restart(string name)
3050 ========================
3052 void VM_cin_restart( void )
3057 VM_SAFEPARMCOUNT( 1, VM_cin_restart );
3059 name = PRVM_G_STRING( OFS_PARM0 );
3060 VM_CheckEmptyString( name );
3062 video = CL_GetVideoByName( name );
3064 CL_RestartVideo( video );
3067 #ifdef SUPPORT_GECKO
3069 ========================
3071 ========================
3073 void VM_Gecko_Init( void ) {
3074 // the prog struct is memset to 0 by Initprog? [12/6/2007 Black]
3075 // FIXME: remove the other _Init functions then, too? [12/6/2007 Black]
3079 ========================
3081 ========================
3083 void VM_Gecko_Destroy( void ) {
3085 for( i = 0 ; i < PRVM_MAX_GECKOINSTANCES ; i++ ) {
3086 clgecko_t **instance = &prog->opengeckoinstances[ i ];
3088 CL_Gecko_DestroyBrowser( *instance );
3095 ========================
3098 float[bool] gecko_create( string name )
3099 ========================
3101 void VM_gecko_create( void ) {
3104 clgecko_t *instance;
3106 VM_SAFEPARMCOUNT( 1, VM_gecko_create );
3108 name = PRVM_G_STRING( OFS_PARM0 );
3109 VM_CheckEmptyString( name );
3111 // find an empty slot for this gecko browser..
3112 for( i = 0 ; i < PRVM_MAX_GECKOINSTANCES ; i++ ) {
3113 if( prog->opengeckoinstances[ i ] == NULL ) {
3117 if( i == PRVM_MAX_GECKOINSTANCES ) {
3118 VM_Warning("VM_gecko_create: %s ran out of gecko handles (%i)\n", PRVM_NAME, PRVM_MAX_GECKOINSTANCES);
3119 PRVM_G_FLOAT( OFS_RETURN ) = 0;
3123 instance = prog->opengeckoinstances[ i ] = CL_Gecko_CreateBrowser( name );
3125 // TODO: error handling [12/3/2007 Black]
3126 PRVM_G_FLOAT( OFS_RETURN ) = 0;
3129 PRVM_G_FLOAT( OFS_RETURN ) = 1;
3133 ========================
3136 void gecko_destroy( string name )
3137 ========================
3139 void VM_gecko_destroy( void ) {
3141 clgecko_t *instance;
3143 VM_SAFEPARMCOUNT( 1, VM_gecko_destroy );
3145 name = PRVM_G_STRING( OFS_PARM0 );
3146 VM_CheckEmptyString( name );
3147 instance = CL_Gecko_FindBrowser( name );
3151 CL_Gecko_DestroyBrowser( instance );
3155 ========================
3158 void gecko_navigate( string name, string URI )
3159 ========================
3161 void VM_gecko_navigate( void ) {
3164 clgecko_t *instance;
3166 VM_SAFEPARMCOUNT( 2, VM_gecko_navigate );
3168 name = PRVM_G_STRING( OFS_PARM0 );
3169 URI = PRVM_G_STRING( OFS_PARM1 );
3170 VM_CheckEmptyString( name );
3171 VM_CheckEmptyString( URI );
3173 instance = CL_Gecko_FindBrowser( name );
3177 CL_Gecko_NavigateToURI( instance, URI );
3181 ========================
3184 float[bool] gecko_keyevent( string name, float key, float eventtype )
3185 ========================
3187 void VM_gecko_keyevent( void ) {
3190 clgecko_buttoneventtype_t eventtype;
3191 clgecko_t *instance;
3193 VM_SAFEPARMCOUNT( 3, VM_gecko_keyevent );
3195 name = PRVM_G_STRING( OFS_PARM0 );
3196 VM_CheckEmptyString( name );
3197 key = (unsigned int) PRVM_G_FLOAT( OFS_PARM1 );
3198 switch( (unsigned int) PRVM_G_FLOAT( OFS_PARM2 ) ) {
3200 eventtype = CLG_BET_DOWN;
3203 eventtype = CLG_BET_UP;
3206 eventtype = CLG_BET_PRESS;
3209 eventtype = CLG_BET_DOUBLECLICK;
3212 // TODO: console printf? [12/3/2007 Black]
3213 PRVM_G_FLOAT( OFS_RETURN ) = 0;
3217 instance = CL_Gecko_FindBrowser( name );
3219 PRVM_G_FLOAT( OFS_RETURN ) = 0;
3223 PRVM_G_FLOAT( OFS_RETURN ) = (CL_Gecko_Event_Key( instance, key, eventtype ) == true);
3227 ========================
3230 void gecko_mousemove( string name, float x, float y )
3231 ========================
3233 void VM_gecko_movemouse( void ) {
3236 clgecko_t *instance;
3238 VM_SAFEPARMCOUNT( 3, VM_gecko_movemouse );
3240 name = PRVM_G_STRING( OFS_PARM0 );
3241 VM_CheckEmptyString( name );
3242 x = PRVM_G_FLOAT( OFS_PARM1 );
3243 y = PRVM_G_FLOAT( OFS_PARM2 );
3245 instance = CL_Gecko_FindBrowser( name );
3249 CL_Gecko_Event_CursorMove( instance, x, y );
3254 ========================
3257 void gecko_resize( string name, float w, float h )
3258 ========================
3260 void VM_gecko_resize( void ) {
3263 clgecko_t *instance;
3265 VM_SAFEPARMCOUNT( 3, VM_gecko_movemouse );
3267 name = PRVM_G_STRING( OFS_PARM0 );
3268 VM_CheckEmptyString( name );
3269 w = PRVM_G_FLOAT( OFS_PARM1 );
3270 h = PRVM_G_FLOAT( OFS_PARM2 );
3272 instance = CL_Gecko_FindBrowser( name );
3276 CL_Gecko_Resize( instance, w, h );
3281 ========================
3282 VM_gecko_get_texture_extent
3284 vector gecko_get_texture_extent( string name )
3285 ========================
3287 void VM_gecko_get_texture_extent( void ) {
3289 clgecko_t *instance;
3291 VM_SAFEPARMCOUNT( 1, VM_gecko_movemouse );
3293 name = PRVM_G_STRING( OFS_PARM0 );
3294 VM_CheckEmptyString( name );
3296 PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
3297 instance = CL_Gecko_FindBrowser( name );
3299 PRVM_G_VECTOR(OFS_RETURN)[0] = 0;
3300 PRVM_G_VECTOR(OFS_RETURN)[1] = 0;
3303 CL_Gecko_GetTextureExtent( instance,
3304 PRVM_G_VECTOR(OFS_RETURN), PRVM_G_VECTOR(OFS_RETURN)+1 );
3314 Writes new values for v_forward, v_up, and v_right based on angles
3315 void makevectors(vector angle)
3318 void VM_makevectors (void)
3320 prvm_eval_t *valforward, *valright, *valup;
3321 valforward = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_forward);
3322 valright = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_right);
3323 valup = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_up);
3324 if (!valforward || !valright || !valup)
3326 VM_Warning("makevectors: could not find v_forward, v_right, or v_up global variables\n");
3329 VM_SAFEPARMCOUNT(1, VM_makevectors);
3330 AngleVectors (PRVM_G_VECTOR(OFS_PARM0), valforward->vector, valright->vector, valup->vector);
3337 Writes new values for v_forward, v_up, and v_right based on the given forward vector
3338 vectorvectors(vector)
3341 void VM_vectorvectors (void)
3343 prvm_eval_t *valforward, *valright, *valup;
3344 valforward = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_forward);
3345 valright = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_right);
3346 valup = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_up);
3347 if (!valforward || !valright || !valup)
3349 VM_Warning("vectorvectors: could not find v_forward, v_right, or v_up global variables\n");
3352 VM_SAFEPARMCOUNT(1, VM_vectorvectors);
3353 VectorNormalize2(PRVM_G_VECTOR(OFS_PARM0), valforward->vector);
3354 VectorVectors(valforward->vector, valright->vector, valup->vector);
3358 ========================
3361 void drawline(float width, vector pos1, vector pos2, vector rgb, float alpha, float flags)
3362 ========================
3364 void VM_drawline (void)
3366 float *c1, *c2, *rgb;
3368 unsigned char flags;
3370 VM_SAFEPARMCOUNT(6, VM_drawline);
3371 width = PRVM_G_FLOAT(OFS_PARM0);
3372 c1 = PRVM_G_VECTOR(OFS_PARM1);
3373 c2 = PRVM_G_VECTOR(OFS_PARM2);
3374 rgb = PRVM_G_VECTOR(OFS_PARM3);
3375 alpha = PRVM_G_FLOAT(OFS_PARM4);
3376 flags = (int)PRVM_G_FLOAT(OFS_PARM5);
3377 DrawQ_Line(width, c1[0], c1[1], c2[0], c2[1], rgb[0], rgb[1], rgb[2], alpha, flags);
3380 // float(float number, float quantity) bitshift (EXT_BITSHIFT)
3381 void VM_bitshift (void)
3384 VM_SAFEPARMCOUNT(2, VM_bitshift);
3386 n1 = (int)fabs((int)PRVM_G_FLOAT(OFS_PARM0));
3387 n2 = (int)PRVM_G_FLOAT(OFS_PARM1);
3389 PRVM_G_FLOAT(OFS_RETURN) = n1;
3392 PRVM_G_FLOAT(OFS_RETURN) = (n1 >> -n2);
3394 PRVM_G_FLOAT(OFS_RETURN) = (n1 << n2);
3397 ////////////////////////////////////////
3398 // AltString functions
3399 ////////////////////////////////////////
3402 ========================
3405 float altstr_count(string)
3406 ========================
3408 void VM_altstr_count( void )
3410 const char *altstr, *pos;
3413 VM_SAFEPARMCOUNT( 1, VM_altstr_count );
3415 altstr = PRVM_G_STRING( OFS_PARM0 );
3416 //VM_CheckEmptyString( altstr );
3418 for( count = 0, pos = altstr ; *pos ; pos++ ) {
3419 if( *pos == '\\' ) {
3423 } else if( *pos == '\'' ) {
3428 PRVM_G_FLOAT( OFS_RETURN ) = (float) (count / 2);
3432 ========================
3435 string altstr_prepare(string)
3436 ========================
3438 void VM_altstr_prepare( void )
3441 const char *instr, *in;
3443 char outstr[VM_STRINGTEMP_LENGTH];
3445 VM_SAFEPARMCOUNT( 1, VM_altstr_prepare );
3447 instr = PRVM_G_STRING( OFS_PARM0 );
3449 for( out = outstr, in = instr, size = sizeof(outstr) - 1 ; size && *in ; size--, in++, out++ )
3458 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
3462 ========================
3465 string altstr_get(string, float)
3466 ========================
3468 void VM_altstr_get( void )
3470 const char *altstr, *pos;
3473 char outstr[VM_STRINGTEMP_LENGTH];
3475 VM_SAFEPARMCOUNT( 2, VM_altstr_get );
3477 altstr = PRVM_G_STRING( OFS_PARM0 );
3479 count = (int)PRVM_G_FLOAT( OFS_PARM1 );
3480 count = count * 2 + 1;
3482 for( pos = altstr ; *pos && count ; pos++ )
3483 if( *pos == '\\' ) {
3486 } else if( *pos == '\'' )
3490 PRVM_G_INT( OFS_RETURN ) = 0;
3494 for( out = outstr, size = sizeof(outstr) - 1 ; size && *pos ; size--, pos++, out++ )
3495 if( *pos == '\\' ) {
3500 } else if( *pos == '\'' )
3506 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
3510 ========================
3513 string altstr_set(string altstr, float num, string set)
3514 ========================
3516 void VM_altstr_set( void )
3519 const char *altstr, *str;
3522 char outstr[VM_STRINGTEMP_LENGTH];
3524 VM_SAFEPARMCOUNT( 3, VM_altstr_set );
3526 altstr = PRVM_G_STRING( OFS_PARM0 );
3528 num = (int)PRVM_G_FLOAT( OFS_PARM1 );
3530 str = PRVM_G_STRING( OFS_PARM2 );
3533 for( num = num * 2 + 1, in = altstr; *in && num; *out++ = *in++ )
3538 } else if( *in == '\'' ) {
3543 for( ; *str; *out++ = *str++ );
3544 // now jump over the old content
3546 if( *in == '\'' || (*in == '\\' && !*++in) )
3549 strlcpy(out, in, outstr + sizeof(outstr) - out);
3550 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
3554 ========================
3557 string altstr_ins(string altstr, float num, string set)
3558 ========================
3560 void VM_altstr_ins(void)
3568 char outstr[VM_STRINGTEMP_LENGTH];
3570 VM_SAFEPARMCOUNT(3, VM_altstr_ins);
3572 in = instr = PRVM_G_STRING( OFS_PARM0 );
3573 num = (int)PRVM_G_FLOAT( OFS_PARM1 );
3574 set = setstr = PRVM_G_STRING( OFS_PARM2 );
3577 for( num = num * 2 + 2 ; *in && num > 0 ; *out++ = *in++ )
3582 } else if( *in == '\'' ) {
3587 for( ; *set ; *out++ = *set++ );
3590 strlcpy(out, in, outstr + sizeof(outstr) - out);
3591 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
3595 ////////////////////////////////////////
3596 // BufString functions
3597 ////////////////////////////////////////
3598 //[515]: string buffers support
3599 #define MAX_QCSTR_BUFFERS 128
3600 #define MAX_QCSTR_STRINGS 1024
3605 char *strings[MAX_QCSTR_STRINGS];
3608 // FIXME: move stringbuffers to prog_t to allow multiple progs!
3609 static qcstrbuffer_t *qcstringbuffers[MAX_QCSTR_BUFFERS];
3610 static int num_qcstringbuffers;
3611 static int buf_sortpower;
3613 #define BUFSTR_BUFFER(a) (a>=MAX_QCSTR_BUFFERS) ? NULL : (qcstringbuffers[a])
3614 #define BUFSTR_ISFREE(a) (a<MAX_QCSTR_BUFFERS&&qcstringbuffers[a]&&qcstringbuffers[a]->num_strings<=0) ? 1 : 0
3616 static int BufStr_FindFreeBuffer (void)
3619 if(num_qcstringbuffers == MAX_QCSTR_BUFFERS)
3621 for(i=0;i<MAX_QCSTR_BUFFERS;i++)
3622 if(!qcstringbuffers[i])
3624 qcstringbuffers[i] = (qcstrbuffer_t *)Z_Malloc(sizeof(qcstrbuffer_t));
3625 memset(qcstringbuffers[i], 0, sizeof(qcstrbuffer_t));
3631 static void BufStr_ClearBuffer (int index)
3633 qcstrbuffer_t *b = qcstringbuffers[index];
3638 if(b->num_strings > 0)
3640 for(i=0;i<b->num_strings;i++)
3642 Z_Free(b->strings[i]);
3643 num_qcstringbuffers--;
3645 Z_Free(qcstringbuffers[index]);
3646 qcstringbuffers[index] = NULL;
3650 static int BufStr_FindFreeString (qcstrbuffer_t *b)
3653 for(i=0;i<b->num_strings;i++)
3654 if(!b->strings[i] || !b->strings[i][0])
3656 if(i == MAX_QCSTR_STRINGS) return -1;
3660 static int BufStr_SortStringsUP (const void *in1, const void *in2)
3663 a = *((const char **) in1);
3664 b = *((const char **) in2);
3666 if(!b[0]) return -1;
3667 return strncmp(a, b, buf_sortpower);
3670 static int BufStr_SortStringsDOWN (const void *in1, const void *in2)
3673 a = *((const char **) in1);
3674 b = *((const char **) in2);
3676 if(!b[0]) return -1;
3677 return strncmp(b, a, buf_sortpower);
3681 ========================
3683 creates new buffer, and returns it's index, returns -1 if failed
3684 float buf_create(void) = #460;
3685 ========================
3687 void VM_buf_create (void)
3690 VM_SAFEPARMCOUNT(0, VM_buf_create);
3691 i = BufStr_FindFreeBuffer();
3693 num_qcstringbuffers++;
3695 //Con_Printf("VM_buf_create: buffers overflow in %s\n", PRVM_NAME);
3696 PRVM_G_FLOAT(OFS_RETURN) = i;
3700 ========================
3702 deletes buffer and all strings in it
3703 void buf_del(float bufhandle) = #461;
3704 ========================
3706 void VM_buf_del (void)
3708 VM_SAFEPARMCOUNT(1, VM_buf_del);
3709 if(BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0)))
3710 BufStr_ClearBuffer((int)PRVM_G_FLOAT(OFS_PARM0));
3713 VM_Warning("VM_buf_del: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3719 ========================
3721 how many strings are stored in buffer
3722 float buf_getsize(float bufhandle) = #462;
3723 ========================
3725 void VM_buf_getsize (void)
3728 VM_SAFEPARMCOUNT(1, VM_buf_getsize);
3730 b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3733 PRVM_G_FLOAT(OFS_RETURN) = -1;
3734 VM_Warning("VM_buf_getsize: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3738 PRVM_G_FLOAT(OFS_RETURN) = b->num_strings;
3742 ========================
3744 copy all content from one buffer to another, make sure it exists
3745 void buf_copy(float bufhandle_from, float bufhandle_to) = #463;
3746 ========================
3748 void VM_buf_copy (void)
3750 qcstrbuffer_t *b1, *b2;
3752 VM_SAFEPARMCOUNT(2, VM_buf_copy);
3754 b1 = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3757 VM_Warning("VM_buf_copy: invalid source buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3760 i = (int)PRVM_G_FLOAT(OFS_PARM1);
3761 if(i == (int)PRVM_G_FLOAT(OFS_PARM0))
3763 VM_Warning("VM_buf_copy: source == destination (%i) in %s\n", i, PRVM_NAME);
3766 b2 = BUFSTR_BUFFER(i);
3769 VM_Warning("VM_buf_copy: invalid destination buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM1), PRVM_NAME);
3773 BufStr_ClearBuffer(i);
3774 qcstringbuffers[i] = (qcstrbuffer_t *)Z_Malloc(sizeof(qcstrbuffer_t));
3775 memset(qcstringbuffers[i], 0, sizeof(qcstrbuffer_t));
3776 b2->num_strings = b1->num_strings;
3778 for(i=0;i<b1->num_strings;i++)
3779 if(b1->strings[i] && b1->strings[i][0])
3782 stringlen = strlen(b1->strings[i]) + 1;
3783 b2->strings[i] = (char *)Z_Malloc(stringlen);
3786 VM_Warning("VM_buf_copy: not enough memory for buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM1), PRVM_NAME);
3789 memcpy(b2->strings[i], b1->strings[i], stringlen);
3794 ========================
3796 sort buffer by beginnings of strings (cmplength defaults it's length)
3797 "backward == TRUE" means that sorting goes upside-down
3798 void buf_sort(float bufhandle, float cmplength, float backward) = #464;
3799 ========================
3801 void VM_buf_sort (void)
3805 VM_SAFEPARMCOUNT(3, VM_buf_sort);
3807 b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3810 VM_Warning("VM_buf_sort: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3813 if(b->num_strings <= 0)
3815 VM_Warning("VM_buf_sort: tried to sort empty buffer %i in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3818 // TODO: please someone rename this to buf_cmplength [12/3/2007 Black]
3819 buf_sortpower = (int)PRVM_G_FLOAT(OFS_PARM1);
3820 if(buf_sortpower <= 0)
3821 buf_sortpower = 99999999;
3823 if(!PRVM_G_FLOAT(OFS_PARM2))
3824 qsort(b->strings, b->num_strings, sizeof(char*), BufStr_SortStringsUP);
3826 qsort(b->strings, b->num_strings, sizeof(char*), BufStr_SortStringsDOWN);
3828 for(i=b->num_strings-1;i>=0;i--) //[515]: delete empty lines
3831 if(b->strings[i][0])
3835 Z_Free(b->strings[i]);
3837 b->strings[i] = NULL;
3845 ========================
3847 concantenates all buffer string into one with "glue" separator and returns it as tempstring
3848 string buf_implode(float bufhandle, string glue) = #465;
3849 ========================
3851 void VM_buf_implode (void)
3854 char k[VM_STRINGTEMP_LENGTH];
3858 VM_SAFEPARMCOUNT(2, VM_buf_implode);
3860 b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3861 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
3864 VM_Warning("VM_buf_implode: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3869 sep = PRVM_G_STRING(OFS_PARM1);
3871 for(l=i=0;i<b->num_strings;i++)
3874 l += (i > 0 ? strlen(sep) : 0) + strlen(b->strings[i]);
3875 if (l >= sizeof(k) - 1)
3877 strlcat(k, sep, sizeof(k));
3878 strlcat(k, b->strings[i], sizeof(k));
3880 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(k);
3884 ========================
3886 get a string from buffer, returns tempstring, dont str_unzone it!
3887 string bufstr_get(float bufhandle, float string_index) = #465;
3888 ========================
3890 void VM_bufstr_get (void)
3894 VM_SAFEPARMCOUNT(2, VM_bufstr_get);
3896 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
3897 b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3900 VM_Warning("VM_bufstr_get: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3903 strindex = (int)PRVM_G_FLOAT(OFS_PARM1);
3904 if(strindex < 0 || strindex > MAX_QCSTR_STRINGS)
3906 VM_Warning("VM_bufstr_get: invalid string index %i used in %s\n", strindex, PRVM_NAME);
3909 if(b->num_strings <= strindex)
3911 if(b->strings[strindex])
3912 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(b->strings[strindex]);
3916 ========================
3918 copies a string into selected slot of buffer
3919 void bufstr_set(float bufhandle, float string_index, string str) = #466;
3920 ========================
3922 void VM_bufstr_set (void)
3924 int bufindex, strindex;
3929 VM_SAFEPARMCOUNT(3, VM_bufstr_set);
3931 bufindex = (int)PRVM_G_FLOAT(OFS_PARM0);
3932 b = BUFSTR_BUFFER(bufindex);
3935 VM_Warning("VM_bufstr_set: invalid buffer %i used in %s\n", bufindex, PRVM_NAME);
3938 strindex = (int)PRVM_G_FLOAT(OFS_PARM1);
3939 if(strindex < 0 || strindex > MAX_QCSTR_STRINGS)
3941 VM_Warning("VM_bufstr_set: invalid string index %i used in %s\n", strindex, PRVM_NAME);
3944 news = PRVM_G_STRING(OFS_PARM2);
3945 if(b->strings[strindex])
3946 Z_Free(b->strings[strindex]);
3947 alloclen = strlen(news) + 1;
3948 b->strings[strindex] = (char *)Z_Malloc(alloclen);
3949 memcpy(b->strings[strindex], news, alloclen);
3953 ========================
3955 adds string to buffer in nearest free slot and returns it
3956 "order == TRUE" means that string will be added after last "full" slot
3957 float bufstr_add(float bufhandle, string str, float order) = #467;
3958 ========================
3960 void VM_bufstr_add (void)
3962 int bufindex, order, strindex;
3967 VM_SAFEPARMCOUNT(3, VM_bufstr_add);
3969 bufindex = (int)PRVM_G_FLOAT(OFS_PARM0);
3970 b = BUFSTR_BUFFER(bufindex);
3971 PRVM_G_FLOAT(OFS_RETURN) = -1;
3974 VM_Warning("VM_bufstr_add: invalid buffer %i used in %s\n", bufindex, PRVM_NAME);
3977 string = PRVM_G_STRING(OFS_PARM1);
3978 order = (int)PRVM_G_FLOAT(OFS_PARM2);
3980 strindex = b->num_strings;
3983 strindex = BufStr_FindFreeString(b);
3986 VM_Warning("VM_bufstr_add: buffer %i has no free string slots in %s\n", bufindex, PRVM_NAME);
3991 while(b->num_strings <= strindex)
3993 if(b->num_strings == MAX_QCSTR_STRINGS)
3995 VM_Warning("VM_bufstr_add: buffer %i has no free string slots in %s\n", bufindex, PRVM_NAME);
3998 b->strings[b->num_strings] = NULL;
4001 if(b->strings[strindex])
4002 Z_Free(b->strings[strindex]);
4003 alloclen = strlen(string) + 1;
4004 b->strings[strindex] = (char *)Z_Malloc(alloclen);
4005 memcpy(b->strings[strindex], string, alloclen);
4006 PRVM_G_FLOAT(OFS_RETURN) = strindex;
4010 ========================
4012 delete string from buffer
4013 void bufstr_free(float bufhandle, float string_index) = #468;
4014 ========================
4016 void VM_bufstr_free (void)
4020 VM_SAFEPARMCOUNT(2, VM_bufstr_free);
4022 b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
4025 VM_Warning("VM_bufstr_free: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4028 i = (int)PRVM_G_FLOAT(OFS_PARM1);
4029 if(i < 0 || i > MAX_QCSTR_STRINGS)
4031 VM_Warning("VM_bufstr_free: invalid string index %i used in %s\n", i, PRVM_NAME);
4035 Z_Free(b->strings[i]);
4036 b->strings[i] = NULL;
4037 if(i+1 == b->num_strings)
4047 This was a major timewaster in progs, so it was converted to C
4050 void VM_changeyaw (void)
4053 float ideal, current, move, speed;
4055 // this is called (VERY HACKISHLY) by SV_MoveToGoal, so it can not use any
4056 // parameters because they are the parameters to SV_MoveToGoal, not this
4057 //VM_SAFEPARMCOUNT(0, VM_changeyaw);
4059 ent = PRVM_PROG_TO_EDICT(PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict);
4060 if (ent == prog->edicts)
4062 VM_Warning("changeyaw: can not modify world entity\n");
4065 if (ent->priv.server->free)
4067 VM_Warning("changeyaw: can not modify free entity\n");
4070 if (prog->fieldoffsets.angles < 0 || prog->fieldoffsets.ideal_yaw < 0 || prog->fieldoffsets.yaw_speed < 0)
4072 VM_Warning("changeyaw: angles, ideal_yaw, or yaw_speed field(s) not found\n");
4075 current = ANGLEMOD(PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.angles)->vector[1]);
4076 ideal = PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.ideal_yaw)->_float;
4077 speed = PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.yaw_speed)->_float;
4079 if (current == ideal)
4081 move = ideal - current;
4082 if (ideal > current)
4103 PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.angles)->vector[1] = ANGLEMOD (current + move);
4111 void VM_changepitch (void)
4114 float ideal, current, move, speed;
4116 VM_SAFEPARMCOUNT(1, VM_changepitch);
4118 ent = PRVM_G_EDICT(OFS_PARM0);
4119 if (ent == prog->edicts)
4121 VM_Warning("changepitch: can not modify world entity\n");
4124 if (ent->priv.server->free)
4126 VM_Warning("changepitch: can not modify free entity\n");
4129 if (prog->fieldoffsets.angles < 0 || prog->fieldoffsets.idealpitch < 0 || prog->fieldoffsets.pitch_speed < 0)
4131 VM_Warning("changepitch: angles, idealpitch, or pitch_speed field(s) not found\n");
4134 current = ANGLEMOD(PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.angles)->vector[0]);
4135 ideal = PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.idealpitch)->_float;
4136 speed = PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.pitch_speed)->_float;
4138 if (current == ideal)
4140 move = ideal - current;
4141 if (ideal > current)
4162 PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.angles)->vector[0] = ANGLEMOD (current + move);
4165 // TODO: adapt all static function names to use a single naming convention... [12/3/2007 Black]
4166 static int Is_Text_Color (char c, char t)
4169 char c2 = c - (c & 128);
4170 char t2 = t - (t & 128);
4172 if(c != STRING_COLOR_TAG && c2 != STRING_COLOR_TAG) return 0;
4173 if(t >= '0' && t <= '9') a = 1;
4174 if(t2 >= '0' && t2 <= '9') a = 1;
4175 /* if(t >= 'A' && t <= 'Z') a = 2;
4176 if(t2 >= 'A' && t2 <= 'Z') a = 2;
4178 if(a == 1 && scr_colortext.integer > 0)
4180 if(a == 2 && scr_multifonts.integer > 0)
4186 void VM_uncolorstring (void)
4189 char out[VM_STRINGTEMP_LENGTH];
4192 VM_SAFEPARMCOUNT(1, VM_uncolorstring);
4193 in = PRVM_G_STRING(OFS_PARM0);
4194 VM_CheckEmptyString (in);
4199 if(Is_Text_Color(in[k], in[k+1]) == 1/* || (in[k] == '&' && in[k+1] == 'r')*/)
4208 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(out);
4211 // #221 float(string str, string sub[, float startpos]) strstrofs (FTE_STRINGS)
4212 //strstr, without generating a new string. Use in conjunction with FRIK_FILE's substring for more similar strstr.
4213 void VM_strstrofs (void)
4215 const char *instr, *match;
4217 VM_SAFEPARMCOUNTRANGE(2, 3, VM_strstrofs);
4218 instr = PRVM_G_STRING(OFS_PARM0);
4219 match = PRVM_G_STRING(OFS_PARM1);
4220 firstofs = (prog->argc > 2)?PRVM_G_FLOAT(OFS_PARM2):0;
4222 if (firstofs && (firstofs < 0 || firstofs > (int)strlen(instr)))
4224 PRVM_G_FLOAT(OFS_RETURN) = -1;
4228 match = strstr(instr+firstofs, match);
4230 PRVM_G_FLOAT(OFS_RETURN) = -1;
4232 PRVM_G_FLOAT(OFS_RETURN) = match - instr;
4235 //#222 string(string s, float index) str2chr (FTE_STRINGS)
4236 void VM_str2chr (void)
4239 VM_SAFEPARMCOUNT(2, VM_str2chr);
4240 s = PRVM_G_STRING(OFS_PARM0);
4241 if((unsigned)PRVM_G_FLOAT(OFS_PARM1) < strlen(s))
4242 PRVM_G_FLOAT(OFS_RETURN) = (unsigned char)s[(unsigned)PRVM_G_FLOAT(OFS_PARM1)];
4244 PRVM_G_FLOAT(OFS_RETURN) = 0;
4247 //#223 string(float c, ...) chr2str (FTE_STRINGS)
4248 void VM_chr2str (void)
4252 VM_SAFEPARMCOUNTRANGE(0, 8, VM_chr2str);
4253 for(i = 0;i < prog->argc && i < (int)sizeof(t) - 1;i++)
4254 t[i] = (unsigned char)PRVM_G_FLOAT(OFS_PARM0+i*3);
4256 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(t);
4259 static int chrconv_number(int i, int base, int conv)
4284 static int chrconv_punct(int i, int base, int conv)
4302 static int chrchar_alpha(int i, int basec, int baset, int convc, int convt, int charnum)
4304 //convert case and colour seperatly...
4321 baset = 128*((charnum&1) == (convt-5));
4337 return i + basec + baset;
4339 // #224 string(float ccase, float calpha, float cnum, string s, ...) strconv (FTE_STRINGS)
4340 //bulk convert a string. change case or colouring.
4341 void VM_strconv (void)
4343 int ccase, redalpha, rednum, len, i;
4344 unsigned char resbuf[VM_STRINGTEMP_LENGTH];
4345 unsigned char *result = resbuf;
4347 VM_SAFEPARMCOUNTRANGE(3, 8, VM_strconv);
4349 ccase = PRVM_G_FLOAT(OFS_PARM0); //0 same, 1 lower, 2 upper
4350 redalpha = PRVM_G_FLOAT(OFS_PARM1); //0 same, 1 white, 2 red, 5 alternate, 6 alternate-alternate
4351 rednum = PRVM_G_FLOAT(OFS_PARM2); //0 same, 1 white, 2 red, 3 redspecial, 4 whitespecial, 5 alternate, 6 alternate-alternate
4352 VM_VarString(3, (char *) resbuf, sizeof(resbuf));
4353 len = strlen((char *) resbuf);
4355 for (i = 0; i < len; i++, result++) //should this be done backwards?
4357 if (*result >= '0' && *result <= '9') //normal numbers...
4358 *result = chrconv_number(*result, '0', rednum);
4359 else if (*result >= '0'+128 && *result <= '9'+128)
4360 *result = chrconv_number(*result, '0'+128, rednum);
4361 else if (*result >= '0'+128-30 && *result <= '9'+128-30)
4362 *result = chrconv_number(*result, '0'+128-30, rednum);
4363 else if (*result >= '0'-30 && *result <= '9'-30)
4364 *result = chrconv_number(*result, '0'-30, rednum);
4366 else if (*result >= 'a' && *result <= 'z') //normal numbers...
4367 *result = chrchar_alpha(*result, 'a', 0, ccase, redalpha, i);
4368 else if (*result >= 'A' && *result <= 'Z') //normal numbers...
4369 *result = chrchar_alpha(*result, 'A', 0, ccase, redalpha, i);
4370 else if (*result >= 'a'+128 && *result <= 'z'+128) //normal numbers...
4371 *result = chrchar_alpha(*result, 'a', 128, ccase, redalpha, i);
4372 else if (*result >= 'A'+128 && *result <= 'Z'+128) //normal numbers...
4373 *result = chrchar_alpha(*result, 'A', 128, ccase, redalpha, i);
4375 else if ((*result & 127) < 16 || !redalpha) //special chars..
4377 else if (*result < 128)
4378 *result = chrconv_punct(*result, 0, redalpha);
4380 *result = chrconv_punct(*result, 128, redalpha);
4384 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString((char *) resbuf);
4387 // #225 string(float chars, string s, ...) strpad (FTE_STRINGS)
4388 void VM_strpad (void)
4390 char src[VM_STRINGTEMP_LENGTH];
4391 char destbuf[VM_STRINGTEMP_LENGTH];
4393 VM_SAFEPARMCOUNTRANGE(1, 8, VM_strpad);
4394 pad = PRVM_G_FLOAT(OFS_PARM0);
4395 VM_VarString(1, src, sizeof(src));
4397 // note: < 0 = left padding, > 0 = right padding,
4398 // this is reverse logic of printf!
4399 dpsnprintf(destbuf, sizeof(destbuf), "%*s", -pad, src);
4401 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(destbuf);
4404 // #226 string(string info, string key, string value, ...) infoadd (FTE_STRINGS)
4405 //uses qw style \key\value strings
4406 void VM_infoadd (void)
4408 const char *info, *key;
4409 char value[VM_STRINGTEMP_LENGTH];
4410 char temp[VM_STRINGTEMP_LENGTH];
4412 VM_SAFEPARMCOUNTRANGE(2, 8, VM_infoadd);
4413 info = PRVM_G_STRING(OFS_PARM0);
4414 key = PRVM_G_STRING(OFS_PARM1);
4415 VM_VarString(2, value, sizeof(value));
4417 strlcpy(temp, info, VM_STRINGTEMP_LENGTH);
4419 InfoString_SetValue(temp, VM_STRINGTEMP_LENGTH, key, value);
4421 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(temp);
4424 // #227 string(string info, string key) infoget (FTE_STRINGS)
4425 //uses qw style \key\value strings
4426 void VM_infoget (void)
4430 char value[VM_STRINGTEMP_LENGTH];
4432 VM_SAFEPARMCOUNT(2, VM_infoget);
4433 info = PRVM_G_STRING(OFS_PARM0);
4434 key = PRVM_G_STRING(OFS_PARM1);
4436 InfoString_GetValue(info, key, value, VM_STRINGTEMP_LENGTH);
4438 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(value);
4441 //#228 float(string s1, string s2, float len) strncmp (FTE_STRINGS)
4442 // also float(string s1, string s2) strcmp (FRIK_FILE)
4443 void VM_strncmp (void)
4445 const char *s1, *s2;
4446 VM_SAFEPARMCOUNTRANGE(2, 3, VM_strncmp);
4447 s1 = PRVM_G_STRING(OFS_PARM0);
4448 s2 = PRVM_G_STRING(OFS_PARM1);
4451 PRVM_G_FLOAT(OFS_RETURN) = strncmp(s1, s2, (size_t)PRVM_G_FLOAT(OFS_PARM2));
4455 PRVM_G_FLOAT(OFS_RETURN) = strcmp(s1, s2);
4459 // #229 float(string s1, string s2) strcasecmp (FTE_STRINGS)
4460 // #230 float(string s1, string s2, float len) strncasecmp (FTE_STRINGS)
4461 void VM_strncasecmp (void)
4463 const char *s1, *s2;
4464 VM_SAFEPARMCOUNTRANGE(2, 3, VM_strncasecmp);
4465 s1 = PRVM_G_STRING(OFS_PARM0);
4466 s2 = PRVM_G_STRING(OFS_PARM1);
4469 PRVM_G_FLOAT(OFS_RETURN) = strncasecmp(s1, s2, (size_t)PRVM_G_FLOAT(OFS_PARM2));
4473 PRVM_G_FLOAT(OFS_RETURN) = strcasecmp(s1, s2);
4477 void VM_wasfreed (void)
4479 VM_SAFEPARMCOUNT(1, VM_wasfreed);
4480 PRVM_G_FLOAT(OFS_RETURN) = PRVM_G_EDICT(OFS_PARM0)->priv.required->free;
4483 void VM_SetTraceGlobals(const trace_t *trace)
4486 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_allsolid)))
4487 val->_float = trace->allsolid;
4488 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_startsolid)))
4489 val->_float = trace->startsolid;
4490 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_fraction)))
4491 val->_float = trace->fraction;
4492 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_inwater)))
4493 val->_float = trace->inwater;
4494 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_inopen)))
4495 val->_float = trace->inopen;
4496 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_endpos)))
4497 VectorCopy(trace->endpos, val->vector);
4498 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_plane_normal)))
4499 VectorCopy(trace->plane.normal, val->vector);
4500 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_plane_dist)))
4501 val->_float = trace->plane.dist;
4502 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_ent)))
4503 val->edict = PRVM_EDICT_TO_PROG(trace->ent ? trace->ent : prog->edicts);
4504 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dpstartcontents)))
4505 val->_float = trace->startsupercontents;
4506 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dphitcontents)))
4507 val->_float = trace->hitsupercontents;
4508 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dphitq3surfaceflags)))
4509 val->_float = trace->hitq3surfaceflags;
4510 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dphittexturename)))
4511 val->string = trace->hittexture ? PRVM_SetTempString(trace->hittexture->name) : 0;
4516 void VM_Cmd_Init(void)
4518 // only init the stuff for the current prog
4521 #ifdef SUPPORT_GECKO
4524 // VM_BufStr_Init();
4527 void VM_Cmd_Reset(void)
4529 CL_PurgeOwner( MENUOWNER );
4531 VM_Files_CloseAll();
4532 #ifdef SUPPORT_GECKO
4535 // VM_BufStr_ShutDown();