2 // Basically every vm builtin cmd should be in here.
3 // All 3 builtin and extension lists can be found here
4 // cause large (I think they will) parts are from pr_cmds the same copyright like in pr_cmds
9 //============================================================================
12 // temp string handling
13 // LordHavoc: added this to semi-fix the problem of using many ftos calls in a print
14 static char vm_string_temp[VM_STRINGTEMP_BUFFERS][VM_STRINGTEMP_LENGTH];
15 static int vm_string_tempindex = 0;
18 #define MAX_VMFILES 256
19 #define MAX_PRVMFILES MAX_VMFILES * PRVM_MAXPROGS
20 #define VM_FILES ((qfile_t**)(vm_files + PRVM_GetProgNr() * MAX_VMFILES))
22 qfile_t *vm_files[MAX_PRVMFILES];
24 // qc fs search handling
25 #define MAX_VMSEARCHES 128
26 #define TOTAL_VMSEARCHES MAX_VMSEARCHES * PRVM_MAXPROGS
27 #define VM_SEARCHLIST ((fssearch_t**)(vm_fssearchlist + PRVM_GetProgNr() * MAX_VMSEARCHES))
29 fssearch_t *vm_fssearchlist[TOTAL_VMSEARCHES];
31 char *VM_GetTempString(void)
34 s = vm_string_temp[vm_string_tempindex];
35 vm_string_tempindex = (vm_string_tempindex + 1) % VM_STRINGTEMP_BUFFERS;
39 void VM_CheckEmptyString (const char *s)
42 PRVM_ERROR ("%s: Bad string", PRVM_NAME);
45 //============================================================================
48 void VM_VarString(int first, char *out, int outlength)
54 outend = out + outlength - 1;
55 for (i = first;i < prog->argc && out < outend;i++)
57 s = PRVM_G_STRING((OFS_PARM0+i*3));
58 while (out < outend && *s)
68 returns true if the extension is supported by the server
70 checkextension(extensionname)
74 // kind of helper function
75 static qboolean checkextension(const char *name)
79 len = (int)strlen(name);
81 for (e = prog->extensionstring;*e;e++)
88 while (*e && *e != ' ')
90 if ((e - start) == len && !strncasecmp(start, name, len))
96 void VM_checkextension (void)
98 VM_SAFEPARMCOUNT(1,VM_checkextension);
100 PRVM_G_FLOAT(OFS_RETURN) = checkextension(PRVM_G_STRING(OFS_PARM0));
107 This is a TERMINAL error, which will kill off the entire prog.
116 char string[VM_STRINGTEMP_LENGTH];
118 VM_VarString(0, string, sizeof(string));
119 Con_Printf("======%s ERROR in %s:\n%s\n", PRVM_NAME, PRVM_GetString(prog->xfunction->s_name), string);
122 ed = PRVM_G_EDICT(prog->self->ofs);
126 PRVM_ERROR ("%s: Program error in function %s:\n%s\n", PRVM_NAME, PRVM_GetString(prog->xfunction->s_name), string);
133 Dumps out self, then an error message. The program is aborted and self is
134 removed, but the level can continue.
139 void VM_objerror (void)
142 char string[VM_STRINGTEMP_LENGTH];
144 VM_VarString(0, string, sizeof(string));
145 Con_Printf("======OBJECT ERROR======\n", PRVM_NAME, PRVM_GetString(prog->xfunction->s_name), string);
148 ed = PRVM_G_EDICT (prog->self->ofs);
154 // objerror has to display the object fields -> else call
155 PRVM_ERROR ("VM_objecterror: self not defined !");
156 Con_Printf("%s OBJECT ERROR in %s:\n%s\n", PRVM_NAME, PRVM_GetString(prog->xfunction->s_name), string);
161 VM_print (actually used only by client and menu)
170 char string[VM_STRINGTEMP_LENGTH];
172 VM_VarString(0, string, sizeof(string));
180 broadcast print to everyone on server
185 void VM_bprint (void)
187 char string[VM_STRINGTEMP_LENGTH];
191 Con_Printf("VM_bprint: game is not server(%s) !\n", PRVM_NAME);
195 VM_VarString(0, string, sizeof(string));
196 SV_BroadcastPrint(string);
201 VM_sprint (menu & client but only if server.active == true)
203 single print to a specific client
205 sprint(float clientnum,...[string])
208 void VM_sprint (void)
212 char string[VM_STRINGTEMP_LENGTH];
214 //find client for this entity
215 clientnum = (int)PRVM_G_FLOAT(OFS_PARM0);
216 if (!sv.active || clientnum < 0 || clientnum >= svs.maxclients || !svs.clients[clientnum].active)
218 Con_Printf("VM_sprint: %s: invalid client or server is not active !\n", PRVM_NAME);
222 client = svs.clients + clientnum;
223 if (!client->netconnection)
226 VM_VarString(1, string, sizeof(string));
227 MSG_WriteChar(&client->netconnection->message,svc_print);
228 MSG_WriteString(&client->netconnection->message, string);
235 single print to the screen
237 centerprint(clientent, value)
240 void VM_centerprint (void)
242 char string[VM_STRINGTEMP_LENGTH];
244 VM_VarString(0, string, sizeof(string));
245 SCR_CenterPrint(string);
252 vector normalize(vector)
255 void VM_normalize (void)
261 VM_SAFEPARMCOUNT(1,VM_normalize);
263 value1 = PRVM_G_VECTOR(OFS_PARM0);
265 f = VectorLength2(value1);
269 VectorScale(value1, f, newvalue);
272 VectorClear(newvalue);
274 VectorCopy (newvalue, PRVM_G_VECTOR(OFS_RETURN));
286 VM_SAFEPARMCOUNT(1,VM_vlen);
287 PRVM_G_FLOAT(OFS_RETURN) = VectorLength(PRVM_G_VECTOR(OFS_PARM0));
294 float vectoyaw(vector)
297 void VM_vectoyaw (void)
302 VM_SAFEPARMCOUNT(1,VM_vectoyaw);
304 value1 = PRVM_G_VECTOR(OFS_PARM0);
306 if (value1[1] == 0 && value1[0] == 0)
310 yaw = (int) (atan2(value1[1], value1[0]) * 180 / M_PI);
315 PRVM_G_FLOAT(OFS_RETURN) = yaw;
323 vector vectoangles(vector)
326 void VM_vectoangles (void)
332 VM_SAFEPARMCOUNT(1,VM_vectoangles);
334 value1 = PRVM_G_VECTOR(OFS_PARM0);
336 if (value1[1] == 0 && value1[0] == 0)
346 // LordHavoc: optimized a bit
349 yaw = (atan2(value1[1], value1[0]) * 180 / M_PI);
353 else if (value1[1] > 0)
358 forward = sqrt(value1[0]*value1[0] + value1[1]*value1[1]);
359 pitch = (atan2(value1[2], forward) * 180 / M_PI);
364 PRVM_G_FLOAT(OFS_RETURN+0) = pitch;
365 PRVM_G_FLOAT(OFS_RETURN+1) = yaw;
366 PRVM_G_FLOAT(OFS_RETURN+2) = 0;
373 Returns a number from 0<= num < 1
378 void VM_random (void)
380 VM_SAFEPARMCOUNT(0,VM_random);
382 PRVM_G_FLOAT(OFS_RETURN) = lhrandom(0, 1);
389 Each entity can have eight independant sound sources, like voice,
392 Channel 0 is an auto-allocate channel, the others override anything
393 already running on that entity/channel pair.
395 An attenuation of 0 will play full volume everywhere in the level.
396 Larger attenuations will drop off.
405 prvm_edict_t *entity;
409 entity = PRVM_G_EDICT(OFS_PARM0);
410 channel = PRVM_G_FLOAT(OFS_PARM1);
411 sample = PRVM_G_STRING(OFS_PARM2);
412 volume = PRVM_G_FLOAT(OFS_PARM3) * 255;
413 attenuation = PRVM_G_FLOAT(OFS_PARM4);
415 if (volume < 0 || volume > 255)
416 Host_Error ("SV_StartSound: volume = %i", volume);
418 if (attenuation < 0 || attenuation > 4)
419 Host_Error ("SV_StartSound: attenuation = %f", attenuation);
421 if (channel < 0 || channel > 7)
422 Host_Error ("SV_StartSound: channel = %i", channel);
424 SV_StartSound (entity, channel, sample, volume, attenuation);
432 localsound(string sample)
435 void VM_localsound(void)
439 VM_SAFEPARMCOUNT(1,VM_localsound);
441 s = PRVM_G_STRING(OFS_PARM0);
443 if(!S_LocalSound (s))
445 Con_Printf("VM_localsound: Failed to play %s for %s !\n", s, PRVM_NAME);
446 PRVM_G_FLOAT(OFS_RETURN) = -4;
450 PRVM_G_FLOAT(OFS_RETURN) = 1;
462 PRVM_ERROR ("%s: break statement", PRVM_NAME);
465 //============================================================================
471 Sends text over to the client's execution buffer
473 [localcmd (string, ...) or]
477 void VM_localcmd (void)
479 char string[VM_STRINGTEMP_LENGTH];
480 VM_VarString(0, string, sizeof(string));
481 Cbuf_AddText(string);
493 VM_SAFEPARMCOUNT(1,VM_cvar);
495 PRVM_G_FLOAT(OFS_RETURN) = Cvar_VariableValue(PRVM_G_STRING(OFS_PARM0));
502 const string VM_cvar_string (string)
505 void VM_cvar_string(void)
509 const char *cvar_string;
510 VM_SAFEPARMCOUNT(1,VM_cvar_string);
512 name = PRVM_G_STRING(OFS_PARM0);
515 PRVM_ERROR("VM_cvar_string: %s: null string", PRVM_NAME);
517 VM_CheckEmptyString(name);
519 out = VM_GetTempString();
521 cvar_string = Cvar_VariableString(name);
523 strcpy(out, cvar_string);
525 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(out);
530 ========================
533 const string VM_cvar_defstring (string)
534 ========================
536 void VM_cvar_defstring (void)
540 const char *cvar_string;
541 VM_SAFEPARMCOUNT(1,VM_cvar_string);
543 name = PRVM_G_STRING(OFS_PARM0);
546 PRVM_ERROR("VM_cvar_defstring: %s: null string", PRVM_NAME);
548 VM_CheckEmptyString(name);
550 out = VM_GetTempString();
552 cvar_string = Cvar_VariableDefString(name);
554 strcpy(out, cvar_string);
556 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(out);
562 void cvar_set (string,string)
565 void VM_cvar_set (void)
567 VM_SAFEPARMCOUNT(2,VM_cvar_set);
569 Cvar_Set(PRVM_G_STRING(OFS_PARM0), PRVM_G_STRING(OFS_PARM1));
579 void VM_dprint (void)
581 char string[VM_STRINGTEMP_LENGTH];
582 if (developer.integer)
584 VM_VarString(0, string, sizeof(string));
586 Con_Printf("%s", string);
588 Con_Printf("%s: %s", PRVM_NAME, string);
606 VM_SAFEPARMCOUNT(1, VM_ftos);
608 v = PRVM_G_FLOAT(OFS_PARM0);
610 s = VM_GetTempString();
611 if ((float)((int)v) == v)
612 sprintf(s, "%i", (int)v);
615 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(s);
630 VM_SAFEPARMCOUNT(1,VM_fabs);
632 v = PRVM_G_FLOAT(OFS_PARM0);
633 PRVM_G_FLOAT(OFS_RETURN) = fabs(v);
648 VM_SAFEPARMCOUNT(1,VM_vtos);
650 s = VM_GetTempString();
651 sprintf (s, "'%5.1f %5.1f %5.1f'", PRVM_G_VECTOR(OFS_PARM0)[0], PRVM_G_VECTOR(OFS_PARM0)[1], PRVM_G_VECTOR(OFS_PARM0)[2]);
652 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(s);
667 VM_SAFEPARMCOUNT(1, VM_etos);
669 s = VM_GetTempString();
670 sprintf (s, "entity %i", PRVM_G_EDICTNUM(OFS_PARM0));
671 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(s);
678 float stof(...[string])
683 char string[VM_STRINGTEMP_LENGTH];
684 VM_VarString(0, string, sizeof(string));
685 PRVM_G_FLOAT(OFS_RETURN) = atof(string);
689 ========================
693 ========================
697 VM_SAFEPARMCOUNT(1, VM_itof);
698 PRVM_G_FLOAT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
702 ========================
706 ========================
711 VM_SAFEPARMCOUNT(1, VM_ftoi);
713 ent = (int)PRVM_G_FLOAT(OFS_PARM0);
714 if(PRVM_PROG_TO_EDICT(ent)->priv.required->free)
715 PRVM_ERROR ("VM_ftoe: %s tried to access a freed entity (entity %i)!", PRVM_NAME, ent);
717 PRVM_G_INT(OFS_RETURN) = ent;
731 prog->xfunction->builtinsprofile += 20;
732 ed = PRVM_ED_Alloc();
744 void VM_remove (void)
747 prog->xfunction->builtinsprofile += 20;
749 VM_SAFEPARMCOUNT(1, VM_remove);
751 ed = PRVM_G_EDICT(OFS_PARM0);
752 if( PRVM_NUM_FOR_EDICT(ed) <= prog->reserved_edicts ) {
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);
786 // LordHavoc: apparently BloodMage does a find(world, weaponmodel, "") and
787 // expects it to find all the monsters, so we must be careful to support
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);
879 // LordHavoc: apparently BloodMage does a find(world, weaponmodel, "") and
880 // expects it to find all the monsters, so we must be careful to support
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 (!PRVM_E_FLOAT(ed,f))
980 if ((int)PRVM_E_FLOAT(ed,f) & s)
987 VM_RETURN_EDICT(prog->edicts);
991 ========================
994 entity findchainflags(.float field, float match)
995 ========================
997 // LordHavoc: chained search for flags in float fields
998 void VM_findchainflags (void)
1004 prvm_edict_t *ent, *chain;
1006 VM_SAFEPARMCOUNT(2, VM_findchainflags);
1008 if(!prog->flag & PRVM_FE_CHAIN)
1009 PRVM_ERROR("VM_findchainflags: %s doesnt have a chain field !", PRVM_NAME);
1011 chain_of = PRVM_ED_FindField("chain")->ofs;
1013 chain = (prvm_edict_t *)prog->edicts;
1015 f = PRVM_G_INT(OFS_PARM0);
1016 s = (int)PRVM_G_FLOAT(OFS_PARM1);
1018 ent = PRVM_NEXT_EDICT(prog->edicts);
1019 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
1021 prog->xfunction->builtinsprofile++;
1022 if (ent->priv.required->free)
1024 if (!PRVM_E_FLOAT(ent,f))
1026 if (!((int)PRVM_E_FLOAT(ent,f) & s))
1029 PRVM_E_INT(ent,chain_of) = PRVM_EDICT_TO_PROG(chain);
1033 VM_RETURN_EDICT(chain);
1043 void VM_coredump (void)
1045 VM_SAFEPARMCOUNT(0,VM_coredump);
1047 Cbuf_AddText("prvm_edicts ");
1048 Cbuf_AddText(PRVM_NAME);
1059 void PRVM_StackTrace(void);
1060 void VM_stackdump (void)
1062 VM_SAFEPARMCOUNT(0, VM_stackdump);
1077 VM_SAFEPARMCOUNT(0, VM_crash);
1079 PRVM_ERROR("Crash called by %s",PRVM_NAME);
1089 void VM_traceon (void)
1091 VM_SAFEPARMCOUNT(0,VM_traceon);
1103 void VM_traceoff (void)
1105 VM_SAFEPARMCOUNT(0,VM_traceoff);
1107 prog->trace = false;
1117 void VM_eprint (void)
1119 VM_SAFEPARMCOUNT(1,VM_eprint);
1121 PRVM_ED_PrintNum (PRVM_G_EDICTNUM(OFS_PARM0));
1135 VM_SAFEPARMCOUNT(1,VM_rint);
1137 f = PRVM_G_FLOAT(OFS_PARM0);
1139 PRVM_G_FLOAT(OFS_RETURN) = (int)(f + 0.5);
1141 PRVM_G_FLOAT(OFS_RETURN) = (int)(f - 0.5);
1151 void VM_floor (void)
1153 VM_SAFEPARMCOUNT(1,VM_floor);
1155 PRVM_G_FLOAT(OFS_RETURN) = floor(PRVM_G_FLOAT(OFS_PARM0));
1167 VM_SAFEPARMCOUNT(1,VM_ceil);
1169 PRVM_G_FLOAT(OFS_RETURN) = ceil(PRVM_G_FLOAT(OFS_PARM0));
1177 entity nextent(entity)
1180 void VM_nextent (void)
1185 i = PRVM_G_EDICTNUM(OFS_PARM0);
1188 prog->xfunction->builtinsprofile++;
1190 if (i == prog->num_edicts)
1192 VM_RETURN_EDICT(prog->edicts);
1195 ent = PRVM_EDICT_NUM(i);
1196 if (!ent->priv.required->free)
1198 VM_RETURN_EDICT(ent);
1204 //=============================================================================
1211 changelevel(string map)
1214 void VM_changelevel (void)
1218 VM_SAFEPARMCOUNT(1, VM_changelevel);
1222 Con_Printf("VM_changelevel: game is not server (%s)\n", PRVM_NAME);
1226 // make sure we don't issue two changelevels
1227 if (svs.changelevel_issued)
1229 svs.changelevel_issued = true;
1231 s = PRVM_G_STRING(OFS_PARM0);
1232 Cbuf_AddText (va("changelevel %s\n",s));
1244 VM_SAFEPARMCOUNT(1,VM_sin);
1245 PRVM_G_FLOAT(OFS_RETURN) = sin(PRVM_G_FLOAT(OFS_PARM0));
1256 VM_SAFEPARMCOUNT(1,VM_cos);
1257 PRVM_G_FLOAT(OFS_RETURN) = cos(PRVM_G_FLOAT(OFS_PARM0));
1269 VM_SAFEPARMCOUNT(1,VM_sqrt);
1270 PRVM_G_FLOAT(OFS_RETURN) = sqrt(PRVM_G_FLOAT(OFS_PARM0));
1277 Returns a vector of length < 1 and > 0
1282 void VM_randomvec (void)
1287 VM_SAFEPARMCOUNT(0, VM_randomvec);
1292 temp[0] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1293 temp[1] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1294 temp[2] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1296 while (DotProduct(temp, temp) >= 1);
1297 VectorCopy (temp, PRVM_G_VECTOR(OFS_RETURN));
1300 temp[0] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1301 temp[1] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1302 temp[2] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1303 // length returned always > 0
1304 length = (rand()&32766 + 1) * (1.0 / 32767.0) / VectorLength(temp);
1305 VectorScale(temp,length, temp);*/
1306 //VectorCopy(temp, PRVM_G_VECTOR(OFS_RETURN));
1309 //=============================================================================
1315 float registercvar (string name, string value, float flags)
1318 void VM_registercvar (void)
1320 const char *name, *value;
1323 VM_SAFEPARMCOUNT(3,VM_registercvar);
1325 name = PRVM_G_STRING(OFS_PARM0);
1326 value = PRVM_G_STRING(OFS_PARM1);
1327 flags = (int)PRVM_G_FLOAT(OFS_PARM2);
1328 PRVM_G_FLOAT(OFS_RETURN) = 0;
1330 if(flags > CVAR_MAXFLAGSVAL)
1333 // first check to see if it has already been defined
1334 if (Cvar_FindVar (name))
1337 // check for overlap with a command
1338 if (Cmd_Exists (name))
1340 Con_Printf("VM_registercvar: %s is a command\n", name);
1344 Cvar_Get(name, value, flags);
1346 PRVM_G_FLOAT(OFS_RETURN) = 1; // success
1353 returns the minimum of two supplied floats
1355 float min(float a, float b, ...[float])
1360 // LordHavoc: 3+ argument enhancement suggested by FrikaC
1361 if (prog->argc == 2)
1362 PRVM_G_FLOAT(OFS_RETURN) = min(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1363 else if (prog->argc >= 3)
1366 float f = PRVM_G_FLOAT(OFS_PARM0);
1367 for (i = 1;i < prog->argc;i++)
1368 if (PRVM_G_FLOAT((OFS_PARM0+i*3)) < f)
1369 f = PRVM_G_FLOAT((OFS_PARM0+i*3));
1370 PRVM_G_FLOAT(OFS_RETURN) = f;
1373 PRVM_ERROR("VM_min: %s must supply at least 2 floats", PRVM_NAME);
1380 returns the maximum of two supplied floats
1382 float max(float a, float b, ...[float])
1387 // LordHavoc: 3+ argument enhancement suggested by FrikaC
1388 if (prog->argc == 2)
1389 PRVM_G_FLOAT(OFS_RETURN) = max(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1390 else if (prog->argc >= 3)
1393 float f = PRVM_G_FLOAT(OFS_PARM0);
1394 for (i = 1;i < prog->argc;i++)
1395 if (PRVM_G_FLOAT((OFS_PARM0+i*3)) > f)
1396 f = PRVM_G_FLOAT((OFS_PARM0+i*3));
1397 PRVM_G_FLOAT(OFS_RETURN) = f;
1400 PRVM_ERROR("VM_max: %s must supply at least 2 floats", PRVM_NAME);
1407 returns number bounded by supplied range
1409 float bound(float min, float value, float max)
1412 void VM_bound (void)
1414 VM_SAFEPARMCOUNT(3,VM_bound);
1415 PRVM_G_FLOAT(OFS_RETURN) = bound(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1), PRVM_G_FLOAT(OFS_PARM2));
1422 returns a raised to power b
1424 float pow(float a, float b)
1429 VM_SAFEPARMCOUNT(2,VM_pow);
1430 PRVM_G_FLOAT(OFS_RETURN) = pow(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1437 copies data from one entity to another
1439 copyentity(entity src, entity dst)
1442 void VM_copyentity (void)
1444 prvm_edict_t *in, *out;
1445 VM_SAFEPARMCOUNT(2,VM_copyentity);
1446 in = PRVM_G_EDICT(OFS_PARM0);
1447 out = PRVM_G_EDICT(OFS_PARM1);
1448 memcpy(out->fields.vp, in->fields.vp, prog->progs->entityfields * 4);
1455 sets the color of a client and broadcasts the update to all connected clients
1457 setcolor(clientent, value)
1460 /*void PF_setcolor (void)
1466 entnum = PRVM_G_EDICTNUM(OFS_PARM0);
1467 i = PRVM_G_FLOAT(OFS_PARM1);
1469 if (entnum < 1 || entnum > svs.maxclients || !svs.clients[entnum-1].active)
1471 Con_Print("tried to setcolor a non-client\n");
1475 client = svs.clients + entnum-1;
1476 if ((val = PRVM_GETEDICTFIELDVALUE(client->edict, eval_clientcolors)))
1479 client->old_colors = i;
1480 client->edict->fields.server->team = (i & 15) + 1;
1482 MSG_WriteByte (&sv.reliable_datagram, svc_updatecolors);
1483 MSG_WriteByte (&sv.reliable_datagram, entnum - 1);
1484 MSG_WriteByte (&sv.reliable_datagram, i);
1487 void VM_Files_Init(void)
1489 memset(VM_FILES, 0, sizeof(qfile_t*[MAX_VMFILES]));
1492 void VM_Files_CloseAll(void)
1495 for (i = 0;i < MAX_VMFILES;i++)
1498 FS_Close(VM_FILES[i]);
1499 //VM_FILES[i] = NULL;
1501 memset(VM_FILES,0,sizeof(qfile_t*[MAX_VMFILES])); // this should be faster (is it ?)
1504 qfile_t *VM_GetFileHandle( int index )
1506 if (index < 0 || index >= MAX_VMFILES)
1508 Con_Printf("VM_GetFileHandle: invalid file handle %i used in %s\n", index, PRVM_NAME);
1511 if (VM_FILES[index] == NULL)
1513 Con_Printf("VM_GetFileHandle: no such file handle %i (or file has been closed) in %s\n", index, PRVM_NAME);
1516 return VM_FILES[index];
1523 float fopen(string filename, float mode)
1526 // float(string filename, float mode) fopen = #110;
1527 // opens a file inside quake/gamedir/data/ (mode is FILE_READ, FILE_APPEND, or FILE_WRITE),
1528 // returns fhandle >= 0 if successful, or fhandle < 0 if unable to open file for any reason
1532 const char *modestring, *filename;
1534 VM_SAFEPARMCOUNT(2,VM_fopen);
1536 for (filenum = 0;filenum < MAX_VMFILES;filenum++)
1537 if (VM_FILES[filenum] == NULL)
1539 if (filenum >= MAX_VMFILES)
1541 Con_Printf("VM_fopen: %s ran out of file handles (%i)\n", PRVM_NAME, MAX_VMFILES);
1542 PRVM_G_FLOAT(OFS_RETURN) = -2;
1545 mode = (int)PRVM_G_FLOAT(OFS_PARM1);
1548 case 0: // FILE_READ
1551 case 1: // FILE_APPEND
1554 case 2: // FILE_WRITE
1558 Con_Printf("VM_fopen: %s: no such mode %i (valid: 0 = read, 1 = append, 2 = write)\n", PRVM_NAME, mode);
1559 PRVM_G_FLOAT(OFS_RETURN) = -3;
1562 filename = PRVM_G_STRING(OFS_PARM0);
1564 VM_FILES[filenum] = FS_Open(va("data/%s", filename), modestring, false, false);
1565 if (VM_FILES[filenum] == NULL && mode == 0)
1566 VM_FILES[filenum] = FS_Open(va("%s", filename), modestring, false, false);
1568 if (VM_FILES[filenum] == NULL)
1570 if (developer.integer >= 10)
1571 Con_Printf("VM_fopen: %s: %s mode %s failed\n", PRVM_NAME, filename, modestring);
1572 PRVM_G_FLOAT(OFS_RETURN) = -1;
1576 if (developer.integer >= 10)
1577 Con_Printf("VM_fopen: %s: %s mode %s opened as #%i\n", PRVM_NAME, filename, modestring, filenum);
1578 PRVM_G_FLOAT(OFS_RETURN) = filenum;
1586 fclose(float fhandle)
1589 //void(float fhandle) fclose = #111; // closes a file
1590 void VM_fclose(void)
1594 VM_SAFEPARMCOUNT(1,VM_fclose);
1596 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1597 if (filenum < 0 || filenum >= MAX_VMFILES)
1599 Con_Printf("VM_fclose: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1602 if (VM_FILES[filenum] == NULL)
1604 Con_Printf("VM_fclose: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1607 if (developer.integer >= 10)
1608 Con_Printf("VM_fclose: %s: #%i closed\n", PRVM_NAME, filenum);
1609 FS_Close(VM_FILES[filenum]);
1610 VM_FILES[filenum] = NULL;
1617 string fgets(float fhandle)
1620 //string(float fhandle) fgets = #112; // reads a line of text from the file and returns as a tempstring
1624 static char string[VM_STRINGTEMP_LENGTH];
1627 VM_SAFEPARMCOUNT(1,VM_fgets);
1629 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1630 if (filenum < 0 || filenum >= MAX_VMFILES)
1632 Con_Printf("VM_fgets: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1635 if (VM_FILES[filenum] == NULL)
1637 Con_Printf("VM_fgets: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1643 c = FS_Getc(VM_FILES[filenum]);
1644 if (c == '\r' || c == '\n' || c < 0)
1646 if (end < VM_STRINGTEMP_LENGTH - 1)
1650 // remove \n following \r
1653 c = FS_Getc(VM_FILES[filenum]);
1655 FS_UnGetc(VM_FILES[filenum], (unsigned char)c);
1657 if (developer.integer >= 100)
1658 Con_Printf("fgets: %s: %s\n", PRVM_NAME, string);
1660 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(string);
1662 PRVM_G_INT(OFS_RETURN) = 0;
1669 fputs(float fhandle, string s)
1672 //void(float fhandle, string s) fputs = #113; // writes a line of text to the end of the file
1676 char string[VM_STRINGTEMP_LENGTH];
1679 VM_SAFEPARMCOUNT(2,VM_fputs);
1681 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1682 if (filenum < 0 || filenum >= MAX_VMFILES)
1684 Con_Printf("VM_fputs: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1687 if (VM_FILES[filenum] == NULL)
1689 Con_Printf("VM_fputs: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1692 VM_VarString(1, string, sizeof(string));
1693 if ((stringlength = (int)strlen(string)))
1694 FS_Write(VM_FILES[filenum], string, stringlength);
1695 if (developer.integer >= 100)
1696 Con_Printf("fputs: %s: %s\n", PRVM_NAME, string);
1703 float strlen(string s)
1706 //float(string s) strlen = #114; // returns how many characters are in a string
1707 void VM_strlen(void)
1711 VM_SAFEPARMCOUNT(1,VM_strlen);
1713 s = PRVM_G_STRING(OFS_PARM0);
1715 PRVM_G_FLOAT(OFS_RETURN) = strlen(s);
1717 PRVM_G_FLOAT(OFS_RETURN) = 0;
1724 string strcat(string,string,...[string])
1727 //string(string s1, string s2) strcat = #115;
1728 // concatenates two strings (for example "abc", "def" would return "abcdef")
1729 // and returns as a tempstring
1730 void VM_strcat(void)
1735 PRVM_ERROR("VM_strcat wrong parameter count (min. 1 expected ) !");
1737 s = VM_GetTempString();
1738 VM_VarString(0, s, VM_STRINGTEMP_LENGTH);
1739 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(s);
1746 string substring(string s, float start, float length)
1749 // string(string s, float start, float length) substring = #116;
1750 // returns a section of a string as a tempstring
1751 void VM_substring(void)
1753 int i, start, length;
1757 VM_SAFEPARMCOUNT(3,VM_substring);
1759 string = VM_GetTempString();
1760 s = PRVM_G_STRING(OFS_PARM0);
1761 start = (int)PRVM_G_FLOAT(OFS_PARM1);
1762 length = (int)PRVM_G_FLOAT(OFS_PARM2);
1765 for (i = 0;i < start && *s;i++, s++);
1766 for (i = 0;i < VM_STRINGTEMP_LENGTH - 1 && *s && i < length;i++, s++)
1769 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(string);
1776 vector stov(string s)
1779 //vector(string s) stov = #117; // returns vector value from a string
1782 char string[VM_STRINGTEMP_LENGTH];
1784 VM_SAFEPARMCOUNT(1,VM_stov);
1786 VM_VarString(0, string, sizeof(string));
1787 Math_atov(string, PRVM_G_VECTOR(OFS_RETURN));
1794 string strzone(string s)
1797 //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)
1798 void VM_strzone(void)
1801 char string[VM_STRINGTEMP_LENGTH];
1803 VM_SAFEPARMCOUNT(1,VM_strzone);
1805 VM_VarString(0, string, sizeof(string));
1806 PRVM_G_INT(OFS_RETURN) = PRVM_AllocString(strlen(string) + 1, &out);
1807 strcpy(out, string);
1817 //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!!!)
1818 void VM_strunzone(void)
1820 VM_SAFEPARMCOUNT(1,VM_strunzone);
1821 PRVM_FreeString(PRVM_G_INT(OFS_PARM0));
1826 VM_command (used by client and menu)
1828 clientcommand(float client, string s) (for client and menu)
1831 //void(entity e, string s) clientcommand = #440; // executes a command string as if it came from the specified client
1832 //this function originally written by KrimZon, made shorter by LordHavoc
1833 void VM_clcommand (void)
1835 client_t *temp_client;
1838 VM_SAFEPARMCOUNT(2,VM_clcommand);
1840 i = (int)PRVM_G_FLOAT(OFS_PARM0);
1841 if (!sv.active || i < 0 || i >= svs.maxclients || !svs.clients[i].active)
1843 Con_Printf("VM_clientcommand: %s: invalid client/server is not active !\n", PRVM_NAME);
1847 temp_client = host_client;
1848 host_client = svs.clients + i;
1849 Cmd_ExecuteString (PRVM_G_STRING(OFS_PARM1), src_client);
1850 host_client = temp_client;
1858 float tokenize(string s)
1861 //float(string s) tokenize = #441; // takes apart a string into individal words (access them with argv), returns how many
1862 //this function originally written by KrimZon, made shorter by LordHavoc
1863 //20040203: rewritten by LordHavoc (no longer uses allocations)
1865 char *tokens[256], tokenbuf[MAX_INPUTLINE];
1866 void VM_tokenize (void)
1871 VM_SAFEPARMCOUNT(1,VM_tokenize);
1873 p = PRVM_G_STRING(OFS_PARM0);
1877 while(COM_ParseToken(&p, false))
1879 if (num_tokens >= (int)(sizeof(tokens)/sizeof(tokens[0])))
1881 if (pos + strlen(com_token) + 1 > sizeof(tokenbuf))
1883 tokens[num_tokens++] = tokenbuf + pos;
1884 strcpy(tokenbuf + pos, com_token);
1885 pos += strlen(com_token) + 1;
1888 PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
1891 //string(float n) argv = #442; // returns a word from the tokenized string (returns nothing for an invalid index)
1892 //this function originally written by KrimZon, made shorter by LordHavoc
1897 VM_SAFEPARMCOUNT(1,VM_argv);
1899 token_num = (int)PRVM_G_FLOAT(OFS_PARM0);
1901 if (token_num >= 0 && token_num < num_tokens)
1902 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(tokens[token_num]);
1904 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(NULL);
1908 //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)
1909 void PF_setattachment (void)
1911 prvm_edict_t *e = PRVM_G_EDICT(OFS_PARM0);
1912 prvm_edict_t *tagentity = PRVM_G_EDICT(OFS_PARM1);
1913 char *tagname = PRVM_G_STRING(OFS_PARM2);
1918 if (tagentity == NULL)
1919 tagentity = prog->edicts;
1921 v = PRVM_GETEDICTFIELDVALUE(e, eval_tag_entity);
1923 fields.server->edict = PRVM_EDICT_TO_PROG(tagentity);
1925 v = PRVM_GETEDICTFIELDVALUE(e, eval_tag_index);
1927 fields.server->_float = 0;
1928 if (tagentity != NULL && tagentity != prog->edicts && tagname && tagname[0])
1930 modelindex = (int)tagentity->fields.server->modelindex;
1931 if (modelindex >= 0 && modelindex < MAX_MODELS)
1933 model = sv.models[modelindex];
1934 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)
1935 for (i = 0;i < model->data_overridetagnamesforskin[(unsigned int)tagentity->fields.server->skin].num_overridetagnames;i++)
1936 if (!strcmp(tagname, model->data_overridetagnamesforskin[(unsigned int)tagentity->fields.server->skin].data_overridetagnames[i].name))
1937 fields.server->_float = i + 1;
1938 // FIXME: use a model function to get tag info (need to handle skeletal)
1939 if (fields.server->_float == 0 && model->num_tags)
1940 for (i = 0;i < model->num_tags;i++)
1941 if (!strcmp(tagname, model->data_tags[i].name))
1942 fields.server->_float = i + 1;
1943 if (fields.server->_float == 0)
1944 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);
1947 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));
1958 void VM_isserver(void)
1960 VM_SAFEPARMCOUNT(0,VM_serverstate);
1962 PRVM_G_FLOAT(OFS_RETURN) = sv.active;
1972 void VM_clientcount(void)
1974 VM_SAFEPARMCOUNT(0,VM_clientcount);
1976 PRVM_G_FLOAT(OFS_RETURN) = svs.maxclients;
1986 void VM_clientstate(void)
1988 VM_SAFEPARMCOUNT(0,VM_clientstate);
1990 PRVM_G_FLOAT(OFS_RETURN) = cls.state;
1997 float getostype(void)
1999 */ // not used at the moment -> not included in the common list
2000 void VM_getostype(void)
2002 VM_SAFEPARMCOUNT(0,VM_getostype);
2007 OS_MAC - not supported
2011 PRVM_G_FLOAT(OFS_RETURN) = 0;
2013 PRVM_G_FLOAT(OFS_RETURN) = 2;
2015 PRVM_G_FLOAT(OFS_RETURN) = 1;
2023 vector getmousepos()
2026 void VM_getmousepos(void)
2029 VM_SAFEPARMCOUNT(0,VM_getmousepos);
2031 PRVM_G_VECTOR(OFS_RETURN)[0] = in_mouse_x * vid_conwidth.integer / vid.width;
2032 PRVM_G_VECTOR(OFS_RETURN)[1] = in_mouse_y * vid_conheight.integer / vid.height;
2033 PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
2043 void VM_gettime(void)
2045 VM_SAFEPARMCOUNT(0,VM_gettime);
2047 PRVM_G_FLOAT(OFS_RETURN) = (float) *prog->time;
2054 loadfromdata(string data)
2057 void VM_loadfromdata(void)
2059 VM_SAFEPARMCOUNT(1,VM_loadentsfromfile);
2061 PRVM_ED_LoadFromFile(PRVM_G_STRING(OFS_PARM0));
2065 ========================
2068 parseentitydata(entity ent, string data)
2069 ========================
2071 void VM_parseentitydata(void)
2076 VM_SAFEPARMCOUNT(2, VM_parseentitydata);
2078 // get edict and test it
2079 ent = PRVM_G_EDICT(OFS_PARM0);
2080 if (ent->priv.required->free)
2081 PRVM_ERROR ("VM_parseentitydata: %s: Can only set already spawned entities (entity %i is free)!", PRVM_NAME, PRVM_NUM_FOR_EDICT(ent));
2083 data = PRVM_G_STRING(OFS_PARM1);
2085 // parse the opening brace
2086 if (!COM_ParseToken(&data, false) || com_token[0] != '{' )
2087 PRVM_ERROR ("VM_parseentitydata: %s: Couldn't parse entity data:\n%s", PRVM_NAME, data );
2089 PRVM_ED_ParseEdict (data, ent);
2096 loadfromfile(string file)
2099 void VM_loadfromfile(void)
2101 const char *filename;
2104 VM_SAFEPARMCOUNT(1,VM_loadfromfile);
2106 filename = PRVM_G_STRING(OFS_PARM0);
2107 // .. is parent directory on many platforms
2108 // / is parent directory on Amiga
2109 // : is root of drive on Amiga (also used as a directory separator on Mac, but / works there too, so that's a bad idea)
2110 // \ is a windows-ism (so it's naughty to use it, / works on all platforms)
2111 if ((filename[0] == '.' && filename[1] == '.') || filename[0] == '/' || strrchr(filename, ':') || strrchr(filename, '\\'))
2113 Con_Printf("VM_loadfromfile: %s dangerous or non-portable filename \"%s\" not allowed. (contains : or \\ or begins with .. or /)\n", PRVM_NAME, filename);
2114 PRVM_G_FLOAT(OFS_RETURN) = -4;
2118 // not conform with VM_fopen
2119 data = (char *)FS_LoadFile(filename, tempmempool, false, NULL);
2121 PRVM_G_FLOAT(OFS_RETURN) = -1;
2123 PRVM_ED_LoadFromFile(data);
2134 float mod(float val, float m)
2137 void VM_modulo(void)
2140 VM_SAFEPARMCOUNT(2,VM_module);
2142 val = (int) PRVM_G_FLOAT(OFS_PARM0);
2143 m = (int) PRVM_G_FLOAT(OFS_PARM1);
2145 PRVM_G_FLOAT(OFS_RETURN) = (float) (val % m);
2148 void VM_Search_Init(void)
2150 memset(VM_SEARCHLIST,0,sizeof(fssearch_t*[MAX_VMSEARCHES]));
2153 void VM_Search_Reset(void)
2156 // reset the fssearch list
2157 for(i = 0; i < MAX_VMSEARCHES; i++)
2158 if(VM_SEARCHLIST[i])
2159 FS_FreeSearch(VM_SEARCHLIST[i]);
2160 memset(VM_SEARCHLIST,0,sizeof(fssearch_t*[MAX_VMSEARCHES]));
2167 float search_begin(string pattern, float caseinsensitive, float quiet)
2170 void VM_search_begin(void)
2173 const char *pattern;
2174 int caseinsens, quiet;
2176 VM_SAFEPARMCOUNT(3, VM_search_begin);
2178 pattern = PRVM_G_STRING(OFS_PARM0);
2180 VM_CheckEmptyString(pattern);
2182 caseinsens = (int)PRVM_G_FLOAT(OFS_PARM1);
2183 quiet = (int)PRVM_G_FLOAT(OFS_PARM2);
2185 for(handle = 0; handle < MAX_VMSEARCHES; handle++)
2186 if(!VM_SEARCHLIST[handle])
2189 if(handle >= MAX_VMSEARCHES)
2191 Con_Printf("VM_search_begin: %s ran out of search handles (%i)\n", PRVM_NAME, MAX_VMSEARCHES);
2192 PRVM_G_FLOAT(OFS_RETURN) = -2;
2196 if(!(VM_SEARCHLIST[handle] = FS_Search(pattern,caseinsens, quiet)))
2197 PRVM_G_FLOAT(OFS_RETURN) = -1;
2199 PRVM_G_FLOAT(OFS_RETURN) = handle;
2206 void search_end(float handle)
2209 void VM_search_end(void)
2212 VM_SAFEPARMCOUNT(1, VM_search_end);
2214 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
2216 if(handle < 0 || handle >= MAX_VMSEARCHES)
2218 Con_Printf("VM_search_end: invalid handle %i used in %s\n", handle, PRVM_NAME);
2221 if(VM_SEARCHLIST[handle] == NULL)
2223 Con_Printf("VM_search_end: no such handle %i in %s\n", handle, PRVM_NAME);
2227 FS_FreeSearch(VM_SEARCHLIST[handle]);
2228 VM_SEARCHLIST[handle] = NULL;
2235 float search_getsize(float handle)
2238 void VM_search_getsize(void)
2241 VM_SAFEPARMCOUNT(1, VM_M_search_getsize);
2243 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
2245 if(handle < 0 || handle >= MAX_VMSEARCHES)
2247 Con_Printf("VM_search_getsize: invalid handle %i used in %s\n", handle, PRVM_NAME);
2250 if(VM_SEARCHLIST[handle] == NULL)
2252 Con_Printf("VM_search_getsize: no such handle %i in %s\n", handle, PRVM_NAME);
2256 PRVM_G_FLOAT(OFS_RETURN) = VM_SEARCHLIST[handle]->numfilenames;
2261 VM_search_getfilename
2263 string search_getfilename(float handle, float num)
2266 void VM_search_getfilename(void)
2268 int handle, filenum;
2270 VM_SAFEPARMCOUNT(2, VM_search_getfilename);
2272 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
2273 filenum = (int)PRVM_G_FLOAT(OFS_PARM1);
2275 if(handle < 0 || handle >= MAX_VMSEARCHES)
2277 Con_Printf("VM_search_getfilename: invalid handle %i used in %s\n", handle, PRVM_NAME);
2280 if(VM_SEARCHLIST[handle] == NULL)
2282 Con_Printf("VM_search_getfilename: no such handle %i in %s\n", handle, PRVM_NAME);
2285 if(filenum < 0 || filenum >= VM_SEARCHLIST[handle]->numfilenames)
2287 Con_Printf("VM_search_getfilename: invalid filenum %i in %s\n", filenum, PRVM_NAME);
2291 tmp = VM_GetTempString();
2292 strcpy(tmp, VM_SEARCHLIST[handle]->filenames[filenum]);
2294 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(tmp);
2301 string chr(float ascii)
2307 VM_SAFEPARMCOUNT(1, VM_chr);
2309 tmp = VM_GetTempString();
2310 tmp[0] = (unsigned char) PRVM_G_FLOAT(OFS_PARM0);
2313 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(tmp);
2316 //=============================================================================
2317 // Draw builtins (client & menu)
2323 float iscachedpic(string pic)
2326 void VM_iscachedpic(void)
2328 VM_SAFEPARMCOUNT(1,VM_iscachedpic);
2330 // drawq hasnt such a function, thus always return true
2331 PRVM_G_FLOAT(OFS_RETURN) = false;
2338 string precache_pic(string pic)
2341 void VM_precache_pic(void)
2345 VM_SAFEPARMCOUNT(1, VM_precache_pic);
2347 s = PRVM_G_STRING(OFS_PARM0);
2348 PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
2351 PRVM_ERROR ("VM_precache_pic: %s: NULL", PRVM_NAME);
2353 VM_CheckEmptyString (s);
2355 // AK Draw_CachePic is supposed to always return a valid pointer
2356 if( Draw_CachePic(s, false)->tex == r_texture_notexture )
2357 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(NULL);
2367 void VM_freepic(void)
2371 VM_SAFEPARMCOUNT(1,VM_freepic);
2373 s = PRVM_G_STRING(OFS_PARM0);
2376 PRVM_ERROR ("VM_freepic: %s: NULL");
2378 VM_CheckEmptyString (s);
2387 float drawcharacter(vector position, float character, vector scale, vector rgb, float alpha, float flag)
2390 void VM_drawcharacter(void)
2392 float *pos,*scale,*rgb;
2395 VM_SAFEPARMCOUNT(6,VM_drawcharacter);
2397 character = (char) PRVM_G_FLOAT(OFS_PARM1);
2400 Con_Printf("VM_drawcharacter: %s passed null character !\n",PRVM_NAME);
2401 PRVM_G_FLOAT(OFS_RETURN) = -1;
2405 pos = PRVM_G_VECTOR(OFS_PARM0);
2406 scale = PRVM_G_VECTOR(OFS_PARM2);
2407 rgb = PRVM_G_VECTOR(OFS_PARM3);
2408 flag = (int)PRVM_G_FLOAT(OFS_PARM5);
2410 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2412 Con_Printf("VM_drawcharacter: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2413 PRVM_G_FLOAT(OFS_RETURN) = -2;
2417 if(pos[2] || scale[2])
2418 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")));
2420 if(!scale[0] || !scale[1])
2422 Con_Printf("VM_drawcharacter: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
2423 PRVM_G_FLOAT(OFS_RETURN) = -3;
2427 DrawQ_String (pos[0], pos[1], &character, 1, scale[0], scale[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM4), flag);
2428 PRVM_G_FLOAT(OFS_RETURN) = 1;
2435 float drawstring(vector position, string text, vector scale, vector rgb, float alpha, float flag)
2438 void VM_drawstring(void)
2440 float *pos,*scale,*rgb;
2443 VM_SAFEPARMCOUNT(6,VM_drawstring);
2445 string = PRVM_G_STRING(OFS_PARM1);
2448 Con_Printf("VM_drawstring: %s passed null string !\n",PRVM_NAME);
2449 PRVM_G_FLOAT(OFS_RETURN) = -1;
2453 //VM_CheckEmptyString(string); Why should it be checked - perhaps the menu wants to support the precolored letters, too?
2455 pos = PRVM_G_VECTOR(OFS_PARM0);
2456 scale = PRVM_G_VECTOR(OFS_PARM2);
2457 rgb = PRVM_G_VECTOR(OFS_PARM3);
2458 flag = (int)PRVM_G_FLOAT(OFS_PARM5);
2460 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2462 Con_Printf("VM_drawstring: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2463 PRVM_G_FLOAT(OFS_RETURN) = -2;
2467 if(!scale[0] || !scale[1])
2469 Con_Printf("VM_drawstring: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
2470 PRVM_G_FLOAT(OFS_RETURN) = -3;
2474 if(pos[2] || scale[2])
2475 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")));
2477 DrawQ_String (pos[0], pos[1], string, 0, scale[0], scale[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM4), flag);
2478 PRVM_G_FLOAT(OFS_RETURN) = 1;
2484 float drawpic(vector position, string pic, vector size, vector rgb, float alpha, float flag)
2487 void VM_drawpic(void)
2489 const char *picname;
2490 float *size, *pos, *rgb;
2493 VM_SAFEPARMCOUNT(6,VM_drawpic);
2495 picname = PRVM_G_STRING(OFS_PARM1);
2499 Con_Printf("VM_drawpic: %s passed null picture name !\n", PRVM_NAME);
2500 PRVM_G_FLOAT(OFS_RETURN) = -1;
2504 VM_CheckEmptyString (picname);
2506 // is pic cached ? no function yet for that
2509 Con_Printf("VM_drawpic: %s: %s not cached !\n", PRVM_NAME, picname);
2510 PRVM_G_FLOAT(OFS_RETURN) = -4;
2514 pos = PRVM_G_VECTOR(OFS_PARM0);
2515 size = PRVM_G_VECTOR(OFS_PARM2);
2516 rgb = PRVM_G_VECTOR(OFS_PARM3);
2517 flag = (int) PRVM_G_FLOAT(OFS_PARM5);
2519 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2521 Con_Printf("VM_drawstring: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2522 PRVM_G_FLOAT(OFS_RETURN) = -2;
2526 if(pos[2] || size[2])
2527 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")));
2529 DrawQ_Pic(pos[0], pos[1], Draw_CachePic(picname, true), size[0], size[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM4), flag);
2530 PRVM_G_FLOAT(OFS_RETURN) = 1;
2537 float drawfill(vector position, vector size, vector rgb, float alpha, float flag)
2540 void VM_drawfill(void)
2542 float *size, *pos, *rgb;
2545 VM_SAFEPARMCOUNT(5,VM_drawfill);
2548 pos = PRVM_G_VECTOR(OFS_PARM0);
2549 size = PRVM_G_VECTOR(OFS_PARM1);
2550 rgb = PRVM_G_VECTOR(OFS_PARM2);
2551 flag = (int) PRVM_G_FLOAT(OFS_PARM4);
2553 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2555 Con_Printf("VM_drawstring: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2556 PRVM_G_FLOAT(OFS_RETURN) = -2;
2560 if(pos[2] || size[2])
2561 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")));
2563 DrawQ_Pic(pos[0], pos[1], NULL, size[0], size[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM3), flag);
2564 PRVM_G_FLOAT(OFS_RETURN) = 1;
2571 drawsetcliparea(float x, float y, float width, float height)
2574 void VM_drawsetcliparea(void)
2577 VM_SAFEPARMCOUNT(4,VM_drawsetcliparea);
2579 x = bound(0, PRVM_G_FLOAT(OFS_PARM0), vid_conwidth.integer);
2580 y = bound(0, PRVM_G_FLOAT(OFS_PARM1), vid_conheight.integer);
2581 w = bound(0, PRVM_G_FLOAT(OFS_PARM2) + PRVM_G_FLOAT(OFS_PARM0) - x, (vid_conwidth.integer - x));
2582 h = bound(0, PRVM_G_FLOAT(OFS_PARM3) + PRVM_G_FLOAT(OFS_PARM1) - y, (vid_conheight.integer - y));
2584 DrawQ_SetClipArea(x, y, w, h);
2589 VM_drawresetcliparea
2594 void VM_drawresetcliparea(void)
2596 VM_SAFEPARMCOUNT(0,VM_drawresetcliparea);
2598 DrawQ_ResetClipArea();
2605 vector getimagesize(string pic)
2608 void VM_getimagesize(void)
2613 VM_SAFEPARMCOUNT(1,VM_getimagesize);
2615 p = PRVM_G_STRING(OFS_PARM0);
2618 PRVM_ERROR("VM_getimagepos: %s passed null picture name !", PRVM_NAME);
2620 VM_CheckEmptyString (p);
2622 pic = Draw_CachePic (p, false);
2624 PRVM_G_VECTOR(OFS_RETURN)[0] = pic->width;
2625 PRVM_G_VECTOR(OFS_RETURN)[1] = pic->height;
2626 PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
2633 string keynumtostring(float keynum)
2636 void VM_keynumtostring (void)
2640 VM_SAFEPARMCOUNT(1, VM_keynumtostring);
2642 keynum = (int)PRVM_G_FLOAT(OFS_PARM0);
2644 tmp = VM_GetTempString();
2646 strcpy(tmp, Key_KeynumToString(keynum));
2648 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(tmp);
2655 float stringtokeynum(string key)
2658 void VM_stringtokeynum (void)
2661 VM_SAFEPARMCOUNT( 1, VM_keynumtostring );
2663 str = PRVM_G_STRING( OFS_PARM0 );
2665 PRVM_G_INT(OFS_RETURN) = Key_StringToKeynum( str );
2668 // CL_Video interface functions
2671 ========================
2674 float cin_open(string file, string name)
2675 ========================
2677 void VM_cin_open( void )
2682 VM_SAFEPARMCOUNT( 2, VM_cin_open );
2684 file = PRVM_G_STRING( OFS_PARM0 );
2685 name = PRVM_G_STRING( OFS_PARM1 );
2687 VM_CheckEmptyString( file );
2688 VM_CheckEmptyString( name );
2690 if( CL_OpenVideo( file, name, MENUOWNER ) )
2691 PRVM_G_FLOAT( OFS_RETURN ) = 1;
2693 PRVM_G_FLOAT( OFS_RETURN ) = 0;
2697 ========================
2700 void cin_close(string name)
2701 ========================
2703 void VM_cin_close( void )
2707 VM_SAFEPARMCOUNT( 1, VM_cin_close );
2709 name = PRVM_G_STRING( OFS_PARM0 );
2710 VM_CheckEmptyString( name );
2712 CL_CloseVideo( CL_GetVideoByName( name ) );
2716 ========================
2718 void cin_setstate(string name, float type)
2719 ========================
2721 void VM_cin_setstate( void )
2724 clvideostate_t state;
2727 VM_SAFEPARMCOUNT( 2, VM_cin_netstate );
2729 name = PRVM_G_STRING( OFS_PARM0 );
2730 VM_CheckEmptyString( name );
2732 state = (clvideostate_t)((int)PRVM_G_FLOAT( OFS_PARM1 ));
2734 video = CL_GetVideoByName( name );
2735 if( video && state > CLVIDEO_UNUSED && state < CLVIDEO_STATECOUNT )
2736 CL_SetVideoState( video, state );
2740 ========================
2743 float cin_getstate(string name)
2744 ========================
2746 void VM_cin_getstate( void )
2751 VM_SAFEPARMCOUNT( 1, VM_cin_getstate );
2753 name = PRVM_G_STRING( OFS_PARM0 );
2754 VM_CheckEmptyString( name );
2756 video = CL_GetVideoByName( name );
2758 PRVM_G_FLOAT( OFS_RETURN ) = (int)video->state;
2760 PRVM_G_FLOAT( OFS_RETURN ) = 0;
2764 ========================
2767 void cin_restart(string name)
2768 ========================
2770 void VM_cin_restart( void )
2775 VM_SAFEPARMCOUNT( 1, VM_cin_restart );
2777 name = PRVM_G_STRING( OFS_PARM0 );
2778 VM_CheckEmptyString( name );
2780 video = CL_GetVideoByName( name );
2782 CL_RestartVideo( video );
2789 Writes new values for v_forward, v_up, and v_right based on the given forward vector
2790 vectorvectors(vector, vector)
2793 void VM_vectorvectors (void)
2795 VectorNormalize2(PRVM_G_VECTOR(OFS_PARM0), prog->globals.server->v_forward);
2796 VectorVectors(prog->globals.server->v_forward, prog->globals.server->v_right, prog->globals.server->v_up);
2800 ========================
2803 void drawline(float width, vector pos1, vector pos2, vector rgb, float alpha, float flags)
2804 ========================
2806 void VM_drawline (void)
2808 float *c1, *c2, *rgb;
2810 unsigned char flags;
2812 VM_SAFEPARMCOUNT(6, VM_drawline);
2813 width = PRVM_G_FLOAT(OFS_PARM0);
2814 c1 = PRVM_G_VECTOR(OFS_PARM1);
2815 c2 = PRVM_G_VECTOR(OFS_PARM2);
2816 rgb = PRVM_G_VECTOR(OFS_PARM3);
2817 alpha = PRVM_G_FLOAT(OFS_PARM4);
2818 flags = (int)PRVM_G_FLOAT(OFS_PARM5);
2819 DrawQ_Line(width, c1[0], c1[1], c2[0], c2[1], rgb[0], rgb[1], rgb[2], alpha, flags);
2822 //====================
2823 //QC POLYGON functions
2824 //====================
2829 float data[36]; //[515]: enough for polygons
2830 unsigned char flags; //[515]: + VM_POLYGON_2D and VM_POLYGON_FL4V flags
2833 //static float vm_polygon_linewidth = 1;
2834 static mempool_t *vm_polygons_pool = NULL;
2835 static unsigned char vm_current_vertices = 0;
2836 static qboolean vm_polygons_initialized = false;
2837 static vm_polygon_t *vm_polygons = NULL;
2838 static unsigned long vm_polygons_num = 0, vm_drawpolygons_num = 0; //[515]: ok long on 64bit ?
2839 static qboolean vm_polygonbegin = false; //[515]: for "no-crap-on-the-screen" check
2840 #define VM_DEFPOLYNUM 64 //[515]: enough for default ?
2842 #define VM_POLYGON_FL3V 16 //more than 2 vertices (used only for lines)
2843 #define VM_POLYGON_FLLINES 32
2844 #define VM_POLYGON_FL2D 64
2845 #define VM_POLYGON_FL4V 128 //4 vertices
2847 void VM_InitPolygons (void)
2849 vm_polygons_pool = Mem_AllocPool("VMPOLY", 0, NULL);
2850 vm_polygons = (vm_polygon_t *)Mem_Alloc(vm_polygons_pool, VM_DEFPOLYNUM*sizeof(vm_polygon_t));
2851 memset(vm_polygons, 0, VM_DEFPOLYNUM*sizeof(vm_polygon_t));
2852 vm_polygons_num = VM_DEFPOLYNUM;
2853 vm_drawpolygons_num = 0;
2854 vm_polygonbegin = false;
2855 vm_polygons_initialized = true;
2858 void VM_DrawPolygonCallback (const entity_render_t *ent, const rtlight_t *rtlight, int numsurfaces, int *surfacelist)
2860 int surfacelistindex;
2861 // LordHavoc: FIXME: this is stupid code
2862 for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
2864 const vm_polygon_t *p = &vm_polygons[surfacelist[surfacelistindex]];
2865 int flags = p->flags & 0x0f;
2867 if(flags == DRAWFLAG_ADDITIVE)
2868 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
2869 else if(flags == DRAWFLAG_MODULATE)
2870 GL_BlendFunc(GL_DST_COLOR, GL_ZERO);
2871 else if(flags == DRAWFLAG_2XMODULATE)
2872 GL_BlendFunc(GL_DST_COLOR,GL_SRC_COLOR);
2874 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
2876 R_Mesh_TexBind(0, R_GetTexture(p->tex));
2879 //[515]: is speed is max ?
2880 if(p->flags & VM_POLYGON_FLLINES) //[515]: lines
2882 qglLineWidth(p->data[13]);CHECKGLERROR
2883 qglBegin(GL_LINE_LOOP);
2884 qglTexCoord1f (p->data[12]);
2885 qglColor4f (p->data[20], p->data[21], p->data[22], p->data[23]);
2886 qglVertex3f (p->data[0] , p->data[1], p->data[2]);
2888 qglTexCoord1f (p->data[14]);
2889 qglColor4f (p->data[24], p->data[25], p->data[26], p->data[27]);
2890 qglVertex3f (p->data[3] , p->data[4], p->data[5]);
2892 if(p->flags & VM_POLYGON_FL3V)
2894 qglTexCoord1f (p->data[16]);
2895 qglColor4f (p->data[28], p->data[29], p->data[30], p->data[31]);
2896 qglVertex3f (p->data[6] , p->data[7], p->data[8]);
2898 if(p->flags & VM_POLYGON_FL4V)
2900 qglTexCoord1f (p->data[18]);
2901 qglColor4f (p->data[32], p->data[33], p->data[34], p->data[35]);
2902 qglVertex3f (p->data[9] , p->data[10], p->data[11]);
2910 qglBegin(GL_POLYGON);
2911 qglTexCoord2f (p->data[12], p->data[13]);
2912 qglColor4f (p->data[20], p->data[21], p->data[22], p->data[23]);
2913 qglVertex3f (p->data[0] , p->data[1], p->data[2]);
2915 qglTexCoord2f (p->data[14], p->data[15]);
2916 qglColor4f (p->data[24], p->data[25], p->data[26], p->data[27]);
2917 qglVertex3f (p->data[3] , p->data[4], p->data[5]);
2919 qglTexCoord2f (p->data[16], p->data[17]);
2920 qglColor4f (p->data[28], p->data[29], p->data[30], p->data[31]);
2921 qglVertex3f (p->data[6] , p->data[7], p->data[8]);
2923 if(p->flags & VM_POLYGON_FL4V)
2925 qglTexCoord2f (p->data[18], p->data[19]);
2926 qglColor4f (p->data[32], p->data[33], p->data[34], p->data[35]);
2927 qglVertex3f (p->data[9] , p->data[10], p->data[11]);
2935 void VM_AddPolygonTo2DScene (vm_polygon_t *p)
2937 drawqueuemesh_t mesh;
2938 static int picelements[6] = {0, 1, 2, 0, 2, 3};
2940 mesh.texture = p->tex;
2941 mesh.data_element3i = picelements;
2942 mesh.data_vertex3f = p->data;
2943 mesh.data_texcoord2f = p->data + 12;
2944 mesh.data_color4f = p->data + 20;
2945 if(p->flags & VM_POLYGON_FL4V)
2947 mesh.num_vertices = 4;
2948 mesh.num_triangles = 2;
2952 mesh.num_vertices = 3;
2953 mesh.num_triangles = 1;
2955 if(p->flags & VM_POLYGON_FLLINES) //[515]: lines
2956 DrawQ_LineLoop (&mesh, (p->flags&0x0f));
2958 DrawQ_Mesh (&mesh, (p->flags&0x0f));
2961 //void(string texturename, float flag, float 2d, float lines) R_BeginPolygon
2962 void VM_R_PolygonBegin (void)
2965 const char *picname;
2967 VM_SAFEPARMCOUNT(2, VM_R_PolygonBegin);
2969 if(!vm_polygons_initialized)
2973 Con_Printf("VM_R_PolygonBegin: called twice without VM_R_PolygonEnd after first\n");
2976 if(vm_drawpolygons_num >= vm_polygons_num)
2978 p = (vm_polygon_t *)Mem_Alloc(vm_polygons_pool, 2 * vm_polygons_num * sizeof(vm_polygon_t));
2979 memset(p, 0, 2 * vm_polygons_num * sizeof(vm_polygon_t));
2980 memcpy(p, vm_polygons, vm_polygons_num * sizeof(vm_polygon_t));
2981 Mem_Free(vm_polygons);
2983 vm_polygons_num *= 2;
2985 p = &vm_polygons[vm_drawpolygons_num];
2986 picname = PRVM_G_STRING(OFS_PARM0);
2988 p->tex = Draw_CachePic(picname, true)->tex;
2990 p->tex = r_texture_notexture;
2991 p->flags = (unsigned char)PRVM_G_FLOAT(OFS_PARM1);
2992 vm_current_vertices = 0;
2993 vm_polygonbegin = true;
2996 if(PRVM_G_FLOAT(OFS_PARM2))
2997 p->flags |= VM_POLYGON_FL2D;
2998 if(prog->argc >= 4 && PRVM_G_FLOAT(OFS_PARM3))
3000 p->data[13] = PRVM_G_FLOAT(OFS_PARM3); //[515]: linewidth
3001 p->flags |= VM_POLYGON_FLLINES;
3006 //void(vector org, vector texcoords, vector rgb, float alpha) R_PolygonVertex
3007 void VM_R_PolygonVertex (void)
3009 float *coords, *tx, *rgb, alpha;
3011 VM_SAFEPARMCOUNT(4, VM_R_PolygonVertex);
3013 if(!vm_polygonbegin)
3015 Con_Printf("VM_R_PolygonVertex: VM_R_PolygonBegin wasn't called\n");
3018 coords = PRVM_G_VECTOR(OFS_PARM0);
3019 tx = PRVM_G_VECTOR(OFS_PARM1);
3020 rgb = PRVM_G_VECTOR(OFS_PARM2);
3021 alpha = PRVM_G_FLOAT(OFS_PARM3);
3023 p = &vm_polygons[vm_drawpolygons_num];
3024 if(vm_current_vertices > 4)
3026 Con_Printf("VM_R_PolygonVertex: may have 4 vertices max\n");
3030 p->data[vm_current_vertices*3] = coords[0];
3031 p->data[1+vm_current_vertices*3] = coords[1];
3032 if(!(p->flags & VM_POLYGON_FL2D))
3033 p->data[2+vm_current_vertices*3] = coords[2];
3035 p->data[12+vm_current_vertices*2] = tx[0];
3036 if(!(p->flags & VM_POLYGON_FLLINES))
3037 p->data[13+vm_current_vertices*2] = tx[1];
3039 p->data[20+vm_current_vertices*4] = rgb[0];
3040 p->data[21+vm_current_vertices*4] = rgb[1];
3041 p->data[22+vm_current_vertices*4] = rgb[2];
3042 p->data[23+vm_current_vertices*4] = alpha;
3044 vm_current_vertices++;
3045 if(vm_current_vertices == 4)
3046 p->flags |= VM_POLYGON_FL4V;
3048 if(vm_current_vertices == 3)
3049 p->flags |= VM_POLYGON_FL3V;
3052 //void() R_EndPolygon
3053 void VM_R_PolygonEnd (void)
3055 if(!vm_polygonbegin)
3057 Con_Printf("VM_R_PolygonEnd: VM_R_PolygonBegin wasn't called\n");
3060 if(vm_current_vertices > 2 || (vm_current_vertices >= 2 && vm_polygons[vm_drawpolygons_num].flags & VM_POLYGON_FLLINES))
3062 if(vm_polygons[vm_drawpolygons_num].flags & VM_POLYGON_FL2D) //[515]: don't use qcpolygons memory if 2D
3063 VM_AddPolygonTo2DScene(&vm_polygons[vm_drawpolygons_num]);
3065 vm_drawpolygons_num++;
3068 Con_Printf("VM_R_PolygonEnd: %i vertices isn't a good choice\n", vm_current_vertices);
3069 vm_polygonbegin = false;
3072 void VM_AddPolygonsToMeshQueue (void)
3075 if(!vm_drawpolygons_num)
3077 for(i = 0;i < vm_drawpolygons_num;i++)
3078 R_MeshQueue_Add(VM_DrawPolygonCallback, NULL, i, NULL);
3079 vm_drawpolygons_num = 0;
3085 // float(float number, float quantity) bitshift (EXT_BITSHIFT)
3086 void VM_bitshift (void)
3089 VM_SAFEPARMCOUNT(2, VM_bitshift);
3091 n1 = (int)fabs((int)PRVM_G_FLOAT(OFS_PARM0));
3092 n2 = (int)PRVM_G_FLOAT(OFS_PARM1);
3094 PRVM_G_FLOAT(OFS_RETURN) = n1;
3097 PRVM_G_FLOAT(OFS_RETURN) = (n1 >> -n2);
3099 PRVM_G_FLOAT(OFS_RETURN) = (n1 << n2);
3102 ////////////////////////////////////////
3103 // AltString functions
3104 ////////////////////////////////////////
3107 ========================
3110 float altstr_count(string)
3111 ========================
3113 void VM_altstr_count( void )
3115 const char *altstr, *pos;
3118 VM_SAFEPARMCOUNT( 1, VM_altstr_count );
3120 altstr = PRVM_G_STRING( OFS_PARM0 );
3121 //VM_CheckEmptyString( altstr );
3123 for( count = 0, pos = altstr ; *pos ; pos++ ) {
3124 if( *pos == '\\' ) {
3128 } else if( *pos == '\'' ) {
3133 PRVM_G_FLOAT( OFS_RETURN ) = (float) (count / 2);
3137 ========================
3140 string altstr_prepare(string)
3141 ========================
3143 void VM_altstr_prepare( void )
3146 const char *instr, *in;
3149 VM_SAFEPARMCOUNT( 1, VM_altstr_prepare );
3151 instr = PRVM_G_STRING( OFS_PARM0 );
3152 //VM_CheckEmptyString( instr );
3153 outstr = VM_GetTempString();
3155 for( out = outstr, in = instr, size = VM_STRINGTEMP_LENGTH - 1 ; size && *in ; size--, in++, out++ )
3164 PRVM_G_INT( OFS_RETURN ) = PRVM_SetEngineString( outstr );
3168 ========================
3171 string altstr_get(string, float)
3172 ========================
3174 void VM_altstr_get( void )
3176 const char *altstr, *pos;
3180 VM_SAFEPARMCOUNT( 2, VM_altstr_get );
3182 altstr = PRVM_G_STRING( OFS_PARM0 );
3183 //VM_CheckEmptyString( altstr );
3185 count = (int)PRVM_G_FLOAT( OFS_PARM1 );
3186 count = count * 2 + 1;
3188 for( pos = altstr ; *pos && count ; pos++ )
3189 if( *pos == '\\' ) {
3192 } else if( *pos == '\'' )
3196 PRVM_G_INT( OFS_RETURN ) = PRVM_SetEngineString( NULL );
3200 outstr = VM_GetTempString();
3201 for( out = outstr, size = VM_STRINGTEMP_LENGTH - 1 ; size && *pos ; size--, pos++, out++ )
3202 if( *pos == '\\' ) {
3207 } else if( *pos == '\'' )
3213 PRVM_G_INT( OFS_RETURN ) = PRVM_SetEngineString( outstr );
3217 ========================
3220 string altstr_set(string altstr, float num, string set)
3221 ========================
3223 void VM_altstr_set( void )
3226 const char *altstr, *str;
3230 VM_SAFEPARMCOUNT( 3, VM_altstr_set );
3232 altstr = PRVM_G_STRING( OFS_PARM0 );
3233 //VM_CheckEmptyString( altstr );
3235 num = (int)PRVM_G_FLOAT( OFS_PARM1 );
3237 str = PRVM_G_STRING( OFS_PARM2 );
3238 //VM_CheckEmptyString( str );
3240 outstr = out = VM_GetTempString();
3241 for( num = num * 2 + 1, in = altstr; *in && num; *out++ = *in++ )
3246 } else if( *in == '\'' ) {
3251 PRVM_G_INT( OFS_RETURN ) = PRVM_SetEngineString( altstr );
3255 for( ; *str; *out++ = *str++ );
3256 // now jump over the old content
3258 if( *in == '\'' || (*in == '\\' && !*++in) )
3262 PRVM_G_INT( OFS_RETURN ) = PRVM_SetEngineString( NULL );
3267 PRVM_G_INT( OFS_RETURN ) = PRVM_SetEngineString( outstr );
3271 ========================
3274 string altstr_ins(string altstr, float num, string set)
3275 ========================
3277 void VM_altstr_ins(void)
3287 in = instr = PRVM_G_STRING( OFS_PARM0 );
3288 num = (int)PRVM_G_FLOAT( OFS_PARM1 );
3289 set = setstr = PRVM_G_STRING( OFS_PARM2 );
3291 out = outstr = VM_GetTempString();
3292 for( num = num * 2 + 2 ; *in && num > 0 ; *out++ = *in++ )
3297 } else if( *in == '\'' ) {
3302 for( ; *set ; *out++ = *set++ );
3306 PRVM_G_INT( OFS_RETURN ) = PRVM_SetEngineString( outstr );
3310 ////////////////////////////////////////
3311 // BufString functions
3312 ////////////////////////////////////////
3313 //[515]: string buffers support
3314 #define MAX_QCSTR_BUFFERS 128
3315 #define MAX_QCSTR_STRINGS 1024
3320 char *strings[MAX_QCSTR_STRINGS];
3323 static qcstrbuffer_t *qcstringbuffers[MAX_QCSTR_BUFFERS];
3324 static int num_qcstringbuffers;
3325 static int buf_sortpower;
3327 #define BUFSTR_BUFFER(a) (a>=MAX_QCSTR_BUFFERS) ? NULL : (qcstringbuffers[a])
3328 #define BUFSTR_ISFREE(a) (a<MAX_QCSTR_BUFFERS&&qcstringbuffers[a]&&qcstringbuffers[a]->num_strings<=0) ? 1 : 0
3330 static int BufStr_FindFreeBuffer (void)
3333 if(num_qcstringbuffers == MAX_QCSTR_BUFFERS)
3335 for(i=0;i<MAX_QCSTR_BUFFERS;i++)
3336 if(!qcstringbuffers[i])
3338 qcstringbuffers[i] = (qcstrbuffer_t *)Z_Malloc(sizeof(qcstrbuffer_t));
3339 memset(qcstringbuffers[i], 0, sizeof(qcstrbuffer_t));
3345 static void BufStr_ClearBuffer (int index)
3347 qcstrbuffer_t *b = qcstringbuffers[index];
3352 if(b->num_strings > 0)
3354 for(i=0;i<b->num_strings;i++)
3356 Z_Free(b->strings[i]);
3357 num_qcstringbuffers--;
3359 Z_Free(qcstringbuffers[index]);
3360 qcstringbuffers[index] = NULL;
3364 static int BufStr_FindFreeString (qcstrbuffer_t *b)
3367 for(i=0;i<b->num_strings;i++)
3368 if(!b->strings[i] || !b->strings[i][0])
3370 if(i == MAX_QCSTR_STRINGS) return -1;
3374 static int BufStr_SortStringsUP (const void *in1, const void *in2)
3377 a = *((const char **) in1);
3378 b = *((const char **) in2);
3380 if(!b[0]) return -1;
3381 return strncmp(a, b, buf_sortpower);
3384 static int BufStr_SortStringsDOWN (const void *in1, const void *in2)
3387 a = *((const char **) in1);
3388 b = *((const char **) in2);
3390 if(!b[0]) return -1;
3391 return strncmp(b, a, buf_sortpower);
3395 static void VM_BufStr_Init (void)
3397 memset(qcstringbuffers, 0, sizeof(qcstringbuffers));
3398 num_qcstringbuffers = 0;
3401 static void VM_BufStr_ShutDown (void)
3404 for(i=0;i<MAX_QCSTR_BUFFERS && num_qcstringbuffers;i++)
3405 BufStr_ClearBuffer(i);
3410 ========================
3412 creates new buffer, and returns it's index, returns -1 if failed
3413 float buf_create(void) = #460;
3414 ========================
3416 void VM_buf_create (void)
3419 VM_SAFEPARMCOUNT(0, VM_buf_create);
3420 i = BufStr_FindFreeBuffer();
3422 num_qcstringbuffers++;
3424 //Con_Printf("VM_buf_create: buffers overflow in %s\n", PRVM_NAME);
3425 PRVM_G_FLOAT(OFS_RETURN) = i;
3429 ========================
3431 deletes buffer and all strings in it
3432 void buf_del(float bufhandle) = #461;
3433 ========================
3435 void VM_buf_del (void)
3437 VM_SAFEPARMCOUNT(1, VM_buf_del);
3438 if(BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0)))
3439 BufStr_ClearBuffer((int)PRVM_G_FLOAT(OFS_PARM0));
3442 Con_Printf("VM_buf_del: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3448 ========================
3450 how many strings are stored in buffer
3451 float buf_getsize(float bufhandle) = #462;
3452 ========================
3454 void VM_buf_getsize (void)
3457 VM_SAFEPARMCOUNT(1, VM_buf_getsize);
3459 b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3462 PRVM_G_FLOAT(OFS_RETURN) = -1;
3463 Con_Printf("VM_buf_getsize: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3467 PRVM_G_FLOAT(OFS_RETURN) = b->num_strings;
3471 ========================
3473 copy all content from one buffer to another, make sure it exists
3474 void buf_copy(float bufhandle_from, float bufhandle_to) = #463;
3475 ========================
3477 void VM_buf_copy (void)
3479 qcstrbuffer_t *b1, *b2;
3481 VM_SAFEPARMCOUNT(2, VM_buf_copy);
3483 b1 = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3486 Con_Printf("VM_buf_copy: invalid source buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3489 i = (int)PRVM_G_FLOAT(OFS_PARM1);
3490 if(i == (int)PRVM_G_FLOAT(OFS_PARM0))
3492 Con_Printf("VM_buf_copy: source == destination (%i) in %s\n", i, PRVM_NAME);
3495 b2 = BUFSTR_BUFFER(i);
3498 Con_Printf("VM_buf_copy: invalid destination buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM1), PRVM_NAME);
3502 BufStr_ClearBuffer(i);
3503 qcstringbuffers[i] = (qcstrbuffer_t *)Z_Malloc(sizeof(qcstrbuffer_t));
3504 memset(qcstringbuffers[i], 0, sizeof(qcstrbuffer_t));
3505 b2->num_strings = b1->num_strings;
3507 for(i=0;i<b1->num_strings;i++)
3508 if(b1->strings[i] && b1->strings[i][0])
3510 b2->strings[i] = (char *)Z_Malloc(strlen(b1->strings[i])+1);
3513 Con_Printf("VM_buf_copy: not enough memory for buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM1), PRVM_NAME);
3516 strcpy(b2->strings[i], b1->strings[i]);
3521 ========================
3523 sort buffer by beginnings of strings (sortpower defaults it's lenght)
3524 "backward == TRUE" means that sorting goes upside-down
3525 void buf_sort(float bufhandle, float sortpower, float backward) = #464;
3526 ========================
3528 void VM_buf_sort (void)
3532 VM_SAFEPARMCOUNT(3, VM_buf_sort);
3534 b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3537 Con_Printf("VM_buf_sort: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3540 if(b->num_strings <= 0)
3542 Con_Printf("VM_buf_sort: tried to sort empty buffer %i in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3545 buf_sortpower = (int)PRVM_G_FLOAT(OFS_PARM1);
3546 if(buf_sortpower <= 0)
3547 buf_sortpower = 99999999;
3549 if(!PRVM_G_FLOAT(OFS_PARM2))
3550 qsort(b->strings, b->num_strings, sizeof(char*), BufStr_SortStringsUP);
3552 qsort(b->strings, b->num_strings, sizeof(char*), BufStr_SortStringsDOWN);
3554 for(i=b->num_strings-1;i>=0;i--) //[515]: delete empty lines
3557 if(b->strings[i][0])
3561 Z_Free(b->strings[i]);
3563 b->strings[i] = NULL;
3571 ========================
3573 concantenates all buffer string into one with "glue" separator and returns it as tempstring
3574 string buf_implode(float bufhandle, string glue) = #465;
3575 ========================
3577 void VM_buf_implode (void)
3584 VM_SAFEPARMCOUNT(2, VM_buf_implode);
3586 b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3587 PRVM_G_INT(OFS_RETURN) = 0;
3590 Con_Printf("VM_buf_implode: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3595 sep = PRVM_G_STRING(OFS_PARM1);
3596 k = VM_GetTempString();
3598 for(l=i=0;i<b->num_strings;i++)
3601 l += strlen(b->strings[i]);
3604 k = strcat(k, b->strings[i]);
3607 if(sep && (i != b->num_strings-1))
3617 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(k);
3621 ========================
3623 get a string from buffer, returns direct pointer, dont str_unzone it!
3624 string bufstr_get(float bufhandle, float string_index) = #465;
3625 ========================
3627 void VM_bufstr_get (void)
3631 VM_SAFEPARMCOUNT(2, VM_bufstr_get);
3633 b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3636 Con_Printf("VM_bufstr_get: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3639 strindex = (int)PRVM_G_FLOAT(OFS_PARM1);
3640 if(strindex < 0 || strindex > MAX_QCSTR_STRINGS)
3642 Con_Printf("VM_bufstr_get: invalid string index %i used in %s\n", strindex, PRVM_NAME);
3645 PRVM_G_INT(OFS_RETURN) = 0;
3646 if(b->num_strings <= strindex)
3648 if(b->strings[strindex])
3649 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(b->strings[strindex]);
3653 ========================
3655 copies a string into selected slot of buffer
3656 void bufstr_set(float bufhandle, float string_index, string str) = #466;
3657 ========================
3659 void VM_bufstr_set (void)
3661 int bufindex, strindex;
3665 VM_SAFEPARMCOUNT(3, VM_bufstr_set);
3667 bufindex = (int)PRVM_G_FLOAT(OFS_PARM0);
3668 b = BUFSTR_BUFFER(bufindex);
3671 Con_Printf("VM_bufstr_set: invalid buffer %i used in %s\n", bufindex, PRVM_NAME);
3674 strindex = (int)PRVM_G_FLOAT(OFS_PARM1);
3675 if(strindex < 0 || strindex > MAX_QCSTR_STRINGS)
3677 Con_Printf("VM_bufstr_set: invalid string index %i used in %s\n", strindex, PRVM_NAME);
3680 news = PRVM_G_STRING(OFS_PARM2);
3683 Con_Printf("VM_bufstr_set: null string used in %s\n", PRVM_NAME);
3686 if(b->strings[strindex])
3687 Z_Free(b->strings[strindex]);
3688 b->strings[strindex] = (char *)Z_Malloc(strlen(news)+1);
3689 strcpy(b->strings[strindex], news);
3693 ========================
3695 adds string to buffer in nearest free slot and returns it
3696 "order == TRUE" means that string will be added after last "full" slot
3697 float bufstr_add(float bufhandle, string str, float order) = #467;
3698 ========================
3700 void VM_bufstr_add (void)
3702 int bufindex, order, strindex;
3706 VM_SAFEPARMCOUNT(3, VM_bufstr_add);
3708 bufindex = (int)PRVM_G_FLOAT(OFS_PARM0);
3709 b = BUFSTR_BUFFER(bufindex);
3710 PRVM_G_FLOAT(OFS_RETURN) = -1;
3713 Con_Printf("VM_bufstr_add: invalid buffer %i used in %s\n", bufindex, PRVM_NAME);
3716 string = PRVM_G_STRING(OFS_PARM1);
3719 Con_Printf("VM_bufstr_add: null string used in %s\n", PRVM_NAME);
3723 order = (int)PRVM_G_FLOAT(OFS_PARM2);
3725 strindex = b->num_strings;
3728 strindex = BufStr_FindFreeString(b);
3731 Con_Printf("VM_bufstr_add: buffer %i has no free string slots in %s\n", bufindex, PRVM_NAME);
3736 while(b->num_strings <= strindex)
3738 if(b->num_strings == MAX_QCSTR_STRINGS)
3740 Con_Printf("VM_bufstr_add: buffer %i has no free string slots in %s\n", bufindex, PRVM_NAME);
3743 b->strings[b->num_strings] = NULL;
3746 if(b->strings[strindex])
3747 Z_Free(b->strings[strindex]);
3748 b->strings[strindex] = (char *)Z_Malloc(strlen(string)+1);
3749 strcpy(b->strings[strindex], string);
3750 PRVM_G_FLOAT(OFS_RETURN) = strindex;
3754 ========================
3756 delete string from buffer
3757 void bufstr_free(float bufhandle, float string_index) = #468;
3758 ========================
3760 void VM_bufstr_free (void)
3764 VM_SAFEPARMCOUNT(2, VM_bufstr_free);
3766 b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3769 Con_Printf("VM_bufstr_free: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3772 i = (int)PRVM_G_FLOAT(OFS_PARM1);
3773 if(i < 0 || i > MAX_QCSTR_STRINGS)
3775 Con_Printf("VM_bufstr_free: invalid string index %i used in %s\n", i, PRVM_NAME);
3779 Z_Free(b->strings[i]);
3780 b->strings[i] = NULL;
3781 if(i+1 == b->num_strings)
3787 void VM_Cmd_Init(void)
3789 // only init the stuff for the current prog
3792 // VM_BufStr_Init();
3793 if(vm_polygons_initialized)
3795 Mem_FreePool(&vm_polygons_pool);
3796 vm_polygons_initialized = false;
3800 void VM_Cmd_Reset(void)
3802 CL_PurgeOwner( MENUOWNER );
3804 VM_Files_CloseAll();
3805 // VM_BufStr_ShutDown();
3806 if(vm_polygons_initialized)
3808 Mem_FreePool(&vm_polygons_pool);
3809 vm_polygons_initialized = false;