2 // Basically every vm builtin cmd should be in here.
3 // All 3 builtin and extension lists can be found here
4 // cause large (I think they will) parts are from pr_cmds the same copyright like in pr_cmds
9 // LordHavoc: changed this to NOT use a return statement, so that it can be used in functions that must return a value
10 void VM_Warning(const char *fmt, ...)
13 char msg[MAX_INPUTLINE];
16 dpvsnprintf(msg,sizeof(msg),fmt,argptr);
24 //============================================================================
27 // temp string handling
28 // LordHavoc: added this to semi-fix the problem of using many ftos calls in a print
29 static char vm_string_temp[VM_STRINGTEMP_BUFFERS][VM_STRINGTEMP_LENGTH];
30 static int vm_string_tempindex = 0;
32 // TODO: move vm_files and vm_fssearchlist to prvm_prog_t struct
34 char *VM_GetTempString(void)
37 s = vm_string_temp[vm_string_tempindex];
38 vm_string_tempindex = (vm_string_tempindex + 1) % VM_STRINGTEMP_BUFFERS;
42 void VM_CheckEmptyString (const char *s)
45 PRVM_ERROR ("%s: Bad string", PRVM_NAME);
48 //============================================================================
51 void VM_VarString(int first, char *out, int outlength)
57 outend = out + outlength - 1;
58 for (i = first;i < prog->argc && out < outend;i++)
60 s = PRVM_G_STRING((OFS_PARM0+i*3));
61 while (out < outend && *s)
71 returns true if the extension is supported by the server
73 checkextension(extensionname)
77 // kind of helper function
78 static qboolean checkextension(const char *name)
82 len = (int)strlen(name);
84 for (e = prog->extensionstring;*e;e++)
91 while (*e && *e != ' ')
93 if ((e - start) == len && !strncasecmp(start, name, len))
99 void VM_checkextension (void)
101 VM_SAFEPARMCOUNT(1,VM_checkextension);
103 PRVM_G_FLOAT(OFS_RETURN) = checkextension(PRVM_G_STRING(OFS_PARM0));
110 This is a TERMINAL error, which will kill off the entire prog.
119 char string[VM_STRINGTEMP_LENGTH];
121 VM_VarString(0, string, sizeof(string));
122 Con_Printf("======%s ERROR in %s:\n%s\n", PRVM_NAME, PRVM_GetString(prog->xfunction->s_name), string);
125 ed = PRVM_G_EDICT(prog->self->ofs);
129 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);
136 Dumps out self, then an error message. The program is aborted and self is
137 removed, but the level can continue.
142 void VM_objerror (void)
145 char string[VM_STRINGTEMP_LENGTH];
147 VM_VarString(0, string, sizeof(string));
148 Con_Printf("======OBJECT ERROR======\n", PRVM_NAME, PRVM_GetString(prog->xfunction->s_name), string);
151 ed = PRVM_G_EDICT (prog->self->ofs);
157 // objerror has to display the object fields -> else call
158 PRVM_ERROR ("VM_objecterror: self not defined !");
159 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);
164 VM_print (actually used only by client and menu)
173 char string[VM_STRINGTEMP_LENGTH];
175 VM_VarString(0, string, sizeof(string));
183 broadcast print to everyone on server
188 void VM_bprint (void)
190 char string[VM_STRINGTEMP_LENGTH];
194 VM_Warning("VM_bprint: game is not server(%s) !\n", PRVM_NAME);
198 VM_VarString(0, string, sizeof(string));
199 SV_BroadcastPrint(string);
204 VM_sprint (menu & client but only if server.active == true)
206 single print to a specific client
208 sprint(float clientnum,...[string])
211 void VM_sprint (void)
215 char string[VM_STRINGTEMP_LENGTH];
217 //find client for this entity
218 clientnum = (int)PRVM_G_FLOAT(OFS_PARM0);
219 if (!sv.active || clientnum < 0 || clientnum >= svs.maxclients || !svs.clients[clientnum].active)
221 VM_Warning("VM_sprint: %s: invalid client or server is not active !\n", PRVM_NAME);
225 client = svs.clients + clientnum;
226 if (!client->netconnection)
229 VM_VarString(1, string, sizeof(string));
230 MSG_WriteChar(&client->netconnection->message,svc_print);
231 MSG_WriteString(&client->netconnection->message, string);
238 single print to the screen
240 centerprint(clientent, value)
243 void VM_centerprint (void)
245 char string[VM_STRINGTEMP_LENGTH];
247 VM_VarString(0, string, sizeof(string));
248 SCR_CenterPrint(string);
255 vector normalize(vector)
258 void VM_normalize (void)
264 VM_SAFEPARMCOUNT(1,VM_normalize);
266 value1 = PRVM_G_VECTOR(OFS_PARM0);
268 f = VectorLength2(value1);
272 VectorScale(value1, f, newvalue);
275 VectorClear(newvalue);
277 VectorCopy (newvalue, PRVM_G_VECTOR(OFS_RETURN));
289 VM_SAFEPARMCOUNT(1,VM_vlen);
290 PRVM_G_FLOAT(OFS_RETURN) = VectorLength(PRVM_G_VECTOR(OFS_PARM0));
297 float vectoyaw(vector)
300 void VM_vectoyaw (void)
305 VM_SAFEPARMCOUNT(1,VM_vectoyaw);
307 value1 = PRVM_G_VECTOR(OFS_PARM0);
309 if (value1[1] == 0 && value1[0] == 0)
313 yaw = (int) (atan2(value1[1], value1[0]) * 180 / M_PI);
318 PRVM_G_FLOAT(OFS_RETURN) = yaw;
326 vector vectoangles(vector)
329 void VM_vectoangles (void)
335 VM_SAFEPARMCOUNT(1,VM_vectoangles);
337 value1 = PRVM_G_VECTOR(OFS_PARM0);
339 if (value1[1] == 0 && value1[0] == 0)
349 // LordHavoc: optimized a bit
352 yaw = (atan2(value1[1], value1[0]) * 180 / M_PI);
356 else if (value1[1] > 0)
361 forward = sqrt(value1[0]*value1[0] + value1[1]*value1[1]);
362 pitch = (atan2(value1[2], forward) * 180 / M_PI);
367 PRVM_G_FLOAT(OFS_RETURN+0) = pitch;
368 PRVM_G_FLOAT(OFS_RETURN+1) = yaw;
369 PRVM_G_FLOAT(OFS_RETURN+2) = 0;
376 Returns a number from 0<= num < 1
381 void VM_random (void)
383 VM_SAFEPARMCOUNT(0,VM_random);
385 PRVM_G_FLOAT(OFS_RETURN) = lhrandom(0, 1);
392 Each entity can have eight independant sound sources, like voice,
395 Channel 0 is an auto-allocate channel, the others override anything
396 already running on that entity/channel pair.
398 An attenuation of 0 will play full volume everywhere in the level.
399 Larger attenuations will drop off.
408 prvm_edict_t *entity;
412 entity = PRVM_G_EDICT(OFS_PARM0);
413 channel = PRVM_G_FLOAT(OFS_PARM1);
414 sample = PRVM_G_STRING(OFS_PARM2);
415 volume = PRVM_G_FLOAT(OFS_PARM3) * 255;
416 attenuation = PRVM_G_FLOAT(OFS_PARM4);
418 if (volume < 0 || volume > 255)
419 Host_Error ("SV_StartSound: volume = %i", volume);
421 if (attenuation < 0 || attenuation > 4)
422 Host_Error ("SV_StartSound: attenuation = %f", attenuation);
424 if (channel < 0 || channel > 7)
425 Host_Error ("SV_StartSound: channel = %i", channel);
427 SV_StartSound (entity, channel, sample, volume, attenuation);
435 localsound(string sample)
438 void VM_localsound(void)
442 VM_SAFEPARMCOUNT(1,VM_localsound);
444 s = PRVM_G_STRING(OFS_PARM0);
446 if(!S_LocalSound (s))
448 PRVM_G_FLOAT(OFS_RETURN) = -4;
449 VM_Warning("VM_localsound: Failed to play %s for %s !\n", s, PRVM_NAME);
453 PRVM_G_FLOAT(OFS_RETURN) = 1;
465 PRVM_ERROR ("%s: break statement", PRVM_NAME);
468 //============================================================================
474 Sends text over to the client's execution buffer
476 [localcmd (string, ...) or]
480 void VM_localcmd (void)
482 char string[VM_STRINGTEMP_LENGTH];
483 VM_VarString(0, string, sizeof(string));
484 Cbuf_AddText(string);
496 VM_SAFEPARMCOUNT(1,VM_cvar);
498 PRVM_G_FLOAT(OFS_RETURN) = Cvar_VariableValue(PRVM_G_STRING(OFS_PARM0));
505 const string VM_cvar_string (string)
508 void VM_cvar_string(void)
512 const char *cvar_string;
513 VM_SAFEPARMCOUNT(1,VM_cvar_string);
515 name = PRVM_G_STRING(OFS_PARM0);
518 PRVM_ERROR("VM_cvar_string: %s: null string", PRVM_NAME);
520 VM_CheckEmptyString(name);
522 out = VM_GetTempString();
524 cvar_string = Cvar_VariableString(name);
526 strlcpy(out, cvar_string, VM_STRINGTEMP_LENGTH);
528 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(out);
533 ========================
536 const string VM_cvar_defstring (string)
537 ========================
539 void VM_cvar_defstring (void)
543 const char *cvar_string;
544 VM_SAFEPARMCOUNT(1,VM_cvar_string);
546 name = PRVM_G_STRING(OFS_PARM0);
549 PRVM_ERROR("VM_cvar_defstring: %s: null string", PRVM_NAME);
551 VM_CheckEmptyString(name);
553 out = VM_GetTempString();
555 cvar_string = Cvar_VariableDefString(name);
557 strlcpy(out, cvar_string, VM_STRINGTEMP_LENGTH);
559 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(out);
565 void cvar_set (string,string)
568 void VM_cvar_set (void)
570 VM_SAFEPARMCOUNT(2,VM_cvar_set);
572 Cvar_Set(PRVM_G_STRING(OFS_PARM0), PRVM_G_STRING(OFS_PARM1));
582 void VM_dprint (void)
584 char string[VM_STRINGTEMP_LENGTH];
585 if (developer.integer)
587 VM_VarString(0, string, sizeof(string));
589 Con_Printf("%s", string);
591 Con_Printf("%s: %s", PRVM_NAME, string);
609 VM_SAFEPARMCOUNT(1, VM_ftos);
611 v = PRVM_G_FLOAT(OFS_PARM0);
613 s = VM_GetTempString();
614 if ((float)((int)v) == v)
615 sprintf(s, "%i", (int)v);
618 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(s);
633 VM_SAFEPARMCOUNT(1,VM_fabs);
635 v = PRVM_G_FLOAT(OFS_PARM0);
636 PRVM_G_FLOAT(OFS_RETURN) = fabs(v);
651 VM_SAFEPARMCOUNT(1,VM_vtos);
653 s = VM_GetTempString();
654 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]);
655 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(s);
670 VM_SAFEPARMCOUNT(1, VM_etos);
672 s = VM_GetTempString();
673 sprintf (s, "entity %i", PRVM_G_EDICTNUM(OFS_PARM0));
674 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(s);
681 float stof(...[string])
686 char string[VM_STRINGTEMP_LENGTH];
687 VM_VarString(0, string, sizeof(string));
688 PRVM_G_FLOAT(OFS_RETURN) = atof(string);
692 ========================
696 ========================
700 VM_SAFEPARMCOUNT(1, VM_itof);
701 PRVM_G_FLOAT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
705 ========================
708 entity ftoe(float num)
709 ========================
714 VM_SAFEPARMCOUNT(1, VM_ftoe);
716 ent = (int)PRVM_G_FLOAT(OFS_PARM0);
717 if (ent < 0 || ent >= MAX_EDICTS || PRVM_PROG_TO_EDICT(ent)->priv.required->free)
718 ent = 0; // return world instead of a free or invalid entity
720 PRVM_G_INT(OFS_RETURN) = ent;
734 prog->xfunction->builtinsprofile += 20;
735 ed = PRVM_ED_Alloc();
747 void VM_remove (void)
750 prog->xfunction->builtinsprofile += 20;
752 VM_SAFEPARMCOUNT(1, VM_remove);
754 ed = PRVM_G_EDICT(OFS_PARM0);
755 if( PRVM_NUM_FOR_EDICT(ed) <= prog->reserved_edicts )
757 if (developer.integer >= 1)
758 VM_Warning( "VM_remove: tried to remove the null entity or a reserved entity!\n" );
760 else if( ed->priv.required->free )
762 if (developer.integer >= 1)
763 VM_Warning( "VM_remove: tried to remove an already freed entity!\n" );
767 // if (ed == prog->edicts)
768 // PRVM_ERROR ("remove: tried to remove world");
769 // if (PRVM_NUM_FOR_EDICT(ed) <= sv.maxclients)
770 // Host_Error("remove: tried to remove a client");
777 entity find(entity start, .string field, string match)
788 VM_SAFEPARMCOUNT(3,VM_find);
790 e = PRVM_G_EDICTNUM(OFS_PARM0);
791 f = PRVM_G_INT(OFS_PARM1);
792 s = PRVM_G_STRING(OFS_PARM2);
794 // LordHavoc: apparently BloodMage does a find(world, weaponmodel, "") and
795 // expects it to find all the monsters, so we must be careful to support
800 for (e++ ; e < prog->num_edicts ; e++)
802 prog->xfunction->builtinsprofile++;
803 ed = PRVM_EDICT_NUM(e);
804 if (ed->priv.required->free)
806 t = PRVM_E_STRING(ed,f);
816 VM_RETURN_EDICT(prog->edicts);
823 entity findfloat(entity start, .float field, float match)
824 entity findentity(entity start, .entity field, entity match)
827 // LordHavoc: added this for searching float, int, and entity reference fields
828 void VM_findfloat (void)
835 VM_SAFEPARMCOUNT(3,VM_findfloat);
837 e = PRVM_G_EDICTNUM(OFS_PARM0);
838 f = PRVM_G_INT(OFS_PARM1);
839 s = PRVM_G_FLOAT(OFS_PARM2);
841 for (e++ ; e < prog->num_edicts ; e++)
843 prog->xfunction->builtinsprofile++;
844 ed = PRVM_EDICT_NUM(e);
845 if (ed->priv.required->free)
847 if (PRVM_E_FLOAT(ed,f) == s)
854 VM_RETURN_EDICT(prog->edicts);
861 entity findchain(.string field, string match)
864 // chained search for strings in entity fields
865 // entity(.string field, string match) findchain = #402;
866 void VM_findchain (void)
872 prvm_edict_t *ent, *chain;
874 VM_SAFEPARMCOUNT(2,VM_findchain);
876 // is the same like !(prog->flag & PRVM_FE_CHAIN) - even if the operator precedence is another
877 if(!prog->flag & PRVM_FE_CHAIN)
878 PRVM_ERROR("VM_findchain: %s doesnt have a chain field !", PRVM_NAME);
880 chain_of = PRVM_ED_FindField("chain")->ofs;
882 chain = prog->edicts;
884 f = PRVM_G_INT(OFS_PARM0);
885 s = PRVM_G_STRING(OFS_PARM1);
887 // LordHavoc: apparently BloodMage does a find(world, weaponmodel, "") and
888 // expects it to find all the monsters, so we must be careful to support
893 ent = PRVM_NEXT_EDICT(prog->edicts);
894 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
896 prog->xfunction->builtinsprofile++;
897 if (ent->priv.required->free)
899 t = PRVM_E_STRING(ent,f);
905 PRVM_E_INT(ent,chain_of) = PRVM_NUM_FOR_EDICT(chain);
909 VM_RETURN_EDICT(chain);
916 entity findchainfloat(.string field, float match)
917 entity findchainentity(.string field, entity match)
920 // LordHavoc: chained search for float, int, and entity reference fields
921 // entity(.string field, float match) findchainfloat = #403;
922 void VM_findchainfloat (void)
928 prvm_edict_t *ent, *chain;
930 VM_SAFEPARMCOUNT(2, VM_findchainfloat);
932 if(!prog->flag & PRVM_FE_CHAIN)
933 PRVM_ERROR("VM_findchainfloat: %s doesnt have a chain field !", PRVM_NAME);
935 chain_of = PRVM_ED_FindField("chain")->ofs;
937 chain = (prvm_edict_t *)prog->edicts;
939 f = PRVM_G_INT(OFS_PARM0);
940 s = PRVM_G_FLOAT(OFS_PARM1);
942 ent = PRVM_NEXT_EDICT(prog->edicts);
943 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
945 prog->xfunction->builtinsprofile++;
946 if (ent->priv.required->free)
948 if (PRVM_E_FLOAT(ent,f) != s)
951 PRVM_E_INT(ent,chain_of) = PRVM_EDICT_TO_PROG(chain);
955 VM_RETURN_EDICT(chain);
959 ========================
962 entity findflags(entity start, .float field, float match)
963 ========================
965 // LordHavoc: search for flags in float fields
966 void VM_findflags (void)
973 VM_SAFEPARMCOUNT(3, VM_findflags);
976 e = PRVM_G_EDICTNUM(OFS_PARM0);
977 f = PRVM_G_INT(OFS_PARM1);
978 s = (int)PRVM_G_FLOAT(OFS_PARM2);
980 for (e++ ; e < prog->num_edicts ; e++)
982 prog->xfunction->builtinsprofile++;
983 ed = PRVM_EDICT_NUM(e);
984 if (ed->priv.required->free)
986 if (!PRVM_E_FLOAT(ed,f))
988 if ((int)PRVM_E_FLOAT(ed,f) & s)
995 VM_RETURN_EDICT(prog->edicts);
999 ========================
1002 entity findchainflags(.float field, float match)
1003 ========================
1005 // LordHavoc: chained search for flags in float fields
1006 void VM_findchainflags (void)
1012 prvm_edict_t *ent, *chain;
1014 VM_SAFEPARMCOUNT(2, VM_findchainflags);
1016 if(!prog->flag & PRVM_FE_CHAIN)
1017 PRVM_ERROR("VM_findchainflags: %s doesnt have a chain field !", PRVM_NAME);
1019 chain_of = PRVM_ED_FindField("chain")->ofs;
1021 chain = (prvm_edict_t *)prog->edicts;
1023 f = PRVM_G_INT(OFS_PARM0);
1024 s = (int)PRVM_G_FLOAT(OFS_PARM1);
1026 ent = PRVM_NEXT_EDICT(prog->edicts);
1027 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
1029 prog->xfunction->builtinsprofile++;
1030 if (ent->priv.required->free)
1032 if (!PRVM_E_FLOAT(ent,f))
1034 if (!((int)PRVM_E_FLOAT(ent,f) & s))
1037 PRVM_E_INT(ent,chain_of) = PRVM_EDICT_TO_PROG(chain);
1041 VM_RETURN_EDICT(chain);
1051 void VM_coredump (void)
1053 VM_SAFEPARMCOUNT(0,VM_coredump);
1055 Cbuf_AddText("prvm_edicts ");
1056 Cbuf_AddText(PRVM_NAME);
1067 void PRVM_StackTrace(void);
1068 void VM_stackdump (void)
1070 VM_SAFEPARMCOUNT(0, VM_stackdump);
1085 VM_SAFEPARMCOUNT(0, VM_crash);
1087 PRVM_ERROR("Crash called by %s",PRVM_NAME);
1097 void VM_traceon (void)
1099 VM_SAFEPARMCOUNT(0,VM_traceon);
1111 void VM_traceoff (void)
1113 VM_SAFEPARMCOUNT(0,VM_traceoff);
1115 prog->trace = false;
1125 void VM_eprint (void)
1127 VM_SAFEPARMCOUNT(1,VM_eprint);
1129 PRVM_ED_PrintNum (PRVM_G_EDICTNUM(OFS_PARM0));
1142 VM_SAFEPARMCOUNT(1,VM_rint);
1144 f = PRVM_G_FLOAT(OFS_PARM0);
1146 PRVM_G_FLOAT(OFS_RETURN) = floor(f + 0.5);
1148 PRVM_G_FLOAT(OFS_RETURN) = ceil(f - 0.5);
1158 void VM_floor (void)
1160 VM_SAFEPARMCOUNT(1,VM_floor);
1162 PRVM_G_FLOAT(OFS_RETURN) = floor(PRVM_G_FLOAT(OFS_PARM0));
1174 VM_SAFEPARMCOUNT(1,VM_ceil);
1176 PRVM_G_FLOAT(OFS_RETURN) = ceil(PRVM_G_FLOAT(OFS_PARM0));
1184 entity nextent(entity)
1187 void VM_nextent (void)
1192 i = PRVM_G_EDICTNUM(OFS_PARM0);
1195 prog->xfunction->builtinsprofile++;
1197 if (i == prog->num_edicts)
1199 VM_RETURN_EDICT(prog->edicts);
1202 ent = PRVM_EDICT_NUM(i);
1203 if (!ent->priv.required->free)
1205 VM_RETURN_EDICT(ent);
1211 //=============================================================================
1218 changelevel(string map)
1221 void VM_changelevel (void)
1225 VM_SAFEPARMCOUNT(1, VM_changelevel);
1229 VM_Warning("VM_changelevel: game is not server (%s)\n", PRVM_NAME);
1233 // make sure we don't issue two changelevels
1234 if (svs.changelevel_issued)
1236 svs.changelevel_issued = true;
1238 s = PRVM_G_STRING(OFS_PARM0);
1239 Cbuf_AddText (va("changelevel %s\n",s));
1251 VM_SAFEPARMCOUNT(1,VM_sin);
1252 PRVM_G_FLOAT(OFS_RETURN) = sin(PRVM_G_FLOAT(OFS_PARM0));
1263 VM_SAFEPARMCOUNT(1,VM_cos);
1264 PRVM_G_FLOAT(OFS_RETURN) = cos(PRVM_G_FLOAT(OFS_PARM0));
1276 VM_SAFEPARMCOUNT(1,VM_sqrt);
1277 PRVM_G_FLOAT(OFS_RETURN) = sqrt(PRVM_G_FLOAT(OFS_PARM0));
1284 Returns a vector of length < 1 and > 0
1289 void VM_randomvec (void)
1294 VM_SAFEPARMCOUNT(0, VM_randomvec);
1299 temp[0] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1300 temp[1] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1301 temp[2] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1303 while (DotProduct(temp, temp) >= 1);
1304 VectorCopy (temp, PRVM_G_VECTOR(OFS_RETURN));
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;
1310 // length returned always > 0
1311 length = (rand()&32766 + 1) * (1.0 / 32767.0) / VectorLength(temp);
1312 VectorScale(temp,length, temp);*/
1313 //VectorCopy(temp, PRVM_G_VECTOR(OFS_RETURN));
1316 //=============================================================================
1322 float registercvar (string name, string value, float flags)
1325 void VM_registercvar (void)
1327 const char *name, *value;
1330 VM_SAFEPARMCOUNT(3,VM_registercvar);
1332 name = PRVM_G_STRING(OFS_PARM0);
1333 value = PRVM_G_STRING(OFS_PARM1);
1334 flags = (int)PRVM_G_FLOAT(OFS_PARM2);
1335 PRVM_G_FLOAT(OFS_RETURN) = 0;
1337 if(flags > CVAR_MAXFLAGSVAL)
1340 // first check to see if it has already been defined
1341 if (Cvar_FindVar (name))
1344 // check for overlap with a command
1345 if (Cmd_Exists (name))
1347 VM_Warning("VM_registercvar: %s is a command\n", name);
1351 Cvar_Get(name, value, flags);
1353 PRVM_G_FLOAT(OFS_RETURN) = 1; // success
1360 returns the minimum of two supplied floats
1362 float min(float a, float b, ...[float])
1367 // LordHavoc: 3+ argument enhancement suggested by FrikaC
1368 if (prog->argc == 2)
1369 PRVM_G_FLOAT(OFS_RETURN) = min(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1370 else if (prog->argc >= 3)
1373 float f = PRVM_G_FLOAT(OFS_PARM0);
1374 for (i = 1;i < prog->argc;i++)
1375 if (PRVM_G_FLOAT((OFS_PARM0+i*3)) < f)
1376 f = PRVM_G_FLOAT((OFS_PARM0+i*3));
1377 PRVM_G_FLOAT(OFS_RETURN) = f;
1380 PRVM_ERROR("VM_min: %s must supply at least 2 floats", PRVM_NAME);
1387 returns the maximum of two supplied floats
1389 float max(float a, float b, ...[float])
1394 // LordHavoc: 3+ argument enhancement suggested by FrikaC
1395 if (prog->argc == 2)
1396 PRVM_G_FLOAT(OFS_RETURN) = max(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1397 else if (prog->argc >= 3)
1400 float f = PRVM_G_FLOAT(OFS_PARM0);
1401 for (i = 1;i < prog->argc;i++)
1402 if (PRVM_G_FLOAT((OFS_PARM0+i*3)) > f)
1403 f = PRVM_G_FLOAT((OFS_PARM0+i*3));
1404 PRVM_G_FLOAT(OFS_RETURN) = f;
1407 PRVM_ERROR("VM_max: %s must supply at least 2 floats", PRVM_NAME);
1414 returns number bounded by supplied range
1416 float bound(float min, float value, float max)
1419 void VM_bound (void)
1421 VM_SAFEPARMCOUNT(3,VM_bound);
1422 PRVM_G_FLOAT(OFS_RETURN) = bound(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1), PRVM_G_FLOAT(OFS_PARM2));
1429 returns a raised to power b
1431 float pow(float a, float b)
1436 VM_SAFEPARMCOUNT(2,VM_pow);
1437 PRVM_G_FLOAT(OFS_RETURN) = pow(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1444 copies data from one entity to another
1446 copyentity(entity src, entity dst)
1449 void VM_copyentity (void)
1451 prvm_edict_t *in, *out;
1452 VM_SAFEPARMCOUNT(2,VM_copyentity);
1453 in = PRVM_G_EDICT(OFS_PARM0);
1454 out = PRVM_G_EDICT(OFS_PARM1);
1455 memcpy(out->fields.vp, in->fields.vp, prog->progs->entityfields * 4);
1462 sets the color of a client and broadcasts the update to all connected clients
1464 setcolor(clientent, value)
1467 /*void PF_setcolor (void)
1473 entnum = PRVM_G_EDICTNUM(OFS_PARM0);
1474 i = PRVM_G_FLOAT(OFS_PARM1);
1476 if (entnum < 1 || entnum > svs.maxclients || !svs.clients[entnum-1].active)
1478 Con_Print("tried to setcolor a non-client\n");
1482 client = svs.clients + entnum-1;
1483 if ((val = PRVM_GETEDICTFIELDVALUE(client->edict, eval_clientcolors)))
1486 client->old_colors = i;
1487 client->edict->fields.server->team = (i & 15) + 1;
1489 MSG_WriteByte (&sv.reliable_datagram, svc_updatecolors);
1490 MSG_WriteByte (&sv.reliable_datagram, entnum - 1);
1491 MSG_WriteByte (&sv.reliable_datagram, i);
1494 void VM_Files_Init(void)
1497 for (i = 0;i < PRVM_MAX_OPENFILES;i++)
1498 prog->openfiles[i] = NULL;
1501 void VM_Files_CloseAll(void)
1504 for (i = 0;i < PRVM_MAX_OPENFILES;i++)
1506 if (prog->openfiles[i])
1507 FS_Close(prog->openfiles[i]);
1508 prog->openfiles[i] = NULL;
1512 qfile_t *VM_GetFileHandle( int index )
1514 if (index < 0 || index >= PRVM_MAX_OPENFILES)
1516 Con_Printf("VM_GetFileHandle: invalid file handle %i used in %s\n", index, PRVM_NAME);
1519 if (prog->openfiles[index] == NULL)
1521 Con_Printf("VM_GetFileHandle: no such file handle %i (or file has been closed) in %s\n", index, PRVM_NAME);
1524 return prog->openfiles[index];
1531 float fopen(string filename, float mode)
1534 // float(string filename, float mode) fopen = #110;
1535 // opens a file inside quake/gamedir/data/ (mode is FILE_READ, FILE_APPEND, or FILE_WRITE),
1536 // returns fhandle >= 0 if successful, or fhandle < 0 if unable to open file for any reason
1540 const char *modestring, *filename;
1542 VM_SAFEPARMCOUNT(2,VM_fopen);
1544 for (filenum = 0;filenum < PRVM_MAX_OPENFILES;filenum++)
1545 if (prog->openfiles[filenum] == NULL)
1547 if (filenum >= PRVM_MAX_OPENFILES)
1549 PRVM_G_FLOAT(OFS_RETURN) = -2;
1550 VM_Warning("VM_fopen: %s ran out of file handles (%i)\n", PRVM_NAME, PRVM_MAX_OPENFILES);
1553 mode = (int)PRVM_G_FLOAT(OFS_PARM1);
1556 case 0: // FILE_READ
1559 case 1: // FILE_APPEND
1562 case 2: // FILE_WRITE
1566 PRVM_G_FLOAT(OFS_RETURN) = -3;
1567 VM_Warning("VM_fopen: %s: no such mode %i (valid: 0 = read, 1 = append, 2 = write)\n", PRVM_NAME, mode);
1570 filename = PRVM_G_STRING(OFS_PARM0);
1572 prog->openfiles[filenum] = FS_Open(va("data/%s", filename), modestring, false, false);
1573 if (prog->openfiles[filenum] == NULL && mode == 0)
1574 prog->openfiles[filenum] = FS_Open(va("%s", filename), modestring, false, false);
1576 if (prog->openfiles[filenum] == NULL)
1578 PRVM_G_FLOAT(OFS_RETURN) = -1;
1579 if (developer.integer >= 10)
1580 VM_Warning("VM_fopen: %s: %s mode %s failed\n", PRVM_NAME, filename, modestring);
1584 PRVM_G_FLOAT(OFS_RETURN) = filenum;
1585 if (developer.integer >= 10)
1586 VM_Warning("VM_fopen: %s: %s mode %s opened as #%i\n", PRVM_NAME, filename, modestring, filenum);
1594 fclose(float fhandle)
1597 //void(float fhandle) fclose = #111; // closes a file
1598 void VM_fclose(void)
1602 VM_SAFEPARMCOUNT(1,VM_fclose);
1604 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1605 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
1607 VM_Warning("VM_fclose: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1610 if (prog->openfiles[filenum] == NULL)
1612 VM_Warning("VM_fclose: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1615 FS_Close(prog->openfiles[filenum]);
1616 prog->openfiles[filenum] = NULL;
1617 if (developer.integer >= 10)
1618 VM_Warning("VM_fclose: %s: #%i closed\n", PRVM_NAME, filenum);
1625 string fgets(float fhandle)
1628 //string(float fhandle) fgets = #112; // reads a line of text from the file and returns as a tempstring
1632 static char string[VM_STRINGTEMP_LENGTH];
1635 VM_SAFEPARMCOUNT(1,VM_fgets);
1637 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1638 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
1640 VM_Warning("VM_fgets: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1643 if (prog->openfiles[filenum] == NULL)
1645 VM_Warning("VM_fgets: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1651 c = FS_Getc(prog->openfiles[filenum]);
1652 if (c == '\r' || c == '\n' || c < 0)
1654 if (end < VM_STRINGTEMP_LENGTH - 1)
1658 // remove \n following \r
1661 c = FS_Getc(prog->openfiles[filenum]);
1663 FS_UnGetc(prog->openfiles[filenum], (unsigned char)c);
1665 if (developer.integer >= 100)
1666 Con_Printf("fgets: %s: %s\n", PRVM_NAME, string);
1668 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(string);
1670 PRVM_G_INT(OFS_RETURN) = 0;
1677 fputs(float fhandle, string s)
1680 //void(float fhandle, string s) fputs = #113; // writes a line of text to the end of the file
1684 char string[VM_STRINGTEMP_LENGTH];
1687 VM_SAFEPARMCOUNT(2,VM_fputs);
1689 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1690 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
1692 VM_Warning("VM_fputs: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1695 if (prog->openfiles[filenum] == NULL)
1697 VM_Warning("VM_fputs: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1700 VM_VarString(1, string, sizeof(string));
1701 if ((stringlength = (int)strlen(string)))
1702 FS_Write(prog->openfiles[filenum], string, stringlength);
1703 if (developer.integer >= 100)
1704 Con_Printf("fputs: %s: %s\n", PRVM_NAME, string);
1711 float strlen(string s)
1714 //float(string s) strlen = #114; // returns how many characters are in a string
1715 void VM_strlen(void)
1719 VM_SAFEPARMCOUNT(1,VM_strlen);
1721 s = PRVM_G_STRING(OFS_PARM0);
1723 PRVM_G_FLOAT(OFS_RETURN) = strlen(s);
1725 PRVM_G_FLOAT(OFS_RETURN) = 0;
1732 string strcat(string,string,...[string])
1735 //string(string s1, string s2) strcat = #115;
1736 // concatenates two strings (for example "abc", "def" would return "abcdef")
1737 // and returns as a tempstring
1738 void VM_strcat(void)
1743 PRVM_ERROR("VM_strcat wrong parameter count (min. 1 expected ) !");
1745 s = VM_GetTempString();
1746 VM_VarString(0, s, VM_STRINGTEMP_LENGTH);
1747 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(s);
1754 string substring(string s, float start, float length)
1757 // string(string s, float start, float length) substring = #116;
1758 // returns a section of a string as a tempstring
1759 void VM_substring(void)
1761 int i, start, length;
1765 VM_SAFEPARMCOUNT(3,VM_substring);
1767 string = VM_GetTempString();
1768 s = PRVM_G_STRING(OFS_PARM0);
1769 start = (int)PRVM_G_FLOAT(OFS_PARM1);
1770 length = (int)PRVM_G_FLOAT(OFS_PARM2);
1773 for (i = 0;i < start && *s;i++, s++);
1774 for (i = 0;i < VM_STRINGTEMP_LENGTH - 1 && *s && i < length;i++, s++)
1777 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(string);
1784 vector stov(string s)
1787 //vector(string s) stov = #117; // returns vector value from a string
1790 char string[VM_STRINGTEMP_LENGTH];
1792 VM_SAFEPARMCOUNT(1,VM_stov);
1794 VM_VarString(0, string, sizeof(string));
1795 Math_atov(string, PRVM_G_VECTOR(OFS_RETURN));
1802 string strzone(string s)
1805 //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)
1806 void VM_strzone(void)
1809 char string[VM_STRINGTEMP_LENGTH];
1812 VM_SAFEPARMCOUNT(1,VM_strzone);
1814 VM_VarString(0, string, sizeof(string));
1815 alloclen = strlen(string) + 1;
1816 PRVM_G_INT(OFS_RETURN) = PRVM_AllocString(alloclen, &out);
1817 memcpy(out, string, alloclen);
1827 //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!!!)
1828 void VM_strunzone(void)
1830 VM_SAFEPARMCOUNT(1,VM_strunzone);
1831 PRVM_FreeString(PRVM_G_INT(OFS_PARM0));
1836 VM_command (used by client and menu)
1838 clientcommand(float client, string s) (for client and menu)
1841 //void(entity e, string s) clientcommand = #440; // executes a command string as if it came from the specified client
1842 //this function originally written by KrimZon, made shorter by LordHavoc
1843 void VM_clcommand (void)
1845 client_t *temp_client;
1848 VM_SAFEPARMCOUNT(2,VM_clcommand);
1850 i = (int)PRVM_G_FLOAT(OFS_PARM0);
1851 if (!sv.active || i < 0 || i >= svs.maxclients || !svs.clients[i].active)
1853 VM_Warning("VM_clientcommand: %s: invalid client/server is not active !\n", PRVM_NAME);
1857 temp_client = host_client;
1858 host_client = svs.clients + i;
1859 Cmd_ExecuteString (PRVM_G_STRING(OFS_PARM1), src_client);
1860 host_client = temp_client;
1868 float tokenize(string s)
1871 //float(string s) tokenize = #441; // takes apart a string into individal words (access them with argv), returns how many
1872 //this function originally written by KrimZon, made shorter by LordHavoc
1873 //20040203: rewritten by LordHavoc (no longer uses allocations)
1875 char *tokens[256], tokenbuf[MAX_INPUTLINE];
1876 void VM_tokenize (void)
1881 VM_SAFEPARMCOUNT(1,VM_tokenize);
1883 p = PRVM_G_STRING(OFS_PARM0);
1887 while(COM_ParseToken(&p, false))
1890 if (num_tokens >= (int)(sizeof(tokens)/sizeof(tokens[0])))
1892 tokenlen = strlen(com_token) + 1;
1893 if (pos + tokenlen > sizeof(tokenbuf))
1895 tokens[num_tokens++] = tokenbuf + pos;
1896 memcpy(tokenbuf + pos, com_token, tokenlen);
1900 PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
1903 //string(float n) argv = #442; // returns a word from the tokenized string (returns nothing for an invalid index)
1904 //this function originally written by KrimZon, made shorter by LordHavoc
1909 VM_SAFEPARMCOUNT(1,VM_argv);
1911 token_num = (int)PRVM_G_FLOAT(OFS_PARM0);
1913 if (token_num >= 0 && token_num < num_tokens)
1914 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(tokens[token_num]);
1916 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(NULL);
1920 //void(entity e, entity tagentity, string tagname) setattachment = #443; // attachs e to a tag on tagentity (note: use "" to attach to entity origin/angles instead of a tag)
1921 void PF_setattachment (void)
1923 prvm_edict_t *e = PRVM_G_EDICT(OFS_PARM0);
1924 prvm_edict_t *tagentity = PRVM_G_EDICT(OFS_PARM1);
1925 char *tagname = PRVM_G_STRING(OFS_PARM2);
1930 if (tagentity == NULL)
1931 tagentity = prog->edicts;
1933 v = PRVM_GETEDICTFIELDVALUE(e, eval_tag_entity);
1935 fields.server->edict = PRVM_EDICT_TO_PROG(tagentity);
1937 v = PRVM_GETEDICTFIELDVALUE(e, eval_tag_index);
1939 fields.server->_float = 0;
1940 if (tagentity != NULL && tagentity != prog->edicts && tagname && tagname[0])
1942 modelindex = (int)tagentity->fields.server->modelindex;
1943 if (modelindex >= 0 && modelindex < MAX_MODELS)
1945 model = sv.models[modelindex];
1946 if (model->data_overridetagnamesforskin && (unsigned int)tagentity->fields.server->skin < (unsigned int)model->numskins && model->data_overridetagnamesforskin[(unsigned int)tagentity->fields.server->skin].num_overridetagnames)
1947 for (i = 0;i < model->data_overridetagnamesforskin[(unsigned int)tagentity->fields.server->skin].num_overridetagnames;i++)
1948 if (!strcmp(tagname, model->data_overridetagnamesforskin[(unsigned int)tagentity->fields.server->skin].data_overridetagnames[i].name))
1949 fields.server->_float = i + 1;
1950 // FIXME: use a model function to get tag info (need to handle skeletal)
1951 if (fields.server->_float == 0 && model->num_tags)
1952 for (i = 0;i < model->num_tags;i++)
1953 if (!strcmp(tagname, model->data_tags[i].name))
1954 fields.server->_float = i + 1;
1955 if (fields.server->_float == 0)
1956 Con_DPrintf("setattachment(edict %i, edict %i, string \"%s\"): tried to find tag named \"%s\" on entity %i (model \"%s\") but could not find it\n", PRVM_NUM_FOR_EDICT(e), PRVM_NUM_FOR_EDICT(tagentity), tagname, tagname, PRVM_NUM_FOR_EDICT(tagentity), model->name);
1959 Con_DPrintf("setattachment(edict %i, edict %i, string \"%s\"): tried to find tag named \"%s\" on entity %i but it has no model\n", PRVM_NUM_FOR_EDICT(e), PRVM_NUM_FOR_EDICT(tagentity), tagname, tagname, PRVM_NUM_FOR_EDICT(tagentity));
1970 void VM_isserver(void)
1972 VM_SAFEPARMCOUNT(0,VM_serverstate);
1974 PRVM_G_FLOAT(OFS_RETURN) = sv.active;
1984 void VM_clientcount(void)
1986 VM_SAFEPARMCOUNT(0,VM_clientcount);
1988 PRVM_G_FLOAT(OFS_RETURN) = svs.maxclients;
1998 void VM_clientstate(void)
2000 VM_SAFEPARMCOUNT(0,VM_clientstate);
2002 PRVM_G_FLOAT(OFS_RETURN) = cls.state;
2009 float getostype(void)
2011 */ // not used at the moment -> not included in the common list
2012 void VM_getostype(void)
2014 VM_SAFEPARMCOUNT(0,VM_getostype);
2019 OS_MAC - not supported
2023 PRVM_G_FLOAT(OFS_RETURN) = 0;
2024 #elif defined(MACOSX)
2025 PRVM_G_FLOAT(OFS_RETURN) = 2;
2027 PRVM_G_FLOAT(OFS_RETURN) = 1;
2035 vector getmousepos()
2038 void VM_getmousepos(void)
2041 VM_SAFEPARMCOUNT(0,VM_getmousepos);
2043 PRVM_G_VECTOR(OFS_RETURN)[0] = in_mouse_x * vid_conwidth.integer / vid.width;
2044 PRVM_G_VECTOR(OFS_RETURN)[1] = in_mouse_y * vid_conheight.integer / vid.height;
2045 PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
2055 void VM_gettime(void)
2057 VM_SAFEPARMCOUNT(0,VM_gettime);
2059 PRVM_G_FLOAT(OFS_RETURN) = (float) *prog->time;
2066 loadfromdata(string data)
2069 void VM_loadfromdata(void)
2071 VM_SAFEPARMCOUNT(1,VM_loadentsfromfile);
2073 PRVM_ED_LoadFromFile(PRVM_G_STRING(OFS_PARM0));
2077 ========================
2080 parseentitydata(entity ent, string data)
2081 ========================
2083 void VM_parseentitydata(void)
2088 VM_SAFEPARMCOUNT(2, VM_parseentitydata);
2090 // get edict and test it
2091 ent = PRVM_G_EDICT(OFS_PARM0);
2092 if (ent->priv.required->free)
2093 PRVM_ERROR ("VM_parseentitydata: %s: Can only set already spawned entities (entity %i is free)!", PRVM_NAME, PRVM_NUM_FOR_EDICT(ent));
2095 data = PRVM_G_STRING(OFS_PARM1);
2097 // parse the opening brace
2098 if (!COM_ParseTokenConsole(&data) || com_token[0] != '{' )
2099 PRVM_ERROR ("VM_parseentitydata: %s: Couldn't parse entity data:\n%s", PRVM_NAME, data );
2101 PRVM_ED_ParseEdict (data, ent);
2108 loadfromfile(string file)
2111 void VM_loadfromfile(void)
2113 const char *filename;
2116 VM_SAFEPARMCOUNT(1,VM_loadfromfile);
2118 filename = PRVM_G_STRING(OFS_PARM0);
2119 // .. is parent directory on many platforms
2120 // / is parent directory on Amiga
2121 // : is root of drive on Amiga (also used as a directory separator on Mac, but / works there too, so that's a bad idea)
2122 // \ is a windows-ism (so it's naughty to use it, / works on all platforms)
2123 if ((filename[0] == '.' && filename[1] == '.') || filename[0] == '/' || strrchr(filename, ':') || strrchr(filename, '\\'))
2125 PRVM_G_FLOAT(OFS_RETURN) = -4;
2126 VM_Warning("VM_loadfromfile: %s dangerous or non-portable filename \"%s\" not allowed. (contains : or \\ or begins with .. or /)\n", PRVM_NAME, filename);
2130 // not conform with VM_fopen
2131 data = (char *)FS_LoadFile(filename, tempmempool, false, NULL);
2133 PRVM_G_FLOAT(OFS_RETURN) = -1;
2135 PRVM_ED_LoadFromFile(data);
2146 float mod(float val, float m)
2149 void VM_modulo(void)
2152 VM_SAFEPARMCOUNT(2,VM_module);
2154 val = (int) PRVM_G_FLOAT(OFS_PARM0);
2155 m = (int) PRVM_G_FLOAT(OFS_PARM1);
2157 PRVM_G_FLOAT(OFS_RETURN) = (float) (val % m);
2160 void VM_Search_Init(void)
2163 for (i = 0;i < PRVM_MAX_OPENSEARCHES;i++)
2164 prog->opensearches[i] = NULL;
2167 void VM_Search_Reset(void)
2170 // reset the fssearch list
2171 for(i = 0; i < PRVM_MAX_OPENSEARCHES; i++)
2173 if(prog->opensearches[i])
2174 FS_FreeSearch(prog->opensearches[i]);
2175 prog->opensearches[i] = NULL;
2183 float search_begin(string pattern, float caseinsensitive, float quiet)
2186 void VM_search_begin(void)
2189 const char *pattern;
2190 int caseinsens, quiet;
2192 VM_SAFEPARMCOUNT(3, VM_search_begin);
2194 pattern = PRVM_G_STRING(OFS_PARM0);
2196 VM_CheckEmptyString(pattern);
2198 caseinsens = (int)PRVM_G_FLOAT(OFS_PARM1);
2199 quiet = (int)PRVM_G_FLOAT(OFS_PARM2);
2201 for(handle = 0; handle < PRVM_MAX_OPENSEARCHES; handle++)
2202 if(!prog->opensearches[handle])
2205 if(handle >= PRVM_MAX_OPENSEARCHES)
2207 PRVM_G_FLOAT(OFS_RETURN) = -2;
2208 VM_Warning("VM_search_begin: %s ran out of search handles (%i)\n", PRVM_NAME, PRVM_MAX_OPENSEARCHES);
2212 if(!(prog->opensearches[handle] = FS_Search(pattern,caseinsens, quiet)))
2213 PRVM_G_FLOAT(OFS_RETURN) = -1;
2215 PRVM_G_FLOAT(OFS_RETURN) = handle;
2222 void search_end(float handle)
2225 void VM_search_end(void)
2228 VM_SAFEPARMCOUNT(1, VM_search_end);
2230 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
2232 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
2234 VM_Warning("VM_search_end: invalid handle %i used in %s\n", handle, PRVM_NAME);
2237 if(prog->opensearches[handle] == NULL)
2239 VM_Warning("VM_search_end: no such handle %i in %s\n", handle, PRVM_NAME);
2243 FS_FreeSearch(prog->opensearches[handle]);
2244 prog->opensearches[handle] = NULL;
2251 float search_getsize(float handle)
2254 void VM_search_getsize(void)
2257 VM_SAFEPARMCOUNT(1, VM_M_search_getsize);
2259 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
2261 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
2263 VM_Warning("VM_search_getsize: invalid handle %i used in %s\n", handle, PRVM_NAME);
2266 if(prog->opensearches[handle] == NULL)
2268 VM_Warning("VM_search_getsize: no such handle %i in %s\n", handle, PRVM_NAME);
2272 PRVM_G_FLOAT(OFS_RETURN) = prog->opensearches[handle]->numfilenames;
2277 VM_search_getfilename
2279 string search_getfilename(float handle, float num)
2282 void VM_search_getfilename(void)
2284 int handle, filenum;
2286 VM_SAFEPARMCOUNT(2, VM_search_getfilename);
2288 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
2289 filenum = (int)PRVM_G_FLOAT(OFS_PARM1);
2291 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
2293 VM_Warning("VM_search_getfilename: invalid handle %i used in %s\n", handle, PRVM_NAME);
2296 if(prog->opensearches[handle] == NULL)
2298 VM_Warning("VM_search_getfilename: no such handle %i in %s\n", handle, PRVM_NAME);
2301 if(filenum < 0 || filenum >= prog->opensearches[handle]->numfilenames)
2303 VM_Warning("VM_search_getfilename: invalid filenum %i in %s\n", filenum, PRVM_NAME);
2307 tmp = VM_GetTempString();
2308 strlcpy(tmp, prog->opensearches[handle]->filenames[filenum], VM_STRINGTEMP_LENGTH);
2310 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(tmp);
2317 string chr(float ascii)
2323 VM_SAFEPARMCOUNT(1, VM_chr);
2325 tmp = VM_GetTempString();
2326 tmp[0] = (unsigned char) PRVM_G_FLOAT(OFS_PARM0);
2329 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(tmp);
2332 //=============================================================================
2333 // Draw builtins (client & menu)
2339 float iscachedpic(string pic)
2342 void VM_iscachedpic(void)
2344 VM_SAFEPARMCOUNT(1,VM_iscachedpic);
2346 // drawq hasnt such a function, thus always return true
2347 PRVM_G_FLOAT(OFS_RETURN) = false;
2354 string precache_pic(string pic)
2357 void VM_precache_pic(void)
2361 VM_SAFEPARMCOUNT(1, VM_precache_pic);
2363 s = PRVM_G_STRING(OFS_PARM0);
2364 PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
2367 PRVM_ERROR ("VM_precache_pic: %s: NULL", PRVM_NAME);
2369 VM_CheckEmptyString (s);
2371 // AK Draw_CachePic is supposed to always return a valid pointer
2372 if( Draw_CachePic(s, false)->tex == r_texture_notexture )
2373 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(NULL);
2383 void VM_freepic(void)
2387 VM_SAFEPARMCOUNT(1,VM_freepic);
2389 s = PRVM_G_STRING(OFS_PARM0);
2392 PRVM_ERROR ("VM_freepic: %s: NULL");
2394 VM_CheckEmptyString (s);
2403 float drawcharacter(vector position, float character, vector scale, vector rgb, float alpha, float flag)
2406 void VM_drawcharacter(void)
2408 float *pos,*scale,*rgb;
2411 VM_SAFEPARMCOUNT(6,VM_drawcharacter);
2413 character = (char) PRVM_G_FLOAT(OFS_PARM1);
2416 PRVM_G_FLOAT(OFS_RETURN) = -1;
2417 VM_Warning("VM_drawcharacter: %s passed null character !\n",PRVM_NAME);
2421 pos = PRVM_G_VECTOR(OFS_PARM0);
2422 scale = PRVM_G_VECTOR(OFS_PARM2);
2423 rgb = PRVM_G_VECTOR(OFS_PARM3);
2424 flag = (int)PRVM_G_FLOAT(OFS_PARM5);
2426 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2428 PRVM_G_FLOAT(OFS_RETURN) = -2;
2429 VM_Warning("VM_drawcharacter: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2433 if(pos[2] || scale[2])
2434 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")));
2436 if(!scale[0] || !scale[1])
2438 PRVM_G_FLOAT(OFS_RETURN) = -3;
2439 VM_Warning("VM_drawcharacter: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
2443 DrawQ_String (pos[0], pos[1], &character, 1, scale[0], scale[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM4), flag);
2444 PRVM_G_FLOAT(OFS_RETURN) = 1;
2451 float drawstring(vector position, string text, vector scale, vector rgb, float alpha, float flag)
2454 void VM_drawstring(void)
2456 float *pos,*scale,*rgb;
2459 VM_SAFEPARMCOUNT(6,VM_drawstring);
2461 string = PRVM_G_STRING(OFS_PARM1);
2464 PRVM_G_FLOAT(OFS_RETURN) = -1;
2465 VM_Warning("VM_drawstring: %s passed null string !\n",PRVM_NAME);
2469 //VM_CheckEmptyString(string); Why should it be checked - perhaps the menu wants to support the precolored letters, too?
2471 pos = PRVM_G_VECTOR(OFS_PARM0);
2472 scale = PRVM_G_VECTOR(OFS_PARM2);
2473 rgb = PRVM_G_VECTOR(OFS_PARM3);
2474 flag = (int)PRVM_G_FLOAT(OFS_PARM5);
2476 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2478 PRVM_G_FLOAT(OFS_RETURN) = -2;
2479 VM_Warning("VM_drawstring: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2483 if(!scale[0] || !scale[1])
2485 PRVM_G_FLOAT(OFS_RETURN) = -3;
2486 VM_Warning("VM_drawstring: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
2490 if(pos[2] || scale[2])
2491 Con_Printf("VM_drawstring: z value%c from %s discarded\n",(pos[2] && scale[2]) ? 's' : 0,((pos[2] && scale[2]) ? "pos and scale" : (pos[2] ? "pos" : "scale")));
2493 DrawQ_String (pos[0], pos[1], string, 0, scale[0], scale[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM4), flag);
2494 PRVM_G_FLOAT(OFS_RETURN) = 1;
2500 float drawpic(vector position, string pic, vector size, vector rgb, float alpha, float flag)
2503 void VM_drawpic(void)
2505 const char *picname;
2506 float *size, *pos, *rgb;
2509 VM_SAFEPARMCOUNT(6,VM_drawpic);
2511 picname = PRVM_G_STRING(OFS_PARM1);
2515 PRVM_G_FLOAT(OFS_RETURN) = -1;
2516 VM_Warning("VM_drawpic: %s passed null picture name !\n", PRVM_NAME);
2520 VM_CheckEmptyString (picname);
2522 // is pic cached ? no function yet for that
2525 PRVM_G_FLOAT(OFS_RETURN) = -4;
2526 VM_Warning("VM_drawpic: %s: %s not cached !\n", PRVM_NAME, picname);
2530 pos = PRVM_G_VECTOR(OFS_PARM0);
2531 size = PRVM_G_VECTOR(OFS_PARM2);
2532 rgb = PRVM_G_VECTOR(OFS_PARM3);
2533 flag = (int) PRVM_G_FLOAT(OFS_PARM5);
2535 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2537 PRVM_G_FLOAT(OFS_RETURN) = -2;
2538 VM_Warning("VM_drawstring: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2542 if(pos[2] || size[2])
2543 Con_Printf("VM_drawstring: z value%c from %s discarded\n",(pos[2] && size[2]) ? 's' : 0,((pos[2] && size[2]) ? "pos and size" : (pos[2] ? "pos" : "size")));
2545 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);
2546 PRVM_G_FLOAT(OFS_RETURN) = 1;
2553 float drawfill(vector position, vector size, vector rgb, float alpha, float flag)
2556 void VM_drawfill(void)
2558 float *size, *pos, *rgb;
2561 VM_SAFEPARMCOUNT(5,VM_drawfill);
2564 pos = PRVM_G_VECTOR(OFS_PARM0);
2565 size = PRVM_G_VECTOR(OFS_PARM1);
2566 rgb = PRVM_G_VECTOR(OFS_PARM2);
2567 flag = (int) PRVM_G_FLOAT(OFS_PARM4);
2569 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2571 PRVM_G_FLOAT(OFS_RETURN) = -2;
2572 VM_Warning("VM_drawstring: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2576 if(pos[2] || size[2])
2577 Con_Printf("VM_drawstring: z value%c from %s discarded\n",(pos[2] && size[2]) ? 's' : 0,((pos[2] && size[2]) ? "pos and size" : (pos[2] ? "pos" : "size")));
2579 DrawQ_Pic(pos[0], pos[1], NULL, size[0], size[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM3), flag);
2580 PRVM_G_FLOAT(OFS_RETURN) = 1;
2587 drawsetcliparea(float x, float y, float width, float height)
2590 void VM_drawsetcliparea(void)
2593 VM_SAFEPARMCOUNT(4,VM_drawsetcliparea);
2595 x = bound(0, PRVM_G_FLOAT(OFS_PARM0), vid_conwidth.integer);
2596 y = bound(0, PRVM_G_FLOAT(OFS_PARM1), vid_conheight.integer);
2597 w = bound(0, PRVM_G_FLOAT(OFS_PARM2) + PRVM_G_FLOAT(OFS_PARM0) - x, (vid_conwidth.integer - x));
2598 h = bound(0, PRVM_G_FLOAT(OFS_PARM3) + PRVM_G_FLOAT(OFS_PARM1) - y, (vid_conheight.integer - y));
2600 DrawQ_SetClipArea(x, y, w, h);
2605 VM_drawresetcliparea
2610 void VM_drawresetcliparea(void)
2612 VM_SAFEPARMCOUNT(0,VM_drawresetcliparea);
2614 DrawQ_ResetClipArea();
2621 vector getimagesize(string pic)
2624 void VM_getimagesize(void)
2629 VM_SAFEPARMCOUNT(1,VM_getimagesize);
2631 p = PRVM_G_STRING(OFS_PARM0);
2634 PRVM_ERROR("VM_getimagepos: %s passed null picture name !", PRVM_NAME);
2636 VM_CheckEmptyString (p);
2638 pic = Draw_CachePic (p, false);
2640 PRVM_G_VECTOR(OFS_RETURN)[0] = pic->width;
2641 PRVM_G_VECTOR(OFS_RETURN)[1] = pic->height;
2642 PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
2649 string keynumtostring(float keynum)
2652 void VM_keynumtostring (void)
2656 VM_SAFEPARMCOUNT(1, VM_keynumtostring);
2658 keynum = (int)PRVM_G_FLOAT(OFS_PARM0);
2660 tmp = VM_GetTempString();
2662 strlcpy(tmp, Key_KeynumToString(keynum), VM_STRINGTEMP_LENGTH);
2664 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(tmp);
2671 float stringtokeynum(string key)
2674 void VM_stringtokeynum (void)
2677 VM_SAFEPARMCOUNT( 1, VM_keynumtostring );
2679 str = PRVM_G_STRING( OFS_PARM0 );
2681 PRVM_G_INT(OFS_RETURN) = Key_StringToKeynum( str );
2684 // CL_Video interface functions
2687 ========================
2690 float cin_open(string file, string name)
2691 ========================
2693 void VM_cin_open( void )
2698 VM_SAFEPARMCOUNT( 2, VM_cin_open );
2700 file = PRVM_G_STRING( OFS_PARM0 );
2701 name = PRVM_G_STRING( OFS_PARM1 );
2703 VM_CheckEmptyString( file );
2704 VM_CheckEmptyString( name );
2706 if( CL_OpenVideo( file, name, MENUOWNER ) )
2707 PRVM_G_FLOAT( OFS_RETURN ) = 1;
2709 PRVM_G_FLOAT( OFS_RETURN ) = 0;
2713 ========================
2716 void cin_close(string name)
2717 ========================
2719 void VM_cin_close( void )
2723 VM_SAFEPARMCOUNT( 1, VM_cin_close );
2725 name = PRVM_G_STRING( OFS_PARM0 );
2726 VM_CheckEmptyString( name );
2728 CL_CloseVideo( CL_GetVideoByName( name ) );
2732 ========================
2734 void cin_setstate(string name, float type)
2735 ========================
2737 void VM_cin_setstate( void )
2740 clvideostate_t state;
2743 VM_SAFEPARMCOUNT( 2, VM_cin_netstate );
2745 name = PRVM_G_STRING( OFS_PARM0 );
2746 VM_CheckEmptyString( name );
2748 state = (clvideostate_t)((int)PRVM_G_FLOAT( OFS_PARM1 ));
2750 video = CL_GetVideoByName( name );
2751 if( video && state > CLVIDEO_UNUSED && state < CLVIDEO_STATECOUNT )
2752 CL_SetVideoState( video, state );
2756 ========================
2759 float cin_getstate(string name)
2760 ========================
2762 void VM_cin_getstate( void )
2767 VM_SAFEPARMCOUNT( 1, VM_cin_getstate );
2769 name = PRVM_G_STRING( OFS_PARM0 );
2770 VM_CheckEmptyString( name );
2772 video = CL_GetVideoByName( name );
2774 PRVM_G_FLOAT( OFS_RETURN ) = (int)video->state;
2776 PRVM_G_FLOAT( OFS_RETURN ) = 0;
2780 ========================
2783 void cin_restart(string name)
2784 ========================
2786 void VM_cin_restart( void )
2791 VM_SAFEPARMCOUNT( 1, VM_cin_restart );
2793 name = PRVM_G_STRING( OFS_PARM0 );
2794 VM_CheckEmptyString( name );
2796 video = CL_GetVideoByName( name );
2798 CL_RestartVideo( video );
2805 Writes new values for v_forward, v_up, and v_right based on the given forward vector
2806 vectorvectors(vector, vector)
2809 void VM_vectorvectors (void)
2811 VectorNormalize2(PRVM_G_VECTOR(OFS_PARM0), prog->globals.server->v_forward);
2812 VectorVectors(prog->globals.server->v_forward, prog->globals.server->v_right, prog->globals.server->v_up);
2816 ========================
2819 void drawline(float width, vector pos1, vector pos2, vector rgb, float alpha, float flags)
2820 ========================
2822 void VM_drawline (void)
2824 float *c1, *c2, *rgb;
2826 unsigned char flags;
2828 VM_SAFEPARMCOUNT(6, VM_drawline);
2829 width = PRVM_G_FLOAT(OFS_PARM0);
2830 c1 = PRVM_G_VECTOR(OFS_PARM1);
2831 c2 = PRVM_G_VECTOR(OFS_PARM2);
2832 rgb = PRVM_G_VECTOR(OFS_PARM3);
2833 alpha = PRVM_G_FLOAT(OFS_PARM4);
2834 flags = (int)PRVM_G_FLOAT(OFS_PARM5);
2835 DrawQ_Line(width, c1[0], c1[1], c2[0], c2[1], rgb[0], rgb[1], rgb[2], alpha, flags);
2838 //====================
2839 //QC POLYGON functions
2840 //====================
2845 float data[36]; //[515]: enough for polygons
2846 unsigned char flags; //[515]: + VM_POLYGON_2D and VM_POLYGON_FL4V flags
2849 //static float vm_polygon_linewidth = 1;
2850 static mempool_t *vm_polygons_pool = NULL;
2851 static unsigned char vm_current_vertices = 0;
2852 static qboolean vm_polygons_initialized = false;
2853 static vm_polygon_t *vm_polygons = NULL;
2854 static unsigned long vm_polygons_num = 0, vm_drawpolygons_num = 0; //[515]: ok long on 64bit ?
2855 static qboolean vm_polygonbegin = false; //[515]: for "no-crap-on-the-screen" check
2856 #define VM_DEFPOLYNUM 64 //[515]: enough for default ?
2858 #define VM_POLYGON_FL3V 16 //more than 2 vertices (used only for lines)
2859 #define VM_POLYGON_FLLINES 32
2860 #define VM_POLYGON_FL2D 64
2861 #define VM_POLYGON_FL4V 128 //4 vertices
2863 void VM_InitPolygons (void)
2865 vm_polygons_pool = Mem_AllocPool("VMPOLY", 0, NULL);
2866 vm_polygons = (vm_polygon_t *)Mem_Alloc(vm_polygons_pool, VM_DEFPOLYNUM*sizeof(vm_polygon_t));
2867 memset(vm_polygons, 0, VM_DEFPOLYNUM*sizeof(vm_polygon_t));
2868 vm_polygons_num = VM_DEFPOLYNUM;
2869 vm_drawpolygons_num = 0;
2870 vm_polygonbegin = false;
2871 vm_polygons_initialized = true;
2874 void VM_DrawPolygonCallback (const entity_render_t *ent, const rtlight_t *rtlight, int numsurfaces, int *surfacelist)
2876 int surfacelistindex;
2877 // LordHavoc: FIXME: this is stupid code
2878 for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
2880 const vm_polygon_t *p = &vm_polygons[surfacelist[surfacelistindex]];
2881 int flags = p->flags & 0x0f;
2883 if(flags == DRAWFLAG_ADDITIVE)
2884 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
2885 else if(flags == DRAWFLAG_MODULATE)
2886 GL_BlendFunc(GL_DST_COLOR, GL_ZERO);
2887 else if(flags == DRAWFLAG_2XMODULATE)
2888 GL_BlendFunc(GL_DST_COLOR,GL_SRC_COLOR);
2890 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
2892 R_Mesh_TexBind(0, R_GetTexture(p->tex));
2895 //[515]: is speed is max ?
2896 if(p->flags & VM_POLYGON_FLLINES) //[515]: lines
2898 qglLineWidth(p->data[13]);CHECKGLERROR
2899 qglBegin(GL_LINE_LOOP);
2900 qglTexCoord1f (p->data[12]);
2901 qglColor4f (p->data[20], p->data[21], p->data[22], p->data[23]);
2902 qglVertex3f (p->data[0] , p->data[1], p->data[2]);
2904 qglTexCoord1f (p->data[14]);
2905 qglColor4f (p->data[24], p->data[25], p->data[26], p->data[27]);
2906 qglVertex3f (p->data[3] , p->data[4], p->data[5]);
2908 if(p->flags & VM_POLYGON_FL3V)
2910 qglTexCoord1f (p->data[16]);
2911 qglColor4f (p->data[28], p->data[29], p->data[30], p->data[31]);
2912 qglVertex3f (p->data[6] , p->data[7], p->data[8]);
2914 if(p->flags & VM_POLYGON_FL4V)
2916 qglTexCoord1f (p->data[18]);
2917 qglColor4f (p->data[32], p->data[33], p->data[34], p->data[35]);
2918 qglVertex3f (p->data[9] , p->data[10], p->data[11]);
2926 qglBegin(GL_POLYGON);
2927 qglTexCoord2f (p->data[12], p->data[13]);
2928 qglColor4f (p->data[20], p->data[21], p->data[22], p->data[23]);
2929 qglVertex3f (p->data[0] , p->data[1], p->data[2]);
2931 qglTexCoord2f (p->data[14], p->data[15]);
2932 qglColor4f (p->data[24], p->data[25], p->data[26], p->data[27]);
2933 qglVertex3f (p->data[3] , p->data[4], p->data[5]);
2935 qglTexCoord2f (p->data[16], p->data[17]);
2936 qglColor4f (p->data[28], p->data[29], p->data[30], p->data[31]);
2937 qglVertex3f (p->data[6] , p->data[7], p->data[8]);
2939 if(p->flags & VM_POLYGON_FL4V)
2941 qglTexCoord2f (p->data[18], p->data[19]);
2942 qglColor4f (p->data[32], p->data[33], p->data[34], p->data[35]);
2943 qglVertex3f (p->data[9] , p->data[10], p->data[11]);
2951 void VM_AddPolygonTo2DScene (vm_polygon_t *p)
2953 drawqueuemesh_t mesh;
2954 static int picelements[6] = {0, 1, 2, 0, 2, 3};
2956 mesh.texture = p->tex;
2957 mesh.data_element3i = picelements;
2958 mesh.data_vertex3f = p->data;
2959 mesh.data_texcoord2f = p->data + 12;
2960 mesh.data_color4f = p->data + 20;
2961 if(p->flags & VM_POLYGON_FL4V)
2963 mesh.num_vertices = 4;
2964 mesh.num_triangles = 2;
2968 mesh.num_vertices = 3;
2969 mesh.num_triangles = 1;
2971 if(p->flags & VM_POLYGON_FLLINES) //[515]: lines
2972 DrawQ_LineLoop (&mesh, (p->flags&0x0f));
2974 DrawQ_Mesh (&mesh, (p->flags&0x0f));
2977 //void(string texturename, float flag, float 2d, float lines) R_BeginPolygon
2978 void VM_R_PolygonBegin (void)
2981 const char *picname;
2983 VM_SAFEPARMCOUNT(2, VM_R_PolygonBegin);
2985 if(!vm_polygons_initialized)
2989 VM_Warning("VM_R_PolygonBegin: called twice without VM_R_PolygonEnd after first\n");
2992 if(vm_drawpolygons_num >= vm_polygons_num)
2994 p = (vm_polygon_t *)Mem_Alloc(vm_polygons_pool, 2 * vm_polygons_num * sizeof(vm_polygon_t));
2995 memset(p, 0, 2 * vm_polygons_num * sizeof(vm_polygon_t));
2996 memcpy(p, vm_polygons, vm_polygons_num * sizeof(vm_polygon_t));
2997 Mem_Free(vm_polygons);
2999 vm_polygons_num *= 2;
3001 p = &vm_polygons[vm_drawpolygons_num];
3002 picname = PRVM_G_STRING(OFS_PARM0);
3004 p->tex = Draw_CachePic(picname, true)->tex;
3006 p->tex = r_texture_white;
3007 p->flags = (unsigned char)PRVM_G_FLOAT(OFS_PARM1);
3008 vm_current_vertices = 0;
3009 vm_polygonbegin = true;
3012 if(PRVM_G_FLOAT(OFS_PARM2))
3013 p->flags |= VM_POLYGON_FL2D;
3014 if(prog->argc >= 4 && PRVM_G_FLOAT(OFS_PARM3))
3016 p->data[13] = PRVM_G_FLOAT(OFS_PARM3); //[515]: linewidth
3017 p->flags |= VM_POLYGON_FLLINES;
3022 //void(vector org, vector texcoords, vector rgb, float alpha) R_PolygonVertex
3023 void VM_R_PolygonVertex (void)
3025 float *coords, *tx, *rgb, alpha;
3027 VM_SAFEPARMCOUNT(4, VM_R_PolygonVertex);
3029 if(!vm_polygonbegin)
3031 VM_Warning("VM_R_PolygonVertex: VM_R_PolygonBegin wasn't called\n");
3034 coords = PRVM_G_VECTOR(OFS_PARM0);
3035 tx = PRVM_G_VECTOR(OFS_PARM1);
3036 rgb = PRVM_G_VECTOR(OFS_PARM2);
3037 alpha = PRVM_G_FLOAT(OFS_PARM3);
3039 p = &vm_polygons[vm_drawpolygons_num];
3040 if(vm_current_vertices > 4)
3042 VM_Warning("VM_R_PolygonVertex: may have 4 vertices max\n");
3046 p->data[vm_current_vertices*3] = coords[0];
3047 p->data[1+vm_current_vertices*3] = coords[1];
3048 p->data[2+vm_current_vertices*3] = coords[2];
3050 p->data[12+vm_current_vertices*2] = tx[0];
3051 if(!(p->flags & VM_POLYGON_FLLINES))
3052 p->data[13+vm_current_vertices*2] = tx[1];
3054 p->data[20+vm_current_vertices*4] = rgb[0];
3055 p->data[21+vm_current_vertices*4] = rgb[1];
3056 p->data[22+vm_current_vertices*4] = rgb[2];
3057 p->data[23+vm_current_vertices*4] = alpha;
3059 vm_current_vertices++;
3060 if(vm_current_vertices == 4)
3061 p->flags |= VM_POLYGON_FL4V;
3063 if(vm_current_vertices == 3)
3064 p->flags |= VM_POLYGON_FL3V;
3067 //void() R_EndPolygon
3068 void VM_R_PolygonEnd (void)
3070 if(!vm_polygonbegin)
3072 VM_Warning("VM_R_PolygonEnd: VM_R_PolygonBegin wasn't called\n");
3075 vm_polygonbegin = false;
3076 if(vm_current_vertices > 2 || (vm_current_vertices >= 2 && vm_polygons[vm_drawpolygons_num].flags & VM_POLYGON_FLLINES))
3078 if(vm_polygons[vm_drawpolygons_num].flags & VM_POLYGON_FL2D) //[515]: don't use qcpolygons memory if 2D
3079 VM_AddPolygonTo2DScene(&vm_polygons[vm_drawpolygons_num]);
3081 vm_drawpolygons_num++;
3084 VM_Warning("VM_R_PolygonEnd: %i vertices isn't a good choice\n", vm_current_vertices);
3087 void VM_AddPolygonsToMeshQueue (void)
3090 if(!vm_drawpolygons_num)
3092 for(i = 0;i < vm_drawpolygons_num;i++)
3093 VM_DrawPolygonCallback(NULL, NULL, i, NULL);
3094 vm_drawpolygons_num = 0;
3100 // float(float number, float quantity) bitshift (EXT_BITSHIFT)
3101 void VM_bitshift (void)
3104 VM_SAFEPARMCOUNT(2, VM_bitshift);
3106 n1 = (int)fabs((int)PRVM_G_FLOAT(OFS_PARM0));
3107 n2 = (int)PRVM_G_FLOAT(OFS_PARM1);
3109 PRVM_G_FLOAT(OFS_RETURN) = n1;
3112 PRVM_G_FLOAT(OFS_RETURN) = (n1 >> -n2);
3114 PRVM_G_FLOAT(OFS_RETURN) = (n1 << n2);
3117 ////////////////////////////////////////
3118 // AltString functions
3119 ////////////////////////////////////////
3122 ========================
3125 float altstr_count(string)
3126 ========================
3128 void VM_altstr_count( void )
3130 const char *altstr, *pos;
3133 VM_SAFEPARMCOUNT( 1, VM_altstr_count );
3135 altstr = PRVM_G_STRING( OFS_PARM0 );
3136 //VM_CheckEmptyString( altstr );
3138 for( count = 0, pos = altstr ; *pos ; pos++ ) {
3139 if( *pos == '\\' ) {
3143 } else if( *pos == '\'' ) {
3148 PRVM_G_FLOAT( OFS_RETURN ) = (float) (count / 2);
3152 ========================
3155 string altstr_prepare(string)
3156 ========================
3158 void VM_altstr_prepare( void )
3161 const char *instr, *in;
3164 VM_SAFEPARMCOUNT( 1, VM_altstr_prepare );
3166 instr = PRVM_G_STRING( OFS_PARM0 );
3167 //VM_CheckEmptyString( instr );
3168 outstr = VM_GetTempString();
3170 for( out = outstr, in = instr, size = VM_STRINGTEMP_LENGTH - 1 ; size && *in ; size--, in++, out++ )
3179 PRVM_G_INT( OFS_RETURN ) = PRVM_SetEngineString( outstr );
3183 ========================
3186 string altstr_get(string, float)
3187 ========================
3189 void VM_altstr_get( void )
3191 const char *altstr, *pos;
3195 VM_SAFEPARMCOUNT( 2, VM_altstr_get );
3197 altstr = PRVM_G_STRING( OFS_PARM0 );
3198 //VM_CheckEmptyString( altstr );
3200 count = (int)PRVM_G_FLOAT( OFS_PARM1 );
3201 count = count * 2 + 1;
3203 for( pos = altstr ; *pos && count ; pos++ )
3204 if( *pos == '\\' ) {
3207 } else if( *pos == '\'' )
3211 PRVM_G_INT( OFS_RETURN ) = PRVM_SetEngineString( NULL );
3215 outstr = VM_GetTempString();
3216 for( out = outstr, size = VM_STRINGTEMP_LENGTH - 1 ; size && *pos ; size--, pos++, out++ )
3217 if( *pos == '\\' ) {
3222 } else if( *pos == '\'' )
3228 PRVM_G_INT( OFS_RETURN ) = PRVM_SetEngineString( outstr );
3232 ========================
3235 string altstr_set(string altstr, float num, string set)
3236 ========================
3238 void VM_altstr_set( void )
3241 const char *altstr, *str;
3245 VM_SAFEPARMCOUNT( 3, VM_altstr_set );
3247 altstr = PRVM_G_STRING( OFS_PARM0 );
3248 //VM_CheckEmptyString( altstr );
3250 num = (int)PRVM_G_FLOAT( OFS_PARM1 );
3252 str = PRVM_G_STRING( OFS_PARM2 );
3253 //VM_CheckEmptyString( str );
3255 outstr = out = VM_GetTempString();
3256 for( num = num * 2 + 1, in = altstr; *in && num; *out++ = *in++ )
3261 } else if( *in == '\'' ) {
3266 PRVM_G_INT( OFS_RETURN ) = PRVM_SetEngineString( altstr );
3270 for( ; *str; *out++ = *str++ );
3271 // now jump over the old content
3273 if( *in == '\'' || (*in == '\\' && !*++in) )
3277 PRVM_G_INT( OFS_RETURN ) = PRVM_SetEngineString( NULL );
3281 strlcpy(out, in, VM_STRINGTEMP_LENGTH);
3282 PRVM_G_INT( OFS_RETURN ) = PRVM_SetEngineString( outstr );
3286 ========================
3289 string altstr_ins(string altstr, float num, string set)
3290 ========================
3292 void VM_altstr_ins(void)
3302 in = instr = PRVM_G_STRING( OFS_PARM0 );
3303 num = (int)PRVM_G_FLOAT( OFS_PARM1 );
3304 set = setstr = PRVM_G_STRING( OFS_PARM2 );
3306 out = outstr = VM_GetTempString();
3307 for( num = num * 2 + 2 ; *in && num > 0 ; *out++ = *in++ )
3312 } else if( *in == '\'' ) {
3317 for( ; *set ; *out++ = *set++ );
3320 strlcpy(out, in, VM_STRINGTEMP_LENGTH);
3321 PRVM_G_INT( OFS_RETURN ) = PRVM_SetEngineString( outstr );
3325 ////////////////////////////////////////
3326 // BufString functions
3327 ////////////////////////////////////////
3328 //[515]: string buffers support
3329 #define MAX_QCSTR_BUFFERS 128
3330 #define MAX_QCSTR_STRINGS 1024
3335 char *strings[MAX_QCSTR_STRINGS];
3338 static qcstrbuffer_t *qcstringbuffers[MAX_QCSTR_BUFFERS];
3339 static int num_qcstringbuffers;
3340 static int buf_sortpower;
3342 #define BUFSTR_BUFFER(a) (a>=MAX_QCSTR_BUFFERS) ? NULL : (qcstringbuffers[a])
3343 #define BUFSTR_ISFREE(a) (a<MAX_QCSTR_BUFFERS&&qcstringbuffers[a]&&qcstringbuffers[a]->num_strings<=0) ? 1 : 0
3345 static int BufStr_FindFreeBuffer (void)
3348 if(num_qcstringbuffers == MAX_QCSTR_BUFFERS)
3350 for(i=0;i<MAX_QCSTR_BUFFERS;i++)
3351 if(!qcstringbuffers[i])
3353 qcstringbuffers[i] = (qcstrbuffer_t *)Z_Malloc(sizeof(qcstrbuffer_t));
3354 memset(qcstringbuffers[i], 0, sizeof(qcstrbuffer_t));
3360 static void BufStr_ClearBuffer (int index)
3362 qcstrbuffer_t *b = qcstringbuffers[index];
3367 if(b->num_strings > 0)
3369 for(i=0;i<b->num_strings;i++)
3371 Z_Free(b->strings[i]);
3372 num_qcstringbuffers--;
3374 Z_Free(qcstringbuffers[index]);
3375 qcstringbuffers[index] = NULL;
3379 static int BufStr_FindFreeString (qcstrbuffer_t *b)
3382 for(i=0;i<b->num_strings;i++)
3383 if(!b->strings[i] || !b->strings[i][0])
3385 if(i == MAX_QCSTR_STRINGS) return -1;
3389 static int BufStr_SortStringsUP (const void *in1, const void *in2)
3392 a = *((const char **) in1);
3393 b = *((const char **) in2);
3395 if(!b[0]) return -1;
3396 return strncmp(a, b, buf_sortpower);
3399 static int BufStr_SortStringsDOWN (const void *in1, const void *in2)
3402 a = *((const char **) in1);
3403 b = *((const char **) in2);
3405 if(!b[0]) return -1;
3406 return strncmp(b, a, buf_sortpower);
3410 static void VM_BufStr_Init (void)
3412 memset(qcstringbuffers, 0, sizeof(qcstringbuffers));
3413 num_qcstringbuffers = 0;
3416 static void VM_BufStr_ShutDown (void)
3419 for(i=0;i<MAX_QCSTR_BUFFERS && num_qcstringbuffers;i++)
3420 BufStr_ClearBuffer(i);
3425 ========================
3427 creates new buffer, and returns it's index, returns -1 if failed
3428 float buf_create(void) = #460;
3429 ========================
3431 void VM_buf_create (void)
3434 VM_SAFEPARMCOUNT(0, VM_buf_create);
3435 i = BufStr_FindFreeBuffer();
3437 num_qcstringbuffers++;
3439 //Con_Printf("VM_buf_create: buffers overflow in %s\n", PRVM_NAME);
3440 PRVM_G_FLOAT(OFS_RETURN) = i;
3444 ========================
3446 deletes buffer and all strings in it
3447 void buf_del(float bufhandle) = #461;
3448 ========================
3450 void VM_buf_del (void)
3452 VM_SAFEPARMCOUNT(1, VM_buf_del);
3453 if(BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0)))
3454 BufStr_ClearBuffer((int)PRVM_G_FLOAT(OFS_PARM0));
3457 VM_Warning("VM_buf_del: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3463 ========================
3465 how many strings are stored in buffer
3466 float buf_getsize(float bufhandle) = #462;
3467 ========================
3469 void VM_buf_getsize (void)
3472 VM_SAFEPARMCOUNT(1, VM_buf_getsize);
3474 b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3477 PRVM_G_FLOAT(OFS_RETURN) = -1;
3478 VM_Warning("VM_buf_getsize: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3482 PRVM_G_FLOAT(OFS_RETURN) = b->num_strings;
3486 ========================
3488 copy all content from one buffer to another, make sure it exists
3489 void buf_copy(float bufhandle_from, float bufhandle_to) = #463;
3490 ========================
3492 void VM_buf_copy (void)
3494 qcstrbuffer_t *b1, *b2;
3496 VM_SAFEPARMCOUNT(2, VM_buf_copy);
3498 b1 = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3501 VM_Warning("VM_buf_copy: invalid source buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3504 i = (int)PRVM_G_FLOAT(OFS_PARM1);
3505 if(i == (int)PRVM_G_FLOAT(OFS_PARM0))
3507 VM_Warning("VM_buf_copy: source == destination (%i) in %s\n", i, PRVM_NAME);
3510 b2 = BUFSTR_BUFFER(i);
3513 VM_Warning("VM_buf_copy: invalid destination buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM1), PRVM_NAME);
3517 BufStr_ClearBuffer(i);
3518 qcstringbuffers[i] = (qcstrbuffer_t *)Z_Malloc(sizeof(qcstrbuffer_t));
3519 memset(qcstringbuffers[i], 0, sizeof(qcstrbuffer_t));
3520 b2->num_strings = b1->num_strings;
3522 for(i=0;i<b1->num_strings;i++)
3523 if(b1->strings[i] && b1->strings[i][0])
3526 stringlen = strlen(b1->strings[i]) + 1;
3527 b2->strings[i] = (char *)Z_Malloc(stringlen);
3530 VM_Warning("VM_buf_copy: not enough memory for buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM1), PRVM_NAME);
3533 memcpy(b2->strings[i], b1->strings[i], stringlen);
3538 ========================
3540 sort buffer by beginnings of strings (sortpower defaults it's lenght)
3541 "backward == TRUE" means that sorting goes upside-down
3542 void buf_sort(float bufhandle, float sortpower, float backward) = #464;
3543 ========================
3545 void VM_buf_sort (void)
3549 VM_SAFEPARMCOUNT(3, VM_buf_sort);
3551 b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3554 VM_Warning("VM_buf_sort: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3557 if(b->num_strings <= 0)
3559 VM_Warning("VM_buf_sort: tried to sort empty buffer %i in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3562 buf_sortpower = (int)PRVM_G_FLOAT(OFS_PARM1);
3563 if(buf_sortpower <= 0)
3564 buf_sortpower = 99999999;
3566 if(!PRVM_G_FLOAT(OFS_PARM2))
3567 qsort(b->strings, b->num_strings, sizeof(char*), BufStr_SortStringsUP);
3569 qsort(b->strings, b->num_strings, sizeof(char*), BufStr_SortStringsDOWN);
3571 for(i=b->num_strings-1;i>=0;i--) //[515]: delete empty lines
3574 if(b->strings[i][0])
3578 Z_Free(b->strings[i]);
3580 b->strings[i] = NULL;
3588 ========================
3590 concantenates all buffer string into one with "glue" separator and returns it as tempstring
3591 string buf_implode(float bufhandle, string glue) = #465;
3592 ========================
3594 void VM_buf_implode (void)
3601 VM_SAFEPARMCOUNT(2, VM_buf_implode);
3603 b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3604 PRVM_G_INT(OFS_RETURN) = 0;
3607 VM_Warning("VM_buf_implode: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3612 sep = PRVM_G_STRING(OFS_PARM1);
3613 k = VM_GetTempString();
3615 for(l=i=0;i<b->num_strings;i++)
3618 l += strlen(b->strings[i]);
3621 strlcat(k, b->strings[i], VM_STRINGTEMP_LENGTH);
3622 if(sep && (i != b->num_strings-1))
3627 strlcat(k, sep, VM_STRINGTEMP_LENGTH);
3630 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(k);
3634 ========================
3636 get a string from buffer, returns direct pointer, dont str_unzone it!
3637 string bufstr_get(float bufhandle, float string_index) = #465;
3638 ========================
3640 void VM_bufstr_get (void)
3644 VM_SAFEPARMCOUNT(2, VM_bufstr_get);
3646 b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3649 VM_Warning("VM_bufstr_get: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3652 strindex = (int)PRVM_G_FLOAT(OFS_PARM1);
3653 if(strindex < 0 || strindex > MAX_QCSTR_STRINGS)
3655 VM_Warning("VM_bufstr_get: invalid string index %i used in %s\n", strindex, PRVM_NAME);
3658 PRVM_G_INT(OFS_RETURN) = 0;
3659 if(b->num_strings <= strindex)
3661 if(b->strings[strindex])
3662 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(b->strings[strindex]);
3666 ========================
3668 copies a string into selected slot of buffer
3669 void bufstr_set(float bufhandle, float string_index, string str) = #466;
3670 ========================
3672 void VM_bufstr_set (void)
3674 int bufindex, strindex;
3679 VM_SAFEPARMCOUNT(3, VM_bufstr_set);
3681 bufindex = (int)PRVM_G_FLOAT(OFS_PARM0);
3682 b = BUFSTR_BUFFER(bufindex);
3685 VM_Warning("VM_bufstr_set: invalid buffer %i used in %s\n", bufindex, PRVM_NAME);
3688 strindex = (int)PRVM_G_FLOAT(OFS_PARM1);
3689 if(strindex < 0 || strindex > MAX_QCSTR_STRINGS)
3691 VM_Warning("VM_bufstr_set: invalid string index %i used in %s\n", strindex, PRVM_NAME);
3694 news = PRVM_G_STRING(OFS_PARM2);
3697 VM_Warning("VM_bufstr_set: null string used in %s\n", PRVM_NAME);
3700 if(b->strings[strindex])
3701 Z_Free(b->strings[strindex]);
3702 alloclen = strlen(news) + 1;
3703 b->strings[strindex] = (char *)Z_Malloc(alloclen);
3704 memcpy(b->strings[strindex], news, alloclen);
3708 ========================
3710 adds string to buffer in nearest free slot and returns it
3711 "order == TRUE" means that string will be added after last "full" slot
3712 float bufstr_add(float bufhandle, string str, float order) = #467;
3713 ========================
3715 void VM_bufstr_add (void)
3717 int bufindex, order, strindex;
3722 VM_SAFEPARMCOUNT(3, VM_bufstr_add);
3724 bufindex = (int)PRVM_G_FLOAT(OFS_PARM0);
3725 b = BUFSTR_BUFFER(bufindex);
3726 PRVM_G_FLOAT(OFS_RETURN) = -1;
3729 VM_Warning("VM_bufstr_add: invalid buffer %i used in %s\n", bufindex, PRVM_NAME);
3732 string = PRVM_G_STRING(OFS_PARM1);
3735 VM_Warning("VM_bufstr_add: null string used in %s\n", PRVM_NAME);
3739 order = (int)PRVM_G_FLOAT(OFS_PARM2);
3741 strindex = b->num_strings;
3744 strindex = BufStr_FindFreeString(b);
3747 VM_Warning("VM_bufstr_add: buffer %i has no free string slots in %s\n", bufindex, PRVM_NAME);
3752 while(b->num_strings <= strindex)
3754 if(b->num_strings == MAX_QCSTR_STRINGS)
3756 VM_Warning("VM_bufstr_add: buffer %i has no free string slots in %s\n", bufindex, PRVM_NAME);
3759 b->strings[b->num_strings] = NULL;
3762 if(b->strings[strindex])
3763 Z_Free(b->strings[strindex]);
3764 alloclen = strlen(string) + 1;
3765 b->strings[strindex] = (char *)Z_Malloc(alloclen);
3766 memcpy(b->strings[strindex], string, alloclen);
3767 PRVM_G_FLOAT(OFS_RETURN) = strindex;
3771 ========================
3773 delete string from buffer
3774 void bufstr_free(float bufhandle, float string_index) = #468;
3775 ========================
3777 void VM_bufstr_free (void)
3781 VM_SAFEPARMCOUNT(2, VM_bufstr_free);
3783 b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3786 VM_Warning("VM_bufstr_free: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3789 i = (int)PRVM_G_FLOAT(OFS_PARM1);
3790 if(i < 0 || i > MAX_QCSTR_STRINGS)
3792 VM_Warning("VM_bufstr_free: invalid string index %i used in %s\n", i, PRVM_NAME);
3796 Z_Free(b->strings[i]);
3797 b->strings[i] = NULL;
3798 if(i+1 == b->num_strings)
3804 void VM_Cmd_Init(void)
3806 // only init the stuff for the current prog
3809 // VM_BufStr_Init();
3810 if(vm_polygons_initialized)
3812 Mem_FreePool(&vm_polygons_pool);
3813 vm_polygons_initialized = false;
3817 void VM_Cmd_Reset(void)
3819 CL_PurgeOwner( MENUOWNER );
3821 VM_Files_CloseAll();
3822 // VM_BufStr_ShutDown();
3823 if(vm_polygons_initialized)
3825 Mem_FreePool(&vm_polygons_pool);
3826 vm_polygons_initialized = false;