2 // Basically every vm builtin cmd should be in here.
3 // All 3 builtin and extension lists can be found here
4 // cause large (I think they will) parts are from pr_cmds the same copyright like in pr_cmds
13 #include "cl_collision.h"
14 #include "clvm_cmds.h"
17 extern cvar_t prvm_backtraceforwarnings;
19 // LordHavoc: changed this to NOT use a return statement, so that it can be used in functions that must return a value
20 void VM_Warning(const char *fmt, ...)
23 char msg[MAX_INPUTLINE];
24 static double recursive = -1;
27 dpvsnprintf(msg,sizeof(msg),fmt,argptr);
32 // TODO: either add a cvar/cmd to control the state dumping or replace some of the calls with Con_Printf [9/13/2006 Black]
33 if(prvm_backtraceforwarnings.integer && recursive != realtime) // NOTE: this compares to the time, just in case if PRVM_PrintState causes a Host_Error and keeps recursive set
42 //============================================================================
45 // TODO DONE: move vm_files and vm_fssearchlist to prvm_prog_t struct
46 // TODO: move vm_files and vm_fssearchlist back [9/13/2006 Black]
47 // TODO: (move vm_files and vm_fssearchlist to prvm_prog_t struct again) [2007-01-23 LordHavoc]
48 // TODO: will this war ever end? [2007-01-23 LordHavoc]
50 void VM_CheckEmptyString (const char *s)
52 if (ISWHITESPACE(s[0]))
53 PRVM_ERROR ("%s: Bad string", PRVM_NAME);
56 void VM_GenerateFrameGroupBlend(framegroupblend_t *framegroupblend, const prvm_edict_t *ed)
58 // self.frame is the interpolation target (new frame)
59 // self.frame1time is the animation base time for the interpolation target
60 // self.frame2 is the interpolation start (previous frame)
61 // self.frame2time is the animation base time for the interpolation start
62 // self.lerpfrac is the interpolation strength for self.frame2
63 // self.lerpfrac3 is the interpolation strength for self.frame3
64 // self.lerpfrac4 is the interpolation strength for self.frame4
65 // pitch angle on a player model where the animator set up 5 sets of
66 // animations and the csqc simply lerps between sets)
67 framegroupblend[0].frame = (int) PRVM_gameedictfloat(ed, frame );
68 framegroupblend[1].frame = (int) PRVM_gameedictfloat(ed, frame2 );
69 framegroupblend[2].frame = (int) PRVM_gameedictfloat(ed, frame3 );
70 framegroupblend[3].frame = (int) PRVM_gameedictfloat(ed, frame4 );
71 framegroupblend[0].start = PRVM_gameedictfloat(ed, frame1time);
72 framegroupblend[1].start = PRVM_gameedictfloat(ed, frame2time);
73 framegroupblend[2].start = PRVM_gameedictfloat(ed, frame3time);
74 framegroupblend[3].start = PRVM_gameedictfloat(ed, frame4time);
75 framegroupblend[1].lerp = PRVM_gameedictfloat(ed, lerpfrac );
76 framegroupblend[2].lerp = PRVM_gameedictfloat(ed, lerpfrac3 );
77 framegroupblend[3].lerp = PRVM_gameedictfloat(ed, lerpfrac4 );
78 // assume that the (missing) lerpfrac1 is whatever remains after lerpfrac2+lerpfrac3+lerpfrac4 are summed
79 framegroupblend[0].lerp = 1 - framegroupblend[1].lerp - framegroupblend[2].lerp - framegroupblend[3].lerp;
82 // LordHavoc: quite tempting to break apart this function to reuse the
83 // duplicated code, but I suspect it is better for performance
85 void VM_FrameBlendFromFrameGroupBlend(frameblend_t *frameblend, const framegroupblend_t *framegroupblend, const dp_model_t *model)
87 int sub2, numframes, f, i, k;
88 int isfirstframegroup = true;
90 double sublerp, lerp, d;
91 const animscene_t *scene;
92 const framegroupblend_t *g;
93 frameblend_t *blend = frameblend;
95 memset(blend, 0, MAX_FRAMEBLENDS * sizeof(*blend));
97 if (!model || !model->surfmesh.isanimated)
103 nolerp = (model->type == mod_sprite) ? !r_lerpsprites.integer : !r_lerpmodels.integer;
104 numframes = model->numframes;
105 for (k = 0, g = framegroupblend;k < MAX_FRAMEGROUPBLENDS;k++, g++)
108 if ((unsigned int)f >= (unsigned int)numframes)
110 Con_DPrintf("VM_FrameBlendFromFrameGroupBlend: no such frame %d in model %s\n", f, model->name);
118 if (isfirstframegroup)
121 isfirstframegroup = false;
126 if (model->animscenes)
128 scene = model->animscenes + f;
129 f = scene->firstframe;
130 if (scene->framecount > 1)
132 // this code path is only used on .zym models and torches
133 sublerp = scene->framerate * (cl.time - g->start);
134 f = (int) floor(sublerp);
137 if (sublerp < (1.0 / 65536.0f))
139 if (sublerp > (65535.0f / 65536.0f))
145 f = (f % scene->framecount);
146 sub2 = (sub2 % scene->framecount);
148 f = bound(0, f, (scene->framecount - 1)) + scene->firstframe;
149 sub2 = bound(0, sub2, (scene->framecount - 1)) + scene->firstframe;
151 // two framelerps produced from one animation
154 for (i = 0;i < MAX_FRAMEBLENDS;i++)
156 if (blend[i].lerp <= 0 || blend[i].subframe == sub2)
158 blend[i].subframe = sub2;
164 d = (1 - sublerp) * lerp;
169 for (i = 0;i < MAX_FRAMEBLENDS;i++)
171 if (blend[i].lerp <= 0 || blend[i].subframe == f)
173 blend[i].subframe = f;
182 void VM_UpdateEdictSkeleton(prvm_edict_t *ed, const dp_model_t *edmodel, const frameblend_t *frameblend)
184 if (ed->priv.server->skeleton.model != edmodel)
186 VM_RemoveEdictSkeleton(ed);
187 ed->priv.server->skeleton.model = edmodel;
189 if (!ed->priv.server->skeleton.model || !ed->priv.server->skeleton.model->num_bones)
191 if(ed->priv.server->skeleton.relativetransforms)
192 Mem_Free(ed->priv.server->skeleton.relativetransforms);
193 ed->priv.server->skeleton.relativetransforms = NULL;
198 int skeletonindex = -1;
199 skeleton_t *skeleton;
200 skeletonindex = (int)PRVM_gameedictfloat(ed, skeletonindex) - 1;
201 if (skeletonindex >= 0 && skeletonindex < MAX_EDICTS && (skeleton = prog->skeletons[skeletonindex]) && skeleton->model->num_bones == ed->priv.server->skeleton.model->num_bones)
203 // custom skeleton controlled by the game (FTE_CSQC_SKELETONOBJECTS)
204 if (!ed->priv.server->skeleton.relativetransforms)
205 ed->priv.server->skeleton.relativetransforms = (matrix4x4_t *)Mem_Alloc(prog->progs_mempool, ed->priv.server->skeleton.model->num_bones * sizeof(matrix4x4_t));
206 memcpy(ed->priv.server->skeleton.relativetransforms, skeleton->relativetransforms, ed->priv.server->skeleton.model->num_bones * sizeof(matrix4x4_t));
210 if(ed->priv.server->skeleton.relativetransforms)
211 Mem_Free(ed->priv.server->skeleton.relativetransforms);
212 ed->priv.server->skeleton.relativetransforms = NULL;
217 void VM_RemoveEdictSkeleton(prvm_edict_t *ed)
219 if (ed->priv.server->skeleton.relativetransforms)
220 Mem_Free(ed->priv.server->skeleton.relativetransforms);
221 memset(&ed->priv.server->skeleton, 0, sizeof(ed->priv.server->skeleton));
227 //============================================================================
230 void VM_VarString(int first, char *out, int outlength)
236 outend = out + outlength - 1;
237 for (i = first;i < prog->argc && out < outend;i++)
239 s = PRVM_G_STRING((OFS_PARM0+i*3));
240 while (out < outend && *s)
250 returns true if the extension is supported by the server
252 checkextension(extensionname)
256 // kind of helper function
257 static qboolean checkextension(const char *name)
260 const char *e, *start;
261 len = (int)strlen(name);
263 for (e = prog->extensionstring;*e;e++)
270 while (*e && *e != ' ')
272 if ((e - start) == len && !strncasecmp(start, name, len))
274 // special sheck for ODE
275 if (!strncasecmp("DP_PHYSICS_ODE", name, 14))
278 return ode_dll ? true : false;
289 void VM_checkextension (void)
291 VM_SAFEPARMCOUNT(1,VM_checkextension);
293 PRVM_G_FLOAT(OFS_RETURN) = checkextension(PRVM_G_STRING(OFS_PARM0));
300 This is a TERMINAL error, which will kill off the entire prog.
309 char string[VM_STRINGTEMP_LENGTH];
311 VM_VarString(0, string, sizeof(string));
312 Con_Printf("======%s ERROR in %s:\n%s\n", PRVM_NAME, PRVM_GetString(prog->xfunction->s_name), string);
313 ed = PRVM_PROG_TO_EDICT(PRVM_allglobaledict(self));
314 PRVM_ED_Print(ed, NULL);
316 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);
323 Dumps out self, then an error message. The program is aborted and self is
324 removed, but the level can continue.
329 void VM_objerror (void)
332 char string[VM_STRINGTEMP_LENGTH];
334 VM_VarString(0, string, sizeof(string));
335 Con_Printf("======OBJECT ERROR======\n"); // , PRVM_NAME, PRVM_GetString(prog->xfunction->s_name), string); // or include them? FIXME
336 ed = PRVM_PROG_TO_EDICT(PRVM_allglobaledict(self));
337 PRVM_ED_Print(ed, NULL);
339 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);
353 char string[VM_STRINGTEMP_LENGTH];
355 VM_VarString(0, string, sizeof(string));
363 broadcast print to everyone on server
368 void VM_bprint (void)
370 char string[VM_STRINGTEMP_LENGTH];
374 VM_Warning("VM_bprint: game is not server(%s) !\n", PRVM_NAME);
378 VM_VarString(0, string, sizeof(string));
379 SV_BroadcastPrint(string);
384 VM_sprint (menu & client but only if server.active == true)
386 single print to a specific client
388 sprint(float clientnum,...[string])
391 void VM_sprint (void)
395 char string[VM_STRINGTEMP_LENGTH];
397 VM_SAFEPARMCOUNTRANGE(1, 8, VM_sprint);
399 //find client for this entity
400 clientnum = (int)PRVM_G_FLOAT(OFS_PARM0);
401 if (!sv.active || clientnum < 0 || clientnum >= svs.maxclients || !svs.clients[clientnum].active)
403 VM_Warning("VM_sprint: %s: invalid client or server is not active !\n", PRVM_NAME);
407 client = svs.clients + clientnum;
408 if (!client->netconnection)
411 VM_VarString(1, string, sizeof(string));
412 MSG_WriteChar(&client->netconnection->message,svc_print);
413 MSG_WriteString(&client->netconnection->message, string);
420 single print to the screen
425 void VM_centerprint (void)
427 char string[VM_STRINGTEMP_LENGTH];
429 VM_SAFEPARMCOUNTRANGE(1, 8, VM_centerprint);
430 VM_VarString(0, string, sizeof(string));
431 SCR_CenterPrint(string);
438 vector normalize(vector)
441 void VM_normalize (void)
447 VM_SAFEPARMCOUNT(1,VM_normalize);
449 value1 = PRVM_G_VECTOR(OFS_PARM0);
451 f = VectorLength2(value1);
455 VectorScale(value1, f, newvalue);
458 VectorClear(newvalue);
460 VectorCopy (newvalue, PRVM_G_VECTOR(OFS_RETURN));
472 VM_SAFEPARMCOUNT(1,VM_vlen);
473 PRVM_G_FLOAT(OFS_RETURN) = VectorLength(PRVM_G_VECTOR(OFS_PARM0));
480 float vectoyaw(vector)
483 void VM_vectoyaw (void)
488 VM_SAFEPARMCOUNT(1,VM_vectoyaw);
490 value1 = PRVM_G_VECTOR(OFS_PARM0);
492 if (value1[1] == 0 && value1[0] == 0)
496 yaw = (int) (atan2(value1[1], value1[0]) * 180 / M_PI);
501 PRVM_G_FLOAT(OFS_RETURN) = yaw;
509 vector vectoangles(vector[, vector])
512 void VM_vectoangles (void)
514 VM_SAFEPARMCOUNTRANGE(1, 2,VM_vectoangles);
516 AnglesFromVectors(PRVM_G_VECTOR(OFS_RETURN), PRVM_G_VECTOR(OFS_PARM0), prog->argc >= 2 ? PRVM_G_VECTOR(OFS_PARM1) : NULL, true);
523 Returns a number from 0<= num < 1
528 void VM_random (void)
530 VM_SAFEPARMCOUNT(0,VM_random);
532 PRVM_G_FLOAT(OFS_RETURN) = lhrandom(0, 1);
539 localsound(string sample)
542 void VM_localsound(void)
546 VM_SAFEPARMCOUNT(1,VM_localsound);
548 s = PRVM_G_STRING(OFS_PARM0);
550 if(!S_LocalSound (s))
552 PRVM_G_FLOAT(OFS_RETURN) = -4;
553 VM_Warning("VM_localsound: Failed to play %s for %s !\n", s, PRVM_NAME);
557 PRVM_G_FLOAT(OFS_RETURN) = 1;
569 PRVM_ERROR ("%s: break statement", PRVM_NAME);
572 //============================================================================
578 Sends text over to the client's execution buffer
580 [localcmd (string, ...) or]
584 void VM_localcmd (void)
586 char string[VM_STRINGTEMP_LENGTH];
587 VM_SAFEPARMCOUNTRANGE(1, 8, VM_localcmd);
588 VM_VarString(0, string, sizeof(string));
589 Cbuf_AddText(string);
592 static qboolean PRVM_Cvar_ReadOk(const char *string)
595 cvar = Cvar_FindVar(string);
596 return ((cvar) && ((cvar->flags & CVAR_PRIVATE) == 0));
608 char string[VM_STRINGTEMP_LENGTH];
609 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar);
610 VM_VarString(0, string, sizeof(string));
611 VM_CheckEmptyString(string);
612 PRVM_G_FLOAT(OFS_RETURN) = PRVM_Cvar_ReadOk(string) ? Cvar_VariableValue(string) : 0;
619 float cvar_type (string)
620 float CVAR_TYPEFLAG_EXISTS = 1;
621 float CVAR_TYPEFLAG_SAVED = 2;
622 float CVAR_TYPEFLAG_PRIVATE = 4;
623 float CVAR_TYPEFLAG_ENGINE = 8;
624 float CVAR_TYPEFLAG_HASDESCRIPTION = 16;
625 float CVAR_TYPEFLAG_READONLY = 32;
628 void VM_cvar_type (void)
630 char string[VM_STRINGTEMP_LENGTH];
634 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar);
635 VM_VarString(0, string, sizeof(string));
636 VM_CheckEmptyString(string);
637 cvar = Cvar_FindVar(string);
642 PRVM_G_FLOAT(OFS_RETURN) = 0;
643 return; // CVAR_TYPE_NONE
646 ret = 1; // CVAR_EXISTS
647 if(cvar->flags & CVAR_SAVE)
648 ret |= 2; // CVAR_TYPE_SAVED
649 if(cvar->flags & CVAR_PRIVATE)
650 ret |= 4; // CVAR_TYPE_PRIVATE
651 if(!(cvar->flags & CVAR_ALLOCATED))
652 ret |= 8; // CVAR_TYPE_ENGINE
653 if(cvar->description != cvar_dummy_description)
654 ret |= 16; // CVAR_TYPE_HASDESCRIPTION
655 if(cvar->flags & CVAR_READONLY)
656 ret |= 32; // CVAR_TYPE_READONLY
658 PRVM_G_FLOAT(OFS_RETURN) = ret;
665 const string VM_cvar_string (string, ...)
668 void VM_cvar_string(void)
670 char string[VM_STRINGTEMP_LENGTH];
671 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar_string);
672 VM_VarString(0, string, sizeof(string));
673 VM_CheckEmptyString(string);
674 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(PRVM_Cvar_ReadOk(string) ? Cvar_VariableString(string) : "");
679 ========================
682 const string VM_cvar_defstring (string, ...)
683 ========================
685 void VM_cvar_defstring (void)
687 char string[VM_STRINGTEMP_LENGTH];
688 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar_defstring);
689 VM_VarString(0, string, sizeof(string));
690 VM_CheckEmptyString(string);
691 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(Cvar_VariableDefString(string));
695 ========================
698 const string VM_cvar_description (string, ...)
699 ========================
701 void VM_cvar_description (void)
703 char string[VM_STRINGTEMP_LENGTH];
704 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar_description);
705 VM_VarString(0, string, sizeof(string));
706 VM_CheckEmptyString(string);
707 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(Cvar_VariableDescription(string));
713 void cvar_set (string,string, ...)
716 void VM_cvar_set (void)
719 char string[VM_STRINGTEMP_LENGTH];
720 VM_SAFEPARMCOUNTRANGE(2,8,VM_cvar_set);
721 VM_VarString(1, string, sizeof(string));
722 name = PRVM_G_STRING(OFS_PARM0);
723 VM_CheckEmptyString(name);
724 Cvar_Set(name, string);
734 void VM_dprint (void)
736 char string[VM_STRINGTEMP_LENGTH];
737 VM_SAFEPARMCOUNTRANGE(1, 8, VM_dprint);
738 VM_VarString(0, string, sizeof(string));
740 Con_DPrintf("%s", string);
742 Con_DPrintf("%s: %s", PRVM_NAME, string);
759 VM_SAFEPARMCOUNT(1, VM_ftos);
761 v = PRVM_G_FLOAT(OFS_PARM0);
763 if ((float)((int)v) == v)
764 dpsnprintf(s, sizeof(s), "%i", (int)v);
766 dpsnprintf(s, sizeof(s), "%f", v);
767 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(s);
782 VM_SAFEPARMCOUNT(1,VM_fabs);
784 v = PRVM_G_FLOAT(OFS_PARM0);
785 PRVM_G_FLOAT(OFS_RETURN) = fabs(v);
800 VM_SAFEPARMCOUNT(1,VM_vtos);
802 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]);
803 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(s);
818 VM_SAFEPARMCOUNT(1, VM_etos);
820 dpsnprintf (s, sizeof(s), "entity %i", PRVM_G_EDICTNUM(OFS_PARM0));
821 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(s);
828 float stof(...[string])
833 char string[VM_STRINGTEMP_LENGTH];
834 VM_SAFEPARMCOUNTRANGE(1, 8, VM_stof);
835 VM_VarString(0, string, sizeof(string));
836 PRVM_G_FLOAT(OFS_RETURN) = atof(string);
840 ========================
844 ========================
848 VM_SAFEPARMCOUNT(1, VM_itof);
849 PRVM_G_FLOAT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
853 ========================
856 entity ftoe(float num)
857 ========================
862 VM_SAFEPARMCOUNT(1, VM_ftoe);
864 ent = (int)PRVM_G_FLOAT(OFS_PARM0);
865 if (ent < 0 || ent >= prog->max_edicts || PRVM_PROG_TO_EDICT(ent)->priv.required->free)
866 ent = 0; // return world instead of a free or invalid entity
868 PRVM_G_INT(OFS_RETURN) = ent;
872 ========================
875 float etof(entity ent)
876 ========================
880 VM_SAFEPARMCOUNT(1, VM_etof);
881 PRVM_G_FLOAT(OFS_RETURN) = PRVM_G_EDICTNUM(OFS_PARM0);
888 string strftime(float uselocaltime, string[, string ...])
891 void VM_strftime(void)
900 char fmt[VM_STRINGTEMP_LENGTH];
901 char result[VM_STRINGTEMP_LENGTH];
902 VM_SAFEPARMCOUNTRANGE(2, 8, VM_strftime);
903 VM_VarString(1, fmt, sizeof(fmt));
906 if (PRVM_G_FLOAT(OFS_PARM0))
907 tmresult = localtime_s(&tm, &t);
909 tmresult = gmtime_s(&tm, &t);
912 if (PRVM_G_FLOAT(OFS_PARM0))
919 PRVM_G_INT(OFS_RETURN) = 0;
923 strftime(result, sizeof(result), fmt, &tm);
925 strftime(result, sizeof(result), fmt, tm);
927 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(result);
941 VM_SAFEPARMCOUNT(0, VM_spawn);
942 prog->xfunction->builtinsprofile += 20;
943 ed = PRVM_ED_Alloc();
955 void VM_remove (void)
958 prog->xfunction->builtinsprofile += 20;
960 VM_SAFEPARMCOUNT(1, VM_remove);
962 ed = PRVM_G_EDICT(OFS_PARM0);
963 if( PRVM_NUM_FOR_EDICT(ed) <= prog->reserved_edicts )
965 if (developer.integer > 0)
966 VM_Warning( "VM_remove: tried to remove the null entity or a reserved entity!\n" );
968 else if( ed->priv.required->free )
970 if (developer.integer > 0)
971 VM_Warning( "VM_remove: tried to remove an already freed entity!\n" );
981 entity find(entity start, .string field, string match)
992 VM_SAFEPARMCOUNT(3,VM_find);
994 e = PRVM_G_EDICTNUM(OFS_PARM0);
995 f = PRVM_G_INT(OFS_PARM1);
996 s = PRVM_G_STRING(OFS_PARM2);
998 // LordHavoc: apparently BloodMage does a find(world, weaponmodel, "") and
999 // expects it to find all the monsters, so we must be careful to support
1002 for (e++ ; e < prog->num_edicts ; e++)
1004 prog->xfunction->builtinsprofile++;
1005 ed = PRVM_EDICT_NUM(e);
1006 if (ed->priv.required->free)
1008 t = PRVM_E_STRING(ed,f);
1013 VM_RETURN_EDICT(ed);
1018 VM_RETURN_EDICT(prog->edicts);
1025 entity findfloat(entity start, .float field, float match)
1026 entity findentity(entity start, .entity field, entity match)
1029 // LordHavoc: added this for searching float, int, and entity reference fields
1030 void VM_findfloat (void)
1037 VM_SAFEPARMCOUNT(3,VM_findfloat);
1039 e = PRVM_G_EDICTNUM(OFS_PARM0);
1040 f = PRVM_G_INT(OFS_PARM1);
1041 s = PRVM_G_FLOAT(OFS_PARM2);
1043 for (e++ ; e < prog->num_edicts ; e++)
1045 prog->xfunction->builtinsprofile++;
1046 ed = PRVM_EDICT_NUM(e);
1047 if (ed->priv.required->free)
1049 if (PRVM_E_FLOAT(ed,f) == s)
1051 VM_RETURN_EDICT(ed);
1056 VM_RETURN_EDICT(prog->edicts);
1063 entity findchain(.string field, string match)
1066 // chained search for strings in entity fields
1067 // entity(.string field, string match) findchain = #402;
1068 void VM_findchain (void)
1073 prvm_edict_t *ent, *chain;
1076 VM_SAFEPARMCOUNTRANGE(2,3,VM_findchain);
1079 chainfield = PRVM_G_INT(OFS_PARM2);
1081 chainfield = prog->fieldoffsets.chain;
1083 PRVM_ERROR("VM_findchain: %s doesnt have the specified chain field !", PRVM_NAME);
1085 chain = prog->edicts;
1087 f = PRVM_G_INT(OFS_PARM0);
1088 s = PRVM_G_STRING(OFS_PARM1);
1090 // LordHavoc: apparently BloodMage does a find(world, weaponmodel, "") and
1091 // expects it to find all the monsters, so we must be careful to support
1094 ent = PRVM_NEXT_EDICT(prog->edicts);
1095 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
1097 prog->xfunction->builtinsprofile++;
1098 if (ent->priv.required->free)
1100 t = PRVM_E_STRING(ent,f);
1106 PRVM_EDICTFIELDEDICT(ent,chainfield) = PRVM_NUM_FOR_EDICT(chain);
1110 VM_RETURN_EDICT(chain);
1117 entity findchainfloat(.string field, float match)
1118 entity findchainentity(.string field, entity match)
1121 // LordHavoc: chained search for float, int, and entity reference fields
1122 // entity(.string field, float match) findchainfloat = #403;
1123 void VM_findchainfloat (void)
1128 prvm_edict_t *ent, *chain;
1131 VM_SAFEPARMCOUNTRANGE(2, 3, VM_findchainfloat);
1134 chainfield = PRVM_G_INT(OFS_PARM2);
1136 chainfield = prog->fieldoffsets.chain;
1138 PRVM_ERROR("VM_findchain: %s doesnt have the specified chain field !", PRVM_NAME);
1140 chain = (prvm_edict_t *)prog->edicts;
1142 f = PRVM_G_INT(OFS_PARM0);
1143 s = PRVM_G_FLOAT(OFS_PARM1);
1145 ent = PRVM_NEXT_EDICT(prog->edicts);
1146 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
1148 prog->xfunction->builtinsprofile++;
1149 if (ent->priv.required->free)
1151 if (PRVM_E_FLOAT(ent,f) != s)
1154 PRVM_EDICTFIELDEDICT(ent,chainfield) = PRVM_EDICT_TO_PROG(chain);
1158 VM_RETURN_EDICT(chain);
1162 ========================
1165 entity findflags(entity start, .float field, float match)
1166 ========================
1168 // LordHavoc: search for flags in float fields
1169 void VM_findflags (void)
1176 VM_SAFEPARMCOUNT(3, VM_findflags);
1179 e = PRVM_G_EDICTNUM(OFS_PARM0);
1180 f = PRVM_G_INT(OFS_PARM1);
1181 s = (int)PRVM_G_FLOAT(OFS_PARM2);
1183 for (e++ ; e < prog->num_edicts ; e++)
1185 prog->xfunction->builtinsprofile++;
1186 ed = PRVM_EDICT_NUM(e);
1187 if (ed->priv.required->free)
1189 if (!PRVM_E_FLOAT(ed,f))
1191 if ((int)PRVM_E_FLOAT(ed,f) & s)
1193 VM_RETURN_EDICT(ed);
1198 VM_RETURN_EDICT(prog->edicts);
1202 ========================
1205 entity findchainflags(.float field, float match)
1206 ========================
1208 // LordHavoc: chained search for flags in float fields
1209 void VM_findchainflags (void)
1214 prvm_edict_t *ent, *chain;
1217 VM_SAFEPARMCOUNTRANGE(2, 3, VM_findchainflags);
1220 chainfield = PRVM_G_INT(OFS_PARM2);
1222 chainfield = prog->fieldoffsets.chain;
1224 PRVM_ERROR("VM_findchain: %s doesnt have the specified chain field !", PRVM_NAME);
1226 chain = (prvm_edict_t *)prog->edicts;
1228 f = PRVM_G_INT(OFS_PARM0);
1229 s = (int)PRVM_G_FLOAT(OFS_PARM1);
1231 ent = PRVM_NEXT_EDICT(prog->edicts);
1232 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
1234 prog->xfunction->builtinsprofile++;
1235 if (ent->priv.required->free)
1237 if (!PRVM_E_FLOAT(ent,f))
1239 if (!((int)PRVM_E_FLOAT(ent,f) & s))
1242 PRVM_EDICTFIELDEDICT(ent,chainfield) = PRVM_EDICT_TO_PROG(chain);
1246 VM_RETURN_EDICT(chain);
1253 string precache_sound (string sample)
1256 void VM_precache_sound (void)
1260 VM_SAFEPARMCOUNT(1, VM_precache_sound);
1262 s = PRVM_G_STRING(OFS_PARM0);
1263 PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
1264 //VM_CheckEmptyString(s);
1266 if(snd_initialized.integer && !S_PrecacheSound(s, true, true))
1268 VM_Warning("VM_precache_sound: Failed to load %s for %s\n", s, PRVM_NAME);
1277 returns the same string as output
1279 does nothing, only used by qcc to build .pak archives
1282 void VM_precache_file (void)
1284 VM_SAFEPARMCOUNT(1,VM_precache_file);
1285 // precache_file is only used to copy files with qcc, it does nothing
1286 PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
1296 void VM_coredump (void)
1298 VM_SAFEPARMCOUNT(0,VM_coredump);
1300 Cbuf_AddText("prvm_edicts ");
1301 Cbuf_AddText(PRVM_NAME);
1312 void PRVM_StackTrace(void);
1313 void VM_stackdump (void)
1315 VM_SAFEPARMCOUNT(0, VM_stackdump);
1330 VM_SAFEPARMCOUNT(0, VM_crash);
1332 PRVM_ERROR("Crash called by %s",PRVM_NAME);
1342 void VM_traceon (void)
1344 VM_SAFEPARMCOUNT(0,VM_traceon);
1356 void VM_traceoff (void)
1358 VM_SAFEPARMCOUNT(0,VM_traceoff);
1360 prog->trace = false;
1370 void VM_eprint (void)
1372 VM_SAFEPARMCOUNT(1,VM_eprint);
1374 PRVM_ED_PrintNum (PRVM_G_EDICTNUM(OFS_PARM0), NULL);
1387 VM_SAFEPARMCOUNT(1,VM_rint);
1389 f = PRVM_G_FLOAT(OFS_PARM0);
1391 PRVM_G_FLOAT(OFS_RETURN) = floor(f + 0.5);
1393 PRVM_G_FLOAT(OFS_RETURN) = ceil(f - 0.5);
1403 void VM_floor (void)
1405 VM_SAFEPARMCOUNT(1,VM_floor);
1407 PRVM_G_FLOAT(OFS_RETURN) = floor(PRVM_G_FLOAT(OFS_PARM0));
1419 VM_SAFEPARMCOUNT(1,VM_ceil);
1421 PRVM_G_FLOAT(OFS_RETURN) = ceil(PRVM_G_FLOAT(OFS_PARM0));
1429 entity nextent(entity)
1432 void VM_nextent (void)
1437 VM_SAFEPARMCOUNT(1, VM_nextent);
1439 i = PRVM_G_EDICTNUM(OFS_PARM0);
1442 prog->xfunction->builtinsprofile++;
1444 if (i == prog->num_edicts)
1446 VM_RETURN_EDICT(prog->edicts);
1449 ent = PRVM_EDICT_NUM(i);
1450 if (!ent->priv.required->free)
1452 VM_RETURN_EDICT(ent);
1458 //=============================================================================
1465 changelevel(string map)
1468 void VM_changelevel (void)
1470 VM_SAFEPARMCOUNT(1, VM_changelevel);
1474 VM_Warning("VM_changelevel: game is not server (%s)\n", PRVM_NAME);
1478 // make sure we don't issue two changelevels
1479 if (svs.changelevel_issued)
1481 svs.changelevel_issued = true;
1483 Cbuf_AddText (va("changelevel %s\n",PRVM_G_STRING(OFS_PARM0)));
1495 VM_SAFEPARMCOUNT(1,VM_sin);
1496 PRVM_G_FLOAT(OFS_RETURN) = sin(PRVM_G_FLOAT(OFS_PARM0));
1507 VM_SAFEPARMCOUNT(1,VM_cos);
1508 PRVM_G_FLOAT(OFS_RETURN) = cos(PRVM_G_FLOAT(OFS_PARM0));
1520 VM_SAFEPARMCOUNT(1,VM_sqrt);
1521 PRVM_G_FLOAT(OFS_RETURN) = sqrt(PRVM_G_FLOAT(OFS_PARM0));
1533 VM_SAFEPARMCOUNT(1,VM_asin);
1534 PRVM_G_FLOAT(OFS_RETURN) = asin(PRVM_G_FLOAT(OFS_PARM0));
1545 VM_SAFEPARMCOUNT(1,VM_acos);
1546 PRVM_G_FLOAT(OFS_RETURN) = acos(PRVM_G_FLOAT(OFS_PARM0));
1557 VM_SAFEPARMCOUNT(1,VM_atan);
1558 PRVM_G_FLOAT(OFS_RETURN) = atan(PRVM_G_FLOAT(OFS_PARM0));
1564 float atan2(float,float)
1567 void VM_atan2 (void)
1569 VM_SAFEPARMCOUNT(2,VM_atan2);
1570 PRVM_G_FLOAT(OFS_RETURN) = atan2(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1581 VM_SAFEPARMCOUNT(1,VM_tan);
1582 PRVM_G_FLOAT(OFS_RETURN) = tan(PRVM_G_FLOAT(OFS_PARM0));
1589 Returns a vector of length < 1 and > 0
1594 void VM_randomvec (void)
1599 VM_SAFEPARMCOUNT(0, VM_randomvec);
1604 temp[0] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1605 temp[1] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1606 temp[2] = (rand()&32767) * (2.0 / 32767.0) - 1.0;
1608 while (DotProduct(temp, temp) >= 1);
1609 VectorCopy (temp, PRVM_G_VECTOR(OFS_RETURN));
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;
1615 // length returned always > 0
1616 length = (rand()&32766 + 1) * (1.0 / 32767.0) / VectorLength(temp);
1617 VectorScale(temp,length, temp);*/
1618 //VectorCopy(temp, PRVM_G_VECTOR(OFS_RETURN));
1621 //=============================================================================
1627 float registercvar (string name, string value[, float flags])
1630 void VM_registercvar (void)
1632 const char *name, *value;
1635 VM_SAFEPARMCOUNTRANGE(2, 3, VM_registercvar);
1637 name = PRVM_G_STRING(OFS_PARM0);
1638 value = PRVM_G_STRING(OFS_PARM1);
1639 flags = prog->argc >= 3 ? (int)PRVM_G_FLOAT(OFS_PARM2) : 0;
1640 PRVM_G_FLOAT(OFS_RETURN) = 0;
1642 if(flags > CVAR_MAXFLAGSVAL)
1645 // first check to see if it has already been defined
1646 if (Cvar_FindVar (name))
1649 // check for overlap with a command
1650 if (Cmd_Exists (name))
1652 VM_Warning("VM_registercvar: %s is a command\n", name);
1656 Cvar_Get(name, value, flags, NULL);
1658 PRVM_G_FLOAT(OFS_RETURN) = 1; // success
1666 returns the minimum of two supplied floats
1668 float min(float a, float b, ...[float])
1673 VM_SAFEPARMCOUNTRANGE(2, 8, VM_min);
1674 // LordHavoc: 3+ argument enhancement suggested by FrikaC
1675 if (prog->argc >= 3)
1678 float f = PRVM_G_FLOAT(OFS_PARM0);
1679 for (i = 1;i < prog->argc;i++)
1680 if (f > PRVM_G_FLOAT((OFS_PARM0+i*3)))
1681 f = PRVM_G_FLOAT((OFS_PARM0+i*3));
1682 PRVM_G_FLOAT(OFS_RETURN) = f;
1685 PRVM_G_FLOAT(OFS_RETURN) = min(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1692 returns the maximum of two supplied floats
1694 float max(float a, float b, ...[float])
1699 VM_SAFEPARMCOUNTRANGE(2, 8, VM_max);
1700 // LordHavoc: 3+ argument enhancement suggested by FrikaC
1701 if (prog->argc >= 3)
1704 float f = PRVM_G_FLOAT(OFS_PARM0);
1705 for (i = 1;i < prog->argc;i++)
1706 if (f < PRVM_G_FLOAT((OFS_PARM0+i*3)))
1707 f = PRVM_G_FLOAT((OFS_PARM0+i*3));
1708 PRVM_G_FLOAT(OFS_RETURN) = f;
1711 PRVM_G_FLOAT(OFS_RETURN) = max(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1718 returns number bounded by supplied range
1720 float bound(float min, float value, float max)
1723 void VM_bound (void)
1725 VM_SAFEPARMCOUNT(3,VM_bound);
1726 PRVM_G_FLOAT(OFS_RETURN) = bound(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1), PRVM_G_FLOAT(OFS_PARM2));
1733 returns a raised to power b
1735 float pow(float a, float b)
1740 VM_SAFEPARMCOUNT(2,VM_pow);
1741 PRVM_G_FLOAT(OFS_RETURN) = pow(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1746 VM_SAFEPARMCOUNT(1,VM_log);
1747 PRVM_G_FLOAT(OFS_RETURN) = log(PRVM_G_FLOAT(OFS_PARM0));
1750 void VM_Files_Init(void)
1753 for (i = 0;i < PRVM_MAX_OPENFILES;i++)
1754 prog->openfiles[i] = NULL;
1757 void VM_Files_CloseAll(void)
1760 for (i = 0;i < PRVM_MAX_OPENFILES;i++)
1762 if (prog->openfiles[i])
1763 FS_Close(prog->openfiles[i]);
1764 prog->openfiles[i] = NULL;
1768 static qfile_t *VM_GetFileHandle( int index )
1770 if (index < 0 || index >= PRVM_MAX_OPENFILES)
1772 Con_Printf("VM_GetFileHandle: invalid file handle %i used in %s\n", index, PRVM_NAME);
1775 if (prog->openfiles[index] == NULL)
1777 Con_Printf("VM_GetFileHandle: no such file handle %i (or file has been closed) in %s\n", index, PRVM_NAME);
1780 return prog->openfiles[index];
1787 float fopen(string filename, float mode)
1790 // float(string filename, float mode) fopen = #110;
1791 // opens a file inside quake/gamedir/data/ (mode is FILE_READ, FILE_APPEND, or FILE_WRITE),
1792 // returns fhandle >= 0 if successful, or fhandle < 0 if unable to open file for any reason
1796 const char *modestring, *filename;
1798 VM_SAFEPARMCOUNT(2,VM_fopen);
1800 for (filenum = 0;filenum < PRVM_MAX_OPENFILES;filenum++)
1801 if (prog->openfiles[filenum] == NULL)
1803 if (filenum >= PRVM_MAX_OPENFILES)
1805 PRVM_G_FLOAT(OFS_RETURN) = -2;
1806 VM_Warning("VM_fopen: %s ran out of file handles (%i)\n", PRVM_NAME, PRVM_MAX_OPENFILES);
1809 filename = PRVM_G_STRING(OFS_PARM0);
1810 mode = (int)PRVM_G_FLOAT(OFS_PARM1);
1813 case 0: // FILE_READ
1815 prog->openfiles[filenum] = FS_OpenVirtualFile(va("data/%s", filename), false);
1816 if (prog->openfiles[filenum] == NULL)
1817 prog->openfiles[filenum] = FS_OpenVirtualFile(va("%s", filename), false);
1819 case 1: // FILE_APPEND
1821 prog->openfiles[filenum] = FS_OpenRealFile(va("data/%s", filename), modestring, false);
1823 case 2: // FILE_WRITE
1825 prog->openfiles[filenum] = FS_OpenRealFile(va("data/%s", filename), modestring, false);
1828 PRVM_G_FLOAT(OFS_RETURN) = -3;
1829 VM_Warning("VM_fopen: %s: no such mode %i (valid: 0 = read, 1 = append, 2 = write)\n", PRVM_NAME, mode);
1833 if (prog->openfiles[filenum] == NULL)
1835 PRVM_G_FLOAT(OFS_RETURN) = -1;
1836 if (developer_extra.integer)
1837 VM_Warning("VM_fopen: %s: %s mode %s failed\n", PRVM_NAME, filename, modestring);
1841 PRVM_G_FLOAT(OFS_RETURN) = filenum;
1842 if (developer_extra.integer)
1843 Con_DPrintf("VM_fopen: %s: %s mode %s opened as #%i\n", PRVM_NAME, filename, modestring, filenum);
1844 prog->openfiles_origin[filenum] = PRVM_AllocationOrigin();
1852 fclose(float fhandle)
1855 //void(float fhandle) fclose = #111; // closes a file
1856 void VM_fclose(void)
1860 VM_SAFEPARMCOUNT(1,VM_fclose);
1862 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1863 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
1865 VM_Warning("VM_fclose: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1868 if (prog->openfiles[filenum] == NULL)
1870 VM_Warning("VM_fclose: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1873 FS_Close(prog->openfiles[filenum]);
1874 prog->openfiles[filenum] = NULL;
1875 if(prog->openfiles_origin[filenum])
1876 PRVM_Free((char *)prog->openfiles_origin[filenum]);
1877 if (developer_extra.integer)
1878 Con_DPrintf("VM_fclose: %s: #%i closed\n", PRVM_NAME, filenum);
1885 string fgets(float fhandle)
1888 //string(float fhandle) fgets = #112; // reads a line of text from the file and returns as a tempstring
1892 char string[VM_STRINGTEMP_LENGTH];
1895 VM_SAFEPARMCOUNT(1,VM_fgets);
1897 // set the return value regardless of any possible errors
1898 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
1900 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1901 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
1903 VM_Warning("VM_fgets: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1906 if (prog->openfiles[filenum] == NULL)
1908 VM_Warning("VM_fgets: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1914 c = FS_Getc(prog->openfiles[filenum]);
1915 if (c == '\r' || c == '\n' || c < 0)
1917 if (end < VM_STRINGTEMP_LENGTH - 1)
1921 // remove \n following \r
1924 c = FS_Getc(prog->openfiles[filenum]);
1926 FS_UnGetc(prog->openfiles[filenum], (unsigned char)c);
1928 if (developer_extra.integer)
1929 Con_DPrintf("fgets: %s: %s\n", PRVM_NAME, string);
1931 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string);
1938 fputs(float fhandle, string s)
1941 //void(float fhandle, string s) fputs = #113; // writes a line of text to the end of the file
1945 char string[VM_STRINGTEMP_LENGTH];
1948 VM_SAFEPARMCOUNT(2,VM_fputs);
1950 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1951 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
1953 VM_Warning("VM_fputs: invalid file handle %i used in %s\n", filenum, PRVM_NAME);
1956 if (prog->openfiles[filenum] == NULL)
1958 VM_Warning("VM_fputs: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
1961 VM_VarString(1, string, sizeof(string));
1962 if ((stringlength = (int)strlen(string)))
1963 FS_Write(prog->openfiles[filenum], string, stringlength);
1964 if (developer_extra.integer)
1965 Con_DPrintf("fputs: %s: %s\n", PRVM_NAME, string);
1972 writetofile(float fhandle, entity ent)
1975 void VM_writetofile(void)
1980 VM_SAFEPARMCOUNT(2, VM_writetofile);
1982 file = VM_GetFileHandle( (int)PRVM_G_FLOAT(OFS_PARM0) );
1985 VM_Warning("VM_writetofile: invalid or closed file handle\n");
1989 ent = PRVM_G_EDICT(OFS_PARM1);
1990 if(ent->priv.required->free)
1992 VM_Warning("VM_writetofile: %s: entity %i is free !\n", PRVM_NAME, PRVM_NUM_FOR_EDICT(ent));
1996 PRVM_ED_Write (file, ent);
1999 // KrimZon - DP_QC_ENTITYDATA
2004 float() numentityfields
2005 Return the number of entity fields - NOT offsets
2008 void VM_numentityfields(void)
2010 PRVM_G_FLOAT(OFS_RETURN) = prog->numfielddefs;
2013 // KrimZon - DP_QC_ENTITYDATA
2018 string(float fieldnum) entityfieldname
2019 Return name of the specified field as a string, or empty if the field is invalid (warning)
2022 void VM_entityfieldname(void)
2025 int i = (int)PRVM_G_FLOAT(OFS_PARM0);
2027 if (i < 0 || i >= prog->numfielddefs)
2029 VM_Warning("VM_entityfieldname: %s: field index out of bounds\n", PRVM_NAME);
2030 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString("");
2034 d = &prog->fielddefs[i];
2035 PRVM_G_INT(OFS_RETURN) = d->s_name; // presuming that s_name points to a string already
2038 // KrimZon - DP_QC_ENTITYDATA
2043 float(float fieldnum) entityfieldtype
2046 void VM_entityfieldtype(void)
2049 int i = (int)PRVM_G_FLOAT(OFS_PARM0);
2051 if (i < 0 || i >= prog->numfielddefs)
2053 VM_Warning("VM_entityfieldtype: %s: field index out of bounds\n", PRVM_NAME);
2054 PRVM_G_FLOAT(OFS_RETURN) = -1.0;
2058 d = &prog->fielddefs[i];
2059 PRVM_G_FLOAT(OFS_RETURN) = (float)d->type;
2062 // KrimZon - DP_QC_ENTITYDATA
2065 VM_getentityfieldstring
2067 string(float fieldnum, entity ent) getentityfieldstring
2070 void VM_getentityfieldstring(void)
2072 // put the data into a string
2077 int i = (int)PRVM_G_FLOAT(OFS_PARM0);
2079 if (i < 0 || i >= prog->numfielddefs)
2081 VM_Warning("VM_entityfielddata: %s: field index out of bounds\n", PRVM_NAME);
2082 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString("");
2086 d = &prog->fielddefs[i];
2089 ent = PRVM_G_EDICT(OFS_PARM1);
2090 if(ent->priv.required->free)
2092 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString("");
2093 VM_Warning("VM_entityfielddata: %s: entity %i is free !\n", PRVM_NAME, PRVM_NUM_FOR_EDICT(ent));
2096 v = (int *)((char *)ent->fields.vp + d->ofs*4);
2098 // if it's 0 or blank, return an empty string
2099 type = d->type & ~DEF_SAVEGLOBAL;
2100 for (j=0 ; j<prvm_type_size[type] ; j++)
2103 if (j == prvm_type_size[type])
2105 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString("");
2109 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(PRVM_UglyValueString((etype_t)d->type, (prvm_eval_t *)v));
2112 // KrimZon - DP_QC_ENTITYDATA
2115 VM_putentityfieldstring
2117 float(float fieldnum, entity ent, string s) putentityfieldstring
2120 void VM_putentityfieldstring(void)
2124 int i = (int)PRVM_G_FLOAT(OFS_PARM0);
2126 if (i < 0 || i >= prog->numfielddefs)
2128 VM_Warning("VM_entityfielddata: %s: field index out of bounds\n", PRVM_NAME);
2129 PRVM_G_FLOAT(OFS_RETURN) = 0.0f;
2133 d = &prog->fielddefs[i];
2136 ent = PRVM_G_EDICT(OFS_PARM1);
2137 if(ent->priv.required->free)
2139 VM_Warning("VM_entityfielddata: %s: entity %i is free !\n", PRVM_NAME, PRVM_NUM_FOR_EDICT(ent));
2140 PRVM_G_FLOAT(OFS_RETURN) = 0.0f;
2144 // parse the string into the value
2145 PRVM_G_FLOAT(OFS_RETURN) = ( PRVM_ED_ParseEpair(ent, d, PRVM_G_STRING(OFS_PARM2), false) ) ? 1.0f : 0.0f;
2152 float strlen(string s)
2155 //float(string s) strlen = #114; // returns how many characters are in a string
2156 void VM_strlen(void)
2158 VM_SAFEPARMCOUNT(1,VM_strlen);
2160 //PRVM_G_FLOAT(OFS_RETURN) = strlen(PRVM_G_STRING(OFS_PARM0));
2161 PRVM_G_FLOAT(OFS_RETURN) = u8_strlen(PRVM_G_STRING(OFS_PARM0));
2164 // DRESK - Decolorized String
2169 string strdecolorize(string s)
2172 // string (string s) strdecolorize = #472; // returns the passed in string with color codes stripped
2173 void VM_strdecolorize(void)
2175 char szNewString[VM_STRINGTEMP_LENGTH];
2176 const char *szString;
2179 VM_SAFEPARMCOUNT(1,VM_strdecolorize);
2180 szString = PRVM_G_STRING(OFS_PARM0);
2181 COM_StringDecolorize(szString, 0, szNewString, sizeof(szNewString), TRUE);
2182 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(szNewString);
2185 // DRESK - String Length (not counting color codes)
2190 float strlennocol(string s)
2193 // float(string s) strlennocol = #471; // returns how many characters are in a string not including color codes
2194 // For example, ^2Dresk returns a length of 5
2195 void VM_strlennocol(void)
2197 const char *szString;
2200 VM_SAFEPARMCOUNT(1,VM_strlennocol);
2202 szString = PRVM_G_STRING(OFS_PARM0);
2204 //nCnt = COM_StringLengthNoColors(szString, 0, NULL);
2205 nCnt = u8_COM_StringLengthNoColors(szString, 0, NULL);
2207 PRVM_G_FLOAT(OFS_RETURN) = nCnt;
2210 // DRESK - String to Uppercase and Lowercase
2215 string strtolower(string s)
2218 // string (string s) strtolower = #480; // returns passed in string in lowercase form
2219 void VM_strtolower(void)
2221 char szNewString[VM_STRINGTEMP_LENGTH];
2222 const char *szString;
2225 VM_SAFEPARMCOUNT(1,VM_strtolower);
2226 szString = PRVM_G_STRING(OFS_PARM0);
2228 COM_ToLowerString(szString, szNewString, sizeof(szNewString) );
2230 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(szNewString);
2237 string strtoupper(string s)
2240 // string (string s) strtoupper = #481; // returns passed in string in uppercase form
2241 void VM_strtoupper(void)
2243 char szNewString[VM_STRINGTEMP_LENGTH];
2244 const char *szString;
2247 VM_SAFEPARMCOUNT(1,VM_strtoupper);
2248 szString = PRVM_G_STRING(OFS_PARM0);
2250 COM_ToUpperString(szString, szNewString, sizeof(szNewString) );
2252 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(szNewString);
2259 string strcat(string,string,...[string])
2262 //string(string s1, string s2) strcat = #115;
2263 // concatenates two strings (for example "abc", "def" would return "abcdef")
2264 // and returns as a tempstring
2265 void VM_strcat(void)
2267 char s[VM_STRINGTEMP_LENGTH];
2268 VM_SAFEPARMCOUNTRANGE(1, 8, VM_strcat);
2270 VM_VarString(0, s, sizeof(s));
2271 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(s);
2278 string substring(string s, float start, float length)
2281 // string(string s, float start, float length) substring = #116;
2282 // returns a section of a string as a tempstring
2283 void VM_substring(void)
2286 int u_slength = 0, u_start;
2289 char string[VM_STRINGTEMP_LENGTH];
2291 VM_SAFEPARMCOUNT(3,VM_substring);
2294 s = PRVM_G_STRING(OFS_PARM0);
2295 start = (int)PRVM_G_FLOAT(OFS_PARM1);
2296 length = (int)PRVM_G_FLOAT(OFS_PARM2);
2297 slength = strlen(s);
2299 if (start < 0) // FTE_STRINGS feature
2301 start = bound(0, start, slength);
2303 if (length < 0) // FTE_STRINGS feature
2304 length += slength - start + 1;
2305 maxlen = min((int)sizeof(string) - 1, slength - start);
2306 length = bound(0, length, maxlen);
2308 memcpy(string, s + start, length);
2310 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string);
2313 s = PRVM_G_STRING(OFS_PARM0);
2314 start = (int)PRVM_G_FLOAT(OFS_PARM1);
2315 length = (int)PRVM_G_FLOAT(OFS_PARM2);
2317 if (start < 0) // FTE_STRINGS feature
2319 u_slength = u8_strlen(s);
2321 start = bound(0, start, u_slength);
2324 if (length < 0) // FTE_STRINGS feature
2326 if (!u_slength) // it's not calculated when it's not needed above
2327 u_slength = u8_strlen(s);
2328 length += u_slength - start + 1;
2331 // positive start, positive length
2332 u_start = u8_byteofs(s, start, NULL);
2335 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString("");
2338 u_length = u8_bytelen(s + u_start, length);
2339 if (u_length >= sizeof(string)-1)
2340 u_length = sizeof(string)-1;
2342 memcpy(string, s + u_start, u_length);
2343 string[u_length] = 0;
2344 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string);
2351 string(string search, string replace, string subject) strreplace = #484;
2354 // replaces all occurrences of search with replace in the string subject, and returns the result
2355 void VM_strreplace(void)
2358 const char *search, *replace, *subject;
2359 char string[VM_STRINGTEMP_LENGTH];
2360 int search_len, replace_len, subject_len;
2362 VM_SAFEPARMCOUNT(3,VM_strreplace);
2364 search = PRVM_G_STRING(OFS_PARM0);
2365 replace = PRVM_G_STRING(OFS_PARM1);
2366 subject = PRVM_G_STRING(OFS_PARM2);
2368 search_len = (int)strlen(search);
2369 replace_len = (int)strlen(replace);
2370 subject_len = (int)strlen(subject);
2373 for (i = 0; i <= subject_len - search_len; i++)
2375 for (j = 0; j < search_len; j++) // thus, i+j < subject_len
2376 if (subject[i+j] != search[j])
2378 if (j == search_len)
2380 // NOTE: if search_len == 0, we always hit THIS case, and never the other
2381 // found it at offset 'i'
2382 for (j = 0; j < replace_len && si < (int)sizeof(string) - 1; j++)
2383 string[si++] = replace[j];
2386 i += search_len - 1;
2390 // the above would subtract 1 from i... so we
2391 // don't do that, but instead output the next
2393 if (si < (int)sizeof(string) - 1)
2394 string[si++] = subject[i];
2399 // in THIS case, we know search_len > 0, thus i < subject_len
2401 if (si < (int)sizeof(string) - 1)
2402 string[si++] = subject[i];
2405 // remaining chars (these cannot match)
2406 for (; i < subject_len; i++)
2407 if (si < (int)sizeof(string) - 1)
2408 string[si++] = subject[i];
2411 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string);
2418 string(string search, string replace, string subject) strireplace = #485;
2421 // case-insensitive version of strreplace
2422 void VM_strireplace(void)
2425 const char *search, *replace, *subject;
2426 char string[VM_STRINGTEMP_LENGTH];
2427 int search_len, replace_len, subject_len;
2429 VM_SAFEPARMCOUNT(3,VM_strreplace);
2431 search = PRVM_G_STRING(OFS_PARM0);
2432 replace = PRVM_G_STRING(OFS_PARM1);
2433 subject = PRVM_G_STRING(OFS_PARM2);
2435 search_len = (int)strlen(search);
2436 replace_len = (int)strlen(replace);
2437 subject_len = (int)strlen(subject);
2440 for (i = 0; i <= subject_len - search_len; i++)
2442 for (j = 0; j < search_len; j++) // thus, i+j < subject_len
2443 if (tolower(subject[i+j]) != tolower(search[j]))
2445 if (j == search_len)
2447 // NOTE: if search_len == 0, we always hit THIS case, and never the other
2448 // found it at offset 'i'
2449 for (j = 0; j < replace_len && si < (int)sizeof(string) - 1; j++)
2450 string[si++] = replace[j];
2453 i += search_len - 1;
2457 // the above would subtract 1 from i... so we
2458 // don't do that, but instead output the next
2460 if (si < (int)sizeof(string) - 1)
2461 string[si++] = subject[i];
2466 // in THIS case, we know search_len > 0, thus i < subject_len
2468 if (si < (int)sizeof(string) - 1)
2469 string[si++] = subject[i];
2472 // remaining chars (these cannot match)
2473 for (; i < subject_len; i++)
2474 if (si < (int)sizeof(string) - 1)
2475 string[si++] = subject[i];
2478 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(string);
2485 vector stov(string s)
2488 //vector(string s) stov = #117; // returns vector value from a string
2491 char string[VM_STRINGTEMP_LENGTH];
2493 VM_SAFEPARMCOUNT(1,VM_stov);
2495 VM_VarString(0, string, sizeof(string));
2496 Math_atov(string, PRVM_G_VECTOR(OFS_RETURN));
2503 string strzone(string s)
2506 //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)
2507 void VM_strzone(void)
2510 char string[VM_STRINGTEMP_LENGTH];
2513 VM_SAFEPARMCOUNT(1,VM_strzone);
2515 VM_VarString(0, string, sizeof(string));
2516 alloclen = strlen(string) + 1;
2517 PRVM_G_INT(OFS_RETURN) = PRVM_AllocString(alloclen, &out);
2518 memcpy(out, string, alloclen);
2528 //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!!!)
2529 void VM_strunzone(void)
2531 VM_SAFEPARMCOUNT(1,VM_strunzone);
2532 PRVM_FreeString(PRVM_G_INT(OFS_PARM0));
2537 VM_command (used by client and menu)
2539 clientcommand(float client, string s) (for client and menu)
2542 //void(entity e, string s) clientcommand = #440; // executes a command string as if it came from the specified client
2543 //this function originally written by KrimZon, made shorter by LordHavoc
2544 void VM_clcommand (void)
2546 client_t *temp_client;
2549 VM_SAFEPARMCOUNT(2,VM_clcommand);
2551 i = (int)PRVM_G_FLOAT(OFS_PARM0);
2552 if (!sv.active || i < 0 || i >= svs.maxclients || !svs.clients[i].active)
2554 VM_Warning("VM_clientcommand: %s: invalid client/server is not active !\n", PRVM_NAME);
2558 temp_client = host_client;
2559 host_client = svs.clients + i;
2560 Cmd_ExecuteString (PRVM_G_STRING(OFS_PARM1), src_client);
2561 host_client = temp_client;
2569 float tokenize(string s)
2572 //float(string s) tokenize = #441; // takes apart a string into individal words (access them with argv), returns how many
2573 //this function originally written by KrimZon, made shorter by LordHavoc
2574 //20040203: rewritten by LordHavoc (no longer uses allocations)
2575 static int num_tokens = 0;
2576 static int tokens[VM_STRINGTEMP_LENGTH / 2];
2577 static int tokens_startpos[VM_STRINGTEMP_LENGTH / 2];
2578 static int tokens_endpos[VM_STRINGTEMP_LENGTH / 2];
2579 static char tokenize_string[VM_STRINGTEMP_LENGTH];
2580 void VM_tokenize (void)
2584 VM_SAFEPARMCOUNT(1,VM_tokenize);
2586 strlcpy(tokenize_string, PRVM_G_STRING(OFS_PARM0), sizeof(tokenize_string));
2587 p = tokenize_string;
2592 if (num_tokens >= (int)(sizeof(tokens)/sizeof(tokens[0])))
2595 // skip whitespace here to find token start pos
2596 while(*p && ISWHITESPACE(*p))
2599 tokens_startpos[num_tokens] = p - tokenize_string;
2600 if(!COM_ParseToken_VM_Tokenize(&p, false))
2602 tokens_endpos[num_tokens] = p - tokenize_string;
2603 tokens[num_tokens] = PRVM_SetTempString(com_token);
2607 PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
2610 //float(string s) tokenize = #514; // takes apart a string into individal words (access them with argv), returns how many
2611 void VM_tokenize_console (void)
2615 VM_SAFEPARMCOUNT(1,VM_tokenize);
2617 strlcpy(tokenize_string, PRVM_G_STRING(OFS_PARM0), sizeof(tokenize_string));
2618 p = tokenize_string;
2623 if (num_tokens >= (int)(sizeof(tokens)/sizeof(tokens[0])))
2626 // skip whitespace here to find token start pos
2627 while(*p && ISWHITESPACE(*p))
2630 tokens_startpos[num_tokens] = p - tokenize_string;
2631 if(!COM_ParseToken_Console(&p))
2633 tokens_endpos[num_tokens] = p - tokenize_string;
2634 tokens[num_tokens] = PRVM_SetTempString(com_token);
2638 PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
2643 VM_tokenizebyseparator
2645 float tokenizebyseparator(string s, string separator1, ...)
2648 //float(string s, string separator1, ...) tokenizebyseparator = #479; // takes apart a string into individal words (access them with argv), returns how many
2649 //this function returns the token preceding each instance of a separator (of
2650 //which there can be multiple), and the text following the last separator
2651 //useful for parsing certain kinds of data like IP addresses
2653 //numnumbers = tokenizebyseparator("10.1.2.3", ".");
2654 //returns 4 and the tokens "10" "1" "2" "3".
2655 void VM_tokenizebyseparator (void)
2659 int separatorlen[7];
2660 const char *separators[7];
2663 char tokentext[MAX_INPUTLINE];
2665 VM_SAFEPARMCOUNTRANGE(2, 8,VM_tokenizebyseparator);
2667 strlcpy(tokenize_string, PRVM_G_STRING(OFS_PARM0), sizeof(tokenize_string));
2668 p = tokenize_string;
2671 for (j = 1;j < prog->argc;j++)
2673 // skip any blank separator strings
2674 const char *s = PRVM_G_STRING(OFS_PARM0+j*3);
2677 separators[numseparators] = s;
2678 separatorlen[numseparators] = strlen(s);
2685 while (num_tokens < (int)(sizeof(tokens)/sizeof(tokens[0])))
2687 token = tokentext + j;
2688 tokens_startpos[num_tokens] = p - tokenize_string;
2692 for (k = 0;k < numseparators;k++)
2694 if (!strncmp(p, separators[k], separatorlen[k]))
2696 p += separatorlen[k];
2700 if (k < numseparators)
2702 if (j < (int)sizeof(tokentext)-1)
2703 tokentext[j++] = *p;
2707 tokens_endpos[num_tokens] = p0 - tokenize_string;
2708 if (j >= (int)sizeof(tokentext))
2711 tokens[num_tokens++] = PRVM_SetTempString(token);
2716 PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
2719 //string(float n) argv = #442; // returns a word from the tokenized string (returns nothing for an invalid index)
2720 //this function originally written by KrimZon, made shorter by LordHavoc
2725 VM_SAFEPARMCOUNT(1,VM_argv);
2727 token_num = (int)PRVM_G_FLOAT(OFS_PARM0);
2730 token_num += num_tokens;
2732 if (token_num >= 0 && token_num < num_tokens)
2733 PRVM_G_INT(OFS_RETURN) = tokens[token_num];
2735 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
2738 //float(float n) argv_start_index = #515; // returns the start index of a token
2739 void VM_argv_start_index (void)
2743 VM_SAFEPARMCOUNT(1,VM_argv);
2745 token_num = (int)PRVM_G_FLOAT(OFS_PARM0);
2748 token_num += num_tokens;
2750 if (token_num >= 0 && token_num < num_tokens)
2751 PRVM_G_FLOAT(OFS_RETURN) = tokens_startpos[token_num];
2753 PRVM_G_FLOAT(OFS_RETURN) = -1;
2756 //float(float n) argv_end_index = #516; // returns the end index of a token
2757 void VM_argv_end_index (void)
2761 VM_SAFEPARMCOUNT(1,VM_argv);
2763 token_num = (int)PRVM_G_FLOAT(OFS_PARM0);
2766 token_num += num_tokens;
2768 if (token_num >= 0 && token_num < num_tokens)
2769 PRVM_G_FLOAT(OFS_RETURN) = tokens_endpos[token_num];
2771 PRVM_G_FLOAT(OFS_RETURN) = -1;
2781 void VM_isserver(void)
2783 VM_SAFEPARMCOUNT(0,VM_serverstate);
2785 PRVM_G_FLOAT(OFS_RETURN) = sv.active && (svs.maxclients > 1 || cls.state == ca_dedicated);
2795 void VM_clientcount(void)
2797 VM_SAFEPARMCOUNT(0,VM_clientcount);
2799 PRVM_G_FLOAT(OFS_RETURN) = svs.maxclients;
2809 void VM_clientstate(void)
2811 VM_SAFEPARMCOUNT(0,VM_clientstate);
2814 switch( cls.state ) {
2815 case ca_uninitialized:
2817 PRVM_G_FLOAT(OFS_RETURN) = 0;
2819 case ca_disconnected:
2820 PRVM_G_FLOAT(OFS_RETURN) = 1;
2823 PRVM_G_FLOAT(OFS_RETURN) = 2;
2826 // should never be reached!
2835 float getostype(void)
2837 */ // not used at the moment -> not included in the common list
2838 void VM_getostype(void)
2840 VM_SAFEPARMCOUNT(0,VM_getostype);
2845 OS_MAC - not supported
2849 PRVM_G_FLOAT(OFS_RETURN) = 0;
2850 #elif defined(MACOSX)
2851 PRVM_G_FLOAT(OFS_RETURN) = 2;
2853 PRVM_G_FLOAT(OFS_RETURN) = 1;
2864 extern double host_starttime;
2865 float CDAudio_GetPosition(void);
2866 void VM_gettime(void)
2870 VM_SAFEPARMCOUNTRANGE(0,1,VM_gettime);
2874 PRVM_G_FLOAT(OFS_RETURN) = (float) realtime;
2878 timer_index = (int) PRVM_G_FLOAT(OFS_PARM0);
2881 case 0: // GETTIME_FRAMESTART
2882 PRVM_G_FLOAT(OFS_RETURN) = (float) realtime;
2884 case 1: // GETTIME_REALTIME
2885 PRVM_G_FLOAT(OFS_RETURN) = (float) Sys_DoubleTime();
2887 case 2: // GETTIME_HIRES
2888 PRVM_G_FLOAT(OFS_RETURN) = (float) (Sys_DoubleTime() - realtime);
2890 case 3: // GETTIME_UPTIME
2891 PRVM_G_FLOAT(OFS_RETURN) = (float) (Sys_DoubleTime() - host_starttime);
2893 case 4: // GETTIME_CDTRACK
2894 PRVM_G_FLOAT(OFS_RETURN) = (float) CDAudio_GetPosition();
2897 VM_Warning("VM_gettime: %s: unsupported timer specified, returning realtime\n", PRVM_NAME);
2898 PRVM_G_FLOAT(OFS_RETURN) = (float) realtime;
2908 float getsoundtime(void)
2912 void VM_getsoundtime (void)
2914 int entnum, entchannel, pnum;
2915 VM_SAFEPARMCOUNT(2,VM_getsoundtime);
2917 pnum = PRVM_GetProgNr();
2918 if (pnum == PRVM_MENUPROG)
2920 VM_Warning("VM_getsoundtime: %s: not supported on this progs\n", PRVM_NAME);
2921 PRVM_G_FLOAT(OFS_RETURN) = -1;
2924 entnum = ((pnum == PRVM_CLIENTPROG) ? MAX_EDICTS : 0) + PRVM_NUM_FOR_EDICT(PRVM_G_EDICT(OFS_PARM0));
2925 entchannel = (int)PRVM_G_FLOAT(OFS_PARM1);
2926 entchannel = CHAN_USER2ENGINE(entchannel);
2927 if (!IS_CHAN(entchannel))
2928 VM_Warning("VM_getsoundtime: %s: bad channel %i\n", PRVM_NAME, entchannel);
2929 PRVM_G_FLOAT(OFS_RETURN) = (float)S_GetEntChannelPosition(entnum, entchannel);
2936 string soundlength (string sample)
2939 void VM_soundlength (void)
2943 VM_SAFEPARMCOUNT(1, VM_soundlength);
2945 s = PRVM_G_STRING(OFS_PARM0);
2946 PRVM_G_FLOAT(OFS_RETURN) = S_SoundLength(s);
2953 loadfromdata(string data)
2956 void VM_loadfromdata(void)
2958 VM_SAFEPARMCOUNT(1,VM_loadentsfromfile);
2960 PRVM_ED_LoadFromFile(PRVM_G_STRING(OFS_PARM0));
2964 ========================
2967 parseentitydata(entity ent, string data)
2968 ========================
2970 void VM_parseentitydata(void)
2975 VM_SAFEPARMCOUNT(2, VM_parseentitydata);
2977 // get edict and test it
2978 ent = PRVM_G_EDICT(OFS_PARM0);
2979 if (ent->priv.required->free)
2980 PRVM_ERROR ("VM_parseentitydata: %s: Can only set already spawned entities (entity %i is free)!", PRVM_NAME, PRVM_NUM_FOR_EDICT(ent));
2982 data = PRVM_G_STRING(OFS_PARM1);
2984 // parse the opening brace
2985 if (!COM_ParseToken_Simple(&data, false, false) || com_token[0] != '{' )
2986 PRVM_ERROR ("VM_parseentitydata: %s: Couldn't parse entity data:\n%s", PRVM_NAME, data );
2988 PRVM_ED_ParseEdict (data, ent);
2995 loadfromfile(string file)
2998 void VM_loadfromfile(void)
3000 const char *filename;
3003 VM_SAFEPARMCOUNT(1,VM_loadfromfile);
3005 filename = PRVM_G_STRING(OFS_PARM0);
3006 if (FS_CheckNastyPath(filename, false))
3008 PRVM_G_FLOAT(OFS_RETURN) = -4;
3009 VM_Warning("VM_loadfromfile: %s dangerous or non-portable filename \"%s\" not allowed. (contains : or \\ or begins with .. or /)\n", PRVM_NAME, filename);
3013 // not conform with VM_fopen
3014 data = (char *)FS_LoadFile(filename, tempmempool, false, NULL);
3016 PRVM_G_FLOAT(OFS_RETURN) = -1;
3018 PRVM_ED_LoadFromFile(data);
3029 float mod(float val, float m)
3032 void VM_modulo(void)
3035 VM_SAFEPARMCOUNT(2,VM_module);
3037 val = (int) PRVM_G_FLOAT(OFS_PARM0);
3038 m = (int) PRVM_G_FLOAT(OFS_PARM1);
3040 PRVM_G_FLOAT(OFS_RETURN) = (float) (val % m);
3043 void VM_Search_Init(void)
3046 for (i = 0;i < PRVM_MAX_OPENSEARCHES;i++)
3047 prog->opensearches[i] = NULL;
3050 void VM_Search_Reset(void)
3053 // reset the fssearch list
3054 for(i = 0; i < PRVM_MAX_OPENSEARCHES; i++)
3056 if(prog->opensearches[i])
3057 FS_FreeSearch(prog->opensearches[i]);
3058 prog->opensearches[i] = NULL;
3066 float search_begin(string pattern, float caseinsensitive, float quiet)
3069 void VM_search_begin(void)
3072 const char *pattern;
3073 int caseinsens, quiet;
3075 VM_SAFEPARMCOUNT(3, VM_search_begin);
3077 pattern = PRVM_G_STRING(OFS_PARM0);
3079 VM_CheckEmptyString(pattern);
3081 caseinsens = (int)PRVM_G_FLOAT(OFS_PARM1);
3082 quiet = (int)PRVM_G_FLOAT(OFS_PARM2);
3084 for(handle = 0; handle < PRVM_MAX_OPENSEARCHES; handle++)
3085 if(!prog->opensearches[handle])
3088 if(handle >= PRVM_MAX_OPENSEARCHES)
3090 PRVM_G_FLOAT(OFS_RETURN) = -2;
3091 VM_Warning("VM_search_begin: %s ran out of search handles (%i)\n", PRVM_NAME, PRVM_MAX_OPENSEARCHES);
3095 if(!(prog->opensearches[handle] = FS_Search(pattern,caseinsens, quiet)))
3096 PRVM_G_FLOAT(OFS_RETURN) = -1;
3099 prog->opensearches_origin[handle] = PRVM_AllocationOrigin();
3100 PRVM_G_FLOAT(OFS_RETURN) = handle;
3108 void search_end(float handle)
3111 void VM_search_end(void)
3114 VM_SAFEPARMCOUNT(1, VM_search_end);
3116 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
3118 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
3120 VM_Warning("VM_search_end: invalid handle %i used in %s\n", handle, PRVM_NAME);
3123 if(prog->opensearches[handle] == NULL)
3125 VM_Warning("VM_search_end: no such handle %i in %s\n", handle, PRVM_NAME);
3129 FS_FreeSearch(prog->opensearches[handle]);
3130 prog->opensearches[handle] = NULL;
3131 if(prog->opensearches_origin[handle])
3132 PRVM_Free((char *)prog->opensearches_origin[handle]);
3139 float search_getsize(float handle)
3142 void VM_search_getsize(void)
3145 VM_SAFEPARMCOUNT(1, VM_M_search_getsize);
3147 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
3149 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
3151 VM_Warning("VM_search_getsize: invalid handle %i used in %s\n", handle, PRVM_NAME);
3154 if(prog->opensearches[handle] == NULL)
3156 VM_Warning("VM_search_getsize: no such handle %i in %s\n", handle, PRVM_NAME);
3160 PRVM_G_FLOAT(OFS_RETURN) = prog->opensearches[handle]->numfilenames;
3165 VM_search_getfilename
3167 string search_getfilename(float handle, float num)
3170 void VM_search_getfilename(void)
3172 int handle, filenum;
3173 VM_SAFEPARMCOUNT(2, VM_search_getfilename);
3175 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
3176 filenum = (int)PRVM_G_FLOAT(OFS_PARM1);
3178 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
3180 VM_Warning("VM_search_getfilename: invalid handle %i used in %s\n", handle, PRVM_NAME);
3183 if(prog->opensearches[handle] == NULL)
3185 VM_Warning("VM_search_getfilename: no such handle %i in %s\n", handle, PRVM_NAME);
3188 if(filenum < 0 || filenum >= prog->opensearches[handle]->numfilenames)
3190 VM_Warning("VM_search_getfilename: invalid filenum %i in %s\n", filenum, PRVM_NAME);
3194 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog->opensearches[handle]->filenames[filenum]);
3201 string chr(float ascii)
3208 VM_SAFEPARMCOUNT(1, VM_chr);
3210 tmp[0] = (unsigned char) PRVM_G_FLOAT(OFS_PARM0);
3213 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(tmp);
3218 VM_SAFEPARMCOUNT(1, VM_chr);
3220 len = u8_fromchar((Uchar)PRVM_G_FLOAT(OFS_PARM0), tmp, sizeof(tmp));
3222 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(tmp);
3225 //=============================================================================
3226 // Draw builtins (client & menu)
3232 float iscachedpic(string pic)
3235 void VM_iscachedpic(void)
3237 VM_SAFEPARMCOUNT(1,VM_iscachedpic);
3239 // drawq hasnt such a function, thus always return true
3240 PRVM_G_FLOAT(OFS_RETURN) = false;
3247 string precache_pic(string pic)
3250 void VM_precache_pic(void)
3254 VM_SAFEPARMCOUNT(1, VM_precache_pic);
3256 s = PRVM_G_STRING(OFS_PARM0);
3257 PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
3258 VM_CheckEmptyString (s);
3260 // AK Draw_CachePic is supposed to always return a valid pointer
3261 if( Draw_CachePic_Flags(s, 0)->tex == r_texture_notexture )
3262 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
3272 void VM_freepic(void)
3276 VM_SAFEPARMCOUNT(1,VM_freepic);
3278 s = PRVM_G_STRING(OFS_PARM0);
3279 VM_CheckEmptyString (s);
3284 void getdrawfontscale(float *sx, float *sy)
3288 VectorCopy(PRVM_drawglobalvector(drawfontscale), v);
3289 if(VectorLength2(v) > 0)
3296 dp_font_t *getdrawfont(void)
3298 int f = (int) PRVM_drawglobalfloat(drawfont);
3299 if(f < 0 || f >= dp_fonts.maxsize)
3300 return FONT_DEFAULT;
3301 return &dp_fonts.f[f];
3308 float drawcharacter(vector position, float character, vector scale, vector rgb, float alpha, float flag)
3311 void VM_drawcharacter(void)
3313 float *pos,*scale,*rgb;
3317 VM_SAFEPARMCOUNT(6,VM_drawcharacter);
3319 character = (char) PRVM_G_FLOAT(OFS_PARM1);
3322 PRVM_G_FLOAT(OFS_RETURN) = -1;
3323 VM_Warning("VM_drawcharacter: %s passed null character !\n",PRVM_NAME);
3327 pos = PRVM_G_VECTOR(OFS_PARM0);
3328 scale = PRVM_G_VECTOR(OFS_PARM2);
3329 rgb = PRVM_G_VECTOR(OFS_PARM3);
3330 flag = (int)PRVM_G_FLOAT(OFS_PARM5);
3332 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3334 PRVM_G_FLOAT(OFS_RETURN) = -2;
3335 VM_Warning("VM_drawcharacter: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
3339 if(pos[2] || scale[2])
3340 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")));
3342 if(!scale[0] || !scale[1])
3344 PRVM_G_FLOAT(OFS_RETURN) = -3;
3345 VM_Warning("VM_drawcharacter: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
3349 getdrawfontscale(&sx, &sy);
3350 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());
3351 PRVM_G_FLOAT(OFS_RETURN) = 1;
3358 float drawstring(vector position, string text, vector scale, vector rgb, float alpha[, float flag])
3361 void VM_drawstring(void)
3363 float *pos,*scale,*rgb;
3367 VM_SAFEPARMCOUNTRANGE(5,6,VM_drawstring);
3369 string = PRVM_G_STRING(OFS_PARM1);
3370 pos = PRVM_G_VECTOR(OFS_PARM0);
3371 scale = PRVM_G_VECTOR(OFS_PARM2);
3372 rgb = PRVM_G_VECTOR(OFS_PARM3);
3373 if (prog->argc >= 6)
3374 flag = (int)PRVM_G_FLOAT(OFS_PARM5);
3376 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3378 PRVM_G_FLOAT(OFS_RETURN) = -2;
3379 VM_Warning("VM_drawstring: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
3383 if(!scale[0] || !scale[1])
3385 PRVM_G_FLOAT(OFS_RETURN) = -3;
3386 VM_Warning("VM_drawstring: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
3390 if(pos[2] || scale[2])
3391 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")));
3393 getdrawfontscale(&sx, &sy);
3394 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());
3395 //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);
3396 PRVM_G_FLOAT(OFS_RETURN) = 1;
3401 VM_drawcolorcodedstring
3403 float drawcolorcodedstring(vector position, string text, vector scale, float alpha, float flag)
3405 float drawcolorcodedstring(vector position, string text, vector scale, vector rgb, float alpha, float flag)
3408 void VM_drawcolorcodedstring(void)
3414 float sx, sy, alpha;
3416 VM_SAFEPARMCOUNTRANGE(5,6,VM_drawcolorcodedstring);
3418 if (prog->argc == 6) // full 6 parms, like normal drawstring
3420 pos = PRVM_G_VECTOR(OFS_PARM0);
3421 string = PRVM_G_STRING(OFS_PARM1);
3422 scale = PRVM_G_VECTOR(OFS_PARM2);
3423 VectorCopy(PRVM_G_VECTOR(OFS_PARM3), rgb);
3424 alpha = PRVM_G_FLOAT(OFS_PARM4);
3425 flag = (int)PRVM_G_FLOAT(OFS_PARM5);
3429 pos = PRVM_G_VECTOR(OFS_PARM0);
3430 string = PRVM_G_STRING(OFS_PARM1);
3431 scale = PRVM_G_VECTOR(OFS_PARM2);
3435 alpha = PRVM_G_FLOAT(OFS_PARM3);
3436 flag = (int)PRVM_G_FLOAT(OFS_PARM4);
3439 if(flag < DRAWFLAG_NORMAL || flag >= DRAWFLAG_NUMFLAGS)
3441 PRVM_G_FLOAT(OFS_RETURN) = -2;
3442 VM_Warning("VM_drawcolorcodedstring: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
3446 if(!scale[0] || !scale[1])
3448 PRVM_G_FLOAT(OFS_RETURN) = -3;
3449 VM_Warning("VM_drawcolorcodedstring: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
3453 if(pos[2] || scale[2])
3454 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")));
3456 getdrawfontscale(&sx, &sy);
3457 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());
3458 if (prog->argc == 6) // also return vector of last color
3459 VectorCopy(DrawQ_Color, PRVM_G_VECTOR(OFS_RETURN));
3461 PRVM_G_FLOAT(OFS_RETURN) = 1;
3467 float stringwidth(string text, float allowColorCodes, float size)
3470 void VM_stringwidth(void)
3474 float mult; // sz is intended font size so we can later add freetype support, mult is font size multiplier in pixels per character cell
3478 VM_SAFEPARMCOUNTRANGE(2,3,VM_drawstring);
3480 getdrawfontscale(&sx, &sy);
3483 szv = PRVM_G_VECTOR(OFS_PARM2);
3488 // we want the width for 8x8 font size, divided by 8
3489 static float defsize[] = {8, 8};
3492 // to make sure snapping is turned off, ALWAYS use a nontrivial scale in this case
3493 if(sx >= 0.9 && sx <= 1.1)
3501 string = PRVM_G_STRING(OFS_PARM0);
3502 colors = (int)PRVM_G_FLOAT(OFS_PARM1);
3504 PRVM_G_FLOAT(OFS_RETURN) = DrawQ_TextWidth_UntilWidth_TrackColors_Scale(string, &maxlen, szv[0], szv[1], sx, sy, NULL, !colors, getdrawfont(), 1000000000) * mult;
3508 mult = sz = PRVM_G_FLOAT(OFS_PARM2);
3516 string = PRVM_G_STRING(OFS_PARM0);
3517 colors = (int)PRVM_G_FLOAT(OFS_PARM1);
3519 PRVM_G_FLOAT(OFS_RETURN) = DrawQ_TextWidth(string, 0, !colors, getdrawfont()) * mult; // 1x1 characters, don't actually draw
3527 float findfont(string s)
3531 float getdrawfontnum(const char *fontname)
3535 for(i = 0; i < dp_fonts.maxsize; ++i)
3536 if(!strcmp(dp_fonts.f[i].title, fontname))
3541 void VM_findfont(void)
3543 VM_SAFEPARMCOUNT(1,VM_findfont);
3544 PRVM_G_FLOAT(OFS_RETURN) = getdrawfontnum(PRVM_G_STRING(OFS_PARM0));
3551 float loadfont(string fontname, string fontmaps, string sizes, float slot)
3555 dp_font_t *FindFont(const char *title, qboolean allocate_new);
3556 void LoadFont(qboolean override, const char *name, dp_font_t *fnt, float scale, float voffset);
3557 void VM_loadfont(void)
3559 const char *fontname, *filelist, *sizes, *c, *cm;
3560 char mainfont[MAX_QPATH];
3562 float sz, scale, voffset;
3565 VM_SAFEPARMCOUNTRANGE(3,6,VM_loadfont);
3567 fontname = PRVM_G_STRING(OFS_PARM0);
3569 fontname = "default";
3571 filelist = PRVM_G_STRING(OFS_PARM1);
3573 filelist = "gfx/conchars";
3575 sizes = PRVM_G_STRING(OFS_PARM2);
3581 if (prog->argc >= 4)
3583 i = PRVM_G_FLOAT(OFS_PARM3);
3584 if (i >= 0 && i < dp_fonts.maxsize)
3587 strlcpy(f->title, fontname, sizeof(f->title)); // replace name
3591 f = FindFont(fontname, true);
3594 PRVM_G_FLOAT(OFS_RETURN) = -1;
3595 return; // something go wrong
3598 memset(f->fallbacks, 0, sizeof(f->fallbacks));
3599 memset(f->fallback_faces, 0, sizeof(f->fallback_faces));
3601 // first font is handled "normally"
3602 c = strchr(filelist, ':');
3603 cm = strchr(filelist, ',');
3604 if(c && (!cm || c < cm))
3605 f->req_face = atoi(c+1);
3611 if(!c || (c - filelist) > MAX_QPATH)
3612 strlcpy(mainfont, filelist, sizeof(mainfont));
3615 memcpy(mainfont, filelist, c - filelist);
3616 mainfont[c - filelist] = 0;
3620 for(i = 0; i < MAX_FONT_FALLBACKS; ++i)
3622 c = strchr(filelist, ',');
3628 c = strchr(filelist, ':');
3629 cm = strchr(filelist, ',');
3630 if(c && (!cm || c < cm))
3631 f->fallback_faces[i] = atoi(c+1);
3634 f->fallback_faces[i] = 0; // f->req_face; could make it stick to the default-font's face index
3637 if(!c || (c-filelist) > MAX_QPATH)
3639 strlcpy(f->fallbacks[i], filelist, sizeof(mainfont));
3643 memcpy(f->fallbacks[i], filelist, c - filelist);
3644 f->fallbacks[i][c - filelist] = 0;
3649 for(i = 0; i < MAX_FONT_SIZES; ++i)
3650 f->req_sizes[i] = -1;
3651 for (numsizes = 0,c = sizes;;)
3653 if (!COM_ParseToken_VM_Tokenize(&c, 0))
3655 sz = atof(com_token);
3657 if (sz < 0.001f || sz > 1000.0f)
3659 VM_Warning("VM_loadfont: crap size %s", com_token);
3663 if (numsizes == MAX_FONT_SIZES)
3665 VM_Warning("VM_loadfont: MAX_FONT_SIZES = %i exceeded", MAX_FONT_SIZES);
3668 f->req_sizes[numsizes] = sz;
3672 // additional scale/hoffset parms
3675 if (prog->argc >= 5)
3677 scale = PRVM_G_FLOAT(OFS_PARM4);
3681 if (prog->argc >= 6)
3682 voffset = PRVM_G_FLOAT(OFS_PARM5);
3685 LoadFont(true, mainfont, f, scale, voffset);
3687 // return index of loaded font
3688 PRVM_G_FLOAT(OFS_RETURN) = (f - dp_fonts.f);
3695 float drawpic(vector position, string pic, vector size, vector rgb, float alpha, float flag)
3698 void VM_drawpic(void)
3700 const char *picname;
3701 float *size, *pos, *rgb;
3704 VM_SAFEPARMCOUNTRANGE(5,6,VM_drawpic);
3706 picname = PRVM_G_STRING(OFS_PARM1);
3707 VM_CheckEmptyString (picname);
3709 // is pic cached ? no function yet for that
3712 PRVM_G_FLOAT(OFS_RETURN) = -4;
3713 VM_Warning("VM_drawpic: %s: %s not cached !\n", PRVM_NAME, picname);
3717 pos = PRVM_G_VECTOR(OFS_PARM0);
3718 size = PRVM_G_VECTOR(OFS_PARM2);
3719 rgb = PRVM_G_VECTOR(OFS_PARM3);
3720 if (prog->argc >= 6)
3721 flag = (int) PRVM_G_FLOAT(OFS_PARM5);
3723 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3725 PRVM_G_FLOAT(OFS_RETURN) = -2;
3726 VM_Warning("VM_drawpic: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
3730 if(pos[2] || size[2])
3731 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")));
3733 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);
3734 PRVM_G_FLOAT(OFS_RETURN) = 1;
3740 float drawrotpic(vector position, string pic, vector size, vector org, float angle, vector rgb, float alpha, float flag)
3743 void VM_drawrotpic(void)
3745 const char *picname;
3746 float *size, *pos, *org, *rgb;
3749 VM_SAFEPARMCOUNT(8,VM_drawrotpic);
3751 picname = PRVM_G_STRING(OFS_PARM1);
3752 VM_CheckEmptyString (picname);
3754 // is pic cached ? no function yet for that
3757 PRVM_G_FLOAT(OFS_RETURN) = -4;
3758 VM_Warning("VM_drawrotpic: %s: %s not cached !\n", PRVM_NAME, picname);
3762 pos = PRVM_G_VECTOR(OFS_PARM0);
3763 size = PRVM_G_VECTOR(OFS_PARM2);
3764 org = PRVM_G_VECTOR(OFS_PARM3);
3765 rgb = PRVM_G_VECTOR(OFS_PARM5);
3766 flag = (int) PRVM_G_FLOAT(OFS_PARM7);
3768 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3770 PRVM_G_FLOAT(OFS_RETURN) = -2;
3771 VM_Warning("VM_drawrotpic: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
3775 if(pos[2] || size[2] || org[2])
3776 Con_Printf("VM_drawrotpic: z value from pos/size/org discarded\n");
3778 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);
3779 PRVM_G_FLOAT(OFS_RETURN) = 1;
3785 float drawsubpic(vector position, vector size, string pic, vector srcPos, vector srcSize, vector rgb, float alpha, float flag)
3789 void VM_drawsubpic(void)
3791 const char *picname;
3792 float *size, *pos, *rgb, *srcPos, *srcSize, alpha;
3795 VM_SAFEPARMCOUNT(8,VM_drawsubpic);
3797 picname = PRVM_G_STRING(OFS_PARM2);
3798 VM_CheckEmptyString (picname);
3800 // is pic cached ? no function yet for that
3803 PRVM_G_FLOAT(OFS_RETURN) = -4;
3804 VM_Warning("VM_drawsubpic: %s: %s not cached !\n", PRVM_NAME, picname);
3808 pos = PRVM_G_VECTOR(OFS_PARM0);
3809 size = PRVM_G_VECTOR(OFS_PARM1);
3810 srcPos = PRVM_G_VECTOR(OFS_PARM3);
3811 srcSize = PRVM_G_VECTOR(OFS_PARM4);
3812 rgb = PRVM_G_VECTOR(OFS_PARM5);
3813 alpha = PRVM_G_FLOAT(OFS_PARM6);
3814 flag = (int) PRVM_G_FLOAT(OFS_PARM7);
3816 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3818 PRVM_G_FLOAT(OFS_RETURN) = -2;
3819 VM_Warning("VM_drawsubpic: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
3823 if(pos[2] || size[2])
3824 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")));
3826 DrawQ_SuperPic(pos[0], pos[1], Draw_CachePic_Flags (picname, CACHEPICFLAG_NOTPERSISTENT),
3828 srcPos[0], srcPos[1], rgb[0], rgb[1], rgb[2], alpha,
3829 srcPos[0] + srcSize[0], srcPos[1], rgb[0], rgb[1], rgb[2], alpha,
3830 srcPos[0], srcPos[1] + srcSize[1], rgb[0], rgb[1], rgb[2], alpha,
3831 srcPos[0] + srcSize[0], srcPos[1] + srcSize[1], rgb[0], rgb[1], rgb[2], alpha,
3833 PRVM_G_FLOAT(OFS_RETURN) = 1;
3840 float drawfill(vector position, vector size, vector rgb, float alpha, float flag)
3843 void VM_drawfill(void)
3845 float *size, *pos, *rgb;
3848 VM_SAFEPARMCOUNT(5,VM_drawfill);
3851 pos = PRVM_G_VECTOR(OFS_PARM0);
3852 size = PRVM_G_VECTOR(OFS_PARM1);
3853 rgb = PRVM_G_VECTOR(OFS_PARM2);
3854 flag = (int) PRVM_G_FLOAT(OFS_PARM4);
3856 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3858 PRVM_G_FLOAT(OFS_RETURN) = -2;
3859 VM_Warning("VM_drawfill: %s: wrong DRAWFLAG %i !\n",PRVM_NAME,flag);
3863 if(pos[2] || size[2])
3864 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")));
3866 DrawQ_Fill(pos[0], pos[1], size[0], size[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM3), flag);
3867 PRVM_G_FLOAT(OFS_RETURN) = 1;
3874 drawsetcliparea(float x, float y, float width, float height)
3877 void VM_drawsetcliparea(void)
3880 VM_SAFEPARMCOUNT(4,VM_drawsetcliparea);
3882 x = bound(0, PRVM_G_FLOAT(OFS_PARM0), vid_conwidth.integer);
3883 y = bound(0, PRVM_G_FLOAT(OFS_PARM1), vid_conheight.integer);
3884 w = bound(0, PRVM_G_FLOAT(OFS_PARM2) + PRVM_G_FLOAT(OFS_PARM0) - x, (vid_conwidth.integer - x));
3885 h = bound(0, PRVM_G_FLOAT(OFS_PARM3) + PRVM_G_FLOAT(OFS_PARM1) - y, (vid_conheight.integer - y));
3887 DrawQ_SetClipArea(x, y, w, h);
3892 VM_drawresetcliparea
3897 void VM_drawresetcliparea(void)
3899 VM_SAFEPARMCOUNT(0,VM_drawresetcliparea);
3901 DrawQ_ResetClipArea();
3908 vector getimagesize(string pic)
3911 void VM_getimagesize(void)
3916 VM_SAFEPARMCOUNT(1,VM_getimagesize);
3918 p = PRVM_G_STRING(OFS_PARM0);
3919 VM_CheckEmptyString (p);
3921 pic = Draw_CachePic_Flags (p, CACHEPICFLAG_NOTPERSISTENT);
3923 PRVM_G_VECTOR(OFS_RETURN)[0] = pic->width;
3924 PRVM_G_VECTOR(OFS_RETURN)[1] = pic->height;
3925 PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
3932 string keynumtostring(float keynum)
3935 void VM_keynumtostring (void)
3937 VM_SAFEPARMCOUNT(1, VM_keynumtostring);
3939 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(Key_KeynumToString((int)PRVM_G_FLOAT(OFS_PARM0)));
3944 VM_findkeysforcommand
3946 string findkeysforcommand(string command, float bindmap)
3948 the returned string is an altstring
3951 #define FKFC_NUMKEYS 5
3952 void M_FindKeysForCommand(const char *command, int *keys);
3953 void VM_findkeysforcommand(void)
3956 char ret[VM_STRINGTEMP_LENGTH];
3957 int keys[FKFC_NUMKEYS];
3961 VM_SAFEPARMCOUNTRANGE(1, 2, VM_findkeysforcommand);
3963 cmd = PRVM_G_STRING(OFS_PARM0);
3965 bindmap = bound(-1, PRVM_G_FLOAT(OFS_PARM1), MAX_BINDMAPS-1);
3967 bindmap = 0; // consistent to "bind"
3969 VM_CheckEmptyString(cmd);
3971 Key_FindKeysForCommand(cmd, keys, FKFC_NUMKEYS, bindmap);
3974 for(i = 0; i < FKFC_NUMKEYS; i++)
3975 strlcat(ret, va(" \'%i\'", keys[i]), sizeof(ret));
3977 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(ret);
3984 float stringtokeynum(string key)
3987 void VM_stringtokeynum (void)
3989 VM_SAFEPARMCOUNT( 1, VM_keynumtostring );
3991 PRVM_G_FLOAT(OFS_RETURN) = Key_StringToKeynum(PRVM_G_STRING(OFS_PARM0));
3998 string getkeybind(float key, float bindmap)
4001 void VM_getkeybind (void)
4004 VM_SAFEPARMCOUNTRANGE(1, 2, VM_CL_getkeybind);
4006 bindmap = bound(-1, PRVM_G_FLOAT(OFS_PARM1), MAX_BINDMAPS-1);
4008 bindmap = 0; // consistent to "bind"
4010 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(Key_GetBind((int)PRVM_G_FLOAT(OFS_PARM0), bindmap));
4017 float setkeybind(float key, string cmd, float bindmap)
4020 void VM_setkeybind (void)
4023 VM_SAFEPARMCOUNTRANGE(2, 3, VM_CL_setkeybind);
4025 bindmap = bound(-1, PRVM_G_FLOAT(OFS_PARM2), MAX_BINDMAPS-1);
4027 bindmap = 0; // consistent to "bind"
4029 PRVM_G_FLOAT(OFS_RETURN) = 0;
4030 if(Key_SetBinding((int)PRVM_G_FLOAT(OFS_PARM0), bindmap, PRVM_G_STRING(OFS_PARM1)))
4031 PRVM_G_FLOAT(OFS_RETURN) = 1;
4038 vector getbindmaps()
4041 void VM_getbindmaps (void)
4044 VM_SAFEPARMCOUNT(0, VM_CL_getbindmap);
4045 Key_GetBindMap(&fg, &bg);
4046 PRVM_G_VECTOR(OFS_RETURN)[0] = fg;
4047 PRVM_G_VECTOR(OFS_RETURN)[1] = bg;
4048 PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
4055 float setbindmaps(vector bindmap)
4058 void VM_setbindmaps (void)
4060 VM_SAFEPARMCOUNT(1, VM_CL_setbindmap);
4061 PRVM_G_FLOAT(OFS_RETURN) = 0;
4062 if(PRVM_G_VECTOR(OFS_PARM0)[2] == 0)
4063 if(Key_SetBindMap((int)PRVM_G_VECTOR(OFS_PARM0)[0], (int)PRVM_G_VECTOR(OFS_PARM0)[1]))
4064 PRVM_G_FLOAT(OFS_RETURN) = 1;
4067 // CL_Video interface functions
4070 ========================
4073 float cin_open(string file, string name)
4074 ========================
4076 void VM_cin_open( void )
4081 VM_SAFEPARMCOUNT( 2, VM_cin_open );
4083 file = PRVM_G_STRING( OFS_PARM0 );
4084 name = PRVM_G_STRING( OFS_PARM1 );
4086 VM_CheckEmptyString( file );
4087 VM_CheckEmptyString( name );
4089 if( CL_OpenVideo( file, name, MENUOWNER, "" ) )
4090 PRVM_G_FLOAT( OFS_RETURN ) = 1;
4092 PRVM_G_FLOAT( OFS_RETURN ) = 0;
4096 ========================
4099 void cin_close(string name)
4100 ========================
4102 void VM_cin_close( void )
4106 VM_SAFEPARMCOUNT( 1, VM_cin_close );
4108 name = PRVM_G_STRING( OFS_PARM0 );
4109 VM_CheckEmptyString( name );
4111 CL_CloseVideo( CL_GetVideoByName( name ) );
4115 ========================
4117 void cin_setstate(string name, float type)
4118 ========================
4120 void VM_cin_setstate( void )
4123 clvideostate_t state;
4126 VM_SAFEPARMCOUNT( 2, VM_cin_netstate );
4128 name = PRVM_G_STRING( OFS_PARM0 );
4129 VM_CheckEmptyString( name );
4131 state = (clvideostate_t)((int)PRVM_G_FLOAT( OFS_PARM1 ));
4133 video = CL_GetVideoByName( name );
4134 if( video && state > CLVIDEO_UNUSED && state < CLVIDEO_STATECOUNT )
4135 CL_SetVideoState( video, state );
4139 ========================
4142 float cin_getstate(string name)
4143 ========================
4145 void VM_cin_getstate( void )
4150 VM_SAFEPARMCOUNT( 1, VM_cin_getstate );
4152 name = PRVM_G_STRING( OFS_PARM0 );
4153 VM_CheckEmptyString( name );
4155 video = CL_GetVideoByName( name );
4157 PRVM_G_FLOAT( OFS_RETURN ) = (int)video->state;
4159 PRVM_G_FLOAT( OFS_RETURN ) = 0;
4163 ========================
4166 void cin_restart(string name)
4167 ========================
4169 void VM_cin_restart( void )
4174 VM_SAFEPARMCOUNT( 1, VM_cin_restart );
4176 name = PRVM_G_STRING( OFS_PARM0 );
4177 VM_CheckEmptyString( name );
4179 video = CL_GetVideoByName( name );
4181 CL_RestartVideo( video );
4185 ========================
4187 ========================
4189 void VM_Gecko_Init( void ) {
4190 // the prog struct is memset to 0 by Initprog? [12/6/2007 Black]
4191 // FIXME: remove the other _Init functions then, too? [12/6/2007 Black]
4195 ========================
4197 ========================
4199 void VM_Gecko_Destroy( void ) {
4201 for( i = 0 ; i < PRVM_MAX_GECKOINSTANCES ; i++ ) {
4202 clgecko_t **instance = &prog->opengeckoinstances[ i ];
4204 CL_Gecko_DestroyBrowser( *instance );
4211 ========================
4214 float[bool] gecko_create( string name )
4215 ========================
4217 void VM_gecko_create( void ) {
4220 clgecko_t *instance;
4222 VM_SAFEPARMCOUNT( 1, VM_gecko_create );
4224 name = PRVM_G_STRING( OFS_PARM0 );
4225 VM_CheckEmptyString( name );
4227 // find an empty slot for this gecko browser..
4228 for( i = 0 ; i < PRVM_MAX_GECKOINSTANCES ; i++ ) {
4229 if( prog->opengeckoinstances[ i ] == NULL ) {
4233 if( i == PRVM_MAX_GECKOINSTANCES ) {
4234 VM_Warning("VM_gecko_create: %s ran out of gecko handles (%i)\n", PRVM_NAME, PRVM_MAX_GECKOINSTANCES);
4235 PRVM_G_FLOAT( OFS_RETURN ) = 0;
4239 instance = prog->opengeckoinstances[ i ] = CL_Gecko_CreateBrowser( name, PRVM_GetProgNr() );
4241 // TODO: error handling [12/3/2007 Black]
4242 PRVM_G_FLOAT( OFS_RETURN ) = 0;
4245 PRVM_G_FLOAT( OFS_RETURN ) = 1;
4249 ========================
4252 void gecko_destroy( string name )
4253 ========================
4255 void VM_gecko_destroy( void ) {
4257 clgecko_t *instance;
4259 VM_SAFEPARMCOUNT( 1, VM_gecko_destroy );
4261 name = PRVM_G_STRING( OFS_PARM0 );
4262 VM_CheckEmptyString( name );
4263 instance = CL_Gecko_FindBrowser( name );
4267 CL_Gecko_DestroyBrowser( instance );
4271 ========================
4274 void gecko_navigate( string name, string URI )
4275 ========================
4277 void VM_gecko_navigate( void ) {
4280 clgecko_t *instance;
4282 VM_SAFEPARMCOUNT( 2, VM_gecko_navigate );
4284 name = PRVM_G_STRING( OFS_PARM0 );
4285 URI = PRVM_G_STRING( OFS_PARM1 );
4286 VM_CheckEmptyString( name );
4287 VM_CheckEmptyString( URI );
4289 instance = CL_Gecko_FindBrowser( name );
4293 CL_Gecko_NavigateToURI( instance, URI );
4297 ========================
4300 float[bool] gecko_keyevent( string name, float key, float eventtype )
4301 ========================
4303 void VM_gecko_keyevent( void ) {
4306 clgecko_buttoneventtype_t eventtype;
4307 clgecko_t *instance;
4309 VM_SAFEPARMCOUNT( 3, VM_gecko_keyevent );
4311 name = PRVM_G_STRING( OFS_PARM0 );
4312 VM_CheckEmptyString( name );
4313 key = (unsigned int) PRVM_G_FLOAT( OFS_PARM1 );
4314 switch( (unsigned int) PRVM_G_FLOAT( OFS_PARM2 ) ) {
4316 eventtype = CLG_BET_DOWN;
4319 eventtype = CLG_BET_UP;
4322 eventtype = CLG_BET_PRESS;
4325 eventtype = CLG_BET_DOUBLECLICK;
4328 // TODO: console printf? [12/3/2007 Black]
4329 PRVM_G_FLOAT( OFS_RETURN ) = 0;
4333 instance = CL_Gecko_FindBrowser( name );
4335 PRVM_G_FLOAT( OFS_RETURN ) = 0;
4339 PRVM_G_FLOAT( OFS_RETURN ) = (CL_Gecko_Event_Key( instance, (keynum_t) key, eventtype ) == true);
4343 ========================
4346 void gecko_mousemove( string name, float x, float y )
4347 ========================
4349 void VM_gecko_movemouse( void ) {
4352 clgecko_t *instance;
4354 VM_SAFEPARMCOUNT( 3, VM_gecko_movemouse );
4356 name = PRVM_G_STRING( OFS_PARM0 );
4357 VM_CheckEmptyString( name );
4358 x = PRVM_G_FLOAT( OFS_PARM1 );
4359 y = PRVM_G_FLOAT( OFS_PARM2 );
4361 instance = CL_Gecko_FindBrowser( name );
4365 CL_Gecko_Event_CursorMove( instance, x, y );
4370 ========================
4373 void gecko_resize( string name, float w, float h )
4374 ========================
4376 void VM_gecko_resize( void ) {
4379 clgecko_t *instance;
4381 VM_SAFEPARMCOUNT( 3, VM_gecko_movemouse );
4383 name = PRVM_G_STRING( OFS_PARM0 );
4384 VM_CheckEmptyString( name );
4385 w = PRVM_G_FLOAT( OFS_PARM1 );
4386 h = PRVM_G_FLOAT( OFS_PARM2 );
4388 instance = CL_Gecko_FindBrowser( name );
4392 CL_Gecko_Resize( instance, (int) w, (int) h );
4397 ========================
4398 VM_gecko_get_texture_extent
4400 vector gecko_get_texture_extent( string name )
4401 ========================
4403 void VM_gecko_get_texture_extent( void ) {
4405 clgecko_t *instance;
4407 VM_SAFEPARMCOUNT( 1, VM_gecko_movemouse );
4409 name = PRVM_G_STRING( OFS_PARM0 );
4410 VM_CheckEmptyString( name );
4412 PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
4413 instance = CL_Gecko_FindBrowser( name );
4415 PRVM_G_VECTOR(OFS_RETURN)[0] = 0;
4416 PRVM_G_VECTOR(OFS_RETURN)[1] = 0;
4419 CL_Gecko_GetTextureExtent( instance,
4420 PRVM_G_VECTOR(OFS_RETURN), PRVM_G_VECTOR(OFS_RETURN)+1 );
4429 Writes new values for v_forward, v_up, and v_right based on angles
4430 void makevectors(vector angle)
4433 void VM_makevectors (void)
4435 VM_SAFEPARMCOUNT(1, VM_makevectors);
4436 AngleVectors(PRVM_G_VECTOR(OFS_PARM0), PRVM_gameglobalvector(v_forward), PRVM_gameglobalvector(v_right), PRVM_gameglobalvector(v_up));
4443 Writes new values for v_forward, v_up, and v_right based on the given forward vector
4444 vectorvectors(vector)
4447 void VM_vectorvectors (void)
4449 VM_SAFEPARMCOUNT(1, VM_vectorvectors);
4450 VectorNormalize2(PRVM_G_VECTOR(OFS_PARM0), PRVM_gameglobalvector(v_forward));
4451 VectorVectors(PRVM_gameglobalvector(v_forward), PRVM_gameglobalvector(v_right), PRVM_gameglobalvector(v_up));
4455 ========================
4458 void drawline(float width, vector pos1, vector pos2, vector rgb, float alpha, float flags)
4459 ========================
4461 void VM_drawline (void)
4463 float *c1, *c2, *rgb;
4465 unsigned char flags;
4467 VM_SAFEPARMCOUNT(6, VM_drawline);
4468 width = PRVM_G_FLOAT(OFS_PARM0);
4469 c1 = PRVM_G_VECTOR(OFS_PARM1);
4470 c2 = PRVM_G_VECTOR(OFS_PARM2);
4471 rgb = PRVM_G_VECTOR(OFS_PARM3);
4472 alpha = PRVM_G_FLOAT(OFS_PARM4);
4473 flags = (int)PRVM_G_FLOAT(OFS_PARM5);
4474 DrawQ_Line(width, c1[0], c1[1], c2[0], c2[1], rgb[0], rgb[1], rgb[2], alpha, flags);
4477 // float(float number, float quantity) bitshift (EXT_BITSHIFT)
4478 void VM_bitshift (void)
4481 VM_SAFEPARMCOUNT(2, VM_bitshift);
4483 n1 = (int)fabs((float)((int)PRVM_G_FLOAT(OFS_PARM0)));
4484 n2 = (int)PRVM_G_FLOAT(OFS_PARM1);
4486 PRVM_G_FLOAT(OFS_RETURN) = n1;
4489 PRVM_G_FLOAT(OFS_RETURN) = (n1 >> -n2);
4491 PRVM_G_FLOAT(OFS_RETURN) = (n1 << n2);
4494 ////////////////////////////////////////
4495 // AltString functions
4496 ////////////////////////////////////////
4499 ========================
4502 float altstr_count(string)
4503 ========================
4505 void VM_altstr_count( void )
4507 const char *altstr, *pos;
4510 VM_SAFEPARMCOUNT( 1, VM_altstr_count );
4512 altstr = PRVM_G_STRING( OFS_PARM0 );
4513 //VM_CheckEmptyString( altstr );
4515 for( count = 0, pos = altstr ; *pos ; pos++ ) {
4516 if( *pos == '\\' ) {
4520 } else if( *pos == '\'' ) {
4525 PRVM_G_FLOAT( OFS_RETURN ) = (float) (count / 2);
4529 ========================
4532 string altstr_prepare(string)
4533 ========================
4535 void VM_altstr_prepare( void )
4538 const char *instr, *in;
4540 char outstr[VM_STRINGTEMP_LENGTH];
4542 VM_SAFEPARMCOUNT( 1, VM_altstr_prepare );
4544 instr = PRVM_G_STRING( OFS_PARM0 );
4546 for( out = outstr, in = instr, size = sizeof(outstr) - 1 ; size && *in ; size--, in++, out++ )
4555 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
4559 ========================
4562 string altstr_get(string, float)
4563 ========================
4565 void VM_altstr_get( void )
4567 const char *altstr, *pos;
4570 char outstr[VM_STRINGTEMP_LENGTH];
4572 VM_SAFEPARMCOUNT( 2, VM_altstr_get );
4574 altstr = PRVM_G_STRING( OFS_PARM0 );
4576 count = (int)PRVM_G_FLOAT( OFS_PARM1 );
4577 count = count * 2 + 1;
4579 for( pos = altstr ; *pos && count ; pos++ )
4580 if( *pos == '\\' ) {
4583 } else if( *pos == '\'' )
4587 PRVM_G_INT( OFS_RETURN ) = 0;
4591 for( out = outstr, size = sizeof(outstr) - 1 ; size && *pos ; size--, pos++, out++ )
4592 if( *pos == '\\' ) {
4597 } else if( *pos == '\'' )
4603 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
4607 ========================
4610 string altstr_set(string altstr, float num, string set)
4611 ========================
4613 void VM_altstr_set( void )
4616 const char *altstr, *str;
4619 char outstr[VM_STRINGTEMP_LENGTH];
4621 VM_SAFEPARMCOUNT( 3, VM_altstr_set );
4623 altstr = PRVM_G_STRING( OFS_PARM0 );
4625 num = (int)PRVM_G_FLOAT( OFS_PARM1 );
4627 str = PRVM_G_STRING( OFS_PARM2 );
4630 for( num = num * 2 + 1, in = altstr; *in && num; *out++ = *in++ )
4635 } else if( *in == '\'' ) {
4640 for( ; *str; *out++ = *str++ );
4641 // now jump over the old content
4643 if( *in == '\'' || (*in == '\\' && !*++in) )
4646 strlcpy(out, in, outstr + sizeof(outstr) - out);
4647 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
4651 ========================
4654 string altstr_ins(string altstr, float num, string set)
4655 ========================
4657 void VM_altstr_ins(void)
4663 char outstr[VM_STRINGTEMP_LENGTH];
4665 VM_SAFEPARMCOUNT(3, VM_altstr_ins);
4667 in = PRVM_G_STRING( OFS_PARM0 );
4668 num = (int)PRVM_G_FLOAT( OFS_PARM1 );
4669 set = PRVM_G_STRING( OFS_PARM2 );
4672 for( num = num * 2 + 2 ; *in && num > 0 ; *out++ = *in++ )
4677 } else if( *in == '\'' ) {
4682 for( ; *set ; *out++ = *set++ );
4685 strlcpy(out, in, outstr + sizeof(outstr) - out);
4686 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString( outstr );
4690 ////////////////////////////////////////
4691 // BufString functions
4692 ////////////////////////////////////////
4693 //[515]: string buffers support
4695 static size_t stringbuffers_sortlength;
4697 static void BufStr_Expand(prvm_stringbuffer_t *stringbuffer, int strindex)
4699 if (stringbuffer->max_strings <= strindex)
4701 char **oldstrings = stringbuffer->strings;
4702 stringbuffer->max_strings = max(stringbuffer->max_strings * 2, 128);
4703 while (stringbuffer->max_strings <= strindex)
4704 stringbuffer->max_strings *= 2;
4705 stringbuffer->strings = (char **) Mem_Alloc(prog->progs_mempool, stringbuffer->max_strings * sizeof(stringbuffer->strings[0]));
4706 if (stringbuffer->num_strings > 0)
4707 memcpy(stringbuffer->strings, oldstrings, stringbuffer->num_strings * sizeof(stringbuffer->strings[0]));
4709 Mem_Free(oldstrings);
4713 static void BufStr_Shrink(prvm_stringbuffer_t *stringbuffer)
4715 // reduce num_strings if there are empty string slots at the end
4716 while (stringbuffer->num_strings > 0 && stringbuffer->strings[stringbuffer->num_strings - 1] == NULL)
4717 stringbuffer->num_strings--;
4719 // if empty, free the string pointer array
4720 if (stringbuffer->num_strings == 0)
4722 stringbuffer->max_strings = 0;
4723 if (stringbuffer->strings)
4724 Mem_Free(stringbuffer->strings);
4725 stringbuffer->strings = NULL;
4729 static int BufStr_SortStringsUP (const void *in1, const void *in2)
4732 a = *((const char **) in1);
4733 b = *((const char **) in2);
4734 if(!a || !a[0]) return 1;
4735 if(!b || !b[0]) return -1;
4736 return strncmp(a, b, stringbuffers_sortlength);
4739 static int BufStr_SortStringsDOWN (const void *in1, const void *in2)
4742 a = *((const char **) in1);
4743 b = *((const char **) in2);
4744 if(!a || !a[0]) return 1;
4745 if(!b || !b[0]) return -1;
4746 return strncmp(b, a, stringbuffers_sortlength);
4750 ========================
4752 creates new buffer, and returns it's index, returns -1 if failed
4753 float buf_create(void) = #460;
4754 float newbuf(string format, float flags) = #460;
4755 ========================
4758 void VM_buf_create (void)
4760 prvm_stringbuffer_t *stringbuffer;
4763 VM_SAFEPARMCOUNTRANGE(0, 2, VM_buf_create);
4765 // VorteX: optional parm1 (buffer format) is unfinished, to keep intact with future databuffers extension must be set to "string"
4766 if(prog->argc >= 1 && strcmp(PRVM_G_STRING(OFS_PARM0), "string"))
4768 PRVM_G_FLOAT(OFS_RETURN) = -1;
4771 stringbuffer = (prvm_stringbuffer_t *) Mem_ExpandableArray_AllocRecord(&prog->stringbuffersarray);
4772 for (i = 0;stringbuffer != Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, i);i++);
4773 stringbuffer->origin = PRVM_AllocationOrigin();
4774 // optional flags parm
4775 if (prog->argc >= 2)
4776 stringbuffer->flags = (int)PRVM_G_FLOAT(OFS_PARM1) & 0xFF;
4777 PRVM_G_FLOAT(OFS_RETURN) = i;
4783 ========================
4785 deletes buffer and all strings in it
4786 void buf_del(float bufhandle) = #461;
4787 ========================
4789 void VM_buf_del (void)
4791 prvm_stringbuffer_t *stringbuffer;
4792 VM_SAFEPARMCOUNT(1, VM_buf_del);
4793 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4797 for (i = 0;i < stringbuffer->num_strings;i++)
4798 if (stringbuffer->strings[i])
4799 Mem_Free(stringbuffer->strings[i]);
4800 if (stringbuffer->strings)
4801 Mem_Free(stringbuffer->strings);
4802 if(stringbuffer->origin)
4803 PRVM_Free((char *)stringbuffer->origin);
4804 Mem_ExpandableArray_FreeRecord(&prog->stringbuffersarray, stringbuffer);
4808 VM_Warning("VM_buf_del: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4814 ========================
4816 how many strings are stored in buffer
4817 float buf_getsize(float bufhandle) = #462;
4818 ========================
4820 void VM_buf_getsize (void)
4822 prvm_stringbuffer_t *stringbuffer;
4823 VM_SAFEPARMCOUNT(1, VM_buf_getsize);
4825 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4828 PRVM_G_FLOAT(OFS_RETURN) = -1;
4829 VM_Warning("VM_buf_getsize: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4833 PRVM_G_FLOAT(OFS_RETURN) = stringbuffer->num_strings;
4837 ========================
4839 copy all content from one buffer to another, make sure it exists
4840 void buf_copy(float bufhandle_from, float bufhandle_to) = #463;
4841 ========================
4843 void VM_buf_copy (void)
4845 prvm_stringbuffer_t *srcstringbuffer, *dststringbuffer;
4847 VM_SAFEPARMCOUNT(2, VM_buf_copy);
4849 srcstringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4850 if(!srcstringbuffer)
4852 VM_Warning("VM_buf_copy: invalid source buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4855 i = (int)PRVM_G_FLOAT(OFS_PARM1);
4856 if(i == (int)PRVM_G_FLOAT(OFS_PARM0))
4858 VM_Warning("VM_buf_copy: source == destination (%i) in %s\n", i, PRVM_NAME);
4861 dststringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4862 if(!dststringbuffer)
4864 VM_Warning("VM_buf_copy: invalid destination buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM1), PRVM_NAME);
4868 for (i = 0;i < dststringbuffer->num_strings;i++)
4869 if (dststringbuffer->strings[i])
4870 Mem_Free(dststringbuffer->strings[i]);
4871 if (dststringbuffer->strings)
4872 Mem_Free(dststringbuffer->strings);
4873 *dststringbuffer = *srcstringbuffer;
4874 if (dststringbuffer->max_strings)
4875 dststringbuffer->strings = (char **)Mem_Alloc(prog->progs_mempool, sizeof(dststringbuffer->strings[0]) * dststringbuffer->max_strings);
4877 for (i = 0;i < dststringbuffer->num_strings;i++)
4879 if (srcstringbuffer->strings[i])
4882 stringlen = strlen(srcstringbuffer->strings[i]) + 1;
4883 dststringbuffer->strings[i] = (char *)Mem_Alloc(prog->progs_mempool, stringlen);
4884 memcpy(dststringbuffer->strings[i], srcstringbuffer->strings[i], stringlen);
4890 ========================
4892 sort buffer by beginnings of strings (cmplength defaults it's length)
4893 "backward == TRUE" means that sorting goes upside-down
4894 void buf_sort(float bufhandle, float cmplength, float backward) = #464;
4895 ========================
4897 void VM_buf_sort (void)
4899 prvm_stringbuffer_t *stringbuffer;
4900 VM_SAFEPARMCOUNT(3, VM_buf_sort);
4902 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4905 VM_Warning("VM_buf_sort: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4908 if(stringbuffer->num_strings <= 0)
4910 VM_Warning("VM_buf_sort: tried to sort empty buffer %i in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4913 stringbuffers_sortlength = (int)PRVM_G_FLOAT(OFS_PARM1);
4914 if(stringbuffers_sortlength <= 0)
4915 stringbuffers_sortlength = 0x7FFFFFFF;
4917 if(!PRVM_G_FLOAT(OFS_PARM2))
4918 qsort(stringbuffer->strings, stringbuffer->num_strings, sizeof(char*), BufStr_SortStringsUP);
4920 qsort(stringbuffer->strings, stringbuffer->num_strings, sizeof(char*), BufStr_SortStringsDOWN);
4922 BufStr_Shrink(stringbuffer);
4926 ========================
4928 concantenates all buffer string into one with "glue" separator and returns it as tempstring
4929 string buf_implode(float bufhandle, string glue) = #465;
4930 ========================
4932 void VM_buf_implode (void)
4934 prvm_stringbuffer_t *stringbuffer;
4935 char k[VM_STRINGTEMP_LENGTH];
4939 VM_SAFEPARMCOUNT(2, VM_buf_implode);
4941 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4942 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
4945 VM_Warning("VM_buf_implode: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4948 if(!stringbuffer->num_strings)
4950 sep = PRVM_G_STRING(OFS_PARM1);
4952 for(l = i = 0;i < stringbuffer->num_strings;i++)
4954 if(stringbuffer->strings[i])
4956 l += (i > 0 ? strlen(sep) : 0) + strlen(stringbuffer->strings[i]);
4957 if (l >= sizeof(k) - 1)
4959 strlcat(k, sep, sizeof(k));
4960 strlcat(k, stringbuffer->strings[i], sizeof(k));
4963 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(k);
4967 ========================
4969 get a string from buffer, returns tempstring, dont str_unzone it!
4970 string bufstr_get(float bufhandle, float string_index) = #465;
4971 ========================
4973 void VM_bufstr_get (void)
4975 prvm_stringbuffer_t *stringbuffer;
4977 VM_SAFEPARMCOUNT(2, VM_bufstr_get);
4979 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
4980 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4983 VM_Warning("VM_bufstr_get: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
4986 strindex = (int)PRVM_G_FLOAT(OFS_PARM1);
4989 // VM_Warning("VM_bufstr_get: invalid string index %i used in %s\n", strindex, PRVM_NAME);
4992 if (strindex < stringbuffer->num_strings && stringbuffer->strings[strindex])
4993 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(stringbuffer->strings[strindex]);
4997 ========================
4999 copies a string into selected slot of buffer
5000 void bufstr_set(float bufhandle, float string_index, string str) = #466;
5001 ========================
5003 void VM_bufstr_set (void)
5007 prvm_stringbuffer_t *stringbuffer;
5010 VM_SAFEPARMCOUNT(3, VM_bufstr_set);
5012 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
5015 VM_Warning("VM_bufstr_set: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
5018 strindex = (int)PRVM_G_FLOAT(OFS_PARM1);
5019 if(strindex < 0 || strindex >= 1000000) // huge number of strings
5021 VM_Warning("VM_bufstr_set: invalid string index %i used in %s\n", strindex, PRVM_NAME);
5025 BufStr_Expand(stringbuffer, strindex);
5026 stringbuffer->num_strings = max(stringbuffer->num_strings, strindex + 1);
5028 if(stringbuffer->strings[strindex])
5029 Mem_Free(stringbuffer->strings[strindex]);
5030 stringbuffer->strings[strindex] = NULL;
5032 if(PRVM_G_INT(OFS_PARM2))
5034 // not the NULL string!
5035 news = PRVM_G_STRING(OFS_PARM2);
5036 alloclen = strlen(news) + 1;
5037 stringbuffer->strings[strindex] = (char *)Mem_Alloc(prog->progs_mempool, alloclen);
5038 memcpy(stringbuffer->strings[strindex], news, alloclen);
5041 BufStr_Shrink(stringbuffer);
5045 ========================
5047 adds string to buffer in first free slot and returns its index
5048 "order == TRUE" means that string will be added after last "full" slot
5049 float bufstr_add(float bufhandle, string str, float order) = #467;
5050 ========================
5052 void VM_bufstr_add (void)
5054 int order, strindex;
5055 prvm_stringbuffer_t *stringbuffer;
5059 VM_SAFEPARMCOUNT(3, VM_bufstr_add);
5061 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
5062 PRVM_G_FLOAT(OFS_RETURN) = -1;
5065 VM_Warning("VM_bufstr_add: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
5068 if(!PRVM_G_INT(OFS_PARM1)) // NULL string
5070 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);
5073 string = PRVM_G_STRING(OFS_PARM1);
5074 order = (int)PRVM_G_FLOAT(OFS_PARM2);
5076 strindex = stringbuffer->num_strings;
5078 for (strindex = 0;strindex < stringbuffer->num_strings;strindex++)
5079 if (stringbuffer->strings[strindex] == NULL)
5082 BufStr_Expand(stringbuffer, strindex);
5084 stringbuffer->num_strings = max(stringbuffer->num_strings, strindex + 1);
5085 alloclen = strlen(string) + 1;
5086 stringbuffer->strings[strindex] = (char *)Mem_Alloc(prog->progs_mempool, alloclen);
5087 memcpy(stringbuffer->strings[strindex], string, alloclen);
5089 PRVM_G_FLOAT(OFS_RETURN) = strindex;
5093 ========================
5095 delete string from buffer
5096 void bufstr_free(float bufhandle, float string_index) = #468;
5097 ========================
5099 void VM_bufstr_free (void)
5102 prvm_stringbuffer_t *stringbuffer;
5103 VM_SAFEPARMCOUNT(2, VM_bufstr_free);
5105 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
5108 VM_Warning("VM_bufstr_free: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
5111 i = (int)PRVM_G_FLOAT(OFS_PARM1);
5114 VM_Warning("VM_bufstr_free: invalid string index %i used in %s\n", i, PRVM_NAME);
5118 if (i < stringbuffer->num_strings)
5120 if(stringbuffer->strings[i])
5121 Mem_Free(stringbuffer->strings[i]);
5122 stringbuffer->strings[i] = NULL;
5125 BufStr_Shrink(stringbuffer);
5134 void VM_buf_cvarlist(void)
5137 const char *partial, *antipartial;
5138 size_t len, antilen;
5140 qboolean ispattern, antiispattern;
5142 prvm_stringbuffer_t *stringbuffer;
5143 VM_SAFEPARMCOUNTRANGE(2, 3, VM_buf_cvarlist);
5145 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
5148 VM_Warning("VM_bufstr_free: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
5152 partial = PRVM_G_STRING(OFS_PARM1);
5156 len = strlen(partial);
5159 antipartial = PRVM_G_STRING(OFS_PARM2);
5165 antilen = strlen(antipartial);
5167 for (n = 0;n < stringbuffer->num_strings;n++)
5168 if (stringbuffer->strings[n])
5169 Mem_Free(stringbuffer->strings[n]);
5170 if (stringbuffer->strings)
5171 Mem_Free(stringbuffer->strings);
5172 stringbuffer->strings = NULL;
5174 ispattern = partial && (strchr(partial, '*') || strchr(partial, '?'));
5175 antiispattern = antipartial && (strchr(antipartial, '*') || strchr(antipartial, '?'));
5178 for(cvar = cvar_vars; cvar; cvar = cvar->next)
5180 if(len && (ispattern ? !matchpattern_with_separator(cvar->name, partial, false, "", false) : strncmp(partial, cvar->name, len)))
5183 if(antilen && (antiispattern ? matchpattern_with_separator(cvar->name, antipartial, false, "", false) : !strncmp(antipartial, cvar->name, antilen)))
5189 stringbuffer->max_strings = stringbuffer->num_strings = n;
5190 if (stringbuffer->max_strings)
5191 stringbuffer->strings = (char **)Mem_Alloc(prog->progs_mempool, sizeof(stringbuffer->strings[0]) * stringbuffer->max_strings);
5194 for(cvar = cvar_vars; cvar; cvar = cvar->next)
5196 if(len && (ispattern ? !matchpattern_with_separator(cvar->name, partial, false, "", false) : strncmp(partial, cvar->name, len)))
5199 if(antilen && (antiispattern ? matchpattern_with_separator(cvar->name, antipartial, false, "", false) : !strncmp(antipartial, cvar->name, antilen)))
5202 alloclen = strlen(cvar->name) + 1;
5203 stringbuffer->strings[n] = (char *)Mem_Alloc(prog->progs_mempool, alloclen);
5204 memcpy(stringbuffer->strings[n], cvar->name, alloclen);
5219 This was a major timewaster in progs, so it was converted to C
5222 void VM_changeyaw (void)
5225 float ideal, current, move, speed;
5227 // this is called (VERY HACKISHLY) by SV_MoveToGoal, so it can not use any
5228 // parameters because they are the parameters to SV_MoveToGoal, not this
5229 //VM_SAFEPARMCOUNT(0, VM_changeyaw);
5231 ent = PRVM_PROG_TO_EDICT(PRVM_gameglobaledict(self));
5232 if (ent == prog->edicts)
5234 VM_Warning("changeyaw: can not modify world entity\n");
5237 if (ent->priv.server->free)
5239 VM_Warning("changeyaw: can not modify free entity\n");
5242 current = PRVM_gameedictvector(ent, angles)[1];
5243 current = ANGLEMOD(current);
5244 ideal = PRVM_gameedictfloat(ent, ideal_yaw);
5245 speed = PRVM_gameedictfloat(ent, yaw_speed);
5247 if (current == ideal)
5249 move = ideal - current;
5250 if (ideal > current)
5272 PRVM_gameedictvector(ent, angles)[1] = ANGLEMOD(current);
5280 void VM_changepitch (void)
5283 float ideal, current, move, speed;
5285 VM_SAFEPARMCOUNT(1, VM_changepitch);
5287 ent = PRVM_G_EDICT(OFS_PARM0);
5288 if (ent == prog->edicts)
5290 VM_Warning("changepitch: can not modify world entity\n");
5293 if (ent->priv.server->free)
5295 VM_Warning("changepitch: can not modify free entity\n");
5298 current = PRVM_gameedictvector(ent, angles)[0];
5299 current = ANGLEMOD(current);
5300 ideal = PRVM_gameedictfloat(ent, idealpitch);
5301 speed = PRVM_gameedictfloat(ent, pitch_speed);
5303 if (current == ideal)
5305 move = ideal - current;
5306 if (ideal > current)
5328 PRVM_gameedictvector(ent, angles)[0] = ANGLEMOD(current);
5332 void VM_uncolorstring (void)
5334 char szNewString[VM_STRINGTEMP_LENGTH];
5335 const char *szString;
5338 VM_SAFEPARMCOUNT(1, VM_uncolorstring);
5339 szString = PRVM_G_STRING(OFS_PARM0);
5340 COM_StringDecolorize(szString, 0, szNewString, sizeof(szNewString), TRUE);
5341 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(szNewString);
5345 // #221 float(string str, string sub[, float startpos]) strstrofs (FTE_STRINGS)
5346 //strstr, without generating a new string. Use in conjunction with FRIK_FILE's substring for more similar strstr.
5347 void VM_strstrofs (void)
5349 const char *instr, *match;
5351 VM_SAFEPARMCOUNTRANGE(2, 3, VM_strstrofs);
5352 instr = PRVM_G_STRING(OFS_PARM0);
5353 match = PRVM_G_STRING(OFS_PARM1);
5354 firstofs = (prog->argc > 2)?(int)PRVM_G_FLOAT(OFS_PARM2):0;
5355 firstofs = u8_bytelen(instr, firstofs);
5357 if (firstofs && (firstofs < 0 || firstofs > (int)strlen(instr)))
5359 PRVM_G_FLOAT(OFS_RETURN) = -1;
5363 match = strstr(instr+firstofs, match);
5365 PRVM_G_FLOAT(OFS_RETURN) = -1;
5367 PRVM_G_FLOAT(OFS_RETURN) = u8_strnlen(instr, match-instr);
5370 //#222 string(string s, float index) str2chr (FTE_STRINGS)
5371 void VM_str2chr (void)
5376 VM_SAFEPARMCOUNT(2, VM_str2chr);
5377 s = PRVM_G_STRING(OFS_PARM0);
5378 index = u8_bytelen(s, (int)PRVM_G_FLOAT(OFS_PARM1));
5380 if((unsigned)index < strlen(s))
5382 if (utf8_enable.integer)
5383 ch = u8_getchar_noendptr(s + index);
5385 ch = (unsigned char)s[index];
5386 PRVM_G_FLOAT(OFS_RETURN) = ch;
5389 PRVM_G_FLOAT(OFS_RETURN) = 0;
5392 //#223 string(float c, ...) chr2str (FTE_STRINGS)
5393 void VM_chr2str (void)
5398 VM_SAFEPARMCOUNTRANGE(0, 8, VM_chr2str);
5399 for(i = 0;i < prog->argc && i < (int)sizeof(t) - 1;i++)
5400 t[i] = (unsigned char)PRVM_G_FLOAT(OFS_PARM0+i*3);
5402 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(t);
5407 VM_SAFEPARMCOUNTRANGE(0, 8, VM_chr2str);
5408 for(i = 0; i < prog->argc && len < sizeof(t)-1; ++i)
5409 len += u8_fromchar((Uchar)PRVM_G_FLOAT(OFS_PARM0+i*3), t + len, sizeof(t)-1);
5411 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(t);
5414 static int chrconv_number(int i, int base, int conv)
5439 static int chrconv_punct(int i, int base, int conv)
5457 static int chrchar_alpha(int i, int basec, int baset, int convc, int convt, int charnum)
5459 //convert case and colour seperatly...
5476 baset = 128*((charnum&1) == (convt-5));
5492 return i + basec + baset;
5494 // #224 string(float ccase, float calpha, float cnum, string s, ...) strconv (FTE_STRINGS)
5495 //bulk convert a string. change case or colouring.
5496 void VM_strconv (void)
5498 int ccase, redalpha, rednum, len, i;
5499 unsigned char resbuf[VM_STRINGTEMP_LENGTH];
5500 unsigned char *result = resbuf;
5502 VM_SAFEPARMCOUNTRANGE(3, 8, VM_strconv);
5504 ccase = (int) PRVM_G_FLOAT(OFS_PARM0); //0 same, 1 lower, 2 upper
5505 redalpha = (int) PRVM_G_FLOAT(OFS_PARM1); //0 same, 1 white, 2 red, 5 alternate, 6 alternate-alternate
5506 rednum = (int) PRVM_G_FLOAT(OFS_PARM2); //0 same, 1 white, 2 red, 3 redspecial, 4 whitespecial, 5 alternate, 6 alternate-alternate
5507 VM_VarString(3, (char *) resbuf, sizeof(resbuf));
5508 len = strlen((char *) resbuf);
5510 for (i = 0; i < len; i++, result++) //should this be done backwards?
5512 if (*result >= '0' && *result <= '9') //normal numbers...
5513 *result = chrconv_number(*result, '0', rednum);
5514 else if (*result >= '0'+128 && *result <= '9'+128)
5515 *result = chrconv_number(*result, '0'+128, rednum);
5516 else if (*result >= '0'+128-30 && *result <= '9'+128-30)
5517 *result = chrconv_number(*result, '0'+128-30, rednum);
5518 else if (*result >= '0'-30 && *result <= '9'-30)
5519 *result = chrconv_number(*result, '0'-30, rednum);
5521 else if (*result >= 'a' && *result <= 'z') //normal numbers...
5522 *result = chrchar_alpha(*result, 'a', 0, ccase, redalpha, i);
5523 else if (*result >= 'A' && *result <= 'Z') //normal numbers...
5524 *result = chrchar_alpha(*result, 'A', 0, ccase, redalpha, i);
5525 else if (*result >= 'a'+128 && *result <= 'z'+128) //normal numbers...
5526 *result = chrchar_alpha(*result, 'a', 128, ccase, redalpha, i);
5527 else if (*result >= 'A'+128 && *result <= 'Z'+128) //normal numbers...
5528 *result = chrchar_alpha(*result, 'A', 128, ccase, redalpha, i);
5530 else if ((*result & 127) < 16 || !redalpha) //special chars..
5532 else if (*result < 128)
5533 *result = chrconv_punct(*result, 0, redalpha);
5535 *result = chrconv_punct(*result, 128, redalpha);
5539 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString((char *) resbuf);
5542 // #225 string(float chars, string s, ...) strpad (FTE_STRINGS)
5543 void VM_strpad (void)
5545 char src[VM_STRINGTEMP_LENGTH];
5546 char destbuf[VM_STRINGTEMP_LENGTH];
5548 VM_SAFEPARMCOUNTRANGE(1, 8, VM_strpad);
5549 pad = (int) PRVM_G_FLOAT(OFS_PARM0);
5550 VM_VarString(1, src, sizeof(src));
5552 // note: < 0 = left padding, > 0 = right padding,
5553 // this is reverse logic of printf!
5554 dpsnprintf(destbuf, sizeof(destbuf), "%*s", -pad, src);
5556 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(destbuf);
5559 // #226 string(string info, string key, string value, ...) infoadd (FTE_STRINGS)
5560 //uses qw style \key\value strings
5561 void VM_infoadd (void)
5563 const char *info, *key;
5564 char value[VM_STRINGTEMP_LENGTH];
5565 char temp[VM_STRINGTEMP_LENGTH];
5567 VM_SAFEPARMCOUNTRANGE(2, 8, VM_infoadd);
5568 info = PRVM_G_STRING(OFS_PARM0);
5569 key = PRVM_G_STRING(OFS_PARM1);
5570 VM_VarString(2, value, sizeof(value));
5572 strlcpy(temp, info, VM_STRINGTEMP_LENGTH);
5574 InfoString_SetValue(temp, VM_STRINGTEMP_LENGTH, key, value);
5576 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(temp);
5579 // #227 string(string info, string key) infoget (FTE_STRINGS)
5580 //uses qw style \key\value strings
5581 void VM_infoget (void)
5585 char value[VM_STRINGTEMP_LENGTH];
5587 VM_SAFEPARMCOUNT(2, VM_infoget);
5588 info = PRVM_G_STRING(OFS_PARM0);
5589 key = PRVM_G_STRING(OFS_PARM1);
5591 InfoString_GetValue(info, key, value, VM_STRINGTEMP_LENGTH);
5593 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(value);
5596 //#228 float(string s1, string s2, float len) strncmp (FTE_STRINGS)
5597 // also float(string s1, string s2) strcmp (FRIK_FILE)
5598 void VM_strncmp (void)
5600 const char *s1, *s2;
5601 VM_SAFEPARMCOUNTRANGE(2, 3, VM_strncmp);
5602 s1 = PRVM_G_STRING(OFS_PARM0);
5603 s2 = PRVM_G_STRING(OFS_PARM1);
5606 PRVM_G_FLOAT(OFS_RETURN) = strncmp(s1, s2, (size_t)PRVM_G_FLOAT(OFS_PARM2));
5610 PRVM_G_FLOAT(OFS_RETURN) = strcmp(s1, s2);
5614 // #229 float(string s1, string s2) strcasecmp (FTE_STRINGS)
5615 // #230 float(string s1, string s2, float len) strncasecmp (FTE_STRINGS)
5616 void VM_strncasecmp (void)
5618 const char *s1, *s2;
5619 VM_SAFEPARMCOUNTRANGE(2, 3, VM_strncasecmp);
5620 s1 = PRVM_G_STRING(OFS_PARM0);
5621 s2 = PRVM_G_STRING(OFS_PARM1);
5624 PRVM_G_FLOAT(OFS_RETURN) = strncasecmp(s1, s2, (size_t)PRVM_G_FLOAT(OFS_PARM2));
5628 PRVM_G_FLOAT(OFS_RETURN) = strcasecmp(s1, s2);
5632 // #494 float(float caseinsensitive, string s, ...) crc16
5636 static char s[VM_STRINGTEMP_LENGTH];
5637 VM_SAFEPARMCOUNTRANGE(2, 8, VM_hash);
5638 insensitive = PRVM_G_FLOAT(OFS_PARM0);
5639 VM_VarString(1, s, sizeof(s));
5640 PRVM_G_FLOAT(OFS_RETURN) = (unsigned short) ((insensitive ? CRC_Block_CaseInsensitive : CRC_Block) ((unsigned char *) s, strlen(s)));
5643 void VM_wasfreed (void)
5645 VM_SAFEPARMCOUNT(1, VM_wasfreed);
5646 PRVM_G_FLOAT(OFS_RETURN) = PRVM_G_EDICT(OFS_PARM0)->priv.required->free;
5649 void VM_SetTraceGlobals(const trace_t *trace)
5651 PRVM_gameglobalfloat(trace_allsolid) = trace->allsolid;
5652 PRVM_gameglobalfloat(trace_startsolid) = trace->startsolid;
5653 PRVM_gameglobalfloat(trace_fraction) = trace->fraction;
5654 PRVM_gameglobalfloat(trace_inwater) = trace->inwater;
5655 PRVM_gameglobalfloat(trace_inopen) = trace->inopen;
5656 VectorCopy(trace->endpos, PRVM_gameglobalvector(trace_endpos));
5657 VectorCopy(trace->plane.normal, PRVM_gameglobalvector(trace_plane_normal));
5658 PRVM_gameglobalfloat(trace_plane_dist) = trace->plane.dist;
5659 PRVM_gameglobaledict(trace_ent) = PRVM_EDICT_TO_PROG(trace->ent ? trace->ent : prog->edicts);
5660 PRVM_gameglobalfloat(trace_dpstartcontents) = trace->startsupercontents;
5661 PRVM_gameglobalfloat(trace_dphitcontents) = trace->hitsupercontents;
5662 PRVM_gameglobalfloat(trace_dphitq3surfaceflags) = trace->hitq3surfaceflags;
5663 PRVM_gameglobalstring(trace_dphittexturename) = trace->hittexture ? PRVM_SetTempString(trace->hittexture->name) : 0;
5666 void VM_ClearTraceGlobals(void)
5668 // clean up all trace globals when leaving the VM (anti-triggerbot safeguard)
5669 PRVM_gameglobalfloat(trace_allsolid) = 0;
5670 PRVM_gameglobalfloat(trace_startsolid) = 0;
5671 PRVM_gameglobalfloat(trace_fraction) = 0;
5672 PRVM_gameglobalfloat(trace_inwater) = 0;
5673 PRVM_gameglobalfloat(trace_inopen) = 0;
5674 VectorClear(PRVM_gameglobalvector(trace_endpos));
5675 VectorClear(PRVM_gameglobalvector(trace_plane_normal));
5676 PRVM_gameglobalfloat(trace_plane_dist) = 0;
5677 PRVM_gameglobaledict(trace_ent) = PRVM_EDICT_TO_PROG(prog->edicts);
5678 PRVM_gameglobalfloat(trace_dpstartcontents) = 0;
5679 PRVM_gameglobalfloat(trace_dphitcontents) = 0;
5680 PRVM_gameglobalfloat(trace_dphitq3surfaceflags) = 0;
5681 PRVM_gameglobalstring(trace_dphittexturename) = 0;
5686 void VM_Cmd_Init(void)
5688 // only init the stuff for the current prog
5692 // VM_BufStr_Init();
5695 void VM_Cmd_Reset(void)
5697 CL_PurgeOwner( MENUOWNER );
5699 VM_Files_CloseAll();
5701 // VM_BufStr_ShutDown();
5704 // #510 string(string input, ...) uri_escape (DP_QC_URI_ESCAPE)
5705 // does URI escaping on a string (replace evil stuff by %AB escapes)
5706 void VM_uri_escape (void)
5708 char src[VM_STRINGTEMP_LENGTH];
5709 char dest[VM_STRINGTEMP_LENGTH];
5711 static const char *hex = "0123456789ABCDEF";
5713 VM_SAFEPARMCOUNTRANGE(1, 8, VM_uri_escape);
5714 VM_VarString(0, src, sizeof(src));
5716 for(p = src, q = dest; *p && q < dest + sizeof(dest) - 3; ++p)
5718 if((*p >= 'A' && *p <= 'Z')
5719 || (*p >= 'a' && *p <= 'z')
5720 || (*p >= '0' && *p <= '9')
5721 || (*p == '-') || (*p == '_') || (*p == '.')
5722 || (*p == '!') || (*p == '~')
5723 || (*p == '\'') || (*p == '(') || (*p == ')'))
5728 *q++ = hex[(*(unsigned char *)p >> 4) & 0xF];
5729 *q++ = hex[ *(unsigned char *)p & 0xF];
5734 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(dest);
5737 // #510 string(string input, ...) uri_unescape (DP_QC_URI_ESCAPE)
5738 // does URI unescaping on a string (get back the evil stuff)
5739 void VM_uri_unescape (void)
5741 char src[VM_STRINGTEMP_LENGTH];
5742 char dest[VM_STRINGTEMP_LENGTH];
5746 VM_SAFEPARMCOUNTRANGE(1, 8, VM_uri_unescape);
5747 VM_VarString(0, src, sizeof(src));
5749 for(p = src, q = dest; *p; ) // no need to check size, because unescape can't expand
5753 if(p[1] >= '0' && p[1] <= '9')
5755 else if(p[1] >= 'a' && p[1] <= 'f')
5756 hi = p[1] - 'a' + 10;
5757 else if(p[1] >= 'A' && p[1] <= 'F')
5758 hi = p[1] - 'A' + 10;
5761 if(p[2] >= '0' && p[2] <= '9')
5763 else if(p[2] >= 'a' && p[2] <= 'f')
5764 lo = p[2] - 'a' + 10;
5765 else if(p[2] >= 'A' && p[2] <= 'F')
5766 lo = p[2] - 'A' + 10;
5769 if(hi != 0 || lo != 0) // don't unescape NUL bytes
5770 *q++ = (char) (hi * 0x10 + lo);
5781 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(dest);
5784 // #502 string(string filename) whichpack (DP_QC_WHICHPACK)
5785 // returns the name of the pack containing a file, or "" if it is not in any pack (but local or non-existant)
5786 void VM_whichpack (void)
5788 const char *fn, *pack;
5790 VM_SAFEPARMCOUNT(1, VM_whichpack);
5791 fn = PRVM_G_STRING(OFS_PARM0);
5792 pack = FS_WhichPack(fn);
5794 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(pack ? pack : "");
5802 char buffer[MAX_INPUTLINE];
5803 unsigned char *postdata; // free when uri_to_prog_t is freed
5805 char *sigdata; // free when uri_to_prog_t is freed
5810 static void uri_to_string_callback(int status, size_t length_received, unsigned char *buffer, void *cbdata)
5812 uri_to_prog_t *handle = (uri_to_prog_t *) cbdata;
5814 if(!PRVM_ProgLoaded(handle->prognr))
5816 // curl reply came too late... so just drop it
5817 if(handle->postdata)
5818 Z_Free(handle->postdata);
5820 Z_Free(handle->sigdata);
5825 PRVM_SetProg(handle->prognr);
5827 if((prog->starttime == handle->starttime) && (PRVM_allfunction(URI_Get_Callback)))
5829 if(length_received >= sizeof(handle->buffer))
5830 length_received = sizeof(handle->buffer) - 1;
5831 handle->buffer[length_received] = 0;
5833 PRVM_G_FLOAT(OFS_PARM0) = handle->id;
5834 PRVM_G_FLOAT(OFS_PARM1) = status;
5835 PRVM_G_INT(OFS_PARM2) = PRVM_SetTempString(handle->buffer);
5836 PRVM_ExecuteProgram(PRVM_allfunction(URI_Get_Callback), "QC function URI_Get_Callback is missing");
5840 if(handle->postdata)
5841 Z_Free(handle->postdata);
5843 Z_Free(handle->sigdata);
5847 // 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
5848 // 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
5849 void VM_uri_get (void)
5854 uri_to_prog_t *handle;
5855 const char *posttype = NULL;
5856 const char *postseparator = NULL;
5857 int poststringbuffer = -1;
5859 const char *query_string = NULL;
5862 if(!PRVM_allfunction(URI_Get_Callback))
5863 PRVM_ERROR("uri_get called by %s without URI_Get_Callback defined", PRVM_NAME);
5865 VM_SAFEPARMCOUNTRANGE(2, 6, VM_uri_get);
5867 url = PRVM_G_STRING(OFS_PARM0);
5868 id = PRVM_G_FLOAT(OFS_PARM1);
5870 posttype = PRVM_G_STRING(OFS_PARM2);
5872 postseparator = PRVM_G_STRING(OFS_PARM3);
5874 poststringbuffer = PRVM_G_FLOAT(OFS_PARM4);
5876 postkeyid = PRVM_G_FLOAT(OFS_PARM5);
5877 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!
5879 query_string = strchr(url, '?');
5882 lq = query_string ? strlen(query_string) : 0;
5884 handle->prognr = PRVM_GetProgNr();
5885 handle->starttime = prog->starttime;
5889 size_t l = strlen(postseparator);
5890 if(poststringbuffer >= 0)
5895 prvm_stringbuffer_t *stringbuffer;
5896 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, poststringbuffer);
5899 VM_Warning("uri_get: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), PRVM_NAME);
5903 for(i = 0;i < stringbuffer->num_strings;i++)
5907 if(stringbuffer->strings[i])
5908 ltotal += strlen(stringbuffer->strings[i]);
5910 handle->postdata = (unsigned char *)Z_Malloc(ltotal + 1 + lq);
5911 handle->postlen = ltotal;
5913 for(i = 0;i < stringbuffer->num_strings;i++)
5917 memcpy(handle->postdata + ltotal, postseparator, l);
5920 if(stringbuffer->strings[i])
5922 memcpy(handle->postdata + ltotal, stringbuffer->strings[i], strlen(stringbuffer->strings[i]));
5923 ltotal += strlen(stringbuffer->strings[i]);
5926 if(ltotal != handle->postlen)
5927 PRVM_ERROR ("%s: string buffer content size mismatch, possible overrun", PRVM_NAME);
5931 handle->postdata = (unsigned char *)Z_Malloc(l + 1 + lq);
5932 handle->postlen = l;
5933 memcpy(handle->postdata, postseparator, l);
5935 handle->postdata[handle->postlen] = 0;
5937 memcpy(handle->postdata + handle->postlen + 1, query_string, lq);
5940 // POST: we sign postdata \0 query string
5942 handle->sigdata = (char *)Z_Malloc(8192);
5943 strlcpy(handle->sigdata, "X-D0-Blind-ID-Detached-Signature: ", 8192);
5944 l = strlen(handle->sigdata);
5945 handle->siglen = Crypto_SignDataDetached(handle->postdata, handle->postlen + 1 + lq, postkeyid, handle->sigdata + l, 8192 - l);
5948 Z_Free(handle->sigdata);
5949 handle->sigdata = NULL;
5952 ll = base64_encode((unsigned char *) (handle->sigdata + l), handle->siglen, 8192 - l - 1);
5955 Z_Free(handle->sigdata);
5956 handle->sigdata = NULL;
5959 handle->siglen = l + ll;
5960 handle->sigdata[handle->siglen] = 0;
5963 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);
5967 if(postkeyid >= 0 && query_string)
5969 // GET: we sign JUST the query string
5971 handle->sigdata = (char *)Z_Malloc(8192);
5972 strlcpy(handle->sigdata, "X-D0-Blind-ID-Detached-Signature: ", 8192);
5973 l = strlen(handle->sigdata);
5974 handle->siglen = Crypto_SignDataDetached(query_string, lq, postkeyid, handle->sigdata + l, 8192 - l);
5977 Z_Free(handle->sigdata);
5978 handle->sigdata = NULL;
5981 ll = base64_encode((unsigned char *) (handle->sigdata + l), handle->siglen, 8192 - l - 1);
5984 Z_Free(handle->sigdata);
5985 handle->sigdata = NULL;
5988 handle->siglen = l + ll;
5989 handle->sigdata[handle->siglen] = 0;
5992 handle->postdata = NULL;
5993 handle->postlen = 0;
5994 ret = Curl_Begin_ToMemory(url, 0, (unsigned char *) handle->buffer, sizeof(handle->buffer), uri_to_string_callback, handle);
5998 PRVM_G_INT(OFS_RETURN) = 1;
6002 if(handle->postdata)
6003 Z_Free(handle->postdata);
6005 Z_Free(handle->sigdata);
6007 PRVM_G_INT(OFS_RETURN) = 0;
6011 void VM_netaddress_resolve (void)
6014 char normalized[128];
6016 lhnetaddress_t addr;
6018 VM_SAFEPARMCOUNTRANGE(1, 2, VM_netaddress_resolve);
6020 ip = PRVM_G_STRING(OFS_PARM0);
6023 port = (int) PRVM_G_FLOAT(OFS_PARM1);
6025 if(LHNETADDRESS_FromString(&addr, ip, port) && LHNETADDRESS_ToString(&addr, normalized, sizeof(normalized), prog->argc > 1))
6026 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(normalized);
6028 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString("");
6031 //string(void) getextresponse = #624; // returns the next extResponse packet that was sent to this client
6032 void VM_CL_getextresponse (void)
6034 VM_SAFEPARMCOUNT(0,VM_argv);
6036 if (cl_net_extresponse_count <= 0)
6037 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
6041 --cl_net_extresponse_count;
6042 first = (cl_net_extresponse_last + NET_EXTRESPONSE_MAX - cl_net_extresponse_count) % NET_EXTRESPONSE_MAX;
6043 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(cl_net_extresponse[first]);
6047 void VM_SV_getextresponse (void)
6049 VM_SAFEPARMCOUNT(0,VM_argv);
6051 if (sv_net_extresponse_count <= 0)
6052 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
6056 --sv_net_extresponse_count;
6057 first = (sv_net_extresponse_last + NET_EXTRESPONSE_MAX - sv_net_extresponse_count) % NET_EXTRESPONSE_MAX;
6058 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(sv_net_extresponse[first]);
6064 Common functions between menu.dat and clsprogs
6068 //#349 float() isdemo
6069 void VM_CL_isdemo (void)
6071 VM_SAFEPARMCOUNT(0, VM_CL_isdemo);
6072 PRVM_G_FLOAT(OFS_RETURN) = cls.demoplayback;
6075 //#355 float() videoplaying
6076 void VM_CL_videoplaying (void)
6078 VM_SAFEPARMCOUNT(0, VM_CL_videoplaying);
6079 PRVM_G_FLOAT(OFS_RETURN) = cl_videoplaying;
6086 callfunction(...,string function_name)
6090 mfunction_t *PRVM_ED_FindFunction (const char *name);
6091 void VM_callfunction(void)
6096 VM_SAFEPARMCOUNTRANGE(1, 8, VM_callfunction);
6098 s = PRVM_G_STRING(OFS_PARM0+(prog->argc - 1)*3);
6100 VM_CheckEmptyString(s);
6102 func = PRVM_ED_FindFunction(s);
6105 PRVM_ERROR("VM_callfunciton: function %s not found !", s);
6106 else if (func->first_statement < 0)
6108 // negative statements are built in functions
6109 int builtinnumber = -func->first_statement;
6110 prog->xfunction->builtinsprofile++;
6111 if (builtinnumber < prog->numbuiltins && prog->builtins[builtinnumber])
6112 prog->builtins[builtinnumber]();
6114 PRVM_ERROR("No such builtin #%i in %s; most likely cause: outdated engine build. Try updating!", builtinnumber, PRVM_NAME);
6116 else if(func - prog->functions > 0)
6119 PRVM_ExecuteProgram(func - prog->functions,"");
6128 float isfunction(string function_name)
6131 mfunction_t *PRVM_ED_FindFunction (const char *name);
6132 void VM_isfunction(void)
6137 VM_SAFEPARMCOUNT(1, VM_isfunction);
6139 s = PRVM_G_STRING(OFS_PARM0);
6141 VM_CheckEmptyString(s);
6143 func = PRVM_ED_FindFunction(s);
6146 PRVM_G_FLOAT(OFS_RETURN) = false;
6148 PRVM_G_FLOAT(OFS_RETURN) = true;
6155 string sprintf(string format, ...)
6159 void VM_sprintf(void)
6162 char outbuf[MAX_INPUTLINE];
6163 char *o = outbuf, *end = outbuf + sizeof(outbuf), *err;
6165 int width, precision, thisarg, flags;
6169 static int dummyivec[3] = {0, 0, 0};
6170 static float dummyvec[3] = {0, 0, 0};
6172 #define PRINTF_ALTERNATE 1
6173 #define PRINTF_ZEROPAD 2
6174 #define PRINTF_LEFT 4
6175 #define PRINTF_SPACEPOSITIVE 8
6176 #define PRINTF_SIGNPOSITIVE 16
6180 s = PRVM_G_STRING(OFS_PARM0);
6182 #define GETARG_FLOAT(a) (((a)>=1 && (a)<prog->argc) ? (PRVM_G_FLOAT(OFS_PARM0 + 3 * (a))) : 0)
6183 #define GETARG_VECTOR(a) (((a)>=1 && (a)<prog->argc) ? (PRVM_G_VECTOR(OFS_PARM0 + 3 * (a))) : dummyvec)
6184 #define GETARG_INT(a) (((a)>=1 && (a)<prog->argc) ? (PRVM_G_INT(OFS_PARM0 + 3 * (a))) : 0)
6185 #define GETARG_INTVECTOR(a) (((a)>=1 && (a)<prog->argc) ? ((int*) PRVM_G_VECTOR(OFS_PARM0 + 3 * (a))) : dummyivec)
6186 #define GETARG_STRING(a) (((a)>=1 && (a)<prog->argc) ? (PRVM_G_STRING(OFS_PARM0 + 3 * (a))) : "")
6201 // complete directive format:
6210 // is number following?
6211 if(*s >= '0' && *s <= '9')
6213 width = strtol(s, &err, 10);
6216 VM_Warning("VM_sprintf: invalid directive in %s: %s\n", PRVM_NAME, s0);
6229 flags |= PRINTF_ZEROPAD;
6231 width = -1; // it was just a flag
6243 case '#': flags |= PRINTF_ALTERNATE; break;
6244 case '0': flags |= PRINTF_ZEROPAD; break;
6245 case '-': flags |= PRINTF_LEFT; break;
6246 case ' ': flags |= PRINTF_SPACEPOSITIVE; break;
6247 case '+': flags |= PRINTF_SIGNPOSITIVE; break;
6257 if(*s >= '0' && *s <= '9')
6259 width = strtol(s, &err, 10);
6260 if(!err || *err != '$')
6262 VM_Warning("VM_sprintf: invalid directive in %s: %s\n", PRVM_NAME, s0);
6269 width = GETARG_FLOAT(width);
6272 flags |= PRINTF_LEFT;
6276 else if(*s >= '0' && *s <= '9')
6278 width = strtol(s, &err, 10);
6281 VM_Warning("VM_sprintf: invalid directive in %s: %s\n", PRVM_NAME, s0);
6287 flags |= PRINTF_LEFT;
6291 // otherwise width stays -1
6300 if(*s >= '0' && *s <= '9')
6302 precision = strtol(s, &err, 10);
6303 if(!err || *err != '$')
6305 VM_Warning("VM_sprintf: invalid directive in %s: %s\n", PRVM_NAME, s0);
6311 precision = argpos++;
6312 precision = GETARG_FLOAT(precision);
6314 else if(*s >= '0' && *s <= '9')
6316 precision = strtol(s, &err, 10);
6319 VM_Warning("VM_sprintf: invalid directive in %s: %s\n", PRVM_NAME, s0);
6326 VM_Warning("VM_sprintf: invalid directive in %s: %s\n", PRVM_NAME, s0);
6335 case 'h': isfloat = 1; break;
6336 case 'l': isfloat = 0; break;
6337 case 'L': isfloat = 0; break;
6348 // now s points to the final directive char and is no longer changed
6363 if(*s != 's' && *s != 'c')
6364 if(flags & PRINTF_ALTERNATE) *f++ = '#';
6365 if(flags & PRINTF_ZEROPAD) *f++ = '0';
6366 if(flags & PRINTF_LEFT) *f++ = '-';
6367 if(flags & PRINTF_SPACEPOSITIVE) *f++ = ' ';
6368 if(flags & PRINTF_SIGNPOSITIVE) *f++ = '+';
6378 if(width < 0) // not set
6384 if(precision < 0) // not set
6385 o += dpsnprintf(o, end - o, formatbuf, width, (isfloat ? (int) GETARG_FLOAT(thisarg) : (int) GETARG_INT(thisarg)));
6387 o += dpsnprintf(o, end - o, formatbuf, width, precision, (isfloat ? (int) GETARG_FLOAT(thisarg) : (int) GETARG_INT(thisarg)));
6389 case 'o': case 'u': case 'x': case 'X':
6390 if(precision < 0) // not set
6391 o += dpsnprintf(o, end - o, formatbuf, width, (isfloat ? (unsigned int) GETARG_FLOAT(thisarg) : (unsigned int) GETARG_INT(thisarg)));
6393 o += dpsnprintf(o, end - o, formatbuf, width, precision, (isfloat ? (unsigned int) GETARG_FLOAT(thisarg) : (unsigned int) GETARG_INT(thisarg)));
6395 case 'e': case 'E': case 'f': case 'F': case 'g': case 'G':
6396 if(precision < 0) // not set
6397 o += dpsnprintf(o, end - o, formatbuf, width, (isfloat ? (double) GETARG_FLOAT(thisarg) : (double) GETARG_INT(thisarg)));
6399 o += dpsnprintf(o, end - o, formatbuf, width, precision, (isfloat ? (double) GETARG_FLOAT(thisarg) : (double) GETARG_INT(thisarg)));
6403 if(precision < 0) // not set
6404 o += dpsnprintf(o, end - o, va("%s %s %s", /* NESTED SPRINTF IS NESTED */ formatbuf, formatbuf, formatbuf),
6405 width, (isfloat ? (double) GETARG_VECTOR(thisarg)[0] : (double) GETARG_INTVECTOR(thisarg)[0]),
6406 width, (isfloat ? (double) GETARG_VECTOR(thisarg)[1] : (double) GETARG_INTVECTOR(thisarg)[1]),
6407 width, (isfloat ? (double) GETARG_VECTOR(thisarg)[2] : (double) GETARG_INTVECTOR(thisarg)[2])
6410 o += dpsnprintf(o, end - o, va("%s %s %s", /* NESTED SPRINTF IS NESTED */ formatbuf, formatbuf, formatbuf),
6411 width, precision, (isfloat ? (double) GETARG_VECTOR(thisarg)[0] : (double) GETARG_INTVECTOR(thisarg)[0]),
6412 width, precision, (isfloat ? (double) GETARG_VECTOR(thisarg)[1] : (double) GETARG_INTVECTOR(thisarg)[1]),
6413 width, precision, (isfloat ? (double) GETARG_VECTOR(thisarg)[2] : (double) GETARG_INTVECTOR(thisarg)[2])
6417 if(flags & PRINTF_ALTERNATE)
6419 if(precision < 0) // not set
6420 o += dpsnprintf(o, end - o, formatbuf, width, (isfloat ? (unsigned int) GETARG_FLOAT(thisarg) : (unsigned int) GETARG_INT(thisarg)));
6422 o += dpsnprintf(o, end - o, formatbuf, width, precision, (isfloat ? (unsigned int) GETARG_FLOAT(thisarg) : (unsigned int) GETARG_INT(thisarg)));
6426 unsigned int c = (isfloat ? (unsigned int) GETARG_FLOAT(thisarg) : (unsigned int) GETARG_INT(thisarg));
6427 const char *buf = u8_encodech(c, NULL);
6430 if(precision < 0) // not set
6431 precision = end - o - 1;
6432 o += u8_strpad(o, end - o, buf, (flags & PRINTF_LEFT) != 0, width, precision);
6436 if(flags & PRINTF_ALTERNATE)
6438 if(precision < 0) // not set
6439 o += dpsnprintf(o, end - o, formatbuf, width, GETARG_STRING(thisarg));
6441 o += dpsnprintf(o, end - o, formatbuf, width, precision, GETARG_STRING(thisarg));
6445 if(precision < 0) // not set
6446 precision = end - o - 1;
6447 o += u8_strpad(o, end - o, GETARG_STRING(thisarg), (flags & PRINTF_LEFT) != 0, width, precision);
6451 VM_Warning("VM_sprintf: invalid directive in %s: %s\n", PRVM_NAME, s0);
6466 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(outbuf);
6472 static dp_model_t *getmodel(prvm_edict_t *ed)
6474 switch(PRVM_GetProgNr())
6476 case PRVM_SERVERPROG:
6477 return SV_GetModelFromEdict(ed);
6478 case PRVM_CLIENTPROG:
6479 return CL_GetModelFromEdict(ed);
6487 unsigned int progid;
6489 frameblend_t frameblend[MAX_FRAMEBLENDS];
6490 skeleton_t *skeleton_p;
6491 skeleton_t skeleton;
6492 float *data_vertex3f;
6493 float *data_svector3f;
6494 float *data_tvector3f;
6495 float *data_normal3f;
6497 float *buf_vertex3f;
6498 float *buf_svector3f;
6499 float *buf_tvector3f;
6500 float *buf_normal3f;
6502 animatemodel_cache_t;
6503 static animatemodel_cache_t animatemodel_cache;
6505 void animatemodel(dp_model_t *model, prvm_edict_t *ed)
6507 skeleton_t *skeleton;
6508 int skeletonindex = -1;
6509 qboolean need = false;
6510 if(!(model->surfmesh.isanimated && model->AnimateVertices))
6512 animatemodel_cache.data_vertex3f = model->surfmesh.data_vertex3f;
6513 animatemodel_cache.data_svector3f = model->surfmesh.data_svector3f;
6514 animatemodel_cache.data_tvector3f = model->surfmesh.data_tvector3f;
6515 animatemodel_cache.data_normal3f = model->surfmesh.data_normal3f;
6518 if(animatemodel_cache.progid != prog->id)
6519 memset(&animatemodel_cache, 0, sizeof(animatemodel_cache));
6520 need |= (animatemodel_cache.model != model);
6521 VM_GenerateFrameGroupBlend(ed->priv.server->framegroupblend, ed);
6522 VM_FrameBlendFromFrameGroupBlend(ed->priv.server->frameblend, ed->priv.server->framegroupblend, model);
6523 need |= (memcmp(&animatemodel_cache.frameblend, &ed->priv.server->frameblend, sizeof(ed->priv.server->frameblend))) != 0;
6524 skeletonindex = (int)PRVM_gameedictfloat(ed, skeletonindex) - 1;
6525 if (!(skeletonindex >= 0 && skeletonindex < MAX_EDICTS && (skeleton = prog->skeletons[skeletonindex]) && skeleton->model->num_bones == ed->priv.server->skeleton.model->num_bones))
6527 need |= (animatemodel_cache.skeleton_p != skeleton);
6529 need |= (memcmp(&animatemodel_cache.skeleton, skeleton, sizeof(ed->priv.server->skeleton))) != 0;
6532 if(model->surfmesh.num_vertices > animatemodel_cache.max_vertices)
6534 animatemodel_cache.max_vertices = model->surfmesh.num_vertices * 2;
6535 if(animatemodel_cache.buf_vertex3f) Mem_Free(animatemodel_cache.buf_vertex3f);
6536 if(animatemodel_cache.buf_svector3f) Mem_Free(animatemodel_cache.buf_svector3f);
6537 if(animatemodel_cache.buf_tvector3f) Mem_Free(animatemodel_cache.buf_tvector3f);
6538 if(animatemodel_cache.buf_normal3f) Mem_Free(animatemodel_cache.buf_normal3f);
6539 animatemodel_cache.buf_vertex3f = (float *)Mem_Alloc(prog->progs_mempool, sizeof(float[3]) * animatemodel_cache.max_vertices);
6540 animatemodel_cache.buf_svector3f = (float *)Mem_Alloc(prog->progs_mempool, sizeof(float[3]) * animatemodel_cache.max_vertices);
6541 animatemodel_cache.buf_tvector3f = (float *)Mem_Alloc(prog->progs_mempool, sizeof(float[3]) * animatemodel_cache.max_vertices);
6542 animatemodel_cache.buf_normal3f = (float *)Mem_Alloc(prog->progs_mempool, sizeof(float[3]) * animatemodel_cache.max_vertices);
6544 animatemodel_cache.data_vertex3f = animatemodel_cache.buf_vertex3f;
6545 animatemodel_cache.data_svector3f = animatemodel_cache.buf_svector3f;
6546 animatemodel_cache.data_tvector3f = animatemodel_cache.buf_tvector3f;
6547 animatemodel_cache.data_normal3f = animatemodel_cache.buf_normal3f;
6548 VM_UpdateEdictSkeleton(ed, model, ed->priv.server->frameblend);
6549 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);
6550 animatemodel_cache.progid = prog->id;
6551 animatemodel_cache.model = model;
6552 memcpy(&animatemodel_cache.frameblend, &ed->priv.server->frameblend, sizeof(ed->priv.server->frameblend));
6553 animatemodel_cache.skeleton_p = skeleton;
6555 memcpy(&animatemodel_cache.skeleton, skeleton, sizeof(ed->priv.server->skeleton));
6558 static void getmatrix(prvm_edict_t *ed, matrix4x4_t *out)
6560 switch(PRVM_GetProgNr())
6562 case PRVM_SERVERPROG:
6563 SV_GetEntityMatrix(ed, out, false);
6565 case PRVM_CLIENTPROG:
6566 CL_GetEntityMatrix(ed, out, false);
6569 *out = identitymatrix;
6574 static void applytransform_forward(const vec3_t in, prvm_edict_t *ed, vec3_t out)
6578 Matrix4x4_Transform(&m, in, out);
6581 static void applytransform_forward_direction(const vec3_t in, prvm_edict_t *ed, vec3_t out)
6585 Matrix4x4_Transform3x3(&m, in, out);
6588 static void applytransform_inverted(const vec3_t in, prvm_edict_t *ed, vec3_t out)
6592 Matrix4x4_Invert_Full(&n, &m);
6593 Matrix4x4_Transform3x3(&n, in, out);
6596 static void applytransform_forward_normal(const vec3_t in, prvm_edict_t *ed, vec3_t out)
6601 Matrix4x4_TransformPositivePlane(&m, in[0], in[1], in[2], 0, p);
6605 static void clippointtosurface(prvm_edict_t *ed, dp_model_t *model, msurface_t *surface, vec3_t p, vec3_t out)
6608 float *v[3], facenormal[3], edgenormal[3], sidenormal[3], temp[3], offsetdist, dist, bestdist;
6610 animatemodel(model, ed);
6611 bestdist = 1000000000;
6613 for (i = 0, e = (model->surfmesh.data_element3i + 3 * surface->num_firsttriangle);i < surface->num_triangles;i++, e += 3)
6615 // clip original point to each triangle of the surface and find the
6616 // triangle that is closest
6617 v[0] = animatemodel_cache.data_vertex3f + e[0] * 3;
6618 v[1] = animatemodel_cache.data_vertex3f + e[1] * 3;
6619 v[2] = animatemodel_cache.data_vertex3f + e[2] * 3;
6620 TriangleNormal(v[0], v[1], v[2], facenormal);
6621 VectorNormalize(facenormal);
6622 offsetdist = DotProduct(v[0], facenormal) - DotProduct(p, facenormal);
6623 VectorMA(p, offsetdist, facenormal, temp);
6624 for (j = 0, k = 2;j < 3;k = j, j++)
6626 VectorSubtract(v[k], v[j], edgenormal);
6627 CrossProduct(edgenormal, facenormal, sidenormal);
6628 VectorNormalize(sidenormal);
6629 offsetdist = DotProduct(v[k], sidenormal) - DotProduct(temp, sidenormal);
6631 VectorMA(temp, offsetdist, sidenormal, temp);
6633 dist = VectorDistance2(temp, p);
6634 if (bestdist > dist)
6637 VectorCopy(temp, out);
6642 static msurface_t *getsurface(dp_model_t *model, int surfacenum)
6644 if (surfacenum < 0 || surfacenum >= model->nummodelsurfaces)
6646 return model->data_surfaces + surfacenum + model->firstmodelsurface;
6650 //PF_getsurfacenumpoints, // #434 float(entity e, float s) getsurfacenumpoints = #434;
6651 void VM_getsurfacenumpoints(void)
6654 msurface_t *surface;
6655 VM_SAFEPARMCOUNT(2, VM_getsurfacenumpoints);
6656 // return 0 if no such surface
6657 if (!(model = getmodel(PRVM_G_EDICT(OFS_PARM0))) || !(surface = getsurface(model, (int)PRVM_G_FLOAT(OFS_PARM1))))
6659 PRVM_G_FLOAT(OFS_RETURN) = 0;
6663 // note: this (incorrectly) assumes it is a simple polygon
6664 PRVM_G_FLOAT(OFS_RETURN) = surface->num_vertices;
6666 //PF_getsurfacepoint, // #435 vector(entity e, float s, float n) getsurfacepoint = #435;
6667 void VM_getsurfacepoint(void)
6671 msurface_t *surface;
6673 VM_SAFEPARMCOUNT(3, VM_getsurfacepoint);
6674 VectorClear(PRVM_G_VECTOR(OFS_RETURN));
6675 ed = PRVM_G_EDICT(OFS_PARM0);
6676 if (!(model = getmodel(ed)) || !(surface = getsurface(model, (int)PRVM_G_FLOAT(OFS_PARM1))))
6678 // note: this (incorrectly) assumes it is a simple polygon
6679 pointnum = (int)PRVM_G_FLOAT(OFS_PARM2);
6680 if (pointnum < 0 || pointnum >= surface->num_vertices)
6682 animatemodel(model, ed);
6683 applytransform_forward(&(animatemodel_cache.data_vertex3f + 3 * surface->num_firstvertex)[pointnum * 3], ed, PRVM_G_VECTOR(OFS_RETURN));
6685 //PF_getsurfacepointattribute, // #486 vector(entity e, float s, float n, float a) getsurfacepointattribute = #486;
6686 // float SPA_POSITION = 0;
6687 // float SPA_S_AXIS = 1;
6688 // float SPA_T_AXIS = 2;
6689 // float SPA_R_AXIS = 3; // same as SPA_NORMAL
6690 // float SPA_TEXCOORDS0 = 4;
6691 // float SPA_LIGHTMAP0_TEXCOORDS = 5;
6692 // float SPA_LIGHTMAP0_COLOR = 6;
6693 void VM_getsurfacepointattribute(void)
6697 msurface_t *surface;
6701 VM_SAFEPARMCOUNT(4, VM_getsurfacepoint);
6702 VectorClear(PRVM_G_VECTOR(OFS_RETURN));
6703 ed = PRVM_G_EDICT(OFS_PARM0);
6704 if (!(model = getmodel(ed)) || !(surface = getsurface(model, (int)PRVM_G_FLOAT(OFS_PARM1))))
6706 pointnum = (int)PRVM_G_FLOAT(OFS_PARM2);
6707 if (pointnum < 0 || pointnum >= surface->num_vertices)
6709 attributetype = (int) PRVM_G_FLOAT(OFS_PARM3);
6711 animatemodel(model, ed);
6713 switch( attributetype ) {
6714 // float SPA_POSITION = 0;
6716 applytransform_forward(&(animatemodel_cache.data_vertex3f + 3 * surface->num_firstvertex)[pointnum * 3], ed, PRVM_G_VECTOR(OFS_RETURN));
6718 // float SPA_S_AXIS = 1;
6720 applytransform_forward_direction(&(animatemodel_cache.data_svector3f + 3 * surface->num_firstvertex)[pointnum * 3], ed, PRVM_G_VECTOR(OFS_RETURN));
6722 // float SPA_T_AXIS = 2;
6724 applytransform_forward_direction(&(animatemodel_cache.data_tvector3f + 3 * surface->num_firstvertex)[pointnum * 3], ed, PRVM_G_VECTOR(OFS_RETURN));
6726 // float SPA_R_AXIS = 3; // same as SPA_NORMAL
6728 applytransform_forward_direction(&(animatemodel_cache.data_normal3f + 3 * surface->num_firstvertex)[pointnum * 3], ed, PRVM_G_VECTOR(OFS_RETURN));
6730 // float SPA_TEXCOORDS0 = 4;
6732 float *ret = PRVM_G_VECTOR(OFS_RETURN);
6733 float *texcoord = &(model->surfmesh.data_texcoordtexture2f + 2 * surface->num_firstvertex)[pointnum * 2];
6734 ret[0] = texcoord[0];
6735 ret[1] = texcoord[1];
6739 // float SPA_LIGHTMAP0_TEXCOORDS = 5;
6741 float *ret = PRVM_G_VECTOR(OFS_RETURN);
6742 float *texcoord = &(model->surfmesh.data_texcoordlightmap2f + 2 * surface->num_firstvertex)[pointnum * 2];
6743 ret[0] = texcoord[0];
6744 ret[1] = texcoord[1];
6748 // float SPA_LIGHTMAP0_COLOR = 6;
6750 // ignore alpha for now..
6751 VectorCopy( &(model->surfmesh.data_lightmapcolor4f + 4 * surface->num_firstvertex)[pointnum * 4], PRVM_G_VECTOR(OFS_RETURN));
6754 VectorSet( PRVM_G_VECTOR(OFS_RETURN), 0.0f, 0.0f, 0.0f );
6758 //PF_getsurfacenormal, // #436 vector(entity e, float s) getsurfacenormal = #436;
6759 void VM_getsurfacenormal(void)
6762 msurface_t *surface;
6764 VM_SAFEPARMCOUNT(2, VM_getsurfacenormal);
6765 VectorClear(PRVM_G_VECTOR(OFS_RETURN));
6766 if (!(model = getmodel(PRVM_G_EDICT(OFS_PARM0))) || !(surface = getsurface(model, (int)PRVM_G_FLOAT(OFS_PARM1))))
6768 // note: this only returns the first triangle, so it doesn't work very
6769 // well for curved surfaces or arbitrary meshes
6770 animatemodel(model, PRVM_G_EDICT(OFS_PARM0));
6771 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);
6772 applytransform_forward_normal(normal, PRVM_G_EDICT(OFS_PARM0), PRVM_G_VECTOR(OFS_RETURN));
6773 VectorNormalize(PRVM_G_VECTOR(OFS_RETURN));
6775 //PF_getsurfacetexture, // #437 string(entity e, float s) getsurfacetexture = #437;
6776 void VM_getsurfacetexture(void)
6779 msurface_t *surface;
6780 VM_SAFEPARMCOUNT(2, VM_getsurfacetexture);
6781 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
6782 if (!(model = getmodel(PRVM_G_EDICT(OFS_PARM0))) || !(surface = getsurface(model, (int)PRVM_G_FLOAT(OFS_PARM1))))
6784 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(surface->texture->name);
6786 //PF_getsurfacenearpoint, // #438 float(entity e, vector p) getsurfacenearpoint = #438;
6787 void VM_getsurfacenearpoint(void)
6789 int surfacenum, best;
6791 vec_t dist, bestdist;
6794 msurface_t *surface;
6796 VM_SAFEPARMCOUNT(2, VM_getsurfacenearpoint);
6797 PRVM_G_FLOAT(OFS_RETURN) = -1;
6798 ed = PRVM_G_EDICT(OFS_PARM0);
6799 point = PRVM_G_VECTOR(OFS_PARM1);
6801 if (!ed || ed->priv.server->free)
6803 model = getmodel(ed);
6804 if (!model || !model->num_surfaces)
6807 animatemodel(model, ed);
6809 applytransform_inverted(point, ed, p);
6811 bestdist = 1000000000;
6812 for (surfacenum = 0;surfacenum < model->nummodelsurfaces;surfacenum++)
6814 surface = model->data_surfaces + surfacenum + model->firstmodelsurface;
6815 // first see if the nearest point on the surface's box is closer than the previous match
6816 clipped[0] = bound(surface->mins[0], p[0], surface->maxs[0]) - p[0];
6817 clipped[1] = bound(surface->mins[1], p[1], surface->maxs[1]) - p[1];
6818 clipped[2] = bound(surface->mins[2], p[2], surface->maxs[2]) - p[2];
6819 dist = VectorLength2(clipped);
6820 if (dist < bestdist)
6822 // it is, check the nearest point on the actual geometry
6823 clippointtosurface(ed, model, surface, p, clipped);
6824 VectorSubtract(clipped, p, clipped);
6825 dist += VectorLength2(clipped);
6826 if (dist < bestdist)
6828 // that's closer too, store it as the best match
6834 PRVM_G_FLOAT(OFS_RETURN) = best;
6836 //PF_getsurfaceclippedpoint, // #439 vector(entity e, float s, vector p) getsurfaceclippedpoint = #439;
6837 void VM_getsurfaceclippedpoint(void)
6841 msurface_t *surface;
6843 VM_SAFEPARMCOUNT(3, VM_te_getsurfaceclippedpoint);
6844 VectorClear(PRVM_G_VECTOR(OFS_RETURN));
6845 ed = PRVM_G_EDICT(OFS_PARM0);
6846 if (!(model = getmodel(ed)) || !(surface = getsurface(model, (int)PRVM_G_FLOAT(OFS_PARM1))))
6848 animatemodel(model, ed);
6849 applytransform_inverted(PRVM_G_VECTOR(OFS_PARM2), ed, p);
6850 clippointtosurface(ed, model, surface, p, out);
6851 VectorAdd(out, PRVM_serveredictvector(ed, origin), PRVM_G_VECTOR(OFS_RETURN));
6854 //PF_getsurfacenumtriangles, // #??? float(entity e, float s) getsurfacenumtriangles = #???;
6855 void VM_getsurfacenumtriangles(void)
6858 msurface_t *surface;
6859 VM_SAFEPARMCOUNT(2, VM_SV_getsurfacenumtriangles);
6860 // return 0 if no such surface
6861 if (!(model = getmodel(PRVM_G_EDICT(OFS_PARM0))) || !(surface = getsurface(model, (int)PRVM_G_FLOAT(OFS_PARM1))))
6863 PRVM_G_FLOAT(OFS_RETURN) = 0;
6867 // note: this (incorrectly) assumes it is a simple polygon
6868 PRVM_G_FLOAT(OFS_RETURN) = surface->num_triangles;
6870 //PF_getsurfacetriangle, // #??? vector(entity e, float s, float n) getsurfacetriangle = #???;
6871 void VM_getsurfacetriangle(void)
6873 const vec3_t d = {-1, -1, -1};
6876 msurface_t *surface;
6878 VM_SAFEPARMCOUNT(3, VM_SV_getsurfacetriangle);
6879 VectorClear(PRVM_G_VECTOR(OFS_RETURN));
6880 ed = PRVM_G_EDICT(OFS_PARM0);
6881 if (!(model = getmodel(ed)) || !(surface = getsurface(model, (int)PRVM_G_FLOAT(OFS_PARM1))))
6883 trinum = (int)PRVM_G_FLOAT(OFS_PARM2);
6884 if (trinum < 0 || trinum >= surface->num_triangles)
6886 // FIXME: implement rotation/scaling
6887 VectorMA(&(model->surfmesh.data_element3i + 3 * surface->num_firsttriangle)[trinum * 3], surface->num_firstvertex, d, PRVM_G_VECTOR(OFS_RETURN));
6894 void World_Physics_ApplyCmd(prvm_edict_t *ed, edict_odefunc_t *f);
6896 #define VM_physics_ApplyCmd(ed,f) if (!ed->priv.server->ode_body) VM_physics_newstackfunction(ed, f); else World_Physics_ApplyCmd(ed, f)
6898 edict_odefunc_t *VM_physics_newstackfunction(prvm_edict_t *ed, edict_odefunc_t *f)
6900 edict_odefunc_t *newfunc, *func;
6902 newfunc = (edict_odefunc_t *)Mem_Alloc(prog->progs_mempool, sizeof(edict_odefunc_t));
6903 memcpy(newfunc, f, sizeof(edict_odefunc_t));
6904 newfunc->next = NULL;
6905 if (!ed->priv.server->ode_func)
6906 ed->priv.server->ode_func = newfunc;
6909 for (func = ed->priv.server->ode_func; func->next; func = func->next);
6910 func->next = newfunc;
6915 // void(entity e, float physics_enabled) physics_enable = #;
6916 void VM_physics_enable(void)
6921 VM_SAFEPARMCOUNT(2, VM_physics_enable);
6922 ed = PRVM_G_EDICT(OFS_PARM0);
6925 if (developer.integer > 0)
6926 VM_Warning("VM_physics_enable: null entity!\n");
6929 // entity should have MOVETYPE_PHYSICS already set, this can damage memory (making leaked allocation) so warn about this even if non-developer
6930 if (PRVM_serveredictfloat(ed, movetype) != MOVETYPE_PHYSICS)
6932 VM_Warning("VM_physics_enable: entity is not MOVETYPE_PHYSICS!\n");
6935 f.type = PRVM_G_FLOAT(OFS_PARM1) == 0 ? ODEFUNC_DISABLE : ODEFUNC_ENABLE;
6936 VM_physics_ApplyCmd(ed, &f);
6939 // void(entity e, vector force, vector relative_ofs) physics_addforce = #;
6940 void VM_physics_addforce(void)
6945 VM_SAFEPARMCOUNT(3, VM_physics_addforce);
6946 ed = PRVM_G_EDICT(OFS_PARM0);
6949 if (developer.integer > 0)
6950 VM_Warning("VM_physics_addforce: null entity!\n");
6953 // entity should have MOVETYPE_PHYSICS already set, this can damage memory (making leaked allocation) so warn about this even if non-developer
6954 if (PRVM_serveredictfloat(ed, movetype) != MOVETYPE_PHYSICS)
6956 VM_Warning("VM_physics_addforce: entity is not MOVETYPE_PHYSICS!\n");
6959 f.type = ODEFUNC_RELFORCEATPOS;
6960 VectorCopy(PRVM_G_VECTOR(OFS_PARM1), f.v1);
6961 VectorSubtract(PRVM_serveredictvector(ed, origin), PRVM_G_VECTOR(OFS_PARM2), f.v2);
6962 VM_physics_ApplyCmd(ed, &f);
6965 // void(entity e, vector torque) physics_addtorque = #;
6966 void VM_physics_addtorque(void)
6971 VM_SAFEPARMCOUNT(2, VM_physics_addtorque);
6972 ed = PRVM_G_EDICT(OFS_PARM0);
6975 if (developer.integer > 0)
6976 VM_Warning("VM_physics_addtorque: null entity!\n");
6979 // entity should have MOVETYPE_PHYSICS already set, this can damage memory (making leaked allocation) so warn about this even if non-developer
6980 if (PRVM_serveredictfloat(ed, movetype) != MOVETYPE_PHYSICS)
6982 VM_Warning("VM_physics_addtorque: entity is not MOVETYPE_PHYSICS!\n");
6985 f.type = ODEFUNC_RELTORQUE;
6986 VectorCopy(PRVM_G_VECTOR(OFS_PARM1), f.v1);
6987 VM_physics_ApplyCmd(ed, &f);