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"
19 extern cvar_t prvm_backtraceforwarnings;
21 // LordHavoc: changed this to NOT use a return statement, so that it can be used in functions that must return a value
22 void VM_Warning(prvm_prog_t *prog, const char *fmt, ...)
25 char msg[MAX_INPUTLINE];
26 static double recursive = -1;
29 dpvsnprintf(msg,sizeof(msg),fmt,argptr);
34 // TODO: either add a cvar/cmd to control the state dumping or replace some of the calls with Con_Printf [9/13/2006 Black]
35 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
38 PRVM_PrintState(prog);
44 //============================================================================
47 // TODO DONE: move vm_files and vm_fssearchlist to prvm_prog_t struct
48 // TODO: move vm_files and vm_fssearchlist back [9/13/2006 Black]
49 // TODO: (move vm_files and vm_fssearchlist to prvm_prog_t struct again) [2007-01-23 LordHavoc]
50 // TODO: will this war ever end? [2007-01-23 LordHavoc]
52 void VM_CheckEmptyString(prvm_prog_t *prog, const char *s)
54 if (ISWHITESPACE(s[0]))
55 prog->error_cmd("%s: Bad string", prog->name);
58 void VM_GenerateFrameGroupBlend(prvm_prog_t *prog, framegroupblend_t *framegroupblend, const prvm_edict_t *ed)
60 // self.frame is the interpolation target (new frame)
61 // self.frame1time is the animation base time for the interpolation target
62 // self.frame2 is the interpolation start (previous frame)
63 // self.frame2time is the animation base time for the interpolation start
64 // self.lerpfrac is the interpolation strength for self.frame2
65 // self.lerpfrac3 is the interpolation strength for self.frame3
66 // self.lerpfrac4 is the interpolation strength for self.frame4
67 // pitch angle on a player model where the animator set up 5 sets of
68 // animations and the csqc simply lerps between sets)
69 framegroupblend[0].frame = (int) PRVM_gameedictfloat(ed, frame );
70 framegroupblend[1].frame = (int) PRVM_gameedictfloat(ed, frame2 );
71 framegroupblend[2].frame = (int) PRVM_gameedictfloat(ed, frame3 );
72 framegroupblend[3].frame = (int) PRVM_gameedictfloat(ed, frame4 );
73 framegroupblend[0].start = PRVM_gameedictfloat(ed, frame1time);
74 framegroupblend[1].start = PRVM_gameedictfloat(ed, frame2time);
75 framegroupblend[2].start = PRVM_gameedictfloat(ed, frame3time);
76 framegroupblend[3].start = PRVM_gameedictfloat(ed, frame4time);
77 framegroupblend[1].lerp = PRVM_gameedictfloat(ed, lerpfrac );
78 framegroupblend[2].lerp = PRVM_gameedictfloat(ed, lerpfrac3 );
79 framegroupblend[3].lerp = PRVM_gameedictfloat(ed, lerpfrac4 );
80 // assume that the (missing) lerpfrac1 is whatever remains after lerpfrac2+lerpfrac3+lerpfrac4 are summed
81 framegroupblend[0].lerp = 1 - framegroupblend[1].lerp - framegroupblend[2].lerp - framegroupblend[3].lerp;
84 // LordHavoc: quite tempting to break apart this function to reuse the
85 // duplicated code, but I suspect it is better for performance
87 void VM_FrameBlendFromFrameGroupBlend(frameblend_t *frameblend, const framegroupblend_t *framegroupblend, const dp_model_t *model)
89 int sub2, numframes, f, i, k;
90 int isfirstframegroup = true;
92 double sublerp, lerp, d;
93 const animscene_t *scene;
94 const framegroupblend_t *g;
95 frameblend_t *blend = frameblend;
97 memset(blend, 0, MAX_FRAMEBLENDS * sizeof(*blend));
99 if (!model || !model->surfmesh.isanimated)
105 nolerp = (model->type == mod_sprite) ? !r_lerpsprites.integer : !r_lerpmodels.integer;
106 numframes = model->numframes;
107 for (k = 0, g = framegroupblend;k < MAX_FRAMEGROUPBLENDS;k++, g++)
110 if ((unsigned int)f >= (unsigned int)numframes)
112 if (developer_extra.integer)
113 Con_DPrintf("VM_FrameBlendFromFrameGroupBlend: no such frame %d in model %s\n", f, model->name);
121 if (isfirstframegroup)
124 isfirstframegroup = false;
129 if (model->animscenes)
131 scene = model->animscenes + f;
132 f = scene->firstframe;
133 if (scene->framecount > 1)
135 // this code path is only used on .zym models and torches
136 sublerp = scene->framerate * (cl.time - g->start);
137 f = (int) floor(sublerp);
140 if (sublerp < (1.0 / 65536.0f))
142 if (sublerp > (65535.0f / 65536.0f))
148 f = (f % scene->framecount);
149 sub2 = (sub2 % scene->framecount);
151 f = bound(0, f, (scene->framecount - 1)) + scene->firstframe;
152 sub2 = bound(0, sub2, (scene->framecount - 1)) + scene->firstframe;
154 // two framelerps produced from one animation
157 for (i = 0;i < MAX_FRAMEBLENDS;i++)
159 if (blend[i].lerp <= 0 || blend[i].subframe == sub2)
161 blend[i].subframe = sub2;
167 d = (1 - sublerp) * lerp;
172 for (i = 0;i < MAX_FRAMEBLENDS;i++)
174 if (blend[i].lerp <= 0 || blend[i].subframe == f)
176 blend[i].subframe = f;
185 void VM_UpdateEdictSkeleton(prvm_prog_t *prog, prvm_edict_t *ed, const dp_model_t *edmodel, const frameblend_t *frameblend)
187 if (ed->priv.server->skeleton.model != edmodel)
189 VM_RemoveEdictSkeleton(prog, ed);
190 ed->priv.server->skeleton.model = edmodel;
192 if (!ed->priv.server->skeleton.model || !ed->priv.server->skeleton.model->num_bones)
194 if(ed->priv.server->skeleton.relativetransforms)
195 Mem_Free(ed->priv.server->skeleton.relativetransforms);
196 ed->priv.server->skeleton.relativetransforms = NULL;
201 int skeletonindex = -1;
202 skeleton_t *skeleton;
203 skeletonindex = (int)PRVM_gameedictfloat(ed, skeletonindex) - 1;
204 if (skeletonindex >= 0 && skeletonindex < MAX_EDICTS && (skeleton = prog->skeletons[skeletonindex]) && skeleton->model->num_bones == ed->priv.server->skeleton.model->num_bones)
206 // custom skeleton controlled by the game (FTE_CSQC_SKELETONOBJECTS)
207 if (!ed->priv.server->skeleton.relativetransforms)
208 ed->priv.server->skeleton.relativetransforms = (matrix4x4_t *)Mem_Alloc(prog->progs_mempool, ed->priv.server->skeleton.model->num_bones * sizeof(matrix4x4_t));
209 memcpy(ed->priv.server->skeleton.relativetransforms, skeleton->relativetransforms, ed->priv.server->skeleton.model->num_bones * sizeof(matrix4x4_t));
213 if(ed->priv.server->skeleton.relativetransforms)
214 Mem_Free(ed->priv.server->skeleton.relativetransforms);
215 ed->priv.server->skeleton.relativetransforms = NULL;
220 void VM_RemoveEdictSkeleton(prvm_prog_t *prog, prvm_edict_t *ed)
222 if (ed->priv.server->skeleton.relativetransforms)
223 Mem_Free(ed->priv.server->skeleton.relativetransforms);
224 memset(&ed->priv.server->skeleton, 0, sizeof(ed->priv.server->skeleton));
230 //============================================================================
233 void VM_VarString(prvm_prog_t *prog, int first, char *out, int outlength)
239 outend = out + outlength - 1;
240 for (i = first;i < prog->argc && out < outend;i++)
242 s = PRVM_G_STRING((OFS_PARM0+i*3));
243 while (out < outend && *s)
253 returns true if the extension is supported by the server
255 checkextension(extensionname)
259 // kind of helper function
260 static qboolean checkextension(prvm_prog_t *prog, const char *name)
263 const char *e, *start;
264 len = (int)strlen(name);
266 for (e = prog->extensionstring;*e;e++)
273 while (*e && *e != ' ')
275 if ((e - start) == len && !strncasecmp(start, name, len))
277 // special sheck for ODE
278 if (!strncasecmp("DP_PHYSICS_ODE", name, 14))
281 return ode_dll ? true : false;
287 // special sheck for d0_blind_id
288 if (!strcasecmp("DP_CRYPTO", name))
289 return Crypto_Available();
290 if (!strcasecmp("DP_QC_DIGEST_SHA256", name))
291 return Crypto_Available();
299 void VM_checkextension(prvm_prog_t *prog)
301 VM_SAFEPARMCOUNT(1,VM_checkextension);
303 PRVM_G_FLOAT(OFS_RETURN) = checkextension(prog, PRVM_G_STRING(OFS_PARM0));
310 This is a TERMINAL error, which will kill off the entire prog.
316 void VM_error(prvm_prog_t *prog)
319 char string[VM_STRINGTEMP_LENGTH];
321 VM_VarString(prog, 0, string, sizeof(string));
322 Con_Printf("======%s ERROR in %s:\n%s\n", prog->name, PRVM_GetString(prog, prog->xfunction->s_name), string);
323 ed = PRVM_PROG_TO_EDICT(PRVM_allglobaledict(self));
324 PRVM_ED_Print(prog, ed, NULL);
326 prog->error_cmd("%s: Program error in function %s:\n%s\nTip: read above for entity information\n", prog->name, PRVM_GetString(prog, prog->xfunction->s_name), string);
333 Dumps out self, then an error message. The program is aborted and self is
334 removed, but the level can continue.
339 void VM_objerror(prvm_prog_t *prog)
342 char string[VM_STRINGTEMP_LENGTH];
344 VM_VarString(prog, 0, string, sizeof(string));
345 Con_Printf("======OBJECT ERROR======\n"); // , prog->name, PRVM_GetString(prog->xfunction->s_name), string); // or include them? FIXME
346 ed = PRVM_PROG_TO_EDICT(PRVM_allglobaledict(self));
347 PRVM_ED_Print(prog, ed, NULL);
348 PRVM_ED_Free (prog, ed);
349 Con_Printf("%s OBJECT ERROR in %s:\n%s\nTip: read above for entity information\n", prog->name, PRVM_GetString(prog, prog->xfunction->s_name), string);
361 void VM_print(prvm_prog_t *prog)
363 char string[VM_STRINGTEMP_LENGTH];
365 VM_VarString(prog, 0, string, sizeof(string));
373 broadcast print to everyone on server
378 void VM_bprint(prvm_prog_t *prog)
380 char string[VM_STRINGTEMP_LENGTH];
384 VM_Warning(prog, "VM_bprint: game is not server(%s) !\n", prog->name);
388 VM_VarString(prog, 0, string, sizeof(string));
389 SV_BroadcastPrint(string);
394 VM_sprint (menu & client but only if server.active == true)
396 single print to a specific client
398 sprint(float clientnum,...[string])
401 void VM_sprint(prvm_prog_t *prog)
405 char string[VM_STRINGTEMP_LENGTH];
407 VM_SAFEPARMCOUNTRANGE(1, 8, VM_sprint);
409 //find client for this entity
410 clientnum = (int)PRVM_G_FLOAT(OFS_PARM0);
411 if (!sv.active || clientnum < 0 || clientnum >= svs.maxclients || !svs.clients[clientnum].active)
413 VM_Warning(prog, "VM_sprint: %s: invalid client or server is not active !\n", prog->name);
417 client = svs.clients + clientnum;
418 if (!client->netconnection)
421 VM_VarString(prog, 1, string, sizeof(string));
422 MSG_WriteChar(&client->netconnection->message,svc_print);
423 MSG_WriteString(&client->netconnection->message, string);
430 single print to the screen
435 void VM_centerprint(prvm_prog_t *prog)
437 char string[VM_STRINGTEMP_LENGTH];
439 VM_SAFEPARMCOUNTRANGE(1, 8, VM_centerprint);
440 VM_VarString(prog, 0, string, sizeof(string));
441 SCR_CenterPrint(string);
448 vector normalize(vector)
451 void VM_normalize(prvm_prog_t *prog)
457 VM_SAFEPARMCOUNT(1,VM_normalize);
459 value1 = PRVM_G_VECTOR(OFS_PARM0);
461 f = VectorLength2(value1);
465 VectorScale(value1, f, newvalue);
468 VectorClear(newvalue);
470 VectorCopy (newvalue, PRVM_G_VECTOR(OFS_RETURN));
480 void VM_vlen(prvm_prog_t *prog)
482 VM_SAFEPARMCOUNT(1,VM_vlen);
483 PRVM_G_FLOAT(OFS_RETURN) = VectorLength(PRVM_G_VECTOR(OFS_PARM0));
490 float vectoyaw(vector)
493 void VM_vectoyaw(prvm_prog_t *prog)
498 VM_SAFEPARMCOUNT(1,VM_vectoyaw);
500 value1 = PRVM_G_VECTOR(OFS_PARM0);
502 if (value1[1] == 0 && value1[0] == 0)
506 yaw = (int) (atan2(value1[1], value1[0]) * 180 / M_PI);
511 PRVM_G_FLOAT(OFS_RETURN) = yaw;
519 vector vectoangles(vector[, vector])
522 void VM_vectoangles(prvm_prog_t *prog)
524 VM_SAFEPARMCOUNTRANGE(1, 2,VM_vectoangles);
526 AnglesFromVectors(PRVM_G_VECTOR(OFS_RETURN), PRVM_G_VECTOR(OFS_PARM0), prog->argc >= 2 ? PRVM_G_VECTOR(OFS_PARM1) : NULL, true);
533 Returns a number from 0<= num < 1
538 void VM_random(prvm_prog_t *prog)
540 VM_SAFEPARMCOUNT(0,VM_random);
542 PRVM_G_FLOAT(OFS_RETURN) = lhrandom(0, 1);
549 localsound(string sample)
552 void VM_localsound(prvm_prog_t *prog)
556 VM_SAFEPARMCOUNT(1,VM_localsound);
558 s = PRVM_G_STRING(OFS_PARM0);
560 if(!S_LocalSound (s))
562 PRVM_G_FLOAT(OFS_RETURN) = -4;
563 VM_Warning(prog, "VM_localsound: Failed to play %s for %s !\n", s, prog->name);
567 PRVM_G_FLOAT(OFS_RETURN) = 1;
577 void VM_break(prvm_prog_t *prog)
579 prog->error_cmd("%s: break statement", prog->name);
582 //============================================================================
588 Sends text over to the client's execution buffer
590 [localcmd (string, ...) or]
594 void VM_localcmd(prvm_prog_t *prog)
596 char string[VM_STRINGTEMP_LENGTH];
597 VM_SAFEPARMCOUNTRANGE(1, 8, VM_localcmd);
598 VM_VarString(prog, 0, string, sizeof(string));
599 Cbuf_AddText(string);
602 static qboolean PRVM_Cvar_ReadOk(const char *string)
605 cvar = Cvar_FindVar(string);
606 return ((cvar) && ((cvar->flags & CVAR_PRIVATE) == 0));
616 void VM_cvar(prvm_prog_t *prog)
618 char string[VM_STRINGTEMP_LENGTH];
619 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar);
620 VM_VarString(prog, 0, string, sizeof(string));
621 VM_CheckEmptyString(prog, string);
622 PRVM_G_FLOAT(OFS_RETURN) = PRVM_Cvar_ReadOk(string) ? Cvar_VariableValue(string) : 0;
629 float cvar_type (string)
630 float CVAR_TYPEFLAG_EXISTS = 1;
631 float CVAR_TYPEFLAG_SAVED = 2;
632 float CVAR_TYPEFLAG_PRIVATE = 4;
633 float CVAR_TYPEFLAG_ENGINE = 8;
634 float CVAR_TYPEFLAG_HASDESCRIPTION = 16;
635 float CVAR_TYPEFLAG_READONLY = 32;
638 void VM_cvar_type(prvm_prog_t *prog)
640 char string[VM_STRINGTEMP_LENGTH];
644 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar);
645 VM_VarString(prog, 0, string, sizeof(string));
646 VM_CheckEmptyString(prog, string);
647 cvar = Cvar_FindVar(string);
652 PRVM_G_FLOAT(OFS_RETURN) = 0;
653 return; // CVAR_TYPE_NONE
656 ret = 1; // CVAR_EXISTS
657 if(cvar->flags & CVAR_SAVE)
658 ret |= 2; // CVAR_TYPE_SAVED
659 if(cvar->flags & CVAR_PRIVATE)
660 ret |= 4; // CVAR_TYPE_PRIVATE
661 if(!(cvar->flags & CVAR_ALLOCATED))
662 ret |= 8; // CVAR_TYPE_ENGINE
663 if(cvar->description != cvar_dummy_description)
664 ret |= 16; // CVAR_TYPE_HASDESCRIPTION
665 if(cvar->flags & CVAR_READONLY)
666 ret |= 32; // CVAR_TYPE_READONLY
668 PRVM_G_FLOAT(OFS_RETURN) = ret;
675 const string VM_cvar_string (string, ...)
678 void VM_cvar_string(prvm_prog_t *prog)
680 char string[VM_STRINGTEMP_LENGTH];
681 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar_string);
682 VM_VarString(prog, 0, string, sizeof(string));
683 VM_CheckEmptyString(prog, string);
684 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, PRVM_Cvar_ReadOk(string) ? Cvar_VariableString(string) : "");
689 ========================
692 const string VM_cvar_defstring (string, ...)
693 ========================
695 void VM_cvar_defstring(prvm_prog_t *prog)
697 char string[VM_STRINGTEMP_LENGTH];
698 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar_defstring);
699 VM_VarString(prog, 0, string, sizeof(string));
700 VM_CheckEmptyString(prog, string);
701 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, Cvar_VariableDefString(string));
705 ========================
708 const string VM_cvar_description (string, ...)
709 ========================
711 void VM_cvar_description(prvm_prog_t *prog)
713 char string[VM_STRINGTEMP_LENGTH];
714 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar_description);
715 VM_VarString(prog, 0, string, sizeof(string));
716 VM_CheckEmptyString(prog, string);
717 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, Cvar_VariableDescription(string));
723 void cvar_set (string,string, ...)
726 void VM_cvar_set(prvm_prog_t *prog)
729 char string[VM_STRINGTEMP_LENGTH];
730 VM_SAFEPARMCOUNTRANGE(2,8,VM_cvar_set);
731 VM_VarString(prog, 1, string, sizeof(string));
732 name = PRVM_G_STRING(OFS_PARM0);
733 VM_CheckEmptyString(prog, name);
734 Cvar_Set(name, string);
744 void VM_dprint(prvm_prog_t *prog)
746 char string[VM_STRINGTEMP_LENGTH];
747 VM_SAFEPARMCOUNTRANGE(1, 8, VM_dprint);
748 VM_VarString(prog, 0, string, sizeof(string));
750 Con_DPrintf("%s", string);
752 Con_DPrintf("%s: %s", prog->name, string);
764 void VM_ftos(prvm_prog_t *prog)
769 VM_SAFEPARMCOUNT(1, VM_ftos);
771 v = PRVM_G_FLOAT(OFS_PARM0);
773 if ((float)((int)v) == v)
774 dpsnprintf(s, sizeof(s), "%i", (int)v);
776 dpsnprintf(s, sizeof(s), "%f", v);
777 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, s);
788 void VM_fabs(prvm_prog_t *prog)
792 VM_SAFEPARMCOUNT(1,VM_fabs);
794 v = PRVM_G_FLOAT(OFS_PARM0);
795 PRVM_G_FLOAT(OFS_RETURN) = fabs(v);
806 void VM_vtos(prvm_prog_t *prog)
810 VM_SAFEPARMCOUNT(1,VM_vtos);
812 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]);
813 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, s);
824 void VM_etos(prvm_prog_t *prog)
828 VM_SAFEPARMCOUNT(1, VM_etos);
830 dpsnprintf (s, sizeof(s), "entity %i", PRVM_G_EDICTNUM(OFS_PARM0));
831 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, s);
838 float stof(...[string])
841 void VM_stof(prvm_prog_t *prog)
843 char string[VM_STRINGTEMP_LENGTH];
844 VM_SAFEPARMCOUNTRANGE(1, 8, VM_stof);
845 VM_VarString(prog, 0, string, sizeof(string));
846 PRVM_G_FLOAT(OFS_RETURN) = atof(string);
850 ========================
854 ========================
856 void VM_itof(prvm_prog_t *prog)
858 VM_SAFEPARMCOUNT(1, VM_itof);
859 PRVM_G_FLOAT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
863 ========================
866 entity ftoe(float num)
867 ========================
869 void VM_ftoe(prvm_prog_t *prog)
872 VM_SAFEPARMCOUNT(1, VM_ftoe);
874 ent = (int)PRVM_G_FLOAT(OFS_PARM0);
875 if (ent < 0 || ent >= prog->max_edicts || PRVM_PROG_TO_EDICT(ent)->priv.required->free)
876 ent = 0; // return world instead of a free or invalid entity
878 PRVM_G_INT(OFS_RETURN) = ent;
882 ========================
885 float etof(entity ent)
886 ========================
888 void VM_etof(prvm_prog_t *prog)
890 VM_SAFEPARMCOUNT(1, VM_etof);
891 PRVM_G_FLOAT(OFS_RETURN) = PRVM_G_EDICTNUM(OFS_PARM0);
898 string strftime(float uselocaltime, string[, string ...])
901 void VM_strftime(prvm_prog_t *prog)
910 char fmt[VM_STRINGTEMP_LENGTH];
911 char result[VM_STRINGTEMP_LENGTH];
912 VM_SAFEPARMCOUNTRANGE(2, 8, VM_strftime);
913 VM_VarString(prog, 1, fmt, sizeof(fmt));
916 if (PRVM_G_FLOAT(OFS_PARM0))
917 tmresult = localtime_s(&tm, &t);
919 tmresult = gmtime_s(&tm, &t);
922 if (PRVM_G_FLOAT(OFS_PARM0))
929 PRVM_G_INT(OFS_RETURN) = 0;
933 strftime(result, sizeof(result), fmt, &tm);
935 strftime(result, sizeof(result), fmt, tm);
937 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, result);
948 void VM_spawn(prvm_prog_t *prog)
951 VM_SAFEPARMCOUNT(0, VM_spawn);
952 prog->xfunction->builtinsprofile += 20;
953 ed = PRVM_ED_Alloc(prog);
965 void VM_remove(prvm_prog_t *prog)
968 prog->xfunction->builtinsprofile += 20;
970 VM_SAFEPARMCOUNT(1, VM_remove);
972 ed = PRVM_G_EDICT(OFS_PARM0);
973 if( PRVM_NUM_FOR_EDICT(ed) <= prog->reserved_edicts )
975 if (developer.integer > 0)
976 VM_Warning(prog, "VM_remove: tried to remove the null entity or a reserved entity!\n" );
978 else if( ed->priv.required->free )
980 if (developer.integer > 0)
981 VM_Warning(prog, "VM_remove: tried to remove an already freed entity!\n" );
984 PRVM_ED_Free (prog, ed);
991 entity find(entity start, .string field, string match)
995 void VM_find(prvm_prog_t *prog)
1002 VM_SAFEPARMCOUNT(3,VM_find);
1004 e = PRVM_G_EDICTNUM(OFS_PARM0);
1005 f = PRVM_G_INT(OFS_PARM1);
1006 s = PRVM_G_STRING(OFS_PARM2);
1008 // LordHavoc: apparently BloodMage does a find(world, weaponmodel, "") and
1009 // expects it to find all the monsters, so we must be careful to support
1012 for (e++ ; e < prog->num_edicts ; e++)
1014 prog->xfunction->builtinsprofile++;
1015 ed = PRVM_EDICT_NUM(e);
1016 if (ed->priv.required->free)
1018 t = PRVM_E_STRING(ed,f);
1023 VM_RETURN_EDICT(ed);
1028 VM_RETURN_EDICT(prog->edicts);
1035 entity findfloat(entity start, .float field, float match)
1036 entity findentity(entity start, .entity field, entity match)
1039 // LordHavoc: added this for searching float, int, and entity reference fields
1040 void VM_findfloat(prvm_prog_t *prog)
1047 VM_SAFEPARMCOUNT(3,VM_findfloat);
1049 e = PRVM_G_EDICTNUM(OFS_PARM0);
1050 f = PRVM_G_INT(OFS_PARM1);
1051 s = PRVM_G_FLOAT(OFS_PARM2);
1053 for (e++ ; e < prog->num_edicts ; e++)
1055 prog->xfunction->builtinsprofile++;
1056 ed = PRVM_EDICT_NUM(e);
1057 if (ed->priv.required->free)
1059 if (PRVM_E_FLOAT(ed,f) == s)
1061 VM_RETURN_EDICT(ed);
1066 VM_RETURN_EDICT(prog->edicts);
1073 entity findchain(.string field, string match)
1076 // chained search for strings in entity fields
1077 // entity(.string field, string match) findchain = #402;
1078 void VM_findchain(prvm_prog_t *prog)
1083 prvm_edict_t *ent, *chain;
1086 VM_SAFEPARMCOUNTRANGE(2,3,VM_findchain);
1089 chainfield = PRVM_G_INT(OFS_PARM2);
1091 chainfield = prog->fieldoffsets.chain;
1093 prog->error_cmd("VM_findchain: %s doesnt have the specified chain field !", prog->name);
1095 chain = prog->edicts;
1097 f = PRVM_G_INT(OFS_PARM0);
1098 s = PRVM_G_STRING(OFS_PARM1);
1100 // LordHavoc: apparently BloodMage does a find(world, weaponmodel, "") and
1101 // expects it to find all the monsters, so we must be careful to support
1104 ent = PRVM_NEXT_EDICT(prog->edicts);
1105 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
1107 prog->xfunction->builtinsprofile++;
1108 if (ent->priv.required->free)
1110 t = PRVM_E_STRING(ent,f);
1116 PRVM_EDICTFIELDEDICT(ent,chainfield) = PRVM_NUM_FOR_EDICT(chain);
1120 VM_RETURN_EDICT(chain);
1127 entity findchainfloat(.string field, float match)
1128 entity findchainentity(.string field, entity match)
1131 // LordHavoc: chained search for float, int, and entity reference fields
1132 // entity(.string field, float match) findchainfloat = #403;
1133 void VM_findchainfloat(prvm_prog_t *prog)
1138 prvm_edict_t *ent, *chain;
1141 VM_SAFEPARMCOUNTRANGE(2, 3, VM_findchainfloat);
1144 chainfield = PRVM_G_INT(OFS_PARM2);
1146 chainfield = prog->fieldoffsets.chain;
1148 prog->error_cmd("VM_findchain: %s doesnt have the specified chain field !", prog->name);
1150 chain = (prvm_edict_t *)prog->edicts;
1152 f = PRVM_G_INT(OFS_PARM0);
1153 s = PRVM_G_FLOAT(OFS_PARM1);
1155 ent = PRVM_NEXT_EDICT(prog->edicts);
1156 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
1158 prog->xfunction->builtinsprofile++;
1159 if (ent->priv.required->free)
1161 if (PRVM_E_FLOAT(ent,f) != s)
1164 PRVM_EDICTFIELDEDICT(ent,chainfield) = PRVM_EDICT_TO_PROG(chain);
1168 VM_RETURN_EDICT(chain);
1172 ========================
1175 entity findflags(entity start, .float field, float match)
1176 ========================
1178 // LordHavoc: search for flags in float fields
1179 void VM_findflags(prvm_prog_t *prog)
1186 VM_SAFEPARMCOUNT(3, VM_findflags);
1189 e = PRVM_G_EDICTNUM(OFS_PARM0);
1190 f = PRVM_G_INT(OFS_PARM1);
1191 s = (int)PRVM_G_FLOAT(OFS_PARM2);
1193 for (e++ ; e < prog->num_edicts ; e++)
1195 prog->xfunction->builtinsprofile++;
1196 ed = PRVM_EDICT_NUM(e);
1197 if (ed->priv.required->free)
1199 if (!PRVM_E_FLOAT(ed,f))
1201 if ((int)PRVM_E_FLOAT(ed,f) & s)
1203 VM_RETURN_EDICT(ed);
1208 VM_RETURN_EDICT(prog->edicts);
1212 ========================
1215 entity findchainflags(.float field, float match)
1216 ========================
1218 // LordHavoc: chained search for flags in float fields
1219 void VM_findchainflags(prvm_prog_t *prog)
1224 prvm_edict_t *ent, *chain;
1227 VM_SAFEPARMCOUNTRANGE(2, 3, VM_findchainflags);
1230 chainfield = PRVM_G_INT(OFS_PARM2);
1232 chainfield = prog->fieldoffsets.chain;
1234 prog->error_cmd("VM_findchain: %s doesnt have the specified chain field !", prog->name);
1236 chain = (prvm_edict_t *)prog->edicts;
1238 f = PRVM_G_INT(OFS_PARM0);
1239 s = (int)PRVM_G_FLOAT(OFS_PARM1);
1241 ent = PRVM_NEXT_EDICT(prog->edicts);
1242 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
1244 prog->xfunction->builtinsprofile++;
1245 if (ent->priv.required->free)
1247 if (!PRVM_E_FLOAT(ent,f))
1249 if (!((int)PRVM_E_FLOAT(ent,f) & s))
1252 PRVM_EDICTFIELDEDICT(ent,chainfield) = PRVM_EDICT_TO_PROG(chain);
1256 VM_RETURN_EDICT(chain);
1263 string precache_sound (string sample)
1266 void VM_precache_sound(prvm_prog_t *prog)
1270 VM_SAFEPARMCOUNT(1, VM_precache_sound);
1272 s = PRVM_G_STRING(OFS_PARM0);
1273 PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
1274 //VM_CheckEmptyString(prog, s);
1276 if(snd_initialized.integer && !S_PrecacheSound(s, true, true))
1278 VM_Warning(prog, "VM_precache_sound: Failed to load %s for %s\n", s, prog->name);
1287 returns the same string as output
1289 does nothing, only used by qcc to build .pak archives
1292 void VM_precache_file(prvm_prog_t *prog)
1294 VM_SAFEPARMCOUNT(1,VM_precache_file);
1295 // precache_file is only used to copy files with qcc, it does nothing
1296 PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
1306 void VM_coredump(prvm_prog_t *prog)
1308 VM_SAFEPARMCOUNT(0,VM_coredump);
1310 Cbuf_AddText("prvm_edicts ");
1311 Cbuf_AddText(prog->name);
1322 void VM_stackdump(prvm_prog_t *prog)
1324 VM_SAFEPARMCOUNT(0, VM_stackdump);
1326 PRVM_StackTrace(prog);
1337 void VM_crash(prvm_prog_t *prog)
1339 VM_SAFEPARMCOUNT(0, VM_crash);
1341 prog->error_cmd("Crash called by %s",prog->name);
1351 void VM_traceon(prvm_prog_t *prog)
1353 VM_SAFEPARMCOUNT(0,VM_traceon);
1365 void VM_traceoff(prvm_prog_t *prog)
1367 VM_SAFEPARMCOUNT(0,VM_traceoff);
1369 prog->trace = false;
1379 void VM_eprint(prvm_prog_t *prog)
1381 VM_SAFEPARMCOUNT(1,VM_eprint);
1383 PRVM_ED_PrintNum (prog, PRVM_G_EDICTNUM(OFS_PARM0), NULL);
1393 void VM_rint(prvm_prog_t *prog)
1396 VM_SAFEPARMCOUNT(1,VM_rint);
1398 f = PRVM_G_FLOAT(OFS_PARM0);
1400 PRVM_G_FLOAT(OFS_RETURN) = floor(f + 0.5);
1402 PRVM_G_FLOAT(OFS_RETURN) = ceil(f - 0.5);
1412 void VM_floor(prvm_prog_t *prog)
1414 VM_SAFEPARMCOUNT(1,VM_floor);
1416 PRVM_G_FLOAT(OFS_RETURN) = floor(PRVM_G_FLOAT(OFS_PARM0));
1426 void VM_ceil(prvm_prog_t *prog)
1428 VM_SAFEPARMCOUNT(1,VM_ceil);
1430 PRVM_G_FLOAT(OFS_RETURN) = ceil(PRVM_G_FLOAT(OFS_PARM0));
1438 entity nextent(entity)
1441 void VM_nextent(prvm_prog_t *prog)
1446 VM_SAFEPARMCOUNT(1, VM_nextent);
1448 i = PRVM_G_EDICTNUM(OFS_PARM0);
1451 prog->xfunction->builtinsprofile++;
1453 if (i == prog->num_edicts)
1455 VM_RETURN_EDICT(prog->edicts);
1458 ent = PRVM_EDICT_NUM(i);
1459 if (!ent->priv.required->free)
1461 VM_RETURN_EDICT(ent);
1467 //=============================================================================
1474 changelevel(string map)
1477 void VM_changelevel(prvm_prog_t *prog)
1480 VM_SAFEPARMCOUNT(1, VM_changelevel);
1484 VM_Warning(prog, "VM_changelevel: game is not server (%s)\n", prog->name);
1488 // make sure we don't issue two changelevels
1489 if (svs.changelevel_issued)
1491 svs.changelevel_issued = true;
1493 Cbuf_AddText(va(vabuf, sizeof(vabuf), "changelevel %s\n",PRVM_G_STRING(OFS_PARM0)));
1503 void VM_sin(prvm_prog_t *prog)
1505 VM_SAFEPARMCOUNT(1,VM_sin);
1506 PRVM_G_FLOAT(OFS_RETURN) = sin(PRVM_G_FLOAT(OFS_PARM0));
1515 void VM_cos(prvm_prog_t *prog)
1517 VM_SAFEPARMCOUNT(1,VM_cos);
1518 PRVM_G_FLOAT(OFS_RETURN) = cos(PRVM_G_FLOAT(OFS_PARM0));
1528 void VM_sqrt(prvm_prog_t *prog)
1530 VM_SAFEPARMCOUNT(1,VM_sqrt);
1531 PRVM_G_FLOAT(OFS_RETURN) = sqrt(PRVM_G_FLOAT(OFS_PARM0));
1541 void VM_asin(prvm_prog_t *prog)
1543 VM_SAFEPARMCOUNT(1,VM_asin);
1544 PRVM_G_FLOAT(OFS_RETURN) = asin(PRVM_G_FLOAT(OFS_PARM0));
1553 void VM_acos(prvm_prog_t *prog)
1555 VM_SAFEPARMCOUNT(1,VM_acos);
1556 PRVM_G_FLOAT(OFS_RETURN) = acos(PRVM_G_FLOAT(OFS_PARM0));
1565 void VM_atan(prvm_prog_t *prog)
1567 VM_SAFEPARMCOUNT(1,VM_atan);
1568 PRVM_G_FLOAT(OFS_RETURN) = atan(PRVM_G_FLOAT(OFS_PARM0));
1574 float atan2(float,float)
1577 void VM_atan2(prvm_prog_t *prog)
1579 VM_SAFEPARMCOUNT(2,VM_atan2);
1580 PRVM_G_FLOAT(OFS_RETURN) = atan2(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1589 void VM_tan(prvm_prog_t *prog)
1591 VM_SAFEPARMCOUNT(1,VM_tan);
1592 PRVM_G_FLOAT(OFS_RETURN) = tan(PRVM_G_FLOAT(OFS_PARM0));
1599 Returns a vector of length < 1 and > 0
1604 void VM_randomvec(prvm_prog_t *prog)
1607 VM_SAFEPARMCOUNT(0, VM_randomvec);
1609 VectorCopy(temp, PRVM_G_VECTOR(OFS_RETURN));
1612 //=============================================================================
1618 float registercvar (string name, string value[, float flags])
1621 void VM_registercvar(prvm_prog_t *prog)
1623 const char *name, *value;
1626 VM_SAFEPARMCOUNTRANGE(2, 3, VM_registercvar);
1628 name = PRVM_G_STRING(OFS_PARM0);
1629 value = PRVM_G_STRING(OFS_PARM1);
1630 flags = prog->argc >= 3 ? (int)PRVM_G_FLOAT(OFS_PARM2) : 0;
1631 PRVM_G_FLOAT(OFS_RETURN) = 0;
1633 if(flags > CVAR_MAXFLAGSVAL)
1636 // first check to see if it has already been defined
1637 if (Cvar_FindVar (name))
1640 // check for overlap with a command
1641 if (Cmd_Exists (name))
1643 VM_Warning(prog, "VM_registercvar: %s is a command\n", name);
1647 Cvar_Get(name, value, flags, NULL);
1649 PRVM_G_FLOAT(OFS_RETURN) = 1; // success
1657 returns the minimum of two supplied floats
1659 float min(float a, float b, ...[float])
1662 void VM_min(prvm_prog_t *prog)
1664 VM_SAFEPARMCOUNTRANGE(2, 8, VM_min);
1665 // LordHavoc: 3+ argument enhancement suggested by FrikaC
1666 if (prog->argc >= 3)
1669 float f = PRVM_G_FLOAT(OFS_PARM0);
1670 for (i = 1;i < prog->argc;i++)
1671 if (f > PRVM_G_FLOAT((OFS_PARM0+i*3)))
1672 f = PRVM_G_FLOAT((OFS_PARM0+i*3));
1673 PRVM_G_FLOAT(OFS_RETURN) = f;
1676 PRVM_G_FLOAT(OFS_RETURN) = min(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1683 returns the maximum of two supplied floats
1685 float max(float a, float b, ...[float])
1688 void VM_max(prvm_prog_t *prog)
1690 VM_SAFEPARMCOUNTRANGE(2, 8, VM_max);
1691 // LordHavoc: 3+ argument enhancement suggested by FrikaC
1692 if (prog->argc >= 3)
1695 float f = PRVM_G_FLOAT(OFS_PARM0);
1696 for (i = 1;i < prog->argc;i++)
1697 if (f < PRVM_G_FLOAT((OFS_PARM0+i*3)))
1698 f = PRVM_G_FLOAT((OFS_PARM0+i*3));
1699 PRVM_G_FLOAT(OFS_RETURN) = f;
1702 PRVM_G_FLOAT(OFS_RETURN) = max(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1709 returns number bounded by supplied range
1711 float bound(float min, float value, float max)
1714 void VM_bound(prvm_prog_t *prog)
1716 VM_SAFEPARMCOUNT(3,VM_bound);
1717 PRVM_G_FLOAT(OFS_RETURN) = bound(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1), PRVM_G_FLOAT(OFS_PARM2));
1724 returns a raised to power b
1726 float pow(float a, float b)
1729 void VM_pow(prvm_prog_t *prog)
1731 VM_SAFEPARMCOUNT(2,VM_pow);
1732 PRVM_G_FLOAT(OFS_RETURN) = pow(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1735 void VM_log(prvm_prog_t *prog)
1737 VM_SAFEPARMCOUNT(1,VM_log);
1738 PRVM_G_FLOAT(OFS_RETURN) = log(PRVM_G_FLOAT(OFS_PARM0));
1741 void VM_Files_Init(prvm_prog_t *prog)
1744 for (i = 0;i < PRVM_MAX_OPENFILES;i++)
1745 prog->openfiles[i] = NULL;
1748 void VM_Files_CloseAll(prvm_prog_t *prog)
1751 for (i = 0;i < PRVM_MAX_OPENFILES;i++)
1753 if (prog->openfiles[i])
1754 FS_Close(prog->openfiles[i]);
1755 prog->openfiles[i] = NULL;
1759 static qfile_t *VM_GetFileHandle(prvm_prog_t *prog, int index)
1761 if (index < 0 || index >= PRVM_MAX_OPENFILES)
1763 Con_Printf("VM_GetFileHandle: invalid file handle %i used in %s\n", index, prog->name);
1766 if (prog->openfiles[index] == NULL)
1768 Con_Printf("VM_GetFileHandle: no such file handle %i (or file has been closed) in %s\n", index, prog->name);
1771 return prog->openfiles[index];
1778 float fopen(string filename, float mode)
1781 // float(string filename, float mode) fopen = #110;
1782 // opens a file inside quake/gamedir/data/ (mode is FILE_READ, FILE_APPEND, or FILE_WRITE),
1783 // returns fhandle >= 0 if successful, or fhandle < 0 if unable to open file for any reason
1784 void VM_fopen(prvm_prog_t *prog)
1787 const char *modestring, *filename;
1790 VM_SAFEPARMCOUNT(2,VM_fopen);
1792 for (filenum = 0;filenum < PRVM_MAX_OPENFILES;filenum++)
1793 if (prog->openfiles[filenum] == NULL)
1795 if (filenum >= PRVM_MAX_OPENFILES)
1797 PRVM_G_FLOAT(OFS_RETURN) = -2;
1798 VM_Warning(prog, "VM_fopen: %s ran out of file handles (%i)\n", prog->name, PRVM_MAX_OPENFILES);
1801 filename = PRVM_G_STRING(OFS_PARM0);
1802 mode = (int)PRVM_G_FLOAT(OFS_PARM1);
1805 case 0: // FILE_READ
1807 prog->openfiles[filenum] = FS_OpenVirtualFile(va(vabuf, sizeof(vabuf), "data/%s", filename), false);
1808 if (prog->openfiles[filenum] == NULL)
1809 prog->openfiles[filenum] = FS_OpenVirtualFile(va(vabuf, sizeof(vabuf), "%s", filename), false);
1811 case 1: // FILE_APPEND
1813 prog->openfiles[filenum] = FS_OpenRealFile(va(vabuf, sizeof(vabuf), "data/%s", filename), modestring, false);
1815 case 2: // FILE_WRITE
1817 prog->openfiles[filenum] = FS_OpenRealFile(va(vabuf, sizeof(vabuf), "data/%s", filename), modestring, false);
1820 PRVM_G_FLOAT(OFS_RETURN) = -3;
1821 VM_Warning(prog, "VM_fopen: %s: no such mode %i (valid: 0 = read, 1 = append, 2 = write)\n", prog->name, mode);
1825 if (prog->openfiles[filenum] == NULL)
1827 PRVM_G_FLOAT(OFS_RETURN) = -1;
1828 if (developer_extra.integer)
1829 VM_Warning(prog, "VM_fopen: %s: %s mode %s failed\n", prog->name, filename, modestring);
1833 PRVM_G_FLOAT(OFS_RETURN) = filenum;
1834 if (developer_extra.integer)
1835 Con_DPrintf("VM_fopen: %s: %s mode %s opened as #%i\n", prog->name, filename, modestring, filenum);
1836 prog->openfiles_origin[filenum] = PRVM_AllocationOrigin(prog);
1844 fclose(float fhandle)
1847 //void(float fhandle) fclose = #111; // closes a file
1848 void VM_fclose(prvm_prog_t *prog)
1852 VM_SAFEPARMCOUNT(1,VM_fclose);
1854 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1855 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
1857 VM_Warning(prog, "VM_fclose: invalid file handle %i used in %s\n", filenum, prog->name);
1860 if (prog->openfiles[filenum] == NULL)
1862 VM_Warning(prog, "VM_fclose: no such file handle %i (or file has been closed) in %s\n", filenum, prog->name);
1865 FS_Close(prog->openfiles[filenum]);
1866 prog->openfiles[filenum] = NULL;
1867 if(prog->openfiles_origin[filenum])
1868 PRVM_Free((char *)prog->openfiles_origin[filenum]);
1869 if (developer_extra.integer)
1870 Con_DPrintf("VM_fclose: %s: #%i closed\n", prog->name, filenum);
1877 string fgets(float fhandle)
1880 //string(float fhandle) fgets = #112; // reads a line of text from the file and returns as a tempstring
1881 void VM_fgets(prvm_prog_t *prog)
1884 char string[VM_STRINGTEMP_LENGTH];
1887 VM_SAFEPARMCOUNT(1,VM_fgets);
1889 // set the return value regardless of any possible errors
1890 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
1892 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1893 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
1895 VM_Warning(prog, "VM_fgets: invalid file handle %i used in %s\n", filenum, prog->name);
1898 if (prog->openfiles[filenum] == NULL)
1900 VM_Warning(prog, "VM_fgets: no such file handle %i (or file has been closed) in %s\n", filenum, prog->name);
1906 c = FS_Getc(prog->openfiles[filenum]);
1907 if (c == '\r' || c == '\n' || c < 0)
1909 if (end < VM_STRINGTEMP_LENGTH - 1)
1913 // remove \n following \r
1916 c = FS_Getc(prog->openfiles[filenum]);
1918 FS_UnGetc(prog->openfiles[filenum], (unsigned char)c);
1920 if (developer_extra.integer)
1921 Con_DPrintf("fgets: %s: %s\n", prog->name, string);
1923 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, string);
1930 fputs(float fhandle, string s)
1933 //void(float fhandle, string s) fputs = #113; // writes a line of text to the end of the file
1934 void VM_fputs(prvm_prog_t *prog)
1937 char string[VM_STRINGTEMP_LENGTH];
1940 VM_SAFEPARMCOUNT(2,VM_fputs);
1942 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1943 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
1945 VM_Warning(prog, "VM_fputs: invalid file handle %i used in %s\n", filenum, prog->name);
1948 if (prog->openfiles[filenum] == NULL)
1950 VM_Warning(prog, "VM_fputs: no such file handle %i (or file has been closed) in %s\n", filenum, prog->name);
1953 VM_VarString(prog, 1, string, sizeof(string));
1954 if ((stringlength = (int)strlen(string)))
1955 FS_Write(prog->openfiles[filenum], string, stringlength);
1956 if (developer_extra.integer)
1957 Con_DPrintf("fputs: %s: %s\n", prog->name, string);
1964 writetofile(float fhandle, entity ent)
1967 void VM_writetofile(prvm_prog_t *prog)
1972 VM_SAFEPARMCOUNT(2, VM_writetofile);
1974 file = VM_GetFileHandle(prog, (int)PRVM_G_FLOAT(OFS_PARM0));
1977 VM_Warning(prog, "VM_writetofile: invalid or closed file handle\n");
1981 ent = PRVM_G_EDICT(OFS_PARM1);
1982 if(ent->priv.required->free)
1984 VM_Warning(prog, "VM_writetofile: %s: entity %i is free !\n", prog->name, PRVM_NUM_FOR_EDICT(ent));
1988 PRVM_ED_Write (prog, file, ent);
1991 // KrimZon - DP_QC_ENTITYDATA
1996 float() numentityfields
1997 Return the number of entity fields - NOT offsets
2000 void VM_numentityfields(prvm_prog_t *prog)
2002 PRVM_G_FLOAT(OFS_RETURN) = prog->numfielddefs;
2005 // KrimZon - DP_QC_ENTITYDATA
2010 string(float fieldnum) entityfieldname
2011 Return name of the specified field as a string, or empty if the field is invalid (warning)
2014 void VM_entityfieldname(prvm_prog_t *prog)
2017 int i = (int)PRVM_G_FLOAT(OFS_PARM0);
2019 if (i < 0 || i >= prog->numfielddefs)
2021 VM_Warning(prog, "VM_entityfieldname: %s: field index out of bounds\n", prog->name);
2022 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, "");
2026 d = &prog->fielddefs[i];
2027 PRVM_G_INT(OFS_RETURN) = d->s_name; // presuming that s_name points to a string already
2030 // KrimZon - DP_QC_ENTITYDATA
2035 float(float fieldnum) entityfieldtype
2038 void VM_entityfieldtype(prvm_prog_t *prog)
2041 int i = (int)PRVM_G_FLOAT(OFS_PARM0);
2043 if (i < 0 || i >= prog->numfielddefs)
2045 VM_Warning(prog, "VM_entityfieldtype: %s: field index out of bounds\n", prog->name);
2046 PRVM_G_FLOAT(OFS_RETURN) = -1.0;
2050 d = &prog->fielddefs[i];
2051 PRVM_G_FLOAT(OFS_RETURN) = (float)d->type;
2054 // KrimZon - DP_QC_ENTITYDATA
2057 VM_getentityfieldstring
2059 string(float fieldnum, entity ent) getentityfieldstring
2062 void VM_getentityfieldstring(prvm_prog_t *prog)
2064 // put the data into a string
2069 int i = (int)PRVM_G_FLOAT(OFS_PARM0);
2070 char valuebuf[MAX_INPUTLINE];
2072 if (i < 0 || i >= prog->numfielddefs)
2074 VM_Warning(prog, "VM_entityfielddata: %s: field index out of bounds\n", prog->name);
2075 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, "");
2079 d = &prog->fielddefs[i];
2082 ent = PRVM_G_EDICT(OFS_PARM1);
2083 if(ent->priv.required->free)
2085 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, "");
2086 VM_Warning(prog, "VM_entityfielddata: %s: entity %i is free !\n", prog->name, PRVM_NUM_FOR_EDICT(ent));
2089 v = (int *)((char *)ent->fields.vp + d->ofs*4);
2091 // if it's 0 or blank, return an empty string
2092 type = d->type & ~DEF_SAVEGLOBAL;
2093 for (j=0 ; j<prvm_type_size[type] ; j++)
2096 if (j == prvm_type_size[type])
2098 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, "");
2102 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, PRVM_UglyValueString(prog, (etype_t)d->type, (prvm_eval_t *)v, valuebuf, sizeof(valuebuf)));
2105 // KrimZon - DP_QC_ENTITYDATA
2108 VM_putentityfieldstring
2110 float(float fieldnum, entity ent, string s) putentityfieldstring
2113 void VM_putentityfieldstring(prvm_prog_t *prog)
2117 int i = (int)PRVM_G_FLOAT(OFS_PARM0);
2119 if (i < 0 || i >= prog->numfielddefs)
2121 VM_Warning(prog, "VM_entityfielddata: %s: field index out of bounds\n", prog->name);
2122 PRVM_G_FLOAT(OFS_RETURN) = 0.0f;
2126 d = &prog->fielddefs[i];
2129 ent = PRVM_G_EDICT(OFS_PARM1);
2130 if(ent->priv.required->free)
2132 VM_Warning(prog, "VM_entityfielddata: %s: entity %i is free !\n", prog->name, PRVM_NUM_FOR_EDICT(ent));
2133 PRVM_G_FLOAT(OFS_RETURN) = 0.0f;
2137 // parse the string into the value
2138 PRVM_G_FLOAT(OFS_RETURN) = ( PRVM_ED_ParseEpair(prog, ent, d, PRVM_G_STRING(OFS_PARM2), false) ) ? 1.0f : 0.0f;
2145 float strlen(string s)
2148 //float(string s) strlen = #114; // returns how many characters are in a string
2149 void VM_strlen(prvm_prog_t *prog)
2151 VM_SAFEPARMCOUNT(1,VM_strlen);
2153 //PRVM_G_FLOAT(OFS_RETURN) = strlen(PRVM_G_STRING(OFS_PARM0));
2154 PRVM_G_FLOAT(OFS_RETURN) = u8_strlen(PRVM_G_STRING(OFS_PARM0));
2157 // DRESK - Decolorized String
2162 string strdecolorize(string s)
2165 // string (string s) strdecolorize = #472; // returns the passed in string with color codes stripped
2166 void VM_strdecolorize(prvm_prog_t *prog)
2168 char szNewString[VM_STRINGTEMP_LENGTH];
2169 const char *szString;
2172 VM_SAFEPARMCOUNT(1,VM_strdecolorize);
2173 szString = PRVM_G_STRING(OFS_PARM0);
2174 COM_StringDecolorize(szString, 0, szNewString, sizeof(szNewString), TRUE);
2175 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, szNewString);
2178 // DRESK - String Length (not counting color codes)
2183 float strlennocol(string s)
2186 // float(string s) strlennocol = #471; // returns how many characters are in a string not including color codes
2187 // For example, ^2Dresk returns a length of 5
2188 void VM_strlennocol(prvm_prog_t *prog)
2190 const char *szString;
2193 VM_SAFEPARMCOUNT(1,VM_strlennocol);
2195 szString = PRVM_G_STRING(OFS_PARM0);
2197 //nCnt = COM_StringLengthNoColors(szString, 0, NULL);
2198 nCnt = u8_COM_StringLengthNoColors(szString, 0, NULL);
2200 PRVM_G_FLOAT(OFS_RETURN) = nCnt;
2203 // DRESK - String to Uppercase and Lowercase
2208 string strtolower(string s)
2211 // string (string s) strtolower = #480; // returns passed in string in lowercase form
2212 void VM_strtolower(prvm_prog_t *prog)
2214 char szNewString[VM_STRINGTEMP_LENGTH];
2215 const char *szString;
2218 VM_SAFEPARMCOUNT(1,VM_strtolower);
2219 szString = PRVM_G_STRING(OFS_PARM0);
2221 COM_ToLowerString(szString, szNewString, sizeof(szNewString) );
2223 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, szNewString);
2230 string strtoupper(string s)
2233 // string (string s) strtoupper = #481; // returns passed in string in uppercase form
2234 void VM_strtoupper(prvm_prog_t *prog)
2236 char szNewString[VM_STRINGTEMP_LENGTH];
2237 const char *szString;
2240 VM_SAFEPARMCOUNT(1,VM_strtoupper);
2241 szString = PRVM_G_STRING(OFS_PARM0);
2243 COM_ToUpperString(szString, szNewString, sizeof(szNewString) );
2245 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, szNewString);
2252 string strcat(string,string,...[string])
2255 //string(string s1, string s2) strcat = #115;
2256 // concatenates two strings (for example "abc", "def" would return "abcdef")
2257 // and returns as a tempstring
2258 void VM_strcat(prvm_prog_t *prog)
2260 char s[VM_STRINGTEMP_LENGTH];
2261 VM_SAFEPARMCOUNTRANGE(1, 8, VM_strcat);
2263 VM_VarString(prog, 0, s, sizeof(s));
2264 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, s);
2271 string substring(string s, float start, float length)
2274 // string(string s, float start, float length) substring = #116;
2275 // returns a section of a string as a tempstring
2276 void VM_substring(prvm_prog_t *prog)
2279 int u_slength = 0, u_start;
2282 char string[VM_STRINGTEMP_LENGTH];
2284 VM_SAFEPARMCOUNT(3,VM_substring);
2287 s = PRVM_G_STRING(OFS_PARM0);
2288 start = (int)PRVM_G_FLOAT(OFS_PARM1);
2289 length = (int)PRVM_G_FLOAT(OFS_PARM2);
2290 slength = strlen(s);
2292 if (start < 0) // FTE_STRINGS feature
2294 start = bound(0, start, slength);
2296 if (length < 0) // FTE_STRINGS feature
2297 length += slength - start + 1;
2298 maxlen = min((int)sizeof(string) - 1, slength - start);
2299 length = bound(0, length, maxlen);
2301 memcpy(string, s + start, length);
2303 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, string);
2306 s = PRVM_G_STRING(OFS_PARM0);
2307 start = (int)PRVM_G_FLOAT(OFS_PARM1);
2308 length = (int)PRVM_G_FLOAT(OFS_PARM2);
2310 if (start < 0) // FTE_STRINGS feature
2312 u_slength = u8_strlen(s);
2314 start = bound(0, start, u_slength);
2317 if (length < 0) // FTE_STRINGS feature
2319 if (!u_slength) // it's not calculated when it's not needed above
2320 u_slength = u8_strlen(s);
2321 length += u_slength - start + 1;
2324 // positive start, positive length
2325 u_start = u8_byteofs(s, start, NULL);
2328 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, "");
2331 u_length = u8_bytelen(s + u_start, length);
2332 if (u_length >= sizeof(string)-1)
2333 u_length = sizeof(string)-1;
2335 memcpy(string, s + u_start, u_length);
2336 string[u_length] = 0;
2337 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, string);
2344 string(string search, string replace, string subject) strreplace = #484;
2347 // replaces all occurrences of search with replace in the string subject, and returns the result
2348 void VM_strreplace(prvm_prog_t *prog)
2351 const char *search, *replace, *subject;
2352 char string[VM_STRINGTEMP_LENGTH];
2353 int search_len, replace_len, subject_len;
2355 VM_SAFEPARMCOUNT(3,VM_strreplace);
2357 search = PRVM_G_STRING(OFS_PARM0);
2358 replace = PRVM_G_STRING(OFS_PARM1);
2359 subject = PRVM_G_STRING(OFS_PARM2);
2361 search_len = (int)strlen(search);
2362 replace_len = (int)strlen(replace);
2363 subject_len = (int)strlen(subject);
2366 for (i = 0; i <= subject_len - search_len; i++)
2368 for (j = 0; j < search_len; j++) // thus, i+j < subject_len
2369 if (subject[i+j] != search[j])
2371 if (j == search_len)
2373 // NOTE: if search_len == 0, we always hit THIS case, and never the other
2374 // found it at offset 'i'
2375 for (j = 0; j < replace_len && si < (int)sizeof(string) - 1; j++)
2376 string[si++] = replace[j];
2379 i += search_len - 1;
2383 // the above would subtract 1 from i... so we
2384 // don't do that, but instead output the next
2386 if (si < (int)sizeof(string) - 1)
2387 string[si++] = subject[i];
2392 // in THIS case, we know search_len > 0, thus i < subject_len
2394 if (si < (int)sizeof(string) - 1)
2395 string[si++] = subject[i];
2398 // remaining chars (these cannot match)
2399 for (; i < subject_len; i++)
2400 if (si < (int)sizeof(string) - 1)
2401 string[si++] = subject[i];
2404 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, string);
2411 string(string search, string replace, string subject) strireplace = #485;
2414 // case-insensitive version of strreplace
2415 void VM_strireplace(prvm_prog_t *prog)
2418 const char *search, *replace, *subject;
2419 char string[VM_STRINGTEMP_LENGTH];
2420 int search_len, replace_len, subject_len;
2422 VM_SAFEPARMCOUNT(3,VM_strreplace);
2424 search = PRVM_G_STRING(OFS_PARM0);
2425 replace = PRVM_G_STRING(OFS_PARM1);
2426 subject = PRVM_G_STRING(OFS_PARM2);
2428 search_len = (int)strlen(search);
2429 replace_len = (int)strlen(replace);
2430 subject_len = (int)strlen(subject);
2433 for (i = 0; i <= subject_len - search_len; i++)
2435 for (j = 0; j < search_len; j++) // thus, i+j < subject_len
2436 if (tolower(subject[i+j]) != tolower(search[j]))
2438 if (j == search_len)
2440 // NOTE: if search_len == 0, we always hit THIS case, and never the other
2441 // found it at offset 'i'
2442 for (j = 0; j < replace_len && si < (int)sizeof(string) - 1; j++)
2443 string[si++] = replace[j];
2446 i += search_len - 1;
2450 // the above would subtract 1 from i... so we
2451 // don't do that, but instead output the next
2453 if (si < (int)sizeof(string) - 1)
2454 string[si++] = subject[i];
2459 // in THIS case, we know search_len > 0, thus i < subject_len
2461 if (si < (int)sizeof(string) - 1)
2462 string[si++] = subject[i];
2465 // remaining chars (these cannot match)
2466 for (; i < subject_len; i++)
2467 if (si < (int)sizeof(string) - 1)
2468 string[si++] = subject[i];
2471 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, string);
2478 vector stov(string s)
2481 //vector(string s) stov = #117; // returns vector value from a string
2482 void VM_stov(prvm_prog_t *prog)
2484 char string[VM_STRINGTEMP_LENGTH];
2486 VM_SAFEPARMCOUNT(1,VM_stov);
2488 VM_VarString(prog, 0, string, sizeof(string));
2489 Math_atov(string, PRVM_G_VECTOR(OFS_RETURN));
2496 string strzone(string s)
2499 //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)
2500 void VM_strzone(prvm_prog_t *prog)
2503 char string[VM_STRINGTEMP_LENGTH];
2506 VM_SAFEPARMCOUNT(1,VM_strzone);
2508 VM_VarString(prog, 0, string, sizeof(string));
2509 alloclen = strlen(string) + 1;
2510 PRVM_G_INT(OFS_RETURN) = PRVM_AllocString(prog, alloclen, &out);
2511 memcpy(out, string, alloclen);
2521 //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!!!)
2522 void VM_strunzone(prvm_prog_t *prog)
2524 VM_SAFEPARMCOUNT(1,VM_strunzone);
2525 PRVM_FreeString(prog, PRVM_G_INT(OFS_PARM0));
2530 VM_command (used by client and menu)
2532 clientcommand(float client, string s) (for client and menu)
2535 //void(entity e, string s) clientcommand = #440; // executes a command string as if it came from the specified client
2536 //this function originally written by KrimZon, made shorter by LordHavoc
2537 void VM_clcommand (prvm_prog_t *prog)
2539 client_t *temp_client;
2542 VM_SAFEPARMCOUNT(2,VM_clcommand);
2544 i = (int)PRVM_G_FLOAT(OFS_PARM0);
2545 if (!sv.active || i < 0 || i >= svs.maxclients || !svs.clients[i].active)
2547 VM_Warning(prog, "VM_clientcommand: %s: invalid client/server is not active !\n", prog->name);
2551 temp_client = host_client;
2552 host_client = svs.clients + i;
2553 Cmd_ExecuteString (PRVM_G_STRING(OFS_PARM1), src_client, true);
2554 host_client = temp_client;
2562 float tokenize(string s)
2565 //float(string s) tokenize = #441; // takes apart a string into individal words (access them with argv), returns how many
2566 //this function originally written by KrimZon, made shorter by LordHavoc
2567 //20040203: rewritten by LordHavoc (no longer uses allocations)
2568 static int num_tokens = 0;
2569 static int tokens[VM_STRINGTEMP_LENGTH / 2];
2570 static int tokens_startpos[VM_STRINGTEMP_LENGTH / 2];
2571 static int tokens_endpos[VM_STRINGTEMP_LENGTH / 2];
2572 static char tokenize_string[VM_STRINGTEMP_LENGTH];
2573 void VM_tokenize (prvm_prog_t *prog)
2577 VM_SAFEPARMCOUNT(1,VM_tokenize);
2579 strlcpy(tokenize_string, PRVM_G_STRING(OFS_PARM0), sizeof(tokenize_string));
2580 p = tokenize_string;
2585 if (num_tokens >= (int)(sizeof(tokens)/sizeof(tokens[0])))
2588 // skip whitespace here to find token start pos
2589 while(*p && ISWHITESPACE(*p))
2592 tokens_startpos[num_tokens] = p - tokenize_string;
2593 if(!COM_ParseToken_VM_Tokenize(&p, false))
2595 tokens_endpos[num_tokens] = p - tokenize_string;
2596 tokens[num_tokens] = PRVM_SetTempString(prog, com_token);
2600 PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
2603 //float(string s) tokenize = #514; // takes apart a string into individal words (access them with argv), returns how many
2604 void VM_tokenize_console (prvm_prog_t *prog)
2608 VM_SAFEPARMCOUNT(1,VM_tokenize);
2610 strlcpy(tokenize_string, PRVM_G_STRING(OFS_PARM0), sizeof(tokenize_string));
2611 p = tokenize_string;
2616 if (num_tokens >= (int)(sizeof(tokens)/sizeof(tokens[0])))
2619 // skip whitespace here to find token start pos
2620 while(*p && ISWHITESPACE(*p))
2623 tokens_startpos[num_tokens] = p - tokenize_string;
2624 if(!COM_ParseToken_Console(&p))
2626 tokens_endpos[num_tokens] = p - tokenize_string;
2627 tokens[num_tokens] = PRVM_SetTempString(prog, com_token);
2631 PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
2636 VM_tokenizebyseparator
2638 float tokenizebyseparator(string s, string separator1, ...)
2641 //float(string s, string separator1, ...) tokenizebyseparator = #479; // takes apart a string into individal words (access them with argv), returns how many
2642 //this function returns the token preceding each instance of a separator (of
2643 //which there can be multiple), and the text following the last separator
2644 //useful for parsing certain kinds of data like IP addresses
2646 //numnumbers = tokenizebyseparator("10.1.2.3", ".");
2647 //returns 4 and the tokens "10" "1" "2" "3".
2648 void VM_tokenizebyseparator (prvm_prog_t *prog)
2652 int separatorlen[7];
2653 const char *separators[7];
2656 char tokentext[MAX_INPUTLINE];
2658 VM_SAFEPARMCOUNTRANGE(2, 8,VM_tokenizebyseparator);
2660 strlcpy(tokenize_string, PRVM_G_STRING(OFS_PARM0), sizeof(tokenize_string));
2661 p = tokenize_string;
2664 for (j = 1;j < prog->argc;j++)
2666 // skip any blank separator strings
2667 const char *s = PRVM_G_STRING(OFS_PARM0+j*3);
2670 separators[numseparators] = s;
2671 separatorlen[numseparators] = strlen(s);
2678 while (num_tokens < (int)(sizeof(tokens)/sizeof(tokens[0])))
2680 token = tokentext + j;
2681 tokens_startpos[num_tokens] = p - tokenize_string;
2685 for (k = 0;k < numseparators;k++)
2687 if (!strncmp(p, separators[k], separatorlen[k]))
2689 p += separatorlen[k];
2693 if (k < numseparators)
2695 if (j < (int)sizeof(tokentext)-1)
2696 tokentext[j++] = *p;
2700 tokens_endpos[num_tokens] = p0 - tokenize_string;
2701 if (j >= (int)sizeof(tokentext))
2704 tokens[num_tokens++] = PRVM_SetTempString(prog, token);
2709 PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
2712 //string(float n) argv = #442; // returns a word from the tokenized string (returns nothing for an invalid index)
2713 //this function originally written by KrimZon, made shorter by LordHavoc
2714 void VM_argv (prvm_prog_t *prog)
2718 VM_SAFEPARMCOUNT(1,VM_argv);
2720 token_num = (int)PRVM_G_FLOAT(OFS_PARM0);
2723 token_num += num_tokens;
2725 if (token_num >= 0 && token_num < num_tokens)
2726 PRVM_G_INT(OFS_RETURN) = tokens[token_num];
2728 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
2731 //float(float n) argv_start_index = #515; // returns the start index of a token
2732 void VM_argv_start_index (prvm_prog_t *prog)
2736 VM_SAFEPARMCOUNT(1,VM_argv);
2738 token_num = (int)PRVM_G_FLOAT(OFS_PARM0);
2741 token_num += num_tokens;
2743 if (token_num >= 0 && token_num < num_tokens)
2744 PRVM_G_FLOAT(OFS_RETURN) = tokens_startpos[token_num];
2746 PRVM_G_FLOAT(OFS_RETURN) = -1;
2749 //float(float n) argv_end_index = #516; // returns the end index of a token
2750 void VM_argv_end_index (prvm_prog_t *prog)
2754 VM_SAFEPARMCOUNT(1,VM_argv);
2756 token_num = (int)PRVM_G_FLOAT(OFS_PARM0);
2759 token_num += num_tokens;
2761 if (token_num >= 0 && token_num < num_tokens)
2762 PRVM_G_FLOAT(OFS_RETURN) = tokens_endpos[token_num];
2764 PRVM_G_FLOAT(OFS_RETURN) = -1;
2774 void VM_isserver(prvm_prog_t *prog)
2776 VM_SAFEPARMCOUNT(0,VM_serverstate);
2778 PRVM_G_FLOAT(OFS_RETURN) = sv.active && (svs.maxclients > 1 || cls.state == ca_dedicated);
2788 void VM_clientcount(prvm_prog_t *prog)
2790 VM_SAFEPARMCOUNT(0,VM_clientcount);
2792 PRVM_G_FLOAT(OFS_RETURN) = svs.maxclients;
2802 void VM_clientstate(prvm_prog_t *prog)
2804 VM_SAFEPARMCOUNT(0,VM_clientstate);
2807 switch( cls.state ) {
2808 case ca_uninitialized:
2810 PRVM_G_FLOAT(OFS_RETURN) = 0;
2812 case ca_disconnected:
2813 PRVM_G_FLOAT(OFS_RETURN) = 1;
2816 PRVM_G_FLOAT(OFS_RETURN) = 2;
2819 // should never be reached!
2828 float getostype(prvm_prog_t *prog)
2830 */ // not used at the moment -> not included in the common list
2831 void VM_getostype(prvm_prog_t *prog)
2833 VM_SAFEPARMCOUNT(0,VM_getostype);
2838 OS_MAC - not supported
2842 PRVM_G_FLOAT(OFS_RETURN) = 0;
2843 #elif defined(MACOSX)
2844 PRVM_G_FLOAT(OFS_RETURN) = 2;
2846 PRVM_G_FLOAT(OFS_RETURN) = 1;
2854 float gettime(prvm_prog_t *prog)
2857 float CDAudio_GetPosition(void);
2858 void VM_gettime(prvm_prog_t *prog)
2862 VM_SAFEPARMCOUNTRANGE(0,1,VM_gettime);
2866 PRVM_G_FLOAT(OFS_RETURN) = (float) realtime;
2870 timer_index = (int) PRVM_G_FLOAT(OFS_PARM0);
2873 case 0: // GETTIME_FRAMESTART
2874 PRVM_G_FLOAT(OFS_RETURN) = realtime;
2876 case 1: // GETTIME_REALTIME
2877 PRVM_G_FLOAT(OFS_RETURN) = Sys_DirtyTime();
2879 case 2: // GETTIME_HIRES
2880 PRVM_G_FLOAT(OFS_RETURN) = (Sys_DirtyTime() - host_dirtytime);
2882 case 3: // GETTIME_UPTIME
2883 PRVM_G_FLOAT(OFS_RETURN) = realtime;
2885 case 4: // GETTIME_CDTRACK
2886 PRVM_G_FLOAT(OFS_RETURN) = CDAudio_GetPosition();
2889 VM_Warning(prog, "VM_gettime: %s: unsupported timer specified, returning realtime\n", prog->name);
2890 PRVM_G_FLOAT(OFS_RETURN) = realtime;
2900 float getsoundtime(prvm_prog_t *prog)
2904 void VM_getsoundtime (prvm_prog_t *prog)
2906 int entnum, entchannel;
2907 VM_SAFEPARMCOUNT(2,VM_getsoundtime);
2909 if (prog == SVVM_prog)
2910 entnum = PRVM_NUM_FOR_EDICT(PRVM_G_EDICT(OFS_PARM0));
2911 else if (prog == CLVM_prog)
2912 entnum = MAX_EDICTS + PRVM_NUM_FOR_EDICT(PRVM_G_EDICT(OFS_PARM0));
2915 VM_Warning(prog, "VM_getsoundtime: %s: not supported on this progs\n", prog->name);
2916 PRVM_G_FLOAT(OFS_RETURN) = -1;
2919 entchannel = (int)PRVM_G_FLOAT(OFS_PARM1);
2920 entchannel = CHAN_USER2ENGINE(entchannel);
2921 if (!IS_CHAN(entchannel))
2922 VM_Warning(prog, "VM_getsoundtime: %s: bad channel %i\n", prog->name, entchannel);
2923 PRVM_G_FLOAT(OFS_RETURN) = (float)S_GetEntChannelPosition(entnum, entchannel);
2930 string soundlength (string sample)
2933 void VM_soundlength (prvm_prog_t *prog)
2937 VM_SAFEPARMCOUNT(1, VM_soundlength);
2939 s = PRVM_G_STRING(OFS_PARM0);
2940 PRVM_G_FLOAT(OFS_RETURN) = S_SoundLength(s);
2947 loadfromdata(string data)
2950 void VM_loadfromdata(prvm_prog_t *prog)
2952 VM_SAFEPARMCOUNT(1,VM_loadentsfromfile);
2954 PRVM_ED_LoadFromFile(prog, PRVM_G_STRING(OFS_PARM0));
2958 ========================
2961 parseentitydata(entity ent, string data)
2962 ========================
2964 void VM_parseentitydata(prvm_prog_t *prog)
2969 VM_SAFEPARMCOUNT(2, VM_parseentitydata);
2971 // get edict and test it
2972 ent = PRVM_G_EDICT(OFS_PARM0);
2973 if (ent->priv.required->free)
2974 prog->error_cmd("VM_parseentitydata: %s: Can only set already spawned entities (entity %i is free)!", prog->name, PRVM_NUM_FOR_EDICT(ent));
2976 data = PRVM_G_STRING(OFS_PARM1);
2978 // parse the opening brace
2979 if (!COM_ParseToken_Simple(&data, false, false, true) || com_token[0] != '{' )
2980 prog->error_cmd("VM_parseentitydata: %s: Couldn't parse entity data:\n%s", prog->name, data );
2982 PRVM_ED_ParseEdict (prog, data, ent);
2989 loadfromfile(string file)
2992 void VM_loadfromfile(prvm_prog_t *prog)
2994 const char *filename;
2997 VM_SAFEPARMCOUNT(1,VM_loadfromfile);
2999 filename = PRVM_G_STRING(OFS_PARM0);
3000 if (FS_CheckNastyPath(filename, false))
3002 PRVM_G_FLOAT(OFS_RETURN) = -4;
3003 VM_Warning(prog, "VM_loadfromfile: %s dangerous or non-portable filename \"%s\" not allowed. (contains : or \\ or begins with .. or /)\n", prog->name, filename);
3007 // not conform with VM_fopen
3008 data = (char *)FS_LoadFile(filename, tempmempool, false, NULL);
3010 PRVM_G_FLOAT(OFS_RETURN) = -1;
3012 PRVM_ED_LoadFromFile(prog, data);
3023 float mod(float val, float m)
3026 void VM_modulo(prvm_prog_t *prog)
3029 VM_SAFEPARMCOUNT(2,VM_module);
3031 val = (int) PRVM_G_FLOAT(OFS_PARM0);
3032 m = (int) PRVM_G_FLOAT(OFS_PARM1);
3034 PRVM_G_FLOAT(OFS_RETURN) = (float) (val % m);
3037 static void VM_Search_Init(prvm_prog_t *prog)
3040 for (i = 0;i < PRVM_MAX_OPENSEARCHES;i++)
3041 prog->opensearches[i] = NULL;
3044 static void VM_Search_Reset(prvm_prog_t *prog)
3047 // reset the fssearch list
3048 for(i = 0; i < PRVM_MAX_OPENSEARCHES; i++)
3050 if(prog->opensearches[i])
3051 FS_FreeSearch(prog->opensearches[i]);
3052 prog->opensearches[i] = NULL;
3060 float search_begin(string pattern, float caseinsensitive, float quiet)
3063 void VM_search_begin(prvm_prog_t *prog)
3066 const char *pattern;
3067 int caseinsens, quiet;
3069 VM_SAFEPARMCOUNT(3, VM_search_begin);
3071 pattern = PRVM_G_STRING(OFS_PARM0);
3073 VM_CheckEmptyString(prog, pattern);
3075 caseinsens = (int)PRVM_G_FLOAT(OFS_PARM1);
3076 quiet = (int)PRVM_G_FLOAT(OFS_PARM2);
3078 for(handle = 0; handle < PRVM_MAX_OPENSEARCHES; handle++)
3079 if(!prog->opensearches[handle])
3082 if(handle >= PRVM_MAX_OPENSEARCHES)
3084 PRVM_G_FLOAT(OFS_RETURN) = -2;
3085 VM_Warning(prog, "VM_search_begin: %s ran out of search handles (%i)\n", prog->name, PRVM_MAX_OPENSEARCHES);
3089 if(!(prog->opensearches[handle] = FS_Search(pattern,caseinsens, quiet)))
3090 PRVM_G_FLOAT(OFS_RETURN) = -1;
3093 prog->opensearches_origin[handle] = PRVM_AllocationOrigin(prog);
3094 PRVM_G_FLOAT(OFS_RETURN) = handle;
3102 void search_end(float handle)
3105 void VM_search_end(prvm_prog_t *prog)
3108 VM_SAFEPARMCOUNT(1, VM_search_end);
3110 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
3112 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
3114 VM_Warning(prog, "VM_search_end: invalid handle %i used in %s\n", handle, prog->name);
3117 if(prog->opensearches[handle] == NULL)
3119 VM_Warning(prog, "VM_search_end: no such handle %i in %s\n", handle, prog->name);
3123 FS_FreeSearch(prog->opensearches[handle]);
3124 prog->opensearches[handle] = NULL;
3125 if(prog->opensearches_origin[handle])
3126 PRVM_Free((char *)prog->opensearches_origin[handle]);
3133 float search_getsize(float handle)
3136 void VM_search_getsize(prvm_prog_t *prog)
3139 VM_SAFEPARMCOUNT(1, VM_M_search_getsize);
3141 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
3143 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
3145 VM_Warning(prog, "VM_search_getsize: invalid handle %i used in %s\n", handle, prog->name);
3148 if(prog->opensearches[handle] == NULL)
3150 VM_Warning(prog, "VM_search_getsize: no such handle %i in %s\n", handle, prog->name);
3154 PRVM_G_FLOAT(OFS_RETURN) = prog->opensearches[handle]->numfilenames;
3159 VM_search_getfilename
3161 string search_getfilename(float handle, float num)
3164 void VM_search_getfilename(prvm_prog_t *prog)
3166 int handle, filenum;
3167 VM_SAFEPARMCOUNT(2, VM_search_getfilename);
3169 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
3170 filenum = (int)PRVM_G_FLOAT(OFS_PARM1);
3172 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
3174 VM_Warning(prog, "VM_search_getfilename: invalid handle %i used in %s\n", handle, prog->name);
3177 if(prog->opensearches[handle] == NULL)
3179 VM_Warning(prog, "VM_search_getfilename: no such handle %i in %s\n", handle, prog->name);
3182 if(filenum < 0 || filenum >= prog->opensearches[handle]->numfilenames)
3184 VM_Warning(prog, "VM_search_getfilename: invalid filenum %i in %s\n", filenum, prog->name);
3188 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, prog->opensearches[handle]->filenames[filenum]);
3195 string chr(float ascii)
3198 void VM_chr(prvm_prog_t *prog)
3202 VM_SAFEPARMCOUNT(1, VM_chr);
3204 tmp[0] = (unsigned char) PRVM_G_FLOAT(OFS_PARM0);
3207 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, tmp);
3212 VM_SAFEPARMCOUNT(1, VM_chr);
3214 len = u8_fromchar((Uchar)PRVM_G_FLOAT(OFS_PARM0), tmp, sizeof(tmp));
3216 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, tmp);
3219 //=============================================================================
3220 // Draw builtins (client & menu)
3226 float iscachedpic(string pic)
3229 void VM_iscachedpic(prvm_prog_t *prog)
3231 VM_SAFEPARMCOUNT(1,VM_iscachedpic);
3233 // drawq hasnt such a function, thus always return true
3234 PRVM_G_FLOAT(OFS_RETURN) = false;
3241 string precache_pic(string pic)
3244 void VM_precache_pic(prvm_prog_t *prog)
3248 VM_SAFEPARMCOUNT(1, VM_precache_pic);
3250 s = PRVM_G_STRING(OFS_PARM0);
3251 PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
3252 VM_CheckEmptyString(prog, s);
3254 // AK Draw_CachePic is supposed to always return a valid pointer
3255 if( Draw_CachePic_Flags(s, 0)->tex == r_texture_notexture )
3256 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
3266 void VM_freepic(prvm_prog_t *prog)
3270 VM_SAFEPARMCOUNT(1,VM_freepic);
3272 s = PRVM_G_STRING(OFS_PARM0);
3273 VM_CheckEmptyString(prog, s);
3278 static void getdrawfontscale(prvm_prog_t *prog, float *sx, float *sy)
3282 VectorCopy(PRVM_drawglobalvector(drawfontscale), v);
3283 if(VectorLength2(v) > 0)
3290 static dp_font_t *getdrawfont(prvm_prog_t *prog)
3292 int f = (int) PRVM_drawglobalfloat(drawfont);
3293 if(f < 0 || f >= dp_fonts.maxsize)
3294 return FONT_DEFAULT;
3295 return &dp_fonts.f[f];
3302 float drawcharacter(vector position, float character, vector scale, vector rgb, float alpha, float flag)
3305 void VM_drawcharacter(prvm_prog_t *prog)
3307 float *pos,*scale,*rgb;
3311 VM_SAFEPARMCOUNT(6,VM_drawcharacter);
3313 character = (char) PRVM_G_FLOAT(OFS_PARM1);
3316 PRVM_G_FLOAT(OFS_RETURN) = -1;
3317 VM_Warning(prog, "VM_drawcharacter: %s passed null character !\n",prog->name);
3321 pos = PRVM_G_VECTOR(OFS_PARM0);
3322 scale = PRVM_G_VECTOR(OFS_PARM2);
3323 rgb = PRVM_G_VECTOR(OFS_PARM3);
3324 flag = (int)PRVM_G_FLOAT(OFS_PARM5);
3326 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3328 PRVM_G_FLOAT(OFS_RETURN) = -2;
3329 VM_Warning(prog, "VM_drawcharacter: %s: wrong DRAWFLAG %i !\n",prog->name,flag);
3333 if(pos[2] || scale[2])
3334 VM_Warning(prog, "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")));
3336 if(!scale[0] || !scale[1])
3338 PRVM_G_FLOAT(OFS_RETURN) = -3;
3339 VM_Warning(prog, "VM_drawcharacter: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
3343 getdrawfontscale(prog, &sx, &sy);
3344 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(prog));
3345 PRVM_G_FLOAT(OFS_RETURN) = 1;
3352 float drawstring(vector position, string text, vector scale, vector rgb, float alpha[, float flag])
3355 void VM_drawstring(prvm_prog_t *prog)
3357 float *pos,*scale,*rgb;
3361 VM_SAFEPARMCOUNTRANGE(5,6,VM_drawstring);
3363 string = PRVM_G_STRING(OFS_PARM1);
3364 pos = PRVM_G_VECTOR(OFS_PARM0);
3365 scale = PRVM_G_VECTOR(OFS_PARM2);
3366 rgb = PRVM_G_VECTOR(OFS_PARM3);
3367 if (prog->argc >= 6)
3368 flag = (int)PRVM_G_FLOAT(OFS_PARM5);
3370 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3372 PRVM_G_FLOAT(OFS_RETURN) = -2;
3373 VM_Warning(prog, "VM_drawstring: %s: wrong DRAWFLAG %i !\n",prog->name,flag);
3377 if(!scale[0] || !scale[1])
3379 PRVM_G_FLOAT(OFS_RETURN) = -3;
3380 VM_Warning(prog, "VM_drawstring: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
3384 if(pos[2] || scale[2])
3385 VM_Warning(prog, "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")));
3387 getdrawfontscale(prog, &sx, &sy);
3388 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(prog));
3389 //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);
3390 PRVM_G_FLOAT(OFS_RETURN) = 1;
3395 VM_drawcolorcodedstring
3397 float drawcolorcodedstring(vector position, string text, vector scale, float alpha, float flag)
3399 float drawcolorcodedstring(vector position, string text, vector scale, vector rgb, float alpha, float flag)
3402 void VM_drawcolorcodedstring(prvm_prog_t *prog)
3408 float sx, sy, alpha;
3410 VM_SAFEPARMCOUNTRANGE(5,6,VM_drawcolorcodedstring);
3412 if (prog->argc == 6) // full 6 parms, like normal drawstring
3414 pos = PRVM_G_VECTOR(OFS_PARM0);
3415 string = PRVM_G_STRING(OFS_PARM1);
3416 scale = PRVM_G_VECTOR(OFS_PARM2);
3417 VectorCopy(PRVM_G_VECTOR(OFS_PARM3), rgb);
3418 alpha = PRVM_G_FLOAT(OFS_PARM4);
3419 flag = (int)PRVM_G_FLOAT(OFS_PARM5);
3423 pos = PRVM_G_VECTOR(OFS_PARM0);
3424 string = PRVM_G_STRING(OFS_PARM1);
3425 scale = PRVM_G_VECTOR(OFS_PARM2);
3429 alpha = PRVM_G_FLOAT(OFS_PARM3);
3430 flag = (int)PRVM_G_FLOAT(OFS_PARM4);
3433 if(flag < DRAWFLAG_NORMAL || flag >= DRAWFLAG_NUMFLAGS)
3435 PRVM_G_FLOAT(OFS_RETURN) = -2;
3436 VM_Warning(prog, "VM_drawcolorcodedstring: %s: wrong DRAWFLAG %i !\n",prog->name,flag);
3440 if(!scale[0] || !scale[1])
3442 PRVM_G_FLOAT(OFS_RETURN) = -3;
3443 VM_Warning(prog, "VM_drawcolorcodedstring: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
3447 if(pos[2] || scale[2])
3448 VM_Warning(prog, "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")));
3450 getdrawfontscale(prog, &sx, &sy);
3451 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(prog));
3452 if (prog->argc == 6) // also return vector of last color
3453 VectorCopy(DrawQ_Color, PRVM_G_VECTOR(OFS_RETURN));
3455 PRVM_G_FLOAT(OFS_RETURN) = 1;
3461 float stringwidth(string text, float allowColorCodes, float size)
3464 void VM_stringwidth(prvm_prog_t *prog)
3468 float mult; // sz is intended font size so we can later add freetype support, mult is font size multiplier in pixels per character cell
3472 VM_SAFEPARMCOUNTRANGE(2,3,VM_drawstring);
3474 getdrawfontscale(prog, &sx, &sy);
3477 szv = PRVM_G_VECTOR(OFS_PARM2);
3482 // we want the width for 8x8 font size, divided by 8
3483 static float defsize[] = {8, 8};
3486 // to make sure snapping is turned off, ALWAYS use a nontrivial scale in this case
3487 if(sx >= 0.9 && sx <= 1.1)
3495 string = PRVM_G_STRING(OFS_PARM0);
3496 colors = (int)PRVM_G_FLOAT(OFS_PARM1);
3498 PRVM_G_FLOAT(OFS_RETURN) = DrawQ_TextWidth_UntilWidth_TrackColors_Scale(string, &maxlen, szv[0], szv[1], sx, sy, NULL, !colors, getdrawfont(prog), 1000000000) * mult;
3502 mult = sz = PRVM_G_FLOAT(OFS_PARM2);
3510 string = PRVM_G_STRING(OFS_PARM0);
3511 colors = (int)PRVM_G_FLOAT(OFS_PARM1);
3513 PRVM_G_FLOAT(OFS_RETURN) = DrawQ_TextWidth(string, 0, !colors, getdrawfont()) * mult; // 1x1 characters, don't actually draw
3521 float findfont(string s)
3525 static float getdrawfontnum(const char *fontname)
3529 for(i = 0; i < dp_fonts.maxsize; ++i)
3530 if(!strcmp(dp_fonts.f[i].title, fontname))
3535 void VM_findfont(prvm_prog_t *prog)
3537 VM_SAFEPARMCOUNT(1,VM_findfont);
3538 PRVM_G_FLOAT(OFS_RETURN) = getdrawfontnum(PRVM_G_STRING(OFS_PARM0));
3545 float loadfont(string fontname, string fontmaps, string sizes, float slot)
3549 void VM_loadfont(prvm_prog_t *prog)
3551 const char *fontname, *filelist, *sizes, *c, *cm;
3552 char mainfont[MAX_QPATH];
3554 float sz, scale, voffset;
3557 VM_SAFEPARMCOUNTRANGE(3,6,VM_loadfont);
3559 fontname = PRVM_G_STRING(OFS_PARM0);
3561 fontname = "default";
3563 filelist = PRVM_G_STRING(OFS_PARM1);
3565 filelist = "gfx/conchars";
3567 sizes = PRVM_G_STRING(OFS_PARM2);
3573 if (prog->argc >= 4)
3575 i = PRVM_G_FLOAT(OFS_PARM3);
3576 if (i >= 0 && i < dp_fonts.maxsize)
3579 strlcpy(f->title, fontname, sizeof(f->title)); // replace name
3583 f = FindFont(fontname, true);
3586 PRVM_G_FLOAT(OFS_RETURN) = -1;
3587 return; // something go wrong
3590 memset(f->fallbacks, 0, sizeof(f->fallbacks));
3591 memset(f->fallback_faces, 0, sizeof(f->fallback_faces));
3593 // first font is handled "normally"
3594 c = strchr(filelist, ':');
3595 cm = strchr(filelist, ',');
3596 if(c && (!cm || c < cm))
3597 f->req_face = atoi(c+1);
3603 if(!c || (c - filelist) > MAX_QPATH)
3604 strlcpy(mainfont, filelist, sizeof(mainfont));
3607 memcpy(mainfont, filelist, c - filelist);
3608 mainfont[c - filelist] = 0;
3612 for(i = 0; i < MAX_FONT_FALLBACKS; ++i)
3614 c = strchr(filelist, ',');
3620 c = strchr(filelist, ':');
3621 cm = strchr(filelist, ',');
3622 if(c && (!cm || c < cm))
3623 f->fallback_faces[i] = atoi(c+1);
3626 f->fallback_faces[i] = 0; // f->req_face; could make it stick to the default-font's face index
3629 if(!c || (c-filelist) > MAX_QPATH)
3631 strlcpy(f->fallbacks[i], filelist, sizeof(mainfont));
3635 memcpy(f->fallbacks[i], filelist, c - filelist);
3636 f->fallbacks[i][c - filelist] = 0;
3641 for(i = 0; i < MAX_FONT_SIZES; ++i)
3642 f->req_sizes[i] = -1;
3643 for (numsizes = 0,c = sizes;;)
3645 if (!COM_ParseToken_VM_Tokenize(&c, 0))
3647 sz = atof(com_token);
3649 if (sz < 0.001f || sz > 1000.0f)
3651 VM_Warning(prog, "VM_loadfont: crap size %s", com_token);
3655 if (numsizes == MAX_FONT_SIZES)
3657 VM_Warning(prog, "VM_loadfont: MAX_FONT_SIZES = %i exceeded", MAX_FONT_SIZES);
3660 f->req_sizes[numsizes] = sz;
3664 // additional scale/hoffset parms
3667 if (prog->argc >= 5)
3669 scale = PRVM_G_FLOAT(OFS_PARM4);
3673 if (prog->argc >= 6)
3674 voffset = PRVM_G_FLOAT(OFS_PARM5);
3677 LoadFont(true, mainfont, f, scale, voffset);
3679 // return index of loaded font
3680 PRVM_G_FLOAT(OFS_RETURN) = (f - dp_fonts.f);
3687 float drawpic(vector position, string pic, vector size, vector rgb, float alpha, float flag)
3690 void VM_drawpic(prvm_prog_t *prog)
3692 const char *picname;
3693 float *size, *pos, *rgb;
3696 VM_SAFEPARMCOUNTRANGE(5,6,VM_drawpic);
3698 picname = PRVM_G_STRING(OFS_PARM1);
3699 VM_CheckEmptyString(prog, picname);
3701 // is pic cached ? no function yet for that
3704 PRVM_G_FLOAT(OFS_RETURN) = -4;
3705 VM_Warning(prog, "VM_drawpic: %s: %s not cached !\n", prog->name, picname);
3709 pos = PRVM_G_VECTOR(OFS_PARM0);
3710 size = PRVM_G_VECTOR(OFS_PARM2);
3711 rgb = PRVM_G_VECTOR(OFS_PARM3);
3712 if (prog->argc >= 6)
3713 flag = (int) PRVM_G_FLOAT(OFS_PARM5);
3715 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3717 PRVM_G_FLOAT(OFS_RETURN) = -2;
3718 VM_Warning(prog, "VM_drawpic: %s: wrong DRAWFLAG %i !\n",prog->name,flag);
3722 if(pos[2] || size[2])
3723 VM_Warning(prog, "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")));
3725 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);
3726 PRVM_G_FLOAT(OFS_RETURN) = 1;
3732 float drawrotpic(vector position, string pic, vector size, vector org, float angle, vector rgb, float alpha, float flag)
3735 void VM_drawrotpic(prvm_prog_t *prog)
3737 const char *picname;
3738 float *size, *pos, *org, *rgb;
3741 VM_SAFEPARMCOUNT(8,VM_drawrotpic);
3743 picname = PRVM_G_STRING(OFS_PARM1);
3744 VM_CheckEmptyString(prog, picname);
3746 // is pic cached ? no function yet for that
3749 PRVM_G_FLOAT(OFS_RETURN) = -4;
3750 VM_Warning(prog, "VM_drawrotpic: %s: %s not cached !\n", prog->name, picname);
3754 pos = PRVM_G_VECTOR(OFS_PARM0);
3755 size = PRVM_G_VECTOR(OFS_PARM2);
3756 org = PRVM_G_VECTOR(OFS_PARM3);
3757 rgb = PRVM_G_VECTOR(OFS_PARM5);
3758 flag = (int) PRVM_G_FLOAT(OFS_PARM7);
3760 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3762 PRVM_G_FLOAT(OFS_RETURN) = -2;
3763 VM_Warning(prog, "VM_drawrotpic: %s: wrong DRAWFLAG %i !\n",prog->name,flag);
3767 if(pos[2] || size[2] || org[2])
3768 VM_Warning(prog, "VM_drawrotpic: z value from pos/size/org discarded\n");
3770 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);
3771 PRVM_G_FLOAT(OFS_RETURN) = 1;
3777 float drawsubpic(vector position, vector size, string pic, vector srcPos, vector srcSize, vector rgb, float alpha, float flag)
3781 void VM_drawsubpic(prvm_prog_t *prog)
3783 const char *picname;
3784 float *size, *pos, *rgb, *srcPos, *srcSize, alpha;
3787 VM_SAFEPARMCOUNT(8,VM_drawsubpic);
3789 picname = PRVM_G_STRING(OFS_PARM2);
3790 VM_CheckEmptyString(prog, picname);
3792 // is pic cached ? no function yet for that
3795 PRVM_G_FLOAT(OFS_RETURN) = -4;
3796 VM_Warning(prog, "VM_drawsubpic: %s: %s not cached !\n", prog->name, picname);
3800 pos = PRVM_G_VECTOR(OFS_PARM0);
3801 size = PRVM_G_VECTOR(OFS_PARM1);
3802 srcPos = PRVM_G_VECTOR(OFS_PARM3);
3803 srcSize = PRVM_G_VECTOR(OFS_PARM4);
3804 rgb = PRVM_G_VECTOR(OFS_PARM5);
3805 alpha = PRVM_G_FLOAT(OFS_PARM6);
3806 flag = (int) PRVM_G_FLOAT(OFS_PARM7);
3808 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3810 PRVM_G_FLOAT(OFS_RETURN) = -2;
3811 VM_Warning(prog, "VM_drawsubpic: %s: wrong DRAWFLAG %i !\n",prog->name,flag);
3815 if(pos[2] || size[2])
3816 VM_Warning(prog, "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")));
3818 DrawQ_SuperPic(pos[0], pos[1], Draw_CachePic_Flags (picname, CACHEPICFLAG_NOTPERSISTENT),
3820 srcPos[0], srcPos[1], rgb[0], rgb[1], rgb[2], alpha,
3821 srcPos[0] + srcSize[0], srcPos[1], rgb[0], rgb[1], rgb[2], alpha,
3822 srcPos[0], srcPos[1] + srcSize[1], rgb[0], rgb[1], rgb[2], alpha,
3823 srcPos[0] + srcSize[0], srcPos[1] + srcSize[1], rgb[0], rgb[1], rgb[2], alpha,
3825 PRVM_G_FLOAT(OFS_RETURN) = 1;
3832 float drawfill(vector position, vector size, vector rgb, float alpha, float flag)
3835 void VM_drawfill(prvm_prog_t *prog)
3837 float *size, *pos, *rgb;
3840 VM_SAFEPARMCOUNT(5,VM_drawfill);
3843 pos = PRVM_G_VECTOR(OFS_PARM0);
3844 size = PRVM_G_VECTOR(OFS_PARM1);
3845 rgb = PRVM_G_VECTOR(OFS_PARM2);
3846 flag = (int) PRVM_G_FLOAT(OFS_PARM4);
3848 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3850 PRVM_G_FLOAT(OFS_RETURN) = -2;
3851 VM_Warning(prog, "VM_drawfill: %s: wrong DRAWFLAG %i !\n",prog->name,flag);
3855 if(pos[2] || size[2])
3856 VM_Warning(prog, "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")));
3858 DrawQ_Fill(pos[0], pos[1], size[0], size[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM3), flag);
3859 PRVM_G_FLOAT(OFS_RETURN) = 1;
3866 drawsetcliparea(float x, float y, float width, float height)
3869 void VM_drawsetcliparea(prvm_prog_t *prog)
3872 VM_SAFEPARMCOUNT(4,VM_drawsetcliparea);
3874 x = bound(0, PRVM_G_FLOAT(OFS_PARM0), vid_conwidth.integer);
3875 y = bound(0, PRVM_G_FLOAT(OFS_PARM1), vid_conheight.integer);
3876 w = bound(0, PRVM_G_FLOAT(OFS_PARM2) + PRVM_G_FLOAT(OFS_PARM0) - x, (vid_conwidth.integer - x));
3877 h = bound(0, PRVM_G_FLOAT(OFS_PARM3) + PRVM_G_FLOAT(OFS_PARM1) - y, (vid_conheight.integer - y));
3879 DrawQ_SetClipArea(x, y, w, h);
3884 VM_drawresetcliparea
3889 void VM_drawresetcliparea(prvm_prog_t *prog)
3891 VM_SAFEPARMCOUNT(0,VM_drawresetcliparea);
3893 DrawQ_ResetClipArea();
3900 vector getimagesize(string pic)
3903 void VM_getimagesize(prvm_prog_t *prog)
3908 VM_SAFEPARMCOUNT(1,VM_getimagesize);
3910 p = PRVM_G_STRING(OFS_PARM0);
3911 VM_CheckEmptyString(prog, p);
3913 pic = Draw_CachePic_Flags (p, CACHEPICFLAG_NOTPERSISTENT);
3914 if( Draw_CachePic_Flags(s, 0)->tex == r_texture_notexture )
3916 PRVM_G_VECTOR(OFS_RETURN)[0] = 0;
3917 PRVM_G_VECTOR(OFS_RETURN)[1] = 0;
3921 PRVM_G_VECTOR(OFS_RETURN)[0] = pic->width;
3922 PRVM_G_VECTOR(OFS_RETURN)[1] = pic->height;
3924 PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
3931 string keynumtostring(float keynum)
3934 void VM_keynumtostring (prvm_prog_t *prog)
3937 VM_SAFEPARMCOUNT(1, VM_keynumtostring);
3939 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, Key_KeynumToString((int)PRVM_G_FLOAT(OFS_PARM0), tinystr, sizeof(tinystr)));
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(prvm_prog_t *prog)
3956 char ret[VM_STRINGTEMP_LENGTH];
3957 int keys[FKFC_NUMKEYS];
3962 VM_SAFEPARMCOUNTRANGE(1, 2, VM_findkeysforcommand);
3964 cmd = PRVM_G_STRING(OFS_PARM0);
3966 bindmap = bound(-1, PRVM_G_FLOAT(OFS_PARM1), MAX_BINDMAPS-1);
3968 bindmap = 0; // consistent to "bind"
3970 VM_CheckEmptyString(prog, cmd);
3972 Key_FindKeysForCommand(cmd, keys, FKFC_NUMKEYS, bindmap);
3975 for(i = 0; i < FKFC_NUMKEYS; i++)
3976 strlcat(ret, va(vabuf, sizeof(vabuf), " \'%i\'", keys[i]), sizeof(ret));
3978 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, ret);
3985 float stringtokeynum(string key)
3988 void VM_stringtokeynum (prvm_prog_t *prog)
3990 VM_SAFEPARMCOUNT( 1, VM_keynumtostring );
3992 PRVM_G_FLOAT(OFS_RETURN) = Key_StringToKeynum(PRVM_G_STRING(OFS_PARM0));
3999 string getkeybind(float key, float bindmap)
4002 void VM_getkeybind (prvm_prog_t *prog)
4005 VM_SAFEPARMCOUNTRANGE(1, 2, VM_CL_getkeybind);
4007 bindmap = bound(-1, PRVM_G_FLOAT(OFS_PARM1), MAX_BINDMAPS-1);
4009 bindmap = 0; // consistent to "bind"
4011 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, Key_GetBind((int)PRVM_G_FLOAT(OFS_PARM0), bindmap));
4018 float setkeybind(float key, string cmd, float bindmap)
4021 void VM_setkeybind (prvm_prog_t *prog)
4024 VM_SAFEPARMCOUNTRANGE(2, 3, VM_CL_setkeybind);
4026 bindmap = bound(-1, PRVM_G_FLOAT(OFS_PARM2), MAX_BINDMAPS-1);
4028 bindmap = 0; // consistent to "bind"
4030 PRVM_G_FLOAT(OFS_RETURN) = 0;
4031 if(Key_SetBinding((int)PRVM_G_FLOAT(OFS_PARM0), bindmap, PRVM_G_STRING(OFS_PARM1)))
4032 PRVM_G_FLOAT(OFS_RETURN) = 1;
4039 vector getbindmaps()
4042 void VM_getbindmaps (prvm_prog_t *prog)
4045 VM_SAFEPARMCOUNT(0, VM_CL_getbindmap);
4046 Key_GetBindMap(&fg, &bg);
4047 PRVM_G_VECTOR(OFS_RETURN)[0] = fg;
4048 PRVM_G_VECTOR(OFS_RETURN)[1] = bg;
4049 PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
4056 float setbindmaps(vector bindmap)
4059 void VM_setbindmaps (prvm_prog_t *prog)
4061 VM_SAFEPARMCOUNT(1, VM_CL_setbindmap);
4062 PRVM_G_FLOAT(OFS_RETURN) = 0;
4063 if(PRVM_G_VECTOR(OFS_PARM0)[2] == 0)
4064 if(Key_SetBindMap((int)PRVM_G_VECTOR(OFS_PARM0)[0], (int)PRVM_G_VECTOR(OFS_PARM0)[1]))
4065 PRVM_G_FLOAT(OFS_RETURN) = 1;
4068 // CL_Video interface functions
4071 ========================
4074 float cin_open(string file, string name)
4075 ========================
4077 void VM_cin_open(prvm_prog_t *prog)
4082 VM_SAFEPARMCOUNT( 2, VM_cin_open );
4084 file = PRVM_G_STRING( OFS_PARM0 );
4085 name = PRVM_G_STRING( OFS_PARM1 );
4087 VM_CheckEmptyString(prog, file );
4088 VM_CheckEmptyString(prog, name );
4090 if( CL_OpenVideo( file, name, MENUOWNER, "" ) )
4091 PRVM_G_FLOAT( OFS_RETURN ) = 1;
4093 PRVM_G_FLOAT( OFS_RETURN ) = 0;
4097 ========================
4100 void cin_close(string name)
4101 ========================
4103 void VM_cin_close(prvm_prog_t *prog)
4107 VM_SAFEPARMCOUNT( 1, VM_cin_close );
4109 name = PRVM_G_STRING( OFS_PARM0 );
4110 VM_CheckEmptyString(prog, name );
4112 CL_CloseVideo( CL_GetVideoByName( name ) );
4116 ========================
4118 void cin_setstate(string name, float type)
4119 ========================
4121 void VM_cin_setstate(prvm_prog_t *prog)
4124 clvideostate_t state;
4127 VM_SAFEPARMCOUNT( 2, VM_cin_netstate );
4129 name = PRVM_G_STRING( OFS_PARM0 );
4130 VM_CheckEmptyString(prog, name );
4132 state = (clvideostate_t)((int)PRVM_G_FLOAT( OFS_PARM1 ));
4134 video = CL_GetVideoByName( name );
4135 if( video && state > CLVIDEO_UNUSED && state < CLVIDEO_STATECOUNT )
4136 CL_SetVideoState( video, state );
4140 ========================
4143 float cin_getstate(string name)
4144 ========================
4146 void VM_cin_getstate(prvm_prog_t *prog)
4151 VM_SAFEPARMCOUNT( 1, VM_cin_getstate );
4153 name = PRVM_G_STRING( OFS_PARM0 );
4154 VM_CheckEmptyString(prog, name );
4156 video = CL_GetVideoByName( name );
4158 PRVM_G_FLOAT( OFS_RETURN ) = (int)video->state;
4160 PRVM_G_FLOAT( OFS_RETURN ) = 0;
4164 ========================
4167 void cin_restart(string name)
4168 ========================
4170 void VM_cin_restart(prvm_prog_t *prog)
4175 VM_SAFEPARMCOUNT( 1, VM_cin_restart );
4177 name = PRVM_G_STRING( OFS_PARM0 );
4178 VM_CheckEmptyString(prog, name );
4180 video = CL_GetVideoByName( name );
4182 CL_RestartVideo( video );
4186 ========================
4189 float[bool] gecko_create( string name )
4190 ========================
4192 void VM_gecko_create(prvm_prog_t *prog) {
4194 PRVM_G_FLOAT( OFS_RETURN ) = 0;
4198 ========================
4201 void gecko_destroy( string name )
4202 ========================
4204 void VM_gecko_destroy(prvm_prog_t *prog) {
4209 ========================
4212 void gecko_navigate( string name, string URI )
4213 ========================
4215 void VM_gecko_navigate(prvm_prog_t *prog) {
4220 ========================
4223 float[bool] gecko_keyevent( string name, float key, float eventtype )
4224 ========================
4226 void VM_gecko_keyevent(prvm_prog_t *prog) {
4228 PRVM_G_FLOAT( OFS_RETURN ) = 0;
4232 ========================
4235 void gecko_mousemove( string name, float x, float y )
4236 ========================
4238 void VM_gecko_movemouse(prvm_prog_t *prog) {
4244 ========================
4247 void gecko_resize( string name, float w, float h )
4248 ========================
4250 void VM_gecko_resize(prvm_prog_t *prog) {
4256 ========================
4257 VM_gecko_get_texture_extent
4259 vector gecko_get_texture_extent( string name )
4260 ========================
4262 void VM_gecko_get_texture_extent(prvm_prog_t *prog) {
4264 PRVM_G_VECTOR(OFS_RETURN)[0] = 0;
4265 PRVM_G_VECTOR(OFS_RETURN)[1] = 0;
4274 Writes new values for v_forward, v_up, and v_right based on angles
4275 void makevectors(vector angle)
4278 void VM_makevectors (prvm_prog_t *prog)
4280 VM_SAFEPARMCOUNT(1, VM_makevectors);
4281 AngleVectors(PRVM_G_VECTOR(OFS_PARM0), PRVM_gameglobalvector(v_forward), PRVM_gameglobalvector(v_right), PRVM_gameglobalvector(v_up));
4288 Writes new values for v_forward, v_up, and v_right based on the given forward vector
4289 vectorvectors(vector)
4292 void VM_vectorvectors (prvm_prog_t *prog)
4294 VM_SAFEPARMCOUNT(1, VM_vectorvectors);
4295 VectorNormalize2(PRVM_G_VECTOR(OFS_PARM0), PRVM_gameglobalvector(v_forward));
4296 VectorVectors(PRVM_gameglobalvector(v_forward), PRVM_gameglobalvector(v_right), PRVM_gameglobalvector(v_up));
4300 ========================
4303 void drawline(float width, vector pos1, vector pos2, vector rgb, float alpha, float flags)
4304 ========================
4306 void VM_drawline (prvm_prog_t *prog)
4308 float *c1, *c2, *rgb;
4310 unsigned char flags;
4312 VM_SAFEPARMCOUNT(6, VM_drawline);
4313 width = PRVM_G_FLOAT(OFS_PARM0);
4314 c1 = PRVM_G_VECTOR(OFS_PARM1);
4315 c2 = PRVM_G_VECTOR(OFS_PARM2);
4316 rgb = PRVM_G_VECTOR(OFS_PARM3);
4317 alpha = PRVM_G_FLOAT(OFS_PARM4);
4318 flags = (int)PRVM_G_FLOAT(OFS_PARM5);
4319 DrawQ_Line(width, c1[0], c1[1], c2[0], c2[1], rgb[0], rgb[1], rgb[2], alpha, flags);
4322 // float(float number, float quantity) bitshift (EXT_BITSHIFT)
4323 void VM_bitshift (prvm_prog_t *prog)
4326 VM_SAFEPARMCOUNT(2, VM_bitshift);
4328 n1 = (int)fabs((float)((int)PRVM_G_FLOAT(OFS_PARM0)));
4329 n2 = (int)PRVM_G_FLOAT(OFS_PARM1);
4331 PRVM_G_FLOAT(OFS_RETURN) = n1;
4334 PRVM_G_FLOAT(OFS_RETURN) = (n1 >> -n2);
4336 PRVM_G_FLOAT(OFS_RETURN) = (n1 << n2);
4339 ////////////////////////////////////////
4340 // AltString functions
4341 ////////////////////////////////////////
4344 ========================
4347 float altstr_count(string)
4348 ========================
4350 void VM_altstr_count(prvm_prog_t *prog)
4352 const char *altstr, *pos;
4355 VM_SAFEPARMCOUNT( 1, VM_altstr_count );
4357 altstr = PRVM_G_STRING( OFS_PARM0 );
4358 //VM_CheckEmptyString(prog, altstr );
4360 for( count = 0, pos = altstr ; *pos ; pos++ ) {
4361 if( *pos == '\\' ) {
4365 } else if( *pos == '\'' ) {
4370 PRVM_G_FLOAT( OFS_RETURN ) = (float) (count / 2);
4374 ========================
4377 string altstr_prepare(string)
4378 ========================
4380 void VM_altstr_prepare(prvm_prog_t *prog)
4383 const char *instr, *in;
4385 char outstr[VM_STRINGTEMP_LENGTH];
4387 VM_SAFEPARMCOUNT( 1, VM_altstr_prepare );
4389 instr = PRVM_G_STRING( OFS_PARM0 );
4391 for( out = outstr, in = instr, size = sizeof(outstr) - 1 ; size && *in ; size--, in++, out++ )
4400 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString(prog, outstr );
4404 ========================
4407 string altstr_get(string, float)
4408 ========================
4410 void VM_altstr_get(prvm_prog_t *prog)
4412 const char *altstr, *pos;
4415 char outstr[VM_STRINGTEMP_LENGTH];
4417 VM_SAFEPARMCOUNT( 2, VM_altstr_get );
4419 altstr = PRVM_G_STRING( OFS_PARM0 );
4421 count = (int)PRVM_G_FLOAT( OFS_PARM1 );
4422 count = count * 2 + 1;
4424 for( pos = altstr ; *pos && count ; pos++ )
4425 if( *pos == '\\' ) {
4428 } else if( *pos == '\'' )
4432 PRVM_G_INT( OFS_RETURN ) = 0;
4436 for( out = outstr, size = sizeof(outstr) - 1 ; size && *pos ; size--, pos++, out++ )
4437 if( *pos == '\\' ) {
4442 } else if( *pos == '\'' )
4448 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString(prog, outstr );
4452 ========================
4455 string altstr_set(string altstr, float num, string set)
4456 ========================
4458 void VM_altstr_set(prvm_prog_t *prog)
4461 const char *altstr, *str;
4464 char outstr[VM_STRINGTEMP_LENGTH];
4466 VM_SAFEPARMCOUNT( 3, VM_altstr_set );
4468 altstr = PRVM_G_STRING( OFS_PARM0 );
4470 num = (int)PRVM_G_FLOAT( OFS_PARM1 );
4472 str = PRVM_G_STRING( OFS_PARM2 );
4475 for( num = num * 2 + 1, in = altstr; *in && num; *out++ = *in++ )
4480 } else if( *in == '\'' ) {
4485 for( ; *str; *out++ = *str++ );
4486 // now jump over the old content
4488 if( *in == '\'' || (*in == '\\' && !*++in) )
4491 strlcpy(out, in, outstr + sizeof(outstr) - out);
4492 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString(prog, outstr );
4496 ========================
4499 string altstr_ins(string altstr, float num, string set)
4500 ========================
4502 void VM_altstr_ins(prvm_prog_t *prog)
4508 char outstr[VM_STRINGTEMP_LENGTH];
4510 VM_SAFEPARMCOUNT(3, VM_altstr_ins);
4512 in = PRVM_G_STRING( OFS_PARM0 );
4513 num = (int)PRVM_G_FLOAT( OFS_PARM1 );
4514 set = PRVM_G_STRING( OFS_PARM2 );
4517 for( num = num * 2 + 2 ; *in && num > 0 ; *out++ = *in++ )
4522 } else if( *in == '\'' ) {
4527 for( ; *set ; *out++ = *set++ );
4530 strlcpy(out, in, outstr + sizeof(outstr) - out);
4531 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString(prog, outstr );
4535 ////////////////////////////////////////
4536 // BufString functions
4537 ////////////////////////////////////////
4538 //[515]: string buffers support
4540 static size_t stringbuffers_sortlength;
4542 static void BufStr_Expand(prvm_prog_t *prog, prvm_stringbuffer_t *stringbuffer, int strindex)
4544 if (stringbuffer->max_strings <= strindex)
4546 char **oldstrings = stringbuffer->strings;
4547 stringbuffer->max_strings = max(stringbuffer->max_strings * 2, 128);
4548 while (stringbuffer->max_strings <= strindex)
4549 stringbuffer->max_strings *= 2;
4550 stringbuffer->strings = (char **) Mem_Alloc(prog->progs_mempool, stringbuffer->max_strings * sizeof(stringbuffer->strings[0]));
4551 if (stringbuffer->num_strings > 0)
4552 memcpy(stringbuffer->strings, oldstrings, stringbuffer->num_strings * sizeof(stringbuffer->strings[0]));
4554 Mem_Free(oldstrings);
4558 static void BufStr_Shrink(prvm_prog_t *prog, prvm_stringbuffer_t *stringbuffer)
4560 // reduce num_strings if there are empty string slots at the end
4561 while (stringbuffer->num_strings > 0 && stringbuffer->strings[stringbuffer->num_strings - 1] == NULL)
4562 stringbuffer->num_strings--;
4564 // if empty, free the string pointer array
4565 if (stringbuffer->num_strings == 0)
4567 stringbuffer->max_strings = 0;
4568 if (stringbuffer->strings)
4569 Mem_Free(stringbuffer->strings);
4570 stringbuffer->strings = NULL;
4574 static int BufStr_SortStringsUP (const void *in1, const void *in2)
4577 a = *((const char **) in1);
4578 b = *((const char **) in2);
4579 if(!a || !a[0]) return 1;
4580 if(!b || !b[0]) return -1;
4581 return strncmp(a, b, stringbuffers_sortlength);
4584 static int BufStr_SortStringsDOWN (const void *in1, const void *in2)
4587 a = *((const char **) in1);
4588 b = *((const char **) in2);
4589 if(!a || !a[0]) return 1;
4590 if(!b || !b[0]) return -1;
4591 return strncmp(b, a, stringbuffers_sortlength);
4595 ========================
4597 creates new buffer, and returns it's index, returns -1 if failed
4598 float buf_create(prvm_prog_t *prog) = #460;
4599 float newbuf(string format, float flags) = #460;
4600 ========================
4603 void VM_buf_create (prvm_prog_t *prog)
4605 prvm_stringbuffer_t *stringbuffer;
4608 VM_SAFEPARMCOUNTRANGE(0, 2, VM_buf_create);
4610 // VorteX: optional parm1 (buffer format) is unfinished, to keep intact with future databuffers extension must be set to "string"
4611 if(prog->argc >= 1 && strcmp(PRVM_G_STRING(OFS_PARM0), "string"))
4613 PRVM_G_FLOAT(OFS_RETURN) = -1;
4616 stringbuffer = (prvm_stringbuffer_t *) Mem_ExpandableArray_AllocRecord(&prog->stringbuffersarray);
4617 for (i = 0;stringbuffer != Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, i);i++);
4618 stringbuffer->origin = PRVM_AllocationOrigin(prog);
4619 // optional flags parm
4620 if (prog->argc >= 2)
4621 stringbuffer->flags = (int)PRVM_G_FLOAT(OFS_PARM1) & 0xFF;
4622 PRVM_G_FLOAT(OFS_RETURN) = i;
4628 ========================
4630 deletes buffer and all strings in it
4631 void buf_del(float bufhandle) = #461;
4632 ========================
4634 void VM_buf_del (prvm_prog_t *prog)
4636 prvm_stringbuffer_t *stringbuffer;
4637 VM_SAFEPARMCOUNT(1, VM_buf_del);
4638 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4642 for (i = 0;i < stringbuffer->num_strings;i++)
4643 if (stringbuffer->strings[i])
4644 Mem_Free(stringbuffer->strings[i]);
4645 if (stringbuffer->strings)
4646 Mem_Free(stringbuffer->strings);
4647 if(stringbuffer->origin)
4648 PRVM_Free((char *)stringbuffer->origin);
4649 Mem_ExpandableArray_FreeRecord(&prog->stringbuffersarray, stringbuffer);
4653 VM_Warning(prog, "VM_buf_del: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), prog->name);
4659 ========================
4661 how many strings are stored in buffer
4662 float buf_getsize(float bufhandle) = #462;
4663 ========================
4665 void VM_buf_getsize (prvm_prog_t *prog)
4667 prvm_stringbuffer_t *stringbuffer;
4668 VM_SAFEPARMCOUNT(1, VM_buf_getsize);
4670 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4673 PRVM_G_FLOAT(OFS_RETURN) = -1;
4674 VM_Warning(prog, "VM_buf_getsize: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), prog->name);
4678 PRVM_G_FLOAT(OFS_RETURN) = stringbuffer->num_strings;
4682 ========================
4684 copy all content from one buffer to another, make sure it exists
4685 void buf_copy(float bufhandle_from, float bufhandle_to) = #463;
4686 ========================
4688 void VM_buf_copy (prvm_prog_t *prog)
4690 prvm_stringbuffer_t *srcstringbuffer, *dststringbuffer;
4692 VM_SAFEPARMCOUNT(2, VM_buf_copy);
4694 srcstringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4695 if(!srcstringbuffer)
4697 VM_Warning(prog, "VM_buf_copy: invalid source buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), prog->name);
4700 i = (int)PRVM_G_FLOAT(OFS_PARM1);
4701 if(i == (int)PRVM_G_FLOAT(OFS_PARM0))
4703 VM_Warning(prog, "VM_buf_copy: source == destination (%i) in %s\n", i, prog->name);
4706 dststringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4707 if(!dststringbuffer)
4709 VM_Warning(prog, "VM_buf_copy: invalid destination buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM1), prog->name);
4713 for (i = 0;i < dststringbuffer->num_strings;i++)
4714 if (dststringbuffer->strings[i])
4715 Mem_Free(dststringbuffer->strings[i]);
4716 if (dststringbuffer->strings)
4717 Mem_Free(dststringbuffer->strings);
4718 *dststringbuffer = *srcstringbuffer;
4719 if (dststringbuffer->max_strings)
4720 dststringbuffer->strings = (char **)Mem_Alloc(prog->progs_mempool, sizeof(dststringbuffer->strings[0]) * dststringbuffer->max_strings);
4722 for (i = 0;i < dststringbuffer->num_strings;i++)
4724 if (srcstringbuffer->strings[i])
4727 stringlen = strlen(srcstringbuffer->strings[i]) + 1;
4728 dststringbuffer->strings[i] = (char *)Mem_Alloc(prog->progs_mempool, stringlen);
4729 memcpy(dststringbuffer->strings[i], srcstringbuffer->strings[i], stringlen);
4735 ========================
4737 sort buffer by beginnings of strings (cmplength defaults it's length)
4738 "backward == TRUE" means that sorting goes upside-down
4739 void buf_sort(float bufhandle, float cmplength, float backward) = #464;
4740 ========================
4742 void VM_buf_sort (prvm_prog_t *prog)
4744 prvm_stringbuffer_t *stringbuffer;
4745 VM_SAFEPARMCOUNT(3, VM_buf_sort);
4747 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4750 VM_Warning(prog, "VM_buf_sort: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), prog->name);
4753 if(stringbuffer->num_strings <= 0)
4755 VM_Warning(prog, "VM_buf_sort: tried to sort empty buffer %i in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), prog->name);
4758 stringbuffers_sortlength = (int)PRVM_G_FLOAT(OFS_PARM1);
4759 if(stringbuffers_sortlength <= 0)
4760 stringbuffers_sortlength = 0x7FFFFFFF;
4762 if(!PRVM_G_FLOAT(OFS_PARM2))
4763 qsort(stringbuffer->strings, stringbuffer->num_strings, sizeof(char*), BufStr_SortStringsUP);
4765 qsort(stringbuffer->strings, stringbuffer->num_strings, sizeof(char*), BufStr_SortStringsDOWN);
4767 BufStr_Shrink(prog, stringbuffer);
4771 ========================
4773 concantenates all buffer string into one with "glue" separator and returns it as tempstring
4774 string buf_implode(float bufhandle, string glue) = #465;
4775 ========================
4777 void VM_buf_implode (prvm_prog_t *prog)
4779 prvm_stringbuffer_t *stringbuffer;
4780 char k[VM_STRINGTEMP_LENGTH];
4784 VM_SAFEPARMCOUNT(2, VM_buf_implode);
4786 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4787 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
4790 VM_Warning(prog, "VM_buf_implode: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), prog->name);
4793 if(!stringbuffer->num_strings)
4795 sep = PRVM_G_STRING(OFS_PARM1);
4797 for(l = i = 0;i < stringbuffer->num_strings;i++)
4799 if(stringbuffer->strings[i])
4801 l += (i > 0 ? strlen(sep) : 0) + strlen(stringbuffer->strings[i]);
4802 if (l >= sizeof(k) - 1)
4804 strlcat(k, sep, sizeof(k));
4805 strlcat(k, stringbuffer->strings[i], sizeof(k));
4808 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, k);
4812 ========================
4814 get a string from buffer, returns tempstring, dont str_unzone it!
4815 string bufstr_get(float bufhandle, float string_index) = #465;
4816 ========================
4818 void VM_bufstr_get (prvm_prog_t *prog)
4820 prvm_stringbuffer_t *stringbuffer;
4822 VM_SAFEPARMCOUNT(2, VM_bufstr_get);
4824 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
4825 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4828 VM_Warning(prog, "VM_bufstr_get: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), prog->name);
4831 strindex = (int)PRVM_G_FLOAT(OFS_PARM1);
4834 // VM_Warning(prog, "VM_bufstr_get: invalid string index %i used in %s\n", strindex, prog->name);
4837 if (strindex < stringbuffer->num_strings && stringbuffer->strings[strindex])
4838 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, stringbuffer->strings[strindex]);
4842 ========================
4844 copies a string into selected slot of buffer
4845 void bufstr_set(float bufhandle, float string_index, string str) = #466;
4846 ========================
4848 void VM_bufstr_set (prvm_prog_t *prog)
4852 prvm_stringbuffer_t *stringbuffer;
4855 VM_SAFEPARMCOUNT(3, VM_bufstr_set);
4857 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4860 VM_Warning(prog, "VM_bufstr_set: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), prog->name);
4863 strindex = (int)PRVM_G_FLOAT(OFS_PARM1);
4864 if(strindex < 0 || strindex >= 1000000) // huge number of strings
4866 VM_Warning(prog, "VM_bufstr_set: invalid string index %i used in %s\n", strindex, prog->name);
4870 BufStr_Expand(prog, stringbuffer, strindex);
4871 stringbuffer->num_strings = max(stringbuffer->num_strings, strindex + 1);
4873 if(stringbuffer->strings[strindex])
4874 Mem_Free(stringbuffer->strings[strindex]);
4875 stringbuffer->strings[strindex] = NULL;
4877 if(PRVM_G_INT(OFS_PARM2))
4879 // not the NULL string!
4880 news = PRVM_G_STRING(OFS_PARM2);
4881 alloclen = strlen(news) + 1;
4882 stringbuffer->strings[strindex] = (char *)Mem_Alloc(prog->progs_mempool, alloclen);
4883 memcpy(stringbuffer->strings[strindex], news, alloclen);
4886 BufStr_Shrink(prog, stringbuffer);
4890 ========================
4892 adds string to buffer in first free slot and returns its index
4893 "order == TRUE" means that string will be added after last "full" slot
4894 float bufstr_add(float bufhandle, string str, float order) = #467;
4895 ========================
4897 void VM_bufstr_add (prvm_prog_t *prog)
4899 int order, strindex;
4900 prvm_stringbuffer_t *stringbuffer;
4904 VM_SAFEPARMCOUNT(3, VM_bufstr_add);
4906 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4907 PRVM_G_FLOAT(OFS_RETURN) = -1;
4910 VM_Warning(prog, "VM_bufstr_add: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), prog->name);
4913 if(!PRVM_G_INT(OFS_PARM1)) // NULL string
4915 VM_Warning(prog, "VM_bufstr_add: can not add an empty string to buffer %i in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), prog->name);
4918 string = PRVM_G_STRING(OFS_PARM1);
4919 order = (int)PRVM_G_FLOAT(OFS_PARM2);
4921 strindex = stringbuffer->num_strings;
4923 for (strindex = 0;strindex < stringbuffer->num_strings;strindex++)
4924 if (stringbuffer->strings[strindex] == NULL)
4927 BufStr_Expand(prog, stringbuffer, strindex);
4929 stringbuffer->num_strings = max(stringbuffer->num_strings, strindex + 1);
4930 alloclen = strlen(string) + 1;
4931 stringbuffer->strings[strindex] = (char *)Mem_Alloc(prog->progs_mempool, alloclen);
4932 memcpy(stringbuffer->strings[strindex], string, alloclen);
4934 PRVM_G_FLOAT(OFS_RETURN) = strindex;
4938 ========================
4940 delete string from buffer
4941 void bufstr_free(float bufhandle, float string_index) = #468;
4942 ========================
4944 void VM_bufstr_free (prvm_prog_t *prog)
4947 prvm_stringbuffer_t *stringbuffer;
4948 VM_SAFEPARMCOUNT(2, VM_bufstr_free);
4950 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4953 VM_Warning(prog, "VM_bufstr_free: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), prog->name);
4956 i = (int)PRVM_G_FLOAT(OFS_PARM1);
4959 VM_Warning(prog, "VM_bufstr_free: invalid string index %i used in %s\n", i, prog->name);
4963 if (i < stringbuffer->num_strings)
4965 if(stringbuffer->strings[i])
4966 Mem_Free(stringbuffer->strings[i]);
4967 stringbuffer->strings[i] = NULL;
4970 BufStr_Shrink(prog, stringbuffer);
4979 void VM_buf_cvarlist(prvm_prog_t *prog)
4982 const char *partial, *antipartial;
4983 size_t len, antilen;
4985 qboolean ispattern, antiispattern;
4987 prvm_stringbuffer_t *stringbuffer;
4988 VM_SAFEPARMCOUNTRANGE(2, 3, VM_buf_cvarlist);
4990 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4993 VM_Warning(prog, "VM_bufstr_free: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), prog->name);
4997 partial = PRVM_G_STRING(OFS_PARM1);
5001 len = strlen(partial);
5004 antipartial = PRVM_G_STRING(OFS_PARM2);
5010 antilen = strlen(antipartial);
5012 for (n = 0;n < stringbuffer->num_strings;n++)
5013 if (stringbuffer->strings[n])
5014 Mem_Free(stringbuffer->strings[n]);
5015 if (stringbuffer->strings)
5016 Mem_Free(stringbuffer->strings);
5017 stringbuffer->strings = NULL;
5019 ispattern = partial && (strchr(partial, '*') || strchr(partial, '?'));
5020 antiispattern = antipartial && (strchr(antipartial, '*') || strchr(antipartial, '?'));
5023 for(cvar = cvar_vars; cvar; cvar = cvar->next)
5025 if(len && (ispattern ? !matchpattern_with_separator(cvar->name, partial, false, "", false) : strncmp(partial, cvar->name, len)))
5028 if(antilen && (antiispattern ? matchpattern_with_separator(cvar->name, antipartial, false, "", false) : !strncmp(antipartial, cvar->name, antilen)))
5034 stringbuffer->max_strings = stringbuffer->num_strings = n;
5035 if (stringbuffer->max_strings)
5036 stringbuffer->strings = (char **)Mem_Alloc(prog->progs_mempool, sizeof(stringbuffer->strings[0]) * stringbuffer->max_strings);
5039 for(cvar = cvar_vars; cvar; cvar = cvar->next)
5041 if(len && (ispattern ? !matchpattern_with_separator(cvar->name, partial, false, "", false) : strncmp(partial, cvar->name, len)))
5044 if(antilen && (antiispattern ? matchpattern_with_separator(cvar->name, antipartial, false, "", false) : !strncmp(antipartial, cvar->name, antilen)))
5047 alloclen = strlen(cvar->name) + 1;
5048 stringbuffer->strings[n] = (char *)Mem_Alloc(prog->progs_mempool, alloclen);
5049 memcpy(stringbuffer->strings[n], cvar->name, alloclen);
5064 This was a major timewaster in progs, so it was converted to C
5067 void VM_changeyaw (prvm_prog_t *prog)
5070 float ideal, current, move, speed;
5072 // this is called (VERY HACKISHLY) by VM_SV_MoveToGoal, so it can not use any
5073 // parameters because they are the parameters to VM_SV_MoveToGoal, not this
5074 //VM_SAFEPARMCOUNT(0, VM_changeyaw);
5076 ent = PRVM_PROG_TO_EDICT(PRVM_gameglobaledict(self));
5077 if (ent == prog->edicts)
5079 VM_Warning(prog, "changeyaw: can not modify world entity\n");
5082 if (ent->priv.server->free)
5084 VM_Warning(prog, "changeyaw: can not modify free entity\n");
5087 current = PRVM_gameedictvector(ent, angles)[1];
5088 current = ANGLEMOD(current);
5089 ideal = PRVM_gameedictfloat(ent, ideal_yaw);
5090 speed = PRVM_gameedictfloat(ent, yaw_speed);
5092 if (current == ideal)
5094 move = ideal - current;
5095 if (ideal > current)
5117 PRVM_gameedictvector(ent, angles)[1] = ANGLEMOD(current);
5125 void VM_changepitch (prvm_prog_t *prog)
5128 float ideal, current, move, speed;
5130 VM_SAFEPARMCOUNT(1, VM_changepitch);
5132 ent = PRVM_G_EDICT(OFS_PARM0);
5133 if (ent == prog->edicts)
5135 VM_Warning(prog, "changepitch: can not modify world entity\n");
5138 if (ent->priv.server->free)
5140 VM_Warning(prog, "changepitch: can not modify free entity\n");
5143 current = PRVM_gameedictvector(ent, angles)[0];
5144 current = ANGLEMOD(current);
5145 ideal = PRVM_gameedictfloat(ent, idealpitch);
5146 speed = PRVM_gameedictfloat(ent, pitch_speed);
5148 if (current == ideal)
5150 move = ideal - current;
5151 if (ideal > current)
5173 PRVM_gameedictvector(ent, angles)[0] = ANGLEMOD(current);
5177 void VM_uncolorstring (prvm_prog_t *prog)
5179 char szNewString[VM_STRINGTEMP_LENGTH];
5180 const char *szString;
5183 VM_SAFEPARMCOUNT(1, VM_uncolorstring);
5184 szString = PRVM_G_STRING(OFS_PARM0);
5185 COM_StringDecolorize(szString, 0, szNewString, sizeof(szNewString), TRUE);
5186 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, szNewString);
5190 // #221 float(string str, string sub[, float startpos]) strstrofs (FTE_STRINGS)
5191 //strstr, without generating a new string. Use in conjunction with FRIK_FILE's substring for more similar strstr.
5192 void VM_strstrofs (prvm_prog_t *prog)
5194 const char *instr, *match;
5196 VM_SAFEPARMCOUNTRANGE(2, 3, VM_strstrofs);
5197 instr = PRVM_G_STRING(OFS_PARM0);
5198 match = PRVM_G_STRING(OFS_PARM1);
5199 firstofs = (prog->argc > 2)?(int)PRVM_G_FLOAT(OFS_PARM2):0;
5200 firstofs = u8_bytelen(instr, firstofs);
5202 if (firstofs && (firstofs < 0 || firstofs > (int)strlen(instr)))
5204 PRVM_G_FLOAT(OFS_RETURN) = -1;
5208 match = strstr(instr+firstofs, match);
5210 PRVM_G_FLOAT(OFS_RETURN) = -1;
5212 PRVM_G_FLOAT(OFS_RETURN) = u8_strnlen(instr, match-instr);
5215 //#222 string(string s, float index) str2chr (FTE_STRINGS)
5216 void VM_str2chr (prvm_prog_t *prog)
5221 VM_SAFEPARMCOUNT(2, VM_str2chr);
5222 s = PRVM_G_STRING(OFS_PARM0);
5223 index = u8_bytelen(s, (int)PRVM_G_FLOAT(OFS_PARM1));
5225 if((unsigned)index < strlen(s))
5227 if (utf8_enable.integer)
5228 ch = u8_getchar_noendptr(s + index);
5230 ch = (unsigned char)s[index];
5231 PRVM_G_FLOAT(OFS_RETURN) = ch;
5234 PRVM_G_FLOAT(OFS_RETURN) = 0;
5237 //#223 string(float c, ...) chr2str (FTE_STRINGS)
5238 void VM_chr2str (prvm_prog_t *prog)
5243 VM_SAFEPARMCOUNTRANGE(0, 8, VM_chr2str);
5244 for(i = 0;i < prog->argc && i < (int)sizeof(t) - 1;i++)
5245 t[i] = (unsigned char)PRVM_G_FLOAT(OFS_PARM0+i*3);
5247 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, t);
5252 VM_SAFEPARMCOUNTRANGE(0, 8, VM_chr2str);
5253 for(i = 0; i < prog->argc && len < sizeof(t)-1; ++i)
5254 len += u8_fromchar((Uchar)PRVM_G_FLOAT(OFS_PARM0+i*3), t + len, sizeof(t)-1);
5256 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, t);
5259 static int chrconv_number(int i, int base, int conv)
5284 static int chrconv_punct(int i, int base, int conv)
5302 static int chrchar_alpha(int i, int basec, int baset, int convc, int convt, int charnum)
5304 //convert case and colour seperatly...
5321 baset = 128*((charnum&1) == (convt-5));
5337 return i + basec + baset;
5339 // #224 string(float ccase, float calpha, float cnum, string s, ...) strconv (FTE_STRINGS)
5340 //bulk convert a string. change case or colouring.
5341 void VM_strconv (prvm_prog_t *prog)
5343 int ccase, redalpha, rednum, len, i;
5344 unsigned char resbuf[VM_STRINGTEMP_LENGTH];
5345 unsigned char *result = resbuf;
5347 VM_SAFEPARMCOUNTRANGE(3, 8, VM_strconv);
5349 ccase = (int) PRVM_G_FLOAT(OFS_PARM0); //0 same, 1 lower, 2 upper
5350 redalpha = (int) PRVM_G_FLOAT(OFS_PARM1); //0 same, 1 white, 2 red, 5 alternate, 6 alternate-alternate
5351 rednum = (int) PRVM_G_FLOAT(OFS_PARM2); //0 same, 1 white, 2 red, 3 redspecial, 4 whitespecial, 5 alternate, 6 alternate-alternate
5352 VM_VarString(prog, 3, (char *) resbuf, sizeof(resbuf));
5353 len = strlen((char *) resbuf);
5355 for (i = 0; i < len; i++, result++) //should this be done backwards?
5357 if (*result >= '0' && *result <= '9') //normal numbers...
5358 *result = chrconv_number(*result, '0', rednum);
5359 else if (*result >= '0'+128 && *result <= '9'+128)
5360 *result = chrconv_number(*result, '0'+128, rednum);
5361 else if (*result >= '0'+128-30 && *result <= '9'+128-30)
5362 *result = chrconv_number(*result, '0'+128-30, rednum);
5363 else if (*result >= '0'-30 && *result <= '9'-30)
5364 *result = chrconv_number(*result, '0'-30, rednum);
5366 else if (*result >= 'a' && *result <= 'z') //normal numbers...
5367 *result = chrchar_alpha(*result, 'a', 0, ccase, redalpha, i);
5368 else if (*result >= 'A' && *result <= 'Z') //normal numbers...
5369 *result = chrchar_alpha(*result, 'A', 0, ccase, redalpha, i);
5370 else if (*result >= 'a'+128 && *result <= 'z'+128) //normal numbers...
5371 *result = chrchar_alpha(*result, 'a', 128, ccase, redalpha, i);
5372 else if (*result >= 'A'+128 && *result <= 'Z'+128) //normal numbers...
5373 *result = chrchar_alpha(*result, 'A', 128, ccase, redalpha, i);
5375 else if ((*result & 127) < 16 || !redalpha) //special chars..
5377 else if (*result < 128)
5378 *result = chrconv_punct(*result, 0, redalpha);
5380 *result = chrconv_punct(*result, 128, redalpha);
5384 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, (char *) resbuf);
5387 // #225 string(float chars, string s, ...) strpad (FTE_STRINGS)
5388 void VM_strpad (prvm_prog_t *prog)
5390 char src[VM_STRINGTEMP_LENGTH];
5391 char destbuf[VM_STRINGTEMP_LENGTH];
5393 VM_SAFEPARMCOUNTRANGE(1, 8, VM_strpad);
5394 pad = (int) PRVM_G_FLOAT(OFS_PARM0);
5395 VM_VarString(prog, 1, src, sizeof(src));
5397 // note: < 0 = left padding, > 0 = right padding,
5398 // this is reverse logic of printf!
5399 dpsnprintf(destbuf, sizeof(destbuf), "%*s", -pad, src);
5401 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, destbuf);
5404 // #226 string(string info, string key, string value, ...) infoadd (FTE_STRINGS)
5405 //uses qw style \key\value strings
5406 void VM_infoadd (prvm_prog_t *prog)
5408 const char *info, *key;
5409 char value[VM_STRINGTEMP_LENGTH];
5410 char temp[VM_STRINGTEMP_LENGTH];
5412 VM_SAFEPARMCOUNTRANGE(2, 8, VM_infoadd);
5413 info = PRVM_G_STRING(OFS_PARM0);
5414 key = PRVM_G_STRING(OFS_PARM1);
5415 VM_VarString(prog, 2, value, sizeof(value));
5417 strlcpy(temp, info, VM_STRINGTEMP_LENGTH);
5419 InfoString_SetValue(temp, VM_STRINGTEMP_LENGTH, key, value);
5421 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, temp);
5424 // #227 string(string info, string key) infoget (FTE_STRINGS)
5425 //uses qw style \key\value strings
5426 void VM_infoget (prvm_prog_t *prog)
5430 char value[VM_STRINGTEMP_LENGTH];
5432 VM_SAFEPARMCOUNT(2, VM_infoget);
5433 info = PRVM_G_STRING(OFS_PARM0);
5434 key = PRVM_G_STRING(OFS_PARM1);
5436 InfoString_GetValue(info, key, value, VM_STRINGTEMP_LENGTH);
5438 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, value);
5441 //#228 float(string s1, string s2, float len) strncmp (FTE_STRINGS)
5442 // also float(string s1, string s2) strcmp (FRIK_FILE)
5443 void VM_strncmp (prvm_prog_t *prog)
5445 const char *s1, *s2;
5446 VM_SAFEPARMCOUNTRANGE(2, 3, VM_strncmp);
5447 s1 = PRVM_G_STRING(OFS_PARM0);
5448 s2 = PRVM_G_STRING(OFS_PARM1);
5451 PRVM_G_FLOAT(OFS_RETURN) = strncmp(s1, s2, (size_t)PRVM_G_FLOAT(OFS_PARM2));
5455 PRVM_G_FLOAT(OFS_RETURN) = strcmp(s1, s2);
5459 // #229 float(string s1, string s2) strcasecmp (FTE_STRINGS)
5460 // #230 float(string s1, string s2, float len) strncasecmp (FTE_STRINGS)
5461 void VM_strncasecmp (prvm_prog_t *prog)
5463 const char *s1, *s2;
5464 VM_SAFEPARMCOUNTRANGE(2, 3, VM_strncasecmp);
5465 s1 = PRVM_G_STRING(OFS_PARM0);
5466 s2 = PRVM_G_STRING(OFS_PARM1);
5469 PRVM_G_FLOAT(OFS_RETURN) = strncasecmp(s1, s2, (size_t)PRVM_G_FLOAT(OFS_PARM2));
5473 PRVM_G_FLOAT(OFS_RETURN) = strcasecmp(s1, s2);
5477 // #494 float(float caseinsensitive, string s, ...) crc16
5478 void VM_crc16(prvm_prog_t *prog)
5481 char s[VM_STRINGTEMP_LENGTH];
5482 VM_SAFEPARMCOUNTRANGE(2, 8, VM_crc16);
5483 insensitive = PRVM_G_FLOAT(OFS_PARM0);
5484 VM_VarString(prog, 1, s, sizeof(s));
5485 PRVM_G_FLOAT(OFS_RETURN) = (unsigned short) ((insensitive ? CRC_Block_CaseInsensitive : CRC_Block) ((unsigned char *) s, strlen(s)));
5488 // #639 float(string digest, string data, ...) digest_hex
5489 void VM_digest_hex(prvm_prog_t *prog)
5497 char s[VM_STRINGTEMP_LENGTH];
5500 VM_SAFEPARMCOUNTRANGE(2, 8, VM_digest_hex);
5501 digest = PRVM_G_STRING(OFS_PARM0);
5504 VM_VarString(prog, 1, s, sizeof(s));
5509 if(!strcmp(digest, "MD4"))
5512 mdfour((unsigned char *) out, (unsigned char *) s, len);
5514 else if(!strcmp(digest, "SHA256") && Crypto_Available())
5517 sha256((unsigned char *) out, (unsigned char *) s, len);
5519 // no warning needed on mismatch - we return string_null to QC
5524 static const char *hexmap = "0123456789abcdef";
5525 for(i = 0; i < outlen; ++i)
5527 outhex[2*i] = hexmap[(out[i] >> 4) & 15];
5528 outhex[2*i+1] = hexmap[(out[i] >> 0) & 15];
5531 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, outhex);
5534 PRVM_G_INT(OFS_RETURN) = 0;
5537 void VM_wasfreed (prvm_prog_t *prog)
5539 VM_SAFEPARMCOUNT(1, VM_wasfreed);
5540 PRVM_G_FLOAT(OFS_RETURN) = PRVM_G_EDICT(OFS_PARM0)->priv.required->free;
5543 void VM_SetTraceGlobals(prvm_prog_t *prog, const trace_t *trace)
5545 PRVM_gameglobalfloat(trace_allsolid) = trace->allsolid;
5546 PRVM_gameglobalfloat(trace_startsolid) = trace->startsolid;
5547 PRVM_gameglobalfloat(trace_fraction) = trace->fraction;
5548 PRVM_gameglobalfloat(trace_inwater) = trace->inwater;
5549 PRVM_gameglobalfloat(trace_inopen) = trace->inopen;
5550 VectorCopy(trace->endpos, PRVM_gameglobalvector(trace_endpos));
5551 VectorCopy(trace->plane.normal, PRVM_gameglobalvector(trace_plane_normal));
5552 PRVM_gameglobalfloat(trace_plane_dist) = trace->plane.dist;
5553 PRVM_gameglobaledict(trace_ent) = PRVM_EDICT_TO_PROG(trace->ent ? trace->ent : prog->edicts);
5554 PRVM_gameglobalfloat(trace_dpstartcontents) = trace->startsupercontents;
5555 PRVM_gameglobalfloat(trace_dphitcontents) = trace->hitsupercontents;
5556 PRVM_gameglobalfloat(trace_dphitq3surfaceflags) = trace->hitq3surfaceflags;
5557 PRVM_gameglobalstring(trace_dphittexturename) = trace->hittexture ? PRVM_SetTempString(prog, trace->hittexture->name) : 0;
5560 void VM_ClearTraceGlobals(prvm_prog_t *prog)
5562 // clean up all trace globals when leaving the VM (anti-triggerbot safeguard)
5563 PRVM_gameglobalfloat(trace_allsolid) = 0;
5564 PRVM_gameglobalfloat(trace_startsolid) = 0;
5565 PRVM_gameglobalfloat(trace_fraction) = 0;
5566 PRVM_gameglobalfloat(trace_inwater) = 0;
5567 PRVM_gameglobalfloat(trace_inopen) = 0;
5568 VectorClear(PRVM_gameglobalvector(trace_endpos));
5569 VectorClear(PRVM_gameglobalvector(trace_plane_normal));
5570 PRVM_gameglobalfloat(trace_plane_dist) = 0;
5571 PRVM_gameglobaledict(trace_ent) = PRVM_EDICT_TO_PROG(prog->edicts);
5572 PRVM_gameglobalfloat(trace_dpstartcontents) = 0;
5573 PRVM_gameglobalfloat(trace_dphitcontents) = 0;
5574 PRVM_gameglobalfloat(trace_dphitq3surfaceflags) = 0;
5575 PRVM_gameglobalstring(trace_dphittexturename) = 0;
5580 void VM_Cmd_Init(prvm_prog_t *prog)
5582 // only init the stuff for the current prog
5583 VM_Files_Init(prog);
5584 VM_Search_Init(prog);
5587 void VM_Cmd_Reset(prvm_prog_t *prog)
5589 CL_PurgeOwner( MENUOWNER );
5590 VM_Search_Reset(prog);
5591 VM_Files_CloseAll(prog);
5594 // #510 string(string input, ...) uri_escape (DP_QC_URI_ESCAPE)
5595 // does URI escaping on a string (replace evil stuff by %AB escapes)
5596 void VM_uri_escape (prvm_prog_t *prog)
5598 char src[VM_STRINGTEMP_LENGTH];
5599 char dest[VM_STRINGTEMP_LENGTH];
5601 static const char *hex = "0123456789ABCDEF";
5603 VM_SAFEPARMCOUNTRANGE(1, 8, VM_uri_escape);
5604 VM_VarString(prog, 0, src, sizeof(src));
5606 for(p = src, q = dest; *p && q < dest + sizeof(dest) - 3; ++p)
5608 if((*p >= 'A' && *p <= 'Z')
5609 || (*p >= 'a' && *p <= 'z')
5610 || (*p >= '0' && *p <= '9')
5611 || (*p == '-') || (*p == '_') || (*p == '.')
5612 || (*p == '!') || (*p == '~')
5613 || (*p == '\'') || (*p == '(') || (*p == ')'))
5618 *q++ = hex[(*(unsigned char *)p >> 4) & 0xF];
5619 *q++ = hex[ *(unsigned char *)p & 0xF];
5624 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, dest);
5627 // #510 string(string input, ...) uri_unescape (DP_QC_URI_ESCAPE)
5628 // does URI unescaping on a string (get back the evil stuff)
5629 void VM_uri_unescape (prvm_prog_t *prog)
5631 char src[VM_STRINGTEMP_LENGTH];
5632 char dest[VM_STRINGTEMP_LENGTH];
5636 VM_SAFEPARMCOUNTRANGE(1, 8, VM_uri_unescape);
5637 VM_VarString(prog, 0, src, sizeof(src));
5639 for(p = src, q = dest; *p; ) // no need to check size, because unescape can't expand
5643 if(p[1] >= '0' && p[1] <= '9')
5645 else if(p[1] >= 'a' && p[1] <= 'f')
5646 hi = p[1] - 'a' + 10;
5647 else if(p[1] >= 'A' && p[1] <= 'F')
5648 hi = p[1] - 'A' + 10;
5651 if(p[2] >= '0' && p[2] <= '9')
5653 else if(p[2] >= 'a' && p[2] <= 'f')
5654 lo = p[2] - 'a' + 10;
5655 else if(p[2] >= 'A' && p[2] <= 'F')
5656 lo = p[2] - 'A' + 10;
5659 if(hi != 0 || lo != 0) // don't unescape NUL bytes
5660 *q++ = (char) (hi * 0x10 + lo);
5671 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, dest);
5674 // #502 string(string filename) whichpack (DP_QC_WHICHPACK)
5675 // returns the name of the pack containing a file, or "" if it is not in any pack (but local or non-existant)
5676 void VM_whichpack (prvm_prog_t *prog)
5678 const char *fn, *pack;
5680 VM_SAFEPARMCOUNT(1, VM_whichpack);
5681 fn = PRVM_G_STRING(OFS_PARM0);
5682 pack = FS_WhichPack(fn);
5684 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, pack ? pack : "");
5692 char buffer[MAX_INPUTLINE];
5693 unsigned char *postdata; // free when uri_to_prog_t is freed
5695 char *sigdata; // free when uri_to_prog_t is freed
5700 static void uri_to_string_callback(int status, size_t length_received, unsigned char *buffer, void *cbdata)
5703 uri_to_prog_t *handle = (uri_to_prog_t *) cbdata;
5705 prog = handle->prog;
5708 // curl reply came too late... so just drop it
5709 if(handle->postdata)
5710 Z_Free(handle->postdata);
5712 Z_Free(handle->sigdata);
5717 if((prog->starttime == handle->starttime) && (PRVM_allfunction(URI_Get_Callback)))
5719 if(length_received >= sizeof(handle->buffer))
5720 length_received = sizeof(handle->buffer) - 1;
5721 handle->buffer[length_received] = 0;
5723 PRVM_G_FLOAT(OFS_PARM0) = handle->id;
5724 PRVM_G_FLOAT(OFS_PARM1) = status;
5725 PRVM_G_INT(OFS_PARM2) = PRVM_SetTempString(prog, handle->buffer);
5726 prog->ExecuteProgram(prog, PRVM_allfunction(URI_Get_Callback), "QC function URI_Get_Callback is missing");
5729 if(handle->postdata)
5730 Z_Free(handle->postdata);
5732 Z_Free(handle->sigdata);
5736 // 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
5737 // 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
5738 void VM_uri_get (prvm_prog_t *prog)
5743 uri_to_prog_t *handle;
5744 const char *posttype = NULL;
5745 const char *postseparator = NULL;
5746 int poststringbuffer = -1;
5748 const char *query_string = NULL;
5751 if(!PRVM_allfunction(URI_Get_Callback))
5752 prog->error_cmd("uri_get called by %s without URI_Get_Callback defined", prog->name);
5754 VM_SAFEPARMCOUNTRANGE(2, 6, VM_uri_get);
5756 url = PRVM_G_STRING(OFS_PARM0);
5757 id = PRVM_G_FLOAT(OFS_PARM1);
5759 posttype = PRVM_G_STRING(OFS_PARM2);
5761 postseparator = PRVM_G_STRING(OFS_PARM3);
5763 poststringbuffer = PRVM_G_FLOAT(OFS_PARM4);
5765 postkeyid = PRVM_G_FLOAT(OFS_PARM5);
5766 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!
5768 query_string = strchr(url, '?');
5771 lq = query_string ? strlen(query_string) : 0;
5773 handle->prog = prog;
5774 handle->starttime = prog->starttime;
5776 if(postseparator && posttype && *posttype)
5778 size_t l = strlen(postseparator);
5779 if(poststringbuffer >= 0)
5784 prvm_stringbuffer_t *stringbuffer;
5785 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, poststringbuffer);
5788 VM_Warning(prog, "uri_get: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), prog->name);
5792 for(i = 0;i < stringbuffer->num_strings;i++)
5796 if(stringbuffer->strings[i])
5797 ltotal += strlen(stringbuffer->strings[i]);
5799 handle->postdata = (unsigned char *)Z_Malloc(ltotal + 1 + lq);
5800 handle->postlen = ltotal;
5802 for(i = 0;i < stringbuffer->num_strings;i++)
5806 memcpy(handle->postdata + ltotal, postseparator, l);
5809 if(stringbuffer->strings[i])
5811 memcpy(handle->postdata + ltotal, stringbuffer->strings[i], strlen(stringbuffer->strings[i]));
5812 ltotal += strlen(stringbuffer->strings[i]);
5815 if(ltotal != handle->postlen)
5816 prog->error_cmd("%s: string buffer content size mismatch, possible overrun", prog->name);
5820 handle->postdata = (unsigned char *)Z_Malloc(l + 1 + lq);
5821 handle->postlen = l;
5822 memcpy(handle->postdata, postseparator, l);
5824 handle->postdata[handle->postlen] = 0;
5826 memcpy(handle->postdata + handle->postlen + 1, query_string, lq);
5829 // POST: we sign postdata \0 query string
5831 handle->sigdata = (char *)Z_Malloc(8192);
5832 strlcpy(handle->sigdata, "X-D0-Blind-ID-Detached-Signature: ", 8192);
5833 l = strlen(handle->sigdata);
5834 handle->siglen = Crypto_SignDataDetached(handle->postdata, handle->postlen + 1 + lq, postkeyid, handle->sigdata + l, 8192 - l);
5837 Z_Free(handle->sigdata);
5838 handle->sigdata = NULL;
5841 ll = base64_encode((unsigned char *) (handle->sigdata + l), handle->siglen, 8192 - l - 1);
5844 Z_Free(handle->sigdata);
5845 handle->sigdata = NULL;
5848 handle->siglen = l + ll;
5849 handle->sigdata[handle->siglen] = 0;
5852 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);
5856 if(postkeyid >= 0 && query_string)
5858 // GET: we sign JUST the query string
5860 handle->sigdata = (char *)Z_Malloc(8192);
5861 strlcpy(handle->sigdata, "X-D0-Blind-ID-Detached-Signature: ", 8192);
5862 l = strlen(handle->sigdata);
5863 handle->siglen = Crypto_SignDataDetached(query_string, lq, postkeyid, handle->sigdata + l, 8192 - l);
5866 Z_Free(handle->sigdata);
5867 handle->sigdata = NULL;
5870 ll = base64_encode((unsigned char *) (handle->sigdata + l), handle->siglen, 8192 - l - 1);
5873 Z_Free(handle->sigdata);
5874 handle->sigdata = NULL;
5877 handle->siglen = l + ll;
5878 handle->sigdata[handle->siglen] = 0;
5881 handle->postdata = NULL;
5882 handle->postlen = 0;
5883 ret = Curl_Begin_ToMemory(url, 0, (unsigned char *) handle->buffer, sizeof(handle->buffer), uri_to_string_callback, handle);
5887 PRVM_G_INT(OFS_RETURN) = 1;
5891 if(handle->postdata)
5892 Z_Free(handle->postdata);
5894 Z_Free(handle->sigdata);
5896 PRVM_G_INT(OFS_RETURN) = 0;
5900 void VM_netaddress_resolve (prvm_prog_t *prog)
5903 char normalized[128];
5905 lhnetaddress_t addr;
5907 VM_SAFEPARMCOUNTRANGE(1, 2, VM_netaddress_resolve);
5909 ip = PRVM_G_STRING(OFS_PARM0);
5912 port = (int) PRVM_G_FLOAT(OFS_PARM1);
5914 if(LHNETADDRESS_FromString(&addr, ip, port) && LHNETADDRESS_ToString(&addr, normalized, sizeof(normalized), prog->argc > 1))
5915 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, normalized);
5917 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, "");
5920 //string(prvm_prog_t *prog) getextresponse = #624; // returns the next extResponse packet that was sent to this client
5921 void VM_CL_getextresponse (prvm_prog_t *prog)
5923 VM_SAFEPARMCOUNT(0,VM_argv);
5925 if (cl_net_extresponse_count <= 0)
5926 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
5930 --cl_net_extresponse_count;
5931 first = (cl_net_extresponse_last + NET_EXTRESPONSE_MAX - cl_net_extresponse_count) % NET_EXTRESPONSE_MAX;
5932 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, cl_net_extresponse[first]);
5936 void VM_SV_getextresponse (prvm_prog_t *prog)
5938 VM_SAFEPARMCOUNT(0,VM_argv);
5940 if (sv_net_extresponse_count <= 0)
5941 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
5945 --sv_net_extresponse_count;
5946 first = (sv_net_extresponse_last + NET_EXTRESPONSE_MAX - sv_net_extresponse_count) % NET_EXTRESPONSE_MAX;
5947 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, sv_net_extresponse[first]);
5953 Common functions between menu.dat and clsprogs
5957 //#349 float() isdemo
5958 void VM_CL_isdemo (prvm_prog_t *prog)
5960 VM_SAFEPARMCOUNT(0, VM_CL_isdemo);
5961 PRVM_G_FLOAT(OFS_RETURN) = cls.demoplayback;
5964 //#355 float() videoplaying
5965 void VM_CL_videoplaying (prvm_prog_t *prog)
5967 VM_SAFEPARMCOUNT(0, VM_CL_videoplaying);
5968 PRVM_G_FLOAT(OFS_RETURN) = cl_videoplaying;
5975 callfunction(...,string function_name)
5979 void VM_callfunction(prvm_prog_t *prog)
5984 VM_SAFEPARMCOUNTRANGE(1, 8, VM_callfunction);
5986 s = PRVM_G_STRING(OFS_PARM0+(prog->argc - 1)*3);
5988 VM_CheckEmptyString(prog, s);
5990 func = PRVM_ED_FindFunction(prog, s);
5993 prog->error_cmd("VM_callfunciton: function %s not found !", s);
5994 else if (func->first_statement < 0)
5996 // negative statements are built in functions
5997 int builtinnumber = -func->first_statement;
5998 prog->xfunction->builtinsprofile++;
5999 if (builtinnumber < prog->numbuiltins && prog->builtins[builtinnumber])
6000 prog->builtins[builtinnumber](prog);
6002 prog->error_cmd("No such builtin #%i in %s; most likely cause: outdated engine build. Try updating!", builtinnumber, prog->name);
6004 else if(func - prog->functions > 0)
6007 prog->ExecuteProgram(prog, func - prog->functions,"");
6016 float isfunction(string function_name)
6019 void VM_isfunction(prvm_prog_t *prog)
6024 VM_SAFEPARMCOUNT(1, VM_isfunction);
6026 s = PRVM_G_STRING(OFS_PARM0);
6028 VM_CheckEmptyString(prog, s);
6030 func = PRVM_ED_FindFunction(prog, s);
6033 PRVM_G_FLOAT(OFS_RETURN) = false;
6035 PRVM_G_FLOAT(OFS_RETURN) = true;
6042 string sprintf(string format, ...)
6046 void VM_sprintf(prvm_prog_t *prog)
6049 char outbuf[MAX_INPUTLINE];
6050 char *o = outbuf, *end = outbuf + sizeof(outbuf), *err;
6052 int width, precision, thisarg, flags;
6056 static int dummyivec[3] = {0, 0, 0};
6057 static float dummyvec[3] = {0, 0, 0};
6060 #define PRINTF_ALTERNATE 1
6061 #define PRINTF_ZEROPAD 2
6062 #define PRINTF_LEFT 4
6063 #define PRINTF_SPACEPOSITIVE 8
6064 #define PRINTF_SIGNPOSITIVE 16
6068 s = PRVM_G_STRING(OFS_PARM0);
6070 #define GETARG_FLOAT(a) (((a)>=1 && (a)<prog->argc) ? (PRVM_G_FLOAT(OFS_PARM0 + 3 * (a))) : 0)
6071 #define GETARG_VECTOR(a) (((a)>=1 && (a)<prog->argc) ? (PRVM_G_VECTOR(OFS_PARM0 + 3 * (a))) : dummyvec)
6072 #define GETARG_INT(a) (((a)>=1 && (a)<prog->argc) ? (PRVM_G_INT(OFS_PARM0 + 3 * (a))) : 0)
6073 #define GETARG_INTVECTOR(a) (((a)>=1 && (a)<prog->argc) ? ((int*) PRVM_G_VECTOR(OFS_PARM0 + 3 * (a))) : dummyivec)
6074 #define GETARG_STRING(a) (((a)>=1 && (a)<prog->argc) ? (PRVM_G_STRING(OFS_PARM0 + 3 * (a))) : "")
6089 // complete directive format:
6098 // is number following?
6099 if(*s >= '0' && *s <= '9')
6101 width = strtol(s, &err, 10);
6104 VM_Warning(prog, "VM_sprintf: invalid directive in %s: %s\n", prog->name, s0);
6117 flags |= PRINTF_ZEROPAD;
6119 width = -1; // it was just a flag
6131 case '#': flags |= PRINTF_ALTERNATE; break;
6132 case '0': flags |= PRINTF_ZEROPAD; break;
6133 case '-': flags |= PRINTF_LEFT; break;
6134 case ' ': flags |= PRINTF_SPACEPOSITIVE; break;
6135 case '+': flags |= PRINTF_SIGNPOSITIVE; break;
6145 if(*s >= '0' && *s <= '9')
6147 width = strtol(s, &err, 10);
6148 if(!err || *err != '$')
6150 VM_Warning(prog, "VM_sprintf: invalid directive in %s: %s\n", prog->name, s0);
6157 width = GETARG_FLOAT(width);
6160 flags |= PRINTF_LEFT;
6164 else if(*s >= '0' && *s <= '9')
6166 width = strtol(s, &err, 10);
6169 VM_Warning(prog, "VM_sprintf: invalid directive in %s: %s\n", prog->name, s0);
6175 flags |= PRINTF_LEFT;
6179 // otherwise width stays -1
6188 if(*s >= '0' && *s <= '9')
6190 precision = strtol(s, &err, 10);
6191 if(!err || *err != '$')
6193 VM_Warning(prog, "VM_sprintf: invalid directive in %s: %s\n", prog->name, s0);
6199 precision = argpos++;
6200 precision = GETARG_FLOAT(precision);
6202 else if(*s >= '0' && *s <= '9')
6204 precision = strtol(s, &err, 10);
6207 VM_Warning(prog, "VM_sprintf: invalid directive in %s: %s\n", prog->name, s0);
6214 VM_Warning(prog, "VM_sprintf: invalid directive in %s: %s\n", prog->name, s0);
6223 case 'h': isfloat = 1; break;
6224 case 'l': isfloat = 0; break;
6225 case 'L': isfloat = 0; break;
6236 // now s points to the final directive char and is no longer changed
6251 if(*s != 's' && *s != 'c')
6252 if(flags & PRINTF_ALTERNATE) *f++ = '#';
6253 if(flags & PRINTF_ZEROPAD) *f++ = '0';
6254 if(flags & PRINTF_LEFT) *f++ = '-';
6255 if(flags & PRINTF_SPACEPOSITIVE) *f++ = ' ';
6256 if(flags & PRINTF_SIGNPOSITIVE) *f++ = '+';
6266 if(width < 0) // not set
6272 if(precision < 0) // not set
6273 o += dpsnprintf(o, end - o, formatbuf, width, (isfloat ? (int) GETARG_FLOAT(thisarg) : (int) GETARG_INT(thisarg)));
6275 o += dpsnprintf(o, end - o, formatbuf, width, precision, (isfloat ? (int) GETARG_FLOAT(thisarg) : (int) GETARG_INT(thisarg)));
6277 case 'o': case 'u': case 'x': case 'X':
6278 if(precision < 0) // not set
6279 o += dpsnprintf(o, end - o, formatbuf, width, (isfloat ? (unsigned int) GETARG_FLOAT(thisarg) : (unsigned int) GETARG_INT(thisarg)));
6281 o += dpsnprintf(o, end - o, formatbuf, width, precision, (isfloat ? (unsigned int) GETARG_FLOAT(thisarg) : (unsigned int) GETARG_INT(thisarg)));
6283 case 'e': case 'E': case 'f': case 'F': case 'g': case 'G':
6284 if(precision < 0) // not set
6285 o += dpsnprintf(o, end - o, formatbuf, width, (isfloat ? (double) GETARG_FLOAT(thisarg) : (double) GETARG_INT(thisarg)));
6287 o += dpsnprintf(o, end - o, formatbuf, width, precision, (isfloat ? (double) GETARG_FLOAT(thisarg) : (double) GETARG_INT(thisarg)));
6291 if(precision < 0) // not set
6292 o += dpsnprintf(o, end - o, va(vabuf, sizeof(vabuf), "%s %s %s", /* NESTED SPRINTF IS NESTED */ formatbuf, formatbuf, formatbuf),
6293 width, (isfloat ? (double) GETARG_VECTOR(thisarg)[0] : (double) GETARG_INTVECTOR(thisarg)[0]),
6294 width, (isfloat ? (double) GETARG_VECTOR(thisarg)[1] : (double) GETARG_INTVECTOR(thisarg)[1]),
6295 width, (isfloat ? (double) GETARG_VECTOR(thisarg)[2] : (double) GETARG_INTVECTOR(thisarg)[2])
6298 o += dpsnprintf(o, end - o, va(vabuf, sizeof(vabuf), "%s %s %s", /* NESTED SPRINTF IS NESTED */ formatbuf, formatbuf, formatbuf),
6299 width, precision, (isfloat ? (double) GETARG_VECTOR(thisarg)[0] : (double) GETARG_INTVECTOR(thisarg)[0]),
6300 width, precision, (isfloat ? (double) GETARG_VECTOR(thisarg)[1] : (double) GETARG_INTVECTOR(thisarg)[1]),
6301 width, precision, (isfloat ? (double) GETARG_VECTOR(thisarg)[2] : (double) GETARG_INTVECTOR(thisarg)[2])
6305 if(flags & PRINTF_ALTERNATE)
6307 if(precision < 0) // not set
6308 o += dpsnprintf(o, end - o, formatbuf, width, (isfloat ? (unsigned int) GETARG_FLOAT(thisarg) : (unsigned int) GETARG_INT(thisarg)));
6310 o += dpsnprintf(o, end - o, formatbuf, width, precision, (isfloat ? (unsigned int) GETARG_FLOAT(thisarg) : (unsigned int) GETARG_INT(thisarg)));
6314 unsigned int c = (isfloat ? (unsigned int) GETARG_FLOAT(thisarg) : (unsigned int) GETARG_INT(thisarg));
6316 const char *buf = u8_encodech(c, NULL, charbuf16);
6319 if(precision < 0) // not set
6320 precision = end - o - 1;
6321 o += u8_strpad(o, end - o, buf, (flags & PRINTF_LEFT) != 0, width, precision);
6325 if(flags & PRINTF_ALTERNATE)
6327 if(precision < 0) // not set
6328 o += dpsnprintf(o, end - o, formatbuf, width, GETARG_STRING(thisarg));
6330 o += dpsnprintf(o, end - o, formatbuf, width, precision, GETARG_STRING(thisarg));
6334 if(precision < 0) // not set
6335 precision = end - o - 1;
6336 if(flags & PRINTF_SIGNPOSITIVE)
6337 o += u8_strpad(o, end - o, GETARG_STRING(thisarg), (flags & PRINTF_LEFT) != 0, width, precision);
6339 o += u8_strpad_colorcodes(o, end - o, GETARG_STRING(thisarg), (flags & PRINTF_LEFT) != 0, width, precision);
6343 VM_Warning(prog, "VM_sprintf: invalid directive in %s: %s\n", prog->name, s0);
6358 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, outbuf);
6364 static dp_model_t *getmodel(prvm_prog_t *prog, prvm_edict_t *ed)
6366 if (prog == SVVM_prog)
6367 return SV_GetModelFromEdict(ed);
6368 else if (prog == CLVM_prog)
6369 return CL_GetModelFromEdict(ed);
6376 unsigned int progid;
6378 frameblend_t frameblend[MAX_FRAMEBLENDS];
6379 skeleton_t *skeleton_p;
6380 skeleton_t skeleton;
6381 float *data_vertex3f;
6382 float *data_svector3f;
6383 float *data_tvector3f;
6384 float *data_normal3f;
6386 float *buf_vertex3f;
6387 float *buf_svector3f;
6388 float *buf_tvector3f;
6389 float *buf_normal3f;
6391 animatemodel_cache_t;
6392 static animatemodel_cache_t animatemodel_cache;
6394 static void animatemodel(prvm_prog_t *prog, dp_model_t *model, prvm_edict_t *ed)
6396 skeleton_t *skeleton;
6397 int skeletonindex = -1;
6398 qboolean need = false;
6399 if(!(model->surfmesh.isanimated && model->AnimateVertices))
6401 animatemodel_cache.data_vertex3f = model->surfmesh.data_vertex3f;
6402 animatemodel_cache.data_svector3f = model->surfmesh.data_svector3f;
6403 animatemodel_cache.data_tvector3f = model->surfmesh.data_tvector3f;
6404 animatemodel_cache.data_normal3f = model->surfmesh.data_normal3f;
6407 if(animatemodel_cache.progid != prog->id)
6408 memset(&animatemodel_cache, 0, sizeof(animatemodel_cache));
6409 need |= (animatemodel_cache.model != model);
6410 VM_GenerateFrameGroupBlend(prog, ed->priv.server->framegroupblend, ed);
6411 VM_FrameBlendFromFrameGroupBlend(ed->priv.server->frameblend, ed->priv.server->framegroupblend, model);
6412 need |= (memcmp(&animatemodel_cache.frameblend, &ed->priv.server->frameblend, sizeof(ed->priv.server->frameblend))) != 0;
6413 skeletonindex = (int)PRVM_gameedictfloat(ed, skeletonindex) - 1;
6414 if (!(skeletonindex >= 0 && skeletonindex < MAX_EDICTS && (skeleton = prog->skeletons[skeletonindex]) && skeleton->model->num_bones == ed->priv.server->skeleton.model->num_bones))
6416 need |= (animatemodel_cache.skeleton_p != skeleton);
6418 need |= (memcmp(&animatemodel_cache.skeleton, skeleton, sizeof(ed->priv.server->skeleton))) != 0;
6421 if(model->surfmesh.num_vertices > animatemodel_cache.max_vertices)
6423 animatemodel_cache.max_vertices = model->surfmesh.num_vertices * 2;
6424 if(animatemodel_cache.buf_vertex3f) Mem_Free(animatemodel_cache.buf_vertex3f);
6425 if(animatemodel_cache.buf_svector3f) Mem_Free(animatemodel_cache.buf_svector3f);
6426 if(animatemodel_cache.buf_tvector3f) Mem_Free(animatemodel_cache.buf_tvector3f);
6427 if(animatemodel_cache.buf_normal3f) Mem_Free(animatemodel_cache.buf_normal3f);
6428 animatemodel_cache.buf_vertex3f = (float *)Mem_Alloc(prog->progs_mempool, sizeof(float[3]) * animatemodel_cache.max_vertices);
6429 animatemodel_cache.buf_svector3f = (float *)Mem_Alloc(prog->progs_mempool, sizeof(float[3]) * animatemodel_cache.max_vertices);
6430 animatemodel_cache.buf_tvector3f = (float *)Mem_Alloc(prog->progs_mempool, sizeof(float[3]) * animatemodel_cache.max_vertices);
6431 animatemodel_cache.buf_normal3f = (float *)Mem_Alloc(prog->progs_mempool, sizeof(float[3]) * animatemodel_cache.max_vertices);
6433 animatemodel_cache.data_vertex3f = animatemodel_cache.buf_vertex3f;
6434 animatemodel_cache.data_svector3f = animatemodel_cache.buf_svector3f;
6435 animatemodel_cache.data_tvector3f = animatemodel_cache.buf_tvector3f;
6436 animatemodel_cache.data_normal3f = animatemodel_cache.buf_normal3f;
6437 VM_UpdateEdictSkeleton(prog, ed, model, ed->priv.server->frameblend);
6438 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);
6439 animatemodel_cache.progid = prog->id;
6440 animatemodel_cache.model = model;
6441 memcpy(&animatemodel_cache.frameblend, &ed->priv.server->frameblend, sizeof(ed->priv.server->frameblend));
6442 animatemodel_cache.skeleton_p = skeleton;
6444 memcpy(&animatemodel_cache.skeleton, skeleton, sizeof(ed->priv.server->skeleton));
6447 static void getmatrix(prvm_prog_t *prog, prvm_edict_t *ed, matrix4x4_t *out)
6449 if (prog == SVVM_prog)
6450 SV_GetEntityMatrix(prog, ed, out, false);
6451 else if (prog == CLVM_prog)
6452 CL_GetEntityMatrix(prog, ed, out, false);
6454 *out = identitymatrix;
6457 static void applytransform_forward(prvm_prog_t *prog, const vec3_t in, prvm_edict_t *ed, vec3_t out)
6460 getmatrix(prog, ed, &m);
6461 Matrix4x4_Transform(&m, in, out);
6464 static void applytransform_forward_direction(prvm_prog_t *prog, const vec3_t in, prvm_edict_t *ed, vec3_t out)
6467 getmatrix(prog, ed, &m);
6468 Matrix4x4_Transform3x3(&m, in, out);
6471 static void applytransform_inverted(prvm_prog_t *prog, const vec3_t in, prvm_edict_t *ed, vec3_t out)
6474 getmatrix(prog, ed, &m);
6475 Matrix4x4_Invert_Full(&n, &m);
6476 Matrix4x4_Transform3x3(&n, in, out);
6479 static void applytransform_forward_normal(prvm_prog_t *prog, const vec3_t in, prvm_edict_t *ed, vec3_t out)
6483 getmatrix(prog, ed, &m);
6484 Matrix4x4_TransformPositivePlane(&m, in[0], in[1], in[2], 0, p);
6488 static void clippointtosurface(prvm_prog_t *prog, prvm_edict_t *ed, dp_model_t *model, msurface_t *surface, vec3_t p, vec3_t out)
6491 float *v[3], facenormal[3], edgenormal[3], sidenormal[3], temp[3], offsetdist, dist, bestdist;
6493 animatemodel(prog, model, ed);
6494 bestdist = 1000000000;
6496 for (i = 0, e = (model->surfmesh.data_element3i + 3 * surface->num_firsttriangle);i < surface->num_triangles;i++, e += 3)
6498 // clip original point to each triangle of the surface and find the
6499 // triangle that is closest
6500 v[0] = animatemodel_cache.data_vertex3f + e[0] * 3;
6501 v[1] = animatemodel_cache.data_vertex3f + e[1] * 3;
6502 v[2] = animatemodel_cache.data_vertex3f + e[2] * 3;
6503 TriangleNormal(v[0], v[1], v[2], facenormal);
6504 VectorNormalize(facenormal);
6505 offsetdist = DotProduct(v[0], facenormal) - DotProduct(p, facenormal);
6506 VectorMA(p, offsetdist, facenormal, temp);
6507 for (j = 0, k = 2;j < 3;k = j, j++)
6509 VectorSubtract(v[k], v[j], edgenormal);
6510 CrossProduct(edgenormal, facenormal, sidenormal);
6511 VectorNormalize(sidenormal);
6512 offsetdist = DotProduct(v[k], sidenormal) - DotProduct(temp, sidenormal);
6514 VectorMA(temp, offsetdist, sidenormal, temp);
6516 dist = VectorDistance2(temp, p);
6517 if (bestdist > dist)
6520 VectorCopy(temp, out);
6525 static msurface_t *getsurface(dp_model_t *model, int surfacenum)
6527 if (surfacenum < 0 || surfacenum >= model->nummodelsurfaces)
6529 return model->data_surfaces + surfacenum + model->firstmodelsurface;
6533 //PF_getsurfacenumpoints, // #434 float(entity e, float s) getsurfacenumpoints = #434;
6534 void VM_getsurfacenumpoints(prvm_prog_t *prog)
6537 msurface_t *surface;
6538 VM_SAFEPARMCOUNT(2, VM_getsurfacenumpoints);
6539 // return 0 if no such surface
6540 if (!(model = getmodel(prog, PRVM_G_EDICT(OFS_PARM0))) || !(surface = getsurface(model, (int)PRVM_G_FLOAT(OFS_PARM1))))
6542 PRVM_G_FLOAT(OFS_RETURN) = 0;
6546 // note: this (incorrectly) assumes it is a simple polygon
6547 PRVM_G_FLOAT(OFS_RETURN) = surface->num_vertices;
6549 //PF_getsurfacepoint, // #435 vector(entity e, float s, float n) getsurfacepoint = #435;
6550 void VM_getsurfacepoint(prvm_prog_t *prog)
6554 msurface_t *surface;
6556 VM_SAFEPARMCOUNT(3, VM_getsurfacepoint);
6557 VectorClear(PRVM_G_VECTOR(OFS_RETURN));
6558 ed = PRVM_G_EDICT(OFS_PARM0);
6559 if (!(model = getmodel(prog, ed)) || !(surface = getsurface(model, (int)PRVM_G_FLOAT(OFS_PARM1))))
6561 // note: this (incorrectly) assumes it is a simple polygon
6562 pointnum = (int)PRVM_G_FLOAT(OFS_PARM2);
6563 if (pointnum < 0 || pointnum >= surface->num_vertices)
6565 animatemodel(prog, model, ed);
6566 applytransform_forward(prog, &(animatemodel_cache.data_vertex3f + 3 * surface->num_firstvertex)[pointnum * 3], ed, PRVM_G_VECTOR(OFS_RETURN));
6568 //PF_getsurfacepointattribute, // #486 vector(entity e, float s, float n, float a) getsurfacepointattribute = #486;
6569 // float SPA_POSITION = 0;
6570 // float SPA_S_AXIS = 1;
6571 // float SPA_T_AXIS = 2;
6572 // float SPA_R_AXIS = 3; // same as SPA_NORMAL
6573 // float SPA_TEXCOORDS0 = 4;
6574 // float SPA_LIGHTMAP0_TEXCOORDS = 5;
6575 // float SPA_LIGHTMAP0_COLOR = 6;
6576 void VM_getsurfacepointattribute(prvm_prog_t *prog)
6580 msurface_t *surface;
6584 VM_SAFEPARMCOUNT(4, VM_getsurfacepoint);
6585 VectorClear(PRVM_G_VECTOR(OFS_RETURN));
6586 ed = PRVM_G_EDICT(OFS_PARM0);
6587 if (!(model = getmodel(prog, ed)) || !(surface = getsurface(model, (int)PRVM_G_FLOAT(OFS_PARM1))))
6589 pointnum = (int)PRVM_G_FLOAT(OFS_PARM2);
6590 if (pointnum < 0 || pointnum >= surface->num_vertices)
6592 attributetype = (int) PRVM_G_FLOAT(OFS_PARM3);
6594 animatemodel(prog, model, ed);
6596 switch( attributetype ) {
6597 // float SPA_POSITION = 0;
6599 applytransform_forward(prog, &(animatemodel_cache.data_vertex3f + 3 * surface->num_firstvertex)[pointnum * 3], ed, PRVM_G_VECTOR(OFS_RETURN));
6601 // float SPA_S_AXIS = 1;
6603 applytransform_forward_direction(prog, &(animatemodel_cache.data_svector3f + 3 * surface->num_firstvertex)[pointnum * 3], ed, PRVM_G_VECTOR(OFS_RETURN));
6605 // float SPA_T_AXIS = 2;
6607 applytransform_forward_direction(prog, &(animatemodel_cache.data_tvector3f + 3 * surface->num_firstvertex)[pointnum * 3], ed, PRVM_G_VECTOR(OFS_RETURN));
6609 // float SPA_R_AXIS = 3; // same as SPA_NORMAL
6611 applytransform_forward_direction(prog, &(animatemodel_cache.data_normal3f + 3 * surface->num_firstvertex)[pointnum * 3], ed, PRVM_G_VECTOR(OFS_RETURN));
6613 // float SPA_TEXCOORDS0 = 4;
6615 float *ret = PRVM_G_VECTOR(OFS_RETURN);
6616 float *texcoord = &(model->surfmesh.data_texcoordtexture2f + 2 * surface->num_firstvertex)[pointnum * 2];
6617 ret[0] = texcoord[0];
6618 ret[1] = texcoord[1];
6622 // float SPA_LIGHTMAP0_TEXCOORDS = 5;
6624 float *ret = PRVM_G_VECTOR(OFS_RETURN);
6625 float *texcoord = &(model->surfmesh.data_texcoordlightmap2f + 2 * surface->num_firstvertex)[pointnum * 2];
6626 ret[0] = texcoord[0];
6627 ret[1] = texcoord[1];
6631 // float SPA_LIGHTMAP0_COLOR = 6;
6633 // ignore alpha for now..
6634 VectorCopy( &(model->surfmesh.data_lightmapcolor4f + 4 * surface->num_firstvertex)[pointnum * 4], PRVM_G_VECTOR(OFS_RETURN));
6637 VectorSet( PRVM_G_VECTOR(OFS_RETURN), 0.0f, 0.0f, 0.0f );
6641 //PF_getsurfacenormal, // #436 vector(entity e, float s) getsurfacenormal = #436;
6642 void VM_getsurfacenormal(prvm_prog_t *prog)
6645 msurface_t *surface;
6647 VM_SAFEPARMCOUNT(2, VM_getsurfacenormal);
6648 VectorClear(PRVM_G_VECTOR(OFS_RETURN));
6649 if (!(model = getmodel(prog, PRVM_G_EDICT(OFS_PARM0))) || !(surface = getsurface(model, (int)PRVM_G_FLOAT(OFS_PARM1))))
6651 // note: this only returns the first triangle, so it doesn't work very
6652 // well for curved surfaces or arbitrary meshes
6653 animatemodel(prog, model, PRVM_G_EDICT(OFS_PARM0));
6654 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);
6655 applytransform_forward_normal(prog, normal, PRVM_G_EDICT(OFS_PARM0), PRVM_G_VECTOR(OFS_RETURN));
6656 VectorNormalize(PRVM_G_VECTOR(OFS_RETURN));
6658 //PF_getsurfacetexture, // #437 string(entity e, float s) getsurfacetexture = #437;
6659 void VM_getsurfacetexture(prvm_prog_t *prog)
6662 msurface_t *surface;
6663 VM_SAFEPARMCOUNT(2, VM_getsurfacetexture);
6664 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
6665 if (!(model = getmodel(prog, PRVM_G_EDICT(OFS_PARM0))) || !(surface = getsurface(model, (int)PRVM_G_FLOAT(OFS_PARM1))))
6667 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, surface->texture->name);
6669 //PF_getsurfacenearpoint, // #438 float(entity e, vector p) getsurfacenearpoint = #438;
6670 void VM_getsurfacenearpoint(prvm_prog_t *prog)
6672 int surfacenum, best;
6674 vec_t dist, bestdist;
6677 msurface_t *surface;
6679 VM_SAFEPARMCOUNT(2, VM_getsurfacenearpoint);
6680 PRVM_G_FLOAT(OFS_RETURN) = -1;
6681 ed = PRVM_G_EDICT(OFS_PARM0);
6682 point = PRVM_G_VECTOR(OFS_PARM1);
6684 if (!ed || ed->priv.server->free)
6686 model = getmodel(prog, ed);
6687 if (!model || !model->num_surfaces)
6690 animatemodel(prog, model, ed);
6692 applytransform_inverted(prog, point, ed, p);
6694 bestdist = 1000000000;
6695 for (surfacenum = 0;surfacenum < model->nummodelsurfaces;surfacenum++)
6697 surface = model->data_surfaces + surfacenum + model->firstmodelsurface;
6698 // first see if the nearest point on the surface's box is closer than the previous match
6699 clipped[0] = bound(surface->mins[0], p[0], surface->maxs[0]) - p[0];
6700 clipped[1] = bound(surface->mins[1], p[1], surface->maxs[1]) - p[1];
6701 clipped[2] = bound(surface->mins[2], p[2], surface->maxs[2]) - p[2];
6702 dist = VectorLength2(clipped);
6703 if (dist < bestdist)
6705 // it is, check the nearest point on the actual geometry
6706 clippointtosurface(prog, ed, model, surface, p, clipped);
6707 VectorSubtract(clipped, p, clipped);
6708 dist += VectorLength2(clipped);
6709 if (dist < bestdist)
6711 // that's closer too, store it as the best match
6717 PRVM_G_FLOAT(OFS_RETURN) = best;
6719 //PF_getsurfaceclippedpoint, // #439 vector(entity e, float s, vector p) getsurfaceclippedpoint = #439;
6720 void VM_getsurfaceclippedpoint(prvm_prog_t *prog)
6724 msurface_t *surface;
6726 VM_SAFEPARMCOUNT(3, VM_te_getsurfaceclippedpoint);
6727 VectorClear(PRVM_G_VECTOR(OFS_RETURN));
6728 ed = PRVM_G_EDICT(OFS_PARM0);
6729 if (!(model = getmodel(prog, ed)) || !(surface = getsurface(model, (int)PRVM_G_FLOAT(OFS_PARM1))))
6731 animatemodel(prog, model, ed);
6732 applytransform_inverted(prog, PRVM_G_VECTOR(OFS_PARM2), ed, p);
6733 clippointtosurface(prog, ed, model, surface, p, out);
6734 VectorAdd(out, PRVM_serveredictvector(ed, origin), PRVM_G_VECTOR(OFS_RETURN));
6737 //PF_getsurfacenumtriangles, // #??? float(entity e, float s) getsurfacenumtriangles = #???;
6738 void VM_getsurfacenumtriangles(prvm_prog_t *prog)
6741 msurface_t *surface;
6742 VM_SAFEPARMCOUNT(2, VM_SV_getsurfacenumtriangles);
6743 // return 0 if no such surface
6744 if (!(model = getmodel(prog, PRVM_G_EDICT(OFS_PARM0))) || !(surface = getsurface(model, (int)PRVM_G_FLOAT(OFS_PARM1))))
6746 PRVM_G_FLOAT(OFS_RETURN) = 0;
6750 PRVM_G_FLOAT(OFS_RETURN) = surface->num_triangles;
6752 //PF_getsurfacetriangle, // #??? vector(entity e, float s, float n) getsurfacetriangle = #???;
6753 void VM_getsurfacetriangle(prvm_prog_t *prog)
6755 const vec3_t d = {-1, -1, -1};
6758 msurface_t *surface;
6760 VM_SAFEPARMCOUNT(3, VM_SV_getsurfacetriangle);
6761 VectorClear(PRVM_G_VECTOR(OFS_RETURN));
6762 ed = PRVM_G_EDICT(OFS_PARM0);
6763 if (!(model = getmodel(prog, ed)) || !(surface = getsurface(model, (int)PRVM_G_FLOAT(OFS_PARM1))))
6765 trinum = (int)PRVM_G_FLOAT(OFS_PARM2);
6766 if (trinum < 0 || trinum >= surface->num_triangles)
6768 // FIXME: implement rotation/scaling
6769 VectorMA(&(model->surfmesh.data_element3i + 3 * surface->num_firsttriangle)[trinum * 3], surface->num_firstvertex, d, PRVM_G_VECTOR(OFS_RETURN));
6776 #define VM_physics_ApplyCmd(ed,f) if (!ed->priv.server->ode_body) VM_physics_newstackfunction(prog, ed, f); else World_Physics_ApplyCmd(ed, f)
6778 static edict_odefunc_t *VM_physics_newstackfunction(prvm_prog_t *prog, prvm_edict_t *ed, edict_odefunc_t *f)
6780 edict_odefunc_t *newfunc, *func;
6782 newfunc = (edict_odefunc_t *)Mem_Alloc(prog->progs_mempool, sizeof(edict_odefunc_t));
6783 memcpy(newfunc, f, sizeof(edict_odefunc_t));
6784 newfunc->next = NULL;
6785 if (!ed->priv.server->ode_func)
6786 ed->priv.server->ode_func = newfunc;
6789 for (func = ed->priv.server->ode_func; func->next; func = func->next);
6790 func->next = newfunc;
6795 // void(entity e, float physics_enabled) physics_enable = #;
6796 void VM_physics_enable(prvm_prog_t *prog)
6801 VM_SAFEPARMCOUNT(2, VM_physics_enable);
6802 ed = PRVM_G_EDICT(OFS_PARM0);
6805 if (developer.integer > 0)
6806 VM_Warning(prog, "VM_physics_enable: null entity!\n");
6809 // entity should have MOVETYPE_PHYSICS already set, this can damage memory (making leaked allocation) so warn about this even if non-developer
6810 if (PRVM_serveredictfloat(ed, movetype) != MOVETYPE_PHYSICS)
6812 VM_Warning(prog, "VM_physics_enable: entity is not MOVETYPE_PHYSICS!\n");
6815 f.type = PRVM_G_FLOAT(OFS_PARM1) == 0 ? ODEFUNC_DISABLE : ODEFUNC_ENABLE;
6816 VM_physics_ApplyCmd(ed, &f);
6819 // void(entity e, vector force, vector relative_ofs) physics_addforce = #;
6820 void VM_physics_addforce(prvm_prog_t *prog)
6825 VM_SAFEPARMCOUNT(3, VM_physics_addforce);
6826 ed = PRVM_G_EDICT(OFS_PARM0);
6829 if (developer.integer > 0)
6830 VM_Warning(prog, "VM_physics_addforce: null entity!\n");
6833 // entity should have MOVETYPE_PHYSICS already set, this can damage memory (making leaked allocation) so warn about this even if non-developer
6834 if (PRVM_serveredictfloat(ed, movetype) != MOVETYPE_PHYSICS)
6836 VM_Warning(prog, "VM_physics_addforce: entity is not MOVETYPE_PHYSICS!\n");
6839 f.type = ODEFUNC_RELFORCEATPOS;
6840 VectorCopy(PRVM_G_VECTOR(OFS_PARM1), f.v1);
6841 VectorSubtract(PRVM_serveredictvector(ed, origin), PRVM_G_VECTOR(OFS_PARM2), f.v2);
6842 VM_physics_ApplyCmd(ed, &f);
6845 // void(entity e, vector torque) physics_addtorque = #;
6846 void VM_physics_addtorque(prvm_prog_t *prog)
6851 VM_SAFEPARMCOUNT(2, VM_physics_addtorque);
6852 ed = PRVM_G_EDICT(OFS_PARM0);
6855 if (developer.integer > 0)
6856 VM_Warning(prog, "VM_physics_addtorque: null entity!\n");
6859 // entity should have MOVETYPE_PHYSICS already set, this can damage memory (making leaked allocation) so warn about this even if non-developer
6860 if (PRVM_serveredictfloat(ed, movetype) != MOVETYPE_PHYSICS)
6862 VM_Warning(prog, "VM_physics_addtorque: entity is not MOVETYPE_PHYSICS!\n");
6865 f.type = ODEFUNC_RELTORQUE;
6866 VectorCopy(PRVM_G_VECTOR(OFS_PARM1), f.v1);
6867 VM_physics_ApplyCmd(ed, &f);