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 = 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 = 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 ) {
753 Con_DPrint( "VM_remove: tried to remove the null entity or a reserved entity!\n" );
754 } else if( ed->priv.required->free ) {
755 Con_DPrint( "VM_remove: tried to remove an already freed entity!\n" );
759 // if (ed == prog->edicts)
760 // PRVM_ERROR ("remove: tried to remove world");
761 // if (PRVM_NUM_FOR_EDICT(ed) <= sv.maxclients)
762 // Host_Error("remove: tried to remove a client");
769 entity find(entity start, .string field, string match)
780 VM_SAFEPARMCOUNT(3,VM_find);
782 e = PRVM_G_EDICTNUM(OFS_PARM0);
783 f = PRVM_G_INT(OFS_PARM1);
784 s = PRVM_G_STRING(OFS_PARM2);
788 // return reserved edict 0 (could be used for whatever the prog wants)
789 VM_RETURN_EDICT(prog->edicts);
793 for (e++ ; e < prog->num_edicts ; e++)
795 prog->xfunction->builtinsprofile++;
796 ed = PRVM_EDICT_NUM(e);
797 if (ed->priv.required->free)
799 t = PRVM_E_STRING(ed,f);
809 VM_RETURN_EDICT(prog->edicts);
816 entity findfloat(entity start, .float field, float match)
817 entity findentity(entity start, .entity field, entity match)
820 // LordHavoc: added this for searching float, int, and entity reference fields
821 void VM_findfloat (void)
828 VM_SAFEPARMCOUNT(3,VM_findfloat);
830 e = PRVM_G_EDICTNUM(OFS_PARM0);
831 f = PRVM_G_INT(OFS_PARM1);
832 s = PRVM_G_FLOAT(OFS_PARM2);
834 for (e++ ; e < prog->num_edicts ; e++)
836 prog->xfunction->builtinsprofile++;
837 ed = PRVM_EDICT_NUM(e);
838 if (ed->priv.required->free)
840 if (PRVM_E_FLOAT(ed,f) == s)
847 VM_RETURN_EDICT(prog->edicts);
854 entity findchain(.string field, string match)
857 // chained search for strings in entity fields
858 // entity(.string field, string match) findchain = #402;
859 void VM_findchain (void)
865 prvm_edict_t *ent, *chain;
867 VM_SAFEPARMCOUNT(2,VM_findchain);
869 // is the same like !(prog->flag & PRVM_FE_CHAIN) - even if the operator precedence is another
870 if(!prog->flag & PRVM_FE_CHAIN)
871 PRVM_ERROR("VM_findchain: %s doesnt have a chain field !", PRVM_NAME);
873 chain_of = PRVM_ED_FindField("chain")->ofs;
875 chain = prog->edicts;
877 f = PRVM_G_INT(OFS_PARM0);
878 s = PRVM_G_STRING(OFS_PARM1);
881 VM_RETURN_EDICT(prog->edicts);
885 ent = PRVM_NEXT_EDICT(prog->edicts);
886 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
888 prog->xfunction->builtinsprofile++;
889 if (ent->priv.required->free)
891 t = PRVM_E_STRING(ent,f);
897 PRVM_E_INT(ent,chain_of) = PRVM_NUM_FOR_EDICT(chain);
901 VM_RETURN_EDICT(chain);
908 entity findchainfloat(.string field, float match)
909 entity findchainentity(.string field, entity match)
912 // LordHavoc: chained search for float, int, and entity reference fields
913 // entity(.string field, float match) findchainfloat = #403;
914 void VM_findchainfloat (void)
920 prvm_edict_t *ent, *chain;
922 VM_SAFEPARMCOUNT(2, VM_findchainfloat);
924 if(!prog->flag & PRVM_FE_CHAIN)
925 PRVM_ERROR("VM_findchainfloat: %s doesnt have a chain field !", PRVM_NAME);
927 chain_of = PRVM_ED_FindField("chain")->ofs;
929 chain = (prvm_edict_t *)prog->edicts;
931 f = PRVM_G_INT(OFS_PARM0);
932 s = PRVM_G_FLOAT(OFS_PARM1);
934 ent = PRVM_NEXT_EDICT(prog->edicts);
935 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
937 prog->xfunction->builtinsprofile++;
938 if (ent->priv.required->free)
940 if (PRVM_E_FLOAT(ent,f) != s)
943 PRVM_E_INT(ent,chain_of) = PRVM_EDICT_TO_PROG(chain);
947 VM_RETURN_EDICT(chain);
951 ========================
954 entity findflags(entity start, .float field, float match)
955 ========================
957 // LordHavoc: search for flags in float fields
958 void VM_findflags (void)
965 VM_SAFEPARMCOUNT(3, VM_findflags);
968 e = PRVM_G_EDICTNUM(OFS_PARM0);
969 f = PRVM_G_INT(OFS_PARM1);
970 s = (int)PRVM_G_FLOAT(OFS_PARM2);
972 for (e++ ; e < prog->num_edicts ; e++)
974 prog->xfunction->builtinsprofile++;
975 ed = PRVM_EDICT_NUM(e);
976 if (ed->priv.required->free)
978 if ((int)PRVM_E_FLOAT(ed,f) & s)
985 VM_RETURN_EDICT(prog->edicts);
989 ========================
992 entity findchainflags(.float field, float match)
993 ========================
995 // LordHavoc: chained search for flags in float fields
996 void VM_findchainflags (void)
1002 prvm_edict_t *ent, *chain;
1004 VM_SAFEPARMCOUNT(2, VM_findchainflags);
1006 if(!prog->flag & PRVM_FE_CHAIN)
1007 PRVM_ERROR("VM_findchainflags: %s doesnt have a chain field !", PRVM_NAME);
1009 chain_of = PRVM_ED_FindField("chain")->ofs;
1011 chain = (prvm_edict_t *)prog->edicts;
1013 f = PRVM_G_INT(OFS_PARM0);
1014 s = (int)PRVM_G_FLOAT(OFS_PARM1);
1016 ent = PRVM_NEXT_EDICT(prog->edicts);
1017 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
1019 prog->xfunction->builtinsprofile++;
1020 if (ent->priv.required->free)
1022 if (!((int)PRVM_E_FLOAT(ent,f) & s))
1025 PRVM_E_INT(ent,chain_of) = PRVM_EDICT_TO_PROG(chain);
1029 VM_RETURN_EDICT(chain);
1039 void VM_coredump (void)
1041 VM_SAFEPARMCOUNT(0,VM_coredump);
1043 Cbuf_AddText("prvm_edicts ");
1044 Cbuf_AddText(PRVM_NAME);
1055 void PRVM_StackTrace(void);
1056 void VM_stackdump (void)
1058 VM_SAFEPARMCOUNT(0, VM_stackdump);
1073 VM_SAFEPARMCOUNT(0, VM_crash);
1075 PRVM_ERROR("Crash called by %s",PRVM_NAME);
1085 void VM_traceon (void)
1087 VM_SAFEPARMCOUNT(0,VM_traceon);
1099 void VM_traceoff (void)
1101 VM_SAFEPARMCOUNT(0,VM_traceoff);
1103 prog->trace = false;
1113 void VM_eprint (void)
1115 VM_SAFEPARMCOUNT(1,VM_eprint);
1117 PRVM_ED_PrintNum (PRVM_G_EDICTNUM(OFS_PARM0));
1131 VM_SAFEPARMCOUNT(1,VM_rint);
1133 f = PRVM_G_FLOAT(OFS_PARM0);
1135 PRVM_G_FLOAT(OFS_RETURN) = (int)(f + 0.5);
1137 PRVM_G_FLOAT(OFS_RETURN) = (int)(f - 0.5);
1147 void VM_floor (void)
1149 VM_SAFEPARMCOUNT(1,VM_floor);
1151 PRVM_G_FLOAT(OFS_RETURN) = floor(PRVM_G_FLOAT(OFS_PARM0));
1163 VM_SAFEPARMCOUNT(1,VM_ceil);
1165 PRVM_G_FLOAT(OFS_RETURN) = ceil(PRVM_G_FLOAT(OFS_PARM0));
1173 entity nextent(entity)
1176 void VM_nextent (void)
1181 i = PRVM_G_EDICTNUM(OFS_PARM0);
1184 prog->xfunction->builtinsprofile++;
1186 if (i == prog->num_edicts)
1188 VM_RETURN_EDICT(prog->edicts);
1191 ent = PRVM_EDICT_NUM(i);
1192 if (!ent->priv.required->free)
1194 VM_RETURN_EDICT(ent);
1200 //=============================================================================
1207 changelevel(string map)
1210 void VM_changelevel (void)
1214 VM_SAFEPARMCOUNT(1, VM_changelevel);
1218 Con_Printf("VM_changelevel: game is not server (%s)\n", PRVM_NAME);
1222 // make sure we don't issue two changelevels
1223 if (svs.changelevel_issued)
1225 svs.changelevel_issued = true;
1227 s = PRVM_G_STRING(OFS_PARM0);
1228 Cbuf_AddText (va("changelevel %s\n",s));
1240 VM_SAFEPARMCOUNT(1,VM_sin);
1241 PRVM_G_FLOAT(OFS_RETURN) = sin(PRVM_G_FLOAT(OFS_PARM0));
1252 VM_SAFEPARMCOUNT(1,VM_cos);
1253 PRVM_G_FLOAT(OFS_RETURN) = cos(PRVM_G_FLOAT(OFS_PARM0));
1265 VM_SAFEPARMCOUNT(1,VM_sqrt);
1266 PRVM_G_FLOAT(OFS_RETURN) = sqrt(PRVM_G_FLOAT(OFS_PARM0));
1273 Returns a vector of length < 1 and > 0
1278 void VM_randomvec (void)
1283 VM_SAFEPARMCOUNT(0, VM_randomvec);
1288 temp[0] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1289 temp[1] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1290 temp[2] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1292 while (DotProduct(temp, temp) >= 1);
1293 VectorCopy (temp, PRVM_G_VECTOR(OFS_RETURN));
1296 temp[0] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1297 temp[1] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1298 temp[2] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1299 // length returned always > 0
1300 length = (rand()&32766 + 1) * (1.0 / 32767.0) / VectorLength(temp);
1301 VectorScale(temp,length, temp);*/
1302 //VectorCopy(temp, PRVM_G_VECTOR(OFS_RETURN));
1305 //=============================================================================
1311 float registercvar (string name, string value, float flags)
1314 void VM_registercvar (void)
1316 const char *name, *value;
1319 VM_SAFEPARMCOUNT(3,VM_registercvar);
1321 name = PRVM_G_STRING(OFS_PARM0);
1322 value = PRVM_G_STRING(OFS_PARM1);
1323 flags = PRVM_G_FLOAT(OFS_PARM2);
1324 PRVM_G_FLOAT(OFS_RETURN) = 0;
1326 if(flags > CVAR_MAXFLAGSVAL)
1329 // first check to see if it has already been defined
1330 if (Cvar_FindVar (name))
1333 // check for overlap with a command
1334 if (Cmd_Exists (name))
1336 Con_Printf("VM_registercvar: %s is a command\n", name);
1340 Cvar_Get(name, value, flags);
1342 PRVM_G_FLOAT(OFS_RETURN) = 1; // success
1349 returns the minimum of two supplied floats
1351 float min(float a, float b, ...[float])
1356 // LordHavoc: 3+ argument enhancement suggested by FrikaC
1357 if (prog->argc == 2)
1358 PRVM_G_FLOAT(OFS_RETURN) = min(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1359 else if (prog->argc >= 3)
1362 float f = PRVM_G_FLOAT(OFS_PARM0);
1363 for (i = 1;i < prog->argc;i++)
1364 if (PRVM_G_FLOAT((OFS_PARM0+i*3)) < f)
1365 f = PRVM_G_FLOAT((OFS_PARM0+i*3));
1366 PRVM_G_FLOAT(OFS_RETURN) = f;
1369 PRVM_ERROR("VM_min: %s must supply at least 2 floats", PRVM_NAME);
1376 returns the maximum of two supplied floats
1378 float max(float a, float b, ...[float])
1383 // LordHavoc: 3+ argument enhancement suggested by FrikaC
1384 if (prog->argc == 2)
1385 PRVM_G_FLOAT(OFS_RETURN) = max(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1386 else if (prog->argc >= 3)
1389 float f = PRVM_G_FLOAT(OFS_PARM0);
1390 for (i = 1;i < prog->argc;i++)
1391 if (PRVM_G_FLOAT((OFS_PARM0+i*3)) > f)
1392 f = PRVM_G_FLOAT((OFS_PARM0+i*3));
1393 PRVM_G_FLOAT(OFS_RETURN) = f;
1396 PRVM_ERROR("VM_max: %s must supply at least 2 floats", PRVM_NAME);
1403 returns number bounded by supplied range
1405 float bound(float min, float value, float max)
1408 void VM_bound (void)
1410 VM_SAFEPARMCOUNT(3,VM_bound);
1411 PRVM_G_FLOAT(OFS_RETURN) = bound(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1), PRVM_G_FLOAT(OFS_PARM2));
1418 returns a raised to power b
1420 float pow(float a, float b)
1425 VM_SAFEPARMCOUNT(2,VM_pow);
1426 PRVM_G_FLOAT(OFS_RETURN) = pow(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1433 copies data from one entity to another
1435 copyentity(entity src, entity dst)
1438 void VM_copyentity (void)
1440 prvm_edict_t *in, *out;
1441 VM_SAFEPARMCOUNT(2,VM_copyentity);
1442 in = PRVM_G_EDICT(OFS_PARM0);
1443 out = PRVM_G_EDICT(OFS_PARM1);
1444 memcpy(out->fields.vp, in->fields.vp, prog->progs->entityfields * 4);
1451 sets the color of a client and broadcasts the update to all connected clients
1453 setcolor(clientent, value)
1456 /*void PF_setcolor (void)
1462 entnum = PRVM_G_EDICTNUM(OFS_PARM0);
1463 i = PRVM_G_FLOAT(OFS_PARM1);
1465 if (entnum < 1 || entnum > svs.maxclients || !svs.clients[entnum-1].active)
1467 Con_Print("tried to setcolor a non-client\n");
1471 client = svs.clients + entnum-1;
1472 if ((val = PRVM_GETEDICTFIELDVALUE(client->edict, eval_clientcolors)))
1475 client->old_colors = i;
1476 client->edict->fields.server->team = (i & 15) + 1;
1478 MSG_WriteByte (&sv.reliable_datagram, svc_updatecolors);
1479 MSG_WriteByte (&sv.reliable_datagram, entnum - 1);
1480 MSG_WriteByte (&sv.reliable_datagram, i);
1483 void VM_Files_Init(void)
1485 memset(VM_FILES, 0, sizeof(qfile_t*[MAX_VMFILES]));
1488 void VM_Files_CloseAll(void)
1491 for (i = 0;i < MAX_VMFILES;i++)
1494 FS_Close(VM_FILES[i]);
1495 //VM_FILES[i] = NULL;
1497 memset(VM_FILES,0,sizeof(qfile_t*[MAX_VMFILES])); // this should be faster (is it ?)
1500 qfile_t *VM_GetFileHandle( int index )
1502 if (index < 0 || index >= MAX_VMFILES)
1504 Con_Printf("VM_GetFileHandle: invalid file handle %i used in %s\n", index, PRVM_NAME);
1507 if (VM_FILES[index] == NULL)
1509 Con_Printf("VM_GetFileHandle: no such file handle %i (or file has been closed) in %s\n", index, PRVM_NAME);
1512 return VM_FILES[index];
1519 float fopen(string filename, float mode)
1522 // float(string filename, float mode) fopen = #110;
1523 // opens a file inside quake/gamedir/data/ (mode is FILE_READ, FILE_APPEND, or FILE_WRITE),
1524 // returns fhandle >= 0 if successful, or fhandle < 0 if unable to open file for any reason
1528 const char *modestring, *filename;
1530 VM_SAFEPARMCOUNT(2,VM_fopen);
1532 for (filenum = 0;filenum < MAX_VMFILES;filenum++)
1533 if (VM_FILES[filenum] == NULL)
1535 if (filenum >= MAX_VMFILES)
1537 Con_Printf("VM_fopen: %s ran out of file handles (%i)\n", PRVM_NAME, MAX_VMFILES);
1538 PRVM_G_FLOAT(OFS_RETURN) = -2;
1541 mode = PRVM_G_FLOAT(OFS_PARM1);
1544 case 0: // FILE_READ
1547 case 1: // FILE_APPEND
1550 case 2: // FILE_WRITE
1554 Con_Printf("VM_fopen: %s: no such mode %i (valid: 0 = read, 1 = append, 2 = write)\n", PRVM_NAME, mode);
1555 PRVM_G_FLOAT(OFS_RETURN) = -3;
1558 filename = PRVM_G_STRING(OFS_PARM0);
1560 VM_FILES[filenum] = FS_Open(va("data/%s", filename), modestring, false, false);
1561 if (VM_FILES[filenum] == NULL && mode == 0)
1562 VM_FILES[filenum] = FS_Open(va("%s", filename), modestring, false, false);
1564 if (VM_FILES[filenum] == NULL)
1566 if (developer.integer)
1567 Con_Printf("VM_fopen: %s: %s mode %s failed\n", PRVM_NAME, filename, modestring);
1568 PRVM_G_FLOAT(OFS_RETURN) = -1;
1572 if (developer.integer)
1573 Con_Printf("VM_fopen: %s: %s mode %s opened as #%i\n", PRVM_NAME, filename, modestring, filenum);
1574 PRVM_G_FLOAT(OFS_RETURN) = filenum;
1582 fclose(float fhandle)
1585 //void(float fhandle) fclose = #111; // closes a file
1586 void VM_fclose(void)
1590 VM_SAFEPARMCOUNT(1,VM_fclose);
1592 filenum = PRVM_G_FLOAT(OFS_PARM0);
1593 if (filenum < 0 || filenum >= MAX_VMFILES)
1595 Con_Printf("VM_fclose: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1598 if (VM_FILES[filenum] == NULL)
1600 Con_Printf("VM_fclose: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1603 if (developer.integer)
1604 Con_Printf("VM_fclose: %s: #%i closed\n", PRVM_NAME, filenum);
1605 FS_Close(VM_FILES[filenum]);
1606 VM_FILES[filenum] = NULL;
1613 string fgets(float fhandle)
1616 //string(float fhandle) fgets = #112; // reads a line of text from the file and returns as a tempstring
1620 static char string[VM_STRINGTEMP_LENGTH];
1623 VM_SAFEPARMCOUNT(1,VM_fgets);
1625 filenum = PRVM_G_FLOAT(OFS_PARM0);
1626 if (filenum < 0 || filenum >= MAX_VMFILES)
1628 Con_Printf("VM_fgets: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1631 if (VM_FILES[filenum] == NULL)
1633 Con_Printf("VM_fgets: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1639 c = FS_Getc(VM_FILES[filenum]);
1640 if (c == '\r' || c == '\n' || c < 0)
1642 if (end < VM_STRINGTEMP_LENGTH - 1)
1646 // remove \n following \r
1649 c = FS_Getc(VM_FILES[filenum]);
1651 FS_UnGetc(VM_FILES[filenum], (unsigned char)c);
1653 if (developer.integer >= 3)
1654 Con_Printf("fgets: %s: %s\n", PRVM_NAME, string);
1656 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(string);
1658 PRVM_G_INT(OFS_RETURN) = 0;
1665 fputs(float fhandle, string s)
1668 //void(float fhandle, string s) fputs = #113; // writes a line of text to the end of the file
1672 char string[VM_STRINGTEMP_LENGTH];
1675 VM_SAFEPARMCOUNT(2,VM_fputs);
1677 filenum = PRVM_G_FLOAT(OFS_PARM0);
1678 if (filenum < 0 || filenum >= MAX_VMFILES)
1680 Con_Printf("VM_fputs: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1683 if (VM_FILES[filenum] == NULL)
1685 Con_Printf("VM_fputs: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1688 VM_VarString(1, string, sizeof(string));
1689 if ((stringlength = (int)strlen(string)))
1690 FS_Write(VM_FILES[filenum], string, stringlength);
1691 if (developer.integer)
1692 Con_Printf("fputs: %s: %s\n", PRVM_NAME, string);
1699 float strlen(string s)
1702 //float(string s) strlen = #114; // returns how many characters are in a string
1703 void VM_strlen(void)
1707 VM_SAFEPARMCOUNT(1,VM_strlen);
1709 s = PRVM_G_STRING(OFS_PARM0);
1711 PRVM_G_FLOAT(OFS_RETURN) = strlen(s);
1713 PRVM_G_FLOAT(OFS_RETURN) = 0;
1720 string strcat(string,string,...[string])
1723 //string(string s1, string s2) strcat = #115;
1724 // concatenates two strings (for example "abc", "def" would return "abcdef")
1725 // and returns as a tempstring
1726 void VM_strcat(void)
1731 PRVM_ERROR("VM_strcat wrong parameter count (min. 1 expected ) !");
1733 s = VM_GetTempString();
1734 VM_VarString(0, s, VM_STRINGTEMP_LENGTH);
1735 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(s);
1742 string substring(string s, float start, float length)
1745 // string(string s, float start, float length) substring = #116;
1746 // returns a section of a string as a tempstring
1747 void VM_substring(void)
1749 int i, start, length;
1753 VM_SAFEPARMCOUNT(3,VM_substring);
1755 string = VM_GetTempString();
1756 s = PRVM_G_STRING(OFS_PARM0);
1757 start = PRVM_G_FLOAT(OFS_PARM1);
1758 length = PRVM_G_FLOAT(OFS_PARM2);
1761 for (i = 0;i < start && *s;i++, s++);
1762 for (i = 0;i < VM_STRINGTEMP_LENGTH - 1 && *s && i < length;i++, s++)
1765 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(string);
1772 vector stov(string s)
1775 //vector(string s) stov = #117; // returns vector value from a string
1778 char string[VM_STRINGTEMP_LENGTH];
1780 VM_SAFEPARMCOUNT(1,VM_stov);
1782 VM_VarString(0, string, sizeof(string));
1783 Math_atov(string, PRVM_G_VECTOR(OFS_RETURN));
1790 string strzone(string s)
1793 //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)
1794 void VM_strzone(void)
1797 char string[VM_STRINGTEMP_LENGTH];
1799 VM_SAFEPARMCOUNT(1,VM_strzone);
1801 VM_VarString(0, string, sizeof(string));
1802 PRVM_G_INT(OFS_RETURN) = PRVM_AllocString(strlen(string) + 1, &out);
1803 strcpy(out, string);
1813 //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!!!)
1814 void VM_strunzone(void)
1816 VM_SAFEPARMCOUNT(1,VM_strunzone);
1817 PRVM_FreeString(PRVM_G_INT(OFS_PARM0));
1822 VM_command (used by client and menu)
1824 clientcommand(float client, string s) (for client and menu)
1827 //void(entity e, string s) clientcommand = #440; // executes a command string as if it came from the specified client
1828 //this function originally written by KrimZon, made shorter by LordHavoc
1829 void VM_clcommand (void)
1831 client_t *temp_client;
1834 VM_SAFEPARMCOUNT(2,VM_clcommand);
1836 i = PRVM_G_FLOAT(OFS_PARM0);
1837 if (!sv.active || i < 0 || i >= svs.maxclients || !svs.clients[i].active)
1839 Con_Printf("VM_clientcommand: %s: invalid client/server is not active !\n", PRVM_NAME);
1843 temp_client = host_client;
1844 host_client = svs.clients + i;
1845 Cmd_ExecuteString (PRVM_G_STRING(OFS_PARM1), src_client);
1846 host_client = temp_client;
1854 float tokenize(string s)
1857 //float(string s) tokenize = #441; // takes apart a string into individal words (access them with argv), returns how many
1858 //this function originally written by KrimZon, made shorter by LordHavoc
1859 //20040203: rewritten by LordHavoc (no longer uses allocations)
1861 char *tokens[256], tokenbuf[MAX_INPUTLINE];
1862 void VM_tokenize (void)
1867 VM_SAFEPARMCOUNT(1,VM_tokenize);
1869 p = PRVM_G_STRING(OFS_PARM0);
1873 while(COM_ParseToken(&p, false))
1875 if (num_tokens >= (int)(sizeof(tokens)/sizeof(tokens[0])))
1877 if (pos + strlen(com_token) + 1 > sizeof(tokenbuf))
1879 tokens[num_tokens++] = tokenbuf + pos;
1880 strcpy(tokenbuf + pos, com_token);
1881 pos += strlen(com_token) + 1;
1884 PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
1887 //string(float n) argv = #442; // returns a word from the tokenized string (returns nothing for an invalid index)
1888 //this function originally written by KrimZon, made shorter by LordHavoc
1893 VM_SAFEPARMCOUNT(1,VM_argv);
1895 token_num = PRVM_G_FLOAT(OFS_PARM0);
1897 if (token_num >= 0 && token_num < num_tokens)
1898 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(tokens[token_num]);
1900 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(NULL);
1904 //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)
1905 void PF_setattachment (void)
1907 prvm_edict_t *e = PRVM_G_EDICT(OFS_PARM0);
1908 prvm_edict_t *tagentity = PRVM_G_EDICT(OFS_PARM1);
1909 char *tagname = PRVM_G_STRING(OFS_PARM2);
1914 if (tagentity == NULL)
1915 tagentity = prog->edicts;
1917 v = PRVM_GETEDICTFIELDVALUE(e, eval_tag_entity);
1919 fields.server->edict = PRVM_EDICT_TO_PROG(tagentity);
1921 v = PRVM_GETEDICTFIELDVALUE(e, eval_tag_index);
1923 fields.server->_float = 0;
1924 if (tagentity != NULL && tagentity != prog->edicts && tagname && tagname[0])
1926 modelindex = (int)tagentity->fields.server->modelindex;
1927 if (modelindex >= 0 && modelindex < MAX_MODELS)
1929 model = sv.models[modelindex];
1930 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)
1931 for (i = 0;i < model->data_overridetagnamesforskin[(unsigned int)tagentity->fields.server->skin].num_overridetagnames;i++)
1932 if (!strcmp(tagname, model->data_overridetagnamesforskin[(unsigned int)tagentity->fields.server->skin].data_overridetagnames[i].name))
1933 fields.server->_float = i + 1;
1934 // FIXME: use a model function to get tag info (need to handle skeletal)
1935 if (fields.server->_float == 0 && model->num_tags)
1936 for (i = 0;i < model->num_tags;i++)
1937 if (!strcmp(tagname, model->data_tags[i].name))
1938 fields.server->_float = i + 1;
1939 if (fields.server->_float == 0)
1940 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);
1943 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));
1954 void VM_isserver(void)
1956 VM_SAFEPARMCOUNT(0,VM_serverstate);
1958 PRVM_G_FLOAT(OFS_RETURN) = sv.active;
1968 void VM_clientcount(void)
1970 VM_SAFEPARMCOUNT(0,VM_clientcount);
1972 PRVM_G_FLOAT(OFS_RETURN) = svs.maxclients;
1982 void VM_clientstate(void)
1984 VM_SAFEPARMCOUNT(0,VM_clientstate);
1986 PRVM_G_FLOAT(OFS_RETURN) = cls.state;
1993 float getostype(void)
1995 */ // not used at the moment -> not included in the common list
1996 void VM_getostype(void)
1998 VM_SAFEPARMCOUNT(0,VM_getostype);
2003 OS_MAC - not supported
2007 PRVM_G_FLOAT(OFS_RETURN) = 0;
2009 PRVM_G_FLOAT(OFS_RETURN) = 2;
2011 PRVM_G_FLOAT(OFS_RETURN) = 1;
2019 vector getmousepos()
2022 void VM_getmousepos(void)
2025 VM_SAFEPARMCOUNT(0,VM_getmousepos);
2027 PRVM_G_VECTOR(OFS_RETURN)[0] = in_mouse_x * vid_conwidth.integer / vid.width;
2028 PRVM_G_VECTOR(OFS_RETURN)[1] = in_mouse_y * vid_conheight.integer / vid.height;
2029 PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
2039 void VM_gettime(void)
2041 VM_SAFEPARMCOUNT(0,VM_gettime);
2043 PRVM_G_FLOAT(OFS_RETURN) = (float) *prog->time;
2050 loadfromdata(string data)
2053 void VM_loadfromdata(void)
2055 VM_SAFEPARMCOUNT(1,VM_loadentsfromfile);
2057 PRVM_ED_LoadFromFile(PRVM_G_STRING(OFS_PARM0));
2061 ========================
2064 parseentitydata(entity ent, string data)
2065 ========================
2067 void VM_parseentitydata(void)
2072 VM_SAFEPARMCOUNT(2, VM_parseentitydata);
2074 // get edict and test it
2075 ent = PRVM_G_EDICT(OFS_PARM0);
2076 if (ent->priv.required->free)
2077 PRVM_ERROR ("VM_parseentitydata: %s: Can only set already spawned entities (entity %i is free)!", PRVM_NAME, PRVM_NUM_FOR_EDICT(ent));
2079 data = PRVM_G_STRING(OFS_PARM1);
2081 // parse the opening brace
2082 if (!COM_ParseToken(&data, false) || com_token[0] != '{' )
2083 PRVM_ERROR ("VM_parseentitydata: %s: Couldn't parse entity data:\n%s", PRVM_NAME, data );
2085 PRVM_ED_ParseEdict (data, ent);
2092 loadfromfile(string file)
2095 void VM_loadfromfile(void)
2097 const char *filename;
2100 VM_SAFEPARMCOUNT(1,VM_loadfromfile);
2102 filename = PRVM_G_STRING(OFS_PARM0);
2103 // .. is parent directory on many platforms
2104 // / is parent directory on Amiga
2105 // : is root of drive on Amiga (also used as a directory separator on Mac, but / works there too, so that's a bad idea)
2106 // \ is a windows-ism (so it's naughty to use it, / works on all platforms)
2107 if ((filename[0] == '.' && filename[1] == '.') || filename[0] == '/' || strrchr(filename, ':') || strrchr(filename, '\\'))
2109 Con_Printf("VM_loadfromfile: %s dangerous or non-portable filename \"%s\" not allowed. (contains : or \\ or begins with .. or /)\n", PRVM_NAME, filename);
2110 PRVM_G_FLOAT(OFS_RETURN) = -4;
2114 // not conform with VM_fopen
2115 data = (char *)FS_LoadFile(filename, tempmempool, false, NULL);
2117 PRVM_G_FLOAT(OFS_RETURN) = -1;
2119 PRVM_ED_LoadFromFile(data);
2130 float mod(float val, float m)
2133 void VM_modulo(void)
2136 VM_SAFEPARMCOUNT(2,VM_module);
2138 val = (int) PRVM_G_FLOAT(OFS_PARM0);
2139 m = (int) PRVM_G_FLOAT(OFS_PARM1);
2141 PRVM_G_FLOAT(OFS_RETURN) = (float) (val % m);
2144 void VM_Search_Init(void)
2146 memset(VM_SEARCHLIST,0,sizeof(fssearch_t*[MAX_VMSEARCHES]));
2149 void VM_Search_Reset(void)
2152 // reset the fssearch list
2153 for(i = 0; i < MAX_VMSEARCHES; i++)
2154 if(VM_SEARCHLIST[i])
2155 FS_FreeSearch(VM_SEARCHLIST[i]);
2156 memset(VM_SEARCHLIST,0,sizeof(fssearch_t*[MAX_VMSEARCHES]));
2163 float search_begin(string pattern, float caseinsensitive, float quiet)
2166 void VM_search_begin(void)
2169 const char *pattern;
2170 int caseinsens, quiet;
2172 VM_SAFEPARMCOUNT(3, VM_search_begin);
2174 pattern = PRVM_G_STRING(OFS_PARM0);
2176 VM_CheckEmptyString(pattern);
2178 caseinsens = PRVM_G_FLOAT(OFS_PARM1);
2179 quiet = PRVM_G_FLOAT(OFS_PARM2);
2181 for(handle = 0; handle < MAX_VMSEARCHES; handle++)
2182 if(!VM_SEARCHLIST[handle])
2185 if(handle >= MAX_VMSEARCHES)
2187 Con_Printf("VM_search_begin: %s ran out of search handles (%i)\n", PRVM_NAME, MAX_VMSEARCHES);
2188 PRVM_G_FLOAT(OFS_RETURN) = -2;
2192 if(!(VM_SEARCHLIST[handle] = FS_Search(pattern,caseinsens, quiet)))
2193 PRVM_G_FLOAT(OFS_RETURN) = -1;
2195 PRVM_G_FLOAT(OFS_RETURN) = handle;
2202 void search_end(float handle)
2205 void VM_search_end(void)
2208 VM_SAFEPARMCOUNT(1, VM_search_end);
2210 handle = PRVM_G_FLOAT(OFS_PARM0);
2212 if(handle < 0 || handle >= MAX_VMSEARCHES)
2214 Con_Printf("VM_search_end: invalid handle %i used in %s\n", handle, PRVM_NAME);
2217 if(VM_SEARCHLIST[handle] == NULL)
2219 Con_Printf("VM_search_end: no such handle %i in %s\n", handle, PRVM_NAME);
2223 FS_FreeSearch(VM_SEARCHLIST[handle]);
2224 VM_SEARCHLIST[handle] = NULL;
2231 float search_getsize(float handle)
2234 void VM_search_getsize(void)
2237 VM_SAFEPARMCOUNT(1, VM_M_search_getsize);
2239 handle = PRVM_G_FLOAT(OFS_PARM0);
2241 if(handle < 0 || handle >= MAX_VMSEARCHES)
2243 Con_Printf("VM_search_getsize: invalid handle %i used in %s\n", handle, PRVM_NAME);
2246 if(VM_SEARCHLIST[handle] == NULL)
2248 Con_Printf("VM_search_getsize: no such handle %i in %s\n", handle, PRVM_NAME);
2252 PRVM_G_FLOAT(OFS_RETURN) = VM_SEARCHLIST[handle]->numfilenames;
2257 VM_search_getfilename
2259 string search_getfilename(float handle, float num)
2262 void VM_search_getfilename(void)
2264 int handle, filenum;
2266 VM_SAFEPARMCOUNT(2, VM_search_getfilename);
2268 handle = PRVM_G_FLOAT(OFS_PARM0);
2269 filenum = PRVM_G_FLOAT(OFS_PARM1);
2271 if(handle < 0 || handle >= MAX_VMSEARCHES)
2273 Con_Printf("VM_search_getfilename: invalid handle %i used in %s\n", handle, PRVM_NAME);
2276 if(VM_SEARCHLIST[handle] == NULL)
2278 Con_Printf("VM_search_getfilename: no such handle %i in %s\n", handle, PRVM_NAME);
2281 if(filenum < 0 || filenum >= VM_SEARCHLIST[handle]->numfilenames)
2283 Con_Printf("VM_search_getfilename: invalid filenum %i in %s\n", filenum, PRVM_NAME);
2287 tmp = VM_GetTempString();
2288 strcpy(tmp, VM_SEARCHLIST[handle]->filenames[filenum]);
2290 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(tmp);
2297 string chr(float ascii)
2303 VM_SAFEPARMCOUNT(1, VM_chr);
2305 tmp = VM_GetTempString();
2306 tmp[0] = (unsigned char) PRVM_G_FLOAT(OFS_PARM0);
2309 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(tmp);
2312 //=============================================================================
2313 // Draw builtins (client & menu)
2319 float iscachedpic(string pic)
2322 void VM_iscachedpic(void)
2324 VM_SAFEPARMCOUNT(1,VM_iscachedpic);
2326 // drawq hasnt such a function, thus always return true
2327 PRVM_G_FLOAT(OFS_RETURN) = false;
2334 string precache_pic(string pic)
2337 void VM_precache_pic(void)
2341 VM_SAFEPARMCOUNT(1, VM_precache_pic);
2343 s = PRVM_G_STRING(OFS_PARM0);
2344 PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
2347 PRVM_ERROR ("VM_precache_pic: %s: NULL", PRVM_NAME);
2349 VM_CheckEmptyString (s);
2351 // AK Draw_CachePic is supposed to always return a valid pointer
2352 if( Draw_CachePic(s, false)->tex == r_texture_notexture )
2353 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(NULL);
2363 void VM_freepic(void)
2367 VM_SAFEPARMCOUNT(1,VM_freepic);
2369 s = PRVM_G_STRING(OFS_PARM0);
2372 PRVM_ERROR ("VM_freepic: %s: NULL");
2374 VM_CheckEmptyString (s);
2383 float drawcharacter(vector position, float character, vector scale, vector rgb, float alpha, float flag)
2386 void VM_drawcharacter(void)
2388 float *pos,*scale,*rgb;
2391 VM_SAFEPARMCOUNT(6,VM_drawcharacter);
2393 character = (char) PRVM_G_FLOAT(OFS_PARM1);
2396 Con_Printf("VM_drawcharacter: %s passed null character !\n",PRVM_NAME);
2397 PRVM_G_FLOAT(OFS_RETURN) = -1;
2401 pos = PRVM_G_VECTOR(OFS_PARM0);
2402 scale = PRVM_G_VECTOR(OFS_PARM2);
2403 rgb = PRVM_G_VECTOR(OFS_PARM3);
2404 flag = (int)PRVM_G_FLOAT(OFS_PARM5);
2406 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2408 Con_Printf("VM_drawcharacter: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2409 PRVM_G_FLOAT(OFS_RETURN) = -2;
2413 if(pos[2] || scale[2])
2414 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")));
2416 if(!scale[0] || !scale[1])
2418 Con_Printf("VM_drawcharacter: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
2419 PRVM_G_FLOAT(OFS_RETURN) = -3;
2423 DrawQ_String (pos[0], pos[1], &character, 1, scale[0], scale[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM4), flag);
2424 PRVM_G_FLOAT(OFS_RETURN) = 1;
2431 float drawstring(vector position, string text, vector scale, vector rgb, float alpha, float flag)
2434 void VM_drawstring(void)
2436 float *pos,*scale,*rgb;
2439 VM_SAFEPARMCOUNT(6,VM_drawstring);
2441 string = PRVM_G_STRING(OFS_PARM1);
2444 Con_Printf("VM_drawstring: %s passed null string !\n",PRVM_NAME);
2445 PRVM_G_FLOAT(OFS_RETURN) = -1;
2449 //VM_CheckEmptyString(string); Why should it be checked - perhaps the menu wants to support the precolored letters, too?
2451 pos = PRVM_G_VECTOR(OFS_PARM0);
2452 scale = PRVM_G_VECTOR(OFS_PARM2);
2453 rgb = PRVM_G_VECTOR(OFS_PARM3);
2454 flag = (int)PRVM_G_FLOAT(OFS_PARM5);
2456 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2458 Con_Printf("VM_drawstring: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2459 PRVM_G_FLOAT(OFS_RETURN) = -2;
2463 if(!scale[0] || !scale[1])
2465 Con_Printf("VM_drawstring: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
2466 PRVM_G_FLOAT(OFS_RETURN) = -3;
2470 if(pos[2] || scale[2])
2471 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")));
2473 DrawQ_String (pos[0], pos[1], string, 0, scale[0], scale[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM4), flag);
2474 PRVM_G_FLOAT(OFS_RETURN) = 1;
2480 float drawpic(vector position, string pic, vector size, vector rgb, float alpha, float flag)
2483 void VM_drawpic(void)
2486 float *size, *pos, *rgb;
2489 VM_SAFEPARMCOUNT(6,VM_drawpic);
2491 pic = PRVM_G_STRING(OFS_PARM1);
2495 Con_Printf("VM_drawpic: %s passed null picture name !\n", PRVM_NAME);
2496 PRVM_G_FLOAT(OFS_RETURN) = -1;
2500 VM_CheckEmptyString (pic);
2502 // is pic cached ? no function yet for that
2505 Con_Printf("VM_drawpic: %s: %s not cached !\n", PRVM_NAME, pic);
2506 PRVM_G_FLOAT(OFS_RETURN) = -4;
2510 pos = PRVM_G_VECTOR(OFS_PARM0);
2511 size = PRVM_G_VECTOR(OFS_PARM2);
2512 rgb = PRVM_G_VECTOR(OFS_PARM3);
2513 flag = (int) PRVM_G_FLOAT(OFS_PARM5);
2515 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2517 Con_Printf("VM_drawstring: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2518 PRVM_G_FLOAT(OFS_RETURN) = -2;
2522 if(pos[2] || size[2])
2523 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")));
2525 DrawQ_Pic(pos[0], pos[1], pic, size[0], size[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM4), flag);
2526 PRVM_G_FLOAT(OFS_RETURN) = 1;
2533 float drawfill(vector position, vector size, vector rgb, float alpha, float flag)
2536 void VM_drawfill(void)
2538 float *size, *pos, *rgb;
2541 VM_SAFEPARMCOUNT(5,VM_drawfill);
2544 pos = PRVM_G_VECTOR(OFS_PARM0);
2545 size = PRVM_G_VECTOR(OFS_PARM1);
2546 rgb = PRVM_G_VECTOR(OFS_PARM2);
2547 flag = (int) PRVM_G_FLOAT(OFS_PARM4);
2549 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2551 Con_Printf("VM_drawstring: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2552 PRVM_G_FLOAT(OFS_RETURN) = -2;
2556 if(pos[2] || size[2])
2557 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")));
2559 DrawQ_Pic(pos[0], pos[1], 0, size[0], size[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM3), flag);
2560 PRVM_G_FLOAT(OFS_RETURN) = 1;
2567 drawsetcliparea(float x, float y, float width, float height)
2570 void VM_drawsetcliparea(void)
2573 VM_SAFEPARMCOUNT(4,VM_drawsetcliparea);
2575 x = bound(0, PRVM_G_FLOAT(OFS_PARM0), vid_conwidth.integer);
2576 y = bound(0, PRVM_G_FLOAT(OFS_PARM1), vid_conheight.integer);
2577 w = bound(0, PRVM_G_FLOAT(OFS_PARM2) + PRVM_G_FLOAT(OFS_PARM0) - x, (vid_conwidth.integer - x));
2578 h = bound(0, PRVM_G_FLOAT(OFS_PARM3) + PRVM_G_FLOAT(OFS_PARM1) - y, (vid_conheight.integer - y));
2580 DrawQ_SetClipArea(x, y, w, h);
2585 VM_drawresetcliparea
2590 void VM_drawresetcliparea(void)
2592 VM_SAFEPARMCOUNT(0,VM_drawresetcliparea);
2594 DrawQ_ResetClipArea();
2601 vector getimagesize(string pic)
2604 void VM_getimagesize(void)
2609 VM_SAFEPARMCOUNT(1,VM_getimagesize);
2611 p = PRVM_G_STRING(OFS_PARM0);
2614 PRVM_ERROR("VM_getimagepos: %s passed null picture name !", PRVM_NAME);
2616 VM_CheckEmptyString (p);
2618 pic = Draw_CachePic (p, false);
2620 PRVM_G_VECTOR(OFS_RETURN)[0] = pic->width;
2621 PRVM_G_VECTOR(OFS_RETURN)[1] = pic->height;
2622 PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
2629 string keynumtostring(float keynum)
2632 void VM_keynumtostring (void)
2636 VM_SAFEPARMCOUNT(1, VM_keynumtostring);
2638 keynum = PRVM_G_FLOAT(OFS_PARM0);
2640 tmp = VM_GetTempString();
2642 strcpy(tmp, Key_KeynumToString(keynum));
2644 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(tmp);
2651 float stringtokeynum(string key)
2654 void VM_stringtokeynum (void)
2657 VM_SAFEPARMCOUNT( 1, VM_keynumtostring );
2659 str = PRVM_G_STRING( OFS_PARM0 );
2661 PRVM_G_INT(OFS_RETURN) = Key_StringToKeynum( str );
2664 // CL_Video interface functions
2667 ========================
2670 float cin_open(string file, string name)
2671 ========================
2673 void VM_cin_open( void )
2678 VM_SAFEPARMCOUNT( 2, VM_cin_open );
2680 file = PRVM_G_STRING( OFS_PARM0 );
2681 name = PRVM_G_STRING( OFS_PARM1 );
2683 VM_CheckEmptyString( file );
2684 VM_CheckEmptyString( name );
2686 if( CL_OpenVideo( file, name, MENUOWNER ) )
2687 PRVM_G_FLOAT( OFS_RETURN ) = 1;
2689 PRVM_G_FLOAT( OFS_RETURN ) = 0;
2693 ========================
2696 void cin_close(string name)
2697 ========================
2699 void VM_cin_close( void )
2703 VM_SAFEPARMCOUNT( 1, VM_cin_close );
2705 name = PRVM_G_STRING( OFS_PARM0 );
2706 VM_CheckEmptyString( name );
2708 CL_CloseVideo( CL_GetVideo( name ) );
2712 ========================
2714 void cin_setstate(string name, float type)
2715 ========================
2717 void VM_cin_setstate( void )
2720 clvideostate_t state;
2723 VM_SAFEPARMCOUNT( 2, VM_cin_netstate );
2725 name = PRVM_G_STRING( OFS_PARM0 );
2726 VM_CheckEmptyString( name );
2728 state = (clvideostate_t)((int)PRVM_G_FLOAT( OFS_PARM1 ));
2730 video = CL_GetVideo( name );
2731 if( video && state > CLVIDEO_UNUSED && state < CLVIDEO_STATECOUNT )
2732 CL_SetVideoState( video, state );
2736 ========================
2739 float cin_getstate(string name)
2740 ========================
2742 void VM_cin_getstate( void )
2747 VM_SAFEPARMCOUNT( 1, VM_cin_getstate );
2749 name = PRVM_G_STRING( OFS_PARM0 );
2750 VM_CheckEmptyString( name );
2752 video = CL_GetVideo( name );
2754 PRVM_G_FLOAT( OFS_RETURN ) = (int)video->state;
2756 PRVM_G_FLOAT( OFS_RETURN ) = 0;
2760 ========================
2763 void cin_restart(string name)
2764 ========================
2766 void VM_cin_restart( void )
2771 VM_SAFEPARMCOUNT( 1, VM_cin_restart );
2773 name = PRVM_G_STRING( OFS_PARM0 );
2774 VM_CheckEmptyString( name );
2776 video = CL_GetVideo( name );
2778 CL_RestartVideo( video );
2785 Writes new values for v_forward, v_up, and v_right based on the given forward vector
2786 vectorvectors(vector, vector)
2789 void VM_vectorvectors (void)
2791 VectorNormalize2(PRVM_G_VECTOR(OFS_PARM0), prog->globals.server->v_forward);
2792 VectorVectors(prog->globals.server->v_forward, prog->globals.server->v_right, prog->globals.server->v_up);
2796 ========================
2799 void drawline(float width, vector pos1, vector pos2, vector rgb, float alpha, float flags)
2800 ========================
2802 void VM_drawline (void)
2804 float *c1, *c2, *rgb;
2806 unsigned char flags;
2808 VM_SAFEPARMCOUNT(6, VM_drawline);
2809 width = PRVM_G_FLOAT(OFS_PARM0);
2810 c1 = PRVM_G_VECTOR(OFS_PARM1);
2811 c2 = PRVM_G_VECTOR(OFS_PARM2);
2812 rgb = PRVM_G_VECTOR(OFS_PARM3);
2813 alpha = PRVM_G_FLOAT(OFS_PARM4);
2814 flags = PRVM_G_FLOAT(OFS_PARM5);
2815 DrawQ_Line(width, c1[0], c1[1], c2[0], c2[1], rgb[0], rgb[1], rgb[2], alpha, flags);
2818 //====================
2819 //QC POLYGON functions
2820 //====================
2825 float data[36]; //[515]: enough for polygons
2826 unsigned char flags; //[515]: + VM_POLYGON_2D and VM_POLYGON_FL4V flags
2829 //static float vm_polygon_linewidth = 1;
2830 static mempool_t *vm_polygons_pool = NULL;
2831 static unsigned char vm_current_vertices = 0;
2832 static qboolean vm_polygons_initialized = false;
2833 static vm_polygon_t *vm_polygons = NULL;
2834 static unsigned long vm_polygons_num = 0, vm_drawpolygons_num = 0; //[515]: ok long on 64bit ?
2835 static qboolean vm_polygonbegin = false; //[515]: for "no-crap-on-the-screen" check
2836 #define VM_DEFPOLYNUM 64 //[515]: enough for default ?
2838 #define VM_POLYGON_FL3V 16 //more than 2 vertices (used only for lines)
2839 #define VM_POLYGON_FLLINES 32
2840 #define VM_POLYGON_FL2D 64
2841 #define VM_POLYGON_FL4V 128 //4 vertices
2843 void VM_InitPolygons (void)
2845 vm_polygons_pool = Mem_AllocPool("VMPOLY", 0, NULL);
2846 vm_polygons = Mem_Alloc(vm_polygons_pool, VM_DEFPOLYNUM*sizeof(vm_polygon_t));
2847 memset(vm_polygons, 0, VM_DEFPOLYNUM*sizeof(vm_polygon_t));
2848 vm_polygons_num = VM_DEFPOLYNUM;
2849 vm_polygonbegin = vm_drawpolygons_num = 0;
2850 vm_polygons_initialized = true;
2853 void VM_DrawPolygonCallback (const entity_render_t *ent, int surfacenumber, const rtlight_t *rtlight)
2855 const vm_polygon_t *p = &vm_polygons[surfacenumber];
2856 int flags = p->flags & 0x0f;
2858 if(flags == DRAWFLAG_ADDITIVE)
2859 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
2860 else if(flags == DRAWFLAG_MODULATE)
2861 GL_BlendFunc(GL_DST_COLOR, GL_ZERO);
2862 else if(flags == DRAWFLAG_2XMODULATE)
2863 GL_BlendFunc(GL_DST_COLOR,GL_SRC_COLOR);
2865 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
2867 R_Mesh_TexBind(0, R_GetTexture(p->tex));
2869 //[515]: is speed is max ?
2870 if(p->flags & VM_POLYGON_FLLINES) //[515]: lines
2872 qglLineWidth(p->data[13]);
2873 qglBegin(GL_LINE_LOOP);
2874 qglTexCoord1f (p->data[12]);
2875 qglColor4f (p->data[20], p->data[21], p->data[22], p->data[23]);
2876 qglVertex3f (p->data[0] , p->data[1], p->data[2]);
2878 qglTexCoord1f (p->data[14]);
2879 qglColor4f (p->data[24], p->data[25], p->data[26], p->data[27]);
2880 qglVertex3f (p->data[3] , p->data[4], p->data[5]);
2882 if(p->flags & VM_POLYGON_FL3V)
2884 qglTexCoord1f (p->data[16]);
2885 qglColor4f (p->data[28], p->data[29], p->data[30], p->data[31]);
2886 qglVertex3f (p->data[6] , p->data[7], p->data[8]);
2888 if(p->flags & VM_POLYGON_FL4V)
2890 qglTexCoord1f (p->data[18]);
2891 qglColor4f (p->data[32], p->data[33], p->data[34], p->data[35]);
2892 qglVertex3f (p->data[9] , p->data[10], p->data[11]);
2899 qglBegin(GL_POLYGON);
2900 qglTexCoord2f (p->data[12], p->data[13]);
2901 qglColor4f (p->data[20], p->data[21], p->data[22], p->data[23]);
2902 qglVertex3f (p->data[0] , p->data[1], p->data[2]);
2904 qglTexCoord2f (p->data[14], p->data[15]);
2905 qglColor4f (p->data[24], p->data[25], p->data[26], p->data[27]);
2906 qglVertex3f (p->data[3] , p->data[4], p->data[5]);
2908 qglTexCoord2f (p->data[16], p->data[17]);
2909 qglColor4f (p->data[28], p->data[29], p->data[30], p->data[31]);
2910 qglVertex3f (p->data[6] , p->data[7], p->data[8]);
2912 if(p->flags & VM_POLYGON_FL4V)
2914 qglTexCoord2f (p->data[18], p->data[19]);
2915 qglColor4f (p->data[32], p->data[33], p->data[34], p->data[35]);
2916 qglVertex3f (p->data[9] , p->data[10], p->data[11]);
2922 void VM_AddPolygonTo2DScene (vm_polygon_t *p)
2924 drawqueuemesh_t mesh;
2925 static int picelements[6] = {0, 1, 2, 0, 2, 3};
2927 mesh.texture = p->tex;
2928 mesh.data_element3i = picelements;
2929 mesh.data_vertex3f = p->data;
2930 mesh.data_texcoord2f = p->data + 12;
2931 mesh.data_color4f = p->data + 20;
2932 if(p->flags & VM_POLYGON_FL4V)
2934 mesh.num_vertices = 4;
2935 mesh.num_triangles = 2;
2939 mesh.num_vertices = 3;
2940 mesh.num_triangles = 1;
2942 if(p->flags & VM_POLYGON_FLLINES) //[515]: lines
2943 DrawQ_LineLoop (&mesh, (p->flags&0x0f));
2945 DrawQ_Mesh (&mesh, (p->flags&0x0f));
2948 //void(string texturename, float flag, float 2d, float lines) R_BeginPolygon
2949 void VM_R_PolygonBegin (void)
2952 const char *picname;
2954 VM_SAFEPARMCOUNT(2, VM_R_PolygonBegin);
2956 if(!vm_polygons_initialized)
2960 Con_Printf("VM_R_PolygonBegin: called twice without VM_R_PolygonEnd after first\n");
2963 if(vm_drawpolygons_num >= vm_polygons_num)
2965 p = Mem_Alloc(vm_polygons_pool, 2 * vm_polygons_num * sizeof(vm_polygon_t));
2966 memset(p, 0, 2 * vm_polygons_num * sizeof(vm_polygon_t));
2967 memcpy(p, vm_polygons, vm_polygons_num * sizeof(vm_polygon_t));
2968 Mem_Free(vm_polygons);
2970 vm_polygons_num *= 2;
2972 p = &vm_polygons[vm_drawpolygons_num];
2973 picname = PRVM_G_STRING(OFS_PARM0);
2975 p->tex = Draw_CachePic(picname, false)->tex;
2977 p->tex = r_texture_notexture;
2978 p->flags = (unsigned char)PRVM_G_FLOAT(OFS_PARM1);
2979 vm_current_vertices = 0;
2980 vm_polygonbegin = true;
2983 if(PRVM_G_FLOAT(OFS_PARM2))
2984 p->flags |= VM_POLYGON_FL2D;
2985 if(prog->argc >= 4 && PRVM_G_FLOAT(OFS_PARM3))
2987 p->data[13] = PRVM_G_FLOAT(OFS_PARM3); //[515]: linewidth
2988 p->flags |= VM_POLYGON_FLLINES;
2993 //void(vector org, vector texcoords, vector rgb, float alpha) R_PolygonVertex
2994 void VM_R_PolygonVertex (void)
2996 float *coords, *tx, *rgb, alpha;
2998 VM_SAFEPARMCOUNT(4, VM_R_PolygonVertex);
3000 if(!vm_polygonbegin)
3002 Con_Printf("VM_R_PolygonVertex: VM_R_PolygonBegin wasn't called\n");
3005 coords = PRVM_G_VECTOR(OFS_PARM0);
3006 tx = PRVM_G_VECTOR(OFS_PARM1);
3007 rgb = PRVM_G_VECTOR(OFS_PARM2);
3008 alpha = PRVM_G_FLOAT(OFS_PARM3);
3010 p = &vm_polygons[vm_drawpolygons_num];
3011 if(vm_current_vertices > 4)
3013 Con_Printf("VM_R_PolygonVertex: may have 4 vertices max\n");
3017 p->data[vm_current_vertices*3] = coords[0];
3018 p->data[1+vm_current_vertices*3] = coords[1];
3019 if(!(p->flags & VM_POLYGON_FL2D))
3020 p->data[2+vm_current_vertices*3] = coords[2];
3022 p->data[12+vm_current_vertices*2] = tx[0];
3023 if(!(p->flags & VM_POLYGON_FLLINES))
3024 p->data[13+vm_current_vertices*2] = tx[1];
3026 p->data[20+vm_current_vertices*4] = rgb[0];
3027 p->data[21+vm_current_vertices*4] = rgb[1];
3028 p->data[22+vm_current_vertices*4] = rgb[2];
3029 p->data[23+vm_current_vertices*4] = alpha;
3031 vm_current_vertices++;
3032 if(vm_current_vertices == 4)
3033 p->flags |= VM_POLYGON_FL4V;
3035 if(vm_current_vertices == 3)
3036 p->flags |= VM_POLYGON_FL3V;
3039 //void() R_EndPolygon
3040 void VM_R_PolygonEnd (void)
3042 if(!vm_polygonbegin)
3044 Con_Printf("VM_R_PolygonEnd: VM_R_PolygonBegin wasn't called\n");
3047 if(vm_current_vertices > 2 || (vm_current_vertices >= 2 && vm_polygons[vm_drawpolygons_num].flags & VM_POLYGON_FLLINES))
3049 if(vm_polygons[vm_drawpolygons_num].flags & VM_POLYGON_FL2D) //[515]: don't use qcpolygons memory if 2D
3050 VM_AddPolygonTo2DScene(&vm_polygons[vm_drawpolygons_num]);
3052 vm_drawpolygons_num++;
3055 Con_Printf("VM_R_PolygonEnd: %i vertices isn't a good choice\n", vm_current_vertices);
3056 vm_polygonbegin = false;
3059 void VM_AddPolygonsToMeshQueue (void)
3062 if(!vm_drawpolygons_num)
3064 for(i = 0;i < vm_drawpolygons_num;i++)
3065 R_MeshQueue_Add(VM_DrawPolygonCallback, NULL, i, NULL);
3066 vm_drawpolygons_num = 0;
3072 // float(float number, float quantity) bitshift (EXT_BITSHIFT)
3073 void VM_bitshift (void)
3076 VM_SAFEPARMCOUNT(2, VM_bitshift);
3078 n1 = (int)fabs((int)PRVM_G_FLOAT(OFS_PARM0));
3079 n2 = (int)PRVM_G_FLOAT(OFS_PARM1);
3081 PRVM_G_FLOAT(OFS_RETURN) = n1;
3084 PRVM_G_FLOAT(OFS_RETURN) = (n1 >> -n2);
3086 PRVM_G_FLOAT(OFS_RETURN) = (n1 << n2);
3089 ////////////////////////////////////////
3090 // AltString functions
3091 ////////////////////////////////////////
3094 ========================
3097 float altstr_count(string)
3098 ========================
3100 void VM_altstr_count( void )
3102 const char *altstr, *pos;
3105 VM_SAFEPARMCOUNT( 1, VM_altstr_count );
3107 altstr = PRVM_G_STRING( OFS_PARM0 );
3108 //VM_CheckEmptyString( altstr );
3110 for( count = 0, pos = altstr ; *pos ; pos++ ) {
3111 if( *pos == '\\' ) {
3115 } else if( *pos == '\'' ) {
3120 PRVM_G_FLOAT( OFS_RETURN ) = (float) (count / 2);
3124 ========================
3127 string altstr_prepare(string)
3128 ========================
3130 void VM_altstr_prepare( void )
3133 const char *instr, *in;
3136 VM_SAFEPARMCOUNT( 1, VM_altstr_prepare );
3138 instr = PRVM_G_STRING( OFS_PARM0 );
3139 //VM_CheckEmptyString( instr );
3140 outstr = VM_GetTempString();
3142 for( out = outstr, in = instr, size = VM_STRINGTEMP_LENGTH - 1 ; size && *in ; size--, in++, out++ )
3151 PRVM_G_INT( OFS_RETURN ) = PRVM_SetEngineString( outstr );
3155 ========================
3158 string altstr_get(string, float)
3159 ========================
3161 void VM_altstr_get( void )
3163 const char *altstr, *pos;
3167 VM_SAFEPARMCOUNT( 2, VM_altstr_get );
3169 altstr = PRVM_G_STRING( OFS_PARM0 );
3170 //VM_CheckEmptyString( altstr );
3172 count = PRVM_G_FLOAT( OFS_PARM1 );
3173 count = count * 2 + 1;
3175 for( pos = altstr ; *pos && count ; pos++ )
3176 if( *pos == '\\' ) {
3179 } else if( *pos == '\'' )
3183 PRVM_G_INT( OFS_RETURN ) = PRVM_SetEngineString( NULL );
3187 outstr = VM_GetTempString();
3188 for( out = outstr, size = VM_STRINGTEMP_LENGTH - 1 ; size && *pos ; size--, pos++, out++ )
3189 if( *pos == '\\' ) {
3194 } else if( *pos == '\'' )
3200 PRVM_G_INT( OFS_RETURN ) = PRVM_SetEngineString( outstr );
3204 ========================
3207 string altstr_set(string altstr, float num, string set)
3208 ========================
3210 void VM_altstr_set( void )
3213 const char *altstr, *str;
3217 VM_SAFEPARMCOUNT( 3, VM_altstr_set );
3219 altstr = PRVM_G_STRING( OFS_PARM0 );
3220 //VM_CheckEmptyString( altstr );
3222 num = PRVM_G_FLOAT( OFS_PARM1 );
3224 str = PRVM_G_STRING( OFS_PARM2 );
3225 //VM_CheckEmptyString( str );
3227 outstr = out = VM_GetTempString();
3228 for( num = num * 2 + 1, in = altstr; *in && num; *out++ = *in++ )
3233 } else if( *in == '\'' ) {
3238 PRVM_G_INT( OFS_RETURN ) = PRVM_SetEngineString( altstr );
3242 for( ; *str; *out++ = *str++ );
3243 // now jump over the old content
3245 if( *in == '\'' || (*in == '\\' && !*++in) )
3249 PRVM_G_INT( OFS_RETURN ) = PRVM_SetEngineString( NULL );
3254 PRVM_G_INT( OFS_RETURN ) = PRVM_SetEngineString( outstr );
3258 ========================
3261 string altstr_ins(string altstr, float num, string set)
3262 ========================
3264 void VM_altstr_ins(void)
3274 in = instr = PRVM_G_STRING( OFS_PARM0 );
3275 num = PRVM_G_FLOAT( OFS_PARM1 );
3276 set = setstr = PRVM_G_STRING( OFS_PARM2 );
3278 out = outstr = VM_GetTempString();
3279 for( num = num * 2 + 2 ; *in && num > 0 ; *out++ = *in++ )
3284 } else if( *in == '\'' ) {
3289 for( ; *set ; *out++ = *set++ );
3293 PRVM_G_INT( OFS_RETURN ) = PRVM_SetEngineString( outstr );
3297 ////////////////////////////////////////
3298 // BufString functions
3299 ////////////////////////////////////////
3300 //[515]: string buffers support
3301 #define MAX_QCSTR_BUFFERS 128
3302 #define MAX_QCSTR_STRINGS 1024
3307 char *strings[MAX_QCSTR_STRINGS];
3310 static qcstrbuffer_t *qcstringbuffers[MAX_QCSTR_BUFFERS];
3311 static int num_qcstringbuffers;
3312 static int buf_sortpower;
3314 #define BUFSTR_BUFFER(a) (a>=MAX_QCSTR_BUFFERS) ? NULL : (qcstringbuffers[a])
3315 #define BUFSTR_ISFREE(a) (a<MAX_QCSTR_BUFFERS&&qcstringbuffers[a]&&qcstringbuffers[a]->num_strings<=0) ? 1 : 0
3317 static int BufStr_FindFreeBuffer (void)
3320 if(num_qcstringbuffers == MAX_QCSTR_BUFFERS)
3322 for(i=0;i<MAX_QCSTR_BUFFERS;i++)
3323 if(!qcstringbuffers[i])
3325 qcstringbuffers[i] = malloc(sizeof(qcstrbuffer_t));
3326 memset(qcstringbuffers[i], 0, sizeof(qcstrbuffer_t));
3332 static void BufStr_ClearBuffer (int index)
3334 qcstrbuffer_t *b = qcstringbuffers[index];
3339 if(b->num_strings > 0)
3341 for(i=0;i<b->num_strings;i++)
3343 free(b->strings[i]);
3344 num_qcstringbuffers--;
3346 free(qcstringbuffers[index]);
3347 qcstringbuffers[index] = NULL;
3351 static int BufStr_FindFreeString (qcstrbuffer_t *b)
3354 for(i=0;i<b->num_strings;i++)
3355 if(!b->strings[i] || !b->strings[i][0])
3357 if(i == MAX_QCSTR_STRINGS) return -1;
3361 static int BufStr_SortStringsUP (const void *in1, const void *in2)
3364 a = *((const char **) in1);
3365 b = *((const char **) in2);
3367 if(!b[0]) return -1;
3368 return strncmp(a, b, buf_sortpower);
3371 static int BufStr_SortStringsDOWN (const void *in1, const void *in2)
3374 a = *((const char **) in1);
3375 b = *((const char **) in2);
3377 if(!b[0]) return -1;
3378 return strncmp(b, a, buf_sortpower);
3382 static void VM_BufStr_Init (void)
3384 memset(qcstringbuffers, 0, sizeof(qcstringbuffers));
3385 num_qcstringbuffers = 0;
3388 static void VM_BufStr_ShutDown (void)
3391 for(i=0;i<MAX_QCSTR_BUFFERS && num_qcstringbuffers;i++)
3392 BufStr_ClearBuffer(i);
3397 ========================
3399 creates new buffer, and returns it's index, returns -1 if failed
3400 float buf_create(void) = #460;
3401 ========================
3403 void VM_buf_create (void)
3406 VM_SAFEPARMCOUNT(0, VM_buf_create);
3407 i = BufStr_FindFreeBuffer();
3409 num_qcstringbuffers++;
3411 //Con_Printf("VM_buf_create: buffers overflow in %s\n", PRVM_NAME);
3412 PRVM_G_FLOAT(OFS_RETURN) = i;
3416 ========================
3418 deletes buffer and all strings in it
3419 void buf_del(float bufhandle) = #461;
3420 ========================
3422 void VM_buf_del (void)
3424 VM_SAFEPARMCOUNT(1, VM_buf_del);
3425 if(BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0)))
3426 BufStr_ClearBuffer((int)PRVM_G_FLOAT(OFS_PARM0));
3429 Con_Printf("VM_buf_del: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3435 ========================
3437 how many strings are stored in buffer
3438 float buf_getsize(float bufhandle) = #462;
3439 ========================
3441 void VM_buf_getsize (void)
3444 VM_SAFEPARMCOUNT(1, VM_buf_getsize);
3446 b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3449 PRVM_G_FLOAT(OFS_RETURN) = -1;
3450 Con_Printf("VM_buf_getsize: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3454 PRVM_G_FLOAT(OFS_RETURN) = b->num_strings;
3458 ========================
3460 copy all content from one buffer to another, make sure it exists
3461 void buf_copy(float bufhandle_from, float bufhandle_to) = #463;
3462 ========================
3464 void VM_buf_copy (void)
3466 qcstrbuffer_t *b1, *b2;
3468 VM_SAFEPARMCOUNT(2, VM_buf_copy);
3470 b1 = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3473 Con_Printf("VM_buf_copy: invalid source buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3476 i = PRVM_G_FLOAT(OFS_PARM1);
3477 if(i == (int)PRVM_G_FLOAT(OFS_PARM0))
3479 Con_Printf("VM_buf_copy: source == destination (%i) in %s\n", i, PRVM_NAME);
3482 b2 = BUFSTR_BUFFER(i);
3485 Con_Printf("VM_buf_copy: invalid destination buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM1), PRVM_NAME);
3489 BufStr_ClearBuffer(i);
3490 qcstringbuffers[i] = malloc(sizeof(qcstrbuffer_t));
3491 memset(qcstringbuffers[i], 0, sizeof(qcstrbuffer_t));
3492 b2->num_strings = b1->num_strings;
3494 for(i=0;i<b1->num_strings;i++)
3495 if(b1->strings[i] && b1->strings[i][0])
3497 b2->strings[i] = malloc(strlen(b1->strings[i])+1);
3500 Con_Printf("VM_buf_copy: not enough memory for buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM1), PRVM_NAME);
3503 strcpy(b2->strings[i], b1->strings[i]);
3508 ========================
3510 sort buffer by beginnings of strings (sortpower defaults it's lenght)
3511 "backward == TRUE" means that sorting goes upside-down
3512 void buf_sort(float bufhandle, float sortpower, float backward) = #464;
3513 ========================
3515 void VM_buf_sort (void)
3519 VM_SAFEPARMCOUNT(3, VM_buf_sort);
3521 b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3524 Con_Printf("VM_buf_sort: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3527 if(b->num_strings <= 0)
3529 Con_Printf("VM_buf_sort: tried to sort empty buffer %i in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3532 buf_sortpower = PRVM_G_FLOAT(OFS_PARM1);
3533 if(buf_sortpower <= 0)
3534 buf_sortpower = 99999999;
3536 if(!PRVM_G_FLOAT(OFS_PARM2))
3537 qsort(b->strings, b->num_strings, sizeof(char*), BufStr_SortStringsUP);
3539 qsort(b->strings, b->num_strings, sizeof(char*), BufStr_SortStringsDOWN);
3541 for(i=b->num_strings-1;i>=0;i--) //[515]: delete empty lines
3544 if(b->strings[i][0])
3548 free(b->strings[i]);
3550 b->strings[i] = NULL;
3558 ========================
3560 concantenates all buffer string into one with "glue" separator and returns it as tempstring
3561 string buf_implode(float bufhandle, string glue) = #465;
3562 ========================
3564 void VM_buf_implode (void)
3571 VM_SAFEPARMCOUNT(2, VM_buf_implode);
3573 b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3574 PRVM_G_INT(OFS_RETURN) = 0;
3577 Con_Printf("VM_buf_implode: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3582 sep = PRVM_G_STRING(OFS_PARM1);
3583 k = VM_GetTempString();
3585 for(l=i=0;i<b->num_strings;i++)
3588 l += strlen(b->strings[i]);
3591 k = strcat(k, b->strings[i]);
3594 if(sep && (i != b->num_strings-1))
3604 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(k);
3608 ========================
3610 get a string from buffer, returns direct pointer, dont str_unzone it!
3611 string bufstr_get(float bufhandle, float string_index) = #465;
3612 ========================
3614 void VM_bufstr_get (void)
3618 VM_SAFEPARMCOUNT(2, VM_bufstr_get);
3620 b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3623 Con_Printf("VM_bufstr_get: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3626 strindex = (int)PRVM_G_FLOAT(OFS_PARM1);
3627 if(strindex < 0 || strindex > MAX_QCSTR_STRINGS)
3629 Con_Printf("VM_bufstr_get: invalid string index %i used in %s\n", strindex, PRVM_NAME);
3632 PRVM_G_INT(OFS_RETURN) = 0;
3633 if(b->num_strings <= strindex)
3635 if(b->strings[strindex])
3636 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(b->strings[strindex]);
3640 ========================
3642 copies a string into selected slot of buffer
3643 void bufstr_set(float bufhandle, float string_index, string str) = #466;
3644 ========================
3646 void VM_bufstr_set (void)
3648 int bufindex, strindex;
3652 VM_SAFEPARMCOUNT(3, VM_bufstr_set);
3654 bufindex = PRVM_G_FLOAT(OFS_PARM0);
3655 b = BUFSTR_BUFFER(bufindex);
3658 Con_Printf("VM_bufstr_set: invalid buffer %i used in %s\n", bufindex, PRVM_NAME);
3661 strindex = PRVM_G_FLOAT(OFS_PARM1);
3662 if(strindex < 0 || strindex > MAX_QCSTR_STRINGS)
3664 Con_Printf("VM_bufstr_set: invalid string index %i used in %s\n", strindex, PRVM_NAME);
3667 news = PRVM_G_STRING(OFS_PARM2);
3670 Con_Printf("VM_bufstr_set: null string used in %s\n", PRVM_NAME);
3673 if(b->strings[strindex])
3674 free(b->strings[strindex]);
3675 b->strings[strindex] = malloc(strlen(news)+1);
3676 strcpy(b->strings[strindex], news);
3680 ========================
3682 adds string to buffer in nearest free slot and returns it
3683 "order == TRUE" means that string will be added after last "full" slot
3684 float bufstr_add(float bufhandle, string str, float order) = #467;
3685 ========================
3687 void VM_bufstr_add (void)
3689 int bufindex, order, strindex;
3693 VM_SAFEPARMCOUNT(3, VM_bufstr_add);
3695 bufindex = PRVM_G_FLOAT(OFS_PARM0);
3696 b = BUFSTR_BUFFER(bufindex);
3697 PRVM_G_FLOAT(OFS_RETURN) = -1;
3700 Con_Printf("VM_bufstr_add: invalid buffer %i used in %s\n", bufindex, PRVM_NAME);
3703 string = PRVM_G_STRING(OFS_PARM1);
3706 Con_Printf("VM_bufstr_add: null string used in %s\n", PRVM_NAME);
3710 order = PRVM_G_FLOAT(OFS_PARM2);
3712 strindex = b->num_strings;
3715 strindex = BufStr_FindFreeString(b);
3718 Con_Printf("VM_bufstr_add: buffer %i has no free string slots in %s\n", bufindex, PRVM_NAME);
3723 while(b->num_strings <= strindex)
3725 if(b->num_strings == MAX_QCSTR_STRINGS)
3727 Con_Printf("VM_bufstr_add: buffer %i has no free string slots in %s\n", bufindex, PRVM_NAME);
3730 b->strings[b->num_strings] = NULL;
3733 if(b->strings[strindex])
3734 free(b->strings[strindex]);
3735 b->strings[strindex] = malloc(strlen(string)+1);
3736 strcpy(b->strings[strindex], string);
3737 PRVM_G_FLOAT(OFS_RETURN) = strindex;
3741 ========================
3743 delete string from buffer
3744 void bufstr_free(float bufhandle, float string_index) = #468;
3745 ========================
3747 void VM_bufstr_free (void)
3751 VM_SAFEPARMCOUNT(2, VM_bufstr_free);
3753 b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3756 Con_Printf("VM_bufstr_free: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3759 i = PRVM_G_FLOAT(OFS_PARM1);
3760 if(i < 0 || i > MAX_QCSTR_STRINGS)
3762 Con_Printf("VM_bufstr_free: invalid string index %i used in %s\n", i, PRVM_NAME);
3766 free(b->strings[i]);
3767 b->strings[i] = NULL;
3768 if(i+1 == b->num_strings)
3774 void VM_Cmd_Init(void)
3776 // only init the stuff for the current prog
3779 // VM_BufStr_Init();
3780 if(vm_polygons_initialized)
3782 Mem_FreePool(&vm_polygons_pool);
3783 vm_polygons_initialized = false;
3787 void VM_Cmd_Reset(void)
3789 CL_PurgeOwner( MENUOWNER );
3791 VM_Files_CloseAll();
3792 // VM_BufStr_ShutDown();
3793 if(vm_polygons_initialized)
3795 Mem_FreePool(&vm_polygons_pool);
3796 vm_polygons_initialized = false;