2 // Basically every vm builtin cmd should be in here.
3 // All 3 builtin and extension lists can be found here
4 // cause large (I think they will) parts are from pr_cmds the same copyright like in pr_cmds
9 // LordHavoc: changed this to NOT use a return statement, so that it can be used in functions that must return a value
10 void VM_Warning(const char *fmt, ...)
13 char msg[MAX_INPUTLINE];
16 dpvsnprintf(msg,sizeof(msg),fmt,argptr);
24 //============================================================================
27 // temp string handling
28 // LordHavoc: added this to semi-fix the problem of using many ftos calls in a print
29 static char vm_string_temp[VM_STRINGTEMP_BUFFERS][VM_STRINGTEMP_LENGTH];
30 static int vm_string_tempindex = 0;
33 #define MAX_VMFILES 256
34 #define MAX_PRVMFILES MAX_VMFILES * PRVM_MAXPROGS
35 #define VM_FILES ((qfile_t**)(vm_files + PRVM_GetProgNr() * MAX_VMFILES))
37 qfile_t *vm_files[MAX_PRVMFILES];
39 // qc fs search handling
40 #define MAX_VMSEARCHES 128
41 #define TOTAL_VMSEARCHES MAX_VMSEARCHES * PRVM_MAXPROGS
42 #define VM_SEARCHLIST ((fssearch_t**)(vm_fssearchlist + PRVM_GetProgNr() * MAX_VMSEARCHES))
44 fssearch_t *vm_fssearchlist[TOTAL_VMSEARCHES];
46 char *VM_GetTempString(void)
49 s = vm_string_temp[vm_string_tempindex];
50 vm_string_tempindex = (vm_string_tempindex + 1) % VM_STRINGTEMP_BUFFERS;
54 void VM_CheckEmptyString (const char *s)
57 PRVM_ERROR ("%s: Bad string", PRVM_NAME);
60 //============================================================================
63 void VM_VarString(int first, char *out, int outlength)
69 outend = out + outlength - 1;
70 for (i = first;i < prog->argc && out < outend;i++)
72 s = PRVM_G_STRING((OFS_PARM0+i*3));
73 while (out < outend && *s)
83 returns true if the extension is supported by the server
85 checkextension(extensionname)
89 // kind of helper function
90 static qboolean checkextension(const char *name)
94 len = (int)strlen(name);
96 for (e = prog->extensionstring;*e;e++)
103 while (*e && *e != ' ')
105 if ((e - start) == len && !strncasecmp(start, name, len))
111 void VM_checkextension (void)
113 VM_SAFEPARMCOUNT(1,VM_checkextension);
115 PRVM_G_FLOAT(OFS_RETURN) = checkextension(PRVM_G_STRING(OFS_PARM0));
122 This is a TERMINAL error, which will kill off the entire prog.
131 char string[VM_STRINGTEMP_LENGTH];
133 VM_VarString(0, string, sizeof(string));
134 Con_Printf("======%s ERROR in %s:\n%s\n", PRVM_NAME, PRVM_GetString(prog->xfunction->s_name), string);
137 ed = PRVM_G_EDICT(prog->self->ofs);
141 PRVM_ERROR ("%s: Program error in function %s:\n%s\nTip: read above for entity information\n", PRVM_NAME, PRVM_GetString(prog->xfunction->s_name), string);
148 Dumps out self, then an error message. The program is aborted and self is
149 removed, but the level can continue.
154 void VM_objerror (void)
157 char string[VM_STRINGTEMP_LENGTH];
159 VM_VarString(0, string, sizeof(string));
160 Con_Printf("======OBJECT ERROR======\n", PRVM_NAME, PRVM_GetString(prog->xfunction->s_name), string);
163 ed = PRVM_G_EDICT (prog->self->ofs);
169 // objerror has to display the object fields -> else call
170 PRVM_ERROR ("VM_objecterror: self not defined !");
171 Con_Printf("%s OBJECT ERROR in %s:\n%s\nTip: read above for entity information\n", PRVM_NAME, PRVM_GetString(prog->xfunction->s_name), string);
176 VM_print (actually used only by client and menu)
185 char string[VM_STRINGTEMP_LENGTH];
187 VM_VarString(0, string, sizeof(string));
195 broadcast print to everyone on server
200 void VM_bprint (void)
202 char string[VM_STRINGTEMP_LENGTH];
206 VM_Warning("VM_bprint: game is not server(%s) !\n", PRVM_NAME);
210 VM_VarString(0, string, sizeof(string));
211 SV_BroadcastPrint(string);
216 VM_sprint (menu & client but only if server.active == true)
218 single print to a specific client
220 sprint(float clientnum,...[string])
223 void VM_sprint (void)
227 char string[VM_STRINGTEMP_LENGTH];
229 //find client for this entity
230 clientnum = (int)PRVM_G_FLOAT(OFS_PARM0);
231 if (!sv.active || clientnum < 0 || clientnum >= svs.maxclients || !svs.clients[clientnum].active)
233 VM_Warning("VM_sprint: %s: invalid client or server is not active !\n", PRVM_NAME);
237 client = svs.clients + clientnum;
238 if (!client->netconnection)
241 VM_VarString(1, string, sizeof(string));
242 MSG_WriteChar(&client->netconnection->message,svc_print);
243 MSG_WriteString(&client->netconnection->message, string);
250 single print to the screen
252 centerprint(clientent, value)
255 void VM_centerprint (void)
257 char string[VM_STRINGTEMP_LENGTH];
259 VM_VarString(0, string, sizeof(string));
260 SCR_CenterPrint(string);
267 vector normalize(vector)
270 void VM_normalize (void)
276 VM_SAFEPARMCOUNT(1,VM_normalize);
278 value1 = PRVM_G_VECTOR(OFS_PARM0);
280 f = VectorLength2(value1);
284 VectorScale(value1, f, newvalue);
287 VectorClear(newvalue);
289 VectorCopy (newvalue, PRVM_G_VECTOR(OFS_RETURN));
301 VM_SAFEPARMCOUNT(1,VM_vlen);
302 PRVM_G_FLOAT(OFS_RETURN) = VectorLength(PRVM_G_VECTOR(OFS_PARM0));
309 float vectoyaw(vector)
312 void VM_vectoyaw (void)
317 VM_SAFEPARMCOUNT(1,VM_vectoyaw);
319 value1 = PRVM_G_VECTOR(OFS_PARM0);
321 if (value1[1] == 0 && value1[0] == 0)
325 yaw = (int) (atan2(value1[1], value1[0]) * 180 / M_PI);
330 PRVM_G_FLOAT(OFS_RETURN) = yaw;
338 vector vectoangles(vector)
341 void VM_vectoangles (void)
347 VM_SAFEPARMCOUNT(1,VM_vectoangles);
349 value1 = PRVM_G_VECTOR(OFS_PARM0);
351 if (value1[1] == 0 && value1[0] == 0)
361 // LordHavoc: optimized a bit
364 yaw = (atan2(value1[1], value1[0]) * 180 / M_PI);
368 else if (value1[1] > 0)
373 forward = sqrt(value1[0]*value1[0] + value1[1]*value1[1]);
374 pitch = (atan2(value1[2], forward) * 180 / M_PI);
379 PRVM_G_FLOAT(OFS_RETURN+0) = pitch;
380 PRVM_G_FLOAT(OFS_RETURN+1) = yaw;
381 PRVM_G_FLOAT(OFS_RETURN+2) = 0;
388 Returns a number from 0<= num < 1
393 void VM_random (void)
395 VM_SAFEPARMCOUNT(0,VM_random);
397 PRVM_G_FLOAT(OFS_RETURN) = lhrandom(0, 1);
404 Each entity can have eight independant sound sources, like voice,
407 Channel 0 is an auto-allocate channel, the others override anything
408 already running on that entity/channel pair.
410 An attenuation of 0 will play full volume everywhere in the level.
411 Larger attenuations will drop off.
420 prvm_edict_t *entity;
424 entity = PRVM_G_EDICT(OFS_PARM0);
425 channel = PRVM_G_FLOAT(OFS_PARM1);
426 sample = PRVM_G_STRING(OFS_PARM2);
427 volume = PRVM_G_FLOAT(OFS_PARM3) * 255;
428 attenuation = PRVM_G_FLOAT(OFS_PARM4);
430 if (volume < 0 || volume > 255)
431 Host_Error ("SV_StartSound: volume = %i", volume);
433 if (attenuation < 0 || attenuation > 4)
434 Host_Error ("SV_StartSound: attenuation = %f", attenuation);
436 if (channel < 0 || channel > 7)
437 Host_Error ("SV_StartSound: channel = %i", channel);
439 SV_StartSound (entity, channel, sample, volume, attenuation);
447 localsound(string sample)
450 void VM_localsound(void)
454 VM_SAFEPARMCOUNT(1,VM_localsound);
456 s = PRVM_G_STRING(OFS_PARM0);
458 if(!S_LocalSound (s))
460 PRVM_G_FLOAT(OFS_RETURN) = -4;
461 VM_Warning("VM_localsound: Failed to play %s for %s !\n", s, PRVM_NAME);
465 PRVM_G_FLOAT(OFS_RETURN) = 1;
477 PRVM_ERROR ("%s: break statement", PRVM_NAME);
480 //============================================================================
486 Sends text over to the client's execution buffer
488 [localcmd (string, ...) or]
492 void VM_localcmd (void)
494 char string[VM_STRINGTEMP_LENGTH];
495 VM_VarString(0, string, sizeof(string));
496 Cbuf_AddText(string);
508 VM_SAFEPARMCOUNT(1,VM_cvar);
510 PRVM_G_FLOAT(OFS_RETURN) = Cvar_VariableValue(PRVM_G_STRING(OFS_PARM0));
517 const string VM_cvar_string (string)
520 void VM_cvar_string(void)
524 const char *cvar_string;
525 VM_SAFEPARMCOUNT(1,VM_cvar_string);
527 name = PRVM_G_STRING(OFS_PARM0);
530 PRVM_ERROR("VM_cvar_string: %s: null string", PRVM_NAME);
532 VM_CheckEmptyString(name);
534 out = VM_GetTempString();
536 cvar_string = Cvar_VariableString(name);
538 strcpy(out, cvar_string);
540 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(out);
545 ========================
548 const string VM_cvar_defstring (string)
549 ========================
551 void VM_cvar_defstring (void)
555 const char *cvar_string;
556 VM_SAFEPARMCOUNT(1,VM_cvar_string);
558 name = PRVM_G_STRING(OFS_PARM0);
561 PRVM_ERROR("VM_cvar_defstring: %s: null string", PRVM_NAME);
563 VM_CheckEmptyString(name);
565 out = VM_GetTempString();
567 cvar_string = Cvar_VariableDefString(name);
569 strcpy(out, cvar_string);
571 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(out);
577 void cvar_set (string,string)
580 void VM_cvar_set (void)
582 VM_SAFEPARMCOUNT(2,VM_cvar_set);
584 Cvar_Set(PRVM_G_STRING(OFS_PARM0), PRVM_G_STRING(OFS_PARM1));
594 void VM_dprint (void)
596 char string[VM_STRINGTEMP_LENGTH];
597 if (developer.integer)
599 VM_VarString(0, string, sizeof(string));
601 Con_Printf("%s", string);
603 Con_Printf("%s: %s", PRVM_NAME, string);
621 VM_SAFEPARMCOUNT(1, VM_ftos);
623 v = PRVM_G_FLOAT(OFS_PARM0);
625 s = VM_GetTempString();
626 if ((float)((int)v) == v)
627 sprintf(s, "%i", (int)v);
630 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(s);
645 VM_SAFEPARMCOUNT(1,VM_fabs);
647 v = PRVM_G_FLOAT(OFS_PARM0);
648 PRVM_G_FLOAT(OFS_RETURN) = fabs(v);
663 VM_SAFEPARMCOUNT(1,VM_vtos);
665 s = VM_GetTempString();
666 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]);
667 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(s);
682 VM_SAFEPARMCOUNT(1, VM_etos);
684 s = VM_GetTempString();
685 sprintf (s, "entity %i", PRVM_G_EDICTNUM(OFS_PARM0));
686 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(s);
693 float stof(...[string])
698 char string[VM_STRINGTEMP_LENGTH];
699 VM_VarString(0, string, sizeof(string));
700 PRVM_G_FLOAT(OFS_RETURN) = atof(string);
704 ========================
708 ========================
712 VM_SAFEPARMCOUNT(1, VM_itof);
713 PRVM_G_FLOAT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
717 ========================
721 ========================
726 VM_SAFEPARMCOUNT(1, VM_ftoi);
728 ent = (int)PRVM_G_FLOAT(OFS_PARM0);
729 if(PRVM_PROG_TO_EDICT(ent)->priv.required->free)
730 PRVM_ERROR ("VM_ftoe: %s tried to access a freed entity (entity %i)!", PRVM_NAME, ent);
732 PRVM_G_INT(OFS_RETURN) = ent;
746 prog->xfunction->builtinsprofile += 20;
747 ed = PRVM_ED_Alloc();
759 void VM_remove (void)
762 prog->xfunction->builtinsprofile += 20;
764 VM_SAFEPARMCOUNT(1, VM_remove);
766 ed = PRVM_G_EDICT(OFS_PARM0);
767 if( PRVM_NUM_FOR_EDICT(ed) <= prog->reserved_edicts )
769 if (developer.integer >= 1)
770 VM_Warning( "VM_remove: tried to remove the null entity or a reserved entity!\n" );
772 else if( ed->priv.required->free )
774 if (developer.integer >= 1)
775 VM_Warning( "VM_remove: tried to remove an already freed entity!\n" );
779 // if (ed == prog->edicts)
780 // PRVM_ERROR ("remove: tried to remove world");
781 // if (PRVM_NUM_FOR_EDICT(ed) <= sv.maxclients)
782 // Host_Error("remove: tried to remove a client");
789 entity find(entity start, .string field, string match)
800 VM_SAFEPARMCOUNT(3,VM_find);
802 e = PRVM_G_EDICTNUM(OFS_PARM0);
803 f = PRVM_G_INT(OFS_PARM1);
804 s = PRVM_G_STRING(OFS_PARM2);
806 // LordHavoc: apparently BloodMage does a find(world, weaponmodel, "") and
807 // expects it to find all the monsters, so we must be careful to support
812 for (e++ ; e < prog->num_edicts ; e++)
814 prog->xfunction->builtinsprofile++;
815 ed = PRVM_EDICT_NUM(e);
816 if (ed->priv.required->free)
818 t = PRVM_E_STRING(ed,f);
828 VM_RETURN_EDICT(prog->edicts);
835 entity findfloat(entity start, .float field, float match)
836 entity findentity(entity start, .entity field, entity match)
839 // LordHavoc: added this for searching float, int, and entity reference fields
840 void VM_findfloat (void)
847 VM_SAFEPARMCOUNT(3,VM_findfloat);
849 e = PRVM_G_EDICTNUM(OFS_PARM0);
850 f = PRVM_G_INT(OFS_PARM1);
851 s = PRVM_G_FLOAT(OFS_PARM2);
853 for (e++ ; e < prog->num_edicts ; e++)
855 prog->xfunction->builtinsprofile++;
856 ed = PRVM_EDICT_NUM(e);
857 if (ed->priv.required->free)
859 if (PRVM_E_FLOAT(ed,f) == s)
866 VM_RETURN_EDICT(prog->edicts);
873 entity findchain(.string field, string match)
876 // chained search for strings in entity fields
877 // entity(.string field, string match) findchain = #402;
878 void VM_findchain (void)
884 prvm_edict_t *ent, *chain;
886 VM_SAFEPARMCOUNT(2,VM_findchain);
888 // is the same like !(prog->flag & PRVM_FE_CHAIN) - even if the operator precedence is another
889 if(!prog->flag & PRVM_FE_CHAIN)
890 PRVM_ERROR("VM_findchain: %s doesnt have a chain field !", PRVM_NAME);
892 chain_of = PRVM_ED_FindField("chain")->ofs;
894 chain = prog->edicts;
896 f = PRVM_G_INT(OFS_PARM0);
897 s = PRVM_G_STRING(OFS_PARM1);
899 // LordHavoc: apparently BloodMage does a find(world, weaponmodel, "") and
900 // expects it to find all the monsters, so we must be careful to support
905 ent = PRVM_NEXT_EDICT(prog->edicts);
906 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
908 prog->xfunction->builtinsprofile++;
909 if (ent->priv.required->free)
911 t = PRVM_E_STRING(ent,f);
917 PRVM_E_INT(ent,chain_of) = PRVM_NUM_FOR_EDICT(chain);
921 VM_RETURN_EDICT(chain);
928 entity findchainfloat(.string field, float match)
929 entity findchainentity(.string field, entity match)
932 // LordHavoc: chained search for float, int, and entity reference fields
933 // entity(.string field, float match) findchainfloat = #403;
934 void VM_findchainfloat (void)
940 prvm_edict_t *ent, *chain;
942 VM_SAFEPARMCOUNT(2, VM_findchainfloat);
944 if(!prog->flag & PRVM_FE_CHAIN)
945 PRVM_ERROR("VM_findchainfloat: %s doesnt have a chain field !", PRVM_NAME);
947 chain_of = PRVM_ED_FindField("chain")->ofs;
949 chain = (prvm_edict_t *)prog->edicts;
951 f = PRVM_G_INT(OFS_PARM0);
952 s = PRVM_G_FLOAT(OFS_PARM1);
954 ent = PRVM_NEXT_EDICT(prog->edicts);
955 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
957 prog->xfunction->builtinsprofile++;
958 if (ent->priv.required->free)
960 if (PRVM_E_FLOAT(ent,f) != s)
963 PRVM_E_INT(ent,chain_of) = PRVM_EDICT_TO_PROG(chain);
967 VM_RETURN_EDICT(chain);
971 ========================
974 entity findflags(entity start, .float field, float match)
975 ========================
977 // LordHavoc: search for flags in float fields
978 void VM_findflags (void)
985 VM_SAFEPARMCOUNT(3, VM_findflags);
988 e = PRVM_G_EDICTNUM(OFS_PARM0);
989 f = PRVM_G_INT(OFS_PARM1);
990 s = (int)PRVM_G_FLOAT(OFS_PARM2);
992 for (e++ ; e < prog->num_edicts ; e++)
994 prog->xfunction->builtinsprofile++;
995 ed = PRVM_EDICT_NUM(e);
996 if (ed->priv.required->free)
998 if (!PRVM_E_FLOAT(ed,f))
1000 if ((int)PRVM_E_FLOAT(ed,f) & s)
1002 VM_RETURN_EDICT(ed);
1007 VM_RETURN_EDICT(prog->edicts);
1011 ========================
1014 entity findchainflags(.float field, float match)
1015 ========================
1017 // LordHavoc: chained search for flags in float fields
1018 void VM_findchainflags (void)
1024 prvm_edict_t *ent, *chain;
1026 VM_SAFEPARMCOUNT(2, VM_findchainflags);
1028 if(!prog->flag & PRVM_FE_CHAIN)
1029 PRVM_ERROR("VM_findchainflags: %s doesnt have a chain field !", PRVM_NAME);
1031 chain_of = PRVM_ED_FindField("chain")->ofs;
1033 chain = (prvm_edict_t *)prog->edicts;
1035 f = PRVM_G_INT(OFS_PARM0);
1036 s = (int)PRVM_G_FLOAT(OFS_PARM1);
1038 ent = PRVM_NEXT_EDICT(prog->edicts);
1039 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
1041 prog->xfunction->builtinsprofile++;
1042 if (ent->priv.required->free)
1044 if (!PRVM_E_FLOAT(ent,f))
1046 if (!((int)PRVM_E_FLOAT(ent,f) & s))
1049 PRVM_E_INT(ent,chain_of) = PRVM_EDICT_TO_PROG(chain);
1053 VM_RETURN_EDICT(chain);
1063 void VM_coredump (void)
1065 VM_SAFEPARMCOUNT(0,VM_coredump);
1067 Cbuf_AddText("prvm_edicts ");
1068 Cbuf_AddText(PRVM_NAME);
1079 void PRVM_StackTrace(void);
1080 void VM_stackdump (void)
1082 VM_SAFEPARMCOUNT(0, VM_stackdump);
1097 VM_SAFEPARMCOUNT(0, VM_crash);
1099 PRVM_ERROR("Crash called by %s",PRVM_NAME);
1109 void VM_traceon (void)
1111 VM_SAFEPARMCOUNT(0,VM_traceon);
1123 void VM_traceoff (void)
1125 VM_SAFEPARMCOUNT(0,VM_traceoff);
1127 prog->trace = false;
1137 void VM_eprint (void)
1139 VM_SAFEPARMCOUNT(1,VM_eprint);
1141 PRVM_ED_PrintNum (PRVM_G_EDICTNUM(OFS_PARM0));
1155 VM_SAFEPARMCOUNT(1,VM_rint);
1157 f = PRVM_G_FLOAT(OFS_PARM0);
1159 PRVM_G_FLOAT(OFS_RETURN) = (int)(f + 0.5);
1161 PRVM_G_FLOAT(OFS_RETURN) = (int)(f - 0.5);
1171 void VM_floor (void)
1173 VM_SAFEPARMCOUNT(1,VM_floor);
1175 PRVM_G_FLOAT(OFS_RETURN) = floor(PRVM_G_FLOAT(OFS_PARM0));
1187 VM_SAFEPARMCOUNT(1,VM_ceil);
1189 PRVM_G_FLOAT(OFS_RETURN) = ceil(PRVM_G_FLOAT(OFS_PARM0));
1197 entity nextent(entity)
1200 void VM_nextent (void)
1205 i = PRVM_G_EDICTNUM(OFS_PARM0);
1208 prog->xfunction->builtinsprofile++;
1210 if (i == prog->num_edicts)
1212 VM_RETURN_EDICT(prog->edicts);
1215 ent = PRVM_EDICT_NUM(i);
1216 if (!ent->priv.required->free)
1218 VM_RETURN_EDICT(ent);
1224 //=============================================================================
1231 changelevel(string map)
1234 void VM_changelevel (void)
1238 VM_SAFEPARMCOUNT(1, VM_changelevel);
1242 VM_Warning("VM_changelevel: game is not server (%s)\n", PRVM_NAME);
1246 // make sure we don't issue two changelevels
1247 if (svs.changelevel_issued)
1249 svs.changelevel_issued = true;
1251 s = PRVM_G_STRING(OFS_PARM0);
1252 Cbuf_AddText (va("changelevel %s\n",s));
1264 VM_SAFEPARMCOUNT(1,VM_sin);
1265 PRVM_G_FLOAT(OFS_RETURN) = sin(PRVM_G_FLOAT(OFS_PARM0));
1276 VM_SAFEPARMCOUNT(1,VM_cos);
1277 PRVM_G_FLOAT(OFS_RETURN) = cos(PRVM_G_FLOAT(OFS_PARM0));
1289 VM_SAFEPARMCOUNT(1,VM_sqrt);
1290 PRVM_G_FLOAT(OFS_RETURN) = sqrt(PRVM_G_FLOAT(OFS_PARM0));
1297 Returns a vector of length < 1 and > 0
1302 void VM_randomvec (void)
1307 VM_SAFEPARMCOUNT(0, VM_randomvec);
1312 temp[0] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1313 temp[1] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1314 temp[2] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1316 while (DotProduct(temp, temp) >= 1);
1317 VectorCopy (temp, PRVM_G_VECTOR(OFS_RETURN));
1320 temp[0] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1321 temp[1] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1322 temp[2] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1323 // length returned always > 0
1324 length = (rand()&32766 + 1) * (1.0 / 32767.0) / VectorLength(temp);
1325 VectorScale(temp,length, temp);*/
1326 //VectorCopy(temp, PRVM_G_VECTOR(OFS_RETURN));
1329 //=============================================================================
1335 float registercvar (string name, string value, float flags)
1338 void VM_registercvar (void)
1340 const char *name, *value;
1343 VM_SAFEPARMCOUNT(3,VM_registercvar);
1345 name = PRVM_G_STRING(OFS_PARM0);
1346 value = PRVM_G_STRING(OFS_PARM1);
1347 flags = (int)PRVM_G_FLOAT(OFS_PARM2);
1348 PRVM_G_FLOAT(OFS_RETURN) = 0;
1350 if(flags > CVAR_MAXFLAGSVAL)
1353 // first check to see if it has already been defined
1354 if (Cvar_FindVar (name))
1357 // check for overlap with a command
1358 if (Cmd_Exists (name))
1360 VM_Warning("VM_registercvar: %s is a command\n", name);
1364 Cvar_Get(name, value, flags);
1366 PRVM_G_FLOAT(OFS_RETURN) = 1; // success
1373 returns the minimum of two supplied floats
1375 float min(float a, float b, ...[float])
1380 // LordHavoc: 3+ argument enhancement suggested by FrikaC
1381 if (prog->argc == 2)
1382 PRVM_G_FLOAT(OFS_RETURN) = min(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1383 else if (prog->argc >= 3)
1386 float f = PRVM_G_FLOAT(OFS_PARM0);
1387 for (i = 1;i < prog->argc;i++)
1388 if (PRVM_G_FLOAT((OFS_PARM0+i*3)) < f)
1389 f = PRVM_G_FLOAT((OFS_PARM0+i*3));
1390 PRVM_G_FLOAT(OFS_RETURN) = f;
1393 PRVM_ERROR("VM_min: %s must supply at least 2 floats", PRVM_NAME);
1400 returns the maximum of two supplied floats
1402 float max(float a, float b, ...[float])
1407 // LordHavoc: 3+ argument enhancement suggested by FrikaC
1408 if (prog->argc == 2)
1409 PRVM_G_FLOAT(OFS_RETURN) = max(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1410 else if (prog->argc >= 3)
1413 float f = PRVM_G_FLOAT(OFS_PARM0);
1414 for (i = 1;i < prog->argc;i++)
1415 if (PRVM_G_FLOAT((OFS_PARM0+i*3)) > f)
1416 f = PRVM_G_FLOAT((OFS_PARM0+i*3));
1417 PRVM_G_FLOAT(OFS_RETURN) = f;
1420 PRVM_ERROR("VM_max: %s must supply at least 2 floats", PRVM_NAME);
1427 returns number bounded by supplied range
1429 float bound(float min, float value, float max)
1432 void VM_bound (void)
1434 VM_SAFEPARMCOUNT(3,VM_bound);
1435 PRVM_G_FLOAT(OFS_RETURN) = bound(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1), PRVM_G_FLOAT(OFS_PARM2));
1442 returns a raised to power b
1444 float pow(float a, float b)
1449 VM_SAFEPARMCOUNT(2,VM_pow);
1450 PRVM_G_FLOAT(OFS_RETURN) = pow(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1457 copies data from one entity to another
1459 copyentity(entity src, entity dst)
1462 void VM_copyentity (void)
1464 prvm_edict_t *in, *out;
1465 VM_SAFEPARMCOUNT(2,VM_copyentity);
1466 in = PRVM_G_EDICT(OFS_PARM0);
1467 out = PRVM_G_EDICT(OFS_PARM1);
1468 memcpy(out->fields.vp, in->fields.vp, prog->progs->entityfields * 4);
1475 sets the color of a client and broadcasts the update to all connected clients
1477 setcolor(clientent, value)
1480 /*void PF_setcolor (void)
1486 entnum = PRVM_G_EDICTNUM(OFS_PARM0);
1487 i = PRVM_G_FLOAT(OFS_PARM1);
1489 if (entnum < 1 || entnum > svs.maxclients || !svs.clients[entnum-1].active)
1491 Con_Print("tried to setcolor a non-client\n");
1495 client = svs.clients + entnum-1;
1496 if ((val = PRVM_GETEDICTFIELDVALUE(client->edict, eval_clientcolors)))
1499 client->old_colors = i;
1500 client->edict->fields.server->team = (i & 15) + 1;
1502 MSG_WriteByte (&sv.reliable_datagram, svc_updatecolors);
1503 MSG_WriteByte (&sv.reliable_datagram, entnum - 1);
1504 MSG_WriteByte (&sv.reliable_datagram, i);
1507 void VM_Files_Init(void)
1509 memset(VM_FILES, 0, sizeof(qfile_t*[MAX_VMFILES]));
1512 void VM_Files_CloseAll(void)
1515 for (i = 0;i < MAX_VMFILES;i++)
1518 FS_Close(VM_FILES[i]);
1519 //VM_FILES[i] = NULL;
1521 memset(VM_FILES,0,sizeof(qfile_t*[MAX_VMFILES])); // this should be faster (is it ?)
1524 qfile_t *VM_GetFileHandle( int index )
1526 if (index < 0 || index >= MAX_VMFILES)
1528 Con_Printf("VM_GetFileHandle: invalid file handle %i used in %s\n", index, PRVM_NAME);
1531 if (VM_FILES[index] == NULL)
1533 Con_Printf("VM_GetFileHandle: no such file handle %i (or file has been closed) in %s\n", index, PRVM_NAME);
1536 return VM_FILES[index];
1543 float fopen(string filename, float mode)
1546 // float(string filename, float mode) fopen = #110;
1547 // opens a file inside quake/gamedir/data/ (mode is FILE_READ, FILE_APPEND, or FILE_WRITE),
1548 // returns fhandle >= 0 if successful, or fhandle < 0 if unable to open file for any reason
1552 const char *modestring, *filename;
1554 VM_SAFEPARMCOUNT(2,VM_fopen);
1556 for (filenum = 0;filenum < MAX_VMFILES;filenum++)
1557 if (VM_FILES[filenum] == NULL)
1559 if (filenum >= MAX_VMFILES)
1561 PRVM_G_FLOAT(OFS_RETURN) = -2;
1562 VM_Warning("VM_fopen: %s ran out of file handles (%i)\n", PRVM_NAME, MAX_VMFILES);
1565 mode = (int)PRVM_G_FLOAT(OFS_PARM1);
1568 case 0: // FILE_READ
1571 case 1: // FILE_APPEND
1574 case 2: // FILE_WRITE
1578 PRVM_G_FLOAT(OFS_RETURN) = -3;
1579 VM_Warning("VM_fopen: %s: no such mode %i (valid: 0 = read, 1 = append, 2 = write)\n", PRVM_NAME, mode);
1582 filename = PRVM_G_STRING(OFS_PARM0);
1584 VM_FILES[filenum] = FS_Open(va("data/%s", filename), modestring, false, false);
1585 if (VM_FILES[filenum] == NULL && mode == 0)
1586 VM_FILES[filenum] = FS_Open(va("%s", filename), modestring, false, false);
1588 if (VM_FILES[filenum] == NULL)
1590 PRVM_G_FLOAT(OFS_RETURN) = -1;
1591 if (developer.integer >= 10)
1592 VM_Warning("VM_fopen: %s: %s mode %s failed\n", PRVM_NAME, filename, modestring);
1596 PRVM_G_FLOAT(OFS_RETURN) = filenum;
1597 if (developer.integer >= 10)
1598 VM_Warning("VM_fopen: %s: %s mode %s opened as #%i\n", PRVM_NAME, filename, modestring, filenum);
1606 fclose(float fhandle)
1609 //void(float fhandle) fclose = #111; // closes a file
1610 void VM_fclose(void)
1614 VM_SAFEPARMCOUNT(1,VM_fclose);
1616 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1617 if (filenum < 0 || filenum >= MAX_VMFILES)
1619 VM_Warning("VM_fclose: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1622 if (VM_FILES[filenum] == NULL)
1624 VM_Warning("VM_fclose: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1627 FS_Close(VM_FILES[filenum]);
1628 VM_FILES[filenum] = NULL;
1629 if (developer.integer >= 10)
1630 VM_Warning("VM_fclose: %s: #%i closed\n", PRVM_NAME, filenum);
1637 string fgets(float fhandle)
1640 //string(float fhandle) fgets = #112; // reads a line of text from the file and returns as a tempstring
1644 static char string[VM_STRINGTEMP_LENGTH];
1647 VM_SAFEPARMCOUNT(1,VM_fgets);
1649 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1650 if (filenum < 0 || filenum >= MAX_VMFILES)
1652 VM_Warning("VM_fgets: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1655 if (VM_FILES[filenum] == NULL)
1657 VM_Warning("VM_fgets: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1663 c = FS_Getc(VM_FILES[filenum]);
1664 if (c == '\r' || c == '\n' || c < 0)
1666 if (end < VM_STRINGTEMP_LENGTH - 1)
1670 // remove \n following \r
1673 c = FS_Getc(VM_FILES[filenum]);
1675 FS_UnGetc(VM_FILES[filenum], (unsigned char)c);
1677 if (developer.integer >= 100)
1678 Con_Printf("fgets: %s: %s\n", PRVM_NAME, string);
1680 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(string);
1682 PRVM_G_INT(OFS_RETURN) = 0;
1689 fputs(float fhandle, string s)
1692 //void(float fhandle, string s) fputs = #113; // writes a line of text to the end of the file
1696 char string[VM_STRINGTEMP_LENGTH];
1699 VM_SAFEPARMCOUNT(2,VM_fputs);
1701 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1702 if (filenum < 0 || filenum >= MAX_VMFILES)
1704 VM_Warning("VM_fputs: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1707 if (VM_FILES[filenum] == NULL)
1709 VM_Warning("VM_fputs: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1712 VM_VarString(1, string, sizeof(string));
1713 if ((stringlength = (int)strlen(string)))
1714 FS_Write(VM_FILES[filenum], string, stringlength);
1715 if (developer.integer >= 100)
1716 Con_Printf("fputs: %s: %s\n", PRVM_NAME, string);
1723 float strlen(string s)
1726 //float(string s) strlen = #114; // returns how many characters are in a string
1727 void VM_strlen(void)
1731 VM_SAFEPARMCOUNT(1,VM_strlen);
1733 s = PRVM_G_STRING(OFS_PARM0);
1735 PRVM_G_FLOAT(OFS_RETURN) = strlen(s);
1737 PRVM_G_FLOAT(OFS_RETURN) = 0;
1744 string strcat(string,string,...[string])
1747 //string(string s1, string s2) strcat = #115;
1748 // concatenates two strings (for example "abc", "def" would return "abcdef")
1749 // and returns as a tempstring
1750 void VM_strcat(void)
1755 PRVM_ERROR("VM_strcat wrong parameter count (min. 1 expected ) !");
1757 s = VM_GetTempString();
1758 VM_VarString(0, s, VM_STRINGTEMP_LENGTH);
1759 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(s);
1766 string substring(string s, float start, float length)
1769 // string(string s, float start, float length) substring = #116;
1770 // returns a section of a string as a tempstring
1771 void VM_substring(void)
1773 int i, start, length;
1777 VM_SAFEPARMCOUNT(3,VM_substring);
1779 string = VM_GetTempString();
1780 s = PRVM_G_STRING(OFS_PARM0);
1781 start = (int)PRVM_G_FLOAT(OFS_PARM1);
1782 length = (int)PRVM_G_FLOAT(OFS_PARM2);
1785 for (i = 0;i < start && *s;i++, s++);
1786 for (i = 0;i < VM_STRINGTEMP_LENGTH - 1 && *s && i < length;i++, s++)
1789 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(string);
1796 vector stov(string s)
1799 //vector(string s) stov = #117; // returns vector value from a string
1802 char string[VM_STRINGTEMP_LENGTH];
1804 VM_SAFEPARMCOUNT(1,VM_stov);
1806 VM_VarString(0, string, sizeof(string));
1807 Math_atov(string, PRVM_G_VECTOR(OFS_RETURN));
1814 string strzone(string s)
1817 //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)
1818 void VM_strzone(void)
1821 char string[VM_STRINGTEMP_LENGTH];
1823 VM_SAFEPARMCOUNT(1,VM_strzone);
1825 VM_VarString(0, string, sizeof(string));
1826 PRVM_G_INT(OFS_RETURN) = PRVM_AllocString(strlen(string) + 1, &out);
1827 strcpy(out, string);
1837 //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!!!)
1838 void VM_strunzone(void)
1840 VM_SAFEPARMCOUNT(1,VM_strunzone);
1841 PRVM_FreeString(PRVM_G_INT(OFS_PARM0));
1846 VM_command (used by client and menu)
1848 clientcommand(float client, string s) (for client and menu)
1851 //void(entity e, string s) clientcommand = #440; // executes a command string as if it came from the specified client
1852 //this function originally written by KrimZon, made shorter by LordHavoc
1853 void VM_clcommand (void)
1855 client_t *temp_client;
1858 VM_SAFEPARMCOUNT(2,VM_clcommand);
1860 i = (int)PRVM_G_FLOAT(OFS_PARM0);
1861 if (!sv.active || i < 0 || i >= svs.maxclients || !svs.clients[i].active)
1863 VM_Warning("VM_clientcommand: %s: invalid client/server is not active !\n", PRVM_NAME);
1867 temp_client = host_client;
1868 host_client = svs.clients + i;
1869 Cmd_ExecuteString (PRVM_G_STRING(OFS_PARM1), src_client);
1870 host_client = temp_client;
1878 float tokenize(string s)
1881 //float(string s) tokenize = #441; // takes apart a string into individal words (access them with argv), returns how many
1882 //this function originally written by KrimZon, made shorter by LordHavoc
1883 //20040203: rewritten by LordHavoc (no longer uses allocations)
1885 char *tokens[256], tokenbuf[MAX_INPUTLINE];
1886 void VM_tokenize (void)
1891 VM_SAFEPARMCOUNT(1,VM_tokenize);
1893 p = PRVM_G_STRING(OFS_PARM0);
1897 while(COM_ParseToken(&p, false))
1899 if (num_tokens >= (int)(sizeof(tokens)/sizeof(tokens[0])))
1901 if (pos + strlen(com_token) + 1 > sizeof(tokenbuf))
1903 tokens[num_tokens++] = tokenbuf + pos;
1904 strcpy(tokenbuf + pos, com_token);
1905 pos += strlen(com_token) + 1;
1908 PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
1911 //string(float n) argv = #442; // returns a word from the tokenized string (returns nothing for an invalid index)
1912 //this function originally written by KrimZon, made shorter by LordHavoc
1917 VM_SAFEPARMCOUNT(1,VM_argv);
1919 token_num = (int)PRVM_G_FLOAT(OFS_PARM0);
1921 if (token_num >= 0 && token_num < num_tokens)
1922 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(tokens[token_num]);
1924 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(NULL);
1928 //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)
1929 void PF_setattachment (void)
1931 prvm_edict_t *e = PRVM_G_EDICT(OFS_PARM0);
1932 prvm_edict_t *tagentity = PRVM_G_EDICT(OFS_PARM1);
1933 char *tagname = PRVM_G_STRING(OFS_PARM2);
1938 if (tagentity == NULL)
1939 tagentity = prog->edicts;
1941 v = PRVM_GETEDICTFIELDVALUE(e, eval_tag_entity);
1943 fields.server->edict = PRVM_EDICT_TO_PROG(tagentity);
1945 v = PRVM_GETEDICTFIELDVALUE(e, eval_tag_index);
1947 fields.server->_float = 0;
1948 if (tagentity != NULL && tagentity != prog->edicts && tagname && tagname[0])
1950 modelindex = (int)tagentity->fields.server->modelindex;
1951 if (modelindex >= 0 && modelindex < MAX_MODELS)
1953 model = sv.models[modelindex];
1954 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)
1955 for (i = 0;i < model->data_overridetagnamesforskin[(unsigned int)tagentity->fields.server->skin].num_overridetagnames;i++)
1956 if (!strcmp(tagname, model->data_overridetagnamesforskin[(unsigned int)tagentity->fields.server->skin].data_overridetagnames[i].name))
1957 fields.server->_float = i + 1;
1958 // FIXME: use a model function to get tag info (need to handle skeletal)
1959 if (fields.server->_float == 0 && model->num_tags)
1960 for (i = 0;i < model->num_tags;i++)
1961 if (!strcmp(tagname, model->data_tags[i].name))
1962 fields.server->_float = i + 1;
1963 if (fields.server->_float == 0)
1964 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);
1967 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));
1978 void VM_isserver(void)
1980 VM_SAFEPARMCOUNT(0,VM_serverstate);
1982 PRVM_G_FLOAT(OFS_RETURN) = sv.active;
1992 void VM_clientcount(void)
1994 VM_SAFEPARMCOUNT(0,VM_clientcount);
1996 PRVM_G_FLOAT(OFS_RETURN) = svs.maxclients;
2006 void VM_clientstate(void)
2008 VM_SAFEPARMCOUNT(0,VM_clientstate);
2010 PRVM_G_FLOAT(OFS_RETURN) = cls.state;
2017 float getostype(void)
2019 */ // not used at the moment -> not included in the common list
2020 void VM_getostype(void)
2022 VM_SAFEPARMCOUNT(0,VM_getostype);
2027 OS_MAC - not supported
2031 PRVM_G_FLOAT(OFS_RETURN) = 0;
2032 #elif defined(MACOSX)
2033 PRVM_G_FLOAT(OFS_RETURN) = 2;
2035 PRVM_G_FLOAT(OFS_RETURN) = 1;
2043 vector getmousepos()
2046 void VM_getmousepos(void)
2049 VM_SAFEPARMCOUNT(0,VM_getmousepos);
2051 PRVM_G_VECTOR(OFS_RETURN)[0] = in_mouse_x * vid_conwidth.integer / vid.width;
2052 PRVM_G_VECTOR(OFS_RETURN)[1] = in_mouse_y * vid_conheight.integer / vid.height;
2053 PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
2063 void VM_gettime(void)
2065 VM_SAFEPARMCOUNT(0,VM_gettime);
2067 PRVM_G_FLOAT(OFS_RETURN) = (float) *prog->time;
2074 loadfromdata(string data)
2077 void VM_loadfromdata(void)
2079 VM_SAFEPARMCOUNT(1,VM_loadentsfromfile);
2081 PRVM_ED_LoadFromFile(PRVM_G_STRING(OFS_PARM0));
2085 ========================
2088 parseentitydata(entity ent, string data)
2089 ========================
2091 void VM_parseentitydata(void)
2096 VM_SAFEPARMCOUNT(2, VM_parseentitydata);
2098 // get edict and test it
2099 ent = PRVM_G_EDICT(OFS_PARM0);
2100 if (ent->priv.required->free)
2101 PRVM_ERROR ("VM_parseentitydata: %s: Can only set already spawned entities (entity %i is free)!", PRVM_NAME, PRVM_NUM_FOR_EDICT(ent));
2103 data = PRVM_G_STRING(OFS_PARM1);
2105 // parse the opening brace
2106 if (!COM_ParseTokenConsole(&data) || com_token[0] != '{' )
2107 PRVM_ERROR ("VM_parseentitydata: %s: Couldn't parse entity data:\n%s", PRVM_NAME, data );
2109 PRVM_ED_ParseEdict (data, ent);
2116 loadfromfile(string file)
2119 void VM_loadfromfile(void)
2121 const char *filename;
2124 VM_SAFEPARMCOUNT(1,VM_loadfromfile);
2126 filename = PRVM_G_STRING(OFS_PARM0);
2127 // .. is parent directory on many platforms
2128 // / is parent directory on Amiga
2129 // : is root of drive on Amiga (also used as a directory separator on Mac, but / works there too, so that's a bad idea)
2130 // \ is a windows-ism (so it's naughty to use it, / works on all platforms)
2131 if ((filename[0] == '.' && filename[1] == '.') || filename[0] == '/' || strrchr(filename, ':') || strrchr(filename, '\\'))
2133 PRVM_G_FLOAT(OFS_RETURN) = -4;
2134 VM_Warning("VM_loadfromfile: %s dangerous or non-portable filename \"%s\" not allowed. (contains : or \\ or begins with .. or /)\n", PRVM_NAME, filename);
2138 // not conform with VM_fopen
2139 data = (char *)FS_LoadFile(filename, tempmempool, false, NULL);
2141 PRVM_G_FLOAT(OFS_RETURN) = -1;
2143 PRVM_ED_LoadFromFile(data);
2154 float mod(float val, float m)
2157 void VM_modulo(void)
2160 VM_SAFEPARMCOUNT(2,VM_module);
2162 val = (int) PRVM_G_FLOAT(OFS_PARM0);
2163 m = (int) PRVM_G_FLOAT(OFS_PARM1);
2165 PRVM_G_FLOAT(OFS_RETURN) = (float) (val % m);
2168 void VM_Search_Init(void)
2170 memset(VM_SEARCHLIST,0,sizeof(fssearch_t*[MAX_VMSEARCHES]));
2173 void VM_Search_Reset(void)
2176 // reset the fssearch list
2177 for(i = 0; i < MAX_VMSEARCHES; i++)
2178 if(VM_SEARCHLIST[i])
2179 FS_FreeSearch(VM_SEARCHLIST[i]);
2180 memset(VM_SEARCHLIST,0,sizeof(fssearch_t*[MAX_VMSEARCHES]));
2187 float search_begin(string pattern, float caseinsensitive, float quiet)
2190 void VM_search_begin(void)
2193 const char *pattern;
2194 int caseinsens, quiet;
2196 VM_SAFEPARMCOUNT(3, VM_search_begin);
2198 pattern = PRVM_G_STRING(OFS_PARM0);
2200 VM_CheckEmptyString(pattern);
2202 caseinsens = (int)PRVM_G_FLOAT(OFS_PARM1);
2203 quiet = (int)PRVM_G_FLOAT(OFS_PARM2);
2205 for(handle = 0; handle < MAX_VMSEARCHES; handle++)
2206 if(!VM_SEARCHLIST[handle])
2209 if(handle >= MAX_VMSEARCHES)
2211 PRVM_G_FLOAT(OFS_RETURN) = -2;
2212 VM_Warning("VM_search_begin: %s ran out of search handles (%i)\n", PRVM_NAME, MAX_VMSEARCHES);
2216 if(!(VM_SEARCHLIST[handle] = FS_Search(pattern,caseinsens, quiet)))
2217 PRVM_G_FLOAT(OFS_RETURN) = -1;
2219 PRVM_G_FLOAT(OFS_RETURN) = handle;
2226 void search_end(float handle)
2229 void VM_search_end(void)
2232 VM_SAFEPARMCOUNT(1, VM_search_end);
2234 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
2236 if(handle < 0 || handle >= MAX_VMSEARCHES)
2238 VM_Warning("VM_search_end: invalid handle %i used in %s\n", handle, PRVM_NAME);
2241 if(VM_SEARCHLIST[handle] == NULL)
2243 VM_Warning("VM_search_end: no such handle %i in %s\n", handle, PRVM_NAME);
2247 FS_FreeSearch(VM_SEARCHLIST[handle]);
2248 VM_SEARCHLIST[handle] = NULL;
2255 float search_getsize(float handle)
2258 void VM_search_getsize(void)
2261 VM_SAFEPARMCOUNT(1, VM_M_search_getsize);
2263 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
2265 if(handle < 0 || handle >= MAX_VMSEARCHES)
2267 VM_Warning("VM_search_getsize: invalid handle %i used in %s\n", handle, PRVM_NAME);
2270 if(VM_SEARCHLIST[handle] == NULL)
2272 VM_Warning("VM_search_getsize: no such handle %i in %s\n", handle, PRVM_NAME);
2276 PRVM_G_FLOAT(OFS_RETURN) = VM_SEARCHLIST[handle]->numfilenames;
2281 VM_search_getfilename
2283 string search_getfilename(float handle, float num)
2286 void VM_search_getfilename(void)
2288 int handle, filenum;
2290 VM_SAFEPARMCOUNT(2, VM_search_getfilename);
2292 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
2293 filenum = (int)PRVM_G_FLOAT(OFS_PARM1);
2295 if(handle < 0 || handle >= MAX_VMSEARCHES)
2297 VM_Warning("VM_search_getfilename: invalid handle %i used in %s\n", handle, PRVM_NAME);
2300 if(VM_SEARCHLIST[handle] == NULL)
2302 VM_Warning("VM_search_getfilename: no such handle %i in %s\n", handle, PRVM_NAME);
2305 if(filenum < 0 || filenum >= VM_SEARCHLIST[handle]->numfilenames)
2307 VM_Warning("VM_search_getfilename: invalid filenum %i in %s\n", filenum, PRVM_NAME);
2311 tmp = VM_GetTempString();
2312 strcpy(tmp, VM_SEARCHLIST[handle]->filenames[filenum]);
2314 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(tmp);
2321 string chr(float ascii)
2327 VM_SAFEPARMCOUNT(1, VM_chr);
2329 tmp = VM_GetTempString();
2330 tmp[0] = (unsigned char) PRVM_G_FLOAT(OFS_PARM0);
2333 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(tmp);
2336 //=============================================================================
2337 // Draw builtins (client & menu)
2343 float iscachedpic(string pic)
2346 void VM_iscachedpic(void)
2348 VM_SAFEPARMCOUNT(1,VM_iscachedpic);
2350 // drawq hasnt such a function, thus always return true
2351 PRVM_G_FLOAT(OFS_RETURN) = false;
2358 string precache_pic(string pic)
2361 void VM_precache_pic(void)
2365 VM_SAFEPARMCOUNT(1, VM_precache_pic);
2367 s = PRVM_G_STRING(OFS_PARM0);
2368 PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
2371 PRVM_ERROR ("VM_precache_pic: %s: NULL", PRVM_NAME);
2373 VM_CheckEmptyString (s);
2375 // AK Draw_CachePic is supposed to always return a valid pointer
2376 if( Draw_CachePic(s, false)->tex == r_texture_notexture )
2377 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(NULL);
2387 void VM_freepic(void)
2391 VM_SAFEPARMCOUNT(1,VM_freepic);
2393 s = PRVM_G_STRING(OFS_PARM0);
2396 PRVM_ERROR ("VM_freepic: %s: NULL");
2398 VM_CheckEmptyString (s);
2407 float drawcharacter(vector position, float character, vector scale, vector rgb, float alpha, float flag)
2410 void VM_drawcharacter(void)
2412 float *pos,*scale,*rgb;
2415 VM_SAFEPARMCOUNT(6,VM_drawcharacter);
2417 character = (char) PRVM_G_FLOAT(OFS_PARM1);
2420 PRVM_G_FLOAT(OFS_RETURN) = -1;
2421 VM_Warning("VM_drawcharacter: %s passed null character !\n",PRVM_NAME);
2425 pos = PRVM_G_VECTOR(OFS_PARM0);
2426 scale = PRVM_G_VECTOR(OFS_PARM2);
2427 rgb = PRVM_G_VECTOR(OFS_PARM3);
2428 flag = (int)PRVM_G_FLOAT(OFS_PARM5);
2430 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2432 PRVM_G_FLOAT(OFS_RETURN) = -2;
2433 VM_Warning("VM_drawcharacter: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2437 if(pos[2] || scale[2])
2438 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")));
2440 if(!scale[0] || !scale[1])
2442 PRVM_G_FLOAT(OFS_RETURN) = -3;
2443 VM_Warning("VM_drawcharacter: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
2447 DrawQ_String (pos[0], pos[1], &character, 1, scale[0], scale[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM4), flag);
2448 PRVM_G_FLOAT(OFS_RETURN) = 1;
2455 float drawstring(vector position, string text, vector scale, vector rgb, float alpha, float flag)
2458 void VM_drawstring(void)
2460 float *pos,*scale,*rgb;
2463 VM_SAFEPARMCOUNT(6,VM_drawstring);
2465 string = PRVM_G_STRING(OFS_PARM1);
2468 PRVM_G_FLOAT(OFS_RETURN) = -1;
2469 VM_Warning("VM_drawstring: %s passed null string !\n",PRVM_NAME);
2473 //VM_CheckEmptyString(string); Why should it be checked - perhaps the menu wants to support the precolored letters, too?
2475 pos = PRVM_G_VECTOR(OFS_PARM0);
2476 scale = PRVM_G_VECTOR(OFS_PARM2);
2477 rgb = PRVM_G_VECTOR(OFS_PARM3);
2478 flag = (int)PRVM_G_FLOAT(OFS_PARM5);
2480 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2482 PRVM_G_FLOAT(OFS_RETURN) = -2;
2483 VM_Warning("VM_drawstring: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2487 if(!scale[0] || !scale[1])
2489 PRVM_G_FLOAT(OFS_RETURN) = -3;
2490 VM_Warning("VM_drawstring: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
2494 if(pos[2] || scale[2])
2495 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")));
2497 DrawQ_String (pos[0], pos[1], string, 0, scale[0], scale[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM4), flag);
2498 PRVM_G_FLOAT(OFS_RETURN) = 1;
2504 float drawpic(vector position, string pic, vector size, vector rgb, float alpha, float flag)
2507 void VM_drawpic(void)
2509 const char *picname;
2510 float *size, *pos, *rgb;
2513 VM_SAFEPARMCOUNT(6,VM_drawpic);
2515 picname = PRVM_G_STRING(OFS_PARM1);
2519 PRVM_G_FLOAT(OFS_RETURN) = -1;
2520 VM_Warning("VM_drawpic: %s passed null picture name !\n", PRVM_NAME);
2524 VM_CheckEmptyString (picname);
2526 // is pic cached ? no function yet for that
2529 PRVM_G_FLOAT(OFS_RETURN) = -4;
2530 VM_Warning("VM_drawpic: %s: %s not cached !\n", PRVM_NAME, picname);
2534 pos = PRVM_G_VECTOR(OFS_PARM0);
2535 size = PRVM_G_VECTOR(OFS_PARM2);
2536 rgb = PRVM_G_VECTOR(OFS_PARM3);
2537 flag = (int) PRVM_G_FLOAT(OFS_PARM5);
2539 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2541 PRVM_G_FLOAT(OFS_RETURN) = -2;
2542 VM_Warning("VM_drawstring: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2546 if(pos[2] || size[2])
2547 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")));
2549 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);
2550 PRVM_G_FLOAT(OFS_RETURN) = 1;
2557 float drawfill(vector position, vector size, vector rgb, float alpha, float flag)
2560 void VM_drawfill(void)
2562 float *size, *pos, *rgb;
2565 VM_SAFEPARMCOUNT(5,VM_drawfill);
2568 pos = PRVM_G_VECTOR(OFS_PARM0);
2569 size = PRVM_G_VECTOR(OFS_PARM1);
2570 rgb = PRVM_G_VECTOR(OFS_PARM2);
2571 flag = (int) PRVM_G_FLOAT(OFS_PARM4);
2573 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2575 PRVM_G_FLOAT(OFS_RETURN) = -2;
2576 VM_Warning("VM_drawstring: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2580 if(pos[2] || size[2])
2581 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")));
2583 DrawQ_Pic(pos[0], pos[1], NULL, size[0], size[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM3), flag);
2584 PRVM_G_FLOAT(OFS_RETURN) = 1;
2591 drawsetcliparea(float x, float y, float width, float height)
2594 void VM_drawsetcliparea(void)
2597 VM_SAFEPARMCOUNT(4,VM_drawsetcliparea);
2599 x = bound(0, PRVM_G_FLOAT(OFS_PARM0), vid_conwidth.integer);
2600 y = bound(0, PRVM_G_FLOAT(OFS_PARM1), vid_conheight.integer);
2601 w = bound(0, PRVM_G_FLOAT(OFS_PARM2) + PRVM_G_FLOAT(OFS_PARM0) - x, (vid_conwidth.integer - x));
2602 h = bound(0, PRVM_G_FLOAT(OFS_PARM3) + PRVM_G_FLOAT(OFS_PARM1) - y, (vid_conheight.integer - y));
2604 DrawQ_SetClipArea(x, y, w, h);
2609 VM_drawresetcliparea
2614 void VM_drawresetcliparea(void)
2616 VM_SAFEPARMCOUNT(0,VM_drawresetcliparea);
2618 DrawQ_ResetClipArea();
2625 vector getimagesize(string pic)
2628 void VM_getimagesize(void)
2633 VM_SAFEPARMCOUNT(1,VM_getimagesize);
2635 p = PRVM_G_STRING(OFS_PARM0);
2638 PRVM_ERROR("VM_getimagepos: %s passed null picture name !", PRVM_NAME);
2640 VM_CheckEmptyString (p);
2642 pic = Draw_CachePic (p, false);
2644 PRVM_G_VECTOR(OFS_RETURN)[0] = pic->width;
2645 PRVM_G_VECTOR(OFS_RETURN)[1] = pic->height;
2646 PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
2653 string keynumtostring(float keynum)
2656 void VM_keynumtostring (void)
2660 VM_SAFEPARMCOUNT(1, VM_keynumtostring);
2662 keynum = (int)PRVM_G_FLOAT(OFS_PARM0);
2664 tmp = VM_GetTempString();
2666 strcpy(tmp, Key_KeynumToString(keynum));
2668 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(tmp);
2675 float stringtokeynum(string key)
2678 void VM_stringtokeynum (void)
2681 VM_SAFEPARMCOUNT( 1, VM_keynumtostring );
2683 str = PRVM_G_STRING( OFS_PARM0 );
2685 PRVM_G_INT(OFS_RETURN) = Key_StringToKeynum( str );
2688 // CL_Video interface functions
2691 ========================
2694 float cin_open(string file, string name)
2695 ========================
2697 void VM_cin_open( void )
2702 VM_SAFEPARMCOUNT( 2, VM_cin_open );
2704 file = PRVM_G_STRING( OFS_PARM0 );
2705 name = PRVM_G_STRING( OFS_PARM1 );
2707 VM_CheckEmptyString( file );
2708 VM_CheckEmptyString( name );
2710 if( CL_OpenVideo( file, name, MENUOWNER ) )
2711 PRVM_G_FLOAT( OFS_RETURN ) = 1;
2713 PRVM_G_FLOAT( OFS_RETURN ) = 0;
2717 ========================
2720 void cin_close(string name)
2721 ========================
2723 void VM_cin_close( void )
2727 VM_SAFEPARMCOUNT( 1, VM_cin_close );
2729 name = PRVM_G_STRING( OFS_PARM0 );
2730 VM_CheckEmptyString( name );
2732 CL_CloseVideo( CL_GetVideoByName( name ) );
2736 ========================
2738 void cin_setstate(string name, float type)
2739 ========================
2741 void VM_cin_setstate( void )
2744 clvideostate_t state;
2747 VM_SAFEPARMCOUNT( 2, VM_cin_netstate );
2749 name = PRVM_G_STRING( OFS_PARM0 );
2750 VM_CheckEmptyString( name );
2752 state = (clvideostate_t)((int)PRVM_G_FLOAT( OFS_PARM1 ));
2754 video = CL_GetVideoByName( name );
2755 if( video && state > CLVIDEO_UNUSED && state < CLVIDEO_STATECOUNT )
2756 CL_SetVideoState( video, state );
2760 ========================
2763 float cin_getstate(string name)
2764 ========================
2766 void VM_cin_getstate( void )
2771 VM_SAFEPARMCOUNT( 1, VM_cin_getstate );
2773 name = PRVM_G_STRING( OFS_PARM0 );
2774 VM_CheckEmptyString( name );
2776 video = CL_GetVideoByName( name );
2778 PRVM_G_FLOAT( OFS_RETURN ) = (int)video->state;
2780 PRVM_G_FLOAT( OFS_RETURN ) = 0;
2784 ========================
2787 void cin_restart(string name)
2788 ========================
2790 void VM_cin_restart( void )
2795 VM_SAFEPARMCOUNT( 1, VM_cin_restart );
2797 name = PRVM_G_STRING( OFS_PARM0 );
2798 VM_CheckEmptyString( name );
2800 video = CL_GetVideoByName( name );
2802 CL_RestartVideo( video );
2809 Writes new values for v_forward, v_up, and v_right based on the given forward vector
2810 vectorvectors(vector, vector)
2813 void VM_vectorvectors (void)
2815 VectorNormalize2(PRVM_G_VECTOR(OFS_PARM0), prog->globals.server->v_forward);
2816 VectorVectors(prog->globals.server->v_forward, prog->globals.server->v_right, prog->globals.server->v_up);
2820 ========================
2823 void drawline(float width, vector pos1, vector pos2, vector rgb, float alpha, float flags)
2824 ========================
2826 void VM_drawline (void)
2828 float *c1, *c2, *rgb;
2830 unsigned char flags;
2832 VM_SAFEPARMCOUNT(6, VM_drawline);
2833 width = PRVM_G_FLOAT(OFS_PARM0);
2834 c1 = PRVM_G_VECTOR(OFS_PARM1);
2835 c2 = PRVM_G_VECTOR(OFS_PARM2);
2836 rgb = PRVM_G_VECTOR(OFS_PARM3);
2837 alpha = PRVM_G_FLOAT(OFS_PARM4);
2838 flags = (int)PRVM_G_FLOAT(OFS_PARM5);
2839 DrawQ_Line(width, c1[0], c1[1], c2[0], c2[1], rgb[0], rgb[1], rgb[2], alpha, flags);
2842 //====================
2843 //QC POLYGON functions
2844 //====================
2849 float data[36]; //[515]: enough for polygons
2850 unsigned char flags; //[515]: + VM_POLYGON_2D and VM_POLYGON_FL4V flags
2853 //static float vm_polygon_linewidth = 1;
2854 static mempool_t *vm_polygons_pool = NULL;
2855 static unsigned char vm_current_vertices = 0;
2856 static qboolean vm_polygons_initialized = false;
2857 static vm_polygon_t *vm_polygons = NULL;
2858 static unsigned long vm_polygons_num = 0, vm_drawpolygons_num = 0; //[515]: ok long on 64bit ?
2859 static qboolean vm_polygonbegin = false; //[515]: for "no-crap-on-the-screen" check
2860 #define VM_DEFPOLYNUM 64 //[515]: enough for default ?
2862 #define VM_POLYGON_FL3V 16 //more than 2 vertices (used only for lines)
2863 #define VM_POLYGON_FLLINES 32
2864 #define VM_POLYGON_FL2D 64
2865 #define VM_POLYGON_FL4V 128 //4 vertices
2867 void VM_InitPolygons (void)
2869 vm_polygons_pool = Mem_AllocPool("VMPOLY", 0, NULL);
2870 vm_polygons = (vm_polygon_t *)Mem_Alloc(vm_polygons_pool, VM_DEFPOLYNUM*sizeof(vm_polygon_t));
2871 memset(vm_polygons, 0, VM_DEFPOLYNUM*sizeof(vm_polygon_t));
2872 vm_polygons_num = VM_DEFPOLYNUM;
2873 vm_drawpolygons_num = 0;
2874 vm_polygonbegin = false;
2875 vm_polygons_initialized = true;
2878 void VM_DrawPolygonCallback (const entity_render_t *ent, const rtlight_t *rtlight, int numsurfaces, int *surfacelist)
2880 int surfacelistindex;
2881 // LordHavoc: FIXME: this is stupid code
2882 for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
2884 const vm_polygon_t *p = &vm_polygons[surfacelist[surfacelistindex]];
2885 int flags = p->flags & 0x0f;
2887 if(flags == DRAWFLAG_ADDITIVE)
2888 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
2889 else if(flags == DRAWFLAG_MODULATE)
2890 GL_BlendFunc(GL_DST_COLOR, GL_ZERO);
2891 else if(flags == DRAWFLAG_2XMODULATE)
2892 GL_BlendFunc(GL_DST_COLOR,GL_SRC_COLOR);
2894 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
2896 R_Mesh_TexBind(0, R_GetTexture(p->tex));
2899 //[515]: is speed is max ?
2900 if(p->flags & VM_POLYGON_FLLINES) //[515]: lines
2902 qglLineWidth(p->data[13]);CHECKGLERROR
2903 qglBegin(GL_LINE_LOOP);
2904 qglTexCoord1f (p->data[12]);
2905 qglColor4f (p->data[20], p->data[21], p->data[22], p->data[23]);
2906 qglVertex3f (p->data[0] , p->data[1], p->data[2]);
2908 qglTexCoord1f (p->data[14]);
2909 qglColor4f (p->data[24], p->data[25], p->data[26], p->data[27]);
2910 qglVertex3f (p->data[3] , p->data[4], p->data[5]);
2912 if(p->flags & VM_POLYGON_FL3V)
2914 qglTexCoord1f (p->data[16]);
2915 qglColor4f (p->data[28], p->data[29], p->data[30], p->data[31]);
2916 qglVertex3f (p->data[6] , p->data[7], p->data[8]);
2918 if(p->flags & VM_POLYGON_FL4V)
2920 qglTexCoord1f (p->data[18]);
2921 qglColor4f (p->data[32], p->data[33], p->data[34], p->data[35]);
2922 qglVertex3f (p->data[9] , p->data[10], p->data[11]);
2930 qglBegin(GL_POLYGON);
2931 qglTexCoord2f (p->data[12], p->data[13]);
2932 qglColor4f (p->data[20], p->data[21], p->data[22], p->data[23]);
2933 qglVertex3f (p->data[0] , p->data[1], p->data[2]);
2935 qglTexCoord2f (p->data[14], p->data[15]);
2936 qglColor4f (p->data[24], p->data[25], p->data[26], p->data[27]);
2937 qglVertex3f (p->data[3] , p->data[4], p->data[5]);
2939 qglTexCoord2f (p->data[16], p->data[17]);
2940 qglColor4f (p->data[28], p->data[29], p->data[30], p->data[31]);
2941 qglVertex3f (p->data[6] , p->data[7], p->data[8]);
2943 if(p->flags & VM_POLYGON_FL4V)
2945 qglTexCoord2f (p->data[18], p->data[19]);
2946 qglColor4f (p->data[32], p->data[33], p->data[34], p->data[35]);
2947 qglVertex3f (p->data[9] , p->data[10], p->data[11]);
2955 void VM_AddPolygonTo2DScene (vm_polygon_t *p)
2957 drawqueuemesh_t mesh;
2958 static int picelements[6] = {0, 1, 2, 0, 2, 3};
2960 mesh.texture = p->tex;
2961 mesh.data_element3i = picelements;
2962 mesh.data_vertex3f = p->data;
2963 mesh.data_texcoord2f = p->data + 12;
2964 mesh.data_color4f = p->data + 20;
2965 if(p->flags & VM_POLYGON_FL4V)
2967 mesh.num_vertices = 4;
2968 mesh.num_triangles = 2;
2972 mesh.num_vertices = 3;
2973 mesh.num_triangles = 1;
2975 if(p->flags & VM_POLYGON_FLLINES) //[515]: lines
2976 DrawQ_LineLoop (&mesh, (p->flags&0x0f));
2978 DrawQ_Mesh (&mesh, (p->flags&0x0f));
2981 //void(string texturename, float flag, float 2d, float lines) R_BeginPolygon
2982 void VM_R_PolygonBegin (void)
2985 const char *picname;
2987 VM_SAFEPARMCOUNT(2, VM_R_PolygonBegin);
2989 if(!vm_polygons_initialized)
2993 VM_Warning("VM_R_PolygonBegin: called twice without VM_R_PolygonEnd after first\n");
2996 if(vm_drawpolygons_num >= vm_polygons_num)
2998 p = (vm_polygon_t *)Mem_Alloc(vm_polygons_pool, 2 * vm_polygons_num * sizeof(vm_polygon_t));
2999 memset(p, 0, 2 * vm_polygons_num * sizeof(vm_polygon_t));
3000 memcpy(p, vm_polygons, vm_polygons_num * sizeof(vm_polygon_t));
3001 Mem_Free(vm_polygons);
3003 vm_polygons_num *= 2;
3005 p = &vm_polygons[vm_drawpolygons_num];
3006 picname = PRVM_G_STRING(OFS_PARM0);
3008 p->tex = Draw_CachePic(picname, true)->tex;
3010 p->tex = r_texture_notexture;
3011 p->flags = (unsigned char)PRVM_G_FLOAT(OFS_PARM1);
3012 vm_current_vertices = 0;
3013 vm_polygonbegin = true;
3016 if(PRVM_G_FLOAT(OFS_PARM2))
3017 p->flags |= VM_POLYGON_FL2D;
3018 if(prog->argc >= 4 && PRVM_G_FLOAT(OFS_PARM3))
3020 p->data[13] = PRVM_G_FLOAT(OFS_PARM3); //[515]: linewidth
3021 p->flags |= VM_POLYGON_FLLINES;
3026 //void(vector org, vector texcoords, vector rgb, float alpha) R_PolygonVertex
3027 void VM_R_PolygonVertex (void)
3029 float *coords, *tx, *rgb, alpha;
3031 VM_SAFEPARMCOUNT(4, VM_R_PolygonVertex);
3033 if(!vm_polygonbegin)
3035 VM_Warning("VM_R_PolygonVertex: VM_R_PolygonBegin wasn't called\n");
3038 coords = PRVM_G_VECTOR(OFS_PARM0);
3039 tx = PRVM_G_VECTOR(OFS_PARM1);
3040 rgb = PRVM_G_VECTOR(OFS_PARM2);
3041 alpha = PRVM_G_FLOAT(OFS_PARM3);
3043 p = &vm_polygons[vm_drawpolygons_num];
3044 if(vm_current_vertices > 4)
3046 VM_Warning("VM_R_PolygonVertex: may have 4 vertices max\n");
3050 p->data[vm_current_vertices*3] = coords[0];
3051 p->data[1+vm_current_vertices*3] = coords[1];
3052 if(!(p->flags & VM_POLYGON_FL2D))
3053 p->data[2+vm_current_vertices*3] = coords[2];
3055 p->data[12+vm_current_vertices*2] = tx[0];
3056 if(!(p->flags & VM_POLYGON_FLLINES))
3057 p->data[13+vm_current_vertices*2] = tx[1];
3059 p->data[20+vm_current_vertices*4] = rgb[0];
3060 p->data[21+vm_current_vertices*4] = rgb[1];
3061 p->data[22+vm_current_vertices*4] = rgb[2];
3062 p->data[23+vm_current_vertices*4] = alpha;
3064 vm_current_vertices++;
3065 if(vm_current_vertices == 4)
3066 p->flags |= VM_POLYGON_FL4V;
3068 if(vm_current_vertices == 3)
3069 p->flags |= VM_POLYGON_FL3V;
3072 //void() R_EndPolygon
3073 void VM_R_PolygonEnd (void)
3075 if(!vm_polygonbegin)
3077 VM_Warning("VM_R_PolygonEnd: VM_R_PolygonBegin wasn't called\n");
3080 vm_polygonbegin = false;
3081 if(vm_current_vertices > 2 || (vm_current_vertices >= 2 && vm_polygons[vm_drawpolygons_num].flags & VM_POLYGON_FLLINES))
3083 if(vm_polygons[vm_drawpolygons_num].flags & VM_POLYGON_FL2D) //[515]: don't use qcpolygons memory if 2D
3084 VM_AddPolygonTo2DScene(&vm_polygons[vm_drawpolygons_num]);
3086 vm_drawpolygons_num++;
3089 VM_Warning("VM_R_PolygonEnd: %i vertices isn't a good choice\n", vm_current_vertices);
3092 void VM_AddPolygonsToMeshQueue (void)
3095 if(!vm_drawpolygons_num)
3097 for(i = 0;i < vm_drawpolygons_num;i++)
3098 VM_DrawPolygonCallback(NULL, NULL, i, NULL);
3099 vm_drawpolygons_num = 0;
3105 // float(float number, float quantity) bitshift (EXT_BITSHIFT)
3106 void VM_bitshift (void)
3109 VM_SAFEPARMCOUNT(2, VM_bitshift);
3111 n1 = (int)fabs((int)PRVM_G_FLOAT(OFS_PARM0));
3112 n2 = (int)PRVM_G_FLOAT(OFS_PARM1);
3114 PRVM_G_FLOAT(OFS_RETURN) = n1;
3117 PRVM_G_FLOAT(OFS_RETURN) = (n1 >> -n2);
3119 PRVM_G_FLOAT(OFS_RETURN) = (n1 << n2);
3122 ////////////////////////////////////////
3123 // AltString functions
3124 ////////////////////////////////////////
3127 ========================
3130 float altstr_count(string)
3131 ========================
3133 void VM_altstr_count( void )
3135 const char *altstr, *pos;
3138 VM_SAFEPARMCOUNT( 1, VM_altstr_count );
3140 altstr = PRVM_G_STRING( OFS_PARM0 );
3141 //VM_CheckEmptyString( altstr );
3143 for( count = 0, pos = altstr ; *pos ; pos++ ) {
3144 if( *pos == '\\' ) {
3148 } else if( *pos == '\'' ) {
3153 PRVM_G_FLOAT( OFS_RETURN ) = (float) (count / 2);
3157 ========================
3160 string altstr_prepare(string)
3161 ========================
3163 void VM_altstr_prepare( void )
3166 const char *instr, *in;
3169 VM_SAFEPARMCOUNT( 1, VM_altstr_prepare );
3171 instr = PRVM_G_STRING( OFS_PARM0 );
3172 //VM_CheckEmptyString( instr );
3173 outstr = VM_GetTempString();
3175 for( out = outstr, in = instr, size = VM_STRINGTEMP_LENGTH - 1 ; size && *in ; size--, in++, out++ )
3184 PRVM_G_INT( OFS_RETURN ) = PRVM_SetEngineString( outstr );
3188 ========================
3191 string altstr_get(string, float)
3192 ========================
3194 void VM_altstr_get( void )
3196 const char *altstr, *pos;
3200 VM_SAFEPARMCOUNT( 2, VM_altstr_get );
3202 altstr = PRVM_G_STRING( OFS_PARM0 );
3203 //VM_CheckEmptyString( altstr );
3205 count = (int)PRVM_G_FLOAT( OFS_PARM1 );
3206 count = count * 2 + 1;
3208 for( pos = altstr ; *pos && count ; pos++ )
3209 if( *pos == '\\' ) {
3212 } else if( *pos == '\'' )
3216 PRVM_G_INT( OFS_RETURN ) = PRVM_SetEngineString( NULL );
3220 outstr = VM_GetTempString();
3221 for( out = outstr, size = VM_STRINGTEMP_LENGTH - 1 ; size && *pos ; size--, pos++, out++ )
3222 if( *pos == '\\' ) {
3227 } else if( *pos == '\'' )
3233 PRVM_G_INT( OFS_RETURN ) = PRVM_SetEngineString( outstr );
3237 ========================
3240 string altstr_set(string altstr, float num, string set)
3241 ========================
3243 void VM_altstr_set( void )
3246 const char *altstr, *str;
3250 VM_SAFEPARMCOUNT( 3, VM_altstr_set );
3252 altstr = PRVM_G_STRING( OFS_PARM0 );
3253 //VM_CheckEmptyString( altstr );
3255 num = (int)PRVM_G_FLOAT( OFS_PARM1 );
3257 str = PRVM_G_STRING( OFS_PARM2 );
3258 //VM_CheckEmptyString( str );
3260 outstr = out = VM_GetTempString();
3261 for( num = num * 2 + 1, in = altstr; *in && num; *out++ = *in++ )
3266 } else if( *in == '\'' ) {
3271 PRVM_G_INT( OFS_RETURN ) = PRVM_SetEngineString( altstr );
3275 for( ; *str; *out++ = *str++ );
3276 // now jump over the old content
3278 if( *in == '\'' || (*in == '\\' && !*++in) )
3282 PRVM_G_INT( OFS_RETURN ) = PRVM_SetEngineString( NULL );
3287 PRVM_G_INT( OFS_RETURN ) = PRVM_SetEngineString( outstr );
3291 ========================
3294 string altstr_ins(string altstr, float num, string set)
3295 ========================
3297 void VM_altstr_ins(void)
3307 in = instr = PRVM_G_STRING( OFS_PARM0 );
3308 num = (int)PRVM_G_FLOAT( OFS_PARM1 );
3309 set = setstr = PRVM_G_STRING( OFS_PARM2 );
3311 out = outstr = VM_GetTempString();
3312 for( num = num * 2 + 2 ; *in && num > 0 ; *out++ = *in++ )
3317 } else if( *in == '\'' ) {
3322 for( ; *set ; *out++ = *set++ );
3326 PRVM_G_INT( OFS_RETURN ) = PRVM_SetEngineString( outstr );
3330 ////////////////////////////////////////
3331 // BufString functions
3332 ////////////////////////////////////////
3333 //[515]: string buffers support
3334 #define MAX_QCSTR_BUFFERS 128
3335 #define MAX_QCSTR_STRINGS 1024
3340 char *strings[MAX_QCSTR_STRINGS];
3343 static qcstrbuffer_t *qcstringbuffers[MAX_QCSTR_BUFFERS];
3344 static int num_qcstringbuffers;
3345 static int buf_sortpower;
3347 #define BUFSTR_BUFFER(a) (a>=MAX_QCSTR_BUFFERS) ? NULL : (qcstringbuffers[a])
3348 #define BUFSTR_ISFREE(a) (a<MAX_QCSTR_BUFFERS&&qcstringbuffers[a]&&qcstringbuffers[a]->num_strings<=0) ? 1 : 0
3350 static int BufStr_FindFreeBuffer (void)
3353 if(num_qcstringbuffers == MAX_QCSTR_BUFFERS)
3355 for(i=0;i<MAX_QCSTR_BUFFERS;i++)
3356 if(!qcstringbuffers[i])
3358 qcstringbuffers[i] = (qcstrbuffer_t *)Z_Malloc(sizeof(qcstrbuffer_t));
3359 memset(qcstringbuffers[i], 0, sizeof(qcstrbuffer_t));
3365 static void BufStr_ClearBuffer (int index)
3367 qcstrbuffer_t *b = qcstringbuffers[index];
3372 if(b->num_strings > 0)
3374 for(i=0;i<b->num_strings;i++)
3376 Z_Free(b->strings[i]);
3377 num_qcstringbuffers--;
3379 Z_Free(qcstringbuffers[index]);
3380 qcstringbuffers[index] = NULL;
3384 static int BufStr_FindFreeString (qcstrbuffer_t *b)
3387 for(i=0;i<b->num_strings;i++)
3388 if(!b->strings[i] || !b->strings[i][0])
3390 if(i == MAX_QCSTR_STRINGS) return -1;
3394 static int BufStr_SortStringsUP (const void *in1, const void *in2)
3397 a = *((const char **) in1);
3398 b = *((const char **) in2);
3400 if(!b[0]) return -1;
3401 return strncmp(a, b, buf_sortpower);
3404 static int BufStr_SortStringsDOWN (const void *in1, const void *in2)
3407 a = *((const char **) in1);
3408 b = *((const char **) in2);
3410 if(!b[0]) return -1;
3411 return strncmp(b, a, buf_sortpower);
3415 static void VM_BufStr_Init (void)
3417 memset(qcstringbuffers, 0, sizeof(qcstringbuffers));
3418 num_qcstringbuffers = 0;
3421 static void VM_BufStr_ShutDown (void)
3424 for(i=0;i<MAX_QCSTR_BUFFERS && num_qcstringbuffers;i++)
3425 BufStr_ClearBuffer(i);
3430 ========================
3432 creates new buffer, and returns it's index, returns -1 if failed
3433 float buf_create(void) = #460;
3434 ========================
3436 void VM_buf_create (void)
3439 VM_SAFEPARMCOUNT(0, VM_buf_create);
3440 i = BufStr_FindFreeBuffer();
3442 num_qcstringbuffers++;
3444 //Con_Printf("VM_buf_create: buffers overflow in %s\n", PRVM_NAME);
3445 PRVM_G_FLOAT(OFS_RETURN) = i;
3449 ========================
3451 deletes buffer and all strings in it
3452 void buf_del(float bufhandle) = #461;
3453 ========================
3455 void VM_buf_del (void)
3457 VM_SAFEPARMCOUNT(1, VM_buf_del);
3458 if(BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0)))
3459 BufStr_ClearBuffer((int)PRVM_G_FLOAT(OFS_PARM0));
3462 VM_Warning("VM_buf_del: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3468 ========================
3470 how many strings are stored in buffer
3471 float buf_getsize(float bufhandle) = #462;
3472 ========================
3474 void VM_buf_getsize (void)
3477 VM_SAFEPARMCOUNT(1, VM_buf_getsize);
3479 b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3482 PRVM_G_FLOAT(OFS_RETURN) = -1;
3483 VM_Warning("VM_buf_getsize: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3487 PRVM_G_FLOAT(OFS_RETURN) = b->num_strings;
3491 ========================
3493 copy all content from one buffer to another, make sure it exists
3494 void buf_copy(float bufhandle_from, float bufhandle_to) = #463;
3495 ========================
3497 void VM_buf_copy (void)
3499 qcstrbuffer_t *b1, *b2;
3501 VM_SAFEPARMCOUNT(2, VM_buf_copy);
3503 b1 = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3506 VM_Warning("VM_buf_copy: invalid source buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3509 i = (int)PRVM_G_FLOAT(OFS_PARM1);
3510 if(i == (int)PRVM_G_FLOAT(OFS_PARM0))
3512 VM_Warning("VM_buf_copy: source == destination (%i) in %s\n", i, PRVM_NAME);
3515 b2 = BUFSTR_BUFFER(i);
3518 VM_Warning("VM_buf_copy: invalid destination buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM1), PRVM_NAME);
3522 BufStr_ClearBuffer(i);
3523 qcstringbuffers[i] = (qcstrbuffer_t *)Z_Malloc(sizeof(qcstrbuffer_t));
3524 memset(qcstringbuffers[i], 0, sizeof(qcstrbuffer_t));
3525 b2->num_strings = b1->num_strings;
3527 for(i=0;i<b1->num_strings;i++)
3528 if(b1->strings[i] && b1->strings[i][0])
3530 b2->strings[i] = (char *)Z_Malloc(strlen(b1->strings[i])+1);
3533 VM_Warning("VM_buf_copy: not enough memory for buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM1), PRVM_NAME);
3536 strcpy(b2->strings[i], b1->strings[i]);
3541 ========================
3543 sort buffer by beginnings of strings (sortpower defaults it's lenght)
3544 "backward == TRUE" means that sorting goes upside-down
3545 void buf_sort(float bufhandle, float sortpower, float backward) = #464;
3546 ========================
3548 void VM_buf_sort (void)
3552 VM_SAFEPARMCOUNT(3, VM_buf_sort);
3554 b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3557 VM_Warning("VM_buf_sort: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3560 if(b->num_strings <= 0)
3562 VM_Warning("VM_buf_sort: tried to sort empty buffer %i in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3565 buf_sortpower = (int)PRVM_G_FLOAT(OFS_PARM1);
3566 if(buf_sortpower <= 0)
3567 buf_sortpower = 99999999;
3569 if(!PRVM_G_FLOAT(OFS_PARM2))
3570 qsort(b->strings, b->num_strings, sizeof(char*), BufStr_SortStringsUP);
3572 qsort(b->strings, b->num_strings, sizeof(char*), BufStr_SortStringsDOWN);
3574 for(i=b->num_strings-1;i>=0;i--) //[515]: delete empty lines
3577 if(b->strings[i][0])
3581 Z_Free(b->strings[i]);
3583 b->strings[i] = NULL;
3591 ========================
3593 concantenates all buffer string into one with "glue" separator and returns it as tempstring
3594 string buf_implode(float bufhandle, string glue) = #465;
3595 ========================
3597 void VM_buf_implode (void)
3604 VM_SAFEPARMCOUNT(2, VM_buf_implode);
3606 b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3607 PRVM_G_INT(OFS_RETURN) = 0;
3610 VM_Warning("VM_buf_implode: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3615 sep = PRVM_G_STRING(OFS_PARM1);
3616 k = VM_GetTempString();
3618 for(l=i=0;i<b->num_strings;i++)
3621 l += strlen(b->strings[i]);
3624 k = strcat(k, b->strings[i]);
3627 if(sep && (i != b->num_strings-1))
3637 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(k);
3641 ========================
3643 get a string from buffer, returns direct pointer, dont str_unzone it!
3644 string bufstr_get(float bufhandle, float string_index) = #465;
3645 ========================
3647 void VM_bufstr_get (void)
3651 VM_SAFEPARMCOUNT(2, VM_bufstr_get);
3653 b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3656 VM_Warning("VM_bufstr_get: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3659 strindex = (int)PRVM_G_FLOAT(OFS_PARM1);
3660 if(strindex < 0 || strindex > MAX_QCSTR_STRINGS)
3662 VM_Warning("VM_bufstr_get: invalid string index %i used in %s\n", strindex, PRVM_NAME);
3665 PRVM_G_INT(OFS_RETURN) = 0;
3666 if(b->num_strings <= strindex)
3668 if(b->strings[strindex])
3669 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(b->strings[strindex]);
3673 ========================
3675 copies a string into selected slot of buffer
3676 void bufstr_set(float bufhandle, float string_index, string str) = #466;
3677 ========================
3679 void VM_bufstr_set (void)
3681 int bufindex, strindex;
3685 VM_SAFEPARMCOUNT(3, VM_bufstr_set);
3687 bufindex = (int)PRVM_G_FLOAT(OFS_PARM0);
3688 b = BUFSTR_BUFFER(bufindex);
3691 VM_Warning("VM_bufstr_set: invalid buffer %i used in %s\n", bufindex, PRVM_NAME);
3694 strindex = (int)PRVM_G_FLOAT(OFS_PARM1);
3695 if(strindex < 0 || strindex > MAX_QCSTR_STRINGS)
3697 VM_Warning("VM_bufstr_set: invalid string index %i used in %s\n", strindex, PRVM_NAME);
3700 news = PRVM_G_STRING(OFS_PARM2);
3703 VM_Warning("VM_bufstr_set: null string used in %s\n", PRVM_NAME);
3706 if(b->strings[strindex])
3707 Z_Free(b->strings[strindex]);
3708 b->strings[strindex] = (char *)Z_Malloc(strlen(news)+1);
3709 strcpy(b->strings[strindex], news);
3713 ========================
3715 adds string to buffer in nearest free slot and returns it
3716 "order == TRUE" means that string will be added after last "full" slot
3717 float bufstr_add(float bufhandle, string str, float order) = #467;
3718 ========================
3720 void VM_bufstr_add (void)
3722 int bufindex, order, strindex;
3726 VM_SAFEPARMCOUNT(3, VM_bufstr_add);
3728 bufindex = (int)PRVM_G_FLOAT(OFS_PARM0);
3729 b = BUFSTR_BUFFER(bufindex);
3730 PRVM_G_FLOAT(OFS_RETURN) = -1;
3733 VM_Warning("VM_bufstr_add: invalid buffer %i used in %s\n", bufindex, PRVM_NAME);
3736 string = PRVM_G_STRING(OFS_PARM1);
3739 VM_Warning("VM_bufstr_add: null string used in %s\n", PRVM_NAME);
3743 order = (int)PRVM_G_FLOAT(OFS_PARM2);
3745 strindex = b->num_strings;
3748 strindex = BufStr_FindFreeString(b);
3751 VM_Warning("VM_bufstr_add: buffer %i has no free string slots in %s\n", bufindex, PRVM_NAME);
3756 while(b->num_strings <= strindex)
3758 if(b->num_strings == MAX_QCSTR_STRINGS)
3760 VM_Warning("VM_bufstr_add: buffer %i has no free string slots in %s\n", bufindex, PRVM_NAME);
3763 b->strings[b->num_strings] = NULL;
3766 if(b->strings[strindex])
3767 Z_Free(b->strings[strindex]);
3768 b->strings[strindex] = (char *)Z_Malloc(strlen(string)+1);
3769 strcpy(b->strings[strindex], string);
3770 PRVM_G_FLOAT(OFS_RETURN) = strindex;
3774 ========================
3776 delete string from buffer
3777 void bufstr_free(float bufhandle, float string_index) = #468;
3778 ========================
3780 void VM_bufstr_free (void)
3784 VM_SAFEPARMCOUNT(2, VM_bufstr_free);
3786 b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3789 VM_Warning("VM_bufstr_free: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3792 i = (int)PRVM_G_FLOAT(OFS_PARM1);
3793 if(i < 0 || i > MAX_QCSTR_STRINGS)
3795 VM_Warning("VM_bufstr_free: invalid string index %i used in %s\n", i, PRVM_NAME);
3799 Z_Free(b->strings[i]);
3800 b->strings[i] = NULL;
3801 if(i+1 == b->num_strings)
3807 void VM_Cmd_Init(void)
3809 // only init the stuff for the current prog
3812 // VM_BufStr_Init();
3813 if(vm_polygons_initialized)
3815 Mem_FreePool(&vm_polygons_pool);
3816 vm_polygons_initialized = false;
3820 void VM_Cmd_Reset(void)
3822 CL_PurgeOwner( MENUOWNER );
3824 VM_Files_CloseAll();
3825 // VM_BufStr_ShutDown();
3826 if(vm_polygons_initialized)
3828 Mem_FreePool(&vm_polygons_pool);
3829 vm_polygons_initialized = false;