2 // Basically every vm builtin cmd should be in here.
3 // All 3 builtin and extension lists can be found here
4 // cause large (I think they will) parts are from pr_cmds the same copyright like in pr_cmds
9 //============================================================================
12 // temp string handling
13 // LordHavoc: added this to semi-fix the problem of using many ftos calls in a print
14 static char vm_string_temp[VM_STRINGTEMP_BUFFERS][VM_STRINGTEMP_LENGTH];
15 static int vm_string_tempindex = 0;
18 #define MAX_VMFILES 256
19 #define MAX_PRVMFILES MAX_VMFILES * PRVM_MAXPROGS
20 #define VM_FILES ((qfile_t**)(vm_files + PRVM_GetProgNr() * MAX_VMFILES))
22 qfile_t *vm_files[MAX_PRVMFILES];
24 // qc fs search handling
25 #define MAX_VMSEARCHES 128
26 #define TOTAL_VMSEARCHES MAX_VMSEARCHES * PRVM_MAXPROGS
27 #define VM_SEARCHLIST ((fssearch_t**)(vm_fssearchlist + PRVM_GetProgNr() * MAX_VMSEARCHES))
29 fssearch_t *vm_fssearchlist[TOTAL_VMSEARCHES];
31 char *VM_GetTempString(void)
34 s = vm_string_temp[vm_string_tempindex];
35 vm_string_tempindex = (vm_string_tempindex + 1) % VM_STRINGTEMP_BUFFERS;
39 void VM_CheckEmptyString (const char *s)
42 PRVM_ERROR ("%s: Bad string", PRVM_NAME);
45 //============================================================================
48 void VM_VarString(int first, char *out, int outlength)
54 outend = out + outlength - 1;
55 for (i = first;i < prog->argc && out < outend;i++)
57 s = PRVM_G_STRING((OFS_PARM0+i*3));
58 while (out < outend && *s)
68 returns true if the extension is supported by the server
70 checkextension(extensionname)
74 // kind of helper function
75 static qboolean checkextension(const char *name)
79 len = (int)strlen(name);
81 for (e = prog->extensionstring;*e;e++)
88 while (*e && *e != ' ')
90 if ((e - start) == len && !strncasecmp(start, name, len))
96 void VM_checkextension (void)
98 VM_SAFEPARMCOUNT(1,VM_checkextension);
100 PRVM_G_FLOAT(OFS_RETURN) = checkextension(PRVM_G_STRING(OFS_PARM0));
107 This is a TERMINAL error, which will kill off the entire prog.
116 char string[VM_STRINGTEMP_LENGTH];
118 VM_VarString(0, string, sizeof(string));
119 Con_Printf("======%s ERROR in %s:\n%s\n", PRVM_NAME, PRVM_GetString(prog->xfunction->s_name), string);
122 ed = PRVM_G_EDICT(prog->self->ofs);
126 PRVM_ERROR ("%s: Program error in function %s:\n%s\n", PRVM_NAME, PRVM_GetString(prog->xfunction->s_name), string);
133 Dumps out self, then an error message. The program is aborted and self is
134 removed, but the level can continue.
139 void VM_objerror (void)
142 char string[VM_STRINGTEMP_LENGTH];
144 VM_VarString(0, string, sizeof(string));
145 Con_Printf("======OBJECT ERROR======\n", PRVM_NAME, PRVM_GetString(prog->xfunction->s_name), string);
148 ed = PRVM_G_EDICT (prog->self->ofs);
154 // objerror has to display the object fields -> else call
155 PRVM_ERROR ("VM_objecterror: self not defined !");
156 Con_Printf("%s OBJECT ERROR in %s:\n%s\n", PRVM_NAME, PRVM_GetString(prog->xfunction->s_name), string);
161 VM_print (actually used only by client and menu)
170 char string[VM_STRINGTEMP_LENGTH];
172 VM_VarString(0, string, sizeof(string));
180 broadcast print to everyone on server
185 void VM_bprint (void)
187 char string[VM_STRINGTEMP_LENGTH];
191 Con_Printf("VM_bprint: game is not server(%s) !\n", PRVM_NAME);
195 VM_VarString(0, string, sizeof(string));
196 SV_BroadcastPrint(string);
201 VM_sprint (menu & client but only if server.active == true)
203 single print to a specific client
205 sprint(float clientnum,...[string])
208 void VM_sprint (void)
212 char string[VM_STRINGTEMP_LENGTH];
214 //find client for this entity
215 clientnum = (int)PRVM_G_FLOAT(OFS_PARM0);
216 if (!sv.active || clientnum < 0 || clientnum >= svs.maxclients || !svs.clients[clientnum].active)
218 Con_Printf("VM_sprint: %s: invalid client or server is not active !\n", PRVM_NAME);
222 client = svs.clients + clientnum;
223 if (!client->netconnection)
226 VM_VarString(1, string, sizeof(string));
227 MSG_WriteChar(&client->netconnection->message,svc_print);
228 MSG_WriteString(&client->netconnection->message, string);
235 single print to the screen
237 centerprint(clientent, value)
240 void VM_centerprint (void)
242 char string[VM_STRINGTEMP_LENGTH];
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)
326 void VM_vectoangles (void)
332 VM_SAFEPARMCOUNT(1,VM_vectoangles);
334 value1 = PRVM_G_VECTOR(OFS_PARM0);
336 if (value1[1] == 0 && value1[0] == 0)
346 // LordHavoc: optimized a bit
349 yaw = (atan2(value1[1], value1[0]) * 180 / M_PI);
353 else if (value1[1] > 0)
358 forward = sqrt(value1[0]*value1[0] + value1[1]*value1[1]);
359 pitch = (atan2(value1[2], forward) * 180 / M_PI);
364 PRVM_G_FLOAT(OFS_RETURN+0) = pitch;
365 PRVM_G_FLOAT(OFS_RETURN+1) = yaw;
366 PRVM_G_FLOAT(OFS_RETURN+2) = 0;
373 Returns a number from 0<= num < 1
378 void VM_random (void)
380 VM_SAFEPARMCOUNT(0,VM_random);
382 PRVM_G_FLOAT(OFS_RETURN) = lhrandom(0, 1);
389 Each entity can have eight independant sound sources, like voice,
392 Channel 0 is an auto-allocate channel, the others override anything
393 already running on that entity/channel pair.
395 An attenuation of 0 will play full volume everywhere in the level.
396 Larger attenuations will drop off.
405 prvm_edict_t *entity;
409 entity = PRVM_G_EDICT(OFS_PARM0);
410 channel = PRVM_G_FLOAT(OFS_PARM1);
411 sample = PRVM_G_STRING(OFS_PARM2);
412 volume = PRVM_G_FLOAT(OFS_PARM3) * 255;
413 attenuation = PRVM_G_FLOAT(OFS_PARM4);
415 if (volume < 0 || volume > 255)
416 Host_Error ("SV_StartSound: volume = %i", volume);
418 if (attenuation < 0 || attenuation > 4)
419 Host_Error ("SV_StartSound: attenuation = %f", attenuation);
421 if (channel < 0 || channel > 7)
422 Host_Error ("SV_StartSound: channel = %i", channel);
424 SV_StartSound (entity, channel, sample, volume, attenuation);
432 localsound(string sample)
435 void VM_localsound(void)
439 VM_SAFEPARMCOUNT(1,VM_localsound);
441 s = PRVM_G_STRING(OFS_PARM0);
443 if(!S_LocalSound (s))
445 Con_Printf("VM_localsound: Failed to play %s for %s !\n", s, PRVM_NAME);
446 PRVM_G_FLOAT(OFS_RETURN) = -4;
450 PRVM_G_FLOAT(OFS_RETURN) = 1;
462 PRVM_ERROR ("%s: break statement", PRVM_NAME);
465 //============================================================================
471 Sends text over to the client's execution buffer
473 [localcmd (string, ...) or]
477 void VM_localcmd (void)
479 char string[VM_STRINGTEMP_LENGTH];
480 VM_VarString(0, string, sizeof(string));
481 Cbuf_AddText(string);
493 VM_SAFEPARMCOUNT(1,VM_cvar);
495 PRVM_G_FLOAT(OFS_RETURN) = Cvar_VariableValue(PRVM_G_STRING(OFS_PARM0));
502 const string VM_cvar_string (string)
505 void VM_cvar_string(void)
509 const char *cvar_string;
510 VM_SAFEPARMCOUNT(1,VM_cvar_string);
512 name = PRVM_G_STRING(OFS_PARM0);
515 PRVM_ERROR("VM_cvar_string: %s: null string", PRVM_NAME);
517 VM_CheckEmptyString(name);
519 out = VM_GetTempString();
521 cvar_string = Cvar_VariableString(name);
523 strcpy(out, cvar_string);
525 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(out);
530 ========================
533 const string VM_cvar_defstring (string)
534 ========================
536 void VM_cvar_defstring (void)
540 const char *cvar_string;
541 VM_SAFEPARMCOUNT(1,VM_cvar_string);
543 name = PRVM_G_STRING(OFS_PARM0);
546 PRVM_ERROR("VM_cvar_defstring: %s: null string", PRVM_NAME);
548 VM_CheckEmptyString(name);
550 out = VM_GetTempString();
552 cvar_string = Cvar_VariableDefString(name);
554 strcpy(out, cvar_string);
556 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(out);
562 void cvar_set (string,string)
565 void VM_cvar_set (void)
567 VM_SAFEPARMCOUNT(2,VM_cvar_set);
569 Cvar_Set(PRVM_G_STRING(OFS_PARM0), PRVM_G_STRING(OFS_PARM1));
579 void VM_dprint (void)
581 char string[VM_STRINGTEMP_LENGTH];
582 if (developer.integer)
584 VM_VarString(0, string, sizeof(string));
586 Con_Printf("%s", string);
588 Con_Printf("%s: %s", PRVM_NAME, string);
606 VM_SAFEPARMCOUNT(1, VM_ftos);
608 v = PRVM_G_FLOAT(OFS_PARM0);
610 s = VM_GetTempString();
611 if ((float)((int)v) == v)
612 sprintf(s, "%i", (int)v);
615 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(s);
630 VM_SAFEPARMCOUNT(1,VM_fabs);
632 v = PRVM_G_FLOAT(OFS_PARM0);
633 PRVM_G_FLOAT(OFS_RETURN) = fabs(v);
648 VM_SAFEPARMCOUNT(1,VM_vtos);
650 s = VM_GetTempString();
651 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]);
652 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(s);
667 VM_SAFEPARMCOUNT(1, VM_etos);
669 s = VM_GetTempString();
670 sprintf (s, "entity %i", PRVM_G_EDICTNUM(OFS_PARM0));
671 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(s);
678 float stof(...[string])
683 char string[VM_STRINGTEMP_LENGTH];
684 VM_VarString(0, string, sizeof(string));
685 PRVM_G_FLOAT(OFS_RETURN) = atof(string);
689 ========================
693 ========================
697 VM_SAFEPARMCOUNT(1, VM_itof);
698 PRVM_G_FLOAT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
702 ========================
706 ========================
711 VM_SAFEPARMCOUNT(1, VM_ftoi);
713 ent = (int)PRVM_G_FLOAT(OFS_PARM0);
714 if(PRVM_PROG_TO_EDICT(ent)->priv.required->free)
715 PRVM_ERROR ("VM_ftoe: %s tried to access a freed entity (entity %i)!", PRVM_NAME, ent);
717 PRVM_G_INT(OFS_RETURN) = ent;
731 prog->xfunction->builtinsprofile += 20;
732 ed = PRVM_ED_Alloc();
744 void VM_remove (void)
747 prog->xfunction->builtinsprofile += 20;
749 VM_SAFEPARMCOUNT(1, VM_remove);
751 ed = PRVM_G_EDICT(OFS_PARM0);
752 if( PRVM_NUM_FOR_EDICT(ed) <= prog->reserved_edicts )
754 Con_DPrint( "VM_remove: tried to remove the null entity or a reserved entity!\n" );
755 if (developer.integer >= 1)
758 else if( ed->priv.required->free )
760 Con_DPrint( "VM_remove: tried to remove an already freed entity!\n" );
761 if (developer.integer >= 1)
766 // if (ed == prog->edicts)
767 // PRVM_ERROR ("remove: tried to remove world");
768 // if (PRVM_NUM_FOR_EDICT(ed) <= sv.maxclients)
769 // Host_Error("remove: tried to remove a client");
776 entity find(entity start, .string field, string match)
787 VM_SAFEPARMCOUNT(3,VM_find);
789 e = PRVM_G_EDICTNUM(OFS_PARM0);
790 f = PRVM_G_INT(OFS_PARM1);
791 s = PRVM_G_STRING(OFS_PARM2);
793 // LordHavoc: apparently BloodMage does a find(world, weaponmodel, "") and
794 // expects it to find all the monsters, so we must be careful to support
799 for (e++ ; e < prog->num_edicts ; e++)
801 prog->xfunction->builtinsprofile++;
802 ed = PRVM_EDICT_NUM(e);
803 if (ed->priv.required->free)
805 t = PRVM_E_STRING(ed,f);
815 VM_RETURN_EDICT(prog->edicts);
822 entity findfloat(entity start, .float field, float match)
823 entity findentity(entity start, .entity field, entity match)
826 // LordHavoc: added this for searching float, int, and entity reference fields
827 void VM_findfloat (void)
834 VM_SAFEPARMCOUNT(3,VM_findfloat);
836 e = PRVM_G_EDICTNUM(OFS_PARM0);
837 f = PRVM_G_INT(OFS_PARM1);
838 s = PRVM_G_FLOAT(OFS_PARM2);
840 for (e++ ; e < prog->num_edicts ; e++)
842 prog->xfunction->builtinsprofile++;
843 ed = PRVM_EDICT_NUM(e);
844 if (ed->priv.required->free)
846 if (PRVM_E_FLOAT(ed,f) == s)
853 VM_RETURN_EDICT(prog->edicts);
860 entity findchain(.string field, string match)
863 // chained search for strings in entity fields
864 // entity(.string field, string match) findchain = #402;
865 void VM_findchain (void)
871 prvm_edict_t *ent, *chain;
873 VM_SAFEPARMCOUNT(2,VM_findchain);
875 // is the same like !(prog->flag & PRVM_FE_CHAIN) - even if the operator precedence is another
876 if(!prog->flag & PRVM_FE_CHAIN)
877 PRVM_ERROR("VM_findchain: %s doesnt have a chain field !", PRVM_NAME);
879 chain_of = PRVM_ED_FindField("chain")->ofs;
881 chain = prog->edicts;
883 f = PRVM_G_INT(OFS_PARM0);
884 s = PRVM_G_STRING(OFS_PARM1);
886 // LordHavoc: apparently BloodMage does a find(world, weaponmodel, "") and
887 // expects it to find all the monsters, so we must be careful to support
892 ent = PRVM_NEXT_EDICT(prog->edicts);
893 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
895 prog->xfunction->builtinsprofile++;
896 if (ent->priv.required->free)
898 t = PRVM_E_STRING(ent,f);
904 PRVM_E_INT(ent,chain_of) = PRVM_NUM_FOR_EDICT(chain);
908 VM_RETURN_EDICT(chain);
915 entity findchainfloat(.string field, float match)
916 entity findchainentity(.string field, entity match)
919 // LordHavoc: chained search for float, int, and entity reference fields
920 // entity(.string field, float match) findchainfloat = #403;
921 void VM_findchainfloat (void)
927 prvm_edict_t *ent, *chain;
929 VM_SAFEPARMCOUNT(2, VM_findchainfloat);
931 if(!prog->flag & PRVM_FE_CHAIN)
932 PRVM_ERROR("VM_findchainfloat: %s doesnt have a chain field !", PRVM_NAME);
934 chain_of = PRVM_ED_FindField("chain")->ofs;
936 chain = (prvm_edict_t *)prog->edicts;
938 f = PRVM_G_INT(OFS_PARM0);
939 s = PRVM_G_FLOAT(OFS_PARM1);
941 ent = PRVM_NEXT_EDICT(prog->edicts);
942 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
944 prog->xfunction->builtinsprofile++;
945 if (ent->priv.required->free)
947 if (PRVM_E_FLOAT(ent,f) != s)
950 PRVM_E_INT(ent,chain_of) = PRVM_EDICT_TO_PROG(chain);
954 VM_RETURN_EDICT(chain);
958 ========================
961 entity findflags(entity start, .float field, float match)
962 ========================
964 // LordHavoc: search for flags in float fields
965 void VM_findflags (void)
972 VM_SAFEPARMCOUNT(3, VM_findflags);
975 e = PRVM_G_EDICTNUM(OFS_PARM0);
976 f = PRVM_G_INT(OFS_PARM1);
977 s = (int)PRVM_G_FLOAT(OFS_PARM2);
979 for (e++ ; e < prog->num_edicts ; e++)
981 prog->xfunction->builtinsprofile++;
982 ed = PRVM_EDICT_NUM(e);
983 if (ed->priv.required->free)
985 if (!PRVM_E_FLOAT(ed,f))
987 if ((int)PRVM_E_FLOAT(ed,f) & s)
994 VM_RETURN_EDICT(prog->edicts);
998 ========================
1001 entity findchainflags(.float field, float match)
1002 ========================
1004 // LordHavoc: chained search for flags in float fields
1005 void VM_findchainflags (void)
1011 prvm_edict_t *ent, *chain;
1013 VM_SAFEPARMCOUNT(2, VM_findchainflags);
1015 if(!prog->flag & PRVM_FE_CHAIN)
1016 PRVM_ERROR("VM_findchainflags: %s doesnt have a chain field !", PRVM_NAME);
1018 chain_of = PRVM_ED_FindField("chain")->ofs;
1020 chain = (prvm_edict_t *)prog->edicts;
1022 f = PRVM_G_INT(OFS_PARM0);
1023 s = (int)PRVM_G_FLOAT(OFS_PARM1);
1025 ent = PRVM_NEXT_EDICT(prog->edicts);
1026 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
1028 prog->xfunction->builtinsprofile++;
1029 if (ent->priv.required->free)
1031 if (!PRVM_E_FLOAT(ent,f))
1033 if (!((int)PRVM_E_FLOAT(ent,f) & s))
1036 PRVM_E_INT(ent,chain_of) = PRVM_EDICT_TO_PROG(chain);
1040 VM_RETURN_EDICT(chain);
1050 void VM_coredump (void)
1052 VM_SAFEPARMCOUNT(0,VM_coredump);
1054 Cbuf_AddText("prvm_edicts ");
1055 Cbuf_AddText(PRVM_NAME);
1066 void PRVM_StackTrace(void);
1067 void VM_stackdump (void)
1069 VM_SAFEPARMCOUNT(0, VM_stackdump);
1084 VM_SAFEPARMCOUNT(0, VM_crash);
1086 PRVM_ERROR("Crash called by %s",PRVM_NAME);
1096 void VM_traceon (void)
1098 VM_SAFEPARMCOUNT(0,VM_traceon);
1110 void VM_traceoff (void)
1112 VM_SAFEPARMCOUNT(0,VM_traceoff);
1114 prog->trace = false;
1124 void VM_eprint (void)
1126 VM_SAFEPARMCOUNT(1,VM_eprint);
1128 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) = (int)(f + 0.5);
1148 PRVM_G_FLOAT(OFS_RETURN) = (int)(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 Con_Printf("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 Con_Printf("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)
1496 memset(VM_FILES, 0, sizeof(qfile_t*[MAX_VMFILES]));
1499 void VM_Files_CloseAll(void)
1502 for (i = 0;i < MAX_VMFILES;i++)
1505 FS_Close(VM_FILES[i]);
1506 //VM_FILES[i] = NULL;
1508 memset(VM_FILES,0,sizeof(qfile_t*[MAX_VMFILES])); // this should be faster (is it ?)
1511 qfile_t *VM_GetFileHandle( int index )
1513 if (index < 0 || index >= MAX_VMFILES)
1515 Con_Printf("VM_GetFileHandle: invalid file handle %i used in %s\n", index, PRVM_NAME);
1518 if (VM_FILES[index] == NULL)
1520 Con_Printf("VM_GetFileHandle: no such file handle %i (or file has been closed) in %s\n", index, PRVM_NAME);
1523 return VM_FILES[index];
1530 float fopen(string filename, float mode)
1533 // float(string filename, float mode) fopen = #110;
1534 // opens a file inside quake/gamedir/data/ (mode is FILE_READ, FILE_APPEND, or FILE_WRITE),
1535 // returns fhandle >= 0 if successful, or fhandle < 0 if unable to open file for any reason
1539 const char *modestring, *filename;
1541 VM_SAFEPARMCOUNT(2,VM_fopen);
1543 for (filenum = 0;filenum < MAX_VMFILES;filenum++)
1544 if (VM_FILES[filenum] == NULL)
1546 if (filenum >= MAX_VMFILES)
1548 Con_Printf("VM_fopen: %s ran out of file handles (%i)\n", PRVM_NAME, MAX_VMFILES);
1549 PRVM_G_FLOAT(OFS_RETURN) = -2;
1552 mode = (int)PRVM_G_FLOAT(OFS_PARM1);
1555 case 0: // FILE_READ
1558 case 1: // FILE_APPEND
1561 case 2: // FILE_WRITE
1565 Con_Printf("VM_fopen: %s: no such mode %i (valid: 0 = read, 1 = append, 2 = write)\n", PRVM_NAME, mode);
1566 PRVM_G_FLOAT(OFS_RETURN) = -3;
1569 filename = PRVM_G_STRING(OFS_PARM0);
1571 VM_FILES[filenum] = FS_Open(va("data/%s", filename), modestring, false, false);
1572 if (VM_FILES[filenum] == NULL && mode == 0)
1573 VM_FILES[filenum] = FS_Open(va("%s", filename), modestring, false, false);
1575 if (VM_FILES[filenum] == NULL)
1577 if (developer.integer >= 10)
1578 Con_Printf("VM_fopen: %s: %s mode %s failed\n", PRVM_NAME, filename, modestring);
1579 PRVM_G_FLOAT(OFS_RETURN) = -1;
1583 if (developer.integer >= 10)
1584 Con_Printf("VM_fopen: %s: %s mode %s opened as #%i\n", PRVM_NAME, filename, modestring, filenum);
1585 PRVM_G_FLOAT(OFS_RETURN) = filenum;
1593 fclose(float fhandle)
1596 //void(float fhandle) fclose = #111; // closes a file
1597 void VM_fclose(void)
1601 VM_SAFEPARMCOUNT(1,VM_fclose);
1603 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1604 if (filenum < 0 || filenum >= MAX_VMFILES)
1606 Con_Printf("VM_fclose: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1609 if (VM_FILES[filenum] == NULL)
1611 Con_Printf("VM_fclose: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1614 if (developer.integer >= 10)
1615 Con_Printf("VM_fclose: %s: #%i closed\n", PRVM_NAME, filenum);
1616 FS_Close(VM_FILES[filenum]);
1617 VM_FILES[filenum] = NULL;
1624 string fgets(float fhandle)
1627 //string(float fhandle) fgets = #112; // reads a line of text from the file and returns as a tempstring
1631 static char string[VM_STRINGTEMP_LENGTH];
1634 VM_SAFEPARMCOUNT(1,VM_fgets);
1636 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1637 if (filenum < 0 || filenum >= MAX_VMFILES)
1639 Con_Printf("VM_fgets: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1642 if (VM_FILES[filenum] == NULL)
1644 Con_Printf("VM_fgets: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1650 c = FS_Getc(VM_FILES[filenum]);
1651 if (c == '\r' || c == '\n' || c < 0)
1653 if (end < VM_STRINGTEMP_LENGTH - 1)
1657 // remove \n following \r
1660 c = FS_Getc(VM_FILES[filenum]);
1662 FS_UnGetc(VM_FILES[filenum], (unsigned char)c);
1664 if (developer.integer >= 100)
1665 Con_Printf("fgets: %s: %s\n", PRVM_NAME, string);
1667 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(string);
1669 PRVM_G_INT(OFS_RETURN) = 0;
1676 fputs(float fhandle, string s)
1679 //void(float fhandle, string s) fputs = #113; // writes a line of text to the end of the file
1683 char string[VM_STRINGTEMP_LENGTH];
1686 VM_SAFEPARMCOUNT(2,VM_fputs);
1688 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1689 if (filenum < 0 || filenum >= MAX_VMFILES)
1691 Con_Printf("VM_fputs: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1694 if (VM_FILES[filenum] == NULL)
1696 Con_Printf("VM_fputs: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1699 VM_VarString(1, string, sizeof(string));
1700 if ((stringlength = (int)strlen(string)))
1701 FS_Write(VM_FILES[filenum], string, stringlength);
1702 if (developer.integer >= 100)
1703 Con_Printf("fputs: %s: %s\n", PRVM_NAME, string);
1710 float strlen(string s)
1713 //float(string s) strlen = #114; // returns how many characters are in a string
1714 void VM_strlen(void)
1718 VM_SAFEPARMCOUNT(1,VM_strlen);
1720 s = PRVM_G_STRING(OFS_PARM0);
1722 PRVM_G_FLOAT(OFS_RETURN) = strlen(s);
1724 PRVM_G_FLOAT(OFS_RETURN) = 0;
1731 string strcat(string,string,...[string])
1734 //string(string s1, string s2) strcat = #115;
1735 // concatenates two strings (for example "abc", "def" would return "abcdef")
1736 // and returns as a tempstring
1737 void VM_strcat(void)
1742 PRVM_ERROR("VM_strcat wrong parameter count (min. 1 expected ) !");
1744 s = VM_GetTempString();
1745 VM_VarString(0, s, VM_STRINGTEMP_LENGTH);
1746 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(s);
1753 string substring(string s, float start, float length)
1756 // string(string s, float start, float length) substring = #116;
1757 // returns a section of a string as a tempstring
1758 void VM_substring(void)
1760 int i, start, length;
1764 VM_SAFEPARMCOUNT(3,VM_substring);
1766 string = VM_GetTempString();
1767 s = PRVM_G_STRING(OFS_PARM0);
1768 start = (int)PRVM_G_FLOAT(OFS_PARM1);
1769 length = (int)PRVM_G_FLOAT(OFS_PARM2);
1772 for (i = 0;i < start && *s;i++, s++);
1773 for (i = 0;i < VM_STRINGTEMP_LENGTH - 1 && *s && i < length;i++, s++)
1776 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(string);
1783 vector stov(string s)
1786 //vector(string s) stov = #117; // returns vector value from a string
1789 char string[VM_STRINGTEMP_LENGTH];
1791 VM_SAFEPARMCOUNT(1,VM_stov);
1793 VM_VarString(0, string, sizeof(string));
1794 Math_atov(string, PRVM_G_VECTOR(OFS_RETURN));
1801 string strzone(string s)
1804 //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)
1805 void VM_strzone(void)
1808 char string[VM_STRINGTEMP_LENGTH];
1810 VM_SAFEPARMCOUNT(1,VM_strzone);
1812 VM_VarString(0, string, sizeof(string));
1813 PRVM_G_INT(OFS_RETURN) = PRVM_AllocString(strlen(string) + 1, &out);
1814 strcpy(out, string);
1824 //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!!!)
1825 void VM_strunzone(void)
1827 VM_SAFEPARMCOUNT(1,VM_strunzone);
1828 PRVM_FreeString(PRVM_G_INT(OFS_PARM0));
1833 VM_command (used by client and menu)
1835 clientcommand(float client, string s) (for client and menu)
1838 //void(entity e, string s) clientcommand = #440; // executes a command string as if it came from the specified client
1839 //this function originally written by KrimZon, made shorter by LordHavoc
1840 void VM_clcommand (void)
1842 client_t *temp_client;
1845 VM_SAFEPARMCOUNT(2,VM_clcommand);
1847 i = (int)PRVM_G_FLOAT(OFS_PARM0);
1848 if (!sv.active || i < 0 || i >= svs.maxclients || !svs.clients[i].active)
1850 Con_Printf("VM_clientcommand: %s: invalid client/server is not active !\n", PRVM_NAME);
1854 temp_client = host_client;
1855 host_client = svs.clients + i;
1856 Cmd_ExecuteString (PRVM_G_STRING(OFS_PARM1), src_client);
1857 host_client = temp_client;
1865 float tokenize(string s)
1868 //float(string s) tokenize = #441; // takes apart a string into individal words (access them with argv), returns how many
1869 //this function originally written by KrimZon, made shorter by LordHavoc
1870 //20040203: rewritten by LordHavoc (no longer uses allocations)
1872 char *tokens[256], tokenbuf[MAX_INPUTLINE];
1873 void VM_tokenize (void)
1878 VM_SAFEPARMCOUNT(1,VM_tokenize);
1880 p = PRVM_G_STRING(OFS_PARM0);
1884 while(COM_ParseToken(&p, false))
1886 if (num_tokens >= (int)(sizeof(tokens)/sizeof(tokens[0])))
1888 if (pos + strlen(com_token) + 1 > sizeof(tokenbuf))
1890 tokens[num_tokens++] = tokenbuf + pos;
1891 strcpy(tokenbuf + pos, com_token);
1892 pos += strlen(com_token) + 1;
1895 PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
1898 //string(float n) argv = #442; // returns a word from the tokenized string (returns nothing for an invalid index)
1899 //this function originally written by KrimZon, made shorter by LordHavoc
1904 VM_SAFEPARMCOUNT(1,VM_argv);
1906 token_num = (int)PRVM_G_FLOAT(OFS_PARM0);
1908 if (token_num >= 0 && token_num < num_tokens)
1909 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(tokens[token_num]);
1911 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(NULL);
1915 //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)
1916 void PF_setattachment (void)
1918 prvm_edict_t *e = PRVM_G_EDICT(OFS_PARM0);
1919 prvm_edict_t *tagentity = PRVM_G_EDICT(OFS_PARM1);
1920 char *tagname = PRVM_G_STRING(OFS_PARM2);
1925 if (tagentity == NULL)
1926 tagentity = prog->edicts;
1928 v = PRVM_GETEDICTFIELDVALUE(e, eval_tag_entity);
1930 fields.server->edict = PRVM_EDICT_TO_PROG(tagentity);
1932 v = PRVM_GETEDICTFIELDVALUE(e, eval_tag_index);
1934 fields.server->_float = 0;
1935 if (tagentity != NULL && tagentity != prog->edicts && tagname && tagname[0])
1937 modelindex = (int)tagentity->fields.server->modelindex;
1938 if (modelindex >= 0 && modelindex < MAX_MODELS)
1940 model = sv.models[modelindex];
1941 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)
1942 for (i = 0;i < model->data_overridetagnamesforskin[(unsigned int)tagentity->fields.server->skin].num_overridetagnames;i++)
1943 if (!strcmp(tagname, model->data_overridetagnamesforskin[(unsigned int)tagentity->fields.server->skin].data_overridetagnames[i].name))
1944 fields.server->_float = i + 1;
1945 // FIXME: use a model function to get tag info (need to handle skeletal)
1946 if (fields.server->_float == 0 && model->num_tags)
1947 for (i = 0;i < model->num_tags;i++)
1948 if (!strcmp(tagname, model->data_tags[i].name))
1949 fields.server->_float = i + 1;
1950 if (fields.server->_float == 0)
1951 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);
1954 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));
1965 void VM_isserver(void)
1967 VM_SAFEPARMCOUNT(0,VM_serverstate);
1969 PRVM_G_FLOAT(OFS_RETURN) = sv.active;
1979 void VM_clientcount(void)
1981 VM_SAFEPARMCOUNT(0,VM_clientcount);
1983 PRVM_G_FLOAT(OFS_RETURN) = svs.maxclients;
1993 void VM_clientstate(void)
1995 VM_SAFEPARMCOUNT(0,VM_clientstate);
1997 PRVM_G_FLOAT(OFS_RETURN) = cls.state;
2004 float getostype(void)
2006 */ // not used at the moment -> not included in the common list
2007 void VM_getostype(void)
2009 VM_SAFEPARMCOUNT(0,VM_getostype);
2014 OS_MAC - not supported
2018 PRVM_G_FLOAT(OFS_RETURN) = 0;
2019 #elif defined(MACOSX)
2020 PRVM_G_FLOAT(OFS_RETURN) = 2;
2022 PRVM_G_FLOAT(OFS_RETURN) = 1;
2030 vector getmousepos()
2033 void VM_getmousepos(void)
2036 VM_SAFEPARMCOUNT(0,VM_getmousepos);
2038 PRVM_G_VECTOR(OFS_RETURN)[0] = in_mouse_x * vid_conwidth.integer / vid.width;
2039 PRVM_G_VECTOR(OFS_RETURN)[1] = in_mouse_y * vid_conheight.integer / vid.height;
2040 PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
2050 void VM_gettime(void)
2052 VM_SAFEPARMCOUNT(0,VM_gettime);
2054 PRVM_G_FLOAT(OFS_RETURN) = (float) *prog->time;
2061 loadfromdata(string data)
2064 void VM_loadfromdata(void)
2066 VM_SAFEPARMCOUNT(1,VM_loadentsfromfile);
2068 PRVM_ED_LoadFromFile(PRVM_G_STRING(OFS_PARM0));
2072 ========================
2075 parseentitydata(entity ent, string data)
2076 ========================
2078 void VM_parseentitydata(void)
2083 VM_SAFEPARMCOUNT(2, VM_parseentitydata);
2085 // get edict and test it
2086 ent = PRVM_G_EDICT(OFS_PARM0);
2087 if (ent->priv.required->free)
2088 PRVM_ERROR ("VM_parseentitydata: %s: Can only set already spawned entities (entity %i is free)!", PRVM_NAME, PRVM_NUM_FOR_EDICT(ent));
2090 data = PRVM_G_STRING(OFS_PARM1);
2092 // parse the opening brace
2093 if (!COM_ParseTokenConsole(&data) || com_token[0] != '{' )
2094 PRVM_ERROR ("VM_parseentitydata: %s: Couldn't parse entity data:\n%s", PRVM_NAME, data );
2096 PRVM_ED_ParseEdict (data, ent);
2103 loadfromfile(string file)
2106 void VM_loadfromfile(void)
2108 const char *filename;
2111 VM_SAFEPARMCOUNT(1,VM_loadfromfile);
2113 filename = PRVM_G_STRING(OFS_PARM0);
2114 // .. is parent directory on many platforms
2115 // / is parent directory on Amiga
2116 // : is root of drive on Amiga (also used as a directory separator on Mac, but / works there too, so that's a bad idea)
2117 // \ is a windows-ism (so it's naughty to use it, / works on all platforms)
2118 if ((filename[0] == '.' && filename[1] == '.') || filename[0] == '/' || strrchr(filename, ':') || strrchr(filename, '\\'))
2120 Con_Printf("VM_loadfromfile: %s dangerous or non-portable filename \"%s\" not allowed. (contains : or \\ or begins with .. or /)\n", PRVM_NAME, filename);
2121 PRVM_G_FLOAT(OFS_RETURN) = -4;
2125 // not conform with VM_fopen
2126 data = (char *)FS_LoadFile(filename, tempmempool, false, NULL);
2128 PRVM_G_FLOAT(OFS_RETURN) = -1;
2130 PRVM_ED_LoadFromFile(data);
2141 float mod(float val, float m)
2144 void VM_modulo(void)
2147 VM_SAFEPARMCOUNT(2,VM_module);
2149 val = (int) PRVM_G_FLOAT(OFS_PARM0);
2150 m = (int) PRVM_G_FLOAT(OFS_PARM1);
2152 PRVM_G_FLOAT(OFS_RETURN) = (float) (val % m);
2155 void VM_Search_Init(void)
2157 memset(VM_SEARCHLIST,0,sizeof(fssearch_t*[MAX_VMSEARCHES]));
2160 void VM_Search_Reset(void)
2163 // reset the fssearch list
2164 for(i = 0; i < MAX_VMSEARCHES; i++)
2165 if(VM_SEARCHLIST[i])
2166 FS_FreeSearch(VM_SEARCHLIST[i]);
2167 memset(VM_SEARCHLIST,0,sizeof(fssearch_t*[MAX_VMSEARCHES]));
2174 float search_begin(string pattern, float caseinsensitive, float quiet)
2177 void VM_search_begin(void)
2180 const char *pattern;
2181 int caseinsens, quiet;
2183 VM_SAFEPARMCOUNT(3, VM_search_begin);
2185 pattern = PRVM_G_STRING(OFS_PARM0);
2187 VM_CheckEmptyString(pattern);
2189 caseinsens = (int)PRVM_G_FLOAT(OFS_PARM1);
2190 quiet = (int)PRVM_G_FLOAT(OFS_PARM2);
2192 for(handle = 0; handle < MAX_VMSEARCHES; handle++)
2193 if(!VM_SEARCHLIST[handle])
2196 if(handle >= MAX_VMSEARCHES)
2198 Con_Printf("VM_search_begin: %s ran out of search handles (%i)\n", PRVM_NAME, MAX_VMSEARCHES);
2199 PRVM_G_FLOAT(OFS_RETURN) = -2;
2203 if(!(VM_SEARCHLIST[handle] = FS_Search(pattern,caseinsens, quiet)))
2204 PRVM_G_FLOAT(OFS_RETURN) = -1;
2206 PRVM_G_FLOAT(OFS_RETURN) = handle;
2213 void search_end(float handle)
2216 void VM_search_end(void)
2219 VM_SAFEPARMCOUNT(1, VM_search_end);
2221 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
2223 if(handle < 0 || handle >= MAX_VMSEARCHES)
2225 Con_Printf("VM_search_end: invalid handle %i used in %s\n", handle, PRVM_NAME);
2228 if(VM_SEARCHLIST[handle] == NULL)
2230 Con_Printf("VM_search_end: no such handle %i in %s\n", handle, PRVM_NAME);
2234 FS_FreeSearch(VM_SEARCHLIST[handle]);
2235 VM_SEARCHLIST[handle] = NULL;
2242 float search_getsize(float handle)
2245 void VM_search_getsize(void)
2248 VM_SAFEPARMCOUNT(1, VM_M_search_getsize);
2250 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
2252 if(handle < 0 || handle >= MAX_VMSEARCHES)
2254 Con_Printf("VM_search_getsize: invalid handle %i used in %s\n", handle, PRVM_NAME);
2257 if(VM_SEARCHLIST[handle] == NULL)
2259 Con_Printf("VM_search_getsize: no such handle %i in %s\n", handle, PRVM_NAME);
2263 PRVM_G_FLOAT(OFS_RETURN) = VM_SEARCHLIST[handle]->numfilenames;
2268 VM_search_getfilename
2270 string search_getfilename(float handle, float num)
2273 void VM_search_getfilename(void)
2275 int handle, filenum;
2277 VM_SAFEPARMCOUNT(2, VM_search_getfilename);
2279 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
2280 filenum = (int)PRVM_G_FLOAT(OFS_PARM1);
2282 if(handle < 0 || handle >= MAX_VMSEARCHES)
2284 Con_Printf("VM_search_getfilename: invalid handle %i used in %s\n", handle, PRVM_NAME);
2287 if(VM_SEARCHLIST[handle] == NULL)
2289 Con_Printf("VM_search_getfilename: no such handle %i in %s\n", handle, PRVM_NAME);
2292 if(filenum < 0 || filenum >= VM_SEARCHLIST[handle]->numfilenames)
2294 Con_Printf("VM_search_getfilename: invalid filenum %i in %s\n", filenum, PRVM_NAME);
2298 tmp = VM_GetTempString();
2299 strcpy(tmp, VM_SEARCHLIST[handle]->filenames[filenum]);
2301 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(tmp);
2308 string chr(float ascii)
2314 VM_SAFEPARMCOUNT(1, VM_chr);
2316 tmp = VM_GetTempString();
2317 tmp[0] = (unsigned char) PRVM_G_FLOAT(OFS_PARM0);
2320 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(tmp);
2323 //=============================================================================
2324 // Draw builtins (client & menu)
2330 float iscachedpic(string pic)
2333 void VM_iscachedpic(void)
2335 VM_SAFEPARMCOUNT(1,VM_iscachedpic);
2337 // drawq hasnt such a function, thus always return true
2338 PRVM_G_FLOAT(OFS_RETURN) = false;
2345 string precache_pic(string pic)
2348 void VM_precache_pic(void)
2352 VM_SAFEPARMCOUNT(1, VM_precache_pic);
2354 s = PRVM_G_STRING(OFS_PARM0);
2355 PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
2358 PRVM_ERROR ("VM_precache_pic: %s: NULL", PRVM_NAME);
2360 VM_CheckEmptyString (s);
2362 // AK Draw_CachePic is supposed to always return a valid pointer
2363 if( Draw_CachePic(s, false)->tex == r_texture_notexture )
2364 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(NULL);
2374 void VM_freepic(void)
2378 VM_SAFEPARMCOUNT(1,VM_freepic);
2380 s = PRVM_G_STRING(OFS_PARM0);
2383 PRVM_ERROR ("VM_freepic: %s: NULL");
2385 VM_CheckEmptyString (s);
2394 float drawcharacter(vector position, float character, vector scale, vector rgb, float alpha, float flag)
2397 void VM_drawcharacter(void)
2399 float *pos,*scale,*rgb;
2402 VM_SAFEPARMCOUNT(6,VM_drawcharacter);
2404 character = (char) PRVM_G_FLOAT(OFS_PARM1);
2407 Con_Printf("VM_drawcharacter: %s passed null character !\n",PRVM_NAME);
2408 PRVM_G_FLOAT(OFS_RETURN) = -1;
2412 pos = PRVM_G_VECTOR(OFS_PARM0);
2413 scale = PRVM_G_VECTOR(OFS_PARM2);
2414 rgb = PRVM_G_VECTOR(OFS_PARM3);
2415 flag = (int)PRVM_G_FLOAT(OFS_PARM5);
2417 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2419 Con_Printf("VM_drawcharacter: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2420 PRVM_G_FLOAT(OFS_RETURN) = -2;
2424 if(pos[2] || scale[2])
2425 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")));
2427 if(!scale[0] || !scale[1])
2429 Con_Printf("VM_drawcharacter: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
2430 PRVM_G_FLOAT(OFS_RETURN) = -3;
2434 DrawQ_String (pos[0], pos[1], &character, 1, scale[0], scale[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM4), flag);
2435 PRVM_G_FLOAT(OFS_RETURN) = 1;
2442 float drawstring(vector position, string text, vector scale, vector rgb, float alpha, float flag)
2445 void VM_drawstring(void)
2447 float *pos,*scale,*rgb;
2450 VM_SAFEPARMCOUNT(6,VM_drawstring);
2452 string = PRVM_G_STRING(OFS_PARM1);
2455 Con_Printf("VM_drawstring: %s passed null string !\n",PRVM_NAME);
2456 PRVM_G_FLOAT(OFS_RETURN) = -1;
2460 //VM_CheckEmptyString(string); Why should it be checked - perhaps the menu wants to support the precolored letters, too?
2462 pos = PRVM_G_VECTOR(OFS_PARM0);
2463 scale = PRVM_G_VECTOR(OFS_PARM2);
2464 rgb = PRVM_G_VECTOR(OFS_PARM3);
2465 flag = (int)PRVM_G_FLOAT(OFS_PARM5);
2467 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2469 Con_Printf("VM_drawstring: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2470 PRVM_G_FLOAT(OFS_RETURN) = -2;
2474 if(!scale[0] || !scale[1])
2476 Con_Printf("VM_drawstring: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
2477 PRVM_G_FLOAT(OFS_RETURN) = -3;
2481 if(pos[2] || scale[2])
2482 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")));
2484 DrawQ_String (pos[0], pos[1], string, 0, scale[0], scale[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM4), flag);
2485 PRVM_G_FLOAT(OFS_RETURN) = 1;
2491 float drawpic(vector position, string pic, vector size, vector rgb, float alpha, float flag)
2494 void VM_drawpic(void)
2496 const char *picname;
2497 float *size, *pos, *rgb;
2500 VM_SAFEPARMCOUNT(6,VM_drawpic);
2502 picname = PRVM_G_STRING(OFS_PARM1);
2506 Con_Printf("VM_drawpic: %s passed null picture name !\n", PRVM_NAME);
2507 PRVM_G_FLOAT(OFS_RETURN) = -1;
2511 VM_CheckEmptyString (picname);
2513 // is pic cached ? no function yet for that
2516 Con_Printf("VM_drawpic: %s: %s not cached !\n", PRVM_NAME, picname);
2517 PRVM_G_FLOAT(OFS_RETURN) = -4;
2521 pos = PRVM_G_VECTOR(OFS_PARM0);
2522 size = PRVM_G_VECTOR(OFS_PARM2);
2523 rgb = PRVM_G_VECTOR(OFS_PARM3);
2524 flag = (int) PRVM_G_FLOAT(OFS_PARM5);
2526 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2528 Con_Printf("VM_drawstring: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2529 PRVM_G_FLOAT(OFS_RETURN) = -2;
2533 if(pos[2] || size[2])
2534 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")));
2536 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);
2537 PRVM_G_FLOAT(OFS_RETURN) = 1;
2544 float drawfill(vector position, vector size, vector rgb, float alpha, float flag)
2547 void VM_drawfill(void)
2549 float *size, *pos, *rgb;
2552 VM_SAFEPARMCOUNT(5,VM_drawfill);
2555 pos = PRVM_G_VECTOR(OFS_PARM0);
2556 size = PRVM_G_VECTOR(OFS_PARM1);
2557 rgb = PRVM_G_VECTOR(OFS_PARM2);
2558 flag = (int) PRVM_G_FLOAT(OFS_PARM4);
2560 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2562 Con_Printf("VM_drawstring: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2563 PRVM_G_FLOAT(OFS_RETURN) = -2;
2567 if(pos[2] || size[2])
2568 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")));
2570 DrawQ_Pic(pos[0], pos[1], NULL, size[0], size[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM3), flag);
2571 PRVM_G_FLOAT(OFS_RETURN) = 1;
2578 drawsetcliparea(float x, float y, float width, float height)
2581 void VM_drawsetcliparea(void)
2584 VM_SAFEPARMCOUNT(4,VM_drawsetcliparea);
2586 x = bound(0, PRVM_G_FLOAT(OFS_PARM0), vid_conwidth.integer);
2587 y = bound(0, PRVM_G_FLOAT(OFS_PARM1), vid_conheight.integer);
2588 w = bound(0, PRVM_G_FLOAT(OFS_PARM2) + PRVM_G_FLOAT(OFS_PARM0) - x, (vid_conwidth.integer - x));
2589 h = bound(0, PRVM_G_FLOAT(OFS_PARM3) + PRVM_G_FLOAT(OFS_PARM1) - y, (vid_conheight.integer - y));
2591 DrawQ_SetClipArea(x, y, w, h);
2596 VM_drawresetcliparea
2601 void VM_drawresetcliparea(void)
2603 VM_SAFEPARMCOUNT(0,VM_drawresetcliparea);
2605 DrawQ_ResetClipArea();
2612 vector getimagesize(string pic)
2615 void VM_getimagesize(void)
2620 VM_SAFEPARMCOUNT(1,VM_getimagesize);
2622 p = PRVM_G_STRING(OFS_PARM0);
2625 PRVM_ERROR("VM_getimagepos: %s passed null picture name !", PRVM_NAME);
2627 VM_CheckEmptyString (p);
2629 pic = Draw_CachePic (p, false);
2631 PRVM_G_VECTOR(OFS_RETURN)[0] = pic->width;
2632 PRVM_G_VECTOR(OFS_RETURN)[1] = pic->height;
2633 PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
2640 string keynumtostring(float keynum)
2643 void VM_keynumtostring (void)
2647 VM_SAFEPARMCOUNT(1, VM_keynumtostring);
2649 keynum = (int)PRVM_G_FLOAT(OFS_PARM0);
2651 tmp = VM_GetTempString();
2653 strcpy(tmp, Key_KeynumToString(keynum));
2655 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(tmp);
2662 float stringtokeynum(string key)
2665 void VM_stringtokeynum (void)
2668 VM_SAFEPARMCOUNT( 1, VM_keynumtostring );
2670 str = PRVM_G_STRING( OFS_PARM0 );
2672 PRVM_G_INT(OFS_RETURN) = Key_StringToKeynum( str );
2675 // CL_Video interface functions
2678 ========================
2681 float cin_open(string file, string name)
2682 ========================
2684 void VM_cin_open( void )
2689 VM_SAFEPARMCOUNT( 2, VM_cin_open );
2691 file = PRVM_G_STRING( OFS_PARM0 );
2692 name = PRVM_G_STRING( OFS_PARM1 );
2694 VM_CheckEmptyString( file );
2695 VM_CheckEmptyString( name );
2697 if( CL_OpenVideo( file, name, MENUOWNER ) )
2698 PRVM_G_FLOAT( OFS_RETURN ) = 1;
2700 PRVM_G_FLOAT( OFS_RETURN ) = 0;
2704 ========================
2707 void cin_close(string name)
2708 ========================
2710 void VM_cin_close( void )
2714 VM_SAFEPARMCOUNT( 1, VM_cin_close );
2716 name = PRVM_G_STRING( OFS_PARM0 );
2717 VM_CheckEmptyString( name );
2719 CL_CloseVideo( CL_GetVideoByName( name ) );
2723 ========================
2725 void cin_setstate(string name, float type)
2726 ========================
2728 void VM_cin_setstate( void )
2731 clvideostate_t state;
2734 VM_SAFEPARMCOUNT( 2, VM_cin_netstate );
2736 name = PRVM_G_STRING( OFS_PARM0 );
2737 VM_CheckEmptyString( name );
2739 state = (clvideostate_t)((int)PRVM_G_FLOAT( OFS_PARM1 ));
2741 video = CL_GetVideoByName( name );
2742 if( video && state > CLVIDEO_UNUSED && state < CLVIDEO_STATECOUNT )
2743 CL_SetVideoState( video, state );
2747 ========================
2750 float cin_getstate(string name)
2751 ========================
2753 void VM_cin_getstate( void )
2758 VM_SAFEPARMCOUNT( 1, VM_cin_getstate );
2760 name = PRVM_G_STRING( OFS_PARM0 );
2761 VM_CheckEmptyString( name );
2763 video = CL_GetVideoByName( name );
2765 PRVM_G_FLOAT( OFS_RETURN ) = (int)video->state;
2767 PRVM_G_FLOAT( OFS_RETURN ) = 0;
2771 ========================
2774 void cin_restart(string name)
2775 ========================
2777 void VM_cin_restart( void )
2782 VM_SAFEPARMCOUNT( 1, VM_cin_restart );
2784 name = PRVM_G_STRING( OFS_PARM0 );
2785 VM_CheckEmptyString( name );
2787 video = CL_GetVideoByName( name );
2789 CL_RestartVideo( video );
2796 Writes new values for v_forward, v_up, and v_right based on the given forward vector
2797 vectorvectors(vector, vector)
2800 void VM_vectorvectors (void)
2802 VectorNormalize2(PRVM_G_VECTOR(OFS_PARM0), prog->globals.server->v_forward);
2803 VectorVectors(prog->globals.server->v_forward, prog->globals.server->v_right, prog->globals.server->v_up);
2807 ========================
2810 void drawline(float width, vector pos1, vector pos2, vector rgb, float alpha, float flags)
2811 ========================
2813 void VM_drawline (void)
2815 float *c1, *c2, *rgb;
2817 unsigned char flags;
2819 VM_SAFEPARMCOUNT(6, VM_drawline);
2820 width = PRVM_G_FLOAT(OFS_PARM0);
2821 c1 = PRVM_G_VECTOR(OFS_PARM1);
2822 c2 = PRVM_G_VECTOR(OFS_PARM2);
2823 rgb = PRVM_G_VECTOR(OFS_PARM3);
2824 alpha = PRVM_G_FLOAT(OFS_PARM4);
2825 flags = (int)PRVM_G_FLOAT(OFS_PARM5);
2826 DrawQ_Line(width, c1[0], c1[1], c2[0], c2[1], rgb[0], rgb[1], rgb[2], alpha, flags);
2829 //====================
2830 //QC POLYGON functions
2831 //====================
2836 float data[36]; //[515]: enough for polygons
2837 unsigned char flags; //[515]: + VM_POLYGON_2D and VM_POLYGON_FL4V flags
2840 //static float vm_polygon_linewidth = 1;
2841 static mempool_t *vm_polygons_pool = NULL;
2842 static unsigned char vm_current_vertices = 0;
2843 static qboolean vm_polygons_initialized = false;
2844 static vm_polygon_t *vm_polygons = NULL;
2845 static unsigned long vm_polygons_num = 0, vm_drawpolygons_num = 0; //[515]: ok long on 64bit ?
2846 static qboolean vm_polygonbegin = false; //[515]: for "no-crap-on-the-screen" check
2847 #define VM_DEFPOLYNUM 64 //[515]: enough for default ?
2849 #define VM_POLYGON_FL3V 16 //more than 2 vertices (used only for lines)
2850 #define VM_POLYGON_FLLINES 32
2851 #define VM_POLYGON_FL2D 64
2852 #define VM_POLYGON_FL4V 128 //4 vertices
2854 void VM_InitPolygons (void)
2856 vm_polygons_pool = Mem_AllocPool("VMPOLY", 0, NULL);
2857 vm_polygons = (vm_polygon_t *)Mem_Alloc(vm_polygons_pool, VM_DEFPOLYNUM*sizeof(vm_polygon_t));
2858 memset(vm_polygons, 0, VM_DEFPOLYNUM*sizeof(vm_polygon_t));
2859 vm_polygons_num = VM_DEFPOLYNUM;
2860 vm_drawpolygons_num = 0;
2861 vm_polygonbegin = false;
2862 vm_polygons_initialized = true;
2865 void VM_DrawPolygonCallback (const entity_render_t *ent, const rtlight_t *rtlight, int numsurfaces, int *surfacelist)
2867 int surfacelistindex;
2868 // LordHavoc: FIXME: this is stupid code
2869 for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
2871 const vm_polygon_t *p = &vm_polygons[surfacelist[surfacelistindex]];
2872 int flags = p->flags & 0x0f;
2874 if(flags == DRAWFLAG_ADDITIVE)
2875 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
2876 else if(flags == DRAWFLAG_MODULATE)
2877 GL_BlendFunc(GL_DST_COLOR, GL_ZERO);
2878 else if(flags == DRAWFLAG_2XMODULATE)
2879 GL_BlendFunc(GL_DST_COLOR,GL_SRC_COLOR);
2881 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
2883 R_Mesh_TexBind(0, R_GetTexture(p->tex));
2886 //[515]: is speed is max ?
2887 if(p->flags & VM_POLYGON_FLLINES) //[515]: lines
2889 qglLineWidth(p->data[13]);CHECKGLERROR
2890 qglBegin(GL_LINE_LOOP);
2891 qglTexCoord1f (p->data[12]);
2892 qglColor4f (p->data[20], p->data[21], p->data[22], p->data[23]);
2893 qglVertex3f (p->data[0] , p->data[1], p->data[2]);
2895 qglTexCoord1f (p->data[14]);
2896 qglColor4f (p->data[24], p->data[25], p->data[26], p->data[27]);
2897 qglVertex3f (p->data[3] , p->data[4], p->data[5]);
2899 if(p->flags & VM_POLYGON_FL3V)
2901 qglTexCoord1f (p->data[16]);
2902 qglColor4f (p->data[28], p->data[29], p->data[30], p->data[31]);
2903 qglVertex3f (p->data[6] , p->data[7], p->data[8]);
2905 if(p->flags & VM_POLYGON_FL4V)
2907 qglTexCoord1f (p->data[18]);
2908 qglColor4f (p->data[32], p->data[33], p->data[34], p->data[35]);
2909 qglVertex3f (p->data[9] , p->data[10], p->data[11]);
2917 qglBegin(GL_POLYGON);
2918 qglTexCoord2f (p->data[12], p->data[13]);
2919 qglColor4f (p->data[20], p->data[21], p->data[22], p->data[23]);
2920 qglVertex3f (p->data[0] , p->data[1], p->data[2]);
2922 qglTexCoord2f (p->data[14], p->data[15]);
2923 qglColor4f (p->data[24], p->data[25], p->data[26], p->data[27]);
2924 qglVertex3f (p->data[3] , p->data[4], p->data[5]);
2926 qglTexCoord2f (p->data[16], p->data[17]);
2927 qglColor4f (p->data[28], p->data[29], p->data[30], p->data[31]);
2928 qglVertex3f (p->data[6] , p->data[7], p->data[8]);
2930 if(p->flags & VM_POLYGON_FL4V)
2932 qglTexCoord2f (p->data[18], p->data[19]);
2933 qglColor4f (p->data[32], p->data[33], p->data[34], p->data[35]);
2934 qglVertex3f (p->data[9] , p->data[10], p->data[11]);
2942 void VM_AddPolygonTo2DScene (vm_polygon_t *p)
2944 drawqueuemesh_t mesh;
2945 static int picelements[6] = {0, 1, 2, 0, 2, 3};
2947 mesh.texture = p->tex;
2948 mesh.data_element3i = picelements;
2949 mesh.data_vertex3f = p->data;
2950 mesh.data_texcoord2f = p->data + 12;
2951 mesh.data_color4f = p->data + 20;
2952 if(p->flags & VM_POLYGON_FL4V)
2954 mesh.num_vertices = 4;
2955 mesh.num_triangles = 2;
2959 mesh.num_vertices = 3;
2960 mesh.num_triangles = 1;
2962 if(p->flags & VM_POLYGON_FLLINES) //[515]: lines
2963 DrawQ_LineLoop (&mesh, (p->flags&0x0f));
2965 DrawQ_Mesh (&mesh, (p->flags&0x0f));
2968 //void(string texturename, float flag, float 2d, float lines) R_BeginPolygon
2969 void VM_R_PolygonBegin (void)
2972 const char *picname;
2974 VM_SAFEPARMCOUNT(2, VM_R_PolygonBegin);
2976 if(!vm_polygons_initialized)
2980 Con_Printf("VM_R_PolygonBegin: called twice without VM_R_PolygonEnd after first\n");
2983 if(vm_drawpolygons_num >= vm_polygons_num)
2985 p = (vm_polygon_t *)Mem_Alloc(vm_polygons_pool, 2 * vm_polygons_num * sizeof(vm_polygon_t));
2986 memset(p, 0, 2 * vm_polygons_num * sizeof(vm_polygon_t));
2987 memcpy(p, vm_polygons, vm_polygons_num * sizeof(vm_polygon_t));
2988 Mem_Free(vm_polygons);
2990 vm_polygons_num *= 2;
2992 p = &vm_polygons[vm_drawpolygons_num];
2993 picname = PRVM_G_STRING(OFS_PARM0);
2995 p->tex = Draw_CachePic(picname, true)->tex;
2997 p->tex = r_texture_notexture;
2998 p->flags = (unsigned char)PRVM_G_FLOAT(OFS_PARM1);
2999 vm_current_vertices = 0;
3000 vm_polygonbegin = true;
3003 if(PRVM_G_FLOAT(OFS_PARM2))
3004 p->flags |= VM_POLYGON_FL2D;
3005 if(prog->argc >= 4 && PRVM_G_FLOAT(OFS_PARM3))
3007 p->data[13] = PRVM_G_FLOAT(OFS_PARM3); //[515]: linewidth
3008 p->flags |= VM_POLYGON_FLLINES;
3013 //void(vector org, vector texcoords, vector rgb, float alpha) R_PolygonVertex
3014 void VM_R_PolygonVertex (void)
3016 float *coords, *tx, *rgb, alpha;
3018 VM_SAFEPARMCOUNT(4, VM_R_PolygonVertex);
3020 if(!vm_polygonbegin)
3022 Con_Printf("VM_R_PolygonVertex: VM_R_PolygonBegin wasn't called\n");
3025 coords = PRVM_G_VECTOR(OFS_PARM0);
3026 tx = PRVM_G_VECTOR(OFS_PARM1);
3027 rgb = PRVM_G_VECTOR(OFS_PARM2);
3028 alpha = PRVM_G_FLOAT(OFS_PARM3);
3030 p = &vm_polygons[vm_drawpolygons_num];
3031 if(vm_current_vertices > 4)
3033 Con_Printf("VM_R_PolygonVertex: may have 4 vertices max\n");
3037 p->data[vm_current_vertices*3] = coords[0];
3038 p->data[1+vm_current_vertices*3] = coords[1];
3039 if(!(p->flags & VM_POLYGON_FL2D))
3040 p->data[2+vm_current_vertices*3] = coords[2];
3042 p->data[12+vm_current_vertices*2] = tx[0];
3043 if(!(p->flags & VM_POLYGON_FLLINES))
3044 p->data[13+vm_current_vertices*2] = tx[1];
3046 p->data[20+vm_current_vertices*4] = rgb[0];
3047 p->data[21+vm_current_vertices*4] = rgb[1];
3048 p->data[22+vm_current_vertices*4] = rgb[2];
3049 p->data[23+vm_current_vertices*4] = alpha;
3051 vm_current_vertices++;
3052 if(vm_current_vertices == 4)
3053 p->flags |= VM_POLYGON_FL4V;
3055 if(vm_current_vertices == 3)
3056 p->flags |= VM_POLYGON_FL3V;
3059 //void() R_EndPolygon
3060 void VM_R_PolygonEnd (void)
3062 if(!vm_polygonbegin)
3064 Con_Printf("VM_R_PolygonEnd: VM_R_PolygonBegin wasn't called\n");
3067 if(vm_current_vertices > 2 || (vm_current_vertices >= 2 && vm_polygons[vm_drawpolygons_num].flags & VM_POLYGON_FLLINES))
3069 if(vm_polygons[vm_drawpolygons_num].flags & VM_POLYGON_FL2D) //[515]: don't use qcpolygons memory if 2D
3070 VM_AddPolygonTo2DScene(&vm_polygons[vm_drawpolygons_num]);
3072 vm_drawpolygons_num++;
3075 Con_Printf("VM_R_PolygonEnd: %i vertices isn't a good choice\n", vm_current_vertices);
3076 vm_polygonbegin = false;
3079 void VM_AddPolygonsToMeshQueue (void)
3082 if(!vm_drawpolygons_num)
3084 for(i = 0;i < vm_drawpolygons_num;i++)
3085 VM_DrawPolygonCallback(NULL, NULL, i, NULL);
3086 vm_drawpolygons_num = 0;
3092 // float(float number, float quantity) bitshift (EXT_BITSHIFT)
3093 void VM_bitshift (void)
3096 VM_SAFEPARMCOUNT(2, VM_bitshift);
3098 n1 = (int)fabs((int)PRVM_G_FLOAT(OFS_PARM0));
3099 n2 = (int)PRVM_G_FLOAT(OFS_PARM1);
3101 PRVM_G_FLOAT(OFS_RETURN) = n1;
3104 PRVM_G_FLOAT(OFS_RETURN) = (n1 >> -n2);
3106 PRVM_G_FLOAT(OFS_RETURN) = (n1 << n2);
3109 ////////////////////////////////////////
3110 // AltString functions
3111 ////////////////////////////////////////
3114 ========================
3117 float altstr_count(string)
3118 ========================
3120 void VM_altstr_count( void )
3122 const char *altstr, *pos;
3125 VM_SAFEPARMCOUNT( 1, VM_altstr_count );
3127 altstr = PRVM_G_STRING( OFS_PARM0 );
3128 //VM_CheckEmptyString( altstr );
3130 for( count = 0, pos = altstr ; *pos ; pos++ ) {
3131 if( *pos == '\\' ) {
3135 } else if( *pos == '\'' ) {
3140 PRVM_G_FLOAT( OFS_RETURN ) = (float) (count / 2);
3144 ========================
3147 string altstr_prepare(string)
3148 ========================
3150 void VM_altstr_prepare( void )
3153 const char *instr, *in;
3156 VM_SAFEPARMCOUNT( 1, VM_altstr_prepare );
3158 instr = PRVM_G_STRING( OFS_PARM0 );
3159 //VM_CheckEmptyString( instr );
3160 outstr = VM_GetTempString();
3162 for( out = outstr, in = instr, size = VM_STRINGTEMP_LENGTH - 1 ; size && *in ; size--, in++, out++ )
3171 PRVM_G_INT( OFS_RETURN ) = PRVM_SetEngineString( outstr );
3175 ========================
3178 string altstr_get(string, float)
3179 ========================
3181 void VM_altstr_get( void )
3183 const char *altstr, *pos;
3187 VM_SAFEPARMCOUNT( 2, VM_altstr_get );
3189 altstr = PRVM_G_STRING( OFS_PARM0 );
3190 //VM_CheckEmptyString( altstr );
3192 count = (int)PRVM_G_FLOAT( OFS_PARM1 );
3193 count = count * 2 + 1;
3195 for( pos = altstr ; *pos && count ; pos++ )
3196 if( *pos == '\\' ) {
3199 } else if( *pos == '\'' )
3203 PRVM_G_INT( OFS_RETURN ) = PRVM_SetEngineString( NULL );
3207 outstr = VM_GetTempString();
3208 for( out = outstr, size = VM_STRINGTEMP_LENGTH - 1 ; size && *pos ; size--, pos++, out++ )
3209 if( *pos == '\\' ) {
3214 } else if( *pos == '\'' )
3220 PRVM_G_INT( OFS_RETURN ) = PRVM_SetEngineString( outstr );
3224 ========================
3227 string altstr_set(string altstr, float num, string set)
3228 ========================
3230 void VM_altstr_set( void )
3233 const char *altstr, *str;
3237 VM_SAFEPARMCOUNT( 3, VM_altstr_set );
3239 altstr = PRVM_G_STRING( OFS_PARM0 );
3240 //VM_CheckEmptyString( altstr );
3242 num = (int)PRVM_G_FLOAT( OFS_PARM1 );
3244 str = PRVM_G_STRING( OFS_PARM2 );
3245 //VM_CheckEmptyString( str );
3247 outstr = out = VM_GetTempString();
3248 for( num = num * 2 + 1, in = altstr; *in && num; *out++ = *in++ )
3253 } else if( *in == '\'' ) {
3258 PRVM_G_INT( OFS_RETURN ) = PRVM_SetEngineString( altstr );
3262 for( ; *str; *out++ = *str++ );
3263 // now jump over the old content
3265 if( *in == '\'' || (*in == '\\' && !*++in) )
3269 PRVM_G_INT( OFS_RETURN ) = PRVM_SetEngineString( NULL );
3274 PRVM_G_INT( OFS_RETURN ) = PRVM_SetEngineString( outstr );
3278 ========================
3281 string altstr_ins(string altstr, float num, string set)
3282 ========================
3284 void VM_altstr_ins(void)
3294 in = instr = PRVM_G_STRING( OFS_PARM0 );
3295 num = (int)PRVM_G_FLOAT( OFS_PARM1 );
3296 set = setstr = PRVM_G_STRING( OFS_PARM2 );
3298 out = outstr = VM_GetTempString();
3299 for( num = num * 2 + 2 ; *in && num > 0 ; *out++ = *in++ )
3304 } else if( *in == '\'' ) {
3309 for( ; *set ; *out++ = *set++ );
3313 PRVM_G_INT( OFS_RETURN ) = PRVM_SetEngineString( outstr );
3317 ////////////////////////////////////////
3318 // BufString functions
3319 ////////////////////////////////////////
3320 //[515]: string buffers support
3321 #define MAX_QCSTR_BUFFERS 128
3322 #define MAX_QCSTR_STRINGS 1024
3327 char *strings[MAX_QCSTR_STRINGS];
3330 static qcstrbuffer_t *qcstringbuffers[MAX_QCSTR_BUFFERS];
3331 static int num_qcstringbuffers;
3332 static int buf_sortpower;
3334 #define BUFSTR_BUFFER(a) (a>=MAX_QCSTR_BUFFERS) ? NULL : (qcstringbuffers[a])
3335 #define BUFSTR_ISFREE(a) (a<MAX_QCSTR_BUFFERS&&qcstringbuffers[a]&&qcstringbuffers[a]->num_strings<=0) ? 1 : 0
3337 static int BufStr_FindFreeBuffer (void)
3340 if(num_qcstringbuffers == MAX_QCSTR_BUFFERS)
3342 for(i=0;i<MAX_QCSTR_BUFFERS;i++)
3343 if(!qcstringbuffers[i])
3345 qcstringbuffers[i] = (qcstrbuffer_t *)Z_Malloc(sizeof(qcstrbuffer_t));
3346 memset(qcstringbuffers[i], 0, sizeof(qcstrbuffer_t));
3352 static void BufStr_ClearBuffer (int index)
3354 qcstrbuffer_t *b = qcstringbuffers[index];
3359 if(b->num_strings > 0)
3361 for(i=0;i<b->num_strings;i++)
3363 Z_Free(b->strings[i]);
3364 num_qcstringbuffers--;
3366 Z_Free(qcstringbuffers[index]);
3367 qcstringbuffers[index] = NULL;
3371 static int BufStr_FindFreeString (qcstrbuffer_t *b)
3374 for(i=0;i<b->num_strings;i++)
3375 if(!b->strings[i] || !b->strings[i][0])
3377 if(i == MAX_QCSTR_STRINGS) return -1;
3381 static int BufStr_SortStringsUP (const void *in1, const void *in2)
3384 a = *((const char **) in1);
3385 b = *((const char **) in2);
3387 if(!b[0]) return -1;
3388 return strncmp(a, b, buf_sortpower);
3391 static int BufStr_SortStringsDOWN (const void *in1, const void *in2)
3394 a = *((const char **) in1);
3395 b = *((const char **) in2);
3397 if(!b[0]) return -1;
3398 return strncmp(b, a, buf_sortpower);
3402 static void VM_BufStr_Init (void)
3404 memset(qcstringbuffers, 0, sizeof(qcstringbuffers));
3405 num_qcstringbuffers = 0;
3408 static void VM_BufStr_ShutDown (void)
3411 for(i=0;i<MAX_QCSTR_BUFFERS && num_qcstringbuffers;i++)
3412 BufStr_ClearBuffer(i);
3417 ========================
3419 creates new buffer, and returns it's index, returns -1 if failed
3420 float buf_create(void) = #460;
3421 ========================
3423 void VM_buf_create (void)
3426 VM_SAFEPARMCOUNT(0, VM_buf_create);
3427 i = BufStr_FindFreeBuffer();
3429 num_qcstringbuffers++;
3431 //Con_Printf("VM_buf_create: buffers overflow in %s\n", PRVM_NAME);
3432 PRVM_G_FLOAT(OFS_RETURN) = i;
3436 ========================
3438 deletes buffer and all strings in it
3439 void buf_del(float bufhandle) = #461;
3440 ========================
3442 void VM_buf_del (void)
3444 VM_SAFEPARMCOUNT(1, VM_buf_del);
3445 if(BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0)))
3446 BufStr_ClearBuffer((int)PRVM_G_FLOAT(OFS_PARM0));
3449 Con_Printf("VM_buf_del: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3455 ========================
3457 how many strings are stored in buffer
3458 float buf_getsize(float bufhandle) = #462;
3459 ========================
3461 void VM_buf_getsize (void)
3464 VM_SAFEPARMCOUNT(1, VM_buf_getsize);
3466 b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3469 PRVM_G_FLOAT(OFS_RETURN) = -1;
3470 Con_Printf("VM_buf_getsize: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3474 PRVM_G_FLOAT(OFS_RETURN) = b->num_strings;
3478 ========================
3480 copy all content from one buffer to another, make sure it exists
3481 void buf_copy(float bufhandle_from, float bufhandle_to) = #463;
3482 ========================
3484 void VM_buf_copy (void)
3486 qcstrbuffer_t *b1, *b2;
3488 VM_SAFEPARMCOUNT(2, VM_buf_copy);
3490 b1 = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3493 Con_Printf("VM_buf_copy: invalid source buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3496 i = (int)PRVM_G_FLOAT(OFS_PARM1);
3497 if(i == (int)PRVM_G_FLOAT(OFS_PARM0))
3499 Con_Printf("VM_buf_copy: source == destination (%i) in %s\n", i, PRVM_NAME);
3502 b2 = BUFSTR_BUFFER(i);
3505 Con_Printf("VM_buf_copy: invalid destination buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM1), PRVM_NAME);
3509 BufStr_ClearBuffer(i);
3510 qcstringbuffers[i] = (qcstrbuffer_t *)Z_Malloc(sizeof(qcstrbuffer_t));
3511 memset(qcstringbuffers[i], 0, sizeof(qcstrbuffer_t));
3512 b2->num_strings = b1->num_strings;
3514 for(i=0;i<b1->num_strings;i++)
3515 if(b1->strings[i] && b1->strings[i][0])
3517 b2->strings[i] = (char *)Z_Malloc(strlen(b1->strings[i])+1);
3520 Con_Printf("VM_buf_copy: not enough memory for buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM1), PRVM_NAME);
3523 strcpy(b2->strings[i], b1->strings[i]);
3528 ========================
3530 sort buffer by beginnings of strings (sortpower defaults it's lenght)
3531 "backward == TRUE" means that sorting goes upside-down
3532 void buf_sort(float bufhandle, float sortpower, float backward) = #464;
3533 ========================
3535 void VM_buf_sort (void)
3539 VM_SAFEPARMCOUNT(3, VM_buf_sort);
3541 b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3544 Con_Printf("VM_buf_sort: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3547 if(b->num_strings <= 0)
3549 Con_Printf("VM_buf_sort: tried to sort empty buffer %i in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3552 buf_sortpower = (int)PRVM_G_FLOAT(OFS_PARM1);
3553 if(buf_sortpower <= 0)
3554 buf_sortpower = 99999999;
3556 if(!PRVM_G_FLOAT(OFS_PARM2))
3557 qsort(b->strings, b->num_strings, sizeof(char*), BufStr_SortStringsUP);
3559 qsort(b->strings, b->num_strings, sizeof(char*), BufStr_SortStringsDOWN);
3561 for(i=b->num_strings-1;i>=0;i--) //[515]: delete empty lines
3564 if(b->strings[i][0])
3568 Z_Free(b->strings[i]);
3570 b->strings[i] = NULL;
3578 ========================
3580 concantenates all buffer string into one with "glue" separator and returns it as tempstring
3581 string buf_implode(float bufhandle, string glue) = #465;
3582 ========================
3584 void VM_buf_implode (void)
3591 VM_SAFEPARMCOUNT(2, VM_buf_implode);
3593 b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3594 PRVM_G_INT(OFS_RETURN) = 0;
3597 Con_Printf("VM_buf_implode: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3602 sep = PRVM_G_STRING(OFS_PARM1);
3603 k = VM_GetTempString();
3605 for(l=i=0;i<b->num_strings;i++)
3608 l += strlen(b->strings[i]);
3611 k = strcat(k, b->strings[i]);
3614 if(sep && (i != b->num_strings-1))
3624 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(k);
3628 ========================
3630 get a string from buffer, returns direct pointer, dont str_unzone it!
3631 string bufstr_get(float bufhandle, float string_index) = #465;
3632 ========================
3634 void VM_bufstr_get (void)
3638 VM_SAFEPARMCOUNT(2, VM_bufstr_get);
3640 b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3643 Con_Printf("VM_bufstr_get: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3646 strindex = (int)PRVM_G_FLOAT(OFS_PARM1);
3647 if(strindex < 0 || strindex > MAX_QCSTR_STRINGS)
3649 Con_Printf("VM_bufstr_get: invalid string index %i used in %s\n", strindex, PRVM_NAME);
3652 PRVM_G_INT(OFS_RETURN) = 0;
3653 if(b->num_strings <= strindex)
3655 if(b->strings[strindex])
3656 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(b->strings[strindex]);
3660 ========================
3662 copies a string into selected slot of buffer
3663 void bufstr_set(float bufhandle, float string_index, string str) = #466;
3664 ========================
3666 void VM_bufstr_set (void)
3668 int bufindex, strindex;
3672 VM_SAFEPARMCOUNT(3, VM_bufstr_set);
3674 bufindex = (int)PRVM_G_FLOAT(OFS_PARM0);
3675 b = BUFSTR_BUFFER(bufindex);
3678 Con_Printf("VM_bufstr_set: invalid buffer %i used in %s\n", bufindex, PRVM_NAME);
3681 strindex = (int)PRVM_G_FLOAT(OFS_PARM1);
3682 if(strindex < 0 || strindex > MAX_QCSTR_STRINGS)
3684 Con_Printf("VM_bufstr_set: invalid string index %i used in %s\n", strindex, PRVM_NAME);
3687 news = PRVM_G_STRING(OFS_PARM2);
3690 Con_Printf("VM_bufstr_set: null string used in %s\n", PRVM_NAME);
3693 if(b->strings[strindex])
3694 Z_Free(b->strings[strindex]);
3695 b->strings[strindex] = (char *)Z_Malloc(strlen(news)+1);
3696 strcpy(b->strings[strindex], news);
3700 ========================
3702 adds string to buffer in nearest free slot and returns it
3703 "order == TRUE" means that string will be added after last "full" slot
3704 float bufstr_add(float bufhandle, string str, float order) = #467;
3705 ========================
3707 void VM_bufstr_add (void)
3709 int bufindex, order, strindex;
3713 VM_SAFEPARMCOUNT(3, VM_bufstr_add);
3715 bufindex = (int)PRVM_G_FLOAT(OFS_PARM0);
3716 b = BUFSTR_BUFFER(bufindex);
3717 PRVM_G_FLOAT(OFS_RETURN) = -1;
3720 Con_Printf("VM_bufstr_add: invalid buffer %i used in %s\n", bufindex, PRVM_NAME);
3723 string = PRVM_G_STRING(OFS_PARM1);
3726 Con_Printf("VM_bufstr_add: null string used in %s\n", PRVM_NAME);
3730 order = (int)PRVM_G_FLOAT(OFS_PARM2);
3732 strindex = b->num_strings;
3735 strindex = BufStr_FindFreeString(b);
3738 Con_Printf("VM_bufstr_add: buffer %i has no free string slots in %s\n", bufindex, PRVM_NAME);
3743 while(b->num_strings <= strindex)
3745 if(b->num_strings == MAX_QCSTR_STRINGS)
3747 Con_Printf("VM_bufstr_add: buffer %i has no free string slots in %s\n", bufindex, PRVM_NAME);
3750 b->strings[b->num_strings] = NULL;
3753 if(b->strings[strindex])
3754 Z_Free(b->strings[strindex]);
3755 b->strings[strindex] = (char *)Z_Malloc(strlen(string)+1);
3756 strcpy(b->strings[strindex], string);
3757 PRVM_G_FLOAT(OFS_RETURN) = strindex;
3761 ========================
3763 delete string from buffer
3764 void bufstr_free(float bufhandle, float string_index) = #468;
3765 ========================
3767 void VM_bufstr_free (void)
3771 VM_SAFEPARMCOUNT(2, VM_bufstr_free);
3773 b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3776 Con_Printf("VM_bufstr_free: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3779 i = (int)PRVM_G_FLOAT(OFS_PARM1);
3780 if(i < 0 || i > MAX_QCSTR_STRINGS)
3782 Con_Printf("VM_bufstr_free: invalid string index %i used in %s\n", i, PRVM_NAME);
3786 Z_Free(b->strings[i]);
3787 b->strings[i] = NULL;
3788 if(i+1 == b->num_strings)
3794 void VM_Cmd_Init(void)
3796 // only init the stuff for the current prog
3799 // VM_BufStr_Init();
3800 if(vm_polygons_initialized)
3802 Mem_FreePool(&vm_polygons_pool);
3803 vm_polygons_initialized = false;
3807 void VM_Cmd_Reset(void)
3809 CL_PurgeOwner( MENUOWNER );
3811 VM_Files_CloseAll();
3812 // VM_BufStr_ShutDown();
3813 if(vm_polygons_initialized)
3815 Mem_FreePool(&vm_polygons_pool);
3816 vm_polygons_initialized = false;