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"
18 extern cvar_t prvm_backtraceforwarnings;
20 // LordHavoc: changed this to NOT use a return statement, so that it can be used in functions that must return a value
21 void VM_Warning(const char *fmt, ...)
24 char msg[MAX_INPUTLINE];
25 static double recursive = -1;
28 dpvsnprintf(msg,sizeof(msg),fmt,argptr);
33 // TODO: either add a cvar/cmd to control the state dumping or replace some of the calls with Con_Printf [9/13/2006 Black]
34 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
43 //============================================================================
46 // TODO DONE: move vm_files and vm_fssearchlist to prvm_prog_t struct
47 // TODO: move vm_files and vm_fssearchlist back [9/13/2006 Black]
48 // TODO: (move vm_files and vm_fssearchlist to prvm_prog_t struct again) [2007-01-23 LordHavoc]
49 // TODO: will this war ever end? [2007-01-23 LordHavoc]
51 void VM_CheckEmptyString (const char *s)
53 if (ISWHITESPACE(s[0]))
54 PRVM_ERROR ("%s: Bad string", PRVM_NAME);
57 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 framegroupblend[0].frame = (int) PRVM_gameedictfloat(ed, frame );
69 framegroupblend[1].frame = (int) PRVM_gameedictfloat(ed, frame2 );
70 framegroupblend[2].frame = (int) PRVM_gameedictfloat(ed, frame3 );
71 framegroupblend[3].frame = (int) PRVM_gameedictfloat(ed, frame4 );
72 framegroupblend[0].start = PRVM_gameedictfloat(ed, frame1time);
73 framegroupblend[1].start = PRVM_gameedictfloat(ed, frame2time);
74 framegroupblend[2].start = PRVM_gameedictfloat(ed, frame3time);
75 framegroupblend[3].start = PRVM_gameedictfloat(ed, frame4time);
76 framegroupblend[1].lerp = PRVM_gameedictfloat(ed, lerpfrac );
77 framegroupblend[2].lerp = PRVM_gameedictfloat(ed, lerpfrac3 );
78 framegroupblend[3].lerp = PRVM_gameedictfloat(ed, lerpfrac4 );
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;
201 skeletonindex = (int)PRVM_gameedictfloat(ed, skeletonindex) - 1;
202 if (skeletonindex >= 0 && skeletonindex < MAX_EDICTS && (skeleton = prog->skeletons[skeletonindex]) && skeleton->model->num_bones == ed->priv.server->skeleton.model->num_bones)
204 // custom skeleton controlled by the game (FTE_CSQC_SKELETONOBJECTS)
205 if (!ed->priv.server->skeleton.relativetransforms)
206 ed->priv.server->skeleton.relativetransforms = (matrix4x4_t *)Mem_Alloc(prog->progs_mempool, ed->priv.server->skeleton.model->num_bones * sizeof(matrix4x4_t));
207 memcpy(ed->priv.server->skeleton.relativetransforms, skeleton->relativetransforms, ed->priv.server->skeleton.model->num_bones * sizeof(matrix4x4_t));
211 if(ed->priv.server->skeleton.relativetransforms)
212 Mem_Free(ed->priv.server->skeleton.relativetransforms);
213 ed->priv.server->skeleton.relativetransforms = NULL;
218 void VM_RemoveEdictSkeleton(prvm_edict_t *ed)
220 if (ed->priv.server->skeleton.relativetransforms)
221 Mem_Free(ed->priv.server->skeleton.relativetransforms);
222 memset(&ed->priv.server->skeleton, 0, sizeof(ed->priv.server->skeleton));
228 //============================================================================
231 void VM_VarString(int first, char *out, int outlength)
237 outend = out + outlength - 1;
238 for (i = first;i < prog->argc && out < outend;i++)
240 s = PRVM_G_STRING((OFS_PARM0+i*3));
241 while (out < outend && *s)
251 returns true if the extension is supported by the server
253 checkextension(extensionname)
257 // kind of helper function
258 static qboolean checkextension(const char *name)
261 const char *e, *start;
262 len = (int)strlen(name);
264 for (e = prog->extensionstring;*e;e++)
271 while (*e && *e != ' ')
273 if ((e - start) == len && !strncasecmp(start, name, len))
275 // special sheck for ODE
276 if (!strncasecmp("DP_PHYSICS_ODE", name, 14))
279 return ode_dll ? true : false;
285 // special sheck for d0_blind_id
286 if (!strcasecmp("DP_CRYPTO", name))
287 return Crypto_Available();
288 if (!strcasecmp("DP_QC_DIGEST_SHA256", name))
289 return Crypto_Available();
297 void VM_checkextension (void)
299 VM_SAFEPARMCOUNT(1,VM_checkextension);
301 PRVM_G_FLOAT(OFS_RETURN) = checkextension(PRVM_G_STRING(OFS_PARM0));
308 This is a TERMINAL error, which will kill off the entire prog.
317 char string[VM_STRINGTEMP_LENGTH];
319 VM_VarString(0, string, sizeof(string));
320 Con_Printf("======%s ERROR in %s:\n%s\n", PRVM_NAME, PRVM_GetString(prog->xfunction->s_name), string);
321 ed = PRVM_PROG_TO_EDICT(PRVM_allglobaledict(self));
322 PRVM_ED_Print(ed, NULL);
324 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);
331 Dumps out self, then an error message. The program is aborted and self is
332 removed, but the level can continue.
337 void VM_objerror (void)
340 char string[VM_STRINGTEMP_LENGTH];
342 VM_VarString(0, string, sizeof(string));
343 Con_Printf("======OBJECT ERROR======\n"); // , PRVM_NAME, PRVM_GetString(prog->xfunction->s_name), string); // or include them? FIXME
344 ed = PRVM_PROG_TO_EDICT(PRVM_allglobaledict(self));
345 PRVM_ED_Print(ed, NULL);
347 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);
361 char string[VM_STRINGTEMP_LENGTH];
363 VM_VarString(0, string, sizeof(string));
371 broadcast print to everyone on server
376 void VM_bprint (void)
378 char string[VM_STRINGTEMP_LENGTH];
382 VM_Warning("VM_bprint: game is not server(%s) !\n", PRVM_NAME);
386 VM_VarString(0, string, sizeof(string));
387 SV_BroadcastPrint(string);
392 VM_sprint (menu & client but only if server.active == true)
394 single print to a specific client
396 sprint(float clientnum,...[string])
399 void VM_sprint (void)
403 char string[VM_STRINGTEMP_LENGTH];
405 VM_SAFEPARMCOUNTRANGE(1, 8, VM_sprint);
407 //find client for this entity
408 clientnum = (int)PRVM_G_FLOAT(OFS_PARM0);
409 if (!sv.active || clientnum < 0 || clientnum >= svs.maxclients || !svs.clients[clientnum].active)
411 VM_Warning("VM_sprint: %s: invalid client or server is not active !\n", PRVM_NAME);
415 client = svs.clients + clientnum;
416 if (!client->netconnection)
419 VM_VarString(1, string, sizeof(string));
420 MSG_WriteChar(&client->netconnection->message,svc_print);
421 MSG_WriteString(&client->netconnection->message, string);
428 single print to the screen
433 void VM_centerprint (void)
435 char string[VM_STRINGTEMP_LENGTH];
437 VM_SAFEPARMCOUNTRANGE(1, 8, VM_centerprint);
438 VM_VarString(0, string, sizeof(string));
439 SCR_CenterPrint(string);
446 vector normalize(vector)
449 void VM_normalize (void)
455 VM_SAFEPARMCOUNT(1,VM_normalize);
457 value1 = PRVM_G_VECTOR(OFS_PARM0);
459 f = VectorLength2(value1);
463 VectorScale(value1, f, newvalue);
466 VectorClear(newvalue);
468 VectorCopy (newvalue, PRVM_G_VECTOR(OFS_RETURN));
480 VM_SAFEPARMCOUNT(1,VM_vlen);
481 PRVM_G_FLOAT(OFS_RETURN) = VectorLength(PRVM_G_VECTOR(OFS_PARM0));
488 float vectoyaw(vector)
491 void VM_vectoyaw (void)
496 VM_SAFEPARMCOUNT(1,VM_vectoyaw);
498 value1 = PRVM_G_VECTOR(OFS_PARM0);
500 if (value1[1] == 0 && value1[0] == 0)
504 yaw = (int) (atan2(value1[1], value1[0]) * 180 / M_PI);
509 PRVM_G_FLOAT(OFS_RETURN) = yaw;
517 vector vectoangles(vector[, vector])
520 void VM_vectoangles (void)
522 VM_SAFEPARMCOUNTRANGE(1, 2,VM_vectoangles);
524 AnglesFromVectors(PRVM_G_VECTOR(OFS_RETURN), PRVM_G_VECTOR(OFS_PARM0), prog->argc >= 2 ? PRVM_G_VECTOR(OFS_PARM1) : NULL, true);
531 Returns a number from 0<= num < 1
536 void VM_random (void)
538 VM_SAFEPARMCOUNT(0,VM_random);
540 PRVM_G_FLOAT(OFS_RETURN) = lhrandom(0, 1);
547 localsound(string sample)
550 void VM_localsound(void)
554 VM_SAFEPARMCOUNT(1,VM_localsound);
556 s = PRVM_G_STRING(OFS_PARM0);
558 if(!S_LocalSound (s))
560 PRVM_G_FLOAT(OFS_RETURN) = -4;
561 VM_Warning("VM_localsound: Failed to play %s for %s !\n", s, PRVM_NAME);
565 PRVM_G_FLOAT(OFS_RETURN) = 1;
577 PRVM_ERROR ("%s: break statement", PRVM_NAME);
580 //============================================================================
586 Sends text over to the client's execution buffer
588 [localcmd (string, ...) or]
592 void VM_localcmd (void)
594 char string[VM_STRINGTEMP_LENGTH];
595 VM_SAFEPARMCOUNTRANGE(1, 8, VM_localcmd);
596 VM_VarString(0, string, sizeof(string));
597 Cbuf_AddText(string);
600 static qboolean PRVM_Cvar_ReadOk(const char *string)
603 cvar = Cvar_FindVar(string);
604 return ((cvar) && ((cvar->flags & CVAR_PRIVATE) == 0));
616 char string[VM_STRINGTEMP_LENGTH];
617 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar);
618 VM_VarString(0, string, sizeof(string));
619 VM_CheckEmptyString(string);
620 PRVM_G_FLOAT(OFS_RETURN) = PRVM_Cvar_ReadOk(string) ? Cvar_VariableValue(string) : 0;
627 float cvar_type (string)
628 float CVAR_TYPEFLAG_EXISTS = 1;
629 float CVAR_TYPEFLAG_SAVED = 2;
630 float CVAR_TYPEFLAG_PRIVATE = 4;
631 float CVAR_TYPEFLAG_ENGINE = 8;
632 float CVAR_TYPEFLAG_HASDESCRIPTION = 16;
633 float CVAR_TYPEFLAG_READONLY = 32;
636 void VM_cvar_type (void)
638 char string[VM_STRINGTEMP_LENGTH];
642 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar);
643 VM_VarString(0, string, sizeof(string));
644 VM_CheckEmptyString(string);
645 cvar = Cvar_FindVar(string);
650 PRVM_G_FLOAT(OFS_RETURN) = 0;
651 return; // CVAR_TYPE_NONE
654 ret = 1; // CVAR_EXISTS
655 if(cvar->flags & CVAR_SAVE)
656 ret |= 2; // CVAR_TYPE_SAVED
657 if(cvar->flags & CVAR_PRIVATE)
658 ret |= 4; // CVAR_TYPE_PRIVATE
659 if(!(cvar->flags & CVAR_ALLOCATED))
660 ret |= 8; // CVAR_TYPE_ENGINE
661 if(cvar->description != cvar_dummy_description)
662 ret |= 16; // CVAR_TYPE_HASDESCRIPTION
663 if(cvar->flags & CVAR_READONLY)
664 ret |= 32; // CVAR_TYPE_READONLY
666 PRVM_G_FLOAT(OFS_RETURN) = ret;
673 const string VM_cvar_string (string, ...)
676 void VM_cvar_string(void)
678 char string[VM_STRINGTEMP_LENGTH];
679 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar_string);
680 VM_VarString(0, string, sizeof(string));
681 VM_CheckEmptyString(string);
682 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(PRVM_Cvar_ReadOk(string) ? Cvar_VariableString(string) : "");
687 ========================
690 const string VM_cvar_defstring (string, ...)
691 ========================
693 void VM_cvar_defstring (void)
695 char string[VM_STRINGTEMP_LENGTH];
696 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar_defstring);
697 VM_VarString(0, string, sizeof(string));
698 VM_CheckEmptyString(string);
699 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(Cvar_VariableDefString(string));
703 ========================
706 const string VM_cvar_description (string, ...)
707 ========================
709 void VM_cvar_description (void)
711 char string[VM_STRINGTEMP_LENGTH];
712 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar_description);
713 VM_VarString(0, string, sizeof(string));
714 VM_CheckEmptyString(string);
715 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(Cvar_VariableDescription(string));
721 void cvar_set (string,string, ...)
724 void VM_cvar_set (void)
727 char string[VM_STRINGTEMP_LENGTH];
728 VM_SAFEPARMCOUNTRANGE(2,8,VM_cvar_set);
729 VM_VarString(1, string, sizeof(string));
730 name = PRVM_G_STRING(OFS_PARM0);
731 VM_CheckEmptyString(name);
732 Cvar_Set(name, string);
742 void VM_dprint (void)
744 char string[VM_STRINGTEMP_LENGTH];
745 VM_SAFEPARMCOUNTRANGE(1, 8, VM_dprint);
746 VM_VarString(0, string, sizeof(string));
748 Con_DPrintf("%s", string);
750 Con_DPrintf("%s: %s", PRVM_NAME, string);
767 VM_SAFEPARMCOUNT(1, VM_ftos);
769 v = PRVM_G_FLOAT(OFS_PARM0);
771 if ((float)((int)v) == v)
772 dpsnprintf(s, sizeof(s), "%i", (int)v);
774 dpsnprintf(s, sizeof(s), "%f", v);
775 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(s);
790 VM_SAFEPARMCOUNT(1,VM_fabs);
792 v = PRVM_G_FLOAT(OFS_PARM0);
793 PRVM_G_FLOAT(OFS_RETURN) = fabs(v);
808 VM_SAFEPARMCOUNT(1,VM_vtos);
810 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]);
811 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(s);
826 VM_SAFEPARMCOUNT(1, VM_etos);
828 dpsnprintf (s, sizeof(s), "entity %i", PRVM_G_EDICTNUM(OFS_PARM0));
829 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(s);
836 float stof(...[string])
841 char string[VM_STRINGTEMP_LENGTH];
842 VM_SAFEPARMCOUNTRANGE(1, 8, VM_stof);
843 VM_VarString(0, string, sizeof(string));
844 PRVM_G_FLOAT(OFS_RETURN) = atof(string);
848 ========================
852 ========================
856 VM_SAFEPARMCOUNT(1, VM_itof);
857 PRVM_G_FLOAT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
861 ========================
864 entity ftoe(float num)
865 ========================
870 VM_SAFEPARMCOUNT(1, VM_ftoe);
872 ent = (int)PRVM_G_FLOAT(OFS_PARM0);
873 if (ent < 0 || ent >= prog->max_edicts || PRVM_PROG_TO_EDICT(ent)->priv.required->free)
874 ent = 0; // return world instead of a free or invalid entity
876 PRVM_G_INT(OFS_RETURN) = ent;
880 ========================
883 float etof(entity ent)
884 ========================
888 VM_SAFEPARMCOUNT(1, VM_etof);
889 PRVM_G_FLOAT(OFS_RETURN) = PRVM_G_EDICTNUM(OFS_PARM0);
896 string strftime(float uselocaltime, string[, string ...])
899 void VM_strftime(void)
908 char fmt[VM_STRINGTEMP_LENGTH];
909 char result[VM_STRINGTEMP_LENGTH];
910 VM_SAFEPARMCOUNTRANGE(2, 8, VM_strftime);
911 VM_VarString(1, fmt, sizeof(fmt));
914 if (PRVM_G_FLOAT(OFS_PARM0))
915 tmresult = localtime_s(&tm, &t);
917 tmresult = gmtime_s(&tm, &t);
920 if (PRVM_G_FLOAT(OFS_PARM0))
927 PRVM_G_INT(OFS_RETURN) = 0;
931 strftime(result, sizeof(result), fmt, &tm);
933 strftime(result, sizeof(result), fmt, tm);
935 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(result);
949 VM_SAFEPARMCOUNT(0, VM_spawn);
950 prog->xfunction->builtinsprofile += 20;
951 ed = PRVM_ED_Alloc();
963 void VM_remove (void)
966 prog->xfunction->builtinsprofile += 20;
968 VM_SAFEPARMCOUNT(1, VM_remove);
970 ed = PRVM_G_EDICT(OFS_PARM0);
971 if( PRVM_NUM_FOR_EDICT(ed) <= prog->reserved_edicts )
973 if (developer.integer > 0)
974 VM_Warning( "VM_remove: tried to remove the null entity or a reserved entity!\n" );
976 else if( ed->priv.required->free )
978 if (developer.integer > 0)
979 VM_Warning( "VM_remove: tried to remove an already freed entity!\n" );
989 entity find(entity start, .string field, string match)
1000 VM_SAFEPARMCOUNT(3,VM_find);
1002 e = PRVM_G_EDICTNUM(OFS_PARM0);
1003 f = PRVM_G_INT(OFS_PARM1);
1004 s = PRVM_G_STRING(OFS_PARM2);
1006 // LordHavoc: apparently BloodMage does a find(world, weaponmodel, "") and
1007 // expects it to find all the monsters, so we must be careful to support
1010 for (e++ ; e < prog->num_edicts ; e++)
1012 prog->xfunction->builtinsprofile++;
1013 ed = PRVM_EDICT_NUM(e);
1014 if (ed->priv.required->free)
1016 t = PRVM_E_STRING(ed,f);
1021 VM_RETURN_EDICT(ed);
1026 VM_RETURN_EDICT(prog->edicts);
1033 entity findfloat(entity start, .float field, float match)
1034 entity findentity(entity start, .entity field, entity match)
1037 // LordHavoc: added this for searching float, int, and entity reference fields
1038 void VM_findfloat (void)
1045 VM_SAFEPARMCOUNT(3,VM_findfloat);
1047 e = PRVM_G_EDICTNUM(OFS_PARM0);
1048 f = PRVM_G_INT(OFS_PARM1);
1049 s = PRVM_G_FLOAT(OFS_PARM2);
1051 for (e++ ; e < prog->num_edicts ; e++)
1053 prog->xfunction->builtinsprofile++;
1054 ed = PRVM_EDICT_NUM(e);
1055 if (ed->priv.required->free)
1057 if (PRVM_E_FLOAT(ed,f) == s)
1059 VM_RETURN_EDICT(ed);
1064 VM_RETURN_EDICT(prog->edicts);
1071 entity findchain(.string field, string match)
1074 // chained search for strings in entity fields
1075 // entity(.string field, string match) findchain = #402;
1076 void VM_findchain (void)
1081 prvm_edict_t *ent, *chain;
1084 VM_SAFEPARMCOUNTRANGE(2,3,VM_findchain);
1087 chainfield = PRVM_G_INT(OFS_PARM2);
1089 chainfield = prog->fieldoffsets.chain;
1091 PRVM_ERROR("VM_findchain: %s doesnt have the specified chain field !", PRVM_NAME);
1093 chain = prog->edicts;
1095 f = PRVM_G_INT(OFS_PARM0);
1096 s = PRVM_G_STRING(OFS_PARM1);
1098 // LordHavoc: apparently BloodMage does a find(world, weaponmodel, "") and
1099 // expects it to find all the monsters, so we must be careful to support
1102 ent = PRVM_NEXT_EDICT(prog->edicts);
1103 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
1105 prog->xfunction->builtinsprofile++;
1106 if (ent->priv.required->free)
1108 t = PRVM_E_STRING(ent,f);
1114 PRVM_EDICTFIELDEDICT(ent,chainfield) = PRVM_NUM_FOR_EDICT(chain);
1118 VM_RETURN_EDICT(chain);
1125 entity findchainfloat(.string field, float match)
1126 entity findchainentity(.string field, entity match)
1129 // LordHavoc: chained search for float, int, and entity reference fields
1130 // entity(.string field, float match) findchainfloat = #403;
1131 void VM_findchainfloat (void)
1136 prvm_edict_t *ent, *chain;
1139 VM_SAFEPARMCOUNTRANGE(2, 3, VM_findchainfloat);
1142 chainfield = PRVM_G_INT(OFS_PARM2);
1144 chainfield = prog->fieldoffsets.chain;
1146 PRVM_ERROR("VM_findchain: %s doesnt have the specified chain field !", PRVM_NAME);
1148 chain = (prvm_edict_t *)prog->edicts;
1150 f = PRVM_G_INT(OFS_PARM0);
1151 s = PRVM_G_FLOAT(OFS_PARM1);
1153 ent = PRVM_NEXT_EDICT(prog->edicts);
1154 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
1156 prog->xfunction->builtinsprofile++;
1157 if (ent->priv.required->free)
1159 if (PRVM_E_FLOAT(ent,f) != s)
1162 PRVM_EDICTFIELDEDICT(ent,chainfield) = PRVM_EDICT_TO_PROG(chain);
1166 VM_RETURN_EDICT(chain);
1170 ========================
1173 entity findflags(entity start, .float field, float match)
1174 ========================
1176 // LordHavoc: search for flags in float fields
1177 void VM_findflags (void)
1184 VM_SAFEPARMCOUNT(3, VM_findflags);
1187 e = PRVM_G_EDICTNUM(OFS_PARM0);
1188 f = PRVM_G_INT(OFS_PARM1);
1189 s = (int)PRVM_G_FLOAT(OFS_PARM2);
1191 for (e++ ; e < prog->num_edicts ; e++)
1193 prog->xfunction->builtinsprofile++;
1194 ed = PRVM_EDICT_NUM(e);
1195 if (ed->priv.required->free)
1197 if (!PRVM_E_FLOAT(ed,f))
1199 if ((int)PRVM_E_FLOAT(ed,f) & s)
1201 VM_RETURN_EDICT(ed);
1206 VM_RETURN_EDICT(prog->edicts);
1210 ========================
1213 entity findchainflags(.float field, float match)
1214 ========================
1216 // LordHavoc: chained search for flags in float fields
1217 void VM_findchainflags (void)
1222 prvm_edict_t *ent, *chain;
1225 VM_SAFEPARMCOUNTRANGE(2, 3, VM_findchainflags);
1228 chainfield = PRVM_G_INT(OFS_PARM2);
1230 chainfield = prog->fieldoffsets.chain;
1232 PRVM_ERROR("VM_findchain: %s doesnt have the specified chain field !", PRVM_NAME);
1234 chain = (prvm_edict_t *)prog->edicts;
1236 f = PRVM_G_INT(OFS_PARM0);
1237 s = (int)PRVM_G_FLOAT(OFS_PARM1);
1239 ent = PRVM_NEXT_EDICT(prog->edicts);
1240 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
1242 prog->xfunction->builtinsprofile++;
1243 if (ent->priv.required->free)
1245 if (!PRVM_E_FLOAT(ent,f))
1247 if (!((int)PRVM_E_FLOAT(ent,f) & s))
1250 PRVM_EDICTFIELDEDICT(ent,chainfield) = PRVM_EDICT_TO_PROG(chain);
1254 VM_RETURN_EDICT(chain);
1261 string precache_sound (string sample)
1264 void VM_precache_sound (void)
1268 VM_SAFEPARMCOUNT(1, VM_precache_sound);
1270 s = PRVM_G_STRING(OFS_PARM0);
1271 PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
1272 //VM_CheckEmptyString(s);
1274 if(snd_initialized.integer && !S_PrecacheSound(s, true, true))
1276 VM_Warning("VM_precache_sound: Failed to load %s for %s\n", s, PRVM_NAME);
1285 returns the same string as output
1287 does nothing, only used by qcc to build .pak archives
1290 void VM_precache_file (void)
1292 VM_SAFEPARMCOUNT(1,VM_precache_file);
1293 // precache_file is only used to copy files with qcc, it does nothing
1294 PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
1304 void VM_coredump (void)
1306 VM_SAFEPARMCOUNT(0,VM_coredump);
1308 Cbuf_AddText("prvm_edicts ");
1309 Cbuf_AddText(PRVM_NAME);
1320 void PRVM_StackTrace(void);
1321 void VM_stackdump (void)
1323 VM_SAFEPARMCOUNT(0, VM_stackdump);
1338 VM_SAFEPARMCOUNT(0, VM_crash);
1340 PRVM_ERROR("Crash called by %s",PRVM_NAME);
1350 void VM_traceon (void)
1352 VM_SAFEPARMCOUNT(0,VM_traceon);
1364 void VM_traceoff (void)
1366 VM_SAFEPARMCOUNT(0,VM_traceoff);
1368 prog->trace = false;
1378 void VM_eprint (void)
1380 VM_SAFEPARMCOUNT(1,VM_eprint);
1382 PRVM_ED_PrintNum (PRVM_G_EDICTNUM(OFS_PARM0), NULL);
1395 VM_SAFEPARMCOUNT(1,VM_rint);
1397 f = PRVM_G_FLOAT(OFS_PARM0);
1399 PRVM_G_FLOAT(OFS_RETURN) = floor(f + 0.5);
1401 PRVM_G_FLOAT(OFS_RETURN) = ceil(f - 0.5);
1411 void VM_floor (void)
1413 VM_SAFEPARMCOUNT(1,VM_floor);
1415 PRVM_G_FLOAT(OFS_RETURN) = floor(PRVM_G_FLOAT(OFS_PARM0));
1427 VM_SAFEPARMCOUNT(1,VM_ceil);
1429 PRVM_G_FLOAT(OFS_RETURN) = ceil(PRVM_G_FLOAT(OFS_PARM0));
1437 entity nextent(entity)
1440 void VM_nextent (void)
1445 VM_SAFEPARMCOUNT(1, VM_nextent);
1447 i = PRVM_G_EDICTNUM(OFS_PARM0);
1450 prog->xfunction->builtinsprofile++;
1452 if (i == prog->num_edicts)
1454 VM_RETURN_EDICT(prog->edicts);
1457 ent = PRVM_EDICT_NUM(i);
1458 if (!ent->priv.required->free)
1460 VM_RETURN_EDICT(ent);
1466 //=============================================================================
1473 changelevel(string map)
1476 void VM_changelevel (void)
1478 VM_SAFEPARMCOUNT(1, VM_changelevel);
1482 VM_Warning("VM_changelevel: game is not server (%s)\n", PRVM_NAME);
1486 // make sure we don't issue two changelevels
1487 if (svs.changelevel_issued)
1489 svs.changelevel_issued = true;
1491 Cbuf_AddText (va("changelevel %s\n",PRVM_G_STRING(OFS_PARM0)));
1503 VM_SAFEPARMCOUNT(1,VM_sin);
1504 PRVM_G_FLOAT(OFS_RETURN) = sin(PRVM_G_FLOAT(OFS_PARM0));
1515 VM_SAFEPARMCOUNT(1,VM_cos);
1516 PRVM_G_FLOAT(OFS_RETURN) = cos(PRVM_G_FLOAT(OFS_PARM0));
1528 VM_SAFEPARMCOUNT(1,VM_sqrt);
1529 PRVM_G_FLOAT(OFS_RETURN) = sqrt(PRVM_G_FLOAT(OFS_PARM0));
1541 VM_SAFEPARMCOUNT(1,VM_asin);
1542 PRVM_G_FLOAT(OFS_RETURN) = asin(PRVM_G_FLOAT(OFS_PARM0));
1553 VM_SAFEPARMCOUNT(1,VM_acos);
1554 PRVM_G_FLOAT(OFS_RETURN) = acos(PRVM_G_FLOAT(OFS_PARM0));
1565 VM_SAFEPARMCOUNT(1,VM_atan);
1566 PRVM_G_FLOAT(OFS_RETURN) = atan(PRVM_G_FLOAT(OFS_PARM0));
1572 float atan2(float,float)
1575 void VM_atan2 (void)
1577 VM_SAFEPARMCOUNT(2,VM_atan2);
1578 PRVM_G_FLOAT(OFS_RETURN) = atan2(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1589 VM_SAFEPARMCOUNT(1,VM_tan);
1590 PRVM_G_FLOAT(OFS_RETURN) = tan(PRVM_G_FLOAT(OFS_PARM0));
1597 Returns a vector of length < 1 and > 0
1602 void VM_randomvec (void)
1607 VM_SAFEPARMCOUNT(0, VM_randomvec);
1612 temp[0] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1613 temp[1] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1614 temp[2] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1616 while (DotProduct(temp, temp) >= 1);
1617 VectorCopy (temp, PRVM_G_VECTOR(OFS_RETURN));
1620 temp[0] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1621 temp[1] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1622 temp[2] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1623 // length returned always > 0
1624 length = (rand()&32766 + 1) * (1.0 / 32767.0) / VectorLength(temp);
1625 VectorScale(temp,length, temp);*/
1626 //VectorCopy(temp, PRVM_G_VECTOR(OFS_RETURN));
1629 //=============================================================================
1635 float registercvar (string name, string value[, float flags])
1638 void VM_registercvar (void)
1640 const char *name, *value;
1643 VM_SAFEPARMCOUNTRANGE(2, 3, VM_registercvar);
1645 name = PRVM_G_STRING(OFS_PARM0);
1646 value = PRVM_G_STRING(OFS_PARM1);
1647 flags = prog->argc >= 3 ? (int)PRVM_G_FLOAT(OFS_PARM2) : 0;
1648 PRVM_G_FLOAT(OFS_RETURN) = 0;
1650 if(flags > CVAR_MAXFLAGSVAL)
1653 // first check to see if it has already been defined
1654 if (Cvar_FindVar (name))
1657 // check for overlap with a command
1658 if (Cmd_Exists (name))
1660 VM_Warning("VM_registercvar: %s is a command\n", name);
1664 Cvar_Get(name, value, flags, NULL);
1666 PRVM_G_FLOAT(OFS_RETURN) = 1; // success
1674 returns the minimum of two supplied floats
1676 float min(float a, float b, ...[float])
1681 VM_SAFEPARMCOUNTRANGE(2, 8, VM_min);
1682 // LordHavoc: 3+ argument enhancement suggested by FrikaC
1683 if (prog->argc >= 3)
1686 float f = PRVM_G_FLOAT(OFS_PARM0);
1687 for (i = 1;i < prog->argc;i++)
1688 if (f > PRVM_G_FLOAT((OFS_PARM0+i*3)))
1689 f = PRVM_G_FLOAT((OFS_PARM0+i*3));
1690 PRVM_G_FLOAT(OFS_RETURN) = f;
1693 PRVM_G_FLOAT(OFS_RETURN) = min(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1700 returns the maximum of two supplied floats
1702 float max(float a, float b, ...[float])
1707 VM_SAFEPARMCOUNTRANGE(2, 8, VM_max);
1708 // LordHavoc: 3+ argument enhancement suggested by FrikaC
1709 if (prog->argc >= 3)
1712 float f = PRVM_G_FLOAT(OFS_PARM0);
1713 for (i = 1;i < prog->argc;i++)
1714 if (f < PRVM_G_FLOAT((OFS_PARM0+i*3)))
1715 f = PRVM_G_FLOAT((OFS_PARM0+i*3));
1716 PRVM_G_FLOAT(OFS_RETURN) = f;
1719 PRVM_G_FLOAT(OFS_RETURN) = max(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1726 returns number bounded by supplied range
1728 float bound(float min, float value, float max)
1731 void VM_bound (void)
1733 VM_SAFEPARMCOUNT(3,VM_bound);
1734 PRVM_G_FLOAT(OFS_RETURN) = bound(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1), PRVM_G_FLOAT(OFS_PARM2));
1741 returns a raised to power b
1743 float pow(float a, float b)
1748 VM_SAFEPARMCOUNT(2,VM_pow);
1749 PRVM_G_FLOAT(OFS_RETURN) = pow(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1754 VM_SAFEPARMCOUNT(1,VM_log);
1755 PRVM_G_FLOAT(OFS_RETURN) = log(PRVM_G_FLOAT(OFS_PARM0));
1758 void VM_Files_Init(void)
1761 for (i = 0;i < PRVM_MAX_OPENFILES;i++)
1762 prog->openfiles[i] = NULL;
1765 void VM_Files_CloseAll(void)
1768 for (i = 0;i < PRVM_MAX_OPENFILES;i++)
1770 if (prog->openfiles[i])
1771 FS_Close(prog->openfiles[i]);
1772 prog->openfiles[i] = NULL;
1776 static qfile_t *VM_GetFileHandle( int index )
1778 if (index < 0 || index >= PRVM_MAX_OPENFILES)
1780 Con_Printf("VM_GetFileHandle: invalid file handle %i used in %s\n", index, PRVM_NAME);
1783 if (prog->openfiles[index] == NULL)
1785 Con_Printf("VM_GetFileHandle: no such file handle %i (or file has been closed) in %s\n", index, PRVM_NAME);
1788 return prog->openfiles[index];
1795 float fopen(string filename, float mode)
1798 // float(string filename, float mode) fopen = #110;
1799 // opens a file inside quake/gamedir/data/ (mode is FILE_READ, FILE_APPEND, or FILE_WRITE),
1800 // returns fhandle >= 0 if successful, or fhandle < 0 if unable to open file for any reason
1804 const char *modestring, *filename;
1806 VM_SAFEPARMCOUNT(2,VM_fopen);
1808 for (filenum = 0;filenum < PRVM_MAX_OPENFILES;filenum++)
1809 if (prog->openfiles[filenum] == NULL)
1811 if (filenum >= PRVM_MAX_OPENFILES)
1813 PRVM_G_FLOAT(OFS_RETURN) = -2;
1814 VM_Warning("VM_fopen: %s ran out of file handles (%i)\n", PRVM_NAME, PRVM_MAX_OPENFILES);
1817 filename = PRVM_G_STRING(OFS_PARM0);
1818 mode = (int)PRVM_G_FLOAT(OFS_PARM1);
1821 case 0: // FILE_READ
1823 prog->openfiles[filenum] = FS_OpenVirtualFile(va("data/%s", filename), false);
1824 if (prog->openfiles[filenum] == NULL)
1825 prog->openfiles[filenum] = FS_OpenVirtualFile(va("%s", filename), false);
1827 case 1: // FILE_APPEND
1829 prog->openfiles[filenum] = FS_OpenRealFile(va("data/%s", filename), modestring, false);
1831 case 2: // FILE_WRITE
1833 prog->openfiles[filenum] = FS_OpenRealFile(va("data/%s", filename), modestring, false);
1836 PRVM_G_FLOAT(OFS_RETURN) = -3;
1837 VM_Warning("VM_fopen: %s: no such mode %i (valid: 0 = read, 1 = append, 2 = write)\n", PRVM_NAME, mode);
1841 if (prog->openfiles[filenum] == NULL)
1843 PRVM_G_FLOAT(OFS_RETURN) = -1;
1844 if (developer_extra.integer)
1845 VM_Warning("VM_fopen: %s: %s mode %s failed\n", PRVM_NAME, filename, modestring);
1849 PRVM_G_FLOAT(OFS_RETURN) = filenum;
1850 if (developer_extra.integer)
1851 Con_DPrintf("VM_fopen: %s: %s mode %s opened as #%i\n", PRVM_NAME, filename, modestring, filenum);
1852 prog->openfiles_origin[filenum] = PRVM_AllocationOrigin();
1860 fclose(float fhandle)
1863 //void(float fhandle) fclose = #111; // closes a file
1864 void VM_fclose(void)
1868 VM_SAFEPARMCOUNT(1,VM_fclose);
1870 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1871 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
1873 VM_Warning("VM_fclose: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1876 if (prog->openfiles[filenum] == NULL)
1878 VM_Warning("VM_fclose: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1881 FS_Close(prog->openfiles[filenum]);
1882 prog->openfiles[filenum] = NULL;
1883 if(prog->openfiles_origin[filenum])
1884 PRVM_Free((char *)prog->openfiles_origin[filenum]);
1885 if (developer_extra.integer)
1886 Con_DPrintf("VM_fclose: %s: #%i closed\n", PRVM_NAME, filenum);
1893 string fgets(float fhandle)
1896 //string(float fhandle) fgets = #112; // reads a line of text from the file and returns as a tempstring
1900 char string[VM_STRINGTEMP_LENGTH];
1903 VM_SAFEPARMCOUNT(1,VM_fgets);
1905 // set the return value regardless of any possible errors
1906 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
1908 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1909 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
1911 VM_Warning("VM_fgets: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1914 if (prog->openfiles[filenum] == NULL)
1916 VM_Warning("VM_fgets: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1922 c = FS_Getc(prog->openfiles[filenum]);
1923 if (c == '\r' || c == '\n' || c < 0)
1925 if (end < VM_STRINGTEMP_LENGTH - 1)
1929 // remove \n following \r
1932 c = FS_Getc(prog->openfiles[filenum]);
1934 FS_UnGetc(prog->openfiles[filenum], (unsigned char)c);
1936 if (developer_extra.integer)
1937 Con_DPrintf("fgets: %s: %s\n", PRVM_NAME, string);
1939 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string);
1946 fputs(float fhandle, string s)
1949 //void(float fhandle, string s) fputs = #113; // writes a line of text to the end of the file
1953 char string[VM_STRINGTEMP_LENGTH];
1956 VM_SAFEPARMCOUNT(2,VM_fputs);
1958 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1959 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
1961 VM_Warning("VM_fputs: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1964 if (prog->openfiles[filenum] == NULL)
1966 VM_Warning("VM_fputs: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1969 VM_VarString(1, string, sizeof(string));
1970 if ((stringlength = (int)strlen(string)))
1971 FS_Write(prog->openfiles[filenum], string, stringlength);
1972 if (developer_extra.integer)
1973 Con_DPrintf("fputs: %s: %s\n", PRVM_NAME, string);
1980 writetofile(float fhandle, entity ent)
1983 void VM_writetofile(void)
1988 VM_SAFEPARMCOUNT(2, VM_writetofile);
1990 file = VM_GetFileHandle( (int)PRVM_G_FLOAT(OFS_PARM0) );
1993 VM_Warning("VM_writetofile: invalid or closed file handle\n");
1997 ent = PRVM_G_EDICT(OFS_PARM1);
1998 if(ent->priv.required->free)
2000 VM_Warning("VM_writetofile: %s: entity %i is free !\n", PRVM_NAME, PRVM_NUM_FOR_EDICT(ent));
2004 PRVM_ED_Write (file, ent);
2007 // KrimZon - DP_QC_ENTITYDATA
2012 float() numentityfields
2013 Return the number of entity fields - NOT offsets
2016 void VM_numentityfields(void)
2018 PRVM_G_FLOAT(OFS_RETURN) = prog->numfielddefs;
2021 // KrimZon - DP_QC_ENTITYDATA
2026 string(float fieldnum) entityfieldname
2027 Return name of the specified field as a string, or empty if the field is invalid (warning)
2030 void VM_entityfieldname(void)
2033 int i = (int)PRVM_G_FLOAT(OFS_PARM0);
2035 if (i < 0 || i >= prog->numfielddefs)
2037 VM_Warning("VM_entityfieldname: %s: field index out of bounds\n", PRVM_NAME);
2038 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString("");
2042 d = &prog->fielddefs[i];
2043 PRVM_G_INT(OFS_RETURN) = d->s_name; // presuming that s_name points to a string already
2046 // KrimZon - DP_QC_ENTITYDATA
2051 float(float fieldnum) entityfieldtype
2054 void VM_entityfieldtype(void)
2057 int i = (int)PRVM_G_FLOAT(OFS_PARM0);
2059 if (i < 0 || i >= prog->numfielddefs)
2061 VM_Warning("VM_entityfieldtype: %s: field index out of bounds\n", PRVM_NAME);
2062 PRVM_G_FLOAT(OFS_RETURN) = -1.0;
2066 d = &prog->fielddefs[i];
2067 PRVM_G_FLOAT(OFS_RETURN) = (float)d->type;
2070 // KrimZon - DP_QC_ENTITYDATA
2073 VM_getentityfieldstring
2075 string(float fieldnum, entity ent) getentityfieldstring
2078 void VM_getentityfieldstring(void)
2080 // put the data into a string
2085 int i = (int)PRVM_G_FLOAT(OFS_PARM0);
2087 if (i < 0 || i >= prog->numfielddefs)
2089 VM_Warning("VM_entityfielddata: %s: field index out of bounds\n", PRVM_NAME);
2090 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString("");
2094 d = &prog->fielddefs[i];
2097 ent = PRVM_G_EDICT(OFS_PARM1);
2098 if(ent->priv.required->free)
2100 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString("");
2101 VM_Warning("VM_entityfielddata: %s: entity %i is free !\n", PRVM_NAME, PRVM_NUM_FOR_EDICT(ent));
2104 v = (int *)((char *)ent->fields.vp + d->ofs*4);
2106 // if it's 0 or blank, return an empty string
2107 type = d->type & ~DEF_SAVEGLOBAL;
2108 for (j=0 ; j<prvm_type_size[type] ; j++)
2111 if (j == prvm_type_size[type])
2113 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString("");
2117 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(PRVM_UglyValueString((etype_t)d->type, (prvm_eval_t *)v));
2120 // KrimZon - DP_QC_ENTITYDATA
2123 VM_putentityfieldstring
2125 float(float fieldnum, entity ent, string s) putentityfieldstring
2128 void VM_putentityfieldstring(void)
2132 int i = (int)PRVM_G_FLOAT(OFS_PARM0);
2134 if (i < 0 || i >= prog->numfielddefs)
2136 VM_Warning("VM_entityfielddata: %s: field index out of bounds\n", PRVM_NAME);
2137 PRVM_G_FLOAT(OFS_RETURN) = 0.0f;
2141 d = &prog->fielddefs[i];
2144 ent = PRVM_G_EDICT(OFS_PARM1);
2145 if(ent->priv.required->free)
2147 VM_Warning("VM_entityfielddata: %s: entity %i is free !\n", PRVM_NAME, PRVM_NUM_FOR_EDICT(ent));
2148 PRVM_G_FLOAT(OFS_RETURN) = 0.0f;
2152 // parse the string into the value
2153 PRVM_G_FLOAT(OFS_RETURN) = ( PRVM_ED_ParseEpair(ent, d, PRVM_G_STRING(OFS_PARM2), false) ) ? 1.0f : 0.0f;
2160 float strlen(string s)
2163 //float(string s) strlen = #114; // returns how many characters are in a string
2164 void VM_strlen(void)
2166 VM_SAFEPARMCOUNT(1,VM_strlen);
2168 //PRVM_G_FLOAT(OFS_RETURN) = strlen(PRVM_G_STRING(OFS_PARM0));
2169 PRVM_G_FLOAT(OFS_RETURN) = u8_strlen(PRVM_G_STRING(OFS_PARM0));
2172 // DRESK - Decolorized String
2177 string strdecolorize(string s)
2180 // string (string s) strdecolorize = #472; // returns the passed in string with color codes stripped
2181 void VM_strdecolorize(void)
2183 char szNewString[VM_STRINGTEMP_LENGTH];
2184 const char *szString;
2187 VM_SAFEPARMCOUNT(1,VM_strdecolorize);
2188 szString = PRVM_G_STRING(OFS_PARM0);
2189 COM_StringDecolorize(szString, 0, szNewString, sizeof(szNewString), TRUE);
2190 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(szNewString);
2193 // DRESK - String Length (not counting color codes)
2198 float strlennocol(string s)
2201 // float(string s) strlennocol = #471; // returns how many characters are in a string not including color codes
2202 // For example, ^2Dresk returns a length of 5
2203 void VM_strlennocol(void)
2205 const char *szString;
2208 VM_SAFEPARMCOUNT(1,VM_strlennocol);
2210 szString = PRVM_G_STRING(OFS_PARM0);
2212 //nCnt = COM_StringLengthNoColors(szString, 0, NULL);
2213 nCnt = u8_COM_StringLengthNoColors(szString, 0, NULL);
2215 PRVM_G_FLOAT(OFS_RETURN) = nCnt;
2218 // DRESK - String to Uppercase and Lowercase
2223 string strtolower(string s)
2226 // string (string s) strtolower = #480; // returns passed in string in lowercase form
2227 void VM_strtolower(void)
2229 char szNewString[VM_STRINGTEMP_LENGTH];
2230 const char *szString;
2233 VM_SAFEPARMCOUNT(1,VM_strtolower);
2234 szString = PRVM_G_STRING(OFS_PARM0);
2236 COM_ToLowerString(szString, szNewString, sizeof(szNewString) );
2238 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(szNewString);
2245 string strtoupper(string s)
2248 // string (string s) strtoupper = #481; // returns passed in string in uppercase form
2249 void VM_strtoupper(void)
2251 char szNewString[VM_STRINGTEMP_LENGTH];
2252 const char *szString;
2255 VM_SAFEPARMCOUNT(1,VM_strtoupper);
2256 szString = PRVM_G_STRING(OFS_PARM0);
2258 COM_ToUpperString(szString, szNewString, sizeof(szNewString) );
2260 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(szNewString);
2267 string strcat(string,string,...[string])
2270 //string(string s1, string s2) strcat = #115;
2271 // concatenates two strings (for example "abc", "def" would return "abcdef")
2272 // and returns as a tempstring
2273 void VM_strcat(void)
2275 char s[VM_STRINGTEMP_LENGTH];
2276 VM_SAFEPARMCOUNTRANGE(1, 8, VM_strcat);
2278 VM_VarString(0, s, sizeof(s));
2279 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(s);
2286 string substring(string s, float start, float length)
2289 // string(string s, float start, float length) substring = #116;
2290 // returns a section of a string as a tempstring
2291 void VM_substring(void)
2294 int u_slength = 0, u_start;
2297 char string[VM_STRINGTEMP_LENGTH];
2299 VM_SAFEPARMCOUNT(3,VM_substring);
2302 s = PRVM_G_STRING(OFS_PARM0);
2303 start = (int)PRVM_G_FLOAT(OFS_PARM1);
2304 length = (int)PRVM_G_FLOAT(OFS_PARM2);
2305 slength = strlen(s);
2307 if (start < 0) // FTE_STRINGS feature
2309 start = bound(0, start, slength);
2311 if (length < 0) // FTE_STRINGS feature
2312 length += slength - start + 1;
2313 maxlen = min((int)sizeof(string) - 1, slength - start);
2314 length = bound(0, length, maxlen);
2316 memcpy(string, s + start, length);
2318 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string);
2321 s = PRVM_G_STRING(OFS_PARM0);
2322 start = (int)PRVM_G_FLOAT(OFS_PARM1);
2323 length = (int)PRVM_G_FLOAT(OFS_PARM2);
2325 if (start < 0) // FTE_STRINGS feature
2327 u_slength = u8_strlen(s);
2329 start = bound(0, start, u_slength);
2332 if (length < 0) // FTE_STRINGS feature
2334 if (!u_slength) // it's not calculated when it's not needed above
2335 u_slength = u8_strlen(s);
2336 length += u_slength - start + 1;
2339 // positive start, positive length
2340 u_start = u8_byteofs(s, start, NULL);
2343 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString("");
2346 u_length = u8_bytelen(s + u_start, length);
2347 if (u_length >= sizeof(string)-1)
2348 u_length = sizeof(string)-1;
2350 memcpy(string, s + u_start, u_length);
2351 string[u_length] = 0;
2352 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string);
2359 string(string search, string replace, string subject) strreplace = #484;
2362 // replaces all occurrences of search with replace in the string subject, and returns the result
2363 void VM_strreplace(void)
2366 const char *search, *replace, *subject;
2367 char string[VM_STRINGTEMP_LENGTH];
2368 int search_len, replace_len, subject_len;
2370 VM_SAFEPARMCOUNT(3,VM_strreplace);
2372 search = PRVM_G_STRING(OFS_PARM0);
2373 replace = PRVM_G_STRING(OFS_PARM1);
2374 subject = PRVM_G_STRING(OFS_PARM2);
2376 search_len = (int)strlen(search);
2377 replace_len = (int)strlen(replace);
2378 subject_len = (int)strlen(subject);
2381 for (i = 0; i <= subject_len - search_len; i++)
2383 for (j = 0; j < search_len; j++) // thus, i+j < subject_len
2384 if (subject[i+j] != search[j])
2386 if (j == search_len)
2388 // NOTE: if search_len == 0, we always hit THIS case, and never the other
2389 // found it at offset 'i'
2390 for (j = 0; j < replace_len && si < (int)sizeof(string) - 1; j++)
2391 string[si++] = replace[j];
2394 i += search_len - 1;
2398 // the above would subtract 1 from i... so we
2399 // don't do that, but instead output the next
2401 if (si < (int)sizeof(string) - 1)
2402 string[si++] = subject[i];
2407 // in THIS case, we know search_len > 0, thus i < subject_len
2409 if (si < (int)sizeof(string) - 1)
2410 string[si++] = subject[i];
2413 // remaining chars (these cannot match)
2414 for (; i < subject_len; i++)
2415 if (si < (int)sizeof(string) - 1)
2416 string[si++] = subject[i];
2419 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string);
2426 string(string search, string replace, string subject) strireplace = #485;
2429 // case-insensitive version of strreplace
2430 void VM_strireplace(void)
2433 const char *search, *replace, *subject;
2434 char string[VM_STRINGTEMP_LENGTH];
2435 int search_len, replace_len, subject_len;
2437 VM_SAFEPARMCOUNT(3,VM_strreplace);
2439 search = PRVM_G_STRING(OFS_PARM0);
2440 replace = PRVM_G_STRING(OFS_PARM1);
2441 subject = PRVM_G_STRING(OFS_PARM2);
2443 search_len = (int)strlen(search);
2444 replace_len = (int)strlen(replace);
2445 subject_len = (int)strlen(subject);
2448 for (i = 0; i <= subject_len - search_len; i++)
2450 for (j = 0; j < search_len; j++) // thus, i+j < subject_len
2451 if (tolower(subject[i+j]) != tolower(search[j]))
2453 if (j == search_len)
2455 // NOTE: if search_len == 0, we always hit THIS case, and never the other
2456 // found it at offset 'i'
2457 for (j = 0; j < replace_len && si < (int)sizeof(string) - 1; j++)
2458 string[si++] = replace[j];
2461 i += search_len - 1;
2465 // the above would subtract 1 from i... so we
2466 // don't do that, but instead output the next
2468 if (si < (int)sizeof(string) - 1)
2469 string[si++] = subject[i];
2474 // in THIS case, we know search_len > 0, thus i < subject_len
2476 if (si < (int)sizeof(string) - 1)
2477 string[si++] = subject[i];
2480 // remaining chars (these cannot match)
2481 for (; i < subject_len; i++)
2482 if (si < (int)sizeof(string) - 1)
2483 string[si++] = subject[i];
2486 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string);
2493 vector stov(string s)
2496 //vector(string s) stov = #117; // returns vector value from a string
2499 char string[VM_STRINGTEMP_LENGTH];
2501 VM_SAFEPARMCOUNT(1,VM_stov);
2503 VM_VarString(0, string, sizeof(string));
2504 Math_atov(string, PRVM_G_VECTOR(OFS_RETURN));
2511 string strzone(string s)
2514 //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)
2515 void VM_strzone(void)
2518 char string[VM_STRINGTEMP_LENGTH];
2521 VM_SAFEPARMCOUNT(1,VM_strzone);
2523 VM_VarString(0, string, sizeof(string));
2524 alloclen = strlen(string) + 1;
2525 PRVM_G_INT(OFS_RETURN) = PRVM_AllocString(alloclen, &out);
2526 memcpy(out, string, alloclen);
2536 //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!!!)
2537 void VM_strunzone(void)
2539 VM_SAFEPARMCOUNT(1,VM_strunzone);
2540 PRVM_FreeString(PRVM_G_INT(OFS_PARM0));
2545 VM_command (used by client and menu)
2547 clientcommand(float client, string s) (for client and menu)
2550 //void(entity e, string s) clientcommand = #440; // executes a command string as if it came from the specified client
2551 //this function originally written by KrimZon, made shorter by LordHavoc
2552 void VM_clcommand (void)
2554 client_t *temp_client;
2557 VM_SAFEPARMCOUNT(2,VM_clcommand);
2559 i = (int)PRVM_G_FLOAT(OFS_PARM0);
2560 if (!sv.active || i < 0 || i >= svs.maxclients || !svs.clients[i].active)
2562 VM_Warning("VM_clientcommand: %s: invalid client/server is not active !\n", PRVM_NAME);
2566 temp_client = host_client;
2567 host_client = svs.clients + i;
2568 Cmd_ExecuteString (PRVM_G_STRING(OFS_PARM1), src_client, true);
2569 host_client = temp_client;
2577 float tokenize(string s)
2580 //float(string s) tokenize = #441; // takes apart a string into individal words (access them with argv), returns how many
2581 //this function originally written by KrimZon, made shorter by LordHavoc
2582 //20040203: rewritten by LordHavoc (no longer uses allocations)
2583 static int num_tokens = 0;
2584 static int tokens[VM_STRINGTEMP_LENGTH / 2];
2585 static int tokens_startpos[VM_STRINGTEMP_LENGTH / 2];
2586 static int tokens_endpos[VM_STRINGTEMP_LENGTH / 2];
2587 static char tokenize_string[VM_STRINGTEMP_LENGTH];
2588 void VM_tokenize (void)
2592 VM_SAFEPARMCOUNT(1,VM_tokenize);
2594 strlcpy(tokenize_string, PRVM_G_STRING(OFS_PARM0), sizeof(tokenize_string));
2595 p = tokenize_string;
2600 if (num_tokens >= (int)(sizeof(tokens)/sizeof(tokens[0])))
2603 // skip whitespace here to find token start pos
2604 while(*p && ISWHITESPACE(*p))
2607 tokens_startpos[num_tokens] = p - tokenize_string;
2608 if(!COM_ParseToken_VM_Tokenize(&p, false))
2610 tokens_endpos[num_tokens] = p - tokenize_string;
2611 tokens[num_tokens] = PRVM_SetTempString(com_token);
2615 PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
2618 //float(string s) tokenize = #514; // takes apart a string into individal words (access them with argv), returns how many
2619 void VM_tokenize_console (void)
2623 VM_SAFEPARMCOUNT(1,VM_tokenize);
2625 strlcpy(tokenize_string, PRVM_G_STRING(OFS_PARM0), sizeof(tokenize_string));
2626 p = tokenize_string;
2631 if (num_tokens >= (int)(sizeof(tokens)/sizeof(tokens[0])))
2634 // skip whitespace here to find token start pos
2635 while(*p && ISWHITESPACE(*p))
2638 tokens_startpos[num_tokens] = p - tokenize_string;
2639 if(!COM_ParseToken_Console(&p))
2641 tokens_endpos[num_tokens] = p - tokenize_string;
2642 tokens[num_tokens] = PRVM_SetTempString(com_token);
2646 PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
2651 VM_tokenizebyseparator
2653 float tokenizebyseparator(string s, string separator1, ...)
2656 //float(string s, string separator1, ...) tokenizebyseparator = #479; // takes apart a string into individal words (access them with argv), returns how many
2657 //this function returns the token preceding each instance of a separator (of
2658 //which there can be multiple), and the text following the last separator
2659 //useful for parsing certain kinds of data like IP addresses
2661 //numnumbers = tokenizebyseparator("10.1.2.3", ".");
2662 //returns 4 and the tokens "10" "1" "2" "3".
2663 void VM_tokenizebyseparator (void)
2667 int separatorlen[7];
2668 const char *separators[7];
2671 char tokentext[MAX_INPUTLINE];
2673 VM_SAFEPARMCOUNTRANGE(2, 8,VM_tokenizebyseparator);
2675 strlcpy(tokenize_string, PRVM_G_STRING(OFS_PARM0), sizeof(tokenize_string));
2676 p = tokenize_string;
2679 for (j = 1;j < prog->argc;j++)
2681 // skip any blank separator strings
2682 const char *s = PRVM_G_STRING(OFS_PARM0+j*3);
2685 separators[numseparators] = s;
2686 separatorlen[numseparators] = strlen(s);
2693 while (num_tokens < (int)(sizeof(tokens)/sizeof(tokens[0])))
2695 token = tokentext + j;
2696 tokens_startpos[num_tokens] = p - tokenize_string;
2700 for (k = 0;k < numseparators;k++)
2702 if (!strncmp(p, separators[k], separatorlen[k]))
2704 p += separatorlen[k];
2708 if (k < numseparators)
2710 if (j < (int)sizeof(tokentext)-1)
2711 tokentext[j++] = *p;
2715 tokens_endpos[num_tokens] = p0 - tokenize_string;
2716 if (j >= (int)sizeof(tokentext))
2719 tokens[num_tokens++] = PRVM_SetTempString(token);
2724 PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
2727 //string(float n) argv = #442; // returns a word from the tokenized string (returns nothing for an invalid index)
2728 //this function originally written by KrimZon, made shorter by LordHavoc
2733 VM_SAFEPARMCOUNT(1,VM_argv);
2735 token_num = (int)PRVM_G_FLOAT(OFS_PARM0);
2738 token_num += num_tokens;
2740 if (token_num >= 0 && token_num < num_tokens)
2741 PRVM_G_INT(OFS_RETURN) = tokens[token_num];
2743 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
2746 //float(float n) argv_start_index = #515; // returns the start index of a token
2747 void VM_argv_start_index (void)
2751 VM_SAFEPARMCOUNT(1,VM_argv);
2753 token_num = (int)PRVM_G_FLOAT(OFS_PARM0);
2756 token_num += num_tokens;
2758 if (token_num >= 0 && token_num < num_tokens)
2759 PRVM_G_FLOAT(OFS_RETURN) = tokens_startpos[token_num];
2761 PRVM_G_FLOAT(OFS_RETURN) = -1;
2764 //float(float n) argv_end_index = #516; // returns the end index of a token
2765 void VM_argv_end_index (void)
2769 VM_SAFEPARMCOUNT(1,VM_argv);
2771 token_num = (int)PRVM_G_FLOAT(OFS_PARM0);
2774 token_num += num_tokens;
2776 if (token_num >= 0 && token_num < num_tokens)
2777 PRVM_G_FLOAT(OFS_RETURN) = tokens_endpos[token_num];
2779 PRVM_G_FLOAT(OFS_RETURN) = -1;
2789 void VM_isserver(void)
2791 VM_SAFEPARMCOUNT(0,VM_serverstate);
2793 PRVM_G_FLOAT(OFS_RETURN) = sv.active && (svs.maxclients > 1 || cls.state == ca_dedicated);
2803 void VM_clientcount(void)
2805 VM_SAFEPARMCOUNT(0,VM_clientcount);
2807 PRVM_G_FLOAT(OFS_RETURN) = svs.maxclients;
2817 void VM_clientstate(void)
2819 VM_SAFEPARMCOUNT(0,VM_clientstate);
2822 switch( cls.state ) {
2823 case ca_uninitialized:
2825 PRVM_G_FLOAT(OFS_RETURN) = 0;
2827 case ca_disconnected:
2828 PRVM_G_FLOAT(OFS_RETURN) = 1;
2831 PRVM_G_FLOAT(OFS_RETURN) = 2;
2834 // should never be reached!
2843 float getostype(void)
2845 */ // not used at the moment -> not included in the common list
2846 void VM_getostype(void)
2848 VM_SAFEPARMCOUNT(0,VM_getostype);
2853 OS_MAC - not supported
2857 PRVM_G_FLOAT(OFS_RETURN) = 0;
2858 #elif defined(MACOSX)
2859 PRVM_G_FLOAT(OFS_RETURN) = 2;
2861 PRVM_G_FLOAT(OFS_RETURN) = 1;
2872 extern double host_starttime;
2873 float CDAudio_GetPosition(void);
2874 void VM_gettime(void)
2878 VM_SAFEPARMCOUNTRANGE(0,1,VM_gettime);
2882 PRVM_G_FLOAT(OFS_RETURN) = (float) realtime;
2886 timer_index = (int) PRVM_G_FLOAT(OFS_PARM0);
2889 case 0: // GETTIME_FRAMESTART
2890 PRVM_G_FLOAT(OFS_RETURN) = (float) realtime;
2892 case 1: // GETTIME_REALTIME
2893 PRVM_G_FLOAT(OFS_RETURN) = (float) Sys_DoubleTime();
2895 case 2: // GETTIME_HIRES
2896 PRVM_G_FLOAT(OFS_RETURN) = (float) (Sys_DoubleTime() - realtime);
2898 case 3: // GETTIME_UPTIME
2899 PRVM_G_FLOAT(OFS_RETURN) = (float) (Sys_DoubleTime() - host_starttime);
2901 case 4: // GETTIME_CDTRACK
2902 PRVM_G_FLOAT(OFS_RETURN) = (float) CDAudio_GetPosition();
2905 VM_Warning("VM_gettime: %s: unsupported timer specified, returning realtime\n", PRVM_NAME);
2906 PRVM_G_FLOAT(OFS_RETURN) = (float) realtime;
2916 float getsoundtime(void)
2920 void VM_getsoundtime (void)
2922 int entnum, entchannel, pnum;
2923 VM_SAFEPARMCOUNT(2,VM_getsoundtime);
2925 pnum = PRVM_GetProgNr();
2926 if (pnum == PRVM_MENUPROG)
2928 VM_Warning("VM_getsoundtime: %s: not supported on this progs\n", PRVM_NAME);
2929 PRVM_G_FLOAT(OFS_RETURN) = -1;
2932 entnum = ((pnum == PRVM_CLIENTPROG) ? MAX_EDICTS : 0) + PRVM_NUM_FOR_EDICT(PRVM_G_EDICT(OFS_PARM0));
2933 entchannel = (int)PRVM_G_FLOAT(OFS_PARM1);
2934 entchannel = CHAN_USER2ENGINE(entchannel);
2935 if (!IS_CHAN(entchannel))
2936 VM_Warning("VM_getsoundtime: %s: bad channel %i\n", PRVM_NAME, entchannel);
2937 PRVM_G_FLOAT(OFS_RETURN) = (float)S_GetEntChannelPosition(entnum, entchannel);
2944 string soundlength (string sample)
2947 void VM_soundlength (void)
2951 VM_SAFEPARMCOUNT(1, VM_soundlength);
2953 s = PRVM_G_STRING(OFS_PARM0);
2954 PRVM_G_FLOAT(OFS_RETURN) = S_SoundLength(s);
2961 loadfromdata(string data)
2964 void VM_loadfromdata(void)
2966 VM_SAFEPARMCOUNT(1,VM_loadentsfromfile);
2968 PRVM_ED_LoadFromFile(PRVM_G_STRING(OFS_PARM0));
2972 ========================
2975 parseentitydata(entity ent, string data)
2976 ========================
2978 void VM_parseentitydata(void)
2983 VM_SAFEPARMCOUNT(2, VM_parseentitydata);
2985 // get edict and test it
2986 ent = PRVM_G_EDICT(OFS_PARM0);
2987 if (ent->priv.required->free)
2988 PRVM_ERROR ("VM_parseentitydata: %s: Can only set already spawned entities (entity %i is free)!", PRVM_NAME, PRVM_NUM_FOR_EDICT(ent));
2990 data = PRVM_G_STRING(OFS_PARM1);
2992 // parse the opening brace
2993 if (!COM_ParseToken_Simple(&data, false, false) || com_token[0] != '{' )
2994 PRVM_ERROR ("VM_parseentitydata: %s: Couldn't parse entity data:\n%s", PRVM_NAME, data );
2996 PRVM_ED_ParseEdict (data, ent);
3003 loadfromfile(string file)
3006 void VM_loadfromfile(void)
3008 const char *filename;
3011 VM_SAFEPARMCOUNT(1,VM_loadfromfile);
3013 filename = PRVM_G_STRING(OFS_PARM0);
3014 if (FS_CheckNastyPath(filename, false))
3016 PRVM_G_FLOAT(OFS_RETURN) = -4;
3017 VM_Warning("VM_loadfromfile: %s dangerous or non-portable filename \"%s\" not allowed. (contains : or \\ or begins with .. or /)\n", PRVM_NAME, filename);
3021 // not conform with VM_fopen
3022 data = (char *)FS_LoadFile(filename, tempmempool, false, NULL);
3024 PRVM_G_FLOAT(OFS_RETURN) = -1;
3026 PRVM_ED_LoadFromFile(data);
3037 float mod(float val, float m)
3040 void VM_modulo(void)
3043 VM_SAFEPARMCOUNT(2,VM_module);
3045 val = (int) PRVM_G_FLOAT(OFS_PARM0);
3046 m = (int) PRVM_G_FLOAT(OFS_PARM1);
3048 PRVM_G_FLOAT(OFS_RETURN) = (float) (val % m);
3051 void VM_Search_Init(void)
3054 for (i = 0;i < PRVM_MAX_OPENSEARCHES;i++)
3055 prog->opensearches[i] = NULL;
3058 void VM_Search_Reset(void)
3061 // reset the fssearch list
3062 for(i = 0; i < PRVM_MAX_OPENSEARCHES; i++)
3064 if(prog->opensearches[i])
3065 FS_FreeSearch(prog->opensearches[i]);
3066 prog->opensearches[i] = NULL;
3074 float search_begin(string pattern, float caseinsensitive, float quiet)
3077 void VM_search_begin(void)
3080 const char *pattern;
3081 int caseinsens, quiet;
3083 VM_SAFEPARMCOUNT(3, VM_search_begin);
3085 pattern = PRVM_G_STRING(OFS_PARM0);
3087 VM_CheckEmptyString(pattern);
3089 caseinsens = (int)PRVM_G_FLOAT(OFS_PARM1);
3090 quiet = (int)PRVM_G_FLOAT(OFS_PARM2);
3092 for(handle = 0; handle < PRVM_MAX_OPENSEARCHES; handle++)
3093 if(!prog->opensearches[handle])
3096 if(handle >= PRVM_MAX_OPENSEARCHES)
3098 PRVM_G_FLOAT(OFS_RETURN) = -2;
3099 VM_Warning("VM_search_begin: %s ran out of search handles (%i)\n", PRVM_NAME, PRVM_MAX_OPENSEARCHES);
3103 if(!(prog->opensearches[handle] = FS_Search(pattern,caseinsens, quiet)))
3104 PRVM_G_FLOAT(OFS_RETURN) = -1;
3107 prog->opensearches_origin[handle] = PRVM_AllocationOrigin();
3108 PRVM_G_FLOAT(OFS_RETURN) = handle;
3116 void search_end(float handle)
3119 void VM_search_end(void)
3122 VM_SAFEPARMCOUNT(1, VM_search_end);
3124 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
3126 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
3128 VM_Warning("VM_search_end: invalid handle %i used in %s\n", handle, PRVM_NAME);
3131 if(prog->opensearches[handle] == NULL)
3133 VM_Warning("VM_search_end: no such handle %i in %s\n", handle, PRVM_NAME);
3137 FS_FreeSearch(prog->opensearches[handle]);
3138 prog->opensearches[handle] = NULL;
3139 if(prog->opensearches_origin[handle])
3140 PRVM_Free((char *)prog->opensearches_origin[handle]);
3147 float search_getsize(float handle)
3150 void VM_search_getsize(void)
3153 VM_SAFEPARMCOUNT(1, VM_M_search_getsize);
3155 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
3157 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
3159 VM_Warning("VM_search_getsize: invalid handle %i used in %s\n", handle, PRVM_NAME);
3162 if(prog->opensearches[handle] == NULL)
3164 VM_Warning("VM_search_getsize: no such handle %i in %s\n", handle, PRVM_NAME);
3168 PRVM_G_FLOAT(OFS_RETURN) = prog->opensearches[handle]->numfilenames;
3173 VM_search_getfilename
3175 string search_getfilename(float handle, float num)
3178 void VM_search_getfilename(void)
3180 int handle, filenum;
3181 VM_SAFEPARMCOUNT(2, VM_search_getfilename);
3183 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
3184 filenum = (int)PRVM_G_FLOAT(OFS_PARM1);
3186 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
3188 VM_Warning("VM_search_getfilename: invalid handle %i used in %s\n", handle, PRVM_NAME);
3191 if(prog->opensearches[handle] == NULL)
3193 VM_Warning("VM_search_getfilename: no such handle %i in %s\n", handle, PRVM_NAME);
3196 if(filenum < 0 || filenum >= prog->opensearches[handle]->numfilenames)
3198 VM_Warning("VM_search_getfilename: invalid filenum %i in %s\n", filenum, PRVM_NAME);
3202 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog->opensearches[handle]->filenames[filenum]);
3209 string chr(float ascii)
3216 VM_SAFEPARMCOUNT(1, VM_chr);
3218 tmp[0] = (unsigned char) PRVM_G_FLOAT(OFS_PARM0);
3221 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(tmp);
3226 VM_SAFEPARMCOUNT(1, VM_chr);
3228 len = u8_fromchar((Uchar)PRVM_G_FLOAT(OFS_PARM0), tmp, sizeof(tmp));
3230 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(tmp);
3233 //=============================================================================
3234 // Draw builtins (client & menu)
3240 float iscachedpic(string pic)
3243 void VM_iscachedpic(void)
3245 VM_SAFEPARMCOUNT(1,VM_iscachedpic);
3247 // drawq hasnt such a function, thus always return true
3248 PRVM_G_FLOAT(OFS_RETURN) = false;
3255 string precache_pic(string pic)
3258 void VM_precache_pic(void)
3262 VM_SAFEPARMCOUNT(1, VM_precache_pic);
3264 s = PRVM_G_STRING(OFS_PARM0);
3265 PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
3266 VM_CheckEmptyString (s);
3268 // AK Draw_CachePic is supposed to always return a valid pointer
3269 if( Draw_CachePic_Flags(s, 0)->tex == r_texture_notexture )
3270 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
3280 void VM_freepic(void)
3284 VM_SAFEPARMCOUNT(1,VM_freepic);
3286 s = PRVM_G_STRING(OFS_PARM0);
3287 VM_CheckEmptyString (s);
3292 void getdrawfontscale(float *sx, float *sy)
3296 VectorCopy(PRVM_drawglobalvector(drawfontscale), v);
3297 if(VectorLength2(v) > 0)
3304 dp_font_t *getdrawfont(void)
3306 int f = (int) PRVM_drawglobalfloat(drawfont);
3307 if(f < 0 || f >= dp_fonts.maxsize)
3308 return FONT_DEFAULT;
3309 return &dp_fonts.f[f];
3316 float drawcharacter(vector position, float character, vector scale, vector rgb, float alpha, float flag)
3319 void VM_drawcharacter(void)
3321 float *pos,*scale,*rgb;
3325 VM_SAFEPARMCOUNT(6,VM_drawcharacter);
3327 character = (char) PRVM_G_FLOAT(OFS_PARM1);
3330 PRVM_G_FLOAT(OFS_RETURN) = -1;
3331 VM_Warning("VM_drawcharacter: %s passed null character !\n",PRVM_NAME);
3335 pos = PRVM_G_VECTOR(OFS_PARM0);
3336 scale = PRVM_G_VECTOR(OFS_PARM2);
3337 rgb = PRVM_G_VECTOR(OFS_PARM3);
3338 flag = (int)PRVM_G_FLOAT(OFS_PARM5);
3340 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3342 PRVM_G_FLOAT(OFS_RETURN) = -2;
3343 VM_Warning("VM_drawcharacter: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
3347 if(pos[2] || scale[2])
3348 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")));
3350 if(!scale[0] || !scale[1])
3352 PRVM_G_FLOAT(OFS_RETURN) = -3;
3353 VM_Warning("VM_drawcharacter: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
3357 getdrawfontscale(&sx, &sy);
3358 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());
3359 PRVM_G_FLOAT(OFS_RETURN) = 1;
3366 float drawstring(vector position, string text, vector scale, vector rgb, float alpha[, float flag])
3369 void VM_drawstring(void)
3371 float *pos,*scale,*rgb;
3375 VM_SAFEPARMCOUNTRANGE(5,6,VM_drawstring);
3377 string = PRVM_G_STRING(OFS_PARM1);
3378 pos = PRVM_G_VECTOR(OFS_PARM0);
3379 scale = PRVM_G_VECTOR(OFS_PARM2);
3380 rgb = PRVM_G_VECTOR(OFS_PARM3);
3381 if (prog->argc >= 6)
3382 flag = (int)PRVM_G_FLOAT(OFS_PARM5);
3384 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3386 PRVM_G_FLOAT(OFS_RETURN) = -2;
3387 VM_Warning("VM_drawstring: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
3391 if(!scale[0] || !scale[1])
3393 PRVM_G_FLOAT(OFS_RETURN) = -3;
3394 VM_Warning("VM_drawstring: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
3398 if(pos[2] || scale[2])
3399 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")));
3401 getdrawfontscale(&sx, &sy);
3402 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());
3403 //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);
3404 PRVM_G_FLOAT(OFS_RETURN) = 1;
3409 VM_drawcolorcodedstring
3411 float drawcolorcodedstring(vector position, string text, vector scale, float alpha, float flag)
3413 float drawcolorcodedstring(vector position, string text, vector scale, vector rgb, float alpha, float flag)
3416 void VM_drawcolorcodedstring(void)
3422 float sx, sy, alpha;
3424 VM_SAFEPARMCOUNTRANGE(5,6,VM_drawcolorcodedstring);
3426 if (prog->argc == 6) // full 6 parms, like normal drawstring
3428 pos = PRVM_G_VECTOR(OFS_PARM0);
3429 string = PRVM_G_STRING(OFS_PARM1);
3430 scale = PRVM_G_VECTOR(OFS_PARM2);
3431 VectorCopy(PRVM_G_VECTOR(OFS_PARM3), rgb);
3432 alpha = PRVM_G_FLOAT(OFS_PARM4);
3433 flag = (int)PRVM_G_FLOAT(OFS_PARM5);
3437 pos = PRVM_G_VECTOR(OFS_PARM0);
3438 string = PRVM_G_STRING(OFS_PARM1);
3439 scale = PRVM_G_VECTOR(OFS_PARM2);
3443 alpha = PRVM_G_FLOAT(OFS_PARM3);
3444 flag = (int)PRVM_G_FLOAT(OFS_PARM4);
3447 if(flag < DRAWFLAG_NORMAL || flag >= DRAWFLAG_NUMFLAGS)
3449 PRVM_G_FLOAT(OFS_RETURN) = -2;
3450 VM_Warning("VM_drawcolorcodedstring: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
3454 if(!scale[0] || !scale[1])
3456 PRVM_G_FLOAT(OFS_RETURN) = -3;
3457 VM_Warning("VM_drawcolorcodedstring: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
3461 if(pos[2] || scale[2])
3462 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")));
3464 getdrawfontscale(&sx, &sy);
3465 DrawQ_String_Scale(pos[0], pos[1], string, 0, scale[0], scale[1], sx, sy, rgb[0], rgb[1], rgb[2], alpha, flag, NULL, false, getdrawfont());
3466 if (prog->argc == 6) // also return vector of last color
3467 VectorCopy(DrawQ_Color, PRVM_G_VECTOR(OFS_RETURN));
3469 PRVM_G_FLOAT(OFS_RETURN) = 1;
3475 float stringwidth(string text, float allowColorCodes, float size)
3478 void VM_stringwidth(void)
3482 float mult; // sz is intended font size so we can later add freetype support, mult is font size multiplier in pixels per character cell
3486 VM_SAFEPARMCOUNTRANGE(2,3,VM_drawstring);
3488 getdrawfontscale(&sx, &sy);
3491 szv = PRVM_G_VECTOR(OFS_PARM2);
3496 // we want the width for 8x8 font size, divided by 8
3497 static float defsize[] = {8, 8};
3500 // to make sure snapping is turned off, ALWAYS use a nontrivial scale in this case
3501 if(sx >= 0.9 && sx <= 1.1)
3509 string = PRVM_G_STRING(OFS_PARM0);
3510 colors = (int)PRVM_G_FLOAT(OFS_PARM1);
3512 PRVM_G_FLOAT(OFS_RETURN) = DrawQ_TextWidth_UntilWidth_TrackColors_Scale(string, &maxlen, szv[0], szv[1], sx, sy, NULL, !colors, getdrawfont(), 1000000000) * mult;
3516 mult = sz = PRVM_G_FLOAT(OFS_PARM2);
3524 string = PRVM_G_STRING(OFS_PARM0);
3525 colors = (int)PRVM_G_FLOAT(OFS_PARM1);
3527 PRVM_G_FLOAT(OFS_RETURN) = DrawQ_TextWidth(string, 0, !colors, getdrawfont()) * mult; // 1x1 characters, don't actually draw
3535 float findfont(string s)
3539 float getdrawfontnum(const char *fontname)
3543 for(i = 0; i < dp_fonts.maxsize; ++i)
3544 if(!strcmp(dp_fonts.f[i].title, fontname))
3549 void VM_findfont(void)
3551 VM_SAFEPARMCOUNT(1,VM_findfont);
3552 PRVM_G_FLOAT(OFS_RETURN) = getdrawfontnum(PRVM_G_STRING(OFS_PARM0));
3559 float loadfont(string fontname, string fontmaps, string sizes, float slot)
3563 dp_font_t *FindFont(const char *title, qboolean allocate_new);
3564 void LoadFont(qboolean override, const char *name, dp_font_t *fnt, float scale, float voffset);
3565 void VM_loadfont(void)
3567 const char *fontname, *filelist, *sizes, *c, *cm;
3568 char mainfont[MAX_QPATH];
3570 float sz, scale, voffset;
3573 VM_SAFEPARMCOUNTRANGE(3,6,VM_loadfont);
3575 fontname = PRVM_G_STRING(OFS_PARM0);
3577 fontname = "default";
3579 filelist = PRVM_G_STRING(OFS_PARM1);
3581 filelist = "gfx/conchars";
3583 sizes = PRVM_G_STRING(OFS_PARM2);
3589 if (prog->argc >= 4)
3591 i = PRVM_G_FLOAT(OFS_PARM3);
3592 if (i >= 0 && i < dp_fonts.maxsize)
3595 strlcpy(f->title, fontname, sizeof(f->title)); // replace name
3599 f = FindFont(fontname, true);
3602 PRVM_G_FLOAT(OFS_RETURN) = -1;
3603 return; // something go wrong
3606 memset(f->fallbacks, 0, sizeof(f->fallbacks));
3607 memset(f->fallback_faces, 0, sizeof(f->fallback_faces));
3609 // first font is handled "normally"
3610 c = strchr(filelist, ':');
3611 cm = strchr(filelist, ',');
3612 if(c && (!cm || c < cm))
3613 f->req_face = atoi(c+1);
3619 if(!c || (c - filelist) > MAX_QPATH)
3620 strlcpy(mainfont, filelist, sizeof(mainfont));
3623 memcpy(mainfont, filelist, c - filelist);
3624 mainfont[c - filelist] = 0;
3628 for(i = 0; i < MAX_FONT_FALLBACKS; ++i)
3630 c = strchr(filelist, ',');
3636 c = strchr(filelist, ':');
3637 cm = strchr(filelist, ',');
3638 if(c && (!cm || c < cm))
3639 f->fallback_faces[i] = atoi(c+1);
3642 f->fallback_faces[i] = 0; // f->req_face; could make it stick to the default-font's face index
3645 if(!c || (c-filelist) > MAX_QPATH)
3647 strlcpy(f->fallbacks[i], filelist, sizeof(mainfont));
3651 memcpy(f->fallbacks[i], filelist, c - filelist);
3652 f->fallbacks[i][c - filelist] = 0;
3657 for(i = 0; i < MAX_FONT_SIZES; ++i)
3658 f->req_sizes[i] = -1;
3659 for (numsizes = 0,c = sizes;;)
3661 if (!COM_ParseToken_VM_Tokenize(&c, 0))
3663 sz = atof(com_token);
3665 if (sz < 0.001f || sz > 1000.0f)
3667 VM_Warning("VM_loadfont: crap size %s", com_token);
3671 if (numsizes == MAX_FONT_SIZES)
3673 VM_Warning("VM_loadfont: MAX_FONT_SIZES = %i exceeded", MAX_FONT_SIZES);
3676 f->req_sizes[numsizes] = sz;
3680 // additional scale/hoffset parms
3683 if (prog->argc >= 5)
3685 scale = PRVM_G_FLOAT(OFS_PARM4);
3689 if (prog->argc >= 6)
3690 voffset = PRVM_G_FLOAT(OFS_PARM5);
3693 LoadFont(true, mainfont, f, scale, voffset);
3695 // return index of loaded font
3696 PRVM_G_FLOAT(OFS_RETURN) = (f - dp_fonts.f);
3703 float drawpic(vector position, string pic, vector size, vector rgb, float alpha, float flag)
3706 void VM_drawpic(void)
3708 const char *picname;
3709 float *size, *pos, *rgb;
3712 VM_SAFEPARMCOUNTRANGE(5,6,VM_drawpic);
3714 picname = PRVM_G_STRING(OFS_PARM1);
3715 VM_CheckEmptyString (picname);
3717 // is pic cached ? no function yet for that
3720 PRVM_G_FLOAT(OFS_RETURN) = -4;
3721 VM_Warning("VM_drawpic: %s: %s not cached !\n", PRVM_NAME, picname);
3725 pos = PRVM_G_VECTOR(OFS_PARM0);
3726 size = PRVM_G_VECTOR(OFS_PARM2);
3727 rgb = PRVM_G_VECTOR(OFS_PARM3);
3728 if (prog->argc >= 6)
3729 flag = (int) PRVM_G_FLOAT(OFS_PARM5);
3731 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3733 PRVM_G_FLOAT(OFS_RETURN) = -2;
3734 VM_Warning("VM_drawpic: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
3738 if(pos[2] || size[2])
3739 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")));
3741 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);
3742 PRVM_G_FLOAT(OFS_RETURN) = 1;
3748 float drawrotpic(vector position, string pic, vector size, vector org, float angle, vector rgb, float alpha, float flag)
3751 void VM_drawrotpic(void)
3753 const char *picname;
3754 float *size, *pos, *org, *rgb;
3757 VM_SAFEPARMCOUNT(8,VM_drawrotpic);
3759 picname = PRVM_G_STRING(OFS_PARM1);
3760 VM_CheckEmptyString (picname);
3762 // is pic cached ? no function yet for that
3765 PRVM_G_FLOAT(OFS_RETURN) = -4;
3766 VM_Warning("VM_drawrotpic: %s: %s not cached !\n", PRVM_NAME, picname);
3770 pos = PRVM_G_VECTOR(OFS_PARM0);
3771 size = PRVM_G_VECTOR(OFS_PARM2);
3772 org = PRVM_G_VECTOR(OFS_PARM3);
3773 rgb = PRVM_G_VECTOR(OFS_PARM5);
3774 flag = (int) PRVM_G_FLOAT(OFS_PARM7);
3776 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3778 PRVM_G_FLOAT(OFS_RETURN) = -2;
3779 VM_Warning("VM_drawrotpic: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
3783 if(pos[2] || size[2] || org[2])
3784 Con_Printf("VM_drawrotpic: z value from pos/size/org discarded\n");
3786 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);
3787 PRVM_G_FLOAT(OFS_RETURN) = 1;
3793 float drawsubpic(vector position, vector size, string pic, vector srcPos, vector srcSize, vector rgb, float alpha, float flag)
3797 void VM_drawsubpic(void)
3799 const char *picname;
3800 float *size, *pos, *rgb, *srcPos, *srcSize, alpha;
3803 VM_SAFEPARMCOUNT(8,VM_drawsubpic);
3805 picname = PRVM_G_STRING(OFS_PARM2);
3806 VM_CheckEmptyString (picname);
3808 // is pic cached ? no function yet for that
3811 PRVM_G_FLOAT(OFS_RETURN) = -4;
3812 VM_Warning("VM_drawsubpic: %s: %s not cached !\n", PRVM_NAME, picname);
3816 pos = PRVM_G_VECTOR(OFS_PARM0);
3817 size = PRVM_G_VECTOR(OFS_PARM1);
3818 srcPos = PRVM_G_VECTOR(OFS_PARM3);
3819 srcSize = PRVM_G_VECTOR(OFS_PARM4);
3820 rgb = PRVM_G_VECTOR(OFS_PARM5);
3821 alpha = PRVM_G_FLOAT(OFS_PARM6);
3822 flag = (int) PRVM_G_FLOAT(OFS_PARM7);
3824 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3826 PRVM_G_FLOAT(OFS_RETURN) = -2;
3827 VM_Warning("VM_drawsubpic: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
3831 if(pos[2] || size[2])
3832 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")));
3834 DrawQ_SuperPic(pos[0], pos[1], Draw_CachePic_Flags (picname, CACHEPICFLAG_NOTPERSISTENT),
3836 srcPos[0], srcPos[1], rgb[0], rgb[1], rgb[2], alpha,
3837 srcPos[0] + srcSize[0], srcPos[1], rgb[0], rgb[1], rgb[2], alpha,
3838 srcPos[0], srcPos[1] + srcSize[1], rgb[0], rgb[1], rgb[2], alpha,
3839 srcPos[0] + srcSize[0], srcPos[1] + srcSize[1], rgb[0], rgb[1], rgb[2], alpha,
3841 PRVM_G_FLOAT(OFS_RETURN) = 1;
3848 float drawfill(vector position, vector size, vector rgb, float alpha, float flag)
3851 void VM_drawfill(void)
3853 float *size, *pos, *rgb;
3856 VM_SAFEPARMCOUNT(5,VM_drawfill);
3859 pos = PRVM_G_VECTOR(OFS_PARM0);
3860 size = PRVM_G_VECTOR(OFS_PARM1);
3861 rgb = PRVM_G_VECTOR(OFS_PARM2);
3862 flag = (int) PRVM_G_FLOAT(OFS_PARM4);
3864 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3866 PRVM_G_FLOAT(OFS_RETURN) = -2;
3867 VM_Warning("VM_drawfill: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
3871 if(pos[2] || size[2])
3872 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")));
3874 DrawQ_Fill(pos[0], pos[1], size[0], size[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM3), flag);
3875 PRVM_G_FLOAT(OFS_RETURN) = 1;
3882 drawsetcliparea(float x, float y, float width, float height)
3885 void VM_drawsetcliparea(void)
3888 VM_SAFEPARMCOUNT(4,VM_drawsetcliparea);
3890 x = bound(0, PRVM_G_FLOAT(OFS_PARM0), vid_conwidth.integer);
3891 y = bound(0, PRVM_G_FLOAT(OFS_PARM1), vid_conheight.integer);
3892 w = bound(0, PRVM_G_FLOAT(OFS_PARM2) + PRVM_G_FLOAT(OFS_PARM0) - x, (vid_conwidth.integer - x));
3893 h = bound(0, PRVM_G_FLOAT(OFS_PARM3) + PRVM_G_FLOAT(OFS_PARM1) - y, (vid_conheight.integer - y));
3895 DrawQ_SetClipArea(x, y, w, h);
3900 VM_drawresetcliparea
3905 void VM_drawresetcliparea(void)
3907 VM_SAFEPARMCOUNT(0,VM_drawresetcliparea);
3909 DrawQ_ResetClipArea();
3916 vector getimagesize(string pic)
3919 void VM_getimagesize(void)
3924 VM_SAFEPARMCOUNT(1,VM_getimagesize);
3926 p = PRVM_G_STRING(OFS_PARM0);
3927 VM_CheckEmptyString (p);
3929 pic = Draw_CachePic_Flags (p, CACHEPICFLAG_NOTPERSISTENT);
3931 PRVM_G_VECTOR(OFS_RETURN)[0] = pic->width;
3932 PRVM_G_VECTOR(OFS_RETURN)[1] = pic->height;
3933 PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
3940 string keynumtostring(float keynum)
3943 void VM_keynumtostring (void)
3945 VM_SAFEPARMCOUNT(1, VM_keynumtostring);
3947 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(Key_KeynumToString((int)PRVM_G_FLOAT(OFS_PARM0)));
3952 VM_findkeysforcommand
3954 string findkeysforcommand(string command, float bindmap)
3956 the returned string is an altstring
3959 #define FKFC_NUMKEYS 5
3960 void M_FindKeysForCommand(const char *command, int *keys);
3961 void VM_findkeysforcommand(void)
3964 char ret[VM_STRINGTEMP_LENGTH];
3965 int keys[FKFC_NUMKEYS];
3969 VM_SAFEPARMCOUNTRANGE(1, 2, VM_findkeysforcommand);
3971 cmd = PRVM_G_STRING(OFS_PARM0);
3973 bindmap = bound(-1, PRVM_G_FLOAT(OFS_PARM1), MAX_BINDMAPS-1);
3975 bindmap = 0; // consistent to "bind"
3977 VM_CheckEmptyString(cmd);
3979 Key_FindKeysForCommand(cmd, keys, FKFC_NUMKEYS, bindmap);
3982 for(i = 0; i < FKFC_NUMKEYS; i++)
3983 strlcat(ret, va(" \'%i\'", keys[i]), sizeof(ret));
3985 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(ret);
3992 float stringtokeynum(string key)
3995 void VM_stringtokeynum (void)
3997 VM_SAFEPARMCOUNT( 1, VM_keynumtostring );
3999 PRVM_G_FLOAT(OFS_RETURN) = Key_StringToKeynum(PRVM_G_STRING(OFS_PARM0));
4006 string getkeybind(float key, float bindmap)
4009 void VM_getkeybind (void)
4012 VM_SAFEPARMCOUNTRANGE(1, 2, VM_CL_getkeybind);
4014 bindmap = bound(-1, PRVM_G_FLOAT(OFS_PARM1), MAX_BINDMAPS-1);
4016 bindmap = 0; // consistent to "bind"
4018 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(Key_GetBind((int)PRVM_G_FLOAT(OFS_PARM0), bindmap));
4025 float setkeybind(float key, string cmd, float bindmap)
4028 void VM_setkeybind (void)
4031 VM_SAFEPARMCOUNTRANGE(2, 3, VM_CL_setkeybind);
4033 bindmap = bound(-1, PRVM_G_FLOAT(OFS_PARM2), MAX_BINDMAPS-1);
4035 bindmap = 0; // consistent to "bind"
4037 PRVM_G_FLOAT(OFS_RETURN) = 0;
4038 if(Key_SetBinding((int)PRVM_G_FLOAT(OFS_PARM0), bindmap, PRVM_G_STRING(OFS_PARM1)))
4039 PRVM_G_FLOAT(OFS_RETURN) = 1;
4046 vector getbindmaps()
4049 void VM_getbindmaps (void)
4052 VM_SAFEPARMCOUNT(0, VM_CL_getbindmap);
4053 Key_GetBindMap(&fg, &bg);
4054 PRVM_G_VECTOR(OFS_RETURN)[0] = fg;
4055 PRVM_G_VECTOR(OFS_RETURN)[1] = bg;
4056 PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
4063 float setbindmaps(vector bindmap)
4066 void VM_setbindmaps (void)
4068 VM_SAFEPARMCOUNT(1, VM_CL_setbindmap);
4069 PRVM_G_FLOAT(OFS_RETURN) = 0;
4070 if(PRVM_G_VECTOR(OFS_PARM0)[2] == 0)
4071 if(Key_SetBindMap((int)PRVM_G_VECTOR(OFS_PARM0)[0], (int)PRVM_G_VECTOR(OFS_PARM0)[1]))
4072 PRVM_G_FLOAT(OFS_RETURN) = 1;
4075 // CL_Video interface functions
4078 ========================
4081 float cin_open(string file, string name)
4082 ========================
4084 void VM_cin_open( void )
4089 VM_SAFEPARMCOUNT( 2, VM_cin_open );
4091 file = PRVM_G_STRING( OFS_PARM0 );
4092 name = PRVM_G_STRING( OFS_PARM1 );
4094 VM_CheckEmptyString( file );
4095 VM_CheckEmptyString( name );
4097 if( CL_OpenVideo( file, name, MENUOWNER, "" ) )
4098 PRVM_G_FLOAT( OFS_RETURN ) = 1;
4100 PRVM_G_FLOAT( OFS_RETURN ) = 0;
4104 ========================
4107 void cin_close(string name)
4108 ========================
4110 void VM_cin_close( void )
4114 VM_SAFEPARMCOUNT( 1, VM_cin_close );
4116 name = PRVM_G_STRING( OFS_PARM0 );
4117 VM_CheckEmptyString( name );
4119 CL_CloseVideo( CL_GetVideoByName( name ) );
4123 ========================
4125 void cin_setstate(string name, float type)
4126 ========================
4128 void VM_cin_setstate( void )
4131 clvideostate_t state;
4134 VM_SAFEPARMCOUNT( 2, VM_cin_netstate );
4136 name = PRVM_G_STRING( OFS_PARM0 );
4137 VM_CheckEmptyString( name );
4139 state = (clvideostate_t)((int)PRVM_G_FLOAT( OFS_PARM1 ));
4141 video = CL_GetVideoByName( name );
4142 if( video && state > CLVIDEO_UNUSED && state < CLVIDEO_STATECOUNT )
4143 CL_SetVideoState( video, state );
4147 ========================
4150 float cin_getstate(string name)
4151 ========================
4153 void VM_cin_getstate( void )
4158 VM_SAFEPARMCOUNT( 1, VM_cin_getstate );
4160 name = PRVM_G_STRING( OFS_PARM0 );
4161 VM_CheckEmptyString( name );
4163 video = CL_GetVideoByName( name );
4165 PRVM_G_FLOAT( OFS_RETURN ) = (int)video->state;
4167 PRVM_G_FLOAT( OFS_RETURN ) = 0;
4171 ========================
4174 void cin_restart(string name)
4175 ========================
4177 void VM_cin_restart( void )
4182 VM_SAFEPARMCOUNT( 1, VM_cin_restart );
4184 name = PRVM_G_STRING( OFS_PARM0 );
4185 VM_CheckEmptyString( name );
4187 video = CL_GetVideoByName( name );
4189 CL_RestartVideo( video );
4193 ========================
4195 ========================
4197 void VM_Gecko_Init( void ) {
4202 ========================
4204 ========================
4206 void VM_Gecko_Destroy( void ) {
4211 ========================
4214 float[bool] gecko_create( string name )
4215 ========================
4217 void VM_gecko_create( void ) {
4219 PRVM_G_FLOAT( OFS_RETURN ) = 0;
4223 ========================
4226 void gecko_destroy( string name )
4227 ========================
4229 void VM_gecko_destroy( void ) {
4234 ========================
4237 void gecko_navigate( string name, string URI )
4238 ========================
4240 void VM_gecko_navigate( void ) {
4245 ========================
4248 float[bool] gecko_keyevent( string name, float key, float eventtype )
4249 ========================
4251 void VM_gecko_keyevent( void ) {
4253 PRVM_G_FLOAT( OFS_RETURN ) = 0;
4257 ========================
4260 void gecko_mousemove( string name, float x, float y )
4261 ========================
4263 void VM_gecko_movemouse( void ) {
4269 ========================
4272 void gecko_resize( string name, float w, float h )
4273 ========================
4275 void VM_gecko_resize( void ) {
4281 ========================
4282 VM_gecko_get_texture_extent
4284 vector gecko_get_texture_extent( string name )
4285 ========================
4287 void VM_gecko_get_texture_extent( void ) {
4289 PRVM_G_VECTOR(OFS_RETURN)[0] = 0;
4290 PRVM_G_VECTOR(OFS_RETURN)[1] = 0;
4299 Writes new values for v_forward, v_up, and v_right based on angles
4300 void makevectors(vector angle)
4303 void VM_makevectors (void)
4305 VM_SAFEPARMCOUNT(1, VM_makevectors);
4306 AngleVectors(PRVM_G_VECTOR(OFS_PARM0), PRVM_gameglobalvector(v_forward), PRVM_gameglobalvector(v_right), PRVM_gameglobalvector(v_up));
4313 Writes new values for v_forward, v_up, and v_right based on the given forward vector
4314 vectorvectors(vector)
4317 void VM_vectorvectors (void)
4319 VM_SAFEPARMCOUNT(1, VM_vectorvectors);
4320 VectorNormalize2(PRVM_G_VECTOR(OFS_PARM0), PRVM_gameglobalvector(v_forward));
4321 VectorVectors(PRVM_gameglobalvector(v_forward), PRVM_gameglobalvector(v_right), PRVM_gameglobalvector(v_up));
4325 ========================
4328 void drawline(float width, vector pos1, vector pos2, vector rgb, float alpha, float flags)
4329 ========================
4331 void VM_drawline (void)
4333 float *c1, *c2, *rgb;
4335 unsigned char flags;
4337 VM_SAFEPARMCOUNT(6, VM_drawline);
4338 width = PRVM_G_FLOAT(OFS_PARM0);
4339 c1 = PRVM_G_VECTOR(OFS_PARM1);
4340 c2 = PRVM_G_VECTOR(OFS_PARM2);
4341 rgb = PRVM_G_VECTOR(OFS_PARM3);
4342 alpha = PRVM_G_FLOAT(OFS_PARM4);
4343 flags = (int)PRVM_G_FLOAT(OFS_PARM5);
4344 DrawQ_Line(width, c1[0], c1[1], c2[0], c2[1], rgb[0], rgb[1], rgb[2], alpha, flags);
4347 // float(float number, float quantity) bitshift (EXT_BITSHIFT)
4348 void VM_bitshift (void)
4351 VM_SAFEPARMCOUNT(2, VM_bitshift);
4353 n1 = (int)fabs((float)((int)PRVM_G_FLOAT(OFS_PARM0)));
4354 n2 = (int)PRVM_G_FLOAT(OFS_PARM1);
4356 PRVM_G_FLOAT(OFS_RETURN) = n1;
4359 PRVM_G_FLOAT(OFS_RETURN) = (n1 >> -n2);
4361 PRVM_G_FLOAT(OFS_RETURN) = (n1 << n2);
4364 ////////////////////////////////////////
4365 // AltString functions
4366 ////////////////////////////////////////
4369 ========================
4372 float altstr_count(string)
4373 ========================
4375 void VM_altstr_count( void )
4377 const char *altstr, *pos;
4380 VM_SAFEPARMCOUNT( 1, VM_altstr_count );
4382 altstr = PRVM_G_STRING( OFS_PARM0 );
4383 //VM_CheckEmptyString( altstr );
4385 for( count = 0, pos = altstr ; *pos ; pos++ ) {
4386 if( *pos == '\\' ) {
4390 } else if( *pos == '\'' ) {
4395 PRVM_G_FLOAT( OFS_RETURN ) = (float) (count / 2);
4399 ========================
4402 string altstr_prepare(string)
4403 ========================
4405 void VM_altstr_prepare( void )
4408 const char *instr, *in;
4410 char outstr[VM_STRINGTEMP_LENGTH];
4412 VM_SAFEPARMCOUNT( 1, VM_altstr_prepare );
4414 instr = PRVM_G_STRING( OFS_PARM0 );
4416 for( out = outstr, in = instr, size = sizeof(outstr) - 1 ; size && *in ; size--, in++, out++ )
4425 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
4429 ========================
4432 string altstr_get(string, float)
4433 ========================
4435 void VM_altstr_get( void )
4437 const char *altstr, *pos;
4440 char outstr[VM_STRINGTEMP_LENGTH];
4442 VM_SAFEPARMCOUNT( 2, VM_altstr_get );
4444 altstr = PRVM_G_STRING( OFS_PARM0 );
4446 count = (int)PRVM_G_FLOAT( OFS_PARM1 );
4447 count = count * 2 + 1;
4449 for( pos = altstr ; *pos && count ; pos++ )
4450 if( *pos == '\\' ) {
4453 } else if( *pos == '\'' )
4457 PRVM_G_INT( OFS_RETURN ) = 0;
4461 for( out = outstr, size = sizeof(outstr) - 1 ; size && *pos ; size--, pos++, out++ )
4462 if( *pos == '\\' ) {
4467 } else if( *pos == '\'' )
4473 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
4477 ========================
4480 string altstr_set(string altstr, float num, string set)
4481 ========================
4483 void VM_altstr_set( void )
4486 const char *altstr, *str;
4489 char outstr[VM_STRINGTEMP_LENGTH];
4491 VM_SAFEPARMCOUNT( 3, VM_altstr_set );
4493 altstr = PRVM_G_STRING( OFS_PARM0 );
4495 num = (int)PRVM_G_FLOAT( OFS_PARM1 );
4497 str = PRVM_G_STRING( OFS_PARM2 );
4500 for( num = num * 2 + 1, in = altstr; *in && num; *out++ = *in++ )
4505 } else if( *in == '\'' ) {
4510 for( ; *str; *out++ = *str++ );
4511 // now jump over the old content
4513 if( *in == '\'' || (*in == '\\' && !*++in) )
4516 strlcpy(out, in, outstr + sizeof(outstr) - out);
4517 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
4521 ========================
4524 string altstr_ins(string altstr, float num, string set)
4525 ========================
4527 void VM_altstr_ins(void)
4533 char outstr[VM_STRINGTEMP_LENGTH];
4535 VM_SAFEPARMCOUNT(3, VM_altstr_ins);
4537 in = PRVM_G_STRING( OFS_PARM0 );
4538 num = (int)PRVM_G_FLOAT( OFS_PARM1 );
4539 set = PRVM_G_STRING( OFS_PARM2 );
4542 for( num = num * 2 + 2 ; *in && num > 0 ; *out++ = *in++ )
4547 } else if( *in == '\'' ) {
4552 for( ; *set ; *out++ = *set++ );
4555 strlcpy(out, in, outstr + sizeof(outstr) - out);
4556 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
4560 ////////////////////////////////////////
4561 // BufString functions
4562 ////////////////////////////////////////
4563 //[515]: string buffers support
4565 static size_t stringbuffers_sortlength;
4567 static void BufStr_Expand(prvm_stringbuffer_t *stringbuffer, int strindex)
4569 if (stringbuffer->max_strings <= strindex)
4571 char **oldstrings = stringbuffer->strings;
4572 stringbuffer->max_strings = max(stringbuffer->max_strings * 2, 128);
4573 while (stringbuffer->max_strings <= strindex)
4574 stringbuffer->max_strings *= 2;
4575 stringbuffer->strings = (char **) Mem_Alloc(prog->progs_mempool, stringbuffer->max_strings * sizeof(stringbuffer->strings[0]));
4576 if (stringbuffer->num_strings > 0)
4577 memcpy(stringbuffer->strings, oldstrings, stringbuffer->num_strings * sizeof(stringbuffer->strings[0]));
4579 Mem_Free(oldstrings);
4583 static void BufStr_Shrink(prvm_stringbuffer_t *stringbuffer)
4585 // reduce num_strings if there are empty string slots at the end
4586 while (stringbuffer->num_strings > 0 && stringbuffer->strings[stringbuffer->num_strings - 1] == NULL)
4587 stringbuffer->num_strings--;
4589 // if empty, free the string pointer array
4590 if (stringbuffer->num_strings == 0)
4592 stringbuffer->max_strings = 0;
4593 if (stringbuffer->strings)
4594 Mem_Free(stringbuffer->strings);
4595 stringbuffer->strings = NULL;
4599 static int BufStr_SortStringsUP (const void *in1, const void *in2)
4602 a = *((const char **) in1);
4603 b = *((const char **) in2);
4604 if(!a || !a[0]) return 1;
4605 if(!b || !b[0]) return -1;
4606 return strncmp(a, b, stringbuffers_sortlength);
4609 static int BufStr_SortStringsDOWN (const void *in1, const void *in2)
4612 a = *((const char **) in1);
4613 b = *((const char **) in2);
4614 if(!a || !a[0]) return 1;
4615 if(!b || !b[0]) return -1;
4616 return strncmp(b, a, stringbuffers_sortlength);
4620 ========================
4622 creates new buffer, and returns it's index, returns -1 if failed
4623 float buf_create(void) = #460;
4624 float newbuf(string format, float flags) = #460;
4625 ========================
4628 void VM_buf_create (void)
4630 prvm_stringbuffer_t *stringbuffer;
4633 VM_SAFEPARMCOUNTRANGE(0, 2, VM_buf_create);
4635 // VorteX: optional parm1 (buffer format) is unfinished, to keep intact with future databuffers extension must be set to "string"
4636 if(prog->argc >= 1 && strcmp(PRVM_G_STRING(OFS_PARM0), "string"))
4638 PRVM_G_FLOAT(OFS_RETURN) = -1;
4641 stringbuffer = (prvm_stringbuffer_t *) Mem_ExpandableArray_AllocRecord(&prog->stringbuffersarray);
4642 for (i = 0;stringbuffer != Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, i);i++);
4643 stringbuffer->origin = PRVM_AllocationOrigin();
4644 // optional flags parm
4645 if (prog->argc >= 2)
4646 stringbuffer->flags = (int)PRVM_G_FLOAT(OFS_PARM1) & 0xFF;
4647 PRVM_G_FLOAT(OFS_RETURN) = i;
4653 ========================
4655 deletes buffer and all strings in it
4656 void buf_del(float bufhandle) = #461;
4657 ========================
4659 void VM_buf_del (void)
4661 prvm_stringbuffer_t *stringbuffer;
4662 VM_SAFEPARMCOUNT(1, VM_buf_del);
4663 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4667 for (i = 0;i < stringbuffer->num_strings;i++)
4668 if (stringbuffer->strings[i])
4669 Mem_Free(stringbuffer->strings[i]);
4670 if (stringbuffer->strings)
4671 Mem_Free(stringbuffer->strings);
4672 if(stringbuffer->origin)
4673 PRVM_Free((char *)stringbuffer->origin);
4674 Mem_ExpandableArray_FreeRecord(&prog->stringbuffersarray, stringbuffer);
4678 VM_Warning("VM_buf_del: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4684 ========================
4686 how many strings are stored in buffer
4687 float buf_getsize(float bufhandle) = #462;
4688 ========================
4690 void VM_buf_getsize (void)
4692 prvm_stringbuffer_t *stringbuffer;
4693 VM_SAFEPARMCOUNT(1, VM_buf_getsize);
4695 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4698 PRVM_G_FLOAT(OFS_RETURN) = -1;
4699 VM_Warning("VM_buf_getsize: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4703 PRVM_G_FLOAT(OFS_RETURN) = stringbuffer->num_strings;
4707 ========================
4709 copy all content from one buffer to another, make sure it exists
4710 void buf_copy(float bufhandle_from, float bufhandle_to) = #463;
4711 ========================
4713 void VM_buf_copy (void)
4715 prvm_stringbuffer_t *srcstringbuffer, *dststringbuffer;
4717 VM_SAFEPARMCOUNT(2, VM_buf_copy);
4719 srcstringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4720 if(!srcstringbuffer)
4722 VM_Warning("VM_buf_copy: invalid source buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4725 i = (int)PRVM_G_FLOAT(OFS_PARM1);
4726 if(i == (int)PRVM_G_FLOAT(OFS_PARM0))
4728 VM_Warning("VM_buf_copy: source == destination (%i) in %s\n", i, PRVM_NAME);
4731 dststringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4732 if(!dststringbuffer)
4734 VM_Warning("VM_buf_copy: invalid destination buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM1), PRVM_NAME);
4738 for (i = 0;i < dststringbuffer->num_strings;i++)
4739 if (dststringbuffer->strings[i])
4740 Mem_Free(dststringbuffer->strings[i]);
4741 if (dststringbuffer->strings)
4742 Mem_Free(dststringbuffer->strings);
4743 *dststringbuffer = *srcstringbuffer;
4744 if (dststringbuffer->max_strings)
4745 dststringbuffer->strings = (char **)Mem_Alloc(prog->progs_mempool, sizeof(dststringbuffer->strings[0]) * dststringbuffer->max_strings);
4747 for (i = 0;i < dststringbuffer->num_strings;i++)
4749 if (srcstringbuffer->strings[i])
4752 stringlen = strlen(srcstringbuffer->strings[i]) + 1;
4753 dststringbuffer->strings[i] = (char *)Mem_Alloc(prog->progs_mempool, stringlen);
4754 memcpy(dststringbuffer->strings[i], srcstringbuffer->strings[i], stringlen);
4760 ========================
4762 sort buffer by beginnings of strings (cmplength defaults it's length)
4763 "backward == TRUE" means that sorting goes upside-down
4764 void buf_sort(float bufhandle, float cmplength, float backward) = #464;
4765 ========================
4767 void VM_buf_sort (void)
4769 prvm_stringbuffer_t *stringbuffer;
4770 VM_SAFEPARMCOUNT(3, VM_buf_sort);
4772 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4775 VM_Warning("VM_buf_sort: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4778 if(stringbuffer->num_strings <= 0)
4780 VM_Warning("VM_buf_sort: tried to sort empty buffer %i in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4783 stringbuffers_sortlength = (int)PRVM_G_FLOAT(OFS_PARM1);
4784 if(stringbuffers_sortlength <= 0)
4785 stringbuffers_sortlength = 0x7FFFFFFF;
4787 if(!PRVM_G_FLOAT(OFS_PARM2))
4788 qsort(stringbuffer->strings, stringbuffer->num_strings, sizeof(char*), BufStr_SortStringsUP);
4790 qsort(stringbuffer->strings, stringbuffer->num_strings, sizeof(char*), BufStr_SortStringsDOWN);
4792 BufStr_Shrink(stringbuffer);
4796 ========================
4798 concantenates all buffer string into one with "glue" separator and returns it as tempstring
4799 string buf_implode(float bufhandle, string glue) = #465;
4800 ========================
4802 void VM_buf_implode (void)
4804 prvm_stringbuffer_t *stringbuffer;
4805 char k[VM_STRINGTEMP_LENGTH];
4809 VM_SAFEPARMCOUNT(2, VM_buf_implode);
4811 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4812 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
4815 VM_Warning("VM_buf_implode: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4818 if(!stringbuffer->num_strings)
4820 sep = PRVM_G_STRING(OFS_PARM1);
4822 for(l = i = 0;i < stringbuffer->num_strings;i++)
4824 if(stringbuffer->strings[i])
4826 l += (i > 0 ? strlen(sep) : 0) + strlen(stringbuffer->strings[i]);
4827 if (l >= sizeof(k) - 1)
4829 strlcat(k, sep, sizeof(k));
4830 strlcat(k, stringbuffer->strings[i], sizeof(k));
4833 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(k);
4837 ========================
4839 get a string from buffer, returns tempstring, dont str_unzone it!
4840 string bufstr_get(float bufhandle, float string_index) = #465;
4841 ========================
4843 void VM_bufstr_get (void)
4845 prvm_stringbuffer_t *stringbuffer;
4847 VM_SAFEPARMCOUNT(2, VM_bufstr_get);
4849 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
4850 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4853 VM_Warning("VM_bufstr_get: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4856 strindex = (int)PRVM_G_FLOAT(OFS_PARM1);
4859 // VM_Warning("VM_bufstr_get: invalid string index %i used in %s\n", strindex, PRVM_NAME);
4862 if (strindex < stringbuffer->num_strings && stringbuffer->strings[strindex])
4863 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(stringbuffer->strings[strindex]);
4867 ========================
4869 copies a string into selected slot of buffer
4870 void bufstr_set(float bufhandle, float string_index, string str) = #466;
4871 ========================
4873 void VM_bufstr_set (void)
4877 prvm_stringbuffer_t *stringbuffer;
4880 VM_SAFEPARMCOUNT(3, VM_bufstr_set);
4882 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4885 VM_Warning("VM_bufstr_set: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4888 strindex = (int)PRVM_G_FLOAT(OFS_PARM1);
4889 if(strindex < 0 || strindex >= 1000000) // huge number of strings
4891 VM_Warning("VM_bufstr_set: invalid string index %i used in %s\n", strindex, PRVM_NAME);
4895 BufStr_Expand(stringbuffer, strindex);
4896 stringbuffer->num_strings = max(stringbuffer->num_strings, strindex + 1);
4898 if(stringbuffer->strings[strindex])
4899 Mem_Free(stringbuffer->strings[strindex]);
4900 stringbuffer->strings[strindex] = NULL;
4902 if(PRVM_G_INT(OFS_PARM2))
4904 // not the NULL string!
4905 news = PRVM_G_STRING(OFS_PARM2);
4906 alloclen = strlen(news) + 1;
4907 stringbuffer->strings[strindex] = (char *)Mem_Alloc(prog->progs_mempool, alloclen);
4908 memcpy(stringbuffer->strings[strindex], news, alloclen);
4911 BufStr_Shrink(stringbuffer);
4915 ========================
4917 adds string to buffer in first free slot and returns its index
4918 "order == TRUE" means that string will be added after last "full" slot
4919 float bufstr_add(float bufhandle, string str, float order) = #467;
4920 ========================
4922 void VM_bufstr_add (void)
4924 int order, strindex;
4925 prvm_stringbuffer_t *stringbuffer;
4929 VM_SAFEPARMCOUNT(3, VM_bufstr_add);
4931 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4932 PRVM_G_FLOAT(OFS_RETURN) = -1;
4935 VM_Warning("VM_bufstr_add: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4938 if(!PRVM_G_INT(OFS_PARM1)) // NULL string
4940 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);
4943 string = PRVM_G_STRING(OFS_PARM1);
4944 order = (int)PRVM_G_FLOAT(OFS_PARM2);
4946 strindex = stringbuffer->num_strings;
4948 for (strindex = 0;strindex < stringbuffer->num_strings;strindex++)
4949 if (stringbuffer->strings[strindex] == NULL)
4952 BufStr_Expand(stringbuffer, strindex);
4954 stringbuffer->num_strings = max(stringbuffer->num_strings, strindex + 1);
4955 alloclen = strlen(string) + 1;
4956 stringbuffer->strings[strindex] = (char *)Mem_Alloc(prog->progs_mempool, alloclen);
4957 memcpy(stringbuffer->strings[strindex], string, alloclen);
4959 PRVM_G_FLOAT(OFS_RETURN) = strindex;
4963 ========================
4965 delete string from buffer
4966 void bufstr_free(float bufhandle, float string_index) = #468;
4967 ========================
4969 void VM_bufstr_free (void)
4972 prvm_stringbuffer_t *stringbuffer;
4973 VM_SAFEPARMCOUNT(2, VM_bufstr_free);
4975 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4978 VM_Warning("VM_bufstr_free: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4981 i = (int)PRVM_G_FLOAT(OFS_PARM1);
4984 VM_Warning("VM_bufstr_free: invalid string index %i used in %s\n", i, PRVM_NAME);
4988 if (i < stringbuffer->num_strings)
4990 if(stringbuffer->strings[i])
4991 Mem_Free(stringbuffer->strings[i]);
4992 stringbuffer->strings[i] = NULL;
4995 BufStr_Shrink(stringbuffer);
5004 void VM_buf_cvarlist(void)
5007 const char *partial, *antipartial;
5008 size_t len, antilen;
5010 qboolean ispattern, antiispattern;
5012 prvm_stringbuffer_t *stringbuffer;
5013 VM_SAFEPARMCOUNTRANGE(2, 3, VM_buf_cvarlist);
5015 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
5018 VM_Warning("VM_bufstr_free: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
5022 partial = PRVM_G_STRING(OFS_PARM1);
5026 len = strlen(partial);
5029 antipartial = PRVM_G_STRING(OFS_PARM2);
5035 antilen = strlen(antipartial);
5037 for (n = 0;n < stringbuffer->num_strings;n++)
5038 if (stringbuffer->strings[n])
5039 Mem_Free(stringbuffer->strings[n]);
5040 if (stringbuffer->strings)
5041 Mem_Free(stringbuffer->strings);
5042 stringbuffer->strings = NULL;
5044 ispattern = partial && (strchr(partial, '*') || strchr(partial, '?'));
5045 antiispattern = antipartial && (strchr(antipartial, '*') || strchr(antipartial, '?'));
5048 for(cvar = cvar_vars; cvar; cvar = cvar->next)
5050 if(len && (ispattern ? !matchpattern_with_separator(cvar->name, partial, false, "", false) : strncmp(partial, cvar->name, len)))
5053 if(antilen && (antiispattern ? matchpattern_with_separator(cvar->name, antipartial, false, "", false) : !strncmp(antipartial, cvar->name, antilen)))
5059 stringbuffer->max_strings = stringbuffer->num_strings = n;
5060 if (stringbuffer->max_strings)
5061 stringbuffer->strings = (char **)Mem_Alloc(prog->progs_mempool, sizeof(stringbuffer->strings[0]) * stringbuffer->max_strings);
5064 for(cvar = cvar_vars; cvar; cvar = cvar->next)
5066 if(len && (ispattern ? !matchpattern_with_separator(cvar->name, partial, false, "", false) : strncmp(partial, cvar->name, len)))
5069 if(antilen && (antiispattern ? matchpattern_with_separator(cvar->name, antipartial, false, "", false) : !strncmp(antipartial, cvar->name, antilen)))
5072 alloclen = strlen(cvar->name) + 1;
5073 stringbuffer->strings[n] = (char *)Mem_Alloc(prog->progs_mempool, alloclen);
5074 memcpy(stringbuffer->strings[n], cvar->name, alloclen);
5089 This was a major timewaster in progs, so it was converted to C
5092 void VM_changeyaw (void)
5095 float ideal, current, move, speed;
5097 // this is called (VERY HACKISHLY) by SV_MoveToGoal, so it can not use any
5098 // parameters because they are the parameters to SV_MoveToGoal, not this
5099 //VM_SAFEPARMCOUNT(0, VM_changeyaw);
5101 ent = PRVM_PROG_TO_EDICT(PRVM_gameglobaledict(self));
5102 if (ent == prog->edicts)
5104 VM_Warning("changeyaw: can not modify world entity\n");
5107 if (ent->priv.server->free)
5109 VM_Warning("changeyaw: can not modify free entity\n");
5112 current = PRVM_gameedictvector(ent, angles)[1];
5113 current = ANGLEMOD(current);
5114 ideal = PRVM_gameedictfloat(ent, ideal_yaw);
5115 speed = PRVM_gameedictfloat(ent, yaw_speed);
5117 if (current == ideal)
5119 move = ideal - current;
5120 if (ideal > current)
5142 PRVM_gameedictvector(ent, angles)[1] = ANGLEMOD(current);
5150 void VM_changepitch (void)
5153 float ideal, current, move, speed;
5155 VM_SAFEPARMCOUNT(1, VM_changepitch);
5157 ent = PRVM_G_EDICT(OFS_PARM0);
5158 if (ent == prog->edicts)
5160 VM_Warning("changepitch: can not modify world entity\n");
5163 if (ent->priv.server->free)
5165 VM_Warning("changepitch: can not modify free entity\n");
5168 current = PRVM_gameedictvector(ent, angles)[0];
5169 current = ANGLEMOD(current);
5170 ideal = PRVM_gameedictfloat(ent, idealpitch);
5171 speed = PRVM_gameedictfloat(ent, pitch_speed);
5173 if (current == ideal)
5175 move = ideal - current;
5176 if (ideal > current)
5198 PRVM_gameedictvector(ent, angles)[0] = ANGLEMOD(current);
5202 void VM_uncolorstring (void)
5204 char szNewString[VM_STRINGTEMP_LENGTH];
5205 const char *szString;
5208 VM_SAFEPARMCOUNT(1, VM_uncolorstring);
5209 szString = PRVM_G_STRING(OFS_PARM0);
5210 COM_StringDecolorize(szString, 0, szNewString, sizeof(szNewString), TRUE);
5211 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(szNewString);
5215 // #221 float(string str, string sub[, float startpos]) strstrofs (FTE_STRINGS)
5216 //strstr, without generating a new string. Use in conjunction with FRIK_FILE's substring for more similar strstr.
5217 void VM_strstrofs (void)
5219 const char *instr, *match;
5221 VM_SAFEPARMCOUNTRANGE(2, 3, VM_strstrofs);
5222 instr = PRVM_G_STRING(OFS_PARM0);
5223 match = PRVM_G_STRING(OFS_PARM1);
5224 firstofs = (prog->argc > 2)?(int)PRVM_G_FLOAT(OFS_PARM2):0;
5225 firstofs = u8_bytelen(instr, firstofs);
5227 if (firstofs && (firstofs < 0 || firstofs > (int)strlen(instr)))
5229 PRVM_G_FLOAT(OFS_RETURN) = -1;
5233 match = strstr(instr+firstofs, match);
5235 PRVM_G_FLOAT(OFS_RETURN) = -1;
5237 PRVM_G_FLOAT(OFS_RETURN) = u8_strnlen(instr, match-instr);
5240 //#222 string(string s, float index) str2chr (FTE_STRINGS)
5241 void VM_str2chr (void)
5246 VM_SAFEPARMCOUNT(2, VM_str2chr);
5247 s = PRVM_G_STRING(OFS_PARM0);
5248 index = u8_bytelen(s, (int)PRVM_G_FLOAT(OFS_PARM1));
5250 if((unsigned)index < strlen(s))
5252 if (utf8_enable.integer)
5253 ch = u8_getchar_noendptr(s + index);
5255 ch = (unsigned char)s[index];
5256 PRVM_G_FLOAT(OFS_RETURN) = ch;
5259 PRVM_G_FLOAT(OFS_RETURN) = 0;
5262 //#223 string(float c, ...) chr2str (FTE_STRINGS)
5263 void VM_chr2str (void)
5268 VM_SAFEPARMCOUNTRANGE(0, 8, VM_chr2str);
5269 for(i = 0;i < prog->argc && i < (int)sizeof(t) - 1;i++)
5270 t[i] = (unsigned char)PRVM_G_FLOAT(OFS_PARM0+i*3);
5272 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(t);
5277 VM_SAFEPARMCOUNTRANGE(0, 8, VM_chr2str);
5278 for(i = 0; i < prog->argc && len < sizeof(t)-1; ++i)
5279 len += u8_fromchar((Uchar)PRVM_G_FLOAT(OFS_PARM0+i*3), t + len, sizeof(t)-1);
5281 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(t);
5284 static int chrconv_number(int i, int base, int conv)
5309 static int chrconv_punct(int i, int base, int conv)
5327 static int chrchar_alpha(int i, int basec, int baset, int convc, int convt, int charnum)
5329 //convert case and colour seperatly...
5346 baset = 128*((charnum&1) == (convt-5));
5362 return i + basec + baset;
5364 // #224 string(float ccase, float calpha, float cnum, string s, ...) strconv (FTE_STRINGS)
5365 //bulk convert a string. change case or colouring.
5366 void VM_strconv (void)
5368 int ccase, redalpha, rednum, len, i;
5369 unsigned char resbuf[VM_STRINGTEMP_LENGTH];
5370 unsigned char *result = resbuf;
5372 VM_SAFEPARMCOUNTRANGE(3, 8, VM_strconv);
5374 ccase = (int) PRVM_G_FLOAT(OFS_PARM0); //0 same, 1 lower, 2 upper
5375 redalpha = (int) PRVM_G_FLOAT(OFS_PARM1); //0 same, 1 white, 2 red, 5 alternate, 6 alternate-alternate
5376 rednum = (int) PRVM_G_FLOAT(OFS_PARM2); //0 same, 1 white, 2 red, 3 redspecial, 4 whitespecial, 5 alternate, 6 alternate-alternate
5377 VM_VarString(3, (char *) resbuf, sizeof(resbuf));
5378 len = strlen((char *) resbuf);
5380 for (i = 0; i < len; i++, result++) //should this be done backwards?
5382 if (*result >= '0' && *result <= '9') //normal numbers...
5383 *result = chrconv_number(*result, '0', rednum);
5384 else if (*result >= '0'+128 && *result <= '9'+128)
5385 *result = chrconv_number(*result, '0'+128, rednum);
5386 else if (*result >= '0'+128-30 && *result <= '9'+128-30)
5387 *result = chrconv_number(*result, '0'+128-30, rednum);
5388 else if (*result >= '0'-30 && *result <= '9'-30)
5389 *result = chrconv_number(*result, '0'-30, rednum);
5391 else if (*result >= 'a' && *result <= 'z') //normal numbers...
5392 *result = chrchar_alpha(*result, 'a', 0, ccase, redalpha, i);
5393 else if (*result >= 'A' && *result <= 'Z') //normal numbers...
5394 *result = chrchar_alpha(*result, 'A', 0, ccase, redalpha, i);
5395 else if (*result >= 'a'+128 && *result <= 'z'+128) //normal numbers...
5396 *result = chrchar_alpha(*result, 'a', 128, ccase, redalpha, i);
5397 else if (*result >= 'A'+128 && *result <= 'Z'+128) //normal numbers...
5398 *result = chrchar_alpha(*result, 'A', 128, ccase, redalpha, i);
5400 else if ((*result & 127) < 16 || !redalpha) //special chars..
5402 else if (*result < 128)
5403 *result = chrconv_punct(*result, 0, redalpha);
5405 *result = chrconv_punct(*result, 128, redalpha);
5409 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString((char *) resbuf);
5412 // #225 string(float chars, string s, ...) strpad (FTE_STRINGS)
5413 void VM_strpad (void)
5415 char src[VM_STRINGTEMP_LENGTH];
5416 char destbuf[VM_STRINGTEMP_LENGTH];
5418 VM_SAFEPARMCOUNTRANGE(1, 8, VM_strpad);
5419 pad = (int) PRVM_G_FLOAT(OFS_PARM0);
5420 VM_VarString(1, src, sizeof(src));
5422 // note: < 0 = left padding, > 0 = right padding,
5423 // this is reverse logic of printf!
5424 dpsnprintf(destbuf, sizeof(destbuf), "%*s", -pad, src);
5426 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(destbuf);
5429 // #226 string(string info, string key, string value, ...) infoadd (FTE_STRINGS)
5430 //uses qw style \key\value strings
5431 void VM_infoadd (void)
5433 const char *info, *key;
5434 char value[VM_STRINGTEMP_LENGTH];
5435 char temp[VM_STRINGTEMP_LENGTH];
5437 VM_SAFEPARMCOUNTRANGE(2, 8, VM_infoadd);
5438 info = PRVM_G_STRING(OFS_PARM0);
5439 key = PRVM_G_STRING(OFS_PARM1);
5440 VM_VarString(2, value, sizeof(value));
5442 strlcpy(temp, info, VM_STRINGTEMP_LENGTH);
5444 InfoString_SetValue(temp, VM_STRINGTEMP_LENGTH, key, value);
5446 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(temp);
5449 // #227 string(string info, string key) infoget (FTE_STRINGS)
5450 //uses qw style \key\value strings
5451 void VM_infoget (void)
5455 char value[VM_STRINGTEMP_LENGTH];
5457 VM_SAFEPARMCOUNT(2, VM_infoget);
5458 info = PRVM_G_STRING(OFS_PARM0);
5459 key = PRVM_G_STRING(OFS_PARM1);
5461 InfoString_GetValue(info, key, value, VM_STRINGTEMP_LENGTH);
5463 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(value);
5466 //#228 float(string s1, string s2, float len) strncmp (FTE_STRINGS)
5467 // also float(string s1, string s2) strcmp (FRIK_FILE)
5468 void VM_strncmp (void)
5470 const char *s1, *s2;
5471 VM_SAFEPARMCOUNTRANGE(2, 3, VM_strncmp);
5472 s1 = PRVM_G_STRING(OFS_PARM0);
5473 s2 = PRVM_G_STRING(OFS_PARM1);
5476 PRVM_G_FLOAT(OFS_RETURN) = strncmp(s1, s2, (size_t)PRVM_G_FLOAT(OFS_PARM2));
5480 PRVM_G_FLOAT(OFS_RETURN) = strcmp(s1, s2);
5484 // #229 float(string s1, string s2) strcasecmp (FTE_STRINGS)
5485 // #230 float(string s1, string s2, float len) strncasecmp (FTE_STRINGS)
5486 void VM_strncasecmp (void)
5488 const char *s1, *s2;
5489 VM_SAFEPARMCOUNTRANGE(2, 3, VM_strncasecmp);
5490 s1 = PRVM_G_STRING(OFS_PARM0);
5491 s2 = PRVM_G_STRING(OFS_PARM1);
5494 PRVM_G_FLOAT(OFS_RETURN) = strncasecmp(s1, s2, (size_t)PRVM_G_FLOAT(OFS_PARM2));
5498 PRVM_G_FLOAT(OFS_RETURN) = strcasecmp(s1, s2);
5502 // #494 float(float caseinsensitive, string s, ...) crc16
5506 static char s[VM_STRINGTEMP_LENGTH];
5507 VM_SAFEPARMCOUNTRANGE(2, 8, VM_crc16);
5508 insensitive = PRVM_G_FLOAT(OFS_PARM0);
5509 VM_VarString(1, s, sizeof(s));
5510 PRVM_G_FLOAT(OFS_RETURN) = (unsigned short) ((insensitive ? CRC_Block_CaseInsensitive : CRC_Block) ((unsigned char *) s, strlen(s)));
5513 // #639 float(string digest, string data, ...) digest_hex
5514 void VM_digest_hex(void)
5518 static char out[32];
5519 static char outhex[65];
5522 static char s[VM_STRINGTEMP_LENGTH];
5525 VM_SAFEPARMCOUNTRANGE(2, 8, VM_digest_hex);
5526 digest = PRVM_G_STRING(OFS_PARM0);
5529 VM_VarString(1, s, sizeof(s));
5534 if(!strcmp(digest, "MD4"))
5537 mdfour((unsigned char *) out, (unsigned char *) s, len);
5539 else if(!strcmp(digest, "SHA256") && Crypto_Available())
5542 sha256((unsigned char *) out, (unsigned char *) s, len);
5544 // no warning needed on mismatch - we return string_null to QC
5549 static const char *hexmap = "0123456789abcdef";
5550 for(i = 0; i < outlen; ++i)
5552 outhex[2*i] = hexmap[(out[i] >> 4) & 15];
5553 outhex[2*i+1] = hexmap[(out[i] >> 0) & 15];
5556 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(outhex);
5559 PRVM_G_INT(OFS_RETURN) = 0;
5562 void VM_wasfreed (void)
5564 VM_SAFEPARMCOUNT(1, VM_wasfreed);
5565 PRVM_G_FLOAT(OFS_RETURN) = PRVM_G_EDICT(OFS_PARM0)->priv.required->free;
5568 void VM_SetTraceGlobals(const trace_t *trace)
5570 PRVM_gameglobalfloat(trace_allsolid) = trace->allsolid;
5571 PRVM_gameglobalfloat(trace_startsolid) = trace->startsolid;
5572 PRVM_gameglobalfloat(trace_fraction) = trace->fraction;
5573 PRVM_gameglobalfloat(trace_inwater) = trace->inwater;
5574 PRVM_gameglobalfloat(trace_inopen) = trace->inopen;
5575 VectorCopy(trace->endpos, PRVM_gameglobalvector(trace_endpos));
5576 VectorCopy(trace->plane.normal, PRVM_gameglobalvector(trace_plane_normal));
5577 PRVM_gameglobalfloat(trace_plane_dist) = trace->plane.dist;
5578 PRVM_gameglobaledict(trace_ent) = PRVM_EDICT_TO_PROG(trace->ent ? trace->ent : prog->edicts);
5579 PRVM_gameglobalfloat(trace_dpstartcontents) = trace->startsupercontents;
5580 PRVM_gameglobalfloat(trace_dphitcontents) = trace->hitsupercontents;
5581 PRVM_gameglobalfloat(trace_dphitq3surfaceflags) = trace->hitq3surfaceflags;
5582 PRVM_gameglobalstring(trace_dphittexturename) = trace->hittexture ? PRVM_SetTempString(trace->hittexture->name) : 0;
5585 void VM_ClearTraceGlobals(void)
5587 // clean up all trace globals when leaving the VM (anti-triggerbot safeguard)
5588 PRVM_gameglobalfloat(trace_allsolid) = 0;
5589 PRVM_gameglobalfloat(trace_startsolid) = 0;
5590 PRVM_gameglobalfloat(trace_fraction) = 0;
5591 PRVM_gameglobalfloat(trace_inwater) = 0;
5592 PRVM_gameglobalfloat(trace_inopen) = 0;
5593 VectorClear(PRVM_gameglobalvector(trace_endpos));
5594 VectorClear(PRVM_gameglobalvector(trace_plane_normal));
5595 PRVM_gameglobalfloat(trace_plane_dist) = 0;
5596 PRVM_gameglobaledict(trace_ent) = PRVM_EDICT_TO_PROG(prog->edicts);
5597 PRVM_gameglobalfloat(trace_dpstartcontents) = 0;
5598 PRVM_gameglobalfloat(trace_dphitcontents) = 0;
5599 PRVM_gameglobalfloat(trace_dphitq3surfaceflags) = 0;
5600 PRVM_gameglobalstring(trace_dphittexturename) = 0;
5605 void VM_Cmd_Init(void)
5607 // only init the stuff for the current prog
5610 // VM_BufStr_Init();
5613 void VM_Cmd_Reset(void)
5615 CL_PurgeOwner( MENUOWNER );
5617 VM_Files_CloseAll();
5618 // VM_BufStr_ShutDown();
5621 // #510 string(string input, ...) uri_escape (DP_QC_URI_ESCAPE)
5622 // does URI escaping on a string (replace evil stuff by %AB escapes)
5623 void VM_uri_escape (void)
5625 char src[VM_STRINGTEMP_LENGTH];
5626 char dest[VM_STRINGTEMP_LENGTH];
5628 static const char *hex = "0123456789ABCDEF";
5630 VM_SAFEPARMCOUNTRANGE(1, 8, VM_uri_escape);
5631 VM_VarString(0, src, sizeof(src));
5633 for(p = src, q = dest; *p && q < dest + sizeof(dest) - 3; ++p)
5635 if((*p >= 'A' && *p <= 'Z')
5636 || (*p >= 'a' && *p <= 'z')
5637 || (*p >= '0' && *p <= '9')
5638 || (*p == '-') || (*p == '_') || (*p == '.')
5639 || (*p == '!') || (*p == '~')
5640 || (*p == '\'') || (*p == '(') || (*p == ')'))
5645 *q++ = hex[(*(unsigned char *)p >> 4) & 0xF];
5646 *q++ = hex[ *(unsigned char *)p & 0xF];
5651 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(dest);
5654 // #510 string(string input, ...) uri_unescape (DP_QC_URI_ESCAPE)
5655 // does URI unescaping on a string (get back the evil stuff)
5656 void VM_uri_unescape (void)
5658 char src[VM_STRINGTEMP_LENGTH];
5659 char dest[VM_STRINGTEMP_LENGTH];
5663 VM_SAFEPARMCOUNTRANGE(1, 8, VM_uri_unescape);
5664 VM_VarString(0, src, sizeof(src));
5666 for(p = src, q = dest; *p; ) // no need to check size, because unescape can't expand
5670 if(p[1] >= '0' && p[1] <= '9')
5672 else if(p[1] >= 'a' && p[1] <= 'f')
5673 hi = p[1] - 'a' + 10;
5674 else if(p[1] >= 'A' && p[1] <= 'F')
5675 hi = p[1] - 'A' + 10;
5678 if(p[2] >= '0' && p[2] <= '9')
5680 else if(p[2] >= 'a' && p[2] <= 'f')
5681 lo = p[2] - 'a' + 10;
5682 else if(p[2] >= 'A' && p[2] <= 'F')
5683 lo = p[2] - 'A' + 10;
5686 if(hi != 0 || lo != 0) // don't unescape NUL bytes
5687 *q++ = (char) (hi * 0x10 + lo);
5698 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(dest);
5701 // #502 string(string filename) whichpack (DP_QC_WHICHPACK)
5702 // returns the name of the pack containing a file, or "" if it is not in any pack (but local or non-existant)
5703 void VM_whichpack (void)
5705 const char *fn, *pack;
5707 VM_SAFEPARMCOUNT(1, VM_whichpack);
5708 fn = PRVM_G_STRING(OFS_PARM0);
5709 pack = FS_WhichPack(fn);
5711 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(pack ? pack : "");
5719 char buffer[MAX_INPUTLINE];
5720 unsigned char *postdata; // free when uri_to_prog_t is freed
5722 char *sigdata; // free when uri_to_prog_t is freed
5727 static void uri_to_string_callback(int status, size_t length_received, unsigned char *buffer, void *cbdata)
5729 uri_to_prog_t *handle = (uri_to_prog_t *) cbdata;
5731 if(!PRVM_ProgLoaded(handle->prognr))
5733 // curl reply came too late... so just drop it
5734 if(handle->postdata)
5735 Z_Free(handle->postdata);
5737 Z_Free(handle->sigdata);
5742 PRVM_SetProg(handle->prognr);
5744 if((prog->starttime == handle->starttime) && (PRVM_allfunction(URI_Get_Callback)))
5746 if(length_received >= sizeof(handle->buffer))
5747 length_received = sizeof(handle->buffer) - 1;
5748 handle->buffer[length_received] = 0;
5750 PRVM_G_FLOAT(OFS_PARM0) = handle->id;
5751 PRVM_G_FLOAT(OFS_PARM1) = status;
5752 PRVM_G_INT(OFS_PARM2) = PRVM_SetTempString(handle->buffer);
5753 PRVM_ExecuteProgram(PRVM_allfunction(URI_Get_Callback), "QC function URI_Get_Callback is missing");
5757 if(handle->postdata)
5758 Z_Free(handle->postdata);
5760 Z_Free(handle->sigdata);
5764 // 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
5765 // 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
5766 void VM_uri_get (void)
5771 uri_to_prog_t *handle;
5772 const char *posttype = NULL;
5773 const char *postseparator = NULL;
5774 int poststringbuffer = -1;
5776 const char *query_string = NULL;
5779 if(!PRVM_allfunction(URI_Get_Callback))
5780 PRVM_ERROR("uri_get called by %s without URI_Get_Callback defined", PRVM_NAME);
5782 VM_SAFEPARMCOUNTRANGE(2, 6, VM_uri_get);
5784 url = PRVM_G_STRING(OFS_PARM0);
5785 id = PRVM_G_FLOAT(OFS_PARM1);
5787 posttype = PRVM_G_STRING(OFS_PARM2);
5789 postseparator = PRVM_G_STRING(OFS_PARM3);
5791 poststringbuffer = PRVM_G_FLOAT(OFS_PARM4);
5793 postkeyid = PRVM_G_FLOAT(OFS_PARM5);
5794 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!
5796 query_string = strchr(url, '?');
5799 lq = query_string ? strlen(query_string) : 0;
5801 handle->prognr = PRVM_GetProgNr();
5802 handle->starttime = prog->starttime;
5804 if(postseparator && posttype && *posttype)
5806 size_t l = strlen(postseparator);
5807 if(poststringbuffer >= 0)
5812 prvm_stringbuffer_t *stringbuffer;
5813 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, poststringbuffer);
5816 VM_Warning("uri_get: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
5820 for(i = 0;i < stringbuffer->num_strings;i++)
5824 if(stringbuffer->strings[i])
5825 ltotal += strlen(stringbuffer->strings[i]);
5827 handle->postdata = (unsigned char *)Z_Malloc(ltotal + 1 + lq);
5828 handle->postlen = ltotal;
5830 for(i = 0;i < stringbuffer->num_strings;i++)
5834 memcpy(handle->postdata + ltotal, postseparator, l);
5837 if(stringbuffer->strings[i])
5839 memcpy(handle->postdata + ltotal, stringbuffer->strings[i], strlen(stringbuffer->strings[i]));
5840 ltotal += strlen(stringbuffer->strings[i]);
5843 if(ltotal != handle->postlen)
5844 PRVM_ERROR ("%s: string buffer content size mismatch, possible overrun", PRVM_NAME);
5848 handle->postdata = (unsigned char *)Z_Malloc(l + 1 + lq);
5849 handle->postlen = l;
5850 memcpy(handle->postdata, postseparator, l);
5852 handle->postdata[handle->postlen] = 0;
5854 memcpy(handle->postdata + handle->postlen + 1, query_string, lq);
5857 // POST: we sign postdata \0 query string
5859 handle->sigdata = (char *)Z_Malloc(8192);
5860 strlcpy(handle->sigdata, "X-D0-Blind-ID-Detached-Signature: ", 8192);
5861 l = strlen(handle->sigdata);
5862 handle->siglen = Crypto_SignDataDetached(handle->postdata, handle->postlen + 1 + lq, postkeyid, handle->sigdata + l, 8192 - l);
5865 Z_Free(handle->sigdata);
5866 handle->sigdata = NULL;
5869 ll = base64_encode((unsigned char *) (handle->sigdata + l), handle->siglen, 8192 - l - 1);
5872 Z_Free(handle->sigdata);
5873 handle->sigdata = NULL;
5876 handle->siglen = l + ll;
5877 handle->sigdata[handle->siglen] = 0;
5880 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);
5884 if(postkeyid >= 0 && query_string)
5886 // GET: we sign JUST the query string
5888 handle->sigdata = (char *)Z_Malloc(8192);
5889 strlcpy(handle->sigdata, "X-D0-Blind-ID-Detached-Signature: ", 8192);
5890 l = strlen(handle->sigdata);
5891 handle->siglen = Crypto_SignDataDetached(query_string, lq, postkeyid, handle->sigdata + l, 8192 - l);
5894 Z_Free(handle->sigdata);
5895 handle->sigdata = NULL;
5898 ll = base64_encode((unsigned char *) (handle->sigdata + l), handle->siglen, 8192 - l - 1);
5901 Z_Free(handle->sigdata);
5902 handle->sigdata = NULL;
5905 handle->siglen = l + ll;
5906 handle->sigdata[handle->siglen] = 0;
5909 handle->postdata = NULL;
5910 handle->postlen = 0;
5911 ret = Curl_Begin_ToMemory(url, 0, (unsigned char *) handle->buffer, sizeof(handle->buffer), uri_to_string_callback, handle);
5915 PRVM_G_INT(OFS_RETURN) = 1;
5919 if(handle->postdata)
5920 Z_Free(handle->postdata);
5922 Z_Free(handle->sigdata);
5924 PRVM_G_INT(OFS_RETURN) = 0;
5928 void VM_netaddress_resolve (void)
5931 char normalized[128];
5933 lhnetaddress_t addr;
5935 VM_SAFEPARMCOUNTRANGE(1, 2, VM_netaddress_resolve);
5937 ip = PRVM_G_STRING(OFS_PARM0);
5940 port = (int) PRVM_G_FLOAT(OFS_PARM1);
5942 if(LHNETADDRESS_FromString(&addr, ip, port) && LHNETADDRESS_ToString(&addr, normalized, sizeof(normalized), prog->argc > 1))
5943 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(normalized);
5945 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString("");
5948 //string(void) getextresponse = #624; // returns the next extResponse packet that was sent to this client
5949 void VM_CL_getextresponse (void)
5951 VM_SAFEPARMCOUNT(0,VM_argv);
5953 if (cl_net_extresponse_count <= 0)
5954 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
5958 --cl_net_extresponse_count;
5959 first = (cl_net_extresponse_last + NET_EXTRESPONSE_MAX - cl_net_extresponse_count) % NET_EXTRESPONSE_MAX;
5960 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(cl_net_extresponse[first]);
5964 void VM_SV_getextresponse (void)
5966 VM_SAFEPARMCOUNT(0,VM_argv);
5968 if (sv_net_extresponse_count <= 0)
5969 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
5973 --sv_net_extresponse_count;
5974 first = (sv_net_extresponse_last + NET_EXTRESPONSE_MAX - sv_net_extresponse_count) % NET_EXTRESPONSE_MAX;
5975 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(sv_net_extresponse[first]);
5981 Common functions between menu.dat and clsprogs
5985 //#349 float() isdemo
5986 void VM_CL_isdemo (void)
5988 VM_SAFEPARMCOUNT(0, VM_CL_isdemo);
5989 PRVM_G_FLOAT(OFS_RETURN) = cls.demoplayback;
5992 //#355 float() videoplaying
5993 void VM_CL_videoplaying (void)
5995 VM_SAFEPARMCOUNT(0, VM_CL_videoplaying);
5996 PRVM_G_FLOAT(OFS_RETURN) = cl_videoplaying;
6003 callfunction(...,string function_name)
6007 mfunction_t *PRVM_ED_FindFunction (const char *name);
6008 void VM_callfunction(void)
6013 VM_SAFEPARMCOUNTRANGE(1, 8, VM_callfunction);
6015 s = PRVM_G_STRING(OFS_PARM0+(prog->argc - 1)*3);
6017 VM_CheckEmptyString(s);
6019 func = PRVM_ED_FindFunction(s);
6022 PRVM_ERROR("VM_callfunciton: function %s not found !", s);
6023 else if (func->first_statement < 0)
6025 // negative statements are built in functions
6026 int builtinnumber = -func->first_statement;
6027 prog->xfunction->builtinsprofile++;
6028 if (builtinnumber < prog->numbuiltins && prog->builtins[builtinnumber])
6029 prog->builtins[builtinnumber]();
6031 PRVM_ERROR("No such builtin #%i in %s; most likely cause: outdated engine build. Try updating!", builtinnumber, PRVM_NAME);
6033 else if(func - prog->functions > 0)
6036 PRVM_ExecuteProgram(func - prog->functions,"");
6045 float isfunction(string function_name)
6048 mfunction_t *PRVM_ED_FindFunction (const char *name);
6049 void VM_isfunction(void)
6054 VM_SAFEPARMCOUNT(1, VM_isfunction);
6056 s = PRVM_G_STRING(OFS_PARM0);
6058 VM_CheckEmptyString(s);
6060 func = PRVM_ED_FindFunction(s);
6063 PRVM_G_FLOAT(OFS_RETURN) = false;
6065 PRVM_G_FLOAT(OFS_RETURN) = true;
6072 string sprintf(string format, ...)
6076 void VM_sprintf(void)
6079 char outbuf[MAX_INPUTLINE];
6080 char *o = outbuf, *end = outbuf + sizeof(outbuf), *err;
6082 int width, precision, thisarg, flags;
6086 static int dummyivec[3] = {0, 0, 0};
6087 static float dummyvec[3] = {0, 0, 0};
6089 #define PRINTF_ALTERNATE 1
6090 #define PRINTF_ZEROPAD 2
6091 #define PRINTF_LEFT 4
6092 #define PRINTF_SPACEPOSITIVE 8
6093 #define PRINTF_SIGNPOSITIVE 16
6097 s = PRVM_G_STRING(OFS_PARM0);
6099 #define GETARG_FLOAT(a) (((a)>=1 && (a)<prog->argc) ? (PRVM_G_FLOAT(OFS_PARM0 + 3 * (a))) : 0)
6100 #define GETARG_VECTOR(a) (((a)>=1 && (a)<prog->argc) ? (PRVM_G_VECTOR(OFS_PARM0 + 3 * (a))) : dummyvec)
6101 #define GETARG_INT(a) (((a)>=1 && (a)<prog->argc) ? (PRVM_G_INT(OFS_PARM0 + 3 * (a))) : 0)
6102 #define GETARG_INTVECTOR(a) (((a)>=1 && (a)<prog->argc) ? ((int*) PRVM_G_VECTOR(OFS_PARM0 + 3 * (a))) : dummyivec)
6103 #define GETARG_STRING(a) (((a)>=1 && (a)<prog->argc) ? (PRVM_G_STRING(OFS_PARM0 + 3 * (a))) : "")
6118 // complete directive format:
6127 // is number following?
6128 if(*s >= '0' && *s <= '9')
6130 width = strtol(s, &err, 10);
6133 VM_Warning("VM_sprintf: invalid directive in %s: %s\n", PRVM_NAME, s0);
6146 flags |= PRINTF_ZEROPAD;
6148 width = -1; // it was just a flag
6160 case '#': flags |= PRINTF_ALTERNATE; break;
6161 case '0': flags |= PRINTF_ZEROPAD; break;
6162 case '-': flags |= PRINTF_LEFT; break;
6163 case ' ': flags |= PRINTF_SPACEPOSITIVE; break;
6164 case '+': flags |= PRINTF_SIGNPOSITIVE; break;
6174 if(*s >= '0' && *s <= '9')
6176 width = strtol(s, &err, 10);
6177 if(!err || *err != '$')
6179 VM_Warning("VM_sprintf: invalid directive in %s: %s\n", PRVM_NAME, s0);
6186 width = GETARG_FLOAT(width);
6189 flags |= PRINTF_LEFT;
6193 else if(*s >= '0' && *s <= '9')
6195 width = strtol(s, &err, 10);
6198 VM_Warning("VM_sprintf: invalid directive in %s: %s\n", PRVM_NAME, s0);
6204 flags |= PRINTF_LEFT;
6208 // otherwise width stays -1
6217 if(*s >= '0' && *s <= '9')
6219 precision = strtol(s, &err, 10);
6220 if(!err || *err != '$')
6222 VM_Warning("VM_sprintf: invalid directive in %s: %s\n", PRVM_NAME, s0);
6228 precision = argpos++;
6229 precision = GETARG_FLOAT(precision);
6231 else if(*s >= '0' && *s <= '9')
6233 precision = strtol(s, &err, 10);
6236 VM_Warning("VM_sprintf: invalid directive in %s: %s\n", PRVM_NAME, s0);
6243 VM_Warning("VM_sprintf: invalid directive in %s: %s\n", PRVM_NAME, s0);
6252 case 'h': isfloat = 1; break;
6253 case 'l': isfloat = 0; break;
6254 case 'L': isfloat = 0; break;
6265 // now s points to the final directive char and is no longer changed
6280 if(*s != 's' && *s != 'c')
6281 if(flags & PRINTF_ALTERNATE) *f++ = '#';
6282 if(flags & PRINTF_ZEROPAD) *f++ = '0';
6283 if(flags & PRINTF_LEFT) *f++ = '-';
6284 if(flags & PRINTF_SPACEPOSITIVE) *f++ = ' ';
6285 if(flags & PRINTF_SIGNPOSITIVE) *f++ = '+';
6295 if(width < 0) // not set
6301 if(precision < 0) // not set
6302 o += dpsnprintf(o, end - o, formatbuf, width, (isfloat ? (int) GETARG_FLOAT(thisarg) : (int) GETARG_INT(thisarg)));
6304 o += dpsnprintf(o, end - o, formatbuf, width, precision, (isfloat ? (int) GETARG_FLOAT(thisarg) : (int) GETARG_INT(thisarg)));
6306 case 'o': case 'u': case 'x': case 'X':
6307 if(precision < 0) // not set
6308 o += dpsnprintf(o, end - o, formatbuf, width, (isfloat ? (unsigned int) GETARG_FLOAT(thisarg) : (unsigned int) GETARG_INT(thisarg)));
6310 o += dpsnprintf(o, end - o, formatbuf, width, precision, (isfloat ? (unsigned int) GETARG_FLOAT(thisarg) : (unsigned int) GETARG_INT(thisarg)));
6312 case 'e': case 'E': case 'f': case 'F': case 'g': case 'G':
6313 if(precision < 0) // not set
6314 o += dpsnprintf(o, end - o, formatbuf, width, (isfloat ? (double) GETARG_FLOAT(thisarg) : (double) GETARG_INT(thisarg)));
6316 o += dpsnprintf(o, end - o, formatbuf, width, precision, (isfloat ? (double) GETARG_FLOAT(thisarg) : (double) GETARG_INT(thisarg)));
6320 if(precision < 0) // not set
6321 o += dpsnprintf(o, end - o, va("%s %s %s", /* NESTED SPRINTF IS NESTED */ formatbuf, formatbuf, formatbuf),
6322 width, (isfloat ? (double) GETARG_VECTOR(thisarg)[0] : (double) GETARG_INTVECTOR(thisarg)[0]),
6323 width, (isfloat ? (double) GETARG_VECTOR(thisarg)[1] : (double) GETARG_INTVECTOR(thisarg)[1]),
6324 width, (isfloat ? (double) GETARG_VECTOR(thisarg)[2] : (double) GETARG_INTVECTOR(thisarg)[2])
6327 o += dpsnprintf(o, end - o, va("%s %s %s", /* NESTED SPRINTF IS NESTED */ formatbuf, formatbuf, formatbuf),
6328 width, precision, (isfloat ? (double) GETARG_VECTOR(thisarg)[0] : (double) GETARG_INTVECTOR(thisarg)[0]),
6329 width, precision, (isfloat ? (double) GETARG_VECTOR(thisarg)[1] : (double) GETARG_INTVECTOR(thisarg)[1]),
6330 width, precision, (isfloat ? (double) GETARG_VECTOR(thisarg)[2] : (double) GETARG_INTVECTOR(thisarg)[2])
6334 if(flags & PRINTF_ALTERNATE)
6336 if(precision < 0) // not set
6337 o += dpsnprintf(o, end - o, formatbuf, width, (isfloat ? (unsigned int) GETARG_FLOAT(thisarg) : (unsigned int) GETARG_INT(thisarg)));
6339 o += dpsnprintf(o, end - o, formatbuf, width, precision, (isfloat ? (unsigned int) GETARG_FLOAT(thisarg) : (unsigned int) GETARG_INT(thisarg)));
6343 unsigned int c = (isfloat ? (unsigned int) GETARG_FLOAT(thisarg) : (unsigned int) GETARG_INT(thisarg));
6344 const char *buf = u8_encodech(c, NULL);
6347 if(precision < 0) // not set
6348 precision = end - o - 1;
6349 o += u8_strpad(o, end - o, buf, (flags & PRINTF_LEFT) != 0, width, precision);
6353 if(flags & PRINTF_ALTERNATE)
6355 if(precision < 0) // not set
6356 o += dpsnprintf(o, end - o, formatbuf, width, GETARG_STRING(thisarg));
6358 o += dpsnprintf(o, end - o, formatbuf, width, precision, GETARG_STRING(thisarg));
6362 if(precision < 0) // not set
6363 precision = end - o - 1;
6364 o += u8_strpad(o, end - o, GETARG_STRING(thisarg), (flags & PRINTF_LEFT) != 0, width, precision);
6368 VM_Warning("VM_sprintf: invalid directive in %s: %s\n", PRVM_NAME, s0);
6383 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(outbuf);
6389 static dp_model_t *getmodel(prvm_edict_t *ed)
6391 switch(PRVM_GetProgNr())
6393 case PRVM_SERVERPROG:
6394 return SV_GetModelFromEdict(ed);
6395 case PRVM_CLIENTPROG:
6396 return CL_GetModelFromEdict(ed);
6404 unsigned int progid;
6406 frameblend_t frameblend[MAX_FRAMEBLENDS];
6407 skeleton_t *skeleton_p;
6408 skeleton_t skeleton;
6409 float *data_vertex3f;
6410 float *data_svector3f;
6411 float *data_tvector3f;
6412 float *data_normal3f;
6414 float *buf_vertex3f;
6415 float *buf_svector3f;
6416 float *buf_tvector3f;
6417 float *buf_normal3f;
6419 animatemodel_cache_t;
6420 static animatemodel_cache_t animatemodel_cache;
6422 void animatemodel(dp_model_t *model, prvm_edict_t *ed)
6424 skeleton_t *skeleton;
6425 int skeletonindex = -1;
6426 qboolean need = false;
6427 if(!(model->surfmesh.isanimated && model->AnimateVertices))
6429 animatemodel_cache.data_vertex3f = model->surfmesh.data_vertex3f;
6430 animatemodel_cache.data_svector3f = model->surfmesh.data_svector3f;
6431 animatemodel_cache.data_tvector3f = model->surfmesh.data_tvector3f;
6432 animatemodel_cache.data_normal3f = model->surfmesh.data_normal3f;
6435 if(animatemodel_cache.progid != prog->id)
6436 memset(&animatemodel_cache, 0, sizeof(animatemodel_cache));
6437 need |= (animatemodel_cache.model != model);
6438 VM_GenerateFrameGroupBlend(ed->priv.server->framegroupblend, ed);
6439 VM_FrameBlendFromFrameGroupBlend(ed->priv.server->frameblend, ed->priv.server->framegroupblend, model);
6440 need |= (memcmp(&animatemodel_cache.frameblend, &ed->priv.server->frameblend, sizeof(ed->priv.server->frameblend))) != 0;
6441 skeletonindex = (int)PRVM_gameedictfloat(ed, skeletonindex) - 1;
6442 if (!(skeletonindex >= 0 && skeletonindex < MAX_EDICTS && (skeleton = prog->skeletons[skeletonindex]) && skeleton->model->num_bones == ed->priv.server->skeleton.model->num_bones))
6444 need |= (animatemodel_cache.skeleton_p != skeleton);
6446 need |= (memcmp(&animatemodel_cache.skeleton, skeleton, sizeof(ed->priv.server->skeleton))) != 0;
6449 if(model->surfmesh.num_vertices > animatemodel_cache.max_vertices)
6451 animatemodel_cache.max_vertices = model->surfmesh.num_vertices * 2;
6452 if(animatemodel_cache.buf_vertex3f) Mem_Free(animatemodel_cache.buf_vertex3f);
6453 if(animatemodel_cache.buf_svector3f) Mem_Free(animatemodel_cache.buf_svector3f);
6454 if(animatemodel_cache.buf_tvector3f) Mem_Free(animatemodel_cache.buf_tvector3f);
6455 if(animatemodel_cache.buf_normal3f) Mem_Free(animatemodel_cache.buf_normal3f);
6456 animatemodel_cache.buf_vertex3f = (float *)Mem_Alloc(prog->progs_mempool, sizeof(float[3]) * animatemodel_cache.max_vertices);
6457 animatemodel_cache.buf_svector3f = (float *)Mem_Alloc(prog->progs_mempool, sizeof(float[3]) * animatemodel_cache.max_vertices);
6458 animatemodel_cache.buf_tvector3f = (float *)Mem_Alloc(prog->progs_mempool, sizeof(float[3]) * animatemodel_cache.max_vertices);
6459 animatemodel_cache.buf_normal3f = (float *)Mem_Alloc(prog->progs_mempool, sizeof(float[3]) * animatemodel_cache.max_vertices);
6461 animatemodel_cache.data_vertex3f = animatemodel_cache.buf_vertex3f;
6462 animatemodel_cache.data_svector3f = animatemodel_cache.buf_svector3f;
6463 animatemodel_cache.data_tvector3f = animatemodel_cache.buf_tvector3f;
6464 animatemodel_cache.data_normal3f = animatemodel_cache.buf_normal3f;
6465 VM_UpdateEdictSkeleton(ed, model, ed->priv.server->frameblend);
6466 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);
6467 animatemodel_cache.progid = prog->id;
6468 animatemodel_cache.model = model;
6469 memcpy(&animatemodel_cache.frameblend, &ed->priv.server->frameblend, sizeof(ed->priv.server->frameblend));
6470 animatemodel_cache.skeleton_p = skeleton;
6472 memcpy(&animatemodel_cache.skeleton, skeleton, sizeof(ed->priv.server->skeleton));
6475 static void getmatrix(prvm_edict_t *ed, matrix4x4_t *out)
6477 switch(PRVM_GetProgNr())
6479 case PRVM_SERVERPROG:
6480 SV_GetEntityMatrix(ed, out, false);
6482 case PRVM_CLIENTPROG:
6483 CL_GetEntityMatrix(ed, out, false);
6486 *out = identitymatrix;
6491 static void applytransform_forward(const vec3_t in, prvm_edict_t *ed, vec3_t out)
6495 Matrix4x4_Transform(&m, in, out);
6498 static void applytransform_forward_direction(const vec3_t in, prvm_edict_t *ed, vec3_t out)
6502 Matrix4x4_Transform3x3(&m, in, out);
6505 static void applytransform_inverted(const vec3_t in, prvm_edict_t *ed, vec3_t out)
6509 Matrix4x4_Invert_Full(&n, &m);
6510 Matrix4x4_Transform3x3(&n, in, out);
6513 static void applytransform_forward_normal(const vec3_t in, prvm_edict_t *ed, vec3_t out)
6518 Matrix4x4_TransformPositivePlane(&m, in[0], in[1], in[2], 0, p);
6522 static void clippointtosurface(prvm_edict_t *ed, dp_model_t *model, msurface_t *surface, vec3_t p, vec3_t out)
6525 float *v[3], facenormal[3], edgenormal[3], sidenormal[3], temp[3], offsetdist, dist, bestdist;
6527 animatemodel(model, ed);
6528 bestdist = 1000000000;
6530 for (i = 0, e = (model->surfmesh.data_element3i + 3 * surface->num_firsttriangle);i < surface->num_triangles;i++, e += 3)
6532 // clip original point to each triangle of the surface and find the
6533 // triangle that is closest
6534 v[0] = animatemodel_cache.data_vertex3f + e[0] * 3;
6535 v[1] = animatemodel_cache.data_vertex3f + e[1] * 3;
6536 v[2] = animatemodel_cache.data_vertex3f + e[2] * 3;
6537 TriangleNormal(v[0], v[1], v[2], facenormal);
6538 VectorNormalize(facenormal);
6539 offsetdist = DotProduct(v[0], facenormal) - DotProduct(p, facenormal);
6540 VectorMA(p, offsetdist, facenormal, temp);
6541 for (j = 0, k = 2;j < 3;k = j, j++)
6543 VectorSubtract(v[k], v[j], edgenormal);
6544 CrossProduct(edgenormal, facenormal, sidenormal);
6545 VectorNormalize(sidenormal);
6546 offsetdist = DotProduct(v[k], sidenormal) - DotProduct(temp, sidenormal);
6548 VectorMA(temp, offsetdist, sidenormal, temp);
6550 dist = VectorDistance2(temp, p);
6551 if (bestdist > dist)
6554 VectorCopy(temp, out);
6559 static msurface_t *getsurface(dp_model_t *model, int surfacenum)
6561 if (surfacenum < 0 || surfacenum >= model->nummodelsurfaces)
6563 return model->data_surfaces + surfacenum + model->firstmodelsurface;
6567 //PF_getsurfacenumpoints, // #434 float(entity e, float s) getsurfacenumpoints = #434;
6568 void VM_getsurfacenumpoints(void)
6571 msurface_t *surface;
6572 VM_SAFEPARMCOUNT(2, VM_getsurfacenumpoints);
6573 // return 0 if no such surface
6574 if (!(model = getmodel(PRVM_G_EDICT(OFS_PARM0))) || !(surface = getsurface(model, (int)PRVM_G_FLOAT(OFS_PARM1))))
6576 PRVM_G_FLOAT(OFS_RETURN) = 0;
6580 // note: this (incorrectly) assumes it is a simple polygon
6581 PRVM_G_FLOAT(OFS_RETURN) = surface->num_vertices;
6583 //PF_getsurfacepoint, // #435 vector(entity e, float s, float n) getsurfacepoint = #435;
6584 void VM_getsurfacepoint(void)
6588 msurface_t *surface;
6590 VM_SAFEPARMCOUNT(3, VM_getsurfacepoint);
6591 VectorClear(PRVM_G_VECTOR(OFS_RETURN));
6592 ed = PRVM_G_EDICT(OFS_PARM0);
6593 if (!(model = getmodel(ed)) || !(surface = getsurface(model, (int)PRVM_G_FLOAT(OFS_PARM1))))
6595 // note: this (incorrectly) assumes it is a simple polygon
6596 pointnum = (int)PRVM_G_FLOAT(OFS_PARM2);
6597 if (pointnum < 0 || pointnum >= surface->num_vertices)
6599 animatemodel(model, ed);
6600 applytransform_forward(&(animatemodel_cache.data_vertex3f + 3 * surface->num_firstvertex)[pointnum * 3], ed, PRVM_G_VECTOR(OFS_RETURN));
6602 //PF_getsurfacepointattribute, // #486 vector(entity e, float s, float n, float a) getsurfacepointattribute = #486;
6603 // float SPA_POSITION = 0;
6604 // float SPA_S_AXIS = 1;
6605 // float SPA_T_AXIS = 2;
6606 // float SPA_R_AXIS = 3; // same as SPA_NORMAL
6607 // float SPA_TEXCOORDS0 = 4;
6608 // float SPA_LIGHTMAP0_TEXCOORDS = 5;
6609 // float SPA_LIGHTMAP0_COLOR = 6;
6610 void VM_getsurfacepointattribute(void)
6614 msurface_t *surface;
6618 VM_SAFEPARMCOUNT(4, VM_getsurfacepoint);
6619 VectorClear(PRVM_G_VECTOR(OFS_RETURN));
6620 ed = PRVM_G_EDICT(OFS_PARM0);
6621 if (!(model = getmodel(ed)) || !(surface = getsurface(model, (int)PRVM_G_FLOAT(OFS_PARM1))))
6623 pointnum = (int)PRVM_G_FLOAT(OFS_PARM2);
6624 if (pointnum < 0 || pointnum >= surface->num_vertices)
6626 attributetype = (int) PRVM_G_FLOAT(OFS_PARM3);
6628 animatemodel(model, ed);
6630 switch( attributetype ) {
6631 // float SPA_POSITION = 0;
6633 applytransform_forward(&(animatemodel_cache.data_vertex3f + 3 * surface->num_firstvertex)[pointnum * 3], ed, PRVM_G_VECTOR(OFS_RETURN));
6635 // float SPA_S_AXIS = 1;
6637 applytransform_forward_direction(&(animatemodel_cache.data_svector3f + 3 * surface->num_firstvertex)[pointnum * 3], ed, PRVM_G_VECTOR(OFS_RETURN));
6639 // float SPA_T_AXIS = 2;
6641 applytransform_forward_direction(&(animatemodel_cache.data_tvector3f + 3 * surface->num_firstvertex)[pointnum * 3], ed, PRVM_G_VECTOR(OFS_RETURN));
6643 // float SPA_R_AXIS = 3; // same as SPA_NORMAL
6645 applytransform_forward_direction(&(animatemodel_cache.data_normal3f + 3 * surface->num_firstvertex)[pointnum * 3], ed, PRVM_G_VECTOR(OFS_RETURN));
6647 // float SPA_TEXCOORDS0 = 4;
6649 float *ret = PRVM_G_VECTOR(OFS_RETURN);
6650 float *texcoord = &(model->surfmesh.data_texcoordtexture2f + 2 * surface->num_firstvertex)[pointnum * 2];
6651 ret[0] = texcoord[0];
6652 ret[1] = texcoord[1];
6656 // float SPA_LIGHTMAP0_TEXCOORDS = 5;
6658 float *ret = PRVM_G_VECTOR(OFS_RETURN);
6659 float *texcoord = &(model->surfmesh.data_texcoordlightmap2f + 2 * surface->num_firstvertex)[pointnum * 2];
6660 ret[0] = texcoord[0];
6661 ret[1] = texcoord[1];
6665 // float SPA_LIGHTMAP0_COLOR = 6;
6667 // ignore alpha for now..
6668 VectorCopy( &(model->surfmesh.data_lightmapcolor4f + 4 * surface->num_firstvertex)[pointnum * 4], PRVM_G_VECTOR(OFS_RETURN));
6671 VectorSet( PRVM_G_VECTOR(OFS_RETURN), 0.0f, 0.0f, 0.0f );
6675 //PF_getsurfacenormal, // #436 vector(entity e, float s) getsurfacenormal = #436;
6676 void VM_getsurfacenormal(void)
6679 msurface_t *surface;
6681 VM_SAFEPARMCOUNT(2, VM_getsurfacenormal);
6682 VectorClear(PRVM_G_VECTOR(OFS_RETURN));
6683 if (!(model = getmodel(PRVM_G_EDICT(OFS_PARM0))) || !(surface = getsurface(model, (int)PRVM_G_FLOAT(OFS_PARM1))))
6685 // note: this only returns the first triangle, so it doesn't work very
6686 // well for curved surfaces or arbitrary meshes
6687 animatemodel(model, PRVM_G_EDICT(OFS_PARM0));
6688 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);
6689 applytransform_forward_normal(normal, PRVM_G_EDICT(OFS_PARM0), PRVM_G_VECTOR(OFS_RETURN));
6690 VectorNormalize(PRVM_G_VECTOR(OFS_RETURN));
6692 //PF_getsurfacetexture, // #437 string(entity e, float s) getsurfacetexture = #437;
6693 void VM_getsurfacetexture(void)
6696 msurface_t *surface;
6697 VM_SAFEPARMCOUNT(2, VM_getsurfacetexture);
6698 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
6699 if (!(model = getmodel(PRVM_G_EDICT(OFS_PARM0))) || !(surface = getsurface(model, (int)PRVM_G_FLOAT(OFS_PARM1))))
6701 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(surface->texture->name);
6703 //PF_getsurfacenearpoint, // #438 float(entity e, vector p) getsurfacenearpoint = #438;
6704 void VM_getsurfacenearpoint(void)
6706 int surfacenum, best;
6708 vec_t dist, bestdist;
6711 msurface_t *surface;
6713 VM_SAFEPARMCOUNT(2, VM_getsurfacenearpoint);
6714 PRVM_G_FLOAT(OFS_RETURN) = -1;
6715 ed = PRVM_G_EDICT(OFS_PARM0);
6716 point = PRVM_G_VECTOR(OFS_PARM1);
6718 if (!ed || ed->priv.server->free)
6720 model = getmodel(ed);
6721 if (!model || !model->num_surfaces)
6724 animatemodel(model, ed);
6726 applytransform_inverted(point, ed, p);
6728 bestdist = 1000000000;
6729 for (surfacenum = 0;surfacenum < model->nummodelsurfaces;surfacenum++)
6731 surface = model->data_surfaces + surfacenum + model->firstmodelsurface;
6732 // first see if the nearest point on the surface's box is closer than the previous match
6733 clipped[0] = bound(surface->mins[0], p[0], surface->maxs[0]) - p[0];
6734 clipped[1] = bound(surface->mins[1], p[1], surface->maxs[1]) - p[1];
6735 clipped[2] = bound(surface->mins[2], p[2], surface->maxs[2]) - p[2];
6736 dist = VectorLength2(clipped);
6737 if (dist < bestdist)
6739 // it is, check the nearest point on the actual geometry
6740 clippointtosurface(ed, model, surface, p, clipped);
6741 VectorSubtract(clipped, p, clipped);
6742 dist += VectorLength2(clipped);
6743 if (dist < bestdist)
6745 // that's closer too, store it as the best match
6751 PRVM_G_FLOAT(OFS_RETURN) = best;
6753 //PF_getsurfaceclippedpoint, // #439 vector(entity e, float s, vector p) getsurfaceclippedpoint = #439;
6754 void VM_getsurfaceclippedpoint(void)
6758 msurface_t *surface;
6760 VM_SAFEPARMCOUNT(3, VM_te_getsurfaceclippedpoint);
6761 VectorClear(PRVM_G_VECTOR(OFS_RETURN));
6762 ed = PRVM_G_EDICT(OFS_PARM0);
6763 if (!(model = getmodel(ed)) || !(surface = getsurface(model, (int)PRVM_G_FLOAT(OFS_PARM1))))
6765 animatemodel(model, ed);
6766 applytransform_inverted(PRVM_G_VECTOR(OFS_PARM2), ed, p);
6767 clippointtosurface(ed, model, surface, p, out);
6768 VectorAdd(out, PRVM_serveredictvector(ed, origin), PRVM_G_VECTOR(OFS_RETURN));
6771 //PF_getsurfacenumtriangles, // #??? float(entity e, float s) getsurfacenumtriangles = #???;
6772 void VM_getsurfacenumtriangles(void)
6775 msurface_t *surface;
6776 VM_SAFEPARMCOUNT(2, VM_SV_getsurfacenumtriangles);
6777 // return 0 if no such surface
6778 if (!(model = getmodel(PRVM_G_EDICT(OFS_PARM0))) || !(surface = getsurface(model, (int)PRVM_G_FLOAT(OFS_PARM1))))
6780 PRVM_G_FLOAT(OFS_RETURN) = 0;
6784 // note: this (incorrectly) assumes it is a simple polygon
6785 PRVM_G_FLOAT(OFS_RETURN) = surface->num_triangles;
6787 //PF_getsurfacetriangle, // #??? vector(entity e, float s, float n) getsurfacetriangle = #???;
6788 void VM_getsurfacetriangle(void)
6790 const vec3_t d = {-1, -1, -1};
6793 msurface_t *surface;
6795 VM_SAFEPARMCOUNT(3, VM_SV_getsurfacetriangle);
6796 VectorClear(PRVM_G_VECTOR(OFS_RETURN));
6797 ed = PRVM_G_EDICT(OFS_PARM0);
6798 if (!(model = getmodel(ed)) || !(surface = getsurface(model, (int)PRVM_G_FLOAT(OFS_PARM1))))
6800 trinum = (int)PRVM_G_FLOAT(OFS_PARM2);
6801 if (trinum < 0 || trinum >= surface->num_triangles)
6803 // FIXME: implement rotation/scaling
6804 VectorMA(&(model->surfmesh.data_element3i + 3 * surface->num_firsttriangle)[trinum * 3], surface->num_firstvertex, d, PRVM_G_VECTOR(OFS_RETURN));
6811 void World_Physics_ApplyCmd(prvm_edict_t *ed, edict_odefunc_t *f);
6813 #define VM_physics_ApplyCmd(ed,f) if (!ed->priv.server->ode_body) VM_physics_newstackfunction(ed, f); else World_Physics_ApplyCmd(ed, f)
6815 edict_odefunc_t *VM_physics_newstackfunction(prvm_edict_t *ed, edict_odefunc_t *f)
6817 edict_odefunc_t *newfunc, *func;
6819 newfunc = (edict_odefunc_t *)Mem_Alloc(prog->progs_mempool, sizeof(edict_odefunc_t));
6820 memcpy(newfunc, f, sizeof(edict_odefunc_t));
6821 newfunc->next = NULL;
6822 if (!ed->priv.server->ode_func)
6823 ed->priv.server->ode_func = newfunc;
6826 for (func = ed->priv.server->ode_func; func->next; func = func->next);
6827 func->next = newfunc;
6832 // void(entity e, float physics_enabled) physics_enable = #;
6833 void VM_physics_enable(void)
6838 VM_SAFEPARMCOUNT(2, VM_physics_enable);
6839 ed = PRVM_G_EDICT(OFS_PARM0);
6842 if (developer.integer > 0)
6843 VM_Warning("VM_physics_enable: null entity!\n");
6846 // entity should have MOVETYPE_PHYSICS already set, this can damage memory (making leaked allocation) so warn about this even if non-developer
6847 if (PRVM_serveredictfloat(ed, movetype) != MOVETYPE_PHYSICS)
6849 VM_Warning("VM_physics_enable: entity is not MOVETYPE_PHYSICS!\n");
6852 f.type = PRVM_G_FLOAT(OFS_PARM1) == 0 ? ODEFUNC_DISABLE : ODEFUNC_ENABLE;
6853 VM_physics_ApplyCmd(ed, &f);
6856 // void(entity e, vector force, vector relative_ofs) physics_addforce = #;
6857 void VM_physics_addforce(void)
6862 VM_SAFEPARMCOUNT(3, VM_physics_addforce);
6863 ed = PRVM_G_EDICT(OFS_PARM0);
6866 if (developer.integer > 0)
6867 VM_Warning("VM_physics_addforce: null entity!\n");
6870 // entity should have MOVETYPE_PHYSICS already set, this can damage memory (making leaked allocation) so warn about this even if non-developer
6871 if (PRVM_serveredictfloat(ed, movetype) != MOVETYPE_PHYSICS)
6873 VM_Warning("VM_physics_addforce: entity is not MOVETYPE_PHYSICS!\n");
6876 f.type = ODEFUNC_RELFORCEATPOS;
6877 VectorCopy(PRVM_G_VECTOR(OFS_PARM1), f.v1);
6878 VectorSubtract(PRVM_serveredictvector(ed, origin), PRVM_G_VECTOR(OFS_PARM2), f.v2);
6879 VM_physics_ApplyCmd(ed, &f);
6882 // void(entity e, vector torque) physics_addtorque = #;
6883 void VM_physics_addtorque(void)
6888 VM_SAFEPARMCOUNT(2, VM_physics_addtorque);
6889 ed = PRVM_G_EDICT(OFS_PARM0);
6892 if (developer.integer > 0)
6893 VM_Warning("VM_physics_addtorque: null entity!\n");
6896 // entity should have MOVETYPE_PHYSICS already set, this can damage memory (making leaked allocation) so warn about this even if non-developer
6897 if (PRVM_serveredictfloat(ed, movetype) != MOVETYPE_PHYSICS)
6899 VM_Warning("VM_physics_addtorque: entity is not MOVETYPE_PHYSICS!\n");
6902 f.type = ODEFUNC_RELTORQUE;
6903 VectorCopy(PRVM_G_VECTOR(OFS_PARM1), f.v1);
6904 VM_physics_ApplyCmd(ed, &f);