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;
20 extern dllhandle_t ode_dll;
22 // LordHavoc: changed this to NOT use a return statement, so that it can be used in functions that must return a value
23 void VM_Warning(prvm_prog_t *prog, const char *fmt, ...)
26 char msg[MAX_INPUTLINE];
27 static double recursive = -1;
30 dpvsnprintf(msg,sizeof(msg),fmt,argptr);
35 // TODO: either add a cvar/cmd to control the state dumping or replace some of the calls with Con_Printf [9/13/2006 Black]
36 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
39 PRVM_PrintState(prog);
45 //============================================================================
48 // TODO DONE: move vm_files and vm_fssearchlist to prvm_prog_t struct
49 // TODO: move vm_files and vm_fssearchlist back [9/13/2006 Black]
50 // TODO: (move vm_files and vm_fssearchlist to prvm_prog_t struct again) [2007-01-23 LordHavoc]
51 // TODO: will this war ever end? [2007-01-23 LordHavoc]
53 void VM_CheckEmptyString(prvm_prog_t *prog, const char *s)
55 if (ISWHITESPACE(s[0]))
56 prog->error_cmd("%s: Bad string", prog->name);
59 void VM_GenerateFrameGroupBlend(prvm_prog_t *prog, framegroupblend_t *framegroupblend, const prvm_edict_t *ed)
61 // self.frame is the interpolation target (new frame)
62 // self.frame1time is the animation base time for the interpolation target
63 // self.frame2 is the interpolation start (previous frame)
64 // self.frame2time is the animation base time for the interpolation start
65 // self.lerpfrac is the interpolation strength for self.frame2
66 // self.lerpfrac3 is the interpolation strength for self.frame3
67 // self.lerpfrac4 is the interpolation strength for self.frame4
68 // pitch angle on a player model where the animator set up 5 sets of
69 // animations and the csqc simply lerps between sets)
70 framegroupblend[0].frame = (int) PRVM_gameedictfloat(ed, frame );
71 framegroupblend[1].frame = (int) PRVM_gameedictfloat(ed, frame2 );
72 framegroupblend[2].frame = (int) PRVM_gameedictfloat(ed, frame3 );
73 framegroupblend[3].frame = (int) PRVM_gameedictfloat(ed, frame4 );
74 framegroupblend[0].start = PRVM_gameedictfloat(ed, frame1time);
75 framegroupblend[1].start = PRVM_gameedictfloat(ed, frame2time);
76 framegroupblend[2].start = PRVM_gameedictfloat(ed, frame3time);
77 framegroupblend[3].start = PRVM_gameedictfloat(ed, frame4time);
78 framegroupblend[1].lerp = PRVM_gameedictfloat(ed, lerpfrac );
79 framegroupblend[2].lerp = PRVM_gameedictfloat(ed, lerpfrac3 );
80 framegroupblend[3].lerp = PRVM_gameedictfloat(ed, lerpfrac4 );
81 // assume that the (missing) lerpfrac1 is whatever remains after lerpfrac2+lerpfrac3+lerpfrac4 are summed
82 framegroupblend[0].lerp = 1 - framegroupblend[1].lerp - framegroupblend[2].lerp - framegroupblend[3].lerp;
85 // LordHavoc: quite tempting to break apart this function to reuse the
86 // duplicated code, but I suspect it is better for performance
88 void VM_FrameBlendFromFrameGroupBlend(frameblend_t *frameblend, const framegroupblend_t *framegroupblend, const dp_model_t *model, double curtime)
90 int sub2, numframes, f, i, k;
91 int isfirstframegroup = true;
93 double sublerp, lerp, d;
94 const animscene_t *scene;
95 const framegroupblend_t *g;
96 frameblend_t *blend = frameblend;
98 memset(blend, 0, MAX_FRAMEBLENDS * sizeof(*blend));
100 if (!model || !model->surfmesh.isanimated)
106 nolerp = (model->type == mod_sprite) ? !r_lerpsprites.integer : !r_lerpmodels.integer;
107 numframes = model->numframes;
108 for (k = 0, g = framegroupblend;k < MAX_FRAMEGROUPBLENDS;k++, g++)
111 if ((unsigned int)f >= (unsigned int)numframes)
113 if (developer_extra.integer)
114 Con_DPrintf("VM_FrameBlendFromFrameGroupBlend: no such frame %d in model %s\n", f, model->name);
122 if (isfirstframegroup)
125 isfirstframegroup = false;
130 if (model->animscenes)
132 scene = model->animscenes + f;
133 f = scene->firstframe;
134 if (scene->framecount > 1)
136 // this code path is only used on .zym models and torches
137 sublerp = scene->framerate * (curtime - g->start);
138 f = (int) floor(sublerp);
141 if (sublerp < (1.0 / 65536.0f))
143 if (sublerp > (65535.0f / 65536.0f))
149 f = (f % scene->framecount);
150 sub2 = (sub2 % scene->framecount);
152 f = bound(0, f, (scene->framecount - 1)) + scene->firstframe;
153 sub2 = bound(0, sub2, (scene->framecount - 1)) + scene->firstframe;
155 // two framelerps produced from one animation
158 for (i = 0;i < MAX_FRAMEBLENDS;i++)
160 if (blend[i].lerp <= 0 || blend[i].subframe == sub2)
162 blend[i].subframe = sub2;
168 d = (1 - sublerp) * lerp;
173 for (i = 0;i < MAX_FRAMEBLENDS;i++)
175 if (blend[i].lerp <= 0 || blend[i].subframe == f)
177 blend[i].subframe = f;
186 void VM_UpdateEdictSkeleton(prvm_prog_t *prog, prvm_edict_t *ed, const dp_model_t *edmodel, const frameblend_t *frameblend)
188 if (ed->priv.server->skeleton.model != edmodel)
190 VM_RemoveEdictSkeleton(prog, ed);
191 ed->priv.server->skeleton.model = edmodel;
193 if (!ed->priv.server->skeleton.model || !ed->priv.server->skeleton.model->num_bones)
195 if(ed->priv.server->skeleton.relativetransforms)
196 Mem_Free(ed->priv.server->skeleton.relativetransforms);
197 ed->priv.server->skeleton.relativetransforms = NULL;
202 int skeletonindex = -1;
203 skeleton_t *skeleton;
204 skeletonindex = (int)PRVM_gameedictfloat(ed, skeletonindex) - 1;
205 if (skeletonindex >= 0 && skeletonindex < MAX_EDICTS && (skeleton = prog->skeletons[skeletonindex]) && skeleton->model->num_bones == ed->priv.server->skeleton.model->num_bones)
207 // custom skeleton controlled by the game (FTE_CSQC_SKELETONOBJECTS)
208 if (!ed->priv.server->skeleton.relativetransforms)
209 ed->priv.server->skeleton.relativetransforms = (matrix4x4_t *)Mem_Alloc(prog->progs_mempool, ed->priv.server->skeleton.model->num_bones * sizeof(matrix4x4_t));
210 memcpy(ed->priv.server->skeleton.relativetransforms, skeleton->relativetransforms, ed->priv.server->skeleton.model->num_bones * sizeof(matrix4x4_t));
214 if(ed->priv.server->skeleton.relativetransforms)
215 Mem_Free(ed->priv.server->skeleton.relativetransforms);
216 ed->priv.server->skeleton.relativetransforms = NULL;
221 void VM_RemoveEdictSkeleton(prvm_prog_t *prog, prvm_edict_t *ed)
223 if (ed->priv.server->skeleton.relativetransforms)
224 Mem_Free(ed->priv.server->skeleton.relativetransforms);
225 memset(&ed->priv.server->skeleton, 0, sizeof(ed->priv.server->skeleton));
231 //============================================================================
234 void VM_VarString(prvm_prog_t *prog, int first, char *out, int outlength)
240 outend = out + outlength - 1;
241 for (i = first;i < prog->argc && out < outend;i++)
243 s = PRVM_G_STRING((OFS_PARM0+i*3));
244 while (out < outend && *s)
254 returns true if the extension is supported by the server
256 checkextension(extensionname)
260 // kind of helper function
261 static qboolean checkextension(prvm_prog_t *prog, const char *name)
264 const char *e, *start;
265 len = (int)strlen(name);
267 for (e = prog->extensionstring;*e;e++)
274 while (*e && *e != ' ')
276 if ((e - start) == len && !strncasecmp(start, name, len))
278 // special sheck for ODE
279 if (!strncasecmp("DP_PHYSICS_ODE", name, 14))
282 return ode_dll ? true : false;
292 // special sheck for d0_blind_id
293 if (!strcasecmp("DP_CRYPTO", name))
294 return Crypto_Available();
295 if (!strcasecmp("DP_QC_DIGEST_SHA256", name))
296 return Crypto_Available();
304 void VM_checkextension(prvm_prog_t *prog)
306 VM_SAFEPARMCOUNT(1,VM_checkextension);
308 PRVM_G_FLOAT(OFS_RETURN) = checkextension(prog, PRVM_G_STRING(OFS_PARM0));
315 This is a TERMINAL error, which will kill off the entire prog.
321 void VM_error(prvm_prog_t *prog)
324 char string[VM_STRINGTEMP_LENGTH];
326 VM_VarString(prog, 0, string, sizeof(string));
327 Con_Printf("======%s ERROR in %s:\n%s\n", prog->name, PRVM_GetString(prog, prog->xfunction->s_name), string);
328 ed = PRVM_PROG_TO_EDICT(PRVM_allglobaledict(self));
329 PRVM_ED_Print(prog, ed, NULL);
331 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);
338 Dumps out self, then an error message. The program is aborted and self is
339 removed, but the level can continue.
344 void VM_objerror(prvm_prog_t *prog)
347 char string[VM_STRINGTEMP_LENGTH];
349 VM_VarString(prog, 0, string, sizeof(string));
350 Con_Printf("======OBJECT ERROR======\n"); // , prog->name, PRVM_GetString(prog->xfunction->s_name), string); // or include them? FIXME
351 ed = PRVM_PROG_TO_EDICT(PRVM_allglobaledict(self));
352 PRVM_ED_Print(prog, ed, NULL);
353 PRVM_ED_Free (prog, ed);
354 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);
366 void VM_print(prvm_prog_t *prog)
368 char string[VM_STRINGTEMP_LENGTH];
370 VM_VarString(prog, 0, string, sizeof(string));
378 broadcast print to everyone on server
383 void VM_bprint(prvm_prog_t *prog)
385 char string[VM_STRINGTEMP_LENGTH];
389 VM_Warning(prog, "VM_bprint: game is not server(%s) !\n", prog->name);
393 VM_VarString(prog, 0, string, sizeof(string));
394 SV_BroadcastPrint(string);
399 VM_sprint (menu & client but only if server.active == true)
401 single print to a specific client
403 sprint(float clientnum,...[string])
406 void VM_sprint(prvm_prog_t *prog)
410 char string[VM_STRINGTEMP_LENGTH];
412 VM_SAFEPARMCOUNTRANGE(1, 8, VM_sprint);
414 //find client for this entity
415 clientnum = (int)PRVM_G_FLOAT(OFS_PARM0);
416 if (!sv.active || clientnum < 0 || clientnum >= svs.maxclients || !svs.clients[clientnum].active)
418 VM_Warning(prog, "VM_sprint: %s: invalid client or server is not active !\n", prog->name);
422 client = svs.clients + clientnum;
423 if (!client->netconnection)
426 VM_VarString(prog, 1, string, sizeof(string));
427 MSG_WriteChar(&client->netconnection->message,svc_print);
428 MSG_WriteString(&client->netconnection->message, string);
435 single print to the screen
440 void VM_centerprint(prvm_prog_t *prog)
442 char string[VM_STRINGTEMP_LENGTH];
444 VM_SAFEPARMCOUNTRANGE(1, 8, VM_centerprint);
445 VM_VarString(prog, 0, string, sizeof(string));
446 SCR_CenterPrint(string);
453 vector normalize(vector)
456 void VM_normalize(prvm_prog_t *prog)
462 VM_SAFEPARMCOUNT(1,VM_normalize);
464 value1 = PRVM_G_VECTOR(OFS_PARM0);
466 f = VectorLength2(value1);
470 VectorScale(value1, f, newvalue);
473 VectorClear(newvalue);
475 VectorCopy (newvalue, PRVM_G_VECTOR(OFS_RETURN));
485 void VM_vlen(prvm_prog_t *prog)
487 VM_SAFEPARMCOUNT(1,VM_vlen);
488 PRVM_G_FLOAT(OFS_RETURN) = VectorLength(PRVM_G_VECTOR(OFS_PARM0));
495 float vectoyaw(vector)
498 void VM_vectoyaw(prvm_prog_t *prog)
503 VM_SAFEPARMCOUNT(1,VM_vectoyaw);
505 value1 = PRVM_G_VECTOR(OFS_PARM0);
507 if (value1[1] == 0 && value1[0] == 0)
511 yaw = (int) (atan2(value1[1], value1[0]) * 180 / M_PI);
516 PRVM_G_FLOAT(OFS_RETURN) = yaw;
524 vector vectoangles(vector[, vector])
527 void VM_vectoangles(prvm_prog_t *prog)
529 vec3_t result, forward, up;
530 VM_SAFEPARMCOUNTRANGE(1, 2,VM_vectoangles);
532 VectorCopy(PRVM_G_VECTOR(OFS_PARM0), forward);
535 VectorCopy(PRVM_G_VECTOR(OFS_PARM1), up);
536 AnglesFromVectors(result, forward, up, true);
539 AnglesFromVectors(result, forward, NULL, true);
540 VectorCopy(result, PRVM_G_VECTOR(OFS_RETURN));
547 Returns a number from 0<= num < 1
552 void VM_random(prvm_prog_t *prog)
554 VM_SAFEPARMCOUNT(0,VM_random);
556 PRVM_G_FLOAT(OFS_RETURN) = lhrandom(0, 1);
563 localsound(string sample)
566 void VM_localsound(prvm_prog_t *prog)
570 VM_SAFEPARMCOUNT(1,VM_localsound);
572 s = PRVM_G_STRING(OFS_PARM0);
574 if(!S_LocalSound (s))
576 PRVM_G_FLOAT(OFS_RETURN) = -4;
577 VM_Warning(prog, "VM_localsound: Failed to play %s for %s !\n", s, prog->name);
581 PRVM_G_FLOAT(OFS_RETURN) = 1;
591 void VM_break(prvm_prog_t *prog)
593 prog->error_cmd("%s: break statement", prog->name);
596 //============================================================================
602 Sends text over to the client's execution buffer
604 [localcmd (string, ...) or]
608 void VM_localcmd(prvm_prog_t *prog)
610 char string[VM_STRINGTEMP_LENGTH];
611 VM_SAFEPARMCOUNTRANGE(1, 8, VM_localcmd);
612 VM_VarString(prog, 0, string, sizeof(string));
613 Cbuf_AddText(string);
616 static qboolean PRVM_Cvar_ReadOk(const char *string)
619 cvar = Cvar_FindVar(string);
620 return ((cvar) && ((cvar->flags & CVAR_PRIVATE) == 0));
630 void VM_cvar(prvm_prog_t *prog)
632 char string[VM_STRINGTEMP_LENGTH];
633 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar);
634 VM_VarString(prog, 0, string, sizeof(string));
635 VM_CheckEmptyString(prog, string);
636 PRVM_G_FLOAT(OFS_RETURN) = PRVM_Cvar_ReadOk(string) ? Cvar_VariableValue(string) : 0;
643 float cvar_type (string)
644 float CVAR_TYPEFLAG_EXISTS = 1;
645 float CVAR_TYPEFLAG_SAVED = 2;
646 float CVAR_TYPEFLAG_PRIVATE = 4;
647 float CVAR_TYPEFLAG_ENGINE = 8;
648 float CVAR_TYPEFLAG_HASDESCRIPTION = 16;
649 float CVAR_TYPEFLAG_READONLY = 32;
652 void VM_cvar_type(prvm_prog_t *prog)
654 char string[VM_STRINGTEMP_LENGTH];
658 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar);
659 VM_VarString(prog, 0, string, sizeof(string));
660 VM_CheckEmptyString(prog, string);
661 cvar = Cvar_FindVar(string);
666 PRVM_G_FLOAT(OFS_RETURN) = 0;
667 return; // CVAR_TYPE_NONE
670 ret = 1; // CVAR_EXISTS
671 if(cvar->flags & CVAR_SAVE)
672 ret |= 2; // CVAR_TYPE_SAVED
673 if(cvar->flags & CVAR_PRIVATE)
674 ret |= 4; // CVAR_TYPE_PRIVATE
675 if(!(cvar->flags & CVAR_ALLOCATED))
676 ret |= 8; // CVAR_TYPE_ENGINE
677 if(cvar->description != cvar_dummy_description)
678 ret |= 16; // CVAR_TYPE_HASDESCRIPTION
679 if(cvar->flags & CVAR_READONLY)
680 ret |= 32; // CVAR_TYPE_READONLY
682 PRVM_G_FLOAT(OFS_RETURN) = ret;
689 const string VM_cvar_string (string, ...)
692 void VM_cvar_string(prvm_prog_t *prog)
694 char string[VM_STRINGTEMP_LENGTH];
695 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar_string);
696 VM_VarString(prog, 0, string, sizeof(string));
697 VM_CheckEmptyString(prog, string);
698 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, PRVM_Cvar_ReadOk(string) ? Cvar_VariableString(string) : "");
703 ========================
706 const string VM_cvar_defstring (string, ...)
707 ========================
709 void VM_cvar_defstring(prvm_prog_t *prog)
711 char string[VM_STRINGTEMP_LENGTH];
712 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar_defstring);
713 VM_VarString(prog, 0, string, sizeof(string));
714 VM_CheckEmptyString(prog, string);
715 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, Cvar_VariableDefString(string));
719 ========================
722 const string VM_cvar_description (string, ...)
723 ========================
725 void VM_cvar_description(prvm_prog_t *prog)
727 char string[VM_STRINGTEMP_LENGTH];
728 VM_SAFEPARMCOUNTRANGE(1,8,VM_cvar_description);
729 VM_VarString(prog, 0, string, sizeof(string));
730 VM_CheckEmptyString(prog, string);
731 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, Cvar_VariableDescription(string));
737 void cvar_set (string,string, ...)
740 void VM_cvar_set(prvm_prog_t *prog)
743 char string[VM_STRINGTEMP_LENGTH];
744 VM_SAFEPARMCOUNTRANGE(2,8,VM_cvar_set);
745 VM_VarString(prog, 1, string, sizeof(string));
746 name = PRVM_G_STRING(OFS_PARM0);
747 VM_CheckEmptyString(prog, name);
748 Cvar_Set(name, string);
758 void VM_dprint(prvm_prog_t *prog)
760 char string[VM_STRINGTEMP_LENGTH];
761 VM_SAFEPARMCOUNTRANGE(1, 8, VM_dprint);
762 VM_VarString(prog, 0, string, sizeof(string));
764 Con_DPrintf("%s", string);
766 Con_DPrintf("%s: %s", prog->name, string);
778 void VM_ftos(prvm_prog_t *prog)
783 VM_SAFEPARMCOUNT(1, VM_ftos);
785 v = PRVM_G_FLOAT(OFS_PARM0);
787 if ((prvm_vec_t)((prvm_int_t)v) == v)
788 dpsnprintf(s, sizeof(s), "%.0f", v);
790 dpsnprintf(s, sizeof(s), "%f", v);
791 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, s);
802 void VM_fabs(prvm_prog_t *prog)
806 VM_SAFEPARMCOUNT(1,VM_fabs);
808 v = PRVM_G_FLOAT(OFS_PARM0);
809 PRVM_G_FLOAT(OFS_RETURN) = fabs(v);
820 void VM_vtos(prvm_prog_t *prog)
824 VM_SAFEPARMCOUNT(1,VM_vtos);
826 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]);
827 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, s);
838 void VM_etos(prvm_prog_t *prog)
842 VM_SAFEPARMCOUNT(1, VM_etos);
844 dpsnprintf (s, sizeof(s), "entity %i", PRVM_G_EDICTNUM(OFS_PARM0));
845 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, s);
852 float stof(...[string])
855 void VM_stof(prvm_prog_t *prog)
857 char string[VM_STRINGTEMP_LENGTH];
858 VM_SAFEPARMCOUNTRANGE(1, 8, VM_stof);
859 VM_VarString(prog, 0, string, sizeof(string));
860 PRVM_G_FLOAT(OFS_RETURN) = atof(string);
864 ========================
868 ========================
870 void VM_itof(prvm_prog_t *prog)
872 VM_SAFEPARMCOUNT(1, VM_itof);
873 PRVM_G_FLOAT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
877 ========================
880 entity ftoe(float num)
881 ========================
883 void VM_ftoe(prvm_prog_t *prog)
886 VM_SAFEPARMCOUNT(1, VM_ftoe);
888 ent = (prvm_int_t)PRVM_G_FLOAT(OFS_PARM0);
889 if (ent < 0 || ent >= prog->max_edicts || PRVM_PROG_TO_EDICT(ent)->priv.required->free)
890 ent = 0; // return world instead of a free or invalid entity
892 PRVM_G_INT(OFS_RETURN) = ent;
896 ========================
899 float etof(entity ent)
900 ========================
902 void VM_etof(prvm_prog_t *prog)
904 VM_SAFEPARMCOUNT(1, VM_etof);
905 PRVM_G_FLOAT(OFS_RETURN) = PRVM_G_EDICTNUM(OFS_PARM0);
912 string strftime(float uselocaltime, string[, string ...])
915 void VM_strftime(prvm_prog_t *prog)
924 char fmt[VM_STRINGTEMP_LENGTH];
925 char result[VM_STRINGTEMP_LENGTH];
926 VM_SAFEPARMCOUNTRANGE(2, 8, VM_strftime);
927 VM_VarString(prog, 1, fmt, sizeof(fmt));
930 if (PRVM_G_FLOAT(OFS_PARM0))
931 tmresult = localtime_s(&tm, &t);
933 tmresult = gmtime_s(&tm, &t);
936 if (PRVM_G_FLOAT(OFS_PARM0))
943 PRVM_G_INT(OFS_RETURN) = 0;
947 strftime(result, sizeof(result), fmt, &tm);
949 strftime(result, sizeof(result), fmt, tm);
951 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, result);
962 void VM_spawn(prvm_prog_t *prog)
965 VM_SAFEPARMCOUNT(0, VM_spawn);
966 prog->xfunction->builtinsprofile += 20;
967 ed = PRVM_ED_Alloc(prog);
979 void VM_remove(prvm_prog_t *prog)
982 prog->xfunction->builtinsprofile += 20;
984 VM_SAFEPARMCOUNT(1, VM_remove);
986 ed = PRVM_G_EDICT(OFS_PARM0);
987 if( PRVM_NUM_FOR_EDICT(ed) <= prog->reserved_edicts )
989 if (developer.integer > 0)
990 VM_Warning(prog, "VM_remove: tried to remove the null entity or a reserved entity!\n" );
992 else if( ed->priv.required->free )
994 if (developer.integer > 0)
995 VM_Warning(prog, "VM_remove: tried to remove an already freed entity!\n" );
998 PRVM_ED_Free (prog, ed);
1005 entity find(entity start, .string field, string match)
1009 void VM_find(prvm_prog_t *prog)
1016 VM_SAFEPARMCOUNT(3,VM_find);
1018 e = PRVM_G_EDICTNUM(OFS_PARM0);
1019 f = PRVM_G_INT(OFS_PARM1);
1020 s = PRVM_G_STRING(OFS_PARM2);
1022 // LordHavoc: apparently BloodMage does a find(world, weaponmodel, "") and
1023 // expects it to find all the monsters, so we must be careful to support
1026 for (e++ ; e < prog->num_edicts ; e++)
1028 prog->xfunction->builtinsprofile++;
1029 ed = PRVM_EDICT_NUM(e);
1030 if (ed->priv.required->free)
1032 t = PRVM_E_STRING(ed,f);
1037 VM_RETURN_EDICT(ed);
1042 VM_RETURN_EDICT(prog->edicts);
1049 entity findfloat(entity start, .float field, float match)
1050 entity findentity(entity start, .entity field, entity match)
1053 // LordHavoc: added this for searching float, int, and entity reference fields
1054 void VM_findfloat(prvm_prog_t *prog)
1061 VM_SAFEPARMCOUNT(3,VM_findfloat);
1063 e = PRVM_G_EDICTNUM(OFS_PARM0);
1064 f = PRVM_G_INT(OFS_PARM1);
1065 s = PRVM_G_FLOAT(OFS_PARM2);
1067 for (e++ ; e < prog->num_edicts ; e++)
1069 prog->xfunction->builtinsprofile++;
1070 ed = PRVM_EDICT_NUM(e);
1071 if (ed->priv.required->free)
1073 if (PRVM_E_FLOAT(ed,f) == s)
1075 VM_RETURN_EDICT(ed);
1080 VM_RETURN_EDICT(prog->edicts);
1087 entity findchain(.string field, string match)
1090 // chained search for strings in entity fields
1091 // entity(.string field, string match) findchain = #402;
1092 void VM_findchain(prvm_prog_t *prog)
1097 prvm_edict_t *ent, *chain;
1100 VM_SAFEPARMCOUNTRANGE(2,3,VM_findchain);
1103 chainfield = PRVM_G_INT(OFS_PARM2);
1105 chainfield = prog->fieldoffsets.chain;
1107 prog->error_cmd("VM_findchain: %s doesnt have the specified chain field !", prog->name);
1109 chain = prog->edicts;
1111 f = PRVM_G_INT(OFS_PARM0);
1112 s = PRVM_G_STRING(OFS_PARM1);
1114 // LordHavoc: apparently BloodMage does a find(world, weaponmodel, "") and
1115 // expects it to find all the monsters, so we must be careful to support
1118 ent = PRVM_NEXT_EDICT(prog->edicts);
1119 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
1121 prog->xfunction->builtinsprofile++;
1122 if (ent->priv.required->free)
1124 t = PRVM_E_STRING(ent,f);
1130 PRVM_EDICTFIELDEDICT(ent,chainfield) = PRVM_NUM_FOR_EDICT(chain);
1134 VM_RETURN_EDICT(chain);
1141 entity findchainfloat(.string field, float match)
1142 entity findchainentity(.string field, entity match)
1145 // LordHavoc: chained search for float, int, and entity reference fields
1146 // entity(.string field, float match) findchainfloat = #403;
1147 void VM_findchainfloat(prvm_prog_t *prog)
1152 prvm_edict_t *ent, *chain;
1155 VM_SAFEPARMCOUNTRANGE(2, 3, VM_findchainfloat);
1158 chainfield = PRVM_G_INT(OFS_PARM2);
1160 chainfield = prog->fieldoffsets.chain;
1162 prog->error_cmd("VM_findchain: %s doesnt have the specified chain field !", prog->name);
1164 chain = (prvm_edict_t *)prog->edicts;
1166 f = PRVM_G_INT(OFS_PARM0);
1167 s = PRVM_G_FLOAT(OFS_PARM1);
1169 ent = PRVM_NEXT_EDICT(prog->edicts);
1170 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
1172 prog->xfunction->builtinsprofile++;
1173 if (ent->priv.required->free)
1175 if (PRVM_E_FLOAT(ent,f) != s)
1178 PRVM_EDICTFIELDEDICT(ent,chainfield) = PRVM_EDICT_TO_PROG(chain);
1182 VM_RETURN_EDICT(chain);
1186 ========================
1189 entity findflags(entity start, .float field, float match)
1190 ========================
1192 // LordHavoc: search for flags in float fields
1193 void VM_findflags(prvm_prog_t *prog)
1200 VM_SAFEPARMCOUNT(3, VM_findflags);
1203 e = PRVM_G_EDICTNUM(OFS_PARM0);
1204 f = PRVM_G_INT(OFS_PARM1);
1205 s = (prvm_int_t)PRVM_G_FLOAT(OFS_PARM2);
1207 for (e++ ; e < prog->num_edicts ; e++)
1209 prog->xfunction->builtinsprofile++;
1210 ed = PRVM_EDICT_NUM(e);
1211 if (ed->priv.required->free)
1213 if (!PRVM_E_FLOAT(ed,f))
1215 if ((prvm_int_t)PRVM_E_FLOAT(ed,f) & s)
1217 VM_RETURN_EDICT(ed);
1222 VM_RETURN_EDICT(prog->edicts);
1226 ========================
1229 entity findchainflags(.float field, float match)
1230 ========================
1232 // LordHavoc: chained search for flags in float fields
1233 void VM_findchainflags(prvm_prog_t *prog)
1238 prvm_edict_t *ent, *chain;
1241 VM_SAFEPARMCOUNTRANGE(2, 3, VM_findchainflags);
1244 chainfield = PRVM_G_INT(OFS_PARM2);
1246 chainfield = prog->fieldoffsets.chain;
1248 prog->error_cmd("VM_findchain: %s doesnt have the specified chain field !", prog->name);
1250 chain = (prvm_edict_t *)prog->edicts;
1252 f = PRVM_G_INT(OFS_PARM0);
1253 s = (prvm_int_t)PRVM_G_FLOAT(OFS_PARM1);
1255 ent = PRVM_NEXT_EDICT(prog->edicts);
1256 for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
1258 prog->xfunction->builtinsprofile++;
1259 if (ent->priv.required->free)
1261 if (!PRVM_E_FLOAT(ent,f))
1263 if (!((prvm_int_t)PRVM_E_FLOAT(ent,f) & s))
1266 PRVM_EDICTFIELDEDICT(ent,chainfield) = PRVM_EDICT_TO_PROG(chain);
1270 VM_RETURN_EDICT(chain);
1277 string precache_sound (string sample)
1280 void VM_precache_sound(prvm_prog_t *prog)
1284 VM_SAFEPARMCOUNT(1, VM_precache_sound);
1286 s = PRVM_G_STRING(OFS_PARM0);
1287 PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
1288 //VM_CheckEmptyString(prog, s);
1290 if(snd_initialized.integer && !S_PrecacheSound(s, true, true))
1292 VM_Warning(prog, "VM_precache_sound: Failed to load %s for %s\n", s, prog->name);
1301 returns the same string as output
1303 does nothing, only used by qcc to build .pak archives
1306 void VM_precache_file(prvm_prog_t *prog)
1308 VM_SAFEPARMCOUNT(1,VM_precache_file);
1309 // precache_file is only used to copy files with qcc, it does nothing
1310 PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
1320 void VM_coredump(prvm_prog_t *prog)
1322 VM_SAFEPARMCOUNT(0,VM_coredump);
1324 Cbuf_AddText("prvm_edicts ");
1325 Cbuf_AddText(prog->name);
1336 void VM_stackdump(prvm_prog_t *prog)
1338 VM_SAFEPARMCOUNT(0, VM_stackdump);
1340 PRVM_StackTrace(prog);
1351 void VM_crash(prvm_prog_t *prog)
1353 VM_SAFEPARMCOUNT(0, VM_crash);
1355 prog->error_cmd("Crash called by %s",prog->name);
1365 void VM_traceon(prvm_prog_t *prog)
1367 VM_SAFEPARMCOUNT(0,VM_traceon);
1379 void VM_traceoff(prvm_prog_t *prog)
1381 VM_SAFEPARMCOUNT(0,VM_traceoff);
1383 prog->trace = false;
1393 void VM_eprint(prvm_prog_t *prog)
1395 VM_SAFEPARMCOUNT(1,VM_eprint);
1397 PRVM_ED_PrintNum (prog, PRVM_G_EDICTNUM(OFS_PARM0), NULL);
1407 void VM_rint(prvm_prog_t *prog)
1410 VM_SAFEPARMCOUNT(1,VM_rint);
1412 f = PRVM_G_FLOAT(OFS_PARM0);
1414 PRVM_G_FLOAT(OFS_RETURN) = floor(f + 0.5);
1416 PRVM_G_FLOAT(OFS_RETURN) = ceil(f - 0.5);
1426 void VM_floor(prvm_prog_t *prog)
1428 VM_SAFEPARMCOUNT(1,VM_floor);
1430 PRVM_G_FLOAT(OFS_RETURN) = floor(PRVM_G_FLOAT(OFS_PARM0));
1440 void VM_ceil(prvm_prog_t *prog)
1442 VM_SAFEPARMCOUNT(1,VM_ceil);
1444 PRVM_G_FLOAT(OFS_RETURN) = ceil(PRVM_G_FLOAT(OFS_PARM0));
1452 entity nextent(entity)
1455 void VM_nextent(prvm_prog_t *prog)
1460 VM_SAFEPARMCOUNT(1, VM_nextent);
1462 i = PRVM_G_EDICTNUM(OFS_PARM0);
1465 prog->xfunction->builtinsprofile++;
1467 if (i == prog->num_edicts)
1469 VM_RETURN_EDICT(prog->edicts);
1472 ent = PRVM_EDICT_NUM(i);
1473 if (!ent->priv.required->free)
1475 VM_RETURN_EDICT(ent);
1481 //=============================================================================
1488 changelevel(string map)
1491 void VM_changelevel(prvm_prog_t *prog)
1494 VM_SAFEPARMCOUNT(1, VM_changelevel);
1498 VM_Warning(prog, "VM_changelevel: game is not server (%s)\n", prog->name);
1502 // make sure we don't issue two changelevels
1503 if (svs.changelevel_issued)
1505 svs.changelevel_issued = true;
1507 Cbuf_AddText(va(vabuf, sizeof(vabuf), "changelevel %s\n",PRVM_G_STRING(OFS_PARM0)));
1517 void VM_sin(prvm_prog_t *prog)
1519 VM_SAFEPARMCOUNT(1,VM_sin);
1520 PRVM_G_FLOAT(OFS_RETURN) = sin(PRVM_G_FLOAT(OFS_PARM0));
1529 void VM_cos(prvm_prog_t *prog)
1531 VM_SAFEPARMCOUNT(1,VM_cos);
1532 PRVM_G_FLOAT(OFS_RETURN) = cos(PRVM_G_FLOAT(OFS_PARM0));
1542 void VM_sqrt(prvm_prog_t *prog)
1544 VM_SAFEPARMCOUNT(1,VM_sqrt);
1545 PRVM_G_FLOAT(OFS_RETURN) = sqrt(PRVM_G_FLOAT(OFS_PARM0));
1555 void VM_asin(prvm_prog_t *prog)
1557 VM_SAFEPARMCOUNT(1,VM_asin);
1558 PRVM_G_FLOAT(OFS_RETURN) = asin(PRVM_G_FLOAT(OFS_PARM0));
1567 void VM_acos(prvm_prog_t *prog)
1569 VM_SAFEPARMCOUNT(1,VM_acos);
1570 PRVM_G_FLOAT(OFS_RETURN) = acos(PRVM_G_FLOAT(OFS_PARM0));
1579 void VM_atan(prvm_prog_t *prog)
1581 VM_SAFEPARMCOUNT(1,VM_atan);
1582 PRVM_G_FLOAT(OFS_RETURN) = atan(PRVM_G_FLOAT(OFS_PARM0));
1588 float atan2(float,float)
1591 void VM_atan2(prvm_prog_t *prog)
1593 VM_SAFEPARMCOUNT(2,VM_atan2);
1594 PRVM_G_FLOAT(OFS_RETURN) = atan2(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1603 void VM_tan(prvm_prog_t *prog)
1605 VM_SAFEPARMCOUNT(1,VM_tan);
1606 PRVM_G_FLOAT(OFS_RETURN) = tan(PRVM_G_FLOAT(OFS_PARM0));
1613 Returns a vector of length < 1 and > 0
1618 void VM_randomvec(prvm_prog_t *prog)
1621 VM_SAFEPARMCOUNT(0, VM_randomvec);
1623 VectorCopy(temp, PRVM_G_VECTOR(OFS_RETURN));
1626 //=============================================================================
1632 float registercvar (string name, string value[, float flags])
1635 void VM_registercvar(prvm_prog_t *prog)
1637 const char *name, *value;
1640 VM_SAFEPARMCOUNTRANGE(2, 3, VM_registercvar);
1642 name = PRVM_G_STRING(OFS_PARM0);
1643 value = PRVM_G_STRING(OFS_PARM1);
1644 flags = prog->argc >= 3 ? (int)PRVM_G_FLOAT(OFS_PARM2) : 0;
1645 PRVM_G_FLOAT(OFS_RETURN) = 0;
1647 if(flags > CVAR_MAXFLAGSVAL)
1650 // first check to see if it has already been defined
1651 if (Cvar_FindVar (name))
1654 // check for overlap with a command
1655 if (Cmd_Exists (name))
1657 VM_Warning(prog, "VM_registercvar: %s is a command\n", name);
1661 Cvar_Get(name, value, flags, NULL);
1663 PRVM_G_FLOAT(OFS_RETURN) = 1; // success
1671 returns the minimum of two supplied floats
1673 float min(float a, float b, ...[float])
1676 void VM_min(prvm_prog_t *prog)
1678 VM_SAFEPARMCOUNTRANGE(2, 8, VM_min);
1679 // LordHavoc: 3+ argument enhancement suggested by FrikaC
1680 if (prog->argc >= 3)
1683 float f = PRVM_G_FLOAT(OFS_PARM0);
1684 for (i = 1;i < prog->argc;i++)
1685 if (f > PRVM_G_FLOAT((OFS_PARM0+i*3)))
1686 f = PRVM_G_FLOAT((OFS_PARM0+i*3));
1687 PRVM_G_FLOAT(OFS_RETURN) = f;
1690 PRVM_G_FLOAT(OFS_RETURN) = min(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1697 returns the maximum of two supplied floats
1699 float max(float a, float b, ...[float])
1702 void VM_max(prvm_prog_t *prog)
1704 VM_SAFEPARMCOUNTRANGE(2, 8, VM_max);
1705 // LordHavoc: 3+ argument enhancement suggested by FrikaC
1706 if (prog->argc >= 3)
1709 float f = PRVM_G_FLOAT(OFS_PARM0);
1710 for (i = 1;i < prog->argc;i++)
1711 if (f < PRVM_G_FLOAT((OFS_PARM0+i*3)))
1712 f = PRVM_G_FLOAT((OFS_PARM0+i*3));
1713 PRVM_G_FLOAT(OFS_RETURN) = f;
1716 PRVM_G_FLOAT(OFS_RETURN) = max(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1723 returns number bounded by supplied range
1725 float bound(float min, float value, float max)
1728 void VM_bound(prvm_prog_t *prog)
1730 VM_SAFEPARMCOUNT(3,VM_bound);
1731 PRVM_G_FLOAT(OFS_RETURN) = bound(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1), PRVM_G_FLOAT(OFS_PARM2));
1738 returns a raised to power b
1740 float pow(float a, float b)
1743 void VM_pow(prvm_prog_t *prog)
1745 VM_SAFEPARMCOUNT(2,VM_pow);
1746 PRVM_G_FLOAT(OFS_RETURN) = pow(PRVM_G_FLOAT(OFS_PARM0), PRVM_G_FLOAT(OFS_PARM1));
1749 void VM_log(prvm_prog_t *prog)
1751 VM_SAFEPARMCOUNT(1,VM_log);
1752 PRVM_G_FLOAT(OFS_RETURN) = log(PRVM_G_FLOAT(OFS_PARM0));
1755 void VM_Files_Init(prvm_prog_t *prog)
1758 for (i = 0;i < PRVM_MAX_OPENFILES;i++)
1759 prog->openfiles[i] = NULL;
1762 void VM_Files_CloseAll(prvm_prog_t *prog)
1765 for (i = 0;i < PRVM_MAX_OPENFILES;i++)
1767 if (prog->openfiles[i])
1768 FS_Close(prog->openfiles[i]);
1769 prog->openfiles[i] = NULL;
1773 static qfile_t *VM_GetFileHandle(prvm_prog_t *prog, int index)
1775 if (index < 0 || index >= PRVM_MAX_OPENFILES)
1777 Con_Printf("VM_GetFileHandle: invalid file handle %i used in %s\n", index, prog->name);
1780 if (prog->openfiles[index] == NULL)
1782 Con_Printf("VM_GetFileHandle: no such file handle %i (or file has been closed) in %s\n", index, prog->name);
1785 return prog->openfiles[index];
1792 float fopen(string filename, float mode)
1795 // float(string filename, float mode) fopen = #110;
1796 // opens a file inside quake/gamedir/data/ (mode is FILE_READ, FILE_APPEND, or FILE_WRITE),
1797 // returns fhandle >= 0 if successful, or fhandle < 0 if unable to open file for any reason
1798 void VM_fopen(prvm_prog_t *prog)
1801 const char *modestring, *filename;
1804 VM_SAFEPARMCOUNT(2,VM_fopen);
1806 for (filenum = 0;filenum < PRVM_MAX_OPENFILES;filenum++)
1807 if (prog->openfiles[filenum] == NULL)
1809 if (filenum >= PRVM_MAX_OPENFILES)
1811 PRVM_G_FLOAT(OFS_RETURN) = -2;
1812 VM_Warning(prog, "VM_fopen: %s ran out of file handles (%i)\n", prog->name, PRVM_MAX_OPENFILES);
1815 filename = PRVM_G_STRING(OFS_PARM0);
1816 mode = (int)PRVM_G_FLOAT(OFS_PARM1);
1819 case 0: // FILE_READ
1821 prog->openfiles[filenum] = FS_OpenVirtualFile(va(vabuf, sizeof(vabuf), "data/%s", filename), false);
1822 if (prog->openfiles[filenum] == NULL)
1823 prog->openfiles[filenum] = FS_OpenVirtualFile(va(vabuf, sizeof(vabuf), "%s", filename), false);
1825 case 1: // FILE_APPEND
1827 prog->openfiles[filenum] = FS_OpenRealFile(va(vabuf, sizeof(vabuf), "data/%s", filename), modestring, false);
1829 case 2: // FILE_WRITE
1831 prog->openfiles[filenum] = FS_OpenRealFile(va(vabuf, sizeof(vabuf), "data/%s", filename), modestring, false);
1834 PRVM_G_FLOAT(OFS_RETURN) = -3;
1835 VM_Warning(prog, "VM_fopen: %s: no such mode %i (valid: 0 = read, 1 = append, 2 = write)\n", prog->name, mode);
1839 if (prog->openfiles[filenum] == NULL)
1841 PRVM_G_FLOAT(OFS_RETURN) = -1;
1842 if (developer_extra.integer)
1843 VM_Warning(prog, "VM_fopen: %s: %s mode %s failed\n", prog->name, filename, modestring);
1847 PRVM_G_FLOAT(OFS_RETURN) = filenum;
1848 if (developer_extra.integer)
1849 Con_DPrintf("VM_fopen: %s: %s mode %s opened as #%i\n", prog->name, filename, modestring, filenum);
1850 prog->openfiles_origin[filenum] = PRVM_AllocationOrigin(prog);
1858 fclose(float fhandle)
1861 //void(float fhandle) fclose = #111; // closes a file
1862 void VM_fclose(prvm_prog_t *prog)
1866 VM_SAFEPARMCOUNT(1,VM_fclose);
1868 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1869 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
1871 VM_Warning(prog, "VM_fclose: invalid file handle %i used in %s\n", filenum, prog->name);
1874 if (prog->openfiles[filenum] == NULL)
1876 VM_Warning(prog, "VM_fclose: no such file handle %i (or file has been closed) in %s\n", filenum, prog->name);
1879 FS_Close(prog->openfiles[filenum]);
1880 prog->openfiles[filenum] = NULL;
1881 if(prog->openfiles_origin[filenum])
1882 PRVM_Free((char *)prog->openfiles_origin[filenum]);
1883 if (developer_extra.integer)
1884 Con_DPrintf("VM_fclose: %s: #%i closed\n", prog->name, filenum);
1891 string fgets(float fhandle)
1894 //string(float fhandle) fgets = #112; // reads a line of text from the file and returns as a tempstring
1895 void VM_fgets(prvm_prog_t *prog)
1898 char string[VM_STRINGTEMP_LENGTH];
1901 VM_SAFEPARMCOUNT(1,VM_fgets);
1903 // set the return value regardless of any possible errors
1904 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
1906 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1907 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
1909 VM_Warning(prog, "VM_fgets: invalid file handle %i used in %s\n", filenum, prog->name);
1912 if (prog->openfiles[filenum] == NULL)
1914 VM_Warning(prog, "VM_fgets: no such file handle %i (or file has been closed) in %s\n", filenum, prog->name);
1920 c = FS_Getc(prog->openfiles[filenum]);
1921 if (c == '\r' || c == '\n' || c < 0)
1923 if (end < VM_STRINGTEMP_LENGTH - 1)
1927 // remove \n following \r
1930 c = FS_Getc(prog->openfiles[filenum]);
1932 FS_UnGetc(prog->openfiles[filenum], (unsigned char)c);
1934 if (developer_extra.integer)
1935 Con_DPrintf("fgets: %s: %s\n", prog->name, string);
1937 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, string);
1944 fputs(float fhandle, string s)
1947 //void(float fhandle, string s) fputs = #113; // writes a line of text to the end of the file
1948 void VM_fputs(prvm_prog_t *prog)
1951 char string[VM_STRINGTEMP_LENGTH];
1954 VM_SAFEPARMCOUNT(2,VM_fputs);
1956 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
1957 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
1959 VM_Warning(prog, "VM_fputs: invalid file handle %i used in %s\n", filenum, prog->name);
1962 if (prog->openfiles[filenum] == NULL)
1964 VM_Warning(prog, "VM_fputs: no such file handle %i (or file has been closed) in %s\n", filenum, prog->name);
1967 VM_VarString(prog, 1, string, sizeof(string));
1968 if ((stringlength = (int)strlen(string)))
1969 FS_Write(prog->openfiles[filenum], string, stringlength);
1970 if (developer_extra.integer)
1971 Con_DPrintf("fputs: %s: %s\n", prog->name, string);
1978 writetofile(float fhandle, entity ent)
1981 void VM_writetofile(prvm_prog_t *prog)
1986 VM_SAFEPARMCOUNT(2, VM_writetofile);
1988 file = VM_GetFileHandle(prog, (int)PRVM_G_FLOAT(OFS_PARM0));
1991 VM_Warning(prog, "VM_writetofile: invalid or closed file handle\n");
1995 ent = PRVM_G_EDICT(OFS_PARM1);
1996 if(ent->priv.required->free)
1998 VM_Warning(prog, "VM_writetofile: %s: entity %i is free !\n", prog->name, PRVM_NUM_FOR_EDICT(ent));
2002 PRVM_ED_Write (prog, file, ent);
2005 // KrimZon - DP_QC_ENTITYDATA
2010 float() numentityfields
2011 Return the number of entity fields - NOT offsets
2014 void VM_numentityfields(prvm_prog_t *prog)
2016 PRVM_G_FLOAT(OFS_RETURN) = prog->numfielddefs;
2019 // KrimZon - DP_QC_ENTITYDATA
2024 string(float fieldnum) entityfieldname
2025 Return name of the specified field as a string, or empty if the field is invalid (warning)
2028 void VM_entityfieldname(prvm_prog_t *prog)
2031 int i = (int)PRVM_G_FLOAT(OFS_PARM0);
2033 if (i < 0 || i >= prog->numfielddefs)
2035 VM_Warning(prog, "VM_entityfieldname: %s: field index out of bounds\n", prog->name);
2036 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, "");
2040 d = &prog->fielddefs[i];
2041 PRVM_G_INT(OFS_RETURN) = d->s_name; // presuming that s_name points to a string already
2044 // KrimZon - DP_QC_ENTITYDATA
2049 float(float fieldnum) entityfieldtype
2052 void VM_entityfieldtype(prvm_prog_t *prog)
2055 int i = (int)PRVM_G_FLOAT(OFS_PARM0);
2057 if (i < 0 || i >= prog->numfielddefs)
2059 VM_Warning(prog, "VM_entityfieldtype: %s: field index out of bounds\n", prog->name);
2060 PRVM_G_FLOAT(OFS_RETURN) = -1.0;
2064 d = &prog->fielddefs[i];
2065 PRVM_G_FLOAT(OFS_RETURN) = (prvm_vec_t)d->type;
2068 // KrimZon - DP_QC_ENTITYDATA
2071 VM_getentityfieldstring
2073 string(float fieldnum, entity ent) getentityfieldstring
2076 void VM_getentityfieldstring(prvm_prog_t *prog)
2078 // put the data into a string
2083 int i = (int)PRVM_G_FLOAT(OFS_PARM0);
2084 char valuebuf[MAX_INPUTLINE];
2086 if (i < 0 || i >= prog->numfielddefs)
2088 VM_Warning(prog, "VM_entityfielddata: %s: field index out of bounds\n", prog->name);
2089 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, "");
2093 d = &prog->fielddefs[i];
2096 ent = PRVM_G_EDICT(OFS_PARM1);
2097 if(ent->priv.required->free)
2099 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, "");
2100 VM_Warning(prog, "VM_entityfielddata: %s: entity %i is free !\n", prog->name, PRVM_NUM_FOR_EDICT(ent));
2103 val = (prvm_eval_t *)(ent->fields.fp + d->ofs);
2105 // if it's 0 or blank, return an empty string
2106 type = d->type & ~DEF_SAVEGLOBAL;
2107 for (j=0 ; j<prvm_type_size[type] ; j++)
2108 if (val->ivector[j])
2110 if (j == prvm_type_size[type])
2112 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, "");
2116 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, PRVM_UglyValueString(prog, (etype_t)d->type, val, valuebuf, sizeof(valuebuf)));
2119 // KrimZon - DP_QC_ENTITYDATA
2122 VM_putentityfieldstring
2124 float(float fieldnum, entity ent, string s) putentityfieldstring
2127 void VM_putentityfieldstring(prvm_prog_t *prog)
2131 int i = (int)PRVM_G_FLOAT(OFS_PARM0);
2133 if (i < 0 || i >= prog->numfielddefs)
2135 VM_Warning(prog, "VM_entityfielddata: %s: field index out of bounds\n", prog->name);
2136 PRVM_G_FLOAT(OFS_RETURN) = 0.0f;
2140 d = &prog->fielddefs[i];
2143 ent = PRVM_G_EDICT(OFS_PARM1);
2144 if(ent->priv.required->free)
2146 VM_Warning(prog, "VM_entityfielddata: %s: entity %i is free !\n", prog->name, PRVM_NUM_FOR_EDICT(ent));
2147 PRVM_G_FLOAT(OFS_RETURN) = 0.0f;
2151 // parse the string into the value
2152 PRVM_G_FLOAT(OFS_RETURN) = ( PRVM_ED_ParseEpair(prog, ent, d, PRVM_G_STRING(OFS_PARM2), false) ) ? 1.0f : 0.0f;
2159 float strlen(string s)
2162 //float(string s) strlen = #114; // returns how many characters are in a string
2163 void VM_strlen(prvm_prog_t *prog)
2165 VM_SAFEPARMCOUNT(1,VM_strlen);
2167 //PRVM_G_FLOAT(OFS_RETURN) = strlen(PRVM_G_STRING(OFS_PARM0));
2168 PRVM_G_FLOAT(OFS_RETURN) = u8_strlen(PRVM_G_STRING(OFS_PARM0));
2171 // DRESK - Decolorized String
2176 string strdecolorize(string s)
2179 // string (string s) strdecolorize = #472; // returns the passed in string with color codes stripped
2180 void VM_strdecolorize(prvm_prog_t *prog)
2182 char szNewString[VM_STRINGTEMP_LENGTH];
2183 const char *szString;
2186 VM_SAFEPARMCOUNT(1,VM_strdecolorize);
2187 szString = PRVM_G_STRING(OFS_PARM0);
2188 COM_StringDecolorize(szString, 0, szNewString, sizeof(szNewString), TRUE);
2189 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, szNewString);
2192 // DRESK - String Length (not counting color codes)
2197 float strlennocol(string s)
2200 // float(string s) strlennocol = #471; // returns how many characters are in a string not including color codes
2201 // For example, ^2Dresk returns a length of 5
2202 void VM_strlennocol(prvm_prog_t *prog)
2204 const char *szString;
2207 VM_SAFEPARMCOUNT(1,VM_strlennocol);
2209 szString = PRVM_G_STRING(OFS_PARM0);
2211 //nCnt = COM_StringLengthNoColors(szString, 0, NULL);
2212 nCnt = u8_COM_StringLengthNoColors(szString, 0, NULL);
2214 PRVM_G_FLOAT(OFS_RETURN) = nCnt;
2217 // DRESK - String to Uppercase and Lowercase
2222 string strtolower(string s)
2225 // string (string s) strtolower = #480; // returns passed in string in lowercase form
2226 void VM_strtolower(prvm_prog_t *prog)
2228 char szNewString[VM_STRINGTEMP_LENGTH];
2229 const char *szString;
2232 VM_SAFEPARMCOUNT(1,VM_strtolower);
2233 szString = PRVM_G_STRING(OFS_PARM0);
2235 COM_ToLowerString(szString, szNewString, sizeof(szNewString) );
2237 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, szNewString);
2244 string strtoupper(string s)
2247 // string (string s) strtoupper = #481; // returns passed in string in uppercase form
2248 void VM_strtoupper(prvm_prog_t *prog)
2250 char szNewString[VM_STRINGTEMP_LENGTH];
2251 const char *szString;
2254 VM_SAFEPARMCOUNT(1,VM_strtoupper);
2255 szString = PRVM_G_STRING(OFS_PARM0);
2257 COM_ToUpperString(szString, szNewString, sizeof(szNewString) );
2259 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, szNewString);
2266 string strcat(string,string,...[string])
2269 //string(string s1, string s2) strcat = #115;
2270 // concatenates two strings (for example "abc", "def" would return "abcdef")
2271 // and returns as a tempstring
2272 void VM_strcat(prvm_prog_t *prog)
2274 char s[VM_STRINGTEMP_LENGTH];
2275 VM_SAFEPARMCOUNTRANGE(1, 8, VM_strcat);
2277 VM_VarString(prog, 0, s, sizeof(s));
2278 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, s);
2285 string substring(string s, float start, float length)
2288 // string(string s, float start, float length) substring = #116;
2289 // returns a section of a string as a tempstring
2290 void VM_substring(prvm_prog_t *prog)
2293 int u_slength = 0, u_start;
2296 char string[VM_STRINGTEMP_LENGTH];
2298 VM_SAFEPARMCOUNT(3,VM_substring);
2301 s = PRVM_G_STRING(OFS_PARM0);
2302 start = (int)PRVM_G_FLOAT(OFS_PARM1);
2303 length = (int)PRVM_G_FLOAT(OFS_PARM2);
2304 slength = strlen(s);
2306 if (start < 0) // FTE_STRINGS feature
2308 start = bound(0, start, slength);
2310 if (length < 0) // FTE_STRINGS feature
2311 length += slength - start + 1;
2312 maxlen = min((int)sizeof(string) - 1, slength - start);
2313 length = bound(0, length, maxlen);
2315 memcpy(string, s + start, length);
2317 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, string);
2320 s = PRVM_G_STRING(OFS_PARM0);
2321 start = (int)PRVM_G_FLOAT(OFS_PARM1);
2322 length = (int)PRVM_G_FLOAT(OFS_PARM2);
2324 if (start < 0) // FTE_STRINGS feature
2326 u_slength = u8_strlen(s);
2328 start = bound(0, start, u_slength);
2331 if (length < 0) // FTE_STRINGS feature
2333 if (!u_slength) // it's not calculated when it's not needed above
2334 u_slength = u8_strlen(s);
2335 length += u_slength - start + 1;
2338 // positive start, positive length
2339 u_start = u8_byteofs(s, start, NULL);
2342 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, "");
2345 u_length = u8_bytelen(s + u_start, length);
2346 if (u_length >= sizeof(string)-1)
2347 u_length = sizeof(string)-1;
2349 memcpy(string, s + u_start, u_length);
2350 string[u_length] = 0;
2351 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, string);
2358 string(string search, string replace, string subject) strreplace = #484;
2361 // replaces all occurrences of search with replace in the string subject, and returns the result
2362 void VM_strreplace(prvm_prog_t *prog)
2365 const char *search, *replace, *subject;
2366 char string[VM_STRINGTEMP_LENGTH];
2367 int search_len, replace_len, subject_len;
2369 VM_SAFEPARMCOUNT(3,VM_strreplace);
2371 search = PRVM_G_STRING(OFS_PARM0);
2372 replace = PRVM_G_STRING(OFS_PARM1);
2373 subject = PRVM_G_STRING(OFS_PARM2);
2375 search_len = (int)strlen(search);
2376 replace_len = (int)strlen(replace);
2377 subject_len = (int)strlen(subject);
2380 for (i = 0; i <= subject_len - search_len; i++)
2382 for (j = 0; j < search_len; j++) // thus, i+j < subject_len
2383 if (subject[i+j] != search[j])
2385 if (j == search_len)
2387 // NOTE: if search_len == 0, we always hit THIS case, and never the other
2388 // found it at offset 'i'
2389 for (j = 0; j < replace_len && si < (int)sizeof(string) - 1; j++)
2390 string[si++] = replace[j];
2393 i += search_len - 1;
2397 // the above would subtract 1 from i... so we
2398 // don't do that, but instead output the next
2400 if (si < (int)sizeof(string) - 1)
2401 string[si++] = subject[i];
2406 // in THIS case, we know search_len > 0, thus i < subject_len
2408 if (si < (int)sizeof(string) - 1)
2409 string[si++] = subject[i];
2412 // remaining chars (these cannot match)
2413 for (; i < subject_len; i++)
2414 if (si < (int)sizeof(string) - 1)
2415 string[si++] = subject[i];
2418 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, string);
2425 string(string search, string replace, string subject) strireplace = #485;
2428 // case-insensitive version of strreplace
2429 void VM_strireplace(prvm_prog_t *prog)
2432 const char *search, *replace, *subject;
2433 char string[VM_STRINGTEMP_LENGTH];
2434 int search_len, replace_len, subject_len;
2436 VM_SAFEPARMCOUNT(3,VM_strreplace);
2438 search = PRVM_G_STRING(OFS_PARM0);
2439 replace = PRVM_G_STRING(OFS_PARM1);
2440 subject = PRVM_G_STRING(OFS_PARM2);
2442 search_len = (int)strlen(search);
2443 replace_len = (int)strlen(replace);
2444 subject_len = (int)strlen(subject);
2447 for (i = 0; i <= subject_len - search_len; i++)
2449 for (j = 0; j < search_len; j++) // thus, i+j < subject_len
2450 if (tolower(subject[i+j]) != tolower(search[j]))
2452 if (j == search_len)
2454 // NOTE: if search_len == 0, we always hit THIS case, and never the other
2455 // found it at offset 'i'
2456 for (j = 0; j < replace_len && si < (int)sizeof(string) - 1; j++)
2457 string[si++] = replace[j];
2460 i += search_len - 1;
2464 // the above would subtract 1 from i... so we
2465 // don't do that, but instead output the next
2467 if (si < (int)sizeof(string) - 1)
2468 string[si++] = subject[i];
2473 // in THIS case, we know search_len > 0, thus i < subject_len
2475 if (si < (int)sizeof(string) - 1)
2476 string[si++] = subject[i];
2479 // remaining chars (these cannot match)
2480 for (; i < subject_len; i++)
2481 if (si < (int)sizeof(string) - 1)
2482 string[si++] = subject[i];
2485 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, string);
2492 vector stov(string s)
2495 //vector(string s) stov = #117; // returns vector value from a string
2496 void VM_stov(prvm_prog_t *prog)
2498 char string[VM_STRINGTEMP_LENGTH];
2500 VM_SAFEPARMCOUNT(1,VM_stov);
2502 VM_VarString(prog, 0, string, sizeof(string));
2503 Math_atov(string, PRVM_G_VECTOR(OFS_RETURN));
2510 string strzone(string s)
2513 //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)
2514 void VM_strzone(prvm_prog_t *prog)
2517 char string[VM_STRINGTEMP_LENGTH];
2520 VM_SAFEPARMCOUNT(1,VM_strzone);
2522 VM_VarString(prog, 0, string, sizeof(string));
2523 alloclen = strlen(string) + 1;
2524 PRVM_G_INT(OFS_RETURN) = PRVM_AllocString(prog, alloclen, &out);
2525 memcpy(out, string, alloclen);
2535 //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!!!)
2536 void VM_strunzone(prvm_prog_t *prog)
2538 VM_SAFEPARMCOUNT(1,VM_strunzone);
2539 PRVM_FreeString(prog, PRVM_G_INT(OFS_PARM0));
2544 VM_command (used by client and menu)
2546 clientcommand(float client, string s) (for client and menu)
2549 //void(entity e, string s) clientcommand = #440; // executes a command string as if it came from the specified client
2550 //this function originally written by KrimZon, made shorter by LordHavoc
2551 void VM_clcommand (prvm_prog_t *prog)
2553 client_t *temp_client;
2556 VM_SAFEPARMCOUNT(2,VM_clcommand);
2558 i = (int)PRVM_G_FLOAT(OFS_PARM0);
2559 if (!sv.active || i < 0 || i >= svs.maxclients || !svs.clients[i].active)
2561 VM_Warning(prog, "VM_clientcommand: %s: invalid client/server is not active !\n", prog->name);
2565 temp_client = host_client;
2566 host_client = svs.clients + i;
2567 Cmd_ExecuteString (PRVM_G_STRING(OFS_PARM1), src_client, true);
2568 host_client = temp_client;
2576 float tokenize(string s)
2579 //float(string s) tokenize = #441; // takes apart a string into individal words (access them with argv), returns how many
2580 //this function originally written by KrimZon, made shorter by LordHavoc
2581 //20040203: rewritten by LordHavoc (no longer uses allocations)
2582 static int num_tokens = 0;
2583 static int tokens[VM_STRINGTEMP_LENGTH / 2];
2584 static int tokens_startpos[VM_STRINGTEMP_LENGTH / 2];
2585 static int tokens_endpos[VM_STRINGTEMP_LENGTH / 2];
2586 static char tokenize_string[VM_STRINGTEMP_LENGTH];
2587 void VM_tokenize (prvm_prog_t *prog)
2591 VM_SAFEPARMCOUNT(1,VM_tokenize);
2593 strlcpy(tokenize_string, PRVM_G_STRING(OFS_PARM0), sizeof(tokenize_string));
2594 p = tokenize_string;
2599 if (num_tokens >= (int)(sizeof(tokens)/sizeof(tokens[0])))
2602 // skip whitespace here to find token start pos
2603 while(*p && ISWHITESPACE(*p))
2606 tokens_startpos[num_tokens] = p - tokenize_string;
2607 if(!COM_ParseToken_VM_Tokenize(&p, false))
2609 tokens_endpos[num_tokens] = p - tokenize_string;
2610 tokens[num_tokens] = PRVM_SetTempString(prog, com_token);
2614 PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
2617 //float(string s) tokenize = #514; // takes apart a string into individal words (access them with argv), returns how many
2618 void VM_tokenize_console (prvm_prog_t *prog)
2622 VM_SAFEPARMCOUNT(1,VM_tokenize);
2624 strlcpy(tokenize_string, PRVM_G_STRING(OFS_PARM0), sizeof(tokenize_string));
2625 p = tokenize_string;
2630 if (num_tokens >= (int)(sizeof(tokens)/sizeof(tokens[0])))
2633 // skip whitespace here to find token start pos
2634 while(*p && ISWHITESPACE(*p))
2637 tokens_startpos[num_tokens] = p - tokenize_string;
2638 if(!COM_ParseToken_Console(&p))
2640 tokens_endpos[num_tokens] = p - tokenize_string;
2641 tokens[num_tokens] = PRVM_SetTempString(prog, com_token);
2645 PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
2650 VM_tokenizebyseparator
2652 float tokenizebyseparator(string s, string separator1, ...)
2655 //float(string s, string separator1, ...) tokenizebyseparator = #479; // takes apart a string into individal words (access them with argv), returns how many
2656 //this function returns the token preceding each instance of a separator (of
2657 //which there can be multiple), and the text following the last separator
2658 //useful for parsing certain kinds of data like IP addresses
2660 //numnumbers = tokenizebyseparator("10.1.2.3", ".");
2661 //returns 4 and the tokens "10" "1" "2" "3".
2662 void VM_tokenizebyseparator (prvm_prog_t *prog)
2666 int separatorlen[7];
2667 const char *separators[7];
2670 char tokentext[MAX_INPUTLINE];
2672 VM_SAFEPARMCOUNTRANGE(2, 8,VM_tokenizebyseparator);
2674 strlcpy(tokenize_string, PRVM_G_STRING(OFS_PARM0), sizeof(tokenize_string));
2675 p = tokenize_string;
2678 for (j = 1;j < prog->argc;j++)
2680 // skip any blank separator strings
2681 const char *s = PRVM_G_STRING(OFS_PARM0+j*3);
2684 separators[numseparators] = s;
2685 separatorlen[numseparators] = strlen(s);
2692 while (num_tokens < (int)(sizeof(tokens)/sizeof(tokens[0])))
2694 token = tokentext + j;
2695 tokens_startpos[num_tokens] = p - tokenize_string;
2699 for (k = 0;k < numseparators;k++)
2701 if (!strncmp(p, separators[k], separatorlen[k]))
2703 p += separatorlen[k];
2707 if (k < numseparators)
2709 if (j < (int)sizeof(tokentext)-1)
2710 tokentext[j++] = *p;
2714 tokens_endpos[num_tokens] = p0 - tokenize_string;
2715 if (j >= (int)sizeof(tokentext))
2718 tokens[num_tokens++] = PRVM_SetTempString(prog, token);
2723 PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
2726 //string(float n) argv = #442; // returns a word from the tokenized string (returns nothing for an invalid index)
2727 //this function originally written by KrimZon, made shorter by LordHavoc
2728 void VM_argv (prvm_prog_t *prog)
2732 VM_SAFEPARMCOUNT(1,VM_argv);
2734 token_num = (int)PRVM_G_FLOAT(OFS_PARM0);
2737 token_num += num_tokens;
2739 if (token_num >= 0 && token_num < num_tokens)
2740 PRVM_G_INT(OFS_RETURN) = tokens[token_num];
2742 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
2745 //float(float n) argv_start_index = #515; // returns the start index of a token
2746 void VM_argv_start_index (prvm_prog_t *prog)
2750 VM_SAFEPARMCOUNT(1,VM_argv);
2752 token_num = (int)PRVM_G_FLOAT(OFS_PARM0);
2755 token_num += num_tokens;
2757 if (token_num >= 0 && token_num < num_tokens)
2758 PRVM_G_FLOAT(OFS_RETURN) = tokens_startpos[token_num];
2760 PRVM_G_FLOAT(OFS_RETURN) = -1;
2763 //float(float n) argv_end_index = #516; // returns the end index of a token
2764 void VM_argv_end_index (prvm_prog_t *prog)
2768 VM_SAFEPARMCOUNT(1,VM_argv);
2770 token_num = (int)PRVM_G_FLOAT(OFS_PARM0);
2773 token_num += num_tokens;
2775 if (token_num >= 0 && token_num < num_tokens)
2776 PRVM_G_FLOAT(OFS_RETURN) = tokens_endpos[token_num];
2778 PRVM_G_FLOAT(OFS_RETURN) = -1;
2788 void VM_isserver(prvm_prog_t *prog)
2790 VM_SAFEPARMCOUNT(0,VM_serverstate);
2792 PRVM_G_FLOAT(OFS_RETURN) = sv.active && (svs.maxclients > 1 || cls.state == ca_dedicated);
2802 void VM_clientcount(prvm_prog_t *prog)
2804 VM_SAFEPARMCOUNT(0,VM_clientcount);
2806 PRVM_G_FLOAT(OFS_RETURN) = svs.maxclients;
2816 void VM_clientstate(prvm_prog_t *prog)
2818 VM_SAFEPARMCOUNT(0,VM_clientstate);
2821 switch( cls.state ) {
2822 case ca_uninitialized:
2824 PRVM_G_FLOAT(OFS_RETURN) = 0;
2826 case ca_disconnected:
2827 PRVM_G_FLOAT(OFS_RETURN) = 1;
2830 PRVM_G_FLOAT(OFS_RETURN) = 2;
2833 // should never be reached!
2842 float getostype(prvm_prog_t *prog)
2844 */ // not used at the moment -> not included in the common list
2845 void VM_getostype(prvm_prog_t *prog)
2847 VM_SAFEPARMCOUNT(0,VM_getostype);
2852 OS_MAC - not supported
2856 PRVM_G_FLOAT(OFS_RETURN) = 0;
2857 #elif defined(MACOSX)
2858 PRVM_G_FLOAT(OFS_RETURN) = 2;
2860 PRVM_G_FLOAT(OFS_RETURN) = 1;
2868 float gettime(prvm_prog_t *prog)
2871 float CDAudio_GetPosition(void);
2872 void VM_gettime(prvm_prog_t *prog)
2876 VM_SAFEPARMCOUNTRANGE(0,1,VM_gettime);
2880 PRVM_G_FLOAT(OFS_RETURN) = (prvm_vec_t) realtime;
2884 timer_index = (int) PRVM_G_FLOAT(OFS_PARM0);
2887 case 0: // GETTIME_FRAMESTART
2888 PRVM_G_FLOAT(OFS_RETURN) = realtime;
2890 case 1: // GETTIME_REALTIME
2891 PRVM_G_FLOAT(OFS_RETURN) = Sys_DirtyTime();
2893 case 2: // GETTIME_HIRES
2894 PRVM_G_FLOAT(OFS_RETURN) = (Sys_DirtyTime() - host_dirtytime);
2896 case 3: // GETTIME_UPTIME
2897 PRVM_G_FLOAT(OFS_RETURN) = realtime;
2899 case 4: // GETTIME_CDTRACK
2900 PRVM_G_FLOAT(OFS_RETURN) = CDAudio_GetPosition();
2903 VM_Warning(prog, "VM_gettime: %s: unsupported timer specified, returning realtime\n", prog->name);
2904 PRVM_G_FLOAT(OFS_RETURN) = realtime;
2914 float getsoundtime(prvm_prog_t *prog)
2918 void VM_getsoundtime (prvm_prog_t *prog)
2920 int entnum, entchannel;
2921 VM_SAFEPARMCOUNT(2,VM_getsoundtime);
2923 if (prog == SVVM_prog)
2924 entnum = PRVM_NUM_FOR_EDICT(PRVM_G_EDICT(OFS_PARM0));
2925 else if (prog == CLVM_prog)
2926 entnum = MAX_EDICTS + PRVM_NUM_FOR_EDICT(PRVM_G_EDICT(OFS_PARM0));
2929 VM_Warning(prog, "VM_getsoundtime: %s: not supported on this progs\n", prog->name);
2930 PRVM_G_FLOAT(OFS_RETURN) = -1;
2933 entchannel = (int)PRVM_G_FLOAT(OFS_PARM1);
2934 entchannel = CHAN_USER2ENGINE(entchannel);
2935 if (!IS_CHAN(entchannel))
2936 VM_Warning(prog, "VM_getsoundtime: %s: bad channel %i\n", prog->name, entchannel);
2937 PRVM_G_FLOAT(OFS_RETURN) = (prvm_vec_t)S_GetEntChannelPosition(entnum, entchannel);
2944 string soundlength (string sample)
2947 void VM_soundlength (prvm_prog_t *prog)
2951 VM_SAFEPARMCOUNT(1, VM_soundlength);
2953 s = PRVM_G_STRING(OFS_PARM0);
2954 PRVM_G_FLOAT(OFS_RETURN) = S_SoundLength(s);
2961 loadfromdata(string data)
2964 void VM_loadfromdata(prvm_prog_t *prog)
2966 VM_SAFEPARMCOUNT(1,VM_loadentsfromfile);
2968 PRVM_ED_LoadFromFile(prog, PRVM_G_STRING(OFS_PARM0));
2972 ========================
2975 parseentitydata(entity ent, string data)
2976 ========================
2978 void VM_parseentitydata(prvm_prog_t *prog)
2983 VM_SAFEPARMCOUNT(2, VM_parseentitydata);
2985 // get edict and test it
2986 ent = PRVM_G_EDICT(OFS_PARM0);
2987 if (ent->priv.required->free)
2988 prog->error_cmd("VM_parseentitydata: %s: Can only set already spawned entities (entity %i is free)!", prog->name, PRVM_NUM_FOR_EDICT(ent));
2990 data = PRVM_G_STRING(OFS_PARM1);
2992 // parse the opening brace
2993 if (!COM_ParseToken_Simple(&data, false, false, true) || com_token[0] != '{' )
2994 prog->error_cmd("VM_parseentitydata: %s: Couldn't parse entity data:\n%s", prog->name, data );
2996 PRVM_ED_ParseEdict (prog, data, ent);
3003 loadfromfile(string file)
3006 void VM_loadfromfile(prvm_prog_t *prog)
3008 const char *filename;
3011 VM_SAFEPARMCOUNT(1,VM_loadfromfile);
3013 filename = PRVM_G_STRING(OFS_PARM0);
3014 if (FS_CheckNastyPath(filename, false))
3016 PRVM_G_FLOAT(OFS_RETURN) = -4;
3017 VM_Warning(prog, "VM_loadfromfile: %s dangerous or non-portable filename \"%s\" not allowed. (contains : or \\ or begins with .. or /)\n", prog->name, filename);
3021 // not conform with VM_fopen
3022 data = (char *)FS_LoadFile(filename, tempmempool, false, NULL);
3024 PRVM_G_FLOAT(OFS_RETURN) = -1;
3026 PRVM_ED_LoadFromFile(prog, data);
3037 float mod(float val, float m)
3040 void VM_modulo(prvm_prog_t *prog)
3043 VM_SAFEPARMCOUNT(2,VM_module);
3045 val = (prvm_int_t) PRVM_G_FLOAT(OFS_PARM0);
3046 m = (prvm_int_t) PRVM_G_FLOAT(OFS_PARM1);
3048 PRVM_G_FLOAT(OFS_RETURN) = (prvm_vec_t) (val % m);
3051 static void VM_Search_Init(prvm_prog_t *prog)
3054 for (i = 0;i < PRVM_MAX_OPENSEARCHES;i++)
3055 prog->opensearches[i] = NULL;
3058 static void VM_Search_Reset(prvm_prog_t *prog)
3061 // reset the fssearch list
3062 for(i = 0; i < PRVM_MAX_OPENSEARCHES; i++)
3064 if(prog->opensearches[i])
3065 FS_FreeSearch(prog->opensearches[i]);
3066 prog->opensearches[i] = NULL;
3074 float search_begin(string pattern, float caseinsensitive, float quiet)
3077 void VM_search_begin(prvm_prog_t *prog)
3080 const char *pattern;
3081 int caseinsens, quiet;
3083 VM_SAFEPARMCOUNT(3, VM_search_begin);
3085 pattern = PRVM_G_STRING(OFS_PARM0);
3087 VM_CheckEmptyString(prog, pattern);
3089 caseinsens = (int)PRVM_G_FLOAT(OFS_PARM1);
3090 quiet = (int)PRVM_G_FLOAT(OFS_PARM2);
3092 for(handle = 0; handle < PRVM_MAX_OPENSEARCHES; handle++)
3093 if(!prog->opensearches[handle])
3096 if(handle >= PRVM_MAX_OPENSEARCHES)
3098 PRVM_G_FLOAT(OFS_RETURN) = -2;
3099 VM_Warning(prog, "VM_search_begin: %s ran out of search handles (%i)\n", prog->name, PRVM_MAX_OPENSEARCHES);
3103 if(!(prog->opensearches[handle] = FS_Search(pattern,caseinsens, quiet)))
3104 PRVM_G_FLOAT(OFS_RETURN) = -1;
3107 prog->opensearches_origin[handle] = PRVM_AllocationOrigin(prog);
3108 PRVM_G_FLOAT(OFS_RETURN) = handle;
3116 void search_end(float handle)
3119 void VM_search_end(prvm_prog_t *prog)
3122 VM_SAFEPARMCOUNT(1, VM_search_end);
3124 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
3126 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
3128 VM_Warning(prog, "VM_search_end: invalid handle %i used in %s\n", handle, prog->name);
3131 if(prog->opensearches[handle] == NULL)
3133 VM_Warning(prog, "VM_search_end: no such handle %i in %s\n", handle, prog->name);
3137 FS_FreeSearch(prog->opensearches[handle]);
3138 prog->opensearches[handle] = NULL;
3139 if(prog->opensearches_origin[handle])
3140 PRVM_Free((char *)prog->opensearches_origin[handle]);
3147 float search_getsize(float handle)
3150 void VM_search_getsize(prvm_prog_t *prog)
3153 VM_SAFEPARMCOUNT(1, VM_M_search_getsize);
3155 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
3157 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
3159 VM_Warning(prog, "VM_search_getsize: invalid handle %i used in %s\n", handle, prog->name);
3162 if(prog->opensearches[handle] == NULL)
3164 VM_Warning(prog, "VM_search_getsize: no such handle %i in %s\n", handle, prog->name);
3168 PRVM_G_FLOAT(OFS_RETURN) = prog->opensearches[handle]->numfilenames;
3173 VM_search_getfilename
3175 string search_getfilename(float handle, float num)
3178 void VM_search_getfilename(prvm_prog_t *prog)
3180 int handle, filenum;
3181 VM_SAFEPARMCOUNT(2, VM_search_getfilename);
3183 handle = (int)PRVM_G_FLOAT(OFS_PARM0);
3184 filenum = (int)PRVM_G_FLOAT(OFS_PARM1);
3186 if(handle < 0 || handle >= PRVM_MAX_OPENSEARCHES)
3188 VM_Warning(prog, "VM_search_getfilename: invalid handle %i used in %s\n", handle, prog->name);
3191 if(prog->opensearches[handle] == NULL)
3193 VM_Warning(prog, "VM_search_getfilename: no such handle %i in %s\n", handle, prog->name);
3196 if(filenum < 0 || filenum >= prog->opensearches[handle]->numfilenames)
3198 VM_Warning(prog, "VM_search_getfilename: invalid filenum %i in %s\n", filenum, prog->name);
3202 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, prog->opensearches[handle]->filenames[filenum]);
3209 string chr(float ascii)
3212 void VM_chr(prvm_prog_t *prog)
3216 VM_SAFEPARMCOUNT(1, VM_chr);
3218 tmp[0] = (unsigned char) PRVM_G_FLOAT(OFS_PARM0);
3221 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, tmp);
3226 VM_SAFEPARMCOUNT(1, VM_chr);
3228 len = u8_fromchar((Uchar)PRVM_G_FLOAT(OFS_PARM0), tmp, sizeof(tmp));
3230 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, tmp);
3233 //=============================================================================
3234 // Draw builtins (client & menu)
3240 float iscachedpic(string pic)
3243 void VM_iscachedpic(prvm_prog_t *prog)
3245 VM_SAFEPARMCOUNT(1,VM_iscachedpic);
3247 // drawq hasnt such a function, thus always return true
3248 PRVM_G_FLOAT(OFS_RETURN) = false;
3255 string precache_pic(string pic)
3258 #define PRECACHE_PIC_FROMWAD 1 /* FTEQW, not supported here */
3259 #define PRECACHE_PIC_NOTPERSISTENT 2
3260 //#define PRECACHE_PIC_NOCLAMP 4
3261 #define PRECACHE_PIC_MIPMAP 8
3262 void VM_precache_pic(prvm_prog_t *prog)
3267 VM_SAFEPARMCOUNTRANGE(1, 2, VM_precache_pic);
3269 s = PRVM_G_STRING(OFS_PARM0);
3270 PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
3271 VM_CheckEmptyString(prog, s);
3275 int f = PRVM_G_FLOAT(OFS_PARM1);
3276 if(f & PRECACHE_PIC_NOTPERSISTENT)
3277 flags |= CACHEPICFLAG_NOTPERSISTENT;
3278 //if(f & PRECACHE_PIC_NOCLAMP)
3279 // flags |= CACHEPICFLAG_NOCLAMP;
3280 if(f & PRECACHE_PIC_MIPMAP)
3281 flags |= CACHEPICFLAG_MIPMAP;
3284 // AK Draw_CachePic is supposed to always return a valid pointer
3285 if( Draw_CachePic_Flags(s, flags)->tex == r_texture_notexture )
3286 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
3296 void VM_freepic(prvm_prog_t *prog)
3300 VM_SAFEPARMCOUNT(1,VM_freepic);
3302 s = PRVM_G_STRING(OFS_PARM0);
3303 VM_CheckEmptyString(prog, s);
3308 static void getdrawfontscale(prvm_prog_t *prog, float *sx, float *sy)
3312 VectorCopy(PRVM_drawglobalvector(drawfontscale), v);
3313 if(VectorLength2(v) > 0)
3320 static dp_font_t *getdrawfont(prvm_prog_t *prog)
3322 int f = (int) PRVM_drawglobalfloat(drawfont);
3323 if(f < 0 || f >= dp_fonts.maxsize)
3324 return FONT_DEFAULT;
3325 return &dp_fonts.f[f];
3332 float drawcharacter(vector position, float character, vector scale, vector rgb, float alpha, float flag)
3335 void VM_drawcharacter(prvm_prog_t *prog)
3337 prvm_vec_t *pos,*scale,*rgb;
3341 VM_SAFEPARMCOUNT(6,VM_drawcharacter);
3343 character = (char) PRVM_G_FLOAT(OFS_PARM1);
3346 PRVM_G_FLOAT(OFS_RETURN) = -1;
3347 VM_Warning(prog, "VM_drawcharacter: %s passed null character !\n",prog->name);
3351 pos = PRVM_G_VECTOR(OFS_PARM0);
3352 scale = PRVM_G_VECTOR(OFS_PARM2);
3353 rgb = PRVM_G_VECTOR(OFS_PARM3);
3354 flag = (int)PRVM_G_FLOAT(OFS_PARM5);
3356 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3358 PRVM_G_FLOAT(OFS_RETURN) = -2;
3359 VM_Warning(prog, "VM_drawcharacter: %s: wrong DRAWFLAG %i !\n",prog->name,flag);
3363 if(pos[2] || scale[2])
3364 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")));
3366 if(!scale[0] || !scale[1])
3368 PRVM_G_FLOAT(OFS_RETURN) = -3;
3369 VM_Warning(prog, "VM_drawcharacter: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
3373 getdrawfontscale(prog, &sx, &sy);
3374 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));
3375 PRVM_G_FLOAT(OFS_RETURN) = 1;
3382 float drawstring(vector position, string text, vector scale, vector rgb, float alpha[, float flag])
3385 void VM_drawstring(prvm_prog_t *prog)
3387 prvm_vec_t *pos,*scale,*rgb;
3391 VM_SAFEPARMCOUNTRANGE(5,6,VM_drawstring);
3393 string = PRVM_G_STRING(OFS_PARM1);
3394 pos = PRVM_G_VECTOR(OFS_PARM0);
3395 scale = PRVM_G_VECTOR(OFS_PARM2);
3396 rgb = PRVM_G_VECTOR(OFS_PARM3);
3397 if (prog->argc >= 6)
3398 flag = (int)PRVM_G_FLOAT(OFS_PARM5);
3400 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3402 PRVM_G_FLOAT(OFS_RETURN) = -2;
3403 VM_Warning(prog, "VM_drawstring: %s: wrong DRAWFLAG %i !\n",prog->name,flag);
3407 if(!scale[0] || !scale[1])
3409 PRVM_G_FLOAT(OFS_RETURN) = -3;
3410 VM_Warning(prog, "VM_drawstring: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
3414 if(pos[2] || scale[2])
3415 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")));
3417 getdrawfontscale(prog, &sx, &sy);
3418 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));
3419 //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);
3420 PRVM_G_FLOAT(OFS_RETURN) = 1;
3425 VM_drawcolorcodedstring
3427 float drawcolorcodedstring(vector position, string text, vector scale, float alpha, float flag)
3429 float drawcolorcodedstring(vector position, string text, vector scale, vector rgb, float alpha, float flag)
3432 void VM_drawcolorcodedstring(prvm_prog_t *prog)
3434 prvm_vec_t *pos, *scale;
3438 float sx, sy, alpha;
3440 VM_SAFEPARMCOUNTRANGE(5,6,VM_drawcolorcodedstring);
3442 if (prog->argc == 6) // full 6 parms, like normal drawstring
3444 pos = PRVM_G_VECTOR(OFS_PARM0);
3445 string = PRVM_G_STRING(OFS_PARM1);
3446 scale = PRVM_G_VECTOR(OFS_PARM2);
3447 VectorCopy(PRVM_G_VECTOR(OFS_PARM3), rgb);
3448 alpha = PRVM_G_FLOAT(OFS_PARM4);
3449 flag = (int)PRVM_G_FLOAT(OFS_PARM5);
3453 pos = PRVM_G_VECTOR(OFS_PARM0);
3454 string = PRVM_G_STRING(OFS_PARM1);
3455 scale = PRVM_G_VECTOR(OFS_PARM2);
3459 alpha = PRVM_G_FLOAT(OFS_PARM3);
3460 flag = (int)PRVM_G_FLOAT(OFS_PARM4);
3463 if(flag < DRAWFLAG_NORMAL || flag >= DRAWFLAG_NUMFLAGS)
3465 PRVM_G_FLOAT(OFS_RETURN) = -2;
3466 VM_Warning(prog, "VM_drawcolorcodedstring: %s: wrong DRAWFLAG %i !\n",prog->name,flag);
3470 if(!scale[0] || !scale[1])
3472 PRVM_G_FLOAT(OFS_RETURN) = -3;
3473 VM_Warning(prog, "VM_drawcolorcodedstring: scale %s is null !\n", (scale[0] == 0) ? ((scale[1] == 0) ? "x and y" : "x") : "y");
3477 if(pos[2] || scale[2])
3478 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")));
3480 getdrawfontscale(prog, &sx, &sy);
3481 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));
3482 if (prog->argc == 6) // also return vector of last color
3483 VectorCopy(DrawQ_Color, PRVM_G_VECTOR(OFS_RETURN));
3485 PRVM_G_FLOAT(OFS_RETURN) = 1;
3491 float stringwidth(string text, float allowColorCodes, float size)
3494 void VM_stringwidth(prvm_prog_t *prog)
3498 float mult; // sz is intended font size so we can later add freetype support, mult is font size multiplier in pixels per character cell
3502 VM_SAFEPARMCOUNTRANGE(2,3,VM_drawstring);
3504 getdrawfontscale(prog, &sx, &sy);
3507 Vector2Copy(PRVM_G_VECTOR(OFS_PARM2), szv);
3512 // we want the width for 8x8 font size, divided by 8
3513 Vector2Set(szv, 8, 8);
3515 // to make sure snapping is turned off, ALWAYS use a nontrivial scale in this case
3516 if(sx >= 0.9 && sx <= 1.1)
3524 string = PRVM_G_STRING(OFS_PARM0);
3525 colors = (int)PRVM_G_FLOAT(OFS_PARM1);
3527 PRVM_G_FLOAT(OFS_RETURN) = DrawQ_TextWidth_UntilWidth_TrackColors_Scale(string, &maxlen, szv[0], szv[1], sx, sy, NULL, !colors, getdrawfont(prog), 1000000000) * mult;
3531 mult = sz = PRVM_G_FLOAT(OFS_PARM2);
3539 string = PRVM_G_STRING(OFS_PARM0);
3540 colors = (int)PRVM_G_FLOAT(OFS_PARM1);
3542 PRVM_G_FLOAT(OFS_RETURN) = DrawQ_TextWidth(string, 0, !colors, getdrawfont()) * mult; // 1x1 characters, don't actually draw
3550 float findfont(string s)
3554 static float getdrawfontnum(const char *fontname)
3558 for(i = 0; i < dp_fonts.maxsize; ++i)
3559 if(!strcmp(dp_fonts.f[i].title, fontname))
3564 void VM_findfont(prvm_prog_t *prog)
3566 VM_SAFEPARMCOUNT(1,VM_findfont);
3567 PRVM_G_FLOAT(OFS_RETURN) = getdrawfontnum(PRVM_G_STRING(OFS_PARM0));
3574 float loadfont(string fontname, string fontmaps, string sizes, float slot)
3578 void VM_loadfont(prvm_prog_t *prog)
3580 const char *fontname, *filelist, *sizes, *c, *cm;
3581 char mainfont[MAX_QPATH];
3583 float sz, scale, voffset;
3586 VM_SAFEPARMCOUNTRANGE(3,6,VM_loadfont);
3588 fontname = PRVM_G_STRING(OFS_PARM0);
3590 fontname = "default";
3592 filelist = PRVM_G_STRING(OFS_PARM1);
3594 filelist = "gfx/conchars";
3596 sizes = PRVM_G_STRING(OFS_PARM2);
3602 if (prog->argc >= 4)
3604 i = PRVM_G_FLOAT(OFS_PARM3);
3605 if (i >= 0 && i < dp_fonts.maxsize)
3608 strlcpy(f->title, fontname, sizeof(f->title)); // replace name
3612 f = FindFont(fontname, true);
3615 PRVM_G_FLOAT(OFS_RETURN) = -1;
3616 return; // something go wrong
3619 memset(f->fallbacks, 0, sizeof(f->fallbacks));
3620 memset(f->fallback_faces, 0, sizeof(f->fallback_faces));
3622 // first font is handled "normally"
3623 c = strchr(filelist, ':');
3624 cm = strchr(filelist, ',');
3625 if(c && (!cm || c < cm))
3626 f->req_face = atoi(c+1);
3632 if(!c || (c - filelist) > MAX_QPATH)
3633 strlcpy(mainfont, filelist, sizeof(mainfont));
3636 memcpy(mainfont, filelist, c - filelist);
3637 mainfont[c - filelist] = 0;
3641 for(i = 0; i < MAX_FONT_FALLBACKS; ++i)
3643 c = strchr(filelist, ',');
3649 c = strchr(filelist, ':');
3650 cm = strchr(filelist, ',');
3651 if(c && (!cm || c < cm))
3652 f->fallback_faces[i] = atoi(c+1);
3655 f->fallback_faces[i] = 0; // f->req_face; could make it stick to the default-font's face index
3658 if(!c || (c-filelist) > MAX_QPATH)
3660 strlcpy(f->fallbacks[i], filelist, sizeof(mainfont));
3664 memcpy(f->fallbacks[i], filelist, c - filelist);
3665 f->fallbacks[i][c - filelist] = 0;
3670 for(i = 0; i < MAX_FONT_SIZES; ++i)
3671 f->req_sizes[i] = -1;
3672 for (numsizes = 0,c = sizes;;)
3674 if (!COM_ParseToken_VM_Tokenize(&c, 0))
3676 sz = atof(com_token);
3678 if (sz < 0.001f || sz > 1000.0f)
3680 VM_Warning(prog, "VM_loadfont: crap size %s", com_token);
3684 if (numsizes == MAX_FONT_SIZES)
3686 VM_Warning(prog, "VM_loadfont: MAX_FONT_SIZES = %i exceeded", MAX_FONT_SIZES);
3689 f->req_sizes[numsizes] = sz;
3693 // additional scale/hoffset parms
3696 if (prog->argc >= 5)
3698 scale = PRVM_G_FLOAT(OFS_PARM4);
3702 if (prog->argc >= 6)
3703 voffset = PRVM_G_FLOAT(OFS_PARM5);
3706 LoadFont(true, mainfont, f, scale, voffset);
3708 // return index of loaded font
3709 PRVM_G_FLOAT(OFS_RETURN) = (f - dp_fonts.f);
3716 float drawpic(vector position, string pic, vector size, vector rgb, float alpha, float flag)
3719 void VM_drawpic(prvm_prog_t *prog)
3721 const char *picname;
3722 prvm_vec_t *size, *pos, *rgb;
3725 VM_SAFEPARMCOUNTRANGE(5,6,VM_drawpic);
3727 picname = PRVM_G_STRING(OFS_PARM1);
3728 VM_CheckEmptyString(prog, picname);
3730 // is pic cached ? no function yet for that
3733 PRVM_G_FLOAT(OFS_RETURN) = -4;
3734 VM_Warning(prog, "VM_drawpic: %s: %s not cached !\n", prog->name, picname);
3738 pos = PRVM_G_VECTOR(OFS_PARM0);
3739 size = PRVM_G_VECTOR(OFS_PARM2);
3740 rgb = PRVM_G_VECTOR(OFS_PARM3);
3741 if (prog->argc >= 6)
3742 flag = (int) PRVM_G_FLOAT(OFS_PARM5);
3744 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3746 PRVM_G_FLOAT(OFS_RETURN) = -2;
3747 VM_Warning(prog, "VM_drawpic: %s: wrong DRAWFLAG %i !\n",prog->name,flag);
3751 if(pos[2] || size[2])
3752 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")));
3754 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);
3755 PRVM_G_FLOAT(OFS_RETURN) = 1;
3761 float drawrotpic(vector position, string pic, vector size, vector org, float angle, vector rgb, float alpha, float flag)
3764 void VM_drawrotpic(prvm_prog_t *prog)
3766 const char *picname;
3767 prvm_vec_t *size, *pos, *org, *rgb;
3770 VM_SAFEPARMCOUNT(8,VM_drawrotpic);
3772 picname = PRVM_G_STRING(OFS_PARM1);
3773 VM_CheckEmptyString(prog, picname);
3775 // is pic cached ? no function yet for that
3778 PRVM_G_FLOAT(OFS_RETURN) = -4;
3779 VM_Warning(prog, "VM_drawrotpic: %s: %s not cached !\n", prog->name, picname);
3783 pos = PRVM_G_VECTOR(OFS_PARM0);
3784 size = PRVM_G_VECTOR(OFS_PARM2);
3785 org = PRVM_G_VECTOR(OFS_PARM3);
3786 rgb = PRVM_G_VECTOR(OFS_PARM5);
3787 flag = (int) PRVM_G_FLOAT(OFS_PARM7);
3789 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3791 PRVM_G_FLOAT(OFS_RETURN) = -2;
3792 VM_Warning(prog, "VM_drawrotpic: %s: wrong DRAWFLAG %i !\n",prog->name,flag);
3796 if(pos[2] || size[2] || org[2])
3797 VM_Warning(prog, "VM_drawrotpic: z value from pos/size/org discarded\n");
3799 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);
3800 PRVM_G_FLOAT(OFS_RETURN) = 1;
3806 float drawsubpic(vector position, vector size, string pic, vector srcPos, vector srcSize, vector rgb, float alpha, float flag)
3810 void VM_drawsubpic(prvm_prog_t *prog)
3812 const char *picname;
3813 prvm_vec_t *size, *pos, *rgb, *srcPos, *srcSize, alpha;
3816 VM_SAFEPARMCOUNT(8,VM_drawsubpic);
3818 picname = PRVM_G_STRING(OFS_PARM2);
3819 VM_CheckEmptyString(prog, picname);
3821 // is pic cached ? no function yet for that
3824 PRVM_G_FLOAT(OFS_RETURN) = -4;
3825 VM_Warning(prog, "VM_drawsubpic: %s: %s not cached !\n", prog->name, picname);
3829 pos = PRVM_G_VECTOR(OFS_PARM0);
3830 size = PRVM_G_VECTOR(OFS_PARM1);
3831 srcPos = PRVM_G_VECTOR(OFS_PARM3);
3832 srcSize = PRVM_G_VECTOR(OFS_PARM4);
3833 rgb = PRVM_G_VECTOR(OFS_PARM5);
3834 alpha = PRVM_G_FLOAT(OFS_PARM6);
3835 flag = (int) PRVM_G_FLOAT(OFS_PARM7);
3837 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3839 PRVM_G_FLOAT(OFS_RETURN) = -2;
3840 VM_Warning(prog, "VM_drawsubpic: %s: wrong DRAWFLAG %i !\n",prog->name,flag);
3844 if(pos[2] || size[2])
3845 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")));
3847 DrawQ_SuperPic(pos[0], pos[1], Draw_CachePic_Flags (picname, CACHEPICFLAG_NOTPERSISTENT),
3849 srcPos[0], srcPos[1], rgb[0], rgb[1], rgb[2], alpha,
3850 srcPos[0] + srcSize[0], srcPos[1], rgb[0], rgb[1], rgb[2], alpha,
3851 srcPos[0], srcPos[1] + srcSize[1], rgb[0], rgb[1], rgb[2], alpha,
3852 srcPos[0] + srcSize[0], srcPos[1] + srcSize[1], rgb[0], rgb[1], rgb[2], alpha,
3854 PRVM_G_FLOAT(OFS_RETURN) = 1;
3861 float drawfill(vector position, vector size, vector rgb, float alpha, float flag)
3864 void VM_drawfill(prvm_prog_t *prog)
3866 prvm_vec_t *size, *pos, *rgb;
3869 VM_SAFEPARMCOUNT(5,VM_drawfill);
3872 pos = PRVM_G_VECTOR(OFS_PARM0);
3873 size = PRVM_G_VECTOR(OFS_PARM1);
3874 rgb = PRVM_G_VECTOR(OFS_PARM2);
3875 flag = (int) PRVM_G_FLOAT(OFS_PARM4);
3877 if(flag < DRAWFLAG_NORMAL || flag >=DRAWFLAG_NUMFLAGS)
3879 PRVM_G_FLOAT(OFS_RETURN) = -2;
3880 VM_Warning(prog, "VM_drawfill: %s: wrong DRAWFLAG %i !\n",prog->name,flag);
3884 if(pos[2] || size[2])
3885 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")));
3887 DrawQ_Fill(pos[0], pos[1], size[0], size[1], rgb[0], rgb[1], rgb[2], PRVM_G_FLOAT(OFS_PARM3), flag);
3888 PRVM_G_FLOAT(OFS_RETURN) = 1;
3895 drawsetcliparea(float x, float y, float width, float height)
3898 void VM_drawsetcliparea(prvm_prog_t *prog)
3901 VM_SAFEPARMCOUNT(4,VM_drawsetcliparea);
3903 x = bound(0, PRVM_G_FLOAT(OFS_PARM0), vid_conwidth.integer);
3904 y = bound(0, PRVM_G_FLOAT(OFS_PARM1), vid_conheight.integer);
3905 w = bound(0, PRVM_G_FLOAT(OFS_PARM2) + PRVM_G_FLOAT(OFS_PARM0) - x, (vid_conwidth.integer - x));
3906 h = bound(0, PRVM_G_FLOAT(OFS_PARM3) + PRVM_G_FLOAT(OFS_PARM1) - y, (vid_conheight.integer - y));
3908 DrawQ_SetClipArea(x, y, w, h);
3913 VM_drawresetcliparea
3918 void VM_drawresetcliparea(prvm_prog_t *prog)
3920 VM_SAFEPARMCOUNT(0,VM_drawresetcliparea);
3922 DrawQ_ResetClipArea();
3929 vector getimagesize(string pic)
3932 void VM_getimagesize(prvm_prog_t *prog)
3937 VM_SAFEPARMCOUNT(1,VM_getimagesize);
3939 p = PRVM_G_STRING(OFS_PARM0);
3940 VM_CheckEmptyString(prog, p);
3942 pic = Draw_CachePic_Flags (p, CACHEPICFLAG_NOTPERSISTENT);
3943 if( pic->tex == r_texture_notexture )
3945 PRVM_G_VECTOR(OFS_RETURN)[0] = 0;
3946 PRVM_G_VECTOR(OFS_RETURN)[1] = 0;
3950 PRVM_G_VECTOR(OFS_RETURN)[0] = pic->width;
3951 PRVM_G_VECTOR(OFS_RETURN)[1] = pic->height;
3953 PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
3960 string keynumtostring(float keynum)
3963 void VM_keynumtostring (prvm_prog_t *prog)
3966 VM_SAFEPARMCOUNT(1, VM_keynumtostring);
3968 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, Key_KeynumToString((int)PRVM_G_FLOAT(OFS_PARM0), tinystr, sizeof(tinystr)));
3973 VM_findkeysforcommand
3975 string findkeysforcommand(string command, float bindmap)
3977 the returned string is an altstring
3980 #define FKFC_NUMKEYS 5
3981 void M_FindKeysForCommand(const char *command, int *keys);
3982 void VM_findkeysforcommand(prvm_prog_t *prog)
3985 char ret[VM_STRINGTEMP_LENGTH];
3986 int keys[FKFC_NUMKEYS];
3991 VM_SAFEPARMCOUNTRANGE(1, 2, VM_findkeysforcommand);
3993 cmd = PRVM_G_STRING(OFS_PARM0);
3995 bindmap = bound(-1, PRVM_G_FLOAT(OFS_PARM1), MAX_BINDMAPS-1);
3997 bindmap = 0; // consistent to "bind"
3999 VM_CheckEmptyString(prog, cmd);
4001 Key_FindKeysForCommand(cmd, keys, FKFC_NUMKEYS, bindmap);
4004 for(i = 0; i < FKFC_NUMKEYS; i++)
4005 strlcat(ret, va(vabuf, sizeof(vabuf), " \'%i\'", keys[i]), sizeof(ret));
4007 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, ret);
4014 float stringtokeynum(string key)
4017 void VM_stringtokeynum (prvm_prog_t *prog)
4019 VM_SAFEPARMCOUNT( 1, VM_keynumtostring );
4021 PRVM_G_FLOAT(OFS_RETURN) = Key_StringToKeynum(PRVM_G_STRING(OFS_PARM0));
4028 string getkeybind(float key, float bindmap)
4031 void VM_getkeybind (prvm_prog_t *prog)
4034 VM_SAFEPARMCOUNTRANGE(1, 2, VM_CL_getkeybind);
4036 bindmap = bound(-1, PRVM_G_FLOAT(OFS_PARM1), MAX_BINDMAPS-1);
4038 bindmap = 0; // consistent to "bind"
4040 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, Key_GetBind((int)PRVM_G_FLOAT(OFS_PARM0), bindmap));
4047 float setkeybind(float key, string cmd, float bindmap)
4050 void VM_setkeybind (prvm_prog_t *prog)
4053 VM_SAFEPARMCOUNTRANGE(2, 3, VM_CL_setkeybind);
4055 bindmap = bound(-1, PRVM_G_FLOAT(OFS_PARM2), MAX_BINDMAPS-1);
4057 bindmap = 0; // consistent to "bind"
4059 PRVM_G_FLOAT(OFS_RETURN) = 0;
4060 if(Key_SetBinding((int)PRVM_G_FLOAT(OFS_PARM0), bindmap, PRVM_G_STRING(OFS_PARM1)))
4061 PRVM_G_FLOAT(OFS_RETURN) = 1;
4068 vector getbindmaps()
4071 void VM_getbindmaps (prvm_prog_t *prog)
4074 VM_SAFEPARMCOUNT(0, VM_CL_getbindmap);
4075 Key_GetBindMap(&fg, &bg);
4076 PRVM_G_VECTOR(OFS_RETURN)[0] = fg;
4077 PRVM_G_VECTOR(OFS_RETURN)[1] = bg;
4078 PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
4085 float setbindmaps(vector bindmap)
4088 void VM_setbindmaps (prvm_prog_t *prog)
4090 VM_SAFEPARMCOUNT(1, VM_CL_setbindmap);
4091 PRVM_G_FLOAT(OFS_RETURN) = 0;
4092 if(PRVM_G_VECTOR(OFS_PARM0)[2] == 0)
4093 if(Key_SetBindMap((int)PRVM_G_VECTOR(OFS_PARM0)[0], (int)PRVM_G_VECTOR(OFS_PARM0)[1]))
4094 PRVM_G_FLOAT(OFS_RETURN) = 1;
4097 // CL_Video interface functions
4100 ========================
4103 float cin_open(string file, string name)
4104 ========================
4106 void VM_cin_open(prvm_prog_t *prog)
4111 VM_SAFEPARMCOUNT( 2, VM_cin_open );
4113 file = PRVM_G_STRING( OFS_PARM0 );
4114 name = PRVM_G_STRING( OFS_PARM1 );
4116 VM_CheckEmptyString(prog, file );
4117 VM_CheckEmptyString(prog, name );
4119 if( CL_OpenVideo( file, name, MENUOWNER, "" ) )
4120 PRVM_G_FLOAT( OFS_RETURN ) = 1;
4122 PRVM_G_FLOAT( OFS_RETURN ) = 0;
4126 ========================
4129 void cin_close(string name)
4130 ========================
4132 void VM_cin_close(prvm_prog_t *prog)
4136 VM_SAFEPARMCOUNT( 1, VM_cin_close );
4138 name = PRVM_G_STRING( OFS_PARM0 );
4139 VM_CheckEmptyString(prog, name );
4141 CL_CloseVideo( CL_GetVideoByName( name ) );
4145 ========================
4147 void cin_setstate(string name, float type)
4148 ========================
4150 void VM_cin_setstate(prvm_prog_t *prog)
4153 clvideostate_t state;
4156 VM_SAFEPARMCOUNT( 2, VM_cin_netstate );
4158 name = PRVM_G_STRING( OFS_PARM0 );
4159 VM_CheckEmptyString(prog, name );
4161 state = (clvideostate_t)((int)PRVM_G_FLOAT( OFS_PARM1 ));
4163 video = CL_GetVideoByName( name );
4164 if( video && state > CLVIDEO_UNUSED && state < CLVIDEO_STATECOUNT )
4165 CL_SetVideoState( video, state );
4169 ========================
4172 float cin_getstate(string name)
4173 ========================
4175 void VM_cin_getstate(prvm_prog_t *prog)
4180 VM_SAFEPARMCOUNT( 1, VM_cin_getstate );
4182 name = PRVM_G_STRING( OFS_PARM0 );
4183 VM_CheckEmptyString(prog, name );
4185 video = CL_GetVideoByName( name );
4187 PRVM_G_FLOAT( OFS_RETURN ) = (int)video->state;
4189 PRVM_G_FLOAT( OFS_RETURN ) = 0;
4193 ========================
4196 void cin_restart(string name)
4197 ========================
4199 void VM_cin_restart(prvm_prog_t *prog)
4204 VM_SAFEPARMCOUNT( 1, VM_cin_restart );
4206 name = PRVM_G_STRING( OFS_PARM0 );
4207 VM_CheckEmptyString(prog, name );
4209 video = CL_GetVideoByName( name );
4211 CL_RestartVideo( video );
4215 ========================
4218 float[bool] gecko_create( string name )
4219 ========================
4221 void VM_gecko_create(prvm_prog_t *prog) {
4223 PRVM_G_FLOAT( OFS_RETURN ) = 0;
4227 ========================
4230 void gecko_destroy( string name )
4231 ========================
4233 void VM_gecko_destroy(prvm_prog_t *prog) {
4238 ========================
4241 void gecko_navigate( string name, string URI )
4242 ========================
4244 void VM_gecko_navigate(prvm_prog_t *prog) {
4249 ========================
4252 float[bool] gecko_keyevent( string name, float key, float eventtype )
4253 ========================
4255 void VM_gecko_keyevent(prvm_prog_t *prog) {
4257 PRVM_G_FLOAT( OFS_RETURN ) = 0;
4261 ========================
4264 void gecko_mousemove( string name, float x, float y )
4265 ========================
4267 void VM_gecko_movemouse(prvm_prog_t *prog) {
4273 ========================
4276 void gecko_resize( string name, float w, float h )
4277 ========================
4279 void VM_gecko_resize(prvm_prog_t *prog) {
4285 ========================
4286 VM_gecko_get_texture_extent
4288 vector gecko_get_texture_extent( string name )
4289 ========================
4291 void VM_gecko_get_texture_extent(prvm_prog_t *prog) {
4293 PRVM_G_VECTOR(OFS_RETURN)[0] = 0;
4294 PRVM_G_VECTOR(OFS_RETURN)[1] = 0;
4303 Writes new values for v_forward, v_up, and v_right based on angles
4304 void makevectors(vector angle)
4307 void VM_makevectors (prvm_prog_t *prog)
4309 vec3_t angles, forward, right, up;
4310 VM_SAFEPARMCOUNT(1, VM_makevectors);
4311 VectorCopy(PRVM_G_VECTOR(OFS_PARM0), angles);
4312 AngleVectors(angles, forward, right, up);
4313 VectorCopy(forward, PRVM_gameglobalvector(v_forward));
4314 VectorCopy(right, PRVM_gameglobalvector(v_right));
4315 VectorCopy(up, PRVM_gameglobalvector(v_up));
4322 Writes new values for v_forward, v_up, and v_right based on the given forward vector
4323 vectorvectors(vector)
4326 void VM_vectorvectors (prvm_prog_t *prog)
4328 vec3_t forward, right, up;
4329 VM_SAFEPARMCOUNT(1, VM_vectorvectors);
4330 VectorNormalize2(PRVM_G_VECTOR(OFS_PARM0), forward);
4331 VectorVectors(forward, right, up);
4332 VectorCopy(forward, PRVM_gameglobalvector(v_forward));
4333 VectorCopy(right, PRVM_gameglobalvector(v_right));
4334 VectorCopy(up, PRVM_gameglobalvector(v_up));
4338 ========================
4341 void drawline(float width, vector pos1, vector pos2, vector rgb, float alpha, float flags)
4342 ========================
4344 void VM_drawline (prvm_prog_t *prog)
4346 prvm_vec_t *c1, *c2, *rgb;
4348 unsigned char flags;
4350 VM_SAFEPARMCOUNT(6, VM_drawline);
4351 width = PRVM_G_FLOAT(OFS_PARM0);
4352 c1 = PRVM_G_VECTOR(OFS_PARM1);
4353 c2 = PRVM_G_VECTOR(OFS_PARM2);
4354 rgb = PRVM_G_VECTOR(OFS_PARM3);
4355 alpha = PRVM_G_FLOAT(OFS_PARM4);
4356 flags = (int)PRVM_G_FLOAT(OFS_PARM5);
4357 DrawQ_Line(width, c1[0], c1[1], c2[0], c2[1], rgb[0], rgb[1], rgb[2], alpha, flags);
4360 // float(float number, float quantity) bitshift (EXT_BITSHIFT)
4361 void VM_bitshift (prvm_prog_t *prog)
4364 VM_SAFEPARMCOUNT(2, VM_bitshift);
4366 n1 = (prvm_int_t)fabs((prvm_vec_t)((prvm_int_t)PRVM_G_FLOAT(OFS_PARM0)));
4367 n2 = (prvm_int_t)PRVM_G_FLOAT(OFS_PARM1);
4369 PRVM_G_FLOAT(OFS_RETURN) = n1;
4372 PRVM_G_FLOAT(OFS_RETURN) = (n1 >> -n2);
4374 PRVM_G_FLOAT(OFS_RETURN) = (n1 << n2);
4377 ////////////////////////////////////////
4378 // AltString functions
4379 ////////////////////////////////////////
4382 ========================
4385 float altstr_count(string)
4386 ========================
4388 void VM_altstr_count(prvm_prog_t *prog)
4390 const char *altstr, *pos;
4393 VM_SAFEPARMCOUNT( 1, VM_altstr_count );
4395 altstr = PRVM_G_STRING( OFS_PARM0 );
4396 //VM_CheckEmptyString(prog, altstr );
4398 for( count = 0, pos = altstr ; *pos ; pos++ ) {
4399 if( *pos == '\\' ) {
4403 } else if( *pos == '\'' ) {
4408 PRVM_G_FLOAT( OFS_RETURN ) = (prvm_vec_t) (count / 2);
4412 ========================
4415 string altstr_prepare(string)
4416 ========================
4418 void VM_altstr_prepare(prvm_prog_t *prog)
4421 const char *instr, *in;
4423 char outstr[VM_STRINGTEMP_LENGTH];
4425 VM_SAFEPARMCOUNT( 1, VM_altstr_prepare );
4427 instr = PRVM_G_STRING( OFS_PARM0 );
4429 for( out = outstr, in = instr, size = sizeof(outstr) - 1 ; size && *in ; size--, in++, out++ )
4438 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString(prog, outstr );
4442 ========================
4445 string altstr_get(string, float)
4446 ========================
4448 void VM_altstr_get(prvm_prog_t *prog)
4450 const char *altstr, *pos;
4453 char outstr[VM_STRINGTEMP_LENGTH];
4455 VM_SAFEPARMCOUNT( 2, VM_altstr_get );
4457 altstr = PRVM_G_STRING( OFS_PARM0 );
4459 count = (int)PRVM_G_FLOAT( OFS_PARM1 );
4460 count = count * 2 + 1;
4462 for( pos = altstr ; *pos && count ; pos++ )
4463 if( *pos == '\\' ) {
4466 } else if( *pos == '\'' )
4470 PRVM_G_INT( OFS_RETURN ) = 0;
4474 for( out = outstr, size = sizeof(outstr) - 1 ; size && *pos ; size--, pos++, out++ )
4475 if( *pos == '\\' ) {
4480 } else if( *pos == '\'' )
4486 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString(prog, outstr );
4490 ========================
4493 string altstr_set(string altstr, float num, string set)
4494 ========================
4496 void VM_altstr_set(prvm_prog_t *prog)
4499 const char *altstr, *str;
4502 char outstr[VM_STRINGTEMP_LENGTH];
4504 VM_SAFEPARMCOUNT( 3, VM_altstr_set );
4506 altstr = PRVM_G_STRING( OFS_PARM0 );
4508 num = (int)PRVM_G_FLOAT( OFS_PARM1 );
4510 str = PRVM_G_STRING( OFS_PARM2 );
4513 for( num = num * 2 + 1, in = altstr; *in && num; *out++ = *in++ )
4518 } else if( *in == '\'' ) {
4523 for( ; *str; *out++ = *str++ );
4524 // now jump over the old content
4526 if( *in == '\'' || (*in == '\\' && !*++in) )
4529 strlcpy(out, in, outstr + sizeof(outstr) - out);
4530 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString(prog, outstr );
4534 ========================
4537 string altstr_ins(string altstr, float num, string set)
4538 ========================
4540 void VM_altstr_ins(prvm_prog_t *prog)
4546 char outstr[VM_STRINGTEMP_LENGTH];
4548 VM_SAFEPARMCOUNT(3, VM_altstr_ins);
4550 in = PRVM_G_STRING( OFS_PARM0 );
4551 num = (int)PRVM_G_FLOAT( OFS_PARM1 );
4552 set = PRVM_G_STRING( OFS_PARM2 );
4555 for( num = num * 2 + 2 ; *in && num > 0 ; *out++ = *in++ )
4560 } else if( *in == '\'' ) {
4565 for( ; *set ; *out++ = *set++ );
4568 strlcpy(out, in, outstr + sizeof(outstr) - out);
4569 PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString(prog, outstr );
4573 ////////////////////////////////////////
4574 // BufString functions
4575 ////////////////////////////////////////
4576 //[515]: string buffers support
4578 static size_t stringbuffers_sortlength;
4580 static void BufStr_Expand(prvm_prog_t *prog, prvm_stringbuffer_t *stringbuffer, int strindex)
4582 if (stringbuffer->max_strings <= strindex)
4584 char **oldstrings = stringbuffer->strings;
4585 stringbuffer->max_strings = max(stringbuffer->max_strings * 2, 128);
4586 while (stringbuffer->max_strings <= strindex)
4587 stringbuffer->max_strings *= 2;
4588 stringbuffer->strings = (char **) Mem_Alloc(prog->progs_mempool, stringbuffer->max_strings * sizeof(stringbuffer->strings[0]));
4589 if (stringbuffer->num_strings > 0)
4590 memcpy(stringbuffer->strings, oldstrings, stringbuffer->num_strings * sizeof(stringbuffer->strings[0]));
4592 Mem_Free(oldstrings);
4596 static void BufStr_Shrink(prvm_prog_t *prog, prvm_stringbuffer_t *stringbuffer)
4598 // reduce num_strings if there are empty string slots at the end
4599 while (stringbuffer->num_strings > 0 && stringbuffer->strings[stringbuffer->num_strings - 1] == NULL)
4600 stringbuffer->num_strings--;
4602 // if empty, free the string pointer array
4603 if (stringbuffer->num_strings == 0)
4605 stringbuffer->max_strings = 0;
4606 if (stringbuffer->strings)
4607 Mem_Free(stringbuffer->strings);
4608 stringbuffer->strings = NULL;
4612 static int BufStr_SortStringsUP (const void *in1, const void *in2)
4615 a = *((const char **) in1);
4616 b = *((const char **) in2);
4617 if(!a || !a[0]) return 1;
4618 if(!b || !b[0]) return -1;
4619 return strncmp(a, b, stringbuffers_sortlength);
4622 static int BufStr_SortStringsDOWN (const void *in1, const void *in2)
4625 a = *((const char **) in1);
4626 b = *((const char **) in2);
4627 if(!a || !a[0]) return 1;
4628 if(!b || !b[0]) return -1;
4629 return strncmp(b, a, stringbuffers_sortlength);
4633 ========================
4635 creates new buffer, and returns it's index, returns -1 if failed
4636 float buf_create(prvm_prog_t *prog) = #460;
4637 float newbuf(string format, float flags) = #460;
4638 ========================
4641 void VM_buf_create (prvm_prog_t *prog)
4643 prvm_stringbuffer_t *stringbuffer;
4646 VM_SAFEPARMCOUNTRANGE(0, 2, VM_buf_create);
4648 // VorteX: optional parm1 (buffer format) is unfinished, to keep intact with future databuffers extension must be set to "string"
4649 if(prog->argc >= 1 && strcmp(PRVM_G_STRING(OFS_PARM0), "string"))
4651 PRVM_G_FLOAT(OFS_RETURN) = -1;
4654 stringbuffer = (prvm_stringbuffer_t *) Mem_ExpandableArray_AllocRecord(&prog->stringbuffersarray);
4655 for (i = 0;stringbuffer != Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, i);i++);
4656 stringbuffer->origin = PRVM_AllocationOrigin(prog);
4657 // optional flags parm
4658 if (prog->argc >= 2)
4659 stringbuffer->flags = (int)PRVM_G_FLOAT(OFS_PARM1) & 0xFF;
4660 PRVM_G_FLOAT(OFS_RETURN) = i;
4666 ========================
4668 deletes buffer and all strings in it
4669 void buf_del(float bufhandle) = #461;
4670 ========================
4672 void VM_buf_del (prvm_prog_t *prog)
4674 prvm_stringbuffer_t *stringbuffer;
4675 VM_SAFEPARMCOUNT(1, VM_buf_del);
4676 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4680 for (i = 0;i < stringbuffer->num_strings;i++)
4681 if (stringbuffer->strings[i])
4682 Mem_Free(stringbuffer->strings[i]);
4683 if (stringbuffer->strings)
4684 Mem_Free(stringbuffer->strings);
4685 if(stringbuffer->origin)
4686 PRVM_Free((char *)stringbuffer->origin);
4687 Mem_ExpandableArray_FreeRecord(&prog->stringbuffersarray, stringbuffer);
4691 VM_Warning(prog, "VM_buf_del: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), prog->name);
4697 ========================
4699 how many strings are stored in buffer
4700 float buf_getsize(float bufhandle) = #462;
4701 ========================
4703 void VM_buf_getsize (prvm_prog_t *prog)
4705 prvm_stringbuffer_t *stringbuffer;
4706 VM_SAFEPARMCOUNT(1, VM_buf_getsize);
4708 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4711 PRVM_G_FLOAT(OFS_RETURN) = -1;
4712 VM_Warning(prog, "VM_buf_getsize: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), prog->name);
4716 PRVM_G_FLOAT(OFS_RETURN) = stringbuffer->num_strings;
4720 ========================
4722 copy all content from one buffer to another, make sure it exists
4723 void buf_copy(float bufhandle_from, float bufhandle_to) = #463;
4724 ========================
4726 void VM_buf_copy (prvm_prog_t *prog)
4728 prvm_stringbuffer_t *srcstringbuffer, *dststringbuffer;
4730 VM_SAFEPARMCOUNT(2, VM_buf_copy);
4732 srcstringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4733 if(!srcstringbuffer)
4735 VM_Warning(prog, "VM_buf_copy: invalid source buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), prog->name);
4738 i = (int)PRVM_G_FLOAT(OFS_PARM1);
4739 if(i == (int)PRVM_G_FLOAT(OFS_PARM0))
4741 VM_Warning(prog, "VM_buf_copy: source == destination (%i) in %s\n", i, prog->name);
4744 dststringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4745 if(!dststringbuffer)
4747 VM_Warning(prog, "VM_buf_copy: invalid destination buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM1), prog->name);
4751 for (i = 0;i < dststringbuffer->num_strings;i++)
4752 if (dststringbuffer->strings[i])
4753 Mem_Free(dststringbuffer->strings[i]);
4754 if (dststringbuffer->strings)
4755 Mem_Free(dststringbuffer->strings);
4756 *dststringbuffer = *srcstringbuffer;
4757 if (dststringbuffer->max_strings)
4758 dststringbuffer->strings = (char **)Mem_Alloc(prog->progs_mempool, sizeof(dststringbuffer->strings[0]) * dststringbuffer->max_strings);
4760 for (i = 0;i < dststringbuffer->num_strings;i++)
4762 if (srcstringbuffer->strings[i])
4765 stringlen = strlen(srcstringbuffer->strings[i]) + 1;
4766 dststringbuffer->strings[i] = (char *)Mem_Alloc(prog->progs_mempool, stringlen);
4767 memcpy(dststringbuffer->strings[i], srcstringbuffer->strings[i], stringlen);
4773 ========================
4775 sort buffer by beginnings of strings (cmplength defaults it's length)
4776 "backward == TRUE" means that sorting goes upside-down
4777 void buf_sort(float bufhandle, float cmplength, float backward) = #464;
4778 ========================
4780 void VM_buf_sort (prvm_prog_t *prog)
4782 prvm_stringbuffer_t *stringbuffer;
4783 VM_SAFEPARMCOUNT(3, VM_buf_sort);
4785 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4788 VM_Warning(prog, "VM_buf_sort: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), prog->name);
4791 if(stringbuffer->num_strings <= 0)
4793 VM_Warning(prog, "VM_buf_sort: tried to sort empty buffer %i in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), prog->name);
4796 stringbuffers_sortlength = (int)PRVM_G_FLOAT(OFS_PARM1);
4797 if(stringbuffers_sortlength <= 0)
4798 stringbuffers_sortlength = 0x7FFFFFFF;
4800 if(!PRVM_G_FLOAT(OFS_PARM2))
4801 qsort(stringbuffer->strings, stringbuffer->num_strings, sizeof(char*), BufStr_SortStringsUP);
4803 qsort(stringbuffer->strings, stringbuffer->num_strings, sizeof(char*), BufStr_SortStringsDOWN);
4805 BufStr_Shrink(prog, stringbuffer);
4809 ========================
4811 concantenates all buffer string into one with "glue" separator and returns it as tempstring
4812 string buf_implode(float bufhandle, string glue) = #465;
4813 ========================
4815 void VM_buf_implode (prvm_prog_t *prog)
4817 prvm_stringbuffer_t *stringbuffer;
4818 char k[VM_STRINGTEMP_LENGTH];
4822 VM_SAFEPARMCOUNT(2, VM_buf_implode);
4824 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4825 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
4828 VM_Warning(prog, "VM_buf_implode: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), prog->name);
4831 if(!stringbuffer->num_strings)
4833 sep = PRVM_G_STRING(OFS_PARM1);
4835 for(l = i = 0;i < stringbuffer->num_strings;i++)
4837 if(stringbuffer->strings[i])
4839 l += (i > 0 ? strlen(sep) : 0) + strlen(stringbuffer->strings[i]);
4840 if (l >= sizeof(k) - 1)
4842 strlcat(k, sep, sizeof(k));
4843 strlcat(k, stringbuffer->strings[i], sizeof(k));
4846 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, k);
4850 ========================
4852 get a string from buffer, returns tempstring, dont str_unzone it!
4853 string bufstr_get(float bufhandle, float string_index) = #465;
4854 ========================
4856 void VM_bufstr_get (prvm_prog_t *prog)
4858 prvm_stringbuffer_t *stringbuffer;
4860 VM_SAFEPARMCOUNT(2, VM_bufstr_get);
4862 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
4863 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4866 VM_Warning(prog, "VM_bufstr_get: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), prog->name);
4869 strindex = (int)PRVM_G_FLOAT(OFS_PARM1);
4872 // VM_Warning(prog, "VM_bufstr_get: invalid string index %i used in %s\n", strindex, prog->name);
4875 if (strindex < stringbuffer->num_strings && stringbuffer->strings[strindex])
4876 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, stringbuffer->strings[strindex]);
4880 ========================
4882 copies a string into selected slot of buffer
4883 void bufstr_set(float bufhandle, float string_index, string str) = #466;
4884 ========================
4886 void VM_bufstr_set (prvm_prog_t *prog)
4890 prvm_stringbuffer_t *stringbuffer;
4893 VM_SAFEPARMCOUNT(3, VM_bufstr_set);
4895 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4898 VM_Warning(prog, "VM_bufstr_set: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), prog->name);
4901 strindex = (int)PRVM_G_FLOAT(OFS_PARM1);
4902 if(strindex < 0 || strindex >= 1000000) // huge number of strings
4904 VM_Warning(prog, "VM_bufstr_set: invalid string index %i used in %s\n", strindex, prog->name);
4908 BufStr_Expand(prog, stringbuffer, strindex);
4909 stringbuffer->num_strings = max(stringbuffer->num_strings, strindex + 1);
4911 if(stringbuffer->strings[strindex])
4912 Mem_Free(stringbuffer->strings[strindex]);
4913 stringbuffer->strings[strindex] = NULL;
4915 if(PRVM_G_INT(OFS_PARM2))
4917 // not the NULL string!
4918 news = PRVM_G_STRING(OFS_PARM2);
4919 alloclen = strlen(news) + 1;
4920 stringbuffer->strings[strindex] = (char *)Mem_Alloc(prog->progs_mempool, alloclen);
4921 memcpy(stringbuffer->strings[strindex], news, alloclen);
4924 BufStr_Shrink(prog, stringbuffer);
4928 ========================
4930 adds string to buffer in first free slot and returns its index
4931 "order == TRUE" means that string will be added after last "full" slot
4932 float bufstr_add(float bufhandle, string str, float order) = #467;
4933 ========================
4935 void VM_bufstr_add (prvm_prog_t *prog)
4937 int order, strindex;
4938 prvm_stringbuffer_t *stringbuffer;
4942 VM_SAFEPARMCOUNT(3, VM_bufstr_add);
4944 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4945 PRVM_G_FLOAT(OFS_RETURN) = -1;
4948 VM_Warning(prog, "VM_bufstr_add: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), prog->name);
4951 if(!PRVM_G_INT(OFS_PARM1)) // NULL string
4953 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);
4956 string = PRVM_G_STRING(OFS_PARM1);
4957 order = (int)PRVM_G_FLOAT(OFS_PARM2);
4959 strindex = stringbuffer->num_strings;
4961 for (strindex = 0;strindex < stringbuffer->num_strings;strindex++)
4962 if (stringbuffer->strings[strindex] == NULL)
4965 BufStr_Expand(prog, stringbuffer, strindex);
4967 stringbuffer->num_strings = max(stringbuffer->num_strings, strindex + 1);
4968 alloclen = strlen(string) + 1;
4969 stringbuffer->strings[strindex] = (char *)Mem_Alloc(prog->progs_mempool, alloclen);
4970 memcpy(stringbuffer->strings[strindex], string, alloclen);
4972 PRVM_G_FLOAT(OFS_RETURN) = strindex;
4976 ========================
4978 delete string from buffer
4979 void bufstr_free(float bufhandle, float string_index) = #468;
4980 ========================
4982 void VM_bufstr_free (prvm_prog_t *prog)
4985 prvm_stringbuffer_t *stringbuffer;
4986 VM_SAFEPARMCOUNT(2, VM_bufstr_free);
4988 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
4991 VM_Warning(prog, "VM_bufstr_free: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), prog->name);
4994 i = (int)PRVM_G_FLOAT(OFS_PARM1);
4997 VM_Warning(prog, "VM_bufstr_free: invalid string index %i used in %s\n", i, prog->name);
5001 if (i < stringbuffer->num_strings)
5003 if(stringbuffer->strings[i])
5004 Mem_Free(stringbuffer->strings[i]);
5005 stringbuffer->strings[i] = NULL;
5008 BufStr_Shrink(prog, stringbuffer);
5012 ========================
5014 load a file into string buffer, return 0 or 1
5015 float buf_loadfile(string filename, float bufhandle) = #535;
5016 ========================
5018 void VM_buf_loadfile(prvm_prog_t *prog)
5021 prvm_stringbuffer_t *stringbuffer;
5022 char string[VM_STRINGTEMP_LENGTH];
5023 int filenum, strindex, c, end;
5024 const char *filename;
5027 VM_SAFEPARMCOUNT(2, VM_buf_loadfile);
5030 filename = PRVM_G_STRING(OFS_PARM0);
5031 for (filenum = 0;filenum < PRVM_MAX_OPENFILES;filenum++)
5032 if (prog->openfiles[filenum] == NULL)
5034 prog->openfiles[filenum] = FS_OpenVirtualFile(va(vabuf, sizeof(vabuf), "data/%s", filename), false);
5035 if (prog->openfiles[filenum] == NULL)
5036 prog->openfiles[filenum] = FS_OpenVirtualFile(va(vabuf, sizeof(vabuf), "%s", filename), false);
5037 if (prog->openfiles[filenum] == NULL)
5039 if (developer_extra.integer)
5040 VM_Warning(prog, "VM_buf_loadfile: failed to open file %s in %s\n", filename, prog->name);
5041 PRVM_G_FLOAT(OFS_RETURN) = 0;
5045 // get string buffer
5046 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM1));
5049 VM_Warning(prog, "VM_buf_loadfile: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM1), prog->name);
5050 PRVM_G_FLOAT(OFS_RETURN) = 0;
5054 // read file (append to the end of buffer)
5055 strindex = stringbuffer->num_strings;
5062 c = FS_Getc(prog->openfiles[filenum]);
5063 if (c == '\r' || c == '\n' || c < 0)
5065 if (end < VM_STRINGTEMP_LENGTH - 1)
5069 // remove \n following \r
5072 c = FS_Getc(prog->openfiles[filenum]);
5074 FS_UnGetc(prog->openfiles[filenum], (unsigned char)c);
5079 BufStr_Expand(prog, stringbuffer, strindex);
5080 stringbuffer->num_strings = max(stringbuffer->num_strings, strindex + 1);
5081 alloclen = strlen(string) + 1;
5082 stringbuffer->strings[strindex] = (char *)Mem_Alloc(prog->progs_mempool, alloclen);
5083 memcpy(stringbuffer->strings[strindex], string, alloclen);
5084 strindex = stringbuffer->num_strings;
5091 FS_Close(prog->openfiles[filenum]);
5092 prog->openfiles[filenum] = NULL;
5093 if (prog->openfiles_origin[filenum])
5094 PRVM_Free((char *)prog->openfiles_origin[filenum]);
5095 PRVM_G_FLOAT(OFS_RETURN) = 1;
5099 ========================
5101 writes stringbuffer to a file, returns 0 or 1
5102 float buf_writefile(float filehandle, float bufhandle, [, float startpos, float numstrings]) = #468;
5103 ========================
5106 void VM_buf_writefile(prvm_prog_t *prog)
5108 int filenum, strindex, strnum, strlength;
5109 prvm_stringbuffer_t *stringbuffer;
5111 VM_SAFEPARMCOUNTRANGE(2, 4, VM_buf_writefile);
5114 filenum = (int)PRVM_G_FLOAT(OFS_PARM0);
5115 if (filenum < 0 || filenum >= PRVM_MAX_OPENFILES)
5117 VM_Warning(prog, "VM_buf_writefile: invalid file handle %i used in %s\n", filenum, prog->name);
5120 if (prog->openfiles[filenum] == NULL)
5122 VM_Warning(prog, "VM_buf_writefile: no such file handle %i (or file has been closed) in %s\n", filenum, prog->name);
5126 // get string buffer
5127 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM1));
5130 VM_Warning(prog, "VM_buf_writefile: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM1), prog->name);
5131 PRVM_G_FLOAT(OFS_RETURN) = 0;
5135 // get start and end parms
5138 strindex = (int)PRVM_G_FLOAT(OFS_PARM2);
5139 strnum = (int)PRVM_G_FLOAT(OFS_PARM3);
5141 else if (prog->argc > 2)
5143 strindex = (int)PRVM_G_FLOAT(OFS_PARM2);
5144 strnum = stringbuffer->num_strings - strindex;
5149 strnum = stringbuffer->num_strings;
5151 if (strindex < 0 || strindex >= stringbuffer->num_strings)
5153 VM_Warning(prog, "VM_buf_writefile: wrong start string index %i used in %s\n", strindex, prog->name);
5154 PRVM_G_FLOAT(OFS_RETURN) = 0;
5159 VM_Warning(prog, "VM_buf_writefile: wrong strings count %i used in %s\n", strnum, prog->name);
5160 PRVM_G_FLOAT(OFS_RETURN) = 0;
5165 while(strindex < stringbuffer->num_strings && strnum)
5167 if (stringbuffer->strings[strindex])
5169 if ((strlength = strlen(stringbuffer->strings[strindex])))
5170 FS_Write(prog->openfiles[filenum], stringbuffer->strings[strindex], strlength);
5171 FS_Write(prog->openfiles[filenum], "\n", 1);
5177 PRVM_G_FLOAT(OFS_RETURN) = 1;
5180 #define MATCH_AUTO 0
5181 #define MATCH_WHOLE 1
5182 #define MATCH_LEFT 2
5183 #define MATCH_RIGHT 3
5184 #define MATCH_MIDDLE 4
5185 #define MATCH_PATTERN 5
5187 static const char *detect_match_rule(char *pattern, int *matchrule)
5192 patternlength = strlen(pattern);
5193 ppos = strchr(pattern, '*');
5194 qpos = strchr(pattern, '?');
5198 *matchrule = MATCH_PATTERN;
5201 // has * - left, mid, right or pattern
5204 // starts with * - may be right/mid or pattern
5205 if ((ppos - pattern) == 0)
5207 ppos = strchr(pattern+1, '*');
5211 *matchrule = MATCH_RIGHT;
5215 if ((ppos - pattern) == patternlength)
5217 *matchrule = MATCH_MIDDLE;
5222 *matchrule = MATCH_PATTERN;
5225 // end with * - left
5226 if ((ppos - pattern) == patternlength)
5228 *matchrule = MATCH_LEFT;
5233 *matchrule = MATCH_PATTERN;
5236 // have no wildcards - whole string
5237 *matchrule = MATCH_WHOLE;
5241 // todo: support UTF8
5242 static qboolean match_rule(const char *string, int max_string, const char *pattern, int patternlength, int rule)
5247 return !strncmp(string, pattern, max_string) ? true : false;
5249 return !strncmp(string, pattern, patternlength) ? true : false;
5252 mid = strstr(string, pattern);
5253 return mid && !*(mid+patternlength);
5256 return strstr(string, pattern) ? true : false;
5258 return matchpattern_with_separator(string, pattern, false, "", false) ? true : false;
5262 ========================
5264 find an index of bufstring matching rule
5265 float bufstr_find(float bufhandle, string match, float matchrule, float startpos, float step) = #468;
5266 ========================
5269 void VM_bufstr_find(prvm_prog_t *prog)
5271 prvm_stringbuffer_t *stringbuffer;
5272 char string[VM_STRINGTEMP_LENGTH];
5273 int matchrule, matchlen, i, step;
5276 VM_SAFEPARMCOUNTRANGE(3, 5, VM_bufstr_find);
5278 PRVM_G_FLOAT(OFS_RETURN) = -1;
5280 // get string buffer
5281 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
5284 VM_Warning(prog, "VM_bufstr_find: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), prog->name);
5289 matchrule = (int)PRVM_G_FLOAT(OFS_PARM2);
5290 if (matchrule < 0 && matchrule > 5)
5292 VM_Warning(prog, "VM_bufstr_find: invalid match rule %i in %s\n", matchrule, prog->name);
5296 match = PRVM_G_STRING(OFS_PARM1);
5299 strlcpy(string, PRVM_G_STRING(OFS_PARM1), sizeof(string));
5300 match = detect_match_rule(string, &matchrule);
5302 matchlen = strlen(match);
5305 i = (prog->argc > 3) ? (int)PRVM_G_FLOAT(OFS_PARM3) : 0;
5306 step = (prog->argc > 4) ? (int)PRVM_G_FLOAT(OFS_PARM4) : 1;
5307 while(i < stringbuffer->num_strings)
5309 if (stringbuffer->strings[i] && match_rule(stringbuffer->strings[i], VM_STRINGTEMP_LENGTH, match, matchlen, matchrule))
5311 PRVM_G_FLOAT(OFS_RETURN) = i;
5319 ========================
5321 float matchpattern(string s, string pattern, float matchrule, float startpos) = #468;
5322 ========================
5324 void VM_matchpattern(prvm_prog_t *prog)
5326 const char *s, *match;
5327 char string[VM_STRINGTEMP_LENGTH];
5330 VM_SAFEPARMCOUNTRANGE(2, 4, VM_matchpattern);
5332 s = PRVM_G_STRING(OFS_PARM0);
5335 matchrule = (int)PRVM_G_FLOAT(OFS_PARM2);
5336 if (matchrule < 0 && matchrule > 5)
5338 VM_Warning(prog, "VM_bufstr_find: invalid match rule %i in %s\n", matchrule, prog->name);
5342 match = PRVM_G_STRING(OFS_PARM1);
5345 strlcpy(string, PRVM_G_STRING(OFS_PARM1), sizeof(string));
5346 match = detect_match_rule(string, &matchrule);
5352 s += max(0, min((unsigned int)PRVM_G_FLOAT(OFS_PARM3), strlen(s)-1));
5355 PRVM_G_FLOAT(OFS_RETURN) = match_rule(s, VM_STRINGTEMP_LENGTH, match, l, matchrule);
5359 ========================
5361 ========================
5364 void VM_buf_cvarlist(prvm_prog_t *prog)
5367 const char *partial, *antipartial;
5368 size_t len, antilen;
5370 qboolean ispattern, antiispattern;
5372 prvm_stringbuffer_t *stringbuffer;
5373 VM_SAFEPARMCOUNTRANGE(2, 3, VM_buf_cvarlist);
5375 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, (int)PRVM_G_FLOAT(OFS_PARM0));
5378 VM_Warning(prog, "VM_bufstr_free: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), prog->name);
5382 partial = PRVM_G_STRING(OFS_PARM1);
5386 len = strlen(partial);
5389 antipartial = PRVM_G_STRING(OFS_PARM2);
5395 antilen = strlen(antipartial);
5397 for (n = 0;n < stringbuffer->num_strings;n++)
5398 if (stringbuffer->strings[n])
5399 Mem_Free(stringbuffer->strings[n]);
5400 if (stringbuffer->strings)
5401 Mem_Free(stringbuffer->strings);
5402 stringbuffer->strings = NULL;
5404 ispattern = partial && (strchr(partial, '*') || strchr(partial, '?'));
5405 antiispattern = antipartial && (strchr(antipartial, '*') || strchr(antipartial, '?'));
5408 for(cvar = cvar_vars; cvar; cvar = cvar->next)
5410 if(len && (ispattern ? !matchpattern_with_separator(cvar->name, partial, false, "", false) : strncmp(partial, cvar->name, len)))
5413 if(antilen && (antiispattern ? matchpattern_with_separator(cvar->name, antipartial, false, "", false) : !strncmp(antipartial, cvar->name, antilen)))
5419 stringbuffer->max_strings = stringbuffer->num_strings = n;
5420 if (stringbuffer->max_strings)
5421 stringbuffer->strings = (char **)Mem_Alloc(prog->progs_mempool, sizeof(stringbuffer->strings[0]) * stringbuffer->max_strings);
5424 for(cvar = cvar_vars; cvar; cvar = cvar->next)
5426 if(len && (ispattern ? !matchpattern_with_separator(cvar->name, partial, false, "", false) : strncmp(partial, cvar->name, len)))
5429 if(antilen && (antiispattern ? matchpattern_with_separator(cvar->name, antipartial, false, "", false) : !strncmp(antipartial, cvar->name, antilen)))
5432 alloclen = strlen(cvar->name) + 1;
5433 stringbuffer->strings[n] = (char *)Mem_Alloc(prog->progs_mempool, alloclen);
5434 memcpy(stringbuffer->strings[n], cvar->name, alloclen);
5449 This was a major timewaster in progs, so it was converted to C
5452 void VM_changeyaw (prvm_prog_t *prog)
5455 float ideal, current, move, speed;
5457 // this is called (VERY HACKISHLY) by VM_SV_MoveToGoal, so it can not use any
5458 // parameters because they are the parameters to VM_SV_MoveToGoal, not this
5459 //VM_SAFEPARMCOUNT(0, VM_changeyaw);
5461 ent = PRVM_PROG_TO_EDICT(PRVM_gameglobaledict(self));
5462 if (ent == prog->edicts)
5464 VM_Warning(prog, "changeyaw: can not modify world entity\n");
5467 if (ent->priv.server->free)
5469 VM_Warning(prog, "changeyaw: can not modify free entity\n");
5472 current = PRVM_gameedictvector(ent, angles)[1];
5473 current = ANGLEMOD(current);
5474 ideal = PRVM_gameedictfloat(ent, ideal_yaw);
5475 speed = PRVM_gameedictfloat(ent, yaw_speed);
5477 if (current == ideal)
5479 move = ideal - current;
5480 if (ideal > current)
5502 PRVM_gameedictvector(ent, angles)[1] = ANGLEMOD(current);
5510 void VM_changepitch (prvm_prog_t *prog)
5513 float ideal, current, move, speed;
5515 VM_SAFEPARMCOUNT(1, VM_changepitch);
5517 ent = PRVM_G_EDICT(OFS_PARM0);
5518 if (ent == prog->edicts)
5520 VM_Warning(prog, "changepitch: can not modify world entity\n");
5523 if (ent->priv.server->free)
5525 VM_Warning(prog, "changepitch: can not modify free entity\n");
5528 current = PRVM_gameedictvector(ent, angles)[0];
5529 current = ANGLEMOD(current);
5530 ideal = PRVM_gameedictfloat(ent, idealpitch);
5531 speed = PRVM_gameedictfloat(ent, pitch_speed);
5533 if (current == ideal)
5535 move = ideal - current;
5536 if (ideal > current)
5558 PRVM_gameedictvector(ent, angles)[0] = ANGLEMOD(current);
5562 void VM_uncolorstring (prvm_prog_t *prog)
5564 char szNewString[VM_STRINGTEMP_LENGTH];
5565 const char *szString;
5568 VM_SAFEPARMCOUNT(1, VM_uncolorstring);
5569 szString = PRVM_G_STRING(OFS_PARM0);
5570 COM_StringDecolorize(szString, 0, szNewString, sizeof(szNewString), TRUE);
5571 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, szNewString);
5575 // #221 float(string str, string sub[, float startpos]) strstrofs (FTE_STRINGS)
5576 //strstr, without generating a new string. Use in conjunction with FRIK_FILE's substring for more similar strstr.
5577 void VM_strstrofs (prvm_prog_t *prog)
5579 const char *instr, *match;
5581 VM_SAFEPARMCOUNTRANGE(2, 3, VM_strstrofs);
5582 instr = PRVM_G_STRING(OFS_PARM0);
5583 match = PRVM_G_STRING(OFS_PARM1);
5584 firstofs = (prog->argc > 2)?(int)PRVM_G_FLOAT(OFS_PARM2):0;
5585 firstofs = u8_bytelen(instr, firstofs);
5587 if (firstofs && (firstofs < 0 || firstofs > (int)strlen(instr)))
5589 PRVM_G_FLOAT(OFS_RETURN) = -1;
5593 match = strstr(instr+firstofs, match);
5595 PRVM_G_FLOAT(OFS_RETURN) = -1;
5597 PRVM_G_FLOAT(OFS_RETURN) = u8_strnlen(instr, match-instr);
5600 //#222 string(string s, float index) str2chr (FTE_STRINGS)
5601 void VM_str2chr (prvm_prog_t *prog)
5606 VM_SAFEPARMCOUNT(2, VM_str2chr);
5607 s = PRVM_G_STRING(OFS_PARM0);
5608 index = u8_bytelen(s, (int)PRVM_G_FLOAT(OFS_PARM1));
5610 if((unsigned)index < strlen(s))
5612 if (utf8_enable.integer)
5613 ch = u8_getchar_noendptr(s + index);
5615 ch = (unsigned char)s[index];
5616 PRVM_G_FLOAT(OFS_RETURN) = ch;
5619 PRVM_G_FLOAT(OFS_RETURN) = 0;
5622 //#223 string(float c, ...) chr2str (FTE_STRINGS)
5623 void VM_chr2str (prvm_prog_t *prog)
5628 VM_SAFEPARMCOUNTRANGE(0, 8, VM_chr2str);
5629 for(i = 0;i < prog->argc && i < (int)sizeof(t) - 1;i++)
5630 t[i] = (unsigned char)PRVM_G_FLOAT(OFS_PARM0+i*3);
5632 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, t);
5637 VM_SAFEPARMCOUNTRANGE(0, 8, VM_chr2str);
5638 for(i = 0; i < prog->argc && len < sizeof(t)-1; ++i)
5639 len += u8_fromchar((Uchar)PRVM_G_FLOAT(OFS_PARM0+i*3), t + len, sizeof(t)-1);
5641 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, t);
5644 static int chrconv_number(int i, int base, int conv)
5669 static int chrconv_punct(int i, int base, int conv)
5687 static int chrchar_alpha(int i, int basec, int baset, int convc, int convt, int charnum)
5689 //convert case and colour seperatly...
5706 baset = 128*((charnum&1) == (convt-5));
5722 return i + basec + baset;
5724 // #224 string(float ccase, float calpha, float cnum, string s, ...) strconv (FTE_STRINGS)
5725 //bulk convert a string. change case or colouring.
5726 void VM_strconv (prvm_prog_t *prog)
5728 int ccase, redalpha, rednum, len, i;
5729 unsigned char resbuf[VM_STRINGTEMP_LENGTH];
5730 unsigned char *result = resbuf;
5732 VM_SAFEPARMCOUNTRANGE(3, 8, VM_strconv);
5734 ccase = (int) PRVM_G_FLOAT(OFS_PARM0); //0 same, 1 lower, 2 upper
5735 redalpha = (int) PRVM_G_FLOAT(OFS_PARM1); //0 same, 1 white, 2 red, 5 alternate, 6 alternate-alternate
5736 rednum = (int) PRVM_G_FLOAT(OFS_PARM2); //0 same, 1 white, 2 red, 3 redspecial, 4 whitespecial, 5 alternate, 6 alternate-alternate
5737 VM_VarString(prog, 3, (char *) resbuf, sizeof(resbuf));
5738 len = strlen((char *) resbuf);
5740 for (i = 0; i < len; i++, result++) //should this be done backwards?
5742 if (*result >= '0' && *result <= '9') //normal numbers...
5743 *result = chrconv_number(*result, '0', rednum);
5744 else if (*result >= '0'+128 && *result <= '9'+128)
5745 *result = chrconv_number(*result, '0'+128, rednum);
5746 else if (*result >= '0'+128-30 && *result <= '9'+128-30)
5747 *result = chrconv_number(*result, '0'+128-30, rednum);
5748 else if (*result >= '0'-30 && *result <= '9'-30)
5749 *result = chrconv_number(*result, '0'-30, rednum);
5751 else if (*result >= 'a' && *result <= 'z') //normal numbers...
5752 *result = chrchar_alpha(*result, 'a', 0, ccase, redalpha, i);
5753 else if (*result >= 'A' && *result <= 'Z') //normal numbers...
5754 *result = chrchar_alpha(*result, 'A', 0, ccase, redalpha, i);
5755 else if (*result >= 'a'+128 && *result <= 'z'+128) //normal numbers...
5756 *result = chrchar_alpha(*result, 'a', 128, ccase, redalpha, i);
5757 else if (*result >= 'A'+128 && *result <= 'Z'+128) //normal numbers...
5758 *result = chrchar_alpha(*result, 'A', 128, ccase, redalpha, i);
5760 else if ((*result & 127) < 16 || !redalpha) //special chars..
5762 else if (*result < 128)
5763 *result = chrconv_punct(*result, 0, redalpha);
5765 *result = chrconv_punct(*result, 128, redalpha);
5769 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, (char *) resbuf);
5772 // #225 string(float chars, string s, ...) strpad (FTE_STRINGS)
5773 void VM_strpad (prvm_prog_t *prog)
5775 char src[VM_STRINGTEMP_LENGTH];
5776 char destbuf[VM_STRINGTEMP_LENGTH];
5778 VM_SAFEPARMCOUNTRANGE(1, 8, VM_strpad);
5779 pad = (int) PRVM_G_FLOAT(OFS_PARM0);
5780 VM_VarString(prog, 1, src, sizeof(src));
5782 // note: < 0 = left padding, > 0 = right padding,
5783 // this is reverse logic of printf!
5784 dpsnprintf(destbuf, sizeof(destbuf), "%*s", -pad, src);
5786 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, destbuf);
5789 // #226 string(string info, string key, string value, ...) infoadd (FTE_STRINGS)
5790 //uses qw style \key\value strings
5791 void VM_infoadd (prvm_prog_t *prog)
5793 const char *info, *key;
5794 char value[VM_STRINGTEMP_LENGTH];
5795 char temp[VM_STRINGTEMP_LENGTH];
5797 VM_SAFEPARMCOUNTRANGE(2, 8, VM_infoadd);
5798 info = PRVM_G_STRING(OFS_PARM0);
5799 key = PRVM_G_STRING(OFS_PARM1);
5800 VM_VarString(prog, 2, value, sizeof(value));
5802 strlcpy(temp, info, VM_STRINGTEMP_LENGTH);
5804 InfoString_SetValue(temp, VM_STRINGTEMP_LENGTH, key, value);
5806 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, temp);
5809 // #227 string(string info, string key) infoget (FTE_STRINGS)
5810 //uses qw style \key\value strings
5811 void VM_infoget (prvm_prog_t *prog)
5815 char value[VM_STRINGTEMP_LENGTH];
5817 VM_SAFEPARMCOUNT(2, VM_infoget);
5818 info = PRVM_G_STRING(OFS_PARM0);
5819 key = PRVM_G_STRING(OFS_PARM1);
5821 InfoString_GetValue(info, key, value, VM_STRINGTEMP_LENGTH);
5823 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, value);
5826 //#228 float(string s1, string s2, float len) strncmp (FTE_STRINGS)
5827 // also float(string s1, string s2) strcmp (FRIK_FILE)
5828 void VM_strncmp (prvm_prog_t *prog)
5830 const char *s1, *s2;
5831 VM_SAFEPARMCOUNTRANGE(2, 3, VM_strncmp);
5832 s1 = PRVM_G_STRING(OFS_PARM0);
5833 s2 = PRVM_G_STRING(OFS_PARM1);
5836 PRVM_G_FLOAT(OFS_RETURN) = strncmp(s1, s2, (size_t)PRVM_G_FLOAT(OFS_PARM2));
5840 PRVM_G_FLOAT(OFS_RETURN) = strcmp(s1, s2);
5844 // #229 float(string s1, string s2) strcasecmp (FTE_STRINGS)
5845 // #230 float(string s1, string s2, float len) strncasecmp (FTE_STRINGS)
5846 void VM_strncasecmp (prvm_prog_t *prog)
5848 const char *s1, *s2;
5849 VM_SAFEPARMCOUNTRANGE(2, 3, VM_strncasecmp);
5850 s1 = PRVM_G_STRING(OFS_PARM0);
5851 s2 = PRVM_G_STRING(OFS_PARM1);
5854 PRVM_G_FLOAT(OFS_RETURN) = strncasecmp(s1, s2, (size_t)PRVM_G_FLOAT(OFS_PARM2));
5858 PRVM_G_FLOAT(OFS_RETURN) = strcasecmp(s1, s2);
5862 // #494 float(float caseinsensitive, string s, ...) crc16
5863 void VM_crc16(prvm_prog_t *prog)
5866 char s[VM_STRINGTEMP_LENGTH];
5867 VM_SAFEPARMCOUNTRANGE(2, 8, VM_crc16);
5868 insensitive = PRVM_G_FLOAT(OFS_PARM0);
5869 VM_VarString(prog, 1, s, sizeof(s));
5870 PRVM_G_FLOAT(OFS_RETURN) = (unsigned short) ((insensitive ? CRC_Block_CaseInsensitive : CRC_Block) ((unsigned char *) s, strlen(s)));
5873 // #639 float(string digest, string data, ...) digest_hex
5874 void VM_digest_hex(prvm_prog_t *prog)
5882 char s[VM_STRINGTEMP_LENGTH];
5885 VM_SAFEPARMCOUNTRANGE(2, 8, VM_digest_hex);
5886 digest = PRVM_G_STRING(OFS_PARM0);
5889 VM_VarString(prog, 1, s, sizeof(s));
5894 if(!strcmp(digest, "MD4"))
5897 mdfour((unsigned char *) out, (unsigned char *) s, len);
5899 else if(!strcmp(digest, "SHA256") && Crypto_Available())
5902 sha256((unsigned char *) out, (unsigned char *) s, len);
5904 // no warning needed on mismatch - we return string_null to QC
5909 static const char *hexmap = "0123456789abcdef";
5910 for(i = 0; i < outlen; ++i)
5912 outhex[2*i] = hexmap[(out[i] >> 4) & 15];
5913 outhex[2*i+1] = hexmap[(out[i] >> 0) & 15];
5916 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, outhex);
5919 PRVM_G_INT(OFS_RETURN) = 0;
5922 void VM_wasfreed (prvm_prog_t *prog)
5924 VM_SAFEPARMCOUNT(1, VM_wasfreed);
5925 PRVM_G_FLOAT(OFS_RETURN) = PRVM_G_EDICT(OFS_PARM0)->priv.required->free;
5928 void VM_SetTraceGlobals(prvm_prog_t *prog, const trace_t *trace)
5930 PRVM_gameglobalfloat(trace_allsolid) = trace->allsolid;
5931 PRVM_gameglobalfloat(trace_startsolid) = trace->startsolid;
5932 PRVM_gameglobalfloat(trace_fraction) = trace->fraction;
5933 PRVM_gameglobalfloat(trace_inwater) = trace->inwater;
5934 PRVM_gameglobalfloat(trace_inopen) = trace->inopen;
5935 VectorCopy(trace->endpos, PRVM_gameglobalvector(trace_endpos));
5936 VectorCopy(trace->plane.normal, PRVM_gameglobalvector(trace_plane_normal));
5937 PRVM_gameglobalfloat(trace_plane_dist) = trace->plane.dist;
5938 PRVM_gameglobaledict(trace_ent) = PRVM_EDICT_TO_PROG(trace->ent ? trace->ent : prog->edicts);
5939 PRVM_gameglobalfloat(trace_dpstartcontents) = trace->startsupercontents;
5940 PRVM_gameglobalfloat(trace_dphitcontents) = trace->hitsupercontents;
5941 PRVM_gameglobalfloat(trace_dphitq3surfaceflags) = trace->hitq3surfaceflags;
5942 PRVM_gameglobalstring(trace_dphittexturename) = trace->hittexture ? PRVM_SetTempString(prog, trace->hittexture->name) : 0;
5945 void VM_ClearTraceGlobals(prvm_prog_t *prog)
5947 // clean up all trace globals when leaving the VM (anti-triggerbot safeguard)
5948 PRVM_gameglobalfloat(trace_allsolid) = 0;
5949 PRVM_gameglobalfloat(trace_startsolid) = 0;
5950 PRVM_gameglobalfloat(trace_fraction) = 0;
5951 PRVM_gameglobalfloat(trace_inwater) = 0;
5952 PRVM_gameglobalfloat(trace_inopen) = 0;
5953 VectorClear(PRVM_gameglobalvector(trace_endpos));
5954 VectorClear(PRVM_gameglobalvector(trace_plane_normal));
5955 PRVM_gameglobalfloat(trace_plane_dist) = 0;
5956 PRVM_gameglobaledict(trace_ent) = PRVM_EDICT_TO_PROG(prog->edicts);
5957 PRVM_gameglobalfloat(trace_dpstartcontents) = 0;
5958 PRVM_gameglobalfloat(trace_dphitcontents) = 0;
5959 PRVM_gameglobalfloat(trace_dphitq3surfaceflags) = 0;
5960 PRVM_gameglobalstring(trace_dphittexturename) = 0;
5965 void VM_Cmd_Init(prvm_prog_t *prog)
5967 // only init the stuff for the current prog
5968 VM_Files_Init(prog);
5969 VM_Search_Init(prog);
5972 void VM_Cmd_Reset(prvm_prog_t *prog)
5974 CL_PurgeOwner( MENUOWNER );
5975 VM_Search_Reset(prog);
5976 VM_Files_CloseAll(prog);
5979 // #510 string(string input, ...) uri_escape (DP_QC_URI_ESCAPE)
5980 // does URI escaping on a string (replace evil stuff by %AB escapes)
5981 void VM_uri_escape (prvm_prog_t *prog)
5983 char src[VM_STRINGTEMP_LENGTH];
5984 char dest[VM_STRINGTEMP_LENGTH];
5986 static const char *hex = "0123456789ABCDEF";
5988 VM_SAFEPARMCOUNTRANGE(1, 8, VM_uri_escape);
5989 VM_VarString(prog, 0, src, sizeof(src));
5991 for(p = src, q = dest; *p && q < dest + sizeof(dest) - 3; ++p)
5993 if((*p >= 'A' && *p <= 'Z')
5994 || (*p >= 'a' && *p <= 'z')
5995 || (*p >= '0' && *p <= '9')
5996 || (*p == '-') || (*p == '_') || (*p == '.')
5997 || (*p == '!') || (*p == '~')
5998 || (*p == '\'') || (*p == '(') || (*p == ')'))
6003 *q++ = hex[(*(unsigned char *)p >> 4) & 0xF];
6004 *q++ = hex[ *(unsigned char *)p & 0xF];
6009 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, dest);
6012 // #510 string(string input, ...) uri_unescape (DP_QC_URI_ESCAPE)
6013 // does URI unescaping on a string (get back the evil stuff)
6014 void VM_uri_unescape (prvm_prog_t *prog)
6016 char src[VM_STRINGTEMP_LENGTH];
6017 char dest[VM_STRINGTEMP_LENGTH];
6021 VM_SAFEPARMCOUNTRANGE(1, 8, VM_uri_unescape);
6022 VM_VarString(prog, 0, src, sizeof(src));
6024 for(p = src, q = dest; *p; ) // no need to check size, because unescape can't expand
6028 if(p[1] >= '0' && p[1] <= '9')
6030 else if(p[1] >= 'a' && p[1] <= 'f')
6031 hi = p[1] - 'a' + 10;
6032 else if(p[1] >= 'A' && p[1] <= 'F')
6033 hi = p[1] - 'A' + 10;
6036 if(p[2] >= '0' && p[2] <= '9')
6038 else if(p[2] >= 'a' && p[2] <= 'f')
6039 lo = p[2] - 'a' + 10;
6040 else if(p[2] >= 'A' && p[2] <= 'F')
6041 lo = p[2] - 'A' + 10;
6044 if(hi != 0 || lo != 0) // don't unescape NUL bytes
6045 *q++ = (char) (hi * 0x10 + lo);
6056 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, dest);
6059 // #502 string(string filename) whichpack (DP_QC_WHICHPACK)
6060 // returns the name of the pack containing a file, or "" if it is not in any pack (but local or non-existant)
6061 void VM_whichpack (prvm_prog_t *prog)
6063 const char *fn, *pack;
6065 VM_SAFEPARMCOUNT(1, VM_whichpack);
6066 fn = PRVM_G_STRING(OFS_PARM0);
6067 pack = FS_WhichPack(fn);
6069 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, pack ? pack : "");
6077 char buffer[MAX_INPUTLINE];
6078 unsigned char *postdata; // free when uri_to_prog_t is freed
6080 char *sigdata; // free when uri_to_prog_t is freed
6085 static void uri_to_string_callback(int status, size_t length_received, unsigned char *buffer, void *cbdata)
6088 uri_to_prog_t *handle = (uri_to_prog_t *) cbdata;
6090 prog = handle->prog;
6093 // curl reply came too late... so just drop it
6094 if(handle->postdata)
6095 Z_Free(handle->postdata);
6097 Z_Free(handle->sigdata);
6102 if((prog->starttime == handle->starttime) && (PRVM_allfunction(URI_Get_Callback)))
6104 if(length_received >= sizeof(handle->buffer))
6105 length_received = sizeof(handle->buffer) - 1;
6106 handle->buffer[length_received] = 0;
6108 PRVM_G_FLOAT(OFS_PARM0) = handle->id;
6109 PRVM_G_FLOAT(OFS_PARM1) = status;
6110 PRVM_G_INT(OFS_PARM2) = PRVM_SetTempString(prog, handle->buffer);
6111 prog->ExecuteProgram(prog, PRVM_allfunction(URI_Get_Callback), "QC function URI_Get_Callback is missing");
6114 if(handle->postdata)
6115 Z_Free(handle->postdata);
6117 Z_Free(handle->sigdata);
6121 // 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
6122 // 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
6123 void VM_uri_get (prvm_prog_t *prog)
6128 uri_to_prog_t *handle;
6129 const char *posttype = NULL;
6130 const char *postseparator = NULL;
6131 int poststringbuffer = -1;
6133 const char *query_string = NULL;
6136 if(!PRVM_allfunction(URI_Get_Callback))
6137 prog->error_cmd("uri_get called by %s without URI_Get_Callback defined", prog->name);
6139 VM_SAFEPARMCOUNTRANGE(2, 6, VM_uri_get);
6141 url = PRVM_G_STRING(OFS_PARM0);
6142 id = PRVM_G_FLOAT(OFS_PARM1);
6144 posttype = PRVM_G_STRING(OFS_PARM2);
6146 postseparator = PRVM_G_STRING(OFS_PARM3);
6148 poststringbuffer = PRVM_G_FLOAT(OFS_PARM4);
6150 postkeyid = PRVM_G_FLOAT(OFS_PARM5);
6151 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!
6153 query_string = strchr(url, '?');
6156 lq = query_string ? strlen(query_string) : 0;
6158 handle->prog = prog;
6159 handle->starttime = prog->starttime;
6161 if(postseparator && posttype && *posttype)
6163 size_t l = strlen(postseparator);
6164 if(poststringbuffer >= 0)
6169 prvm_stringbuffer_t *stringbuffer;
6170 stringbuffer = (prvm_stringbuffer_t *)Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, poststringbuffer);
6173 VM_Warning(prog, "uri_get: invalid buffer %i used in %s\n", (int)PRVM_G_FLOAT(OFS_PARM0), prog->name);
6177 for(i = 0;i < stringbuffer->num_strings;i++)
6181 if(stringbuffer->strings[i])
6182 ltotal += strlen(stringbuffer->strings[i]);
6184 handle->postdata = (unsigned char *)Z_Malloc(ltotal + 1 + lq);
6185 handle->postlen = ltotal;
6187 for(i = 0;i < stringbuffer->num_strings;i++)
6191 memcpy(handle->postdata + ltotal, postseparator, l);
6194 if(stringbuffer->strings[i])
6196 memcpy(handle->postdata + ltotal, stringbuffer->strings[i], strlen(stringbuffer->strings[i]));
6197 ltotal += strlen(stringbuffer->strings[i]);
6200 if(ltotal != handle->postlen)
6201 prog->error_cmd("%s: string buffer content size mismatch, possible overrun", prog->name);
6205 handle->postdata = (unsigned char *)Z_Malloc(l + 1 + lq);
6206 handle->postlen = l;
6207 memcpy(handle->postdata, postseparator, l);
6209 handle->postdata[handle->postlen] = 0;
6211 memcpy(handle->postdata + handle->postlen + 1, query_string, lq);
6214 // POST: we sign postdata \0 query string
6216 handle->sigdata = (char *)Z_Malloc(8192);
6217 strlcpy(handle->sigdata, "X-D0-Blind-ID-Detached-Signature: ", 8192);
6218 l = strlen(handle->sigdata);
6219 handle->siglen = Crypto_SignDataDetached(handle->postdata, handle->postlen + 1 + lq, postkeyid, handle->sigdata + l, 8192 - l);
6222 Z_Free(handle->sigdata);
6223 handle->sigdata = NULL;
6226 ll = base64_encode((unsigned char *) (handle->sigdata + l), handle->siglen, 8192 - l - 1);
6229 Z_Free(handle->sigdata);
6230 handle->sigdata = NULL;
6233 handle->siglen = l + ll;
6234 handle->sigdata[handle->siglen] = 0;
6237 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);
6241 if(postkeyid >= 0 && query_string)
6243 // GET: we sign JUST the query string
6245 handle->sigdata = (char *)Z_Malloc(8192);
6246 strlcpy(handle->sigdata, "X-D0-Blind-ID-Detached-Signature: ", 8192);
6247 l = strlen(handle->sigdata);
6248 handle->siglen = Crypto_SignDataDetached(query_string, lq, postkeyid, handle->sigdata + l, 8192 - l);
6251 Z_Free(handle->sigdata);
6252 handle->sigdata = NULL;
6255 ll = base64_encode((unsigned char *) (handle->sigdata + l), handle->siglen, 8192 - l - 1);
6258 Z_Free(handle->sigdata);
6259 handle->sigdata = NULL;
6262 handle->siglen = l + ll;
6263 handle->sigdata[handle->siglen] = 0;
6266 handle->postdata = NULL;
6267 handle->postlen = 0;
6268 ret = Curl_Begin_ToMemory_POST(url, handle->sigdata, 0, NULL, NULL, 0, (unsigned char *) handle->buffer, sizeof(handle->buffer), uri_to_string_callback, handle);
6272 PRVM_G_INT(OFS_RETURN) = 1;
6276 if(handle->postdata)
6277 Z_Free(handle->postdata);
6279 Z_Free(handle->sigdata);
6281 PRVM_G_INT(OFS_RETURN) = 0;
6285 void VM_netaddress_resolve (prvm_prog_t *prog)
6288 char normalized[128];
6290 lhnetaddress_t addr;
6292 VM_SAFEPARMCOUNTRANGE(1, 2, VM_netaddress_resolve);
6294 ip = PRVM_G_STRING(OFS_PARM0);
6297 port = (int) PRVM_G_FLOAT(OFS_PARM1);
6299 if(LHNETADDRESS_FromString(&addr, ip, port) && LHNETADDRESS_ToString(&addr, normalized, sizeof(normalized), prog->argc > 1))
6300 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, normalized);
6302 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, "");
6305 //string(prvm_prog_t *prog) getextresponse = #624; // returns the next extResponse packet that was sent to this client
6306 void VM_CL_getextresponse (prvm_prog_t *prog)
6308 VM_SAFEPARMCOUNT(0,VM_argv);
6310 if (cl_net_extresponse_count <= 0)
6311 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
6315 --cl_net_extresponse_count;
6316 first = (cl_net_extresponse_last + NET_EXTRESPONSE_MAX - cl_net_extresponse_count) % NET_EXTRESPONSE_MAX;
6317 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, cl_net_extresponse[first]);
6321 void VM_SV_getextresponse (prvm_prog_t *prog)
6323 VM_SAFEPARMCOUNT(0,VM_argv);
6325 if (sv_net_extresponse_count <= 0)
6326 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
6330 --sv_net_extresponse_count;
6331 first = (sv_net_extresponse_last + NET_EXTRESPONSE_MAX - sv_net_extresponse_count) % NET_EXTRESPONSE_MAX;
6332 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, sv_net_extresponse[first]);
6338 Common functions between menu.dat and clsprogs
6342 //#349 float() isdemo
6343 void VM_CL_isdemo (prvm_prog_t *prog)
6345 VM_SAFEPARMCOUNT(0, VM_CL_isdemo);
6346 PRVM_G_FLOAT(OFS_RETURN) = cls.demoplayback;
6349 //#355 float() videoplaying
6350 void VM_CL_videoplaying (prvm_prog_t *prog)
6352 VM_SAFEPARMCOUNT(0, VM_CL_videoplaying);
6353 PRVM_G_FLOAT(OFS_RETURN) = cl_videoplaying;
6360 callfunction(...,string function_name)
6364 void VM_callfunction(prvm_prog_t *prog)
6369 VM_SAFEPARMCOUNTRANGE(1, 8, VM_callfunction);
6371 s = PRVM_G_STRING(OFS_PARM0+(prog->argc - 1)*3);
6373 VM_CheckEmptyString(prog, s);
6375 func = PRVM_ED_FindFunction(prog, s);
6378 prog->error_cmd("VM_callfunciton: function %s not found !", s);
6379 else if (func->first_statement < 0)
6381 // negative statements are built in functions
6382 int builtinnumber = -func->first_statement;
6383 prog->xfunction->builtinsprofile++;
6384 if (builtinnumber < prog->numbuiltins && prog->builtins[builtinnumber])
6385 prog->builtins[builtinnumber](prog);
6387 prog->error_cmd("No such builtin #%i in %s; most likely cause: outdated engine build. Try updating!", builtinnumber, prog->name);
6389 else if(func - prog->functions > 0)
6392 prog->ExecuteProgram(prog, func - prog->functions,"");
6401 float isfunction(string function_name)
6404 void VM_isfunction(prvm_prog_t *prog)
6409 VM_SAFEPARMCOUNT(1, VM_isfunction);
6411 s = PRVM_G_STRING(OFS_PARM0);
6413 VM_CheckEmptyString(prog, s);
6415 func = PRVM_ED_FindFunction(prog, s);
6418 PRVM_G_FLOAT(OFS_RETURN) = false;
6420 PRVM_G_FLOAT(OFS_RETURN) = true;
6427 string sprintf(string format, ...)
6431 void VM_sprintf(prvm_prog_t *prog)
6434 char outbuf[MAX_INPUTLINE];
6435 char *o = outbuf, *end = outbuf + sizeof(outbuf), *err;
6437 int width, precision, thisarg, flags;
6441 static prvm_int_t dummyivec[3] = {0, 0, 0};
6442 static prvm_vec_t dummyvec[3] = {0, 0, 0};
6445 #define PRINTF_ALTERNATE 1
6446 #define PRINTF_ZEROPAD 2
6447 #define PRINTF_LEFT 4
6448 #define PRINTF_SPACEPOSITIVE 8
6449 #define PRINTF_SIGNPOSITIVE 16
6453 s = PRVM_G_STRING(OFS_PARM0);
6455 #define GETARG_FLOAT(a) (((a)>=1 && (a)<prog->argc) ? (PRVM_G_FLOAT(OFS_PARM0 + 3 * (a))) : 0)
6456 #define GETARG_VECTOR(a) (((a)>=1 && (a)<prog->argc) ? (PRVM_G_VECTOR(OFS_PARM0 + 3 * (a))) : dummyvec)
6457 #define GETARG_INT(a) (((a)>=1 && (a)<prog->argc) ? (PRVM_G_INT(OFS_PARM0 + 3 * (a))) : 0)
6458 #define GETARG_INTVECTOR(a) (((a)>=1 && (a)<prog->argc) ? ((prvm_int_t*) PRVM_G_VECTOR(OFS_PARM0 + 3 * (a))) : dummyivec)
6459 #define GETARG_STRING(a) (((a)>=1 && (a)<prog->argc) ? (PRVM_G_STRING(OFS_PARM0 + 3 * (a))) : "")
6474 // complete directive format:
6483 // is number following?
6484 if(*s >= '0' && *s <= '9')
6486 width = strtol(s, &err, 10);
6489 VM_Warning(prog, "VM_sprintf: invalid directive in %s: %s\n", prog->name, s0);
6502 flags |= PRINTF_ZEROPAD;
6504 width = -1; // it was just a flag
6516 case '#': flags |= PRINTF_ALTERNATE; break;
6517 case '0': flags |= PRINTF_ZEROPAD; break;
6518 case '-': flags |= PRINTF_LEFT; break;
6519 case ' ': flags |= PRINTF_SPACEPOSITIVE; break;
6520 case '+': flags |= PRINTF_SIGNPOSITIVE; break;
6530 if(*s >= '0' && *s <= '9')
6532 width = strtol(s, &err, 10);
6533 if(!err || *err != '$')
6535 VM_Warning(prog, "VM_sprintf: invalid directive in %s: %s\n", prog->name, s0);
6542 width = GETARG_FLOAT(width);
6545 flags |= PRINTF_LEFT;
6549 else if(*s >= '0' && *s <= '9')
6551 width = strtol(s, &err, 10);
6554 VM_Warning(prog, "VM_sprintf: invalid directive in %s: %s\n", prog->name, s0);
6560 flags |= PRINTF_LEFT;
6564 // otherwise width stays -1
6573 if(*s >= '0' && *s <= '9')
6575 precision = strtol(s, &err, 10);
6576 if(!err || *err != '$')
6578 VM_Warning(prog, "VM_sprintf: invalid directive in %s: %s\n", prog->name, s0);
6584 precision = argpos++;
6585 precision = GETARG_FLOAT(precision);
6587 else if(*s >= '0' && *s <= '9')
6589 precision = strtol(s, &err, 10);
6592 VM_Warning(prog, "VM_sprintf: invalid directive in %s: %s\n", prog->name, s0);
6599 VM_Warning(prog, "VM_sprintf: invalid directive in %s: %s\n", prog->name, s0);
6608 case 'h': isfloat = 1; break;
6609 case 'l': isfloat = 0; break;
6610 case 'L': isfloat = 0; break;
6621 // now s points to the final directive char and is no longer changed
6636 if(*s != 's' && *s != 'c')
6637 if(flags & PRINTF_ALTERNATE) *f++ = '#';
6638 if(flags & PRINTF_ZEROPAD) *f++ = '0';
6639 if(flags & PRINTF_LEFT) *f++ = '-';
6640 if(flags & PRINTF_SPACEPOSITIVE) *f++ = ' ';
6641 if(flags & PRINTF_SIGNPOSITIVE) *f++ = '+';
6651 if(width < 0) // not set
6657 if(precision < 0) // not set
6658 o += dpsnprintf(o, end - o, formatbuf, width, (isfloat ? (int) GETARG_FLOAT(thisarg) : (int) GETARG_INT(thisarg)));
6660 o += dpsnprintf(o, end - o, formatbuf, width, precision, (isfloat ? (int) GETARG_FLOAT(thisarg) : (int) GETARG_INT(thisarg)));
6662 case 'o': case 'u': case 'x': case 'X':
6663 if(precision < 0) // not set
6664 o += dpsnprintf(o, end - o, formatbuf, width, (isfloat ? (unsigned int) GETARG_FLOAT(thisarg) : (unsigned int) GETARG_INT(thisarg)));
6666 o += dpsnprintf(o, end - o, formatbuf, width, precision, (isfloat ? (unsigned int) GETARG_FLOAT(thisarg) : (unsigned int) GETARG_INT(thisarg)));
6668 case 'e': case 'E': case 'f': case 'F': case 'g': case 'G':
6669 if(precision < 0) // not set
6670 o += dpsnprintf(o, end - o, formatbuf, width, (isfloat ? (double) GETARG_FLOAT(thisarg) : (double) GETARG_INT(thisarg)));
6672 o += dpsnprintf(o, end - o, formatbuf, width, precision, (isfloat ? (double) GETARG_FLOAT(thisarg) : (double) GETARG_INT(thisarg)));
6676 if(precision < 0) // not set
6677 o += dpsnprintf(o, end - o, va(vabuf, sizeof(vabuf), "%s %s %s", /* NESTED SPRINTF IS NESTED */ formatbuf, formatbuf, formatbuf),
6678 width, (isfloat ? (double) GETARG_VECTOR(thisarg)[0] : (double) GETARG_INTVECTOR(thisarg)[0]),
6679 width, (isfloat ? (double) GETARG_VECTOR(thisarg)[1] : (double) GETARG_INTVECTOR(thisarg)[1]),
6680 width, (isfloat ? (double) GETARG_VECTOR(thisarg)[2] : (double) GETARG_INTVECTOR(thisarg)[2])
6683 o += dpsnprintf(o, end - o, va(vabuf, sizeof(vabuf), "%s %s %s", /* NESTED SPRINTF IS NESTED */ formatbuf, formatbuf, formatbuf),
6684 width, precision, (isfloat ? (double) GETARG_VECTOR(thisarg)[0] : (double) GETARG_INTVECTOR(thisarg)[0]),
6685 width, precision, (isfloat ? (double) GETARG_VECTOR(thisarg)[1] : (double) GETARG_INTVECTOR(thisarg)[1]),
6686 width, precision, (isfloat ? (double) GETARG_VECTOR(thisarg)[2] : (double) GETARG_INTVECTOR(thisarg)[2])
6690 if(flags & PRINTF_ALTERNATE)
6692 if(precision < 0) // not set
6693 o += dpsnprintf(o, end - o, formatbuf, width, (isfloat ? (unsigned int) GETARG_FLOAT(thisarg) : (unsigned int) GETARG_INT(thisarg)));
6695 o += dpsnprintf(o, end - o, formatbuf, width, precision, (isfloat ? (unsigned int) GETARG_FLOAT(thisarg) : (unsigned int) GETARG_INT(thisarg)));
6699 unsigned int c = (isfloat ? (unsigned int) GETARG_FLOAT(thisarg) : (unsigned int) GETARG_INT(thisarg));
6701 const char *buf = u8_encodech(c, NULL, charbuf16);
6704 if(precision < 0) // not set
6705 precision = end - o - 1;
6706 o += u8_strpad(o, end - o, buf, (flags & PRINTF_LEFT) != 0, width, precision);
6710 if(flags & PRINTF_ALTERNATE)
6712 if(precision < 0) // not set
6713 o += dpsnprintf(o, end - o, formatbuf, width, GETARG_STRING(thisarg));
6715 o += dpsnprintf(o, end - o, formatbuf, width, precision, GETARG_STRING(thisarg));
6719 if(precision < 0) // not set
6720 precision = end - o - 1;
6721 if(flags & PRINTF_SIGNPOSITIVE)
6722 o += u8_strpad(o, end - o, GETARG_STRING(thisarg), (flags & PRINTF_LEFT) != 0, width, precision);
6724 o += u8_strpad_colorcodes(o, end - o, GETARG_STRING(thisarg), (flags & PRINTF_LEFT) != 0, width, precision);
6728 VM_Warning(prog, "VM_sprintf: invalid directive in %s: %s\n", prog->name, s0);
6743 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, outbuf);
6749 static dp_model_t *getmodel(prvm_prog_t *prog, prvm_edict_t *ed)
6751 if (prog == SVVM_prog)
6752 return SV_GetModelFromEdict(ed);
6753 else if (prog == CLVM_prog)
6754 return CL_GetModelFromEdict(ed);
6761 unsigned int progid;
6763 frameblend_t frameblend[MAX_FRAMEBLENDS];
6764 skeleton_t *skeleton_p;
6765 skeleton_t skeleton;
6766 float *data_vertex3f;
6767 float *data_svector3f;
6768 float *data_tvector3f;
6769 float *data_normal3f;
6771 float *buf_vertex3f;
6772 float *buf_svector3f;
6773 float *buf_tvector3f;
6774 float *buf_normal3f;
6776 animatemodel_cache_t;
6777 static animatemodel_cache_t animatemodel_cache;
6779 static void animatemodel(prvm_prog_t *prog, dp_model_t *model, prvm_edict_t *ed)
6781 skeleton_t *skeleton;
6782 int skeletonindex = -1;
6783 qboolean need = false;
6784 if(!(model->surfmesh.isanimated && model->AnimateVertices))
6786 animatemodel_cache.data_vertex3f = model->surfmesh.data_vertex3f;
6787 animatemodel_cache.data_svector3f = model->surfmesh.data_svector3f;
6788 animatemodel_cache.data_tvector3f = model->surfmesh.data_tvector3f;
6789 animatemodel_cache.data_normal3f = model->surfmesh.data_normal3f;
6792 if(animatemodel_cache.progid != prog->id)
6793 memset(&animatemodel_cache, 0, sizeof(animatemodel_cache));
6794 need |= (animatemodel_cache.model != model);
6795 VM_GenerateFrameGroupBlend(prog, ed->priv.server->framegroupblend, ed);
6796 VM_FrameBlendFromFrameGroupBlend(ed->priv.server->frameblend, ed->priv.server->framegroupblend, model, PRVM_serverglobalfloat(time));
6797 need |= (memcmp(&animatemodel_cache.frameblend, &ed->priv.server->frameblend, sizeof(ed->priv.server->frameblend))) != 0;
6798 skeletonindex = (int)PRVM_gameedictfloat(ed, skeletonindex) - 1;
6799 if (!(skeletonindex >= 0 && skeletonindex < MAX_EDICTS && (skeleton = prog->skeletons[skeletonindex]) && skeleton->model->num_bones == ed->priv.server->skeleton.model->num_bones))
6801 need |= (animatemodel_cache.skeleton_p != skeleton);
6803 need |= (memcmp(&animatemodel_cache.skeleton, skeleton, sizeof(ed->priv.server->skeleton))) != 0;
6806 if(model->surfmesh.num_vertices > animatemodel_cache.max_vertices)
6808 animatemodel_cache.max_vertices = model->surfmesh.num_vertices * 2;
6809 if(animatemodel_cache.buf_vertex3f) Mem_Free(animatemodel_cache.buf_vertex3f);
6810 if(animatemodel_cache.buf_svector3f) Mem_Free(animatemodel_cache.buf_svector3f);
6811 if(animatemodel_cache.buf_tvector3f) Mem_Free(animatemodel_cache.buf_tvector3f);
6812 if(animatemodel_cache.buf_normal3f) Mem_Free(animatemodel_cache.buf_normal3f);
6813 animatemodel_cache.buf_vertex3f = (float *)Mem_Alloc(prog->progs_mempool, sizeof(float[3]) * animatemodel_cache.max_vertices);
6814 animatemodel_cache.buf_svector3f = (float *)Mem_Alloc(prog->progs_mempool, sizeof(float[3]) * animatemodel_cache.max_vertices);
6815 animatemodel_cache.buf_tvector3f = (float *)Mem_Alloc(prog->progs_mempool, sizeof(float[3]) * animatemodel_cache.max_vertices);
6816 animatemodel_cache.buf_normal3f = (float *)Mem_Alloc(prog->progs_mempool, sizeof(float[3]) * animatemodel_cache.max_vertices);
6818 animatemodel_cache.data_vertex3f = animatemodel_cache.buf_vertex3f;
6819 animatemodel_cache.data_svector3f = animatemodel_cache.buf_svector3f;
6820 animatemodel_cache.data_tvector3f = animatemodel_cache.buf_tvector3f;
6821 animatemodel_cache.data_normal3f = animatemodel_cache.buf_normal3f;
6822 VM_UpdateEdictSkeleton(prog, ed, model, ed->priv.server->frameblend);
6823 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);
6824 animatemodel_cache.progid = prog->id;
6825 animatemodel_cache.model = model;
6826 memcpy(&animatemodel_cache.frameblend, &ed->priv.server->frameblend, sizeof(ed->priv.server->frameblend));
6827 animatemodel_cache.skeleton_p = skeleton;
6829 memcpy(&animatemodel_cache.skeleton, skeleton, sizeof(ed->priv.server->skeleton));
6832 static void getmatrix(prvm_prog_t *prog, prvm_edict_t *ed, matrix4x4_t *out)
6834 if (prog == SVVM_prog)
6835 SV_GetEntityMatrix(prog, ed, out, false);
6836 else if (prog == CLVM_prog)
6837 CL_GetEntityMatrix(prog, ed, out, false);
6839 *out = identitymatrix;
6842 static void applytransform_forward(prvm_prog_t *prog, const vec3_t in, prvm_edict_t *ed, vec3_t out)
6845 getmatrix(prog, ed, &m);
6846 Matrix4x4_Transform(&m, in, out);
6849 static void applytransform_forward_direction(prvm_prog_t *prog, const vec3_t in, prvm_edict_t *ed, vec3_t out)
6852 getmatrix(prog, ed, &m);
6853 Matrix4x4_Transform3x3(&m, in, out);
6856 static void applytransform_inverted(prvm_prog_t *prog, const vec3_t in, prvm_edict_t *ed, vec3_t out)
6859 getmatrix(prog, ed, &m);
6860 Matrix4x4_Invert_Full(&n, &m);
6861 Matrix4x4_Transform3x3(&n, in, out);
6864 static void applytransform_forward_normal(prvm_prog_t *prog, const vec3_t in, prvm_edict_t *ed, vec3_t out)
6868 getmatrix(prog, ed, &m);
6869 Matrix4x4_TransformPositivePlane(&m, in[0], in[1], in[2], 0, p);
6873 static void clippointtosurface(prvm_prog_t *prog, prvm_edict_t *ed, dp_model_t *model, msurface_t *surface, vec3_t p, vec3_t out)
6876 float *v[3], facenormal[3], edgenormal[3], sidenormal[3], temp[3], offsetdist, dist, bestdist;
6878 animatemodel(prog, model, ed);
6879 bestdist = 1000000000;
6881 for (i = 0, e = (model->surfmesh.data_element3i + 3 * surface->num_firsttriangle);i < surface->num_triangles;i++, e += 3)
6883 // clip original point to each triangle of the surface and find the
6884 // triangle that is closest
6885 v[0] = animatemodel_cache.data_vertex3f + e[0] * 3;
6886 v[1] = animatemodel_cache.data_vertex3f + e[1] * 3;
6887 v[2] = animatemodel_cache.data_vertex3f + e[2] * 3;
6888 TriangleNormal(v[0], v[1], v[2], facenormal);
6889 VectorNormalize(facenormal);
6890 offsetdist = DotProduct(v[0], facenormal) - DotProduct(p, facenormal);
6891 VectorMA(p, offsetdist, facenormal, temp);
6892 for (j = 0, k = 2;j < 3;k = j, j++)
6894 VectorSubtract(v[k], v[j], edgenormal);
6895 CrossProduct(edgenormal, facenormal, sidenormal);
6896 VectorNormalize(sidenormal);
6897 offsetdist = DotProduct(v[k], sidenormal) - DotProduct(temp, sidenormal);
6899 VectorMA(temp, offsetdist, sidenormal, temp);
6901 dist = VectorDistance2(temp, p);
6902 if (bestdist > dist)
6905 VectorCopy(temp, out);
6910 static msurface_t *getsurface(dp_model_t *model, int surfacenum)
6912 if (surfacenum < 0 || surfacenum >= model->nummodelsurfaces)
6914 return model->data_surfaces + surfacenum + model->firstmodelsurface;
6918 //PF_getsurfacenumpoints, // #434 float(entity e, float s) getsurfacenumpoints = #434;
6919 void VM_getsurfacenumpoints(prvm_prog_t *prog)
6922 msurface_t *surface;
6923 VM_SAFEPARMCOUNT(2, VM_getsurfacenumpoints);
6924 // return 0 if no such surface
6925 if (!(model = getmodel(prog, PRVM_G_EDICT(OFS_PARM0))) || !(surface = getsurface(model, (int)PRVM_G_FLOAT(OFS_PARM1))))
6927 PRVM_G_FLOAT(OFS_RETURN) = 0;
6931 // note: this (incorrectly) assumes it is a simple polygon
6932 PRVM_G_FLOAT(OFS_RETURN) = surface->num_vertices;
6934 //PF_getsurfacepoint, // #435 vector(entity e, float s, float n) getsurfacepoint = #435;
6935 void VM_getsurfacepoint(prvm_prog_t *prog)
6939 msurface_t *surface;
6942 VM_SAFEPARMCOUNT(3, VM_getsurfacepoint);
6943 VectorClear(PRVM_G_VECTOR(OFS_RETURN));
6944 ed = PRVM_G_EDICT(OFS_PARM0);
6945 if (!(model = getmodel(prog, ed)) || !(surface = getsurface(model, (int)PRVM_G_FLOAT(OFS_PARM1))))
6947 // note: this (incorrectly) assumes it is a simple polygon
6948 pointnum = (int)PRVM_G_FLOAT(OFS_PARM2);
6949 if (pointnum < 0 || pointnum >= surface->num_vertices)
6951 animatemodel(prog, model, ed);
6952 applytransform_forward(prog, &(animatemodel_cache.data_vertex3f + 3 * surface->num_firstvertex)[pointnum * 3], ed, result);
6953 VectorCopy(result, PRVM_G_VECTOR(OFS_RETURN));
6955 //PF_getsurfacepointattribute, // #486 vector(entity e, float s, float n, float a) getsurfacepointattribute = #486;
6956 // float SPA_POSITION = 0;
6957 // float SPA_S_AXIS = 1;
6958 // float SPA_T_AXIS = 2;
6959 // float SPA_R_AXIS = 3; // same as SPA_NORMAL
6960 // float SPA_TEXCOORDS0 = 4;
6961 // float SPA_LIGHTMAP0_TEXCOORDS = 5;
6962 // float SPA_LIGHTMAP0_COLOR = 6;
6963 void VM_getsurfacepointattribute(prvm_prog_t *prog)
6967 msurface_t *surface;
6972 VM_SAFEPARMCOUNT(4, VM_getsurfacepoint);
6973 VectorClear(PRVM_G_VECTOR(OFS_RETURN));
6974 ed = PRVM_G_EDICT(OFS_PARM0);
6975 if (!(model = getmodel(prog, ed)) || !(surface = getsurface(model, (int)PRVM_G_FLOAT(OFS_PARM1))))
6977 pointnum = (int)PRVM_G_FLOAT(OFS_PARM2);
6978 if (pointnum < 0 || pointnum >= surface->num_vertices)
6980 attributetype = (int) PRVM_G_FLOAT(OFS_PARM3);
6982 animatemodel(prog, model, ed);
6984 switch( attributetype ) {
6985 // float SPA_POSITION = 0;
6987 applytransform_forward(prog, &(animatemodel_cache.data_vertex3f + 3 * surface->num_firstvertex)[pointnum * 3], ed, result);
6988 VectorCopy(result, PRVM_G_VECTOR(OFS_RETURN));
6990 // float SPA_S_AXIS = 1;
6992 applytransform_forward_direction(prog, &(animatemodel_cache.data_svector3f + 3 * surface->num_firstvertex)[pointnum * 3], ed, result);
6993 VectorCopy(result, PRVM_G_VECTOR(OFS_RETURN));
6995 // float SPA_T_AXIS = 2;
6997 applytransform_forward_direction(prog, &(animatemodel_cache.data_tvector3f + 3 * surface->num_firstvertex)[pointnum * 3], ed, result);
6998 VectorCopy(result, PRVM_G_VECTOR(OFS_RETURN));
7000 // float SPA_R_AXIS = 3; // same as SPA_NORMAL
7002 applytransform_forward_direction(prog, &(animatemodel_cache.data_normal3f + 3 * surface->num_firstvertex)[pointnum * 3], ed, result);
7003 VectorCopy(result, PRVM_G_VECTOR(OFS_RETURN));
7005 // float SPA_TEXCOORDS0 = 4;
7007 float *texcoord = &(model->surfmesh.data_texcoordtexture2f + 2 * surface->num_firstvertex)[pointnum * 2];
7008 result[0] = texcoord[0];
7009 result[1] = texcoord[1];
7011 VectorCopy(result, PRVM_G_VECTOR(OFS_RETURN));
7014 // float SPA_LIGHTMAP0_TEXCOORDS = 5;
7016 float *texcoord = &(model->surfmesh.data_texcoordlightmap2f + 2 * surface->num_firstvertex)[pointnum * 2];
7017 result[0] = texcoord[0];
7018 result[1] = texcoord[1];
7020 VectorCopy(result, PRVM_G_VECTOR(OFS_RETURN));
7023 // float SPA_LIGHTMAP0_COLOR = 6;
7025 // ignore alpha for now..
7026 VectorCopy( &(model->surfmesh.data_lightmapcolor4f + 4 * surface->num_firstvertex)[pointnum * 4], PRVM_G_VECTOR(OFS_RETURN));
7029 VectorSet( PRVM_G_VECTOR(OFS_RETURN), 0.0f, 0.0f, 0.0f );
7033 //PF_getsurfacenormal, // #436 vector(entity e, float s) getsurfacenormal = #436;
7034 void VM_getsurfacenormal(prvm_prog_t *prog)
7037 msurface_t *surface;
7040 VM_SAFEPARMCOUNT(2, VM_getsurfacenormal);
7041 VectorClear(PRVM_G_VECTOR(OFS_RETURN));
7042 if (!(model = getmodel(prog, PRVM_G_EDICT(OFS_PARM0))) || !(surface = getsurface(model, (int)PRVM_G_FLOAT(OFS_PARM1))))
7044 // note: this only returns the first triangle, so it doesn't work very
7045 // well for curved surfaces or arbitrary meshes
7046 animatemodel(prog, model, PRVM_G_EDICT(OFS_PARM0));
7047 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);
7048 applytransform_forward_normal(prog, normal, PRVM_G_EDICT(OFS_PARM0), result);
7049 VectorNormalize(result);
7050 VectorCopy(result, PRVM_G_VECTOR(OFS_RETURN));
7052 //PF_getsurfacetexture, // #437 string(entity e, float s) getsurfacetexture = #437;
7053 void VM_getsurfacetexture(prvm_prog_t *prog)
7056 msurface_t *surface;
7057 VM_SAFEPARMCOUNT(2, VM_getsurfacetexture);
7058 PRVM_G_INT(OFS_RETURN) = OFS_NULL;
7059 if (!(model = getmodel(prog, PRVM_G_EDICT(OFS_PARM0))) || !(surface = getsurface(model, (int)PRVM_G_FLOAT(OFS_PARM1))))
7061 PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, surface->texture->name);
7063 //PF_getsurfacenearpoint, // #438 float(entity e, vector p) getsurfacenearpoint = #438;
7064 void VM_getsurfacenearpoint(prvm_prog_t *prog)
7066 int surfacenum, best;
7068 vec_t dist, bestdist;
7071 msurface_t *surface;
7073 VM_SAFEPARMCOUNT(2, VM_getsurfacenearpoint);
7074 PRVM_G_FLOAT(OFS_RETURN) = -1;
7075 ed = PRVM_G_EDICT(OFS_PARM0);
7076 VectorCopy(PRVM_G_VECTOR(OFS_PARM1), point);
7078 if (!ed || ed->priv.server->free)
7080 model = getmodel(prog, ed);
7081 if (!model || !model->num_surfaces)
7084 animatemodel(prog, model, ed);
7086 applytransform_inverted(prog, point, ed, p);
7088 bestdist = 1000000000;
7089 for (surfacenum = 0;surfacenum < model->nummodelsurfaces;surfacenum++)
7091 surface = model->data_surfaces + surfacenum + model->firstmodelsurface;
7092 // first see if the nearest point on the surface's box is closer than the previous match
7093 clipped[0] = bound(surface->mins[0], p[0], surface->maxs[0]) - p[0];
7094 clipped[1] = bound(surface->mins[1], p[1], surface->maxs[1]) - p[1];
7095 clipped[2] = bound(surface->mins[2], p[2], surface->maxs[2]) - p[2];
7096 dist = VectorLength2(clipped);
7097 if (dist < bestdist)
7099 // it is, check the nearest point on the actual geometry
7100 clippointtosurface(prog, ed, model, surface, p, clipped);
7101 VectorSubtract(clipped, p, clipped);
7102 dist += VectorLength2(clipped);
7103 if (dist < bestdist)
7105 // that's closer too, store it as the best match
7111 PRVM_G_FLOAT(OFS_RETURN) = best;
7113 //PF_getsurfaceclippedpoint, // #439 vector(entity e, float s, vector p) getsurfaceclippedpoint = #439;
7114 void VM_getsurfaceclippedpoint(prvm_prog_t *prog)
7118 msurface_t *surface;
7120 VM_SAFEPARMCOUNT(3, VM_te_getsurfaceclippedpoint);
7121 VectorClear(PRVM_G_VECTOR(OFS_RETURN));
7122 ed = PRVM_G_EDICT(OFS_PARM0);
7123 if (!(model = getmodel(prog, ed)) || !(surface = getsurface(model, (int)PRVM_G_FLOAT(OFS_PARM1))))
7125 animatemodel(prog, model, ed);
7126 VectorCopy(PRVM_G_VECTOR(OFS_PARM2), inp);
7127 applytransform_inverted(prog, inp, ed, p);
7128 clippointtosurface(prog, ed, model, surface, p, out);
7129 VectorAdd(out, PRVM_serveredictvector(ed, origin), PRVM_G_VECTOR(OFS_RETURN));
7132 //PF_getsurfacenumtriangles, // #??? float(entity e, float s) getsurfacenumtriangles = #???;
7133 void VM_getsurfacenumtriangles(prvm_prog_t *prog)
7136 msurface_t *surface;
7137 VM_SAFEPARMCOUNT(2, VM_SV_getsurfacenumtriangles);
7138 // return 0 if no such surface
7139 if (!(model = getmodel(prog, PRVM_G_EDICT(OFS_PARM0))) || !(surface = getsurface(model, (int)PRVM_G_FLOAT(OFS_PARM1))))
7141 PRVM_G_FLOAT(OFS_RETURN) = 0;
7145 PRVM_G_FLOAT(OFS_RETURN) = surface->num_triangles;
7147 //PF_getsurfacetriangle, // #??? vector(entity e, float s, float n) getsurfacetriangle = #???;
7148 void VM_getsurfacetriangle(prvm_prog_t *prog)
7150 const vec3_t d = {-1, -1, -1};
7153 msurface_t *surface;
7155 VM_SAFEPARMCOUNT(3, VM_SV_getsurfacetriangle);
7156 VectorClear(PRVM_G_VECTOR(OFS_RETURN));
7157 ed = PRVM_G_EDICT(OFS_PARM0);
7158 if (!(model = getmodel(prog, ed)) || !(surface = getsurface(model, (int)PRVM_G_FLOAT(OFS_PARM1))))
7160 trinum = (int)PRVM_G_FLOAT(OFS_PARM2);
7161 if (trinum < 0 || trinum >= surface->num_triangles)
7163 // FIXME: implement rotation/scaling
7164 VectorMA(&(model->surfmesh.data_element3i + 3 * surface->num_firsttriangle)[trinum * 3], surface->num_firstvertex, d, PRVM_G_VECTOR(OFS_RETURN));
7171 #define VM_physics_ApplyCmd(ed,f) if (!ed->priv.server->ode_body) VM_physics_newstackfunction(prog, ed, f); else World_Physics_ApplyCmd(ed, f)
7173 static edict_odefunc_t *VM_physics_newstackfunction(prvm_prog_t *prog, prvm_edict_t *ed, edict_odefunc_t *f)
7175 edict_odefunc_t *newfunc, *func;
7177 newfunc = (edict_odefunc_t *)Mem_Alloc(prog->progs_mempool, sizeof(edict_odefunc_t));
7178 memcpy(newfunc, f, sizeof(edict_odefunc_t));
7179 newfunc->next = NULL;
7180 if (!ed->priv.server->ode_func)
7181 ed->priv.server->ode_func = newfunc;
7184 for (func = ed->priv.server->ode_func; func->next; func = func->next);
7185 func->next = newfunc;
7190 // void(entity e, float physics_enabled) physics_enable = #;
7191 void VM_physics_enable(prvm_prog_t *prog)
7196 VM_SAFEPARMCOUNT(2, VM_physics_enable);
7197 ed = PRVM_G_EDICT(OFS_PARM0);
7200 if (developer.integer > 0)
7201 VM_Warning(prog, "VM_physics_enable: null entity!\n");
7204 // entity should have MOVETYPE_PHYSICS already set, this can damage memory (making leaked allocation) so warn about this even if non-developer
7205 if (PRVM_serveredictfloat(ed, movetype) != MOVETYPE_PHYSICS)
7207 VM_Warning(prog, "VM_physics_enable: entity is not MOVETYPE_PHYSICS!\n");
7210 f.type = PRVM_G_FLOAT(OFS_PARM1) == 0 ? ODEFUNC_DISABLE : ODEFUNC_ENABLE;
7211 VM_physics_ApplyCmd(ed, &f);
7214 // void(entity e, vector force, vector relative_ofs) physics_addforce = #;
7215 void VM_physics_addforce(prvm_prog_t *prog)
7220 VM_SAFEPARMCOUNT(3, VM_physics_addforce);
7221 ed = PRVM_G_EDICT(OFS_PARM0);
7224 if (developer.integer > 0)
7225 VM_Warning(prog, "VM_physics_addforce: null entity!\n");
7228 // entity should have MOVETYPE_PHYSICS already set, this can damage memory (making leaked allocation) so warn about this even if non-developer
7229 if (PRVM_serveredictfloat(ed, movetype) != MOVETYPE_PHYSICS)
7231 VM_Warning(prog, "VM_physics_addforce: entity is not MOVETYPE_PHYSICS!\n");
7234 f.type = ODEFUNC_FORCE;
7235 VectorCopy(PRVM_G_VECTOR(OFS_PARM1), f.v1);
7236 VectorCopy(PRVM_G_VECTOR(OFS_PARM2), f.v2);
7237 VM_physics_ApplyCmd(ed, &f);
7240 // void(entity e, vector torque) physics_addtorque = #;
7241 void VM_physics_addtorque(prvm_prog_t *prog)
7246 VM_SAFEPARMCOUNT(2, VM_physics_addtorque);
7247 ed = PRVM_G_EDICT(OFS_PARM0);
7250 if (developer.integer > 0)
7251 VM_Warning(prog, "VM_physics_addtorque: null entity!\n");
7254 // entity should have MOVETYPE_PHYSICS already set, this can damage memory (making leaked allocation) so warn about this even if non-developer
7255 if (PRVM_serveredictfloat(ed, movetype) != MOVETYPE_PHYSICS)
7257 VM_Warning(prog, "VM_physics_addtorque: entity is not MOVETYPE_PHYSICS!\n");
7260 f.type = ODEFUNC_TORQUE;
7261 VectorCopy(PRVM_G_VECTOR(OFS_PARM1), f.v1);
7262 VM_physics_ApplyCmd(ed, &f);