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);
20 // TODO: either add a cvar/cmd to control the state dumping or replace some of the calls with Con_Printf [9/13/2006 Black]
25 //============================================================================
28 // temp string handling
29 // LordHavoc: added this to semi-fix the problem of using many ftos calls in a print
30 static char vm_string_temp[VM_STRINGTEMP_BUFFERS][VM_STRINGTEMP_LENGTH];
31 static int vm_string_tempindex = 0;
33 // TODO: (move vm_files and vm_fssearchlist to prvm_prog_t struct)
34 // TODO: move vm_files and vm_fssearchlist back [9/13/2006 Black]
35 char *VM_GetTempString(void)
38 s = vm_string_temp[vm_string_tempindex];
39 vm_string_tempindex = (vm_string_tempindex + 1) % VM_STRINGTEMP_BUFFERS;
43 void VM_CheckEmptyString (const char *s)
46 PRVM_ERROR ("%s: Bad string", PRVM_NAME);
49 //============================================================================
52 void VM_VarString(int first, char *out, int outlength)
58 outend = out + outlength - 1;
59 for (i = first;i < prog->argc && out < outend;i++)
61 s = PRVM_G_STRING((OFS_PARM0+i*3));
62 while (out < outend && *s)
72 returns true if the extension is supported by the server
74 checkextension(extensionname)
78 // kind of helper function
79 static qboolean checkextension(const char *name)
83 len = (int)strlen(name);
85 for (e = prog->extensionstring;*e;e++)
92 while (*e && *e != ' ')
94 if ((e - start) == len && !strncasecmp(start, name, len))
100 void VM_checkextension (void)
102 VM_SAFEPARMCOUNT(1,VM_checkextension);
104 PRVM_G_FLOAT(OFS_RETURN) = checkextension(PRVM_G_STRING(OFS_PARM0));
111 This is a TERMINAL error, which will kill off the entire prog.
120 char string[VM_STRINGTEMP_LENGTH];
122 VM_VarString(0, string, sizeof(string));
123 Con_Printf("======%s ERROR in %s:\n%s\n", PRVM_NAME, PRVM_GetString(prog->xfunction->s_name), string);
126 ed = PRVM_G_EDICT(prog->self->ofs);
130 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);
137 Dumps out self, then an error message. The program is aborted and self is
138 removed, but the level can continue.
143 void VM_objerror (void)
146 char string[VM_STRINGTEMP_LENGTH];
148 VM_VarString(0, string, sizeof(string));
149 Con_Printf("======OBJECT ERROR======\n"); // , PRVM_NAME, PRVM_GetString(prog->xfunction->s_name), string); // or include them? FIXME
152 ed = PRVM_G_EDICT (prog->self->ofs);
158 // objerror has to display the object fields -> else call
159 PRVM_ERROR ("VM_objecterror: self not defined !");
160 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);
165 VM_print (actually used only by client and menu)
174 char string[VM_STRINGTEMP_LENGTH];
176 VM_VarString(0, string, sizeof(string));
184 broadcast print to everyone on server
189 void VM_bprint (void)
191 char string[VM_STRINGTEMP_LENGTH];
195 VM_Warning("VM_bprint: game is not server(%s) !\n", PRVM_NAME);
199 VM_VarString(0, string, sizeof(string));
200 SV_BroadcastPrint(string);
205 VM_sprint (menu & client but only if server.active == true)
207 single print to a specific client
209 sprint(float clientnum,...[string])
212 void VM_sprint (void)
216 char string[VM_STRINGTEMP_LENGTH];
218 //find client for this entity
219 clientnum = (int)PRVM_G_FLOAT(OFS_PARM0);
220 if (!sv.active || clientnum < 0 || clientnum >= svs.maxclients || !svs.clients[clientnum].active)
222 VM_Warning("VM_sprint: %s: invalid client or server is not active !\n", PRVM_NAME);
226 client = svs.clients + clientnum;
227 if (!client->netconnection)
230 VM_VarString(1, string, sizeof(string));
231 MSG_WriteChar(&client->netconnection->message,svc_print);
232 MSG_WriteString(&client->netconnection->message, string);
239 single print to the screen
241 centerprint(clientent, value)
244 void VM_centerprint (void)
246 char string[VM_STRINGTEMP_LENGTH];
248 VM_VarString(0, string, sizeof(string));
249 SCR_CenterPrint(string);
256 vector normalize(vector)
259 void VM_normalize (void)
265 VM_SAFEPARMCOUNT(1,VM_normalize);
267 value1 = PRVM_G_VECTOR(OFS_PARM0);
269 f = VectorLength2(value1);
273 VectorScale(value1, f, newvalue);
276 VectorClear(newvalue);
278 VectorCopy (newvalue, PRVM_G_VECTOR(OFS_RETURN));
290 VM_SAFEPARMCOUNT(1,VM_vlen);
291 PRVM_G_FLOAT(OFS_RETURN) = VectorLength(PRVM_G_VECTOR(OFS_PARM0));
298 float vectoyaw(vector)
301 void VM_vectoyaw (void)
306 VM_SAFEPARMCOUNT(1,VM_vectoyaw);
308 value1 = PRVM_G_VECTOR(OFS_PARM0);
310 if (value1[1] == 0 && value1[0] == 0)
314 yaw = (int) (atan2(value1[1], value1[0]) * 180 / M_PI);
319 PRVM_G_FLOAT(OFS_RETURN) = yaw;
327 vector vectoangles(vector)
330 void VM_vectoangles (void)
336 VM_SAFEPARMCOUNT(1,VM_vectoangles);
338 value1 = PRVM_G_VECTOR(OFS_PARM0);
340 if (value1[1] == 0 && value1[0] == 0)
350 // LordHavoc: optimized a bit
353 yaw = (atan2(value1[1], value1[0]) * 180 / M_PI);
357 else if (value1[1] > 0)
362 forward = sqrt(value1[0]*value1[0] + value1[1]*value1[1]);
363 pitch = (atan2(value1[2], forward) * 180 / M_PI);
368 PRVM_G_FLOAT(OFS_RETURN+0) = pitch;
369 PRVM_G_FLOAT(OFS_RETURN+1) = yaw;
370 PRVM_G_FLOAT(OFS_RETURN+2) = 0;
377 Returns a number from 0<= num < 1
382 void VM_random (void)
384 VM_SAFEPARMCOUNT(0,VM_random);
386 PRVM_G_FLOAT(OFS_RETURN) = lhrandom(0, 1);
393 Each entity can have eight independant sound sources, like voice,
396 Channel 0 is an auto-allocate channel, the others override anything
397 already running on that entity/channel pair.
399 An attenuation of 0 will play full volume everywhere in the level.
400 Larger attenuations will drop off.
409 prvm_edict_t *entity;
413 entity = PRVM_G_EDICT(OFS_PARM0);
414 channel = PRVM_G_FLOAT(OFS_PARM1);
415 sample = PRVM_G_STRING(OFS_PARM2);
416 volume = PRVM_G_FLOAT(OFS_PARM3) * 255;
417 attenuation = PRVM_G_FLOAT(OFS_PARM4);
419 if (volume < 0 || volume > 255)
420 Host_Error ("SV_StartSound: volume = %i", volume);
422 if (attenuation < 0 || attenuation > 4)
423 Host_Error ("SV_StartSound: attenuation = %f", attenuation);
425 if (channel < 0 || channel > 7)
426 Host_Error ("SV_StartSound: channel = %i", channel);
428 SV_StartSound (entity, channel, sample, volume, attenuation);
436 localsound(string sample)
439 void VM_localsound(void)
443 VM_SAFEPARMCOUNT(1,VM_localsound);
445 s = PRVM_G_STRING(OFS_PARM0);
447 if(!S_LocalSound (s))
449 PRVM_G_FLOAT(OFS_RETURN) = -4;
450 VM_Warning("VM_localsound: Failed to play %s for %s !\n", s, PRVM_NAME);
454 PRVM_G_FLOAT(OFS_RETURN) = 1;
466 PRVM_ERROR ("%s: break statement", PRVM_NAME);
469 //============================================================================
475 Sends text over to the client's execution buffer
477 [localcmd (string, ...) or]
481 void VM_localcmd (void)
483 char string[VM_STRINGTEMP_LENGTH];
484 VM_VarString(0, string, sizeof(string));
485 Cbuf_AddText(string);
497 VM_SAFEPARMCOUNT(1,VM_cvar);
499 PRVM_G_FLOAT(OFS_RETURN) = Cvar_VariableValue(PRVM_G_STRING(OFS_PARM0));
506 const string VM_cvar_string (string)
509 void VM_cvar_string(void)
513 const char *cvar_string;
514 VM_SAFEPARMCOUNT(1,VM_cvar_string);
516 name = PRVM_G_STRING(OFS_PARM0);
519 PRVM_ERROR("VM_cvar_string: %s: null string", PRVM_NAME);
521 VM_CheckEmptyString(name);
523 out = VM_GetTempString();
525 cvar_string = Cvar_VariableString(name);
527 strlcpy(out, cvar_string, VM_STRINGTEMP_LENGTH);
529 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(out);
534 ========================
537 const string VM_cvar_defstring (string)
538 ========================
540 void VM_cvar_defstring (void)
544 const char *cvar_string;
545 VM_SAFEPARMCOUNT(1,VM_cvar_string);
547 name = PRVM_G_STRING(OFS_PARM0);
550 PRVM_ERROR("VM_cvar_defstring: %s: null string", PRVM_NAME);
552 VM_CheckEmptyString(name);
554 out = VM_GetTempString();
556 cvar_string = Cvar_VariableDefString(name);
558 strlcpy(out, cvar_string, VM_STRINGTEMP_LENGTH);
560 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(out);
566 void cvar_set (string,string)
569 void VM_cvar_set (void)
571 VM_SAFEPARMCOUNT(2,VM_cvar_set);
573 Cvar_Set(PRVM_G_STRING(OFS_PARM0), PRVM_G_STRING(OFS_PARM1));
583 void VM_dprint (void)
585 char string[VM_STRINGTEMP_LENGTH];
586 if (developer.integer)
588 VM_VarString(0, string, sizeof(string));
590 Con_Printf("%s", string);
592 Con_Printf("%s: %s", PRVM_NAME, string);
610 VM_SAFEPARMCOUNT(1, VM_ftos);
612 v = PRVM_G_FLOAT(OFS_PARM0);
614 s = VM_GetTempString();
615 if ((float)((int)v) == v)
616 sprintf(s, "%i", (int)v);
619 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(s);
634 VM_SAFEPARMCOUNT(1,VM_fabs);
636 v = PRVM_G_FLOAT(OFS_PARM0);
637 PRVM_G_FLOAT(OFS_RETURN) = fabs(v);
652 VM_SAFEPARMCOUNT(1,VM_vtos);
654 s = VM_GetTempString();
655 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]);
656 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(s);
671 VM_SAFEPARMCOUNT(1, VM_etos);
673 s = VM_GetTempString();
674 sprintf (s, "entity %i", PRVM_G_EDICTNUM(OFS_PARM0));
675 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(s);
682 float stof(...[string])
687 char string[VM_STRINGTEMP_LENGTH];
688 VM_VarString(0, string, sizeof(string));
689 PRVM_G_FLOAT(OFS_RETURN) = atof(string);
693 ========================
697 ========================
701 VM_SAFEPARMCOUNT(1, VM_itof);
702 PRVM_G_FLOAT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
706 ========================
709 entity ftoe(float num)
710 ========================
715 VM_SAFEPARMCOUNT(1, VM_ftoe);
717 ent = (int)PRVM_G_FLOAT(OFS_PARM0);
718 if (ent < 0 || ent >= MAX_EDICTS || PRVM_PROG_TO_EDICT(ent)->priv.required->free)
719 ent = 0; // return world instead of a free or invalid entity
721 PRVM_G_INT(OFS_RETURN) = ent;
735 prog->xfunction->builtinsprofile += 20;
736 ed = PRVM_ED_Alloc();
748 void VM_remove (void)
751 prog->xfunction->builtinsprofile += 20;
753 VM_SAFEPARMCOUNT(1, VM_remove);
755 ed = PRVM_G_EDICT(OFS_PARM0);
756 if( PRVM_NUM_FOR_EDICT(ed) <= prog->reserved_edicts )
758 if (developer.integer >= 1)
759 VM_Warning( "VM_remove: tried to remove the null entity or a reserved entity!\n" );
761 else if( ed->priv.required->free )
763 if (developer.integer >= 1)
764 VM_Warning( "VM_remove: tried to remove an already freed entity!\n" );
768 // if (ed == prog->edicts)
769 // PRVM_ERROR ("remove: tried to remove world");
770 // if (PRVM_NUM_FOR_EDICT(ed) <= sv.maxclients)
771 // Host_Error("remove: tried to remove a client");
778 entity find(entity start, .string field, string match)
789 VM_SAFEPARMCOUNT(3,VM_find);
791 e = PRVM_G_EDICTNUM(OFS_PARM0);
792 f = PRVM_G_INT(OFS_PARM1);
793 s = PRVM_G_STRING(OFS_PARM2);
795 // LordHavoc: apparently BloodMage does a find(world, weaponmodel, "") and
796 // expects it to find all the monsters, so we must be careful to support
801 for (e++ ; e < prog->num_edicts ; e++)
803 prog->xfunction->builtinsprofile++;
804 ed = PRVM_EDICT_NUM(e);
805 if (ed->priv.required->free)
807 t = PRVM_E_STRING(ed,f);
817 VM_RETURN_EDICT(prog->edicts);
824 entity findfloat(entity start, .float field, float match)
825 entity findentity(entity start, .entity field, entity match)
828 // LordHavoc: added this for searching float, int, and entity reference fields
829 void VM_findfloat (void)
836 VM_SAFEPARMCOUNT(3,VM_findfloat);
838 e = PRVM_G_EDICTNUM(OFS_PARM0);
839 f = PRVM_G_INT(OFS_PARM1);
840 s = PRVM_G_FLOAT(OFS_PARM2);
842 for (e++ ; e < prog->num_edicts ; e++)
844 prog->xfunction->builtinsprofile++;
845 ed = PRVM_EDICT_NUM(e);
846 if (ed->priv.required->free)
848 if (PRVM_E_FLOAT(ed,f) == s)
855 VM_RETURN_EDICT(prog->edicts);
862 entity findchain(.string field, string match)
865 // chained search for strings in entity fields
866 // entity(.string field, string match) findchain = #402;
867 void VM_findchain (void)
873 prvm_edict_t *ent, *chain;
875 VM_SAFEPARMCOUNT(2,VM_findchain);
877 // is the same like !(prog->flag & PRVM_FE_CHAIN) - even if the operator precedence is another
878 if(!prog->flag & PRVM_FE_CHAIN)
879 PRVM_ERROR("VM_findchain: %s doesnt have a chain field !", PRVM_NAME);
881 chain_of = PRVM_ED_FindField("chain")->ofs;
883 chain = prog->edicts;
885 f = PRVM_G_INT(OFS_PARM0);
886 s = PRVM_G_STRING(OFS_PARM1);
888 // LordHavoc: apparently BloodMage does a find(world, weaponmodel, "") and
889 // expects it to find all the monsters, so we must be careful to support
894 ent = PRVM_NEXT_EDICT(prog->edicts);
895 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
897 prog->xfunction->builtinsprofile++;
898 if (ent->priv.required->free)
900 t = PRVM_E_STRING(ent,f);
906 PRVM_E_INT(ent,chain_of) = PRVM_NUM_FOR_EDICT(chain);
910 VM_RETURN_EDICT(chain);
917 entity findchainfloat(.string field, float match)
918 entity findchainentity(.string field, entity match)
921 // LordHavoc: chained search for float, int, and entity reference fields
922 // entity(.string field, float match) findchainfloat = #403;
923 void VM_findchainfloat (void)
929 prvm_edict_t *ent, *chain;
931 VM_SAFEPARMCOUNT(2, VM_findchainfloat);
933 if(!prog->flag & PRVM_FE_CHAIN)
934 PRVM_ERROR("VM_findchainfloat: %s doesnt have a chain field !", PRVM_NAME);
936 chain_of = PRVM_ED_FindField("chain")->ofs;
938 chain = (prvm_edict_t *)prog->edicts;
940 f = PRVM_G_INT(OFS_PARM0);
941 s = PRVM_G_FLOAT(OFS_PARM1);
943 ent = PRVM_NEXT_EDICT(prog->edicts);
944 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
946 prog->xfunction->builtinsprofile++;
947 if (ent->priv.required->free)
949 if (PRVM_E_FLOAT(ent,f) != s)
952 PRVM_E_INT(ent,chain_of) = PRVM_EDICT_TO_PROG(chain);
956 VM_RETURN_EDICT(chain);
960 ========================
963 entity findflags(entity start, .float field, float match)
964 ========================
966 // LordHavoc: search for flags in float fields
967 void VM_findflags (void)
974 VM_SAFEPARMCOUNT(3, VM_findflags);
977 e = PRVM_G_EDICTNUM(OFS_PARM0);
978 f = PRVM_G_INT(OFS_PARM1);
979 s = (int)PRVM_G_FLOAT(OFS_PARM2);
981 for (e++ ; e < prog->num_edicts ; e++)
983 prog->xfunction->builtinsprofile++;
984 ed = PRVM_EDICT_NUM(e);
985 if (ed->priv.required->free)
987 if (!PRVM_E_FLOAT(ed,f))
989 if ((int)PRVM_E_FLOAT(ed,f) & s)
996 VM_RETURN_EDICT(prog->edicts);
1000 ========================
1003 entity findchainflags(.float field, float match)
1004 ========================
1006 // LordHavoc: chained search for flags in float fields
1007 void VM_findchainflags (void)
1013 prvm_edict_t *ent, *chain;
1015 VM_SAFEPARMCOUNT(2, VM_findchainflags);
1017 if(!prog->flag & PRVM_FE_CHAIN)
1018 PRVM_ERROR("VM_findchainflags: %s doesnt have a chain field !", PRVM_NAME);
1020 chain_of = PRVM_ED_FindField("chain")->ofs;
1022 chain = (prvm_edict_t *)prog->edicts;
1024 f = PRVM_G_INT(OFS_PARM0);
1025 s = (int)PRVM_G_FLOAT(OFS_PARM1);
1027 ent = PRVM_NEXT_EDICT(prog->edicts);
1028 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
1030 prog->xfunction->builtinsprofile++;
1031 if (ent->priv.required->free)
1033 if (!PRVM_E_FLOAT(ent,f))
1035 if (!((int)PRVM_E_FLOAT(ent,f) & s))
1038 PRVM_E_INT(ent,chain_of) = PRVM_EDICT_TO_PROG(chain);
1042 VM_RETURN_EDICT(chain);
1052 void VM_coredump (void)
1054 VM_SAFEPARMCOUNT(0,VM_coredump);
1056 Cbuf_AddText("prvm_edicts ");
1057 Cbuf_AddText(PRVM_NAME);
1068 void PRVM_StackTrace(void);
1069 void VM_stackdump (void)
1071 VM_SAFEPARMCOUNT(0, VM_stackdump);
1086 VM_SAFEPARMCOUNT(0, VM_crash);
1088 PRVM_ERROR("Crash called by %s",PRVM_NAME);
1098 void VM_traceon (void)
1100 VM_SAFEPARMCOUNT(0,VM_traceon);
1112 void VM_traceoff (void)
1114 VM_SAFEPARMCOUNT(0,VM_traceoff);
1116 prog->trace = false;
1126 void VM_eprint (void)
1128 VM_SAFEPARMCOUNT(1,VM_eprint);
1130 PRVM_ED_PrintNum (PRVM_G_EDICTNUM(OFS_PARM0));
1143 VM_SAFEPARMCOUNT(1,VM_rint);
1145 f = PRVM_G_FLOAT(OFS_PARM0);
1147 PRVM_G_FLOAT(OFS_RETURN) = floor(f + 0.5);
1149 PRVM_G_FLOAT(OFS_RETURN) = ceil(f - 0.5);
1159 void VM_floor (void)
1161 VM_SAFEPARMCOUNT(1,VM_floor);
1163 PRVM_G_FLOAT(OFS_RETURN) = floor(PRVM_G_FLOAT(OFS_PARM0));
1175 VM_SAFEPARMCOUNT(1,VM_ceil);
1177 PRVM_G_FLOAT(OFS_RETURN) = ceil(PRVM_G_FLOAT(OFS_PARM0));
1185 entity nextent(entity)
1188 void VM_nextent (void)
1193 i = PRVM_G_EDICTNUM(OFS_PARM0);
1196 prog->xfunction->builtinsprofile++;
1198 if (i == prog->num_edicts)
1200 VM_RETURN_EDICT(prog->edicts);
1203 ent = PRVM_EDICT_NUM(i);
1204 if (!ent->priv.required->free)
1206 VM_RETURN_EDICT(ent);
1212 //=============================================================================
1219 changelevel(string map)
1222 void VM_changelevel (void)
1226 VM_SAFEPARMCOUNT(1, VM_changelevel);
1230 VM_Warning("VM_changelevel: game is not server (%s)\n", PRVM_NAME);
1234 // make sure we don't issue two changelevels
1235 if (svs.changelevel_issued)
1237 svs.changelevel_issued = true;
1239 s = PRVM_G_STRING(OFS_PARM0);
1240 Cbuf_AddText (va("changelevel %s\n",s));
1252 VM_SAFEPARMCOUNT(1,VM_sin);
1253 PRVM_G_FLOAT(OFS_RETURN) = sin(PRVM_G_FLOAT(OFS_PARM0));
1264 VM_SAFEPARMCOUNT(1,VM_cos);
1265 PRVM_G_FLOAT(OFS_RETURN) = cos(PRVM_G_FLOAT(OFS_PARM0));
1277 VM_SAFEPARMCOUNT(1,VM_sqrt);
1278 PRVM_G_FLOAT(OFS_RETURN) = sqrt(PRVM_G_FLOAT(OFS_PARM0));
1290 VM_SAFEPARMCOUNT(1,VM_asin);
1291 PRVM_G_FLOAT(OFS_RETURN) = asin(PRVM_G_FLOAT(OFS_PARM0));
1302 VM_SAFEPARMCOUNT(1,VM_acos);
1303 PRVM_G_FLOAT(OFS_RETURN) = acos(PRVM_G_FLOAT(OFS_PARM0));
1314 VM_SAFEPARMCOUNT(1,VM_atan);
1315 PRVM_G_FLOAT(OFS_RETURN) = atan(PRVM_G_FLOAT(OFS_PARM0));
1321 float atan2(float,float)
1324 void VM_atan2 (void)
1326 VM_SAFEPARMCOUNT(2,VM_atan2);
1327 PRVM_G_FLOAT(OFS_RETURN) = atan2(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1338 VM_SAFEPARMCOUNT(1,VM_tan);
1339 PRVM_G_FLOAT(OFS_RETURN) = tan(PRVM_G_FLOAT(OFS_PARM0));
1346 Returns a vector of length < 1 and > 0
1351 void VM_randomvec (void)
1356 VM_SAFEPARMCOUNT(0, VM_randomvec);
1361 temp[0] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1362 temp[1] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1363 temp[2] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1365 while (DotProduct(temp, temp) >= 1);
1366 VectorCopy (temp, PRVM_G_VECTOR(OFS_RETURN));
1369 temp[0] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1370 temp[1] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1371 temp[2] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1372 // length returned always > 0
1373 length = (rand()&32766 + 1) * (1.0 / 32767.0) / VectorLength(temp);
1374 VectorScale(temp,length, temp);*/
1375 //VectorCopy(temp, PRVM_G_VECTOR(OFS_RETURN));
1378 //=============================================================================
1384 float registercvar (string name, string value, float flags)
1387 void VM_registercvar (void)
1389 const char *name, *value;
1392 VM_SAFEPARMCOUNT(3,VM_registercvar);
1394 name = PRVM_G_STRING(OFS_PARM0);
1395 value = PRVM_G_STRING(OFS_PARM1);
1396 flags = (int)PRVM_G_FLOAT(OFS_PARM2);
1397 PRVM_G_FLOAT(OFS_RETURN) = 0;
1399 if(flags > CVAR_MAXFLAGSVAL)
1402 // first check to see if it has already been defined
1403 if (Cvar_FindVar (name))
1406 // check for overlap with a command
1407 if (Cmd_Exists (name))
1409 VM_Warning("VM_registercvar: %s is a command\n", name);
1413 Cvar_Get(name, value, flags);
1415 PRVM_G_FLOAT(OFS_RETURN) = 1; // success
1422 returns the minimum of two supplied floats
1424 float min(float a, float b, ...[float])
1429 // LordHavoc: 3+ argument enhancement suggested by FrikaC
1430 if (prog->argc == 2)
1431 PRVM_G_FLOAT(OFS_RETURN) = min(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1432 else if (prog->argc >= 3)
1435 float f = PRVM_G_FLOAT(OFS_PARM0);
1436 for (i = 1;i < prog->argc;i++)
1437 if (PRVM_G_FLOAT((OFS_PARM0+i*3)) < f)
1438 f = PRVM_G_FLOAT((OFS_PARM0+i*3));
1439 PRVM_G_FLOAT(OFS_RETURN) = f;
1442 PRVM_ERROR("VM_min: %s must supply at least 2 floats", PRVM_NAME);
1449 returns the maximum of two supplied floats
1451 float max(float a, float b, ...[float])
1456 // LordHavoc: 3+ argument enhancement suggested by FrikaC
1457 if (prog->argc == 2)
1458 PRVM_G_FLOAT(OFS_RETURN) = max(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1459 else if (prog->argc >= 3)
1462 float f = PRVM_G_FLOAT(OFS_PARM0);
1463 for (i = 1;i < prog->argc;i++)
1464 if (PRVM_G_FLOAT((OFS_PARM0+i*3)) > f)
1465 f = PRVM_G_FLOAT((OFS_PARM0+i*3));
1466 PRVM_G_FLOAT(OFS_RETURN) = f;
1469 PRVM_ERROR("VM_max: %s must supply at least 2 floats", PRVM_NAME);
1476 returns number bounded by supplied range
1478 float bound(float min, float value, float max)
1481 void VM_bound (void)
1483 VM_SAFEPARMCOUNT(3,VM_bound);
1484 PRVM_G_FLOAT(OFS_RETURN) = bound(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1), PRVM_G_FLOAT(OFS_PARM2));
1491 returns a raised to power b
1493 float pow(float a, float b)
1498 VM_SAFEPARMCOUNT(2,VM_pow);
1499 PRVM_G_FLOAT(OFS_RETURN) = pow(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1506 copies data from one entity to another
1508 copyentity(entity src, entity dst)
1511 void VM_copyentity (void)
1513 prvm_edict_t *in, *out;
1514 VM_SAFEPARMCOUNT(2,VM_copyentity);
1515 in = PRVM_G_EDICT(OFS_PARM0);
1516 out = PRVM_G_EDICT(OFS_PARM1);
1517 memcpy(out->fields.vp, in->fields.vp, prog->progs->entityfields * 4);
1524 sets the color of a client and broadcasts the update to all connected clients
1526 setcolor(clientent, value)
1529 /*void PF_setcolor (void)
1535 entnum = PRVM_G_EDICTNUM(OFS_PARM0);
1536 i = PRVM_G_FLOAT(OFS_PARM1);
1538 if (entnum < 1 || entnum > svs.maxclients || !svs.clients[entnum-1].active)
1540 Con_Print("tried to setcolor a non-client\n");
1544 client = svs.clients + entnum-1;
1545 if ((val = PRVM_GETEDICTFIELDVALUE(client->edict, eval_clientcolors)))
1548 client->old_colors = i;
1549 client->edict->fields.server->team = (i & 15) + 1;
1551 MSG_WriteByte (&sv.reliable_datagram, svc_updatecolors);
1552 MSG_WriteByte (&sv.reliable_datagram, entnum - 1);
1553 MSG_WriteByte (&sv.reliable_datagram, i);
1556 void VM_Files_Init(void)
1559 for (i = 0;i < PRVM_MAX_OPENFILES;i++)
1560 prog->openfiles[i] = NULL;
1563 void VM_Files_CloseAll(void)
1566 for (i = 0;i < PRVM_MAX_OPENFILES;i++)
1568 if (prog->openfiles[i])
1569 FS_Close(prog->openfiles[i]);
1570 prog->openfiles[i] = NULL;
1574 qfile_t *VM_GetFileHandle( int index )
1576 if (index < 0 || index >= PRVM_MAX_OPENFILES)
1578 Con_Printf("VM_GetFileHandle: invalid file handle %i used in %s\n", index, PRVM_NAME);
1581 if (prog->openfiles[index] == NULL)
1583 Con_Printf("VM_GetFileHandle: no such file handle %i (or file has been closed) in %s\n", index, PRVM_NAME);
1586 return prog->openfiles[index];
1593 float fopen(string filename, float mode)
1596 // float(string filename, float mode) fopen = #110;
1597 // opens a file inside quake/gamedir/data/ (mode is FILE_READ, FILE_APPEND, or FILE_WRITE),
1598 // returns fhandle >= 0 if successful, or fhandle < 0 if unable to open file for any reason
1602 const char *modestring, *filename;
1604 VM_SAFEPARMCOUNT(2,VM_fopen);
1606 for (filenum = 0;filenum < PRVM_MAX_OPENFILES;filenum++)
1607 if (prog->openfiles[filenum] == NULL)
1609 if (filenum >= PRVM_MAX_OPENFILES)
1611 PRVM_G_FLOAT(OFS_RETURN) = -2;
1612 VM_Warning("VM_fopen: %s ran out of file handles (%i)\n", PRVM_NAME, PRVM_MAX_OPENFILES);
1615 mode = (int)PRVM_G_FLOAT(OFS_PARM1);
1618 case 0: // FILE_READ
1621 case 1: // FILE_APPEND
1624 case 2: // FILE_WRITE
1628 PRVM_G_FLOAT(OFS_RETURN) = -3;
1629 VM_Warning("VM_fopen: %s: no such mode %i (valid: 0 = read, 1 = append, 2 = write)\n", PRVM_NAME, mode);
1632 filename = PRVM_G_STRING(OFS_PARM0);
1634 prog->openfiles[filenum] = FS_Open(va("data/%s", filename), modestring, false, false);
1635 if (prog->openfiles[filenum] == NULL && mode == 0)
1636 prog->openfiles[filenum] = FS_Open(va("%s", filename), modestring, false, false);
1638 if (prog->openfiles[filenum] == NULL)
1640 PRVM_G_FLOAT(OFS_RETURN) = -1;
1641 if (developer.integer >= 100)
1642 VM_Warning("VM_fopen: %s: %s mode %s failed\n", PRVM_NAME, filename, modestring);
1646 PRVM_G_FLOAT(OFS_RETURN) = filenum;
1647 if (developer.integer >= 100)
1648 Con_Printf("VM_fopen: %s: %s mode %s opened as #%i\n", PRVM_NAME, filename, modestring, filenum);
1656 fclose(float fhandle)
1659 //void(float fhandle) fclose = #111; // closes a file
1660 void VM_fclose(void)
1664 VM_SAFEPARMCOUNT(1,VM_fclose);
1666 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1667 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
1669 VM_Warning("VM_fclose: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1672 if (prog->openfiles[filenum] == NULL)
1674 VM_Warning("VM_fclose: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1677 FS_Close(prog->openfiles[filenum]);
1678 prog->openfiles[filenum] = NULL;
1679 if (developer.integer >= 100)
1680 Con_Printf("VM_fclose: %s: #%i closed\n", PRVM_NAME, filenum);
1687 string fgets(float fhandle)
1690 //string(float fhandle) fgets = #112; // reads a line of text from the file and returns as a tempstring
1694 static char string[VM_STRINGTEMP_LENGTH];
1697 VM_SAFEPARMCOUNT(1,VM_fgets);
1699 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1700 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
1702 VM_Warning("VM_fgets: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1705 if (prog->openfiles[filenum] == NULL)
1707 VM_Warning("VM_fgets: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1713 c = FS_Getc(prog->openfiles[filenum]);
1714 if (c == '\r' || c == '\n' || c < 0)
1716 if (end < VM_STRINGTEMP_LENGTH - 1)
1720 // remove \n following \r
1723 c = FS_Getc(prog->openfiles[filenum]);
1725 FS_UnGetc(prog->openfiles[filenum], (unsigned char)c);
1727 if (developer.integer >= 100)
1728 Con_Printf("fgets: %s: %s\n", PRVM_NAME, string);
1730 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(string);
1732 PRVM_G_INT(OFS_RETURN) = 0;
1739 fputs(float fhandle, string s)
1742 //void(float fhandle, string s) fputs = #113; // writes a line of text to the end of the file
1746 char string[VM_STRINGTEMP_LENGTH];
1749 VM_SAFEPARMCOUNT(2,VM_fputs);
1751 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1752 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
1754 VM_Warning("VM_fputs: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1757 if (prog->openfiles[filenum] == NULL)
1759 VM_Warning("VM_fputs: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1762 VM_VarString(1, string, sizeof(string));
1763 if ((stringlength = (int)strlen(string)))
1764 FS_Write(prog->openfiles[filenum], string, stringlength);
1765 if (developer.integer >= 100)
1766 Con_Printf("fputs: %s: %s\n", PRVM_NAME, string);
1773 float strlen(string s)
1776 //float(string s) strlen = #114; // returns how many characters are in a string
1777 void VM_strlen(void)
1781 VM_SAFEPARMCOUNT(1,VM_strlen);
1783 s = PRVM_G_STRING(OFS_PARM0);
1785 PRVM_G_FLOAT(OFS_RETURN) = strlen(s);
1787 PRVM_G_FLOAT(OFS_RETURN) = 0;
1790 // DRESK - Decolorized String
1795 string strdecolorize(string s)
1798 // string (string s) strdecolorize = #472; // returns the passed in string with color codes stripped
1799 void VM_strdecolorize(void)
1802 const char *szString;
1813 VM_SAFEPARMCOUNT(1,VM_strdecolorize);
1814 szString = PRVM_G_STRING(OFS_PARM0);
1815 szNewString = VM_GetTempString();
1818 { // Traverse through String
1819 if( szString[nPos] == '\n' || szString[nPos] == '\r' || szString[nPos] <= 0)
1820 { // String End Found
1821 szNewString[nFillPos++] = szString[nPos];
1825 if( szString[nPos] == STRING_COLOR_TAG)
1826 { // Color Code Located
1827 if( szString[nPos + 1] == STRING_COLOR_TAG)
1828 { // Valid Characters to Include
1829 szNewString[nFillPos++] = szString[nPos];
1831 szNewString[nFillPos++] = szString[nPos];
1834 if( szString[nPos + 1] >= '0' && szString[nPos + 1] <= '9' )
1835 { // Color Code Found; Increment Position
1839 { // Unknown Color Code; Include
1840 szNewString[nFillPos++] = szString[nPos];
1845 // Include Character
1846 szNewString[nFillPos++] = szString[nPos];
1848 // Increment Position
1852 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(szNewString);
1855 // DRESK - String Length (not counting color codes)
1860 float strlennocol(string s)
1863 // float(string s) strlennocol = #471; // returns how many characters are in a string not including color codes
1864 // For example, ^2Dresk returns a length of 5
1865 void VM_strlennocol(void)
1867 const char *szString;
1875 VM_SAFEPARMCOUNT(1,VM_strlennocol);
1877 szString = PRVM_G_STRING(OFS_PARM0);
1881 { // Count Characters
1882 // SV_BroadcastPrintf("Position '%d'; Character '%c'; Length '%d'\n", nPos, szString[nPos], nCnt);
1884 if( szString[nPos] == '\n' || szString[nPos] == '\r' || szString[nPos] <= 0)
1885 { // String End Found
1886 // SV_BroadcastPrintf("Found End of String at '%d'\n", nPos);
1890 if( szString[nPos] == STRING_COLOR_TAG)
1891 { // Color Code Located
1892 if( szString[nPos + 1] == STRING_COLOR_TAG)
1893 { // Increment Length; Skip Color Code
1898 if( szString[nPos + 1] >= '0' && szString[nPos + 1] <= '9' )
1899 { // Color Code Found; Increment Position
1900 // SV_BroadcastPrintf("Found Color Codes at '%d'\n", nPos);
1904 { // Unknown Color Code; Increment Length!
1910 // Increment String Length
1913 // Increment Position
1916 PRVM_G_FLOAT(OFS_RETURN) = nCnt;
1919 PRVM_G_FLOAT(OFS_RETURN) = 0;
1926 string strcat(string,string,...[string])
1929 //string(string s1, string s2) strcat = #115;
1930 // concatenates two strings (for example "abc", "def" would return "abcdef")
1931 // and returns as a tempstring
1932 void VM_strcat(void)
1937 PRVM_ERROR("VM_strcat wrong parameter count (min. 1 expected ) !");
1939 s = VM_GetTempString();
1940 VM_VarString(0, s, VM_STRINGTEMP_LENGTH);
1941 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(s);
1948 string substring(string s, float start, float length)
1951 // string(string s, float start, float length) substring = #116;
1952 // returns a section of a string as a tempstring
1953 void VM_substring(void)
1955 int i, start, length;
1959 VM_SAFEPARMCOUNT(3,VM_substring);
1961 string = VM_GetTempString();
1962 s = PRVM_G_STRING(OFS_PARM0);
1963 start = (int)PRVM_G_FLOAT(OFS_PARM1);
1964 length = (int)PRVM_G_FLOAT(OFS_PARM2);
1967 for (i = 0;i < start && *s;i++, s++);
1968 for (i = 0;i < VM_STRINGTEMP_LENGTH - 1 && *s && i < length;i++, s++)
1971 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(string);
1978 vector stov(string s)
1981 //vector(string s) stov = #117; // returns vector value from a string
1984 char string[VM_STRINGTEMP_LENGTH];
1986 VM_SAFEPARMCOUNT(1,VM_stov);
1988 VM_VarString(0, string, sizeof(string));
1989 Math_atov(string, PRVM_G_VECTOR(OFS_RETURN));
1996 string strzone(string s)
1999 //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)
2000 void VM_strzone(void)
2003 char string[VM_STRINGTEMP_LENGTH];
2006 VM_SAFEPARMCOUNT(1,VM_strzone);
2008 VM_VarString(0, string, sizeof(string));
2009 alloclen = strlen(string) + 1;
2010 PRVM_G_INT(OFS_RETURN) = PRVM_AllocString(alloclen, &out);
2011 memcpy(out, string, alloclen);
2021 //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!!!)
2022 void VM_strunzone(void)
2024 VM_SAFEPARMCOUNT(1,VM_strunzone);
2025 PRVM_FreeString(PRVM_G_INT(OFS_PARM0));
2030 VM_command (used by client and menu)
2032 clientcommand(float client, string s) (for client and menu)
2035 //void(entity e, string s) clientcommand = #440; // executes a command string as if it came from the specified client
2036 //this function originally written by KrimZon, made shorter by LordHavoc
2037 void VM_clcommand (void)
2039 client_t *temp_client;
2042 VM_SAFEPARMCOUNT(2,VM_clcommand);
2044 i = (int)PRVM_G_FLOAT(OFS_PARM0);
2045 if (!sv.active || i < 0 || i >= svs.maxclients || !svs.clients[i].active)
2047 VM_Warning("VM_clientcommand: %s: invalid client/server is not active !\n", PRVM_NAME);
2051 temp_client = host_client;
2052 host_client = svs.clients + i;
2053 Cmd_ExecuteString (PRVM_G_STRING(OFS_PARM1), src_client);
2054 host_client = temp_client;
2062 float tokenize(string s)
2065 //float(string s) tokenize = #441; // takes apart a string into individal words (access them with argv), returns how many
2066 //this function originally written by KrimZon, made shorter by LordHavoc
2067 //20040203: rewritten by LordHavoc (no longer uses allocations)
2069 char *tokens[256], tokenbuf[MAX_INPUTLINE];
2070 void VM_tokenize (void)
2075 VM_SAFEPARMCOUNT(1,VM_tokenize);
2077 p = PRVM_G_STRING(OFS_PARM0);
2081 while(COM_ParseToken(&p, false))
2084 if (num_tokens >= (int)(sizeof(tokens)/sizeof(tokens[0])))
2086 tokenlen = strlen(com_token) + 1;
2087 if (pos + tokenlen > sizeof(tokenbuf))
2089 tokens[num_tokens++] = tokenbuf + pos;
2090 memcpy(tokenbuf + pos, com_token, tokenlen);
2094 PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
2097 //string(float n) argv = #442; // returns a word from the tokenized string (returns nothing for an invalid index)
2098 //this function originally written by KrimZon, made shorter by LordHavoc
2103 VM_SAFEPARMCOUNT(1,VM_argv);
2105 token_num = (int)PRVM_G_FLOAT(OFS_PARM0);
2107 if (token_num >= 0 && token_num < num_tokens)
2108 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(tokens[token_num]);
2110 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(NULL);
2114 //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)
2115 void PF_setattachment (void)
2117 prvm_edict_t *e = PRVM_G_EDICT(OFS_PARM0);
2118 prvm_edict_t *tagentity = PRVM_G_EDICT(OFS_PARM1);
2119 char *tagname = PRVM_G_STRING(OFS_PARM2);
2124 if (tagentity == NULL)
2125 tagentity = prog->edicts;
2127 v = PRVM_GETEDICTFIELDVALUE(e, eval_tag_entity);
2129 fields.server->edict = PRVM_EDICT_TO_PROG(tagentity);
2131 v = PRVM_GETEDICTFIELDVALUE(e, eval_tag_index);
2133 fields.server->_float = 0;
2134 if (tagentity != NULL && tagentity != prog->edicts && tagname && tagname[0])
2136 modelindex = (int)tagentity->fields.server->modelindex;
2137 if (modelindex >= 0 && modelindex < MAX_MODELS)
2139 model = sv.models[modelindex];
2140 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)
2141 for (i = 0;i < model->data_overridetagnamesforskin[(unsigned int)tagentity->fields.server->skin].num_overridetagnames;i++)
2142 if (!strcmp(tagname, model->data_overridetagnamesforskin[(unsigned int)tagentity->fields.server->skin].data_overridetagnames[i].name))
2143 fields.server->_float = i + 1;
2144 // FIXME: use a model function to get tag info (need to handle skeletal)
2145 if (fields.server->_float == 0 && model->num_tags)
2146 for (i = 0;i < model->num_tags;i++)
2147 if (!strcmp(tagname, model->data_tags[i].name))
2148 fields.server->_float = i + 1;
2149 if (fields.server->_float == 0)
2150 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);
2153 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));
2164 void VM_isserver(void)
2166 VM_SAFEPARMCOUNT(0,VM_serverstate);
2168 PRVM_G_FLOAT(OFS_RETURN) = sv.active;
2178 void VM_clientcount(void)
2180 VM_SAFEPARMCOUNT(0,VM_clientcount);
2182 PRVM_G_FLOAT(OFS_RETURN) = svs.maxclients;
2192 void VM_clientstate(void)
2194 VM_SAFEPARMCOUNT(0,VM_clientstate);
2196 PRVM_G_FLOAT(OFS_RETURN) = cls.state;
2203 float getostype(void)
2205 */ // not used at the moment -> not included in the common list
2206 void VM_getostype(void)
2208 VM_SAFEPARMCOUNT(0,VM_getostype);
2213 OS_MAC - not supported
2217 PRVM_G_FLOAT(OFS_RETURN) = 0;
2218 #elif defined(MACOSX)
2219 PRVM_G_FLOAT(OFS_RETURN) = 2;
2221 PRVM_G_FLOAT(OFS_RETURN) = 1;
2229 vector getmousepos()
2232 void VM_getmousepos(void)
2235 VM_SAFEPARMCOUNT(0,VM_getmousepos);
2237 PRVM_G_VECTOR(OFS_RETURN)[0] = in_mouse_x * vid_conwidth.integer / vid.width;
2238 PRVM_G_VECTOR(OFS_RETURN)[1] = in_mouse_y * vid_conheight.integer / vid.height;
2239 PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
2249 void VM_gettime(void)
2251 VM_SAFEPARMCOUNT(0,VM_gettime);
2253 PRVM_G_FLOAT(OFS_RETURN) = (float) *prog->time;
2260 loadfromdata(string data)
2263 void VM_loadfromdata(void)
2265 VM_SAFEPARMCOUNT(1,VM_loadentsfromfile);
2267 PRVM_ED_LoadFromFile(PRVM_G_STRING(OFS_PARM0));
2271 ========================
2274 parseentitydata(entity ent, string data)
2275 ========================
2277 void VM_parseentitydata(void)
2282 VM_SAFEPARMCOUNT(2, VM_parseentitydata);
2284 // get edict and test it
2285 ent = PRVM_G_EDICT(OFS_PARM0);
2286 if (ent->priv.required->free)
2287 PRVM_ERROR ("VM_parseentitydata: %s: Can only set already spawned entities (entity %i is free)!", PRVM_NAME, PRVM_NUM_FOR_EDICT(ent));
2289 data = PRVM_G_STRING(OFS_PARM1);
2291 // parse the opening brace
2292 if (!COM_ParseTokenConsole(&data) || com_token[0] != '{' )
2293 PRVM_ERROR ("VM_parseentitydata: %s: Couldn't parse entity data:\n%s", PRVM_NAME, data );
2295 PRVM_ED_ParseEdict (data, ent);
2302 loadfromfile(string file)
2305 void VM_loadfromfile(void)
2307 const char *filename;
2310 VM_SAFEPARMCOUNT(1,VM_loadfromfile);
2312 filename = PRVM_G_STRING(OFS_PARM0);
2313 // .. is parent directory on many platforms
2314 // / is parent directory on Amiga
2315 // : is root of drive on Amiga (also used as a directory separator on Mac, but / works there too, so that's a bad idea)
2316 // \ is a windows-ism (so it's naughty to use it, / works on all platforms)
2317 if ((filename[0] == '.' && filename[1] == '.') || filename[0] == '/' || strrchr(filename, ':') || strrchr(filename, '\\'))
2319 PRVM_G_FLOAT(OFS_RETURN) = -4;
2320 VM_Warning("VM_loadfromfile: %s dangerous or non-portable filename \"%s\" not allowed. (contains : or \\ or begins with .. or /)\n", PRVM_NAME, filename);
2324 // not conform with VM_fopen
2325 data = (char *)FS_LoadFile(filename, tempmempool, false, NULL);
2327 PRVM_G_FLOAT(OFS_RETURN) = -1;
2329 PRVM_ED_LoadFromFile(data);
2340 float mod(float val, float m)
2343 void VM_modulo(void)
2346 VM_SAFEPARMCOUNT(2,VM_module);
2348 val = (int) PRVM_G_FLOAT(OFS_PARM0);
2349 m = (int) PRVM_G_FLOAT(OFS_PARM1);
2351 PRVM_G_FLOAT(OFS_RETURN) = (float) (val % m);
2354 void VM_Search_Init(void)
2357 for (i = 0;i < PRVM_MAX_OPENSEARCHES;i++)
2358 prog->opensearches[i] = NULL;
2361 void VM_Search_Reset(void)
2364 // reset the fssearch list
2365 for(i = 0; i < PRVM_MAX_OPENSEARCHES; i++)
2367 if(prog->opensearches[i])
2368 FS_FreeSearch(prog->opensearches[i]);
2369 prog->opensearches[i] = NULL;
2377 float search_begin(string pattern, float caseinsensitive, float quiet)
2380 void VM_search_begin(void)
2383 const char *pattern;
2384 int caseinsens, quiet;
2386 VM_SAFEPARMCOUNT(3, VM_search_begin);
2388 pattern = PRVM_G_STRING(OFS_PARM0);
2390 VM_CheckEmptyString(pattern);
2392 caseinsens = (int)PRVM_G_FLOAT(OFS_PARM1);
2393 quiet = (int)PRVM_G_FLOAT(OFS_PARM2);
2395 for(handle = 0; handle < PRVM_MAX_OPENSEARCHES; handle++)
2396 if(!prog->opensearches[handle])
2399 if(handle >= PRVM_MAX_OPENSEARCHES)
2401 PRVM_G_FLOAT(OFS_RETURN) = -2;
2402 VM_Warning("VM_search_begin: %s ran out of search handles (%i)\n", PRVM_NAME, PRVM_MAX_OPENSEARCHES);
2406 if(!(prog->opensearches[handle] = FS_Search(pattern,caseinsens, quiet)))
2407 PRVM_G_FLOAT(OFS_RETURN) = -1;
2409 PRVM_G_FLOAT(OFS_RETURN) = handle;
2416 void search_end(float handle)
2419 void VM_search_end(void)
2422 VM_SAFEPARMCOUNT(1, VM_search_end);
2424 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
2426 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
2428 VM_Warning("VM_search_end: invalid handle %i used in %s\n", handle, PRVM_NAME);
2431 if(prog->opensearches[handle] == NULL)
2433 VM_Warning("VM_search_end: no such handle %i in %s\n", handle, PRVM_NAME);
2437 FS_FreeSearch(prog->opensearches[handle]);
2438 prog->opensearches[handle] = NULL;
2445 float search_getsize(float handle)
2448 void VM_search_getsize(void)
2451 VM_SAFEPARMCOUNT(1, VM_M_search_getsize);
2453 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
2455 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
2457 VM_Warning("VM_search_getsize: invalid handle %i used in %s\n", handle, PRVM_NAME);
2460 if(prog->opensearches[handle] == NULL)
2462 VM_Warning("VM_search_getsize: no such handle %i in %s\n", handle, PRVM_NAME);
2466 PRVM_G_FLOAT(OFS_RETURN) = prog->opensearches[handle]->numfilenames;
2471 VM_search_getfilename
2473 string search_getfilename(float handle, float num)
2476 void VM_search_getfilename(void)
2478 int handle, filenum;
2480 VM_SAFEPARMCOUNT(2, VM_search_getfilename);
2482 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
2483 filenum = (int)PRVM_G_FLOAT(OFS_PARM1);
2485 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
2487 VM_Warning("VM_search_getfilename: invalid handle %i used in %s\n", handle, PRVM_NAME);
2490 if(prog->opensearches[handle] == NULL)
2492 VM_Warning("VM_search_getfilename: no such handle %i in %s\n", handle, PRVM_NAME);
2495 if(filenum < 0 || filenum >= prog->opensearches[handle]->numfilenames)
2497 VM_Warning("VM_search_getfilename: invalid filenum %i in %s\n", filenum, PRVM_NAME);
2501 tmp = VM_GetTempString();
2502 strlcpy(tmp, prog->opensearches[handle]->filenames[filenum], VM_STRINGTEMP_LENGTH);
2504 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(tmp);
2511 string chr(float ascii)
2517 VM_SAFEPARMCOUNT(1, VM_chr);
2519 tmp = VM_GetTempString();
2520 tmp[0] = (unsigned char) PRVM_G_FLOAT(OFS_PARM0);
2523 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(tmp);
2526 //=============================================================================
2527 // Draw builtins (client & menu)
2533 float iscachedpic(string pic)
2536 void VM_iscachedpic(void)
2538 VM_SAFEPARMCOUNT(1,VM_iscachedpic);
2540 // drawq hasnt such a function, thus always return true
2541 PRVM_G_FLOAT(OFS_RETURN) = false;
2548 string precache_pic(string pic)
2551 void VM_precache_pic(void)
2555 VM_SAFEPARMCOUNT(1, VM_precache_pic);
2557 s = PRVM_G_STRING(OFS_PARM0);
2558 PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
2561 PRVM_ERROR ("VM_precache_pic: %s: NULL", PRVM_NAME);
2563 VM_CheckEmptyString (s);
2565 // AK Draw_CachePic is supposed to always return a valid pointer
2566 if( Draw_CachePic(s, false)->tex == r_texture_notexture )
2567 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(NULL);
2577 void VM_freepic(void)
2581 VM_SAFEPARMCOUNT(1,VM_freepic);
2583 s = PRVM_G_STRING(OFS_PARM0);
2586 PRVM_ERROR ("VM_freepic: NULL");
2588 VM_CheckEmptyString (s);
2597 float drawcharacter(vector position, float character, vector scale, vector rgb, float alpha, float flag)
2600 void VM_drawcharacter(void)
2602 float *pos,*scale,*rgb;
2605 VM_SAFEPARMCOUNT(6,VM_drawcharacter);
2607 character = (char) PRVM_G_FLOAT(OFS_PARM1);
2610 PRVM_G_FLOAT(OFS_RETURN) = -1;
2611 VM_Warning("VM_drawcharacter: %s passed null character !\n",PRVM_NAME);
2615 pos = PRVM_G_VECTOR(OFS_PARM0);
2616 scale = PRVM_G_VECTOR(OFS_PARM2);
2617 rgb = PRVM_G_VECTOR(OFS_PARM3);
2618 flag = (int)PRVM_G_FLOAT(OFS_PARM5);
2620 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2622 PRVM_G_FLOAT(OFS_RETURN) = -2;
2623 VM_Warning("VM_drawcharacter: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2627 if(pos[2] || scale[2])
2628 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")));
2630 if(!scale[0] || !scale[1])
2632 PRVM_G_FLOAT(OFS_RETURN) = -3;
2633 VM_Warning("VM_drawcharacter: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
2637 DrawQ_String (pos[0], pos[1], &character, 1, scale[0], scale[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM4), flag);
2638 PRVM_G_FLOAT(OFS_RETURN) = 1;
2645 float drawstring(vector position, string text, vector scale, vector rgb, float alpha, float flag)
2648 void VM_drawstring(void)
2650 float *pos,*scale,*rgb;
2653 VM_SAFEPARMCOUNT(6,VM_drawstring);
2655 string = PRVM_G_STRING(OFS_PARM1);
2658 PRVM_G_FLOAT(OFS_RETURN) = -1;
2659 VM_Warning("VM_drawstring: %s passed null string !\n",PRVM_NAME);
2663 //VM_CheckEmptyString(string); Why should it be checked - perhaps the menu wants to support the precolored letters, too?
2665 pos = PRVM_G_VECTOR(OFS_PARM0);
2666 scale = PRVM_G_VECTOR(OFS_PARM2);
2667 rgb = PRVM_G_VECTOR(OFS_PARM3);
2668 flag = (int)PRVM_G_FLOAT(OFS_PARM5);
2670 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2672 PRVM_G_FLOAT(OFS_RETURN) = -2;
2673 VM_Warning("VM_drawstring: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2677 if(!scale[0] || !scale[1])
2679 PRVM_G_FLOAT(OFS_RETURN) = -3;
2680 VM_Warning("VM_drawstring: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
2684 if(pos[2] || scale[2])
2685 Con_Printf("VM_drawstring: z value%s from %s discarded\n",(pos[2] && scale[2]) ? "s" : " ",((pos[2] && scale[2]) ? "pos and scale" : (pos[2] ? "pos" : "scale")));
2687 DrawQ_String (pos[0], pos[1], string, 0, scale[0], scale[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM4), flag);
2688 PRVM_G_FLOAT(OFS_RETURN) = 1;
2694 float drawpic(vector position, string pic, vector size, vector rgb, float alpha, float flag)
2697 void VM_drawpic(void)
2699 const char *picname;
2700 float *size, *pos, *rgb;
2703 VM_SAFEPARMCOUNT(6,VM_drawpic);
2705 picname = PRVM_G_STRING(OFS_PARM1);
2709 PRVM_G_FLOAT(OFS_RETURN) = -1;
2710 VM_Warning("VM_drawpic: %s passed null picture name !\n", PRVM_NAME);
2714 VM_CheckEmptyString (picname);
2716 // is pic cached ? no function yet for that
2719 PRVM_G_FLOAT(OFS_RETURN) = -4;
2720 VM_Warning("VM_drawpic: %s: %s not cached !\n", PRVM_NAME, picname);
2724 pos = PRVM_G_VECTOR(OFS_PARM0);
2725 size = PRVM_G_VECTOR(OFS_PARM2);
2726 rgb = PRVM_G_VECTOR(OFS_PARM3);
2727 flag = (int) PRVM_G_FLOAT(OFS_PARM5);
2729 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2731 PRVM_G_FLOAT(OFS_RETURN) = -2;
2732 VM_Warning("VM_drawpic: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2736 if(pos[2] || size[2])
2737 Con_Printf("VM_drawpic: z value%s from %s discarded\n",(pos[2] && size[2]) ? "s" : " ",((pos[2] && size[2]) ? "pos and size" : (pos[2] ? "pos" : "size")));
2739 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);
2740 PRVM_G_FLOAT(OFS_RETURN) = 1;
2747 float drawfill(vector position, vector size, vector rgb, float alpha, float flag)
2750 void VM_drawfill(void)
2752 float *size, *pos, *rgb;
2755 VM_SAFEPARMCOUNT(5,VM_drawfill);
2758 pos = PRVM_G_VECTOR(OFS_PARM0);
2759 size = PRVM_G_VECTOR(OFS_PARM1);
2760 rgb = PRVM_G_VECTOR(OFS_PARM2);
2761 flag = (int) PRVM_G_FLOAT(OFS_PARM4);
2763 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2765 PRVM_G_FLOAT(OFS_RETURN) = -2;
2766 VM_Warning("VM_drawfill: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2770 if(pos[2] || size[2])
2771 Con_Printf("VM_drawfill: z value%s from %s discarded\n",(pos[2] && size[2]) ? "s" : " ",((pos[2] && size[2]) ? "pos and size" : (pos[2] ? "pos" : "size")));
2773 DrawQ_Pic(pos[0], pos[1], NULL, size[0], size[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM3), flag);
2774 PRVM_G_FLOAT(OFS_RETURN) = 1;
2781 drawsetcliparea(float x, float y, float width, float height)
2784 void VM_drawsetcliparea(void)
2787 VM_SAFEPARMCOUNT(4,VM_drawsetcliparea);
2789 x = bound(0, PRVM_G_FLOAT(OFS_PARM0), vid_conwidth.integer);
2790 y = bound(0, PRVM_G_FLOAT(OFS_PARM1), vid_conheight.integer);
2791 w = bound(0, PRVM_G_FLOAT(OFS_PARM2) + PRVM_G_FLOAT(OFS_PARM0) - x, (vid_conwidth.integer - x));
2792 h = bound(0, PRVM_G_FLOAT(OFS_PARM3) + PRVM_G_FLOAT(OFS_PARM1) - y, (vid_conheight.integer - y));
2794 DrawQ_SetClipArea(x, y, w, h);
2799 VM_drawresetcliparea
2804 void VM_drawresetcliparea(void)
2806 VM_SAFEPARMCOUNT(0,VM_drawresetcliparea);
2808 DrawQ_ResetClipArea();
2815 vector getimagesize(string pic)
2818 void VM_getimagesize(void)
2823 VM_SAFEPARMCOUNT(1,VM_getimagesize);
2825 p = PRVM_G_STRING(OFS_PARM0);
2828 PRVM_ERROR("VM_getimagepos: %s passed null picture name !", PRVM_NAME);
2830 VM_CheckEmptyString (p);
2832 pic = Draw_CachePic (p, false);
2834 PRVM_G_VECTOR(OFS_RETURN)[0] = pic->width;
2835 PRVM_G_VECTOR(OFS_RETURN)[1] = pic->height;
2836 PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
2843 string keynumtostring(float keynum)
2846 void VM_keynumtostring (void)
2850 VM_SAFEPARMCOUNT(1, VM_keynumtostring);
2852 keynum = (int)PRVM_G_FLOAT(OFS_PARM0);
2854 tmp = VM_GetTempString();
2856 strlcpy(tmp, Key_KeynumToString(keynum), VM_STRINGTEMP_LENGTH);
2858 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(tmp);
2865 float stringtokeynum(string key)
2868 void VM_stringtokeynum (void)
2871 VM_SAFEPARMCOUNT( 1, VM_keynumtostring );
2873 str = PRVM_G_STRING( OFS_PARM0 );
2875 PRVM_G_INT(OFS_RETURN) = Key_StringToKeynum( str );
2878 // CL_Video interface functions
2881 ========================
2884 float cin_open(string file, string name)
2885 ========================
2887 void VM_cin_open( void )
2892 VM_SAFEPARMCOUNT( 2, VM_cin_open );
2894 file = PRVM_G_STRING( OFS_PARM0 );
2895 name = PRVM_G_STRING( OFS_PARM1 );
2897 VM_CheckEmptyString( file );
2898 VM_CheckEmptyString( name );
2900 if( CL_OpenVideo( file, name, MENUOWNER ) )
2901 PRVM_G_FLOAT( OFS_RETURN ) = 1;
2903 PRVM_G_FLOAT( OFS_RETURN ) = 0;
2907 ========================
2910 void cin_close(string name)
2911 ========================
2913 void VM_cin_close( void )
2917 VM_SAFEPARMCOUNT( 1, VM_cin_close );
2919 name = PRVM_G_STRING( OFS_PARM0 );
2920 VM_CheckEmptyString( name );
2922 CL_CloseVideo( CL_GetVideoByName( name ) );
2926 ========================
2928 void cin_setstate(string name, float type)
2929 ========================
2931 void VM_cin_setstate( void )
2934 clvideostate_t state;
2937 VM_SAFEPARMCOUNT( 2, VM_cin_netstate );
2939 name = PRVM_G_STRING( OFS_PARM0 );
2940 VM_CheckEmptyString( name );
2942 state = (clvideostate_t)((int)PRVM_G_FLOAT( OFS_PARM1 ));
2944 video = CL_GetVideoByName( name );
2945 if( video && state > CLVIDEO_UNUSED && state < CLVIDEO_STATECOUNT )
2946 CL_SetVideoState( video, state );
2950 ========================
2953 float cin_getstate(string name)
2954 ========================
2956 void VM_cin_getstate( void )
2961 VM_SAFEPARMCOUNT( 1, VM_cin_getstate );
2963 name = PRVM_G_STRING( OFS_PARM0 );
2964 VM_CheckEmptyString( name );
2966 video = CL_GetVideoByName( name );
2968 PRVM_G_FLOAT( OFS_RETURN ) = (int)video->state;
2970 PRVM_G_FLOAT( OFS_RETURN ) = 0;
2974 ========================
2977 void cin_restart(string name)
2978 ========================
2980 void VM_cin_restart( void )
2985 VM_SAFEPARMCOUNT( 1, VM_cin_restart );
2987 name = PRVM_G_STRING( OFS_PARM0 );
2988 VM_CheckEmptyString( name );
2990 video = CL_GetVideoByName( name );
2992 CL_RestartVideo( video );
2999 Writes new values for v_forward, v_up, and v_right based on the given forward vector
3000 vectorvectors(vector, vector)
3003 void VM_vectorvectors (void)
3005 VectorNormalize2(PRVM_G_VECTOR(OFS_PARM0), prog->globals.server->v_forward);
3006 VectorVectors(prog->globals.server->v_forward, prog->globals.server->v_right, prog->globals.server->v_up);
3010 ========================
3013 void drawline(float width, vector pos1, vector pos2, vector rgb, float alpha, float flags)
3014 ========================
3016 void VM_drawline (void)
3018 float *c1, *c2, *rgb;
3020 unsigned char flags;
3022 VM_SAFEPARMCOUNT(6, VM_drawline);
3023 width = PRVM_G_FLOAT(OFS_PARM0);
3024 c1 = PRVM_G_VECTOR(OFS_PARM1);
3025 c2 = PRVM_G_VECTOR(OFS_PARM2);
3026 rgb = PRVM_G_VECTOR(OFS_PARM3);
3027 alpha = PRVM_G_FLOAT(OFS_PARM4);
3028 flags = (int)PRVM_G_FLOAT(OFS_PARM5);
3029 DrawQ_Line(width, c1[0], c1[1], c2[0], c2[1], rgb[0], rgb[1], rgb[2], alpha, flags);
3032 //====================
3033 //QC POLYGON functions
3034 //====================
3039 float data[36]; //[515]: enough for polygons
3040 unsigned char flags; //[515]: + VM_POLYGON_2D and VM_POLYGON_FL4V flags
3043 //static float vm_polygon_linewidth = 1;
3044 static mempool_t *vm_polygons_pool = NULL;
3045 static unsigned char vm_current_vertices = 0;
3046 static qboolean vm_polygons_initialized = false;
3047 static vm_polygon_t *vm_polygons = NULL;
3048 static unsigned long vm_polygons_num = 0, vm_drawpolygons_num = 0; //[515]: ok long on 64bit ?
3049 static qboolean vm_polygonbegin = false; //[515]: for "no-crap-on-the-screen" check
3050 #define VM_DEFPOLYNUM 64 //[515]: enough for default ?
3052 #define VM_POLYGON_FL3V 16 //more than 2 vertices (used only for lines)
3053 #define VM_POLYGON_FLLINES 32
3054 #define VM_POLYGON_FL2D 64
3055 #define VM_POLYGON_FL4V 128 //4 vertices
3057 void VM_InitPolygons (void)
3059 vm_polygons_pool = Mem_AllocPool("VMPOLY", 0, NULL);
3060 vm_polygons = (vm_polygon_t *)Mem_Alloc(vm_polygons_pool, VM_DEFPOLYNUM*sizeof(vm_polygon_t));
3061 memset(vm_polygons, 0, VM_DEFPOLYNUM*sizeof(vm_polygon_t));
3062 vm_polygons_num = VM_DEFPOLYNUM;
3063 vm_drawpolygons_num = 0;
3064 vm_polygonbegin = false;
3065 vm_polygons_initialized = true;
3068 void VM_DrawPolygonCallback (const entity_render_t *ent, const rtlight_t *rtlight, int numsurfaces, int *surfacelist)
3070 int surfacelistindex;
3071 // LordHavoc: FIXME: this is stupid code
3072 for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
3074 const vm_polygon_t *p = &vm_polygons[surfacelist[surfacelistindex]];
3075 int flags = p->flags & 0x0f;
3077 if(flags == DRAWFLAG_ADDITIVE)
3078 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
3079 else if(flags == DRAWFLAG_MODULATE)
3080 GL_BlendFunc(GL_DST_COLOR, GL_ZERO);
3081 else if(flags == DRAWFLAG_2XMODULATE)
3082 GL_BlendFunc(GL_DST_COLOR,GL_SRC_COLOR);
3084 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
3086 R_Mesh_TexBind(0, R_GetTexture(p->tex));
3089 //[515]: is speed is max ?
3090 if(p->flags & VM_POLYGON_FLLINES) //[515]: lines
3092 qglLineWidth(p->data[13]);CHECKGLERROR
3093 qglBegin(GL_LINE_LOOP);
3094 qglTexCoord1f (p->data[12]);
3095 qglColor4f (p->data[20], p->data[21], p->data[22], p->data[23]);
3096 qglVertex3f (p->data[0] , p->data[1], p->data[2]);
3098 qglTexCoord1f (p->data[14]);
3099 qglColor4f (p->data[24], p->data[25], p->data[26], p->data[27]);
3100 qglVertex3f (p->data[3] , p->data[4], p->data[5]);
3102 if(p->flags & VM_POLYGON_FL3V)
3104 qglTexCoord1f (p->data[16]);
3105 qglColor4f (p->data[28], p->data[29], p->data[30], p->data[31]);
3106 qglVertex3f (p->data[6] , p->data[7], p->data[8]);
3108 if(p->flags & VM_POLYGON_FL4V)
3110 qglTexCoord1f (p->data[18]);
3111 qglColor4f (p->data[32], p->data[33], p->data[34], p->data[35]);
3112 qglVertex3f (p->data[9] , p->data[10], p->data[11]);
3120 qglBegin(GL_POLYGON);
3121 qglTexCoord2f (p->data[12], p->data[13]);
3122 qglColor4f (p->data[20], p->data[21], p->data[22], p->data[23]);
3123 qglVertex3f (p->data[0] , p->data[1], p->data[2]);
3125 qglTexCoord2f (p->data[14], p->data[15]);
3126 qglColor4f (p->data[24], p->data[25], p->data[26], p->data[27]);
3127 qglVertex3f (p->data[3] , p->data[4], p->data[5]);
3129 qglTexCoord2f (p->data[16], p->data[17]);
3130 qglColor4f (p->data[28], p->data[29], p->data[30], p->data[31]);
3131 qglVertex3f (p->data[6] , p->data[7], p->data[8]);
3133 if(p->flags & VM_POLYGON_FL4V)
3135 qglTexCoord2f (p->data[18], p->data[19]);
3136 qglColor4f (p->data[32], p->data[33], p->data[34], p->data[35]);
3137 qglVertex3f (p->data[9] , p->data[10], p->data[11]);
3145 void VM_AddPolygonTo2DScene (vm_polygon_t *p)
3147 drawqueuemesh_t mesh;
3148 static int picelements[6] = {0, 1, 2, 0, 2, 3};
3150 mesh.texture = p->tex;
3151 mesh.data_element3i = picelements;
3152 mesh.data_vertex3f = p->data;
3153 mesh.data_texcoord2f = p->data + 12;
3154 mesh.data_color4f = p->data + 20;
3155 if(p->flags & VM_POLYGON_FL4V)
3157 mesh.num_vertices = 4;
3158 mesh.num_triangles = 2;
3162 mesh.num_vertices = 3;
3163 mesh.num_triangles = 1;
3165 if(p->flags & VM_POLYGON_FLLINES) //[515]: lines
3166 DrawQ_LineLoop (&mesh, (p->flags&0x0f));
3168 DrawQ_Mesh (&mesh, (p->flags&0x0f));
3171 //void(string texturename, float flag, float 2d, float lines) R_BeginPolygon
3172 void VM_R_PolygonBegin (void)
3175 const char *picname;
3177 VM_SAFEPARMCOUNT(2, VM_R_PolygonBegin);
3179 if(!vm_polygons_initialized)
3183 VM_Warning("VM_R_PolygonBegin: called twice without VM_R_PolygonEnd after first\n");
3186 if(vm_drawpolygons_num >= vm_polygons_num)
3188 p = (vm_polygon_t *)Mem_Alloc(vm_polygons_pool, 2 * vm_polygons_num * sizeof(vm_polygon_t));
3189 memset(p, 0, 2 * vm_polygons_num * sizeof(vm_polygon_t));
3190 memcpy(p, vm_polygons, vm_polygons_num * sizeof(vm_polygon_t));
3191 Mem_Free(vm_polygons);
3193 vm_polygons_num *= 2;
3195 p = &vm_polygons[vm_drawpolygons_num];
3196 picname = PRVM_G_STRING(OFS_PARM0);
3198 p->tex = Draw_CachePic(picname, true)->tex;
3200 p->tex = r_texture_white;
3201 p->flags = (unsigned char)PRVM_G_FLOAT(OFS_PARM1);
3202 vm_current_vertices = 0;
3203 vm_polygonbegin = true;
3206 if(PRVM_G_FLOAT(OFS_PARM2))
3207 p->flags |= VM_POLYGON_FL2D;
3208 if(prog->argc >= 4 && PRVM_G_FLOAT(OFS_PARM3))
3210 p->data[13] = PRVM_G_FLOAT(OFS_PARM3); //[515]: linewidth
3211 p->flags |= VM_POLYGON_FLLINES;
3216 //void(vector org, vector texcoords, vector rgb, float alpha) R_PolygonVertex
3217 void VM_R_PolygonVertex (void)
3219 float *coords, *tx, *rgb, alpha;
3221 VM_SAFEPARMCOUNT(4, VM_R_PolygonVertex);
3223 if(!vm_polygonbegin)
3225 VM_Warning("VM_R_PolygonVertex: VM_R_PolygonBegin wasn't called\n");
3228 coords = PRVM_G_VECTOR(OFS_PARM0);
3229 tx = PRVM_G_VECTOR(OFS_PARM1);
3230 rgb = PRVM_G_VECTOR(OFS_PARM2);
3231 alpha = PRVM_G_FLOAT(OFS_PARM3);
3233 p = &vm_polygons[vm_drawpolygons_num];
3234 if(vm_current_vertices > 4)
3236 VM_Warning("VM_R_PolygonVertex: may have 4 vertices max\n");
3240 p->data[vm_current_vertices*3] = coords[0];
3241 p->data[1+vm_current_vertices*3] = coords[1];
3242 p->data[2+vm_current_vertices*3] = coords[2];
3244 p->data[12+vm_current_vertices*2] = tx[0];
3245 if(!(p->flags & VM_POLYGON_FLLINES))
3246 p->data[13+vm_current_vertices*2] = tx[1];
3248 p->data[20+vm_current_vertices*4] = rgb[0];
3249 p->data[21+vm_current_vertices*4] = rgb[1];
3250 p->data[22+vm_current_vertices*4] = rgb[2];
3251 p->data[23+vm_current_vertices*4] = alpha;
3253 vm_current_vertices++;
3254 if(vm_current_vertices == 4)
3255 p->flags |= VM_POLYGON_FL4V;
3257 if(vm_current_vertices == 3)
3258 p->flags |= VM_POLYGON_FL3V;
3261 //void() R_EndPolygon
3262 void VM_R_PolygonEnd (void)
3264 if(!vm_polygonbegin)
3266 VM_Warning("VM_R_PolygonEnd: VM_R_PolygonBegin wasn't called\n");
3269 vm_polygonbegin = false;
3270 if(vm_current_vertices > 2 || (vm_current_vertices >= 2 && vm_polygons[vm_drawpolygons_num].flags & VM_POLYGON_FLLINES))
3272 if(vm_polygons[vm_drawpolygons_num].flags & VM_POLYGON_FL2D) //[515]: don't use qcpolygons memory if 2D
3273 VM_AddPolygonTo2DScene(&vm_polygons[vm_drawpolygons_num]);
3275 vm_drawpolygons_num++;
3278 VM_Warning("VM_R_PolygonEnd: %i vertices isn't a good choice\n", vm_current_vertices);
3281 void VM_AddPolygonsToMeshQueue (void)
3284 if(!vm_drawpolygons_num)
3286 for(i = 0;i < vm_drawpolygons_num;i++)
3287 VM_DrawPolygonCallback(NULL, NULL, i, NULL);
3288 vm_drawpolygons_num = 0;
3294 // float(float number, float quantity) bitshift (EXT_BITSHIFT)
3295 void VM_bitshift (void)
3298 VM_SAFEPARMCOUNT(2, VM_bitshift);
3300 n1 = (int)fabs((int)PRVM_G_FLOAT(OFS_PARM0));
3301 n2 = (int)PRVM_G_FLOAT(OFS_PARM1);
3303 PRVM_G_FLOAT(OFS_RETURN) = n1;
3306 PRVM_G_FLOAT(OFS_RETURN) = (n1 >> -n2);
3308 PRVM_G_FLOAT(OFS_RETURN) = (n1 << n2);
3311 ////////////////////////////////////////
3312 // AltString functions
3313 ////////////////////////////////////////
3316 ========================
3319 float altstr_count(string)
3320 ========================
3322 void VM_altstr_count( void )
3324 const char *altstr, *pos;
3327 VM_SAFEPARMCOUNT( 1, VM_altstr_count );
3329 altstr = PRVM_G_STRING( OFS_PARM0 );
3330 //VM_CheckEmptyString( altstr );
3332 for( count = 0, pos = altstr ; *pos ; pos++ ) {
3333 if( *pos == '\\' ) {
3337 } else if( *pos == '\'' ) {
3342 PRVM_G_FLOAT( OFS_RETURN ) = (float) (count / 2);
3346 ========================
3349 string altstr_prepare(string)
3350 ========================
3352 void VM_altstr_prepare( void )
3355 const char *instr, *in;
3358 VM_SAFEPARMCOUNT( 1, VM_altstr_prepare );
3360 instr = PRVM_G_STRING( OFS_PARM0 );
3361 //VM_CheckEmptyString( instr );
3362 outstr = VM_GetTempString();
3364 for( out = outstr, in = instr, size = VM_STRINGTEMP_LENGTH - 1 ; size && *in ; size--, in++, out++ )
3373 PRVM_G_INT( OFS_RETURN ) = PRVM_SetEngineString( outstr );
3377 ========================
3380 string altstr_get(string, float)
3381 ========================
3383 void VM_altstr_get( void )
3385 const char *altstr, *pos;
3389 VM_SAFEPARMCOUNT( 2, VM_altstr_get );
3391 altstr = PRVM_G_STRING( OFS_PARM0 );
3392 //VM_CheckEmptyString( altstr );
3394 count = (int)PRVM_G_FLOAT( OFS_PARM1 );
3395 count = count * 2 + 1;
3397 for( pos = altstr ; *pos && count ; pos++ )
3398 if( *pos == '\\' ) {
3401 } else if( *pos == '\'' )
3405 PRVM_G_INT( OFS_RETURN ) = PRVM_SetEngineString( NULL );
3409 outstr = VM_GetTempString();
3410 for( out = outstr, size = VM_STRINGTEMP_LENGTH - 1 ; size && *pos ; size--, pos++, out++ )
3411 if( *pos == '\\' ) {
3416 } else if( *pos == '\'' )
3422 PRVM_G_INT( OFS_RETURN ) = PRVM_SetEngineString( outstr );
3426 ========================
3429 string altstr_set(string altstr, float num, string set)
3430 ========================
3432 void VM_altstr_set( void )
3435 const char *altstr, *str;
3439 VM_SAFEPARMCOUNT( 3, VM_altstr_set );
3441 altstr = PRVM_G_STRING( OFS_PARM0 );
3442 //VM_CheckEmptyString( altstr );
3444 num = (int)PRVM_G_FLOAT( OFS_PARM1 );
3446 str = PRVM_G_STRING( OFS_PARM2 );
3447 //VM_CheckEmptyString( str );
3449 outstr = out = VM_GetTempString();
3450 for( num = num * 2 + 1, in = altstr; *in && num; *out++ = *in++ )
3455 } else if( *in == '\'' ) {
3460 PRVM_G_INT( OFS_RETURN ) = PRVM_SetEngineString( altstr );
3464 for( ; *str; *out++ = *str++ );
3465 // now jump over the old content
3467 if( *in == '\'' || (*in == '\\' && !*++in) )
3471 PRVM_G_INT( OFS_RETURN ) = PRVM_SetEngineString( NULL );
3475 strlcpy(out, in, VM_STRINGTEMP_LENGTH);
3476 PRVM_G_INT( OFS_RETURN ) = PRVM_SetEngineString( outstr );
3480 ========================
3483 string altstr_ins(string altstr, float num, string set)
3484 ========================
3486 void VM_altstr_ins(void)
3496 in = instr = PRVM_G_STRING( OFS_PARM0 );
3497 num = (int)PRVM_G_FLOAT( OFS_PARM1 );
3498 set = setstr = PRVM_G_STRING( OFS_PARM2 );
3500 out = outstr = VM_GetTempString();
3501 for( num = num * 2 + 2 ; *in && num > 0 ; *out++ = *in++ )
3506 } else if( *in == '\'' ) {
3511 for( ; *set ; *out++ = *set++ );
3514 strlcpy(out, in, VM_STRINGTEMP_LENGTH);
3515 PRVM_G_INT( OFS_RETURN ) = PRVM_SetEngineString( outstr );
3519 ////////////////////////////////////////
3520 // BufString functions
3521 ////////////////////////////////////////
3522 //[515]: string buffers support
3523 #define MAX_QCSTR_BUFFERS 128
3524 #define MAX_QCSTR_STRINGS 1024
3529 char *strings[MAX_QCSTR_STRINGS];
3532 static qcstrbuffer_t *qcstringbuffers[MAX_QCSTR_BUFFERS];
3533 static int num_qcstringbuffers;
3534 static int buf_sortpower;
3536 #define BUFSTR_BUFFER(a) (a>=MAX_QCSTR_BUFFERS) ? NULL : (qcstringbuffers[a])
3537 #define BUFSTR_ISFREE(a) (a<MAX_QCSTR_BUFFERS&&qcstringbuffers[a]&&qcstringbuffers[a]->num_strings<=0) ? 1 : 0
3539 static int BufStr_FindFreeBuffer (void)
3542 if(num_qcstringbuffers == MAX_QCSTR_BUFFERS)
3544 for(i=0;i<MAX_QCSTR_BUFFERS;i++)
3545 if(!qcstringbuffers[i])
3547 qcstringbuffers[i] = (qcstrbuffer_t *)Z_Malloc(sizeof(qcstrbuffer_t));
3548 memset(qcstringbuffers[i], 0, sizeof(qcstrbuffer_t));
3554 static void BufStr_ClearBuffer (int index)
3556 qcstrbuffer_t *b = qcstringbuffers[index];
3561 if(b->num_strings > 0)
3563 for(i=0;i<b->num_strings;i++)
3565 Z_Free(b->strings[i]);
3566 num_qcstringbuffers--;
3568 Z_Free(qcstringbuffers[index]);
3569 qcstringbuffers[index] = NULL;
3573 static int BufStr_FindFreeString (qcstrbuffer_t *b)
3576 for(i=0;i<b->num_strings;i++)
3577 if(!b->strings[i] || !b->strings[i][0])
3579 if(i == MAX_QCSTR_STRINGS) return -1;
3583 static int BufStr_SortStringsUP (const void *in1, const void *in2)
3586 a = *((const char **) in1);
3587 b = *((const char **) in2);
3589 if(!b[0]) return -1;
3590 return strncmp(a, b, buf_sortpower);
3593 static int BufStr_SortStringsDOWN (const void *in1, const void *in2)
3596 a = *((const char **) in1);
3597 b = *((const char **) in2);
3599 if(!b[0]) return -1;
3600 return strncmp(b, a, buf_sortpower);
3604 static void VM_BufStr_Init (void)
3606 memset(qcstringbuffers, 0, sizeof(qcstringbuffers));
3607 num_qcstringbuffers = 0;
3610 static void VM_BufStr_ShutDown (void)
3613 for(i=0;i<MAX_QCSTR_BUFFERS && num_qcstringbuffers;i++)
3614 BufStr_ClearBuffer(i);
3619 ========================
3621 creates new buffer, and returns it's index, returns -1 if failed
3622 float buf_create(void) = #460;
3623 ========================
3625 void VM_buf_create (void)
3628 VM_SAFEPARMCOUNT(0, VM_buf_create);
3629 i = BufStr_FindFreeBuffer();
3631 num_qcstringbuffers++;
3633 //Con_Printf("VM_buf_create: buffers overflow in %s\n", PRVM_NAME);
3634 PRVM_G_FLOAT(OFS_RETURN) = i;
3638 ========================
3640 deletes buffer and all strings in it
3641 void buf_del(float bufhandle) = #461;
3642 ========================
3644 void VM_buf_del (void)
3646 VM_SAFEPARMCOUNT(1, VM_buf_del);
3647 if(BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0)))
3648 BufStr_ClearBuffer((int)PRVM_G_FLOAT(OFS_PARM0));
3651 VM_Warning("VM_buf_del: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3657 ========================
3659 how many strings are stored in buffer
3660 float buf_getsize(float bufhandle) = #462;
3661 ========================
3663 void VM_buf_getsize (void)
3666 VM_SAFEPARMCOUNT(1, VM_buf_getsize);
3668 b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3671 PRVM_G_FLOAT(OFS_RETURN) = -1;
3672 VM_Warning("VM_buf_getsize: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3676 PRVM_G_FLOAT(OFS_RETURN) = b->num_strings;
3680 ========================
3682 copy all content from one buffer to another, make sure it exists
3683 void buf_copy(float bufhandle_from, float bufhandle_to) = #463;
3684 ========================
3686 void VM_buf_copy (void)
3688 qcstrbuffer_t *b1, *b2;
3690 VM_SAFEPARMCOUNT(2, VM_buf_copy);
3692 b1 = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3695 VM_Warning("VM_buf_copy: invalid source buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3698 i = (int)PRVM_G_FLOAT(OFS_PARM1);
3699 if(i == (int)PRVM_G_FLOAT(OFS_PARM0))
3701 VM_Warning("VM_buf_copy: source == destination (%i) in %s\n", i, PRVM_NAME);
3704 b2 = BUFSTR_BUFFER(i);
3707 VM_Warning("VM_buf_copy: invalid destination buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM1), PRVM_NAME);
3711 BufStr_ClearBuffer(i);
3712 qcstringbuffers[i] = (qcstrbuffer_t *)Z_Malloc(sizeof(qcstrbuffer_t));
3713 memset(qcstringbuffers[i], 0, sizeof(qcstrbuffer_t));
3714 b2->num_strings = b1->num_strings;
3716 for(i=0;i<b1->num_strings;i++)
3717 if(b1->strings[i] && b1->strings[i][0])
3720 stringlen = strlen(b1->strings[i]) + 1;
3721 b2->strings[i] = (char *)Z_Malloc(stringlen);
3724 VM_Warning("VM_buf_copy: not enough memory for buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM1), PRVM_NAME);
3727 memcpy(b2->strings[i], b1->strings[i], stringlen);
3732 ========================
3734 sort buffer by beginnings of strings (sortpower defaults it's lenght)
3735 "backward == TRUE" means that sorting goes upside-down
3736 void buf_sort(float bufhandle, float sortpower, float backward) = #464;
3737 ========================
3739 void VM_buf_sort (void)
3743 VM_SAFEPARMCOUNT(3, VM_buf_sort);
3745 b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3748 VM_Warning("VM_buf_sort: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3751 if(b->num_strings <= 0)
3753 VM_Warning("VM_buf_sort: tried to sort empty buffer %i in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3756 buf_sortpower = (int)PRVM_G_FLOAT(OFS_PARM1);
3757 if(buf_sortpower <= 0)
3758 buf_sortpower = 99999999;
3760 if(!PRVM_G_FLOAT(OFS_PARM2))
3761 qsort(b->strings, b->num_strings, sizeof(char*), BufStr_SortStringsUP);
3763 qsort(b->strings, b->num_strings, sizeof(char*), BufStr_SortStringsDOWN);
3765 for(i=b->num_strings-1;i>=0;i--) //[515]: delete empty lines
3768 if(b->strings[i][0])
3772 Z_Free(b->strings[i]);
3774 b->strings[i] = NULL;
3782 ========================
3784 concantenates all buffer string into one with "glue" separator and returns it as tempstring
3785 string buf_implode(float bufhandle, string glue) = #465;
3786 ========================
3788 void VM_buf_implode (void)
3795 VM_SAFEPARMCOUNT(2, VM_buf_implode);
3797 b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3798 PRVM_G_INT(OFS_RETURN) = 0;
3801 VM_Warning("VM_buf_implode: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3806 sep = PRVM_G_STRING(OFS_PARM1);
3807 k = VM_GetTempString();
3809 for(l=i=0;i<b->num_strings;i++)
3812 l += strlen(b->strings[i]);
3815 strlcat(k, b->strings[i], VM_STRINGTEMP_LENGTH);
3816 if(sep && (i != b->num_strings-1))
3821 strlcat(k, sep, VM_STRINGTEMP_LENGTH);
3824 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(k);
3828 ========================
3830 get a string from buffer, returns direct pointer, dont str_unzone it!
3831 string bufstr_get(float bufhandle, float string_index) = #465;
3832 ========================
3834 void VM_bufstr_get (void)
3838 VM_SAFEPARMCOUNT(2, VM_bufstr_get);
3840 b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3843 VM_Warning("VM_bufstr_get: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3846 strindex = (int)PRVM_G_FLOAT(OFS_PARM1);
3847 if(strindex < 0 || strindex > MAX_QCSTR_STRINGS)
3849 VM_Warning("VM_bufstr_get: invalid string index %i used in %s\n", strindex, PRVM_NAME);
3852 PRVM_G_INT(OFS_RETURN) = 0;
3853 if(b->num_strings <= strindex)
3855 if(b->strings[strindex])
3856 PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(b->strings[strindex]);
3860 ========================
3862 copies a string into selected slot of buffer
3863 void bufstr_set(float bufhandle, float string_index, string str) = #466;
3864 ========================
3866 void VM_bufstr_set (void)
3868 int bufindex, strindex;
3873 VM_SAFEPARMCOUNT(3, VM_bufstr_set);
3875 bufindex = (int)PRVM_G_FLOAT(OFS_PARM0);
3876 b = BUFSTR_BUFFER(bufindex);
3879 VM_Warning("VM_bufstr_set: invalid buffer %i used in %s\n", bufindex, PRVM_NAME);
3882 strindex = (int)PRVM_G_FLOAT(OFS_PARM1);
3883 if(strindex < 0 || strindex > MAX_QCSTR_STRINGS)
3885 VM_Warning("VM_bufstr_set: invalid string index %i used in %s\n", strindex, PRVM_NAME);
3888 news = PRVM_G_STRING(OFS_PARM2);
3891 VM_Warning("VM_bufstr_set: null string used in %s\n", PRVM_NAME);
3894 if(b->strings[strindex])
3895 Z_Free(b->strings[strindex]);
3896 alloclen = strlen(news) + 1;
3897 b->strings[strindex] = (char *)Z_Malloc(alloclen);
3898 memcpy(b->strings[strindex], news, alloclen);
3902 ========================
3904 adds string to buffer in nearest free slot and returns it
3905 "order == TRUE" means that string will be added after last "full" slot
3906 float bufstr_add(float bufhandle, string str, float order) = #467;
3907 ========================
3909 void VM_bufstr_add (void)
3911 int bufindex, order, strindex;
3916 VM_SAFEPARMCOUNT(3, VM_bufstr_add);
3918 bufindex = (int)PRVM_G_FLOAT(OFS_PARM0);
3919 b = BUFSTR_BUFFER(bufindex);
3920 PRVM_G_FLOAT(OFS_RETURN) = -1;
3923 VM_Warning("VM_bufstr_add: invalid buffer %i used in %s\n", bufindex, PRVM_NAME);
3926 string = PRVM_G_STRING(OFS_PARM1);
3929 VM_Warning("VM_bufstr_add: null string used in %s\n", PRVM_NAME);
3933 order = (int)PRVM_G_FLOAT(OFS_PARM2);
3935 strindex = b->num_strings;
3938 strindex = BufStr_FindFreeString(b);
3941 VM_Warning("VM_bufstr_add: buffer %i has no free string slots in %s\n", bufindex, PRVM_NAME);
3946 while(b->num_strings <= strindex)
3948 if(b->num_strings == MAX_QCSTR_STRINGS)
3950 VM_Warning("VM_bufstr_add: buffer %i has no free string slots in %s\n", bufindex, PRVM_NAME);
3953 b->strings[b->num_strings] = NULL;
3956 if(b->strings[strindex])
3957 Z_Free(b->strings[strindex]);
3958 alloclen = strlen(string) + 1;
3959 b->strings[strindex] = (char *)Z_Malloc(alloclen);
3960 memcpy(b->strings[strindex], string, alloclen);
3961 PRVM_G_FLOAT(OFS_RETURN) = strindex;
3965 ========================
3967 delete string from buffer
3968 void bufstr_free(float bufhandle, float string_index) = #468;
3969 ========================
3971 void VM_bufstr_free (void)
3975 VM_SAFEPARMCOUNT(2, VM_bufstr_free);
3977 b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3980 VM_Warning("VM_bufstr_free: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3983 i = (int)PRVM_G_FLOAT(OFS_PARM1);
3984 if(i < 0 || i > MAX_QCSTR_STRINGS)
3986 VM_Warning("VM_bufstr_free: invalid string index %i used in %s\n", i, PRVM_NAME);
3990 Z_Free(b->strings[i]);
3991 b->strings[i] = NULL;
3992 if(i+1 == b->num_strings)
3998 void VM_Cmd_Init(void)
4000 // only init the stuff for the current prog
4003 // VM_BufStr_Init();
4004 if(vm_polygons_initialized)
4006 Mem_FreePool(&vm_polygons_pool);
4007 vm_polygons_initialized = false;
4011 void VM_Cmd_Reset(void)
4013 CL_PurgeOwner( MENUOWNER );
4015 VM_Files_CloseAll();
4016 // VM_BufStr_ShutDown();
4017 if(vm_polygons_initialized)
4019 Mem_FreePool(&vm_polygons_pool);
4020 vm_polygons_initialized = false;