2 // Basically every vm builtin cmd should be in here.
3 // All 3 builtin and extension lists can be found here
4 // cause large (I think they will) parts are from pr_cmds the same copyright like in pr_cmds
9 //============================================================================
12 // temp string handling
13 // LordHavoc: added this to semi-fix the problem of using many ftos calls in a print
14 static char vm_string_temp[VM_STRINGTEMP_BUFFERS][VM_STRINGTEMP_LENGTH];
15 static int vm_string_tempindex = 0;
18 #define MAX_VMFILES 256
19 #define MAX_PRVMFILES MAX_VMFILES * PRVM_MAXPROGS
20 #define VM_FILES ((qfile_t**)(vm_files + PRVM_GetProgNr() * MAX_VMFILES))
22 qfile_t *vm_files[MAX_PRVMFILES];
24 // qc fs search handling
25 #define MAX_VMSEARCHES 128
26 #define TOTAL_VMSEARCHES MAX_VMSEARCHES * PRVM_MAXPROGS
27 #define VM_SEARCHLIST ((fssearch_t**)(vm_fssearchlist + PRVM_GetProgNr() * MAX_VMSEARCHES))
29 fssearch_t *vm_fssearchlist[TOTAL_VMSEARCHES];
31 char *VM_GetTempString(void)
34 s = vm_string_temp[vm_string_tempindex];
35 vm_string_tempindex = (vm_string_tempindex + 1) % VM_STRINGTEMP_BUFFERS;
39 void VM_CheckEmptyString (const char *s)
42 PRVM_ERROR ("%s: Bad string", PRVM_NAME);
45 //============================================================================
48 void VM_VarString(int first, char *out, int outlength)
54 outend = out + outlength - 1;
55 for (i = first;i < prog->argc && out < outend;i++)
57 s = PRVM_G_STRING((OFS_PARM0+i*3));
58 while (out < outend && *s)
68 returns true if the extension is supported by the server
70 checkextension(extensionname)
74 // kind of helper function
75 static qboolean checkextension(const char *name)
79 len = (int)strlen(name);
81 for (e = prog->extensionstring;*e;e++)
88 while (*e && *e != ' ')
90 if ((e - start) == len && !strncasecmp(start, name, len))
96 void VM_checkextension (void)
98 VM_SAFEPARMCOUNT(1,VM_checkextension);
100 PRVM_G_FLOAT(OFS_RETURN) = checkextension(PRVM_G_STRING(OFS_PARM0));
107 This is a TERMINAL error, which will kill off the entire prog.
116 char string[VM_STRINGTEMP_LENGTH];
118 VM_VarString(0, string, sizeof(string));
119 Con_Printf("======%S ERROR in %s:\n%s\n", PRVM_NAME, PRVM_GetString(prog->xfunction->s_name), string);
122 ed = PRVM_G_EDICT(prog->self->ofs);
126 PRVM_ERROR ("%s: Program error", PRVM_NAME);
133 Dumps out self, then an error message. The program is aborted and self is
134 removed, but the level can continue.
139 void VM_objerror (void)
142 char string[VM_STRINGTEMP_LENGTH];
144 VM_VarString(0, string, sizeof(string));
145 Con_Printf("======%s OBJECT ERROR in %s:\n%s\n", PRVM_NAME, PRVM_GetString(prog->xfunction->s_name), string);
148 ed = PRVM_G_EDICT (prog->self->ofs);
154 // objerror has to display the object fields -> else call
155 PRVM_ERROR ("VM_objecterror: self not defined !");
160 VM_print (actually used only by client and menu)
169 char string[VM_STRINGTEMP_LENGTH];
171 VM_VarString(0, string, sizeof(string));
179 broadcast print to everyone on server
184 void VM_bprint (void)
186 char string[VM_STRINGTEMP_LENGTH];
190 Con_Printf("VM_bprint: game is not server(%s) !\n", PRVM_NAME);
194 VM_VarString(0, string, sizeof(string));
195 SV_BroadcastPrint(string);
200 VM_sprint (menu & client but only if server.active == true)
202 single print to a specific client
204 sprint(float clientnum,...[string])
207 void VM_sprint (void)
211 char string[VM_STRINGTEMP_LENGTH];
213 //find client for this entity
214 clientnum = PRVM_G_FLOAT(OFS_PARM0);
215 if (!sv.active || clientnum < 0 || clientnum >= svs.maxclients || !svs.clients[clientnum].active)
217 Con_Printf("VM_sprint: %s: invalid client or server is not active !\n", PRVM_NAME);
221 client = svs.clients + clientnum;
222 VM_VarString(1, string, sizeof(string));
223 MSG_WriteChar(&client->message,svc_print);
224 MSG_WriteString(&client->message, string);
231 single print to the screen
233 centerprint(clientent, value)
236 void VM_centerprint (void)
238 char string[VM_STRINGTEMP_LENGTH];
240 VM_VarString(0, string, sizeof(string));
241 SCR_CenterPrint(string);
248 vector normalize(vector)
251 void VM_normalize (void)
257 VM_SAFEPARMCOUNT(1,VM_normalize);
259 value1 = PRVM_G_VECTOR(OFS_PARM0);
261 f = VectorLength2(value1);
265 VectorScale(value1, f, newvalue);
268 VectorClear(newvalue);
270 VectorCopy (newvalue, PRVM_G_VECTOR(OFS_RETURN));
282 VM_SAFEPARMCOUNT(1,VM_vlen);
283 PRVM_G_FLOAT(OFS_RETURN) = VectorLength(PRVM_G_VECTOR(OFS_PARM0));
290 float vectoyaw(vector)
293 void VM_vectoyaw (void)
298 VM_SAFEPARMCOUNT(1,VM_vectoyaw);
300 value1 = PRVM_G_VECTOR(OFS_PARM0);
302 if (value1[1] == 0 && value1[0] == 0)
306 yaw = (int) (atan2(value1[1], value1[0]) * 180 / M_PI);
311 PRVM_G_FLOAT(OFS_RETURN) = yaw;
319 vector vectoangles(vector)
322 void VM_vectoangles (void)
328 VM_SAFEPARMCOUNT(1,VM_vectoangles);
330 value1 = PRVM_G_VECTOR(OFS_PARM0);
332 if (value1[1] == 0 && value1[0] == 0)
342 // LordHavoc: optimized a bit
345 yaw = (atan2(value1[1], value1[0]) * 180 / M_PI);
349 else if (value1[1] > 0)
354 forward = sqrt(value1[0]*value1[0] + value1[1]*value1[1]);
355 pitch = (atan2(value1[2], forward) * 180 / M_PI);
360 PRVM_G_FLOAT(OFS_RETURN+0) = pitch;
361 PRVM_G_FLOAT(OFS_RETURN+1) = yaw;
362 PRVM_G_FLOAT(OFS_RETURN+2) = 0;
369 Returns a number from 0<= num < 1
374 void VM_random (void)
376 VM_SAFEPARMCOUNT(0,VM_random);
378 PRVM_G_FLOAT(OFS_RETURN) = lhrandom(0, 1);
385 Each entity can have eight independant sound sources, like voice,
388 Channel 0 is an auto-allocate channel, the others override anything
389 already running on that entity/channel pair.
391 An attenuation of 0 will play full volume everywhere in the level.
392 Larger attenuations will drop off.
401 prvm_edict_t *entity;
405 entity = PRVM_G_EDICT(OFS_PARM0);
406 channel = PRVM_G_FLOAT(OFS_PARM1);
407 sample = PRVM_G_STRING(OFS_PARM2);
408 volume = PRVM_G_FLOAT(OFS_PARM3) * 255;
409 attenuation = PRVM_G_FLOAT(OFS_PARM4);
411 if (volume < 0 || volume > 255)
412 Host_Error ("SV_StartSound: volume = %i", volume);
414 if (attenuation < 0 || attenuation > 4)
415 Host_Error ("SV_StartSound: attenuation = %f", attenuation);
417 if (channel < 0 || channel > 7)
418 Host_Error ("SV_StartSound: channel = %i", channel);
420 SV_StartSound (entity, channel, sample, volume, attenuation);
428 localsound(string sample)
431 void VM_localsound(void)
435 VM_SAFEPARMCOUNT(1,VM_localsound);
437 s = PRVM_G_STRING(OFS_PARM0);
439 if(!S_LocalSound (s))
441 Con_Printf("VM_localsound: Failed to play %s for %s !\n", s, PRVM_NAME);
442 PRVM_G_FLOAT(OFS_RETURN) = -4;
446 PRVM_G_FLOAT(OFS_RETURN) = 1;
458 PRVM_ERROR ("%s: break statement", PRVM_NAME);
461 //============================================================================
467 Sends text over to the client's execution buffer
469 [localcmd (string, ...) or]
473 void VM_localcmd (void)
475 char string[VM_STRINGTEMP_LENGTH];
476 VM_VarString(0, string, sizeof(string));
477 Cbuf_AddText(string);
489 VM_SAFEPARMCOUNT(1,VM_cvar);
491 PRVM_G_FLOAT(OFS_RETURN) = Cvar_VariableValue(PRVM_G_STRING(OFS_PARM0));
498 const string VM_cvar_string (string)
501 void VM_cvar_string(void)
505 const char *cvar_string;
506 VM_SAFEPARMCOUNT(1,VM_cvar_string);
508 name = PRVM_G_STRING(OFS_PARM0);
511 PRVM_ERROR("VM_cvar_string: %s: null string", PRVM_NAME);
513 VM_CheckEmptyString(name);
515 out = VM_GetTempString();
517 cvar_string = Cvar_VariableString(name);
519 strcpy(out, cvar_string);
521 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(out);
526 ========================
529 const string VM_cvar_defstring (string)
530 ========================
532 void VM_cvar_defstring (void)
536 const char *cvar_string;
537 VM_SAFEPARMCOUNT(1,VM_cvar_string);
539 name = PRVM_G_STRING(OFS_PARM0);
542 PRVM_ERROR("VM_cvar_defstring: %s: null string", PRVM_NAME);
544 VM_CheckEmptyString(name);
546 out = VM_GetTempString();
548 cvar_string = Cvar_VariableDefString(name);
550 strcpy(out, cvar_string);
552 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(out);
558 void cvar_set (string,string)
561 void VM_cvar_set (void)
563 VM_SAFEPARMCOUNT(2,VM_cvar_set);
565 Cvar_Set(PRVM_G_STRING(OFS_PARM0), PRVM_G_STRING(OFS_PARM1));
575 void VM_dprint (void)
577 char string[VM_STRINGTEMP_LENGTH];
578 if (developer.integer)
580 VM_VarString(0, string, sizeof(string));
582 Con_Printf("%s", string);
584 Con_Printf("%s: %s", PRVM_NAME, string);
602 VM_SAFEPARMCOUNT(1, VM_ftos);
604 v = PRVM_G_FLOAT(OFS_PARM0);
606 s = VM_GetTempString();
607 if ((float)((int)v) == v)
608 sprintf(s, "%i", (int)v);
611 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(s);
626 VM_SAFEPARMCOUNT(1,VM_fabs);
628 v = PRVM_G_FLOAT(OFS_PARM0);
629 PRVM_G_FLOAT(OFS_RETURN) = fabs(v);
644 VM_SAFEPARMCOUNT(1,VM_vtos);
646 s = VM_GetTempString();
647 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]);
648 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(s);
663 VM_SAFEPARMCOUNT(1, VM_etos);
665 s = VM_GetTempString();
666 sprintf (s, "entity %i", PRVM_G_EDICTNUM(OFS_PARM0));
667 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(s);
674 float stof(...[string])
679 char string[VM_STRINGTEMP_LENGTH];
680 VM_VarString(0, string, sizeof(string));
681 PRVM_G_FLOAT(OFS_RETURN) = atof(string);
685 ========================
689 ========================
693 VM_SAFEPARMCOUNT(1, VM_itof);
694 PRVM_G_FLOAT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
698 ========================
702 ========================
707 VM_SAFEPARMCOUNT(1, VM_ftoi);
709 ent = PRVM_G_FLOAT(OFS_PARM0);
710 if(PRVM_PROG_TO_EDICT(ent)->priv.required->free)
711 PRVM_ERROR ("VM_ftoe: %s tried to access a freed entity (entity %i)!", PRVM_NAME, ent);
713 PRVM_G_INT(OFS_RETURN) = ent;
727 prog->xfunction->builtinsprofile += 20;
728 ed = PRVM_ED_Alloc();
740 void VM_remove (void)
743 prog->xfunction->builtinsprofile += 20;
745 VM_SAFEPARMCOUNT(1, VM_remove);
747 ed = PRVM_G_EDICT(OFS_PARM0);
748 if( PRVM_NUM_FOR_EDICT(ed) <= prog->reserved_edicts ) {
749 Con_DPrint( "VM_remove: tried to remove the null entity or a reserved entity!\n" );
750 } else if( ed->priv.required->free ) {
751 Con_DPrint( "VM_remove: tried to remove an already freed entity!\n" );
755 // if (ed == prog->edicts)
756 // PRVM_ERROR ("remove: tried to remove world");
757 // if (PRVM_NUM_FOR_EDICT(ed) <= sv.maxclients)
758 // Host_Error("remove: tried to remove a client");
765 entity find(entity start, .string field, string match)
776 VM_SAFEPARMCOUNT(3,VM_find);
778 e = PRVM_G_EDICTNUM(OFS_PARM0);
779 f = PRVM_G_INT(OFS_PARM1);
780 s = PRVM_G_STRING(OFS_PARM2);
784 // return reserved edict 0 (could be used for whatever the prog wants)
785 VM_RETURN_EDICT(prog->edicts);
789 for (e++ ; e < prog->num_edicts ; e++)
791 prog->xfunction->builtinsprofile++;
792 ed = PRVM_EDICT_NUM(e);
793 if (ed->priv.required->free)
795 t = PRVM_E_STRING(ed,f);
805 VM_RETURN_EDICT(prog->edicts);
812 entity findfloat(entity start, .float field, float match)
813 entity findentity(entity start, .entity field, entity match)
816 // LordHavoc: added this for searching float, int, and entity reference fields
817 void VM_findfloat (void)
824 VM_SAFEPARMCOUNT(3,VM_findfloat);
826 e = PRVM_G_EDICTNUM(OFS_PARM0);
827 f = PRVM_G_INT(OFS_PARM1);
828 s = PRVM_G_FLOAT(OFS_PARM2);
830 for (e++ ; e < prog->num_edicts ; e++)
832 prog->xfunction->builtinsprofile++;
833 ed = PRVM_EDICT_NUM(e);
834 if (ed->priv.required->free)
836 if (PRVM_E_FLOAT(ed,f) == s)
843 VM_RETURN_EDICT(prog->edicts);
850 entity findchain(.string field, string match)
853 // chained search for strings in entity fields
854 // entity(.string field, string match) findchain = #402;
855 void VM_findchain (void)
861 prvm_edict_t *ent, *chain;
863 VM_SAFEPARMCOUNT(2,VM_findchain);
865 // is the same like !(prog->flag & PRVM_FE_CHAIN) - even if the operator precedence is another
866 if(!prog->flag & PRVM_FE_CHAIN)
867 PRVM_ERROR("VM_findchain: %s doesnt have a chain field !", PRVM_NAME);
869 chain_of = PRVM_ED_FindField("chain")->ofs;
871 chain = prog->edicts;
873 f = PRVM_G_INT(OFS_PARM0);
874 s = PRVM_G_STRING(OFS_PARM1);
877 VM_RETURN_EDICT(prog->edicts);
881 ent = PRVM_NEXT_EDICT(prog->edicts);
882 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
884 prog->xfunction->builtinsprofile++;
885 if (ent->priv.required->free)
887 t = PRVM_E_STRING(ent,f);
893 PRVM_E_INT(ent,chain_of) = PRVM_NUM_FOR_EDICT(chain);
897 VM_RETURN_EDICT(chain);
904 entity findchainfloat(.string field, float match)
905 entity findchainentity(.string field, entity match)
908 // LordHavoc: chained search for float, int, and entity reference fields
909 // entity(.string field, float match) findchainfloat = #403;
910 void VM_findchainfloat (void)
916 prvm_edict_t *ent, *chain;
918 VM_SAFEPARMCOUNT(2, VM_findchainfloat);
920 if(!prog->flag & PRVM_FE_CHAIN)
921 PRVM_ERROR("VM_findchainfloat: %s doesnt have a chain field !", PRVM_NAME);
923 chain_of = PRVM_ED_FindField("chain")->ofs;
925 chain = (prvm_edict_t *)prog->edicts;
927 f = PRVM_G_INT(OFS_PARM0);
928 s = PRVM_G_FLOAT(OFS_PARM1);
930 ent = PRVM_NEXT_EDICT(prog->edicts);
931 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
933 prog->xfunction->builtinsprofile++;
934 if (ent->priv.required->free)
936 if (PRVM_E_FLOAT(ent,f) != s)
939 PRVM_E_INT(ent,chain_of) = PRVM_EDICT_TO_PROG(chain);
943 VM_RETURN_EDICT(chain);
947 ========================
950 entity findflags(entity start, .float field, float match)
951 ========================
953 // LordHavoc: search for flags in float fields
954 void VM_findflags (void)
961 VM_SAFEPARMCOUNT(3, VM_findflags);
964 e = PRVM_G_EDICTNUM(OFS_PARM0);
965 f = PRVM_G_INT(OFS_PARM1);
966 s = (int)PRVM_G_FLOAT(OFS_PARM2);
968 for (e++ ; e < prog->num_edicts ; e++)
970 prog->xfunction->builtinsprofile++;
971 ed = PRVM_EDICT_NUM(e);
972 if (ed->priv.required->free)
974 if ((int)PRVM_E_FLOAT(ed,f) & s)
981 VM_RETURN_EDICT(prog->edicts);
985 ========================
988 entity findchainflags(.float field, float match)
989 ========================
991 // LordHavoc: chained search for flags in float fields
992 void VM_findchainflags (void)
998 prvm_edict_t *ent, *chain;
1000 VM_SAFEPARMCOUNT(2, VM_findchainflags);
1002 if(!prog->flag & PRVM_FE_CHAIN)
1003 PRVM_ERROR("VM_findchainflags: %s doesnt have a chain field !", PRVM_NAME);
1005 chain_of = PRVM_ED_FindField("chain")->ofs;
1007 chain = (prvm_edict_t *)prog->edicts;
1009 f = PRVM_G_INT(OFS_PARM0);
1010 s = (int)PRVM_G_FLOAT(OFS_PARM1);
1012 ent = PRVM_NEXT_EDICT(prog->edicts);
1013 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
1015 prog->xfunction->builtinsprofile++;
1016 if (ent->priv.required->free)
1018 if (!((int)PRVM_E_FLOAT(ent,f) & s))
1021 PRVM_E_INT(ent,chain_of) = PRVM_EDICT_TO_PROG(chain);
1025 VM_RETURN_EDICT(chain);
1035 void VM_coredump (void)
1037 VM_SAFEPARMCOUNT(0,VM_coredump);
1039 Cbuf_AddText("prvm_edicts ");
1040 Cbuf_AddText(PRVM_NAME);
1051 void PRVM_StackTrace(void);
1052 void VM_stackdump (void)
1054 VM_SAFEPARMCOUNT(0, VM_stackdump);
1069 VM_SAFEPARMCOUNT(0, VM_crash);
1071 PRVM_ERROR("Crash called by %s",PRVM_NAME);
1081 void VM_traceon (void)
1083 VM_SAFEPARMCOUNT(0,VM_traceon);
1095 void VM_traceoff (void)
1097 VM_SAFEPARMCOUNT(0,VM_traceoff);
1099 prog->trace = false;
1109 void VM_eprint (void)
1111 VM_SAFEPARMCOUNT(1,VM_eprint);
1113 PRVM_ED_PrintNum (PRVM_G_EDICTNUM(OFS_PARM0));
1127 VM_SAFEPARMCOUNT(1,VM_rint);
1129 f = PRVM_G_FLOAT(OFS_PARM0);
1131 PRVM_G_FLOAT(OFS_RETURN) = (int)(f + 0.5);
1133 PRVM_G_FLOAT(OFS_RETURN) = (int)(f - 0.5);
1143 void VM_floor (void)
1145 VM_SAFEPARMCOUNT(1,VM_floor);
1147 PRVM_G_FLOAT(OFS_RETURN) = floor(PRVM_G_FLOAT(OFS_PARM0));
1159 VM_SAFEPARMCOUNT(1,VM_ceil);
1161 PRVM_G_FLOAT(OFS_RETURN) = ceil(PRVM_G_FLOAT(OFS_PARM0));
1169 entity nextent(entity)
1172 void VM_nextent (void)
1177 i = PRVM_G_EDICTNUM(OFS_PARM0);
1180 prog->xfunction->builtinsprofile++;
1182 if (i == prog->num_edicts)
1184 VM_RETURN_EDICT(prog->edicts);
1187 ent = PRVM_EDICT_NUM(i);
1188 if (!ent->priv.required->free)
1190 VM_RETURN_EDICT(ent);
1196 //=============================================================================
1203 changelevel(string map)
1206 void VM_changelevel (void)
1210 VM_SAFEPARMCOUNT(1, VM_changelevel);
1214 Con_Printf("VM_changelevel: game is not server (%s)\n", PRVM_NAME);
1218 // make sure we don't issue two changelevels
1219 if (svs.changelevel_issued)
1221 svs.changelevel_issued = true;
1223 s = PRVM_G_STRING(OFS_PARM0);
1224 Cbuf_AddText (va("changelevel %s\n",s));
1236 VM_SAFEPARMCOUNT(1,VM_sin);
1237 PRVM_G_FLOAT(OFS_RETURN) = sin(PRVM_G_FLOAT(OFS_PARM0));
1248 VM_SAFEPARMCOUNT(1,VM_cos);
1249 PRVM_G_FLOAT(OFS_RETURN) = cos(PRVM_G_FLOAT(OFS_PARM0));
1261 VM_SAFEPARMCOUNT(1,VM_sqrt);
1262 PRVM_G_FLOAT(OFS_RETURN) = sqrt(PRVM_G_FLOAT(OFS_PARM0));
1269 Returns a vector of length < 1 and > 0
1274 void VM_randomvec (void)
1279 VM_SAFEPARMCOUNT(0, VM_randomvec);
1284 temp[0] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1285 temp[1] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1286 temp[2] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1288 while (DotProduct(temp, temp) >= 1);
1289 VectorCopy (temp, PRVM_G_VECTOR(OFS_RETURN));
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;
1295 // length returned always > 0
1296 length = (rand()&32766 + 1) * (1.0 / 32767.0) / VectorLength(temp);
1297 VectorScale(temp,length, temp);*/
1298 //VectorCopy(temp, PRVM_G_VECTOR(OFS_RETURN));
1301 //=============================================================================
1307 float registercvar (string name, string value, float flags)
1310 void VM_registercvar (void)
1312 const char *name, *value;
1315 VM_SAFEPARMCOUNT(3,VM_registercvar);
1317 name = PRVM_G_STRING(OFS_PARM0);
1318 value = PRVM_G_STRING(OFS_PARM1);
1319 flags = PRVM_G_FLOAT(OFS_PARM2);
1320 PRVM_G_FLOAT(OFS_RETURN) = 0;
1322 if(flags > CVAR_MAXFLAGSVAL)
1325 // first check to see if it has already been defined
1326 if (Cvar_FindVar (name))
1329 // check for overlap with a command
1330 if (Cmd_Exists (name))
1332 Con_Printf("VM_registercvar: %s is a command\n", name);
1336 Cvar_Get(name, value, flags);
1338 PRVM_G_FLOAT(OFS_RETURN) = 1; // success
1345 returns the minimum of two supplied floats
1347 float min(float a, float b, ...[float])
1352 // LordHavoc: 3+ argument enhancement suggested by FrikaC
1353 if (prog->argc == 2)
1354 PRVM_G_FLOAT(OFS_RETURN) = min(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1355 else if (prog->argc >= 3)
1358 float f = PRVM_G_FLOAT(OFS_PARM0);
1359 for (i = 1;i < prog->argc;i++)
1360 if (PRVM_G_FLOAT((OFS_PARM0+i*3)) < f)
1361 f = PRVM_G_FLOAT((OFS_PARM0+i*3));
1362 PRVM_G_FLOAT(OFS_RETURN) = f;
1365 PRVM_ERROR("VM_min: %s must supply at least 2 floats", PRVM_NAME);
1372 returns the maximum of two supplied floats
1374 float max(float a, float b, ...[float])
1379 // LordHavoc: 3+ argument enhancement suggested by FrikaC
1380 if (prog->argc == 2)
1381 PRVM_G_FLOAT(OFS_RETURN) = max(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1382 else if (prog->argc >= 3)
1385 float f = PRVM_G_FLOAT(OFS_PARM0);
1386 for (i = 1;i < prog->argc;i++)
1387 if (PRVM_G_FLOAT((OFS_PARM0+i*3)) > f)
1388 f = PRVM_G_FLOAT((OFS_PARM0+i*3));
1389 PRVM_G_FLOAT(OFS_RETURN) = f;
1392 PRVM_ERROR("VM_max: %s must supply at least 2 floats", PRVM_NAME);
1399 returns number bounded by supplied range
1401 float bound(float min, float value, float max)
1404 void VM_bound (void)
1406 VM_SAFEPARMCOUNT(3,VM_bound);
1407 PRVM_G_FLOAT(OFS_RETURN) = bound(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1), PRVM_G_FLOAT(OFS_PARM2));
1414 returns a raised to power b
1416 float pow(float a, float b)
1421 VM_SAFEPARMCOUNT(2,VM_pow);
1422 PRVM_G_FLOAT(OFS_RETURN) = pow(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1429 copies data from one entity to another
1431 copyentity(entity src, entity dst)
1434 void VM_copyentity (void)
1436 prvm_edict_t *in, *out;
1437 VM_SAFEPARMCOUNT(2,VM_copyentity);
1438 in = PRVM_G_EDICT(OFS_PARM0);
1439 out = PRVM_G_EDICT(OFS_PARM1);
1440 memcpy(out->fields.vp, in->fields.vp, prog->progs->entityfields * 4);
1447 sets the color of a client and broadcasts the update to all connected clients
1449 setcolor(clientent, value)
1452 /*void PF_setcolor (void)
1458 entnum = PRVM_G_EDICTNUM(OFS_PARM0);
1459 i = PRVM_G_FLOAT(OFS_PARM1);
1461 if (entnum < 1 || entnum > svs.maxclients || !svs.clients[entnum-1].active)
1463 Con_Print("tried to setcolor a non-client\n");
1467 client = svs.clients + entnum-1;
1468 if ((val = PRVM_GETEDICTFIELDVALUE(client->edict, eval_clientcolors)))
1471 client->old_colors = i;
1472 client->edict->fields.server->team = (i & 15) + 1;
1474 MSG_WriteByte (&sv.reliable_datagram, svc_updatecolors);
1475 MSG_WriteByte (&sv.reliable_datagram, entnum - 1);
1476 MSG_WriteByte (&sv.reliable_datagram, i);
1479 void VM_Files_Init(void)
1481 memset(VM_FILES, 0, sizeof(qfile_t*[MAX_VMFILES]));
1484 void VM_Files_CloseAll(void)
1487 for (i = 0;i < MAX_VMFILES;i++)
1490 FS_Close(VM_FILES[i]);
1491 //VM_FILES[i] = NULL;
1493 memset(VM_FILES,0,sizeof(qfile_t*[MAX_VMFILES])); // this should be faster (is it ?)
1496 qfile_t *VM_GetFileHandle( int index )
1498 if (index < 0 || index >= MAX_VMFILES)
1500 Con_Printf("VM_GetFileHandle: invalid file handle %i used in %s\n", index, PRVM_NAME);
1503 if (VM_FILES[index] == NULL)
1505 Con_Printf("VM_GetFileHandle: no such file handle %i (or file has been closed) in %s\n", index, PRVM_NAME);
1508 return VM_FILES[index];
1515 float fopen(string filename, float mode)
1518 // float(string filename, float mode) fopen = #110;
1519 // opens a file inside quake/gamedir/data/ (mode is FILE_READ, FILE_APPEND, or FILE_WRITE),
1520 // returns fhandle >= 0 if successful, or fhandle < 0 if unable to open file for any reason
1524 const char *modestring, *filename;
1526 VM_SAFEPARMCOUNT(2,VM_fopen);
1528 for (filenum = 0;filenum < MAX_VMFILES;filenum++)
1529 if (VM_FILES[filenum] == NULL)
1531 if (filenum >= MAX_VMFILES)
1533 Con_Printf("VM_fopen: %s ran out of file handles (%i)\n", PRVM_NAME, MAX_VMFILES);
1534 PRVM_G_FLOAT(OFS_RETURN) = -2;
1537 mode = PRVM_G_FLOAT(OFS_PARM1);
1540 case 0: // FILE_READ
1543 case 1: // FILE_APPEND
1546 case 2: // FILE_WRITE
1550 Con_Printf("VM_fopen: %s: no such mode %i (valid: 0 = read, 1 = append, 2 = write)\n", PRVM_NAME, mode);
1551 PRVM_G_FLOAT(OFS_RETURN) = -3;
1554 filename = PRVM_G_STRING(OFS_PARM0);
1556 VM_FILES[filenum] = FS_Open(va("data/%s", filename), modestring, false, false);
1557 if (VM_FILES[filenum] == NULL && mode == 0)
1558 VM_FILES[filenum] = FS_Open(va("%s", filename), modestring, false, false);
1560 if (VM_FILES[filenum] == NULL)
1562 if (developer.integer)
1563 Con_Printf("VM_fopen: %s: %s mode %s failed\n", PRVM_NAME, filename, modestring);
1564 PRVM_G_FLOAT(OFS_RETURN) = -1;
1568 if (developer.integer)
1569 Con_Printf("VM_fopen: %s: %s mode %s opened as #%i\n", PRVM_NAME, filename, modestring, filenum);
1570 PRVM_G_FLOAT(OFS_RETURN) = filenum;
1578 fclose(float fhandle)
1581 //void(float fhandle) fclose = #111; // closes a file
1582 void VM_fclose(void)
1586 VM_SAFEPARMCOUNT(1,VM_fclose);
1588 filenum = PRVM_G_FLOAT(OFS_PARM0);
1589 if (filenum < 0 || filenum >= MAX_VMFILES)
1591 Con_Printf("VM_fclose: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1594 if (VM_FILES[filenum] == NULL)
1596 Con_Printf("VM_fclose: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1599 if (developer.integer)
1600 Con_Printf("VM_fclose: %s: #%i closed\n", PRVM_NAME, filenum);
1601 FS_Close(VM_FILES[filenum]);
1602 VM_FILES[filenum] = NULL;
1609 string fgets(float fhandle)
1612 //string(float fhandle) fgets = #112; // reads a line of text from the file and returns as a tempstring
1616 static char string[VM_STRINGTEMP_LENGTH];
1619 VM_SAFEPARMCOUNT(1,VM_fgets);
1621 filenum = PRVM_G_FLOAT(OFS_PARM0);
1622 if (filenum < 0 || filenum >= MAX_VMFILES)
1624 Con_Printf("VM_fgets: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1627 if (VM_FILES[filenum] == NULL)
1629 Con_Printf("VM_fgets: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1635 c = FS_Getc(VM_FILES[filenum]);
1636 if (c == '\r' || c == '\n' || c < 0)
1638 if (end < VM_STRINGTEMP_LENGTH - 1)
1642 // remove \n following \r
1645 c = FS_Getc(VM_FILES[filenum]);
1647 FS_UnGetc(VM_FILES[filenum], (unsigned char)c);
1649 if (developer.integer >= 3)
1650 Con_Printf("fgets: %s: %s\n", PRVM_NAME, string);
1652 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(string);
1654 PRVM_G_INT(OFS_RETURN) = 0;
1661 fputs(float fhandle, string s)
1664 //void(float fhandle, string s) fputs = #113; // writes a line of text to the end of the file
1668 char string[VM_STRINGTEMP_LENGTH];
1671 VM_SAFEPARMCOUNT(2,VM_fputs);
1673 filenum = PRVM_G_FLOAT(OFS_PARM0);
1674 if (filenum < 0 || filenum >= MAX_VMFILES)
1676 Con_Printf("VM_fputs: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1679 if (VM_FILES[filenum] == NULL)
1681 Con_Printf("VM_fputs: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1684 VM_VarString(1, string, sizeof(string));
1685 if ((stringlength = (int)strlen(string)))
1686 FS_Write(VM_FILES[filenum], string, stringlength);
1687 if (developer.integer)
1688 Con_Printf("fputs: %s: %s\n", PRVM_NAME, string);
1695 float strlen(string s)
1698 //float(string s) strlen = #114; // returns how many characters are in a string
1699 void VM_strlen(void)
1703 VM_SAFEPARMCOUNT(1,VM_strlen);
1705 s = PRVM_G_STRING(OFS_PARM0);
1707 PRVM_G_FLOAT(OFS_RETURN) = strlen(s);
1709 PRVM_G_FLOAT(OFS_RETURN) = 0;
1716 string strcat(string,string,...[string])
1719 //string(string s1, string s2) strcat = #115;
1720 // concatenates two strings (for example "abc", "def" would return "abcdef")
1721 // and returns as a tempstring
1722 void VM_strcat(void)
1727 PRVM_ERROR("VM_strcat wrong parameter count (min. 1 expected ) !");
1729 s = VM_GetTempString();
1730 VM_VarString(0, s, VM_STRINGTEMP_LENGTH);
1731 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(s);
1738 string substring(string s, float start, float length)
1741 // string(string s, float start, float length) substring = #116;
1742 // returns a section of a string as a tempstring
1743 void VM_substring(void)
1745 int i, start, length;
1749 VM_SAFEPARMCOUNT(3,VM_substring);
1751 string = VM_GetTempString();
1752 s = PRVM_G_STRING(OFS_PARM0);
1753 start = PRVM_G_FLOAT(OFS_PARM1);
1754 length = PRVM_G_FLOAT(OFS_PARM2);
1757 for (i = 0;i < start && *s;i++, s++);
1758 for (i = 0;i < VM_STRINGTEMP_LENGTH - 1 && *s && i < length;i++, s++)
1761 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(string);
1768 vector stov(string s)
1771 //vector(string s) stov = #117; // returns vector value from a string
1774 char string[VM_STRINGTEMP_LENGTH];
1776 VM_SAFEPARMCOUNT(1,VM_stov);
1778 VM_VarString(0, string, sizeof(string));
1779 Math_atov(string, PRVM_G_VECTOR(OFS_RETURN));
1786 string strzone(string s)
1789 //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)
1790 void VM_strzone(void)
1793 char string[VM_STRINGTEMP_LENGTH];
1795 VM_SAFEPARMCOUNT(1,VM_strzone);
1797 VM_VarString(0, string, sizeof(string));
1798 PRVM_G_INT(OFS_RETURN) = PRVM_AllocString(strlen(string) + 1, &out);
1799 strcpy(out, string);
1809 //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!!!)
1810 void VM_strunzone(void)
1812 VM_SAFEPARMCOUNT(1,VM_strunzone);
1813 PRVM_FreeString(PRVM_G_INT(OFS_PARM0));
1818 VM_command (used by client and menu)
1820 clientcommand(float client, string s) (for client and menu)
1823 //void(entity e, string s) clientcommand = #440; // executes a command string as if it came from the specified client
1824 //this function originally written by KrimZon, made shorter by LordHavoc
1825 void VM_clcommand (void)
1827 client_t *temp_client;
1830 VM_SAFEPARMCOUNT(2,VM_clcommand);
1832 i = PRVM_G_FLOAT(OFS_PARM0);
1833 if (!sv.active || i < 0 || i >= svs.maxclients || !svs.clients[i].active)
1835 Con_Printf("VM_clientcommand: %s: invalid client/server is not active !\n", PRVM_NAME);
1839 temp_client = host_client;
1840 host_client = svs.clients + i;
1841 Cmd_ExecuteString (PRVM_G_STRING(OFS_PARM1), src_client);
1842 host_client = temp_client;
1850 float tokenize(string s)
1853 //float(string s) tokenize = #441; // takes apart a string into individal words (access them with argv), returns how many
1854 //this function originally written by KrimZon, made shorter by LordHavoc
1855 //20040203: rewritten by LordHavoc (no longer uses allocations)
1857 char *tokens[256], tokenbuf[MAX_INPUTLINE];
1858 void VM_tokenize (void)
1863 VM_SAFEPARMCOUNT(1,VM_tokenize);
1865 p = PRVM_G_STRING(OFS_PARM0);
1869 while(COM_ParseToken(&p, false))
1871 if (num_tokens >= (int)(sizeof(tokens)/sizeof(tokens[0])))
1873 if (pos + strlen(com_token) + 1 > sizeof(tokenbuf))
1875 tokens[num_tokens++] = tokenbuf + pos;
1876 strcpy(tokenbuf + pos, com_token);
1877 pos += strlen(com_token) + 1;
1880 PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
1883 //string(float n) argv = #442; // returns a word from the tokenized string (returns nothing for an invalid index)
1884 //this function originally written by KrimZon, made shorter by LordHavoc
1889 VM_SAFEPARMCOUNT(1,VM_argv);
1891 token_num = PRVM_G_FLOAT(OFS_PARM0);
1893 if (token_num >= 0 && token_num < num_tokens)
1894 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(tokens[token_num]);
1896 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(NULL);
1900 //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)
1901 void PF_setattachment (void)
1903 prvm_edict_t *e = PRVM_G_EDICT(OFS_PARM0);
1904 prvm_edict_t *tagentity = PRVM_G_EDICT(OFS_PARM1);
1905 char *tagname = PRVM_G_STRING(OFS_PARM2);
1910 if (tagentity == NULL)
1911 tagentity = prog->edicts;
1913 v = PRVM_GETEDICTFIELDVALUE(e, eval_tag_entity);
1915 fields.server->edict = PRVM_EDICT_TO_PROG(tagentity);
1917 v = PRVM_GETEDICTFIELDVALUE(e, eval_tag_index);
1919 fields.server->_float = 0;
1920 if (tagentity != NULL && tagentity != prog->edicts && tagname && tagname[0])
1922 modelindex = (int)tagentity->fields.server->modelindex;
1923 if (modelindex >= 0 && modelindex < MAX_MODELS)
1925 model = sv.models[modelindex];
1926 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)
1927 for (i = 0;i < model->data_overridetagnamesforskin[(unsigned int)tagentity->fields.server->skin].num_overridetagnames;i++)
1928 if (!strcmp(tagname, model->data_overridetagnamesforskin[(unsigned int)tagentity->fields.server->skin].data_overridetagnames[i].name))
1929 fields.server->_float = i + 1;
1930 // FIXME: use a model function to get tag info (need to handle skeletal)
1931 if (fields.server->_float == 0 && model->num_tags)
1932 for (i = 0;i < model->num_tags;i++)
1933 if (!strcmp(tagname, model->data_tags[i].name))
1934 fields.server->_float = i + 1;
1935 if (fields.server->_float == 0)
1936 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);
1939 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));
1950 void VM_isserver(void)
1952 VM_SAFEPARMCOUNT(0,VM_serverstate);
1954 PRVM_G_FLOAT(OFS_RETURN) = sv.active;
1964 void VM_clientcount(void)
1966 VM_SAFEPARMCOUNT(0,VM_clientcount);
1968 PRVM_G_FLOAT(OFS_RETURN) = svs.maxclients;
1978 void VM_clientstate(void)
1980 VM_SAFEPARMCOUNT(0,VM_clientstate);
1982 PRVM_G_FLOAT(OFS_RETURN) = cls.state;
1989 float getostype(void)
1991 */ // not used at the moment -> not included in the common list
1992 void VM_getostype(void)
1994 VM_SAFEPARMCOUNT(0,VM_getostype);
1999 OS_MAC - not supported
2003 PRVM_G_FLOAT(OFS_RETURN) = 0;
2005 PRVM_G_FLOAT(OFS_RETURN) = 2;
2007 PRVM_G_FLOAT(OFS_RETURN) = 1;
2015 vector getmousepos()
2018 void VM_getmousepos(void)
2021 VM_SAFEPARMCOUNT(0,VM_getmousepos);
2023 PRVM_G_VECTOR(OFS_RETURN)[0] = in_mouse_x * vid_conwidth.integer / vid.width;
2024 PRVM_G_VECTOR(OFS_RETURN)[1] = in_mouse_y * vid_conheight.integer / vid.height;
2025 PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
2035 void VM_gettime(void)
2037 VM_SAFEPARMCOUNT(0,VM_gettime);
2039 PRVM_G_FLOAT(OFS_RETURN) = (float) *prog->time;
2046 loadfromdata(string data)
2049 void VM_loadfromdata(void)
2051 VM_SAFEPARMCOUNT(1,VM_loadentsfromfile);
2053 PRVM_ED_LoadFromFile(PRVM_G_STRING(OFS_PARM0));
2057 ========================
2060 parseentitydata(entity ent, string data)
2061 ========================
2063 void VM_parseentitydata(void)
2068 VM_SAFEPARMCOUNT(2, VM_parseentitydata);
2070 // get edict and test it
2071 ent = PRVM_G_EDICT(OFS_PARM0);
2072 if (ent->priv.required->free)
2073 PRVM_ERROR ("VM_parseentitydata: %s: Can only set already spawned entities (entity %i is free)!", PRVM_NAME, PRVM_NUM_FOR_EDICT(ent));
2075 data = PRVM_G_STRING(OFS_PARM1);
2077 // parse the opening brace
2078 if (!COM_ParseToken(&data, false) || com_token[0] != '{' )
2079 PRVM_ERROR ("VM_parseentitydata: %s: Couldn't parse entity data:\n%s", PRVM_NAME, data );
2081 PRVM_ED_ParseEdict (data, ent);
2088 loadfromfile(string file)
2091 void VM_loadfromfile(void)
2093 const char *filename;
2096 VM_SAFEPARMCOUNT(1,VM_loadfromfile);
2098 filename = PRVM_G_STRING(OFS_PARM0);
2099 // .. is parent directory on many platforms
2100 // / is parent directory on Amiga
2101 // : is root of drive on Amiga (also used as a directory separator on Mac, but / works there too, so that's a bad idea)
2102 // \ is a windows-ism (so it's naughty to use it, / works on all platforms)
2103 if ((filename[0] == '.' && filename[1] == '.') || filename[0] == '/' || strrchr(filename, ':') || strrchr(filename, '\\'))
2105 Con_Printf("VM_loadfromfile: %s dangerous or non-portable filename \"%s\" not allowed. (contains : or \\ or begins with .. or /)\n", PRVM_NAME, filename);
2106 PRVM_G_FLOAT(OFS_RETURN) = -4;
2110 // not conform with VM_fopen
2111 data = (char *)FS_LoadFile(filename, tempmempool, false, NULL);
2113 PRVM_G_FLOAT(OFS_RETURN) = -1;
2115 PRVM_ED_LoadFromFile(data);
2126 float mod(float val, float m)
2129 void VM_modulo(void)
2132 VM_SAFEPARMCOUNT(2,VM_module);
2134 val = (int) PRVM_G_FLOAT(OFS_PARM0);
2135 m = (int) PRVM_G_FLOAT(OFS_PARM1);
2137 PRVM_G_FLOAT(OFS_RETURN) = (float) (val % m);
2140 void VM_Search_Init(void)
2142 memset(VM_SEARCHLIST,0,sizeof(fssearch_t*[MAX_VMSEARCHES]));
2145 void VM_Search_Reset(void)
2148 // reset the fssearch list
2149 for(i = 0; i < MAX_VMSEARCHES; i++)
2150 if(VM_SEARCHLIST[i])
2151 FS_FreeSearch(VM_SEARCHLIST[i]);
2152 memset(VM_SEARCHLIST,0,sizeof(fssearch_t*[MAX_VMSEARCHES]));
2159 float search_begin(string pattern, float caseinsensitive, float quiet)
2162 void VM_search_begin(void)
2165 const char *pattern;
2166 int caseinsens, quiet;
2168 VM_SAFEPARMCOUNT(3, VM_search_begin);
2170 pattern = PRVM_G_STRING(OFS_PARM0);
2172 VM_CheckEmptyString(pattern);
2174 caseinsens = PRVM_G_FLOAT(OFS_PARM1);
2175 quiet = PRVM_G_FLOAT(OFS_PARM2);
2177 for(handle = 0; handle < MAX_VMSEARCHES; handle++)
2178 if(!VM_SEARCHLIST[handle])
2181 if(handle >= MAX_VMSEARCHES)
2183 Con_Printf("VM_search_begin: %s ran out of search handles (%i)\n", PRVM_NAME, MAX_VMSEARCHES);
2184 PRVM_G_FLOAT(OFS_RETURN) = -2;
2188 if(!(VM_SEARCHLIST[handle] = FS_Search(pattern,caseinsens, quiet)))
2189 PRVM_G_FLOAT(OFS_RETURN) = -1;
2191 PRVM_G_FLOAT(OFS_RETURN) = handle;
2198 void search_end(float handle)
2201 void VM_search_end(void)
2204 VM_SAFEPARMCOUNT(1, VM_search_end);
2206 handle = PRVM_G_FLOAT(OFS_PARM0);
2208 if(handle < 0 || handle >= MAX_VMSEARCHES)
2210 Con_Printf("VM_search_end: invalid handle %i used in %s\n", handle, PRVM_NAME);
2213 if(VM_SEARCHLIST[handle] == NULL)
2215 Con_Printf("VM_search_end: no such handle %i in %s\n", handle, PRVM_NAME);
2219 FS_FreeSearch(VM_SEARCHLIST[handle]);
2220 VM_SEARCHLIST[handle] = NULL;
2227 float search_getsize(float handle)
2230 void VM_search_getsize(void)
2233 VM_SAFEPARMCOUNT(1, VM_M_search_getsize);
2235 handle = PRVM_G_FLOAT(OFS_PARM0);
2237 if(handle < 0 || handle >= MAX_VMSEARCHES)
2239 Con_Printf("VM_search_getsize: invalid handle %i used in %s\n", handle, PRVM_NAME);
2242 if(VM_SEARCHLIST[handle] == NULL)
2244 Con_Printf("VM_search_getsize: no such handle %i in %s\n", handle, PRVM_NAME);
2248 PRVM_G_FLOAT(OFS_RETURN) = VM_SEARCHLIST[handle]->numfilenames;
2253 VM_search_getfilename
2255 string search_getfilename(float handle, float num)
2258 void VM_search_getfilename(void)
2260 int handle, filenum;
2262 VM_SAFEPARMCOUNT(2, VM_search_getfilename);
2264 handle = PRVM_G_FLOAT(OFS_PARM0);
2265 filenum = PRVM_G_FLOAT(OFS_PARM1);
2267 if(handle < 0 || handle >= MAX_VMSEARCHES)
2269 Con_Printf("VM_search_getfilename: invalid handle %i used in %s\n", handle, PRVM_NAME);
2272 if(VM_SEARCHLIST[handle] == NULL)
2274 Con_Printf("VM_search_getfilename: no such handle %i in %s\n", handle, PRVM_NAME);
2277 if(filenum < 0 || filenum >= VM_SEARCHLIST[handle]->numfilenames)
2279 Con_Printf("VM_search_getfilename: invalid filenum %i in %s\n", filenum, PRVM_NAME);
2283 tmp = VM_GetTempString();
2284 strcpy(tmp, VM_SEARCHLIST[handle]->filenames[filenum]);
2286 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(tmp);
2293 string chr(float ascii)
2299 VM_SAFEPARMCOUNT(1, VM_chr);
2301 tmp = VM_GetTempString();
2302 tmp[0] = (unsigned char) PRVM_G_FLOAT(OFS_PARM0);
2305 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(tmp);
2308 //=============================================================================
2309 // Draw builtins (client & menu)
2315 float iscachedpic(string pic)
2318 void VM_iscachedpic(void)
2320 VM_SAFEPARMCOUNT(1,VM_iscachedpic);
2322 // drawq hasnt such a function, thus always return true
2323 PRVM_G_FLOAT(OFS_RETURN) = false;
2330 string precache_pic(string pic)
2333 void VM_precache_pic(void)
2337 VM_SAFEPARMCOUNT(1, VM_precache_pic);
2339 s = PRVM_G_STRING(OFS_PARM0);
2340 PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
2343 PRVM_ERROR ("VM_precache_pic: %s: NULL", PRVM_NAME);
2345 VM_CheckEmptyString (s);
2347 // AK Draw_CachePic is supposed to always return a valid pointer
2348 if( Draw_CachePic(s, false)->tex == r_texture_notexture )
2349 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(NULL);
2359 void VM_freepic(void)
2363 VM_SAFEPARMCOUNT(1,VM_freepic);
2365 s = PRVM_G_STRING(OFS_PARM0);
2368 PRVM_ERROR ("VM_freepic: %s: NULL");
2370 VM_CheckEmptyString (s);
2379 float drawcharacter(vector position, float character, vector scale, vector rgb, float alpha, float flag)
2382 void VM_drawcharacter(void)
2384 float *pos,*scale,*rgb;
2387 VM_SAFEPARMCOUNT(6,VM_drawcharacter);
2389 character = (char) PRVM_G_FLOAT(OFS_PARM1);
2392 Con_Printf("VM_drawcharacter: %s passed null character !\n",PRVM_NAME);
2393 PRVM_G_FLOAT(OFS_RETURN) = -1;
2397 pos = PRVM_G_VECTOR(OFS_PARM0);
2398 scale = PRVM_G_VECTOR(OFS_PARM2);
2399 rgb = PRVM_G_VECTOR(OFS_PARM3);
2400 flag = (int)PRVM_G_FLOAT(OFS_PARM5);
2402 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2404 Con_Printf("VM_drawcharacter: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2405 PRVM_G_FLOAT(OFS_RETURN) = -2;
2409 if(pos[2] || scale[2])
2410 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")));
2412 if(!scale[0] || !scale[1])
2414 Con_Printf("VM_drawcharacter: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
2415 PRVM_G_FLOAT(OFS_RETURN) = -3;
2419 DrawQ_String (pos[0], pos[1], &character, 1, scale[0], scale[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM4), flag);
2420 PRVM_G_FLOAT(OFS_RETURN) = 1;
2427 float drawstring(vector position, string text, vector scale, vector rgb, float alpha, float flag)
2430 void VM_drawstring(void)
2432 float *pos,*scale,*rgb;
2435 VM_SAFEPARMCOUNT(6,VM_drawstring);
2437 string = PRVM_G_STRING(OFS_PARM1);
2440 Con_Printf("VM_drawstring: %s passed null string !\n",PRVM_NAME);
2441 PRVM_G_FLOAT(OFS_RETURN) = -1;
2445 //VM_CheckEmptyString(string); Why should it be checked - perhaps the menu wants to support the precolored letters, too?
2447 pos = PRVM_G_VECTOR(OFS_PARM0);
2448 scale = PRVM_G_VECTOR(OFS_PARM2);
2449 rgb = PRVM_G_VECTOR(OFS_PARM3);
2450 flag = (int)PRVM_G_FLOAT(OFS_PARM5);
2452 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2454 Con_Printf("VM_drawstring: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2455 PRVM_G_FLOAT(OFS_RETURN) = -2;
2459 if(!scale[0] || !scale[1])
2461 Con_Printf("VM_drawstring: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
2462 PRVM_G_FLOAT(OFS_RETURN) = -3;
2466 if(pos[2] || scale[2])
2467 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")));
2469 DrawQ_String (pos[0], pos[1], string, 0, scale[0], scale[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM4), flag);
2470 PRVM_G_FLOAT(OFS_RETURN) = 1;
2476 float drawpic(vector position, string pic, vector size, vector rgb, float alpha, float flag)
2479 void VM_drawpic(void)
2482 float *size, *pos, *rgb;
2485 VM_SAFEPARMCOUNT(6,VM_drawpic);
2487 pic = PRVM_G_STRING(OFS_PARM1);
2491 Con_Printf("VM_drawpic: %s passed null picture name !\n", PRVM_NAME);
2492 PRVM_G_FLOAT(OFS_RETURN) = -1;
2496 VM_CheckEmptyString (pic);
2498 // is pic cached ? no function yet for that
2501 Con_Printf("VM_drawpic: %s: %s not cached !\n", PRVM_NAME, pic);
2502 PRVM_G_FLOAT(OFS_RETURN) = -4;
2506 pos = PRVM_G_VECTOR(OFS_PARM0);
2507 size = PRVM_G_VECTOR(OFS_PARM2);
2508 rgb = PRVM_G_VECTOR(OFS_PARM3);
2509 flag = (int) PRVM_G_FLOAT(OFS_PARM5);
2511 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2513 Con_Printf("VM_drawstring: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2514 PRVM_G_FLOAT(OFS_RETURN) = -2;
2518 if(pos[2] || size[2])
2519 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")));
2521 DrawQ_Pic(pos[0], pos[1], pic, size[0], size[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM4), flag);
2522 PRVM_G_FLOAT(OFS_RETURN) = 1;
2529 float drawfill(vector position, vector size, vector rgb, float alpha, float flag)
2532 void VM_drawfill(void)
2534 float *size, *pos, *rgb;
2537 VM_SAFEPARMCOUNT(5,VM_drawfill);
2540 pos = PRVM_G_VECTOR(OFS_PARM0);
2541 size = PRVM_G_VECTOR(OFS_PARM1);
2542 rgb = PRVM_G_VECTOR(OFS_PARM2);
2543 flag = (int) PRVM_G_FLOAT(OFS_PARM4);
2545 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2547 Con_Printf("VM_drawstring: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2548 PRVM_G_FLOAT(OFS_RETURN) = -2;
2552 if(pos[2] || size[2])
2553 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")));
2555 DrawQ_Pic(pos[0], pos[1], 0, size[0], size[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM3), flag);
2556 PRVM_G_FLOAT(OFS_RETURN) = 1;
2563 drawsetcliparea(float x, float y, float width, float height)
2566 void VM_drawsetcliparea(void)
2569 VM_SAFEPARMCOUNT(4,VM_drawsetcliparea);
2571 x = bound(0, PRVM_G_FLOAT(OFS_PARM0), vid_conwidth.integer);
2572 y = bound(0, PRVM_G_FLOAT(OFS_PARM1), vid_conheight.integer);
2573 w = bound(0, PRVM_G_FLOAT(OFS_PARM2) + PRVM_G_FLOAT(OFS_PARM0) - x, (vid_conwidth.integer - x));
2574 h = bound(0, PRVM_G_FLOAT(OFS_PARM3) + PRVM_G_FLOAT(OFS_PARM1) - y, (vid_conheight.integer - y));
2576 DrawQ_SetClipArea(x, y, w, h);
2581 VM_drawresetcliparea
2586 void VM_drawresetcliparea(void)
2588 VM_SAFEPARMCOUNT(0,VM_drawresetcliparea);
2590 DrawQ_ResetClipArea();
2597 vector getimagesize(string pic)
2600 void VM_getimagesize(void)
2605 VM_SAFEPARMCOUNT(1,VM_getimagesize);
2607 p = PRVM_G_STRING(OFS_PARM0);
2610 PRVM_ERROR("VM_getimagepos: %s passed null picture name !", PRVM_NAME);
2612 VM_CheckEmptyString (p);
2614 pic = Draw_CachePic (p, false);
2616 PRVM_G_VECTOR(OFS_RETURN)[0] = pic->width;
2617 PRVM_G_VECTOR(OFS_RETURN)[1] = pic->height;
2618 PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
2625 string keynumtostring(float keynum)
2628 void VM_keynumtostring (void)
2632 VM_SAFEPARMCOUNT(1, VM_keynumtostring);
2634 keynum = PRVM_G_FLOAT(OFS_PARM0);
2636 tmp = VM_GetTempString();
2638 strcpy(tmp, Key_KeynumToString(keynum));
2640 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(tmp);
2647 float stringtokeynum(string key)
2650 void VM_stringtokeynum (void)
2653 VM_SAFEPARMCOUNT( 1, VM_keynumtostring );
2655 str = PRVM_G_STRING( OFS_PARM0 );
2657 PRVM_G_INT(OFS_RETURN) = Key_StringToKeynum( str );
2660 // CL_Video interface functions
2663 ========================
2666 float cin_open(string file, string name)
2667 ========================
2669 void VM_cin_open( void )
2674 VM_SAFEPARMCOUNT( 2, VM_cin_open );
2676 file = PRVM_G_STRING( OFS_PARM0 );
2677 name = PRVM_G_STRING( OFS_PARM1 );
2679 VM_CheckEmptyString( file );
2680 VM_CheckEmptyString( name );
2682 if( CL_OpenVideo( file, name, MENUOWNER ) )
2683 PRVM_G_FLOAT( OFS_RETURN ) = 1;
2685 PRVM_G_FLOAT( OFS_RETURN ) = 0;
2689 ========================
2692 void cin_close(string name)
2693 ========================
2695 void VM_cin_close( void )
2699 VM_SAFEPARMCOUNT( 1, VM_cin_close );
2701 name = PRVM_G_STRING( OFS_PARM0 );
2702 VM_CheckEmptyString( name );
2704 CL_CloseVideo( CL_GetVideo( name ) );
2708 ========================
2710 void cin_setstate(string name, float type)
2711 ========================
2713 void VM_cin_setstate( void )
2716 clvideostate_t state;
2719 VM_SAFEPARMCOUNT( 2, VM_cin_netstate );
2721 name = PRVM_G_STRING( OFS_PARM0 );
2722 VM_CheckEmptyString( name );
2724 state = (clvideostate_t)((int)PRVM_G_FLOAT( OFS_PARM1 ));
2726 video = CL_GetVideo( name );
2727 if( video && state > CLVIDEO_UNUSED && state < CLVIDEO_STATECOUNT )
2728 CL_SetVideoState( video, state );
2732 ========================
2735 float cin_getstate(string name)
2736 ========================
2738 void VM_cin_getstate( void )
2743 VM_SAFEPARMCOUNT( 1, VM_cin_getstate );
2745 name = PRVM_G_STRING( OFS_PARM0 );
2746 VM_CheckEmptyString( name );
2748 video = CL_GetVideo( name );
2750 PRVM_G_FLOAT( OFS_RETURN ) = (int)video->state;
2752 PRVM_G_FLOAT( OFS_RETURN ) = 0;
2756 ========================
2759 void cin_restart(string name)
2760 ========================
2762 void VM_cin_restart( void )
2767 VM_SAFEPARMCOUNT( 1, VM_cin_restart );
2769 name = PRVM_G_STRING( OFS_PARM0 );
2770 VM_CheckEmptyString( name );
2772 video = CL_GetVideo( name );
2774 CL_RestartVideo( video );
2781 Writes new values for v_forward, v_up, and v_right based on the given forward vector
2782 vectorvectors(vector, vector)
2785 void VM_vectorvectors (void)
2787 VectorNormalize2(PRVM_G_VECTOR(OFS_PARM0), prog->globals.server->v_forward);
2788 VectorVectors(prog->globals.server->v_forward, prog->globals.server->v_right, prog->globals.server->v_up);
2792 ========================
2795 void drawline(float width, vector pos1, vector pos2, vector rgb, float alpha, float flags)
2796 ========================
2798 void VM_drawline (void)
2800 float *c1, *c2, *rgb;
2802 unsigned char flags;
2804 VM_SAFEPARMCOUNT(6, VM_drawline);
2805 width = PRVM_G_FLOAT(OFS_PARM0);
2806 c1 = PRVM_G_VECTOR(OFS_PARM1);
2807 c2 = PRVM_G_VECTOR(OFS_PARM2);
2808 rgb = PRVM_G_VECTOR(OFS_PARM3);
2809 alpha = PRVM_G_FLOAT(OFS_PARM4);
2810 flags = PRVM_G_FLOAT(OFS_PARM5);
2811 DrawQ_Line(width, c1[0], c1[1], c2[0], c2[1], rgb[0], rgb[1], rgb[2], alpha, flags);
2814 //====================
2815 //QC POLYGON functions
2816 //====================
2821 float data[36]; //[515]: enough for polygons
2822 unsigned char flags; //[515]: + VM_POLYGON_2D and VM_POLYGON_FL4V flags
2825 //static float vm_polygon_linewidth = 1;
2826 static mempool_t *vm_polygons_pool = NULL;
2827 static unsigned char vm_current_vertices = 0;
2828 static qboolean vm_polygons_initialized = false;
2829 static vm_polygon_t *vm_polygons = NULL;
2830 static unsigned long vm_polygons_num = 0, vm_drawpolygons_num = 0; //[515]: ok long on 64bit ?
2831 static qboolean vm_polygonbegin = false; //[515]: for "no-crap-on-the-screen" check
2832 #define VM_DEFPOLYNUM 64 //[515]: enough for default ?
2834 #define VM_POLYGON_FL3V 16 //more than 2 vertices (used only for lines)
2835 #define VM_POLYGON_FLLINES 32
2836 #define VM_POLYGON_FL2D 64
2837 #define VM_POLYGON_FL4V 128 //4 vertices
2839 void VM_InitPolygons (void)
2841 vm_polygons_pool = Mem_AllocPool("VMPOLY", 0, NULL);
2842 vm_polygons = Mem_Alloc(vm_polygons_pool, VM_DEFPOLYNUM*sizeof(vm_polygon_t));
2843 memset(vm_polygons, 0, VM_DEFPOLYNUM*sizeof(vm_polygon_t));
2844 vm_polygons_num = VM_DEFPOLYNUM;
2845 vm_polygonbegin = vm_drawpolygons_num = 0;
2846 vm_polygons_initialized = true;
2849 void VM_DrawPolygonCallback (const entity_render_t *ent, int surfacenumber, const rtlight_t *rtlight)
2851 const vm_polygon_t *p = &vm_polygons[surfacenumber];
2852 int flags = p->flags & 0x0f;
2854 if(flags == DRAWFLAG_ADDITIVE)
2855 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
2856 else if(flags == DRAWFLAG_MODULATE)
2857 GL_BlendFunc(GL_DST_COLOR, GL_ZERO);
2858 else if(flags == DRAWFLAG_2XMODULATE)
2859 GL_BlendFunc(GL_DST_COLOR,GL_SRC_COLOR);
2861 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
2863 R_Mesh_TexBind(0, R_GetTexture(p->tex));
2865 //[515]: is speed is max ?
2866 if(p->flags & VM_POLYGON_FLLINES) //[515]: lines
2868 qglLineWidth(p->data[13]);
2869 qglBegin(GL_LINE_LOOP);
2870 qglTexCoord1f (p->data[12]);
2871 qglColor4f (p->data[20], p->data[21], p->data[22], p->data[23]);
2872 qglVertex3f (p->data[0] , p->data[1], p->data[2]);
2874 qglTexCoord1f (p->data[14]);
2875 qglColor4f (p->data[24], p->data[25], p->data[26], p->data[27]);
2876 qglVertex3f (p->data[3] , p->data[4], p->data[5]);
2878 if(p->flags & VM_POLYGON_FL3V)
2880 qglTexCoord1f (p->data[16]);
2881 qglColor4f (p->data[28], p->data[29], p->data[30], p->data[31]);
2882 qglVertex3f (p->data[6] , p->data[7], p->data[8]);
2884 if(p->flags & VM_POLYGON_FL4V)
2886 qglTexCoord1f (p->data[18]);
2887 qglColor4f (p->data[32], p->data[33], p->data[34], p->data[35]);
2888 qglVertex3f (p->data[9] , p->data[10], p->data[11]);
2895 qglBegin(GL_POLYGON);
2896 qglTexCoord2f (p->data[12], p->data[13]);
2897 qglColor4f (p->data[20], p->data[21], p->data[22], p->data[23]);
2898 qglVertex3f (p->data[0] , p->data[1], p->data[2]);
2900 qglTexCoord2f (p->data[14], p->data[15]);
2901 qglColor4f (p->data[24], p->data[25], p->data[26], p->data[27]);
2902 qglVertex3f (p->data[3] , p->data[4], p->data[5]);
2904 qglTexCoord2f (p->data[16], p->data[17]);
2905 qglColor4f (p->data[28], p->data[29], p->data[30], p->data[31]);
2906 qglVertex3f (p->data[6] , p->data[7], p->data[8]);
2908 if(p->flags & VM_POLYGON_FL4V)
2910 qglTexCoord2f (p->data[18], p->data[19]);
2911 qglColor4f (p->data[32], p->data[33], p->data[34], p->data[35]);
2912 qglVertex3f (p->data[9] , p->data[10], p->data[11]);
2918 void VM_AddPolygonTo2DScene (vm_polygon_t *p)
2920 drawqueuemesh_t mesh;
2921 static int picelements[6] = {0, 1, 2, 0, 2, 3};
2923 mesh.texture = p->tex;
2924 mesh.data_element3i = picelements;
2925 mesh.data_vertex3f = p->data;
2926 mesh.data_texcoord2f = p->data + 12;
2927 mesh.data_color4f = p->data + 20;
2928 if(p->flags & VM_POLYGON_FL4V)
2930 mesh.num_vertices = 4;
2931 mesh.num_triangles = 2;
2935 mesh.num_vertices = 3;
2936 mesh.num_triangles = 1;
2938 if(p->flags & VM_POLYGON_FLLINES) //[515]: lines
2939 DrawQ_LineLoop (&mesh, (p->flags&0x0f));
2941 DrawQ_Mesh (&mesh, (p->flags&0x0f));
2944 //void(string texturename, float flag, float 2d, float lines) R_BeginPolygon
2945 void VM_R_PolygonBegin (void)
2948 const char *picname;
2950 VM_SAFEPARMCOUNT(2, VM_R_PolygonBegin);
2952 if(!vm_polygons_initialized)
2956 Con_Printf("VM_R_PolygonBegin: called twice without VM_R_PolygonEnd after first\n");
2959 if(vm_drawpolygons_num >= vm_polygons_num)
2961 p = Mem_Alloc(vm_polygons_pool, 2 * vm_polygons_num * sizeof(vm_polygon_t));
2962 memset(p, 0, 2 * vm_polygons_num * sizeof(vm_polygon_t));
2963 memcpy(p, vm_polygons, vm_polygons_num * sizeof(vm_polygon_t));
2964 Mem_Free(vm_polygons);
2966 vm_polygons_num *= 2;
2968 p = &vm_polygons[vm_drawpolygons_num];
2969 picname = PRVM_G_STRING(OFS_PARM0);
2971 p->tex = Draw_CachePic(picname, false)->tex;
2973 p->tex = r_texture_notexture;
2974 p->flags = (unsigned char)PRVM_G_FLOAT(OFS_PARM1);
2975 vm_current_vertices = 0;
2976 vm_polygonbegin = true;
2979 if(PRVM_G_FLOAT(OFS_PARM2))
2980 p->flags |= VM_POLYGON_FL2D;
2981 if(prog->argc >= 4 && PRVM_G_FLOAT(OFS_PARM3))
2983 p->data[13] = PRVM_G_FLOAT(OFS_PARM3); //[515]: linewidth
2984 p->flags |= VM_POLYGON_FLLINES;
2989 //void(vector org, vector texcoords, vector rgb, float alpha) R_PolygonVertex
2990 void VM_R_PolygonVertex (void)
2992 float *coords, *tx, *rgb, alpha;
2994 VM_SAFEPARMCOUNT(4, VM_R_PolygonVertex);
2996 if(!vm_polygonbegin)
2998 Con_Printf("VM_R_PolygonVertex: VM_R_PolygonBegin wasn't called\n");
3001 coords = PRVM_G_VECTOR(OFS_PARM0);
3002 tx = PRVM_G_VECTOR(OFS_PARM1);
3003 rgb = PRVM_G_VECTOR(OFS_PARM2);
3004 alpha = PRVM_G_FLOAT(OFS_PARM3);
3006 p = &vm_polygons[vm_drawpolygons_num];
3007 if(vm_current_vertices > 4)
3009 Con_Printf("VM_R_PolygonVertex: may have 4 vertices max\n");
3013 p->data[vm_current_vertices*3] = coords[0];
3014 p->data[1+vm_current_vertices*3] = coords[1];
3015 if(!(p->flags & VM_POLYGON_FL2D))
3016 p->data[2+vm_current_vertices*3] = coords[2];
3018 p->data[12+vm_current_vertices*2] = tx[0];
3019 if(!(p->flags & VM_POLYGON_FLLINES))
3020 p->data[13+vm_current_vertices*2] = tx[1];
3022 p->data[20+vm_current_vertices*4] = rgb[0];
3023 p->data[21+vm_current_vertices*4] = rgb[1];
3024 p->data[22+vm_current_vertices*4] = rgb[2];
3025 p->data[23+vm_current_vertices*4] = alpha;
3027 vm_current_vertices++;
3028 if(vm_current_vertices == 4)
3029 p->flags |= VM_POLYGON_FL4V;
3031 if(vm_current_vertices == 3)
3032 p->flags |= VM_POLYGON_FL3V;
3035 //void() R_EndPolygon
3036 void VM_R_PolygonEnd (void)
3038 if(!vm_polygonbegin)
3040 Con_Printf("VM_R_PolygonEnd: VM_R_PolygonBegin wasn't called\n");
3043 if(vm_current_vertices > 2 || (vm_current_vertices >= 2 && vm_polygons[vm_drawpolygons_num].flags & VM_POLYGON_FLLINES))
3045 if(vm_polygons[vm_drawpolygons_num].flags & VM_POLYGON_FL2D) //[515]: don't use qcpolygons memory if 2D
3046 VM_AddPolygonTo2DScene(&vm_polygons[vm_drawpolygons_num]);
3048 vm_drawpolygons_num++;
3051 Con_Printf("VM_R_PolygonEnd: %i vertices isn't a good choice\n", vm_current_vertices);
3052 vm_polygonbegin = false;
3055 void VM_AddPolygonsToMeshQueue (void)
3058 if(!vm_drawpolygons_num)
3060 for(i = 0;i < vm_drawpolygons_num;i++)
3061 R_MeshQueue_Add(VM_DrawPolygonCallback, NULL, i, NULL);
3062 vm_drawpolygons_num = 0;
3068 // float(float number, float quantity) bitshift (EXT_BITSHIFT)
3069 void VM_bitshift (void)
3072 VM_SAFEPARMCOUNT(2, VM_bitshift);
3074 n1 = (int)fabs((int)PRVM_G_FLOAT(OFS_PARM0));
3075 n2 = (int)PRVM_G_FLOAT(OFS_PARM1);
3077 PRVM_G_FLOAT(OFS_RETURN) = n1;
3080 PRVM_G_FLOAT(OFS_RETURN) = (n1 >> -n2);
3082 PRVM_G_FLOAT(OFS_RETURN) = (n1 << n2);
3085 ////////////////////////////////////////
3086 // AltString functions
3087 ////////////////////////////////////////
3090 ========================
3093 float altstr_count(string)
3094 ========================
3096 void VM_altstr_count( void )
3098 const char *altstr, *pos;
3101 VM_SAFEPARMCOUNT( 1, VM_altstr_count );
3103 altstr = PRVM_G_STRING( OFS_PARM0 );
3104 //VM_CheckEmptyString( altstr );
3106 for( count = 0, pos = altstr ; *pos ; pos++ ) {
3107 if( *pos == '\\' ) {
3111 } else if( *pos == '\'' ) {
3116 PRVM_G_FLOAT( OFS_RETURN ) = (float) (count / 2);
3120 ========================
3123 string altstr_prepare(string)
3124 ========================
3126 void VM_altstr_prepare( void )
3129 const char *instr, *in;
3132 VM_SAFEPARMCOUNT( 1, VM_altstr_prepare );
3134 instr = PRVM_G_STRING( OFS_PARM0 );
3135 //VM_CheckEmptyString( instr );
3136 outstr = VM_GetTempString();
3138 for( out = outstr, in = instr, size = VM_STRINGTEMP_LENGTH - 1 ; size && *in ; size--, in++, out++ )
3147 PRVM_G_INT( OFS_RETURN ) = PRVM_SetEngineString( outstr );
3151 ========================
3154 string altstr_get(string, float)
3155 ========================
3157 void VM_altstr_get( void )
3159 const char *altstr, *pos;
3163 VM_SAFEPARMCOUNT( 2, VM_altstr_get );
3165 altstr = PRVM_G_STRING( OFS_PARM0 );
3166 //VM_CheckEmptyString( altstr );
3168 count = PRVM_G_FLOAT( OFS_PARM1 );
3169 count = count * 2 + 1;
3171 for( pos = altstr ; *pos && count ; pos++ )
3172 if( *pos == '\\' ) {
3175 } else if( *pos == '\'' )
3179 PRVM_G_INT( OFS_RETURN ) = PRVM_SetEngineString( NULL );
3183 outstr = VM_GetTempString();
3184 for( out = outstr, size = VM_STRINGTEMP_LENGTH - 1 ; size && *pos ; size--, pos++, out++ )
3185 if( *pos == '\\' ) {
3190 } else if( *pos == '\'' )
3196 PRVM_G_INT( OFS_RETURN ) = PRVM_SetEngineString( outstr );
3200 ========================
3203 string altstr_set(string altstr, float num, string set)
3204 ========================
3206 void VM_altstr_set( void )
3209 const char *altstr, *str;
3213 VM_SAFEPARMCOUNT( 3, VM_altstr_set );
3215 altstr = PRVM_G_STRING( OFS_PARM0 );
3216 //VM_CheckEmptyString( altstr );
3218 num = PRVM_G_FLOAT( OFS_PARM1 );
3220 str = PRVM_G_STRING( OFS_PARM2 );
3221 //VM_CheckEmptyString( str );
3223 outstr = out = VM_GetTempString();
3224 for( num = num * 2 + 1, in = altstr; *in && num; *out++ = *in++ )
3229 } else if( *in == '\'' ) {
3234 PRVM_G_INT( OFS_RETURN ) = PRVM_SetEngineString( altstr );
3238 for( ; *str; *out++ = *str++ );
3239 // now jump over the old content
3241 if( *in == '\'' || (*in == '\\' && !*++in) )
3245 PRVM_G_INT( OFS_RETURN ) = PRVM_SetEngineString( NULL );
3250 PRVM_G_INT( OFS_RETURN ) = PRVM_SetEngineString( outstr );
3254 ========================
3257 string altstr_ins(string altstr, float num, string set)
3258 ========================
3260 void VM_altstr_ins(void)
3270 in = instr = PRVM_G_STRING( OFS_PARM0 );
3271 num = PRVM_G_FLOAT( OFS_PARM1 );
3272 set = setstr = PRVM_G_STRING( OFS_PARM2 );
3274 out = outstr = VM_GetTempString();
3275 for( num = num * 2 + 2 ; *in && num > 0 ; *out++ = *in++ )
3280 } else if( *in == '\'' ) {
3285 for( ; *set ; *out++ = *set++ );
3289 PRVM_G_INT( OFS_RETURN ) = PRVM_SetEngineString( outstr );
3293 ////////////////////////////////////////
3294 // BufString functions
3295 ////////////////////////////////////////
3296 //[515]: string buffers support
3297 #define MAX_QCSTR_BUFFERS 128
3298 #define MAX_QCSTR_STRINGS 1024
3303 char *strings[MAX_QCSTR_STRINGS];
3306 static qcstrbuffer_t *qcstringbuffers[MAX_QCSTR_BUFFERS];
3307 static int num_qcstringbuffers;
3308 static int buf_sortpower;
3310 #define BUFSTR_BUFFER(a) (a>=MAX_QCSTR_BUFFERS) ? NULL : (qcstringbuffers[a])
3311 #define BUFSTR_ISFREE(a) (a<MAX_QCSTR_BUFFERS&&qcstringbuffers[a]&&qcstringbuffers[a]->num_strings<=0) ? 1 : 0
3313 static int BufStr_FindFreeBuffer (void)
3316 if(num_qcstringbuffers == MAX_QCSTR_BUFFERS)
3318 for(i=0;i<MAX_QCSTR_BUFFERS;i++)
3319 if(!qcstringbuffers[i])
3321 qcstringbuffers[i] = malloc(sizeof(qcstrbuffer_t));
3322 memset(qcstringbuffers[i], 0, sizeof(qcstrbuffer_t));
3328 static void BufStr_ClearBuffer (int index)
3330 qcstrbuffer_t *b = qcstringbuffers[index];
3335 if(b->num_strings > 0)
3337 for(i=0;i<b->num_strings;i++)
3339 free(b->strings[i]);
3340 num_qcstringbuffers--;
3342 free(qcstringbuffers[index]);
3343 qcstringbuffers[index] = NULL;
3347 static int BufStr_FindFreeString (qcstrbuffer_t *b)
3350 for(i=0;i<b->num_strings;i++)
3351 if(!b->strings[i] || !b->strings[i][0])
3353 if(i == MAX_QCSTR_STRINGS) return -1;
3357 static int BufStr_SortStringsUP (const void *in1, const void *in2)
3360 a = *((const char **) in1);
3361 b = *((const char **) in2);
3363 if(!b[0]) return -1;
3364 return strncmp(a, b, buf_sortpower);
3367 static int BufStr_SortStringsDOWN (const void *in1, const void *in2)
3370 a = *((const char **) in1);
3371 b = *((const char **) in2);
3373 if(!b[0]) return -1;
3374 return strncmp(b, a, buf_sortpower);
3378 static void VM_BufStr_Init (void)
3380 memset(qcstringbuffers, 0, sizeof(qcstringbuffers));
3381 num_qcstringbuffers = 0;
3384 static void VM_BufStr_ShutDown (void)
3387 for(i=0;i<MAX_QCSTR_BUFFERS && num_qcstringbuffers;i++)
3388 BufStr_ClearBuffer(i);
3393 ========================
3395 creates new buffer, and returns it's index, returns -1 if failed
3396 float buf_create(void) = #460;
3397 ========================
3399 void VM_buf_create (void)
3402 VM_SAFEPARMCOUNT(0, VM_buf_create);
3403 i = BufStr_FindFreeBuffer();
3405 num_qcstringbuffers++;
3407 //Con_Printf("VM_buf_create: buffers overflow in %s\n", PRVM_NAME);
3408 PRVM_G_FLOAT(OFS_RETURN) = i;
3412 ========================
3414 deletes buffer and all strings in it
3415 void buf_del(float bufhandle) = #461;
3416 ========================
3418 void VM_buf_del (void)
3420 VM_SAFEPARMCOUNT(1, VM_buf_del);
3421 if(BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0)))
3422 BufStr_ClearBuffer((int)PRVM_G_FLOAT(OFS_PARM0));
3425 Con_Printf("VM_buf_del: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3431 ========================
3433 how many strings are stored in buffer
3434 float buf_getsize(float bufhandle) = #462;
3435 ========================
3437 void VM_buf_getsize (void)
3440 VM_SAFEPARMCOUNT(1, VM_buf_getsize);
3442 b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3445 PRVM_G_FLOAT(OFS_RETURN) = -1;
3446 Con_Printf("VM_buf_getsize: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3450 PRVM_G_FLOAT(OFS_RETURN) = b->num_strings;
3454 ========================
3456 copy all content from one buffer to another, make sure it exists
3457 void buf_copy(float bufhandle_from, float bufhandle_to) = #463;
3458 ========================
3460 void VM_buf_copy (void)
3462 qcstrbuffer_t *b1, *b2;
3464 VM_SAFEPARMCOUNT(2, VM_buf_copy);
3466 b1 = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3469 Con_Printf("VM_buf_copy: invalid source buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3472 i = PRVM_G_FLOAT(OFS_PARM1);
3473 if(i == (int)PRVM_G_FLOAT(OFS_PARM0))
3475 Con_Printf("VM_buf_copy: source == destination (%i) in %s\n", i, PRVM_NAME);
3478 b2 = BUFSTR_BUFFER(i);
3481 Con_Printf("VM_buf_copy: invalid destination buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM1), PRVM_NAME);
3485 BufStr_ClearBuffer(i);
3486 qcstringbuffers[i] = malloc(sizeof(qcstrbuffer_t));
3487 memset(qcstringbuffers[i], 0, sizeof(qcstrbuffer_t));
3488 b2->num_strings = b1->num_strings;
3490 for(i=0;i<b1->num_strings;i++)
3491 if(b1->strings[i] && b1->strings[i][0])
3493 b2->strings[i] = malloc(strlen(b1->strings[i])+1);
3496 Con_Printf("VM_buf_copy: not enough memory for buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM1), PRVM_NAME);
3499 strcpy(b2->strings[i], b1->strings[i]);
3504 ========================
3506 sort buffer by beginnings of strings (sortpower defaults it's lenght)
3507 "backward == TRUE" means that sorting goes upside-down
3508 void buf_sort(float bufhandle, float sortpower, float backward) = #464;
3509 ========================
3511 void VM_buf_sort (void)
3515 VM_SAFEPARMCOUNT(3, VM_buf_sort);
3517 b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3520 Con_Printf("VM_buf_sort: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3523 if(b->num_strings <= 0)
3525 Con_Printf("VM_buf_sort: tried to sort empty buffer %i in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3528 buf_sortpower = PRVM_G_FLOAT(OFS_PARM1);
3529 if(buf_sortpower <= 0)
3530 buf_sortpower = 99999999;
3532 if(!PRVM_G_FLOAT(OFS_PARM2))
3533 qsort(b->strings, b->num_strings, sizeof(char*), BufStr_SortStringsUP);
3535 qsort(b->strings, b->num_strings, sizeof(char*), BufStr_SortStringsDOWN);
3537 for(i=b->num_strings-1;i>=0;i--) //[515]: delete empty lines
3540 if(b->strings[i][0])
3544 free(b->strings[i]);
3546 b->strings[i] = NULL;
3554 ========================
3556 concantenates all buffer string into one with "glue" separator and returns it as tempstring
3557 string buf_implode(float bufhandle, string glue) = #465;
3558 ========================
3560 void VM_buf_implode (void)
3566 VM_SAFEPARMCOUNT(2, VM_buf_implode);
3568 b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3569 PRVM_G_INT(OFS_RETURN) = 0;
3572 Con_Printf("VM_buf_implode: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3577 sep = PRVM_G_STRING(OFS_PARM1);
3578 k = VM_GetTempString();
3580 for(l=i=0;i<b->num_strings;i++)
3583 l += strlen(b->strings[i]);
3586 k = strcat(k, b->strings[i]);
3589 if(sep && (i != b->num_strings-1))
3599 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(k);
3603 ========================
3605 get a string from buffer, returns direct pointer, dont str_unzone it!
3606 string bufstr_get(float bufhandle, float string_index) = #465;
3607 ========================
3609 void VM_bufstr_get (void)
3613 VM_SAFEPARMCOUNT(2, VM_bufstr_get);
3615 b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3618 Con_Printf("VM_bufstr_get: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3621 strindex = (int)PRVM_G_FLOAT(OFS_PARM1);
3622 if(strindex < 0 || strindex > MAX_QCSTR_STRINGS)
3624 Con_Printf("VM_bufstr_get: invalid string index %i used in %s\n", strindex, PRVM_NAME);
3627 PRVM_G_INT(OFS_RETURN) = 0;
3628 if(b->num_strings <= strindex)
3630 if(b->strings[strindex])
3631 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(b->strings[strindex]);
3635 ========================
3637 copies a string into selected slot of buffer
3638 void bufstr_set(float bufhandle, float string_index, string str) = #466;
3639 ========================
3641 void VM_bufstr_set (void)
3643 int bufindex, strindex;
3647 VM_SAFEPARMCOUNT(3, VM_bufstr_set);
3649 bufindex = PRVM_G_FLOAT(OFS_PARM0);
3650 b = BUFSTR_BUFFER(bufindex);
3653 Con_Printf("VM_bufstr_set: invalid buffer %i used in %s\n", bufindex, PRVM_NAME);
3656 strindex = PRVM_G_FLOAT(OFS_PARM1);
3657 if(strindex < 0 || strindex > MAX_QCSTR_STRINGS)
3659 Con_Printf("VM_bufstr_set: invalid string index %i used in %s\n", strindex, PRVM_NAME);
3662 news = PRVM_G_STRING(OFS_PARM2);
3665 Con_Printf("VM_bufstr_set: null string used in %s\n", PRVM_NAME);
3668 if(b->strings[strindex])
3669 free(b->strings[strindex]);
3670 b->strings[strindex] = malloc(strlen(news)+1);
3671 strcpy(b->strings[strindex], news);
3675 ========================
3677 adds string to buffer in nearest free slot and returns it
3678 "order == TRUE" means that string will be added after last "full" slot
3679 float bufstr_add(float bufhandle, string str, float order) = #467;
3680 ========================
3682 void VM_bufstr_add (void)
3684 int bufindex, order, strindex;
3688 VM_SAFEPARMCOUNT(3, VM_bufstr_add);
3690 bufindex = PRVM_G_FLOAT(OFS_PARM0);
3691 b = BUFSTR_BUFFER(bufindex);
3692 PRVM_G_FLOAT(OFS_RETURN) = -1;
3695 Con_Printf("VM_bufstr_add: invalid buffer %i used in %s\n", bufindex, PRVM_NAME);
3698 string = PRVM_G_STRING(OFS_PARM1);
3701 Con_Printf("VM_bufstr_add: null string used in %s\n", PRVM_NAME);
3705 order = PRVM_G_FLOAT(OFS_PARM2);
3707 strindex = b->num_strings;
3710 strindex = BufStr_FindFreeString(b);
3713 Con_Printf("VM_bufstr_add: buffer %i has no free string slots in %s\n", bufindex, PRVM_NAME);
3718 while(b->num_strings <= strindex)
3720 if(b->num_strings == MAX_QCSTR_STRINGS)
3722 Con_Printf("VM_bufstr_add: buffer %i has no free string slots in %s\n", bufindex, PRVM_NAME);
3725 b->strings[b->num_strings] = NULL;
3728 if(b->strings[strindex])
3729 free(b->strings[strindex]);
3730 b->strings[strindex] = malloc(strlen(string)+1);
3731 strcpy(b->strings[strindex], string);
3732 PRVM_G_FLOAT(OFS_RETURN) = strindex;
3736 ========================
3738 delete string from buffer
3739 void bufstr_free(float bufhandle, float string_index) = #468;
3740 ========================
3742 void VM_bufstr_free (void)
3746 VM_SAFEPARMCOUNT(2, VM_bufstr_free);
3748 b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3751 Con_Printf("VM_bufstr_free: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3754 i = PRVM_G_FLOAT(OFS_PARM1);
3755 if(i < 0 || i > MAX_QCSTR_STRINGS)
3757 Con_Printf("VM_bufstr_free: invalid string index %i used in %s\n", i, PRVM_NAME);
3761 free(b->strings[i]);
3762 b->strings[i] = NULL;
3763 if(i+1 == b->num_strings)
3769 void VM_Cmd_Init(void)
3771 // only init the stuff for the current prog
3774 // VM_BufStr_Init();
3775 if(vm_polygons_initialized)
3777 Mem_FreePool(&vm_polygons_pool);
3778 vm_polygons_initialized = false;
3782 void VM_Cmd_Reset(void)
3784 CL_PurgeOwner( MENUOWNER );
3786 VM_Files_CloseAll();
3787 // VM_BufStr_ShutDown();
3788 if(vm_polygons_initialized)
3790 Mem_FreePool(&vm_polygons_pool);
3791 vm_polygons_initialized = false;