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 // TODO DONE: move vm_files and vm_fssearchlist to prvm_prog_t struct
29 // TODO: move vm_files and vm_fssearchlist back [9/13/2006 Black]
30 // TODO: (move vm_files and vm_fssearchlist to prvm_prog_t struct again) [2007-01-23 LordHavoc]
31 // TODO: will this war ever end? [2007-01-23 LordHavoc]
33 void VM_CheckEmptyString (const char *s)
36 PRVM_ERROR ("%s: Bad string", PRVM_NAME);
39 //============================================================================
42 void VM_VarString(int first, char *out, int outlength)
48 outend = out + outlength - 1;
49 for (i = first;i < prog->argc && out < outend;i++)
51 s = PRVM_G_STRING((OFS_PARM0+i*3));
52 while (out < outend && *s)
62 returns true if the extension is supported by the server
64 checkextension(extensionname)
68 // kind of helper function
69 static qboolean checkextension(const char *name)
73 len = (int)strlen(name);
75 for (e = prog->extensionstring;*e;e++)
82 while (*e && *e != ' ')
84 if ((e - start) == len && !strncasecmp(start, name, len))
90 void VM_checkextension (void)
92 VM_SAFEPARMCOUNT(1,VM_checkextension);
94 PRVM_G_FLOAT(OFS_RETURN) = checkextension(PRVM_G_STRING(OFS_PARM0));
101 This is a TERMINAL error, which will kill off the entire prog.
110 char string[VM_STRINGTEMP_LENGTH];
112 VM_VarString(0, string, sizeof(string));
113 Con_Printf("======%s ERROR in %s:\n%s\n", PRVM_NAME, PRVM_GetString(prog->xfunction->s_name), string);
116 ed = PRVM_G_EDICT(prog->self->ofs);
120 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);
127 Dumps out self, then an error message. The program is aborted and self is
128 removed, but the level can continue.
133 void VM_objerror (void)
136 char string[VM_STRINGTEMP_LENGTH];
138 VM_VarString(0, string, sizeof(string));
139 Con_Printf("======OBJECT ERROR======\n"); // , PRVM_NAME, PRVM_GetString(prog->xfunction->s_name), string); // or include them? FIXME
142 ed = PRVM_G_EDICT (prog->self->ofs);
148 // objerror has to display the object fields -> else call
149 PRVM_ERROR ("VM_objecterror: self not defined !");
150 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);
164 char string[VM_STRINGTEMP_LENGTH];
166 VM_VarString(0, string, sizeof(string));
174 broadcast print to everyone on server
179 void VM_bprint (void)
181 char string[VM_STRINGTEMP_LENGTH];
185 VM_Warning("VM_bprint: game is not server(%s) !\n", PRVM_NAME);
189 VM_VarString(0, string, sizeof(string));
190 SV_BroadcastPrint(string);
195 VM_sprint (menu & client but only if server.active == true)
197 single print to a specific client
199 sprint(float clientnum,...[string])
202 void VM_sprint (void)
206 char string[VM_STRINGTEMP_LENGTH];
208 //find client for this entity
209 clientnum = (int)PRVM_G_FLOAT(OFS_PARM0);
210 if (!sv.active || clientnum < 0 || clientnum >= svs.maxclients || !svs.clients[clientnum].active)
212 VM_Warning("VM_sprint: %s: invalid client or server is not active !\n", PRVM_NAME);
216 client = svs.clients + clientnum;
217 if (!client->netconnection)
220 VM_VarString(1, string, sizeof(string));
221 MSG_WriteChar(&client->netconnection->message,svc_print);
222 MSG_WriteString(&client->netconnection->message, string);
229 single print to the screen
231 centerprint(clientent, value)
234 void VM_centerprint (void)
236 char string[VM_STRINGTEMP_LENGTH];
238 VM_VarString(0, string, sizeof(string));
239 SCR_CenterPrint(string);
246 vector normalize(vector)
249 void VM_normalize (void)
255 VM_SAFEPARMCOUNT(1,VM_normalize);
257 value1 = PRVM_G_VECTOR(OFS_PARM0);
259 f = VectorLength2(value1);
263 VectorScale(value1, f, newvalue);
266 VectorClear(newvalue);
268 VectorCopy (newvalue, PRVM_G_VECTOR(OFS_RETURN));
280 VM_SAFEPARMCOUNT(1,VM_vlen);
281 PRVM_G_FLOAT(OFS_RETURN) = VectorLength(PRVM_G_VECTOR(OFS_PARM0));
288 float vectoyaw(vector)
291 void VM_vectoyaw (void)
296 VM_SAFEPARMCOUNT(1,VM_vectoyaw);
298 value1 = PRVM_G_VECTOR(OFS_PARM0);
300 if (value1[1] == 0 && value1[0] == 0)
304 yaw = (int) (atan2(value1[1], value1[0]) * 180 / M_PI);
309 PRVM_G_FLOAT(OFS_RETURN) = yaw;
317 vector vectoangles(vector)
320 void VM_vectoangles (void)
326 VM_SAFEPARMCOUNT(1,VM_vectoangles);
328 value1 = PRVM_G_VECTOR(OFS_PARM0);
330 if (value1[1] == 0 && value1[0] == 0)
340 // LordHavoc: optimized a bit
343 yaw = (atan2(value1[1], value1[0]) * 180 / M_PI);
347 else if (value1[1] > 0)
352 forward = sqrt(value1[0]*value1[0] + value1[1]*value1[1]);
353 pitch = (atan2(value1[2], forward) * 180 / M_PI);
358 PRVM_G_FLOAT(OFS_RETURN+0) = pitch;
359 PRVM_G_FLOAT(OFS_RETURN+1) = yaw;
360 PRVM_G_FLOAT(OFS_RETURN+2) = 0;
367 Returns a number from 0<= num < 1
372 void VM_random (void)
374 VM_SAFEPARMCOUNT(0,VM_random);
376 PRVM_G_FLOAT(OFS_RETURN) = lhrandom(0, 1);
383 Each entity can have eight independant sound sources, like voice,
386 Channel 0 is an auto-allocate channel, the others override anything
387 already running on that entity/channel pair.
389 An attenuation of 0 will play full volume everywhere in the level.
390 Larger attenuations will drop off.
399 prvm_edict_t *entity;
403 entity = PRVM_G_EDICT(OFS_PARM0);
404 channel = PRVM_G_FLOAT(OFS_PARM1);
405 sample = PRVM_G_STRING(OFS_PARM2);
406 volume = PRVM_G_FLOAT(OFS_PARM3) * 255;
407 attenuation = PRVM_G_FLOAT(OFS_PARM4);
409 if (volume < 0 || volume > 255)
410 Host_Error ("SV_StartSound: volume = %i", volume);
412 if (attenuation < 0 || attenuation > 4)
413 Host_Error ("SV_StartSound: attenuation = %f", attenuation);
415 if (channel < 0 || channel > 7)
416 Host_Error ("SV_StartSound: channel = %i", channel);
418 SV_StartSound (entity, channel, sample, volume, attenuation);
426 localsound(string sample)
429 void VM_localsound(void)
433 VM_SAFEPARMCOUNT(1,VM_localsound);
435 s = PRVM_G_STRING(OFS_PARM0);
437 if(!S_LocalSound (s))
439 PRVM_G_FLOAT(OFS_RETURN) = -4;
440 VM_Warning("VM_localsound: Failed to play %s for %s !\n", s, PRVM_NAME);
444 PRVM_G_FLOAT(OFS_RETURN) = 1;
456 PRVM_ERROR ("%s: break statement", PRVM_NAME);
459 //============================================================================
465 Sends text over to the client's execution buffer
467 [localcmd (string, ...) or]
471 void VM_localcmd (void)
473 char string[VM_STRINGTEMP_LENGTH];
474 VM_VarString(0, string, sizeof(string));
475 Cbuf_AddText(string);
487 VM_SAFEPARMCOUNT(1,VM_cvar);
489 PRVM_G_FLOAT(OFS_RETURN) = Cvar_VariableValue(PRVM_G_STRING(OFS_PARM0));
496 const string VM_cvar_string (string)
499 void VM_cvar_string(void)
502 VM_SAFEPARMCOUNT(1,VM_cvar_string);
504 name = PRVM_G_STRING(OFS_PARM0);
506 VM_CheckEmptyString(name);
508 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(Cvar_VariableString(name));
513 ========================
516 const string VM_cvar_defstring (string)
517 ========================
519 void VM_cvar_defstring (void)
522 VM_SAFEPARMCOUNT(1,VM_cvar_string);
524 name = PRVM_G_STRING(OFS_PARM0);
526 VM_CheckEmptyString(name);
528 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(Cvar_VariableDefString(name));
534 void cvar_set (string,string)
537 void VM_cvar_set (void)
539 VM_SAFEPARMCOUNT(2,VM_cvar_set);
541 Cvar_Set(PRVM_G_STRING(OFS_PARM0), PRVM_G_STRING(OFS_PARM1));
551 void VM_dprint (void)
553 char string[VM_STRINGTEMP_LENGTH];
554 if (developer.integer)
556 VM_VarString(0, string, sizeof(string));
558 Con_Printf("%s", string);
560 Con_Printf("%s: %s", PRVM_NAME, string);
578 VM_SAFEPARMCOUNT(1, VM_ftos);
580 v = PRVM_G_FLOAT(OFS_PARM0);
582 if ((float)((int)v) == v)
583 sprintf(s, "%i", (int)v);
586 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(s);
601 VM_SAFEPARMCOUNT(1,VM_fabs);
603 v = PRVM_G_FLOAT(OFS_PARM0);
604 PRVM_G_FLOAT(OFS_RETURN) = fabs(v);
619 VM_SAFEPARMCOUNT(1,VM_vtos);
621 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]);
622 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(s);
637 VM_SAFEPARMCOUNT(1, VM_etos);
639 sprintf (s, "entity %i", PRVM_G_EDICTNUM(OFS_PARM0));
640 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(s);
647 float stof(...[string])
652 char string[VM_STRINGTEMP_LENGTH];
653 VM_VarString(0, string, sizeof(string));
654 PRVM_G_FLOAT(OFS_RETURN) = atof(string);
658 ========================
662 ========================
666 VM_SAFEPARMCOUNT(1, VM_itof);
667 PRVM_G_FLOAT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
671 ========================
674 entity ftoe(float num)
675 ========================
680 VM_SAFEPARMCOUNT(1, VM_ftoe);
682 ent = (int)PRVM_G_FLOAT(OFS_PARM0);
683 if (ent < 0 || ent >= MAX_EDICTS || PRVM_PROG_TO_EDICT(ent)->priv.required->free)
684 ent = 0; // return world instead of a free or invalid entity
686 PRVM_G_INT(OFS_RETURN) = ent;
700 prog->xfunction->builtinsprofile += 20;
701 ed = PRVM_ED_Alloc();
713 void VM_remove (void)
716 prog->xfunction->builtinsprofile += 20;
718 VM_SAFEPARMCOUNT(1, VM_remove);
720 ed = PRVM_G_EDICT(OFS_PARM0);
721 if( PRVM_NUM_FOR_EDICT(ed) <= prog->reserved_edicts )
723 if (developer.integer >= 1)
724 VM_Warning( "VM_remove: tried to remove the null entity or a reserved entity!\n" );
726 else if( ed->priv.required->free )
728 if (developer.integer >= 1)
729 VM_Warning( "VM_remove: tried to remove an already freed entity!\n" );
733 // if (ed == prog->edicts)
734 // PRVM_ERROR ("remove: tried to remove world");
735 // if (PRVM_NUM_FOR_EDICT(ed) <= sv.maxclients)
736 // Host_Error("remove: tried to remove a client");
743 entity find(entity start, .string field, string match)
754 VM_SAFEPARMCOUNT(3,VM_find);
756 e = PRVM_G_EDICTNUM(OFS_PARM0);
757 f = PRVM_G_INT(OFS_PARM1);
758 s = PRVM_G_STRING(OFS_PARM2);
760 // LordHavoc: apparently BloodMage does a find(world, weaponmodel, "") and
761 // expects it to find all the monsters, so we must be careful to support
764 for (e++ ; e < prog->num_edicts ; e++)
766 prog->xfunction->builtinsprofile++;
767 ed = PRVM_EDICT_NUM(e);
768 if (ed->priv.required->free)
770 t = PRVM_E_STRING(ed,f);
780 VM_RETURN_EDICT(prog->edicts);
787 entity findfloat(entity start, .float field, float match)
788 entity findentity(entity start, .entity field, entity match)
791 // LordHavoc: added this for searching float, int, and entity reference fields
792 void VM_findfloat (void)
799 VM_SAFEPARMCOUNT(3,VM_findfloat);
801 e = PRVM_G_EDICTNUM(OFS_PARM0);
802 f = PRVM_G_INT(OFS_PARM1);
803 s = PRVM_G_FLOAT(OFS_PARM2);
805 for (e++ ; e < prog->num_edicts ; e++)
807 prog->xfunction->builtinsprofile++;
808 ed = PRVM_EDICT_NUM(e);
809 if (ed->priv.required->free)
811 if (PRVM_E_FLOAT(ed,f) == s)
818 VM_RETURN_EDICT(prog->edicts);
825 entity findchain(.string field, string match)
828 // chained search for strings in entity fields
829 // entity(.string field, string match) findchain = #402;
830 void VM_findchain (void)
836 prvm_edict_t *ent, *chain;
838 VM_SAFEPARMCOUNT(2,VM_findchain);
840 // is the same like !(prog->flag & PRVM_FE_CHAIN) - even if the operator precedence is another
841 if(!prog->flag & PRVM_FE_CHAIN)
842 PRVM_ERROR("VM_findchain: %s doesnt have a chain field !", PRVM_NAME);
844 chain_of = PRVM_ED_FindField("chain")->ofs;
846 chain = prog->edicts;
848 f = PRVM_G_INT(OFS_PARM0);
849 s = PRVM_G_STRING(OFS_PARM1);
851 // LordHavoc: apparently BloodMage does a find(world, weaponmodel, "") and
852 // expects it to find all the monsters, so we must be careful to support
855 ent = PRVM_NEXT_EDICT(prog->edicts);
856 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
858 prog->xfunction->builtinsprofile++;
859 if (ent->priv.required->free)
861 t = PRVM_E_STRING(ent,f);
867 PRVM_E_INT(ent,chain_of) = PRVM_NUM_FOR_EDICT(chain);
871 VM_RETURN_EDICT(chain);
878 entity findchainfloat(.string field, float match)
879 entity findchainentity(.string field, entity match)
882 // LordHavoc: chained search for float, int, and entity reference fields
883 // entity(.string field, float match) findchainfloat = #403;
884 void VM_findchainfloat (void)
890 prvm_edict_t *ent, *chain;
892 VM_SAFEPARMCOUNT(2, VM_findchainfloat);
894 if(!prog->flag & PRVM_FE_CHAIN)
895 PRVM_ERROR("VM_findchainfloat: %s doesnt have a chain field !", PRVM_NAME);
897 chain_of = PRVM_ED_FindField("chain")->ofs;
899 chain = (prvm_edict_t *)prog->edicts;
901 f = PRVM_G_INT(OFS_PARM0);
902 s = PRVM_G_FLOAT(OFS_PARM1);
904 ent = PRVM_NEXT_EDICT(prog->edicts);
905 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
907 prog->xfunction->builtinsprofile++;
908 if (ent->priv.required->free)
910 if (PRVM_E_FLOAT(ent,f) != s)
913 PRVM_E_INT(ent,chain_of) = PRVM_EDICT_TO_PROG(chain);
917 VM_RETURN_EDICT(chain);
921 ========================
924 entity findflags(entity start, .float field, float match)
925 ========================
927 // LordHavoc: search for flags in float fields
928 void VM_findflags (void)
935 VM_SAFEPARMCOUNT(3, VM_findflags);
938 e = PRVM_G_EDICTNUM(OFS_PARM0);
939 f = PRVM_G_INT(OFS_PARM1);
940 s = (int)PRVM_G_FLOAT(OFS_PARM2);
942 for (e++ ; e < prog->num_edicts ; e++)
944 prog->xfunction->builtinsprofile++;
945 ed = PRVM_EDICT_NUM(e);
946 if (ed->priv.required->free)
948 if (!PRVM_E_FLOAT(ed,f))
950 if ((int)PRVM_E_FLOAT(ed,f) & s)
957 VM_RETURN_EDICT(prog->edicts);
961 ========================
964 entity findchainflags(.float field, float match)
965 ========================
967 // LordHavoc: chained search for flags in float fields
968 void VM_findchainflags (void)
974 prvm_edict_t *ent, *chain;
976 VM_SAFEPARMCOUNT(2, VM_findchainflags);
978 if(!prog->flag & PRVM_FE_CHAIN)
979 PRVM_ERROR("VM_findchainflags: %s doesnt have a chain field !", PRVM_NAME);
981 chain_of = PRVM_ED_FindField("chain")->ofs;
983 chain = (prvm_edict_t *)prog->edicts;
985 f = PRVM_G_INT(OFS_PARM0);
986 s = (int)PRVM_G_FLOAT(OFS_PARM1);
988 ent = PRVM_NEXT_EDICT(prog->edicts);
989 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
991 prog->xfunction->builtinsprofile++;
992 if (ent->priv.required->free)
994 if (!PRVM_E_FLOAT(ent,f))
996 if (!((int)PRVM_E_FLOAT(ent,f) & s))
999 PRVM_E_INT(ent,chain_of) = PRVM_EDICT_TO_PROG(chain);
1003 VM_RETURN_EDICT(chain);
1013 void VM_coredump (void)
1015 VM_SAFEPARMCOUNT(0,VM_coredump);
1017 Cbuf_AddText("prvm_edicts ");
1018 Cbuf_AddText(PRVM_NAME);
1029 void PRVM_StackTrace(void);
1030 void VM_stackdump (void)
1032 VM_SAFEPARMCOUNT(0, VM_stackdump);
1047 VM_SAFEPARMCOUNT(0, VM_crash);
1049 PRVM_ERROR("Crash called by %s",PRVM_NAME);
1059 void VM_traceon (void)
1061 VM_SAFEPARMCOUNT(0,VM_traceon);
1073 void VM_traceoff (void)
1075 VM_SAFEPARMCOUNT(0,VM_traceoff);
1077 prog->trace = false;
1087 void VM_eprint (void)
1089 VM_SAFEPARMCOUNT(1,VM_eprint);
1091 PRVM_ED_PrintNum (PRVM_G_EDICTNUM(OFS_PARM0));
1104 VM_SAFEPARMCOUNT(1,VM_rint);
1106 f = PRVM_G_FLOAT(OFS_PARM0);
1108 PRVM_G_FLOAT(OFS_RETURN) = floor(f + 0.5);
1110 PRVM_G_FLOAT(OFS_RETURN) = ceil(f - 0.5);
1120 void VM_floor (void)
1122 VM_SAFEPARMCOUNT(1,VM_floor);
1124 PRVM_G_FLOAT(OFS_RETURN) = floor(PRVM_G_FLOAT(OFS_PARM0));
1136 VM_SAFEPARMCOUNT(1,VM_ceil);
1138 PRVM_G_FLOAT(OFS_RETURN) = ceil(PRVM_G_FLOAT(OFS_PARM0));
1146 entity nextent(entity)
1149 void VM_nextent (void)
1154 i = PRVM_G_EDICTNUM(OFS_PARM0);
1157 prog->xfunction->builtinsprofile++;
1159 if (i == prog->num_edicts)
1161 VM_RETURN_EDICT(prog->edicts);
1164 ent = PRVM_EDICT_NUM(i);
1165 if (!ent->priv.required->free)
1167 VM_RETURN_EDICT(ent);
1173 //=============================================================================
1180 changelevel(string map)
1183 void VM_changelevel (void)
1185 VM_SAFEPARMCOUNT(1, VM_changelevel);
1189 VM_Warning("VM_changelevel: game is not server (%s)\n", PRVM_NAME);
1193 // make sure we don't issue two changelevels
1194 if (svs.changelevel_issued)
1196 svs.changelevel_issued = true;
1198 Cbuf_AddText (va("changelevel %s\n",PRVM_G_STRING(OFS_PARM0)));
1210 VM_SAFEPARMCOUNT(1,VM_sin);
1211 PRVM_G_FLOAT(OFS_RETURN) = sin(PRVM_G_FLOAT(OFS_PARM0));
1222 VM_SAFEPARMCOUNT(1,VM_cos);
1223 PRVM_G_FLOAT(OFS_RETURN) = cos(PRVM_G_FLOAT(OFS_PARM0));
1235 VM_SAFEPARMCOUNT(1,VM_sqrt);
1236 PRVM_G_FLOAT(OFS_RETURN) = sqrt(PRVM_G_FLOAT(OFS_PARM0));
1248 VM_SAFEPARMCOUNT(1,VM_asin);
1249 PRVM_G_FLOAT(OFS_RETURN) = asin(PRVM_G_FLOAT(OFS_PARM0));
1260 VM_SAFEPARMCOUNT(1,VM_acos);
1261 PRVM_G_FLOAT(OFS_RETURN) = acos(PRVM_G_FLOAT(OFS_PARM0));
1272 VM_SAFEPARMCOUNT(1,VM_atan);
1273 PRVM_G_FLOAT(OFS_RETURN) = atan(PRVM_G_FLOAT(OFS_PARM0));
1279 float atan2(float,float)
1282 void VM_atan2 (void)
1284 VM_SAFEPARMCOUNT(2,VM_atan2);
1285 PRVM_G_FLOAT(OFS_RETURN) = atan2(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1296 VM_SAFEPARMCOUNT(1,VM_tan);
1297 PRVM_G_FLOAT(OFS_RETURN) = tan(PRVM_G_FLOAT(OFS_PARM0));
1304 Returns a vector of length < 1 and > 0
1309 void VM_randomvec (void)
1314 VM_SAFEPARMCOUNT(0, VM_randomvec);
1319 temp[0] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1320 temp[1] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1321 temp[2] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1323 while (DotProduct(temp, temp) >= 1);
1324 VectorCopy (temp, PRVM_G_VECTOR(OFS_RETURN));
1327 temp[0] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1328 temp[1] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1329 temp[2] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1330 // length returned always > 0
1331 length = (rand()&32766 + 1) * (1.0 / 32767.0) / VectorLength(temp);
1332 VectorScale(temp,length, temp);*/
1333 //VectorCopy(temp, PRVM_G_VECTOR(OFS_RETURN));
1336 //=============================================================================
1342 float registercvar (string name, string value, float flags)
1345 void VM_registercvar (void)
1347 const char *name, *value;
1350 VM_SAFEPARMCOUNT(3,VM_registercvar);
1352 name = PRVM_G_STRING(OFS_PARM0);
1353 value = PRVM_G_STRING(OFS_PARM1);
1354 flags = (int)PRVM_G_FLOAT(OFS_PARM2);
1355 PRVM_G_FLOAT(OFS_RETURN) = 0;
1357 if(flags > CVAR_MAXFLAGSVAL)
1360 // first check to see if it has already been defined
1361 if (Cvar_FindVar (name))
1364 // check for overlap with a command
1365 if (Cmd_Exists (name))
1367 VM_Warning("VM_registercvar: %s is a command\n", name);
1371 Cvar_Get(name, value, flags);
1373 PRVM_G_FLOAT(OFS_RETURN) = 1; // success
1380 returns the minimum of two supplied floats
1382 float min(float a, float b, ...[float])
1387 // LordHavoc: 3+ argument enhancement suggested by FrikaC
1388 if (prog->argc == 2)
1389 PRVM_G_FLOAT(OFS_RETURN) = min(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1390 else if (prog->argc >= 3)
1393 float f = PRVM_G_FLOAT(OFS_PARM0);
1394 for (i = 1;i < prog->argc;i++)
1395 if (PRVM_G_FLOAT((OFS_PARM0+i*3)) < f)
1396 f = PRVM_G_FLOAT((OFS_PARM0+i*3));
1397 PRVM_G_FLOAT(OFS_RETURN) = f;
1400 PRVM_ERROR("VM_min: %s must supply at least 2 floats", PRVM_NAME);
1407 returns the maximum of two supplied floats
1409 float max(float a, float b, ...[float])
1414 // LordHavoc: 3+ argument enhancement suggested by FrikaC
1415 if (prog->argc == 2)
1416 PRVM_G_FLOAT(OFS_RETURN) = max(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1417 else if (prog->argc >= 3)
1420 float f = PRVM_G_FLOAT(OFS_PARM0);
1421 for (i = 1;i < prog->argc;i++)
1422 if (PRVM_G_FLOAT((OFS_PARM0+i*3)) > f)
1423 f = PRVM_G_FLOAT((OFS_PARM0+i*3));
1424 PRVM_G_FLOAT(OFS_RETURN) = f;
1427 PRVM_ERROR("VM_max: %s must supply at least 2 floats", PRVM_NAME);
1434 returns number bounded by supplied range
1436 float bound(float min, float value, float max)
1439 void VM_bound (void)
1441 VM_SAFEPARMCOUNT(3,VM_bound);
1442 PRVM_G_FLOAT(OFS_RETURN) = bound(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1), PRVM_G_FLOAT(OFS_PARM2));
1449 returns a raised to power b
1451 float pow(float a, float b)
1456 VM_SAFEPARMCOUNT(2,VM_pow);
1457 PRVM_G_FLOAT(OFS_RETURN) = pow(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1464 copies data from one entity to another
1466 copyentity(entity src, entity dst)
1469 void VM_copyentity (void)
1471 prvm_edict_t *in, *out;
1472 VM_SAFEPARMCOUNT(2,VM_copyentity);
1473 in = PRVM_G_EDICT(OFS_PARM0);
1474 out = PRVM_G_EDICT(OFS_PARM1);
1475 memcpy(out->fields.vp, in->fields.vp, prog->progs->entityfields * 4);
1482 sets the color of a client and broadcasts the update to all connected clients
1484 setcolor(clientent, value)
1487 /*void PF_setcolor (void)
1493 entnum = PRVM_G_EDICTNUM(OFS_PARM0);
1494 i = PRVM_G_FLOAT(OFS_PARM1);
1496 if (entnum < 1 || entnum > svs.maxclients || !svs.clients[entnum-1].active)
1498 Con_Print("tried to setcolor a non-client\n");
1502 client = svs.clients + entnum-1;
1503 if ((val = PRVM_GETEDICTFIELDVALUE(client->edict, eval_clientcolors)))
1506 client->old_colors = i;
1507 client->edict->fields.server->team = (i & 15) + 1;
1509 MSG_WriteByte (&sv.reliable_datagram, svc_updatecolors);
1510 MSG_WriteByte (&sv.reliable_datagram, entnum - 1);
1511 MSG_WriteByte (&sv.reliable_datagram, i);
1514 void VM_Files_Init(void)
1517 for (i = 0;i < PRVM_MAX_OPENFILES;i++)
1518 prog->openfiles[i] = NULL;
1521 void VM_Files_CloseAll(void)
1524 for (i = 0;i < PRVM_MAX_OPENFILES;i++)
1526 if (prog->openfiles[i])
1527 FS_Close(prog->openfiles[i]);
1528 prog->openfiles[i] = NULL;
1532 qfile_t *VM_GetFileHandle( int index )
1534 if (index < 0 || index >= PRVM_MAX_OPENFILES)
1536 Con_Printf("VM_GetFileHandle: invalid file handle %i used in %s\n", index, PRVM_NAME);
1539 if (prog->openfiles[index] == NULL)
1541 Con_Printf("VM_GetFileHandle: no such file handle %i (or file has been closed) in %s\n", index, PRVM_NAME);
1544 return prog->openfiles[index];
1551 float fopen(string filename, float mode)
1554 // float(string filename, float mode) fopen = #110;
1555 // opens a file inside quake/gamedir/data/ (mode is FILE_READ, FILE_APPEND, or FILE_WRITE),
1556 // returns fhandle >= 0 if successful, or fhandle < 0 if unable to open file for any reason
1560 const char *modestring, *filename;
1562 VM_SAFEPARMCOUNT(2,VM_fopen);
1564 for (filenum = 0;filenum < PRVM_MAX_OPENFILES;filenum++)
1565 if (prog->openfiles[filenum] == NULL)
1567 if (filenum >= PRVM_MAX_OPENFILES)
1569 PRVM_G_FLOAT(OFS_RETURN) = -2;
1570 VM_Warning("VM_fopen: %s ran out of file handles (%i)\n", PRVM_NAME, PRVM_MAX_OPENFILES);
1573 mode = (int)PRVM_G_FLOAT(OFS_PARM1);
1576 case 0: // FILE_READ
1579 case 1: // FILE_APPEND
1582 case 2: // FILE_WRITE
1586 PRVM_G_FLOAT(OFS_RETURN) = -3;
1587 VM_Warning("VM_fopen: %s: no such mode %i (valid: 0 = read, 1 = append, 2 = write)\n", PRVM_NAME, mode);
1590 filename = PRVM_G_STRING(OFS_PARM0);
1592 prog->openfiles[filenum] = FS_Open(va("data/%s", filename), modestring, false, false);
1593 if (prog->openfiles[filenum] == NULL && mode == 0)
1594 prog->openfiles[filenum] = FS_Open(va("%s", filename), modestring, false, false);
1596 if (prog->openfiles[filenum] == NULL)
1598 PRVM_G_FLOAT(OFS_RETURN) = -1;
1599 if (developer.integer >= 100)
1600 VM_Warning("VM_fopen: %s: %s mode %s failed\n", PRVM_NAME, filename, modestring);
1604 PRVM_G_FLOAT(OFS_RETURN) = filenum;
1605 if (developer.integer >= 100)
1606 Con_Printf("VM_fopen: %s: %s mode %s opened as #%i\n", PRVM_NAME, filename, modestring, filenum);
1614 fclose(float fhandle)
1617 //void(float fhandle) fclose = #111; // closes a file
1618 void VM_fclose(void)
1622 VM_SAFEPARMCOUNT(1,VM_fclose);
1624 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1625 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
1627 VM_Warning("VM_fclose: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1630 if (prog->openfiles[filenum] == NULL)
1632 VM_Warning("VM_fclose: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1635 FS_Close(prog->openfiles[filenum]);
1636 prog->openfiles[filenum] = NULL;
1637 if (developer.integer >= 100)
1638 Con_Printf("VM_fclose: %s: #%i closed\n", PRVM_NAME, filenum);
1645 string fgets(float fhandle)
1648 //string(float fhandle) fgets = #112; // reads a line of text from the file and returns as a tempstring
1652 char string[VM_STRINGTEMP_LENGTH];
1655 VM_SAFEPARMCOUNT(1,VM_fgets);
1657 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1658 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
1660 VM_Warning("VM_fgets: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1663 if (prog->openfiles[filenum] == NULL)
1665 VM_Warning("VM_fgets: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1671 c = FS_Getc(prog->openfiles[filenum]);
1672 if (c == '\r' || c == '\n' || c < 0)
1674 if (end < VM_STRINGTEMP_LENGTH - 1)
1678 // remove \n following \r
1681 c = FS_Getc(prog->openfiles[filenum]);
1683 FS_UnGetc(prog->openfiles[filenum], (unsigned char)c);
1685 if (developer.integer >= 100)
1686 Con_Printf("fgets: %s: %s\n", PRVM_NAME, string);
1688 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string);
1690 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
1697 fputs(float fhandle, string s)
1700 //void(float fhandle, string s) fputs = #113; // writes a line of text to the end of the file
1704 char string[VM_STRINGTEMP_LENGTH];
1707 VM_SAFEPARMCOUNT(2,VM_fputs);
1709 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1710 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
1712 VM_Warning("VM_fputs: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1715 if (prog->openfiles[filenum] == NULL)
1717 VM_Warning("VM_fputs: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1720 VM_VarString(1, string, sizeof(string));
1721 if ((stringlength = (int)strlen(string)))
1722 FS_Write(prog->openfiles[filenum], string, stringlength);
1723 if (developer.integer >= 100)
1724 Con_Printf("fputs: %s: %s\n", PRVM_NAME, string);
1731 float strlen(string s)
1734 //float(string s) strlen = #114; // returns how many characters are in a string
1735 void VM_strlen(void)
1737 VM_SAFEPARMCOUNT(1,VM_strlen);
1739 PRVM_G_FLOAT(OFS_RETURN) = strlen(PRVM_G_STRING(OFS_PARM0));
1742 // DRESK - Decolorized String
1747 string strdecolorize(string s)
1750 // string (string s) strdecolorize = #472; // returns the passed in string with color codes stripped
1751 void VM_strdecolorize(void)
1753 char szNewString[VM_STRINGTEMP_LENGTH];
1754 const char *szString;
1765 VM_SAFEPARMCOUNT(1,VM_strdecolorize);
1766 szString = PRVM_G_STRING(OFS_PARM0);
1769 { // Traverse through String
1770 if( szString[nPos] == '\n' || szString[nPos] == '\r' || szString[nPos] <= 0)
1771 { // String End Found
1772 szNewString[nFillPos++] = szString[nPos];
1776 if( szString[nPos] == STRING_COLOR_TAG)
1777 { // Color Code Located
1778 if( szString[nPos + 1] == STRING_COLOR_TAG)
1779 { // Valid Characters to Include
1780 szNewString[nFillPos++] = szString[nPos];
1782 szNewString[nFillPos++] = szString[nPos];
1785 if( szString[nPos + 1] >= '0' && szString[nPos + 1] <= '9' )
1786 { // Color Code Found; Increment Position
1790 { // Unknown Color Code; Include
1791 szNewString[nFillPos++] = szString[nPos];
1796 // Include Character
1797 szNewString[nFillPos++] = szString[nPos];
1799 // Increment Position
1803 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(szNewString);
1806 // DRESK - String Length (not counting color codes)
1811 float strlennocol(string s)
1814 // float(string s) strlennocol = #471; // returns how many characters are in a string not including color codes
1815 // For example, ^2Dresk returns a length of 5
1816 void VM_strlennocol(void)
1818 const char *szString;
1826 VM_SAFEPARMCOUNT(1,VM_strlennocol);
1828 szString = PRVM_G_STRING(OFS_PARM0);
1831 { // Count Characters
1832 // SV_BroadcastPrintf("Position '%d'; Character '%c'; Length '%d'\n", nPos, szString[nPos], nCnt);
1834 if( szString[nPos] == '\n' || szString[nPos] == '\r' || szString[nPos] <= 0)
1835 { // String End Found
1836 // SV_BroadcastPrintf("Found End of String at '%d'\n", nPos);
1840 if( szString[nPos] == STRING_COLOR_TAG)
1841 { // Color Code Located
1842 if( szString[nPos + 1] == STRING_COLOR_TAG)
1843 { // Increment Length; Skip Color Code
1848 if( szString[nPos + 1] >= '0' && szString[nPos + 1] <= '9' )
1849 { // Color Code Found; Increment Position
1850 // SV_BroadcastPrintf("Found Color Codes at '%d'\n", nPos);
1854 { // Unknown Color Code; Increment Length!
1860 // Increment String Length
1863 // Increment Position
1866 PRVM_G_FLOAT(OFS_RETURN) = nCnt;
1873 string strcat(string,string,...[string])
1876 //string(string s1, string s2) strcat = #115;
1877 // concatenates two strings (for example "abc", "def" would return "abcdef")
1878 // and returns as a tempstring
1879 void VM_strcat(void)
1881 char s[VM_STRINGTEMP_LENGTH];
1884 PRVM_ERROR("VM_strcat wrong parameter count (min. 1 expected ) !");
1886 VM_VarString(0, s, sizeof(s));
1887 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(s);
1894 string substring(string s, float start, float length)
1897 // string(string s, float start, float length) substring = #116;
1898 // returns a section of a string as a tempstring
1899 void VM_substring(void)
1901 int i, start, length;
1903 char string[VM_STRINGTEMP_LENGTH];
1905 VM_SAFEPARMCOUNT(3,VM_substring);
1907 s = PRVM_G_STRING(OFS_PARM0);
1908 start = (int)PRVM_G_FLOAT(OFS_PARM1);
1909 length = (int)PRVM_G_FLOAT(OFS_PARM2);
1910 for (i = 0;i < start && *s;i++, s++);
1911 for (i = 0;i < (int)sizeof(string) - 1 && *s && i < length;i++, s++)
1914 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string);
1921 vector stov(string s)
1924 //vector(string s) stov = #117; // returns vector value from a string
1927 char string[VM_STRINGTEMP_LENGTH];
1929 VM_SAFEPARMCOUNT(1,VM_stov);
1931 VM_VarString(0, string, sizeof(string));
1932 Math_atov(string, PRVM_G_VECTOR(OFS_RETURN));
1939 string strzone(string s)
1942 //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)
1943 void VM_strzone(void)
1946 char string[VM_STRINGTEMP_LENGTH];
1949 VM_SAFEPARMCOUNT(1,VM_strzone);
1951 VM_VarString(0, string, sizeof(string));
1952 alloclen = strlen(string) + 1;
1953 PRVM_G_INT(OFS_RETURN) = PRVM_AllocString(alloclen, &out);
1954 memcpy(out, string, alloclen);
1964 //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!!!)
1965 void VM_strunzone(void)
1967 VM_SAFEPARMCOUNT(1,VM_strunzone);
1968 PRVM_FreeString(PRVM_G_INT(OFS_PARM0));
1973 VM_command (used by client and menu)
1975 clientcommand(float client, string s) (for client and menu)
1978 //void(entity e, string s) clientcommand = #440; // executes a command string as if it came from the specified client
1979 //this function originally written by KrimZon, made shorter by LordHavoc
1980 void VM_clcommand (void)
1982 client_t *temp_client;
1985 VM_SAFEPARMCOUNT(2,VM_clcommand);
1987 i = (int)PRVM_G_FLOAT(OFS_PARM0);
1988 if (!sv.active || i < 0 || i >= svs.maxclients || !svs.clients[i].active)
1990 VM_Warning("VM_clientcommand: %s: invalid client/server is not active !\n", PRVM_NAME);
1994 temp_client = host_client;
1995 host_client = svs.clients + i;
1996 Cmd_ExecuteString (PRVM_G_STRING(OFS_PARM1), src_client);
1997 host_client = temp_client;
2005 float tokenize(string s)
2008 //float(string s) tokenize = #441; // takes apart a string into individal words (access them with argv), returns how many
2009 //this function originally written by KrimZon, made shorter by LordHavoc
2010 //20040203: rewritten by LordHavoc (no longer uses allocations)
2013 void VM_tokenize (void)
2018 char tokenbuf[MAX_INPUTLINE];
2022 VM_SAFEPARMCOUNT(1,VM_tokenize);
2024 p = PRVM_G_STRING(OFS_PARM0);
2027 while(COM_ParseToken(&p, false))
2029 if (num_tokens >= (int)(sizeof(tokens)/sizeof(tokens[0])))
2032 tokenlen = strlen(com_token) + 1;
2033 if (pos + tokenlen > sizeof(tokenbuf))
2035 tokens[num_tokens++] = PRVM_SetEngineString(tokenbuf + pos);
2036 memcpy(tokenbuf + pos, com_token, tokenlen);
2039 tokens[num_tokens++] = PRVM_SetTempString(com_token);
2043 PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
2046 //string(float n) argv = #442; // returns a word from the tokenized string (returns nothing for an invalid index)
2047 //this function originally written by KrimZon, made shorter by LordHavoc
2052 VM_SAFEPARMCOUNT(1,VM_argv);
2054 token_num = (int)PRVM_G_FLOAT(OFS_PARM0);
2056 if (token_num >= 0 && token_num < num_tokens)
2057 PRVM_G_INT(OFS_RETURN) = tokens[token_num];
2059 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
2063 //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)
2064 void PF_setattachment (void)
2066 prvm_edict_t *e = PRVM_G_EDICT(OFS_PARM0);
2067 prvm_edict_t *tagentity = PRVM_G_EDICT(OFS_PARM1);
2068 char *tagname = PRVM_G_STRING(OFS_PARM2);
2073 if (tagentity == NULL)
2074 tagentity = prog->edicts;
2076 v = PRVM_GETEDICTFIELDVALUE(e, eval_tag_entity);
2078 fields.server->edict = PRVM_EDICT_TO_PROG(tagentity);
2080 v = PRVM_GETEDICTFIELDVALUE(e, eval_tag_index);
2082 fields.server->_float = 0;
2083 if (tagentity != NULL && tagentity != prog->edicts && tagname && tagname[0])
2085 modelindex = (int)tagentity->fields.server->modelindex;
2086 if (modelindex >= 0 && modelindex < MAX_MODELS)
2088 model = sv.models[modelindex];
2089 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)
2090 for (i = 0;i < model->data_overridetagnamesforskin[(unsigned int)tagentity->fields.server->skin].num_overridetagnames;i++)
2091 if (!strcmp(tagname, model->data_overridetagnamesforskin[(unsigned int)tagentity->fields.server->skin].data_overridetagnames[i].name))
2092 fields.server->_float = i + 1;
2093 // FIXME: use a model function to get tag info (need to handle skeletal)
2094 if (fields.server->_float == 0 && model->num_tags)
2095 for (i = 0;i < model->num_tags;i++)
2096 if (!strcmp(tagname, model->data_tags[i].name))
2097 fields.server->_float = i + 1;
2098 if (fields.server->_float == 0)
2099 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);
2102 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));
2113 void VM_isserver(void)
2115 VM_SAFEPARMCOUNT(0,VM_serverstate);
2117 PRVM_G_FLOAT(OFS_RETURN) = sv.active;
2127 void VM_clientcount(void)
2129 VM_SAFEPARMCOUNT(0,VM_clientcount);
2131 PRVM_G_FLOAT(OFS_RETURN) = svs.maxclients;
2141 void VM_clientstate(void)
2143 VM_SAFEPARMCOUNT(0,VM_clientstate);
2145 PRVM_G_FLOAT(OFS_RETURN) = cls.state;
2152 float getostype(void)
2154 */ // not used at the moment -> not included in the common list
2155 void VM_getostype(void)
2157 VM_SAFEPARMCOUNT(0,VM_getostype);
2162 OS_MAC - not supported
2166 PRVM_G_FLOAT(OFS_RETURN) = 0;
2167 #elif defined(MACOSX)
2168 PRVM_G_FLOAT(OFS_RETURN) = 2;
2170 PRVM_G_FLOAT(OFS_RETURN) = 1;
2178 vector getmousepos()
2181 void VM_getmousepos(void)
2184 VM_SAFEPARMCOUNT(0,VM_getmousepos);
2186 PRVM_G_VECTOR(OFS_RETURN)[0] = in_mouse_x * vid_conwidth.integer / vid.width;
2187 PRVM_G_VECTOR(OFS_RETURN)[1] = in_mouse_y * vid_conheight.integer / vid.height;
2188 PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
2198 void VM_gettime(void)
2200 VM_SAFEPARMCOUNT(0,VM_gettime);
2202 PRVM_G_FLOAT(OFS_RETURN) = (float) *prog->time;
2209 loadfromdata(string data)
2212 void VM_loadfromdata(void)
2214 VM_SAFEPARMCOUNT(1,VM_loadentsfromfile);
2216 PRVM_ED_LoadFromFile(PRVM_G_STRING(OFS_PARM0));
2220 ========================
2223 parseentitydata(entity ent, string data)
2224 ========================
2226 void VM_parseentitydata(void)
2231 VM_SAFEPARMCOUNT(2, VM_parseentitydata);
2233 // get edict and test it
2234 ent = PRVM_G_EDICT(OFS_PARM0);
2235 if (ent->priv.required->free)
2236 PRVM_ERROR ("VM_parseentitydata: %s: Can only set already spawned entities (entity %i is free)!", PRVM_NAME, PRVM_NUM_FOR_EDICT(ent));
2238 data = PRVM_G_STRING(OFS_PARM1);
2240 // parse the opening brace
2241 if (!COM_ParseTokenConsole(&data) || com_token[0] != '{' )
2242 PRVM_ERROR ("VM_parseentitydata: %s: Couldn't parse entity data:\n%s", PRVM_NAME, data );
2244 PRVM_ED_ParseEdict (data, ent);
2251 loadfromfile(string file)
2254 void VM_loadfromfile(void)
2256 const char *filename;
2259 VM_SAFEPARMCOUNT(1,VM_loadfromfile);
2261 filename = PRVM_G_STRING(OFS_PARM0);
2262 if (FS_CheckNastyPath(filename, false))
2264 PRVM_G_FLOAT(OFS_RETURN) = -4;
2265 VM_Warning("VM_loadfromfile: %s dangerous or non-portable filename \"%s\" not allowed. (contains : or \\ or begins with .. or /)\n", PRVM_NAME, filename);
2269 // not conform with VM_fopen
2270 data = (char *)FS_LoadFile(filename, tempmempool, false, NULL);
2272 PRVM_G_FLOAT(OFS_RETURN) = -1;
2274 PRVM_ED_LoadFromFile(data);
2285 float mod(float val, float m)
2288 void VM_modulo(void)
2291 VM_SAFEPARMCOUNT(2,VM_module);
2293 val = (int) PRVM_G_FLOAT(OFS_PARM0);
2294 m = (int) PRVM_G_FLOAT(OFS_PARM1);
2296 PRVM_G_FLOAT(OFS_RETURN) = (float) (val % m);
2299 void VM_Search_Init(void)
2302 for (i = 0;i < PRVM_MAX_OPENSEARCHES;i++)
2303 prog->opensearches[i] = NULL;
2306 void VM_Search_Reset(void)
2309 // reset the fssearch list
2310 for(i = 0; i < PRVM_MAX_OPENSEARCHES; i++)
2312 if(prog->opensearches[i])
2313 FS_FreeSearch(prog->opensearches[i]);
2314 prog->opensearches[i] = NULL;
2322 float search_begin(string pattern, float caseinsensitive, float quiet)
2325 void VM_search_begin(void)
2328 const char *pattern;
2329 int caseinsens, quiet;
2331 VM_SAFEPARMCOUNT(3, VM_search_begin);
2333 pattern = PRVM_G_STRING(OFS_PARM0);
2335 VM_CheckEmptyString(pattern);
2337 caseinsens = (int)PRVM_G_FLOAT(OFS_PARM1);
2338 quiet = (int)PRVM_G_FLOAT(OFS_PARM2);
2340 for(handle = 0; handle < PRVM_MAX_OPENSEARCHES; handle++)
2341 if(!prog->opensearches[handle])
2344 if(handle >= PRVM_MAX_OPENSEARCHES)
2346 PRVM_G_FLOAT(OFS_RETURN) = -2;
2347 VM_Warning("VM_search_begin: %s ran out of search handles (%i)\n", PRVM_NAME, PRVM_MAX_OPENSEARCHES);
2351 if(!(prog->opensearches[handle] = FS_Search(pattern,caseinsens, quiet)))
2352 PRVM_G_FLOAT(OFS_RETURN) = -1;
2354 PRVM_G_FLOAT(OFS_RETURN) = handle;
2361 void search_end(float handle)
2364 void VM_search_end(void)
2367 VM_SAFEPARMCOUNT(1, VM_search_end);
2369 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
2371 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
2373 VM_Warning("VM_search_end: invalid handle %i used in %s\n", handle, PRVM_NAME);
2376 if(prog->opensearches[handle] == NULL)
2378 VM_Warning("VM_search_end: no such handle %i in %s\n", handle, PRVM_NAME);
2382 FS_FreeSearch(prog->opensearches[handle]);
2383 prog->opensearches[handle] = NULL;
2390 float search_getsize(float handle)
2393 void VM_search_getsize(void)
2396 VM_SAFEPARMCOUNT(1, VM_M_search_getsize);
2398 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
2400 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
2402 VM_Warning("VM_search_getsize: invalid handle %i used in %s\n", handle, PRVM_NAME);
2405 if(prog->opensearches[handle] == NULL)
2407 VM_Warning("VM_search_getsize: no such handle %i in %s\n", handle, PRVM_NAME);
2411 PRVM_G_FLOAT(OFS_RETURN) = prog->opensearches[handle]->numfilenames;
2416 VM_search_getfilename
2418 string search_getfilename(float handle, float num)
2421 void VM_search_getfilename(void)
2423 int handle, filenum;
2424 VM_SAFEPARMCOUNT(2, VM_search_getfilename);
2426 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
2427 filenum = (int)PRVM_G_FLOAT(OFS_PARM1);
2429 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
2431 VM_Warning("VM_search_getfilename: invalid handle %i used in %s\n", handle, PRVM_NAME);
2434 if(prog->opensearches[handle] == NULL)
2436 VM_Warning("VM_search_getfilename: no such handle %i in %s\n", handle, PRVM_NAME);
2439 if(filenum < 0 || filenum >= prog->opensearches[handle]->numfilenames)
2441 VM_Warning("VM_search_getfilename: invalid filenum %i in %s\n", filenum, PRVM_NAME);
2445 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog->opensearches[handle]->filenames[filenum]);
2452 string chr(float ascii)
2458 VM_SAFEPARMCOUNT(1, VM_chr);
2460 tmp[0] = (unsigned char) PRVM_G_FLOAT(OFS_PARM0);
2463 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(tmp);
2466 //=============================================================================
2467 // Draw builtins (client & menu)
2473 float iscachedpic(string pic)
2476 void VM_iscachedpic(void)
2478 VM_SAFEPARMCOUNT(1,VM_iscachedpic);
2480 // drawq hasnt such a function, thus always return true
2481 PRVM_G_FLOAT(OFS_RETURN) = false;
2488 string precache_pic(string pic)
2491 void VM_precache_pic(void)
2495 VM_SAFEPARMCOUNT(1, VM_precache_pic);
2497 s = PRVM_G_STRING(OFS_PARM0);
2498 PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
2499 VM_CheckEmptyString (s);
2501 // AK Draw_CachePic is supposed to always return a valid pointer
2502 if( Draw_CachePic(s, false)->tex == r_texture_notexture )
2503 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
2513 void VM_freepic(void)
2517 VM_SAFEPARMCOUNT(1,VM_freepic);
2519 s = PRVM_G_STRING(OFS_PARM0);
2520 VM_CheckEmptyString (s);
2529 float drawcharacter(vector position, float character, vector scale, vector rgb, float alpha, float flag)
2532 void VM_drawcharacter(void)
2534 float *pos,*scale,*rgb;
2537 VM_SAFEPARMCOUNT(6,VM_drawcharacter);
2539 character = (char) PRVM_G_FLOAT(OFS_PARM1);
2542 PRVM_G_FLOAT(OFS_RETURN) = -1;
2543 VM_Warning("VM_drawcharacter: %s passed null character !\n",PRVM_NAME);
2547 pos = PRVM_G_VECTOR(OFS_PARM0);
2548 scale = PRVM_G_VECTOR(OFS_PARM2);
2549 rgb = PRVM_G_VECTOR(OFS_PARM3);
2550 flag = (int)PRVM_G_FLOAT(OFS_PARM5);
2552 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2554 PRVM_G_FLOAT(OFS_RETURN) = -2;
2555 VM_Warning("VM_drawcharacter: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2559 if(pos[2] || scale[2])
2560 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")));
2562 if(!scale[0] || !scale[1])
2564 PRVM_G_FLOAT(OFS_RETURN) = -3;
2565 VM_Warning("VM_drawcharacter: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
2569 DrawQ_String (pos[0], pos[1], &character, 1, scale[0], scale[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM4), flag);
2570 PRVM_G_FLOAT(OFS_RETURN) = 1;
2577 float drawstring(vector position, string text, vector scale, vector rgb, float alpha, float flag)
2580 void VM_drawstring(void)
2582 float *pos,*scale,*rgb;
2585 VM_SAFEPARMCOUNT(6,VM_drawstring);
2587 string = PRVM_G_STRING(OFS_PARM1);
2588 pos = PRVM_G_VECTOR(OFS_PARM0);
2589 scale = PRVM_G_VECTOR(OFS_PARM2);
2590 rgb = PRVM_G_VECTOR(OFS_PARM3);
2591 flag = (int)PRVM_G_FLOAT(OFS_PARM5);
2593 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2595 PRVM_G_FLOAT(OFS_RETURN) = -2;
2596 VM_Warning("VM_drawstring: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2600 if(!scale[0] || !scale[1])
2602 PRVM_G_FLOAT(OFS_RETURN) = -3;
2603 VM_Warning("VM_drawstring: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
2607 if(pos[2] || scale[2])
2608 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")));
2610 DrawQ_String (pos[0], pos[1], string, 0, scale[0], scale[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM4), flag);
2611 PRVM_G_FLOAT(OFS_RETURN) = 1;
2617 float drawpic(vector position, string pic, vector size, vector rgb, float alpha, float flag)
2620 void VM_drawpic(void)
2622 const char *picname;
2623 float *size, *pos, *rgb;
2626 VM_SAFEPARMCOUNT(6,VM_drawpic);
2628 picname = PRVM_G_STRING(OFS_PARM1);
2629 VM_CheckEmptyString (picname);
2631 // is pic cached ? no function yet for that
2634 PRVM_G_FLOAT(OFS_RETURN) = -4;
2635 VM_Warning("VM_drawpic: %s: %s not cached !\n", PRVM_NAME, picname);
2639 pos = PRVM_G_VECTOR(OFS_PARM0);
2640 size = PRVM_G_VECTOR(OFS_PARM2);
2641 rgb = PRVM_G_VECTOR(OFS_PARM3);
2642 flag = (int) PRVM_G_FLOAT(OFS_PARM5);
2644 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2646 PRVM_G_FLOAT(OFS_RETURN) = -2;
2647 VM_Warning("VM_drawpic: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2651 if(pos[2] || size[2])
2652 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")));
2654 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);
2655 PRVM_G_FLOAT(OFS_RETURN) = 1;
2662 float drawfill(vector position, vector size, vector rgb, float alpha, float flag)
2665 void VM_drawfill(void)
2667 float *size, *pos, *rgb;
2670 VM_SAFEPARMCOUNT(5,VM_drawfill);
2673 pos = PRVM_G_VECTOR(OFS_PARM0);
2674 size = PRVM_G_VECTOR(OFS_PARM1);
2675 rgb = PRVM_G_VECTOR(OFS_PARM2);
2676 flag = (int) PRVM_G_FLOAT(OFS_PARM4);
2678 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2680 PRVM_G_FLOAT(OFS_RETURN) = -2;
2681 VM_Warning("VM_drawfill: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2685 if(pos[2] || size[2])
2686 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")));
2688 DrawQ_Pic(pos[0], pos[1], NULL, size[0], size[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM3), flag);
2689 PRVM_G_FLOAT(OFS_RETURN) = 1;
2696 drawsetcliparea(float x, float y, float width, float height)
2699 void VM_drawsetcliparea(void)
2702 VM_SAFEPARMCOUNT(4,VM_drawsetcliparea);
2704 x = bound(0, PRVM_G_FLOAT(OFS_PARM0), vid_conwidth.integer);
2705 y = bound(0, PRVM_G_FLOAT(OFS_PARM1), vid_conheight.integer);
2706 w = bound(0, PRVM_G_FLOAT(OFS_PARM2) + PRVM_G_FLOAT(OFS_PARM0) - x, (vid_conwidth.integer - x));
2707 h = bound(0, PRVM_G_FLOAT(OFS_PARM3) + PRVM_G_FLOAT(OFS_PARM1) - y, (vid_conheight.integer - y));
2709 DrawQ_SetClipArea(x, y, w, h);
2714 VM_drawresetcliparea
2719 void VM_drawresetcliparea(void)
2721 VM_SAFEPARMCOUNT(0,VM_drawresetcliparea);
2723 DrawQ_ResetClipArea();
2730 vector getimagesize(string pic)
2733 void VM_getimagesize(void)
2738 VM_SAFEPARMCOUNT(1,VM_getimagesize);
2740 p = PRVM_G_STRING(OFS_PARM0);
2741 VM_CheckEmptyString (p);
2743 pic = Draw_CachePic (p, false);
2745 PRVM_G_VECTOR(OFS_RETURN)[0] = pic->width;
2746 PRVM_G_VECTOR(OFS_RETURN)[1] = pic->height;
2747 PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
2754 string keynumtostring(float keynum)
2757 void VM_keynumtostring (void)
2759 VM_SAFEPARMCOUNT(1, VM_keynumtostring);
2761 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(Key_KeynumToString((int)PRVM_G_FLOAT(OFS_PARM0)));
2768 float stringtokeynum(string key)
2771 void VM_stringtokeynum (void)
2773 VM_SAFEPARMCOUNT( 1, VM_keynumtostring );
2775 PRVM_G_INT(OFS_RETURN) = Key_StringToKeynum(PRVM_G_STRING(OFS_PARM0));
2778 // CL_Video interface functions
2781 ========================
2784 float cin_open(string file, string name)
2785 ========================
2787 void VM_cin_open( void )
2792 VM_SAFEPARMCOUNT( 2, VM_cin_open );
2794 file = PRVM_G_STRING( OFS_PARM0 );
2795 name = PRVM_G_STRING( OFS_PARM1 );
2797 VM_CheckEmptyString( file );
2798 VM_CheckEmptyString( name );
2800 if( CL_OpenVideo( file, name, MENUOWNER ) )
2801 PRVM_G_FLOAT( OFS_RETURN ) = 1;
2803 PRVM_G_FLOAT( OFS_RETURN ) = 0;
2807 ========================
2810 void cin_close(string name)
2811 ========================
2813 void VM_cin_close( void )
2817 VM_SAFEPARMCOUNT( 1, VM_cin_close );
2819 name = PRVM_G_STRING( OFS_PARM0 );
2820 VM_CheckEmptyString( name );
2822 CL_CloseVideo( CL_GetVideoByName( name ) );
2826 ========================
2828 void cin_setstate(string name, float type)
2829 ========================
2831 void VM_cin_setstate( void )
2834 clvideostate_t state;
2837 VM_SAFEPARMCOUNT( 2, VM_cin_netstate );
2839 name = PRVM_G_STRING( OFS_PARM0 );
2840 VM_CheckEmptyString( name );
2842 state = (clvideostate_t)((int)PRVM_G_FLOAT( OFS_PARM1 ));
2844 video = CL_GetVideoByName( name );
2845 if( video && state > CLVIDEO_UNUSED && state < CLVIDEO_STATECOUNT )
2846 CL_SetVideoState( video, state );
2850 ========================
2853 float cin_getstate(string name)
2854 ========================
2856 void VM_cin_getstate( void )
2861 VM_SAFEPARMCOUNT( 1, VM_cin_getstate );
2863 name = PRVM_G_STRING( OFS_PARM0 );
2864 VM_CheckEmptyString( name );
2866 video = CL_GetVideoByName( name );
2868 PRVM_G_FLOAT( OFS_RETURN ) = (int)video->state;
2870 PRVM_G_FLOAT( OFS_RETURN ) = 0;
2874 ========================
2877 void cin_restart(string name)
2878 ========================
2880 void VM_cin_restart( void )
2885 VM_SAFEPARMCOUNT( 1, VM_cin_restart );
2887 name = PRVM_G_STRING( OFS_PARM0 );
2888 VM_CheckEmptyString( name );
2890 video = CL_GetVideoByName( name );
2892 CL_RestartVideo( video );
2899 Writes new values for v_forward, v_up, and v_right based on the given forward vector
2900 vectorvectors(vector, vector)
2903 void VM_vectorvectors (void)
2905 VectorNormalize2(PRVM_G_VECTOR(OFS_PARM0), prog->globals.server->v_forward);
2906 VectorVectors(prog->globals.server->v_forward, prog->globals.server->v_right, prog->globals.server->v_up);
2910 ========================
2913 void drawline(float width, vector pos1, vector pos2, vector rgb, float alpha, float flags)
2914 ========================
2916 void VM_drawline (void)
2918 float *c1, *c2, *rgb;
2920 unsigned char flags;
2922 VM_SAFEPARMCOUNT(6, VM_drawline);
2923 width = PRVM_G_FLOAT(OFS_PARM0);
2924 c1 = PRVM_G_VECTOR(OFS_PARM1);
2925 c2 = PRVM_G_VECTOR(OFS_PARM2);
2926 rgb = PRVM_G_VECTOR(OFS_PARM3);
2927 alpha = PRVM_G_FLOAT(OFS_PARM4);
2928 flags = (int)PRVM_G_FLOAT(OFS_PARM5);
2929 DrawQ_Line(width, c1[0], c1[1], c2[0], c2[1], rgb[0], rgb[1], rgb[2], alpha, flags);
2932 //====================
2933 //QC POLYGON functions
2934 //====================
2939 float data[36]; //[515]: enough for polygons
2940 unsigned char flags; //[515]: + VM_POLYGON_2D and VM_POLYGON_FL4V flags
2943 //static float vm_polygon_linewidth = 1;
2944 static mempool_t *vm_polygons_pool = NULL;
2945 static unsigned char vm_current_vertices = 0;
2946 static qboolean vm_polygons_initialized = false;
2947 static vm_polygon_t *vm_polygons = NULL;
2948 static unsigned long vm_polygons_num = 0, vm_drawpolygons_num = 0; //[515]: ok long on 64bit ?
2949 static qboolean vm_polygonbegin = false; //[515]: for "no-crap-on-the-screen" check
2950 #define VM_DEFPOLYNUM 64 //[515]: enough for default ?
2952 #define VM_POLYGON_FL3V 16 //more than 2 vertices (used only for lines)
2953 #define VM_POLYGON_FLLINES 32
2954 #define VM_POLYGON_FL2D 64
2955 #define VM_POLYGON_FL4V 128 //4 vertices
2957 void VM_InitPolygons (void)
2959 vm_polygons_pool = Mem_AllocPool("VMPOLY", 0, NULL);
2960 vm_polygons = (vm_polygon_t *)Mem_Alloc(vm_polygons_pool, VM_DEFPOLYNUM*sizeof(vm_polygon_t));
2961 memset(vm_polygons, 0, VM_DEFPOLYNUM*sizeof(vm_polygon_t));
2962 vm_polygons_num = VM_DEFPOLYNUM;
2963 vm_drawpolygons_num = 0;
2964 vm_polygonbegin = false;
2965 vm_polygons_initialized = true;
2968 void VM_DrawPolygonCallback (const entity_render_t *ent, const rtlight_t *rtlight, int numsurfaces, int *surfacelist)
2970 int surfacelistindex;
2971 // LordHavoc: FIXME: this is stupid code
2972 for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
2974 const vm_polygon_t *p = &vm_polygons[surfacelist[surfacelistindex]];
2975 int flags = p->flags & 0x0f;
2977 if(flags == DRAWFLAG_ADDITIVE)
2978 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
2979 else if(flags == DRAWFLAG_MODULATE)
2980 GL_BlendFunc(GL_DST_COLOR, GL_ZERO);
2981 else if(flags == DRAWFLAG_2XMODULATE)
2982 GL_BlendFunc(GL_DST_COLOR,GL_SRC_COLOR);
2984 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
2986 R_Mesh_TexBind(0, R_GetTexture(p->tex));
2989 //[515]: is speed is max ?
2990 if(p->flags & VM_POLYGON_FLLINES) //[515]: lines
2992 qglLineWidth(p->data[13]);CHECKGLERROR
2993 qglBegin(GL_LINE_LOOP);
2994 qglTexCoord1f (p->data[12]);
2995 qglColor4f (p->data[20], p->data[21], p->data[22], p->data[23]);
2996 qglVertex3f (p->data[0] , p->data[1], p->data[2]);
2998 qglTexCoord1f (p->data[14]);
2999 qglColor4f (p->data[24], p->data[25], p->data[26], p->data[27]);
3000 qglVertex3f (p->data[3] , p->data[4], p->data[5]);
3002 if(p->flags & VM_POLYGON_FL3V)
3004 qglTexCoord1f (p->data[16]);
3005 qglColor4f (p->data[28], p->data[29], p->data[30], p->data[31]);
3006 qglVertex3f (p->data[6] , p->data[7], p->data[8]);
3008 if(p->flags & VM_POLYGON_FL4V)
3010 qglTexCoord1f (p->data[18]);
3011 qglColor4f (p->data[32], p->data[33], p->data[34], p->data[35]);
3012 qglVertex3f (p->data[9] , p->data[10], p->data[11]);
3020 qglBegin(GL_POLYGON);
3021 qglTexCoord2f (p->data[12], p->data[13]);
3022 qglColor4f (p->data[20], p->data[21], p->data[22], p->data[23]);
3023 qglVertex3f (p->data[0] , p->data[1], p->data[2]);
3025 qglTexCoord2f (p->data[14], p->data[15]);
3026 qglColor4f (p->data[24], p->data[25], p->data[26], p->data[27]);
3027 qglVertex3f (p->data[3] , p->data[4], p->data[5]);
3029 qglTexCoord2f (p->data[16], p->data[17]);
3030 qglColor4f (p->data[28], p->data[29], p->data[30], p->data[31]);
3031 qglVertex3f (p->data[6] , p->data[7], p->data[8]);
3033 if(p->flags & VM_POLYGON_FL4V)
3035 qglTexCoord2f (p->data[18], p->data[19]);
3036 qglColor4f (p->data[32], p->data[33], p->data[34], p->data[35]);
3037 qglVertex3f (p->data[9] , p->data[10], p->data[11]);
3045 void VM_AddPolygonTo2DScene (vm_polygon_t *p)
3047 drawqueuemesh_t mesh;
3048 static int picelements[6] = {0, 1, 2, 0, 2, 3};
3050 mesh.texture = p->tex;
3051 mesh.data_element3i = picelements;
3052 mesh.data_vertex3f = p->data;
3053 mesh.data_texcoord2f = p->data + 12;
3054 mesh.data_color4f = p->data + 20;
3055 if(p->flags & VM_POLYGON_FL4V)
3057 mesh.num_vertices = 4;
3058 mesh.num_triangles = 2;
3062 mesh.num_vertices = 3;
3063 mesh.num_triangles = 1;
3065 if(p->flags & VM_POLYGON_FLLINES) //[515]: lines
3066 DrawQ_LineLoop (&mesh, (p->flags&0x0f));
3068 DrawQ_Mesh (&mesh, (p->flags&0x0f));
3071 //void(string texturename, float flag, float 2d, float lines) R_BeginPolygon
3072 void VM_R_PolygonBegin (void)
3075 const char *picname;
3077 VM_SAFEPARMCOUNT(2, VM_R_PolygonBegin);
3079 if(!vm_polygons_initialized)
3083 VM_Warning("VM_R_PolygonBegin: called twice without VM_R_PolygonEnd after first\n");
3086 if(vm_drawpolygons_num >= vm_polygons_num)
3088 p = (vm_polygon_t *)Mem_Alloc(vm_polygons_pool, 2 * vm_polygons_num * sizeof(vm_polygon_t));
3089 memset(p, 0, 2 * vm_polygons_num * sizeof(vm_polygon_t));
3090 memcpy(p, vm_polygons, vm_polygons_num * sizeof(vm_polygon_t));
3091 Mem_Free(vm_polygons);
3093 vm_polygons_num *= 2;
3095 p = &vm_polygons[vm_drawpolygons_num];
3096 picname = PRVM_G_STRING(OFS_PARM0);
3098 p->tex = Draw_CachePic(picname, true)->tex;
3100 p->tex = r_texture_white;
3101 p->flags = (unsigned char)PRVM_G_FLOAT(OFS_PARM1);
3102 vm_current_vertices = 0;
3103 vm_polygonbegin = true;
3106 if(PRVM_G_FLOAT(OFS_PARM2))
3107 p->flags |= VM_POLYGON_FL2D;
3108 if(prog->argc >= 4 && PRVM_G_FLOAT(OFS_PARM3))
3110 p->data[13] = PRVM_G_FLOAT(OFS_PARM3); //[515]: linewidth
3111 p->flags |= VM_POLYGON_FLLINES;
3116 //void(vector org, vector texcoords, vector rgb, float alpha) R_PolygonVertex
3117 void VM_R_PolygonVertex (void)
3119 float *coords, *tx, *rgb, alpha;
3121 VM_SAFEPARMCOUNT(4, VM_R_PolygonVertex);
3123 if(!vm_polygonbegin)
3125 VM_Warning("VM_R_PolygonVertex: VM_R_PolygonBegin wasn't called\n");
3128 coords = PRVM_G_VECTOR(OFS_PARM0);
3129 tx = PRVM_G_VECTOR(OFS_PARM1);
3130 rgb = PRVM_G_VECTOR(OFS_PARM2);
3131 alpha = PRVM_G_FLOAT(OFS_PARM3);
3133 p = &vm_polygons[vm_drawpolygons_num];
3134 if(vm_current_vertices > 4)
3136 VM_Warning("VM_R_PolygonVertex: may have 4 vertices max\n");
3140 p->data[vm_current_vertices*3] = coords[0];
3141 p->data[1+vm_current_vertices*3] = coords[1];
3142 p->data[2+vm_current_vertices*3] = coords[2];
3144 p->data[12+vm_current_vertices*2] = tx[0];
3145 if(!(p->flags & VM_POLYGON_FLLINES))
3146 p->data[13+vm_current_vertices*2] = tx[1];
3148 p->data[20+vm_current_vertices*4] = rgb[0];
3149 p->data[21+vm_current_vertices*4] = rgb[1];
3150 p->data[22+vm_current_vertices*4] = rgb[2];
3151 p->data[23+vm_current_vertices*4] = alpha;
3153 vm_current_vertices++;
3154 if(vm_current_vertices == 4)
3155 p->flags |= VM_POLYGON_FL4V;
3157 if(vm_current_vertices == 3)
3158 p->flags |= VM_POLYGON_FL3V;
3161 //void() R_EndPolygon
3162 void VM_R_PolygonEnd (void)
3164 if(!vm_polygonbegin)
3166 VM_Warning("VM_R_PolygonEnd: VM_R_PolygonBegin wasn't called\n");
3169 vm_polygonbegin = false;
3170 if(vm_current_vertices > 2 || (vm_current_vertices >= 2 && vm_polygons[vm_drawpolygons_num].flags & VM_POLYGON_FLLINES))
3172 if(vm_polygons[vm_drawpolygons_num].flags & VM_POLYGON_FL2D) //[515]: don't use qcpolygons memory if 2D
3173 VM_AddPolygonTo2DScene(&vm_polygons[vm_drawpolygons_num]);
3175 vm_drawpolygons_num++;
3178 VM_Warning("VM_R_PolygonEnd: %i vertices isn't a good choice\n", vm_current_vertices);
3181 void VM_AddPolygonsToMeshQueue (void)
3184 if(!vm_drawpolygons_num)
3186 R_Mesh_Matrix(&identitymatrix);
3187 GL_CullFace(GL_NONE);
3188 for(i = 0;i < (int)vm_drawpolygons_num;i++)
3189 VM_DrawPolygonCallback(NULL, NULL, 1, &i);
3190 vm_drawpolygons_num = 0;
3193 void Debug_PolygonBegin(const char *picname, int flags, qboolean draw2d, float linewidth)
3197 if(!vm_polygons_initialized)
3201 Con_Printf("Debug_PolygonBegin: called twice without Debug_PolygonEnd after first\n");
3204 // limit polygons to a vaguely sane amount, beyond this each one just
3205 // replaces the last one
3206 vm_drawpolygons_num = min(vm_drawpolygons_num, (1<<20)-1);
3207 if(vm_drawpolygons_num >= vm_polygons_num)
3209 p = (vm_polygon_t *)Mem_Alloc(vm_polygons_pool, 2 * vm_polygons_num * sizeof(vm_polygon_t));
3210 memset(p, 0, 2 * vm_polygons_num * sizeof(vm_polygon_t));
3211 memcpy(p, vm_polygons, vm_polygons_num * sizeof(vm_polygon_t));
3212 Mem_Free(vm_polygons);
3214 vm_polygons_num *= 2;
3216 p = &vm_polygons[vm_drawpolygons_num];
3217 if(picname && picname[0])
3218 p->tex = Draw_CachePic(picname, true)->tex;
3220 p->tex = r_texture_white;
3222 vm_current_vertices = 0;
3223 vm_polygonbegin = true;
3225 p->flags |= VM_POLYGON_FL2D;
3228 p->data[13] = linewidth; //[515]: linewidth
3229 p->flags |= VM_POLYGON_FLLINES;
3233 void Debug_PolygonVertex(float x, float y, float z, float s, float t, float r, float g, float b, float a)
3237 if(!vm_polygonbegin)
3239 Con_Printf("Debug_PolygonVertex: Debug_PolygonBegin wasn't called\n");
3243 p = &vm_polygons[vm_drawpolygons_num];
3244 if(vm_current_vertices > 4)
3246 Con_Printf("Debug_PolygonVertex: may have 4 vertices max\n");
3250 p->data[vm_current_vertices*3] = x;
3251 p->data[1+vm_current_vertices*3] = y;
3252 p->data[2+vm_current_vertices*3] = z;
3254 p->data[12+vm_current_vertices*2] = s;
3255 if(!(p->flags & VM_POLYGON_FLLINES))
3256 p->data[13+vm_current_vertices*2] = t;
3258 p->data[20+vm_current_vertices*4] = r;
3259 p->data[21+vm_current_vertices*4] = g;
3260 p->data[22+vm_current_vertices*4] = b;
3261 p->data[23+vm_current_vertices*4] = a;
3263 vm_current_vertices++;
3264 if(vm_current_vertices == 4)
3265 p->flags |= VM_POLYGON_FL4V;
3267 if(vm_current_vertices == 3)
3268 p->flags |= VM_POLYGON_FL3V;
3271 void Debug_PolygonEnd(void)
3273 if(!vm_polygonbegin)
3275 Con_Printf("Debug_PolygonEnd: Debug_PolygonBegin wasn't called\n");
3278 vm_polygonbegin = false;
3279 if(vm_current_vertices > 2 || (vm_current_vertices >= 2 && vm_polygons[vm_drawpolygons_num].flags & VM_POLYGON_FLLINES))
3281 if(vm_polygons[vm_drawpolygons_num].flags & VM_POLYGON_FL2D) //[515]: don't use qcpolygons memory if 2D
3282 VM_AddPolygonTo2DScene(&vm_polygons[vm_drawpolygons_num]);
3284 vm_drawpolygons_num++;
3287 Con_Printf("Debug_PolygonEnd: %i vertices isn't a good choice\n", vm_current_vertices);
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;
3357 char outstr[VM_STRINGTEMP_LENGTH];
3359 VM_SAFEPARMCOUNT( 1, VM_altstr_prepare );
3361 instr = PRVM_G_STRING( OFS_PARM0 );
3363 for( out = outstr, in = instr, size = sizeof(outstr) - 1 ; size && *in ; size--, in++, out++ )
3372 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
3376 ========================
3379 string altstr_get(string, float)
3380 ========================
3382 void VM_altstr_get( void )
3384 const char *altstr, *pos;
3387 char outstr[VM_STRINGTEMP_LENGTH];
3389 VM_SAFEPARMCOUNT( 2, VM_altstr_get );
3391 altstr = PRVM_G_STRING( OFS_PARM0 );
3393 count = (int)PRVM_G_FLOAT( OFS_PARM1 );
3394 count = count * 2 + 1;
3396 for( pos = altstr ; *pos && count ; pos++ )
3397 if( *pos == '\\' ) {
3400 } else if( *pos == '\'' )
3404 PRVM_G_INT( OFS_RETURN ) = 0;
3408 for( out = outstr, size = sizeof(outstr) - 1 ; size && *pos ; size--, pos++, out++ )
3409 if( *pos == '\\' ) {
3414 } else if( *pos == '\'' )
3420 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
3424 ========================
3427 string altstr_set(string altstr, float num, string set)
3428 ========================
3430 void VM_altstr_set( void )
3433 const char *altstr, *str;
3436 char outstr[VM_STRINGTEMP_LENGTH];
3438 VM_SAFEPARMCOUNT( 3, VM_altstr_set );
3440 altstr = PRVM_G_STRING( OFS_PARM0 );
3442 num = (int)PRVM_G_FLOAT( OFS_PARM1 );
3444 str = PRVM_G_STRING( OFS_PARM2 );
3447 for( num = num * 2 + 1, in = altstr; *in && num; *out++ = *in++ )
3452 } else if( *in == '\'' ) {
3457 for( ; *str; *out++ = *str++ );
3458 // now jump over the old content
3460 if( *in == '\'' || (*in == '\\' && !*++in) )
3463 strlcpy(out, in, outstr + sizeof(outstr) - out);
3464 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
3468 ========================
3471 string altstr_ins(string altstr, float num, string set)
3472 ========================
3474 void VM_altstr_ins(void)
3482 char outstr[VM_STRINGTEMP_LENGTH];
3484 in = instr = PRVM_G_STRING( OFS_PARM0 );
3485 num = (int)PRVM_G_FLOAT( OFS_PARM1 );
3486 set = setstr = PRVM_G_STRING( OFS_PARM2 );
3489 for( num = num * 2 + 2 ; *in && num > 0 ; *out++ = *in++ )
3494 } else if( *in == '\'' ) {
3499 for( ; *set ; *out++ = *set++ );
3502 strlcpy(out, in, outstr + sizeof(outstr) - out);
3503 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
3507 ////////////////////////////////////////
3508 // BufString functions
3509 ////////////////////////////////////////
3510 //[515]: string buffers support
3511 #define MAX_QCSTR_BUFFERS 128
3512 #define MAX_QCSTR_STRINGS 1024
3517 char *strings[MAX_QCSTR_STRINGS];
3520 static qcstrbuffer_t *qcstringbuffers[MAX_QCSTR_BUFFERS];
3521 static int num_qcstringbuffers;
3522 static int buf_sortpower;
3524 #define BUFSTR_BUFFER(a) (a>=MAX_QCSTR_BUFFERS) ? NULL : (qcstringbuffers[a])
3525 #define BUFSTR_ISFREE(a) (a<MAX_QCSTR_BUFFERS&&qcstringbuffers[a]&&qcstringbuffers[a]->num_strings<=0) ? 1 : 0
3527 static int BufStr_FindFreeBuffer (void)
3530 if(num_qcstringbuffers == MAX_QCSTR_BUFFERS)
3532 for(i=0;i<MAX_QCSTR_BUFFERS;i++)
3533 if(!qcstringbuffers[i])
3535 qcstringbuffers[i] = (qcstrbuffer_t *)Z_Malloc(sizeof(qcstrbuffer_t));
3536 memset(qcstringbuffers[i], 0, sizeof(qcstrbuffer_t));
3542 static void BufStr_ClearBuffer (int index)
3544 qcstrbuffer_t *b = qcstringbuffers[index];
3549 if(b->num_strings > 0)
3551 for(i=0;i<b->num_strings;i++)
3553 Z_Free(b->strings[i]);
3554 num_qcstringbuffers--;
3556 Z_Free(qcstringbuffers[index]);
3557 qcstringbuffers[index] = NULL;
3561 static int BufStr_FindFreeString (qcstrbuffer_t *b)
3564 for(i=0;i<b->num_strings;i++)
3565 if(!b->strings[i] || !b->strings[i][0])
3567 if(i == MAX_QCSTR_STRINGS) return -1;
3571 static int BufStr_SortStringsUP (const void *in1, const void *in2)
3574 a = *((const char **) in1);
3575 b = *((const char **) in2);
3577 if(!b[0]) return -1;
3578 return strncmp(a, b, buf_sortpower);
3581 static int BufStr_SortStringsDOWN (const void *in1, const void *in2)
3584 a = *((const char **) in1);
3585 b = *((const char **) in2);
3587 if(!b[0]) return -1;
3588 return strncmp(b, a, buf_sortpower);
3592 static void VM_BufStr_Init (void)
3594 memset(qcstringbuffers, 0, sizeof(qcstringbuffers));
3595 num_qcstringbuffers = 0;
3598 static void VM_BufStr_ShutDown (void)
3601 for(i=0;i<MAX_QCSTR_BUFFERS && num_qcstringbuffers;i++)
3602 BufStr_ClearBuffer(i);
3607 ========================
3609 creates new buffer, and returns it's index, returns -1 if failed
3610 float buf_create(void) = #460;
3611 ========================
3613 void VM_buf_create (void)
3616 VM_SAFEPARMCOUNT(0, VM_buf_create);
3617 i = BufStr_FindFreeBuffer();
3619 num_qcstringbuffers++;
3621 //Con_Printf("VM_buf_create: buffers overflow in %s\n", PRVM_NAME);
3622 PRVM_G_FLOAT(OFS_RETURN) = i;
3626 ========================
3628 deletes buffer and all strings in it
3629 void buf_del(float bufhandle) = #461;
3630 ========================
3632 void VM_buf_del (void)
3634 VM_SAFEPARMCOUNT(1, VM_buf_del);
3635 if(BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0)))
3636 BufStr_ClearBuffer((int)PRVM_G_FLOAT(OFS_PARM0));
3639 VM_Warning("VM_buf_del: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3645 ========================
3647 how many strings are stored in buffer
3648 float buf_getsize(float bufhandle) = #462;
3649 ========================
3651 void VM_buf_getsize (void)
3654 VM_SAFEPARMCOUNT(1, VM_buf_getsize);
3656 b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3659 PRVM_G_FLOAT(OFS_RETURN) = -1;
3660 VM_Warning("VM_buf_getsize: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3664 PRVM_G_FLOAT(OFS_RETURN) = b->num_strings;
3668 ========================
3670 copy all content from one buffer to another, make sure it exists
3671 void buf_copy(float bufhandle_from, float bufhandle_to) = #463;
3672 ========================
3674 void VM_buf_copy (void)
3676 qcstrbuffer_t *b1, *b2;
3678 VM_SAFEPARMCOUNT(2, VM_buf_copy);
3680 b1 = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3683 VM_Warning("VM_buf_copy: invalid source buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3686 i = (int)PRVM_G_FLOAT(OFS_PARM1);
3687 if(i == (int)PRVM_G_FLOAT(OFS_PARM0))
3689 VM_Warning("VM_buf_copy: source == destination (%i) in %s\n", i, PRVM_NAME);
3692 b2 = BUFSTR_BUFFER(i);
3695 VM_Warning("VM_buf_copy: invalid destination buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM1), PRVM_NAME);
3699 BufStr_ClearBuffer(i);
3700 qcstringbuffers[i] = (qcstrbuffer_t *)Z_Malloc(sizeof(qcstrbuffer_t));
3701 memset(qcstringbuffers[i], 0, sizeof(qcstrbuffer_t));
3702 b2->num_strings = b1->num_strings;
3704 for(i=0;i<b1->num_strings;i++)
3705 if(b1->strings[i] && b1->strings[i][0])
3708 stringlen = strlen(b1->strings[i]) + 1;
3709 b2->strings[i] = (char *)Z_Malloc(stringlen);
3712 VM_Warning("VM_buf_copy: not enough memory for buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM1), PRVM_NAME);
3715 memcpy(b2->strings[i], b1->strings[i], stringlen);
3720 ========================
3722 sort buffer by beginnings of strings (sortpower defaults it's lenght)
3723 "backward == TRUE" means that sorting goes upside-down
3724 void buf_sort(float bufhandle, float sortpower, float backward) = #464;
3725 ========================
3727 void VM_buf_sort (void)
3731 VM_SAFEPARMCOUNT(3, VM_buf_sort);
3733 b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3736 VM_Warning("VM_buf_sort: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3739 if(b->num_strings <= 0)
3741 VM_Warning("VM_buf_sort: tried to sort empty buffer %i in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3744 buf_sortpower = (int)PRVM_G_FLOAT(OFS_PARM1);
3745 if(buf_sortpower <= 0)
3746 buf_sortpower = 99999999;
3748 if(!PRVM_G_FLOAT(OFS_PARM2))
3749 qsort(b->strings, b->num_strings, sizeof(char*), BufStr_SortStringsUP);
3751 qsort(b->strings, b->num_strings, sizeof(char*), BufStr_SortStringsDOWN);
3753 for(i=b->num_strings-1;i>=0;i--) //[515]: delete empty lines
3756 if(b->strings[i][0])
3760 Z_Free(b->strings[i]);
3762 b->strings[i] = NULL;
3770 ========================
3772 concantenates all buffer string into one with "glue" separator and returns it as tempstring
3773 string buf_implode(float bufhandle, string glue) = #465;
3774 ========================
3776 void VM_buf_implode (void)
3779 char k[VM_STRINGTEMP_LENGTH];
3783 VM_SAFEPARMCOUNT(2, VM_buf_implode);
3785 b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3786 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
3789 VM_Warning("VM_buf_implode: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3794 sep = PRVM_G_STRING(OFS_PARM1);
3796 for(l=i=0;i<b->num_strings;i++)
3799 l += (i > 0 ? strlen(sep) : 0) + strlen(b->strings[i]);
3800 if (l >= sizeof(k) - 1)
3802 strlcat(k, sep, sizeof(k));
3803 strlcat(k, b->strings[i], sizeof(k));
3805 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(k);
3809 ========================
3811 get a string from buffer, returns tempstring, dont str_unzone it!
3812 string bufstr_get(float bufhandle, float string_index) = #465;
3813 ========================
3815 void VM_bufstr_get (void)
3819 VM_SAFEPARMCOUNT(2, VM_bufstr_get);
3821 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
3822 b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3825 VM_Warning("VM_bufstr_get: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3828 strindex = (int)PRVM_G_FLOAT(OFS_PARM1);
3829 if(strindex < 0 || strindex > MAX_QCSTR_STRINGS)
3831 VM_Warning("VM_bufstr_get: invalid string index %i used in %s\n", strindex, PRVM_NAME);
3834 if(b->num_strings <= strindex)
3836 if(b->strings[strindex])
3837 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(b->strings[strindex]);
3841 ========================
3843 copies a string into selected slot of buffer
3844 void bufstr_set(float bufhandle, float string_index, string str) = #466;
3845 ========================
3847 void VM_bufstr_set (void)
3849 int bufindex, strindex;
3854 VM_SAFEPARMCOUNT(3, VM_bufstr_set);
3856 bufindex = (int)PRVM_G_FLOAT(OFS_PARM0);
3857 b = BUFSTR_BUFFER(bufindex);
3860 VM_Warning("VM_bufstr_set: invalid buffer %i used in %s\n", bufindex, PRVM_NAME);
3863 strindex = (int)PRVM_G_FLOAT(OFS_PARM1);
3864 if(strindex < 0 || strindex > MAX_QCSTR_STRINGS)
3866 VM_Warning("VM_bufstr_set: invalid string index %i used in %s\n", strindex, PRVM_NAME);
3869 news = PRVM_G_STRING(OFS_PARM2);
3870 if(b->strings[strindex])
3871 Z_Free(b->strings[strindex]);
3872 alloclen = strlen(news) + 1;
3873 b->strings[strindex] = (char *)Z_Malloc(alloclen);
3874 memcpy(b->strings[strindex], news, alloclen);
3878 ========================
3880 adds string to buffer in nearest free slot and returns it
3881 "order == TRUE" means that string will be added after last "full" slot
3882 float bufstr_add(float bufhandle, string str, float order) = #467;
3883 ========================
3885 void VM_bufstr_add (void)
3887 int bufindex, order, strindex;
3892 VM_SAFEPARMCOUNT(3, VM_bufstr_add);
3894 bufindex = (int)PRVM_G_FLOAT(OFS_PARM0);
3895 b = BUFSTR_BUFFER(bufindex);
3896 PRVM_G_FLOAT(OFS_RETURN) = -1;
3899 VM_Warning("VM_bufstr_add: invalid buffer %i used in %s\n", bufindex, PRVM_NAME);
3902 string = PRVM_G_STRING(OFS_PARM1);
3903 order = (int)PRVM_G_FLOAT(OFS_PARM2);
3905 strindex = b->num_strings;
3908 strindex = BufStr_FindFreeString(b);
3911 VM_Warning("VM_bufstr_add: buffer %i has no free string slots in %s\n", bufindex, PRVM_NAME);
3916 while(b->num_strings <= strindex)
3918 if(b->num_strings == MAX_QCSTR_STRINGS)
3920 VM_Warning("VM_bufstr_add: buffer %i has no free string slots in %s\n", bufindex, PRVM_NAME);
3923 b->strings[b->num_strings] = NULL;
3926 if(b->strings[strindex])
3927 Z_Free(b->strings[strindex]);
3928 alloclen = strlen(string) + 1;
3929 b->strings[strindex] = (char *)Z_Malloc(alloclen);
3930 memcpy(b->strings[strindex], string, alloclen);
3931 PRVM_G_FLOAT(OFS_RETURN) = strindex;
3935 ========================
3937 delete string from buffer
3938 void bufstr_free(float bufhandle, float string_index) = #468;
3939 ========================
3941 void VM_bufstr_free (void)
3945 VM_SAFEPARMCOUNT(2, VM_bufstr_free);
3947 b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3950 VM_Warning("VM_bufstr_free: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3953 i = (int)PRVM_G_FLOAT(OFS_PARM1);
3954 if(i < 0 || i > MAX_QCSTR_STRINGS)
3956 VM_Warning("VM_bufstr_free: invalid string index %i used in %s\n", i, PRVM_NAME);
3960 Z_Free(b->strings[i]);
3961 b->strings[i] = NULL;
3962 if(i+1 == b->num_strings)
3968 void VM_Cmd_Init(void)
3970 // only init the stuff for the current prog
3973 // VM_BufStr_Init();
3974 if(vm_polygons_initialized)
3976 Mem_FreePool(&vm_polygons_pool);
3977 vm_polygons_initialized = false;
3981 void VM_Cmd_Reset(void)
3983 CL_PurgeOwner( MENUOWNER );
3985 VM_Files_CloseAll();
3986 // VM_BufStr_ShutDown();
3987 if(vm_polygons_initialized)
3989 Mem_FreePool(&vm_polygons_pool);
3990 vm_polygons_initialized = false;