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", PRVM_NAME);
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("======%s OBJECT ERROR in %s:\n%s\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 !");
160 VM_print (actually used only by client and menu)
169 char string[VM_STRINGTEMP_LENGTH];
171 VM_VarString(0, string, sizeof(string));
179 broadcast print to everyone on server
184 void VM_bprint (void)
186 char string[VM_STRINGTEMP_LENGTH];
190 Con_Printf("VM_bprint: game is not server(%s) !\n", PRVM_NAME);
194 VM_VarString(0, string, sizeof(string));
195 SV_BroadcastPrint(string);
200 VM_sprint (menu & client but only if server.active == true)
202 single print to a specific client
204 sprint(float clientnum,...[string])
207 void VM_sprint (void)
211 char string[VM_STRINGTEMP_LENGTH];
213 //find client for this entity
214 clientnum = PRVM_G_FLOAT(OFS_PARM0);
215 if (!sv.active || clientnum < 0 || clientnum >= svs.maxclients || !svs.clients[clientnum].active)
217 Con_Printf("VM_sprint: %s: invalid client or server is not active !\n", PRVM_NAME);
221 client = svs.clients + clientnum;
222 if (!client->netconnection)
225 VM_VarString(1, string, sizeof(string));
226 MSG_WriteChar(&client->netconnection->message,svc_print);
227 MSG_WriteString(&client->netconnection->message, string);
234 single print to the screen
236 centerprint(clientent, value)
239 void VM_centerprint (void)
241 char string[VM_STRINGTEMP_LENGTH];
243 VM_VarString(0, string, sizeof(string));
244 SCR_CenterPrint(string);
251 vector normalize(vector)
254 void VM_normalize (void)
260 VM_SAFEPARMCOUNT(1,VM_normalize);
262 value1 = PRVM_G_VECTOR(OFS_PARM0);
264 f = VectorLength2(value1);
268 VectorScale(value1, f, newvalue);
271 VectorClear(newvalue);
273 VectorCopy (newvalue, PRVM_G_VECTOR(OFS_RETURN));
285 VM_SAFEPARMCOUNT(1,VM_vlen);
286 PRVM_G_FLOAT(OFS_RETURN) = VectorLength(PRVM_G_VECTOR(OFS_PARM0));
293 float vectoyaw(vector)
296 void VM_vectoyaw (void)
301 VM_SAFEPARMCOUNT(1,VM_vectoyaw);
303 value1 = PRVM_G_VECTOR(OFS_PARM0);
305 if (value1[1] == 0 && value1[0] == 0)
309 yaw = (int) (atan2(value1[1], value1[0]) * 180 / M_PI);
314 PRVM_G_FLOAT(OFS_RETURN) = yaw;
322 vector vectoangles(vector)
325 void VM_vectoangles (void)
331 VM_SAFEPARMCOUNT(1,VM_vectoangles);
333 value1 = PRVM_G_VECTOR(OFS_PARM0);
335 if (value1[1] == 0 && value1[0] == 0)
345 // LordHavoc: optimized a bit
348 yaw = (atan2(value1[1], value1[0]) * 180 / M_PI);
352 else if (value1[1] > 0)
357 forward = sqrt(value1[0]*value1[0] + value1[1]*value1[1]);
358 pitch = (atan2(value1[2], forward) * 180 / M_PI);
363 PRVM_G_FLOAT(OFS_RETURN+0) = pitch;
364 PRVM_G_FLOAT(OFS_RETURN+1) = yaw;
365 PRVM_G_FLOAT(OFS_RETURN+2) = 0;
372 Returns a number from 0<= num < 1
377 void VM_random (void)
379 VM_SAFEPARMCOUNT(0,VM_random);
381 PRVM_G_FLOAT(OFS_RETURN) = lhrandom(0, 1);
388 Each entity can have eight independant sound sources, like voice,
391 Channel 0 is an auto-allocate channel, the others override anything
392 already running on that entity/channel pair.
394 An attenuation of 0 will play full volume everywhere in the level.
395 Larger attenuations will drop off.
404 prvm_edict_t *entity;
408 entity = PRVM_G_EDICT(OFS_PARM0);
409 channel = PRVM_G_FLOAT(OFS_PARM1);
410 sample = PRVM_G_STRING(OFS_PARM2);
411 volume = PRVM_G_FLOAT(OFS_PARM3) * 255;
412 attenuation = PRVM_G_FLOAT(OFS_PARM4);
414 if (volume < 0 || volume > 255)
415 Host_Error ("SV_StartSound: volume = %i", volume);
417 if (attenuation < 0 || attenuation > 4)
418 Host_Error ("SV_StartSound: attenuation = %f", attenuation);
420 if (channel < 0 || channel > 7)
421 Host_Error ("SV_StartSound: channel = %i", channel);
423 SV_StartSound (entity, channel, sample, volume, attenuation);
431 localsound(string sample)
434 void VM_localsound(void)
438 VM_SAFEPARMCOUNT(1,VM_localsound);
440 s = PRVM_G_STRING(OFS_PARM0);
442 if(!S_LocalSound (s))
444 Con_Printf("VM_localsound: Failed to play %s for %s !\n", s, PRVM_NAME);
445 PRVM_G_FLOAT(OFS_RETURN) = -4;
449 PRVM_G_FLOAT(OFS_RETURN) = 1;
461 PRVM_ERROR ("%s: break statement", PRVM_NAME);
464 //============================================================================
470 Sends text over to the client's execution buffer
472 [localcmd (string, ...) or]
476 void VM_localcmd (void)
478 char string[VM_STRINGTEMP_LENGTH];
479 VM_VarString(0, string, sizeof(string));
480 Cbuf_AddText(string);
492 VM_SAFEPARMCOUNT(1,VM_cvar);
494 PRVM_G_FLOAT(OFS_RETURN) = Cvar_VariableValue(PRVM_G_STRING(OFS_PARM0));
501 const string VM_cvar_string (string)
504 void VM_cvar_string(void)
508 const char *cvar_string;
509 VM_SAFEPARMCOUNT(1,VM_cvar_string);
511 name = PRVM_G_STRING(OFS_PARM0);
514 PRVM_ERROR("VM_cvar_string: %s: null string", PRVM_NAME);
516 VM_CheckEmptyString(name);
518 out = VM_GetTempString();
520 cvar_string = Cvar_VariableString(name);
522 strcpy(out, cvar_string);
524 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(out);
529 ========================
532 const string VM_cvar_defstring (string)
533 ========================
535 void VM_cvar_defstring (void)
539 const char *cvar_string;
540 VM_SAFEPARMCOUNT(1,VM_cvar_string);
542 name = PRVM_G_STRING(OFS_PARM0);
545 PRVM_ERROR("VM_cvar_defstring: %s: null string", PRVM_NAME);
547 VM_CheckEmptyString(name);
549 out = VM_GetTempString();
551 cvar_string = Cvar_VariableDefString(name);
553 strcpy(out, cvar_string);
555 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(out);
561 void cvar_set (string,string)
564 void VM_cvar_set (void)
566 VM_SAFEPARMCOUNT(2,VM_cvar_set);
568 Cvar_Set(PRVM_G_STRING(OFS_PARM0), PRVM_G_STRING(OFS_PARM1));
578 void VM_dprint (void)
580 char string[VM_STRINGTEMP_LENGTH];
581 if (developer.integer)
583 VM_VarString(0, string, sizeof(string));
585 Con_Printf("%s", string);
587 Con_Printf("%s: %s", PRVM_NAME, string);
605 VM_SAFEPARMCOUNT(1, VM_ftos);
607 v = PRVM_G_FLOAT(OFS_PARM0);
609 s = VM_GetTempString();
610 if ((float)((int)v) == v)
611 sprintf(s, "%i", (int)v);
614 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(s);
629 VM_SAFEPARMCOUNT(1,VM_fabs);
631 v = PRVM_G_FLOAT(OFS_PARM0);
632 PRVM_G_FLOAT(OFS_RETURN) = fabs(v);
647 VM_SAFEPARMCOUNT(1,VM_vtos);
649 s = VM_GetTempString();
650 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]);
651 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(s);
666 VM_SAFEPARMCOUNT(1, VM_etos);
668 s = VM_GetTempString();
669 sprintf (s, "entity %i", PRVM_G_EDICTNUM(OFS_PARM0));
670 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(s);
677 float stof(...[string])
682 char string[VM_STRINGTEMP_LENGTH];
683 VM_VarString(0, string, sizeof(string));
684 PRVM_G_FLOAT(OFS_RETURN) = atof(string);
688 ========================
692 ========================
696 VM_SAFEPARMCOUNT(1, VM_itof);
697 PRVM_G_FLOAT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
701 ========================
705 ========================
710 VM_SAFEPARMCOUNT(1, VM_ftoi);
712 ent = PRVM_G_FLOAT(OFS_PARM0);
713 if(PRVM_PROG_TO_EDICT(ent)->priv.required->free)
714 PRVM_ERROR ("VM_ftoe: %s tried to access a freed entity (entity %i)!", PRVM_NAME, ent);
716 PRVM_G_INT(OFS_RETURN) = ent;
730 prog->xfunction->builtinsprofile += 20;
731 ed = PRVM_ED_Alloc();
743 void VM_remove (void)
746 prog->xfunction->builtinsprofile += 20;
748 VM_SAFEPARMCOUNT(1, VM_remove);
750 ed = PRVM_G_EDICT(OFS_PARM0);
751 if( PRVM_NUM_FOR_EDICT(ed) <= prog->reserved_edicts ) {
752 Con_DPrint( "VM_remove: tried to remove the null entity or a reserved entity!\n" );
753 } else if( ed->priv.required->free ) {
754 Con_DPrint( "VM_remove: tried to remove an already freed entity!\n" );
758 // if (ed == prog->edicts)
759 // PRVM_ERROR ("remove: tried to remove world");
760 // if (PRVM_NUM_FOR_EDICT(ed) <= sv.maxclients)
761 // Host_Error("remove: tried to remove a client");
768 entity find(entity start, .string field, string match)
779 VM_SAFEPARMCOUNT(3,VM_find);
781 e = PRVM_G_EDICTNUM(OFS_PARM0);
782 f = PRVM_G_INT(OFS_PARM1);
783 s = PRVM_G_STRING(OFS_PARM2);
787 // return reserved edict 0 (could be used for whatever the prog wants)
788 VM_RETURN_EDICT(prog->edicts);
792 for (e++ ; e < prog->num_edicts ; e++)
794 prog->xfunction->builtinsprofile++;
795 ed = PRVM_EDICT_NUM(e);
796 if (ed->priv.required->free)
798 t = PRVM_E_STRING(ed,f);
808 VM_RETURN_EDICT(prog->edicts);
815 entity findfloat(entity start, .float field, float match)
816 entity findentity(entity start, .entity field, entity match)
819 // LordHavoc: added this for searching float, int, and entity reference fields
820 void VM_findfloat (void)
827 VM_SAFEPARMCOUNT(3,VM_findfloat);
829 e = PRVM_G_EDICTNUM(OFS_PARM0);
830 f = PRVM_G_INT(OFS_PARM1);
831 s = PRVM_G_FLOAT(OFS_PARM2);
833 for (e++ ; e < prog->num_edicts ; e++)
835 prog->xfunction->builtinsprofile++;
836 ed = PRVM_EDICT_NUM(e);
837 if (ed->priv.required->free)
839 if (PRVM_E_FLOAT(ed,f) == s)
846 VM_RETURN_EDICT(prog->edicts);
853 entity findchain(.string field, string match)
856 // chained search for strings in entity fields
857 // entity(.string field, string match) findchain = #402;
858 void VM_findchain (void)
864 prvm_edict_t *ent, *chain;
866 VM_SAFEPARMCOUNT(2,VM_findchain);
868 // is the same like !(prog->flag & PRVM_FE_CHAIN) - even if the operator precedence is another
869 if(!prog->flag & PRVM_FE_CHAIN)
870 PRVM_ERROR("VM_findchain: %s doesnt have a chain field !", PRVM_NAME);
872 chain_of = PRVM_ED_FindField("chain")->ofs;
874 chain = prog->edicts;
876 f = PRVM_G_INT(OFS_PARM0);
877 s = PRVM_G_STRING(OFS_PARM1);
880 VM_RETURN_EDICT(prog->edicts);
884 ent = PRVM_NEXT_EDICT(prog->edicts);
885 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
887 prog->xfunction->builtinsprofile++;
888 if (ent->priv.required->free)
890 t = PRVM_E_STRING(ent,f);
896 PRVM_E_INT(ent,chain_of) = PRVM_NUM_FOR_EDICT(chain);
900 VM_RETURN_EDICT(chain);
907 entity findchainfloat(.string field, float match)
908 entity findchainentity(.string field, entity match)
911 // LordHavoc: chained search for float, int, and entity reference fields
912 // entity(.string field, float match) findchainfloat = #403;
913 void VM_findchainfloat (void)
919 prvm_edict_t *ent, *chain;
921 VM_SAFEPARMCOUNT(2, VM_findchainfloat);
923 if(!prog->flag & PRVM_FE_CHAIN)
924 PRVM_ERROR("VM_findchainfloat: %s doesnt have a chain field !", PRVM_NAME);
926 chain_of = PRVM_ED_FindField("chain")->ofs;
928 chain = (prvm_edict_t *)prog->edicts;
930 f = PRVM_G_INT(OFS_PARM0);
931 s = PRVM_G_FLOAT(OFS_PARM1);
933 ent = PRVM_NEXT_EDICT(prog->edicts);
934 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
936 prog->xfunction->builtinsprofile++;
937 if (ent->priv.required->free)
939 if (PRVM_E_FLOAT(ent,f) != s)
942 PRVM_E_INT(ent,chain_of) = PRVM_EDICT_TO_PROG(chain);
946 VM_RETURN_EDICT(chain);
950 ========================
953 entity findflags(entity start, .float field, float match)
954 ========================
956 // LordHavoc: search for flags in float fields
957 void VM_findflags (void)
964 VM_SAFEPARMCOUNT(3, VM_findflags);
967 e = PRVM_G_EDICTNUM(OFS_PARM0);
968 f = PRVM_G_INT(OFS_PARM1);
969 s = (int)PRVM_G_FLOAT(OFS_PARM2);
971 for (e++ ; e < prog->num_edicts ; e++)
973 prog->xfunction->builtinsprofile++;
974 ed = PRVM_EDICT_NUM(e);
975 if (ed->priv.required->free)
977 if ((int)PRVM_E_FLOAT(ed,f) & s)
984 VM_RETURN_EDICT(prog->edicts);
988 ========================
991 entity findchainflags(.float field, float match)
992 ========================
994 // LordHavoc: chained search for flags in float fields
995 void VM_findchainflags (void)
1001 prvm_edict_t *ent, *chain;
1003 VM_SAFEPARMCOUNT(2, VM_findchainflags);
1005 if(!prog->flag & PRVM_FE_CHAIN)
1006 PRVM_ERROR("VM_findchainflags: %s doesnt have a chain field !", PRVM_NAME);
1008 chain_of = PRVM_ED_FindField("chain")->ofs;
1010 chain = (prvm_edict_t *)prog->edicts;
1012 f = PRVM_G_INT(OFS_PARM0);
1013 s = (int)PRVM_G_FLOAT(OFS_PARM1);
1015 ent = PRVM_NEXT_EDICT(prog->edicts);
1016 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
1018 prog->xfunction->builtinsprofile++;
1019 if (ent->priv.required->free)
1021 if (!((int)PRVM_E_FLOAT(ent,f) & s))
1024 PRVM_E_INT(ent,chain_of) = PRVM_EDICT_TO_PROG(chain);
1028 VM_RETURN_EDICT(chain);
1038 void VM_coredump (void)
1040 VM_SAFEPARMCOUNT(0,VM_coredump);
1042 Cbuf_AddText("prvm_edicts ");
1043 Cbuf_AddText(PRVM_NAME);
1054 void PRVM_StackTrace(void);
1055 void VM_stackdump (void)
1057 VM_SAFEPARMCOUNT(0, VM_stackdump);
1072 VM_SAFEPARMCOUNT(0, VM_crash);
1074 PRVM_ERROR("Crash called by %s",PRVM_NAME);
1084 void VM_traceon (void)
1086 VM_SAFEPARMCOUNT(0,VM_traceon);
1098 void VM_traceoff (void)
1100 VM_SAFEPARMCOUNT(0,VM_traceoff);
1102 prog->trace = false;
1112 void VM_eprint (void)
1114 VM_SAFEPARMCOUNT(1,VM_eprint);
1116 PRVM_ED_PrintNum (PRVM_G_EDICTNUM(OFS_PARM0));
1130 VM_SAFEPARMCOUNT(1,VM_rint);
1132 f = PRVM_G_FLOAT(OFS_PARM0);
1134 PRVM_G_FLOAT(OFS_RETURN) = (int)(f + 0.5);
1136 PRVM_G_FLOAT(OFS_RETURN) = (int)(f - 0.5);
1146 void VM_floor (void)
1148 VM_SAFEPARMCOUNT(1,VM_floor);
1150 PRVM_G_FLOAT(OFS_RETURN) = floor(PRVM_G_FLOAT(OFS_PARM0));
1162 VM_SAFEPARMCOUNT(1,VM_ceil);
1164 PRVM_G_FLOAT(OFS_RETURN) = ceil(PRVM_G_FLOAT(OFS_PARM0));
1172 entity nextent(entity)
1175 void VM_nextent (void)
1180 i = PRVM_G_EDICTNUM(OFS_PARM0);
1183 prog->xfunction->builtinsprofile++;
1185 if (i == prog->num_edicts)
1187 VM_RETURN_EDICT(prog->edicts);
1190 ent = PRVM_EDICT_NUM(i);
1191 if (!ent->priv.required->free)
1193 VM_RETURN_EDICT(ent);
1199 //=============================================================================
1206 changelevel(string map)
1209 void VM_changelevel (void)
1213 VM_SAFEPARMCOUNT(1, VM_changelevel);
1217 Con_Printf("VM_changelevel: game is not server (%s)\n", PRVM_NAME);
1221 // make sure we don't issue two changelevels
1222 if (svs.changelevel_issued)
1224 svs.changelevel_issued = true;
1226 s = PRVM_G_STRING(OFS_PARM0);
1227 Cbuf_AddText (va("changelevel %s\n",s));
1239 VM_SAFEPARMCOUNT(1,VM_sin);
1240 PRVM_G_FLOAT(OFS_RETURN) = sin(PRVM_G_FLOAT(OFS_PARM0));
1251 VM_SAFEPARMCOUNT(1,VM_cos);
1252 PRVM_G_FLOAT(OFS_RETURN) = cos(PRVM_G_FLOAT(OFS_PARM0));
1264 VM_SAFEPARMCOUNT(1,VM_sqrt);
1265 PRVM_G_FLOAT(OFS_RETURN) = sqrt(PRVM_G_FLOAT(OFS_PARM0));
1272 Returns a vector of length < 1 and > 0
1277 void VM_randomvec (void)
1282 VM_SAFEPARMCOUNT(0, VM_randomvec);
1287 temp[0] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1288 temp[1] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1289 temp[2] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1291 while (DotProduct(temp, temp) >= 1);
1292 VectorCopy (temp, PRVM_G_VECTOR(OFS_RETURN));
1295 temp[0] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1296 temp[1] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1297 temp[2] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1298 // length returned always > 0
1299 length = (rand()&32766 + 1) * (1.0 / 32767.0) / VectorLength(temp);
1300 VectorScale(temp,length, temp);*/
1301 //VectorCopy(temp, PRVM_G_VECTOR(OFS_RETURN));
1304 //=============================================================================
1310 float registercvar (string name, string value, float flags)
1313 void VM_registercvar (void)
1315 const char *name, *value;
1318 VM_SAFEPARMCOUNT(3,VM_registercvar);
1320 name = PRVM_G_STRING(OFS_PARM0);
1321 value = PRVM_G_STRING(OFS_PARM1);
1322 flags = PRVM_G_FLOAT(OFS_PARM2);
1323 PRVM_G_FLOAT(OFS_RETURN) = 0;
1325 if(flags > CVAR_MAXFLAGSVAL)
1328 // first check to see if it has already been defined
1329 if (Cvar_FindVar (name))
1332 // check for overlap with a command
1333 if (Cmd_Exists (name))
1335 Con_Printf("VM_registercvar: %s is a command\n", name);
1339 Cvar_Get(name, value, flags);
1341 PRVM_G_FLOAT(OFS_RETURN) = 1; // success
1348 returns the minimum of two supplied floats
1350 float min(float a, float b, ...[float])
1355 // LordHavoc: 3+ argument enhancement suggested by FrikaC
1356 if (prog->argc == 2)
1357 PRVM_G_FLOAT(OFS_RETURN) = min(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1358 else if (prog->argc >= 3)
1361 float f = PRVM_G_FLOAT(OFS_PARM0);
1362 for (i = 1;i < prog->argc;i++)
1363 if (PRVM_G_FLOAT((OFS_PARM0+i*3)) < f)
1364 f = PRVM_G_FLOAT((OFS_PARM0+i*3));
1365 PRVM_G_FLOAT(OFS_RETURN) = f;
1368 PRVM_ERROR("VM_min: %s must supply at least 2 floats", PRVM_NAME);
1375 returns the maximum of two supplied floats
1377 float max(float a, float b, ...[float])
1382 // LordHavoc: 3+ argument enhancement suggested by FrikaC
1383 if (prog->argc == 2)
1384 PRVM_G_FLOAT(OFS_RETURN) = max(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1385 else if (prog->argc >= 3)
1388 float f = PRVM_G_FLOAT(OFS_PARM0);
1389 for (i = 1;i < prog->argc;i++)
1390 if (PRVM_G_FLOAT((OFS_PARM0+i*3)) > f)
1391 f = PRVM_G_FLOAT((OFS_PARM0+i*3));
1392 PRVM_G_FLOAT(OFS_RETURN) = f;
1395 PRVM_ERROR("VM_max: %s must supply at least 2 floats", PRVM_NAME);
1402 returns number bounded by supplied range
1404 float bound(float min, float value, float max)
1407 void VM_bound (void)
1409 VM_SAFEPARMCOUNT(3,VM_bound);
1410 PRVM_G_FLOAT(OFS_RETURN) = bound(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1), PRVM_G_FLOAT(OFS_PARM2));
1417 returns a raised to power b
1419 float pow(float a, float b)
1424 VM_SAFEPARMCOUNT(2,VM_pow);
1425 PRVM_G_FLOAT(OFS_RETURN) = pow(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1432 copies data from one entity to another
1434 copyentity(entity src, entity dst)
1437 void VM_copyentity (void)
1439 prvm_edict_t *in, *out;
1440 VM_SAFEPARMCOUNT(2,VM_copyentity);
1441 in = PRVM_G_EDICT(OFS_PARM0);
1442 out = PRVM_G_EDICT(OFS_PARM1);
1443 memcpy(out->fields.vp, in->fields.vp, prog->progs->entityfields * 4);
1450 sets the color of a client and broadcasts the update to all connected clients
1452 setcolor(clientent, value)
1455 /*void PF_setcolor (void)
1461 entnum = PRVM_G_EDICTNUM(OFS_PARM0);
1462 i = PRVM_G_FLOAT(OFS_PARM1);
1464 if (entnum < 1 || entnum > svs.maxclients || !svs.clients[entnum-1].active)
1466 Con_Print("tried to setcolor a non-client\n");
1470 client = svs.clients + entnum-1;
1471 if ((val = PRVM_GETEDICTFIELDVALUE(client->edict, eval_clientcolors)))
1474 client->old_colors = i;
1475 client->edict->fields.server->team = (i & 15) + 1;
1477 MSG_WriteByte (&sv.reliable_datagram, svc_updatecolors);
1478 MSG_WriteByte (&sv.reliable_datagram, entnum - 1);
1479 MSG_WriteByte (&sv.reliable_datagram, i);
1482 void VM_Files_Init(void)
1484 memset(VM_FILES, 0, sizeof(qfile_t*[MAX_VMFILES]));
1487 void VM_Files_CloseAll(void)
1490 for (i = 0;i < MAX_VMFILES;i++)
1493 FS_Close(VM_FILES[i]);
1494 //VM_FILES[i] = NULL;
1496 memset(VM_FILES,0,sizeof(qfile_t*[MAX_VMFILES])); // this should be faster (is it ?)
1499 qfile_t *VM_GetFileHandle( int index )
1501 if (index < 0 || index >= MAX_VMFILES)
1503 Con_Printf("VM_GetFileHandle: invalid file handle %i used in %s\n", index, PRVM_NAME);
1506 if (VM_FILES[index] == NULL)
1508 Con_Printf("VM_GetFileHandle: no such file handle %i (or file has been closed) in %s\n", index, PRVM_NAME);
1511 return VM_FILES[index];
1518 float fopen(string filename, float mode)
1521 // float(string filename, float mode) fopen = #110;
1522 // opens a file inside quake/gamedir/data/ (mode is FILE_READ, FILE_APPEND, or FILE_WRITE),
1523 // returns fhandle >= 0 if successful, or fhandle < 0 if unable to open file for any reason
1527 const char *modestring, *filename;
1529 VM_SAFEPARMCOUNT(2,VM_fopen);
1531 for (filenum = 0;filenum < MAX_VMFILES;filenum++)
1532 if (VM_FILES[filenum] == NULL)
1534 if (filenum >= MAX_VMFILES)
1536 Con_Printf("VM_fopen: %s ran out of file handles (%i)\n", PRVM_NAME, MAX_VMFILES);
1537 PRVM_G_FLOAT(OFS_RETURN) = -2;
1540 mode = PRVM_G_FLOAT(OFS_PARM1);
1543 case 0: // FILE_READ
1546 case 1: // FILE_APPEND
1549 case 2: // FILE_WRITE
1553 Con_Printf("VM_fopen: %s: no such mode %i (valid: 0 = read, 1 = append, 2 = write)\n", PRVM_NAME, mode);
1554 PRVM_G_FLOAT(OFS_RETURN) = -3;
1557 filename = PRVM_G_STRING(OFS_PARM0);
1559 VM_FILES[filenum] = FS_Open(va("data/%s", filename), modestring, false, false);
1560 if (VM_FILES[filenum] == NULL && mode == 0)
1561 VM_FILES[filenum] = FS_Open(va("%s", filename), modestring, false, false);
1563 if (VM_FILES[filenum] == NULL)
1565 if (developer.integer)
1566 Con_Printf("VM_fopen: %s: %s mode %s failed\n", PRVM_NAME, filename, modestring);
1567 PRVM_G_FLOAT(OFS_RETURN) = -1;
1571 if (developer.integer)
1572 Con_Printf("VM_fopen: %s: %s mode %s opened as #%i\n", PRVM_NAME, filename, modestring, filenum);
1573 PRVM_G_FLOAT(OFS_RETURN) = filenum;
1581 fclose(float fhandle)
1584 //void(float fhandle) fclose = #111; // closes a file
1585 void VM_fclose(void)
1589 VM_SAFEPARMCOUNT(1,VM_fclose);
1591 filenum = PRVM_G_FLOAT(OFS_PARM0);
1592 if (filenum < 0 || filenum >= MAX_VMFILES)
1594 Con_Printf("VM_fclose: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1597 if (VM_FILES[filenum] == NULL)
1599 Con_Printf("VM_fclose: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1602 if (developer.integer)
1603 Con_Printf("VM_fclose: %s: #%i closed\n", PRVM_NAME, filenum);
1604 FS_Close(VM_FILES[filenum]);
1605 VM_FILES[filenum] = NULL;
1612 string fgets(float fhandle)
1615 //string(float fhandle) fgets = #112; // reads a line of text from the file and returns as a tempstring
1619 static char string[VM_STRINGTEMP_LENGTH];
1622 VM_SAFEPARMCOUNT(1,VM_fgets);
1624 filenum = PRVM_G_FLOAT(OFS_PARM0);
1625 if (filenum < 0 || filenum >= MAX_VMFILES)
1627 Con_Printf("VM_fgets: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1630 if (VM_FILES[filenum] == NULL)
1632 Con_Printf("VM_fgets: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1638 c = FS_Getc(VM_FILES[filenum]);
1639 if (c == '\r' || c == '\n' || c < 0)
1641 if (end < VM_STRINGTEMP_LENGTH - 1)
1645 // remove \n following \r
1648 c = FS_Getc(VM_FILES[filenum]);
1650 FS_UnGetc(VM_FILES[filenum], (unsigned char)c);
1652 if (developer.integer >= 3)
1653 Con_Printf("fgets: %s: %s\n", PRVM_NAME, string);
1655 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(string);
1657 PRVM_G_INT(OFS_RETURN) = 0;
1664 fputs(float fhandle, string s)
1667 //void(float fhandle, string s) fputs = #113; // writes a line of text to the end of the file
1671 char string[VM_STRINGTEMP_LENGTH];
1674 VM_SAFEPARMCOUNT(2,VM_fputs);
1676 filenum = PRVM_G_FLOAT(OFS_PARM0);
1677 if (filenum < 0 || filenum >= MAX_VMFILES)
1679 Con_Printf("VM_fputs: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1682 if (VM_FILES[filenum] == NULL)
1684 Con_Printf("VM_fputs: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1687 VM_VarString(1, string, sizeof(string));
1688 if ((stringlength = (int)strlen(string)))
1689 FS_Write(VM_FILES[filenum], string, stringlength);
1690 if (developer.integer)
1691 Con_Printf("fputs: %s: %s\n", PRVM_NAME, string);
1698 float strlen(string s)
1701 //float(string s) strlen = #114; // returns how many characters are in a string
1702 void VM_strlen(void)
1706 VM_SAFEPARMCOUNT(1,VM_strlen);
1708 s = PRVM_G_STRING(OFS_PARM0);
1710 PRVM_G_FLOAT(OFS_RETURN) = strlen(s);
1712 PRVM_G_FLOAT(OFS_RETURN) = 0;
1719 string strcat(string,string,...[string])
1722 //string(string s1, string s2) strcat = #115;
1723 // concatenates two strings (for example "abc", "def" would return "abcdef")
1724 // and returns as a tempstring
1725 void VM_strcat(void)
1730 PRVM_ERROR("VM_strcat wrong parameter count (min. 1 expected ) !");
1732 s = VM_GetTempString();
1733 VM_VarString(0, s, VM_STRINGTEMP_LENGTH);
1734 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(s);
1741 string substring(string s, float start, float length)
1744 // string(string s, float start, float length) substring = #116;
1745 // returns a section of a string as a tempstring
1746 void VM_substring(void)
1748 int i, start, length;
1752 VM_SAFEPARMCOUNT(3,VM_substring);
1754 string = VM_GetTempString();
1755 s = PRVM_G_STRING(OFS_PARM0);
1756 start = PRVM_G_FLOAT(OFS_PARM1);
1757 length = PRVM_G_FLOAT(OFS_PARM2);
1760 for (i = 0;i < start && *s;i++, s++);
1761 for (i = 0;i < VM_STRINGTEMP_LENGTH - 1 && *s && i < length;i++, s++)
1764 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(string);
1771 vector stov(string s)
1774 //vector(string s) stov = #117; // returns vector value from a string
1777 char string[VM_STRINGTEMP_LENGTH];
1779 VM_SAFEPARMCOUNT(1,VM_stov);
1781 VM_VarString(0, string, sizeof(string));
1782 Math_atov(string, PRVM_G_VECTOR(OFS_RETURN));
1789 string strzone(string s)
1792 //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)
1793 void VM_strzone(void)
1796 char string[VM_STRINGTEMP_LENGTH];
1798 VM_SAFEPARMCOUNT(1,VM_strzone);
1800 VM_VarString(0, string, sizeof(string));
1801 PRVM_G_INT(OFS_RETURN) = PRVM_AllocString(strlen(string) + 1, &out);
1802 strcpy(out, string);
1812 //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!!!)
1813 void VM_strunzone(void)
1815 VM_SAFEPARMCOUNT(1,VM_strunzone);
1816 PRVM_FreeString(PRVM_G_INT(OFS_PARM0));
1821 VM_command (used by client and menu)
1823 clientcommand(float client, string s) (for client and menu)
1826 //void(entity e, string s) clientcommand = #440; // executes a command string as if it came from the specified client
1827 //this function originally written by KrimZon, made shorter by LordHavoc
1828 void VM_clcommand (void)
1830 client_t *temp_client;
1833 VM_SAFEPARMCOUNT(2,VM_clcommand);
1835 i = PRVM_G_FLOAT(OFS_PARM0);
1836 if (!sv.active || i < 0 || i >= svs.maxclients || !svs.clients[i].active)
1838 Con_Printf("VM_clientcommand: %s: invalid client/server is not active !\n", PRVM_NAME);
1842 temp_client = host_client;
1843 host_client = svs.clients + i;
1844 Cmd_ExecuteString (PRVM_G_STRING(OFS_PARM1), src_client);
1845 host_client = temp_client;
1853 float tokenize(string s)
1856 //float(string s) tokenize = #441; // takes apart a string into individal words (access them with argv), returns how many
1857 //this function originally written by KrimZon, made shorter by LordHavoc
1858 //20040203: rewritten by LordHavoc (no longer uses allocations)
1860 char *tokens[256], tokenbuf[MAX_INPUTLINE];
1861 void VM_tokenize (void)
1866 VM_SAFEPARMCOUNT(1,VM_tokenize);
1868 p = PRVM_G_STRING(OFS_PARM0);
1872 while(COM_ParseToken(&p, false))
1874 if (num_tokens >= (int)(sizeof(tokens)/sizeof(tokens[0])))
1876 if (pos + strlen(com_token) + 1 > sizeof(tokenbuf))
1878 tokens[num_tokens++] = tokenbuf + pos;
1879 strcpy(tokenbuf + pos, com_token);
1880 pos += strlen(com_token) + 1;
1883 PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
1886 //string(float n) argv = #442; // returns a word from the tokenized string (returns nothing for an invalid index)
1887 //this function originally written by KrimZon, made shorter by LordHavoc
1892 VM_SAFEPARMCOUNT(1,VM_argv);
1894 token_num = PRVM_G_FLOAT(OFS_PARM0);
1896 if (token_num >= 0 && token_num < num_tokens)
1897 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(tokens[token_num]);
1899 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(NULL);
1903 //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)
1904 void PF_setattachment (void)
1906 prvm_edict_t *e = PRVM_G_EDICT(OFS_PARM0);
1907 prvm_edict_t *tagentity = PRVM_G_EDICT(OFS_PARM1);
1908 char *tagname = PRVM_G_STRING(OFS_PARM2);
1913 if (tagentity == NULL)
1914 tagentity = prog->edicts;
1916 v = PRVM_GETEDICTFIELDVALUE(e, eval_tag_entity);
1918 fields.server->edict = PRVM_EDICT_TO_PROG(tagentity);
1920 v = PRVM_GETEDICTFIELDVALUE(e, eval_tag_index);
1922 fields.server->_float = 0;
1923 if (tagentity != NULL && tagentity != prog->edicts && tagname && tagname[0])
1925 modelindex = (int)tagentity->fields.server->modelindex;
1926 if (modelindex >= 0 && modelindex < MAX_MODELS)
1928 model = sv.models[modelindex];
1929 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)
1930 for (i = 0;i < model->data_overridetagnamesforskin[(unsigned int)tagentity->fields.server->skin].num_overridetagnames;i++)
1931 if (!strcmp(tagname, model->data_overridetagnamesforskin[(unsigned int)tagentity->fields.server->skin].data_overridetagnames[i].name))
1932 fields.server->_float = i + 1;
1933 // FIXME: use a model function to get tag info (need to handle skeletal)
1934 if (fields.server->_float == 0 && model->num_tags)
1935 for (i = 0;i < model->num_tags;i++)
1936 if (!strcmp(tagname, model->data_tags[i].name))
1937 fields.server->_float = i + 1;
1938 if (fields.server->_float == 0)
1939 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);
1942 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));
1953 void VM_isserver(void)
1955 VM_SAFEPARMCOUNT(0,VM_serverstate);
1957 PRVM_G_FLOAT(OFS_RETURN) = sv.active;
1967 void VM_clientcount(void)
1969 VM_SAFEPARMCOUNT(0,VM_clientcount);
1971 PRVM_G_FLOAT(OFS_RETURN) = svs.maxclients;
1981 void VM_clientstate(void)
1983 VM_SAFEPARMCOUNT(0,VM_clientstate);
1985 PRVM_G_FLOAT(OFS_RETURN) = cls.state;
1992 float getostype(void)
1994 */ // not used at the moment -> not included in the common list
1995 void VM_getostype(void)
1997 VM_SAFEPARMCOUNT(0,VM_getostype);
2002 OS_MAC - not supported
2006 PRVM_G_FLOAT(OFS_RETURN) = 0;
2008 PRVM_G_FLOAT(OFS_RETURN) = 2;
2010 PRVM_G_FLOAT(OFS_RETURN) = 1;
2018 vector getmousepos()
2021 void VM_getmousepos(void)
2024 VM_SAFEPARMCOUNT(0,VM_getmousepos);
2026 PRVM_G_VECTOR(OFS_RETURN)[0] = in_mouse_x * vid_conwidth.integer / vid.width;
2027 PRVM_G_VECTOR(OFS_RETURN)[1] = in_mouse_y * vid_conheight.integer / vid.height;
2028 PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
2038 void VM_gettime(void)
2040 VM_SAFEPARMCOUNT(0,VM_gettime);
2042 PRVM_G_FLOAT(OFS_RETURN) = (float) *prog->time;
2049 loadfromdata(string data)
2052 void VM_loadfromdata(void)
2054 VM_SAFEPARMCOUNT(1,VM_loadentsfromfile);
2056 PRVM_ED_LoadFromFile(PRVM_G_STRING(OFS_PARM0));
2060 ========================
2063 parseentitydata(entity ent, string data)
2064 ========================
2066 void VM_parseentitydata(void)
2071 VM_SAFEPARMCOUNT(2, VM_parseentitydata);
2073 // get edict and test it
2074 ent = PRVM_G_EDICT(OFS_PARM0);
2075 if (ent->priv.required->free)
2076 PRVM_ERROR ("VM_parseentitydata: %s: Can only set already spawned entities (entity %i is free)!", PRVM_NAME, PRVM_NUM_FOR_EDICT(ent));
2078 data = PRVM_G_STRING(OFS_PARM1);
2080 // parse the opening brace
2081 if (!COM_ParseToken(&data, false) || com_token[0] != '{' )
2082 PRVM_ERROR ("VM_parseentitydata: %s: Couldn't parse entity data:\n%s", PRVM_NAME, data );
2084 PRVM_ED_ParseEdict (data, ent);
2091 loadfromfile(string file)
2094 void VM_loadfromfile(void)
2096 const char *filename;
2099 VM_SAFEPARMCOUNT(1,VM_loadfromfile);
2101 filename = PRVM_G_STRING(OFS_PARM0);
2102 // .. is parent directory on many platforms
2103 // / is parent directory on Amiga
2104 // : is root of drive on Amiga (also used as a directory separator on Mac, but / works there too, so that's a bad idea)
2105 // \ is a windows-ism (so it's naughty to use it, / works on all platforms)
2106 if ((filename[0] == '.' && filename[1] == '.') || filename[0] == '/' || strrchr(filename, ':') || strrchr(filename, '\\'))
2108 Con_Printf("VM_loadfromfile: %s dangerous or non-portable filename \"%s\" not allowed. (contains : or \\ or begins with .. or /)\n", PRVM_NAME, filename);
2109 PRVM_G_FLOAT(OFS_RETURN) = -4;
2113 // not conform with VM_fopen
2114 data = (char *)FS_LoadFile(filename, tempmempool, false, NULL);
2116 PRVM_G_FLOAT(OFS_RETURN) = -1;
2118 PRVM_ED_LoadFromFile(data);
2129 float mod(float val, float m)
2132 void VM_modulo(void)
2135 VM_SAFEPARMCOUNT(2,VM_module);
2137 val = (int) PRVM_G_FLOAT(OFS_PARM0);
2138 m = (int) PRVM_G_FLOAT(OFS_PARM1);
2140 PRVM_G_FLOAT(OFS_RETURN) = (float) (val % m);
2143 void VM_Search_Init(void)
2145 memset(VM_SEARCHLIST,0,sizeof(fssearch_t*[MAX_VMSEARCHES]));
2148 void VM_Search_Reset(void)
2151 // reset the fssearch list
2152 for(i = 0; i < MAX_VMSEARCHES; i++)
2153 if(VM_SEARCHLIST[i])
2154 FS_FreeSearch(VM_SEARCHLIST[i]);
2155 memset(VM_SEARCHLIST,0,sizeof(fssearch_t*[MAX_VMSEARCHES]));
2162 float search_begin(string pattern, float caseinsensitive, float quiet)
2165 void VM_search_begin(void)
2168 const char *pattern;
2169 int caseinsens, quiet;
2171 VM_SAFEPARMCOUNT(3, VM_search_begin);
2173 pattern = PRVM_G_STRING(OFS_PARM0);
2175 VM_CheckEmptyString(pattern);
2177 caseinsens = PRVM_G_FLOAT(OFS_PARM1);
2178 quiet = PRVM_G_FLOAT(OFS_PARM2);
2180 for(handle = 0; handle < MAX_VMSEARCHES; handle++)
2181 if(!VM_SEARCHLIST[handle])
2184 if(handle >= MAX_VMSEARCHES)
2186 Con_Printf("VM_search_begin: %s ran out of search handles (%i)\n", PRVM_NAME, MAX_VMSEARCHES);
2187 PRVM_G_FLOAT(OFS_RETURN) = -2;
2191 if(!(VM_SEARCHLIST[handle] = FS_Search(pattern,caseinsens, quiet)))
2192 PRVM_G_FLOAT(OFS_RETURN) = -1;
2194 PRVM_G_FLOAT(OFS_RETURN) = handle;
2201 void search_end(float handle)
2204 void VM_search_end(void)
2207 VM_SAFEPARMCOUNT(1, VM_search_end);
2209 handle = PRVM_G_FLOAT(OFS_PARM0);
2211 if(handle < 0 || handle >= MAX_VMSEARCHES)
2213 Con_Printf("VM_search_end: invalid handle %i used in %s\n", handle, PRVM_NAME);
2216 if(VM_SEARCHLIST[handle] == NULL)
2218 Con_Printf("VM_search_end: no such handle %i in %s\n", handle, PRVM_NAME);
2222 FS_FreeSearch(VM_SEARCHLIST[handle]);
2223 VM_SEARCHLIST[handle] = NULL;
2230 float search_getsize(float handle)
2233 void VM_search_getsize(void)
2236 VM_SAFEPARMCOUNT(1, VM_M_search_getsize);
2238 handle = PRVM_G_FLOAT(OFS_PARM0);
2240 if(handle < 0 || handle >= MAX_VMSEARCHES)
2242 Con_Printf("VM_search_getsize: invalid handle %i used in %s\n", handle, PRVM_NAME);
2245 if(VM_SEARCHLIST[handle] == NULL)
2247 Con_Printf("VM_search_getsize: no such handle %i in %s\n", handle, PRVM_NAME);
2251 PRVM_G_FLOAT(OFS_RETURN) = VM_SEARCHLIST[handle]->numfilenames;
2256 VM_search_getfilename
2258 string search_getfilename(float handle, float num)
2261 void VM_search_getfilename(void)
2263 int handle, filenum;
2265 VM_SAFEPARMCOUNT(2, VM_search_getfilename);
2267 handle = PRVM_G_FLOAT(OFS_PARM0);
2268 filenum = PRVM_G_FLOAT(OFS_PARM1);
2270 if(handle < 0 || handle >= MAX_VMSEARCHES)
2272 Con_Printf("VM_search_getfilename: invalid handle %i used in %s\n", handle, PRVM_NAME);
2275 if(VM_SEARCHLIST[handle] == NULL)
2277 Con_Printf("VM_search_getfilename: no such handle %i in %s\n", handle, PRVM_NAME);
2280 if(filenum < 0 || filenum >= VM_SEARCHLIST[handle]->numfilenames)
2282 Con_Printf("VM_search_getfilename: invalid filenum %i in %s\n", filenum, PRVM_NAME);
2286 tmp = VM_GetTempString();
2287 strcpy(tmp, VM_SEARCHLIST[handle]->filenames[filenum]);
2289 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(tmp);
2296 string chr(float ascii)
2302 VM_SAFEPARMCOUNT(1, VM_chr);
2304 tmp = VM_GetTempString();
2305 tmp[0] = (unsigned char) PRVM_G_FLOAT(OFS_PARM0);
2308 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(tmp);
2311 //=============================================================================
2312 // Draw builtins (client & menu)
2318 float iscachedpic(string pic)
2321 void VM_iscachedpic(void)
2323 VM_SAFEPARMCOUNT(1,VM_iscachedpic);
2325 // drawq hasnt such a function, thus always return true
2326 PRVM_G_FLOAT(OFS_RETURN) = false;
2333 string precache_pic(string pic)
2336 void VM_precache_pic(void)
2340 VM_SAFEPARMCOUNT(1, VM_precache_pic);
2342 s = PRVM_G_STRING(OFS_PARM0);
2343 PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
2346 PRVM_ERROR ("VM_precache_pic: %s: NULL", PRVM_NAME);
2348 VM_CheckEmptyString (s);
2350 // AK Draw_CachePic is supposed to always return a valid pointer
2351 if( Draw_CachePic(s, false)->tex == r_texture_notexture )
2352 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(NULL);
2362 void VM_freepic(void)
2366 VM_SAFEPARMCOUNT(1,VM_freepic);
2368 s = PRVM_G_STRING(OFS_PARM0);
2371 PRVM_ERROR ("VM_freepic: %s: NULL");
2373 VM_CheckEmptyString (s);
2382 float drawcharacter(vector position, float character, vector scale, vector rgb, float alpha, float flag)
2385 void VM_drawcharacter(void)
2387 float *pos,*scale,*rgb;
2390 VM_SAFEPARMCOUNT(6,VM_drawcharacter);
2392 character = (char) PRVM_G_FLOAT(OFS_PARM1);
2395 Con_Printf("VM_drawcharacter: %s passed null character !\n",PRVM_NAME);
2396 PRVM_G_FLOAT(OFS_RETURN) = -1;
2400 pos = PRVM_G_VECTOR(OFS_PARM0);
2401 scale = PRVM_G_VECTOR(OFS_PARM2);
2402 rgb = PRVM_G_VECTOR(OFS_PARM3);
2403 flag = (int)PRVM_G_FLOAT(OFS_PARM5);
2405 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2407 Con_Printf("VM_drawcharacter: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2408 PRVM_G_FLOAT(OFS_RETURN) = -2;
2412 if(pos[2] || scale[2])
2413 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")));
2415 if(!scale[0] || !scale[1])
2417 Con_Printf("VM_drawcharacter: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
2418 PRVM_G_FLOAT(OFS_RETURN) = -3;
2422 DrawQ_String (pos[0], pos[1], &character, 1, scale[0], scale[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM4), flag);
2423 PRVM_G_FLOAT(OFS_RETURN) = 1;
2430 float drawstring(vector position, string text, vector scale, vector rgb, float alpha, float flag)
2433 void VM_drawstring(void)
2435 float *pos,*scale,*rgb;
2438 VM_SAFEPARMCOUNT(6,VM_drawstring);
2440 string = PRVM_G_STRING(OFS_PARM1);
2443 Con_Printf("VM_drawstring: %s passed null string !\n",PRVM_NAME);
2444 PRVM_G_FLOAT(OFS_RETURN) = -1;
2448 //VM_CheckEmptyString(string); Why should it be checked - perhaps the menu wants to support the precolored letters, too?
2450 pos = PRVM_G_VECTOR(OFS_PARM0);
2451 scale = PRVM_G_VECTOR(OFS_PARM2);
2452 rgb = PRVM_G_VECTOR(OFS_PARM3);
2453 flag = (int)PRVM_G_FLOAT(OFS_PARM5);
2455 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2457 Con_Printf("VM_drawstring: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2458 PRVM_G_FLOAT(OFS_RETURN) = -2;
2462 if(!scale[0] || !scale[1])
2464 Con_Printf("VM_drawstring: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
2465 PRVM_G_FLOAT(OFS_RETURN) = -3;
2469 if(pos[2] || scale[2])
2470 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")));
2472 DrawQ_String (pos[0], pos[1], string, 0, scale[0], scale[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM4), flag);
2473 PRVM_G_FLOAT(OFS_RETURN) = 1;
2479 float drawpic(vector position, string pic, vector size, vector rgb, float alpha, float flag)
2482 void VM_drawpic(void)
2485 float *size, *pos, *rgb;
2488 VM_SAFEPARMCOUNT(6,VM_drawpic);
2490 pic = PRVM_G_STRING(OFS_PARM1);
2494 Con_Printf("VM_drawpic: %s passed null picture name !\n", PRVM_NAME);
2495 PRVM_G_FLOAT(OFS_RETURN) = -1;
2499 VM_CheckEmptyString (pic);
2501 // is pic cached ? no function yet for that
2504 Con_Printf("VM_drawpic: %s: %s not cached !\n", PRVM_NAME, pic);
2505 PRVM_G_FLOAT(OFS_RETURN) = -4;
2509 pos = PRVM_G_VECTOR(OFS_PARM0);
2510 size = PRVM_G_VECTOR(OFS_PARM2);
2511 rgb = PRVM_G_VECTOR(OFS_PARM3);
2512 flag = (int) PRVM_G_FLOAT(OFS_PARM5);
2514 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2516 Con_Printf("VM_drawstring: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2517 PRVM_G_FLOAT(OFS_RETURN) = -2;
2521 if(pos[2] || size[2])
2522 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")));
2524 DrawQ_Pic(pos[0], pos[1], pic, size[0], size[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM4), flag);
2525 PRVM_G_FLOAT(OFS_RETURN) = 1;
2532 float drawfill(vector position, vector size, vector rgb, float alpha, float flag)
2535 void VM_drawfill(void)
2537 float *size, *pos, *rgb;
2540 VM_SAFEPARMCOUNT(5,VM_drawfill);
2543 pos = PRVM_G_VECTOR(OFS_PARM0);
2544 size = PRVM_G_VECTOR(OFS_PARM1);
2545 rgb = PRVM_G_VECTOR(OFS_PARM2);
2546 flag = (int) PRVM_G_FLOAT(OFS_PARM4);
2548 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2550 Con_Printf("VM_drawstring: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2551 PRVM_G_FLOAT(OFS_RETURN) = -2;
2555 if(pos[2] || size[2])
2556 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")));
2558 DrawQ_Pic(pos[0], pos[1], 0, size[0], size[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM3), flag);
2559 PRVM_G_FLOAT(OFS_RETURN) = 1;
2566 drawsetcliparea(float x, float y, float width, float height)
2569 void VM_drawsetcliparea(void)
2572 VM_SAFEPARMCOUNT(4,VM_drawsetcliparea);
2574 x = bound(0, PRVM_G_FLOAT(OFS_PARM0), vid_conwidth.integer);
2575 y = bound(0, PRVM_G_FLOAT(OFS_PARM1), vid_conheight.integer);
2576 w = bound(0, PRVM_G_FLOAT(OFS_PARM2) + PRVM_G_FLOAT(OFS_PARM0) - x, (vid_conwidth.integer - x));
2577 h = bound(0, PRVM_G_FLOAT(OFS_PARM3) + PRVM_G_FLOAT(OFS_PARM1) - y, (vid_conheight.integer - y));
2579 DrawQ_SetClipArea(x, y, w, h);
2584 VM_drawresetcliparea
2589 void VM_drawresetcliparea(void)
2591 VM_SAFEPARMCOUNT(0,VM_drawresetcliparea);
2593 DrawQ_ResetClipArea();
2600 vector getimagesize(string pic)
2603 void VM_getimagesize(void)
2608 VM_SAFEPARMCOUNT(1,VM_getimagesize);
2610 p = PRVM_G_STRING(OFS_PARM0);
2613 PRVM_ERROR("VM_getimagepos: %s passed null picture name !", PRVM_NAME);
2615 VM_CheckEmptyString (p);
2617 pic = Draw_CachePic (p, false);
2619 PRVM_G_VECTOR(OFS_RETURN)[0] = pic->width;
2620 PRVM_G_VECTOR(OFS_RETURN)[1] = pic->height;
2621 PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
2628 string keynumtostring(float keynum)
2631 void VM_keynumtostring (void)
2635 VM_SAFEPARMCOUNT(1, VM_keynumtostring);
2637 keynum = PRVM_G_FLOAT(OFS_PARM0);
2639 tmp = VM_GetTempString();
2641 strcpy(tmp, Key_KeynumToString(keynum));
2643 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(tmp);
2650 float stringtokeynum(string key)
2653 void VM_stringtokeynum (void)
2656 VM_SAFEPARMCOUNT( 1, VM_keynumtostring );
2658 str = PRVM_G_STRING( OFS_PARM0 );
2660 PRVM_G_INT(OFS_RETURN) = Key_StringToKeynum( str );
2663 // CL_Video interface functions
2666 ========================
2669 float cin_open(string file, string name)
2670 ========================
2672 void VM_cin_open( void )
2677 VM_SAFEPARMCOUNT( 2, VM_cin_open );
2679 file = PRVM_G_STRING( OFS_PARM0 );
2680 name = PRVM_G_STRING( OFS_PARM1 );
2682 VM_CheckEmptyString( file );
2683 VM_CheckEmptyString( name );
2685 if( CL_OpenVideo( file, name, MENUOWNER ) )
2686 PRVM_G_FLOAT( OFS_RETURN ) = 1;
2688 PRVM_G_FLOAT( OFS_RETURN ) = 0;
2692 ========================
2695 void cin_close(string name)
2696 ========================
2698 void VM_cin_close( void )
2702 VM_SAFEPARMCOUNT( 1, VM_cin_close );
2704 name = PRVM_G_STRING( OFS_PARM0 );
2705 VM_CheckEmptyString( name );
2707 CL_CloseVideo( CL_GetVideo( name ) );
2711 ========================
2713 void cin_setstate(string name, float type)
2714 ========================
2716 void VM_cin_setstate( void )
2719 clvideostate_t state;
2722 VM_SAFEPARMCOUNT( 2, VM_cin_netstate );
2724 name = PRVM_G_STRING( OFS_PARM0 );
2725 VM_CheckEmptyString( name );
2727 state = (clvideostate_t)((int)PRVM_G_FLOAT( OFS_PARM1 ));
2729 video = CL_GetVideo( name );
2730 if( video && state > CLVIDEO_UNUSED && state < CLVIDEO_STATECOUNT )
2731 CL_SetVideoState( video, state );
2735 ========================
2738 float cin_getstate(string name)
2739 ========================
2741 void VM_cin_getstate( void )
2746 VM_SAFEPARMCOUNT( 1, VM_cin_getstate );
2748 name = PRVM_G_STRING( OFS_PARM0 );
2749 VM_CheckEmptyString( name );
2751 video = CL_GetVideo( name );
2753 PRVM_G_FLOAT( OFS_RETURN ) = (int)video->state;
2755 PRVM_G_FLOAT( OFS_RETURN ) = 0;
2759 ========================
2762 void cin_restart(string name)
2763 ========================
2765 void VM_cin_restart( void )
2770 VM_SAFEPARMCOUNT( 1, VM_cin_restart );
2772 name = PRVM_G_STRING( OFS_PARM0 );
2773 VM_CheckEmptyString( name );
2775 video = CL_GetVideo( name );
2777 CL_RestartVideo( video );
2784 Writes new values for v_forward, v_up, and v_right based on the given forward vector
2785 vectorvectors(vector, vector)
2788 void VM_vectorvectors (void)
2790 VectorNormalize2(PRVM_G_VECTOR(OFS_PARM0), prog->globals.server->v_forward);
2791 VectorVectors(prog->globals.server->v_forward, prog->globals.server->v_right, prog->globals.server->v_up);
2795 ========================
2798 void drawline(float width, vector pos1, vector pos2, vector rgb, float alpha, float flags)
2799 ========================
2801 void VM_drawline (void)
2803 float *c1, *c2, *rgb;
2805 unsigned char flags;
2807 VM_SAFEPARMCOUNT(6, VM_drawline);
2808 width = PRVM_G_FLOAT(OFS_PARM0);
2809 c1 = PRVM_G_VECTOR(OFS_PARM1);
2810 c2 = PRVM_G_VECTOR(OFS_PARM2);
2811 rgb = PRVM_G_VECTOR(OFS_PARM3);
2812 alpha = PRVM_G_FLOAT(OFS_PARM4);
2813 flags = PRVM_G_FLOAT(OFS_PARM5);
2814 DrawQ_Line(width, c1[0], c1[1], c2[0], c2[1], rgb[0], rgb[1], rgb[2], alpha, flags);
2817 //====================
2818 //QC POLYGON functions
2819 //====================
2824 float data[36]; //[515]: enough for polygons
2825 unsigned char flags; //[515]: + VM_POLYGON_2D and VM_POLYGON_FL4V flags
2828 //static float vm_polygon_linewidth = 1;
2829 static mempool_t *vm_polygons_pool = NULL;
2830 static unsigned char vm_current_vertices = 0;
2831 static qboolean vm_polygons_initialized = false;
2832 static vm_polygon_t *vm_polygons = NULL;
2833 static unsigned long vm_polygons_num = 0, vm_drawpolygons_num = 0; //[515]: ok long on 64bit ?
2834 static qboolean vm_polygonbegin = false; //[515]: for "no-crap-on-the-screen" check
2835 #define VM_DEFPOLYNUM 64 //[515]: enough for default ?
2837 #define VM_POLYGON_FL3V 16 //more than 2 vertices (used only for lines)
2838 #define VM_POLYGON_FLLINES 32
2839 #define VM_POLYGON_FL2D 64
2840 #define VM_POLYGON_FL4V 128 //4 vertices
2842 void VM_InitPolygons (void)
2844 vm_polygons_pool = Mem_AllocPool("VMPOLY", 0, NULL);
2845 vm_polygons = Mem_Alloc(vm_polygons_pool, VM_DEFPOLYNUM*sizeof(vm_polygon_t));
2846 memset(vm_polygons, 0, VM_DEFPOLYNUM*sizeof(vm_polygon_t));
2847 vm_polygons_num = VM_DEFPOLYNUM;
2848 vm_polygonbegin = vm_drawpolygons_num = 0;
2849 vm_polygons_initialized = true;
2852 void VM_DrawPolygonCallback (const entity_render_t *ent, int surfacenumber, const rtlight_t *rtlight)
2854 const vm_polygon_t *p = &vm_polygons[surfacenumber];
2855 int flags = p->flags & 0x0f;
2857 if(flags == DRAWFLAG_ADDITIVE)
2858 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
2859 else if(flags == DRAWFLAG_MODULATE)
2860 GL_BlendFunc(GL_DST_COLOR, GL_ZERO);
2861 else if(flags == DRAWFLAG_2XMODULATE)
2862 GL_BlendFunc(GL_DST_COLOR,GL_SRC_COLOR);
2864 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
2866 R_Mesh_TexBind(0, R_GetTexture(p->tex));
2868 //[515]: is speed is max ?
2869 if(p->flags & VM_POLYGON_FLLINES) //[515]: lines
2871 qglLineWidth(p->data[13]);
2872 qglBegin(GL_LINE_LOOP);
2873 qglTexCoord1f (p->data[12]);
2874 qglColor4f (p->data[20], p->data[21], p->data[22], p->data[23]);
2875 qglVertex3f (p->data[0] , p->data[1], p->data[2]);
2877 qglTexCoord1f (p->data[14]);
2878 qglColor4f (p->data[24], p->data[25], p->data[26], p->data[27]);
2879 qglVertex3f (p->data[3] , p->data[4], p->data[5]);
2881 if(p->flags & VM_POLYGON_FL3V)
2883 qglTexCoord1f (p->data[16]);
2884 qglColor4f (p->data[28], p->data[29], p->data[30], p->data[31]);
2885 qglVertex3f (p->data[6] , p->data[7], p->data[8]);
2887 if(p->flags & VM_POLYGON_FL4V)
2889 qglTexCoord1f (p->data[18]);
2890 qglColor4f (p->data[32], p->data[33], p->data[34], p->data[35]);
2891 qglVertex3f (p->data[9] , p->data[10], p->data[11]);
2898 qglBegin(GL_POLYGON);
2899 qglTexCoord2f (p->data[12], p->data[13]);
2900 qglColor4f (p->data[20], p->data[21], p->data[22], p->data[23]);
2901 qglVertex3f (p->data[0] , p->data[1], p->data[2]);
2903 qglTexCoord2f (p->data[14], p->data[15]);
2904 qglColor4f (p->data[24], p->data[25], p->data[26], p->data[27]);
2905 qglVertex3f (p->data[3] , p->data[4], p->data[5]);
2907 qglTexCoord2f (p->data[16], p->data[17]);
2908 qglColor4f (p->data[28], p->data[29], p->data[30], p->data[31]);
2909 qglVertex3f (p->data[6] , p->data[7], p->data[8]);
2911 if(p->flags & VM_POLYGON_FL4V)
2913 qglTexCoord2f (p->data[18], p->data[19]);
2914 qglColor4f (p->data[32], p->data[33], p->data[34], p->data[35]);
2915 qglVertex3f (p->data[9] , p->data[10], p->data[11]);
2921 void VM_AddPolygonTo2DScene (vm_polygon_t *p)
2923 drawqueuemesh_t mesh;
2924 static int picelements[6] = {0, 1, 2, 0, 2, 3};
2926 mesh.texture = p->tex;
2927 mesh.data_element3i = picelements;
2928 mesh.data_vertex3f = p->data;
2929 mesh.data_texcoord2f = p->data + 12;
2930 mesh.data_color4f = p->data + 20;
2931 if(p->flags & VM_POLYGON_FL4V)
2933 mesh.num_vertices = 4;
2934 mesh.num_triangles = 2;
2938 mesh.num_vertices = 3;
2939 mesh.num_triangles = 1;
2941 if(p->flags & VM_POLYGON_FLLINES) //[515]: lines
2942 DrawQ_LineLoop (&mesh, (p->flags&0x0f));
2944 DrawQ_Mesh (&mesh, (p->flags&0x0f));
2947 //void(string texturename, float flag, float 2d, float lines) R_BeginPolygon
2948 void VM_R_PolygonBegin (void)
2951 const char *picname;
2953 VM_SAFEPARMCOUNT(2, VM_R_PolygonBegin);
2955 if(!vm_polygons_initialized)
2959 Con_Printf("VM_R_PolygonBegin: called twice without VM_R_PolygonEnd after first\n");
2962 if(vm_drawpolygons_num >= vm_polygons_num)
2964 p = Mem_Alloc(vm_polygons_pool, 2 * vm_polygons_num * sizeof(vm_polygon_t));
2965 memset(p, 0, 2 * vm_polygons_num * sizeof(vm_polygon_t));
2966 memcpy(p, vm_polygons, vm_polygons_num * sizeof(vm_polygon_t));
2967 Mem_Free(vm_polygons);
2969 vm_polygons_num *= 2;
2971 p = &vm_polygons[vm_drawpolygons_num];
2972 picname = PRVM_G_STRING(OFS_PARM0);
2974 p->tex = Draw_CachePic(picname, false)->tex;
2976 p->tex = r_texture_notexture;
2977 p->flags = (unsigned char)PRVM_G_FLOAT(OFS_PARM1);
2978 vm_current_vertices = 0;
2979 vm_polygonbegin = true;
2982 if(PRVM_G_FLOAT(OFS_PARM2))
2983 p->flags |= VM_POLYGON_FL2D;
2984 if(prog->argc >= 4 && PRVM_G_FLOAT(OFS_PARM3))
2986 p->data[13] = PRVM_G_FLOAT(OFS_PARM3); //[515]: linewidth
2987 p->flags |= VM_POLYGON_FLLINES;
2992 //void(vector org, vector texcoords, vector rgb, float alpha) R_PolygonVertex
2993 void VM_R_PolygonVertex (void)
2995 float *coords, *tx, *rgb, alpha;
2997 VM_SAFEPARMCOUNT(4, VM_R_PolygonVertex);
2999 if(!vm_polygonbegin)
3001 Con_Printf("VM_R_PolygonVertex: VM_R_PolygonBegin wasn't called\n");
3004 coords = PRVM_G_VECTOR(OFS_PARM0);
3005 tx = PRVM_G_VECTOR(OFS_PARM1);
3006 rgb = PRVM_G_VECTOR(OFS_PARM2);
3007 alpha = PRVM_G_FLOAT(OFS_PARM3);
3009 p = &vm_polygons[vm_drawpolygons_num];
3010 if(vm_current_vertices > 4)
3012 Con_Printf("VM_R_PolygonVertex: may have 4 vertices max\n");
3016 p->data[vm_current_vertices*3] = coords[0];
3017 p->data[1+vm_current_vertices*3] = coords[1];
3018 if(!(p->flags & VM_POLYGON_FL2D))
3019 p->data[2+vm_current_vertices*3] = coords[2];
3021 p->data[12+vm_current_vertices*2] = tx[0];
3022 if(!(p->flags & VM_POLYGON_FLLINES))
3023 p->data[13+vm_current_vertices*2] = tx[1];
3025 p->data[20+vm_current_vertices*4] = rgb[0];
3026 p->data[21+vm_current_vertices*4] = rgb[1];
3027 p->data[22+vm_current_vertices*4] = rgb[2];
3028 p->data[23+vm_current_vertices*4] = alpha;
3030 vm_current_vertices++;
3031 if(vm_current_vertices == 4)
3032 p->flags |= VM_POLYGON_FL4V;
3034 if(vm_current_vertices == 3)
3035 p->flags |= VM_POLYGON_FL3V;
3038 //void() R_EndPolygon
3039 void VM_R_PolygonEnd (void)
3041 if(!vm_polygonbegin)
3043 Con_Printf("VM_R_PolygonEnd: VM_R_PolygonBegin wasn't called\n");
3046 if(vm_current_vertices > 2 || (vm_current_vertices >= 2 && vm_polygons[vm_drawpolygons_num].flags & VM_POLYGON_FLLINES))
3048 if(vm_polygons[vm_drawpolygons_num].flags & VM_POLYGON_FL2D) //[515]: don't use qcpolygons memory if 2D
3049 VM_AddPolygonTo2DScene(&vm_polygons[vm_drawpolygons_num]);
3051 vm_drawpolygons_num++;
3054 Con_Printf("VM_R_PolygonEnd: %i vertices isn't a good choice\n", vm_current_vertices);
3055 vm_polygonbegin = false;
3058 void VM_AddPolygonsToMeshQueue (void)
3061 if(!vm_drawpolygons_num)
3063 for(i = 0;i < vm_drawpolygons_num;i++)
3064 R_MeshQueue_Add(VM_DrawPolygonCallback, NULL, i, NULL);
3065 vm_drawpolygons_num = 0;
3071 // float(float number, float quantity) bitshift (EXT_BITSHIFT)
3072 void VM_bitshift (void)
3075 VM_SAFEPARMCOUNT(2, VM_bitshift);
3077 n1 = (int)fabs((int)PRVM_G_FLOAT(OFS_PARM0));
3078 n2 = (int)PRVM_G_FLOAT(OFS_PARM1);
3080 PRVM_G_FLOAT(OFS_RETURN) = n1;
3083 PRVM_G_FLOAT(OFS_RETURN) = (n1 >> -n2);
3085 PRVM_G_FLOAT(OFS_RETURN) = (n1 << n2);
3088 ////////////////////////////////////////
3089 // AltString functions
3090 ////////////////////////////////////////
3093 ========================
3096 float altstr_count(string)
3097 ========================
3099 void VM_altstr_count( void )
3101 const char *altstr, *pos;
3104 VM_SAFEPARMCOUNT( 1, VM_altstr_count );
3106 altstr = PRVM_G_STRING( OFS_PARM0 );
3107 //VM_CheckEmptyString( altstr );
3109 for( count = 0, pos = altstr ; *pos ; pos++ ) {
3110 if( *pos == '\\' ) {
3114 } else if( *pos == '\'' ) {
3119 PRVM_G_FLOAT( OFS_RETURN ) = (float) (count / 2);
3123 ========================
3126 string altstr_prepare(string)
3127 ========================
3129 void VM_altstr_prepare( void )
3132 const char *instr, *in;
3135 VM_SAFEPARMCOUNT( 1, VM_altstr_prepare );
3137 instr = PRVM_G_STRING( OFS_PARM0 );
3138 //VM_CheckEmptyString( instr );
3139 outstr = VM_GetTempString();
3141 for( out = outstr, in = instr, size = VM_STRINGTEMP_LENGTH - 1 ; size && *in ; size--, in++, out++ )
3150 PRVM_G_INT( OFS_RETURN ) = PRVM_SetEngineString( outstr );
3154 ========================
3157 string altstr_get(string, float)
3158 ========================
3160 void VM_altstr_get( void )
3162 const char *altstr, *pos;
3166 VM_SAFEPARMCOUNT( 2, VM_altstr_get );
3168 altstr = PRVM_G_STRING( OFS_PARM0 );
3169 //VM_CheckEmptyString( altstr );
3171 count = PRVM_G_FLOAT( OFS_PARM1 );
3172 count = count * 2 + 1;
3174 for( pos = altstr ; *pos && count ; pos++ )
3175 if( *pos == '\\' ) {
3178 } else if( *pos == '\'' )
3182 PRVM_G_INT( OFS_RETURN ) = PRVM_SetEngineString( NULL );
3186 outstr = VM_GetTempString();
3187 for( out = outstr, size = VM_STRINGTEMP_LENGTH - 1 ; size && *pos ; size--, pos++, out++ )
3188 if( *pos == '\\' ) {
3193 } else if( *pos == '\'' )
3199 PRVM_G_INT( OFS_RETURN ) = PRVM_SetEngineString( outstr );
3203 ========================
3206 string altstr_set(string altstr, float num, string set)
3207 ========================
3209 void VM_altstr_set( void )
3212 const char *altstr, *str;
3216 VM_SAFEPARMCOUNT( 3, VM_altstr_set );
3218 altstr = PRVM_G_STRING( OFS_PARM0 );
3219 //VM_CheckEmptyString( altstr );
3221 num = PRVM_G_FLOAT( OFS_PARM1 );
3223 str = PRVM_G_STRING( OFS_PARM2 );
3224 //VM_CheckEmptyString( str );
3226 outstr = out = VM_GetTempString();
3227 for( num = num * 2 + 1, in = altstr; *in && num; *out++ = *in++ )
3232 } else if( *in == '\'' ) {
3237 PRVM_G_INT( OFS_RETURN ) = PRVM_SetEngineString( altstr );
3241 for( ; *str; *out++ = *str++ );
3242 // now jump over the old content
3244 if( *in == '\'' || (*in == '\\' && !*++in) )
3248 PRVM_G_INT( OFS_RETURN ) = PRVM_SetEngineString( NULL );
3253 PRVM_G_INT( OFS_RETURN ) = PRVM_SetEngineString( outstr );
3257 ========================
3260 string altstr_ins(string altstr, float num, string set)
3261 ========================
3263 void VM_altstr_ins(void)
3273 in = instr = PRVM_G_STRING( OFS_PARM0 );
3274 num = PRVM_G_FLOAT( OFS_PARM1 );
3275 set = setstr = PRVM_G_STRING( OFS_PARM2 );
3277 out = outstr = VM_GetTempString();
3278 for( num = num * 2 + 2 ; *in && num > 0 ; *out++ = *in++ )
3283 } else if( *in == '\'' ) {
3288 for( ; *set ; *out++ = *set++ );
3292 PRVM_G_INT( OFS_RETURN ) = PRVM_SetEngineString( outstr );
3296 ////////////////////////////////////////
3297 // BufString functions
3298 ////////////////////////////////////////
3299 //[515]: string buffers support
3300 #define MAX_QCSTR_BUFFERS 128
3301 #define MAX_QCSTR_STRINGS 1024
3306 char *strings[MAX_QCSTR_STRINGS];
3309 static qcstrbuffer_t *qcstringbuffers[MAX_QCSTR_BUFFERS];
3310 static int num_qcstringbuffers;
3311 static int buf_sortpower;
3313 #define BUFSTR_BUFFER(a) (a>=MAX_QCSTR_BUFFERS) ? NULL : (qcstringbuffers[a])
3314 #define BUFSTR_ISFREE(a) (a<MAX_QCSTR_BUFFERS&&qcstringbuffers[a]&&qcstringbuffers[a]->num_strings<=0) ? 1 : 0
3316 static int BufStr_FindFreeBuffer (void)
3319 if(num_qcstringbuffers == MAX_QCSTR_BUFFERS)
3321 for(i=0;i<MAX_QCSTR_BUFFERS;i++)
3322 if(!qcstringbuffers[i])
3324 qcstringbuffers[i] = malloc(sizeof(qcstrbuffer_t));
3325 memset(qcstringbuffers[i], 0, sizeof(qcstrbuffer_t));
3331 static void BufStr_ClearBuffer (int index)
3333 qcstrbuffer_t *b = qcstringbuffers[index];
3338 if(b->num_strings > 0)
3340 for(i=0;i<b->num_strings;i++)
3342 free(b->strings[i]);
3343 num_qcstringbuffers--;
3345 free(qcstringbuffers[index]);
3346 qcstringbuffers[index] = NULL;
3350 static int BufStr_FindFreeString (qcstrbuffer_t *b)
3353 for(i=0;i<b->num_strings;i++)
3354 if(!b->strings[i] || !b->strings[i][0])
3356 if(i == MAX_QCSTR_STRINGS) return -1;
3360 static int BufStr_SortStringsUP (const void *in1, const void *in2)
3363 a = *((const char **) in1);
3364 b = *((const char **) in2);
3366 if(!b[0]) return -1;
3367 return strncmp(a, b, buf_sortpower);
3370 static int BufStr_SortStringsDOWN (const void *in1, const void *in2)
3373 a = *((const char **) in1);
3374 b = *((const char **) in2);
3376 if(!b[0]) return -1;
3377 return strncmp(b, a, buf_sortpower);
3381 static void VM_BufStr_Init (void)
3383 memset(qcstringbuffers, 0, sizeof(qcstringbuffers));
3384 num_qcstringbuffers = 0;
3387 static void VM_BufStr_ShutDown (void)
3390 for(i=0;i<MAX_QCSTR_BUFFERS && num_qcstringbuffers;i++)
3391 BufStr_ClearBuffer(i);
3396 ========================
3398 creates new buffer, and returns it's index, returns -1 if failed
3399 float buf_create(void) = #460;
3400 ========================
3402 void VM_buf_create (void)
3405 VM_SAFEPARMCOUNT(0, VM_buf_create);
3406 i = BufStr_FindFreeBuffer();
3408 num_qcstringbuffers++;
3410 //Con_Printf("VM_buf_create: buffers overflow in %s\n", PRVM_NAME);
3411 PRVM_G_FLOAT(OFS_RETURN) = i;
3415 ========================
3417 deletes buffer and all strings in it
3418 void buf_del(float bufhandle) = #461;
3419 ========================
3421 void VM_buf_del (void)
3423 VM_SAFEPARMCOUNT(1, VM_buf_del);
3424 if(BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0)))
3425 BufStr_ClearBuffer((int)PRVM_G_FLOAT(OFS_PARM0));
3428 Con_Printf("VM_buf_del: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3434 ========================
3436 how many strings are stored in buffer
3437 float buf_getsize(float bufhandle) = #462;
3438 ========================
3440 void VM_buf_getsize (void)
3443 VM_SAFEPARMCOUNT(1, VM_buf_getsize);
3445 b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3448 PRVM_G_FLOAT(OFS_RETURN) = -1;
3449 Con_Printf("VM_buf_getsize: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3453 PRVM_G_FLOAT(OFS_RETURN) = b->num_strings;
3457 ========================
3459 copy all content from one buffer to another, make sure it exists
3460 void buf_copy(float bufhandle_from, float bufhandle_to) = #463;
3461 ========================
3463 void VM_buf_copy (void)
3465 qcstrbuffer_t *b1, *b2;
3467 VM_SAFEPARMCOUNT(2, VM_buf_copy);
3469 b1 = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3472 Con_Printf("VM_buf_copy: invalid source buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3475 i = PRVM_G_FLOAT(OFS_PARM1);
3476 if(i == (int)PRVM_G_FLOAT(OFS_PARM0))
3478 Con_Printf("VM_buf_copy: source == destination (%i) in %s\n", i, PRVM_NAME);
3481 b2 = BUFSTR_BUFFER(i);
3484 Con_Printf("VM_buf_copy: invalid destination buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM1), PRVM_NAME);
3488 BufStr_ClearBuffer(i);
3489 qcstringbuffers[i] = malloc(sizeof(qcstrbuffer_t));
3490 memset(qcstringbuffers[i], 0, sizeof(qcstrbuffer_t));
3491 b2->num_strings = b1->num_strings;
3493 for(i=0;i<b1->num_strings;i++)
3494 if(b1->strings[i] && b1->strings[i][0])
3496 b2->strings[i] = malloc(strlen(b1->strings[i])+1);
3499 Con_Printf("VM_buf_copy: not enough memory for buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM1), PRVM_NAME);
3502 strcpy(b2->strings[i], b1->strings[i]);
3507 ========================
3509 sort buffer by beginnings of strings (sortpower defaults it's lenght)
3510 "backward == TRUE" means that sorting goes upside-down
3511 void buf_sort(float bufhandle, float sortpower, float backward) = #464;
3512 ========================
3514 void VM_buf_sort (void)
3518 VM_SAFEPARMCOUNT(3, VM_buf_sort);
3520 b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3523 Con_Printf("VM_buf_sort: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3526 if(b->num_strings <= 0)
3528 Con_Printf("VM_buf_sort: tried to sort empty buffer %i in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3531 buf_sortpower = PRVM_G_FLOAT(OFS_PARM1);
3532 if(buf_sortpower <= 0)
3533 buf_sortpower = 99999999;
3535 if(!PRVM_G_FLOAT(OFS_PARM2))
3536 qsort(b->strings, b->num_strings, sizeof(char*), BufStr_SortStringsUP);
3538 qsort(b->strings, b->num_strings, sizeof(char*), BufStr_SortStringsDOWN);
3540 for(i=b->num_strings-1;i>=0;i--) //[515]: delete empty lines
3543 if(b->strings[i][0])
3547 free(b->strings[i]);
3549 b->strings[i] = NULL;
3557 ========================
3559 concantenates all buffer string into one with "glue" separator and returns it as tempstring
3560 string buf_implode(float bufhandle, string glue) = #465;
3561 ========================
3563 void VM_buf_implode (void)
3570 VM_SAFEPARMCOUNT(2, VM_buf_implode);
3572 b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3573 PRVM_G_INT(OFS_RETURN) = 0;
3576 Con_Printf("VM_buf_implode: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3581 sep = PRVM_G_STRING(OFS_PARM1);
3582 k = VM_GetTempString();
3584 for(l=i=0;i<b->num_strings;i++)
3587 l += strlen(b->strings[i]);
3590 k = strcat(k, b->strings[i]);
3593 if(sep && (i != b->num_strings-1))
3603 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(k);
3607 ========================
3609 get a string from buffer, returns direct pointer, dont str_unzone it!
3610 string bufstr_get(float bufhandle, float string_index) = #465;
3611 ========================
3613 void VM_bufstr_get (void)
3617 VM_SAFEPARMCOUNT(2, VM_bufstr_get);
3619 b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3622 Con_Printf("VM_bufstr_get: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3625 strindex = (int)PRVM_G_FLOAT(OFS_PARM1);
3626 if(strindex < 0 || strindex > MAX_QCSTR_STRINGS)
3628 Con_Printf("VM_bufstr_get: invalid string index %i used in %s\n", strindex, PRVM_NAME);
3631 PRVM_G_INT(OFS_RETURN) = 0;
3632 if(b->num_strings <= strindex)
3634 if(b->strings[strindex])
3635 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(b->strings[strindex]);
3639 ========================
3641 copies a string into selected slot of buffer
3642 void bufstr_set(float bufhandle, float string_index, string str) = #466;
3643 ========================
3645 void VM_bufstr_set (void)
3647 int bufindex, strindex;
3651 VM_SAFEPARMCOUNT(3, VM_bufstr_set);
3653 bufindex = PRVM_G_FLOAT(OFS_PARM0);
3654 b = BUFSTR_BUFFER(bufindex);
3657 Con_Printf("VM_bufstr_set: invalid buffer %i used in %s\n", bufindex, PRVM_NAME);
3660 strindex = PRVM_G_FLOAT(OFS_PARM1);
3661 if(strindex < 0 || strindex > MAX_QCSTR_STRINGS)
3663 Con_Printf("VM_bufstr_set: invalid string index %i used in %s\n", strindex, PRVM_NAME);
3666 news = PRVM_G_STRING(OFS_PARM2);
3669 Con_Printf("VM_bufstr_set: null string used in %s\n", PRVM_NAME);
3672 if(b->strings[strindex])
3673 free(b->strings[strindex]);
3674 b->strings[strindex] = malloc(strlen(news)+1);
3675 strcpy(b->strings[strindex], news);
3679 ========================
3681 adds string to buffer in nearest free slot and returns it
3682 "order == TRUE" means that string will be added after last "full" slot
3683 float bufstr_add(float bufhandle, string str, float order) = #467;
3684 ========================
3686 void VM_bufstr_add (void)
3688 int bufindex, order, strindex;
3692 VM_SAFEPARMCOUNT(3, VM_bufstr_add);
3694 bufindex = PRVM_G_FLOAT(OFS_PARM0);
3695 b = BUFSTR_BUFFER(bufindex);
3696 PRVM_G_FLOAT(OFS_RETURN) = -1;
3699 Con_Printf("VM_bufstr_add: invalid buffer %i used in %s\n", bufindex, PRVM_NAME);
3702 string = PRVM_G_STRING(OFS_PARM1);
3705 Con_Printf("VM_bufstr_add: null string used in %s\n", PRVM_NAME);
3709 order = PRVM_G_FLOAT(OFS_PARM2);
3711 strindex = b->num_strings;
3714 strindex = BufStr_FindFreeString(b);
3717 Con_Printf("VM_bufstr_add: buffer %i has no free string slots in %s\n", bufindex, PRVM_NAME);
3722 while(b->num_strings <= strindex)
3724 if(b->num_strings == MAX_QCSTR_STRINGS)
3726 Con_Printf("VM_bufstr_add: buffer %i has no free string slots in %s\n", bufindex, PRVM_NAME);
3729 b->strings[b->num_strings] = NULL;
3732 if(b->strings[strindex])
3733 free(b->strings[strindex]);
3734 b->strings[strindex] = malloc(strlen(string)+1);
3735 strcpy(b->strings[strindex], string);
3736 PRVM_G_FLOAT(OFS_RETURN) = strindex;
3740 ========================
3742 delete string from buffer
3743 void bufstr_free(float bufhandle, float string_index) = #468;
3744 ========================
3746 void VM_bufstr_free (void)
3750 VM_SAFEPARMCOUNT(2, VM_bufstr_free);
3752 b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3755 Con_Printf("VM_bufstr_free: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3758 i = PRVM_G_FLOAT(OFS_PARM1);
3759 if(i < 0 || i > MAX_QCSTR_STRINGS)
3761 Con_Printf("VM_bufstr_free: invalid string index %i used in %s\n", i, PRVM_NAME);
3765 free(b->strings[i]);
3766 b->strings[i] = NULL;
3767 if(i+1 == b->num_strings)
3773 void VM_Cmd_Init(void)
3775 // only init the stuff for the current prog
3778 // VM_BufStr_Init();
3779 if(vm_polygons_initialized)
3781 Mem_FreePool(&vm_polygons_pool);
3782 vm_polygons_initialized = false;
3786 void VM_Cmd_Reset(void)
3788 CL_PurgeOwner( MENUOWNER );
3790 VM_Files_CloseAll();
3791 // VM_BufStr_ShutDown();
3792 if(vm_polygons_initialized)
3794 Mem_FreePool(&vm_polygons_pool);
3795 vm_polygons_initialized = false;