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
13 #include "cl_collision.h"
14 #include "clvm_cmds.h"
17 extern cvar_t prvm_backtraceforwarnings;
19 // LordHavoc: changed this to NOT use a return statement, so that it can be used in functions that must return a value
20 void VM_Warning(const char *fmt, ...)
23 char msg[MAX_INPUTLINE];
24 static double recursive = -1;
27 dpvsnprintf(msg,sizeof(msg),fmt,argptr);
32 // TODO: either add a cvar/cmd to control the state dumping or replace some of the calls with Con_Printf [9/13/2006 Black]
33 if(prvm_backtraceforwarnings.integer && recursive != realtime) // NOTE: this compares to the time, just in case if PRVM_PrintState causes a Host_Error and keeps recursive set
42 //============================================================================
45 // TODO DONE: move vm_files and vm_fssearchlist to prvm_prog_t struct
46 // TODO: move vm_files and vm_fssearchlist back [9/13/2006 Black]
47 // TODO: (move vm_files and vm_fssearchlist to prvm_prog_t struct again) [2007-01-23 LordHavoc]
48 // TODO: will this war ever end? [2007-01-23 LordHavoc]
50 void VM_CheckEmptyString (const char *s)
52 if (ISWHITESPACE(s[0]))
53 PRVM_ERROR ("%s: Bad string", PRVM_NAME);
56 void VM_GenerateFrameGroupBlend(framegroupblend_t *framegroupblend, const prvm_edict_t *ed)
59 // self.frame is the interpolation target (new frame)
60 // self.frame1time is the animation base time for the interpolation target
61 // self.frame2 is the interpolation start (previous frame)
62 // self.frame2time is the animation base time for the interpolation start
63 // self.lerpfrac is the interpolation strength for self.frame2
64 // self.lerpfrac3 is the interpolation strength for self.frame3
65 // self.lerpfrac4 is the interpolation strength for self.frame4
66 // pitch angle on a player model where the animator set up 5 sets of
67 // animations and the csqc simply lerps between sets)
68 if ((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.frame))) framegroupblend[0].frame = (int) val->_float;
69 if ((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.frame2))) framegroupblend[1].frame = (int) val->_float;
70 if ((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.frame3))) framegroupblend[2].frame = (int) val->_float;
71 if ((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.frame4))) framegroupblend[3].frame = (int) val->_float;
72 if ((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.frame1time))) framegroupblend[0].start = val->_float;
73 if ((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.frame2time))) framegroupblend[1].start = val->_float;
74 if ((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.frame3time))) framegroupblend[2].start = val->_float;
75 if ((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.frame4time))) framegroupblend[3].start = val->_float;
76 if ((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.lerpfrac))) framegroupblend[1].lerp = val->_float;
77 if ((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.lerpfrac3))) framegroupblend[2].lerp = val->_float;
78 if ((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.lerpfrac4))) framegroupblend[3].lerp = val->_float;
79 // assume that the (missing) lerpfrac1 is whatever remains after lerpfrac2+lerpfrac3+lerpfrac4 are summed
80 framegroupblend[0].lerp = 1 - framegroupblend[1].lerp - framegroupblend[2].lerp - framegroupblend[3].lerp;
83 // LordHavoc: quite tempting to break apart this function to reuse the
84 // duplicated code, but I suspect it is better for performance
86 void VM_FrameBlendFromFrameGroupBlend(frameblend_t *frameblend, const framegroupblend_t *framegroupblend, const dp_model_t *model)
88 int sub2, numframes, f, i, k;
89 int isfirstframegroup = true;
91 double sublerp, lerp, d;
92 const animscene_t *scene;
93 const framegroupblend_t *g;
94 frameblend_t *blend = frameblend;
96 memset(blend, 0, MAX_FRAMEBLENDS * sizeof(*blend));
98 if (!model || !model->surfmesh.isanimated)
104 nolerp = (model->type == mod_sprite) ? !r_lerpsprites.integer : !r_lerpmodels.integer;
105 numframes = model->numframes;
106 for (k = 0, g = framegroupblend;k < MAX_FRAMEGROUPBLENDS;k++, g++)
109 if ((unsigned int)f >= (unsigned int)numframes)
111 Con_DPrintf("VM_FrameBlendFromFrameGroupBlend: no such frame %d in model %s\n", f, model->name);
119 if (isfirstframegroup)
122 isfirstframegroup = false;
127 if (model->animscenes)
129 scene = model->animscenes + f;
130 f = scene->firstframe;
131 if (scene->framecount > 1)
133 // this code path is only used on .zym models and torches
134 sublerp = scene->framerate * (cl.time - g->start);
135 f = (int) floor(sublerp);
138 if (sublerp < (1.0 / 65536.0f))
140 if (sublerp > (65535.0f / 65536.0f))
146 f = (f % scene->framecount);
147 sub2 = (sub2 % scene->framecount);
149 f = bound(0, f, (scene->framecount - 1)) + scene->firstframe;
150 sub2 = bound(0, sub2, (scene->framecount - 1)) + scene->firstframe;
152 // two framelerps produced from one animation
155 for (i = 0;i < MAX_FRAMEBLENDS;i++)
157 if (blend[i].lerp <= 0 || blend[i].subframe == sub2)
159 blend[i].subframe = sub2;
165 d = (1 - sublerp) * lerp;
170 for (i = 0;i < MAX_FRAMEBLENDS;i++)
172 if (blend[i].lerp <= 0 || blend[i].subframe == f)
174 blend[i].subframe = f;
183 void VM_UpdateEdictSkeleton(prvm_edict_t *ed, const dp_model_t *edmodel, const frameblend_t *frameblend)
185 if (ed->priv.server->skeleton.model != edmodel)
187 VM_RemoveEdictSkeleton(ed);
188 ed->priv.server->skeleton.model = edmodel;
190 if (!ed->priv.server->skeleton.relativetransforms && ed->priv.server->skeleton.model && ed->priv.server->skeleton.model->num_bones)
191 ed->priv.server->skeleton.relativetransforms = Mem_Alloc(prog->progs_mempool, ed->priv.server->skeleton.model->num_bones * sizeof(matrix4x4_t));
192 if (ed->priv.server->skeleton.relativetransforms)
194 int skeletonindex = -1;
195 skeleton_t *skeleton;
197 if ((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.skeletonindex))) skeletonindex = (int)val->_float - 1;
198 if (skeletonindex >= 0 && skeletonindex < MAX_EDICTS && (skeleton = prog->skeletons[skeletonindex]) && skeleton->model->num_bones == ed->priv.server->skeleton.model->num_bones)
200 // custom skeleton controlled by the game (FTE_CSQC_SKELETONOBJECTS)
201 memcpy(ed->priv.server->skeleton.relativetransforms, skeleton->relativetransforms, ed->priv.server->skeleton.model->num_bones * sizeof(matrix4x4_t));
205 // generated skeleton from frame animation
208 int numbones = ed->priv.server->skeleton.model->num_bones;
209 const short *framebones6s;
211 float scale = ed->priv.server->skeleton.model->num_posescale;
212 matrix4x4_t *relativetransforms = ed->priv.server->skeleton.relativetransforms;
214 memset(relativetransforms, 0, numbones * sizeof(matrix4x4_t));
215 for (blendindex = 0;blendindex < MAX_FRAMEBLENDS && frameblend[blendindex].lerp > 0;blendindex++)
217 lerp = frameblend[blendindex].lerp;
218 framebones6s = ed->priv.server->skeleton.model->data_poses6s + 6 * frameblend[blendindex].subframe * numbones;
219 for (bonenum = 0;bonenum < numbones;bonenum++)
221 Matrix4x4_FromBonePose6s(&matrix, scale, framebones6s + 6 * bonenum);
222 Matrix4x4_Accumulate(&ed->priv.server->skeleton.relativetransforms[bonenum], &matrix, lerp);
229 void VM_RemoveEdictSkeleton(prvm_edict_t *ed)
231 if (ed->priv.server->skeleton.relativetransforms)
232 Mem_Free(ed->priv.server->skeleton.relativetransforms);
233 memset(&ed->priv.server->skeleton, 0, sizeof(ed->priv.server->skeleton));
239 //============================================================================
242 void VM_VarString(int first, char *out, int outlength)
248 outend = out + outlength - 1;
249 for (i = first;i < prog->argc && out < outend;i++)
251 s = PRVM_G_STRING((OFS_PARM0+i*3));
252 while (out < outend && *s)
262 returns true if the extension is supported by the server
264 checkextension(extensionname)
268 // kind of helper function
269 static qboolean checkextension(const char *name)
273 len = (int)strlen(name);
275 for (e = prog->extensionstring;*e;e++)
282 while (*e && *e != ' ')
284 if ((e - start) == len && !strncasecmp(start, name, len))
290 void VM_checkextension (void)
292 VM_SAFEPARMCOUNT(1,VM_checkextension);
294 PRVM_G_FLOAT(OFS_RETURN) = checkextension(PRVM_G_STRING(OFS_PARM0));
301 This is a TERMINAL error, which will kill off the entire prog.
310 char string[VM_STRINGTEMP_LENGTH];
312 VM_VarString(0, string, sizeof(string));
313 Con_Printf("======%s ERROR in %s:\n%s\n", PRVM_NAME, PRVM_GetString(prog->xfunction->s_name), string);
314 if (prog->globaloffsets.self >= 0)
316 ed = PRVM_PROG_TO_EDICT(PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict);
317 PRVM_ED_Print(ed, NULL);
320 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);
327 Dumps out self, then an error message. The program is aborted and self is
328 removed, but the level can continue.
333 void VM_objerror (void)
336 char string[VM_STRINGTEMP_LENGTH];
338 VM_VarString(0, string, sizeof(string));
339 Con_Printf("======OBJECT ERROR======\n"); // , PRVM_NAME, PRVM_GetString(prog->xfunction->s_name), string); // or include them? FIXME
340 if (prog->globaloffsets.self >= 0)
342 ed = PRVM_PROG_TO_EDICT(PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict);
343 PRVM_ED_Print(ed, NULL);
348 // objerror has to display the object fields -> else call
349 PRVM_ERROR ("VM_objecterror: self not defined !");
350 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);
364 char string[VM_STRINGTEMP_LENGTH];
366 VM_VarString(0, string, sizeof(string));
374 broadcast print to everyone on server
379 void VM_bprint (void)
381 char string[VM_STRINGTEMP_LENGTH];
385 VM_Warning("VM_bprint: game is not server(%s) !\n", PRVM_NAME);
389 VM_VarString(0, string, sizeof(string));
390 SV_BroadcastPrint(string);
395 VM_sprint (menu & client but only if server.active == true)
397 single print to a specific client
399 sprint(float clientnum,...[string])
402 void VM_sprint (void)
406 char string[VM_STRINGTEMP_LENGTH];
408 VM_SAFEPARMCOUNTRANGE(1, 8, VM_sprint);
410 //find client for this entity
411 clientnum = (int)PRVM_G_FLOAT(OFS_PARM0);
412 if (!sv.active || clientnum < 0 || clientnum >= svs.maxclients || !svs.clients[clientnum].active)
414 VM_Warning("VM_sprint: %s: invalid client or server is not active !\n", PRVM_NAME);
418 client = svs.clients + clientnum;
419 if (!client->netconnection)
422 VM_VarString(1, string, sizeof(string));
423 MSG_WriteChar(&client->netconnection->message,svc_print);
424 MSG_WriteString(&client->netconnection->message, string);
431 single print to the screen
436 void VM_centerprint (void)
438 char string[VM_STRINGTEMP_LENGTH];
440 VM_SAFEPARMCOUNTRANGE(1, 8, VM_centerprint);
441 VM_VarString(0, string, sizeof(string));
442 SCR_CenterPrint(string);
449 vector normalize(vector)
452 void VM_normalize (void)
458 VM_SAFEPARMCOUNT(1,VM_normalize);
460 value1 = PRVM_G_VECTOR(OFS_PARM0);
462 f = VectorLength2(value1);
466 VectorScale(value1, f, newvalue);
469 VectorClear(newvalue);
471 VectorCopy (newvalue, PRVM_G_VECTOR(OFS_RETURN));
483 VM_SAFEPARMCOUNT(1,VM_vlen);
484 PRVM_G_FLOAT(OFS_RETURN) = VectorLength(PRVM_G_VECTOR(OFS_PARM0));
491 float vectoyaw(vector)
494 void VM_vectoyaw (void)
499 VM_SAFEPARMCOUNT(1,VM_vectoyaw);
501 value1 = PRVM_G_VECTOR(OFS_PARM0);
503 if (value1[1] == 0 && value1[0] == 0)
507 yaw = (int) (atan2(value1[1], value1[0]) * 180 / M_PI);
512 PRVM_G_FLOAT(OFS_RETURN) = yaw;
520 vector vectoangles(vector[, vector])
523 void VM_vectoangles (void)
525 VM_SAFEPARMCOUNTRANGE(1, 2,VM_vectoangles);
527 AnglesFromVectors(PRVM_G_VECTOR(OFS_RETURN), PRVM_G_VECTOR(OFS_PARM0), prog->argc >= 2 ? PRVM_G_VECTOR(OFS_PARM1) : NULL, true);
534 Returns a number from 0<= num < 1
539 void VM_random (void)
541 VM_SAFEPARMCOUNT(0,VM_random);
543 PRVM_G_FLOAT(OFS_RETURN) = lhrandom(0, 1);
550 localsound(string sample)
553 void VM_localsound(void)
557 VM_SAFEPARMCOUNT(1,VM_localsound);
559 s = PRVM_G_STRING(OFS_PARM0);
561 if(!S_LocalSound (s))
563 PRVM_G_FLOAT(OFS_RETURN) = -4;
564 VM_Warning("VM_localsound: Failed to play %s for %s !\n", s, PRVM_NAME);
568 PRVM_G_FLOAT(OFS_RETURN) = 1;
580 PRVM_ERROR ("%s: break statement", PRVM_NAME);
583 //============================================================================
589 Sends text over to the client's execution buffer
591 [localcmd (string, ...) or]
595 void VM_localcmd (void)
597 char string[VM_STRINGTEMP_LENGTH];
598 VM_SAFEPARMCOUNTRANGE(1, 8, VM_localcmd);
599 VM_VarString(0, string, sizeof(string));
600 Cbuf_AddText(string);
603 static qboolean PRVM_Cvar_ReadOk(const char *string)
606 cvar = Cvar_FindVar(string);
607 return ((cvar) && ((cvar->flags & CVAR_PRIVATE) == 0));
619 char string[VM_STRINGTEMP_LENGTH];
620 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar);
621 VM_VarString(0, string, sizeof(string));
622 VM_CheckEmptyString(string);
623 PRVM_G_FLOAT(OFS_RETURN) = PRVM_Cvar_ReadOk(string) ? Cvar_VariableValue(string) : 0;
630 float cvar_type (string)
631 float CVAR_TYPEFLAG_EXISTS = 1;
632 float CVAR_TYPEFLAG_SAVED = 2;
633 float CVAR_TYPEFLAG_PRIVATE = 4;
634 float CVAR_TYPEFLAG_ENGINE = 8;
635 float CVAR_TYPEFLAG_HASDESCRIPTION = 16;
636 float CVAR_TYPEFLAG_READONLY = 32;
639 void VM_cvar_type (void)
641 char string[VM_STRINGTEMP_LENGTH];
645 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar);
646 VM_VarString(0, string, sizeof(string));
647 VM_CheckEmptyString(string);
648 cvar = Cvar_FindVar(string);
653 PRVM_G_FLOAT(OFS_RETURN) = 0;
654 return; // CVAR_TYPE_NONE
657 ret = 1; // CVAR_EXISTS
658 if(cvar->flags & CVAR_SAVE)
659 ret |= 2; // CVAR_TYPE_SAVED
660 if(cvar->flags & CVAR_PRIVATE)
661 ret |= 4; // CVAR_TYPE_PRIVATE
662 if(!(cvar->flags & CVAR_ALLOCATED))
663 ret |= 8; // CVAR_TYPE_ENGINE
664 if(cvar->description != cvar_dummy_description)
665 ret |= 16; // CVAR_TYPE_HASDESCRIPTION
666 if(cvar->flags & CVAR_READONLY)
667 ret |= 32; // CVAR_TYPE_READONLY
669 PRVM_G_FLOAT(OFS_RETURN) = ret;
676 const string VM_cvar_string (string, ...)
679 void VM_cvar_string(void)
681 char string[VM_STRINGTEMP_LENGTH];
682 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar_string);
683 VM_VarString(0, string, sizeof(string));
684 VM_CheckEmptyString(string);
685 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(PRVM_Cvar_ReadOk(string) ? Cvar_VariableString(string) : "");
690 ========================
693 const string VM_cvar_defstring (string, ...)
694 ========================
696 void VM_cvar_defstring (void)
698 char string[VM_STRINGTEMP_LENGTH];
699 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar_defstring);
700 VM_VarString(0, string, sizeof(string));
701 VM_CheckEmptyString(string);
702 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(Cvar_VariableDefString(string));
706 ========================
709 const string VM_cvar_description (string, ...)
710 ========================
712 void VM_cvar_description (void)
714 char string[VM_STRINGTEMP_LENGTH];
715 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar_description);
716 VM_VarString(0, string, sizeof(string));
717 VM_CheckEmptyString(string);
718 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(Cvar_VariableDescription(string));
724 void cvar_set (string,string, ...)
727 void VM_cvar_set (void)
730 char string[VM_STRINGTEMP_LENGTH];
731 VM_SAFEPARMCOUNTRANGE(2,8,VM_cvar_set);
732 VM_VarString(1, string, sizeof(string));
733 name = PRVM_G_STRING(OFS_PARM0);
734 VM_CheckEmptyString(name);
735 Cvar_Set(name, string);
745 void VM_dprint (void)
747 char string[VM_STRINGTEMP_LENGTH];
748 VM_SAFEPARMCOUNTRANGE(1, 8, VM_dprint);
749 VM_VarString(0, string, sizeof(string));
751 Con_DPrintf("%s", string);
753 Con_DPrintf("%s: %s", PRVM_NAME, string);
770 VM_SAFEPARMCOUNT(1, VM_ftos);
772 v = PRVM_G_FLOAT(OFS_PARM0);
774 if ((float)((int)v) == v)
775 dpsnprintf(s, sizeof(s), "%i", (int)v);
777 dpsnprintf(s, sizeof(s), "%f", v);
778 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(s);
793 VM_SAFEPARMCOUNT(1,VM_fabs);
795 v = PRVM_G_FLOAT(OFS_PARM0);
796 PRVM_G_FLOAT(OFS_RETURN) = fabs(v);
811 VM_SAFEPARMCOUNT(1,VM_vtos);
813 dpsnprintf (s, sizeof(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]);
814 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(s);
829 VM_SAFEPARMCOUNT(1, VM_etos);
831 dpsnprintf (s, sizeof(s), "entity %i", PRVM_G_EDICTNUM(OFS_PARM0));
832 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(s);
839 float stof(...[string])
844 char string[VM_STRINGTEMP_LENGTH];
845 VM_SAFEPARMCOUNTRANGE(1, 8, VM_stof);
846 VM_VarString(0, string, sizeof(string));
847 PRVM_G_FLOAT(OFS_RETURN) = atof(string);
851 ========================
855 ========================
859 VM_SAFEPARMCOUNT(1, VM_itof);
860 PRVM_G_FLOAT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
864 ========================
867 entity ftoe(float num)
868 ========================
873 VM_SAFEPARMCOUNT(1, VM_ftoe);
875 ent = (int)PRVM_G_FLOAT(OFS_PARM0);
876 if (ent < 0 || ent >= MAX_EDICTS || PRVM_PROG_TO_EDICT(ent)->priv.required->free)
877 ent = 0; // return world instead of a free or invalid entity
879 PRVM_G_INT(OFS_RETURN) = ent;
883 ========================
886 float etof(entity ent)
887 ========================
891 VM_SAFEPARMCOUNT(1, VM_etof);
892 PRVM_G_FLOAT(OFS_RETURN) = PRVM_G_EDICTNUM(OFS_PARM0);
899 string strftime(float uselocaltime, string[, string ...])
902 void VM_strftime(void)
911 char fmt[VM_STRINGTEMP_LENGTH];
912 char result[VM_STRINGTEMP_LENGTH];
913 VM_SAFEPARMCOUNTRANGE(2, 8, VM_strftime);
914 VM_VarString(1, fmt, sizeof(fmt));
917 if (PRVM_G_FLOAT(OFS_PARM0))
918 tmresult = localtime_s(&tm, &t);
920 tmresult = gmtime_s(&tm, &t);
923 if (PRVM_G_FLOAT(OFS_PARM0))
930 PRVM_G_INT(OFS_RETURN) = 0;
934 strftime(result, sizeof(result), fmt, &tm);
936 strftime(result, sizeof(result), fmt, tm);
938 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(result);
952 VM_SAFEPARMCOUNT(0, VM_spawn);
953 prog->xfunction->builtinsprofile += 20;
954 ed = PRVM_ED_Alloc();
966 void VM_remove (void)
969 prog->xfunction->builtinsprofile += 20;
971 VM_SAFEPARMCOUNT(1, VM_remove);
973 ed = PRVM_G_EDICT(OFS_PARM0);
974 if( PRVM_NUM_FOR_EDICT(ed) <= prog->reserved_edicts )
976 if (developer.integer > 0)
977 VM_Warning( "VM_remove: tried to remove the null entity or a reserved entity!\n" );
979 else if( ed->priv.required->free )
981 if (developer.integer > 0)
982 VM_Warning( "VM_remove: tried to remove an already freed entity!\n" );
992 entity find(entity start, .string field, string match)
1003 VM_SAFEPARMCOUNT(3,VM_find);
1005 e = PRVM_G_EDICTNUM(OFS_PARM0);
1006 f = PRVM_G_INT(OFS_PARM1);
1007 s = PRVM_G_STRING(OFS_PARM2);
1009 // LordHavoc: apparently BloodMage does a find(world, weaponmodel, "") and
1010 // expects it to find all the monsters, so we must be careful to support
1013 for (e++ ; e < prog->num_edicts ; e++)
1015 prog->xfunction->builtinsprofile++;
1016 ed = PRVM_EDICT_NUM(e);
1017 if (ed->priv.required->free)
1019 t = PRVM_E_STRING(ed,f);
1024 VM_RETURN_EDICT(ed);
1029 VM_RETURN_EDICT(prog->edicts);
1036 entity findfloat(entity start, .float field, float match)
1037 entity findentity(entity start, .entity field, entity match)
1040 // LordHavoc: added this for searching float, int, and entity reference fields
1041 void VM_findfloat (void)
1048 VM_SAFEPARMCOUNT(3,VM_findfloat);
1050 e = PRVM_G_EDICTNUM(OFS_PARM0);
1051 f = PRVM_G_INT(OFS_PARM1);
1052 s = PRVM_G_FLOAT(OFS_PARM2);
1054 for (e++ ; e < prog->num_edicts ; e++)
1056 prog->xfunction->builtinsprofile++;
1057 ed = PRVM_EDICT_NUM(e);
1058 if (ed->priv.required->free)
1060 if (PRVM_E_FLOAT(ed,f) == s)
1062 VM_RETURN_EDICT(ed);
1067 VM_RETURN_EDICT(prog->edicts);
1074 entity findchain(.string field, string match)
1077 // chained search for strings in entity fields
1078 // entity(.string field, string match) findchain = #402;
1079 void VM_findchain (void)
1084 prvm_edict_t *ent, *chain;
1087 VM_SAFEPARMCOUNTRANGE(2,3,VM_findchain);
1090 chainfield = PRVM_G_INT(OFS_PARM2);
1092 chainfield = prog->fieldoffsets.chain;
1094 PRVM_ERROR("VM_findchain: %s doesnt have the specified chain field !", PRVM_NAME);
1096 chain = prog->edicts;
1098 f = PRVM_G_INT(OFS_PARM0);
1099 s = PRVM_G_STRING(OFS_PARM1);
1101 // LordHavoc: apparently BloodMage does a find(world, weaponmodel, "") and
1102 // expects it to find all the monsters, so we must be careful to support
1105 ent = PRVM_NEXT_EDICT(prog->edicts);
1106 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
1108 prog->xfunction->builtinsprofile++;
1109 if (ent->priv.required->free)
1111 t = PRVM_E_STRING(ent,f);
1117 PRVM_EDICTFIELDVALUE(ent,chainfield)->edict = PRVM_NUM_FOR_EDICT(chain);
1121 VM_RETURN_EDICT(chain);
1128 entity findchainfloat(.string field, float match)
1129 entity findchainentity(.string field, entity match)
1132 // LordHavoc: chained search for float, int, and entity reference fields
1133 // entity(.string field, float match) findchainfloat = #403;
1134 void VM_findchainfloat (void)
1139 prvm_edict_t *ent, *chain;
1142 VM_SAFEPARMCOUNTRANGE(2, 3, VM_findchainfloat);
1145 chainfield = PRVM_G_INT(OFS_PARM2);
1147 chainfield = prog->fieldoffsets.chain;
1149 PRVM_ERROR("VM_findchain: %s doesnt have the specified chain field !", PRVM_NAME);
1151 chain = (prvm_edict_t *)prog->edicts;
1153 f = PRVM_G_INT(OFS_PARM0);
1154 s = PRVM_G_FLOAT(OFS_PARM1);
1156 ent = PRVM_NEXT_EDICT(prog->edicts);
1157 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
1159 prog->xfunction->builtinsprofile++;
1160 if (ent->priv.required->free)
1162 if (PRVM_E_FLOAT(ent,f) != s)
1165 PRVM_EDICTFIELDVALUE(ent,chainfield)->edict = PRVM_EDICT_TO_PROG(chain);
1169 VM_RETURN_EDICT(chain);
1173 ========================
1176 entity findflags(entity start, .float field, float match)
1177 ========================
1179 // LordHavoc: search for flags in float fields
1180 void VM_findflags (void)
1187 VM_SAFEPARMCOUNT(3, VM_findflags);
1190 e = PRVM_G_EDICTNUM(OFS_PARM0);
1191 f = PRVM_G_INT(OFS_PARM1);
1192 s = (int)PRVM_G_FLOAT(OFS_PARM2);
1194 for (e++ ; e < prog->num_edicts ; e++)
1196 prog->xfunction->builtinsprofile++;
1197 ed = PRVM_EDICT_NUM(e);
1198 if (ed->priv.required->free)
1200 if (!PRVM_E_FLOAT(ed,f))
1202 if ((int)PRVM_E_FLOAT(ed,f) & s)
1204 VM_RETURN_EDICT(ed);
1209 VM_RETURN_EDICT(prog->edicts);
1213 ========================
1216 entity findchainflags(.float field, float match)
1217 ========================
1219 // LordHavoc: chained search for flags in float fields
1220 void VM_findchainflags (void)
1225 prvm_edict_t *ent, *chain;
1228 VM_SAFEPARMCOUNTRANGE(2, 3, VM_findchainflags);
1231 chainfield = PRVM_G_INT(OFS_PARM2);
1233 chainfield = prog->fieldoffsets.chain;
1235 PRVM_ERROR("VM_findchain: %s doesnt have the specified chain field !", PRVM_NAME);
1237 chain = (prvm_edict_t *)prog->edicts;
1239 f = PRVM_G_INT(OFS_PARM0);
1240 s = (int)PRVM_G_FLOAT(OFS_PARM1);
1242 ent = PRVM_NEXT_EDICT(prog->edicts);
1243 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
1245 prog->xfunction->builtinsprofile++;
1246 if (ent->priv.required->free)
1248 if (!PRVM_E_FLOAT(ent,f))
1250 if (!((int)PRVM_E_FLOAT(ent,f) & s))
1253 PRVM_EDICTFIELDVALUE(ent,chainfield)->edict = PRVM_EDICT_TO_PROG(chain);
1257 VM_RETURN_EDICT(chain);
1264 string precache_sound (string sample)
1267 void VM_precache_sound (void)
1271 VM_SAFEPARMCOUNT(1, VM_precache_sound);
1273 s = PRVM_G_STRING(OFS_PARM0);
1274 PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
1275 //VM_CheckEmptyString(s);
1277 if(snd_initialized.integer && !S_PrecacheSound(s, true, true))
1279 VM_Warning("VM_precache_sound: Failed to load %s for %s\n", s, PRVM_NAME);
1288 returns the same string as output
1290 does nothing, only used by qcc to build .pak archives
1293 void VM_precache_file (void)
1295 VM_SAFEPARMCOUNT(1,VM_precache_file);
1296 // precache_file is only used to copy files with qcc, it does nothing
1297 PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
1307 void VM_coredump (void)
1309 VM_SAFEPARMCOUNT(0,VM_coredump);
1311 Cbuf_AddText("prvm_edicts ");
1312 Cbuf_AddText(PRVM_NAME);
1323 void PRVM_StackTrace(void);
1324 void VM_stackdump (void)
1326 VM_SAFEPARMCOUNT(0, VM_stackdump);
1341 VM_SAFEPARMCOUNT(0, VM_crash);
1343 PRVM_ERROR("Crash called by %s",PRVM_NAME);
1353 void VM_traceon (void)
1355 VM_SAFEPARMCOUNT(0,VM_traceon);
1367 void VM_traceoff (void)
1369 VM_SAFEPARMCOUNT(0,VM_traceoff);
1371 prog->trace = false;
1381 void VM_eprint (void)
1383 VM_SAFEPARMCOUNT(1,VM_eprint);
1385 PRVM_ED_PrintNum (PRVM_G_EDICTNUM(OFS_PARM0), NULL);
1398 VM_SAFEPARMCOUNT(1,VM_rint);
1400 f = PRVM_G_FLOAT(OFS_PARM0);
1402 PRVM_G_FLOAT(OFS_RETURN) = floor(f + 0.5);
1404 PRVM_G_FLOAT(OFS_RETURN) = ceil(f - 0.5);
1414 void VM_floor (void)
1416 VM_SAFEPARMCOUNT(1,VM_floor);
1418 PRVM_G_FLOAT(OFS_RETURN) = floor(PRVM_G_FLOAT(OFS_PARM0));
1430 VM_SAFEPARMCOUNT(1,VM_ceil);
1432 PRVM_G_FLOAT(OFS_RETURN) = ceil(PRVM_G_FLOAT(OFS_PARM0));
1440 entity nextent(entity)
1443 void VM_nextent (void)
1448 VM_SAFEPARMCOUNT(1, VM_nextent);
1450 i = PRVM_G_EDICTNUM(OFS_PARM0);
1453 prog->xfunction->builtinsprofile++;
1455 if (i == prog->num_edicts)
1457 VM_RETURN_EDICT(prog->edicts);
1460 ent = PRVM_EDICT_NUM(i);
1461 if (!ent->priv.required->free)
1463 VM_RETURN_EDICT(ent);
1469 //=============================================================================
1476 changelevel(string map)
1479 void VM_changelevel (void)
1481 VM_SAFEPARMCOUNT(1, VM_changelevel);
1485 VM_Warning("VM_changelevel: game is not server (%s)\n", PRVM_NAME);
1489 // make sure we don't issue two changelevels
1490 if (svs.changelevel_issued)
1492 svs.changelevel_issued = true;
1494 Cbuf_AddText (va("changelevel %s\n",PRVM_G_STRING(OFS_PARM0)));
1506 VM_SAFEPARMCOUNT(1,VM_sin);
1507 PRVM_G_FLOAT(OFS_RETURN) = sin(PRVM_G_FLOAT(OFS_PARM0));
1518 VM_SAFEPARMCOUNT(1,VM_cos);
1519 PRVM_G_FLOAT(OFS_RETURN) = cos(PRVM_G_FLOAT(OFS_PARM0));
1531 VM_SAFEPARMCOUNT(1,VM_sqrt);
1532 PRVM_G_FLOAT(OFS_RETURN) = sqrt(PRVM_G_FLOAT(OFS_PARM0));
1544 VM_SAFEPARMCOUNT(1,VM_asin);
1545 PRVM_G_FLOAT(OFS_RETURN) = asin(PRVM_G_FLOAT(OFS_PARM0));
1556 VM_SAFEPARMCOUNT(1,VM_acos);
1557 PRVM_G_FLOAT(OFS_RETURN) = acos(PRVM_G_FLOAT(OFS_PARM0));
1568 VM_SAFEPARMCOUNT(1,VM_atan);
1569 PRVM_G_FLOAT(OFS_RETURN) = atan(PRVM_G_FLOAT(OFS_PARM0));
1575 float atan2(float,float)
1578 void VM_atan2 (void)
1580 VM_SAFEPARMCOUNT(2,VM_atan2);
1581 PRVM_G_FLOAT(OFS_RETURN) = atan2(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1592 VM_SAFEPARMCOUNT(1,VM_tan);
1593 PRVM_G_FLOAT(OFS_RETURN) = tan(PRVM_G_FLOAT(OFS_PARM0));
1600 Returns a vector of length < 1 and > 0
1605 void VM_randomvec (void)
1610 VM_SAFEPARMCOUNT(0, VM_randomvec);
1615 temp[0] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1616 temp[1] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1617 temp[2] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1619 while (DotProduct(temp, temp) >= 1);
1620 VectorCopy (temp, PRVM_G_VECTOR(OFS_RETURN));
1623 temp[0] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1624 temp[1] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1625 temp[2] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1626 // length returned always > 0
1627 length = (rand()&32766 + 1) * (1.0 / 32767.0) / VectorLength(temp);
1628 VectorScale(temp,length, temp);*/
1629 //VectorCopy(temp, PRVM_G_VECTOR(OFS_RETURN));
1632 //=============================================================================
1638 float registercvar (string name, string value[, float flags])
1641 void VM_registercvar (void)
1643 const char *name, *value;
1646 VM_SAFEPARMCOUNTRANGE(2, 3, VM_registercvar);
1648 name = PRVM_G_STRING(OFS_PARM0);
1649 value = PRVM_G_STRING(OFS_PARM1);
1650 flags = prog->argc >= 3 ? (int)PRVM_G_FLOAT(OFS_PARM2) : 0;
1651 PRVM_G_FLOAT(OFS_RETURN) = 0;
1653 if(flags > CVAR_MAXFLAGSVAL)
1656 // first check to see if it has already been defined
1657 if (Cvar_FindVar (name))
1660 // check for overlap with a command
1661 if (Cmd_Exists (name))
1663 VM_Warning("VM_registercvar: %s is a command\n", name);
1667 Cvar_Get(name, value, flags, NULL);
1669 PRVM_G_FLOAT(OFS_RETURN) = 1; // success
1677 returns the minimum of two supplied floats
1679 float min(float a, float b, ...[float])
1684 VM_SAFEPARMCOUNTRANGE(2, 8, VM_min);
1685 // LordHavoc: 3+ argument enhancement suggested by FrikaC
1686 if (prog->argc >= 3)
1689 float f = PRVM_G_FLOAT(OFS_PARM0);
1690 for (i = 1;i < prog->argc;i++)
1691 if (f > PRVM_G_FLOAT((OFS_PARM0+i*3)))
1692 f = PRVM_G_FLOAT((OFS_PARM0+i*3));
1693 PRVM_G_FLOAT(OFS_RETURN) = f;
1696 PRVM_G_FLOAT(OFS_RETURN) = min(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1703 returns the maximum of two supplied floats
1705 float max(float a, float b, ...[float])
1710 VM_SAFEPARMCOUNTRANGE(2, 8, VM_max);
1711 // LordHavoc: 3+ argument enhancement suggested by FrikaC
1712 if (prog->argc >= 3)
1715 float f = PRVM_G_FLOAT(OFS_PARM0);
1716 for (i = 1;i < prog->argc;i++)
1717 if (f < PRVM_G_FLOAT((OFS_PARM0+i*3)))
1718 f = PRVM_G_FLOAT((OFS_PARM0+i*3));
1719 PRVM_G_FLOAT(OFS_RETURN) = f;
1722 PRVM_G_FLOAT(OFS_RETURN) = max(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1729 returns number bounded by supplied range
1731 float bound(float min, float value, float max)
1734 void VM_bound (void)
1736 VM_SAFEPARMCOUNT(3,VM_bound);
1737 PRVM_G_FLOAT(OFS_RETURN) = bound(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1), PRVM_G_FLOAT(OFS_PARM2));
1744 returns a raised to power b
1746 float pow(float a, float b)
1751 VM_SAFEPARMCOUNT(2,VM_pow);
1752 PRVM_G_FLOAT(OFS_RETURN) = pow(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1757 VM_SAFEPARMCOUNT(1,VM_log);
1758 PRVM_G_FLOAT(OFS_RETURN) = log(PRVM_G_FLOAT(OFS_PARM0));
1761 void VM_Files_Init(void)
1764 for (i = 0;i < PRVM_MAX_OPENFILES;i++)
1765 prog->openfiles[i] = NULL;
1768 void VM_Files_CloseAll(void)
1771 for (i = 0;i < PRVM_MAX_OPENFILES;i++)
1773 if (prog->openfiles[i])
1774 FS_Close(prog->openfiles[i]);
1775 prog->openfiles[i] = NULL;
1779 static qfile_t *VM_GetFileHandle( int index )
1781 if (index < 0 || index >= PRVM_MAX_OPENFILES)
1783 Con_Printf("VM_GetFileHandle: invalid file handle %i used in %s\n", index, PRVM_NAME);
1786 if (prog->openfiles[index] == NULL)
1788 Con_Printf("VM_GetFileHandle: no such file handle %i (or file has been closed) in %s\n", index, PRVM_NAME);
1791 return prog->openfiles[index];
1798 float fopen(string filename, float mode)
1801 // float(string filename, float mode) fopen = #110;
1802 // opens a file inside quake/gamedir/data/ (mode is FILE_READ, FILE_APPEND, or FILE_WRITE),
1803 // returns fhandle >= 0 if successful, or fhandle < 0 if unable to open file for any reason
1807 const char *modestring, *filename;
1809 VM_SAFEPARMCOUNT(2,VM_fopen);
1811 for (filenum = 0;filenum < PRVM_MAX_OPENFILES;filenum++)
1812 if (prog->openfiles[filenum] == NULL)
1814 if (filenum >= PRVM_MAX_OPENFILES)
1816 PRVM_G_FLOAT(OFS_RETURN) = -2;
1817 VM_Warning("VM_fopen: %s ran out of file handles (%i)\n", PRVM_NAME, PRVM_MAX_OPENFILES);
1820 filename = PRVM_G_STRING(OFS_PARM0);
1821 mode = (int)PRVM_G_FLOAT(OFS_PARM1);
1824 case 0: // FILE_READ
1826 prog->openfiles[filenum] = FS_OpenVirtualFile(va("data/%s", filename), false);
1827 if (prog->openfiles[filenum] == NULL)
1828 prog->openfiles[filenum] = FS_OpenVirtualFile(va("%s", filename), false);
1830 case 1: // FILE_APPEND
1832 prog->openfiles[filenum] = FS_OpenRealFile(va("data/%s", filename), modestring, false);
1834 case 2: // FILE_WRITE
1836 prog->openfiles[filenum] = FS_OpenRealFile(va("data/%s", filename), modestring, false);
1839 PRVM_G_FLOAT(OFS_RETURN) = -3;
1840 VM_Warning("VM_fopen: %s: no such mode %i (valid: 0 = read, 1 = append, 2 = write)\n", PRVM_NAME, mode);
1844 if (prog->openfiles[filenum] == NULL)
1846 PRVM_G_FLOAT(OFS_RETURN) = -1;
1847 if (developer_extra.integer)
1848 VM_Warning("VM_fopen: %s: %s mode %s failed\n", PRVM_NAME, filename, modestring);
1852 PRVM_G_FLOAT(OFS_RETURN) = filenum;
1853 if (developer_extra.integer)
1854 Con_DPrintf("VM_fopen: %s: %s mode %s opened as #%i\n", PRVM_NAME, filename, modestring, filenum);
1855 prog->openfiles_origin[filenum] = PRVM_AllocationOrigin();
1863 fclose(float fhandle)
1866 //void(float fhandle) fclose = #111; // closes a file
1867 void VM_fclose(void)
1871 VM_SAFEPARMCOUNT(1,VM_fclose);
1873 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1874 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
1876 VM_Warning("VM_fclose: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1879 if (prog->openfiles[filenum] == NULL)
1881 VM_Warning("VM_fclose: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1884 FS_Close(prog->openfiles[filenum]);
1885 prog->openfiles[filenum] = NULL;
1886 if(prog->openfiles_origin[filenum])
1887 PRVM_Free((char *)prog->openfiles_origin[filenum]);
1888 if (developer_extra.integer)
1889 Con_DPrintf("VM_fclose: %s: #%i closed\n", PRVM_NAME, filenum);
1896 string fgets(float fhandle)
1899 //string(float fhandle) fgets = #112; // reads a line of text from the file and returns as a tempstring
1903 char string[VM_STRINGTEMP_LENGTH];
1906 VM_SAFEPARMCOUNT(1,VM_fgets);
1908 // set the return value regardless of any possible errors
1909 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
1911 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1912 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
1914 VM_Warning("VM_fgets: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1917 if (prog->openfiles[filenum] == NULL)
1919 VM_Warning("VM_fgets: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1925 c = FS_Getc(prog->openfiles[filenum]);
1926 if (c == '\r' || c == '\n' || c < 0)
1928 if (end < VM_STRINGTEMP_LENGTH - 1)
1932 // remove \n following \r
1935 c = FS_Getc(prog->openfiles[filenum]);
1937 FS_UnGetc(prog->openfiles[filenum], (unsigned char)c);
1939 if (developer_extra.integer)
1940 Con_DPrintf("fgets: %s: %s\n", PRVM_NAME, string);
1942 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string);
1949 fputs(float fhandle, string s)
1952 //void(float fhandle, string s) fputs = #113; // writes a line of text to the end of the file
1956 char string[VM_STRINGTEMP_LENGTH];
1959 VM_SAFEPARMCOUNT(2,VM_fputs);
1961 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1962 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
1964 VM_Warning("VM_fputs: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1967 if (prog->openfiles[filenum] == NULL)
1969 VM_Warning("VM_fputs: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1972 VM_VarString(1, string, sizeof(string));
1973 if ((stringlength = (int)strlen(string)))
1974 FS_Write(prog->openfiles[filenum], string, stringlength);
1975 if (developer_extra.integer)
1976 Con_DPrintf("fputs: %s: %s\n", PRVM_NAME, string);
1983 writetofile(float fhandle, entity ent)
1986 void VM_writetofile(void)
1991 VM_SAFEPARMCOUNT(2, VM_writetofile);
1993 file = VM_GetFileHandle( (int)PRVM_G_FLOAT(OFS_PARM0) );
1996 VM_Warning("VM_writetofile: invalid or closed file handle\n");
2000 ent = PRVM_G_EDICT(OFS_PARM1);
2001 if(ent->priv.required->free)
2003 VM_Warning("VM_writetofile: %s: entity %i is free !\n", PRVM_NAME, PRVM_NUM_FOR_EDICT(ent));
2007 PRVM_ED_Write (file, ent);
2010 // KrimZon - DP_QC_ENTITYDATA
2015 float() numentityfields
2016 Return the number of entity fields - NOT offsets
2019 void VM_numentityfields(void)
2021 PRVM_G_FLOAT(OFS_RETURN) = prog->progs->numfielddefs;
2024 // KrimZon - DP_QC_ENTITYDATA
2029 string(float fieldnum) entityfieldname
2030 Return name of the specified field as a string, or empty if the field is invalid (warning)
2033 void VM_entityfieldname(void)
2036 int i = (int)PRVM_G_FLOAT(OFS_PARM0);
2038 if (i < 0 || i >= prog->progs->numfielddefs)
2040 VM_Warning("VM_entityfieldname: %s: field index out of bounds\n", PRVM_NAME);
2041 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString("");
2045 d = &prog->fielddefs[i];
2046 PRVM_G_INT(OFS_RETURN) = d->s_name; // presuming that s_name points to a string already
2049 // KrimZon - DP_QC_ENTITYDATA
2054 float(float fieldnum) entityfieldtype
2057 void VM_entityfieldtype(void)
2060 int i = (int)PRVM_G_FLOAT(OFS_PARM0);
2062 if (i < 0 || i >= prog->progs->numfielddefs)
2064 VM_Warning("VM_entityfieldtype: %s: field index out of bounds\n", PRVM_NAME);
2065 PRVM_G_FLOAT(OFS_RETURN) = -1.0;
2069 d = &prog->fielddefs[i];
2070 PRVM_G_FLOAT(OFS_RETURN) = (float)d->type;
2073 // KrimZon - DP_QC_ENTITYDATA
2076 VM_getentityfieldstring
2078 string(float fieldnum, entity ent) getentityfieldstring
2081 void VM_getentityfieldstring(void)
2083 // put the data into a string
2088 int i = (int)PRVM_G_FLOAT(OFS_PARM0);
2090 if (i < 0 || i >= prog->progs->numfielddefs)
2092 VM_Warning("VM_entityfielddata: %s: field index out of bounds\n", PRVM_NAME);
2093 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString("");
2097 d = &prog->fielddefs[i];
2100 ent = PRVM_G_EDICT(OFS_PARM1);
2101 if(ent->priv.required->free)
2103 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString("");
2104 VM_Warning("VM_entityfielddata: %s: entity %i is free !\n", PRVM_NAME, PRVM_NUM_FOR_EDICT(ent));
2107 v = (int *)((char *)ent->fields.vp + d->ofs*4);
2109 // if it's 0 or blank, return an empty string
2110 type = d->type & ~DEF_SAVEGLOBAL;
2111 for (j=0 ; j<prvm_type_size[type] ; j++)
2114 if (j == prvm_type_size[type])
2116 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString("");
2120 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(PRVM_UglyValueString((etype_t)d->type, (prvm_eval_t *)v));
2123 // KrimZon - DP_QC_ENTITYDATA
2126 VM_putentityfieldstring
2128 float(float fieldnum, entity ent, string s) putentityfieldstring
2131 void VM_putentityfieldstring(void)
2135 int i = (int)PRVM_G_FLOAT(OFS_PARM0);
2137 if (i < 0 || i >= prog->progs->numfielddefs)
2139 VM_Warning("VM_entityfielddata: %s: field index out of bounds\n", PRVM_NAME);
2140 PRVM_G_FLOAT(OFS_RETURN) = 0.0f;
2144 d = &prog->fielddefs[i];
2147 ent = PRVM_G_EDICT(OFS_PARM1);
2148 if(ent->priv.required->free)
2150 VM_Warning("VM_entityfielddata: %s: entity %i is free !\n", PRVM_NAME, PRVM_NUM_FOR_EDICT(ent));
2151 PRVM_G_FLOAT(OFS_RETURN) = 0.0f;
2155 // parse the string into the value
2156 PRVM_G_FLOAT(OFS_RETURN) = ( PRVM_ED_ParseEpair(ent, d, PRVM_G_STRING(OFS_PARM2), false) ) ? 1.0f : 0.0f;
2163 float strlen(string s)
2166 //float(string s) strlen = #114; // returns how many characters are in a string
2167 void VM_strlen(void)
2169 VM_SAFEPARMCOUNT(1,VM_strlen);
2171 //PRVM_G_FLOAT(OFS_RETURN) = strlen(PRVM_G_STRING(OFS_PARM0));
2172 PRVM_G_FLOAT(OFS_RETURN) = u8_strlen(PRVM_G_STRING(OFS_PARM0));
2175 // DRESK - Decolorized String
2180 string strdecolorize(string s)
2183 // string (string s) strdecolorize = #472; // returns the passed in string with color codes stripped
2184 void VM_strdecolorize(void)
2186 char szNewString[VM_STRINGTEMP_LENGTH];
2187 const char *szString;
2190 VM_SAFEPARMCOUNT(1,VM_strdecolorize);
2191 szString = PRVM_G_STRING(OFS_PARM0);
2192 COM_StringDecolorize(szString, 0, szNewString, sizeof(szNewString), TRUE);
2193 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(szNewString);
2196 // DRESK - String Length (not counting color codes)
2201 float strlennocol(string s)
2204 // float(string s) strlennocol = #471; // returns how many characters are in a string not including color codes
2205 // For example, ^2Dresk returns a length of 5
2206 void VM_strlennocol(void)
2208 const char *szString;
2211 VM_SAFEPARMCOUNT(1,VM_strlennocol);
2213 szString = PRVM_G_STRING(OFS_PARM0);
2215 //nCnt = COM_StringLengthNoColors(szString, 0, NULL);
2216 nCnt = u8_COM_StringLengthNoColors(szString, 0, NULL);
2218 PRVM_G_FLOAT(OFS_RETURN) = nCnt;
2221 // DRESK - String to Uppercase and Lowercase
2226 string strtolower(string s)
2229 // string (string s) strtolower = #480; // returns passed in string in lowercase form
2230 void VM_strtolower(void)
2232 char szNewString[VM_STRINGTEMP_LENGTH];
2233 const char *szString;
2236 VM_SAFEPARMCOUNT(1,VM_strtolower);
2237 szString = PRVM_G_STRING(OFS_PARM0);
2239 COM_ToLowerString(szString, szNewString, sizeof(szNewString) );
2241 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(szNewString);
2248 string strtoupper(string s)
2251 // string (string s) strtoupper = #481; // returns passed in string in uppercase form
2252 void VM_strtoupper(void)
2254 char szNewString[VM_STRINGTEMP_LENGTH];
2255 const char *szString;
2258 VM_SAFEPARMCOUNT(1,VM_strtoupper);
2259 szString = PRVM_G_STRING(OFS_PARM0);
2261 COM_ToUpperString(szString, szNewString, sizeof(szNewString) );
2263 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(szNewString);
2270 string strcat(string,string,...[string])
2273 //string(string s1, string s2) strcat = #115;
2274 // concatenates two strings (for example "abc", "def" would return "abcdef")
2275 // and returns as a tempstring
2276 void VM_strcat(void)
2278 char s[VM_STRINGTEMP_LENGTH];
2279 VM_SAFEPARMCOUNTRANGE(1, 8, VM_strcat);
2281 VM_VarString(0, s, sizeof(s));
2282 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(s);
2289 string substring(string s, float start, float length)
2292 // string(string s, float start, float length) substring = #116;
2293 // returns a section of a string as a tempstring
2294 void VM_substring(void)
2297 int u_slength = 0, u_start;
2300 char string[VM_STRINGTEMP_LENGTH];
2302 VM_SAFEPARMCOUNT(3,VM_substring);
2305 s = PRVM_G_STRING(OFS_PARM0);
2306 start = (int)PRVM_G_FLOAT(OFS_PARM1);
2307 length = (int)PRVM_G_FLOAT(OFS_PARM2);
2308 slength = strlen(s);
2310 if (start < 0) // FTE_STRINGS feature
2312 start = bound(0, start, slength);
2314 if (length < 0) // FTE_STRINGS feature
2315 length += slength - start + 1;
2316 maxlen = min((int)sizeof(string) - 1, slength - start);
2317 length = bound(0, length, maxlen);
2319 memcpy(string, s + start, length);
2321 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string);
2324 s = PRVM_G_STRING(OFS_PARM0);
2325 start = (int)PRVM_G_FLOAT(OFS_PARM1);
2326 length = (int)PRVM_G_FLOAT(OFS_PARM2);
2328 if (start < 0) // FTE_STRINGS feature
2330 u_slength = u8_strlen(s);
2332 start = bound(0, start, u_slength);
2335 if (length < 0) // FTE_STRINGS feature
2337 if (!u_slength) // it's not calculated when it's not needed above
2338 u_slength = u8_strlen(s);
2339 length += u_slength - start + 1;
2342 // positive start, positive length
2343 u_start = u8_byteofs(s, start, NULL);
2346 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString("");
2349 u_length = u8_bytelen(s + u_start, length);
2350 if (u_length >= sizeof(string)-1)
2351 u_length = sizeof(string)-1;
2353 memcpy(string, s + u_start, u_length);
2354 string[u_length] = 0;
2355 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string);
2362 string(string search, string replace, string subject) strreplace = #484;
2365 // replaces all occurrences of search with replace in the string subject, and returns the result
2366 void VM_strreplace(void)
2369 const char *search, *replace, *subject;
2370 char string[VM_STRINGTEMP_LENGTH];
2371 int search_len, replace_len, subject_len;
2373 VM_SAFEPARMCOUNT(3,VM_strreplace);
2375 search = PRVM_G_STRING(OFS_PARM0);
2376 replace = PRVM_G_STRING(OFS_PARM1);
2377 subject = PRVM_G_STRING(OFS_PARM2);
2379 search_len = (int)strlen(search);
2380 replace_len = (int)strlen(replace);
2381 subject_len = (int)strlen(subject);
2384 for (i = 0; i < subject_len; i++)
2386 for (j = 0; j < search_len && i+j < subject_len; j++)
2387 if (subject[i+j] != search[j])
2389 if (j == search_len || i+j == subject_len)
2391 // found it at offset 'i'
2392 for (j = 0; j < replace_len && si < (int)sizeof(string) - 1; j++)
2393 string[si++] = replace[j];
2394 i += search_len - 1;
2399 if (si < (int)sizeof(string) - 1)
2400 string[si++] = subject[i];
2405 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string);
2412 string(string search, string replace, string subject) strireplace = #485;
2415 // case-insensitive version of strreplace
2416 void VM_strireplace(void)
2419 const char *search, *replace, *subject;
2420 char string[VM_STRINGTEMP_LENGTH];
2421 int search_len, replace_len, subject_len;
2423 VM_SAFEPARMCOUNT(3,VM_strreplace);
2425 search = PRVM_G_STRING(OFS_PARM0);
2426 replace = PRVM_G_STRING(OFS_PARM1);
2427 subject = PRVM_G_STRING(OFS_PARM2);
2429 search_len = (int)strlen(search);
2430 replace_len = (int)strlen(replace);
2431 subject_len = (int)strlen(subject);
2434 for (i = 0; i < subject_len; i++)
2436 for (j = 0; j < search_len && i+j < subject_len; j++)
2437 if (tolower(subject[i+j]) != tolower(search[j]))
2439 if (j == search_len || i+j == subject_len)
2441 // found it at offset 'i'
2442 for (j = 0; j < replace_len && si < (int)sizeof(string) - 1; j++)
2443 string[si++] = replace[j];
2444 i += search_len - 1;
2449 if (si < (int)sizeof(string) - 1)
2450 string[si++] = subject[i];
2455 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string);
2462 vector stov(string s)
2465 //vector(string s) stov = #117; // returns vector value from a string
2468 char string[VM_STRINGTEMP_LENGTH];
2470 VM_SAFEPARMCOUNT(1,VM_stov);
2472 VM_VarString(0, string, sizeof(string));
2473 Math_atov(string, PRVM_G_VECTOR(OFS_RETURN));
2480 string strzone(string s)
2483 //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)
2484 void VM_strzone(void)
2487 char string[VM_STRINGTEMP_LENGTH];
2490 VM_SAFEPARMCOUNT(1,VM_strzone);
2492 VM_VarString(0, string, sizeof(string));
2493 alloclen = strlen(string) + 1;
2494 PRVM_G_INT(OFS_RETURN) = PRVM_AllocString(alloclen, &out);
2495 memcpy(out, string, alloclen);
2505 //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!!!)
2506 void VM_strunzone(void)
2508 VM_SAFEPARMCOUNT(1,VM_strunzone);
2509 PRVM_FreeString(PRVM_G_INT(OFS_PARM0));
2514 VM_command (used by client and menu)
2516 clientcommand(float client, string s) (for client and menu)
2519 //void(entity e, string s) clientcommand = #440; // executes a command string as if it came from the specified client
2520 //this function originally written by KrimZon, made shorter by LordHavoc
2521 void VM_clcommand (void)
2523 client_t *temp_client;
2526 VM_SAFEPARMCOUNT(2,VM_clcommand);
2528 i = (int)PRVM_G_FLOAT(OFS_PARM0);
2529 if (!sv.active || i < 0 || i >= svs.maxclients || !svs.clients[i].active)
2531 VM_Warning("VM_clientcommand: %s: invalid client/server is not active !\n", PRVM_NAME);
2535 temp_client = host_client;
2536 host_client = svs.clients + i;
2537 Cmd_ExecuteString (PRVM_G_STRING(OFS_PARM1), src_client);
2538 host_client = temp_client;
2546 float tokenize(string s)
2549 //float(string s) tokenize = #441; // takes apart a string into individal words (access them with argv), returns how many
2550 //this function originally written by KrimZon, made shorter by LordHavoc
2551 //20040203: rewritten by LordHavoc (no longer uses allocations)
2552 static int num_tokens = 0;
2553 static int tokens[VM_STRINGTEMP_LENGTH / 2];
2554 static int tokens_startpos[VM_STRINGTEMP_LENGTH / 2];
2555 static int tokens_endpos[VM_STRINGTEMP_LENGTH / 2];
2556 static char tokenize_string[VM_STRINGTEMP_LENGTH];
2557 void VM_tokenize (void)
2561 VM_SAFEPARMCOUNT(1,VM_tokenize);
2563 strlcpy(tokenize_string, PRVM_G_STRING(OFS_PARM0), sizeof(tokenize_string));
2564 p = tokenize_string;
2569 if (num_tokens >= (int)(sizeof(tokens)/sizeof(tokens[0])))
2572 // skip whitespace here to find token start pos
2573 while(*p && ISWHITESPACE(*p))
2576 tokens_startpos[num_tokens] = p - tokenize_string;
2577 if(!COM_ParseToken_VM_Tokenize(&p, false))
2579 tokens_endpos[num_tokens] = p - tokenize_string;
2580 tokens[num_tokens] = PRVM_SetTempString(com_token);
2584 PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
2587 //float(string s) tokenize = #514; // takes apart a string into individal words (access them with argv), returns how many
2588 void VM_tokenize_console (void)
2592 VM_SAFEPARMCOUNT(1,VM_tokenize);
2594 strlcpy(tokenize_string, PRVM_G_STRING(OFS_PARM0), sizeof(tokenize_string));
2595 p = tokenize_string;
2600 if (num_tokens >= (int)(sizeof(tokens)/sizeof(tokens[0])))
2603 // skip whitespace here to find token start pos
2604 while(*p && ISWHITESPACE(*p))
2607 tokens_startpos[num_tokens] = p - tokenize_string;
2608 if(!COM_ParseToken_Console(&p))
2610 tokens_endpos[num_tokens] = p - tokenize_string;
2611 tokens[num_tokens] = PRVM_SetTempString(com_token);
2615 PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
2620 VM_tokenizebyseparator
2622 float tokenizebyseparator(string s, string separator1, ...)
2625 //float(string s, string separator1, ...) tokenizebyseparator = #479; // takes apart a string into individal words (access them with argv), returns how many
2626 //this function returns the token preceding each instance of a separator (of
2627 //which there can be multiple), and the text following the last separator
2628 //useful for parsing certain kinds of data like IP addresses
2630 //numnumbers = tokenizebyseparator("10.1.2.3", ".");
2631 //returns 4 and the tokens "10" "1" "2" "3".
2632 void VM_tokenizebyseparator (void)
2636 int separatorlen[7];
2637 const char *separators[7];
2640 char tokentext[MAX_INPUTLINE];
2642 VM_SAFEPARMCOUNTRANGE(2, 8,VM_tokenizebyseparator);
2644 strlcpy(tokenize_string, PRVM_G_STRING(OFS_PARM0), sizeof(tokenize_string));
2645 p = tokenize_string;
2648 for (j = 1;j < prog->argc;j++)
2650 // skip any blank separator strings
2651 const char *s = PRVM_G_STRING(OFS_PARM0+j*3);
2654 separators[numseparators] = s;
2655 separatorlen[numseparators] = strlen(s);
2662 while (num_tokens < (int)(sizeof(tokens)/sizeof(tokens[0])))
2664 token = tokentext + j;
2665 tokens_startpos[num_tokens] = p - tokenize_string;
2669 for (k = 0;k < numseparators;k++)
2671 if (!strncmp(p, separators[k], separatorlen[k]))
2673 p += separatorlen[k];
2677 if (k < numseparators)
2679 if (j < (int)sizeof(tokentext)-1)
2680 tokentext[j++] = *p;
2684 tokens_endpos[num_tokens] = p0 - tokenize_string;
2685 if (j >= (int)sizeof(tokentext))
2688 tokens[num_tokens++] = PRVM_SetTempString(token);
2693 PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
2696 //string(float n) argv = #442; // returns a word from the tokenized string (returns nothing for an invalid index)
2697 //this function originally written by KrimZon, made shorter by LordHavoc
2702 VM_SAFEPARMCOUNT(1,VM_argv);
2704 token_num = (int)PRVM_G_FLOAT(OFS_PARM0);
2707 token_num += num_tokens;
2709 if (token_num >= 0 && token_num < num_tokens)
2710 PRVM_G_INT(OFS_RETURN) = tokens[token_num];
2712 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
2715 //float(float n) argv_start_index = #515; // returns the start index of a token
2716 void VM_argv_start_index (void)
2720 VM_SAFEPARMCOUNT(1,VM_argv);
2722 token_num = (int)PRVM_G_FLOAT(OFS_PARM0);
2725 token_num += num_tokens;
2727 if (token_num >= 0 && token_num < num_tokens)
2728 PRVM_G_FLOAT(OFS_RETURN) = tokens_startpos[token_num];
2730 PRVM_G_FLOAT(OFS_RETURN) = -1;
2733 //float(float n) argv_end_index = #516; // returns the end index of a token
2734 void VM_argv_end_index (void)
2738 VM_SAFEPARMCOUNT(1,VM_argv);
2740 token_num = (int)PRVM_G_FLOAT(OFS_PARM0);
2743 token_num += num_tokens;
2745 if (token_num >= 0 && token_num < num_tokens)
2746 PRVM_G_FLOAT(OFS_RETURN) = tokens_endpos[token_num];
2748 PRVM_G_FLOAT(OFS_RETURN) = -1;
2758 void VM_isserver(void)
2760 VM_SAFEPARMCOUNT(0,VM_serverstate);
2762 PRVM_G_FLOAT(OFS_RETURN) = sv.active && (svs.maxclients > 1 || cls.state == ca_dedicated);
2772 void VM_clientcount(void)
2774 VM_SAFEPARMCOUNT(0,VM_clientcount);
2776 PRVM_G_FLOAT(OFS_RETURN) = svs.maxclients;
2786 void VM_clientstate(void)
2788 VM_SAFEPARMCOUNT(0,VM_clientstate);
2791 switch( cls.state ) {
2792 case ca_uninitialized:
2794 PRVM_G_FLOAT(OFS_RETURN) = 0;
2796 case ca_disconnected:
2797 PRVM_G_FLOAT(OFS_RETURN) = 1;
2800 PRVM_G_FLOAT(OFS_RETURN) = 2;
2803 // should never be reached!
2812 float getostype(void)
2814 */ // not used at the moment -> not included in the common list
2815 void VM_getostype(void)
2817 VM_SAFEPARMCOUNT(0,VM_getostype);
2822 OS_MAC - not supported
2826 PRVM_G_FLOAT(OFS_RETURN) = 0;
2827 #elif defined(MACOSX)
2828 PRVM_G_FLOAT(OFS_RETURN) = 2;
2830 PRVM_G_FLOAT(OFS_RETURN) = 1;
2841 extern double host_starttime;
2842 float CDAudio_GetPosition(void);
2843 void VM_gettime(void)
2847 VM_SAFEPARMCOUNTRANGE(0,1,VM_gettime);
2851 PRVM_G_FLOAT(OFS_RETURN) = (float) realtime;
2855 timer_index = (int) PRVM_G_FLOAT(OFS_PARM0);
2858 case 0: // GETTIME_FRAMESTART
2859 PRVM_G_FLOAT(OFS_RETURN) = (float) realtime;
2861 case 1: // GETTIME_REALTIME
2862 PRVM_G_FLOAT(OFS_RETURN) = (float) Sys_DoubleTime();
2864 case 2: // GETTIME_HIRES
2865 PRVM_G_FLOAT(OFS_RETURN) = (float) (Sys_DoubleTime() - realtime);
2867 case 3: // GETTIME_UPTIME
2868 PRVM_G_FLOAT(OFS_RETURN) = (float) (Sys_DoubleTime() - host_starttime);
2870 case 4: // GETTIME_CDTRACK
2871 PRVM_G_FLOAT(OFS_RETURN) = (float) CDAudio_GetPosition();
2874 VM_Warning("VM_gettime: %s: unsupported timer specified, returning realtime\n", PRVM_NAME);
2875 PRVM_G_FLOAT(OFS_RETURN) = (float) realtime;
2885 float getsoundtime(void)
2889 void VM_getsoundtime (void)
2891 int entnum, entchannel, pnum;
2892 VM_SAFEPARMCOUNT(2,VM_getsoundtime);
2894 pnum = PRVM_GetProgNr();
2895 if (pnum == PRVM_MENUPROG)
2897 VM_Warning("VM_getsoundtime: %s: not supported on this progs\n", PRVM_NAME);
2898 PRVM_G_FLOAT(OFS_RETURN) = -1;
2901 entnum = ((pnum == PRVM_CLIENTPROG) ? MAX_EDICTS : 0) + PRVM_NUM_FOR_EDICT(PRVM_G_EDICT(OFS_PARM0));
2902 entchannel = (int)PRVM_G_FLOAT(OFS_PARM1);
2903 if (entchannel <= 0 || entchannel > 8)
2904 VM_Warning("VM_getsoundtime: %s: bad channel %i\n", PRVM_NAME, entchannel);
2905 PRVM_G_FLOAT(OFS_RETURN) = (float)S_GetEntChannelPosition(entnum, entchannel);
2912 string soundlength (string sample)
2915 void VM_soundlength (void)
2919 VM_SAFEPARMCOUNT(1, VM_soundlength);
2921 s = PRVM_G_STRING(OFS_PARM0);
2922 PRVM_G_FLOAT(OFS_RETURN) = S_SoundLength(s);
2929 loadfromdata(string data)
2932 void VM_loadfromdata(void)
2934 VM_SAFEPARMCOUNT(1,VM_loadentsfromfile);
2936 PRVM_ED_LoadFromFile(PRVM_G_STRING(OFS_PARM0));
2940 ========================
2943 parseentitydata(entity ent, string data)
2944 ========================
2946 void VM_parseentitydata(void)
2951 VM_SAFEPARMCOUNT(2, VM_parseentitydata);
2953 // get edict and test it
2954 ent = PRVM_G_EDICT(OFS_PARM0);
2955 if (ent->priv.required->free)
2956 PRVM_ERROR ("VM_parseentitydata: %s: Can only set already spawned entities (entity %i is free)!", PRVM_NAME, PRVM_NUM_FOR_EDICT(ent));
2958 data = PRVM_G_STRING(OFS_PARM1);
2960 // parse the opening brace
2961 if (!COM_ParseToken_Simple(&data, false, false) || com_token[0] != '{' )
2962 PRVM_ERROR ("VM_parseentitydata: %s: Couldn't parse entity data:\n%s", PRVM_NAME, data );
2964 PRVM_ED_ParseEdict (data, ent);
2971 loadfromfile(string file)
2974 void VM_loadfromfile(void)
2976 const char *filename;
2979 VM_SAFEPARMCOUNT(1,VM_loadfromfile);
2981 filename = PRVM_G_STRING(OFS_PARM0);
2982 if (FS_CheckNastyPath(filename, false))
2984 PRVM_G_FLOAT(OFS_RETURN) = -4;
2985 VM_Warning("VM_loadfromfile: %s dangerous or non-portable filename \"%s\" not allowed. (contains : or \\ or begins with .. or /)\n", PRVM_NAME, filename);
2989 // not conform with VM_fopen
2990 data = (char *)FS_LoadFile(filename, tempmempool, false, NULL);
2992 PRVM_G_FLOAT(OFS_RETURN) = -1;
2994 PRVM_ED_LoadFromFile(data);
3005 float mod(float val, float m)
3008 void VM_modulo(void)
3011 VM_SAFEPARMCOUNT(2,VM_module);
3013 val = (int) PRVM_G_FLOAT(OFS_PARM0);
3014 m = (int) PRVM_G_FLOAT(OFS_PARM1);
3016 PRVM_G_FLOAT(OFS_RETURN) = (float) (val % m);
3019 void VM_Search_Init(void)
3022 for (i = 0;i < PRVM_MAX_OPENSEARCHES;i++)
3023 prog->opensearches[i] = NULL;
3026 void VM_Search_Reset(void)
3029 // reset the fssearch list
3030 for(i = 0; i < PRVM_MAX_OPENSEARCHES; i++)
3032 if(prog->opensearches[i])
3033 FS_FreeSearch(prog->opensearches[i]);
3034 prog->opensearches[i] = NULL;
3042 float search_begin(string pattern, float caseinsensitive, float quiet)
3045 void VM_search_begin(void)
3048 const char *pattern;
3049 int caseinsens, quiet;
3051 VM_SAFEPARMCOUNT(3, VM_search_begin);
3053 pattern = PRVM_G_STRING(OFS_PARM0);
3055 VM_CheckEmptyString(pattern);
3057 caseinsens = (int)PRVM_G_FLOAT(OFS_PARM1);
3058 quiet = (int)PRVM_G_FLOAT(OFS_PARM2);
3060 for(handle = 0; handle < PRVM_MAX_OPENSEARCHES; handle++)
3061 if(!prog->opensearches[handle])
3064 if(handle >= PRVM_MAX_OPENSEARCHES)
3066 PRVM_G_FLOAT(OFS_RETURN) = -2;
3067 VM_Warning("VM_search_begin: %s ran out of search handles (%i)\n", PRVM_NAME, PRVM_MAX_OPENSEARCHES);
3071 if(!(prog->opensearches[handle] = FS_Search(pattern,caseinsens, quiet)))
3072 PRVM_G_FLOAT(OFS_RETURN) = -1;
3075 prog->opensearches_origin[handle] = PRVM_AllocationOrigin();
3076 PRVM_G_FLOAT(OFS_RETURN) = handle;
3084 void search_end(float handle)
3087 void VM_search_end(void)
3090 VM_SAFEPARMCOUNT(1, VM_search_end);
3092 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
3094 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
3096 VM_Warning("VM_search_end: invalid handle %i used in %s\n", handle, PRVM_NAME);
3099 if(prog->opensearches[handle] == NULL)
3101 VM_Warning("VM_search_end: no such handle %i in %s\n", handle, PRVM_NAME);
3105 FS_FreeSearch(prog->opensearches[handle]);
3106 prog->opensearches[handle] = NULL;
3107 if(prog->opensearches_origin[handle])
3108 PRVM_Free((char *)prog->opensearches_origin[handle]);
3115 float search_getsize(float handle)
3118 void VM_search_getsize(void)
3121 VM_SAFEPARMCOUNT(1, VM_M_search_getsize);
3123 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
3125 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
3127 VM_Warning("VM_search_getsize: invalid handle %i used in %s\n", handle, PRVM_NAME);
3130 if(prog->opensearches[handle] == NULL)
3132 VM_Warning("VM_search_getsize: no such handle %i in %s\n", handle, PRVM_NAME);
3136 PRVM_G_FLOAT(OFS_RETURN) = prog->opensearches[handle]->numfilenames;
3141 VM_search_getfilename
3143 string search_getfilename(float handle, float num)
3146 void VM_search_getfilename(void)
3148 int handle, filenum;
3149 VM_SAFEPARMCOUNT(2, VM_search_getfilename);
3151 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
3152 filenum = (int)PRVM_G_FLOAT(OFS_PARM1);
3154 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
3156 VM_Warning("VM_search_getfilename: invalid handle %i used in %s\n", handle, PRVM_NAME);
3159 if(prog->opensearches[handle] == NULL)
3161 VM_Warning("VM_search_getfilename: no such handle %i in %s\n", handle, PRVM_NAME);
3164 if(filenum < 0 || filenum >= prog->opensearches[handle]->numfilenames)
3166 VM_Warning("VM_search_getfilename: invalid filenum %i in %s\n", filenum, PRVM_NAME);
3170 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog->opensearches[handle]->filenames[filenum]);
3177 string chr(float ascii)
3184 VM_SAFEPARMCOUNT(1, VM_chr);
3186 tmp[0] = (unsigned char) PRVM_G_FLOAT(OFS_PARM0);
3189 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(tmp);
3194 VM_SAFEPARMCOUNT(1, VM_chr);
3196 len = u8_fromchar((Uchar)PRVM_G_FLOAT(OFS_PARM0), tmp, sizeof(tmp));
3198 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(tmp);
3201 //=============================================================================
3202 // Draw builtins (client & menu)
3208 float iscachedpic(string pic)
3211 void VM_iscachedpic(void)
3213 VM_SAFEPARMCOUNT(1,VM_iscachedpic);
3215 // drawq hasnt such a function, thus always return true
3216 PRVM_G_FLOAT(OFS_RETURN) = false;
3223 string precache_pic(string pic)
3226 void VM_precache_pic(void)
3230 VM_SAFEPARMCOUNT(1, VM_precache_pic);
3232 s = PRVM_G_STRING(OFS_PARM0);
3233 PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
3234 VM_CheckEmptyString (s);
3236 // AK Draw_CachePic is supposed to always return a valid pointer
3237 if( Draw_CachePic_Flags(s, 0)->tex == r_texture_notexture )
3238 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
3248 void VM_freepic(void)
3252 VM_SAFEPARMCOUNT(1,VM_freepic);
3254 s = PRVM_G_STRING(OFS_PARM0);
3255 VM_CheckEmptyString (s);
3260 void getdrawfontscale(float *sx, float *sy)
3264 if(prog->globaloffsets.drawfontscale >= 0)
3266 VectorCopy(PRVM_G_VECTOR(prog->globaloffsets.drawfontscale), v);
3267 if(VectorLength2(v) > 0)
3275 dp_font_t *getdrawfont(void)
3277 if(prog->globaloffsets.drawfont >= 0)
3279 int f = (int) PRVM_G_FLOAT(prog->globaloffsets.drawfont);
3280 if(f < 0 || f >= dp_fonts.maxsize)
3281 return FONT_DEFAULT;
3282 return &dp_fonts.f[f];
3285 return FONT_DEFAULT;
3292 float drawcharacter(vector position, float character, vector scale, vector rgb, float alpha, float flag)
3295 void VM_drawcharacter(void)
3297 float *pos,*scale,*rgb;
3301 VM_SAFEPARMCOUNT(6,VM_drawcharacter);
3303 character = (char) PRVM_G_FLOAT(OFS_PARM1);
3306 PRVM_G_FLOAT(OFS_RETURN) = -1;
3307 VM_Warning("VM_drawcharacter: %s passed null character !\n",PRVM_NAME);
3311 pos = PRVM_G_VECTOR(OFS_PARM0);
3312 scale = PRVM_G_VECTOR(OFS_PARM2);
3313 rgb = PRVM_G_VECTOR(OFS_PARM3);
3314 flag = (int)PRVM_G_FLOAT(OFS_PARM5);
3316 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3318 PRVM_G_FLOAT(OFS_RETURN) = -2;
3319 VM_Warning("VM_drawcharacter: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
3323 if(pos[2] || scale[2])
3324 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")));
3326 if(!scale[0] || !scale[1])
3328 PRVM_G_FLOAT(OFS_RETURN) = -3;
3329 VM_Warning("VM_drawcharacter: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
3333 getdrawfontscale(&sx, &sy);
3334 DrawQ_String_Scale(pos[0], pos[1], &character, 1, scale[0], scale[1], sx, sy, rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM4), flag, NULL, true, getdrawfont());
3335 PRVM_G_FLOAT(OFS_RETURN) = 1;
3342 float drawstring(vector position, string text, vector scale, vector rgb, float alpha, float flag)
3345 void VM_drawstring(void)
3347 float *pos,*scale,*rgb;
3351 VM_SAFEPARMCOUNT(6,VM_drawstring);
3353 string = PRVM_G_STRING(OFS_PARM1);
3354 pos = PRVM_G_VECTOR(OFS_PARM0);
3355 scale = PRVM_G_VECTOR(OFS_PARM2);
3356 rgb = PRVM_G_VECTOR(OFS_PARM3);
3357 flag = (int)PRVM_G_FLOAT(OFS_PARM5);
3359 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3361 PRVM_G_FLOAT(OFS_RETURN) = -2;
3362 VM_Warning("VM_drawstring: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
3366 if(!scale[0] || !scale[1])
3368 PRVM_G_FLOAT(OFS_RETURN) = -3;
3369 VM_Warning("VM_drawstring: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
3373 if(pos[2] || scale[2])
3374 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")));
3376 getdrawfontscale(&sx, &sy);
3377 DrawQ_String_Scale(pos[0], pos[1], string, 0, scale[0], scale[1], sx, sy, rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM4), flag, NULL, true, getdrawfont());
3378 //Font_DrawString(pos[0], pos[1], string, 0, scale[0], scale[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM4), flag, NULL, true);
3379 PRVM_G_FLOAT(OFS_RETURN) = 1;
3384 VM_drawcolorcodedstring
3386 float drawcolorcodedstring(vector position, string text, vector scale, float alpha, float flag)
3389 void VM_drawcolorcodedstring(void)
3395 VM_SAFEPARMCOUNT(5,VM_drawstring);
3397 string = PRVM_G_STRING(OFS_PARM1);
3398 pos = PRVM_G_VECTOR(OFS_PARM0);
3399 scale = PRVM_G_VECTOR(OFS_PARM2);
3400 flag = (int)PRVM_G_FLOAT(OFS_PARM4);
3402 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3404 PRVM_G_FLOAT(OFS_RETURN) = -2;
3405 VM_Warning("VM_drawcolorcodedstring: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
3409 if(!scale[0] || !scale[1])
3411 PRVM_G_FLOAT(OFS_RETURN) = -3;
3412 VM_Warning("VM_drawcolorcodedstring: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
3416 if(pos[2] || scale[2])
3417 Con_Printf("VM_drawcolorcodedstring: z value%s from %s discarded\n",(pos[2] && scale[2]) ? "s" : " ",((pos[2] && scale[2]) ? "pos and scale" : (pos[2] ? "pos" : "scale")));
3419 getdrawfontscale(&sx, &sy);
3420 DrawQ_String_Scale(pos[0], pos[1], string, 0, scale[0], scale[1], sx, sy, 1, 1, 1, PRVM_G_FLOAT(OFS_PARM3), flag, NULL, false, getdrawfont());
3421 PRVM_G_FLOAT(OFS_RETURN) = 1;
3427 float stringwidth(string text, float allowColorCodes, float size)
3430 void VM_stringwidth(void)
3434 float mult; // sz is intended font size so we can later add freetype support, mult is font size multiplier in pixels per character cell
3438 VM_SAFEPARMCOUNTRANGE(2,3,VM_drawstring);
3440 getdrawfontscale(&sx, &sy);
3443 szv = PRVM_G_VECTOR(OFS_PARM2);
3448 // we want the width for 8x8 font size, divided by 8
3449 static float defsize[] = {8, 8};
3452 // to make sure snapping is turned off, ALWAYS use a nontrivial scale in this case
3453 if(sx >= 0.9 && sx <= 1.1)
3461 string = PRVM_G_STRING(OFS_PARM0);
3462 colors = (int)PRVM_G_FLOAT(OFS_PARM1);
3464 PRVM_G_FLOAT(OFS_RETURN) = DrawQ_TextWidth_UntilWidth_TrackColors_Scale(string, &maxlen, szv[0], szv[1], sx, sy, NULL, !colors, getdrawfont(), 1000000000) * mult;
3468 mult = sz = PRVM_G_FLOAT(OFS_PARM2);
3476 string = PRVM_G_STRING(OFS_PARM0);
3477 colors = (int)PRVM_G_FLOAT(OFS_PARM1);
3479 PRVM_G_FLOAT(OFS_RETURN) = DrawQ_TextWidth(string, 0, !colors, getdrawfont()) * mult; // 1x1 characters, don't actually draw
3487 float findfont(string s)
3491 float getdrawfontnum(const char *fontname)
3495 for(i = 0; i < dp_fonts.maxsize; ++i)
3496 if(!strcmp(dp_fonts.f[i].title, fontname))
3501 void VM_findfont(void)
3503 VM_SAFEPARMCOUNT(1,VM_findfont);
3504 PRVM_G_FLOAT(OFS_RETURN) = getdrawfontnum(PRVM_G_STRING(OFS_PARM0));
3511 float loadfont(string fontname, string fontmaps, string sizes, float slot)
3515 dp_font_t *FindFont(const char *title, qboolean allocate_new);
3516 void LoadFont(qboolean override, const char *name, dp_font_t *fnt, float scale, float voffset);
3517 void VM_loadfont(void)
3519 const char *fontname, *filelist, *sizes, *c, *cm;
3520 char mainfont[MAX_QPATH];
3522 float sz, scale, voffset;
3525 VM_SAFEPARMCOUNTRANGE(3,6,VM_loadfont);
3527 fontname = PRVM_G_STRING(OFS_PARM0);
3529 fontname = "default";
3531 filelist = PRVM_G_STRING(OFS_PARM1);
3533 filelist = "gfx/conchars";
3535 sizes = PRVM_G_STRING(OFS_PARM2);
3541 if (prog->argc >= 4)
3543 i = PRVM_G_FLOAT(OFS_PARM3);
3544 if (i >= 0 && i < dp_fonts.maxsize)
3547 strlcpy(f->title, fontname, sizeof(f->title)); // replace name
3551 f = FindFont(fontname, true);
3554 PRVM_G_FLOAT(OFS_RETURN) = -1;
3555 return; // something go wrong
3558 memset(f->fallbacks, 0, sizeof(f->fallbacks));
3559 memset(f->fallback_faces, 0, sizeof(f->fallback_faces));
3561 // first font is handled "normally"
3562 c = strchr(filelist, ':');
3563 cm = strchr(filelist, ',');
3564 if(c && (!cm || c < cm))
3565 f->req_face = atoi(c+1);
3571 if(!c || (c - filelist) > MAX_QPATH)
3572 strlcpy(mainfont, filelist, sizeof(mainfont));
3575 memcpy(mainfont, filelist, c - filelist);
3576 mainfont[c - filelist] = 0;
3580 for(i = 0; i < MAX_FONT_FALLBACKS; ++i)
3582 c = strchr(filelist, ',');
3588 c = strchr(filelist, ':');
3589 cm = strchr(filelist, ',');
3590 if(c && (!cm || c < cm))
3591 f->fallback_faces[i] = atoi(c+1);
3594 f->fallback_faces[i] = 0; // f->req_face; could make it stick to the default-font's face index
3597 if(!c || (c-filelist) > MAX_QPATH)
3599 strlcpy(f->fallbacks[i], filelist, sizeof(mainfont));
3603 memcpy(f->fallbacks[i], filelist, c - filelist);
3604 f->fallbacks[i][c - filelist] = 0;
3609 for(i = 0; i < MAX_FONT_SIZES; ++i)
3610 f->req_sizes[i] = -1;
3611 for (numsizes = 0,c = sizes;;)
3613 if (!COM_ParseToken_VM_Tokenize(&c, 0))
3615 sz = atof(com_token);
3617 if (sz < 0.001f || sz > 1000.0f)
3619 VM_Warning("VM_loadfont: crap size %s", com_token);
3623 if (numsizes == MAX_FONT_SIZES)
3625 VM_Warning("VM_loadfont: MAX_FONT_SIZES = %i exceeded", MAX_FONT_SIZES);
3628 f->req_sizes[numsizes] = sz;
3632 // additional scale/hoffset parms
3635 if (prog->argc >= 5)
3637 scale = PRVM_G_FLOAT(OFS_PARM4);
3641 if (prog->argc >= 6)
3642 voffset = PRVM_G_FLOAT(OFS_PARM5);
3645 LoadFont(true, mainfont, f, scale, voffset);
3647 // return index of loaded font
3648 PRVM_G_FLOAT(OFS_RETURN) = (f - dp_fonts.f);
3655 float drawpic(vector position, string pic, vector size, vector rgb, float alpha, float flag)
3658 void VM_drawpic(void)
3660 const char *picname;
3661 float *size, *pos, *rgb;
3664 VM_SAFEPARMCOUNT(6,VM_drawpic);
3666 picname = PRVM_G_STRING(OFS_PARM1);
3667 VM_CheckEmptyString (picname);
3669 // is pic cached ? no function yet for that
3672 PRVM_G_FLOAT(OFS_RETURN) = -4;
3673 VM_Warning("VM_drawpic: %s: %s not cached !\n", PRVM_NAME, picname);
3677 pos = PRVM_G_VECTOR(OFS_PARM0);
3678 size = PRVM_G_VECTOR(OFS_PARM2);
3679 rgb = PRVM_G_VECTOR(OFS_PARM3);
3680 flag = (int) PRVM_G_FLOAT(OFS_PARM5);
3682 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3684 PRVM_G_FLOAT(OFS_RETURN) = -2;
3685 VM_Warning("VM_drawpic: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
3689 if(pos[2] || size[2])
3690 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")));
3692 DrawQ_Pic(pos[0], pos[1], Draw_CachePic_Flags (picname, CACHEPICFLAG_NOTPERSISTENT), size[0], size[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM4), flag);
3693 PRVM_G_FLOAT(OFS_RETURN) = 1;
3699 float drawrotpic(vector position, string pic, vector size, vector org, float angle, vector rgb, float alpha, float flag)
3702 void VM_drawrotpic(void)
3704 const char *picname;
3705 float *size, *pos, *org, *rgb;
3708 VM_SAFEPARMCOUNT(8,VM_drawrotpic);
3710 picname = PRVM_G_STRING(OFS_PARM1);
3711 VM_CheckEmptyString (picname);
3713 // is pic cached ? no function yet for that
3716 PRVM_G_FLOAT(OFS_RETURN) = -4;
3717 VM_Warning("VM_drawrotpic: %s: %s not cached !\n", PRVM_NAME, picname);
3721 pos = PRVM_G_VECTOR(OFS_PARM0);
3722 size = PRVM_G_VECTOR(OFS_PARM2);
3723 org = PRVM_G_VECTOR(OFS_PARM3);
3724 rgb = PRVM_G_VECTOR(OFS_PARM5);
3725 flag = (int) PRVM_G_FLOAT(OFS_PARM7);
3727 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3729 PRVM_G_FLOAT(OFS_RETURN) = -2;
3730 VM_Warning("VM_drawrotpic: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
3734 if(pos[2] || size[2] || org[2])
3735 Con_Printf("VM_drawrotpic: z value from pos/size/org discarded\n");
3737 DrawQ_RotPic(pos[0], pos[1], Draw_CachePic_Flags(picname, CACHEPICFLAG_NOTPERSISTENT), size[0], size[1], org[0], org[1], PRVM_G_FLOAT(OFS_PARM4), rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM6), flag);
3738 PRVM_G_FLOAT(OFS_RETURN) = 1;
3744 float drawsubpic(vector position, vector size, string pic, vector srcPos, vector srcSize, vector rgb, float alpha, float flag)
3748 void VM_drawsubpic(void)
3750 const char *picname;
3751 float *size, *pos, *rgb, *srcPos, *srcSize, alpha;
3754 VM_SAFEPARMCOUNT(8,VM_drawsubpic);
3756 picname = PRVM_G_STRING(OFS_PARM2);
3757 VM_CheckEmptyString (picname);
3759 // is pic cached ? no function yet for that
3762 PRVM_G_FLOAT(OFS_RETURN) = -4;
3763 VM_Warning("VM_drawsubpic: %s: %s not cached !\n", PRVM_NAME, picname);
3767 pos = PRVM_G_VECTOR(OFS_PARM0);
3768 size = PRVM_G_VECTOR(OFS_PARM1);
3769 srcPos = PRVM_G_VECTOR(OFS_PARM3);
3770 srcSize = PRVM_G_VECTOR(OFS_PARM4);
3771 rgb = PRVM_G_VECTOR(OFS_PARM5);
3772 alpha = PRVM_G_FLOAT(OFS_PARM6);
3773 flag = (int) PRVM_G_FLOAT(OFS_PARM7);
3775 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3777 PRVM_G_FLOAT(OFS_RETURN) = -2;
3778 VM_Warning("VM_drawsubpic: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
3782 if(pos[2] || size[2])
3783 Con_Printf("VM_drawsubpic: z value%s from %s discarded\n",(pos[2] && size[2]) ? "s" : " ",((pos[2] && size[2]) ? "pos and size" : (pos[2] ? "pos" : "size")));
3785 DrawQ_SuperPic(pos[0], pos[1], Draw_CachePic_Flags (picname, CACHEPICFLAG_NOTPERSISTENT),
3787 srcPos[0], srcPos[1], rgb[0], rgb[1], rgb[2], alpha,
3788 srcPos[0] + srcSize[0], srcPos[1], rgb[0], rgb[1], rgb[2], alpha,
3789 srcPos[0], srcPos[1] + srcSize[1], rgb[0], rgb[1], rgb[2], alpha,
3790 srcPos[0] + srcSize[0], srcPos[1] + srcSize[1], rgb[0], rgb[1], rgb[2], alpha,
3792 PRVM_G_FLOAT(OFS_RETURN) = 1;
3799 float drawfill(vector position, vector size, vector rgb, float alpha, float flag)
3802 void VM_drawfill(void)
3804 float *size, *pos, *rgb;
3807 VM_SAFEPARMCOUNT(5,VM_drawfill);
3810 pos = PRVM_G_VECTOR(OFS_PARM0);
3811 size = PRVM_G_VECTOR(OFS_PARM1);
3812 rgb = PRVM_G_VECTOR(OFS_PARM2);
3813 flag = (int) PRVM_G_FLOAT(OFS_PARM4);
3815 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3817 PRVM_G_FLOAT(OFS_RETURN) = -2;
3818 VM_Warning("VM_drawfill: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
3822 if(pos[2] || size[2])
3823 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")));
3825 DrawQ_Fill(pos[0], pos[1], size[0], size[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM3), flag);
3826 PRVM_G_FLOAT(OFS_RETURN) = 1;
3833 drawsetcliparea(float x, float y, float width, float height)
3836 void VM_drawsetcliparea(void)
3839 VM_SAFEPARMCOUNT(4,VM_drawsetcliparea);
3841 x = bound(0, PRVM_G_FLOAT(OFS_PARM0), vid_conwidth.integer);
3842 y = bound(0, PRVM_G_FLOAT(OFS_PARM1), vid_conheight.integer);
3843 w = bound(0, PRVM_G_FLOAT(OFS_PARM2) + PRVM_G_FLOAT(OFS_PARM0) - x, (vid_conwidth.integer - x));
3844 h = bound(0, PRVM_G_FLOAT(OFS_PARM3) + PRVM_G_FLOAT(OFS_PARM1) - y, (vid_conheight.integer - y));
3846 DrawQ_SetClipArea(x, y, w, h);
3851 VM_drawresetcliparea
3856 void VM_drawresetcliparea(void)
3858 VM_SAFEPARMCOUNT(0,VM_drawresetcliparea);
3860 DrawQ_ResetClipArea();
3867 vector getimagesize(string pic)
3870 void VM_getimagesize(void)
3875 VM_SAFEPARMCOUNT(1,VM_getimagesize);
3877 p = PRVM_G_STRING(OFS_PARM0);
3878 VM_CheckEmptyString (p);
3880 pic = Draw_CachePic_Flags (p, CACHEPICFLAG_NOTPERSISTENT);
3882 PRVM_G_VECTOR(OFS_RETURN)[0] = pic->width;
3883 PRVM_G_VECTOR(OFS_RETURN)[1] = pic->height;
3884 PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
3891 string keynumtostring(float keynum)
3894 void VM_keynumtostring (void)
3896 VM_SAFEPARMCOUNT(1, VM_keynumtostring);
3898 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(Key_KeynumToString((int)PRVM_G_FLOAT(OFS_PARM0)));
3903 VM_findkeysforcommand
3905 string findkeysforcommand(string command, float bindmap)
3907 the returned string is an altstring
3910 #define FKFC_NUMKEYS 5
3911 void M_FindKeysForCommand(const char *command, int *keys);
3912 void VM_findkeysforcommand(void)
3915 char ret[VM_STRINGTEMP_LENGTH];
3916 int keys[FKFC_NUMKEYS];
3920 VM_SAFEPARMCOUNTRANGE(1, 2, VM_findkeysforcommand);
3922 cmd = PRVM_G_STRING(OFS_PARM0);
3924 bindmap = bound(-1, PRVM_G_FLOAT(OFS_PARM1), MAX_BINDMAPS-1);
3926 bindmap = 0; // consistent to "bind"
3928 VM_CheckEmptyString(cmd);
3930 Key_FindKeysForCommand(cmd, keys, FKFC_NUMKEYS, bindmap);
3933 for(i = 0; i < FKFC_NUMKEYS; i++)
3934 strlcat(ret, va(" \'%i\'", keys[i]), sizeof(ret));
3936 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(ret);
3943 float stringtokeynum(string key)
3946 void VM_stringtokeynum (void)
3948 VM_SAFEPARMCOUNT( 1, VM_keynumtostring );
3950 PRVM_G_FLOAT(OFS_RETURN) = Key_StringToKeynum(PRVM_G_STRING(OFS_PARM0));
3957 string getkeybind(float key, float bindmap)
3960 void VM_getkeybind (void)
3963 VM_SAFEPARMCOUNTRANGE(1, 2, VM_CL_getkeybind);
3965 bindmap = bound(-1, PRVM_G_FLOAT(OFS_PARM1), MAX_BINDMAPS-1);
3967 bindmap = 0; // consistent to "bind"
3969 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(Key_GetBind((int)PRVM_G_FLOAT(OFS_PARM0), bindmap));
3976 float setkeybind(float key, string cmd, float bindmap)
3979 void VM_setkeybind (void)
3982 VM_SAFEPARMCOUNTRANGE(2, 3, VM_CL_setkeybind);
3984 bindmap = bound(-1, PRVM_G_FLOAT(OFS_PARM2), MAX_BINDMAPS-1);
3986 bindmap = 0; // consistent to "bind"
3988 PRVM_G_FLOAT(OFS_RETURN) = 0;
3989 if(Key_SetBinding((int)PRVM_G_FLOAT(OFS_PARM0), bindmap, PRVM_G_STRING(OFS_PARM1)))
3990 PRVM_G_FLOAT(OFS_RETURN) = 1;
3997 vector getbindmaps()
4000 void VM_getbindmaps (void)
4003 VM_SAFEPARMCOUNT(0, VM_CL_getbindmap);
4004 Key_GetBindMap(&fg, &bg);
4005 PRVM_G_VECTOR(OFS_RETURN)[0] = fg;
4006 PRVM_G_VECTOR(OFS_RETURN)[1] = bg;
4007 PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
4014 float setbindmaps(vector bindmap)
4017 void VM_setbindmaps (void)
4019 VM_SAFEPARMCOUNT(1, VM_CL_setbindmap);
4020 PRVM_G_FLOAT(OFS_RETURN) = 0;
4021 if(PRVM_G_VECTOR(OFS_PARM0)[2] == 0)
4022 if(Key_SetBindMap((int)PRVM_G_VECTOR(OFS_PARM0)[0], (int)PRVM_G_VECTOR(OFS_PARM0)[1]))
4023 PRVM_G_FLOAT(OFS_RETURN) = 1;
4026 // CL_Video interface functions
4029 ========================
4032 float cin_open(string file, string name)
4033 ========================
4035 void VM_cin_open( void )
4040 VM_SAFEPARMCOUNT( 2, VM_cin_open );
4042 file = PRVM_G_STRING( OFS_PARM0 );
4043 name = PRVM_G_STRING( OFS_PARM1 );
4045 VM_CheckEmptyString( file );
4046 VM_CheckEmptyString( name );
4048 if( CL_OpenVideo( file, name, MENUOWNER, "" ) )
4049 PRVM_G_FLOAT( OFS_RETURN ) = 1;
4051 PRVM_G_FLOAT( OFS_RETURN ) = 0;
4055 ========================
4058 void cin_close(string name)
4059 ========================
4061 void VM_cin_close( void )
4065 VM_SAFEPARMCOUNT( 1, VM_cin_close );
4067 name = PRVM_G_STRING( OFS_PARM0 );
4068 VM_CheckEmptyString( name );
4070 CL_CloseVideo( CL_GetVideoByName( name ) );
4074 ========================
4076 void cin_setstate(string name, float type)
4077 ========================
4079 void VM_cin_setstate( void )
4082 clvideostate_t state;
4085 VM_SAFEPARMCOUNT( 2, VM_cin_netstate );
4087 name = PRVM_G_STRING( OFS_PARM0 );
4088 VM_CheckEmptyString( name );
4090 state = (clvideostate_t)((int)PRVM_G_FLOAT( OFS_PARM1 ));
4092 video = CL_GetVideoByName( name );
4093 if( video && state > CLVIDEO_UNUSED && state < CLVIDEO_STATECOUNT )
4094 CL_SetVideoState( video, state );
4098 ========================
4101 float cin_getstate(string name)
4102 ========================
4104 void VM_cin_getstate( void )
4109 VM_SAFEPARMCOUNT( 1, VM_cin_getstate );
4111 name = PRVM_G_STRING( OFS_PARM0 );
4112 VM_CheckEmptyString( name );
4114 video = CL_GetVideoByName( name );
4116 PRVM_G_FLOAT( OFS_RETURN ) = (int)video->state;
4118 PRVM_G_FLOAT( OFS_RETURN ) = 0;
4122 ========================
4125 void cin_restart(string name)
4126 ========================
4128 void VM_cin_restart( void )
4133 VM_SAFEPARMCOUNT( 1, VM_cin_restart );
4135 name = PRVM_G_STRING( OFS_PARM0 );
4136 VM_CheckEmptyString( name );
4138 video = CL_GetVideoByName( name );
4140 CL_RestartVideo( video );
4144 ========================
4146 ========================
4148 void VM_Gecko_Init( void ) {
4149 // the prog struct is memset to 0 by Initprog? [12/6/2007 Black]
4150 // FIXME: remove the other _Init functions then, too? [12/6/2007 Black]
4154 ========================
4156 ========================
4158 void VM_Gecko_Destroy( void ) {
4160 for( i = 0 ; i < PRVM_MAX_GECKOINSTANCES ; i++ ) {
4161 clgecko_t **instance = &prog->opengeckoinstances[ i ];
4163 CL_Gecko_DestroyBrowser( *instance );
4170 ========================
4173 float[bool] gecko_create( string name )
4174 ========================
4176 void VM_gecko_create( void ) {
4179 clgecko_t *instance;
4181 VM_SAFEPARMCOUNT( 1, VM_gecko_create );
4183 name = PRVM_G_STRING( OFS_PARM0 );
4184 VM_CheckEmptyString( name );
4186 // find an empty slot for this gecko browser..
4187 for( i = 0 ; i < PRVM_MAX_GECKOINSTANCES ; i++ ) {
4188 if( prog->opengeckoinstances[ i ] == NULL ) {
4192 if( i == PRVM_MAX_GECKOINSTANCES ) {
4193 VM_Warning("VM_gecko_create: %s ran out of gecko handles (%i)\n", PRVM_NAME, PRVM_MAX_GECKOINSTANCES);
4194 PRVM_G_FLOAT( OFS_RETURN ) = 0;
4198 instance = prog->opengeckoinstances[ i ] = CL_Gecko_CreateBrowser( name, PRVM_GetProgNr() );
4200 // TODO: error handling [12/3/2007 Black]
4201 PRVM_G_FLOAT( OFS_RETURN ) = 0;
4204 PRVM_G_FLOAT( OFS_RETURN ) = 1;
4208 ========================
4211 void gecko_destroy( string name )
4212 ========================
4214 void VM_gecko_destroy( void ) {
4216 clgecko_t *instance;
4218 VM_SAFEPARMCOUNT( 1, VM_gecko_destroy );
4220 name = PRVM_G_STRING( OFS_PARM0 );
4221 VM_CheckEmptyString( name );
4222 instance = CL_Gecko_FindBrowser( name );
4226 CL_Gecko_DestroyBrowser( instance );
4230 ========================
4233 void gecko_navigate( string name, string URI )
4234 ========================
4236 void VM_gecko_navigate( void ) {
4239 clgecko_t *instance;
4241 VM_SAFEPARMCOUNT( 2, VM_gecko_navigate );
4243 name = PRVM_G_STRING( OFS_PARM0 );
4244 URI = PRVM_G_STRING( OFS_PARM1 );
4245 VM_CheckEmptyString( name );
4246 VM_CheckEmptyString( URI );
4248 instance = CL_Gecko_FindBrowser( name );
4252 CL_Gecko_NavigateToURI( instance, URI );
4256 ========================
4259 float[bool] gecko_keyevent( string name, float key, float eventtype )
4260 ========================
4262 void VM_gecko_keyevent( void ) {
4265 clgecko_buttoneventtype_t eventtype;
4266 clgecko_t *instance;
4268 VM_SAFEPARMCOUNT( 3, VM_gecko_keyevent );
4270 name = PRVM_G_STRING( OFS_PARM0 );
4271 VM_CheckEmptyString( name );
4272 key = (unsigned int) PRVM_G_FLOAT( OFS_PARM1 );
4273 switch( (unsigned int) PRVM_G_FLOAT( OFS_PARM2 ) ) {
4275 eventtype = CLG_BET_DOWN;
4278 eventtype = CLG_BET_UP;
4281 eventtype = CLG_BET_PRESS;
4284 eventtype = CLG_BET_DOUBLECLICK;
4287 // TODO: console printf? [12/3/2007 Black]
4288 PRVM_G_FLOAT( OFS_RETURN ) = 0;
4292 instance = CL_Gecko_FindBrowser( name );
4294 PRVM_G_FLOAT( OFS_RETURN ) = 0;
4298 PRVM_G_FLOAT( OFS_RETURN ) = (CL_Gecko_Event_Key( instance, (keynum_t) key, eventtype ) == true);
4302 ========================
4305 void gecko_mousemove( string name, float x, float y )
4306 ========================
4308 void VM_gecko_movemouse( void ) {
4311 clgecko_t *instance;
4313 VM_SAFEPARMCOUNT( 3, VM_gecko_movemouse );
4315 name = PRVM_G_STRING( OFS_PARM0 );
4316 VM_CheckEmptyString( name );
4317 x = PRVM_G_FLOAT( OFS_PARM1 );
4318 y = PRVM_G_FLOAT( OFS_PARM2 );
4320 instance = CL_Gecko_FindBrowser( name );
4324 CL_Gecko_Event_CursorMove( instance, x, y );
4329 ========================
4332 void gecko_resize( string name, float w, float h )
4333 ========================
4335 void VM_gecko_resize( void ) {
4338 clgecko_t *instance;
4340 VM_SAFEPARMCOUNT( 3, VM_gecko_movemouse );
4342 name = PRVM_G_STRING( OFS_PARM0 );
4343 VM_CheckEmptyString( name );
4344 w = PRVM_G_FLOAT( OFS_PARM1 );
4345 h = PRVM_G_FLOAT( OFS_PARM2 );
4347 instance = CL_Gecko_FindBrowser( name );
4351 CL_Gecko_Resize( instance, (int) w, (int) h );
4356 ========================
4357 VM_gecko_get_texture_extent
4359 vector gecko_get_texture_extent( string name )
4360 ========================
4362 void VM_gecko_get_texture_extent( void ) {
4364 clgecko_t *instance;
4366 VM_SAFEPARMCOUNT( 1, VM_gecko_movemouse );
4368 name = PRVM_G_STRING( OFS_PARM0 );
4369 VM_CheckEmptyString( name );
4371 PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
4372 instance = CL_Gecko_FindBrowser( name );
4374 PRVM_G_VECTOR(OFS_RETURN)[0] = 0;
4375 PRVM_G_VECTOR(OFS_RETURN)[1] = 0;
4378 CL_Gecko_GetTextureExtent( instance,
4379 PRVM_G_VECTOR(OFS_RETURN), PRVM_G_VECTOR(OFS_RETURN)+1 );
4388 Writes new values for v_forward, v_up, and v_right based on angles
4389 void makevectors(vector angle)
4392 void VM_makevectors (void)
4394 prvm_eval_t *valforward, *valright, *valup;
4395 valforward = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_forward);
4396 valright = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_right);
4397 valup = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_up);
4398 if (!valforward || !valright || !valup)
4400 VM_Warning("makevectors: could not find v_forward, v_right, or v_up global variables\n");
4403 VM_SAFEPARMCOUNT(1, VM_makevectors);
4404 AngleVectors (PRVM_G_VECTOR(OFS_PARM0), valforward->vector, valright->vector, valup->vector);
4411 Writes new values for v_forward, v_up, and v_right based on the given forward vector
4412 vectorvectors(vector)
4415 void VM_vectorvectors (void)
4417 prvm_eval_t *valforward, *valright, *valup;
4418 valforward = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_forward);
4419 valright = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_right);
4420 valup = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_up);
4421 if (!valforward || !valright || !valup)
4423 VM_Warning("vectorvectors: could not find v_forward, v_right, or v_up global variables\n");
4426 VM_SAFEPARMCOUNT(1, VM_vectorvectors);
4427 VectorNormalize2(PRVM_G_VECTOR(OFS_PARM0), valforward->vector);
4428 VectorVectors(valforward->vector, valright->vector, valup->vector);
4432 ========================
4435 void drawline(float width, vector pos1, vector pos2, vector rgb, float alpha, float flags)
4436 ========================
4438 void VM_drawline (void)
4440 float *c1, *c2, *rgb;
4442 unsigned char flags;
4444 VM_SAFEPARMCOUNT(6, VM_drawline);
4445 width = PRVM_G_FLOAT(OFS_PARM0);
4446 c1 = PRVM_G_VECTOR(OFS_PARM1);
4447 c2 = PRVM_G_VECTOR(OFS_PARM2);
4448 rgb = PRVM_G_VECTOR(OFS_PARM3);
4449 alpha = PRVM_G_FLOAT(OFS_PARM4);
4450 flags = (int)PRVM_G_FLOAT(OFS_PARM5);
4451 DrawQ_Line(width, c1[0], c1[1], c2[0], c2[1], rgb[0], rgb[1], rgb[2], alpha, flags);
4454 // float(float number, float quantity) bitshift (EXT_BITSHIFT)
4455 void VM_bitshift (void)
4458 VM_SAFEPARMCOUNT(2, VM_bitshift);
4460 n1 = (int)fabs((float)((int)PRVM_G_FLOAT(OFS_PARM0)));
4461 n2 = (int)PRVM_G_FLOAT(OFS_PARM1);
4463 PRVM_G_FLOAT(OFS_RETURN) = n1;
4466 PRVM_G_FLOAT(OFS_RETURN) = (n1 >> -n2);
4468 PRVM_G_FLOAT(OFS_RETURN) = (n1 << n2);
4471 ////////////////////////////////////////
4472 // AltString functions
4473 ////////////////////////////////////////
4476 ========================
4479 float altstr_count(string)
4480 ========================
4482 void VM_altstr_count( void )
4484 const char *altstr, *pos;
4487 VM_SAFEPARMCOUNT( 1, VM_altstr_count );
4489 altstr = PRVM_G_STRING( OFS_PARM0 );
4490 //VM_CheckEmptyString( altstr );
4492 for( count = 0, pos = altstr ; *pos ; pos++ ) {
4493 if( *pos == '\\' ) {
4497 } else if( *pos == '\'' ) {
4502 PRVM_G_FLOAT( OFS_RETURN ) = (float) (count / 2);
4506 ========================
4509 string altstr_prepare(string)
4510 ========================
4512 void VM_altstr_prepare( void )
4515 const char *instr, *in;
4517 char outstr[VM_STRINGTEMP_LENGTH];
4519 VM_SAFEPARMCOUNT( 1, VM_altstr_prepare );
4521 instr = PRVM_G_STRING( OFS_PARM0 );
4523 for( out = outstr, in = instr, size = sizeof(outstr) - 1 ; size && *in ; size--, in++, out++ )
4532 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
4536 ========================
4539 string altstr_get(string, float)
4540 ========================
4542 void VM_altstr_get( void )
4544 const char *altstr, *pos;
4547 char outstr[VM_STRINGTEMP_LENGTH];
4549 VM_SAFEPARMCOUNT( 2, VM_altstr_get );
4551 altstr = PRVM_G_STRING( OFS_PARM0 );
4553 count = (int)PRVM_G_FLOAT( OFS_PARM1 );
4554 count = count * 2 + 1;
4556 for( pos = altstr ; *pos && count ; pos++ )
4557 if( *pos == '\\' ) {
4560 } else if( *pos == '\'' )
4564 PRVM_G_INT( OFS_RETURN ) = 0;
4568 for( out = outstr, size = sizeof(outstr) - 1 ; size && *pos ; size--, pos++, out++ )
4569 if( *pos == '\\' ) {
4574 } else if( *pos == '\'' )
4580 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
4584 ========================
4587 string altstr_set(string altstr, float num, string set)
4588 ========================
4590 void VM_altstr_set( void )
4593 const char *altstr, *str;
4596 char outstr[VM_STRINGTEMP_LENGTH];
4598 VM_SAFEPARMCOUNT( 3, VM_altstr_set );
4600 altstr = PRVM_G_STRING( OFS_PARM0 );
4602 num = (int)PRVM_G_FLOAT( OFS_PARM1 );
4604 str = PRVM_G_STRING( OFS_PARM2 );
4607 for( num = num * 2 + 1, in = altstr; *in && num; *out++ = *in++ )
4612 } else if( *in == '\'' ) {
4617 for( ; *str; *out++ = *str++ );
4618 // now jump over the old content
4620 if( *in == '\'' || (*in == '\\' && !*++in) )
4623 strlcpy(out, in, outstr + sizeof(outstr) - out);
4624 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
4628 ========================
4631 string altstr_ins(string altstr, float num, string set)
4632 ========================
4634 void VM_altstr_ins(void)
4640 char outstr[VM_STRINGTEMP_LENGTH];
4642 VM_SAFEPARMCOUNT(3, VM_altstr_ins);
4644 in = PRVM_G_STRING( OFS_PARM0 );
4645 num = (int)PRVM_G_FLOAT( OFS_PARM1 );
4646 set = PRVM_G_STRING( OFS_PARM2 );
4649 for( num = num * 2 + 2 ; *in && num > 0 ; *out++ = *in++ )
4654 } else if( *in == '\'' ) {
4659 for( ; *set ; *out++ = *set++ );
4662 strlcpy(out, in, outstr + sizeof(outstr) - out);
4663 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
4667 ////////////////////////////////////////
4668 // BufString functions
4669 ////////////////////////////////////////
4670 //[515]: string buffers support
4672 static size_t stringbuffers_sortlength;
4674 static void BufStr_Expand(prvm_stringbuffer_t *stringbuffer, int strindex)
4676 if (stringbuffer->max_strings <= strindex)
4678 char **oldstrings = stringbuffer->strings;
4679 stringbuffer->max_strings = max(stringbuffer->max_strings * 2, 128);
4680 while (stringbuffer->max_strings <= strindex)
4681 stringbuffer->max_strings *= 2;
4682 stringbuffer->strings = (char **) Mem_Alloc(prog->progs_mempool, stringbuffer->max_strings * sizeof(stringbuffer->strings[0]));
4683 if (stringbuffer->num_strings > 0)
4684 memcpy(stringbuffer->strings, oldstrings, stringbuffer->num_strings * sizeof(stringbuffer->strings[0]));
4686 Mem_Free(oldstrings);
4690 static void BufStr_Shrink(prvm_stringbuffer_t *stringbuffer)
4692 // reduce num_strings if there are empty string slots at the end
4693 while (stringbuffer->num_strings > 0 && stringbuffer->strings[stringbuffer->num_strings - 1] == NULL)
4694 stringbuffer->num_strings--;
4696 // if empty, free the string pointer array
4697 if (stringbuffer->num_strings == 0)
4699 stringbuffer->max_strings = 0;
4700 if (stringbuffer->strings)
4701 Mem_Free(stringbuffer->strings);
4702 stringbuffer->strings = NULL;
4706 static int BufStr_SortStringsUP (const void *in1, const void *in2)
4709 a = *((const char **) in1);
4710 b = *((const char **) in2);
4711 if(!a || !a[0]) return 1;
4712 if(!b || !b[0]) return -1;
4713 return strncmp(a, b, stringbuffers_sortlength);
4716 static int BufStr_SortStringsDOWN (const void *in1, const void *in2)
4719 a = *((const char **) in1);
4720 b = *((const char **) in2);
4721 if(!a || !a[0]) return 1;
4722 if(!b || !b[0]) return -1;
4723 return strncmp(b, a, stringbuffers_sortlength);
4727 ========================
4729 creates new buffer, and returns it's index, returns -1 if failed
4730 float buf_create(void) = #460;
4731 float newbuf(string format, float flags) = #460;
4732 ========================
4735 void VM_buf_create (void)
4737 prvm_stringbuffer_t *stringbuffer;
4740 VM_SAFEPARMCOUNTRANGE(0, 2, VM_buf_create);
4742 // VorteX: optional parm1 (buffer format) is unfinished, to keep intact with future databuffers extension must be set to "string"
4743 if(prog->argc >= 1 && strcmp(PRVM_G_STRING(OFS_PARM0), "string"))
4745 PRVM_G_FLOAT(OFS_RETURN) = -1;
4748 stringbuffer = (prvm_stringbuffer_t *) Mem_ExpandableArray_AllocRecord(&prog->stringbuffersarray);
4749 for (i = 0;stringbuffer != Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, i);i++);
4750 stringbuffer->origin = PRVM_AllocationOrigin();
4751 // optional flags parm
4753 stringbuffer->flags = (int)PRVM_G_FLOAT(OFS_PARM1) & 0xFF;
4754 PRVM_G_FLOAT(OFS_RETURN) = i;
4760 ========================
4762 deletes buffer and all strings in it
4763 void buf_del(float bufhandle) = #461;
4764 ========================
4766 void VM_buf_del (void)
4768 prvm_stringbuffer_t *stringbuffer;
4769 VM_SAFEPARMCOUNT(1, VM_buf_del);
4770 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4774 for (i = 0;i < stringbuffer->num_strings;i++)
4775 if (stringbuffer->strings[i])
4776 Mem_Free(stringbuffer->strings[i]);
4777 if (stringbuffer->strings)
4778 Mem_Free(stringbuffer->strings);
4779 if(stringbuffer->origin)
4780 PRVM_Free((char *)stringbuffer->origin);
4781 Mem_ExpandableArray_FreeRecord(&prog->stringbuffersarray, stringbuffer);
4785 VM_Warning("VM_buf_del: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4791 ========================
4793 how many strings are stored in buffer
4794 float buf_getsize(float bufhandle) = #462;
4795 ========================
4797 void VM_buf_getsize (void)
4799 prvm_stringbuffer_t *stringbuffer;
4800 VM_SAFEPARMCOUNT(1, VM_buf_getsize);
4802 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4805 PRVM_G_FLOAT(OFS_RETURN) = -1;
4806 VM_Warning("VM_buf_getsize: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4810 PRVM_G_FLOAT(OFS_RETURN) = stringbuffer->num_strings;
4814 ========================
4816 copy all content from one buffer to another, make sure it exists
4817 void buf_copy(float bufhandle_from, float bufhandle_to) = #463;
4818 ========================
4820 void VM_buf_copy (void)
4822 prvm_stringbuffer_t *srcstringbuffer, *dststringbuffer;
4824 VM_SAFEPARMCOUNT(2, VM_buf_copy);
4826 srcstringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4827 if(!srcstringbuffer)
4829 VM_Warning("VM_buf_copy: invalid source buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4832 i = (int)PRVM_G_FLOAT(OFS_PARM1);
4833 if(i == (int)PRVM_G_FLOAT(OFS_PARM0))
4835 VM_Warning("VM_buf_copy: source == destination (%i) in %s\n", i, PRVM_NAME);
4838 dststringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4839 if(!dststringbuffer)
4841 VM_Warning("VM_buf_copy: invalid destination buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM1), PRVM_NAME);
4845 for (i = 0;i < dststringbuffer->num_strings;i++)
4846 if (dststringbuffer->strings[i])
4847 Mem_Free(dststringbuffer->strings[i]);
4848 if (dststringbuffer->strings)
4849 Mem_Free(dststringbuffer->strings);
4850 *dststringbuffer = *srcstringbuffer;
4851 if (dststringbuffer->max_strings)
4852 dststringbuffer->strings = (char **)Mem_Alloc(prog->progs_mempool, sizeof(dststringbuffer->strings[0]) * dststringbuffer->max_strings);
4854 for (i = 0;i < dststringbuffer->num_strings;i++)
4856 if (srcstringbuffer->strings[i])
4859 stringlen = strlen(srcstringbuffer->strings[i]) + 1;
4860 dststringbuffer->strings[i] = (char *)Mem_Alloc(prog->progs_mempool, stringlen);
4861 memcpy(dststringbuffer->strings[i], srcstringbuffer->strings[i], stringlen);
4867 ========================
4869 sort buffer by beginnings of strings (cmplength defaults it's length)
4870 "backward == TRUE" means that sorting goes upside-down
4871 void buf_sort(float bufhandle, float cmplength, float backward) = #464;
4872 ========================
4874 void VM_buf_sort (void)
4876 prvm_stringbuffer_t *stringbuffer;
4877 VM_SAFEPARMCOUNT(3, VM_buf_sort);
4879 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4882 VM_Warning("VM_buf_sort: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4885 if(stringbuffer->num_strings <= 0)
4887 VM_Warning("VM_buf_sort: tried to sort empty buffer %i in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4890 stringbuffers_sortlength = (int)PRVM_G_FLOAT(OFS_PARM1);
4891 if(stringbuffers_sortlength <= 0)
4892 stringbuffers_sortlength = 0x7FFFFFFF;
4894 if(!PRVM_G_FLOAT(OFS_PARM2))
4895 qsort(stringbuffer->strings, stringbuffer->num_strings, sizeof(char*), BufStr_SortStringsUP);
4897 qsort(stringbuffer->strings, stringbuffer->num_strings, sizeof(char*), BufStr_SortStringsDOWN);
4899 BufStr_Shrink(stringbuffer);
4903 ========================
4905 concantenates all buffer string into one with "glue" separator and returns it as tempstring
4906 string buf_implode(float bufhandle, string glue) = #465;
4907 ========================
4909 void VM_buf_implode (void)
4911 prvm_stringbuffer_t *stringbuffer;
4912 char k[VM_STRINGTEMP_LENGTH];
4916 VM_SAFEPARMCOUNT(2, VM_buf_implode);
4918 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4919 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
4922 VM_Warning("VM_buf_implode: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4925 if(!stringbuffer->num_strings)
4927 sep = PRVM_G_STRING(OFS_PARM1);
4929 for(l = i = 0;i < stringbuffer->num_strings;i++)
4931 if(stringbuffer->strings[i])
4933 l += (i > 0 ? strlen(sep) : 0) + strlen(stringbuffer->strings[i]);
4934 if (l >= sizeof(k) - 1)
4936 strlcat(k, sep, sizeof(k));
4937 strlcat(k, stringbuffer->strings[i], sizeof(k));
4940 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(k);
4944 ========================
4946 get a string from buffer, returns tempstring, dont str_unzone it!
4947 string bufstr_get(float bufhandle, float string_index) = #465;
4948 ========================
4950 void VM_bufstr_get (void)
4952 prvm_stringbuffer_t *stringbuffer;
4954 VM_SAFEPARMCOUNT(2, VM_bufstr_get);
4956 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
4957 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4960 VM_Warning("VM_bufstr_get: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4963 strindex = (int)PRVM_G_FLOAT(OFS_PARM1);
4966 VM_Warning("VM_bufstr_get: invalid string index %i used in %s\n", strindex, PRVM_NAME);
4969 if (strindex < stringbuffer->num_strings && stringbuffer->strings[strindex])
4970 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(stringbuffer->strings[strindex]);
4974 ========================
4976 copies a string into selected slot of buffer
4977 void bufstr_set(float bufhandle, float string_index, string str) = #466;
4978 ========================
4980 void VM_bufstr_set (void)
4984 prvm_stringbuffer_t *stringbuffer;
4987 VM_SAFEPARMCOUNT(3, VM_bufstr_set);
4989 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4992 VM_Warning("VM_bufstr_set: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4995 strindex = (int)PRVM_G_FLOAT(OFS_PARM1);
4996 if(strindex < 0 || strindex >= 1000000) // huge number of strings
4998 VM_Warning("VM_bufstr_set: invalid string index %i used in %s\n", strindex, PRVM_NAME);
5002 BufStr_Expand(stringbuffer, strindex);
5003 stringbuffer->num_strings = max(stringbuffer->num_strings, strindex + 1);
5005 if(stringbuffer->strings[strindex])
5006 Mem_Free(stringbuffer->strings[strindex]);
5007 stringbuffer->strings[strindex] = NULL;
5009 if(PRVM_G_INT(OFS_PARM2))
5011 // not the NULL string!
5012 news = PRVM_G_STRING(OFS_PARM2);
5013 alloclen = strlen(news) + 1;
5014 stringbuffer->strings[strindex] = (char *)Mem_Alloc(prog->progs_mempool, alloclen);
5015 memcpy(stringbuffer->strings[strindex], news, alloclen);
5018 BufStr_Shrink(stringbuffer);
5022 ========================
5024 adds string to buffer in first free slot and returns its index
5025 "order == TRUE" means that string will be added after last "full" slot
5026 float bufstr_add(float bufhandle, string str, float order) = #467;
5027 ========================
5029 void VM_bufstr_add (void)
5031 int order, strindex;
5032 prvm_stringbuffer_t *stringbuffer;
5036 VM_SAFEPARMCOUNT(3, VM_bufstr_add);
5038 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
5039 PRVM_G_FLOAT(OFS_RETURN) = -1;
5042 VM_Warning("VM_bufstr_add: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
5045 if(!PRVM_G_INT(OFS_PARM1)) // NULL string
5047 VM_Warning("VM_bufstr_add: can not add an empty string to buffer %i in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
5050 string = PRVM_G_STRING(OFS_PARM1);
5051 order = (int)PRVM_G_FLOAT(OFS_PARM2);
5053 strindex = stringbuffer->num_strings;
5055 for (strindex = 0;strindex < stringbuffer->num_strings;strindex++)
5056 if (stringbuffer->strings[strindex] == NULL)
5059 BufStr_Expand(stringbuffer, strindex);
5061 stringbuffer->num_strings = max(stringbuffer->num_strings, strindex + 1);
5062 alloclen = strlen(string) + 1;
5063 stringbuffer->strings[strindex] = (char *)Mem_Alloc(prog->progs_mempool, alloclen);
5064 memcpy(stringbuffer->strings[strindex], string, alloclen);
5066 PRVM_G_FLOAT(OFS_RETURN) = strindex;
5070 ========================
5072 delete string from buffer
5073 void bufstr_free(float bufhandle, float string_index) = #468;
5074 ========================
5076 void VM_bufstr_free (void)
5079 prvm_stringbuffer_t *stringbuffer;
5080 VM_SAFEPARMCOUNT(2, VM_bufstr_free);
5082 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
5085 VM_Warning("VM_bufstr_free: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
5088 i = (int)PRVM_G_FLOAT(OFS_PARM1);
5091 VM_Warning("VM_bufstr_free: invalid string index %i used in %s\n", i, PRVM_NAME);
5095 if (i < stringbuffer->num_strings)
5097 if(stringbuffer->strings[i])
5098 Mem_Free(stringbuffer->strings[i]);
5099 stringbuffer->strings[i] = NULL;
5102 BufStr_Shrink(stringbuffer);
5111 void VM_buf_cvarlist(void)
5114 const char *partial, *antipartial;
5115 size_t len, antilen;
5117 qboolean ispattern, antiispattern;
5119 prvm_stringbuffer_t *stringbuffer;
5120 VM_SAFEPARMCOUNTRANGE(2, 3, VM_buf_cvarlist);
5122 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
5125 VM_Warning("VM_bufstr_free: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
5129 partial = PRVM_G_STRING(OFS_PARM1);
5133 len = strlen(partial);
5136 antipartial = PRVM_G_STRING(OFS_PARM2);
5142 antilen = strlen(antipartial);
5144 for (n = 0;n < stringbuffer->num_strings;n++)
5145 if (stringbuffer->strings[n])
5146 Mem_Free(stringbuffer->strings[n]);
5147 if (stringbuffer->strings)
5148 Mem_Free(stringbuffer->strings);
5149 stringbuffer->strings = NULL;
5151 ispattern = partial && (strchr(partial, '*') || strchr(partial, '?'));
5152 antiispattern = antipartial && (strchr(antipartial, '*') || strchr(antipartial, '?'));
5155 for(cvar = cvar_vars; cvar; cvar = cvar->next)
5157 if(len && (ispattern ? !matchpattern_with_separator(cvar->name, partial, false, "", false) : strncmp(partial, cvar->name, len)))
5160 if(antilen && (antiispattern ? matchpattern_with_separator(cvar->name, antipartial, false, "", false) : !strncmp(antipartial, cvar->name, antilen)))
5166 stringbuffer->max_strings = stringbuffer->num_strings = n;
5167 if (stringbuffer->max_strings)
5168 stringbuffer->strings = (char **)Mem_Alloc(prog->progs_mempool, sizeof(stringbuffer->strings[0]) * stringbuffer->max_strings);
5171 for(cvar = cvar_vars; cvar; cvar = cvar->next)
5173 if(len && (ispattern ? !matchpattern_with_separator(cvar->name, partial, false, "", false) : strncmp(partial, cvar->name, len)))
5176 if(antilen && (antiispattern ? matchpattern_with_separator(cvar->name, antipartial, false, "", false) : !strncmp(antipartial, cvar->name, antilen)))
5179 alloclen = strlen(cvar->name) + 1;
5180 stringbuffer->strings[n] = (char *)Mem_Alloc(prog->progs_mempool, alloclen);
5181 memcpy(stringbuffer->strings[n], cvar->name, alloclen);
5196 This was a major timewaster in progs, so it was converted to C
5199 void VM_changeyaw (void)
5202 float ideal, current, move, speed;
5204 // this is called (VERY HACKISHLY) by SV_MoveToGoal, so it can not use any
5205 // parameters because they are the parameters to SV_MoveToGoal, not this
5206 //VM_SAFEPARMCOUNT(0, VM_changeyaw);
5208 ent = PRVM_PROG_TO_EDICT(PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict);
5209 if (ent == prog->edicts)
5211 VM_Warning("changeyaw: can not modify world entity\n");
5214 if (ent->priv.server->free)
5216 VM_Warning("changeyaw: can not modify free entity\n");
5219 if (prog->fieldoffsets.angles < 0 || prog->fieldoffsets.ideal_yaw < 0 || prog->fieldoffsets.yaw_speed < 0)
5221 VM_Warning("changeyaw: angles, ideal_yaw, or yaw_speed field(s) not found\n");
5224 current = ANGLEMOD(PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.angles)->vector[1]);
5225 ideal = PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.ideal_yaw)->_float;
5226 speed = PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.yaw_speed)->_float;
5228 if (current == ideal)
5230 move = ideal - current;
5231 if (ideal > current)
5252 PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.angles)->vector[1] = ANGLEMOD (current + move);
5260 void VM_changepitch (void)
5263 float ideal, current, move, speed;
5265 VM_SAFEPARMCOUNT(1, VM_changepitch);
5267 ent = PRVM_G_EDICT(OFS_PARM0);
5268 if (ent == prog->edicts)
5270 VM_Warning("changepitch: can not modify world entity\n");
5273 if (ent->priv.server->free)
5275 VM_Warning("changepitch: can not modify free entity\n");
5278 if (prog->fieldoffsets.angles < 0 || prog->fieldoffsets.idealpitch < 0 || prog->fieldoffsets.pitch_speed < 0)
5280 VM_Warning("changepitch: angles, idealpitch, or pitch_speed field(s) not found\n");
5283 current = ANGLEMOD(PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.angles)->vector[0]);
5284 ideal = PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.idealpitch)->_float;
5285 speed = PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.pitch_speed)->_float;
5287 if (current == ideal)
5289 move = ideal - current;
5290 if (ideal > current)
5311 PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.angles)->vector[0] = ANGLEMOD (current + move);
5315 void VM_uncolorstring (void)
5317 char szNewString[VM_STRINGTEMP_LENGTH];
5318 const char *szString;
5321 VM_SAFEPARMCOUNT(1, VM_uncolorstring);
5322 szString = PRVM_G_STRING(OFS_PARM0);
5323 COM_StringDecolorize(szString, 0, szNewString, sizeof(szNewString), TRUE);
5324 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(szNewString);
5328 // #221 float(string str, string sub[, float startpos]) strstrofs (FTE_STRINGS)
5329 //strstr, without generating a new string. Use in conjunction with FRIK_FILE's substring for more similar strstr.
5330 void VM_strstrofs (void)
5332 const char *instr, *match;
5334 VM_SAFEPARMCOUNTRANGE(2, 3, VM_strstrofs);
5335 instr = PRVM_G_STRING(OFS_PARM0);
5336 match = PRVM_G_STRING(OFS_PARM1);
5337 firstofs = (prog->argc > 2)?(int)PRVM_G_FLOAT(OFS_PARM2):0;
5338 firstofs = u8_bytelen(instr, firstofs);
5340 if (firstofs && (firstofs < 0 || firstofs > (int)strlen(instr)))
5342 PRVM_G_FLOAT(OFS_RETURN) = -1;
5346 match = strstr(instr+firstofs, match);
5348 PRVM_G_FLOAT(OFS_RETURN) = -1;
5350 PRVM_G_FLOAT(OFS_RETURN) = u8_strnlen(instr, match-instr);
5353 //#222 string(string s, float index) str2chr (FTE_STRINGS)
5354 void VM_str2chr (void)
5359 VM_SAFEPARMCOUNT(2, VM_str2chr);
5360 s = PRVM_G_STRING(OFS_PARM0);
5361 index = u8_bytelen(s, (int)PRVM_G_FLOAT(OFS_PARM1));
5363 if((unsigned)index < strlen(s))
5365 if (utf8_enable.integer)
5366 ch = u8_getchar_noendptr(s + index);
5368 ch = (unsigned char)s[index];
5369 PRVM_G_FLOAT(OFS_RETURN) = ch;
5372 PRVM_G_FLOAT(OFS_RETURN) = 0;
5375 //#223 string(float c, ...) chr2str (FTE_STRINGS)
5376 void VM_chr2str (void)
5381 VM_SAFEPARMCOUNTRANGE(0, 8, VM_chr2str);
5382 for(i = 0;i < prog->argc && i < (int)sizeof(t) - 1;i++)
5383 t[i] = (unsigned char)PRVM_G_FLOAT(OFS_PARM0+i*3);
5385 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(t);
5390 VM_SAFEPARMCOUNTRANGE(0, 8, VM_chr2str);
5391 for(i = 0; i < prog->argc && len < sizeof(t)-1; ++i)
5392 len += u8_fromchar((Uchar)PRVM_G_FLOAT(OFS_PARM0+i*3), t + len, sizeof(t)-1);
5394 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(t);
5397 static int chrconv_number(int i, int base, int conv)
5422 static int chrconv_punct(int i, int base, int conv)
5440 static int chrchar_alpha(int i, int basec, int baset, int convc, int convt, int charnum)
5442 //convert case and colour seperatly...
5459 baset = 128*((charnum&1) == (convt-5));
5475 return i + basec + baset;
5477 // #224 string(float ccase, float calpha, float cnum, string s, ...) strconv (FTE_STRINGS)
5478 //bulk convert a string. change case or colouring.
5479 void VM_strconv (void)
5481 int ccase, redalpha, rednum, len, i;
5482 unsigned char resbuf[VM_STRINGTEMP_LENGTH];
5483 unsigned char *result = resbuf;
5485 VM_SAFEPARMCOUNTRANGE(3, 8, VM_strconv);
5487 ccase = (int) PRVM_G_FLOAT(OFS_PARM0); //0 same, 1 lower, 2 upper
5488 redalpha = (int) PRVM_G_FLOAT(OFS_PARM1); //0 same, 1 white, 2 red, 5 alternate, 6 alternate-alternate
5489 rednum = (int) PRVM_G_FLOAT(OFS_PARM2); //0 same, 1 white, 2 red, 3 redspecial, 4 whitespecial, 5 alternate, 6 alternate-alternate
5490 VM_VarString(3, (char *) resbuf, sizeof(resbuf));
5491 len = strlen((char *) resbuf);
5493 for (i = 0; i < len; i++, result++) //should this be done backwards?
5495 if (*result >= '0' && *result <= '9') //normal numbers...
5496 *result = chrconv_number(*result, '0', rednum);
5497 else if (*result >= '0'+128 && *result <= '9'+128)
5498 *result = chrconv_number(*result, '0'+128, rednum);
5499 else if (*result >= '0'+128-30 && *result <= '9'+128-30)
5500 *result = chrconv_number(*result, '0'+128-30, rednum);
5501 else if (*result >= '0'-30 && *result <= '9'-30)
5502 *result = chrconv_number(*result, '0'-30, rednum);
5504 else if (*result >= 'a' && *result <= 'z') //normal numbers...
5505 *result = chrchar_alpha(*result, 'a', 0, ccase, redalpha, i);
5506 else if (*result >= 'A' && *result <= 'Z') //normal numbers...
5507 *result = chrchar_alpha(*result, 'A', 0, ccase, redalpha, i);
5508 else if (*result >= 'a'+128 && *result <= 'z'+128) //normal numbers...
5509 *result = chrchar_alpha(*result, 'a', 128, ccase, redalpha, i);
5510 else if (*result >= 'A'+128 && *result <= 'Z'+128) //normal numbers...
5511 *result = chrchar_alpha(*result, 'A', 128, ccase, redalpha, i);
5513 else if ((*result & 127) < 16 || !redalpha) //special chars..
5515 else if (*result < 128)
5516 *result = chrconv_punct(*result, 0, redalpha);
5518 *result = chrconv_punct(*result, 128, redalpha);
5522 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString((char *) resbuf);
5525 // #225 string(float chars, string s, ...) strpad (FTE_STRINGS)
5526 void VM_strpad (void)
5528 char src[VM_STRINGTEMP_LENGTH];
5529 char destbuf[VM_STRINGTEMP_LENGTH];
5531 VM_SAFEPARMCOUNTRANGE(1, 8, VM_strpad);
5532 pad = (int) PRVM_G_FLOAT(OFS_PARM0);
5533 VM_VarString(1, src, sizeof(src));
5535 // note: < 0 = left padding, > 0 = right padding,
5536 // this is reverse logic of printf!
5537 dpsnprintf(destbuf, sizeof(destbuf), "%*s", -pad, src);
5539 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(destbuf);
5542 // #226 string(string info, string key, string value, ...) infoadd (FTE_STRINGS)
5543 //uses qw style \key\value strings
5544 void VM_infoadd (void)
5546 const char *info, *key;
5547 char value[VM_STRINGTEMP_LENGTH];
5548 char temp[VM_STRINGTEMP_LENGTH];
5550 VM_SAFEPARMCOUNTRANGE(2, 8, VM_infoadd);
5551 info = PRVM_G_STRING(OFS_PARM0);
5552 key = PRVM_G_STRING(OFS_PARM1);
5553 VM_VarString(2, value, sizeof(value));
5555 strlcpy(temp, info, VM_STRINGTEMP_LENGTH);
5557 InfoString_SetValue(temp, VM_STRINGTEMP_LENGTH, key, value);
5559 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(temp);
5562 // #227 string(string info, string key) infoget (FTE_STRINGS)
5563 //uses qw style \key\value strings
5564 void VM_infoget (void)
5568 char value[VM_STRINGTEMP_LENGTH];
5570 VM_SAFEPARMCOUNT(2, VM_infoget);
5571 info = PRVM_G_STRING(OFS_PARM0);
5572 key = PRVM_G_STRING(OFS_PARM1);
5574 InfoString_GetValue(info, key, value, VM_STRINGTEMP_LENGTH);
5576 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(value);
5579 //#228 float(string s1, string s2, float len) strncmp (FTE_STRINGS)
5580 // also float(string s1, string s2) strcmp (FRIK_FILE)
5581 void VM_strncmp (void)
5583 const char *s1, *s2;
5584 VM_SAFEPARMCOUNTRANGE(2, 3, VM_strncmp);
5585 s1 = PRVM_G_STRING(OFS_PARM0);
5586 s2 = PRVM_G_STRING(OFS_PARM1);
5589 PRVM_G_FLOAT(OFS_RETURN) = strncmp(s1, s2, (size_t)PRVM_G_FLOAT(OFS_PARM2));
5593 PRVM_G_FLOAT(OFS_RETURN) = strcmp(s1, s2);
5597 // #229 float(string s1, string s2) strcasecmp (FTE_STRINGS)
5598 // #230 float(string s1, string s2, float len) strncasecmp (FTE_STRINGS)
5599 void VM_strncasecmp (void)
5601 const char *s1, *s2;
5602 VM_SAFEPARMCOUNTRANGE(2, 3, VM_strncasecmp);
5603 s1 = PRVM_G_STRING(OFS_PARM0);
5604 s2 = PRVM_G_STRING(OFS_PARM1);
5607 PRVM_G_FLOAT(OFS_RETURN) = strncasecmp(s1, s2, (size_t)PRVM_G_FLOAT(OFS_PARM2));
5611 PRVM_G_FLOAT(OFS_RETURN) = strcasecmp(s1, s2);
5615 // #494 float(float caseinsensitive, string s, ...) crc16
5619 static char s[VM_STRINGTEMP_LENGTH];
5620 VM_SAFEPARMCOUNTRANGE(2, 8, VM_hash);
5621 insensitive = PRVM_G_FLOAT(OFS_PARM0);
5622 VM_VarString(1, s, sizeof(s));
5623 PRVM_G_FLOAT(OFS_RETURN) = (unsigned short) ((insensitive ? CRC_Block_CaseInsensitive : CRC_Block) ((unsigned char *) s, strlen(s)));
5626 void VM_wasfreed (void)
5628 VM_SAFEPARMCOUNT(1, VM_wasfreed);
5629 PRVM_G_FLOAT(OFS_RETURN) = PRVM_G_EDICT(OFS_PARM0)->priv.required->free;
5632 void VM_SetTraceGlobals(const trace_t *trace)
5635 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_allsolid)))
5636 val->_float = trace->allsolid;
5637 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_startsolid)))
5638 val->_float = trace->startsolid;
5639 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_fraction)))
5640 val->_float = trace->fraction;
5641 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_inwater)))
5642 val->_float = trace->inwater;
5643 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_inopen)))
5644 val->_float = trace->inopen;
5645 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_endpos)))
5646 VectorCopy(trace->endpos, val->vector);
5647 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_plane_normal)))
5648 VectorCopy(trace->plane.normal, val->vector);
5649 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_plane_dist)))
5650 val->_float = trace->plane.dist;
5651 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_ent)))
5652 val->edict = PRVM_EDICT_TO_PROG(trace->ent ? trace->ent : prog->edicts);
5653 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dpstartcontents)))
5654 val->_float = trace->startsupercontents;
5655 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dphitcontents)))
5656 val->_float = trace->hitsupercontents;
5657 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dphitq3surfaceflags)))
5658 val->_float = trace->hitq3surfaceflags;
5659 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dphittexturename)))
5660 val->string = trace->hittexture ? PRVM_SetTempString(trace->hittexture->name) : 0;
5663 void VM_ClearTraceGlobals(void)
5665 // clean up all trace globals when leaving the VM (anti-triggerbot safeguard)
5667 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_allsolid)))
5669 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_startsolid)))
5671 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_fraction)))
5673 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_inwater)))
5675 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_inopen)))
5677 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_endpos)))
5678 VectorClear(val->vector);
5679 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_plane_normal)))
5680 VectorClear(val->vector);
5681 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_plane_dist)))
5683 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_ent)))
5684 val->edict = PRVM_EDICT_TO_PROG(prog->edicts);
5685 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dpstartcontents)))
5687 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dphitcontents)))
5689 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dphitq3surfaceflags)))
5691 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dphittexturename)))
5697 void VM_Cmd_Init(void)
5699 // only init the stuff for the current prog
5703 // VM_BufStr_Init();
5706 void VM_Cmd_Reset(void)
5708 CL_PurgeOwner( MENUOWNER );
5710 VM_Files_CloseAll();
5712 // VM_BufStr_ShutDown();
5715 // #510 string(string input, ...) uri_escape (DP_QC_URI_ESCAPE)
5716 // does URI escaping on a string (replace evil stuff by %AB escapes)
5717 void VM_uri_escape (void)
5719 char src[VM_STRINGTEMP_LENGTH];
5720 char dest[VM_STRINGTEMP_LENGTH];
5722 static const char *hex = "0123456789ABCDEF";
5724 VM_SAFEPARMCOUNTRANGE(1, 8, VM_uri_escape);
5725 VM_VarString(0, src, sizeof(src));
5727 for(p = src, q = dest; *p && q < dest + sizeof(dest) - 3; ++p)
5729 if((*p >= 'A' && *p <= 'Z')
5730 || (*p >= 'a' && *p <= 'z')
5731 || (*p >= '0' && *p <= '9')
5732 || (*p == '-') || (*p == '_') || (*p == '.')
5733 || (*p == '!') || (*p == '~')
5734 || (*p == '\'') || (*p == '(') || (*p == ')'))
5739 *q++ = hex[(*(unsigned char *)p >> 4) & 0xF];
5740 *q++ = hex[ *(unsigned char *)p & 0xF];
5745 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(dest);
5748 // #510 string(string input, ...) uri_unescape (DP_QC_URI_ESCAPE)
5749 // does URI unescaping on a string (get back the evil stuff)
5750 void VM_uri_unescape (void)
5752 char src[VM_STRINGTEMP_LENGTH];
5753 char dest[VM_STRINGTEMP_LENGTH];
5757 VM_SAFEPARMCOUNTRANGE(1, 8, VM_uri_unescape);
5758 VM_VarString(0, src, sizeof(src));
5760 for(p = src, q = dest; *p; ) // no need to check size, because unescape can't expand
5764 if(p[1] >= '0' && p[1] <= '9')
5766 else if(p[1] >= 'a' && p[1] <= 'f')
5767 hi = p[1] - 'a' + 10;
5768 else if(p[1] >= 'A' && p[1] <= 'F')
5769 hi = p[1] - 'A' + 10;
5772 if(p[2] >= '0' && p[2] <= '9')
5774 else if(p[2] >= 'a' && p[2] <= 'f')
5775 lo = p[2] - 'a' + 10;
5776 else if(p[2] >= 'A' && p[2] <= 'F')
5777 lo = p[2] - 'A' + 10;
5780 if(hi != 0 || lo != 0) // don't unescape NUL bytes
5781 *q++ = (char) (hi * 0x10 + lo);
5792 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(dest);
5795 // #502 string(string filename) whichpack (DP_QC_WHICHPACK)
5796 // returns the name of the pack containing a file, or "" if it is not in any pack (but local or non-existant)
5797 void VM_whichpack (void)
5799 const char *fn, *pack;
5801 VM_SAFEPARMCOUNT(1, VM_whichpack);
5802 fn = PRVM_G_STRING(OFS_PARM0);
5803 pack = FS_WhichPack(fn);
5805 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(pack ? pack : "");
5813 char buffer[MAX_INPUTLINE];
5817 static void uri_to_string_callback(int status, size_t length_received, unsigned char *buffer, void *cbdata)
5819 uri_to_prog_t *handle = (uri_to_prog_t *) cbdata;
5821 if(!PRVM_ProgLoaded(handle->prognr))
5823 // curl reply came too late... so just drop it
5828 PRVM_SetProg(handle->prognr);
5830 if((prog->starttime == handle->starttime) && (prog->funcoffsets.URI_Get_Callback))
5832 if(length_received >= sizeof(handle->buffer))
5833 length_received = sizeof(handle->buffer) - 1;
5834 handle->buffer[length_received] = 0;
5836 PRVM_G_FLOAT(OFS_PARM0) = handle->id;
5837 PRVM_G_FLOAT(OFS_PARM1) = status;
5838 PRVM_G_INT(OFS_PARM2) = PRVM_SetTempString(handle->buffer);
5839 PRVM_ExecuteProgram(prog->funcoffsets.URI_Get_Callback, "QC function URI_Get_Callback is missing");
5846 // uri_get() gets content from an URL and calls a callback "uri_get_callback" with it set as string; an unique ID of the transfer is returned
5847 // returns 1 on success, and then calls the callback with the ID, 0 or the HTTP status code, and the received data in a string
5848 void VM_uri_get (void)
5853 uri_to_prog_t *handle;
5855 if(!prog->funcoffsets.URI_Get_Callback)
5856 PRVM_ERROR("uri_get called by %s without URI_Get_Callback defined", PRVM_NAME);
5858 VM_SAFEPARMCOUNT(2, VM_uri_get);
5860 url = PRVM_G_STRING(OFS_PARM0);
5861 id = PRVM_G_FLOAT(OFS_PARM1);
5862 handle = (uri_to_prog_t *) Z_Malloc(sizeof(*handle)); // this can't be the prog's mem pool, as curl may call the callback later!
5864 handle->prognr = PRVM_GetProgNr();
5865 handle->starttime = prog->starttime;
5867 ret = Curl_Begin_ToMemory(url, 0, (unsigned char *) handle->buffer, sizeof(handle->buffer), uri_to_string_callback, handle);
5870 PRVM_G_INT(OFS_RETURN) = 1;
5875 PRVM_G_INT(OFS_RETURN) = 0;
5879 void VM_netaddress_resolve (void)
5882 char normalized[128];
5884 lhnetaddress_t addr;
5886 VM_SAFEPARMCOUNTRANGE(1, 2, VM_netaddress_resolve);
5888 ip = PRVM_G_STRING(OFS_PARM0);
5891 port = (int) PRVM_G_FLOAT(OFS_PARM1);
5893 if(LHNETADDRESS_FromString(&addr, ip, port) && LHNETADDRESS_ToString(&addr, normalized, sizeof(normalized), prog->argc > 1))
5894 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(normalized);
5896 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString("");
5899 //string(void) getextresponse = #624; // returns the next extResponse packet that was sent to this client
5900 void VM_CL_getextresponse (void)
5902 VM_SAFEPARMCOUNT(0,VM_argv);
5904 if (cl_net_extresponse_count <= 0)
5905 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
5909 --cl_net_extresponse_count;
5910 first = (cl_net_extresponse_last + NET_EXTRESPONSE_MAX - cl_net_extresponse_count) % NET_EXTRESPONSE_MAX;
5911 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(cl_net_extresponse[first]);
5915 void VM_SV_getextresponse (void)
5917 VM_SAFEPARMCOUNT(0,VM_argv);
5919 if (sv_net_extresponse_count <= 0)
5920 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
5924 --sv_net_extresponse_count;
5925 first = (sv_net_extresponse_last + NET_EXTRESPONSE_MAX - sv_net_extresponse_count) % NET_EXTRESPONSE_MAX;
5926 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(sv_net_extresponse[first]);
5932 Common functions between menu.dat and clsprogs
5936 //#349 float() isdemo
5937 void VM_CL_isdemo (void)
5939 VM_SAFEPARMCOUNT(0, VM_CL_isdemo);
5940 PRVM_G_FLOAT(OFS_RETURN) = cls.demoplayback;
5943 //#355 float() videoplaying
5944 void VM_CL_videoplaying (void)
5946 VM_SAFEPARMCOUNT(0, VM_CL_videoplaying);
5947 PRVM_G_FLOAT(OFS_RETURN) = cl_videoplaying;
5954 callfunction(...,string function_name)
5958 mfunction_t *PRVM_ED_FindFunction (const char *name);
5959 void VM_callfunction(void)
5964 VM_SAFEPARMCOUNTRANGE(1, 8, VM_callfunction);
5966 s = PRVM_G_STRING(OFS_PARM0+(prog->argc - 1)*3);
5968 VM_CheckEmptyString(s);
5970 func = PRVM_ED_FindFunction(s);
5973 PRVM_ERROR("VM_callfunciton: function %s not found !", s);
5974 else if (func->first_statement < 0)
5976 // negative statements are built in functions
5977 int builtinnumber = -func->first_statement;
5978 prog->xfunction->builtinsprofile++;
5979 if (builtinnumber < prog->numbuiltins && prog->builtins[builtinnumber])
5980 prog->builtins[builtinnumber]();
5982 PRVM_ERROR("No such builtin #%i in %s; most likely cause: outdated engine build. Try updating!", builtinnumber, PRVM_NAME);
5984 else if(func - prog->functions > 0)
5987 PRVM_ExecuteProgram(func - prog->functions,"");
5996 float isfunction(string function_name)
5999 mfunction_t *PRVM_ED_FindFunction (const char *name);
6000 void VM_isfunction(void)
6005 VM_SAFEPARMCOUNT(1, VM_isfunction);
6007 s = PRVM_G_STRING(OFS_PARM0);
6009 VM_CheckEmptyString(s);
6011 func = PRVM_ED_FindFunction(s);
6014 PRVM_G_FLOAT(OFS_RETURN) = false;
6016 PRVM_G_FLOAT(OFS_RETURN) = true;
6023 string sprintf(string format, ...)
6027 void VM_sprintf(void)
6030 char outbuf[MAX_INPUTLINE];
6031 char *o = outbuf, *end = outbuf + sizeof(outbuf), *err;
6033 int width, precision, thisarg, flags;
6037 static int dummyivec[3] = {0, 0, 0};
6038 static float dummyvec[3] = {0, 0, 0};
6040 #define PRINTF_ALTERNATE 1
6041 #define PRINTF_ZEROPAD 2
6042 #define PRINTF_LEFT 4
6043 #define PRINTF_SPACEPOSITIVE 8
6044 #define PRINTF_SIGNPOSITIVE 16
6048 s = PRVM_G_STRING(OFS_PARM0);
6050 #define GETARG_FLOAT(a) (((a)>=1 && (a)<prog->argc) ? (PRVM_G_FLOAT(OFS_PARM0 + 3 * (a))) : 0)
6051 #define GETARG_VECTOR(a) (((a)>=1 && (a)<prog->argc) ? (PRVM_G_VECTOR(OFS_PARM0 + 3 * (a))) : dummyvec)
6052 #define GETARG_INT(a) (((a)>=1 && (a)<prog->argc) ? (PRVM_G_INT(OFS_PARM0 + 3 * (a))) : 0)
6053 #define GETARG_INTVECTOR(a) (((a)>=1 && (a)<prog->argc) ? ((int*) PRVM_G_VECTOR(OFS_PARM0 + 3 * (a))) : dummyivec)
6054 #define GETARG_STRING(a) (((a)>=1 && (a)<prog->argc) ? (PRVM_G_STRING(OFS_PARM0 + 3 * (a))) : "")
6069 // complete directive format:
6078 // is number following?
6079 if(*s >= '0' && *s <= '9')
6081 width = strtol(s, &err, 10);
6084 VM_Warning("VM_sprintf: invalid directive in %s: %s\n", PRVM_NAME, s0);
6097 flags |= PRINTF_ZEROPAD;
6099 width = -1; // it was just a flag
6111 case '#': flags |= PRINTF_ALTERNATE; break;
6112 case '0': flags |= PRINTF_ZEROPAD; break;
6113 case '-': flags |= PRINTF_LEFT; break;
6114 case ' ': flags |= PRINTF_SPACEPOSITIVE; break;
6115 case '+': flags |= PRINTF_SIGNPOSITIVE; break;
6125 if(*s >= '0' && *s <= '9')
6127 width = strtol(s, &err, 10);
6128 if(!err || *err != '$')
6130 VM_Warning("VM_sprintf: invalid directive in %s: %s\n", PRVM_NAME, s0);
6137 width = GETARG_FLOAT(width);
6140 flags |= PRINTF_LEFT;
6144 else if(*s >= '0' && *s <= '9')
6146 width = strtol(s, &err, 10);
6149 VM_Warning("VM_sprintf: invalid directive in %s: %s\n", PRVM_NAME, s0);
6155 flags |= PRINTF_LEFT;
6159 // otherwise width stays -1
6168 if(*s >= '0' && *s <= '9')
6170 precision = strtol(s, &err, 10);
6171 if(!err || *err != '$')
6173 VM_Warning("VM_sprintf: invalid directive in %s: %s\n", PRVM_NAME, s0);
6179 precision = argpos++;
6180 precision = GETARG_FLOAT(precision);
6182 else if(*s >= '0' && *s <= '9')
6184 precision = strtol(s, &err, 10);
6187 VM_Warning("VM_sprintf: invalid directive in %s: %s\n", PRVM_NAME, s0);
6194 VM_Warning("VM_sprintf: invalid directive in %s: %s\n", PRVM_NAME, s0);
6203 case 'h': isfloat = 1; break;
6204 case 'l': isfloat = 0; break;
6205 case 'L': isfloat = 0; break;
6216 // now s points to the final directive char and is no longer changed
6231 if(*s != 's' && *s != 'c')
6232 if(flags & PRINTF_ALTERNATE) *f++ = '#';
6233 if(flags & PRINTF_ZEROPAD) *f++ = '0';
6234 if(flags & PRINTF_LEFT) *f++ = '-';
6235 if(flags & PRINTF_SPACEPOSITIVE) *f++ = ' ';
6236 if(flags & PRINTF_SIGNPOSITIVE) *f++ = '+';
6243 if(width < 0) // not set
6249 if(precision < 0) // not set
6251 o += dpsnprintf(o, end - o, formatbuf, width, precision, (isfloat ? (int) GETARG_FLOAT(thisarg) : (int) GETARG_INT(thisarg)));
6253 case 'o': case 'u': case 'x': case 'X':
6254 if(precision < 0) // not set
6256 o += dpsnprintf(o, end - o, formatbuf, width, precision, (isfloat ? (unsigned int) GETARG_FLOAT(thisarg) : (unsigned int) GETARG_INT(thisarg)));
6258 case 'e': case 'E': case 'f': case 'F': case 'g': case 'G':
6259 if(precision < 0) // not set
6261 o += dpsnprintf(o, end - o, formatbuf, width, precision, (isfloat ? (double) GETARG_FLOAT(thisarg) : (double) GETARG_INT(thisarg)));
6265 if(precision < 0) // not set
6267 o += dpsnprintf(o, end - o, va("%s %s %s", /* NESTED SPRINTF IS NESTED */ formatbuf, formatbuf, formatbuf),
6268 width, precision, (isfloat ? (double) GETARG_VECTOR(thisarg)[0] : (double) GETARG_INTVECTOR(thisarg)[0]),
6269 width, precision, (isfloat ? (double) GETARG_VECTOR(thisarg)[1] : (double) GETARG_INTVECTOR(thisarg)[1]),
6270 width, precision, (isfloat ? (double) GETARG_VECTOR(thisarg)[2] : (double) GETARG_INTVECTOR(thisarg)[2])
6274 if(precision < 0) // not set
6275 precision = end - o - 1;
6276 if(flags & PRINTF_ALTERNATE)
6277 o += dpsnprintf(o, end - o, formatbuf, width, precision, (isfloat ? (unsigned int) GETARG_FLOAT(thisarg) : (unsigned int) GETARG_INT(thisarg)));
6280 unsigned int c = (isfloat ? (unsigned int) GETARG_FLOAT(thisarg) : (unsigned int) GETARG_INT(thisarg));
6281 const char *buf = u8_encodech(c, NULL);
6284 o += u8_strpad(o, end - o, buf, (flags & PRINTF_LEFT) != 0, width, precision);
6288 if(precision < 0) // not set
6289 precision = end - o - 1;
6290 if(flags & PRINTF_ALTERNATE)
6291 o += dpsnprintf(o, end - o, formatbuf, width, precision, GETARG_STRING(thisarg));
6293 o += u8_strpad(o, end - o, GETARG_STRING(thisarg), (flags & PRINTF_LEFT) != 0, width, precision);
6296 VM_Warning("VM_sprintf: invalid directive in %s: %s\n", PRVM_NAME, s0);
6311 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(outbuf);
6317 static dp_model_t *getmodel(prvm_edict_t *ed)
6319 switch(PRVM_GetProgNr())
6321 case PRVM_SERVERPROG:
6322 return SV_GetModelFromEdict(ed);
6323 case PRVM_CLIENTPROG:
6324 return CL_GetModelFromEdict(ed);
6332 unsigned int progid;
6334 frameblend_t frameblend[MAX_FRAMEBLENDS];
6335 skeleton_t *skeleton_p;
6336 skeleton_t skeleton;
6337 float *data_vertex3f;
6338 float *data_svector3f;
6339 float *data_tvector3f;
6340 float *data_normal3f;
6342 float *buf_vertex3f;
6343 float *buf_svector3f;
6344 float *buf_tvector3f;
6345 float *buf_normal3f;
6347 animatemodel_cache_t;
6348 static animatemodel_cache_t animatemodel_cache;
6350 void animatemodel(dp_model_t *model, prvm_edict_t *ed)
6353 skeleton_t *skeleton;
6354 int skeletonindex = -1;
6355 qboolean need = false;
6356 if(!model->AnimateVertices)
6358 animatemodel_cache.data_vertex3f = model->surfmesh.data_vertex3f;
6359 animatemodel_cache.data_svector3f = model->surfmesh.data_svector3f;
6360 animatemodel_cache.data_tvector3f = model->surfmesh.data_tvector3f;
6361 animatemodel_cache.data_normal3f = model->surfmesh.data_normal3f;
6364 if(animatemodel_cache.progid != prog->id)
6365 memset(&animatemodel_cache, 0, sizeof(animatemodel_cache));
6366 need |= (animatemodel_cache.model != model);
6367 VM_GenerateFrameGroupBlend(ed->priv.server->framegroupblend, ed);
6368 VM_FrameBlendFromFrameGroupBlend(ed->priv.server->frameblend, ed->priv.server->framegroupblend, model);
6369 need |= (memcmp(&animatemodel_cache.frameblend, &ed->priv.server->frameblend, sizeof(ed->priv.server->frameblend)));
6370 if ((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.skeletonindex))) skeletonindex = (int)val->_float - 1;
6371 if (!(skeletonindex >= 0 && skeletonindex < MAX_EDICTS && (skeleton = prog->skeletons[skeletonindex]) && skeleton->model->num_bones == ed->priv.server->skeleton.model->num_bones))
6373 need |= (animatemodel_cache.skeleton_p != skeleton);
6375 need |= (memcmp(&animatemodel_cache.skeleton, skeleton, sizeof(ed->priv.server->skeleton)));
6378 if(model->surfmesh.num_vertices > animatemodel_cache.max_vertices)
6380 animatemodel_cache.max_vertices = model->surfmesh.num_vertices * 2;
6381 if(animatemodel_cache.buf_vertex3f) Mem_Free(animatemodel_cache.buf_vertex3f);
6382 if(animatemodel_cache.buf_svector3f) Mem_Free(animatemodel_cache.buf_svector3f);
6383 if(animatemodel_cache.buf_tvector3f) Mem_Free(animatemodel_cache.buf_tvector3f);
6384 if(animatemodel_cache.buf_normal3f) Mem_Free(animatemodel_cache.buf_normal3f);
6385 animatemodel_cache.buf_vertex3f = Mem_Alloc(prog->progs_mempool, sizeof(float[3]) * animatemodel_cache.max_vertices);
6386 animatemodel_cache.buf_svector3f = Mem_Alloc(prog->progs_mempool, sizeof(float[3]) * animatemodel_cache.max_vertices);
6387 animatemodel_cache.buf_tvector3f = Mem_Alloc(prog->progs_mempool, sizeof(float[3]) * animatemodel_cache.max_vertices);
6388 animatemodel_cache.buf_normal3f = Mem_Alloc(prog->progs_mempool, sizeof(float[3]) * animatemodel_cache.max_vertices);
6390 animatemodel_cache.data_vertex3f = animatemodel_cache.buf_vertex3f;
6391 animatemodel_cache.data_svector3f = animatemodel_cache.buf_svector3f;
6392 animatemodel_cache.data_tvector3f = animatemodel_cache.buf_tvector3f;
6393 animatemodel_cache.data_normal3f = animatemodel_cache.buf_normal3f;
6394 VM_UpdateEdictSkeleton(ed, model, ed->priv.server->frameblend);
6395 model->AnimateVertices(model, ed->priv.server->frameblend, &ed->priv.server->skeleton, animatemodel_cache.data_vertex3f, animatemodel_cache.data_normal3f, animatemodel_cache.data_svector3f, animatemodel_cache.data_tvector3f);
6396 animatemodel_cache.progid = prog->id;
6397 animatemodel_cache.model = model;
6398 memcpy(&animatemodel_cache.frameblend, &ed->priv.server->frameblend, sizeof(ed->priv.server->frameblend));
6399 animatemodel_cache.skeleton_p = skeleton;
6401 memcpy(&animatemodel_cache.skeleton, skeleton, sizeof(ed->priv.server->skeleton));
6404 static void getmatrix(prvm_edict_t *ed, matrix4x4_t *out)
6406 switch(PRVM_GetProgNr())
6408 case PRVM_SERVERPROG:
6409 SV_GetEntityMatrix(ed, out, false);
6411 case PRVM_CLIENTPROG:
6412 CL_GetEntityMatrix(ed, out, false);
6415 *out = identitymatrix;
6420 static void applytransform_forward(const vec3_t in, prvm_edict_t *ed, vec3_t out)
6424 Matrix4x4_Transform(&m, in, out);
6427 static void applytransform_forward_direction(const vec3_t in, prvm_edict_t *ed, vec3_t out)
6431 Matrix4x4_Transform3x3(&m, in, out);
6434 static void applytransform_inverted(const vec3_t in, prvm_edict_t *ed, vec3_t out)
6438 Matrix4x4_Invert_Full(&n, &m);
6439 Matrix4x4_Transform3x3(&n, in, out);
6442 static void applytransform_forward_normal(const vec3_t in, prvm_edict_t *ed, vec3_t out)
6447 Matrix4x4_TransformPositivePlane(&m, in[0], in[1], in[2], 0, p);
6451 static void clippointtosurface(prvm_edict_t *ed, dp_model_t *model, msurface_t *surface, vec3_t p, vec3_t out)
6454 float *v[3], facenormal[3], edgenormal[3], sidenormal[3], temp[3], offsetdist, dist, bestdist;
6456 animatemodel(model, ed);
6457 bestdist = 1000000000;
6459 for (i = 0, e = (model->surfmesh.data_element3i + 3 * surface->num_firsttriangle);i < surface->num_triangles;i++, e += 3)
6461 // clip original point to each triangle of the surface and find the
6462 // triangle that is closest
6463 v[0] = animatemodel_cache.data_vertex3f + e[0] * 3;
6464 v[1] = animatemodel_cache.data_vertex3f + e[1] * 3;
6465 v[2] = animatemodel_cache.data_vertex3f + e[2] * 3;
6466 TriangleNormal(v[0], v[1], v[2], facenormal);
6467 VectorNormalize(facenormal);
6468 offsetdist = DotProduct(v[0], facenormal) - DotProduct(p, facenormal);
6469 VectorMA(p, offsetdist, facenormal, temp);
6470 for (j = 0, k = 2;j < 3;k = j, j++)
6472 VectorSubtract(v[k], v[j], edgenormal);
6473 CrossProduct(edgenormal, facenormal, sidenormal);
6474 VectorNormalize(sidenormal);
6475 offsetdist = DotProduct(v[k], sidenormal) - DotProduct(temp, sidenormal);
6477 VectorMA(temp, offsetdist, sidenormal, temp);
6479 dist = VectorDistance2(temp, p);
6480 if (bestdist > dist)
6483 VectorCopy(temp, out);
6488 static msurface_t *getsurface(dp_model_t *model, int surfacenum)
6490 if (surfacenum < 0 || surfacenum >= model->nummodelsurfaces)
6492 return model->data_surfaces + surfacenum + model->firstmodelsurface;
6496 //PF_getsurfacenumpoints, // #434 float(entity e, float s) getsurfacenumpoints = #434;
6497 void VM_getsurfacenumpoints(void)
6500 msurface_t *surface;
6501 VM_SAFEPARMCOUNT(2, VM_getsurfacenumpoints);
6502 // return 0 if no such surface
6503 if (!(model = getmodel(PRVM_G_EDICT(OFS_PARM0))) || !(surface = getsurface(model, (int)PRVM_G_FLOAT(OFS_PARM1))))
6505 PRVM_G_FLOAT(OFS_RETURN) = 0;
6509 // note: this (incorrectly) assumes it is a simple polygon
6510 PRVM_G_FLOAT(OFS_RETURN) = surface->num_vertices;
6512 //PF_getsurfacepoint, // #435 vector(entity e, float s, float n) getsurfacepoint = #435;
6513 void VM_getsurfacepoint(void)
6517 msurface_t *surface;
6519 VM_SAFEPARMCOUNT(3, VM_getsurfacepoint);
6520 VectorClear(PRVM_G_VECTOR(OFS_RETURN));
6521 ed = PRVM_G_EDICT(OFS_PARM0);
6522 if (!(model = getmodel(ed)) || !(surface = getsurface(model, (int)PRVM_G_FLOAT(OFS_PARM1))))
6524 // note: this (incorrectly) assumes it is a simple polygon
6525 pointnum = (int)PRVM_G_FLOAT(OFS_PARM2);
6526 if (pointnum < 0 || pointnum >= surface->num_vertices)
6528 animatemodel(model, ed);
6529 applytransform_forward(&(animatemodel_cache.data_vertex3f + 3 * surface->num_firstvertex)[pointnum * 3], ed, PRVM_G_VECTOR(OFS_RETURN));
6531 //PF_getsurfacepointattribute, // #486 vector(entity e, float s, float n, float a) getsurfacepointattribute = #486;
6532 // float SPA_POSITION = 0;
6533 // float SPA_S_AXIS = 1;
6534 // float SPA_T_AXIS = 2;
6535 // float SPA_R_AXIS = 3; // same as SPA_NORMAL
6536 // float SPA_TEXCOORDS0 = 4;
6537 // float SPA_LIGHTMAP0_TEXCOORDS = 5;
6538 // float SPA_LIGHTMAP0_COLOR = 6;
6539 void VM_getsurfacepointattribute(void)
6543 msurface_t *surface;
6547 VM_SAFEPARMCOUNT(4, VM_getsurfacepoint);
6548 VectorClear(PRVM_G_VECTOR(OFS_RETURN));
6549 ed = PRVM_G_EDICT(OFS_PARM0);
6550 if (!(model = getmodel(ed)) || !(surface = getsurface(model, (int)PRVM_G_FLOAT(OFS_PARM1))))
6552 pointnum = (int)PRVM_G_FLOAT(OFS_PARM2);
6553 if (pointnum < 0 || pointnum >= surface->num_vertices)
6555 attributetype = (int) PRVM_G_FLOAT(OFS_PARM3);
6557 animatemodel(model, ed);
6559 switch( attributetype ) {
6560 // float SPA_POSITION = 0;
6562 applytransform_forward(&(animatemodel_cache.data_vertex3f + 3 * surface->num_firstvertex)[pointnum * 3], ed, PRVM_G_VECTOR(OFS_RETURN));
6564 // float SPA_S_AXIS = 1;
6566 applytransform_forward_direction(&(animatemodel_cache.data_svector3f + 3 * surface->num_firstvertex)[pointnum * 3], ed, PRVM_G_VECTOR(OFS_RETURN));
6568 // float SPA_T_AXIS = 2;
6570 applytransform_forward_direction(&(animatemodel_cache.data_tvector3f + 3 * surface->num_firstvertex)[pointnum * 3], ed, PRVM_G_VECTOR(OFS_RETURN));
6572 // float SPA_R_AXIS = 3; // same as SPA_NORMAL
6574 applytransform_forward_direction(&(animatemodel_cache.data_normal3f + 3 * surface->num_firstvertex)[pointnum * 3], ed, PRVM_G_VECTOR(OFS_RETURN));
6576 // float SPA_TEXCOORDS0 = 4;
6578 float *ret = PRVM_G_VECTOR(OFS_RETURN);
6579 float *texcoord = &(model->surfmesh.data_texcoordtexture2f + 2 * surface->num_firstvertex)[pointnum * 2];
6580 ret[0] = texcoord[0];
6581 ret[1] = texcoord[1];
6585 // float SPA_LIGHTMAP0_TEXCOORDS = 5;
6587 float *ret = PRVM_G_VECTOR(OFS_RETURN);
6588 float *texcoord = &(model->surfmesh.data_texcoordlightmap2f + 2 * surface->num_firstvertex)[pointnum * 2];
6589 ret[0] = texcoord[0];
6590 ret[1] = texcoord[1];
6594 // float SPA_LIGHTMAP0_COLOR = 6;
6596 // ignore alpha for now..
6597 VectorCopy( &(model->surfmesh.data_lightmapcolor4f + 4 * surface->num_firstvertex)[pointnum * 4], PRVM_G_VECTOR(OFS_RETURN));
6600 VectorSet( PRVM_G_VECTOR(OFS_RETURN), 0.0f, 0.0f, 0.0f );
6604 //PF_getsurfacenormal, // #436 vector(entity e, float s) getsurfacenormal = #436;
6605 void VM_getsurfacenormal(void)
6608 msurface_t *surface;
6610 VM_SAFEPARMCOUNT(2, VM_getsurfacenormal);
6611 VectorClear(PRVM_G_VECTOR(OFS_RETURN));
6612 if (!(model = getmodel(PRVM_G_EDICT(OFS_PARM0))) || !(surface = getsurface(model, (int)PRVM_G_FLOAT(OFS_PARM1))))
6614 // note: this only returns the first triangle, so it doesn't work very
6615 // well for curved surfaces or arbitrary meshes
6616 animatemodel(model, PRVM_G_EDICT(OFS_PARM0));
6617 TriangleNormal((animatemodel_cache.data_vertex3f + 3 * surface->num_firstvertex), (animatemodel_cache.data_vertex3f + 3 * surface->num_firstvertex) + 3, (animatemodel_cache.data_vertex3f + 3 * surface->num_firstvertex) + 6, normal);
6618 applytransform_forward_normal(normal, PRVM_G_EDICT(OFS_PARM0), PRVM_G_VECTOR(OFS_RETURN));
6619 VectorNormalize(PRVM_G_VECTOR(OFS_RETURN));
6621 //PF_getsurfacetexture, // #437 string(entity e, float s) getsurfacetexture = #437;
6622 void VM_getsurfacetexture(void)
6625 msurface_t *surface;
6626 VM_SAFEPARMCOUNT(2, VM_getsurfacetexture);
6627 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
6628 if (!(model = getmodel(PRVM_G_EDICT(OFS_PARM0))) || !(surface = getsurface(model, (int)PRVM_G_FLOAT(OFS_PARM1))))
6630 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(surface->texture->name);
6632 //PF_getsurfacenearpoint, // #438 float(entity e, vector p) getsurfacenearpoint = #438;
6633 void VM_getsurfacenearpoint(void)
6635 int surfacenum, best;
6637 vec_t dist, bestdist;
6640 msurface_t *surface;
6642 VM_SAFEPARMCOUNT(2, VM_getsurfacenearpoint);
6643 PRVM_G_FLOAT(OFS_RETURN) = -1;
6644 ed = PRVM_G_EDICT(OFS_PARM0);
6645 point = PRVM_G_VECTOR(OFS_PARM1);
6647 if (!ed || ed->priv.server->free)
6649 model = getmodel(ed);
6650 if (!model || !model->num_surfaces)
6653 animatemodel(model, ed);
6655 applytransform_inverted(point, ed, p);
6657 bestdist = 1000000000;
6658 for (surfacenum = 0;surfacenum < model->nummodelsurfaces;surfacenum++)
6660 surface = model->data_surfaces + surfacenum + model->firstmodelsurface;
6661 // first see if the nearest point on the surface's box is closer than the previous match
6662 clipped[0] = bound(surface->mins[0], p[0], surface->maxs[0]) - p[0];
6663 clipped[1] = bound(surface->mins[1], p[1], surface->maxs[1]) - p[1];
6664 clipped[2] = bound(surface->mins[2], p[2], surface->maxs[2]) - p[2];
6665 dist = VectorLength2(clipped);
6666 if (dist < bestdist)
6668 // it is, check the nearest point on the actual geometry
6669 clippointtosurface(ed, model, surface, p, clipped);
6670 VectorSubtract(clipped, p, clipped);
6671 dist += VectorLength2(clipped);
6672 if (dist < bestdist)
6674 // that's closer too, store it as the best match
6680 PRVM_G_FLOAT(OFS_RETURN) = best;
6682 //PF_getsurfaceclippedpoint, // #439 vector(entity e, float s, vector p) getsurfaceclippedpoint = #439;
6683 void VM_getsurfaceclippedpoint(void)
6687 msurface_t *surface;
6689 VM_SAFEPARMCOUNT(3, VM_te_getsurfaceclippedpoint);
6690 VectorClear(PRVM_G_VECTOR(OFS_RETURN));
6691 ed = PRVM_G_EDICT(OFS_PARM0);
6692 if (!(model = getmodel(ed)) || !(surface = getsurface(model, (int)PRVM_G_FLOAT(OFS_PARM1))))
6694 animatemodel(model, ed);
6695 applytransform_inverted(PRVM_G_VECTOR(OFS_PARM2), ed, p);
6696 clippointtosurface(ed, model, surface, p, out);
6697 VectorAdd(out, ed->fields.server->origin, PRVM_G_VECTOR(OFS_RETURN));
6700 //PF_getsurfacenumtriangles, // #??? float(entity e, float s) getsurfacenumtriangles = #???;
6701 void VM_getsurfacenumtriangles(void)
6704 msurface_t *surface;
6705 VM_SAFEPARMCOUNT(2, VM_SV_getsurfacenumtriangles);
6706 // return 0 if no such surface
6707 if (!(model = getmodel(PRVM_G_EDICT(OFS_PARM0))) || !(surface = getsurface(model, (int)PRVM_G_FLOAT(OFS_PARM1))))
6709 PRVM_G_FLOAT(OFS_RETURN) = 0;
6713 // note: this (incorrectly) assumes it is a simple polygon
6714 PRVM_G_FLOAT(OFS_RETURN) = surface->num_triangles;
6716 //PF_getsurfacetriangle, // #??? vector(entity e, float s, float n) getsurfacetriangle = #???;
6717 void VM_getsurfacetriangle(void)
6719 const vec3_t d = {-1, -1, -1};
6722 msurface_t *surface;
6724 VM_SAFEPARMCOUNT(3, VM_SV_getsurfacetriangle);
6725 VectorClear(PRVM_G_VECTOR(OFS_RETURN));
6726 ed = PRVM_G_EDICT(OFS_PARM0);
6727 if (!(model = getmodel(ed)) || !(surface = getsurface(model, (int)PRVM_G_FLOAT(OFS_PARM1))))
6729 trinum = (int)PRVM_G_FLOAT(OFS_PARM2);
6730 if (trinum < 0 || trinum >= surface->num_triangles)
6732 // FIXME: implement rotation/scaling
6733 VectorMA(&(model->surfmesh.data_element3i + 3 * surface->num_firsttriangle)[trinum * 3], surface->num_firstvertex, d, PRVM_G_VECTOR(OFS_RETURN));