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.model || !ed->priv.server->skeleton.model->num_bones)
192 if(ed->priv.server->skeleton.relativetransforms)
193 Mem_Free(ed->priv.server->skeleton.relativetransforms);
194 ed->priv.server->skeleton.relativetransforms = NULL;
199 int skeletonindex = -1;
200 skeleton_t *skeleton;
202 if ((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.skeletonindex))) skeletonindex = (int)val->_float - 1;
203 if (skeletonindex >= 0 && skeletonindex < MAX_EDICTS && (skeleton = prog->skeletons[skeletonindex]) && skeleton->model->num_bones == ed->priv.server->skeleton.model->num_bones)
205 // custom skeleton controlled by the game (FTE_CSQC_SKELETONOBJECTS)
206 if (!ed->priv.server->skeleton.relativetransforms)
207 ed->priv.server->skeleton.relativetransforms = (matrix4x4_t *)Mem_Alloc(prog->progs_mempool, ed->priv.server->skeleton.model->num_bones * sizeof(matrix4x4_t));
208 memcpy(ed->priv.server->skeleton.relativetransforms, skeleton->relativetransforms, ed->priv.server->skeleton.model->num_bones * sizeof(matrix4x4_t));
212 if(ed->priv.server->skeleton.relativetransforms)
213 Mem_Free(ed->priv.server->skeleton.relativetransforms);
214 ed->priv.server->skeleton.relativetransforms = NULL;
219 void VM_RemoveEdictSkeleton(prvm_edict_t *ed)
221 if (ed->priv.server->skeleton.relativetransforms)
222 Mem_Free(ed->priv.server->skeleton.relativetransforms);
223 memset(&ed->priv.server->skeleton, 0, sizeof(ed->priv.server->skeleton));
229 //============================================================================
232 void VM_VarString(int first, char *out, int outlength)
238 outend = out + outlength - 1;
239 for (i = first;i < prog->argc && out < outend;i++)
241 s = PRVM_G_STRING((OFS_PARM0+i*3));
242 while (out < outend && *s)
252 returns true if the extension is supported by the server
254 checkextension(extensionname)
258 // kind of helper function
259 static qboolean checkextension(const char *name)
262 const char *e, *start;
263 len = (int)strlen(name);
265 for (e = prog->extensionstring;*e;e++)
272 while (*e && *e != ' ')
274 if ((e - start) == len && !strncasecmp(start, name, len))
280 void VM_checkextension (void)
282 VM_SAFEPARMCOUNT(1,VM_checkextension);
284 PRVM_G_FLOAT(OFS_RETURN) = checkextension(PRVM_G_STRING(OFS_PARM0));
291 This is a TERMINAL error, which will kill off the entire prog.
300 char string[VM_STRINGTEMP_LENGTH];
302 VM_VarString(0, string, sizeof(string));
303 Con_Printf("======%s ERROR in %s:\n%s\n", PRVM_NAME, PRVM_GetString(prog->xfunction->s_name), string);
304 if (prog->globaloffsets.self >= 0)
306 ed = PRVM_PROG_TO_EDICT(PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict);
307 PRVM_ED_Print(ed, NULL);
310 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);
317 Dumps out self, then an error message. The program is aborted and self is
318 removed, but the level can continue.
323 void VM_objerror (void)
326 char string[VM_STRINGTEMP_LENGTH];
328 VM_VarString(0, string, sizeof(string));
329 Con_Printf("======OBJECT ERROR======\n"); // , PRVM_NAME, PRVM_GetString(prog->xfunction->s_name), string); // or include them? FIXME
330 if (prog->globaloffsets.self >= 0)
332 ed = PRVM_PROG_TO_EDICT(PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict);
333 PRVM_ED_Print(ed, NULL);
338 // objerror has to display the object fields -> else call
339 PRVM_ERROR ("VM_objecterror: self not defined !");
340 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);
354 char string[VM_STRINGTEMP_LENGTH];
356 VM_VarString(0, string, sizeof(string));
364 broadcast print to everyone on server
369 void VM_bprint (void)
371 char string[VM_STRINGTEMP_LENGTH];
375 VM_Warning("VM_bprint: game is not server(%s) !\n", PRVM_NAME);
379 VM_VarString(0, string, sizeof(string));
380 SV_BroadcastPrint(string);
385 VM_sprint (menu & client but only if server.active == true)
387 single print to a specific client
389 sprint(float clientnum,...[string])
392 void VM_sprint (void)
396 char string[VM_STRINGTEMP_LENGTH];
398 VM_SAFEPARMCOUNTRANGE(1, 8, VM_sprint);
400 //find client for this entity
401 clientnum = (int)PRVM_G_FLOAT(OFS_PARM0);
402 if (!sv.active || clientnum < 0 || clientnum >= svs.maxclients || !svs.clients[clientnum].active)
404 VM_Warning("VM_sprint: %s: invalid client or server is not active !\n", PRVM_NAME);
408 client = svs.clients + clientnum;
409 if (!client->netconnection)
412 VM_VarString(1, string, sizeof(string));
413 MSG_WriteChar(&client->netconnection->message,svc_print);
414 MSG_WriteString(&client->netconnection->message, string);
421 single print to the screen
426 void VM_centerprint (void)
428 char string[VM_STRINGTEMP_LENGTH];
430 VM_SAFEPARMCOUNTRANGE(1, 8, VM_centerprint);
431 VM_VarString(0, string, sizeof(string));
432 SCR_CenterPrint(string);
439 vector normalize(vector)
442 void VM_normalize (void)
448 VM_SAFEPARMCOUNT(1,VM_normalize);
450 value1 = PRVM_G_VECTOR(OFS_PARM0);
452 f = VectorLength2(value1);
456 VectorScale(value1, f, newvalue);
459 VectorClear(newvalue);
461 VectorCopy (newvalue, PRVM_G_VECTOR(OFS_RETURN));
473 VM_SAFEPARMCOUNT(1,VM_vlen);
474 PRVM_G_FLOAT(OFS_RETURN) = VectorLength(PRVM_G_VECTOR(OFS_PARM0));
481 float vectoyaw(vector)
484 void VM_vectoyaw (void)
489 VM_SAFEPARMCOUNT(1,VM_vectoyaw);
491 value1 = PRVM_G_VECTOR(OFS_PARM0);
493 if (value1[1] == 0 && value1[0] == 0)
497 yaw = (int) (atan2(value1[1], value1[0]) * 180 / M_PI);
502 PRVM_G_FLOAT(OFS_RETURN) = yaw;
510 vector vectoangles(vector[, vector])
513 void VM_vectoangles (void)
515 VM_SAFEPARMCOUNTRANGE(1, 2,VM_vectoangles);
517 AnglesFromVectors(PRVM_G_VECTOR(OFS_RETURN), PRVM_G_VECTOR(OFS_PARM0), prog->argc >= 2 ? PRVM_G_VECTOR(OFS_PARM1) : NULL, true);
524 Returns a number from 0<= num < 1
529 void VM_random (void)
531 VM_SAFEPARMCOUNT(0,VM_random);
533 PRVM_G_FLOAT(OFS_RETURN) = lhrandom(0, 1);
540 localsound(string sample)
543 void VM_localsound(void)
547 VM_SAFEPARMCOUNT(1,VM_localsound);
549 s = PRVM_G_STRING(OFS_PARM0);
551 if(!S_LocalSound (s))
553 PRVM_G_FLOAT(OFS_RETURN) = -4;
554 VM_Warning("VM_localsound: Failed to play %s for %s !\n", s, PRVM_NAME);
558 PRVM_G_FLOAT(OFS_RETURN) = 1;
570 PRVM_ERROR ("%s: break statement", PRVM_NAME);
573 //============================================================================
579 Sends text over to the client's execution buffer
581 [localcmd (string, ...) or]
585 void VM_localcmd (void)
587 char string[VM_STRINGTEMP_LENGTH];
588 VM_SAFEPARMCOUNTRANGE(1, 8, VM_localcmd);
589 VM_VarString(0, string, sizeof(string));
590 Cbuf_AddText(string);
593 static qboolean PRVM_Cvar_ReadOk(const char *string)
596 cvar = Cvar_FindVar(string);
597 return ((cvar) && ((cvar->flags & CVAR_PRIVATE) == 0));
609 char string[VM_STRINGTEMP_LENGTH];
610 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar);
611 VM_VarString(0, string, sizeof(string));
612 VM_CheckEmptyString(string);
613 PRVM_G_FLOAT(OFS_RETURN) = PRVM_Cvar_ReadOk(string) ? Cvar_VariableValue(string) : 0;
620 float cvar_type (string)
621 float CVAR_TYPEFLAG_EXISTS = 1;
622 float CVAR_TYPEFLAG_SAVED = 2;
623 float CVAR_TYPEFLAG_PRIVATE = 4;
624 float CVAR_TYPEFLAG_ENGINE = 8;
625 float CVAR_TYPEFLAG_HASDESCRIPTION = 16;
626 float CVAR_TYPEFLAG_READONLY = 32;
629 void VM_cvar_type (void)
631 char string[VM_STRINGTEMP_LENGTH];
635 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar);
636 VM_VarString(0, string, sizeof(string));
637 VM_CheckEmptyString(string);
638 cvar = Cvar_FindVar(string);
643 PRVM_G_FLOAT(OFS_RETURN) = 0;
644 return; // CVAR_TYPE_NONE
647 ret = 1; // CVAR_EXISTS
648 if(cvar->flags & CVAR_SAVE)
649 ret |= 2; // CVAR_TYPE_SAVED
650 if(cvar->flags & CVAR_PRIVATE)
651 ret |= 4; // CVAR_TYPE_PRIVATE
652 if(!(cvar->flags & CVAR_ALLOCATED))
653 ret |= 8; // CVAR_TYPE_ENGINE
654 if(cvar->description != cvar_dummy_description)
655 ret |= 16; // CVAR_TYPE_HASDESCRIPTION
656 if(cvar->flags & CVAR_READONLY)
657 ret |= 32; // CVAR_TYPE_READONLY
659 PRVM_G_FLOAT(OFS_RETURN) = ret;
666 const string VM_cvar_string (string, ...)
669 void VM_cvar_string(void)
671 char string[VM_STRINGTEMP_LENGTH];
672 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar_string);
673 VM_VarString(0, string, sizeof(string));
674 VM_CheckEmptyString(string);
675 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(PRVM_Cvar_ReadOk(string) ? Cvar_VariableString(string) : "");
680 ========================
683 const string VM_cvar_defstring (string, ...)
684 ========================
686 void VM_cvar_defstring (void)
688 char string[VM_STRINGTEMP_LENGTH];
689 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar_defstring);
690 VM_VarString(0, string, sizeof(string));
691 VM_CheckEmptyString(string);
692 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(Cvar_VariableDefString(string));
696 ========================
699 const string VM_cvar_description (string, ...)
700 ========================
702 void VM_cvar_description (void)
704 char string[VM_STRINGTEMP_LENGTH];
705 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar_description);
706 VM_VarString(0, string, sizeof(string));
707 VM_CheckEmptyString(string);
708 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(Cvar_VariableDescription(string));
714 void cvar_set (string,string, ...)
717 void VM_cvar_set (void)
720 char string[VM_STRINGTEMP_LENGTH];
721 VM_SAFEPARMCOUNTRANGE(2,8,VM_cvar_set);
722 VM_VarString(1, string, sizeof(string));
723 name = PRVM_G_STRING(OFS_PARM0);
724 VM_CheckEmptyString(name);
725 Cvar_Set(name, string);
735 void VM_dprint (void)
737 char string[VM_STRINGTEMP_LENGTH];
738 VM_SAFEPARMCOUNTRANGE(1, 8, VM_dprint);
739 VM_VarString(0, string, sizeof(string));
741 Con_DPrintf("%s", string);
743 Con_DPrintf("%s: %s", PRVM_NAME, string);
760 VM_SAFEPARMCOUNT(1, VM_ftos);
762 v = PRVM_G_FLOAT(OFS_PARM0);
764 if ((float)((int)v) == v)
765 dpsnprintf(s, sizeof(s), "%i", (int)v);
767 dpsnprintf(s, sizeof(s), "%f", v);
768 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(s);
783 VM_SAFEPARMCOUNT(1,VM_fabs);
785 v = PRVM_G_FLOAT(OFS_PARM0);
786 PRVM_G_FLOAT(OFS_RETURN) = fabs(v);
801 VM_SAFEPARMCOUNT(1,VM_vtos);
803 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]);
804 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(s);
819 VM_SAFEPARMCOUNT(1, VM_etos);
821 dpsnprintf (s, sizeof(s), "entity %i", PRVM_G_EDICTNUM(OFS_PARM0));
822 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(s);
829 float stof(...[string])
834 char string[VM_STRINGTEMP_LENGTH];
835 VM_SAFEPARMCOUNTRANGE(1, 8, VM_stof);
836 VM_VarString(0, string, sizeof(string));
837 PRVM_G_FLOAT(OFS_RETURN) = atof(string);
841 ========================
845 ========================
849 VM_SAFEPARMCOUNT(1, VM_itof);
850 PRVM_G_FLOAT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
854 ========================
857 entity ftoe(float num)
858 ========================
863 VM_SAFEPARMCOUNT(1, VM_ftoe);
865 ent = (int)PRVM_G_FLOAT(OFS_PARM0);
866 if (ent < 0 || ent >= MAX_EDICTS || PRVM_PROG_TO_EDICT(ent)->priv.required->free)
867 ent = 0; // return world instead of a free or invalid entity
869 PRVM_G_INT(OFS_RETURN) = ent;
873 ========================
876 float etof(entity ent)
877 ========================
881 VM_SAFEPARMCOUNT(1, VM_etof);
882 PRVM_G_FLOAT(OFS_RETURN) = PRVM_G_EDICTNUM(OFS_PARM0);
889 string strftime(float uselocaltime, string[, string ...])
892 void VM_strftime(void)
901 char fmt[VM_STRINGTEMP_LENGTH];
902 char result[VM_STRINGTEMP_LENGTH];
903 VM_SAFEPARMCOUNTRANGE(2, 8, VM_strftime);
904 VM_VarString(1, fmt, sizeof(fmt));
907 if (PRVM_G_FLOAT(OFS_PARM0))
908 tmresult = localtime_s(&tm, &t);
910 tmresult = gmtime_s(&tm, &t);
913 if (PRVM_G_FLOAT(OFS_PARM0))
920 PRVM_G_INT(OFS_RETURN) = 0;
924 strftime(result, sizeof(result), fmt, &tm);
926 strftime(result, sizeof(result), fmt, tm);
928 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(result);
942 VM_SAFEPARMCOUNT(0, VM_spawn);
943 prog->xfunction->builtinsprofile += 20;
944 ed = PRVM_ED_Alloc();
956 void VM_remove (void)
959 prog->xfunction->builtinsprofile += 20;
961 VM_SAFEPARMCOUNT(1, VM_remove);
963 ed = PRVM_G_EDICT(OFS_PARM0);
964 if( PRVM_NUM_FOR_EDICT(ed) <= prog->reserved_edicts )
966 if (developer.integer > 0)
967 VM_Warning( "VM_remove: tried to remove the null entity or a reserved entity!\n" );
969 else if( ed->priv.required->free )
971 if (developer.integer > 0)
972 VM_Warning( "VM_remove: tried to remove an already freed entity!\n" );
982 entity find(entity start, .string field, string match)
993 VM_SAFEPARMCOUNT(3,VM_find);
995 e = PRVM_G_EDICTNUM(OFS_PARM0);
996 f = PRVM_G_INT(OFS_PARM1);
997 s = PRVM_G_STRING(OFS_PARM2);
999 // LordHavoc: apparently BloodMage does a find(world, weaponmodel, "") and
1000 // expects it to find all the monsters, so we must be careful to support
1003 for (e++ ; e < prog->num_edicts ; e++)
1005 prog->xfunction->builtinsprofile++;
1006 ed = PRVM_EDICT_NUM(e);
1007 if (ed->priv.required->free)
1009 t = PRVM_E_STRING(ed,f);
1014 VM_RETURN_EDICT(ed);
1019 VM_RETURN_EDICT(prog->edicts);
1026 entity findfloat(entity start, .float field, float match)
1027 entity findentity(entity start, .entity field, entity match)
1030 // LordHavoc: added this for searching float, int, and entity reference fields
1031 void VM_findfloat (void)
1038 VM_SAFEPARMCOUNT(3,VM_findfloat);
1040 e = PRVM_G_EDICTNUM(OFS_PARM0);
1041 f = PRVM_G_INT(OFS_PARM1);
1042 s = PRVM_G_FLOAT(OFS_PARM2);
1044 for (e++ ; e < prog->num_edicts ; e++)
1046 prog->xfunction->builtinsprofile++;
1047 ed = PRVM_EDICT_NUM(e);
1048 if (ed->priv.required->free)
1050 if (PRVM_E_FLOAT(ed,f) == s)
1052 VM_RETURN_EDICT(ed);
1057 VM_RETURN_EDICT(prog->edicts);
1064 entity findchain(.string field, string match)
1067 // chained search for strings in entity fields
1068 // entity(.string field, string match) findchain = #402;
1069 void VM_findchain (void)
1074 prvm_edict_t *ent, *chain;
1077 VM_SAFEPARMCOUNTRANGE(2,3,VM_findchain);
1080 chainfield = PRVM_G_INT(OFS_PARM2);
1082 chainfield = prog->fieldoffsets.chain;
1084 PRVM_ERROR("VM_findchain: %s doesnt have the specified chain field !", PRVM_NAME);
1086 chain = prog->edicts;
1088 f = PRVM_G_INT(OFS_PARM0);
1089 s = PRVM_G_STRING(OFS_PARM1);
1091 // LordHavoc: apparently BloodMage does a find(world, weaponmodel, "") and
1092 // expects it to find all the monsters, so we must be careful to support
1095 ent = PRVM_NEXT_EDICT(prog->edicts);
1096 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
1098 prog->xfunction->builtinsprofile++;
1099 if (ent->priv.required->free)
1101 t = PRVM_E_STRING(ent,f);
1107 PRVM_EDICTFIELDVALUE(ent,chainfield)->edict = PRVM_NUM_FOR_EDICT(chain);
1111 VM_RETURN_EDICT(chain);
1118 entity findchainfloat(.string field, float match)
1119 entity findchainentity(.string field, entity match)
1122 // LordHavoc: chained search for float, int, and entity reference fields
1123 // entity(.string field, float match) findchainfloat = #403;
1124 void VM_findchainfloat (void)
1129 prvm_edict_t *ent, *chain;
1132 VM_SAFEPARMCOUNTRANGE(2, 3, VM_findchainfloat);
1135 chainfield = PRVM_G_INT(OFS_PARM2);
1137 chainfield = prog->fieldoffsets.chain;
1139 PRVM_ERROR("VM_findchain: %s doesnt have the specified chain field !", PRVM_NAME);
1141 chain = (prvm_edict_t *)prog->edicts;
1143 f = PRVM_G_INT(OFS_PARM0);
1144 s = PRVM_G_FLOAT(OFS_PARM1);
1146 ent = PRVM_NEXT_EDICT(prog->edicts);
1147 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
1149 prog->xfunction->builtinsprofile++;
1150 if (ent->priv.required->free)
1152 if (PRVM_E_FLOAT(ent,f) != s)
1155 PRVM_EDICTFIELDVALUE(ent,chainfield)->edict = PRVM_EDICT_TO_PROG(chain);
1159 VM_RETURN_EDICT(chain);
1163 ========================
1166 entity findflags(entity start, .float field, float match)
1167 ========================
1169 // LordHavoc: search for flags in float fields
1170 void VM_findflags (void)
1177 VM_SAFEPARMCOUNT(3, VM_findflags);
1180 e = PRVM_G_EDICTNUM(OFS_PARM0);
1181 f = PRVM_G_INT(OFS_PARM1);
1182 s = (int)PRVM_G_FLOAT(OFS_PARM2);
1184 for (e++ ; e < prog->num_edicts ; e++)
1186 prog->xfunction->builtinsprofile++;
1187 ed = PRVM_EDICT_NUM(e);
1188 if (ed->priv.required->free)
1190 if (!PRVM_E_FLOAT(ed,f))
1192 if ((int)PRVM_E_FLOAT(ed,f) & s)
1194 VM_RETURN_EDICT(ed);
1199 VM_RETURN_EDICT(prog->edicts);
1203 ========================
1206 entity findchainflags(.float field, float match)
1207 ========================
1209 // LordHavoc: chained search for flags in float fields
1210 void VM_findchainflags (void)
1215 prvm_edict_t *ent, *chain;
1218 VM_SAFEPARMCOUNTRANGE(2, 3, VM_findchainflags);
1221 chainfield = PRVM_G_INT(OFS_PARM2);
1223 chainfield = prog->fieldoffsets.chain;
1225 PRVM_ERROR("VM_findchain: %s doesnt have the specified chain field !", PRVM_NAME);
1227 chain = (prvm_edict_t *)prog->edicts;
1229 f = PRVM_G_INT(OFS_PARM0);
1230 s = (int)PRVM_G_FLOAT(OFS_PARM1);
1232 ent = PRVM_NEXT_EDICT(prog->edicts);
1233 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
1235 prog->xfunction->builtinsprofile++;
1236 if (ent->priv.required->free)
1238 if (!PRVM_E_FLOAT(ent,f))
1240 if (!((int)PRVM_E_FLOAT(ent,f) & s))
1243 PRVM_EDICTFIELDVALUE(ent,chainfield)->edict = PRVM_EDICT_TO_PROG(chain);
1247 VM_RETURN_EDICT(chain);
1254 string precache_sound (string sample)
1257 void VM_precache_sound (void)
1261 VM_SAFEPARMCOUNT(1, VM_precache_sound);
1263 s = PRVM_G_STRING(OFS_PARM0);
1264 PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
1265 //VM_CheckEmptyString(s);
1267 if(snd_initialized.integer && !S_PrecacheSound(s, true, true))
1269 VM_Warning("VM_precache_sound: Failed to load %s for %s\n", s, PRVM_NAME);
1278 returns the same string as output
1280 does nothing, only used by qcc to build .pak archives
1283 void VM_precache_file (void)
1285 VM_SAFEPARMCOUNT(1,VM_precache_file);
1286 // precache_file is only used to copy files with qcc, it does nothing
1287 PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
1297 void VM_coredump (void)
1299 VM_SAFEPARMCOUNT(0,VM_coredump);
1301 Cbuf_AddText("prvm_edicts ");
1302 Cbuf_AddText(PRVM_NAME);
1313 void PRVM_StackTrace(void);
1314 void VM_stackdump (void)
1316 VM_SAFEPARMCOUNT(0, VM_stackdump);
1331 VM_SAFEPARMCOUNT(0, VM_crash);
1333 PRVM_ERROR("Crash called by %s",PRVM_NAME);
1343 void VM_traceon (void)
1345 VM_SAFEPARMCOUNT(0,VM_traceon);
1357 void VM_traceoff (void)
1359 VM_SAFEPARMCOUNT(0,VM_traceoff);
1361 prog->trace = false;
1371 void VM_eprint (void)
1373 VM_SAFEPARMCOUNT(1,VM_eprint);
1375 PRVM_ED_PrintNum (PRVM_G_EDICTNUM(OFS_PARM0), NULL);
1388 VM_SAFEPARMCOUNT(1,VM_rint);
1390 f = PRVM_G_FLOAT(OFS_PARM0);
1392 PRVM_G_FLOAT(OFS_RETURN) = floor(f + 0.5);
1394 PRVM_G_FLOAT(OFS_RETURN) = ceil(f - 0.5);
1404 void VM_floor (void)
1406 VM_SAFEPARMCOUNT(1,VM_floor);
1408 PRVM_G_FLOAT(OFS_RETURN) = floor(PRVM_G_FLOAT(OFS_PARM0));
1420 VM_SAFEPARMCOUNT(1,VM_ceil);
1422 PRVM_G_FLOAT(OFS_RETURN) = ceil(PRVM_G_FLOAT(OFS_PARM0));
1430 entity nextent(entity)
1433 void VM_nextent (void)
1438 VM_SAFEPARMCOUNT(1, VM_nextent);
1440 i = PRVM_G_EDICTNUM(OFS_PARM0);
1443 prog->xfunction->builtinsprofile++;
1445 if (i == prog->num_edicts)
1447 VM_RETURN_EDICT(prog->edicts);
1450 ent = PRVM_EDICT_NUM(i);
1451 if (!ent->priv.required->free)
1453 VM_RETURN_EDICT(ent);
1459 //=============================================================================
1466 changelevel(string map)
1469 void VM_changelevel (void)
1471 VM_SAFEPARMCOUNT(1, VM_changelevel);
1475 VM_Warning("VM_changelevel: game is not server (%s)\n", PRVM_NAME);
1479 // make sure we don't issue two changelevels
1480 if (svs.changelevel_issued)
1482 svs.changelevel_issued = true;
1484 Cbuf_AddText (va("changelevel %s\n",PRVM_G_STRING(OFS_PARM0)));
1496 VM_SAFEPARMCOUNT(1,VM_sin);
1497 PRVM_G_FLOAT(OFS_RETURN) = sin(PRVM_G_FLOAT(OFS_PARM0));
1508 VM_SAFEPARMCOUNT(1,VM_cos);
1509 PRVM_G_FLOAT(OFS_RETURN) = cos(PRVM_G_FLOAT(OFS_PARM0));
1521 VM_SAFEPARMCOUNT(1,VM_sqrt);
1522 PRVM_G_FLOAT(OFS_RETURN) = sqrt(PRVM_G_FLOAT(OFS_PARM0));
1534 VM_SAFEPARMCOUNT(1,VM_asin);
1535 PRVM_G_FLOAT(OFS_RETURN) = asin(PRVM_G_FLOAT(OFS_PARM0));
1546 VM_SAFEPARMCOUNT(1,VM_acos);
1547 PRVM_G_FLOAT(OFS_RETURN) = acos(PRVM_G_FLOAT(OFS_PARM0));
1558 VM_SAFEPARMCOUNT(1,VM_atan);
1559 PRVM_G_FLOAT(OFS_RETURN) = atan(PRVM_G_FLOAT(OFS_PARM0));
1565 float atan2(float,float)
1568 void VM_atan2 (void)
1570 VM_SAFEPARMCOUNT(2,VM_atan2);
1571 PRVM_G_FLOAT(OFS_RETURN) = atan2(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1582 VM_SAFEPARMCOUNT(1,VM_tan);
1583 PRVM_G_FLOAT(OFS_RETURN) = tan(PRVM_G_FLOAT(OFS_PARM0));
1590 Returns a vector of length < 1 and > 0
1595 void VM_randomvec (void)
1600 VM_SAFEPARMCOUNT(0, VM_randomvec);
1605 temp[0] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1606 temp[1] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1607 temp[2] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1609 while (DotProduct(temp, temp) >= 1);
1610 VectorCopy (temp, PRVM_G_VECTOR(OFS_RETURN));
1613 temp[0] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1614 temp[1] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1615 temp[2] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1616 // length returned always > 0
1617 length = (rand()&32766 + 1) * (1.0 / 32767.0) / VectorLength(temp);
1618 VectorScale(temp,length, temp);*/
1619 //VectorCopy(temp, PRVM_G_VECTOR(OFS_RETURN));
1622 //=============================================================================
1628 float registercvar (string name, string value[, float flags])
1631 void VM_registercvar (void)
1633 const char *name, *value;
1636 VM_SAFEPARMCOUNTRANGE(2, 3, VM_registercvar);
1638 name = PRVM_G_STRING(OFS_PARM0);
1639 value = PRVM_G_STRING(OFS_PARM1);
1640 flags = prog->argc >= 3 ? (int)PRVM_G_FLOAT(OFS_PARM2) : 0;
1641 PRVM_G_FLOAT(OFS_RETURN) = 0;
1643 if(flags > CVAR_MAXFLAGSVAL)
1646 // first check to see if it has already been defined
1647 if (Cvar_FindVar (name))
1650 // check for overlap with a command
1651 if (Cmd_Exists (name))
1653 VM_Warning("VM_registercvar: %s is a command\n", name);
1657 Cvar_Get(name, value, flags, NULL);
1659 PRVM_G_FLOAT(OFS_RETURN) = 1; // success
1667 returns the minimum of two supplied floats
1669 float min(float a, float b, ...[float])
1674 VM_SAFEPARMCOUNTRANGE(2, 8, VM_min);
1675 // LordHavoc: 3+ argument enhancement suggested by FrikaC
1676 if (prog->argc >= 3)
1679 float f = PRVM_G_FLOAT(OFS_PARM0);
1680 for (i = 1;i < prog->argc;i++)
1681 if (f > PRVM_G_FLOAT((OFS_PARM0+i*3)))
1682 f = PRVM_G_FLOAT((OFS_PARM0+i*3));
1683 PRVM_G_FLOAT(OFS_RETURN) = f;
1686 PRVM_G_FLOAT(OFS_RETURN) = min(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1693 returns the maximum of two supplied floats
1695 float max(float a, float b, ...[float])
1700 VM_SAFEPARMCOUNTRANGE(2, 8, VM_max);
1701 // LordHavoc: 3+ argument enhancement suggested by FrikaC
1702 if (prog->argc >= 3)
1705 float f = PRVM_G_FLOAT(OFS_PARM0);
1706 for (i = 1;i < prog->argc;i++)
1707 if (f < PRVM_G_FLOAT((OFS_PARM0+i*3)))
1708 f = PRVM_G_FLOAT((OFS_PARM0+i*3));
1709 PRVM_G_FLOAT(OFS_RETURN) = f;
1712 PRVM_G_FLOAT(OFS_RETURN) = max(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1719 returns number bounded by supplied range
1721 float bound(float min, float value, float max)
1724 void VM_bound (void)
1726 VM_SAFEPARMCOUNT(3,VM_bound);
1727 PRVM_G_FLOAT(OFS_RETURN) = bound(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1), PRVM_G_FLOAT(OFS_PARM2));
1734 returns a raised to power b
1736 float pow(float a, float b)
1741 VM_SAFEPARMCOUNT(2,VM_pow);
1742 PRVM_G_FLOAT(OFS_RETURN) = pow(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1747 VM_SAFEPARMCOUNT(1,VM_log);
1748 PRVM_G_FLOAT(OFS_RETURN) = log(PRVM_G_FLOAT(OFS_PARM0));
1751 void VM_Files_Init(void)
1754 for (i = 0;i < PRVM_MAX_OPENFILES;i++)
1755 prog->openfiles[i] = NULL;
1758 void VM_Files_CloseAll(void)
1761 for (i = 0;i < PRVM_MAX_OPENFILES;i++)
1763 if (prog->openfiles[i])
1764 FS_Close(prog->openfiles[i]);
1765 prog->openfiles[i] = NULL;
1769 static qfile_t *VM_GetFileHandle( int index )
1771 if (index < 0 || index >= PRVM_MAX_OPENFILES)
1773 Con_Printf("VM_GetFileHandle: invalid file handle %i used in %s\n", index, PRVM_NAME);
1776 if (prog->openfiles[index] == NULL)
1778 Con_Printf("VM_GetFileHandle: no such file handle %i (or file has been closed) in %s\n", index, PRVM_NAME);
1781 return prog->openfiles[index];
1788 float fopen(string filename, float mode)
1791 // float(string filename, float mode) fopen = #110;
1792 // opens a file inside quake/gamedir/data/ (mode is FILE_READ, FILE_APPEND, or FILE_WRITE),
1793 // returns fhandle >= 0 if successful, or fhandle < 0 if unable to open file for any reason
1797 const char *modestring, *filename;
1799 VM_SAFEPARMCOUNT(2,VM_fopen);
1801 for (filenum = 0;filenum < PRVM_MAX_OPENFILES;filenum++)
1802 if (prog->openfiles[filenum] == NULL)
1804 if (filenum >= PRVM_MAX_OPENFILES)
1806 PRVM_G_FLOAT(OFS_RETURN) = -2;
1807 VM_Warning("VM_fopen: %s ran out of file handles (%i)\n", PRVM_NAME, PRVM_MAX_OPENFILES);
1810 filename = PRVM_G_STRING(OFS_PARM0);
1811 mode = (int)PRVM_G_FLOAT(OFS_PARM1);
1814 case 0: // FILE_READ
1816 prog->openfiles[filenum] = FS_OpenVirtualFile(va("data/%s", filename), false);
1817 if (prog->openfiles[filenum] == NULL)
1818 prog->openfiles[filenum] = FS_OpenVirtualFile(va("%s", filename), false);
1820 case 1: // FILE_APPEND
1822 prog->openfiles[filenum] = FS_OpenRealFile(va("data/%s", filename), modestring, false);
1824 case 2: // FILE_WRITE
1826 prog->openfiles[filenum] = FS_OpenRealFile(va("data/%s", filename), modestring, false);
1829 PRVM_G_FLOAT(OFS_RETURN) = -3;
1830 VM_Warning("VM_fopen: %s: no such mode %i (valid: 0 = read, 1 = append, 2 = write)\n", PRVM_NAME, mode);
1834 if (prog->openfiles[filenum] == NULL)
1836 PRVM_G_FLOAT(OFS_RETURN) = -1;
1837 if (developer_extra.integer)
1838 VM_Warning("VM_fopen: %s: %s mode %s failed\n", PRVM_NAME, filename, modestring);
1842 PRVM_G_FLOAT(OFS_RETURN) = filenum;
1843 if (developer_extra.integer)
1844 Con_DPrintf("VM_fopen: %s: %s mode %s opened as #%i\n", PRVM_NAME, filename, modestring, filenum);
1845 prog->openfiles_origin[filenum] = PRVM_AllocationOrigin();
1853 fclose(float fhandle)
1856 //void(float fhandle) fclose = #111; // closes a file
1857 void VM_fclose(void)
1861 VM_SAFEPARMCOUNT(1,VM_fclose);
1863 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1864 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
1866 VM_Warning("VM_fclose: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1869 if (prog->openfiles[filenum] == NULL)
1871 VM_Warning("VM_fclose: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1874 FS_Close(prog->openfiles[filenum]);
1875 prog->openfiles[filenum] = NULL;
1876 if(prog->openfiles_origin[filenum])
1877 PRVM_Free((char *)prog->openfiles_origin[filenum]);
1878 if (developer_extra.integer)
1879 Con_DPrintf("VM_fclose: %s: #%i closed\n", PRVM_NAME, filenum);
1886 string fgets(float fhandle)
1889 //string(float fhandle) fgets = #112; // reads a line of text from the file and returns as a tempstring
1893 char string[VM_STRINGTEMP_LENGTH];
1896 VM_SAFEPARMCOUNT(1,VM_fgets);
1898 // set the return value regardless of any possible errors
1899 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
1901 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1902 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
1904 VM_Warning("VM_fgets: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1907 if (prog->openfiles[filenum] == NULL)
1909 VM_Warning("VM_fgets: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1915 c = FS_Getc(prog->openfiles[filenum]);
1916 if (c == '\r' || c == '\n' || c < 0)
1918 if (end < VM_STRINGTEMP_LENGTH - 1)
1922 // remove \n following \r
1925 c = FS_Getc(prog->openfiles[filenum]);
1927 FS_UnGetc(prog->openfiles[filenum], (unsigned char)c);
1929 if (developer_extra.integer)
1930 Con_DPrintf("fgets: %s: %s\n", PRVM_NAME, string);
1932 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string);
1939 fputs(float fhandle, string s)
1942 //void(float fhandle, string s) fputs = #113; // writes a line of text to the end of the file
1946 char string[VM_STRINGTEMP_LENGTH];
1949 VM_SAFEPARMCOUNT(2,VM_fputs);
1951 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1952 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
1954 VM_Warning("VM_fputs: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1957 if (prog->openfiles[filenum] == NULL)
1959 VM_Warning("VM_fputs: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1962 VM_VarString(1, string, sizeof(string));
1963 if ((stringlength = (int)strlen(string)))
1964 FS_Write(prog->openfiles[filenum], string, stringlength);
1965 if (developer_extra.integer)
1966 Con_DPrintf("fputs: %s: %s\n", PRVM_NAME, string);
1973 writetofile(float fhandle, entity ent)
1976 void VM_writetofile(void)
1981 VM_SAFEPARMCOUNT(2, VM_writetofile);
1983 file = VM_GetFileHandle( (int)PRVM_G_FLOAT(OFS_PARM0) );
1986 VM_Warning("VM_writetofile: invalid or closed file handle\n");
1990 ent = PRVM_G_EDICT(OFS_PARM1);
1991 if(ent->priv.required->free)
1993 VM_Warning("VM_writetofile: %s: entity %i is free !\n", PRVM_NAME, PRVM_NUM_FOR_EDICT(ent));
1997 PRVM_ED_Write (file, ent);
2000 // KrimZon - DP_QC_ENTITYDATA
2005 float() numentityfields
2006 Return the number of entity fields - NOT offsets
2009 void VM_numentityfields(void)
2011 PRVM_G_FLOAT(OFS_RETURN) = prog->progs->numfielddefs;
2014 // KrimZon - DP_QC_ENTITYDATA
2019 string(float fieldnum) entityfieldname
2020 Return name of the specified field as a string, or empty if the field is invalid (warning)
2023 void VM_entityfieldname(void)
2026 int i = (int)PRVM_G_FLOAT(OFS_PARM0);
2028 if (i < 0 || i >= prog->progs->numfielddefs)
2030 VM_Warning("VM_entityfieldname: %s: field index out of bounds\n", PRVM_NAME);
2031 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString("");
2035 d = &prog->fielddefs[i];
2036 PRVM_G_INT(OFS_RETURN) = d->s_name; // presuming that s_name points to a string already
2039 // KrimZon - DP_QC_ENTITYDATA
2044 float(float fieldnum) entityfieldtype
2047 void VM_entityfieldtype(void)
2050 int i = (int)PRVM_G_FLOAT(OFS_PARM0);
2052 if (i < 0 || i >= prog->progs->numfielddefs)
2054 VM_Warning("VM_entityfieldtype: %s: field index out of bounds\n", PRVM_NAME);
2055 PRVM_G_FLOAT(OFS_RETURN) = -1.0;
2059 d = &prog->fielddefs[i];
2060 PRVM_G_FLOAT(OFS_RETURN) = (float)d->type;
2063 // KrimZon - DP_QC_ENTITYDATA
2066 VM_getentityfieldstring
2068 string(float fieldnum, entity ent) getentityfieldstring
2071 void VM_getentityfieldstring(void)
2073 // put the data into a string
2078 int i = (int)PRVM_G_FLOAT(OFS_PARM0);
2080 if (i < 0 || i >= prog->progs->numfielddefs)
2082 VM_Warning("VM_entityfielddata: %s: field index out of bounds\n", PRVM_NAME);
2083 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString("");
2087 d = &prog->fielddefs[i];
2090 ent = PRVM_G_EDICT(OFS_PARM1);
2091 if(ent->priv.required->free)
2093 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString("");
2094 VM_Warning("VM_entityfielddata: %s: entity %i is free !\n", PRVM_NAME, PRVM_NUM_FOR_EDICT(ent));
2097 v = (int *)((char *)ent->fields.vp + d->ofs*4);
2099 // if it's 0 or blank, return an empty string
2100 type = d->type & ~DEF_SAVEGLOBAL;
2101 for (j=0 ; j<prvm_type_size[type] ; j++)
2104 if (j == prvm_type_size[type])
2106 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString("");
2110 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(PRVM_UglyValueString((etype_t)d->type, (prvm_eval_t *)v));
2113 // KrimZon - DP_QC_ENTITYDATA
2116 VM_putentityfieldstring
2118 float(float fieldnum, entity ent, string s) putentityfieldstring
2121 void VM_putentityfieldstring(void)
2125 int i = (int)PRVM_G_FLOAT(OFS_PARM0);
2127 if (i < 0 || i >= prog->progs->numfielddefs)
2129 VM_Warning("VM_entityfielddata: %s: field index out of bounds\n", PRVM_NAME);
2130 PRVM_G_FLOAT(OFS_RETURN) = 0.0f;
2134 d = &prog->fielddefs[i];
2137 ent = PRVM_G_EDICT(OFS_PARM1);
2138 if(ent->priv.required->free)
2140 VM_Warning("VM_entityfielddata: %s: entity %i is free !\n", PRVM_NAME, PRVM_NUM_FOR_EDICT(ent));
2141 PRVM_G_FLOAT(OFS_RETURN) = 0.0f;
2145 // parse the string into the value
2146 PRVM_G_FLOAT(OFS_RETURN) = ( PRVM_ED_ParseEpair(ent, d, PRVM_G_STRING(OFS_PARM2), false) ) ? 1.0f : 0.0f;
2153 float strlen(string s)
2156 //float(string s) strlen = #114; // returns how many characters are in a string
2157 void VM_strlen(void)
2159 VM_SAFEPARMCOUNT(1,VM_strlen);
2161 //PRVM_G_FLOAT(OFS_RETURN) = strlen(PRVM_G_STRING(OFS_PARM0));
2162 PRVM_G_FLOAT(OFS_RETURN) = u8_strlen(PRVM_G_STRING(OFS_PARM0));
2165 // DRESK - Decolorized String
2170 string strdecolorize(string s)
2173 // string (string s) strdecolorize = #472; // returns the passed in string with color codes stripped
2174 void VM_strdecolorize(void)
2176 char szNewString[VM_STRINGTEMP_LENGTH];
2177 const char *szString;
2180 VM_SAFEPARMCOUNT(1,VM_strdecolorize);
2181 szString = PRVM_G_STRING(OFS_PARM0);
2182 COM_StringDecolorize(szString, 0, szNewString, sizeof(szNewString), TRUE);
2183 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(szNewString);
2186 // DRESK - String Length (not counting color codes)
2191 float strlennocol(string s)
2194 // float(string s) strlennocol = #471; // returns how many characters are in a string not including color codes
2195 // For example, ^2Dresk returns a length of 5
2196 void VM_strlennocol(void)
2198 const char *szString;
2201 VM_SAFEPARMCOUNT(1,VM_strlennocol);
2203 szString = PRVM_G_STRING(OFS_PARM0);
2205 //nCnt = COM_StringLengthNoColors(szString, 0, NULL);
2206 nCnt = u8_COM_StringLengthNoColors(szString, 0, NULL);
2208 PRVM_G_FLOAT(OFS_RETURN) = nCnt;
2211 // DRESK - String to Uppercase and Lowercase
2216 string strtolower(string s)
2219 // string (string s) strtolower = #480; // returns passed in string in lowercase form
2220 void VM_strtolower(void)
2222 char szNewString[VM_STRINGTEMP_LENGTH];
2223 const char *szString;
2226 VM_SAFEPARMCOUNT(1,VM_strtolower);
2227 szString = PRVM_G_STRING(OFS_PARM0);
2229 COM_ToLowerString(szString, szNewString, sizeof(szNewString) );
2231 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(szNewString);
2238 string strtoupper(string s)
2241 // string (string s) strtoupper = #481; // returns passed in string in uppercase form
2242 void VM_strtoupper(void)
2244 char szNewString[VM_STRINGTEMP_LENGTH];
2245 const char *szString;
2248 VM_SAFEPARMCOUNT(1,VM_strtoupper);
2249 szString = PRVM_G_STRING(OFS_PARM0);
2251 COM_ToUpperString(szString, szNewString, sizeof(szNewString) );
2253 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(szNewString);
2260 string strcat(string,string,...[string])
2263 //string(string s1, string s2) strcat = #115;
2264 // concatenates two strings (for example "abc", "def" would return "abcdef")
2265 // and returns as a tempstring
2266 void VM_strcat(void)
2268 char s[VM_STRINGTEMP_LENGTH];
2269 VM_SAFEPARMCOUNTRANGE(1, 8, VM_strcat);
2271 VM_VarString(0, s, sizeof(s));
2272 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(s);
2279 string substring(string s, float start, float length)
2282 // string(string s, float start, float length) substring = #116;
2283 // returns a section of a string as a tempstring
2284 void VM_substring(void)
2287 int u_slength = 0, u_start;
2290 char string[VM_STRINGTEMP_LENGTH];
2292 VM_SAFEPARMCOUNT(3,VM_substring);
2295 s = PRVM_G_STRING(OFS_PARM0);
2296 start = (int)PRVM_G_FLOAT(OFS_PARM1);
2297 length = (int)PRVM_G_FLOAT(OFS_PARM2);
2298 slength = strlen(s);
2300 if (start < 0) // FTE_STRINGS feature
2302 start = bound(0, start, slength);
2304 if (length < 0) // FTE_STRINGS feature
2305 length += slength - start + 1;
2306 maxlen = min((int)sizeof(string) - 1, slength - start);
2307 length = bound(0, length, maxlen);
2309 memcpy(string, s + start, length);
2311 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string);
2314 s = PRVM_G_STRING(OFS_PARM0);
2315 start = (int)PRVM_G_FLOAT(OFS_PARM1);
2316 length = (int)PRVM_G_FLOAT(OFS_PARM2);
2318 if (start < 0) // FTE_STRINGS feature
2320 u_slength = u8_strlen(s);
2322 start = bound(0, start, u_slength);
2325 if (length < 0) // FTE_STRINGS feature
2327 if (!u_slength) // it's not calculated when it's not needed above
2328 u_slength = u8_strlen(s);
2329 length += u_slength - start + 1;
2332 // positive start, positive length
2333 u_start = u8_byteofs(s, start, NULL);
2336 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString("");
2339 u_length = u8_bytelen(s + u_start, length);
2340 if (u_length >= sizeof(string)-1)
2341 u_length = sizeof(string)-1;
2343 memcpy(string, s + u_start, u_length);
2344 string[u_length] = 0;
2345 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string);
2352 string(string search, string replace, string subject) strreplace = #484;
2355 // replaces all occurrences of search with replace in the string subject, and returns the result
2356 void VM_strreplace(void)
2359 const char *search, *replace, *subject;
2360 char string[VM_STRINGTEMP_LENGTH];
2361 int search_len, replace_len, subject_len;
2363 VM_SAFEPARMCOUNT(3,VM_strreplace);
2365 search = PRVM_G_STRING(OFS_PARM0);
2366 replace = PRVM_G_STRING(OFS_PARM1);
2367 subject = PRVM_G_STRING(OFS_PARM2);
2369 search_len = (int)strlen(search);
2370 replace_len = (int)strlen(replace);
2371 subject_len = (int)strlen(subject);
2374 for (i = 0; i < subject_len; i++)
2376 for (j = 0; j < search_len && i+j < subject_len; j++)
2377 if (subject[i+j] != search[j])
2379 if (j == search_len || i+j == subject_len)
2381 // found it at offset 'i'
2382 for (j = 0; j < replace_len && si < (int)sizeof(string) - 1; j++)
2383 string[si++] = replace[j];
2384 i += search_len - 1;
2389 if (si < (int)sizeof(string) - 1)
2390 string[si++] = subject[i];
2395 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string);
2402 string(string search, string replace, string subject) strireplace = #485;
2405 // case-insensitive version of strreplace
2406 void VM_strireplace(void)
2409 const char *search, *replace, *subject;
2410 char string[VM_STRINGTEMP_LENGTH];
2411 int search_len, replace_len, subject_len;
2413 VM_SAFEPARMCOUNT(3,VM_strreplace);
2415 search = PRVM_G_STRING(OFS_PARM0);
2416 replace = PRVM_G_STRING(OFS_PARM1);
2417 subject = PRVM_G_STRING(OFS_PARM2);
2419 search_len = (int)strlen(search);
2420 replace_len = (int)strlen(replace);
2421 subject_len = (int)strlen(subject);
2424 for (i = 0; i < subject_len; i++)
2426 for (j = 0; j < search_len && i+j < subject_len; j++)
2427 if (tolower(subject[i+j]) != tolower(search[j]))
2429 if (j == search_len || i+j == subject_len)
2431 // found it at offset 'i'
2432 for (j = 0; j < replace_len && si < (int)sizeof(string) - 1; j++)
2433 string[si++] = replace[j];
2434 i += search_len - 1;
2439 if (si < (int)sizeof(string) - 1)
2440 string[si++] = subject[i];
2445 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string);
2452 vector stov(string s)
2455 //vector(string s) stov = #117; // returns vector value from a string
2458 char string[VM_STRINGTEMP_LENGTH];
2460 VM_SAFEPARMCOUNT(1,VM_stov);
2462 VM_VarString(0, string, sizeof(string));
2463 Math_atov(string, PRVM_G_VECTOR(OFS_RETURN));
2470 string strzone(string s)
2473 //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)
2474 void VM_strzone(void)
2477 char string[VM_STRINGTEMP_LENGTH];
2480 VM_SAFEPARMCOUNT(1,VM_strzone);
2482 VM_VarString(0, string, sizeof(string));
2483 alloclen = strlen(string) + 1;
2484 PRVM_G_INT(OFS_RETURN) = PRVM_AllocString(alloclen, &out);
2485 memcpy(out, string, alloclen);
2495 //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!!!)
2496 void VM_strunzone(void)
2498 VM_SAFEPARMCOUNT(1,VM_strunzone);
2499 PRVM_FreeString(PRVM_G_INT(OFS_PARM0));
2504 VM_command (used by client and menu)
2506 clientcommand(float client, string s) (for client and menu)
2509 //void(entity e, string s) clientcommand = #440; // executes a command string as if it came from the specified client
2510 //this function originally written by KrimZon, made shorter by LordHavoc
2511 void VM_clcommand (void)
2513 client_t *temp_client;
2516 VM_SAFEPARMCOUNT(2,VM_clcommand);
2518 i = (int)PRVM_G_FLOAT(OFS_PARM0);
2519 if (!sv.active || i < 0 || i >= svs.maxclients || !svs.clients[i].active)
2521 VM_Warning("VM_clientcommand: %s: invalid client/server is not active !\n", PRVM_NAME);
2525 temp_client = host_client;
2526 host_client = svs.clients + i;
2527 Cmd_ExecuteString (PRVM_G_STRING(OFS_PARM1), src_client);
2528 host_client = temp_client;
2536 float tokenize(string s)
2539 //float(string s) tokenize = #441; // takes apart a string into individal words (access them with argv), returns how many
2540 //this function originally written by KrimZon, made shorter by LordHavoc
2541 //20040203: rewritten by LordHavoc (no longer uses allocations)
2542 static int num_tokens = 0;
2543 static int tokens[VM_STRINGTEMP_LENGTH / 2];
2544 static int tokens_startpos[VM_STRINGTEMP_LENGTH / 2];
2545 static int tokens_endpos[VM_STRINGTEMP_LENGTH / 2];
2546 static char tokenize_string[VM_STRINGTEMP_LENGTH];
2547 void VM_tokenize (void)
2551 VM_SAFEPARMCOUNT(1,VM_tokenize);
2553 strlcpy(tokenize_string, PRVM_G_STRING(OFS_PARM0), sizeof(tokenize_string));
2554 p = tokenize_string;
2559 if (num_tokens >= (int)(sizeof(tokens)/sizeof(tokens[0])))
2562 // skip whitespace here to find token start pos
2563 while(*p && ISWHITESPACE(*p))
2566 tokens_startpos[num_tokens] = p - tokenize_string;
2567 if(!COM_ParseToken_VM_Tokenize(&p, false))
2569 tokens_endpos[num_tokens] = p - tokenize_string;
2570 tokens[num_tokens] = PRVM_SetTempString(com_token);
2574 PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
2577 //float(string s) tokenize = #514; // takes apart a string into individal words (access them with argv), returns how many
2578 void VM_tokenize_console (void)
2582 VM_SAFEPARMCOUNT(1,VM_tokenize);
2584 strlcpy(tokenize_string, PRVM_G_STRING(OFS_PARM0), sizeof(tokenize_string));
2585 p = tokenize_string;
2590 if (num_tokens >= (int)(sizeof(tokens)/sizeof(tokens[0])))
2593 // skip whitespace here to find token start pos
2594 while(*p && ISWHITESPACE(*p))
2597 tokens_startpos[num_tokens] = p - tokenize_string;
2598 if(!COM_ParseToken_Console(&p))
2600 tokens_endpos[num_tokens] = p - tokenize_string;
2601 tokens[num_tokens] = PRVM_SetTempString(com_token);
2605 PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
2610 VM_tokenizebyseparator
2612 float tokenizebyseparator(string s, string separator1, ...)
2615 //float(string s, string separator1, ...) tokenizebyseparator = #479; // takes apart a string into individal words (access them with argv), returns how many
2616 //this function returns the token preceding each instance of a separator (of
2617 //which there can be multiple), and the text following the last separator
2618 //useful for parsing certain kinds of data like IP addresses
2620 //numnumbers = tokenizebyseparator("10.1.2.3", ".");
2621 //returns 4 and the tokens "10" "1" "2" "3".
2622 void VM_tokenizebyseparator (void)
2626 int separatorlen[7];
2627 const char *separators[7];
2630 char tokentext[MAX_INPUTLINE];
2632 VM_SAFEPARMCOUNTRANGE(2, 8,VM_tokenizebyseparator);
2634 strlcpy(tokenize_string, PRVM_G_STRING(OFS_PARM0), sizeof(tokenize_string));
2635 p = tokenize_string;
2638 for (j = 1;j < prog->argc;j++)
2640 // skip any blank separator strings
2641 const char *s = PRVM_G_STRING(OFS_PARM0+j*3);
2644 separators[numseparators] = s;
2645 separatorlen[numseparators] = strlen(s);
2652 while (num_tokens < (int)(sizeof(tokens)/sizeof(tokens[0])))
2654 token = tokentext + j;
2655 tokens_startpos[num_tokens] = p - tokenize_string;
2659 for (k = 0;k < numseparators;k++)
2661 if (!strncmp(p, separators[k], separatorlen[k]))
2663 p += separatorlen[k];
2667 if (k < numseparators)
2669 if (j < (int)sizeof(tokentext)-1)
2670 tokentext[j++] = *p;
2674 tokens_endpos[num_tokens] = p0 - tokenize_string;
2675 if (j >= (int)sizeof(tokentext))
2678 tokens[num_tokens++] = PRVM_SetTempString(token);
2683 PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
2686 //string(float n) argv = #442; // returns a word from the tokenized string (returns nothing for an invalid index)
2687 //this function originally written by KrimZon, made shorter by LordHavoc
2692 VM_SAFEPARMCOUNT(1,VM_argv);
2694 token_num = (int)PRVM_G_FLOAT(OFS_PARM0);
2697 token_num += num_tokens;
2699 if (token_num >= 0 && token_num < num_tokens)
2700 PRVM_G_INT(OFS_RETURN) = tokens[token_num];
2702 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
2705 //float(float n) argv_start_index = #515; // returns the start index of a token
2706 void VM_argv_start_index (void)
2710 VM_SAFEPARMCOUNT(1,VM_argv);
2712 token_num = (int)PRVM_G_FLOAT(OFS_PARM0);
2715 token_num += num_tokens;
2717 if (token_num >= 0 && token_num < num_tokens)
2718 PRVM_G_FLOAT(OFS_RETURN) = tokens_startpos[token_num];
2720 PRVM_G_FLOAT(OFS_RETURN) = -1;
2723 //float(float n) argv_end_index = #516; // returns the end index of a token
2724 void VM_argv_end_index (void)
2728 VM_SAFEPARMCOUNT(1,VM_argv);
2730 token_num = (int)PRVM_G_FLOAT(OFS_PARM0);
2733 token_num += num_tokens;
2735 if (token_num >= 0 && token_num < num_tokens)
2736 PRVM_G_FLOAT(OFS_RETURN) = tokens_endpos[token_num];
2738 PRVM_G_FLOAT(OFS_RETURN) = -1;
2748 void VM_isserver(void)
2750 VM_SAFEPARMCOUNT(0,VM_serverstate);
2752 PRVM_G_FLOAT(OFS_RETURN) = sv.active && (svs.maxclients > 1 || cls.state == ca_dedicated);
2762 void VM_clientcount(void)
2764 VM_SAFEPARMCOUNT(0,VM_clientcount);
2766 PRVM_G_FLOAT(OFS_RETURN) = svs.maxclients;
2776 void VM_clientstate(void)
2778 VM_SAFEPARMCOUNT(0,VM_clientstate);
2781 switch( cls.state ) {
2782 case ca_uninitialized:
2784 PRVM_G_FLOAT(OFS_RETURN) = 0;
2786 case ca_disconnected:
2787 PRVM_G_FLOAT(OFS_RETURN) = 1;
2790 PRVM_G_FLOAT(OFS_RETURN) = 2;
2793 // should never be reached!
2802 float getostype(void)
2804 */ // not used at the moment -> not included in the common list
2805 void VM_getostype(void)
2807 VM_SAFEPARMCOUNT(0,VM_getostype);
2812 OS_MAC - not supported
2816 PRVM_G_FLOAT(OFS_RETURN) = 0;
2817 #elif defined(MACOSX)
2818 PRVM_G_FLOAT(OFS_RETURN) = 2;
2820 PRVM_G_FLOAT(OFS_RETURN) = 1;
2831 extern double host_starttime;
2832 float CDAudio_GetPosition(void);
2833 void VM_gettime(void)
2837 VM_SAFEPARMCOUNTRANGE(0,1,VM_gettime);
2841 PRVM_G_FLOAT(OFS_RETURN) = (float) realtime;
2845 timer_index = (int) PRVM_G_FLOAT(OFS_PARM0);
2848 case 0: // GETTIME_FRAMESTART
2849 PRVM_G_FLOAT(OFS_RETURN) = (float) realtime;
2851 case 1: // GETTIME_REALTIME
2852 PRVM_G_FLOAT(OFS_RETURN) = (float) Sys_DoubleTime();
2854 case 2: // GETTIME_HIRES
2855 PRVM_G_FLOAT(OFS_RETURN) = (float) (Sys_DoubleTime() - realtime);
2857 case 3: // GETTIME_UPTIME
2858 PRVM_G_FLOAT(OFS_RETURN) = (float) (Sys_DoubleTime() - host_starttime);
2860 case 4: // GETTIME_CDTRACK
2861 PRVM_G_FLOAT(OFS_RETURN) = (float) CDAudio_GetPosition();
2864 VM_Warning("VM_gettime: %s: unsupported timer specified, returning realtime\n", PRVM_NAME);
2865 PRVM_G_FLOAT(OFS_RETURN) = (float) realtime;
2875 float getsoundtime(void)
2879 void VM_getsoundtime (void)
2881 int entnum, entchannel, pnum;
2882 VM_SAFEPARMCOUNT(2,VM_getsoundtime);
2884 pnum = PRVM_GetProgNr();
2885 if (pnum == PRVM_MENUPROG)
2887 VM_Warning("VM_getsoundtime: %s: not supported on this progs\n", PRVM_NAME);
2888 PRVM_G_FLOAT(OFS_RETURN) = -1;
2891 entnum = ((pnum == PRVM_CLIENTPROG) ? MAX_EDICTS : 0) + PRVM_NUM_FOR_EDICT(PRVM_G_EDICT(OFS_PARM0));
2892 entchannel = (int)PRVM_G_FLOAT(OFS_PARM1);
2893 if (entchannel <= 0 || entchannel > 8)
2894 VM_Warning("VM_getsoundtime: %s: bad channel %i\n", PRVM_NAME, entchannel);
2895 PRVM_G_FLOAT(OFS_RETURN) = (float)S_GetEntChannelPosition(entnum, entchannel);
2902 string soundlength (string sample)
2905 void VM_soundlength (void)
2909 VM_SAFEPARMCOUNT(1, VM_soundlength);
2911 s = PRVM_G_STRING(OFS_PARM0);
2912 PRVM_G_FLOAT(OFS_RETURN) = S_SoundLength(s);
2919 loadfromdata(string data)
2922 void VM_loadfromdata(void)
2924 VM_SAFEPARMCOUNT(1,VM_loadentsfromfile);
2926 PRVM_ED_LoadFromFile(PRVM_G_STRING(OFS_PARM0));
2930 ========================
2933 parseentitydata(entity ent, string data)
2934 ========================
2936 void VM_parseentitydata(void)
2941 VM_SAFEPARMCOUNT(2, VM_parseentitydata);
2943 // get edict and test it
2944 ent = PRVM_G_EDICT(OFS_PARM0);
2945 if (ent->priv.required->free)
2946 PRVM_ERROR ("VM_parseentitydata: %s: Can only set already spawned entities (entity %i is free)!", PRVM_NAME, PRVM_NUM_FOR_EDICT(ent));
2948 data = PRVM_G_STRING(OFS_PARM1);
2950 // parse the opening brace
2951 if (!COM_ParseToken_Simple(&data, false, false) || com_token[0] != '{' )
2952 PRVM_ERROR ("VM_parseentitydata: %s: Couldn't parse entity data:\n%s", PRVM_NAME, data );
2954 PRVM_ED_ParseEdict (data, ent);
2961 loadfromfile(string file)
2964 void VM_loadfromfile(void)
2966 const char *filename;
2969 VM_SAFEPARMCOUNT(1,VM_loadfromfile);
2971 filename = PRVM_G_STRING(OFS_PARM0);
2972 if (FS_CheckNastyPath(filename, false))
2974 PRVM_G_FLOAT(OFS_RETURN) = -4;
2975 VM_Warning("VM_loadfromfile: %s dangerous or non-portable filename \"%s\" not allowed. (contains : or \\ or begins with .. or /)\n", PRVM_NAME, filename);
2979 // not conform with VM_fopen
2980 data = (char *)FS_LoadFile(filename, tempmempool, false, NULL);
2982 PRVM_G_FLOAT(OFS_RETURN) = -1;
2984 PRVM_ED_LoadFromFile(data);
2995 float mod(float val, float m)
2998 void VM_modulo(void)
3001 VM_SAFEPARMCOUNT(2,VM_module);
3003 val = (int) PRVM_G_FLOAT(OFS_PARM0);
3004 m = (int) PRVM_G_FLOAT(OFS_PARM1);
3006 PRVM_G_FLOAT(OFS_RETURN) = (float) (val % m);
3009 void VM_Search_Init(void)
3012 for (i = 0;i < PRVM_MAX_OPENSEARCHES;i++)
3013 prog->opensearches[i] = NULL;
3016 void VM_Search_Reset(void)
3019 // reset the fssearch list
3020 for(i = 0; i < PRVM_MAX_OPENSEARCHES; i++)
3022 if(prog->opensearches[i])
3023 FS_FreeSearch(prog->opensearches[i]);
3024 prog->opensearches[i] = NULL;
3032 float search_begin(string pattern, float caseinsensitive, float quiet)
3035 void VM_search_begin(void)
3038 const char *pattern;
3039 int caseinsens, quiet;
3041 VM_SAFEPARMCOUNT(3, VM_search_begin);
3043 pattern = PRVM_G_STRING(OFS_PARM0);
3045 VM_CheckEmptyString(pattern);
3047 caseinsens = (int)PRVM_G_FLOAT(OFS_PARM1);
3048 quiet = (int)PRVM_G_FLOAT(OFS_PARM2);
3050 for(handle = 0; handle < PRVM_MAX_OPENSEARCHES; handle++)
3051 if(!prog->opensearches[handle])
3054 if(handle >= PRVM_MAX_OPENSEARCHES)
3056 PRVM_G_FLOAT(OFS_RETURN) = -2;
3057 VM_Warning("VM_search_begin: %s ran out of search handles (%i)\n", PRVM_NAME, PRVM_MAX_OPENSEARCHES);
3061 if(!(prog->opensearches[handle] = FS_Search(pattern,caseinsens, quiet)))
3062 PRVM_G_FLOAT(OFS_RETURN) = -1;
3065 prog->opensearches_origin[handle] = PRVM_AllocationOrigin();
3066 PRVM_G_FLOAT(OFS_RETURN) = handle;
3074 void search_end(float handle)
3077 void VM_search_end(void)
3080 VM_SAFEPARMCOUNT(1, VM_search_end);
3082 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
3084 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
3086 VM_Warning("VM_search_end: invalid handle %i used in %s\n", handle, PRVM_NAME);
3089 if(prog->opensearches[handle] == NULL)
3091 VM_Warning("VM_search_end: no such handle %i in %s\n", handle, PRVM_NAME);
3095 FS_FreeSearch(prog->opensearches[handle]);
3096 prog->opensearches[handle] = NULL;
3097 if(prog->opensearches_origin[handle])
3098 PRVM_Free((char *)prog->opensearches_origin[handle]);
3105 float search_getsize(float handle)
3108 void VM_search_getsize(void)
3111 VM_SAFEPARMCOUNT(1, VM_M_search_getsize);
3113 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
3115 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
3117 VM_Warning("VM_search_getsize: invalid handle %i used in %s\n", handle, PRVM_NAME);
3120 if(prog->opensearches[handle] == NULL)
3122 VM_Warning("VM_search_getsize: no such handle %i in %s\n", handle, PRVM_NAME);
3126 PRVM_G_FLOAT(OFS_RETURN) = prog->opensearches[handle]->numfilenames;
3131 VM_search_getfilename
3133 string search_getfilename(float handle, float num)
3136 void VM_search_getfilename(void)
3138 int handle, filenum;
3139 VM_SAFEPARMCOUNT(2, VM_search_getfilename);
3141 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
3142 filenum = (int)PRVM_G_FLOAT(OFS_PARM1);
3144 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
3146 VM_Warning("VM_search_getfilename: invalid handle %i used in %s\n", handle, PRVM_NAME);
3149 if(prog->opensearches[handle] == NULL)
3151 VM_Warning("VM_search_getfilename: no such handle %i in %s\n", handle, PRVM_NAME);
3154 if(filenum < 0 || filenum >= prog->opensearches[handle]->numfilenames)
3156 VM_Warning("VM_search_getfilename: invalid filenum %i in %s\n", filenum, PRVM_NAME);
3160 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog->opensearches[handle]->filenames[filenum]);
3167 string chr(float ascii)
3174 VM_SAFEPARMCOUNT(1, VM_chr);
3176 tmp[0] = (unsigned char) PRVM_G_FLOAT(OFS_PARM0);
3179 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(tmp);
3184 VM_SAFEPARMCOUNT(1, VM_chr);
3186 len = u8_fromchar((Uchar)PRVM_G_FLOAT(OFS_PARM0), tmp, sizeof(tmp));
3188 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(tmp);
3191 //=============================================================================
3192 // Draw builtins (client & menu)
3198 float iscachedpic(string pic)
3201 void VM_iscachedpic(void)
3203 VM_SAFEPARMCOUNT(1,VM_iscachedpic);
3205 // drawq hasnt such a function, thus always return true
3206 PRVM_G_FLOAT(OFS_RETURN) = false;
3213 string precache_pic(string pic)
3216 void VM_precache_pic(void)
3220 VM_SAFEPARMCOUNT(1, VM_precache_pic);
3222 s = PRVM_G_STRING(OFS_PARM0);
3223 PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
3224 VM_CheckEmptyString (s);
3226 // AK Draw_CachePic is supposed to always return a valid pointer
3227 if( Draw_CachePic_Flags(s, 0)->tex == r_texture_notexture )
3228 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
3238 void VM_freepic(void)
3242 VM_SAFEPARMCOUNT(1,VM_freepic);
3244 s = PRVM_G_STRING(OFS_PARM0);
3245 VM_CheckEmptyString (s);
3250 void getdrawfontscale(float *sx, float *sy)
3254 if(prog->globaloffsets.drawfontscale >= 0)
3256 VectorCopy(PRVM_G_VECTOR(prog->globaloffsets.drawfontscale), v);
3257 if(VectorLength2(v) > 0)
3265 dp_font_t *getdrawfont(void)
3267 if(prog->globaloffsets.drawfont >= 0)
3269 int f = (int) PRVM_G_FLOAT(prog->globaloffsets.drawfont);
3270 if(f < 0 || f >= dp_fonts.maxsize)
3271 return FONT_DEFAULT;
3272 return &dp_fonts.f[f];
3275 return FONT_DEFAULT;
3282 float drawcharacter(vector position, float character, vector scale, vector rgb, float alpha, float flag)
3285 void VM_drawcharacter(void)
3287 float *pos,*scale,*rgb;
3291 VM_SAFEPARMCOUNT(6,VM_drawcharacter);
3293 character = (char) PRVM_G_FLOAT(OFS_PARM1);
3296 PRVM_G_FLOAT(OFS_RETURN) = -1;
3297 VM_Warning("VM_drawcharacter: %s passed null character !\n",PRVM_NAME);
3301 pos = PRVM_G_VECTOR(OFS_PARM0);
3302 scale = PRVM_G_VECTOR(OFS_PARM2);
3303 rgb = PRVM_G_VECTOR(OFS_PARM3);
3304 flag = (int)PRVM_G_FLOAT(OFS_PARM5);
3306 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3308 PRVM_G_FLOAT(OFS_RETURN) = -2;
3309 VM_Warning("VM_drawcharacter: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
3313 if(pos[2] || scale[2])
3314 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")));
3316 if(!scale[0] || !scale[1])
3318 PRVM_G_FLOAT(OFS_RETURN) = -3;
3319 VM_Warning("VM_drawcharacter: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
3323 getdrawfontscale(&sx, &sy);
3324 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());
3325 PRVM_G_FLOAT(OFS_RETURN) = 1;
3332 float drawstring(vector position, string text, vector scale, vector rgb, float alpha, float flag)
3335 void VM_drawstring(void)
3337 float *pos,*scale,*rgb;
3341 VM_SAFEPARMCOUNT(6,VM_drawstring);
3343 string = PRVM_G_STRING(OFS_PARM1);
3344 pos = PRVM_G_VECTOR(OFS_PARM0);
3345 scale = PRVM_G_VECTOR(OFS_PARM2);
3346 rgb = PRVM_G_VECTOR(OFS_PARM3);
3347 flag = (int)PRVM_G_FLOAT(OFS_PARM5);
3349 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3351 PRVM_G_FLOAT(OFS_RETURN) = -2;
3352 VM_Warning("VM_drawstring: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
3356 if(!scale[0] || !scale[1])
3358 PRVM_G_FLOAT(OFS_RETURN) = -3;
3359 VM_Warning("VM_drawstring: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
3363 if(pos[2] || scale[2])
3364 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")));
3366 getdrawfontscale(&sx, &sy);
3367 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());
3368 //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);
3369 PRVM_G_FLOAT(OFS_RETURN) = 1;
3374 VM_drawcolorcodedstring
3376 float drawcolorcodedstring(vector position, string text, vector scale, float alpha, float flag)
3379 void VM_drawcolorcodedstring(void)
3385 VM_SAFEPARMCOUNT(5,VM_drawstring);
3387 string = PRVM_G_STRING(OFS_PARM1);
3388 pos = PRVM_G_VECTOR(OFS_PARM0);
3389 scale = PRVM_G_VECTOR(OFS_PARM2);
3390 flag = (int)PRVM_G_FLOAT(OFS_PARM4);
3392 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3394 PRVM_G_FLOAT(OFS_RETURN) = -2;
3395 VM_Warning("VM_drawcolorcodedstring: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
3399 if(!scale[0] || !scale[1])
3401 PRVM_G_FLOAT(OFS_RETURN) = -3;
3402 VM_Warning("VM_drawcolorcodedstring: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
3406 if(pos[2] || scale[2])
3407 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")));
3409 getdrawfontscale(&sx, &sy);
3410 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());
3411 PRVM_G_FLOAT(OFS_RETURN) = 1;
3417 float stringwidth(string text, float allowColorCodes, float size)
3420 void VM_stringwidth(void)
3424 float mult; // sz is intended font size so we can later add freetype support, mult is font size multiplier in pixels per character cell
3428 VM_SAFEPARMCOUNTRANGE(2,3,VM_drawstring);
3430 getdrawfontscale(&sx, &sy);
3433 szv = PRVM_G_VECTOR(OFS_PARM2);
3438 // we want the width for 8x8 font size, divided by 8
3439 static float defsize[] = {8, 8};
3442 // to make sure snapping is turned off, ALWAYS use a nontrivial scale in this case
3443 if(sx >= 0.9 && sx <= 1.1)
3451 string = PRVM_G_STRING(OFS_PARM0);
3452 colors = (int)PRVM_G_FLOAT(OFS_PARM1);
3454 PRVM_G_FLOAT(OFS_RETURN) = DrawQ_TextWidth_UntilWidth_TrackColors_Scale(string, &maxlen, szv[0], szv[1], sx, sy, NULL, !colors, getdrawfont(), 1000000000) * mult;
3458 mult = sz = PRVM_G_FLOAT(OFS_PARM2);
3466 string = PRVM_G_STRING(OFS_PARM0);
3467 colors = (int)PRVM_G_FLOAT(OFS_PARM1);
3469 PRVM_G_FLOAT(OFS_RETURN) = DrawQ_TextWidth(string, 0, !colors, getdrawfont()) * mult; // 1x1 characters, don't actually draw
3477 float findfont(string s)
3481 float getdrawfontnum(const char *fontname)
3485 for(i = 0; i < dp_fonts.maxsize; ++i)
3486 if(!strcmp(dp_fonts.f[i].title, fontname))
3491 void VM_findfont(void)
3493 VM_SAFEPARMCOUNT(1,VM_findfont);
3494 PRVM_G_FLOAT(OFS_RETURN) = getdrawfontnum(PRVM_G_STRING(OFS_PARM0));
3501 float loadfont(string fontname, string fontmaps, string sizes, float slot)
3505 dp_font_t *FindFont(const char *title, qboolean allocate_new);
3506 void LoadFont(qboolean override, const char *name, dp_font_t *fnt, float scale, float voffset);
3507 void VM_loadfont(void)
3509 const char *fontname, *filelist, *sizes, *c, *cm;
3510 char mainfont[MAX_QPATH];
3512 float sz, scale, voffset;
3515 VM_SAFEPARMCOUNTRANGE(3,6,VM_loadfont);
3517 fontname = PRVM_G_STRING(OFS_PARM0);
3519 fontname = "default";
3521 filelist = PRVM_G_STRING(OFS_PARM1);
3523 filelist = "gfx/conchars";
3525 sizes = PRVM_G_STRING(OFS_PARM2);
3531 if (prog->argc >= 4)
3533 i = PRVM_G_FLOAT(OFS_PARM3);
3534 if (i >= 0 && i < dp_fonts.maxsize)
3537 strlcpy(f->title, fontname, sizeof(f->title)); // replace name
3541 f = FindFont(fontname, true);
3544 PRVM_G_FLOAT(OFS_RETURN) = -1;
3545 return; // something go wrong
3548 memset(f->fallbacks, 0, sizeof(f->fallbacks));
3549 memset(f->fallback_faces, 0, sizeof(f->fallback_faces));
3551 // first font is handled "normally"
3552 c = strchr(filelist, ':');
3553 cm = strchr(filelist, ',');
3554 if(c && (!cm || c < cm))
3555 f->req_face = atoi(c+1);
3561 if(!c || (c - filelist) > MAX_QPATH)
3562 strlcpy(mainfont, filelist, sizeof(mainfont));
3565 memcpy(mainfont, filelist, c - filelist);
3566 mainfont[c - filelist] = 0;
3570 for(i = 0; i < MAX_FONT_FALLBACKS; ++i)
3572 c = strchr(filelist, ',');
3578 c = strchr(filelist, ':');
3579 cm = strchr(filelist, ',');
3580 if(c && (!cm || c < cm))
3581 f->fallback_faces[i] = atoi(c+1);
3584 f->fallback_faces[i] = 0; // f->req_face; could make it stick to the default-font's face index
3587 if(!c || (c-filelist) > MAX_QPATH)
3589 strlcpy(f->fallbacks[i], filelist, sizeof(mainfont));
3593 memcpy(f->fallbacks[i], filelist, c - filelist);
3594 f->fallbacks[i][c - filelist] = 0;
3599 for(i = 0; i < MAX_FONT_SIZES; ++i)
3600 f->req_sizes[i] = -1;
3601 for (numsizes = 0,c = sizes;;)
3603 if (!COM_ParseToken_VM_Tokenize(&c, 0))
3605 sz = atof(com_token);
3607 if (sz < 0.001f || sz > 1000.0f)
3609 VM_Warning("VM_loadfont: crap size %s", com_token);
3613 if (numsizes == MAX_FONT_SIZES)
3615 VM_Warning("VM_loadfont: MAX_FONT_SIZES = %i exceeded", MAX_FONT_SIZES);
3618 f->req_sizes[numsizes] = sz;
3622 // additional scale/hoffset parms
3625 if (prog->argc >= 5)
3627 scale = PRVM_G_FLOAT(OFS_PARM4);
3631 if (prog->argc >= 6)
3632 voffset = PRVM_G_FLOAT(OFS_PARM5);
3635 LoadFont(true, mainfont, f, scale, voffset);
3637 // return index of loaded font
3638 PRVM_G_FLOAT(OFS_RETURN) = (f - dp_fonts.f);
3645 float drawpic(vector position, string pic, vector size, vector rgb, float alpha, float flag)
3648 void VM_drawpic(void)
3650 const char *picname;
3651 float *size, *pos, *rgb;
3654 VM_SAFEPARMCOUNT(6,VM_drawpic);
3656 picname = PRVM_G_STRING(OFS_PARM1);
3657 VM_CheckEmptyString (picname);
3659 // is pic cached ? no function yet for that
3662 PRVM_G_FLOAT(OFS_RETURN) = -4;
3663 VM_Warning("VM_drawpic: %s: %s not cached !\n", PRVM_NAME, picname);
3667 pos = PRVM_G_VECTOR(OFS_PARM0);
3668 size = PRVM_G_VECTOR(OFS_PARM2);
3669 rgb = PRVM_G_VECTOR(OFS_PARM3);
3670 flag = (int) PRVM_G_FLOAT(OFS_PARM5);
3672 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3674 PRVM_G_FLOAT(OFS_RETURN) = -2;
3675 VM_Warning("VM_drawpic: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
3679 if(pos[2] || size[2])
3680 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")));
3682 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);
3683 PRVM_G_FLOAT(OFS_RETURN) = 1;
3689 float drawrotpic(vector position, string pic, vector size, vector org, float angle, vector rgb, float alpha, float flag)
3692 void VM_drawrotpic(void)
3694 const char *picname;
3695 float *size, *pos, *org, *rgb;
3698 VM_SAFEPARMCOUNT(8,VM_drawrotpic);
3700 picname = PRVM_G_STRING(OFS_PARM1);
3701 VM_CheckEmptyString (picname);
3703 // is pic cached ? no function yet for that
3706 PRVM_G_FLOAT(OFS_RETURN) = -4;
3707 VM_Warning("VM_drawrotpic: %s: %s not cached !\n", PRVM_NAME, picname);
3711 pos = PRVM_G_VECTOR(OFS_PARM0);
3712 size = PRVM_G_VECTOR(OFS_PARM2);
3713 org = PRVM_G_VECTOR(OFS_PARM3);
3714 rgb = PRVM_G_VECTOR(OFS_PARM5);
3715 flag = (int) PRVM_G_FLOAT(OFS_PARM7);
3717 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3719 PRVM_G_FLOAT(OFS_RETURN) = -2;
3720 VM_Warning("VM_drawrotpic: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
3724 if(pos[2] || size[2] || org[2])
3725 Con_Printf("VM_drawrotpic: z value from pos/size/org discarded\n");
3727 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);
3728 PRVM_G_FLOAT(OFS_RETURN) = 1;
3734 float drawsubpic(vector position, vector size, string pic, vector srcPos, vector srcSize, vector rgb, float alpha, float flag)
3738 void VM_drawsubpic(void)
3740 const char *picname;
3741 float *size, *pos, *rgb, *srcPos, *srcSize, alpha;
3744 VM_SAFEPARMCOUNT(8,VM_drawsubpic);
3746 picname = PRVM_G_STRING(OFS_PARM2);
3747 VM_CheckEmptyString (picname);
3749 // is pic cached ? no function yet for that
3752 PRVM_G_FLOAT(OFS_RETURN) = -4;
3753 VM_Warning("VM_drawsubpic: %s: %s not cached !\n", PRVM_NAME, picname);
3757 pos = PRVM_G_VECTOR(OFS_PARM0);
3758 size = PRVM_G_VECTOR(OFS_PARM1);
3759 srcPos = PRVM_G_VECTOR(OFS_PARM3);
3760 srcSize = PRVM_G_VECTOR(OFS_PARM4);
3761 rgb = PRVM_G_VECTOR(OFS_PARM5);
3762 alpha = PRVM_G_FLOAT(OFS_PARM6);
3763 flag = (int) PRVM_G_FLOAT(OFS_PARM7);
3765 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3767 PRVM_G_FLOAT(OFS_RETURN) = -2;
3768 VM_Warning("VM_drawsubpic: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
3772 if(pos[2] || size[2])
3773 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")));
3775 DrawQ_SuperPic(pos[0], pos[1], Draw_CachePic_Flags (picname, CACHEPICFLAG_NOTPERSISTENT),
3777 srcPos[0], srcPos[1], rgb[0], rgb[1], rgb[2], alpha,
3778 srcPos[0] + srcSize[0], srcPos[1], rgb[0], rgb[1], rgb[2], alpha,
3779 srcPos[0], srcPos[1] + srcSize[1], rgb[0], rgb[1], rgb[2], alpha,
3780 srcPos[0] + srcSize[0], srcPos[1] + srcSize[1], rgb[0], rgb[1], rgb[2], alpha,
3782 PRVM_G_FLOAT(OFS_RETURN) = 1;
3789 float drawfill(vector position, vector size, vector rgb, float alpha, float flag)
3792 void VM_drawfill(void)
3794 float *size, *pos, *rgb;
3797 VM_SAFEPARMCOUNT(5,VM_drawfill);
3800 pos = PRVM_G_VECTOR(OFS_PARM0);
3801 size = PRVM_G_VECTOR(OFS_PARM1);
3802 rgb = PRVM_G_VECTOR(OFS_PARM2);
3803 flag = (int) PRVM_G_FLOAT(OFS_PARM4);
3805 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3807 PRVM_G_FLOAT(OFS_RETURN) = -2;
3808 VM_Warning("VM_drawfill: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
3812 if(pos[2] || size[2])
3813 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")));
3815 DrawQ_Fill(pos[0], pos[1], size[0], size[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM3), flag);
3816 PRVM_G_FLOAT(OFS_RETURN) = 1;
3823 drawsetcliparea(float x, float y, float width, float height)
3826 void VM_drawsetcliparea(void)
3829 VM_SAFEPARMCOUNT(4,VM_drawsetcliparea);
3831 x = bound(0, PRVM_G_FLOAT(OFS_PARM0), vid_conwidth.integer);
3832 y = bound(0, PRVM_G_FLOAT(OFS_PARM1), vid_conheight.integer);
3833 w = bound(0, PRVM_G_FLOAT(OFS_PARM2) + PRVM_G_FLOAT(OFS_PARM0) - x, (vid_conwidth.integer - x));
3834 h = bound(0, PRVM_G_FLOAT(OFS_PARM3) + PRVM_G_FLOAT(OFS_PARM1) - y, (vid_conheight.integer - y));
3836 DrawQ_SetClipArea(x, y, w, h);
3841 VM_drawresetcliparea
3846 void VM_drawresetcliparea(void)
3848 VM_SAFEPARMCOUNT(0,VM_drawresetcliparea);
3850 DrawQ_ResetClipArea();
3857 vector getimagesize(string pic)
3860 void VM_getimagesize(void)
3865 VM_SAFEPARMCOUNT(1,VM_getimagesize);
3867 p = PRVM_G_STRING(OFS_PARM0);
3868 VM_CheckEmptyString (p);
3870 pic = Draw_CachePic_Flags (p, CACHEPICFLAG_NOTPERSISTENT);
3872 PRVM_G_VECTOR(OFS_RETURN)[0] = pic->width;
3873 PRVM_G_VECTOR(OFS_RETURN)[1] = pic->height;
3874 PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
3881 string keynumtostring(float keynum)
3884 void VM_keynumtostring (void)
3886 VM_SAFEPARMCOUNT(1, VM_keynumtostring);
3888 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(Key_KeynumToString((int)PRVM_G_FLOAT(OFS_PARM0)));
3893 VM_findkeysforcommand
3895 string findkeysforcommand(string command, float bindmap)
3897 the returned string is an altstring
3900 #define FKFC_NUMKEYS 5
3901 void M_FindKeysForCommand(const char *command, int *keys);
3902 void VM_findkeysforcommand(void)
3905 char ret[VM_STRINGTEMP_LENGTH];
3906 int keys[FKFC_NUMKEYS];
3910 VM_SAFEPARMCOUNTRANGE(1, 2, VM_findkeysforcommand);
3912 cmd = PRVM_G_STRING(OFS_PARM0);
3914 bindmap = bound(-1, PRVM_G_FLOAT(OFS_PARM1), MAX_BINDMAPS-1);
3916 bindmap = 0; // consistent to "bind"
3918 VM_CheckEmptyString(cmd);
3920 Key_FindKeysForCommand(cmd, keys, FKFC_NUMKEYS, bindmap);
3923 for(i = 0; i < FKFC_NUMKEYS; i++)
3924 strlcat(ret, va(" \'%i\'", keys[i]), sizeof(ret));
3926 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(ret);
3933 float stringtokeynum(string key)
3936 void VM_stringtokeynum (void)
3938 VM_SAFEPARMCOUNT( 1, VM_keynumtostring );
3940 PRVM_G_FLOAT(OFS_RETURN) = Key_StringToKeynum(PRVM_G_STRING(OFS_PARM0));
3947 string getkeybind(float key, float bindmap)
3950 void VM_getkeybind (void)
3953 VM_SAFEPARMCOUNTRANGE(1, 2, VM_CL_getkeybind);
3955 bindmap = bound(-1, PRVM_G_FLOAT(OFS_PARM1), MAX_BINDMAPS-1);
3957 bindmap = 0; // consistent to "bind"
3959 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(Key_GetBind((int)PRVM_G_FLOAT(OFS_PARM0), bindmap));
3966 float setkeybind(float key, string cmd, float bindmap)
3969 void VM_setkeybind (void)
3972 VM_SAFEPARMCOUNTRANGE(2, 3, VM_CL_setkeybind);
3974 bindmap = bound(-1, PRVM_G_FLOAT(OFS_PARM2), MAX_BINDMAPS-1);
3976 bindmap = 0; // consistent to "bind"
3978 PRVM_G_FLOAT(OFS_RETURN) = 0;
3979 if(Key_SetBinding((int)PRVM_G_FLOAT(OFS_PARM0), bindmap, PRVM_G_STRING(OFS_PARM1)))
3980 PRVM_G_FLOAT(OFS_RETURN) = 1;
3987 vector getbindmaps()
3990 void VM_getbindmaps (void)
3993 VM_SAFEPARMCOUNT(0, VM_CL_getbindmap);
3994 Key_GetBindMap(&fg, &bg);
3995 PRVM_G_VECTOR(OFS_RETURN)[0] = fg;
3996 PRVM_G_VECTOR(OFS_RETURN)[1] = bg;
3997 PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
4004 float setbindmaps(vector bindmap)
4007 void VM_setbindmaps (void)
4009 VM_SAFEPARMCOUNT(1, VM_CL_setbindmap);
4010 PRVM_G_FLOAT(OFS_RETURN) = 0;
4011 if(PRVM_G_VECTOR(OFS_PARM0)[2] == 0)
4012 if(Key_SetBindMap((int)PRVM_G_VECTOR(OFS_PARM0)[0], (int)PRVM_G_VECTOR(OFS_PARM0)[1]))
4013 PRVM_G_FLOAT(OFS_RETURN) = 1;
4016 // CL_Video interface functions
4019 ========================
4022 float cin_open(string file, string name)
4023 ========================
4025 void VM_cin_open( void )
4030 VM_SAFEPARMCOUNT( 2, VM_cin_open );
4032 file = PRVM_G_STRING( OFS_PARM0 );
4033 name = PRVM_G_STRING( OFS_PARM1 );
4035 VM_CheckEmptyString( file );
4036 VM_CheckEmptyString( name );
4038 if( CL_OpenVideo( file, name, MENUOWNER, "" ) )
4039 PRVM_G_FLOAT( OFS_RETURN ) = 1;
4041 PRVM_G_FLOAT( OFS_RETURN ) = 0;
4045 ========================
4048 void cin_close(string name)
4049 ========================
4051 void VM_cin_close( void )
4055 VM_SAFEPARMCOUNT( 1, VM_cin_close );
4057 name = PRVM_G_STRING( OFS_PARM0 );
4058 VM_CheckEmptyString( name );
4060 CL_CloseVideo( CL_GetVideoByName( name ) );
4064 ========================
4066 void cin_setstate(string name, float type)
4067 ========================
4069 void VM_cin_setstate( void )
4072 clvideostate_t state;
4075 VM_SAFEPARMCOUNT( 2, VM_cin_netstate );
4077 name = PRVM_G_STRING( OFS_PARM0 );
4078 VM_CheckEmptyString( name );
4080 state = (clvideostate_t)((int)PRVM_G_FLOAT( OFS_PARM1 ));
4082 video = CL_GetVideoByName( name );
4083 if( video && state > CLVIDEO_UNUSED && state < CLVIDEO_STATECOUNT )
4084 CL_SetVideoState( video, state );
4088 ========================
4091 float cin_getstate(string name)
4092 ========================
4094 void VM_cin_getstate( void )
4099 VM_SAFEPARMCOUNT( 1, VM_cin_getstate );
4101 name = PRVM_G_STRING( OFS_PARM0 );
4102 VM_CheckEmptyString( name );
4104 video = CL_GetVideoByName( name );
4106 PRVM_G_FLOAT( OFS_RETURN ) = (int)video->state;
4108 PRVM_G_FLOAT( OFS_RETURN ) = 0;
4112 ========================
4115 void cin_restart(string name)
4116 ========================
4118 void VM_cin_restart( void )
4123 VM_SAFEPARMCOUNT( 1, VM_cin_restart );
4125 name = PRVM_G_STRING( OFS_PARM0 );
4126 VM_CheckEmptyString( name );
4128 video = CL_GetVideoByName( name );
4130 CL_RestartVideo( video );
4134 ========================
4136 ========================
4138 void VM_Gecko_Init( void ) {
4139 // the prog struct is memset to 0 by Initprog? [12/6/2007 Black]
4140 // FIXME: remove the other _Init functions then, too? [12/6/2007 Black]
4144 ========================
4146 ========================
4148 void VM_Gecko_Destroy( void ) {
4150 for( i = 0 ; i < PRVM_MAX_GECKOINSTANCES ; i++ ) {
4151 clgecko_t **instance = &prog->opengeckoinstances[ i ];
4153 CL_Gecko_DestroyBrowser( *instance );
4160 ========================
4163 float[bool] gecko_create( string name )
4164 ========================
4166 void VM_gecko_create( void ) {
4169 clgecko_t *instance;
4171 VM_SAFEPARMCOUNT( 1, VM_gecko_create );
4173 name = PRVM_G_STRING( OFS_PARM0 );
4174 VM_CheckEmptyString( name );
4176 // find an empty slot for this gecko browser..
4177 for( i = 0 ; i < PRVM_MAX_GECKOINSTANCES ; i++ ) {
4178 if( prog->opengeckoinstances[ i ] == NULL ) {
4182 if( i == PRVM_MAX_GECKOINSTANCES ) {
4183 VM_Warning("VM_gecko_create: %s ran out of gecko handles (%i)\n", PRVM_NAME, PRVM_MAX_GECKOINSTANCES);
4184 PRVM_G_FLOAT( OFS_RETURN ) = 0;
4188 instance = prog->opengeckoinstances[ i ] = CL_Gecko_CreateBrowser( name, PRVM_GetProgNr() );
4190 // TODO: error handling [12/3/2007 Black]
4191 PRVM_G_FLOAT( OFS_RETURN ) = 0;
4194 PRVM_G_FLOAT( OFS_RETURN ) = 1;
4198 ========================
4201 void gecko_destroy( string name )
4202 ========================
4204 void VM_gecko_destroy( void ) {
4206 clgecko_t *instance;
4208 VM_SAFEPARMCOUNT( 1, VM_gecko_destroy );
4210 name = PRVM_G_STRING( OFS_PARM0 );
4211 VM_CheckEmptyString( name );
4212 instance = CL_Gecko_FindBrowser( name );
4216 CL_Gecko_DestroyBrowser( instance );
4220 ========================
4223 void gecko_navigate( string name, string URI )
4224 ========================
4226 void VM_gecko_navigate( void ) {
4229 clgecko_t *instance;
4231 VM_SAFEPARMCOUNT( 2, VM_gecko_navigate );
4233 name = PRVM_G_STRING( OFS_PARM0 );
4234 URI = PRVM_G_STRING( OFS_PARM1 );
4235 VM_CheckEmptyString( name );
4236 VM_CheckEmptyString( URI );
4238 instance = CL_Gecko_FindBrowser( name );
4242 CL_Gecko_NavigateToURI( instance, URI );
4246 ========================
4249 float[bool] gecko_keyevent( string name, float key, float eventtype )
4250 ========================
4252 void VM_gecko_keyevent( void ) {
4255 clgecko_buttoneventtype_t eventtype;
4256 clgecko_t *instance;
4258 VM_SAFEPARMCOUNT( 3, VM_gecko_keyevent );
4260 name = PRVM_G_STRING( OFS_PARM0 );
4261 VM_CheckEmptyString( name );
4262 key = (unsigned int) PRVM_G_FLOAT( OFS_PARM1 );
4263 switch( (unsigned int) PRVM_G_FLOAT( OFS_PARM2 ) ) {
4265 eventtype = CLG_BET_DOWN;
4268 eventtype = CLG_BET_UP;
4271 eventtype = CLG_BET_PRESS;
4274 eventtype = CLG_BET_DOUBLECLICK;
4277 // TODO: console printf? [12/3/2007 Black]
4278 PRVM_G_FLOAT( OFS_RETURN ) = 0;
4282 instance = CL_Gecko_FindBrowser( name );
4284 PRVM_G_FLOAT( OFS_RETURN ) = 0;
4288 PRVM_G_FLOAT( OFS_RETURN ) = (CL_Gecko_Event_Key( instance, (keynum_t) key, eventtype ) == true);
4292 ========================
4295 void gecko_mousemove( string name, float x, float y )
4296 ========================
4298 void VM_gecko_movemouse( void ) {
4301 clgecko_t *instance;
4303 VM_SAFEPARMCOUNT( 3, VM_gecko_movemouse );
4305 name = PRVM_G_STRING( OFS_PARM0 );
4306 VM_CheckEmptyString( name );
4307 x = PRVM_G_FLOAT( OFS_PARM1 );
4308 y = PRVM_G_FLOAT( OFS_PARM2 );
4310 instance = CL_Gecko_FindBrowser( name );
4314 CL_Gecko_Event_CursorMove( instance, x, y );
4319 ========================
4322 void gecko_resize( string name, float w, float h )
4323 ========================
4325 void VM_gecko_resize( void ) {
4328 clgecko_t *instance;
4330 VM_SAFEPARMCOUNT( 3, VM_gecko_movemouse );
4332 name = PRVM_G_STRING( OFS_PARM0 );
4333 VM_CheckEmptyString( name );
4334 w = PRVM_G_FLOAT( OFS_PARM1 );
4335 h = PRVM_G_FLOAT( OFS_PARM2 );
4337 instance = CL_Gecko_FindBrowser( name );
4341 CL_Gecko_Resize( instance, (int) w, (int) h );
4346 ========================
4347 VM_gecko_get_texture_extent
4349 vector gecko_get_texture_extent( string name )
4350 ========================
4352 void VM_gecko_get_texture_extent( void ) {
4354 clgecko_t *instance;
4356 VM_SAFEPARMCOUNT( 1, VM_gecko_movemouse );
4358 name = PRVM_G_STRING( OFS_PARM0 );
4359 VM_CheckEmptyString( name );
4361 PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
4362 instance = CL_Gecko_FindBrowser( name );
4364 PRVM_G_VECTOR(OFS_RETURN)[0] = 0;
4365 PRVM_G_VECTOR(OFS_RETURN)[1] = 0;
4368 CL_Gecko_GetTextureExtent( instance,
4369 PRVM_G_VECTOR(OFS_RETURN), PRVM_G_VECTOR(OFS_RETURN)+1 );
4378 Writes new values for v_forward, v_up, and v_right based on angles
4379 void makevectors(vector angle)
4382 void VM_makevectors (void)
4384 prvm_eval_t *valforward, *valright, *valup;
4385 valforward = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_forward);
4386 valright = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_right);
4387 valup = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_up);
4388 if (!valforward || !valright || !valup)
4390 VM_Warning("makevectors: could not find v_forward, v_right, or v_up global variables\n");
4393 VM_SAFEPARMCOUNT(1, VM_makevectors);
4394 AngleVectors (PRVM_G_VECTOR(OFS_PARM0), valforward->vector, valright->vector, valup->vector);
4401 Writes new values for v_forward, v_up, and v_right based on the given forward vector
4402 vectorvectors(vector)
4405 void VM_vectorvectors (void)
4407 prvm_eval_t *valforward, *valright, *valup;
4408 valforward = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_forward);
4409 valright = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_right);
4410 valup = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.v_up);
4411 if (!valforward || !valright || !valup)
4413 VM_Warning("vectorvectors: could not find v_forward, v_right, or v_up global variables\n");
4416 VM_SAFEPARMCOUNT(1, VM_vectorvectors);
4417 VectorNormalize2(PRVM_G_VECTOR(OFS_PARM0), valforward->vector);
4418 VectorVectors(valforward->vector, valright->vector, valup->vector);
4422 ========================
4425 void drawline(float width, vector pos1, vector pos2, vector rgb, float alpha, float flags)
4426 ========================
4428 void VM_drawline (void)
4430 float *c1, *c2, *rgb;
4432 unsigned char flags;
4434 VM_SAFEPARMCOUNT(6, VM_drawline);
4435 width = PRVM_G_FLOAT(OFS_PARM0);
4436 c1 = PRVM_G_VECTOR(OFS_PARM1);
4437 c2 = PRVM_G_VECTOR(OFS_PARM2);
4438 rgb = PRVM_G_VECTOR(OFS_PARM3);
4439 alpha = PRVM_G_FLOAT(OFS_PARM4);
4440 flags = (int)PRVM_G_FLOAT(OFS_PARM5);
4441 DrawQ_Line(width, c1[0], c1[1], c2[0], c2[1], rgb[0], rgb[1], rgb[2], alpha, flags);
4444 // float(float number, float quantity) bitshift (EXT_BITSHIFT)
4445 void VM_bitshift (void)
4448 VM_SAFEPARMCOUNT(2, VM_bitshift);
4450 n1 = (int)fabs((float)((int)PRVM_G_FLOAT(OFS_PARM0)));
4451 n2 = (int)PRVM_G_FLOAT(OFS_PARM1);
4453 PRVM_G_FLOAT(OFS_RETURN) = n1;
4456 PRVM_G_FLOAT(OFS_RETURN) = (n1 >> -n2);
4458 PRVM_G_FLOAT(OFS_RETURN) = (n1 << n2);
4461 ////////////////////////////////////////
4462 // AltString functions
4463 ////////////////////////////////////////
4466 ========================
4469 float altstr_count(string)
4470 ========================
4472 void VM_altstr_count( void )
4474 const char *altstr, *pos;
4477 VM_SAFEPARMCOUNT( 1, VM_altstr_count );
4479 altstr = PRVM_G_STRING( OFS_PARM0 );
4480 //VM_CheckEmptyString( altstr );
4482 for( count = 0, pos = altstr ; *pos ; pos++ ) {
4483 if( *pos == '\\' ) {
4487 } else if( *pos == '\'' ) {
4492 PRVM_G_FLOAT( OFS_RETURN ) = (float) (count / 2);
4496 ========================
4499 string altstr_prepare(string)
4500 ========================
4502 void VM_altstr_prepare( void )
4505 const char *instr, *in;
4507 char outstr[VM_STRINGTEMP_LENGTH];
4509 VM_SAFEPARMCOUNT( 1, VM_altstr_prepare );
4511 instr = PRVM_G_STRING( OFS_PARM0 );
4513 for( out = outstr, in = instr, size = sizeof(outstr) - 1 ; size && *in ; size--, in++, out++ )
4522 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
4526 ========================
4529 string altstr_get(string, float)
4530 ========================
4532 void VM_altstr_get( void )
4534 const char *altstr, *pos;
4537 char outstr[VM_STRINGTEMP_LENGTH];
4539 VM_SAFEPARMCOUNT( 2, VM_altstr_get );
4541 altstr = PRVM_G_STRING( OFS_PARM0 );
4543 count = (int)PRVM_G_FLOAT( OFS_PARM1 );
4544 count = count * 2 + 1;
4546 for( pos = altstr ; *pos && count ; pos++ )
4547 if( *pos == '\\' ) {
4550 } else if( *pos == '\'' )
4554 PRVM_G_INT( OFS_RETURN ) = 0;
4558 for( out = outstr, size = sizeof(outstr) - 1 ; size && *pos ; size--, pos++, out++ )
4559 if( *pos == '\\' ) {
4564 } else if( *pos == '\'' )
4570 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
4574 ========================
4577 string altstr_set(string altstr, float num, string set)
4578 ========================
4580 void VM_altstr_set( void )
4583 const char *altstr, *str;
4586 char outstr[VM_STRINGTEMP_LENGTH];
4588 VM_SAFEPARMCOUNT( 3, VM_altstr_set );
4590 altstr = PRVM_G_STRING( OFS_PARM0 );
4592 num = (int)PRVM_G_FLOAT( OFS_PARM1 );
4594 str = PRVM_G_STRING( OFS_PARM2 );
4597 for( num = num * 2 + 1, in = altstr; *in && num; *out++ = *in++ )
4602 } else if( *in == '\'' ) {
4607 for( ; *str; *out++ = *str++ );
4608 // now jump over the old content
4610 if( *in == '\'' || (*in == '\\' && !*++in) )
4613 strlcpy(out, in, outstr + sizeof(outstr) - out);
4614 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
4618 ========================
4621 string altstr_ins(string altstr, float num, string set)
4622 ========================
4624 void VM_altstr_ins(void)
4630 char outstr[VM_STRINGTEMP_LENGTH];
4632 VM_SAFEPARMCOUNT(3, VM_altstr_ins);
4634 in = PRVM_G_STRING( OFS_PARM0 );
4635 num = (int)PRVM_G_FLOAT( OFS_PARM1 );
4636 set = PRVM_G_STRING( OFS_PARM2 );
4639 for( num = num * 2 + 2 ; *in && num > 0 ; *out++ = *in++ )
4644 } else if( *in == '\'' ) {
4649 for( ; *set ; *out++ = *set++ );
4652 strlcpy(out, in, outstr + sizeof(outstr) - out);
4653 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
4657 ////////////////////////////////////////
4658 // BufString functions
4659 ////////////////////////////////////////
4660 //[515]: string buffers support
4662 static size_t stringbuffers_sortlength;
4664 static void BufStr_Expand(prvm_stringbuffer_t *stringbuffer, int strindex)
4666 if (stringbuffer->max_strings <= strindex)
4668 char **oldstrings = stringbuffer->strings;
4669 stringbuffer->max_strings = max(stringbuffer->max_strings * 2, 128);
4670 while (stringbuffer->max_strings <= strindex)
4671 stringbuffer->max_strings *= 2;
4672 stringbuffer->strings = (char **) Mem_Alloc(prog->progs_mempool, stringbuffer->max_strings * sizeof(stringbuffer->strings[0]));
4673 if (stringbuffer->num_strings > 0)
4674 memcpy(stringbuffer->strings, oldstrings, stringbuffer->num_strings * sizeof(stringbuffer->strings[0]));
4676 Mem_Free(oldstrings);
4680 static void BufStr_Shrink(prvm_stringbuffer_t *stringbuffer)
4682 // reduce num_strings if there are empty string slots at the end
4683 while (stringbuffer->num_strings > 0 && stringbuffer->strings[stringbuffer->num_strings - 1] == NULL)
4684 stringbuffer->num_strings--;
4686 // if empty, free the string pointer array
4687 if (stringbuffer->num_strings == 0)
4689 stringbuffer->max_strings = 0;
4690 if (stringbuffer->strings)
4691 Mem_Free(stringbuffer->strings);
4692 stringbuffer->strings = NULL;
4696 static int BufStr_SortStringsUP (const void *in1, const void *in2)
4699 a = *((const char **) in1);
4700 b = *((const char **) in2);
4701 if(!a || !a[0]) return 1;
4702 if(!b || !b[0]) return -1;
4703 return strncmp(a, b, stringbuffers_sortlength);
4706 static int BufStr_SortStringsDOWN (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(b, a, stringbuffers_sortlength);
4717 ========================
4719 creates new buffer, and returns it's index, returns -1 if failed
4720 float buf_create(void) = #460;
4721 float newbuf(string format, float flags) = #460;
4722 ========================
4725 void VM_buf_create (void)
4727 prvm_stringbuffer_t *stringbuffer;
4730 VM_SAFEPARMCOUNTRANGE(0, 2, VM_buf_create);
4732 // VorteX: optional parm1 (buffer format) is unfinished, to keep intact with future databuffers extension must be set to "string"
4733 if(prog->argc >= 1 && strcmp(PRVM_G_STRING(OFS_PARM0), "string"))
4735 PRVM_G_FLOAT(OFS_RETURN) = -1;
4738 stringbuffer = (prvm_stringbuffer_t *) Mem_ExpandableArray_AllocRecord(&prog->stringbuffersarray);
4739 for (i = 0;stringbuffer != Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, i);i++);
4740 stringbuffer->origin = PRVM_AllocationOrigin();
4741 // optional flags parm
4743 stringbuffer->flags = (int)PRVM_G_FLOAT(OFS_PARM1) & 0xFF;
4744 PRVM_G_FLOAT(OFS_RETURN) = i;
4750 ========================
4752 deletes buffer and all strings in it
4753 void buf_del(float bufhandle) = #461;
4754 ========================
4756 void VM_buf_del (void)
4758 prvm_stringbuffer_t *stringbuffer;
4759 VM_SAFEPARMCOUNT(1, VM_buf_del);
4760 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4764 for (i = 0;i < stringbuffer->num_strings;i++)
4765 if (stringbuffer->strings[i])
4766 Mem_Free(stringbuffer->strings[i]);
4767 if (stringbuffer->strings)
4768 Mem_Free(stringbuffer->strings);
4769 if(stringbuffer->origin)
4770 PRVM_Free((char *)stringbuffer->origin);
4771 Mem_ExpandableArray_FreeRecord(&prog->stringbuffersarray, stringbuffer);
4775 VM_Warning("VM_buf_del: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4781 ========================
4783 how many strings are stored in buffer
4784 float buf_getsize(float bufhandle) = #462;
4785 ========================
4787 void VM_buf_getsize (void)
4789 prvm_stringbuffer_t *stringbuffer;
4790 VM_SAFEPARMCOUNT(1, VM_buf_getsize);
4792 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4795 PRVM_G_FLOAT(OFS_RETURN) = -1;
4796 VM_Warning("VM_buf_getsize: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4800 PRVM_G_FLOAT(OFS_RETURN) = stringbuffer->num_strings;
4804 ========================
4806 copy all content from one buffer to another, make sure it exists
4807 void buf_copy(float bufhandle_from, float bufhandle_to) = #463;
4808 ========================
4810 void VM_buf_copy (void)
4812 prvm_stringbuffer_t *srcstringbuffer, *dststringbuffer;
4814 VM_SAFEPARMCOUNT(2, VM_buf_copy);
4816 srcstringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4817 if(!srcstringbuffer)
4819 VM_Warning("VM_buf_copy: invalid source buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4822 i = (int)PRVM_G_FLOAT(OFS_PARM1);
4823 if(i == (int)PRVM_G_FLOAT(OFS_PARM0))
4825 VM_Warning("VM_buf_copy: source == destination (%i) in %s\n", i, PRVM_NAME);
4828 dststringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4829 if(!dststringbuffer)
4831 VM_Warning("VM_buf_copy: invalid destination buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM1), PRVM_NAME);
4835 for (i = 0;i < dststringbuffer->num_strings;i++)
4836 if (dststringbuffer->strings[i])
4837 Mem_Free(dststringbuffer->strings[i]);
4838 if (dststringbuffer->strings)
4839 Mem_Free(dststringbuffer->strings);
4840 *dststringbuffer = *srcstringbuffer;
4841 if (dststringbuffer->max_strings)
4842 dststringbuffer->strings = (char **)Mem_Alloc(prog->progs_mempool, sizeof(dststringbuffer->strings[0]) * dststringbuffer->max_strings);
4844 for (i = 0;i < dststringbuffer->num_strings;i++)
4846 if (srcstringbuffer->strings[i])
4849 stringlen = strlen(srcstringbuffer->strings[i]) + 1;
4850 dststringbuffer->strings[i] = (char *)Mem_Alloc(prog->progs_mempool, stringlen);
4851 memcpy(dststringbuffer->strings[i], srcstringbuffer->strings[i], stringlen);
4857 ========================
4859 sort buffer by beginnings of strings (cmplength defaults it's length)
4860 "backward == TRUE" means that sorting goes upside-down
4861 void buf_sort(float bufhandle, float cmplength, float backward) = #464;
4862 ========================
4864 void VM_buf_sort (void)
4866 prvm_stringbuffer_t *stringbuffer;
4867 VM_SAFEPARMCOUNT(3, VM_buf_sort);
4869 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4872 VM_Warning("VM_buf_sort: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4875 if(stringbuffer->num_strings <= 0)
4877 VM_Warning("VM_buf_sort: tried to sort empty buffer %i in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4880 stringbuffers_sortlength = (int)PRVM_G_FLOAT(OFS_PARM1);
4881 if(stringbuffers_sortlength <= 0)
4882 stringbuffers_sortlength = 0x7FFFFFFF;
4884 if(!PRVM_G_FLOAT(OFS_PARM2))
4885 qsort(stringbuffer->strings, stringbuffer->num_strings, sizeof(char*), BufStr_SortStringsUP);
4887 qsort(stringbuffer->strings, stringbuffer->num_strings, sizeof(char*), BufStr_SortStringsDOWN);
4889 BufStr_Shrink(stringbuffer);
4893 ========================
4895 concantenates all buffer string into one with "glue" separator and returns it as tempstring
4896 string buf_implode(float bufhandle, string glue) = #465;
4897 ========================
4899 void VM_buf_implode (void)
4901 prvm_stringbuffer_t *stringbuffer;
4902 char k[VM_STRINGTEMP_LENGTH];
4906 VM_SAFEPARMCOUNT(2, VM_buf_implode);
4908 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4909 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
4912 VM_Warning("VM_buf_implode: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4915 if(!stringbuffer->num_strings)
4917 sep = PRVM_G_STRING(OFS_PARM1);
4919 for(l = i = 0;i < stringbuffer->num_strings;i++)
4921 if(stringbuffer->strings[i])
4923 l += (i > 0 ? strlen(sep) : 0) + strlen(stringbuffer->strings[i]);
4924 if (l >= sizeof(k) - 1)
4926 strlcat(k, sep, sizeof(k));
4927 strlcat(k, stringbuffer->strings[i], sizeof(k));
4930 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(k);
4934 ========================
4936 get a string from buffer, returns tempstring, dont str_unzone it!
4937 string bufstr_get(float bufhandle, float string_index) = #465;
4938 ========================
4940 void VM_bufstr_get (void)
4942 prvm_stringbuffer_t *stringbuffer;
4944 VM_SAFEPARMCOUNT(2, VM_bufstr_get);
4946 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
4947 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4950 VM_Warning("VM_bufstr_get: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4953 strindex = (int)PRVM_G_FLOAT(OFS_PARM1);
4956 VM_Warning("VM_bufstr_get: invalid string index %i used in %s\n", strindex, PRVM_NAME);
4959 if (strindex < stringbuffer->num_strings && stringbuffer->strings[strindex])
4960 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(stringbuffer->strings[strindex]);
4964 ========================
4966 copies a string into selected slot of buffer
4967 void bufstr_set(float bufhandle, float string_index, string str) = #466;
4968 ========================
4970 void VM_bufstr_set (void)
4974 prvm_stringbuffer_t *stringbuffer;
4977 VM_SAFEPARMCOUNT(3, VM_bufstr_set);
4979 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4982 VM_Warning("VM_bufstr_set: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4985 strindex = (int)PRVM_G_FLOAT(OFS_PARM1);
4986 if(strindex < 0 || strindex >= 1000000) // huge number of strings
4988 VM_Warning("VM_bufstr_set: invalid string index %i used in %s\n", strindex, PRVM_NAME);
4992 BufStr_Expand(stringbuffer, strindex);
4993 stringbuffer->num_strings = max(stringbuffer->num_strings, strindex + 1);
4995 if(stringbuffer->strings[strindex])
4996 Mem_Free(stringbuffer->strings[strindex]);
4997 stringbuffer->strings[strindex] = NULL;
4999 if(PRVM_G_INT(OFS_PARM2))
5001 // not the NULL string!
5002 news = PRVM_G_STRING(OFS_PARM2);
5003 alloclen = strlen(news) + 1;
5004 stringbuffer->strings[strindex] = (char *)Mem_Alloc(prog->progs_mempool, alloclen);
5005 memcpy(stringbuffer->strings[strindex], news, alloclen);
5008 BufStr_Shrink(stringbuffer);
5012 ========================
5014 adds string to buffer in first free slot and returns its index
5015 "order == TRUE" means that string will be added after last "full" slot
5016 float bufstr_add(float bufhandle, string str, float order) = #467;
5017 ========================
5019 void VM_bufstr_add (void)
5021 int order, strindex;
5022 prvm_stringbuffer_t *stringbuffer;
5026 VM_SAFEPARMCOUNT(3, VM_bufstr_add);
5028 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
5029 PRVM_G_FLOAT(OFS_RETURN) = -1;
5032 VM_Warning("VM_bufstr_add: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
5035 if(!PRVM_G_INT(OFS_PARM1)) // NULL string
5037 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);
5040 string = PRVM_G_STRING(OFS_PARM1);
5041 order = (int)PRVM_G_FLOAT(OFS_PARM2);
5043 strindex = stringbuffer->num_strings;
5045 for (strindex = 0;strindex < stringbuffer->num_strings;strindex++)
5046 if (stringbuffer->strings[strindex] == NULL)
5049 BufStr_Expand(stringbuffer, strindex);
5051 stringbuffer->num_strings = max(stringbuffer->num_strings, strindex + 1);
5052 alloclen = strlen(string) + 1;
5053 stringbuffer->strings[strindex] = (char *)Mem_Alloc(prog->progs_mempool, alloclen);
5054 memcpy(stringbuffer->strings[strindex], string, alloclen);
5056 PRVM_G_FLOAT(OFS_RETURN) = strindex;
5060 ========================
5062 delete string from buffer
5063 void bufstr_free(float bufhandle, float string_index) = #468;
5064 ========================
5066 void VM_bufstr_free (void)
5069 prvm_stringbuffer_t *stringbuffer;
5070 VM_SAFEPARMCOUNT(2, VM_bufstr_free);
5072 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
5075 VM_Warning("VM_bufstr_free: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
5078 i = (int)PRVM_G_FLOAT(OFS_PARM1);
5081 VM_Warning("VM_bufstr_free: invalid string index %i used in %s\n", i, PRVM_NAME);
5085 if (i < stringbuffer->num_strings)
5087 if(stringbuffer->strings[i])
5088 Mem_Free(stringbuffer->strings[i]);
5089 stringbuffer->strings[i] = NULL;
5092 BufStr_Shrink(stringbuffer);
5101 void VM_buf_cvarlist(void)
5104 const char *partial, *antipartial;
5105 size_t len, antilen;
5107 qboolean ispattern, antiispattern;
5109 prvm_stringbuffer_t *stringbuffer;
5110 VM_SAFEPARMCOUNTRANGE(2, 3, VM_buf_cvarlist);
5112 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
5115 VM_Warning("VM_bufstr_free: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
5119 partial = PRVM_G_STRING(OFS_PARM1);
5123 len = strlen(partial);
5126 antipartial = PRVM_G_STRING(OFS_PARM2);
5132 antilen = strlen(antipartial);
5134 for (n = 0;n < stringbuffer->num_strings;n++)
5135 if (stringbuffer->strings[n])
5136 Mem_Free(stringbuffer->strings[n]);
5137 if (stringbuffer->strings)
5138 Mem_Free(stringbuffer->strings);
5139 stringbuffer->strings = NULL;
5141 ispattern = partial && (strchr(partial, '*') || strchr(partial, '?'));
5142 antiispattern = antipartial && (strchr(antipartial, '*') || strchr(antipartial, '?'));
5145 for(cvar = cvar_vars; cvar; cvar = cvar->next)
5147 if(len && (ispattern ? !matchpattern_with_separator(cvar->name, partial, false, "", false) : strncmp(partial, cvar->name, len)))
5150 if(antilen && (antiispattern ? matchpattern_with_separator(cvar->name, antipartial, false, "", false) : !strncmp(antipartial, cvar->name, antilen)))
5156 stringbuffer->max_strings = stringbuffer->num_strings = n;
5157 if (stringbuffer->max_strings)
5158 stringbuffer->strings = (char **)Mem_Alloc(prog->progs_mempool, sizeof(stringbuffer->strings[0]) * stringbuffer->max_strings);
5161 for(cvar = cvar_vars; cvar; cvar = cvar->next)
5163 if(len && (ispattern ? !matchpattern_with_separator(cvar->name, partial, false, "", false) : strncmp(partial, cvar->name, len)))
5166 if(antilen && (antiispattern ? matchpattern_with_separator(cvar->name, antipartial, false, "", false) : !strncmp(antipartial, cvar->name, antilen)))
5169 alloclen = strlen(cvar->name) + 1;
5170 stringbuffer->strings[n] = (char *)Mem_Alloc(prog->progs_mempool, alloclen);
5171 memcpy(stringbuffer->strings[n], cvar->name, alloclen);
5186 This was a major timewaster in progs, so it was converted to C
5189 void VM_changeyaw (void)
5192 float ideal, current, move, speed;
5194 // this is called (VERY HACKISHLY) by SV_MoveToGoal, so it can not use any
5195 // parameters because they are the parameters to SV_MoveToGoal, not this
5196 //VM_SAFEPARMCOUNT(0, VM_changeyaw);
5198 ent = PRVM_PROG_TO_EDICT(PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict);
5199 if (ent == prog->edicts)
5201 VM_Warning("changeyaw: can not modify world entity\n");
5204 if (ent->priv.server->free)
5206 VM_Warning("changeyaw: can not modify free entity\n");
5209 if (prog->fieldoffsets.angles < 0 || prog->fieldoffsets.ideal_yaw < 0 || prog->fieldoffsets.yaw_speed < 0)
5211 VM_Warning("changeyaw: angles, ideal_yaw, or yaw_speed field(s) not found\n");
5214 current = ANGLEMOD(PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.angles)->vector[1]);
5215 ideal = PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.ideal_yaw)->_float;
5216 speed = PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.yaw_speed)->_float;
5218 if (current == ideal)
5220 move = ideal - current;
5221 if (ideal > current)
5242 PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.angles)->vector[1] = ANGLEMOD (current + move);
5250 void VM_changepitch (void)
5253 float ideal, current, move, speed;
5255 VM_SAFEPARMCOUNT(1, VM_changepitch);
5257 ent = PRVM_G_EDICT(OFS_PARM0);
5258 if (ent == prog->edicts)
5260 VM_Warning("changepitch: can not modify world entity\n");
5263 if (ent->priv.server->free)
5265 VM_Warning("changepitch: can not modify free entity\n");
5268 if (prog->fieldoffsets.angles < 0 || prog->fieldoffsets.idealpitch < 0 || prog->fieldoffsets.pitch_speed < 0)
5270 VM_Warning("changepitch: angles, idealpitch, or pitch_speed field(s) not found\n");
5273 current = ANGLEMOD(PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.angles)->vector[0]);
5274 ideal = PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.idealpitch)->_float;
5275 speed = PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.pitch_speed)->_float;
5277 if (current == ideal)
5279 move = ideal - current;
5280 if (ideal > current)
5301 PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.angles)->vector[0] = ANGLEMOD (current + move);
5305 void VM_uncolorstring (void)
5307 char szNewString[VM_STRINGTEMP_LENGTH];
5308 const char *szString;
5311 VM_SAFEPARMCOUNT(1, VM_uncolorstring);
5312 szString = PRVM_G_STRING(OFS_PARM0);
5313 COM_StringDecolorize(szString, 0, szNewString, sizeof(szNewString), TRUE);
5314 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(szNewString);
5318 // #221 float(string str, string sub[, float startpos]) strstrofs (FTE_STRINGS)
5319 //strstr, without generating a new string. Use in conjunction with FRIK_FILE's substring for more similar strstr.
5320 void VM_strstrofs (void)
5322 const char *instr, *match;
5324 VM_SAFEPARMCOUNTRANGE(2, 3, VM_strstrofs);
5325 instr = PRVM_G_STRING(OFS_PARM0);
5326 match = PRVM_G_STRING(OFS_PARM1);
5327 firstofs = (prog->argc > 2)?(int)PRVM_G_FLOAT(OFS_PARM2):0;
5328 firstofs = u8_bytelen(instr, firstofs);
5330 if (firstofs && (firstofs < 0 || firstofs > (int)strlen(instr)))
5332 PRVM_G_FLOAT(OFS_RETURN) = -1;
5336 match = strstr(instr+firstofs, match);
5338 PRVM_G_FLOAT(OFS_RETURN) = -1;
5340 PRVM_G_FLOAT(OFS_RETURN) = u8_strnlen(instr, match-instr);
5343 //#222 string(string s, float index) str2chr (FTE_STRINGS)
5344 void VM_str2chr (void)
5349 VM_SAFEPARMCOUNT(2, VM_str2chr);
5350 s = PRVM_G_STRING(OFS_PARM0);
5351 index = u8_bytelen(s, (int)PRVM_G_FLOAT(OFS_PARM1));
5353 if((unsigned)index < strlen(s))
5355 if (utf8_enable.integer)
5356 ch = u8_getchar_noendptr(s + index);
5358 ch = (unsigned char)s[index];
5359 PRVM_G_FLOAT(OFS_RETURN) = ch;
5362 PRVM_G_FLOAT(OFS_RETURN) = 0;
5365 //#223 string(float c, ...) chr2str (FTE_STRINGS)
5366 void VM_chr2str (void)
5371 VM_SAFEPARMCOUNTRANGE(0, 8, VM_chr2str);
5372 for(i = 0;i < prog->argc && i < (int)sizeof(t) - 1;i++)
5373 t[i] = (unsigned char)PRVM_G_FLOAT(OFS_PARM0+i*3);
5375 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(t);
5380 VM_SAFEPARMCOUNTRANGE(0, 8, VM_chr2str);
5381 for(i = 0; i < prog->argc && len < sizeof(t)-1; ++i)
5382 len += u8_fromchar((Uchar)PRVM_G_FLOAT(OFS_PARM0+i*3), t + len, sizeof(t)-1);
5384 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(t);
5387 static int chrconv_number(int i, int base, int conv)
5412 static int chrconv_punct(int i, int base, int conv)
5430 static int chrchar_alpha(int i, int basec, int baset, int convc, int convt, int charnum)
5432 //convert case and colour seperatly...
5449 baset = 128*((charnum&1) == (convt-5));
5465 return i + basec + baset;
5467 // #224 string(float ccase, float calpha, float cnum, string s, ...) strconv (FTE_STRINGS)
5468 //bulk convert a string. change case or colouring.
5469 void VM_strconv (void)
5471 int ccase, redalpha, rednum, len, i;
5472 unsigned char resbuf[VM_STRINGTEMP_LENGTH];
5473 unsigned char *result = resbuf;
5475 VM_SAFEPARMCOUNTRANGE(3, 8, VM_strconv);
5477 ccase = (int) PRVM_G_FLOAT(OFS_PARM0); //0 same, 1 lower, 2 upper
5478 redalpha = (int) PRVM_G_FLOAT(OFS_PARM1); //0 same, 1 white, 2 red, 5 alternate, 6 alternate-alternate
5479 rednum = (int) PRVM_G_FLOAT(OFS_PARM2); //0 same, 1 white, 2 red, 3 redspecial, 4 whitespecial, 5 alternate, 6 alternate-alternate
5480 VM_VarString(3, (char *) resbuf, sizeof(resbuf));
5481 len = strlen((char *) resbuf);
5483 for (i = 0; i < len; i++, result++) //should this be done backwards?
5485 if (*result >= '0' && *result <= '9') //normal numbers...
5486 *result = chrconv_number(*result, '0', rednum);
5487 else if (*result >= '0'+128 && *result <= '9'+128)
5488 *result = chrconv_number(*result, '0'+128, rednum);
5489 else if (*result >= '0'+128-30 && *result <= '9'+128-30)
5490 *result = chrconv_number(*result, '0'+128-30, rednum);
5491 else if (*result >= '0'-30 && *result <= '9'-30)
5492 *result = chrconv_number(*result, '0'-30, rednum);
5494 else if (*result >= 'a' && *result <= 'z') //normal numbers...
5495 *result = chrchar_alpha(*result, 'a', 0, ccase, redalpha, i);
5496 else if (*result >= 'A' && *result <= 'Z') //normal numbers...
5497 *result = chrchar_alpha(*result, 'A', 0, ccase, redalpha, i);
5498 else if (*result >= 'a'+128 && *result <= 'z'+128) //normal numbers...
5499 *result = chrchar_alpha(*result, 'a', 128, ccase, redalpha, i);
5500 else if (*result >= 'A'+128 && *result <= 'Z'+128) //normal numbers...
5501 *result = chrchar_alpha(*result, 'A', 128, ccase, redalpha, i);
5503 else if ((*result & 127) < 16 || !redalpha) //special chars..
5505 else if (*result < 128)
5506 *result = chrconv_punct(*result, 0, redalpha);
5508 *result = chrconv_punct(*result, 128, redalpha);
5512 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString((char *) resbuf);
5515 // #225 string(float chars, string s, ...) strpad (FTE_STRINGS)
5516 void VM_strpad (void)
5518 char src[VM_STRINGTEMP_LENGTH];
5519 char destbuf[VM_STRINGTEMP_LENGTH];
5521 VM_SAFEPARMCOUNTRANGE(1, 8, VM_strpad);
5522 pad = (int) PRVM_G_FLOAT(OFS_PARM0);
5523 VM_VarString(1, src, sizeof(src));
5525 // note: < 0 = left padding, > 0 = right padding,
5526 // this is reverse logic of printf!
5527 dpsnprintf(destbuf, sizeof(destbuf), "%*s", -pad, src);
5529 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(destbuf);
5532 // #226 string(string info, string key, string value, ...) infoadd (FTE_STRINGS)
5533 //uses qw style \key\value strings
5534 void VM_infoadd (void)
5536 const char *info, *key;
5537 char value[VM_STRINGTEMP_LENGTH];
5538 char temp[VM_STRINGTEMP_LENGTH];
5540 VM_SAFEPARMCOUNTRANGE(2, 8, VM_infoadd);
5541 info = PRVM_G_STRING(OFS_PARM0);
5542 key = PRVM_G_STRING(OFS_PARM1);
5543 VM_VarString(2, value, sizeof(value));
5545 strlcpy(temp, info, VM_STRINGTEMP_LENGTH);
5547 InfoString_SetValue(temp, VM_STRINGTEMP_LENGTH, key, value);
5549 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(temp);
5552 // #227 string(string info, string key) infoget (FTE_STRINGS)
5553 //uses qw style \key\value strings
5554 void VM_infoget (void)
5558 char value[VM_STRINGTEMP_LENGTH];
5560 VM_SAFEPARMCOUNT(2, VM_infoget);
5561 info = PRVM_G_STRING(OFS_PARM0);
5562 key = PRVM_G_STRING(OFS_PARM1);
5564 InfoString_GetValue(info, key, value, VM_STRINGTEMP_LENGTH);
5566 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(value);
5569 //#228 float(string s1, string s2, float len) strncmp (FTE_STRINGS)
5570 // also float(string s1, string s2) strcmp (FRIK_FILE)
5571 void VM_strncmp (void)
5573 const char *s1, *s2;
5574 VM_SAFEPARMCOUNTRANGE(2, 3, VM_strncmp);
5575 s1 = PRVM_G_STRING(OFS_PARM0);
5576 s2 = PRVM_G_STRING(OFS_PARM1);
5579 PRVM_G_FLOAT(OFS_RETURN) = strncmp(s1, s2, (size_t)PRVM_G_FLOAT(OFS_PARM2));
5583 PRVM_G_FLOAT(OFS_RETURN) = strcmp(s1, s2);
5587 // #229 float(string s1, string s2) strcasecmp (FTE_STRINGS)
5588 // #230 float(string s1, string s2, float len) strncasecmp (FTE_STRINGS)
5589 void VM_strncasecmp (void)
5591 const char *s1, *s2;
5592 VM_SAFEPARMCOUNTRANGE(2, 3, VM_strncasecmp);
5593 s1 = PRVM_G_STRING(OFS_PARM0);
5594 s2 = PRVM_G_STRING(OFS_PARM1);
5597 PRVM_G_FLOAT(OFS_RETURN) = strncasecmp(s1, s2, (size_t)PRVM_G_FLOAT(OFS_PARM2));
5601 PRVM_G_FLOAT(OFS_RETURN) = strcasecmp(s1, s2);
5605 // #494 float(float caseinsensitive, string s, ...) crc16
5609 static char s[VM_STRINGTEMP_LENGTH];
5610 VM_SAFEPARMCOUNTRANGE(2, 8, VM_hash);
5611 insensitive = PRVM_G_FLOAT(OFS_PARM0);
5612 VM_VarString(1, s, sizeof(s));
5613 PRVM_G_FLOAT(OFS_RETURN) = (unsigned short) ((insensitive ? CRC_Block_CaseInsensitive : CRC_Block) ((unsigned char *) s, strlen(s)));
5616 void VM_wasfreed (void)
5618 VM_SAFEPARMCOUNT(1, VM_wasfreed);
5619 PRVM_G_FLOAT(OFS_RETURN) = PRVM_G_EDICT(OFS_PARM0)->priv.required->free;
5622 void VM_SetTraceGlobals(const trace_t *trace)
5625 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_allsolid)))
5626 val->_float = trace->allsolid;
5627 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_startsolid)))
5628 val->_float = trace->startsolid;
5629 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_fraction)))
5630 val->_float = trace->fraction;
5631 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_inwater)))
5632 val->_float = trace->inwater;
5633 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_inopen)))
5634 val->_float = trace->inopen;
5635 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_endpos)))
5636 VectorCopy(trace->endpos, val->vector);
5637 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_plane_normal)))
5638 VectorCopy(trace->plane.normal, val->vector);
5639 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_plane_dist)))
5640 val->_float = trace->plane.dist;
5641 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_ent)))
5642 val->edict = PRVM_EDICT_TO_PROG(trace->ent ? trace->ent : prog->edicts);
5643 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dpstartcontents)))
5644 val->_float = trace->startsupercontents;
5645 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dphitcontents)))
5646 val->_float = trace->hitsupercontents;
5647 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dphitq3surfaceflags)))
5648 val->_float = trace->hitq3surfaceflags;
5649 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dphittexturename)))
5650 val->string = trace->hittexture ? PRVM_SetTempString(trace->hittexture->name) : 0;
5653 void VM_ClearTraceGlobals(void)
5655 // clean up all trace globals when leaving the VM (anti-triggerbot safeguard)
5657 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_allsolid)))
5659 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_startsolid)))
5661 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_fraction)))
5663 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_inwater)))
5665 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_inopen)))
5667 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_endpos)))
5668 VectorClear(val->vector);
5669 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_plane_normal)))
5670 VectorClear(val->vector);
5671 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_plane_dist)))
5673 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_ent)))
5674 val->edict = PRVM_EDICT_TO_PROG(prog->edicts);
5675 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dpstartcontents)))
5677 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dphitcontents)))
5679 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dphitq3surfaceflags)))
5681 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.trace_dphittexturename)))
5687 void VM_Cmd_Init(void)
5689 // only init the stuff for the current prog
5693 // VM_BufStr_Init();
5696 void VM_Cmd_Reset(void)
5698 CL_PurgeOwner( MENUOWNER );
5700 VM_Files_CloseAll();
5702 // VM_BufStr_ShutDown();
5705 // #510 string(string input, ...) uri_escape (DP_QC_URI_ESCAPE)
5706 // does URI escaping on a string (replace evil stuff by %AB escapes)
5707 void VM_uri_escape (void)
5709 char src[VM_STRINGTEMP_LENGTH];
5710 char dest[VM_STRINGTEMP_LENGTH];
5712 static const char *hex = "0123456789ABCDEF";
5714 VM_SAFEPARMCOUNTRANGE(1, 8, VM_uri_escape);
5715 VM_VarString(0, src, sizeof(src));
5717 for(p = src, q = dest; *p && q < dest + sizeof(dest) - 3; ++p)
5719 if((*p >= 'A' && *p <= 'Z')
5720 || (*p >= 'a' && *p <= 'z')
5721 || (*p >= '0' && *p <= '9')
5722 || (*p == '-') || (*p == '_') || (*p == '.')
5723 || (*p == '!') || (*p == '~')
5724 || (*p == '\'') || (*p == '(') || (*p == ')'))
5729 *q++ = hex[(*(unsigned char *)p >> 4) & 0xF];
5730 *q++ = hex[ *(unsigned char *)p & 0xF];
5735 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(dest);
5738 // #510 string(string input, ...) uri_unescape (DP_QC_URI_ESCAPE)
5739 // does URI unescaping on a string (get back the evil stuff)
5740 void VM_uri_unescape (void)
5742 char src[VM_STRINGTEMP_LENGTH];
5743 char dest[VM_STRINGTEMP_LENGTH];
5747 VM_SAFEPARMCOUNTRANGE(1, 8, VM_uri_unescape);
5748 VM_VarString(0, src, sizeof(src));
5750 for(p = src, q = dest; *p; ) // no need to check size, because unescape can't expand
5754 if(p[1] >= '0' && p[1] <= '9')
5756 else if(p[1] >= 'a' && p[1] <= 'f')
5757 hi = p[1] - 'a' + 10;
5758 else if(p[1] >= 'A' && p[1] <= 'F')
5759 hi = p[1] - 'A' + 10;
5762 if(p[2] >= '0' && p[2] <= '9')
5764 else if(p[2] >= 'a' && p[2] <= 'f')
5765 lo = p[2] - 'a' + 10;
5766 else if(p[2] >= 'A' && p[2] <= 'F')
5767 lo = p[2] - 'A' + 10;
5770 if(hi != 0 || lo != 0) // don't unescape NUL bytes
5771 *q++ = (char) (hi * 0x10 + lo);
5782 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(dest);
5785 // #502 string(string filename) whichpack (DP_QC_WHICHPACK)
5786 // returns the name of the pack containing a file, or "" if it is not in any pack (but local or non-existant)
5787 void VM_whichpack (void)
5789 const char *fn, *pack;
5791 VM_SAFEPARMCOUNT(1, VM_whichpack);
5792 fn = PRVM_G_STRING(OFS_PARM0);
5793 pack = FS_WhichPack(fn);
5795 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(pack ? pack : "");
5803 char buffer[MAX_INPUTLINE];
5804 unsigned char *postdata; // free when uri_to_prog_t is freed
5806 char *sigdata; // free when uri_to_prog_t is freed
5811 static void uri_to_string_callback(int status, size_t length_received, unsigned char *buffer, void *cbdata)
5813 uri_to_prog_t *handle = (uri_to_prog_t *) cbdata;
5815 if(!PRVM_ProgLoaded(handle->prognr))
5817 // curl reply came too late... so just drop it
5818 if(handle->postdata)
5819 Z_Free(handle->postdata);
5821 Z_Free(handle->sigdata);
5826 PRVM_SetProg(handle->prognr);
5828 if((prog->starttime == handle->starttime) && (prog->funcoffsets.URI_Get_Callback))
5830 if(length_received >= sizeof(handle->buffer))
5831 length_received = sizeof(handle->buffer) - 1;
5832 handle->buffer[length_received] = 0;
5834 PRVM_G_FLOAT(OFS_PARM0) = handle->id;
5835 PRVM_G_FLOAT(OFS_PARM1) = status;
5836 PRVM_G_INT(OFS_PARM2) = PRVM_SetTempString(handle->buffer);
5837 PRVM_ExecuteProgram(prog->funcoffsets.URI_Get_Callback, "QC function URI_Get_Callback is missing");
5841 if(handle->postdata)
5842 Z_Free(handle->postdata);
5844 Z_Free(handle->sigdata);
5848 // 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
5849 // 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
5850 void VM_uri_get (void)
5855 uri_to_prog_t *handle;
5856 const char *posttype = NULL;
5857 const char *postseparator = NULL;
5858 int poststringbuffer = -1;
5861 if(!prog->funcoffsets.URI_Get_Callback)
5862 PRVM_ERROR("uri_get called by %s without URI_Get_Callback defined", PRVM_NAME);
5864 VM_SAFEPARMCOUNTRANGE(2, 6, VM_uri_get);
5866 url = PRVM_G_STRING(OFS_PARM0);
5867 id = PRVM_G_FLOAT(OFS_PARM1);
5869 posttype = PRVM_G_STRING(OFS_PARM2);
5871 postseparator = PRVM_G_STRING(OFS_PARM3);
5873 poststringbuffer = PRVM_G_FLOAT(OFS_PARM4);
5875 postkeyid = PRVM_G_FLOAT(OFS_PARM5);
5876 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!
5878 handle->prognr = PRVM_GetProgNr();
5879 handle->starttime = prog->starttime;
5883 size_t l = strlen(postseparator);
5884 if(poststringbuffer >= 0)
5889 prvm_stringbuffer_t *stringbuffer;
5890 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, poststringbuffer);
5893 VM_Warning("uri_get: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
5897 for(i = 0;i < stringbuffer->num_strings;i++)
5901 if(stringbuffer->strings[i])
5902 ltotal += strlen(stringbuffer->strings[i]);
5904 handle->postdata = (unsigned char *)Z_Malloc(ltotal);
5905 handle->postlen = ltotal;
5907 for(i = 0;i < stringbuffer->num_strings;i++)
5911 memcpy(handle->postdata + ltotal, postseparator, l);
5914 if(stringbuffer->strings[i])
5916 memcpy(handle->postdata + ltotal, stringbuffer->strings[i], strlen(stringbuffer->strings[i]));
5917 ltotal += strlen(stringbuffer->strings[i]);
5920 if(ltotal != handle->postlen)
5921 PRVM_ERROR ("%s: string buffer content size mismatch, possible overrun", PRVM_NAME);
5925 handle->postdata = (unsigned char *)Z_Malloc(l);
5926 handle->postlen = l;
5927 memcpy(handle->postdata, postseparator, l);
5932 handle->sigdata = (char *)Z_Malloc(8192);
5933 strlcpy(handle->sigdata, "X-D0-Blind-ID-Detached-Signature: ", 8192);
5934 l = strlen(handle->sigdata);
5935 handle->siglen = Crypto_SignDataDetached(handle->postdata, handle->postlen, postkeyid, handle->sigdata + l, 8192 - l);
5938 Z_Free(handle->sigdata);
5939 Z_Free(handle->postdata);
5943 ll = base64_encode((unsigned char *) (handle->sigdata + l), handle->siglen, 8192 - l - 1);
5946 Z_Free(handle->sigdata);
5947 Z_Free(handle->postdata);
5951 handle->siglen = l + ll;
5952 handle->sigdata[handle->siglen] = 0;
5954 ret = Curl_Begin_ToMemory_POST(url, handle->sigdata, 0, posttype, handle->postdata, handle->postlen, (unsigned char *) handle->buffer, sizeof(handle->buffer), uri_to_string_callback, handle);
5958 handle->postdata = NULL;
5959 handle->postlen = 0;
5960 ret = Curl_Begin_ToMemory(url, 0, (unsigned char *) handle->buffer, sizeof(handle->buffer), uri_to_string_callback, handle);
5964 PRVM_G_INT(OFS_RETURN) = 1;
5968 if(handle->postdata)
5969 Z_Free(handle->postdata);
5971 Z_Free(handle->sigdata);
5973 PRVM_G_INT(OFS_RETURN) = 0;
5977 void VM_netaddress_resolve (void)
5980 char normalized[128];
5982 lhnetaddress_t addr;
5984 VM_SAFEPARMCOUNTRANGE(1, 2, VM_netaddress_resolve);
5986 ip = PRVM_G_STRING(OFS_PARM0);
5989 port = (int) PRVM_G_FLOAT(OFS_PARM1);
5991 if(LHNETADDRESS_FromString(&addr, ip, port) && LHNETADDRESS_ToString(&addr, normalized, sizeof(normalized), prog->argc > 1))
5992 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(normalized);
5994 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString("");
5997 //string(void) getextresponse = #624; // returns the next extResponse packet that was sent to this client
5998 void VM_CL_getextresponse (void)
6000 VM_SAFEPARMCOUNT(0,VM_argv);
6002 if (cl_net_extresponse_count <= 0)
6003 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
6007 --cl_net_extresponse_count;
6008 first = (cl_net_extresponse_last + NET_EXTRESPONSE_MAX - cl_net_extresponse_count) % NET_EXTRESPONSE_MAX;
6009 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(cl_net_extresponse[first]);
6013 void VM_SV_getextresponse (void)
6015 VM_SAFEPARMCOUNT(0,VM_argv);
6017 if (sv_net_extresponse_count <= 0)
6018 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
6022 --sv_net_extresponse_count;
6023 first = (sv_net_extresponse_last + NET_EXTRESPONSE_MAX - sv_net_extresponse_count) % NET_EXTRESPONSE_MAX;
6024 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(sv_net_extresponse[first]);
6030 Common functions between menu.dat and clsprogs
6034 //#349 float() isdemo
6035 void VM_CL_isdemo (void)
6037 VM_SAFEPARMCOUNT(0, VM_CL_isdemo);
6038 PRVM_G_FLOAT(OFS_RETURN) = cls.demoplayback;
6041 //#355 float() videoplaying
6042 void VM_CL_videoplaying (void)
6044 VM_SAFEPARMCOUNT(0, VM_CL_videoplaying);
6045 PRVM_G_FLOAT(OFS_RETURN) = cl_videoplaying;
6052 callfunction(...,string function_name)
6056 mfunction_t *PRVM_ED_FindFunction (const char *name);
6057 void VM_callfunction(void)
6062 VM_SAFEPARMCOUNTRANGE(1, 8, VM_callfunction);
6064 s = PRVM_G_STRING(OFS_PARM0+(prog->argc - 1)*3);
6066 VM_CheckEmptyString(s);
6068 func = PRVM_ED_FindFunction(s);
6071 PRVM_ERROR("VM_callfunciton: function %s not found !", s);
6072 else if (func->first_statement < 0)
6074 // negative statements are built in functions
6075 int builtinnumber = -func->first_statement;
6076 prog->xfunction->builtinsprofile++;
6077 if (builtinnumber < prog->numbuiltins && prog->builtins[builtinnumber])
6078 prog->builtins[builtinnumber]();
6080 PRVM_ERROR("No such builtin #%i in %s; most likely cause: outdated engine build. Try updating!", builtinnumber, PRVM_NAME);
6082 else if(func - prog->functions > 0)
6085 PRVM_ExecuteProgram(func - prog->functions,"");
6094 float isfunction(string function_name)
6097 mfunction_t *PRVM_ED_FindFunction (const char *name);
6098 void VM_isfunction(void)
6103 VM_SAFEPARMCOUNT(1, VM_isfunction);
6105 s = PRVM_G_STRING(OFS_PARM0);
6107 VM_CheckEmptyString(s);
6109 func = PRVM_ED_FindFunction(s);
6112 PRVM_G_FLOAT(OFS_RETURN) = false;
6114 PRVM_G_FLOAT(OFS_RETURN) = true;
6121 string sprintf(string format, ...)
6125 void VM_sprintf(void)
6128 char outbuf[MAX_INPUTLINE];
6129 char *o = outbuf, *end = outbuf + sizeof(outbuf), *err;
6131 int width, precision, thisarg, flags;
6135 static int dummyivec[3] = {0, 0, 0};
6136 static float dummyvec[3] = {0, 0, 0};
6138 #define PRINTF_ALTERNATE 1
6139 #define PRINTF_ZEROPAD 2
6140 #define PRINTF_LEFT 4
6141 #define PRINTF_SPACEPOSITIVE 8
6142 #define PRINTF_SIGNPOSITIVE 16
6146 s = PRVM_G_STRING(OFS_PARM0);
6148 #define GETARG_FLOAT(a) (((a)>=1 && (a)<prog->argc) ? (PRVM_G_FLOAT(OFS_PARM0 + 3 * (a))) : 0)
6149 #define GETARG_VECTOR(a) (((a)>=1 && (a)<prog->argc) ? (PRVM_G_VECTOR(OFS_PARM0 + 3 * (a))) : dummyvec)
6150 #define GETARG_INT(a) (((a)>=1 && (a)<prog->argc) ? (PRVM_G_INT(OFS_PARM0 + 3 * (a))) : 0)
6151 #define GETARG_INTVECTOR(a) (((a)>=1 && (a)<prog->argc) ? ((int*) PRVM_G_VECTOR(OFS_PARM0 + 3 * (a))) : dummyivec)
6152 #define GETARG_STRING(a) (((a)>=1 && (a)<prog->argc) ? (PRVM_G_STRING(OFS_PARM0 + 3 * (a))) : "")
6167 // complete directive format:
6176 // is number following?
6177 if(*s >= '0' && *s <= '9')
6179 width = strtol(s, &err, 10);
6182 VM_Warning("VM_sprintf: invalid directive in %s: %s\n", PRVM_NAME, s0);
6195 flags |= PRINTF_ZEROPAD;
6197 width = -1; // it was just a flag
6209 case '#': flags |= PRINTF_ALTERNATE; break;
6210 case '0': flags |= PRINTF_ZEROPAD; break;
6211 case '-': flags |= PRINTF_LEFT; break;
6212 case ' ': flags |= PRINTF_SPACEPOSITIVE; break;
6213 case '+': flags |= PRINTF_SIGNPOSITIVE; break;
6223 if(*s >= '0' && *s <= '9')
6225 width = strtol(s, &err, 10);
6226 if(!err || *err != '$')
6228 VM_Warning("VM_sprintf: invalid directive in %s: %s\n", PRVM_NAME, s0);
6235 width = GETARG_FLOAT(width);
6238 flags |= PRINTF_LEFT;
6242 else if(*s >= '0' && *s <= '9')
6244 width = strtol(s, &err, 10);
6247 VM_Warning("VM_sprintf: invalid directive in %s: %s\n", PRVM_NAME, s0);
6253 flags |= PRINTF_LEFT;
6257 // otherwise width stays -1
6266 if(*s >= '0' && *s <= '9')
6268 precision = strtol(s, &err, 10);
6269 if(!err || *err != '$')
6271 VM_Warning("VM_sprintf: invalid directive in %s: %s\n", PRVM_NAME, s0);
6277 precision = argpos++;
6278 precision = GETARG_FLOAT(precision);
6280 else if(*s >= '0' && *s <= '9')
6282 precision = strtol(s, &err, 10);
6285 VM_Warning("VM_sprintf: invalid directive in %s: %s\n", PRVM_NAME, s0);
6292 VM_Warning("VM_sprintf: invalid directive in %s: %s\n", PRVM_NAME, s0);
6301 case 'h': isfloat = 1; break;
6302 case 'l': isfloat = 0; break;
6303 case 'L': isfloat = 0; break;
6314 // now s points to the final directive char and is no longer changed
6329 if(*s != 's' && *s != 'c')
6330 if(flags & PRINTF_ALTERNATE) *f++ = '#';
6331 if(flags & PRINTF_ZEROPAD) *f++ = '0';
6332 if(flags & PRINTF_LEFT) *f++ = '-';
6333 if(flags & PRINTF_SPACEPOSITIVE) *f++ = ' ';
6334 if(flags & PRINTF_SIGNPOSITIVE) *f++ = '+';
6341 if(width < 0) // not set
6347 if(precision < 0) // not set
6349 o += dpsnprintf(o, end - o, formatbuf, width, precision, (isfloat ? (int) GETARG_FLOAT(thisarg) : (int) GETARG_INT(thisarg)));
6351 case 'o': case 'u': case 'x': case 'X':
6352 if(precision < 0) // not set
6354 o += dpsnprintf(o, end - o, formatbuf, width, precision, (isfloat ? (unsigned int) GETARG_FLOAT(thisarg) : (unsigned int) GETARG_INT(thisarg)));
6356 case 'e': case 'E': case 'f': case 'F': case 'g': case 'G':
6357 if(precision < 0) // not set
6359 o += dpsnprintf(o, end - o, formatbuf, width, precision, (isfloat ? (double) GETARG_FLOAT(thisarg) : (double) GETARG_INT(thisarg)));
6363 if(precision < 0) // not set
6365 o += dpsnprintf(o, end - o, va("%s %s %s", /* NESTED SPRINTF IS NESTED */ formatbuf, formatbuf, formatbuf),
6366 width, precision, (isfloat ? (double) GETARG_VECTOR(thisarg)[0] : (double) GETARG_INTVECTOR(thisarg)[0]),
6367 width, precision, (isfloat ? (double) GETARG_VECTOR(thisarg)[1] : (double) GETARG_INTVECTOR(thisarg)[1]),
6368 width, precision, (isfloat ? (double) GETARG_VECTOR(thisarg)[2] : (double) GETARG_INTVECTOR(thisarg)[2])
6372 if(precision < 0) // not set
6373 precision = end - o - 1;
6374 if(flags & PRINTF_ALTERNATE)
6375 o += dpsnprintf(o, end - o, formatbuf, width, precision, (isfloat ? (unsigned int) GETARG_FLOAT(thisarg) : (unsigned int) GETARG_INT(thisarg)));
6378 unsigned int c = (isfloat ? (unsigned int) GETARG_FLOAT(thisarg) : (unsigned int) GETARG_INT(thisarg));
6379 const char *buf = u8_encodech(c, NULL);
6382 o += u8_strpad(o, end - o, buf, (flags & PRINTF_LEFT) != 0, width, precision);
6386 if(precision < 0) // not set
6387 precision = end - o - 1;
6388 if(flags & PRINTF_ALTERNATE)
6389 o += dpsnprintf(o, end - o, formatbuf, width, precision, GETARG_STRING(thisarg));
6391 o += u8_strpad(o, end - o, GETARG_STRING(thisarg), (flags & PRINTF_LEFT) != 0, width, precision);
6394 VM_Warning("VM_sprintf: invalid directive in %s: %s\n", PRVM_NAME, s0);
6409 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(outbuf);
6415 static dp_model_t *getmodel(prvm_edict_t *ed)
6417 switch(PRVM_GetProgNr())
6419 case PRVM_SERVERPROG:
6420 return SV_GetModelFromEdict(ed);
6421 case PRVM_CLIENTPROG:
6422 return CL_GetModelFromEdict(ed);
6430 unsigned int progid;
6432 frameblend_t frameblend[MAX_FRAMEBLENDS];
6433 skeleton_t *skeleton_p;
6434 skeleton_t skeleton;
6435 float *data_vertex3f;
6436 float *data_svector3f;
6437 float *data_tvector3f;
6438 float *data_normal3f;
6440 float *buf_vertex3f;
6441 float *buf_svector3f;
6442 float *buf_tvector3f;
6443 float *buf_normal3f;
6445 animatemodel_cache_t;
6446 static animatemodel_cache_t animatemodel_cache;
6448 void animatemodel(dp_model_t *model, prvm_edict_t *ed)
6451 skeleton_t *skeleton;
6452 int skeletonindex = -1;
6453 qboolean need = false;
6454 if(!(model->surfmesh.isanimated && model->AnimateVertices))
6456 animatemodel_cache.data_vertex3f = model->surfmesh.data_vertex3f;
6457 animatemodel_cache.data_svector3f = model->surfmesh.data_svector3f;
6458 animatemodel_cache.data_tvector3f = model->surfmesh.data_tvector3f;
6459 animatemodel_cache.data_normal3f = model->surfmesh.data_normal3f;
6462 if(animatemodel_cache.progid != prog->id)
6463 memset(&animatemodel_cache, 0, sizeof(animatemodel_cache));
6464 need |= (animatemodel_cache.model != model);
6465 VM_GenerateFrameGroupBlend(ed->priv.server->framegroupblend, ed);
6466 VM_FrameBlendFromFrameGroupBlend(ed->priv.server->frameblend, ed->priv.server->framegroupblend, model);
6467 need |= (memcmp(&animatemodel_cache.frameblend, &ed->priv.server->frameblend, sizeof(ed->priv.server->frameblend))) != 0;
6468 if ((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.skeletonindex))) skeletonindex = (int)val->_float - 1;
6469 if (!(skeletonindex >= 0 && skeletonindex < MAX_EDICTS && (skeleton = prog->skeletons[skeletonindex]) && skeleton->model->num_bones == ed->priv.server->skeleton.model->num_bones))
6471 need |= (animatemodel_cache.skeleton_p != skeleton);
6473 need |= (memcmp(&animatemodel_cache.skeleton, skeleton, sizeof(ed->priv.server->skeleton))) != 0;
6476 if(model->surfmesh.num_vertices > animatemodel_cache.max_vertices)
6478 animatemodel_cache.max_vertices = model->surfmesh.num_vertices * 2;
6479 if(animatemodel_cache.buf_vertex3f) Mem_Free(animatemodel_cache.buf_vertex3f);
6480 if(animatemodel_cache.buf_svector3f) Mem_Free(animatemodel_cache.buf_svector3f);
6481 if(animatemodel_cache.buf_tvector3f) Mem_Free(animatemodel_cache.buf_tvector3f);
6482 if(animatemodel_cache.buf_normal3f) Mem_Free(animatemodel_cache.buf_normal3f);
6483 animatemodel_cache.buf_vertex3f = (float *)Mem_Alloc(prog->progs_mempool, sizeof(float[3]) * animatemodel_cache.max_vertices);
6484 animatemodel_cache.buf_svector3f = (float *)Mem_Alloc(prog->progs_mempool, sizeof(float[3]) * animatemodel_cache.max_vertices);
6485 animatemodel_cache.buf_tvector3f = (float *)Mem_Alloc(prog->progs_mempool, sizeof(float[3]) * animatemodel_cache.max_vertices);
6486 animatemodel_cache.buf_normal3f = (float *)Mem_Alloc(prog->progs_mempool, sizeof(float[3]) * animatemodel_cache.max_vertices);
6488 animatemodel_cache.data_vertex3f = animatemodel_cache.buf_vertex3f;
6489 animatemodel_cache.data_svector3f = animatemodel_cache.buf_svector3f;
6490 animatemodel_cache.data_tvector3f = animatemodel_cache.buf_tvector3f;
6491 animatemodel_cache.data_normal3f = animatemodel_cache.buf_normal3f;
6492 VM_UpdateEdictSkeleton(ed, model, ed->priv.server->frameblend);
6493 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);
6494 animatemodel_cache.progid = prog->id;
6495 animatemodel_cache.model = model;
6496 memcpy(&animatemodel_cache.frameblend, &ed->priv.server->frameblend, sizeof(ed->priv.server->frameblend));
6497 animatemodel_cache.skeleton_p = skeleton;
6499 memcpy(&animatemodel_cache.skeleton, skeleton, sizeof(ed->priv.server->skeleton));
6502 static void getmatrix(prvm_edict_t *ed, matrix4x4_t *out)
6504 switch(PRVM_GetProgNr())
6506 case PRVM_SERVERPROG:
6507 SV_GetEntityMatrix(ed, out, false);
6509 case PRVM_CLIENTPROG:
6510 CL_GetEntityMatrix(ed, out, false);
6513 *out = identitymatrix;
6518 static void applytransform_forward(const vec3_t in, prvm_edict_t *ed, vec3_t out)
6522 Matrix4x4_Transform(&m, in, out);
6525 static void applytransform_forward_direction(const vec3_t in, prvm_edict_t *ed, vec3_t out)
6529 Matrix4x4_Transform3x3(&m, in, out);
6532 static void applytransform_inverted(const vec3_t in, prvm_edict_t *ed, vec3_t out)
6536 Matrix4x4_Invert_Full(&n, &m);
6537 Matrix4x4_Transform3x3(&n, in, out);
6540 static void applytransform_forward_normal(const vec3_t in, prvm_edict_t *ed, vec3_t out)
6545 Matrix4x4_TransformPositivePlane(&m, in[0], in[1], in[2], 0, p);
6549 static void clippointtosurface(prvm_edict_t *ed, dp_model_t *model, msurface_t *surface, vec3_t p, vec3_t out)
6552 float *v[3], facenormal[3], edgenormal[3], sidenormal[3], temp[3], offsetdist, dist, bestdist;
6554 animatemodel(model, ed);
6555 bestdist = 1000000000;
6557 for (i = 0, e = (model->surfmesh.data_element3i + 3 * surface->num_firsttriangle);i < surface->num_triangles;i++, e += 3)
6559 // clip original point to each triangle of the surface and find the
6560 // triangle that is closest
6561 v[0] = animatemodel_cache.data_vertex3f + e[0] * 3;
6562 v[1] = animatemodel_cache.data_vertex3f + e[1] * 3;
6563 v[2] = animatemodel_cache.data_vertex3f + e[2] * 3;
6564 TriangleNormal(v[0], v[1], v[2], facenormal);
6565 VectorNormalize(facenormal);
6566 offsetdist = DotProduct(v[0], facenormal) - DotProduct(p, facenormal);
6567 VectorMA(p, offsetdist, facenormal, temp);
6568 for (j = 0, k = 2;j < 3;k = j, j++)
6570 VectorSubtract(v[k], v[j], edgenormal);
6571 CrossProduct(edgenormal, facenormal, sidenormal);
6572 VectorNormalize(sidenormal);
6573 offsetdist = DotProduct(v[k], sidenormal) - DotProduct(temp, sidenormal);
6575 VectorMA(temp, offsetdist, sidenormal, temp);
6577 dist = VectorDistance2(temp, p);
6578 if (bestdist > dist)
6581 VectorCopy(temp, out);
6586 static msurface_t *getsurface(dp_model_t *model, int surfacenum)
6588 if (surfacenum < 0 || surfacenum >= model->nummodelsurfaces)
6590 return model->data_surfaces + surfacenum + model->firstmodelsurface;
6594 //PF_getsurfacenumpoints, // #434 float(entity e, float s) getsurfacenumpoints = #434;
6595 void VM_getsurfacenumpoints(void)
6598 msurface_t *surface;
6599 VM_SAFEPARMCOUNT(2, VM_getsurfacenumpoints);
6600 // return 0 if no such surface
6601 if (!(model = getmodel(PRVM_G_EDICT(OFS_PARM0))) || !(surface = getsurface(model, (int)PRVM_G_FLOAT(OFS_PARM1))))
6603 PRVM_G_FLOAT(OFS_RETURN) = 0;
6607 // note: this (incorrectly) assumes it is a simple polygon
6608 PRVM_G_FLOAT(OFS_RETURN) = surface->num_vertices;
6610 //PF_getsurfacepoint, // #435 vector(entity e, float s, float n) getsurfacepoint = #435;
6611 void VM_getsurfacepoint(void)
6615 msurface_t *surface;
6617 VM_SAFEPARMCOUNT(3, VM_getsurfacepoint);
6618 VectorClear(PRVM_G_VECTOR(OFS_RETURN));
6619 ed = PRVM_G_EDICT(OFS_PARM0);
6620 if (!(model = getmodel(ed)) || !(surface = getsurface(model, (int)PRVM_G_FLOAT(OFS_PARM1))))
6622 // note: this (incorrectly) assumes it is a simple polygon
6623 pointnum = (int)PRVM_G_FLOAT(OFS_PARM2);
6624 if (pointnum < 0 || pointnum >= surface->num_vertices)
6626 animatemodel(model, ed);
6627 applytransform_forward(&(animatemodel_cache.data_vertex3f + 3 * surface->num_firstvertex)[pointnum * 3], ed, PRVM_G_VECTOR(OFS_RETURN));
6629 //PF_getsurfacepointattribute, // #486 vector(entity e, float s, float n, float a) getsurfacepointattribute = #486;
6630 // float SPA_POSITION = 0;
6631 // float SPA_S_AXIS = 1;
6632 // float SPA_T_AXIS = 2;
6633 // float SPA_R_AXIS = 3; // same as SPA_NORMAL
6634 // float SPA_TEXCOORDS0 = 4;
6635 // float SPA_LIGHTMAP0_TEXCOORDS = 5;
6636 // float SPA_LIGHTMAP0_COLOR = 6;
6637 void VM_getsurfacepointattribute(void)
6641 msurface_t *surface;
6645 VM_SAFEPARMCOUNT(4, VM_getsurfacepoint);
6646 VectorClear(PRVM_G_VECTOR(OFS_RETURN));
6647 ed = PRVM_G_EDICT(OFS_PARM0);
6648 if (!(model = getmodel(ed)) || !(surface = getsurface(model, (int)PRVM_G_FLOAT(OFS_PARM1))))
6650 pointnum = (int)PRVM_G_FLOAT(OFS_PARM2);
6651 if (pointnum < 0 || pointnum >= surface->num_vertices)
6653 attributetype = (int) PRVM_G_FLOAT(OFS_PARM3);
6655 animatemodel(model, ed);
6657 switch( attributetype ) {
6658 // float SPA_POSITION = 0;
6660 applytransform_forward(&(animatemodel_cache.data_vertex3f + 3 * surface->num_firstvertex)[pointnum * 3], ed, PRVM_G_VECTOR(OFS_RETURN));
6662 // float SPA_S_AXIS = 1;
6664 applytransform_forward_direction(&(animatemodel_cache.data_svector3f + 3 * surface->num_firstvertex)[pointnum * 3], ed, PRVM_G_VECTOR(OFS_RETURN));
6666 // float SPA_T_AXIS = 2;
6668 applytransform_forward_direction(&(animatemodel_cache.data_tvector3f + 3 * surface->num_firstvertex)[pointnum * 3], ed, PRVM_G_VECTOR(OFS_RETURN));
6670 // float SPA_R_AXIS = 3; // same as SPA_NORMAL
6672 applytransform_forward_direction(&(animatemodel_cache.data_normal3f + 3 * surface->num_firstvertex)[pointnum * 3], ed, PRVM_G_VECTOR(OFS_RETURN));
6674 // float SPA_TEXCOORDS0 = 4;
6676 float *ret = PRVM_G_VECTOR(OFS_RETURN);
6677 float *texcoord = &(model->surfmesh.data_texcoordtexture2f + 2 * surface->num_firstvertex)[pointnum * 2];
6678 ret[0] = texcoord[0];
6679 ret[1] = texcoord[1];
6683 // float SPA_LIGHTMAP0_TEXCOORDS = 5;
6685 float *ret = PRVM_G_VECTOR(OFS_RETURN);
6686 float *texcoord = &(model->surfmesh.data_texcoordlightmap2f + 2 * surface->num_firstvertex)[pointnum * 2];
6687 ret[0] = texcoord[0];
6688 ret[1] = texcoord[1];
6692 // float SPA_LIGHTMAP0_COLOR = 6;
6694 // ignore alpha for now..
6695 VectorCopy( &(model->surfmesh.data_lightmapcolor4f + 4 * surface->num_firstvertex)[pointnum * 4], PRVM_G_VECTOR(OFS_RETURN));
6698 VectorSet( PRVM_G_VECTOR(OFS_RETURN), 0.0f, 0.0f, 0.0f );
6702 //PF_getsurfacenormal, // #436 vector(entity e, float s) getsurfacenormal = #436;
6703 void VM_getsurfacenormal(void)
6706 msurface_t *surface;
6708 VM_SAFEPARMCOUNT(2, VM_getsurfacenormal);
6709 VectorClear(PRVM_G_VECTOR(OFS_RETURN));
6710 if (!(model = getmodel(PRVM_G_EDICT(OFS_PARM0))) || !(surface = getsurface(model, (int)PRVM_G_FLOAT(OFS_PARM1))))
6712 // note: this only returns the first triangle, so it doesn't work very
6713 // well for curved surfaces or arbitrary meshes
6714 animatemodel(model, PRVM_G_EDICT(OFS_PARM0));
6715 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);
6716 applytransform_forward_normal(normal, PRVM_G_EDICT(OFS_PARM0), PRVM_G_VECTOR(OFS_RETURN));
6717 VectorNormalize(PRVM_G_VECTOR(OFS_RETURN));
6719 //PF_getsurfacetexture, // #437 string(entity e, float s) getsurfacetexture = #437;
6720 void VM_getsurfacetexture(void)
6723 msurface_t *surface;
6724 VM_SAFEPARMCOUNT(2, VM_getsurfacetexture);
6725 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
6726 if (!(model = getmodel(PRVM_G_EDICT(OFS_PARM0))) || !(surface = getsurface(model, (int)PRVM_G_FLOAT(OFS_PARM1))))
6728 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(surface->texture->name);
6730 //PF_getsurfacenearpoint, // #438 float(entity e, vector p) getsurfacenearpoint = #438;
6731 void VM_getsurfacenearpoint(void)
6733 int surfacenum, best;
6735 vec_t dist, bestdist;
6738 msurface_t *surface;
6740 VM_SAFEPARMCOUNT(2, VM_getsurfacenearpoint);
6741 PRVM_G_FLOAT(OFS_RETURN) = -1;
6742 ed = PRVM_G_EDICT(OFS_PARM0);
6743 point = PRVM_G_VECTOR(OFS_PARM1);
6745 if (!ed || ed->priv.server->free)
6747 model = getmodel(ed);
6748 if (!model || !model->num_surfaces)
6751 animatemodel(model, ed);
6753 applytransform_inverted(point, ed, p);
6755 bestdist = 1000000000;
6756 for (surfacenum = 0;surfacenum < model->nummodelsurfaces;surfacenum++)
6758 surface = model->data_surfaces + surfacenum + model->firstmodelsurface;
6759 // first see if the nearest point on the surface's box is closer than the previous match
6760 clipped[0] = bound(surface->mins[0], p[0], surface->maxs[0]) - p[0];
6761 clipped[1] = bound(surface->mins[1], p[1], surface->maxs[1]) - p[1];
6762 clipped[2] = bound(surface->mins[2], p[2], surface->maxs[2]) - p[2];
6763 dist = VectorLength2(clipped);
6764 if (dist < bestdist)
6766 // it is, check the nearest point on the actual geometry
6767 clippointtosurface(ed, model, surface, p, clipped);
6768 VectorSubtract(clipped, p, clipped);
6769 dist += VectorLength2(clipped);
6770 if (dist < bestdist)
6772 // that's closer too, store it as the best match
6778 PRVM_G_FLOAT(OFS_RETURN) = best;
6780 //PF_getsurfaceclippedpoint, // #439 vector(entity e, float s, vector p) getsurfaceclippedpoint = #439;
6781 void VM_getsurfaceclippedpoint(void)
6785 msurface_t *surface;
6787 VM_SAFEPARMCOUNT(3, VM_te_getsurfaceclippedpoint);
6788 VectorClear(PRVM_G_VECTOR(OFS_RETURN));
6789 ed = PRVM_G_EDICT(OFS_PARM0);
6790 if (!(model = getmodel(ed)) || !(surface = getsurface(model, (int)PRVM_G_FLOAT(OFS_PARM1))))
6792 animatemodel(model, ed);
6793 applytransform_inverted(PRVM_G_VECTOR(OFS_PARM2), ed, p);
6794 clippointtosurface(ed, model, surface, p, out);
6795 VectorAdd(out, ed->fields.server->origin, PRVM_G_VECTOR(OFS_RETURN));
6798 //PF_getsurfacenumtriangles, // #??? float(entity e, float s) getsurfacenumtriangles = #???;
6799 void VM_getsurfacenumtriangles(void)
6802 msurface_t *surface;
6803 VM_SAFEPARMCOUNT(2, VM_SV_getsurfacenumtriangles);
6804 // return 0 if no such surface
6805 if (!(model = getmodel(PRVM_G_EDICT(OFS_PARM0))) || !(surface = getsurface(model, (int)PRVM_G_FLOAT(OFS_PARM1))))
6807 PRVM_G_FLOAT(OFS_RETURN) = 0;
6811 // note: this (incorrectly) assumes it is a simple polygon
6812 PRVM_G_FLOAT(OFS_RETURN) = surface->num_triangles;
6814 //PF_getsurfacetriangle, // #??? vector(entity e, float s, float n) getsurfacetriangle = #???;
6815 void VM_getsurfacetriangle(void)
6817 const vec3_t d = {-1, -1, -1};
6820 msurface_t *surface;
6822 VM_SAFEPARMCOUNT(3, VM_SV_getsurfacetriangle);
6823 VectorClear(PRVM_G_VECTOR(OFS_RETURN));
6824 ed = PRVM_G_EDICT(OFS_PARM0);
6825 if (!(model = getmodel(ed)) || !(surface = getsurface(model, (int)PRVM_G_FLOAT(OFS_PARM1))))
6827 trinum = (int)PRVM_G_FLOAT(OFS_PARM2);
6828 if (trinum < 0 || trinum >= surface->num_triangles)
6830 // FIXME: implement rotation/scaling
6831 VectorMA(&(model->surfmesh.data_element3i + 3 * surface->num_firsttriangle)[trinum * 3], surface->num_firstvertex, d, PRVM_G_VECTOR(OFS_RETURN));