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
10 // LordHavoc: changed this to NOT use a return statement, so that it can be used in functions that must return a value
11 void VM_Warning(const char *fmt, ...)
14 char msg[MAX_INPUTLINE];
17 dpvsnprintf(msg,sizeof(msg),fmt,argptr);
21 // TODO: either add a cvar/cmd to control the state dumping or replace some of the calls with Con_Printf [9/13/2006 Black]
26 //============================================================================
29 // TODO DONE: move vm_files and vm_fssearchlist to prvm_prog_t struct
30 // TODO: move vm_files and vm_fssearchlist back [9/13/2006 Black]
31 // TODO: (move vm_files and vm_fssearchlist to prvm_prog_t struct again) [2007-01-23 LordHavoc]
32 // TODO: will this war ever end? [2007-01-23 LordHavoc]
34 void VM_CheckEmptyString (const char *s)
37 PRVM_ERROR ("%s: Bad string", PRVM_NAME);
40 //============================================================================
43 void VM_VarString(int first, char *out, int outlength)
49 outend = out + outlength - 1;
50 for (i = first;i < prog->argc && out < outend;i++)
52 s = PRVM_G_STRING((OFS_PARM0+i*3));
53 while (out < outend && *s)
63 returns true if the extension is supported by the server
65 checkextension(extensionname)
69 // kind of helper function
70 static qboolean checkextension(const char *name)
74 len = (int)strlen(name);
76 for (e = prog->extensionstring;*e;e++)
83 while (*e && *e != ' ')
85 if ((e - start) == len && !strncasecmp(start, name, len))
91 void VM_checkextension (void)
93 VM_SAFEPARMCOUNT(1,VM_checkextension);
95 PRVM_G_FLOAT(OFS_RETURN) = checkextension(PRVM_G_STRING(OFS_PARM0));
102 This is a TERMINAL error, which will kill off the entire prog.
111 char string[VM_STRINGTEMP_LENGTH];
113 VM_VarString(0, string, sizeof(string));
114 Con_Printf("======%s ERROR in %s:\n%s\n", PRVM_NAME, PRVM_GetString(prog->xfunction->s_name), string);
117 ed = PRVM_G_EDICT(prog->self->ofs);
121 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);
128 Dumps out self, then an error message. The program is aborted and self is
129 removed, but the level can continue.
134 void VM_objerror (void)
137 char string[VM_STRINGTEMP_LENGTH];
139 VM_VarString(0, string, sizeof(string));
140 Con_Printf("======OBJECT ERROR======\n"); // , PRVM_NAME, PRVM_GetString(prog->xfunction->s_name), string); // or include them? FIXME
143 ed = PRVM_G_EDICT (prog->self->ofs);
149 // objerror has to display the object fields -> else call
150 PRVM_ERROR ("VM_objecterror: self not defined !");
151 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 char string[VM_STRINGTEMP_LENGTH];
167 VM_VarString(0, string, sizeof(string));
175 broadcast print to everyone on server
180 void VM_bprint (void)
182 char string[VM_STRINGTEMP_LENGTH];
186 VM_Warning("VM_bprint: game is not server(%s) !\n", PRVM_NAME);
190 VM_VarString(0, string, sizeof(string));
191 SV_BroadcastPrint(string);
196 VM_sprint (menu & client but only if server.active == true)
198 single print to a specific client
200 sprint(float clientnum,...[string])
203 void VM_sprint (void)
207 char string[VM_STRINGTEMP_LENGTH];
209 //find client for this entity
210 clientnum = (int)PRVM_G_FLOAT(OFS_PARM0);
211 if (!sv.active || clientnum < 0 || clientnum >= svs.maxclients || !svs.clients[clientnum].active)
213 VM_Warning("VM_sprint: %s: invalid client or server is not active !\n", PRVM_NAME);
217 client = svs.clients + clientnum;
218 if (!client->netconnection)
221 VM_VarString(1, string, sizeof(string));
222 MSG_WriteChar(&client->netconnection->message,svc_print);
223 MSG_WriteString(&client->netconnection->message, string);
230 single print to the screen
232 centerprint(clientent, value)
235 void VM_centerprint (void)
237 char string[VM_STRINGTEMP_LENGTH];
239 VM_VarString(0, string, sizeof(string));
240 SCR_CenterPrint(string);
247 vector normalize(vector)
250 void VM_normalize (void)
256 VM_SAFEPARMCOUNT(1,VM_normalize);
258 value1 = PRVM_G_VECTOR(OFS_PARM0);
260 f = VectorLength2(value1);
264 VectorScale(value1, f, newvalue);
267 VectorClear(newvalue);
269 VectorCopy (newvalue, PRVM_G_VECTOR(OFS_RETURN));
281 VM_SAFEPARMCOUNT(1,VM_vlen);
282 PRVM_G_FLOAT(OFS_RETURN) = VectorLength(PRVM_G_VECTOR(OFS_PARM0));
289 float vectoyaw(vector)
292 void VM_vectoyaw (void)
297 VM_SAFEPARMCOUNT(1,VM_vectoyaw);
299 value1 = PRVM_G_VECTOR(OFS_PARM0);
301 if (value1[1] == 0 && value1[0] == 0)
305 yaw = (int) (atan2(value1[1], value1[0]) * 180 / M_PI);
310 PRVM_G_FLOAT(OFS_RETURN) = yaw;
318 vector vectoangles(vector)
321 void VM_vectoangles (void)
327 VM_SAFEPARMCOUNT(1,VM_vectoangles);
329 value1 = PRVM_G_VECTOR(OFS_PARM0);
331 if (value1[1] == 0 && value1[0] == 0)
341 // LordHavoc: optimized a bit
344 yaw = (atan2(value1[1], value1[0]) * 180 / M_PI);
348 else if (value1[1] > 0)
353 forward = sqrt(value1[0]*value1[0] + value1[1]*value1[1]);
354 pitch = (atan2(value1[2], forward) * 180 / M_PI);
359 PRVM_G_FLOAT(OFS_RETURN+0) = pitch;
360 PRVM_G_FLOAT(OFS_RETURN+1) = yaw;
361 PRVM_G_FLOAT(OFS_RETURN+2) = 0;
368 Returns a number from 0<= num < 1
373 void VM_random (void)
375 VM_SAFEPARMCOUNT(0,VM_random);
377 PRVM_G_FLOAT(OFS_RETURN) = lhrandom(0, 1);
384 Each entity can have eight independant sound sources, like voice,
387 Channel 0 is an auto-allocate channel, the others override anything
388 already running on that entity/channel pair.
390 An attenuation of 0 will play full volume everywhere in the level.
391 Larger attenuations will drop off.
400 prvm_edict_t *entity;
404 entity = PRVM_G_EDICT(OFS_PARM0);
405 channel = PRVM_G_FLOAT(OFS_PARM1);
406 sample = PRVM_G_STRING(OFS_PARM2);
407 volume = PRVM_G_FLOAT(OFS_PARM3) * 255;
408 attenuation = PRVM_G_FLOAT(OFS_PARM4);
410 if (volume < 0 || volume > 255)
411 Host_Error ("SV_StartSound: volume = %i", volume);
413 if (attenuation < 0 || attenuation > 4)
414 Host_Error ("SV_StartSound: attenuation = %f", attenuation);
416 if (channel < 0 || channel > 7)
417 Host_Error ("SV_StartSound: channel = %i", channel);
419 SV_StartSound (entity, channel, sample, volume, attenuation);
427 localsound(string sample)
430 void VM_localsound(void)
434 VM_SAFEPARMCOUNT(1,VM_localsound);
436 s = PRVM_G_STRING(OFS_PARM0);
438 if(!S_LocalSound (s))
440 PRVM_G_FLOAT(OFS_RETURN) = -4;
441 VM_Warning("VM_localsound: Failed to play %s for %s !\n", s, PRVM_NAME);
445 PRVM_G_FLOAT(OFS_RETURN) = 1;
457 PRVM_ERROR ("%s: break statement", PRVM_NAME);
460 //============================================================================
466 Sends text over to the client's execution buffer
468 [localcmd (string, ...) or]
472 void VM_localcmd (void)
474 char string[VM_STRINGTEMP_LENGTH];
475 VM_VarString(0, string, sizeof(string));
476 Cbuf_AddText(string);
488 VM_SAFEPARMCOUNT(1,VM_cvar);
490 PRVM_G_FLOAT(OFS_RETURN) = Cvar_VariableValue(PRVM_G_STRING(OFS_PARM0));
497 const string VM_cvar_string (string)
500 void VM_cvar_string(void)
503 VM_SAFEPARMCOUNT(1,VM_cvar_string);
505 name = PRVM_G_STRING(OFS_PARM0);
507 VM_CheckEmptyString(name);
509 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(Cvar_VariableString(name));
514 ========================
517 const string VM_cvar_defstring (string)
518 ========================
520 void VM_cvar_defstring (void)
523 VM_SAFEPARMCOUNT(1,VM_cvar_string);
525 name = PRVM_G_STRING(OFS_PARM0);
527 VM_CheckEmptyString(name);
529 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(Cvar_VariableDefString(name));
535 void cvar_set (string,string)
538 void VM_cvar_set (void)
540 VM_SAFEPARMCOUNT(2,VM_cvar_set);
542 Cvar_Set(PRVM_G_STRING(OFS_PARM0), PRVM_G_STRING(OFS_PARM1));
552 void VM_dprint (void)
554 char string[VM_STRINGTEMP_LENGTH];
555 if (developer.integer)
557 VM_VarString(0, string, sizeof(string));
559 Con_Printf("%s", string);
561 Con_Printf("%s: %s", PRVM_NAME, string);
579 VM_SAFEPARMCOUNT(1, VM_ftos);
581 v = PRVM_G_FLOAT(OFS_PARM0);
583 if ((float)((int)v) == v)
584 sprintf(s, "%i", (int)v);
587 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(s);
602 VM_SAFEPARMCOUNT(1,VM_fabs);
604 v = PRVM_G_FLOAT(OFS_PARM0);
605 PRVM_G_FLOAT(OFS_RETURN) = fabs(v);
620 VM_SAFEPARMCOUNT(1,VM_vtos);
622 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]);
623 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(s);
638 VM_SAFEPARMCOUNT(1, VM_etos);
640 sprintf (s, "entity %i", PRVM_G_EDICTNUM(OFS_PARM0));
641 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(s);
648 float stof(...[string])
653 char string[VM_STRINGTEMP_LENGTH];
654 VM_VarString(0, string, sizeof(string));
655 PRVM_G_FLOAT(OFS_RETURN) = atof(string);
659 ========================
663 ========================
667 VM_SAFEPARMCOUNT(1, VM_itof);
668 PRVM_G_FLOAT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
672 ========================
675 entity ftoe(float num)
676 ========================
681 VM_SAFEPARMCOUNT(1, VM_ftoe);
683 ent = (int)PRVM_G_FLOAT(OFS_PARM0);
684 if (ent < 0 || ent >= MAX_EDICTS || PRVM_PROG_TO_EDICT(ent)->priv.required->free)
685 ent = 0; // return world instead of a free or invalid entity
687 PRVM_G_INT(OFS_RETURN) = ent;
694 string strftime(float uselocaltime, string[, string ...])
697 void VM_strftime(void)
701 char fmt[VM_STRINGTEMP_LENGTH];
702 char result[VM_STRINGTEMP_LENGTH];
703 VM_VarString(0, fmt, sizeof(fmt));
705 if (PRVM_G_FLOAT(OFS_PARM0))
711 PRVM_G_FLOAT(OFS_RETURN) = 0;
714 strftime(result, sizeof(result), fmt, tm);
715 PRVM_G_FLOAT(OFS_RETURN) = PRVM_SetTempString(result);
729 prog->xfunction->builtinsprofile += 20;
730 ed = PRVM_ED_Alloc();
742 void VM_remove (void)
745 prog->xfunction->builtinsprofile += 20;
747 VM_SAFEPARMCOUNT(1, VM_remove);
749 ed = PRVM_G_EDICT(OFS_PARM0);
750 if( PRVM_NUM_FOR_EDICT(ed) <= prog->reserved_edicts )
752 if (developer.integer >= 1)
753 VM_Warning( "VM_remove: tried to remove the null entity or a reserved entity!\n" );
755 else if( ed->priv.required->free )
757 if (developer.integer >= 1)
758 VM_Warning( "VM_remove: tried to remove an already freed entity!\n" );
762 // if (ed == prog->edicts)
763 // PRVM_ERROR ("remove: tried to remove world");
764 // if (PRVM_NUM_FOR_EDICT(ed) <= sv.maxclients)
765 // Host_Error("remove: tried to remove a client");
772 entity find(entity start, .string field, string match)
783 VM_SAFEPARMCOUNT(3,VM_find);
785 e = PRVM_G_EDICTNUM(OFS_PARM0);
786 f = PRVM_G_INT(OFS_PARM1);
787 s = PRVM_G_STRING(OFS_PARM2);
789 // LordHavoc: apparently BloodMage does a find(world, weaponmodel, "") and
790 // expects it to find all the monsters, so we must be careful to support
793 for (e++ ; e < prog->num_edicts ; e++)
795 prog->xfunction->builtinsprofile++;
796 ed = PRVM_EDICT_NUM(e);
797 if (ed->priv.required->free)
799 t = PRVM_E_STRING(ed,f);
809 VM_RETURN_EDICT(prog->edicts);
816 entity findfloat(entity start, .float field, float match)
817 entity findentity(entity start, .entity field, entity match)
820 // LordHavoc: added this for searching float, int, and entity reference fields
821 void VM_findfloat (void)
828 VM_SAFEPARMCOUNT(3,VM_findfloat);
830 e = PRVM_G_EDICTNUM(OFS_PARM0);
831 f = PRVM_G_INT(OFS_PARM1);
832 s = PRVM_G_FLOAT(OFS_PARM2);
834 for (e++ ; e < prog->num_edicts ; e++)
836 prog->xfunction->builtinsprofile++;
837 ed = PRVM_EDICT_NUM(e);
838 if (ed->priv.required->free)
840 if (PRVM_E_FLOAT(ed,f) == s)
847 VM_RETURN_EDICT(prog->edicts);
854 entity findchain(.string field, string match)
857 // chained search for strings in entity fields
858 // entity(.string field, string match) findchain = #402;
859 void VM_findchain (void)
865 prvm_edict_t *ent, *chain;
867 VM_SAFEPARMCOUNT(2,VM_findchain);
869 // is the same like !(prog->flag & PRVM_FE_CHAIN) - even if the operator precedence is another
870 if(!prog->flag & PRVM_FE_CHAIN)
871 PRVM_ERROR("VM_findchain: %s doesnt have a chain field !", PRVM_NAME);
873 chain_of = PRVM_ED_FindField("chain")->ofs;
875 chain = prog->edicts;
877 f = PRVM_G_INT(OFS_PARM0);
878 s = PRVM_G_STRING(OFS_PARM1);
880 // LordHavoc: apparently BloodMage does a find(world, weaponmodel, "") and
881 // expects it to find all the monsters, so we must be careful to support
884 ent = PRVM_NEXT_EDICT(prog->edicts);
885 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
887 prog->xfunction->builtinsprofile++;
888 if (ent->priv.required->free)
890 t = PRVM_E_STRING(ent,f);
896 PRVM_E_INT(ent,chain_of) = PRVM_NUM_FOR_EDICT(chain);
900 VM_RETURN_EDICT(chain);
907 entity findchainfloat(.string field, float match)
908 entity findchainentity(.string field, entity match)
911 // LordHavoc: chained search for float, int, and entity reference fields
912 // entity(.string field, float match) findchainfloat = #403;
913 void VM_findchainfloat (void)
919 prvm_edict_t *ent, *chain;
921 VM_SAFEPARMCOUNT(2, VM_findchainfloat);
923 if(!prog->flag & PRVM_FE_CHAIN)
924 PRVM_ERROR("VM_findchainfloat: %s doesnt have a chain field !", PRVM_NAME);
926 chain_of = PRVM_ED_FindField("chain")->ofs;
928 chain = (prvm_edict_t *)prog->edicts;
930 f = PRVM_G_INT(OFS_PARM0);
931 s = PRVM_G_FLOAT(OFS_PARM1);
933 ent = PRVM_NEXT_EDICT(prog->edicts);
934 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
936 prog->xfunction->builtinsprofile++;
937 if (ent->priv.required->free)
939 if (PRVM_E_FLOAT(ent,f) != s)
942 PRVM_E_INT(ent,chain_of) = PRVM_EDICT_TO_PROG(chain);
946 VM_RETURN_EDICT(chain);
950 ========================
953 entity findflags(entity start, .float field, float match)
954 ========================
956 // LordHavoc: search for flags in float fields
957 void VM_findflags (void)
964 VM_SAFEPARMCOUNT(3, VM_findflags);
967 e = PRVM_G_EDICTNUM(OFS_PARM0);
968 f = PRVM_G_INT(OFS_PARM1);
969 s = (int)PRVM_G_FLOAT(OFS_PARM2);
971 for (e++ ; e < prog->num_edicts ; e++)
973 prog->xfunction->builtinsprofile++;
974 ed = PRVM_EDICT_NUM(e);
975 if (ed->priv.required->free)
977 if (!PRVM_E_FLOAT(ed,f))
979 if ((int)PRVM_E_FLOAT(ed,f) & s)
986 VM_RETURN_EDICT(prog->edicts);
990 ========================
993 entity findchainflags(.float field, float match)
994 ========================
996 // LordHavoc: chained search for flags in float fields
997 void VM_findchainflags (void)
1003 prvm_edict_t *ent, *chain;
1005 VM_SAFEPARMCOUNT(2, VM_findchainflags);
1007 if(!prog->flag & PRVM_FE_CHAIN)
1008 PRVM_ERROR("VM_findchainflags: %s doesnt have a chain field !", PRVM_NAME);
1010 chain_of = PRVM_ED_FindField("chain")->ofs;
1012 chain = (prvm_edict_t *)prog->edicts;
1014 f = PRVM_G_INT(OFS_PARM0);
1015 s = (int)PRVM_G_FLOAT(OFS_PARM1);
1017 ent = PRVM_NEXT_EDICT(prog->edicts);
1018 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
1020 prog->xfunction->builtinsprofile++;
1021 if (ent->priv.required->free)
1023 if (!PRVM_E_FLOAT(ent,f))
1025 if (!((int)PRVM_E_FLOAT(ent,f) & s))
1028 PRVM_E_INT(ent,chain_of) = PRVM_EDICT_TO_PROG(chain);
1032 VM_RETURN_EDICT(chain);
1042 void VM_coredump (void)
1044 VM_SAFEPARMCOUNT(0,VM_coredump);
1046 Cbuf_AddText("prvm_edicts ");
1047 Cbuf_AddText(PRVM_NAME);
1058 void PRVM_StackTrace(void);
1059 void VM_stackdump (void)
1061 VM_SAFEPARMCOUNT(0, VM_stackdump);
1076 VM_SAFEPARMCOUNT(0, VM_crash);
1078 PRVM_ERROR("Crash called by %s",PRVM_NAME);
1088 void VM_traceon (void)
1090 VM_SAFEPARMCOUNT(0,VM_traceon);
1102 void VM_traceoff (void)
1104 VM_SAFEPARMCOUNT(0,VM_traceoff);
1106 prog->trace = false;
1116 void VM_eprint (void)
1118 VM_SAFEPARMCOUNT(1,VM_eprint);
1120 PRVM_ED_PrintNum (PRVM_G_EDICTNUM(OFS_PARM0));
1133 VM_SAFEPARMCOUNT(1,VM_rint);
1135 f = PRVM_G_FLOAT(OFS_PARM0);
1137 PRVM_G_FLOAT(OFS_RETURN) = floor(f + 0.5);
1139 PRVM_G_FLOAT(OFS_RETURN) = ceil(f - 0.5);
1149 void VM_floor (void)
1151 VM_SAFEPARMCOUNT(1,VM_floor);
1153 PRVM_G_FLOAT(OFS_RETURN) = floor(PRVM_G_FLOAT(OFS_PARM0));
1165 VM_SAFEPARMCOUNT(1,VM_ceil);
1167 PRVM_G_FLOAT(OFS_RETURN) = ceil(PRVM_G_FLOAT(OFS_PARM0));
1175 entity nextent(entity)
1178 void VM_nextent (void)
1183 i = PRVM_G_EDICTNUM(OFS_PARM0);
1186 prog->xfunction->builtinsprofile++;
1188 if (i == prog->num_edicts)
1190 VM_RETURN_EDICT(prog->edicts);
1193 ent = PRVM_EDICT_NUM(i);
1194 if (!ent->priv.required->free)
1196 VM_RETURN_EDICT(ent);
1202 //=============================================================================
1209 changelevel(string map)
1212 void VM_changelevel (void)
1214 VM_SAFEPARMCOUNT(1, VM_changelevel);
1218 VM_Warning("VM_changelevel: game is not server (%s)\n", PRVM_NAME);
1222 // make sure we don't issue two changelevels
1223 if (svs.changelevel_issued)
1225 svs.changelevel_issued = true;
1227 Cbuf_AddText (va("changelevel %s\n",PRVM_G_STRING(OFS_PARM0)));
1239 VM_SAFEPARMCOUNT(1,VM_sin);
1240 PRVM_G_FLOAT(OFS_RETURN) = sin(PRVM_G_FLOAT(OFS_PARM0));
1251 VM_SAFEPARMCOUNT(1,VM_cos);
1252 PRVM_G_FLOAT(OFS_RETURN) = cos(PRVM_G_FLOAT(OFS_PARM0));
1264 VM_SAFEPARMCOUNT(1,VM_sqrt);
1265 PRVM_G_FLOAT(OFS_RETURN) = sqrt(PRVM_G_FLOAT(OFS_PARM0));
1277 VM_SAFEPARMCOUNT(1,VM_asin);
1278 PRVM_G_FLOAT(OFS_RETURN) = asin(PRVM_G_FLOAT(OFS_PARM0));
1289 VM_SAFEPARMCOUNT(1,VM_acos);
1290 PRVM_G_FLOAT(OFS_RETURN) = acos(PRVM_G_FLOAT(OFS_PARM0));
1301 VM_SAFEPARMCOUNT(1,VM_atan);
1302 PRVM_G_FLOAT(OFS_RETURN) = atan(PRVM_G_FLOAT(OFS_PARM0));
1308 float atan2(float,float)
1311 void VM_atan2 (void)
1313 VM_SAFEPARMCOUNT(2,VM_atan2);
1314 PRVM_G_FLOAT(OFS_RETURN) = atan2(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1325 VM_SAFEPARMCOUNT(1,VM_tan);
1326 PRVM_G_FLOAT(OFS_RETURN) = tan(PRVM_G_FLOAT(OFS_PARM0));
1333 Returns a vector of length < 1 and > 0
1338 void VM_randomvec (void)
1343 VM_SAFEPARMCOUNT(0, VM_randomvec);
1348 temp[0] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1349 temp[1] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1350 temp[2] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1352 while (DotProduct(temp, temp) >= 1);
1353 VectorCopy (temp, PRVM_G_VECTOR(OFS_RETURN));
1356 temp[0] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1357 temp[1] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1358 temp[2] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1359 // length returned always > 0
1360 length = (rand()&32766 + 1) * (1.0 / 32767.0) / VectorLength(temp);
1361 VectorScale(temp,length, temp);*/
1362 //VectorCopy(temp, PRVM_G_VECTOR(OFS_RETURN));
1365 //=============================================================================
1371 float registercvar (string name, string value, float flags)
1374 void VM_registercvar (void)
1376 const char *name, *value;
1379 VM_SAFEPARMCOUNT(3,VM_registercvar);
1381 name = PRVM_G_STRING(OFS_PARM0);
1382 value = PRVM_G_STRING(OFS_PARM1);
1383 flags = (int)PRVM_G_FLOAT(OFS_PARM2);
1384 PRVM_G_FLOAT(OFS_RETURN) = 0;
1386 if(flags > CVAR_MAXFLAGSVAL)
1389 // first check to see if it has already been defined
1390 if (Cvar_FindVar (name))
1393 // check for overlap with a command
1394 if (Cmd_Exists (name))
1396 VM_Warning("VM_registercvar: %s is a command\n", name);
1400 Cvar_Get(name, value, flags);
1402 PRVM_G_FLOAT(OFS_RETURN) = 1; // success
1409 returns the minimum of two supplied floats
1411 float min(float a, float b, ...[float])
1416 // LordHavoc: 3+ argument enhancement suggested by FrikaC
1417 if (prog->argc == 2)
1418 PRVM_G_FLOAT(OFS_RETURN) = min(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1419 else if (prog->argc >= 3)
1422 float f = PRVM_G_FLOAT(OFS_PARM0);
1423 for (i = 1;i < prog->argc;i++)
1424 if (PRVM_G_FLOAT((OFS_PARM0+i*3)) < f)
1425 f = PRVM_G_FLOAT((OFS_PARM0+i*3));
1426 PRVM_G_FLOAT(OFS_RETURN) = f;
1429 PRVM_ERROR("VM_min: %s must supply at least 2 floats", PRVM_NAME);
1436 returns the maximum of two supplied floats
1438 float max(float a, float b, ...[float])
1443 // LordHavoc: 3+ argument enhancement suggested by FrikaC
1444 if (prog->argc == 2)
1445 PRVM_G_FLOAT(OFS_RETURN) = max(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1446 else if (prog->argc >= 3)
1449 float f = PRVM_G_FLOAT(OFS_PARM0);
1450 for (i = 1;i < prog->argc;i++)
1451 if (PRVM_G_FLOAT((OFS_PARM0+i*3)) > f)
1452 f = PRVM_G_FLOAT((OFS_PARM0+i*3));
1453 PRVM_G_FLOAT(OFS_RETURN) = f;
1456 PRVM_ERROR("VM_max: %s must supply at least 2 floats", PRVM_NAME);
1463 returns number bounded by supplied range
1465 float bound(float min, float value, float max)
1468 void VM_bound (void)
1470 VM_SAFEPARMCOUNT(3,VM_bound);
1471 PRVM_G_FLOAT(OFS_RETURN) = bound(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1), PRVM_G_FLOAT(OFS_PARM2));
1478 returns a raised to power b
1480 float pow(float a, float b)
1485 VM_SAFEPARMCOUNT(2,VM_pow);
1486 PRVM_G_FLOAT(OFS_RETURN) = pow(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1493 copies data from one entity to another
1495 copyentity(entity src, entity dst)
1498 void VM_copyentity (void)
1500 prvm_edict_t *in, *out;
1501 VM_SAFEPARMCOUNT(2,VM_copyentity);
1502 in = PRVM_G_EDICT(OFS_PARM0);
1503 out = PRVM_G_EDICT(OFS_PARM1);
1504 memcpy(out->fields.vp, in->fields.vp, prog->progs->entityfields * 4);
1511 sets the color of a client and broadcasts the update to all connected clients
1513 setcolor(clientent, value)
1516 /*void PF_setcolor (void)
1522 entnum = PRVM_G_EDICTNUM(OFS_PARM0);
1523 i = PRVM_G_FLOAT(OFS_PARM1);
1525 if (entnum < 1 || entnum > svs.maxclients || !svs.clients[entnum-1].active)
1527 Con_Print("tried to setcolor a non-client\n");
1531 client = svs.clients + entnum-1;
1532 if ((val = PRVM_GETEDICTFIELDVALUE(client->edict, eval_clientcolors)))
1535 client->old_colors = i;
1536 client->edict->fields.server->team = (i & 15) + 1;
1538 MSG_WriteByte (&sv.reliable_datagram, svc_updatecolors);
1539 MSG_WriteByte (&sv.reliable_datagram, entnum - 1);
1540 MSG_WriteByte (&sv.reliable_datagram, i);
1543 void VM_Files_Init(void)
1546 for (i = 0;i < PRVM_MAX_OPENFILES;i++)
1547 prog->openfiles[i] = NULL;
1550 void VM_Files_CloseAll(void)
1553 for (i = 0;i < PRVM_MAX_OPENFILES;i++)
1555 if (prog->openfiles[i])
1556 FS_Close(prog->openfiles[i]);
1557 prog->openfiles[i] = NULL;
1561 qfile_t *VM_GetFileHandle( int index )
1563 if (index < 0 || index >= PRVM_MAX_OPENFILES)
1565 Con_Printf("VM_GetFileHandle: invalid file handle %i used in %s\n", index, PRVM_NAME);
1568 if (prog->openfiles[index] == NULL)
1570 Con_Printf("VM_GetFileHandle: no such file handle %i (or file has been closed) in %s\n", index, PRVM_NAME);
1573 return prog->openfiles[index];
1580 float fopen(string filename, float mode)
1583 // float(string filename, float mode) fopen = #110;
1584 // opens a file inside quake/gamedir/data/ (mode is FILE_READ, FILE_APPEND, or FILE_WRITE),
1585 // returns fhandle >= 0 if successful, or fhandle < 0 if unable to open file for any reason
1589 const char *modestring, *filename;
1591 VM_SAFEPARMCOUNT(2,VM_fopen);
1593 for (filenum = 0;filenum < PRVM_MAX_OPENFILES;filenum++)
1594 if (prog->openfiles[filenum] == NULL)
1596 if (filenum >= PRVM_MAX_OPENFILES)
1598 PRVM_G_FLOAT(OFS_RETURN) = -2;
1599 VM_Warning("VM_fopen: %s ran out of file handles (%i)\n", PRVM_NAME, PRVM_MAX_OPENFILES);
1602 mode = (int)PRVM_G_FLOAT(OFS_PARM1);
1605 case 0: // FILE_READ
1608 case 1: // FILE_APPEND
1611 case 2: // FILE_WRITE
1615 PRVM_G_FLOAT(OFS_RETURN) = -3;
1616 VM_Warning("VM_fopen: %s: no such mode %i (valid: 0 = read, 1 = append, 2 = write)\n", PRVM_NAME, mode);
1619 filename = PRVM_G_STRING(OFS_PARM0);
1621 prog->openfiles[filenum] = FS_Open(va("data/%s", filename), modestring, false, false);
1622 if (prog->openfiles[filenum] == NULL && mode == 0)
1623 prog->openfiles[filenum] = FS_Open(va("%s", filename), modestring, false, false);
1625 if (prog->openfiles[filenum] == NULL)
1627 PRVM_G_FLOAT(OFS_RETURN) = -1;
1628 if (developer.integer >= 100)
1629 VM_Warning("VM_fopen: %s: %s mode %s failed\n", PRVM_NAME, filename, modestring);
1633 PRVM_G_FLOAT(OFS_RETURN) = filenum;
1634 if (developer.integer >= 100)
1635 Con_Printf("VM_fopen: %s: %s mode %s opened as #%i\n", PRVM_NAME, filename, modestring, filenum);
1643 fclose(float fhandle)
1646 //void(float fhandle) fclose = #111; // closes a file
1647 void VM_fclose(void)
1651 VM_SAFEPARMCOUNT(1,VM_fclose);
1653 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1654 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
1656 VM_Warning("VM_fclose: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1659 if (prog->openfiles[filenum] == NULL)
1661 VM_Warning("VM_fclose: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1664 FS_Close(prog->openfiles[filenum]);
1665 prog->openfiles[filenum] = NULL;
1666 if (developer.integer >= 100)
1667 Con_Printf("VM_fclose: %s: #%i closed\n", PRVM_NAME, filenum);
1674 string fgets(float fhandle)
1677 //string(float fhandle) fgets = #112; // reads a line of text from the file and returns as a tempstring
1681 char string[VM_STRINGTEMP_LENGTH];
1684 VM_SAFEPARMCOUNT(1,VM_fgets);
1686 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1687 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
1689 VM_Warning("VM_fgets: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1692 if (prog->openfiles[filenum] == NULL)
1694 VM_Warning("VM_fgets: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1700 c = FS_Getc(prog->openfiles[filenum]);
1701 if (c == '\r' || c == '\n' || c < 0)
1703 if (end < VM_STRINGTEMP_LENGTH - 1)
1707 // remove \n following \r
1710 c = FS_Getc(prog->openfiles[filenum]);
1712 FS_UnGetc(prog->openfiles[filenum], (unsigned char)c);
1714 if (developer.integer >= 100)
1715 Con_Printf("fgets: %s: %s\n", PRVM_NAME, string);
1717 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string);
1719 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
1726 fputs(float fhandle, string s)
1729 //void(float fhandle, string s) fputs = #113; // writes a line of text to the end of the file
1733 char string[VM_STRINGTEMP_LENGTH];
1736 VM_SAFEPARMCOUNT(2,VM_fputs);
1738 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1739 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
1741 VM_Warning("VM_fputs: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1744 if (prog->openfiles[filenum] == NULL)
1746 VM_Warning("VM_fputs: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1749 VM_VarString(1, string, sizeof(string));
1750 if ((stringlength = (int)strlen(string)))
1751 FS_Write(prog->openfiles[filenum], string, stringlength);
1752 if (developer.integer >= 100)
1753 Con_Printf("fputs: %s: %s\n", PRVM_NAME, string);
1760 float strlen(string s)
1763 //float(string s) strlen = #114; // returns how many characters are in a string
1764 void VM_strlen(void)
1766 VM_SAFEPARMCOUNT(1,VM_strlen);
1768 PRVM_G_FLOAT(OFS_RETURN) = strlen(PRVM_G_STRING(OFS_PARM0));
1771 // DRESK - Decolorized String
1776 string strdecolorize(string s)
1779 // string (string s) strdecolorize = #472; // returns the passed in string with color codes stripped
1780 void VM_strdecolorize(void)
1782 char szNewString[VM_STRINGTEMP_LENGTH];
1783 const char *szString;
1794 VM_SAFEPARMCOUNT(1,VM_strdecolorize);
1795 szString = PRVM_G_STRING(OFS_PARM0);
1798 { // Traverse through String
1799 if( szString[nPos] == '\n' || szString[nPos] == '\r' || szString[nPos] <= 0)
1800 { // String End Found
1801 szNewString[nFillPos++] = szString[nPos];
1805 if( szString[nPos] == STRING_COLOR_TAG)
1806 { // Color Code Located
1807 if( szString[nPos + 1] == STRING_COLOR_TAG)
1808 { // Valid Characters to Include
1809 szNewString[nFillPos++] = szString[nPos];
1811 szNewString[nFillPos++] = szString[nPos];
1814 if( szString[nPos + 1] >= '0' && szString[nPos + 1] <= '9' )
1815 { // Color Code Found; Increment Position
1819 { // Unknown Color Code; Include
1820 szNewString[nFillPos++] = szString[nPos];
1825 // Include Character
1826 szNewString[nFillPos++] = szString[nPos];
1828 // Increment Position
1832 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(szNewString);
1835 // DRESK - String Length (not counting color codes)
1840 float strlennocol(string s)
1843 // float(string s) strlennocol = #471; // returns how many characters are in a string not including color codes
1844 // For example, ^2Dresk returns a length of 5
1845 void VM_strlennocol(void)
1847 const char *szString;
1855 VM_SAFEPARMCOUNT(1,VM_strlennocol);
1857 szString = PRVM_G_STRING(OFS_PARM0);
1860 { // Count Characters
1861 // SV_BroadcastPrintf("Position '%d'; Character '%c'; Length '%d'\n", nPos, szString[nPos], nCnt);
1863 if( szString[nPos] == '\n' || szString[nPos] == '\r' || szString[nPos] <= 0)
1864 { // String End Found
1865 // SV_BroadcastPrintf("Found End of String at '%d'\n", nPos);
1869 if( szString[nPos] == STRING_COLOR_TAG)
1870 { // Color Code Located
1871 if( szString[nPos + 1] == STRING_COLOR_TAG)
1872 { // Increment Length; Skip Color Code
1877 if( szString[nPos + 1] >= '0' && szString[nPos + 1] <= '9' )
1878 { // Color Code Found; Increment Position
1879 // SV_BroadcastPrintf("Found Color Codes at '%d'\n", nPos);
1883 { // Unknown Color Code; Increment Length!
1889 // Increment String Length
1892 // Increment Position
1895 PRVM_G_FLOAT(OFS_RETURN) = nCnt;
1902 string strcat(string,string,...[string])
1905 //string(string s1, string s2) strcat = #115;
1906 // concatenates two strings (for example "abc", "def" would return "abcdef")
1907 // and returns as a tempstring
1908 void VM_strcat(void)
1910 char s[VM_STRINGTEMP_LENGTH];
1913 PRVM_ERROR("VM_strcat wrong parameter count (min. 1 expected ) !");
1915 VM_VarString(0, s, sizeof(s));
1916 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(s);
1923 string substring(string s, float start, float length)
1926 // string(string s, float start, float length) substring = #116;
1927 // returns a section of a string as a tempstring
1928 void VM_substring(void)
1930 int i, start, length;
1932 char string[VM_STRINGTEMP_LENGTH];
1934 VM_SAFEPARMCOUNT(3,VM_substring);
1936 s = PRVM_G_STRING(OFS_PARM0);
1937 start = (int)PRVM_G_FLOAT(OFS_PARM1);
1938 length = (int)PRVM_G_FLOAT(OFS_PARM2);
1939 for (i = 0;i < start && *s;i++, s++);
1940 for (i = 0;i < (int)sizeof(string) - 1 && *s && i < length;i++, s++)
1943 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string);
1950 vector stov(string s)
1953 //vector(string s) stov = #117; // returns vector value from a string
1956 char string[VM_STRINGTEMP_LENGTH];
1958 VM_SAFEPARMCOUNT(1,VM_stov);
1960 VM_VarString(0, string, sizeof(string));
1961 Math_atov(string, PRVM_G_VECTOR(OFS_RETURN));
1968 string strzone(string s)
1971 //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)
1972 void VM_strzone(void)
1975 char string[VM_STRINGTEMP_LENGTH];
1978 VM_SAFEPARMCOUNT(1,VM_strzone);
1980 VM_VarString(0, string, sizeof(string));
1981 alloclen = strlen(string) + 1;
1982 PRVM_G_INT(OFS_RETURN) = PRVM_AllocString(alloclen, &out);
1983 memcpy(out, string, alloclen);
1993 //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!!!)
1994 void VM_strunzone(void)
1996 VM_SAFEPARMCOUNT(1,VM_strunzone);
1997 PRVM_FreeString(PRVM_G_INT(OFS_PARM0));
2002 VM_command (used by client and menu)
2004 clientcommand(float client, string s) (for client and menu)
2007 //void(entity e, string s) clientcommand = #440; // executes a command string as if it came from the specified client
2008 //this function originally written by KrimZon, made shorter by LordHavoc
2009 void VM_clcommand (void)
2011 client_t *temp_client;
2014 VM_SAFEPARMCOUNT(2,VM_clcommand);
2016 i = (int)PRVM_G_FLOAT(OFS_PARM0);
2017 if (!sv.active || i < 0 || i >= svs.maxclients || !svs.clients[i].active)
2019 VM_Warning("VM_clientcommand: %s: invalid client/server is not active !\n", PRVM_NAME);
2023 temp_client = host_client;
2024 host_client = svs.clients + i;
2025 Cmd_ExecuteString (PRVM_G_STRING(OFS_PARM1), src_client);
2026 host_client = temp_client;
2034 float tokenize(string s)
2037 //float(string s) tokenize = #441; // takes apart a string into individal words (access them with argv), returns how many
2038 //this function originally written by KrimZon, made shorter by LordHavoc
2039 //20040203: rewritten by LordHavoc (no longer uses allocations)
2042 void VM_tokenize (void)
2047 char tokenbuf[MAX_INPUTLINE];
2051 VM_SAFEPARMCOUNT(1,VM_tokenize);
2053 p = PRVM_G_STRING(OFS_PARM0);
2056 while(COM_ParseToken(&p, false))
2058 if (num_tokens >= (int)(sizeof(tokens)/sizeof(tokens[0])))
2061 tokenlen = strlen(com_token) + 1;
2062 if (pos + tokenlen > sizeof(tokenbuf))
2064 tokens[num_tokens++] = PRVM_SetEngineString(tokenbuf + pos);
2065 memcpy(tokenbuf + pos, com_token, tokenlen);
2068 tokens[num_tokens++] = PRVM_SetTempString(com_token);
2072 PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
2075 //string(float n) argv = #442; // returns a word from the tokenized string (returns nothing for an invalid index)
2076 //this function originally written by KrimZon, made shorter by LordHavoc
2081 VM_SAFEPARMCOUNT(1,VM_argv);
2083 token_num = (int)PRVM_G_FLOAT(OFS_PARM0);
2085 if (token_num >= 0 && token_num < num_tokens)
2086 PRVM_G_INT(OFS_RETURN) = tokens[token_num];
2088 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
2092 //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)
2093 void PF_setattachment (void)
2095 prvm_edict_t *e = PRVM_G_EDICT(OFS_PARM0);
2096 prvm_edict_t *tagentity = PRVM_G_EDICT(OFS_PARM1);
2097 char *tagname = PRVM_G_STRING(OFS_PARM2);
2102 if (tagentity == NULL)
2103 tagentity = prog->edicts;
2105 v = PRVM_GETEDICTFIELDVALUE(e, eval_tag_entity);
2107 fields.server->edict = PRVM_EDICT_TO_PROG(tagentity);
2109 v = PRVM_GETEDICTFIELDVALUE(e, eval_tag_index);
2111 fields.server->_float = 0;
2112 if (tagentity != NULL && tagentity != prog->edicts && tagname && tagname[0])
2114 modelindex = (int)tagentity->fields.server->modelindex;
2115 if (modelindex >= 0 && modelindex < MAX_MODELS)
2117 model = sv.models[modelindex];
2118 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)
2119 for (i = 0;i < model->data_overridetagnamesforskin[(unsigned int)tagentity->fields.server->skin].num_overridetagnames;i++)
2120 if (!strcmp(tagname, model->data_overridetagnamesforskin[(unsigned int)tagentity->fields.server->skin].data_overridetagnames[i].name))
2121 fields.server->_float = i + 1;
2122 // FIXME: use a model function to get tag info (need to handle skeletal)
2123 if (fields.server->_float == 0 && model->num_tags)
2124 for (i = 0;i < model->num_tags;i++)
2125 if (!strcmp(tagname, model->data_tags[i].name))
2126 fields.server->_float = i + 1;
2127 if (fields.server->_float == 0)
2128 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);
2131 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));
2142 void VM_isserver(void)
2144 VM_SAFEPARMCOUNT(0,VM_serverstate);
2146 PRVM_G_FLOAT(OFS_RETURN) = sv.active;
2156 void VM_clientcount(void)
2158 VM_SAFEPARMCOUNT(0,VM_clientcount);
2160 PRVM_G_FLOAT(OFS_RETURN) = svs.maxclients;
2170 void VM_clientstate(void)
2172 VM_SAFEPARMCOUNT(0,VM_clientstate);
2174 PRVM_G_FLOAT(OFS_RETURN) = cls.state;
2181 float getostype(void)
2183 */ // not used at the moment -> not included in the common list
2184 void VM_getostype(void)
2186 VM_SAFEPARMCOUNT(0,VM_getostype);
2191 OS_MAC - not supported
2195 PRVM_G_FLOAT(OFS_RETURN) = 0;
2196 #elif defined(MACOSX)
2197 PRVM_G_FLOAT(OFS_RETURN) = 2;
2199 PRVM_G_FLOAT(OFS_RETURN) = 1;
2207 vector getmousepos()
2210 void VM_getmousepos(void)
2213 VM_SAFEPARMCOUNT(0,VM_getmousepos);
2215 PRVM_G_VECTOR(OFS_RETURN)[0] = in_mouse_x * vid_conwidth.integer / vid.width;
2216 PRVM_G_VECTOR(OFS_RETURN)[1] = in_mouse_y * vid_conheight.integer / vid.height;
2217 PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
2227 void VM_gettime(void)
2229 VM_SAFEPARMCOUNT(0,VM_gettime);
2231 PRVM_G_FLOAT(OFS_RETURN) = (float) *prog->time;
2238 loadfromdata(string data)
2241 void VM_loadfromdata(void)
2243 VM_SAFEPARMCOUNT(1,VM_loadentsfromfile);
2245 PRVM_ED_LoadFromFile(PRVM_G_STRING(OFS_PARM0));
2249 ========================
2252 parseentitydata(entity ent, string data)
2253 ========================
2255 void VM_parseentitydata(void)
2260 VM_SAFEPARMCOUNT(2, VM_parseentitydata);
2262 // get edict and test it
2263 ent = PRVM_G_EDICT(OFS_PARM0);
2264 if (ent->priv.required->free)
2265 PRVM_ERROR ("VM_parseentitydata: %s: Can only set already spawned entities (entity %i is free)!", PRVM_NAME, PRVM_NUM_FOR_EDICT(ent));
2267 data = PRVM_G_STRING(OFS_PARM1);
2269 // parse the opening brace
2270 if (!COM_ParseTokenConsole(&data) || com_token[0] != '{' )
2271 PRVM_ERROR ("VM_parseentitydata: %s: Couldn't parse entity data:\n%s", PRVM_NAME, data );
2273 PRVM_ED_ParseEdict (data, ent);
2280 loadfromfile(string file)
2283 void VM_loadfromfile(void)
2285 const char *filename;
2288 VM_SAFEPARMCOUNT(1,VM_loadfromfile);
2290 filename = PRVM_G_STRING(OFS_PARM0);
2291 if (FS_CheckNastyPath(filename, false))
2293 PRVM_G_FLOAT(OFS_RETURN) = -4;
2294 VM_Warning("VM_loadfromfile: %s dangerous or non-portable filename \"%s\" not allowed. (contains : or \\ or begins with .. or /)\n", PRVM_NAME, filename);
2298 // not conform with VM_fopen
2299 data = (char *)FS_LoadFile(filename, tempmempool, false, NULL);
2301 PRVM_G_FLOAT(OFS_RETURN) = -1;
2303 PRVM_ED_LoadFromFile(data);
2314 float mod(float val, float m)
2317 void VM_modulo(void)
2320 VM_SAFEPARMCOUNT(2,VM_module);
2322 val = (int) PRVM_G_FLOAT(OFS_PARM0);
2323 m = (int) PRVM_G_FLOAT(OFS_PARM1);
2325 PRVM_G_FLOAT(OFS_RETURN) = (float) (val % m);
2328 void VM_Search_Init(void)
2331 for (i = 0;i < PRVM_MAX_OPENSEARCHES;i++)
2332 prog->opensearches[i] = NULL;
2335 void VM_Search_Reset(void)
2338 // reset the fssearch list
2339 for(i = 0; i < PRVM_MAX_OPENSEARCHES; i++)
2341 if(prog->opensearches[i])
2342 FS_FreeSearch(prog->opensearches[i]);
2343 prog->opensearches[i] = NULL;
2351 float search_begin(string pattern, float caseinsensitive, float quiet)
2354 void VM_search_begin(void)
2357 const char *pattern;
2358 int caseinsens, quiet;
2360 VM_SAFEPARMCOUNT(3, VM_search_begin);
2362 pattern = PRVM_G_STRING(OFS_PARM0);
2364 VM_CheckEmptyString(pattern);
2366 caseinsens = (int)PRVM_G_FLOAT(OFS_PARM1);
2367 quiet = (int)PRVM_G_FLOAT(OFS_PARM2);
2369 for(handle = 0; handle < PRVM_MAX_OPENSEARCHES; handle++)
2370 if(!prog->opensearches[handle])
2373 if(handle >= PRVM_MAX_OPENSEARCHES)
2375 PRVM_G_FLOAT(OFS_RETURN) = -2;
2376 VM_Warning("VM_search_begin: %s ran out of search handles (%i)\n", PRVM_NAME, PRVM_MAX_OPENSEARCHES);
2380 if(!(prog->opensearches[handle] = FS_Search(pattern,caseinsens, quiet)))
2381 PRVM_G_FLOAT(OFS_RETURN) = -1;
2383 PRVM_G_FLOAT(OFS_RETURN) = handle;
2390 void search_end(float handle)
2393 void VM_search_end(void)
2396 VM_SAFEPARMCOUNT(1, VM_search_end);
2398 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
2400 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
2402 VM_Warning("VM_search_end: invalid handle %i used in %s\n", handle, PRVM_NAME);
2405 if(prog->opensearches[handle] == NULL)
2407 VM_Warning("VM_search_end: no such handle %i in %s\n", handle, PRVM_NAME);
2411 FS_FreeSearch(prog->opensearches[handle]);
2412 prog->opensearches[handle] = NULL;
2419 float search_getsize(float handle)
2422 void VM_search_getsize(void)
2425 VM_SAFEPARMCOUNT(1, VM_M_search_getsize);
2427 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
2429 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
2431 VM_Warning("VM_search_getsize: invalid handle %i used in %s\n", handle, PRVM_NAME);
2434 if(prog->opensearches[handle] == NULL)
2436 VM_Warning("VM_search_getsize: no such handle %i in %s\n", handle, PRVM_NAME);
2440 PRVM_G_FLOAT(OFS_RETURN) = prog->opensearches[handle]->numfilenames;
2445 VM_search_getfilename
2447 string search_getfilename(float handle, float num)
2450 void VM_search_getfilename(void)
2452 int handle, filenum;
2453 VM_SAFEPARMCOUNT(2, VM_search_getfilename);
2455 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
2456 filenum = (int)PRVM_G_FLOAT(OFS_PARM1);
2458 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
2460 VM_Warning("VM_search_getfilename: invalid handle %i used in %s\n", handle, PRVM_NAME);
2463 if(prog->opensearches[handle] == NULL)
2465 VM_Warning("VM_search_getfilename: no such handle %i in %s\n", handle, PRVM_NAME);
2468 if(filenum < 0 || filenum >= prog->opensearches[handle]->numfilenames)
2470 VM_Warning("VM_search_getfilename: invalid filenum %i in %s\n", filenum, PRVM_NAME);
2474 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog->opensearches[handle]->filenames[filenum]);
2481 string chr(float ascii)
2487 VM_SAFEPARMCOUNT(1, VM_chr);
2489 tmp[0] = (unsigned char) PRVM_G_FLOAT(OFS_PARM0);
2492 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(tmp);
2495 //=============================================================================
2496 // Draw builtins (client & menu)
2502 float iscachedpic(string pic)
2505 void VM_iscachedpic(void)
2507 VM_SAFEPARMCOUNT(1,VM_iscachedpic);
2509 // drawq hasnt such a function, thus always return true
2510 PRVM_G_FLOAT(OFS_RETURN) = false;
2517 string precache_pic(string pic)
2520 void VM_precache_pic(void)
2524 VM_SAFEPARMCOUNT(1, VM_precache_pic);
2526 s = PRVM_G_STRING(OFS_PARM0);
2527 PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
2528 VM_CheckEmptyString (s);
2530 // AK Draw_CachePic is supposed to always return a valid pointer
2531 if( Draw_CachePic(s, false)->tex == r_texture_notexture )
2532 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
2542 void VM_freepic(void)
2546 VM_SAFEPARMCOUNT(1,VM_freepic);
2548 s = PRVM_G_STRING(OFS_PARM0);
2549 VM_CheckEmptyString (s);
2558 float drawcharacter(vector position, float character, vector scale, vector rgb, float alpha, float flag)
2561 void VM_drawcharacter(void)
2563 float *pos,*scale,*rgb;
2566 VM_SAFEPARMCOUNT(6,VM_drawcharacter);
2568 character = (char) PRVM_G_FLOAT(OFS_PARM1);
2571 PRVM_G_FLOAT(OFS_RETURN) = -1;
2572 VM_Warning("VM_drawcharacter: %s passed null character !\n",PRVM_NAME);
2576 pos = PRVM_G_VECTOR(OFS_PARM0);
2577 scale = PRVM_G_VECTOR(OFS_PARM2);
2578 rgb = PRVM_G_VECTOR(OFS_PARM3);
2579 flag = (int)PRVM_G_FLOAT(OFS_PARM5);
2581 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2583 PRVM_G_FLOAT(OFS_RETURN) = -2;
2584 VM_Warning("VM_drawcharacter: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2588 if(pos[2] || scale[2])
2589 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")));
2591 if(!scale[0] || !scale[1])
2593 PRVM_G_FLOAT(OFS_RETURN) = -3;
2594 VM_Warning("VM_drawcharacter: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
2598 DrawQ_String (pos[0], pos[1], &character, 1, scale[0], scale[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM4), flag);
2599 PRVM_G_FLOAT(OFS_RETURN) = 1;
2606 float drawstring(vector position, string text, vector scale, vector rgb, float alpha, float flag)
2609 void VM_drawstring(void)
2611 float *pos,*scale,*rgb;
2614 VM_SAFEPARMCOUNT(6,VM_drawstring);
2616 string = PRVM_G_STRING(OFS_PARM1);
2617 pos = PRVM_G_VECTOR(OFS_PARM0);
2618 scale = PRVM_G_VECTOR(OFS_PARM2);
2619 rgb = PRVM_G_VECTOR(OFS_PARM3);
2620 flag = (int)PRVM_G_FLOAT(OFS_PARM5);
2622 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2624 PRVM_G_FLOAT(OFS_RETURN) = -2;
2625 VM_Warning("VM_drawstring: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2629 if(!scale[0] || !scale[1])
2631 PRVM_G_FLOAT(OFS_RETURN) = -3;
2632 VM_Warning("VM_drawstring: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
2636 if(pos[2] || scale[2])
2637 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")));
2639 DrawQ_String (pos[0], pos[1], string, 0, scale[0], scale[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM4), flag);
2640 PRVM_G_FLOAT(OFS_RETURN) = 1;
2646 float drawpic(vector position, string pic, vector size, vector rgb, float alpha, float flag)
2649 void VM_drawpic(void)
2651 const char *picname;
2652 float *size, *pos, *rgb;
2655 VM_SAFEPARMCOUNT(6,VM_drawpic);
2657 picname = PRVM_G_STRING(OFS_PARM1);
2658 VM_CheckEmptyString (picname);
2660 // is pic cached ? no function yet for that
2663 PRVM_G_FLOAT(OFS_RETURN) = -4;
2664 VM_Warning("VM_drawpic: %s: %s not cached !\n", PRVM_NAME, picname);
2668 pos = PRVM_G_VECTOR(OFS_PARM0);
2669 size = PRVM_G_VECTOR(OFS_PARM2);
2670 rgb = PRVM_G_VECTOR(OFS_PARM3);
2671 flag = (int) PRVM_G_FLOAT(OFS_PARM5);
2673 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2675 PRVM_G_FLOAT(OFS_RETURN) = -2;
2676 VM_Warning("VM_drawpic: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2680 if(pos[2] || size[2])
2681 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")));
2683 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);
2684 PRVM_G_FLOAT(OFS_RETURN) = 1;
2691 float drawfill(vector position, vector size, vector rgb, float alpha, float flag)
2694 void VM_drawfill(void)
2696 float *size, *pos, *rgb;
2699 VM_SAFEPARMCOUNT(5,VM_drawfill);
2702 pos = PRVM_G_VECTOR(OFS_PARM0);
2703 size = PRVM_G_VECTOR(OFS_PARM1);
2704 rgb = PRVM_G_VECTOR(OFS_PARM2);
2705 flag = (int) PRVM_G_FLOAT(OFS_PARM4);
2707 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
2709 PRVM_G_FLOAT(OFS_RETURN) = -2;
2710 VM_Warning("VM_drawfill: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
2714 if(pos[2] || size[2])
2715 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")));
2717 DrawQ_Pic(pos[0], pos[1], NULL, size[0], size[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM3), flag);
2718 PRVM_G_FLOAT(OFS_RETURN) = 1;
2725 drawsetcliparea(float x, float y, float width, float height)
2728 void VM_drawsetcliparea(void)
2731 VM_SAFEPARMCOUNT(4,VM_drawsetcliparea);
2733 x = bound(0, PRVM_G_FLOAT(OFS_PARM0), vid_conwidth.integer);
2734 y = bound(0, PRVM_G_FLOAT(OFS_PARM1), vid_conheight.integer);
2735 w = bound(0, PRVM_G_FLOAT(OFS_PARM2) + PRVM_G_FLOAT(OFS_PARM0) - x, (vid_conwidth.integer - x));
2736 h = bound(0, PRVM_G_FLOAT(OFS_PARM3) + PRVM_G_FLOAT(OFS_PARM1) - y, (vid_conheight.integer - y));
2738 DrawQ_SetClipArea(x, y, w, h);
2743 VM_drawresetcliparea
2748 void VM_drawresetcliparea(void)
2750 VM_SAFEPARMCOUNT(0,VM_drawresetcliparea);
2752 DrawQ_ResetClipArea();
2759 vector getimagesize(string pic)
2762 void VM_getimagesize(void)
2767 VM_SAFEPARMCOUNT(1,VM_getimagesize);
2769 p = PRVM_G_STRING(OFS_PARM0);
2770 VM_CheckEmptyString (p);
2772 pic = Draw_CachePic (p, false);
2774 PRVM_G_VECTOR(OFS_RETURN)[0] = pic->width;
2775 PRVM_G_VECTOR(OFS_RETURN)[1] = pic->height;
2776 PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
2783 string keynumtostring(float keynum)
2786 void VM_keynumtostring (void)
2788 VM_SAFEPARMCOUNT(1, VM_keynumtostring);
2790 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(Key_KeynumToString((int)PRVM_G_FLOAT(OFS_PARM0)));
2797 float stringtokeynum(string key)
2800 void VM_stringtokeynum (void)
2802 VM_SAFEPARMCOUNT( 1, VM_keynumtostring );
2804 PRVM_G_INT(OFS_RETURN) = Key_StringToKeynum(PRVM_G_STRING(OFS_PARM0));
2807 // CL_Video interface functions
2810 ========================
2813 float cin_open(string file, string name)
2814 ========================
2816 void VM_cin_open( void )
2821 VM_SAFEPARMCOUNT( 2, VM_cin_open );
2823 file = PRVM_G_STRING( OFS_PARM0 );
2824 name = PRVM_G_STRING( OFS_PARM1 );
2826 VM_CheckEmptyString( file );
2827 VM_CheckEmptyString( name );
2829 if( CL_OpenVideo( file, name, MENUOWNER ) )
2830 PRVM_G_FLOAT( OFS_RETURN ) = 1;
2832 PRVM_G_FLOAT( OFS_RETURN ) = 0;
2836 ========================
2839 void cin_close(string name)
2840 ========================
2842 void VM_cin_close( void )
2846 VM_SAFEPARMCOUNT( 1, VM_cin_close );
2848 name = PRVM_G_STRING( OFS_PARM0 );
2849 VM_CheckEmptyString( name );
2851 CL_CloseVideo( CL_GetVideoByName( name ) );
2855 ========================
2857 void cin_setstate(string name, float type)
2858 ========================
2860 void VM_cin_setstate( void )
2863 clvideostate_t state;
2866 VM_SAFEPARMCOUNT( 2, VM_cin_netstate );
2868 name = PRVM_G_STRING( OFS_PARM0 );
2869 VM_CheckEmptyString( name );
2871 state = (clvideostate_t)((int)PRVM_G_FLOAT( OFS_PARM1 ));
2873 video = CL_GetVideoByName( name );
2874 if( video && state > CLVIDEO_UNUSED && state < CLVIDEO_STATECOUNT )
2875 CL_SetVideoState( video, state );
2879 ========================
2882 float cin_getstate(string name)
2883 ========================
2885 void VM_cin_getstate( void )
2890 VM_SAFEPARMCOUNT( 1, VM_cin_getstate );
2892 name = PRVM_G_STRING( OFS_PARM0 );
2893 VM_CheckEmptyString( name );
2895 video = CL_GetVideoByName( name );
2897 PRVM_G_FLOAT( OFS_RETURN ) = (int)video->state;
2899 PRVM_G_FLOAT( OFS_RETURN ) = 0;
2903 ========================
2906 void cin_restart(string name)
2907 ========================
2909 void VM_cin_restart( void )
2914 VM_SAFEPARMCOUNT( 1, VM_cin_restart );
2916 name = PRVM_G_STRING( OFS_PARM0 );
2917 VM_CheckEmptyString( name );
2919 video = CL_GetVideoByName( name );
2921 CL_RestartVideo( video );
2928 Writes new values for v_forward, v_up, and v_right based on the given forward vector
2929 vectorvectors(vector, vector)
2932 void VM_vectorvectors (void)
2934 VectorNormalize2(PRVM_G_VECTOR(OFS_PARM0), prog->globals.server->v_forward);
2935 VectorVectors(prog->globals.server->v_forward, prog->globals.server->v_right, prog->globals.server->v_up);
2939 ========================
2942 void drawline(float width, vector pos1, vector pos2, vector rgb, float alpha, float flags)
2943 ========================
2945 void VM_drawline (void)
2947 float *c1, *c2, *rgb;
2949 unsigned char flags;
2951 VM_SAFEPARMCOUNT(6, VM_drawline);
2952 width = PRVM_G_FLOAT(OFS_PARM0);
2953 c1 = PRVM_G_VECTOR(OFS_PARM1);
2954 c2 = PRVM_G_VECTOR(OFS_PARM2);
2955 rgb = PRVM_G_VECTOR(OFS_PARM3);
2956 alpha = PRVM_G_FLOAT(OFS_PARM4);
2957 flags = (int)PRVM_G_FLOAT(OFS_PARM5);
2958 DrawQ_Line(width, c1[0], c1[1], c2[0], c2[1], rgb[0], rgb[1], rgb[2], alpha, flags);
2961 //====================
2962 //QC POLYGON functions
2963 //====================
2968 float data[36]; //[515]: enough for polygons
2969 unsigned char flags; //[515]: + VM_POLYGON_2D and VM_POLYGON_FL4V flags
2972 //static float vm_polygon_linewidth = 1;
2973 static mempool_t *vm_polygons_pool = NULL;
2974 static unsigned char vm_current_vertices = 0;
2975 static qboolean vm_polygons_initialized = false;
2976 static vm_polygon_t *vm_polygons = NULL;
2977 static unsigned long vm_polygons_num = 0, vm_drawpolygons_num = 0; //[515]: ok long on 64bit ?
2978 static qboolean vm_polygonbegin = false; //[515]: for "no-crap-on-the-screen" check
2979 #define VM_DEFPOLYNUM 64 //[515]: enough for default ?
2981 #define VM_POLYGON_FL3V 16 //more than 2 vertices (used only for lines)
2982 #define VM_POLYGON_FLLINES 32
2983 #define VM_POLYGON_FL2D 64
2984 #define VM_POLYGON_FL4V 128 //4 vertices
2986 void VM_InitPolygons (void)
2988 vm_polygons_pool = Mem_AllocPool("VMPOLY", 0, NULL);
2989 vm_polygons = (vm_polygon_t *)Mem_Alloc(vm_polygons_pool, VM_DEFPOLYNUM*sizeof(vm_polygon_t));
2990 memset(vm_polygons, 0, VM_DEFPOLYNUM*sizeof(vm_polygon_t));
2991 vm_polygons_num = VM_DEFPOLYNUM;
2992 vm_drawpolygons_num = 0;
2993 vm_polygonbegin = false;
2994 vm_polygons_initialized = true;
2997 void VM_DrawPolygonCallback (const entity_render_t *ent, const rtlight_t *rtlight, int numsurfaces, int *surfacelist)
2999 int surfacelistindex;
3000 // LordHavoc: FIXME: this is stupid code
3001 for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
3003 const vm_polygon_t *p = &vm_polygons[surfacelist[surfacelistindex]];
3004 int flags = p->flags & 0x0f;
3006 if(flags == DRAWFLAG_ADDITIVE)
3007 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
3008 else if(flags == DRAWFLAG_MODULATE)
3009 GL_BlendFunc(GL_DST_COLOR, GL_ZERO);
3010 else if(flags == DRAWFLAG_2XMODULATE)
3011 GL_BlendFunc(GL_DST_COLOR,GL_SRC_COLOR);
3013 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
3015 R_Mesh_TexBind(0, R_GetTexture(p->tex));
3018 //[515]: is speed is max ?
3019 if(p->flags & VM_POLYGON_FLLINES) //[515]: lines
3021 qglLineWidth(p->data[13]);CHECKGLERROR
3022 qglBegin(GL_LINE_LOOP);
3023 qglTexCoord1f (p->data[12]);
3024 qglColor4f (p->data[20], p->data[21], p->data[22], p->data[23]);
3025 qglVertex3f (p->data[0] , p->data[1], p->data[2]);
3027 qglTexCoord1f (p->data[14]);
3028 qglColor4f (p->data[24], p->data[25], p->data[26], p->data[27]);
3029 qglVertex3f (p->data[3] , p->data[4], p->data[5]);
3031 if(p->flags & VM_POLYGON_FL3V)
3033 qglTexCoord1f (p->data[16]);
3034 qglColor4f (p->data[28], p->data[29], p->data[30], p->data[31]);
3035 qglVertex3f (p->data[6] , p->data[7], p->data[8]);
3037 if(p->flags & VM_POLYGON_FL4V)
3039 qglTexCoord1f (p->data[18]);
3040 qglColor4f (p->data[32], p->data[33], p->data[34], p->data[35]);
3041 qglVertex3f (p->data[9] , p->data[10], p->data[11]);
3049 qglBegin(GL_POLYGON);
3050 qglTexCoord2f (p->data[12], p->data[13]);
3051 qglColor4f (p->data[20], p->data[21], p->data[22], p->data[23]);
3052 qglVertex3f (p->data[0] , p->data[1], p->data[2]);
3054 qglTexCoord2f (p->data[14], p->data[15]);
3055 qglColor4f (p->data[24], p->data[25], p->data[26], p->data[27]);
3056 qglVertex3f (p->data[3] , p->data[4], p->data[5]);
3058 qglTexCoord2f (p->data[16], p->data[17]);
3059 qglColor4f (p->data[28], p->data[29], p->data[30], p->data[31]);
3060 qglVertex3f (p->data[6] , p->data[7], p->data[8]);
3062 if(p->flags & VM_POLYGON_FL4V)
3064 qglTexCoord2f (p->data[18], p->data[19]);
3065 qglColor4f (p->data[32], p->data[33], p->data[34], p->data[35]);
3066 qglVertex3f (p->data[9] , p->data[10], p->data[11]);
3074 void VM_AddPolygonTo2DScene (vm_polygon_t *p)
3076 drawqueuemesh_t mesh;
3077 static int picelements[6] = {0, 1, 2, 0, 2, 3};
3079 mesh.texture = p->tex;
3080 mesh.data_element3i = picelements;
3081 mesh.data_vertex3f = p->data;
3082 mesh.data_texcoord2f = p->data + 12;
3083 mesh.data_color4f = p->data + 20;
3084 if(p->flags & VM_POLYGON_FL4V)
3086 mesh.num_vertices = 4;
3087 mesh.num_triangles = 2;
3091 mesh.num_vertices = 3;
3092 mesh.num_triangles = 1;
3094 if(p->flags & VM_POLYGON_FLLINES) //[515]: lines
3095 DrawQ_LineLoop (&mesh, (p->flags&0x0f));
3097 DrawQ_Mesh (&mesh, (p->flags&0x0f));
3100 //void(string texturename, float flag, float 2d, float lines) R_BeginPolygon
3101 void VM_R_PolygonBegin (void)
3104 const char *picname;
3106 VM_SAFEPARMCOUNT(2, VM_R_PolygonBegin);
3108 if(!vm_polygons_initialized)
3112 VM_Warning("VM_R_PolygonBegin: called twice without VM_R_PolygonEnd after first\n");
3115 if(vm_drawpolygons_num >= vm_polygons_num)
3117 p = (vm_polygon_t *)Mem_Alloc(vm_polygons_pool, 2 * vm_polygons_num * sizeof(vm_polygon_t));
3118 memset(p, 0, 2 * vm_polygons_num * sizeof(vm_polygon_t));
3119 memcpy(p, vm_polygons, vm_polygons_num * sizeof(vm_polygon_t));
3120 Mem_Free(vm_polygons);
3122 vm_polygons_num *= 2;
3124 p = &vm_polygons[vm_drawpolygons_num];
3125 picname = PRVM_G_STRING(OFS_PARM0);
3127 p->tex = Draw_CachePic(picname, true)->tex;
3129 p->tex = r_texture_white;
3130 p->flags = (unsigned char)PRVM_G_FLOAT(OFS_PARM1);
3131 vm_current_vertices = 0;
3132 vm_polygonbegin = true;
3135 if(PRVM_G_FLOAT(OFS_PARM2))
3136 p->flags |= VM_POLYGON_FL2D;
3137 if(prog->argc >= 4 && PRVM_G_FLOAT(OFS_PARM3))
3139 p->data[13] = PRVM_G_FLOAT(OFS_PARM3); //[515]: linewidth
3140 p->flags |= VM_POLYGON_FLLINES;
3145 //void(vector org, vector texcoords, vector rgb, float alpha) R_PolygonVertex
3146 void VM_R_PolygonVertex (void)
3148 float *coords, *tx, *rgb, alpha;
3150 VM_SAFEPARMCOUNT(4, VM_R_PolygonVertex);
3152 if(!vm_polygonbegin)
3154 VM_Warning("VM_R_PolygonVertex: VM_R_PolygonBegin wasn't called\n");
3157 coords = PRVM_G_VECTOR(OFS_PARM0);
3158 tx = PRVM_G_VECTOR(OFS_PARM1);
3159 rgb = PRVM_G_VECTOR(OFS_PARM2);
3160 alpha = PRVM_G_FLOAT(OFS_PARM3);
3162 p = &vm_polygons[vm_drawpolygons_num];
3163 if(vm_current_vertices > 4)
3165 VM_Warning("VM_R_PolygonVertex: may have 4 vertices max\n");
3169 p->data[vm_current_vertices*3] = coords[0];
3170 p->data[1+vm_current_vertices*3] = coords[1];
3171 p->data[2+vm_current_vertices*3] = coords[2];
3173 p->data[12+vm_current_vertices*2] = tx[0];
3174 if(!(p->flags & VM_POLYGON_FLLINES))
3175 p->data[13+vm_current_vertices*2] = tx[1];
3177 p->data[20+vm_current_vertices*4] = rgb[0];
3178 p->data[21+vm_current_vertices*4] = rgb[1];
3179 p->data[22+vm_current_vertices*4] = rgb[2];
3180 p->data[23+vm_current_vertices*4] = alpha;
3182 vm_current_vertices++;
3183 if(vm_current_vertices == 4)
3184 p->flags |= VM_POLYGON_FL4V;
3186 if(vm_current_vertices == 3)
3187 p->flags |= VM_POLYGON_FL3V;
3190 //void() R_EndPolygon
3191 void VM_R_PolygonEnd (void)
3193 if(!vm_polygonbegin)
3195 VM_Warning("VM_R_PolygonEnd: VM_R_PolygonBegin wasn't called\n");
3198 vm_polygonbegin = false;
3199 if(vm_current_vertices > 2 || (vm_current_vertices >= 2 && vm_polygons[vm_drawpolygons_num].flags & VM_POLYGON_FLLINES))
3201 if(vm_polygons[vm_drawpolygons_num].flags & VM_POLYGON_FL2D) //[515]: don't use qcpolygons memory if 2D
3202 VM_AddPolygonTo2DScene(&vm_polygons[vm_drawpolygons_num]);
3204 vm_drawpolygons_num++;
3207 VM_Warning("VM_R_PolygonEnd: %i vertices isn't a good choice\n", vm_current_vertices);
3210 void VM_AddPolygonsToMeshQueue (void)
3213 if(!vm_drawpolygons_num)
3215 R_Mesh_Matrix(&identitymatrix);
3216 GL_CullFace(GL_NONE);
3217 for(i = 0;i < (int)vm_drawpolygons_num;i++)
3218 VM_DrawPolygonCallback(NULL, NULL, 1, &i);
3219 vm_drawpolygons_num = 0;
3222 void Debug_PolygonBegin(const char *picname, int flags, qboolean draw2d, float linewidth)
3226 if(!vm_polygons_initialized)
3230 Con_Printf("Debug_PolygonBegin: called twice without Debug_PolygonEnd after first\n");
3233 // limit polygons to a vaguely sane amount, beyond this each one just
3234 // replaces the last one
3235 vm_drawpolygons_num = min(vm_drawpolygons_num, (1<<20)-1);
3236 if(vm_drawpolygons_num >= vm_polygons_num)
3238 p = (vm_polygon_t *)Mem_Alloc(vm_polygons_pool, 2 * vm_polygons_num * sizeof(vm_polygon_t));
3239 memset(p, 0, 2 * vm_polygons_num * sizeof(vm_polygon_t));
3240 memcpy(p, vm_polygons, vm_polygons_num * sizeof(vm_polygon_t));
3241 Mem_Free(vm_polygons);
3243 vm_polygons_num *= 2;
3245 p = &vm_polygons[vm_drawpolygons_num];
3246 if(picname && picname[0])
3247 p->tex = Draw_CachePic(picname, true)->tex;
3249 p->tex = r_texture_white;
3251 vm_current_vertices = 0;
3252 vm_polygonbegin = true;
3254 p->flags |= VM_POLYGON_FL2D;
3257 p->data[13] = linewidth; //[515]: linewidth
3258 p->flags |= VM_POLYGON_FLLINES;
3262 void Debug_PolygonVertex(float x, float y, float z, float s, float t, float r, float g, float b, float a)
3266 if(!vm_polygonbegin)
3268 Con_Printf("Debug_PolygonVertex: Debug_PolygonBegin wasn't called\n");
3272 p = &vm_polygons[vm_drawpolygons_num];
3273 if(vm_current_vertices > 4)
3275 Con_Printf("Debug_PolygonVertex: may have 4 vertices max\n");
3279 p->data[vm_current_vertices*3] = x;
3280 p->data[1+vm_current_vertices*3] = y;
3281 p->data[2+vm_current_vertices*3] = z;
3283 p->data[12+vm_current_vertices*2] = s;
3284 if(!(p->flags & VM_POLYGON_FLLINES))
3285 p->data[13+vm_current_vertices*2] = t;
3287 p->data[20+vm_current_vertices*4] = r;
3288 p->data[21+vm_current_vertices*4] = g;
3289 p->data[22+vm_current_vertices*4] = b;
3290 p->data[23+vm_current_vertices*4] = a;
3292 vm_current_vertices++;
3293 if(vm_current_vertices == 4)
3294 p->flags |= VM_POLYGON_FL4V;
3296 if(vm_current_vertices == 3)
3297 p->flags |= VM_POLYGON_FL3V;
3300 void Debug_PolygonEnd(void)
3302 if(!vm_polygonbegin)
3304 Con_Printf("Debug_PolygonEnd: Debug_PolygonBegin wasn't called\n");
3307 vm_polygonbegin = false;
3308 if(vm_current_vertices > 2 || (vm_current_vertices >= 2 && vm_polygons[vm_drawpolygons_num].flags & VM_POLYGON_FLLINES))
3310 if(vm_polygons[vm_drawpolygons_num].flags & VM_POLYGON_FL2D) //[515]: don't use qcpolygons memory if 2D
3311 VM_AddPolygonTo2DScene(&vm_polygons[vm_drawpolygons_num]);
3313 vm_drawpolygons_num++;
3316 Con_Printf("Debug_PolygonEnd: %i vertices isn't a good choice\n", vm_current_vertices);
3323 // float(float number, float quantity) bitshift (EXT_BITSHIFT)
3324 void VM_bitshift (void)
3327 VM_SAFEPARMCOUNT(2, VM_bitshift);
3329 n1 = (int)fabs((int)PRVM_G_FLOAT(OFS_PARM0));
3330 n2 = (int)PRVM_G_FLOAT(OFS_PARM1);
3332 PRVM_G_FLOAT(OFS_RETURN) = n1;
3335 PRVM_G_FLOAT(OFS_RETURN) = (n1 >> -n2);
3337 PRVM_G_FLOAT(OFS_RETURN) = (n1 << n2);
3340 ////////////////////////////////////////
3341 // AltString functions
3342 ////////////////////////////////////////
3345 ========================
3348 float altstr_count(string)
3349 ========================
3351 void VM_altstr_count( void )
3353 const char *altstr, *pos;
3356 VM_SAFEPARMCOUNT( 1, VM_altstr_count );
3358 altstr = PRVM_G_STRING( OFS_PARM0 );
3359 //VM_CheckEmptyString( altstr );
3361 for( count = 0, pos = altstr ; *pos ; pos++ ) {
3362 if( *pos == '\\' ) {
3366 } else if( *pos == '\'' ) {
3371 PRVM_G_FLOAT( OFS_RETURN ) = (float) (count / 2);
3375 ========================
3378 string altstr_prepare(string)
3379 ========================
3381 void VM_altstr_prepare( void )
3384 const char *instr, *in;
3386 char outstr[VM_STRINGTEMP_LENGTH];
3388 VM_SAFEPARMCOUNT( 1, VM_altstr_prepare );
3390 instr = PRVM_G_STRING( OFS_PARM0 );
3392 for( out = outstr, in = instr, size = sizeof(outstr) - 1 ; size && *in ; size--, in++, out++ )
3401 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
3405 ========================
3408 string altstr_get(string, float)
3409 ========================
3411 void VM_altstr_get( void )
3413 const char *altstr, *pos;
3416 char outstr[VM_STRINGTEMP_LENGTH];
3418 VM_SAFEPARMCOUNT( 2, VM_altstr_get );
3420 altstr = PRVM_G_STRING( OFS_PARM0 );
3422 count = (int)PRVM_G_FLOAT( OFS_PARM1 );
3423 count = count * 2 + 1;
3425 for( pos = altstr ; *pos && count ; pos++ )
3426 if( *pos == '\\' ) {
3429 } else if( *pos == '\'' )
3433 PRVM_G_INT( OFS_RETURN ) = 0;
3437 for( out = outstr, size = sizeof(outstr) - 1 ; size && *pos ; size--, pos++, out++ )
3438 if( *pos == '\\' ) {
3443 } else if( *pos == '\'' )
3449 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
3453 ========================
3456 string altstr_set(string altstr, float num, string set)
3457 ========================
3459 void VM_altstr_set( void )
3462 const char *altstr, *str;
3465 char outstr[VM_STRINGTEMP_LENGTH];
3467 VM_SAFEPARMCOUNT( 3, VM_altstr_set );
3469 altstr = PRVM_G_STRING( OFS_PARM0 );
3471 num = (int)PRVM_G_FLOAT( OFS_PARM1 );
3473 str = PRVM_G_STRING( OFS_PARM2 );
3476 for( num = num * 2 + 1, in = altstr; *in && num; *out++ = *in++ )
3481 } else if( *in == '\'' ) {
3486 for( ; *str; *out++ = *str++ );
3487 // now jump over the old content
3489 if( *in == '\'' || (*in == '\\' && !*++in) )
3492 strlcpy(out, in, outstr + sizeof(outstr) - out);
3493 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
3497 ========================
3500 string altstr_ins(string altstr, float num, string set)
3501 ========================
3503 void VM_altstr_ins(void)
3511 char outstr[VM_STRINGTEMP_LENGTH];
3513 in = instr = PRVM_G_STRING( OFS_PARM0 );
3514 num = (int)PRVM_G_FLOAT( OFS_PARM1 );
3515 set = setstr = PRVM_G_STRING( OFS_PARM2 );
3518 for( num = num * 2 + 2 ; *in && num > 0 ; *out++ = *in++ )
3523 } else if( *in == '\'' ) {
3528 for( ; *set ; *out++ = *set++ );
3531 strlcpy(out, in, outstr + sizeof(outstr) - out);
3532 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
3536 ////////////////////////////////////////
3537 // BufString functions
3538 ////////////////////////////////////////
3539 //[515]: string buffers support
3540 #define MAX_QCSTR_BUFFERS 128
3541 #define MAX_QCSTR_STRINGS 1024
3546 char *strings[MAX_QCSTR_STRINGS];
3549 static qcstrbuffer_t *qcstringbuffers[MAX_QCSTR_BUFFERS];
3550 static int num_qcstringbuffers;
3551 static int buf_sortpower;
3553 #define BUFSTR_BUFFER(a) (a>=MAX_QCSTR_BUFFERS) ? NULL : (qcstringbuffers[a])
3554 #define BUFSTR_ISFREE(a) (a<MAX_QCSTR_BUFFERS&&qcstringbuffers[a]&&qcstringbuffers[a]->num_strings<=0) ? 1 : 0
3556 static int BufStr_FindFreeBuffer (void)
3559 if(num_qcstringbuffers == MAX_QCSTR_BUFFERS)
3561 for(i=0;i<MAX_QCSTR_BUFFERS;i++)
3562 if(!qcstringbuffers[i])
3564 qcstringbuffers[i] = (qcstrbuffer_t *)Z_Malloc(sizeof(qcstrbuffer_t));
3565 memset(qcstringbuffers[i], 0, sizeof(qcstrbuffer_t));
3571 static void BufStr_ClearBuffer (int index)
3573 qcstrbuffer_t *b = qcstringbuffers[index];
3578 if(b->num_strings > 0)
3580 for(i=0;i<b->num_strings;i++)
3582 Z_Free(b->strings[i]);
3583 num_qcstringbuffers--;
3585 Z_Free(qcstringbuffers[index]);
3586 qcstringbuffers[index] = NULL;
3590 static int BufStr_FindFreeString (qcstrbuffer_t *b)
3593 for(i=0;i<b->num_strings;i++)
3594 if(!b->strings[i] || !b->strings[i][0])
3596 if(i == MAX_QCSTR_STRINGS) return -1;
3600 static int BufStr_SortStringsUP (const void *in1, const void *in2)
3603 a = *((const char **) in1);
3604 b = *((const char **) in2);
3606 if(!b[0]) return -1;
3607 return strncmp(a, b, buf_sortpower);
3610 static int BufStr_SortStringsDOWN (const void *in1, const void *in2)
3613 a = *((const char **) in1);
3614 b = *((const char **) in2);
3616 if(!b[0]) return -1;
3617 return strncmp(b, a, buf_sortpower);
3621 static void VM_BufStr_Init (void)
3623 memset(qcstringbuffers, 0, sizeof(qcstringbuffers));
3624 num_qcstringbuffers = 0;
3627 static void VM_BufStr_ShutDown (void)
3630 for(i=0;i<MAX_QCSTR_BUFFERS && num_qcstringbuffers;i++)
3631 BufStr_ClearBuffer(i);
3636 ========================
3638 creates new buffer, and returns it's index, returns -1 if failed
3639 float buf_create(void) = #460;
3640 ========================
3642 void VM_buf_create (void)
3645 VM_SAFEPARMCOUNT(0, VM_buf_create);
3646 i = BufStr_FindFreeBuffer();
3648 num_qcstringbuffers++;
3650 //Con_Printf("VM_buf_create: buffers overflow in %s\n", PRVM_NAME);
3651 PRVM_G_FLOAT(OFS_RETURN) = i;
3655 ========================
3657 deletes buffer and all strings in it
3658 void buf_del(float bufhandle) = #461;
3659 ========================
3661 void VM_buf_del (void)
3663 VM_SAFEPARMCOUNT(1, VM_buf_del);
3664 if(BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0)))
3665 BufStr_ClearBuffer((int)PRVM_G_FLOAT(OFS_PARM0));
3668 VM_Warning("VM_buf_del: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3674 ========================
3676 how many strings are stored in buffer
3677 float buf_getsize(float bufhandle) = #462;
3678 ========================
3680 void VM_buf_getsize (void)
3683 VM_SAFEPARMCOUNT(1, VM_buf_getsize);
3685 b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3688 PRVM_G_FLOAT(OFS_RETURN) = -1;
3689 VM_Warning("VM_buf_getsize: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3693 PRVM_G_FLOAT(OFS_RETURN) = b->num_strings;
3697 ========================
3699 copy all content from one buffer to another, make sure it exists
3700 void buf_copy(float bufhandle_from, float bufhandle_to) = #463;
3701 ========================
3703 void VM_buf_copy (void)
3705 qcstrbuffer_t *b1, *b2;
3707 VM_SAFEPARMCOUNT(2, VM_buf_copy);
3709 b1 = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3712 VM_Warning("VM_buf_copy: invalid source buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3715 i = (int)PRVM_G_FLOAT(OFS_PARM1);
3716 if(i == (int)PRVM_G_FLOAT(OFS_PARM0))
3718 VM_Warning("VM_buf_copy: source == destination (%i) in %s\n", i, PRVM_NAME);
3721 b2 = BUFSTR_BUFFER(i);
3724 VM_Warning("VM_buf_copy: invalid destination buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM1), PRVM_NAME);
3728 BufStr_ClearBuffer(i);
3729 qcstringbuffers[i] = (qcstrbuffer_t *)Z_Malloc(sizeof(qcstrbuffer_t));
3730 memset(qcstringbuffers[i], 0, sizeof(qcstrbuffer_t));
3731 b2->num_strings = b1->num_strings;
3733 for(i=0;i<b1->num_strings;i++)
3734 if(b1->strings[i] && b1->strings[i][0])
3737 stringlen = strlen(b1->strings[i]) + 1;
3738 b2->strings[i] = (char *)Z_Malloc(stringlen);
3741 VM_Warning("VM_buf_copy: not enough memory for buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM1), PRVM_NAME);
3744 memcpy(b2->strings[i], b1->strings[i], stringlen);
3749 ========================
3751 sort buffer by beginnings of strings (sortpower defaults it's lenght)
3752 "backward == TRUE" means that sorting goes upside-down
3753 void buf_sort(float bufhandle, float sortpower, float backward) = #464;
3754 ========================
3756 void VM_buf_sort (void)
3760 VM_SAFEPARMCOUNT(3, VM_buf_sort);
3762 b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3765 VM_Warning("VM_buf_sort: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3768 if(b->num_strings <= 0)
3770 VM_Warning("VM_buf_sort: tried to sort empty buffer %i in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3773 buf_sortpower = (int)PRVM_G_FLOAT(OFS_PARM1);
3774 if(buf_sortpower <= 0)
3775 buf_sortpower = 99999999;
3777 if(!PRVM_G_FLOAT(OFS_PARM2))
3778 qsort(b->strings, b->num_strings, sizeof(char*), BufStr_SortStringsUP);
3780 qsort(b->strings, b->num_strings, sizeof(char*), BufStr_SortStringsDOWN);
3782 for(i=b->num_strings-1;i>=0;i--) //[515]: delete empty lines
3785 if(b->strings[i][0])
3789 Z_Free(b->strings[i]);
3791 b->strings[i] = NULL;
3799 ========================
3801 concantenates all buffer string into one with "glue" separator and returns it as tempstring
3802 string buf_implode(float bufhandle, string glue) = #465;
3803 ========================
3805 void VM_buf_implode (void)
3808 char k[VM_STRINGTEMP_LENGTH];
3812 VM_SAFEPARMCOUNT(2, VM_buf_implode);
3814 b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3815 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
3818 VM_Warning("VM_buf_implode: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3823 sep = PRVM_G_STRING(OFS_PARM1);
3825 for(l=i=0;i<b->num_strings;i++)
3828 l += (i > 0 ? strlen(sep) : 0) + strlen(b->strings[i]);
3829 if (l >= sizeof(k) - 1)
3831 strlcat(k, sep, sizeof(k));
3832 strlcat(k, b->strings[i], sizeof(k));
3834 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(k);
3838 ========================
3840 get a string from buffer, returns tempstring, dont str_unzone it!
3841 string bufstr_get(float bufhandle, float string_index) = #465;
3842 ========================
3844 void VM_bufstr_get (void)
3848 VM_SAFEPARMCOUNT(2, VM_bufstr_get);
3850 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
3851 b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3854 VM_Warning("VM_bufstr_get: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3857 strindex = (int)PRVM_G_FLOAT(OFS_PARM1);
3858 if(strindex < 0 || strindex > MAX_QCSTR_STRINGS)
3860 VM_Warning("VM_bufstr_get: invalid string index %i used in %s\n", strindex, PRVM_NAME);
3863 if(b->num_strings <= strindex)
3865 if(b->strings[strindex])
3866 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(b->strings[strindex]);
3870 ========================
3872 copies a string into selected slot of buffer
3873 void bufstr_set(float bufhandle, float string_index, string str) = #466;
3874 ========================
3876 void VM_bufstr_set (void)
3878 int bufindex, strindex;
3883 VM_SAFEPARMCOUNT(3, VM_bufstr_set);
3885 bufindex = (int)PRVM_G_FLOAT(OFS_PARM0);
3886 b = BUFSTR_BUFFER(bufindex);
3889 VM_Warning("VM_bufstr_set: invalid buffer %i used in %s\n", bufindex, PRVM_NAME);
3892 strindex = (int)PRVM_G_FLOAT(OFS_PARM1);
3893 if(strindex < 0 || strindex > MAX_QCSTR_STRINGS)
3895 VM_Warning("VM_bufstr_set: invalid string index %i used in %s\n", strindex, PRVM_NAME);
3898 news = PRVM_G_STRING(OFS_PARM2);
3899 if(b->strings[strindex])
3900 Z_Free(b->strings[strindex]);
3901 alloclen = strlen(news) + 1;
3902 b->strings[strindex] = (char *)Z_Malloc(alloclen);
3903 memcpy(b->strings[strindex], news, alloclen);
3907 ========================
3909 adds string to buffer in nearest free slot and returns it
3910 "order == TRUE" means that string will be added after last "full" slot
3911 float bufstr_add(float bufhandle, string str, float order) = #467;
3912 ========================
3914 void VM_bufstr_add (void)
3916 int bufindex, order, strindex;
3921 VM_SAFEPARMCOUNT(3, VM_bufstr_add);
3923 bufindex = (int)PRVM_G_FLOAT(OFS_PARM0);
3924 b = BUFSTR_BUFFER(bufindex);
3925 PRVM_G_FLOAT(OFS_RETURN) = -1;
3928 VM_Warning("VM_bufstr_add: invalid buffer %i used in %s\n", bufindex, PRVM_NAME);
3931 string = PRVM_G_STRING(OFS_PARM1);
3932 order = (int)PRVM_G_FLOAT(OFS_PARM2);
3934 strindex = b->num_strings;
3937 strindex = BufStr_FindFreeString(b);
3940 VM_Warning("VM_bufstr_add: buffer %i has no free string slots in %s\n", bufindex, PRVM_NAME);
3945 while(b->num_strings <= strindex)
3947 if(b->num_strings == MAX_QCSTR_STRINGS)
3949 VM_Warning("VM_bufstr_add: buffer %i has no free string slots in %s\n", bufindex, PRVM_NAME);
3952 b->strings[b->num_strings] = NULL;
3955 if(b->strings[strindex])
3956 Z_Free(b->strings[strindex]);
3957 alloclen = strlen(string) + 1;
3958 b->strings[strindex] = (char *)Z_Malloc(alloclen);
3959 memcpy(b->strings[strindex], string, alloclen);
3960 PRVM_G_FLOAT(OFS_RETURN) = strindex;
3964 ========================
3966 delete string from buffer
3967 void bufstr_free(float bufhandle, float string_index) = #468;
3968 ========================
3970 void VM_bufstr_free (void)
3974 VM_SAFEPARMCOUNT(2, VM_bufstr_free);
3976 b = BUFSTR_BUFFER((int)PRVM_G_FLOAT(OFS_PARM0));
3979 VM_Warning("VM_bufstr_free: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
3982 i = (int)PRVM_G_FLOAT(OFS_PARM1);
3983 if(i < 0 || i > MAX_QCSTR_STRINGS)
3985 VM_Warning("VM_bufstr_free: invalid string index %i used in %s\n", i, PRVM_NAME);
3989 Z_Free(b->strings[i]);
3990 b->strings[i] = NULL;
3991 if(i+1 == b->num_strings)
3997 void VM_Cmd_Init(void)
3999 // only init the stuff for the current prog
4002 // VM_BufStr_Init();
4003 if(vm_polygons_initialized)
4005 Mem_FreePool(&vm_polygons_pool);
4006 vm_polygons_initialized = false;
4010 void VM_Cmd_Reset(void)
4012 CL_PurgeOwner( MENUOWNER );
4014 VM_Files_CloseAll();
4015 // VM_BufStr_ShutDown();
4016 if(vm_polygons_initialized)
4018 Mem_FreePool(&vm_polygons_pool);
4019 vm_polygons_initialized = false;